DBA Data[Home] [Help]

PACKAGE BODY: APPS.FII_AP_INV_SUM_INIT

Source


1 PACKAGE BODY FII_AP_INV_SUM_INIT AS
2 /* $Header: FIIAP18B.pls 120.13 2006/10/10 23:32:07 vkazhipu noship $ */
3 
4 g_errbuf                 VARCHAR2(2000) := NULL;
5 g_retcode                VARCHAR2(200)  := NULL;
6 g_exception_msg          VARCHAR2(4000) := NULL;
7 g_prim_currency          VARCHAR2(15)   := NULL;
8 g_sec_currency           VARCHAR2(15)   := NULL;
9 g_state                  VARCHAR2(200);
10 g_start_date             DATE;
11 g_end_date               DATE;
12 l_date_mask              VARCHAR2(240);
13 g_sysdate                DATE := TRUNC(SYSDATE);
14 g_seq_id                 NUMBER;
15 g_debug_flag             VARCHAR2(1) := NVL(FND_PROFILE.value('FII_DEBUG_MODE'), 'N');
16 g_oper_imp_prof_flag     VARCHAR2(1) := NVL(FND_PROFILE.value('FII_AP_DBI_IMP'), 'N');
17 g_exp_imp_prof_flag      VARCHAR2(1) := NVL(FND_PROFILE.value('FII_AP_DBI_EXP_IMP'), 'N');
18 g_manual_sources         VARCHAR2(2000) := FND_PROFILE.value('FII_AP_MANUAL_SOURCES');
19 ONE_SECOND               CONSTANT NUMBER := 0.000011574;  -- 1 second
20 INTERVAL_SIZE            NUMBER := 50000;
21 MAX_LOOP                 CONSTANT NUMBER := 60;
22 
23 g_prim_rate_type         VARCHAR2(30);
24 g_sec_rate_type          VARCHAR2(30);
25 g_primary_mau            NUMBER;
26 g_secondary_mau          NUMBER;
27 g_fii_user_id            NUMBER(15);
28 g_fii_login_id           NUMBER(15);
29 g_no_worker              NUMBER;
30 
31 g_fii_schema             VARCHAR2(30);
32 G_TABLE_NOT_EXIST        EXCEPTION;
33                          PRAGMA EXCEPTION_INIT(G_TABLE_NOT_EXIST, -942);
34 G_PROCEDURE_FAILURE      EXCEPTION;
35 G_NO_CHILD_PROCESS       EXCEPTION;
36 G_LOGIN_INFO_NOT_AVABLE  EXCEPTION;
37 G_IMP_NOT_SET            EXCEPTION;
38 G_MISSING_RATES          EXCEPTION;
39 G_MISS_GLOBAL_PARAMS     EXCEPTION;
40 G_NEED_SECONDARY_INFO    EXCEPTION;
41 G_INVALID_MANUAL_SOURCE  EXCEPTION;
42 
43 g_due_bucket1            NUMBER := 31;
44 g_due_bucket2            NUMBER := 30;
45 g_due_bucket3            NUMBER := 15;
46 
47 g_past_bucket1           NUMBER := 31;
48 g_past_bucket2           NUMBER := 30;
49 g_past_bucket3           NUMBER := 15;
50 
51 g_bucket_interval        NUMBER := 15;
52 g_no_buckets             NUMBER := 6;
53 
54 g_start_range            NUMBER;
55 g_end_range              NUMBER;
56 
57 g_timestamp1 DATE;
58 g_timestamp2 DATE;
59 g_timestamp3 DATE;
60 g_act_part1 NUMBER;
61 g_act_part2 NUMBER;
62 
63 g_ap_schema VARCHAR2(30) := 'AP';
64 -- ------------------------------------------------------------
65 -- Private Functions and Procedures
66 -- ------------------------------------------------------------
67 
68 ---------------------------------------------------
69 -- PROCEDURE TRUNCATE_TABLE
70 ---------------------------------------------------
71 
72 PROCEDURE Truncate_table (p_table_name VARCHAR2) IS
73     l_stmt VARCHAR2(100);
74 BEGIN
75     l_stmt := 'TRUNCATE table '||g_fii_schema||'.'||p_table_name;
76     if g_debug_flag = 'Y' then
77       FII_UTIL.put_line('g_fii_schema '||g_fii_schema);
78       FII_UTIL.put_line('');
79       FII_UTIL.put_line(l_stmt);
80     end if;
81     EXECUTE IMMEDIATE l_stmt;
82 
83 EXCEPTION
84     WHEN G_TABLE_NOT_EXIST THEN
85         null;      -- Oracle 942, table does not exist, no actions
86     WHEN OTHERS THEN
87         g_errbuf := 'Error in Procedure: TRUNCATE_TABLE  Message: '||sqlerrm;
88         RAISE;
89 END Truncate_Table;
90 
91 
92 -------------------------------------------------------------------
93 -- PROCEDURE Init
94 -- Purpose
95 -- This procedure INIT initialises the global variables.
96 -------------------------------------------------------------------
97 PROCEDURE Init is
98 
99   l_status              VARCHAR2(30);
100   l_industry            VARCHAR2(30);
101   l_global_param_list dbms_sql.varchar2_table;
102 
103 
104 BEGIN
105 
106   g_state := 'Initializing the global variables';
107 
108   -- --------------------------------------------------------
109   -- Find the schema owner
110   -- --------------------------------------------------------
111 
112   IF (FND_INSTALLATION.GET_APP_INFO('FII', l_status, l_industry, g_fii_schema)) THEN
113       NULL;
114       if g_debug_flag = 'Y' then
115          FII_UTIL.put_line('g_fii_schema is '||g_fii_schema);
116       end if;
117   END IF;
118 
119   -- --------------------------------------------------------
120   -- Find the schema owner (AP)
121   -- --------------------------------------------------------
122 
123   g_ap_schema := FII_UTIL.get_schema_name('SQLAP');
124   if g_debug_flag = 'Y' then
125      FII_UTIL.put_line('g_ap_schema is '||g_ap_schema);
126   end if;
127 
128 
129   if g_debug_flag = 'Y' then
130      FII_UTIL.put_line('Initializing the Global Currency Precision');
131   end if;
132 
133   g_primary_mau := nvl(fii_currency.get_mau_primary, 0.01 );
134   g_secondary_mau:= nvl(fii_currency.get_mau_secondary, 0.01);
135 
136   if g_debug_flag = 'Y' then
137      FII_UTIL.put_line('Initializing the Global Currencies');
138   end if;
139 
140   g_prim_currency := bis_common_parameters.get_currency_code;
141   g_sec_currency := bis_common_parameters.get_secondary_currency_code;
142 
143   if g_debug_flag = 'Y' then
144      FII_UTIL.put_line('Initializing Global Currency Rate Types');
145   end if;
146 
147   g_prim_rate_type := bis_common_parameters.get_rate_type;
148   g_sec_rate_type := bis_common_parameters.get_secondary_rate_type;
149 
150   l_global_param_list(1) := 'BIS_GLOBAL_START_DATE';
151   l_global_param_list(2) := 'BIS_PRIMARY_CURRENCY_CODE';
152   l_global_param_list(3) := 'BIS_PRIMARY_RATE_TYPE';
153   IF (NOT bis_common_parameters.check_global_parameters(l_global_param_list)) THEN
154        RAISE G_MISS_GLOBAL_PARAMS;
155   END IF;
156 
157   if ((g_sec_currency IS NULL and g_sec_rate_type IS NOT NULL) OR
158       (g_sec_currency IS NOT NULL and g_sec_rate_type IS NULL)) THEN
159          RAISE G_NEED_SECONDARY_INFO;
160   END IF;
161 
162   g_fii_user_id :=  FND_GLOBAL.User_Id;
163   g_fii_login_id := FND_GLOBAL.Login_Id;
164 
165   IF (g_fii_user_id IS NULL OR g_fii_login_id IS NULL) THEN
166       RAISE G_LOGIN_INFO_NOT_AVABLE;
167   END IF;
168 
169   if g_debug_flag = 'Y' then
170      FII_UTIL.put_line('User ID: ' || g_fii_user_id || '  Login ID: ' || g_fii_login_id);
171   end if;
172 
173 EXCEPTION
174    WHEN G_LOGIN_INFO_NOT_AVABLE THEN
175         g_retcode := -1;
176         g_errbuf := 'Can not get User ID and Login ID, program exit';
177         RAISE;
178 
179    WHEN G_MISS_GLOBAL_PARAMS THEN
180         g_retcode := -1;
181         g_errbuf := fnd_message.get_string('FII', 'FII_BAD_GLOBAL_PARA');
182         RAISE;
183 
184    WHEN G_NEED_SECONDARY_INFO THEN
185         g_retcode := -1;
186         g_errbuf := fnd_message.get_string('FII', 'FII_AP_SEC_MISS');
187         RAISE;
188 
189   WHEN OTHERS THEN
190        g_retcode := -1;
191        g_errbuf := '
192 ---------------------------------
193 Error in Procedure: INIT
194 Message: '||sqlerrm;
195        RAISE;
196 
197 END Init;
198 
199 
200 -----------------------------------------------------------
201 --PROCEDURE CHILD_SETUP
202 -----------------------------------------------------------
203 PROCEDURE CHILD_SETUP(p_object_name VARCHAR2) IS
204   l_dir         VARCHAR2(400);
205   l_stmt        VARCHAR2(100);
206 BEGIN
207   g_state := 'Inside the procedure CHILD_SETUP';
208   if g_debug_flag = 'Y' then
209     FII_UTIL.put_line(g_state);
210   end if;
211 
212   ------------------------------------------------------
213   -- Set default directory in case if the profile option
214   -- BIS_DEBUG_LOG_DIRECTORY is not set up
215   ------------------------------------------------------
216   l_dir:='/sqlcom/log';
217 
218   ----------------------------------------------------------------
219   -- fii_util.initialize will get profile options FII_DEBUG_MODE
220   -- and BIS_DEBUG_LOG_DIRECTORY and set up the directory where
221   -- the log files and output files are written to
222   ----------------------------------------------------------------
223   FII_UTIL.initialize(p_object_name||'.log',p_object_name||'.out',l_dir,'FII_AP_INV_SUM_INIT_Worker');
224 
225   g_fii_user_id := FND_GLOBAL.User_Id;
226   g_fii_login_id := FND_GLOBAL.Login_Id;
227 
228 EXCEPTION
229   WHEN OTHERS THEN
230        rollback;
231        g_retcode := -2;
232        g_errbuf := 'Error in Procedure: CHILD_SETUP  Message: '||sqlerrm;
233        RAISE g_procedure_failure;
234 END CHILD_SETUP;
235 
236 
237 
238 -----------------------------------------------------------
239 --  PROCEDURE REGISTER_JOBS
240 --  This procedure REGISTER_JOBS will insert the start and end
241 --  numbers into the worker jobs table.
242 -----------------------------------------------------------
243 PROCEDURE REGISTER_JOBS IS
244 
245   l_max_number      NUMBER;
246   l_start_number    NUMBER;
247   l_end_number      NUMBER;
248   l_count           NUMBER := 0;
249   l_inv_count       NUMBER := 0;
250   l_job_size        NUMBER := 0;
251 
252 
253 BEGIN
254 
255   g_state := 'Register jobs for workers';
256   if g_debug_flag = 'Y' then
257      FII_UTIL.put_line('Register jobs for workers');
258   end if;
259 
260   SELECT max(invoice_ID), min(invoice_ID), COUNT(*)
261   INTO   l_max_number, l_start_number, l_inv_count
262   FROM   FII_AP_INVOICE_B;
263 
264   IF l_inv_count < (g_no_worker * 20000) THEN
265      l_job_size := round(l_inv_count/g_no_worker);
266   ELSE
267      l_job_size := 20000;
268   END IF;
269 
270   WHILE (l_start_number < (l_max_number + 1))
271   LOOP
272 
273    SELECT MAX(INVOICE_ID) INTO l_end_number
274     FROM (SELECT invoice_id
275           FROM fii_ap_invoice_b
276           WHERE invoice_id >= l_start_number
277           ORDER BY invoice_id)
278     WHERE rownum < l_job_size;
279 
280     INSERT INTO FII_AP_PS_WORK_JOBS
281           (Start_Range,
282            End_Range,
283            Worker_Number,
284            Status)
285     VALUES
286           (l_start_number,
287            least(l_end_number, l_max_number),
288            0,
289            'UNASSIGNED');
290 
291     l_count := l_count + 1;
292     l_start_number := least(l_end_number, l_max_number) + 1;
293   END LOOP;
294 
295   if g_debug_flag = 'Y' then
296     FII_UTIL.put_line('Inserted ' || l_count || ' jobs into FII_AP_PS_WORK_JOBS table');
297   end if;
298 
299 EXCEPTION
300   WHEN OTHERS THEN
301        g_retcode := -2;
302        g_errbuf := '
303 Error in Procedure: REGISTER_JOBS
304 Phase: '||g_state||'
305 Message: '||sqlerrm;
306        RAISE g_procedure_failure;
307 
308 END REGISTER_JOBS;
309 
310 
311 
312 ---------------------------------------------------
313 -- FUNCTION LAUNCH_WORKER
314 -- This function LAUNCH_WORKER will submit the subworker
315 -- request.
316 -- p_worker_no is the worker number of this particular worker
317 ---------------------------------------------------
318 FUNCTION LAUNCH_WORKER(p_worker_no  NUMBER) RETURN NUMBER IS
319   l_request_id         NUMBER;
320 BEGIN
321 
322 
323   g_state := 'Inside Launch Worker procedure for worker ' || p_worker_no;
324   if g_debug_flag = 'Y' then
325      FII_UTIL.put_line(g_state);
326   end if;
327 
328   l_request_id := FND_REQUEST.SUBMIT_REQUEST
329                          ('FII',
330                           'FII_AP_INV_SUM_INIT_SUBWORKER',
331                           NULL,
332                           NULL,
333                           FALSE,
334                           g_start_date,
335                           g_end_date,
336                           p_worker_no);
337 
338   -- This is the concurrent executable of the subworker.
339 
340   IF (l_request_id = 0) THEN
341       rollback;
342       g_retcode := -2;
343       g_errbuf := '
344 Error in Procedure: LAUNCH_WORKER
345 Message: '||fnd_message.get;
346       RAISE G_NO_CHILD_PROCESS;
347 
348   END IF;
349 
350   RETURN l_request_id;
351 
352 EXCEPTION
353   WHEN G_NO_CHILD_PROCESS THEN
354        g_retcode := -1;
355        FII_UTIL.put_line('No child process launched');
356        RAISE;
357    WHEN OTHERS THEN
358         ROLLBACK;
359         g_retcode := -2;
360         g_errbuf := '
361 Error in Procedure: LAUNCH_WORKER
362 Message: '||sqlerrm;
363         RAISE g_procedure_failure;
364 
365 END LAUNCH_WORKER;
366 
367 
368 
369 ---------------------------------------------------
370 -- PROCEDURE MONITOR_WORKER
371 -- This function MONITOR_WORKER will monitor the subworker
372 -- request.
373 ---------------------------------------------------
374 PROCEDURE MONITOR_WORKER IS
375 
376   l_unassigned_cnt       NUMBER := 0;
377   l_completed_cnt        NUMBER := 0;
378   l_wip_cnt              NUMBER := 0;
379   l_failed_cnt           NUMBER := 0;
380   l_tot_cnt              NUMBER := 0;
381   l_last_unassigned_cnt  NUMBER := 0;
382   l_last_completed_cnt   NUMBER := 0;
383   l_last_wip_cnt         NUMBER := 0;
384   l_cycle                NUMBER := 0;
385 
386 BEGIN
387 
388   g_state := 'Inside Monitor Workers';
389   if g_debug_flag = 'Y' then
390      FII_UTIL.put_line('Register jobs for workers');
391   end if;
392 
393   LOOP
394 
395     SELECT nvl(sum(decode(status,'UNASSIGNED',1,0)),0),
396            nvl(sum(decode(status,'COMPLETED',1,0)),0),
397            nvl(sum(decode(status,'IN PROCESS',1,0)),0),
398            nvl(sum(decode(status,'FAILED',1,0)),0),
399            count(*)
400     INTO   l_unassigned_cnt,
401            l_completed_cnt,
402            l_wip_cnt,
403            l_failed_cnt,
404            l_tot_cnt
405     FROM   FII_AP_PS_WORK_JOBS;
406 
407     if g_debug_flag = 'Y' then
408        FII_UTIL.put_line('Job status - Unassigned:'||l_unassigned_cnt||
409                          ' In Process:'||l_wip_cnt||
410                          ' Completed:'||l_completed_cnt||
411                          ' Failed:'||l_failed_cnt);
412     end if;
413 
414     IF l_failed_cnt > 0 THEN
415        g_retcode := -2;
416        g_errbuf := '
417 Error in Main Procedure:
418 Message: At least one of the workers have errored out';
419        RAISE g_procedure_failure;
420 
421     END IF;
422 
423     ----------------------------------------------
424     -- IF the number of complete count equals to
425     -- the total count, then that means all workers
426     -- have completed.  Then we can exit the loop
427     ----------------------------------------------
428 
429     IF l_tot_cnt = l_completed_cnt THEN
430 
431        if g_debug_flag = 'Y' then
432           FII_UTIL.put_line('Job status - Total: '|| l_tot_cnt);
433        end if;
434        EXIT;
435     END IF;
436 
437     -------------------------
438     -- Detect infinite loops
439     -------------------------
440     IF (l_unassigned_cnt = l_last_unassigned_cnt AND
441         l_completed_cnt = l_last_completed_cnt AND
442         l_wip_cnt = l_last_wip_cnt) THEN
443         l_cycle := l_cycle + 1;
444     ELSE
445         l_cycle := 1;
446     END IF;
447 
448     ----------------------------------------
449     -- MAX_LOOP is a global variable you set.
450     -- It represents the number of minutes
451     -- you want to wait for each worker to
452     -- complete.  We can set it to 30 minutes
453     -- for now
454     ----------------------------------------
455 
456     IF (l_cycle > MAX_LOOP) THEN
457         g_retcode := -2;
458         g_errbuf := '
459 Error in Main Procedure:
460 Message: No progress have been made for '||MAX_LOOP||' minutes Terminating';
461 
462         RAISE g_procedure_failure;
463     END IF;
464 
465     -------------------------
466     -- Sleep 60 Seconds
467     -------------------------
468     dbms_lock.sleep(60);
469 
470     l_last_unassigned_cnt := l_unassigned_cnt;
471     l_last_completed_cnt := l_completed_cnt;
472     l_last_wip_cnt := l_wip_cnt;
473 
474   END LOOP;
475 
476   if g_debug_flag = 'Y' then
477      FII_UTIL.stop_timer;
478      FII_UTIL.print_timer('Duration');
479   end if;
480 
481 
482 EXCEPTION
483    WHEN G_PROCEDURE_FAILURE THEN
484         ROLLBACK;
485         RAISE g_procedure_failure;
486    WHEN OTHERS THEN
487         ROLLBACK;
488         g_retcode := -2;
489         g_errbuf := '
490 Error in Procedure: MONITOR_WORKER
491 Message: '||sqlerrm;
492         RAISE g_procedure_failure;
493 
494 END MONITOR_WORKER;
495 
496 
497 ------------------------------------------------------------------
498 -- Procedure insert_wh_prepay_amount
499 -- Purpose
500 -- This fuction INSERT_WH_PREPAY_AMOUNT inserts the prepayment and
501 -- withholding amount applicable for a payment schedule into the
502 -- temp table.
503 ------------------------------------------------------------------
504 Procedure INSERT_WH_PREPAY_AMOUNT IS
505 
506 BEGIN
507 
508   g_state := 'Inserting records into the FII_AP_WH_TAX_T table';
509   if g_debug_flag = 'Y' then
510      FII_UTIL.put_line('');
511      FII_UTIL.put_line(g_state);
512      fii_util.start_timer;
513      fii_util.put_line('');
514   end if;
515 
516 
517   /* Selecting the prorated prepayment and withholding amount for a
518      payment schedule and inserting into the temp table */
519 
520 
521   INSERT /*+ append parallel(T) */ INTO fii_ap_wh_tax_t T
522         (Invoice_ID,
523          Payment_Num,
524 	 Creation_Date,
525          Due_Date,
526          Discount_Date,
527          Second_Discount_Date,
528          Third_Discount_Date,
529          Invoice_Type,
530          Entered_Date,
531          WH_Tax_Amount)
532   SELECT /*+ ordered use_hash(AID,AI,FC,PS) parallel(AID) parallel(AI) parallel(FC)
533              parallel(PS) */
534          AI.Invoice_ID,
535          PS.Payment_Num,
536 	 TRUNC(AID.Creation_Date) Creation_Date,
537          TRUNC(PS.Due_Date),
538          TRUNC(PS.Discount_Date),
539          TRUNC(PS.Second_Discount_Date),
540          TRUNC(PS.Third_Discount_Date),
541          AI.Invoice_Type,
542          TRUNC(AI.Entered_Date),
543          -1 * DECODE(AI.Invoice_Amount, 0, 0,
544                  DECODE(FC.Minimum_Accountable_Unit, NULL,
545                    ROUND(PS.Gross_Amount *
546                               SUM(AID.Amount)/AI.Invoice_Amount
547                            / 0.01) * 0.01,
548                    ROUND(PS.Gross_Amount *
549                               SUM(AID.Amount)/AI.Invoice_Amount
550                            / FC.Minimum_Accountable_Unit) * FC.Minimum_Accountable_Unit))
551                 WH_Tax_Amount
552   FROM   FII_AP_Invoice_B AI,
553          AP_Invoice_Distributions_All AID,
554          AP_Invoice_Lines_All AIL,
555          AP_Payment_Schedules_All PS,
556          FND_Currencies FC
557   WHERE  AI.Invoice_ID             = AID.Invoice_ID
558   AND    AID.Invoice_ID = AIL.Invoice_ID
559   AND    AID.Invoice_Line_Number = AIL.Line_Number
560   AND    AI.Cancel_Date IS NULL
561   AND    (AID.Line_Type_Lookup_Code IN ('AWT') OR (AID.Line_Type_Lookup_Code IN ('NONREC_TAX', 'REC_TAX') AND AID.Prepay_Distribution_ID IS NOT NULL))
562   AND    (AIL.Invoice_Includes_Prepay_Flag IS NULL or AIL.Invoice_Includes_Prepay_Flag = 'N')
563   AND    PS.Invoice_ID             = AI.Invoice_ID
564   AND    FC.Currency_Code          = AI.Payment_Currency_Code
565   GROUP  BY AI.Invoice_ID,
566             AI.Invoice_Amount,
567             PS.Payment_Num,
568             PS.Gross_Amount,
569 	    TRUNC(AID.Creation_Date),
570             TRUNC(PS.Due_Date),
571             TRUNC(PS.Discount_Date),
572             TRUNC(PS.Second_Discount_Date),
573             TRUNC(PS.Third_Discount_Date),
574             AI.Invoice_Type,
575             TRUNC(AI.Entered_Date),
576             FC.Precision,
577             FC.Minimum_Accountable_Unit;
578 
579 
580   if g_debug_flag = 'Y' then
581      fii_util.put_line('Inserted '||SQL%ROWCOUNT||' records into FII_AP_WH_TAX_T');
582      fii_util.stop_timer;
583      fii_util.print_timer('Duration');
584   end if;
585 
586   FND_STATS.GATHER_TABLE_STATS(OWNNAME => 'FII', TABNAME => 'FII_AP_WH_TAX_T');
587   EXECUTE IMMEDIATE 'ALTER SESSION ENABLE PARALLEL DML';
588 
589   COMMIT;
590 
591 
592 
593   g_state := 'Inserting records into the FII_AP_Prepay_T table';
594     if g_debug_flag = 'Y' then
595        FII_UTIL.put_line('');
596        FII_UTIL.put_line(g_state);
597        fii_util.start_timer;
598        fii_util.put_line('');
599     end if;
600 
601   INSERT /*+ append parallel(T) */ INTO fii_ap_prepay_t T
602 	 (Invoice_ID,
603 	  Payment_Num,
604 	  Creation_Date,
605           Due_Date,
606           Discount_Date,
607           Second_Discount_Date,
608           Third_Discount_Date,
609           Entered_Date,
610 	  Prepay_Amount,
611 	  Check_ID)
612   SELECT  /*+ parallel(AID_prepay) parallel(PS_Prepay) */
613           PS_Prepay.Invoice_ID Invoice_ID,
614           PS_Prepay.Payment_Num Payment_Num,
615           AID_Prepay.Creation_Date Creation_Date,
616           PS_Prepay.Due_Date Due_Date,
617           PS_Prepay.Discount_Date Discount_Date,
618           PS_Prepay.Second_Discount_Date Second_Discount_Date,
619           PS_Prepay.Third_Discount_Date Third_Discount_Date,
620           PS_Prepay.Entered_Date Entered_Date,
621           CASE
622               WHEN    PS_Prepay.First_PP + 1 <= AID_Prepay.First + 1 AND AID_Prepay.First + 1 <= PS_Prepay.Last_PP
623                       THEN LEAST(PS_Prepay.Last_PP, AID_Prepay.Last) - AID_Prepay.First
624               WHEN    AID_Prepay.First + 1 <= PS_Prepay.First_PP + 1 AND PS_Prepay.First_PP + 1 <= AID_Prepay.Last
625                       THEN LEAST(PS_Prepay.Last_PP, AID_Prepay.Last) - PS_Prepay.First_PP
626           END Prepay_Amount,
627 	  AID_Prepay.Check_ID Check_ID
628   FROM    (SELECT /*+ use_hash(AID,TEMP,AIP) parallel(AID) parallel(TEMP) parallel(AIP) */
629                   AID.Invoice_ID Invoice_ID,
630                   TRUNC(AID.Creation_Date) Creation_Date,
631                   SUM(-1*ROUND((AID.Amount * AI.Payment_Cross_Rate)
632                                    / NVL(FC.Minimum_Accountable_Unit, 0.01))
633                             * NVL(FC.Minimum_Accountable_Unit, 0.01)) Prepymt,
634                   (SUM(SUM(-1*ROUND((AID.Amount * AI.Payment_Cross_Rate)
635                                    / NVL(FC.Minimum_Accountable_Unit, 0.01))
636                             * NVL(FC.Minimum_Accountable_Unit, 0.01)))
637                                                  OVER (PARTITION BY AID.Invoice_ID
638                                                  ORDER BY AID.Invoice_ID, TRUNC(AID.Creation_Date)
639                                                  ROWS UNBOUNDED PRECEDING))
640                         - SUM(-1*ROUND((AID.Amount * AI.Payment_Cross_Rate)
641                                    / NVL(FC.Minimum_Accountable_Unit, 0.01))
642                             * NVL(FC.Minimum_Accountable_Unit, 0.01)) AS First,
643                   SUM(SUM(-1*AID.Amount * AI.Payment_Cross_Rate)) OVER (PARTITION BY AID.Invoice_ID
644                                                 ORDER BY AID.Invoice_ID, TRUNC(AID.Creation_Date)
645                                                 ROWS UNBOUNDED PRECEDING) AS Last,
646 		  AIP.Check_ID Check_ID
647           FROM    AP_Invoice_Distributions_All AID,
648                   AP_Invoice_Lines_All AIL,
649 		  AP_Invoice_Distributions_All TEMP,
650 		  (SELECT /*+ parallel(AIP1) */ Invoice_ID Invoice_ID,
651 			  MIN(Check_ID) Check_ID
652 		   FROM	  AP_Invoice_Payments_All AIP1
653 		   GROUP BY Invoice_ID) AIP,
654 		  AP_Invoices_All AI,
655                   FND_Currencies FC
656           WHERE   AID.Invoice_ID = AI.Invoice_ID
657           AND     AID.Invoice_ID = AIL.Invoice_ID
658           AND     AID.Invoice_Line_Number = AIL.Line_Number
659           AND     AID.Line_Type_Lookup_Code = 'PREPAY'
660 	  --AND 	  AID.Reversal_Flag IS NULL
661 	  AND NVL(AID.Reversal_Flag,'N') = 'N'
662 	  AND 	  (AIL.Invoice_Includes_Prepay_Flag IS NULL OR AIL.Invoice_Includes_Prepay_Flag = 'N')
663           AND	  AID.Prepay_Distribution_ID = TEMP.Invoice_Distribution_ID
664 	  AND	  TEMP.Invoice_ID = AIP.Invoice_ID
665           AND     AI.Payment_Currency_Code = FC.Currency_Code
666 	  GROUP BY AID.Invoice_ID, TRUNC(AID.Creation_Date), AIP.Check_ID) AID_Prepay,
667           (SELECT /*+ parallel(PP) */
668                   PP.Invoice_ID Invoice_ID,
669                   PP.Payment_Num Payment_Num,
670                   PP.Due_Date Due_Date,
671                   PP.Discount_Date Discount_Date,
672                   PP.Second_Discount_Date Second_Discount_Date,
673                   PP.Third_Discount_Date Third_Discount_Date,
674                   PP.Entered_Date Entered_Date,
675                   PP.PP_Amount,
676                   (SUM(PP.PP_Amount) OVER (PARTITION BY PP.Invoice_ID
677                                            ORDER BY PP.Invoice_ID, PP.Payment_Num
678                                            ROWS UNBOUNDED PRECEDING)) - PP.PP_Amount AS First_PP,
679                   SUM(PP.PP_Amount) OVER (PARTITION BY PP.Invoice_ID
680                                           ORDER BY PP.Invoice_ID, PP.Payment_Num
681                                           ROWS UNBOUNDED PRECEDING) AS Last_PP
682           FROM (SELECT /*+ use_hash(AI) parallel(AI) parallel(PS) parallel(PAY) parallel(TEMP) */
683                        PS.Invoice_ID Invoice_ID,
684                        PS.Payment_Num Payment_Num,
685                        TRUNC(PS.Due_Date) Due_Date,
686                        TRUNC(PS.Discount_Date) Discount_Date,
687                        TRUNC(PS.Second_Discount_Date) Second_Discount_Date,
688                        TRUNC(PS.Third_Discount_Date) Third_Discount_Date,
689                        TRUNC(AI.Entered_Date) Entered_Date,
690                        PS.Gross_Amount - PS.Amount_Remaining - NVL(PAY.Payment_Amount,0) - NVL(TEMP.WH_Tax_Amount, 0) PP_Amount
691                FROM   FII_AP_INVOICE_B AI,
692                       AP_PAYMENT_SCHEDULES_ALL PS,
693                       (SELECT /*+ parallel(AI) parallel(AIP) parallel(PS)  */
694                               AIP.Invoice_ID Invoice_ID,
695                               AIP.Payment_Num Payment_Num,
696                               SUM(AIP.Amount + NVL(AIP.Discount_Taken, 0)) Payment_Amount
697                       FROM    AP_Invoice_Payments_All AIP,
698                               FII_AP_Invoice_B AI,
699                               AP_Payment_Schedules_All PS
700                       WHERE  PS.Invoice_ID   = AI.Invoice_ID
701                       AND    AIP.Invoice_ID  = AI.Invoice_ID
702                       AND    AI.Invoice_Type NOT IN ('PREPAYMENT')
703                       AND    AI.Cancel_Date IS NULL
704                       AND    AIP.Payment_Num = PS.Payment_Num
705    	              GROUP BY AIP.Invoice_ID, AIP.Payment_Num) PAY,
706                       (SELECT /*+ parallel(STG) */ Invoice_ID,
707                               Payment_Num,
708                               SUM(WH_Tax_Amount) WH_Tax_Amount
709                       FROM    FII_AP_WH_TAX_T STG
710                       GROUP BY Invoice_ID, Payment_Num) TEMP
711               WHERE   PS.Invoice_ID = PAY.Invoice_ID (+)
712               AND     PS.Payment_Num = PAY.Payment_Num (+)
713               AND     PS.Invoice_ID = TEMP.Invoice_ID (+)
714               AND     PS.Payment_Num = TEMP.Payment_Num (+)
715               AND     AI.Invoice_ID = PS.Invoice_ID
716               ORDER BY PS.Invoice_ID, PS.Payment_Num) PP
717           WHERE   PP.PP_Amount > 0) PS_Prepay
718   WHERE   AID_Prepay.Invoice_ID = PS_Prepay.Invoice_ID
719   AND     AID_Prepay.Prepymt > 0
720   AND     ((PS_Prepay.First_PP + 1 <= AID_Prepay.First + 1 AND AID_Prepay.First + 1 <= PS_Prepay.Last_PP) OR
721            (AID_Prepay.First + 1 <= PS_Prepay.First_PP + 1 AND PS_Prepay.First_PP + 1 <= AID_Prepay.Last));
722 
723 
724   if g_debug_flag = 'Y' then
725      fii_util.put_line('Inserted '||SQL%ROWCOUNT||' records into FII_AP_Prepay_T');
726      fii_util.stop_timer;
727      fii_util.print_timer('Duration');
728   end if;
729 
730 
731   FND_STATS.GATHER_TABLE_STATS(OWNNAME => 'FII', TABNAME => 'FII_AP_PREPAY_T');
732   EXECUTE IMMEDIATE 'ALTER SESSION ENABLE PARALLEL DML';
733 
734   COMMIT;
735 
736 
737 EXCEPTION
738    WHEN OTHERS THEN
739       g_errbuf:=sqlerrm;
740       g_retcode:= -1;
741       g_exception_msg  := g_retcode || ':' || g_errbuf;
742       FII_UTIL.put_line('Error occured while ' || g_state);
743       FII_UTIL.put_line(g_exception_msg);
744       RAISE;
745 
746 END Insert_WH_Prepay_Amount;
747 
748 
749 ------------------------------------------------------------------
750 -- Procedure insert_payment_check_info
751 -- Purpose
752 -- This fuction INSERT_PAYMENT_CHECK_INFO inserts the payment and
753 -- check information into a staging table
754 ------------------------------------------------------------------
755 Procedure INSERT_PAYMENT_CHECK_INFO IS
756 
757 BEGIN
758 
759   g_state := 'Inserting records into the FII_AP_PAY_CHK_STG table';
760   if g_debug_flag = 'Y' then
761      FII_UTIL.put_line('');
762      FII_UTIL.put_line(g_state);
763      fii_util.start_timer;
764      fii_util.put_line('');
765   end if;
766 
767 
768   INSERT /*+ append parallel(S) */ INTO FII_AP_PAY_CHK_STG S
769         (Invoice_ID,
770          Payment_Num,
771          Check_Date,
772          Payment_Amount,
773          Discount_Taken,
774          Invoice_Type,
775          Due_Date,
776          Discount_Date,
777          Second_Discount_Date,
778          Third_Discount_Date,
779          Entered_Date,
780          Invp_Creation_Date)
781   SELECT /*+ use_hash(PS, AIP, AI, AC) parallel(PS) parallel(AIP) parallel(AI) parallel(AC)  */
782          PS.Invoice_ID Invoice_Id,
783          PS.Payment_Num Payment_Num,
784          TRUNC(AC.Check_Date) Check_Date,
785          AIP.Amount + NVL(AIP.Discount_Taken,0) Payment_Amount,
786          NVL(AIP.Discount_Taken,0) Discount_Taken,
787          AI.Invoice_Type Invoice_Type,
788          TRUNC(PS.Due_Date) Due_Date,
789          TRUNC(PS.Discount_Date) Discount_Date,
790          TRUNC(PS.Second_Discount_Date) Second_Discount_Date,
791          TRUNC(PS.Third_Discount_Date) Third_Discount_Date,
792          TRUNC(AI.Entered_Date) Entered_Date,
793          TRUNC(AIP.Creation_Date) Invp_Creation_Date
794   FROM   FII_AP_INVOICE_B AI,
795          AP_Checks_All AC,
796          AP_Invoice_Payments_All AIP,
797          AP_Payment_Schedules_All PS
798   WHERE  AI.Invoice_ID  = PS.Invoice_ID
799   AND    PS.Invoice_ID  = AIP.Invoice_ID
800   AND    PS.Payment_Num = AIP.Payment_Num
801   AND    AC.Check_ID    = AIP.Check_ID
802   AND    AC.Void_Date   IS NULL;
803 
804   if g_debug_flag = 'Y' then
805      fii_util.put_line('Inserted '||SQL%ROWCOUNT||' records into FII_AP_PAY_CHK_STG');
806      fii_util.stop_timer;
807      fii_util.print_timer('Duration');
808   end if;
809 
810   FND_STATS.GATHER_TABLE_STATS(OWNNAME => 'FII', TABNAME => 'FII_AP_PAY_CHK_STG');
811   EXECUTE IMMEDIATE 'ALTER SESSION ENABLE PARALLEL DML';
812 
813   COMMIT;
814 
815 EXCEPTION
816    WHEN OTHERS THEN
817       g_errbuf:=sqlerrm;
818       g_retcode:= -1;
819       g_exception_msg  := g_retcode || ':' || g_errbuf;
820       FII_UTIL.put_line('Error occured while ' || g_state);
821       FII_UTIL.put_line(g_exception_msg);
822       RAISE;
823 
824 END Insert_Payment_Check_Info;
825 
826 
827 
828 -----------------------------------------------------------
829 --  FUNCTION VERIFY_MISSING_RATES
830 -----------------------------------------------------------
831 FUNCTION Verify_Missing_Rates RETURN NUMBER IS
832 --  l_miss_rates_prim   NUMBER := 0;
833 --  l_miss_rates_sec    NUMBER := 0;
834   l_miss_rates_ps     NUMBER := 0;
835   l_payment_currency  VARCHAR2(2000) := NULL;
836   l_trx_date          VARCHAR2(2000) := NULL;
837   l_miss_rates_func   NUMBER := 0;
838 
839   --------------------------------------------------------
840   -- Cursor declaration required to generate output file
841   -- containing rows with MISSING CONVERSION RATES
842   --------------------------------------------------------
843 
844   CURSOR prim_MissingRate IS
845   SELECT DISTINCT Functional_Currency From_Currency,
846          decode(prim_conversion_rate,-3,  to_date('01/01/1999','MM/DD/RRRR'),
847          LEAST(TRX_DATE,sysdate)) Trx_Date
848   FROM   FII_AP_PS_RATES_TEMP RATES
849   WHERE  RATES.Prim_Conversion_Rate < 0 ;
850 
851   CURSOR sec_MissingRate IS
852   SELECT DISTINCT FUNCTIONAL_CURRENCY From_Currency,
853          decode(sec_conversion_rate,-3,  to_date('01/01/1999','MM/DD/RRRR'),
854          LEAST(TRX_DATE,sysdate)) Trx_Date
855   FROM   FII_AP_PS_RATES_TEMP RATES
856   WHERE  RATES.Sec_Conversion_Rate < 0 ;
857 
858   CURSOR func_MissingRate IS
859   SELECT DISTINCT From_Currency,
860          To_Currency,
861          decode(conversion_rate,-3,  to_date('01/01/1999','MM/DD/RRRR'),
862          LEAST(TRX_DATE,sysdate)) Trx_Date,
863          Conversion_Type
864   FROM   FII_AP_FUNC_RATES_TEMP RATES
865   WHERE  RATES.Conversion_Rate < 0 ;
866 
867 BEGIN
868   g_state := 'Checking to see which additional rates need to be defined, if any';
869 
870   if g_debug_flag = 'Y' then
871      fii_util.put_line(' ');
872      fii_util.put_line(g_state);
873      fii_util.start_timer;
874      fii_util.put_line('');
875   end if;
876 
877   BEGIN
878     SELECT 1
879     INTO l_miss_rates_ps
880     FROM FII_AP_PS_RATES_TEMP RATES
881     WHERE (RATES.Prim_Conversion_Rate < 0
882     OR RATES.Sec_Conversion_Rate < 0)
883     AND   ROWNUM = 1;
884   EXCEPTION
885     WHEN NO_DATA_FOUND THEN l_miss_rates_ps := 0;
886   END;
887 
888   BEGIN
889     SELECT 1
890     INTO   l_miss_rates_func
891     FROM   FII_AP_FUNC_RATES_TEMP RATES
892     WHERE  RATES.Conversion_Rate < 0
893     AND    ROWNUM = 1;
894   EXCEPTION
895     WHEN NO_DATA_FOUND THEN l_miss_rates_func := 0;
896   END;
897 
898   --------------------------------------------------------
899   -- Print out translated messages to let user know there
900   -- are missing exchange rate information
901   --------------------------------------------------------
902   IF (l_miss_rates_ps > 0 OR
903       l_miss_rates_func > 0) THEN
904       FII_MESSAGE.write_log(
905       msg_name    => 'BIS_DBI_CURR_PARTIAL_LOAD',
906       token_num   => 0);
907   END IF;
908 
909   --------------------------------------------------------
910   -- Print out missing rates report
911   --------------------------------------------------------
912 
913    IF (l_miss_rates_ps > 0 OR
914        l_miss_rates_func > 0) THEN
915        BIS_COLLECTION_UTILITIES.writeMissingRateHeader;
916 
917 
918       FOR rate_record in func_MissingRate
919       LOOP
920           BIS_COLLECTION_UTILITIES.writeMissingRate(
921                         rate_record.conversion_type,
922                         rate_record.from_currency,
923                         rate_record.to_currency,
924                         rate_record.trx_date);
925       END LOOP;
926 
927 
928          FOR rate_record in prim_MissingRate
929          LOOP
930              BIS_COLLECTION_UTILITIES.writeMissingRate(
931                            g_prim_rate_type,
932                            rate_record.from_currency,
933                            g_prim_currency,
934                            rate_record.trx_date);
935          END LOOP;
936 
937 
938          FOR rate_record in sec_MissingRate
939          LOOP
940              BIS_COLLECTION_UTILITIES.writeMissingRate(
941                         g_sec_rate_type,
942                         rate_record.from_currency,
943                         g_sec_currency,
944                         rate_record.trx_date);
945          END LOOP;
946          RETURN -1;
947 
948   ELSE
949         RETURN 1;
950   END IF;  /* IF (l_miss_rates_prim > 0) */
951 
952 EXCEPTION
953   WHEN OTHERS THEN
954        g_errbuf:=sqlerrm;
955        g_retcode:= -1;
956        g_exception_msg  := g_retcode || ':' || g_errbuf;
957        FII_UTIL.put_line('Error occured while ' || g_state);
958        FII_UTIL.put_line(g_exception_msg);
959        RAISE;
960 END Verify_Missing_Rates;
961 
962 
963 ------------------------------------
964 ---- PROCEDURE INSERT_RATES
965 ------------------------------------
966 
967 PROCEDURE Insert_Rates IS
968 l_host_var VARCHAR2(100);
969 
970 BEGIN
971 
972   g_state := 'Loading data into rates table';
973 
974   if g_debug_flag = 'Y' then
975      fii_util.put_line(' ');
976      fii_util.put_line(g_state);
977      fii_util.start_timer;
978      fii_util.put_line('');
979   end if;
980 
981   INSERT /*+ append parallel(T) */
982   INTO    FII_AP_PS_RATES_TEMP T
983         (Functional_Currency,
984          Trx_Date,
985          Prim_Conversion_Rate,
986          Sec_Conversion_Rate)
987   SELECT Curr_Code,
988          Trx_Date,
989 	 DECODE(Curr_Code, g_prim_currency, 1,
990                    FII_CURRENCY.GET_GLOBAL_RATE_PRIMARY (Curr_Code, LEAST(Trx_Date,sysdate)))
991 	    PRIM_CONVERSION_RATE,
992          DECODE(Curr_Code, g_sec_currency, 1,
993                    FII_CURRENCY.GET_GLOBAL_RATE_SECONDARY(Curr_Code, LEAST(Trx_Date,sysdate)))
994              SEC_CONVERSION_RATE
995   FROM  (
996          SELECT /*+ parallel(ASP) parallel(AI) use_hash(AI) */
997                 Distinct ASP.Base_Currency_code Curr_Code,
998                 TRUNC(AI.Invoice_Date) Trx_Date
999          FROM   AP_Invoices_All AI,
1000                 AP_System_Parameters_All ASP
1001          WHERE  AI.Org_ID = ASP.Org_ID
1002          AND    AI.Invoice_Type_Lookup_Code <> 'EXPENSE REPORT'
1003          AND    AI.Invoice_Amount <> 0
1004          AND    TRUNC(AI.Creation_Date) >= g_start_date
1005          AND    TRUNC(AI.Creation_Date) + 0 <= g_end_date + 0.99999);
1006 
1007   if g_debug_flag = 'Y' then
1008      fii_util.put_line('Processed '||SQL%ROWCOUNT||' rows');
1009      fii_util.stop_timer;
1010      fii_util.print_timer('Duration');
1011   end if;
1012 
1013   COMMIT;
1014 
1015   if g_debug_flag = 'Y' then
1016      fii_util.put_line(' ');
1017      fii_util.put_line('Loading data into functional rates table');
1018      fii_util.start_timer;
1019      fii_util.put_line('');
1020   end if;
1021 
1022 
1023   INSERT /*+ append parallel(T)*/
1024   INTO   FII_AP_FUNC_RATES_TEMP T
1025         (From_Currency,
1026          To_Currency,
1027          Trx_Date,
1028          Conversion_Type,
1029          Conversion_Rate,
1030          Functional_MAU)
1031   SELECT From_Currency,
1032          To_Currency,
1033          Trx_Date,
1034          Exchange_Rate_Type,
1035          DECODE(Exchange_Rate_Type, 'User', Exchange_Rate, 'No Rate Type', 1,
1036                  DECODE(From_Currency, To_Currency, 1,
1037                          FII_CURRENCY.get_rate(From_Currency, To_Currency,
1038                                                       LEAST(Trx_Date,sysdate), Exchange_Rate_Type)))
1039              Conversion_Rate,
1040          Functional_MAU
1041   FROM  (
1042          SELECT /*+ parallel(AI) parallel(ASP) parallel(FC) use_hash(AI,ASP,FC) */
1043                 Distinct AI.Payment_Currency_Code From_Currency,
1044                 ASP.Base_Currency_code To_Currency,
1045                 TRUNC(NVL(AI.Exchange_Date, AI.Invoice_Date)) Trx_Date,
1046                 NVL(AI.Exchange_Rate_Type,'No Rate Type') Exchange_Rate_Type,
1047                 DECODE(AI.Exchange_Rate_Type, 'User', AI.Exchange_Rate, 1)
1048                               Exchange_Rate,
1049                 NVL(FC.Minimum_Accountable_Unit, 0.01) Functional_MAU
1050          FROM   AP_Invoices_All AI,
1051                 AP_System_Parameters_All ASP,
1052                 FND_Currencies FC
1053          WHERE  AI.Org_ID = ASP.Org_ID
1054          AND    AI.Invoice_Type_Lookup_Code <> 'EXPENSE REPORT'
1055          AND    AI.Invoice_Amount <> 0
1056          AND    TRUNC(AI.Creation_Date) >= g_start_date
1057          AND    TRUNC(AI.Creation_Date) + 0 <= g_end_date + 0.99999
1058          AND    ASP.Base_Currency_Code = FC.Currency_Code);
1059 
1060 
1061   if g_debug_flag = 'Y' then
1062      fii_util.put_line('Processed '||SQL%ROWCOUNT||' rows');
1063      fii_util.stop_timer;
1064      fii_util.print_timer('Duration');
1065   end if;
1066 
1067   COMMIT;
1068 
1069   l_host_var := 'ALTER TABLE ' || g_fii_schema || '.FII_AP_PS_RATES_TEMP CACHE';
1070   EXECUTE IMMEDIATE l_host_var;
1071   l_host_var := 'ALTER TABLE ' || g_fii_schema || '.FII_AP_FUNC_RATES_TEMP CACHE';
1072   EXECUTE IMMEDIATE l_host_var;
1073 
1074 
1075 EXCEPTION
1076    WHEN OTHERS THEN
1077       g_errbuf:=sqlerrm;
1078       g_retcode:= -1;
1079       g_exception_msg  := g_retcode || ':' || g_errbuf;
1080       FII_UTIL.put_line('Error occured while ' || g_state);
1081       FII_UTIL.put_line(g_exception_msg);
1082       RAISE;
1083 
1084 END Insert_Rates;
1085 
1086 ------------------------------------------------------------------
1087 -- Procedure POPULATE_HOLDS_SUM
1088 -- Purpose
1089 --   This POPULATE_HOLDS_SUM routine inserts records into the
1090 --   FII AP Holds summary tables.
1091 ------------------------------------------------------------------
1092 
1093 PROCEDURE POPULATE_HOLDS_SUM IS
1094 
1095 BEGIN
1096 
1097   g_state := 'Deleting records from FII_AP_INV_HOLDS_B that are already existing';
1098   if g_debug_flag = 'Y' then
1099      FII_UTIL.put_line('');
1100      FII_UTIL.put_line(g_state);
1101   end if;
1102 
1103   /* For Initial Load we will truncate the data in the holds summary table
1104      and re-populate this table */
1105   TRUNCATE_TABLE('MLOG$_FII_AP_INV_HOLDS_B');
1106   TRUNCATE_TABLE('FII_AP_INV_HOLDS_B');
1107 
1108 
1109   g_state := 'Populating FII_AP_INV_HOLDS_B FROM AP_HOLDS_ALL table';
1110   if g_debug_flag = 'Y' then
1111      FII_UTIL.put_line(g_state);
1112      FII_UTIL.start_timer;
1113      FII_UTIL.put_line('');
1114   end if;
1115 
1116   INSERT /*+ append parallel(S) */ INTO FII_AP_INV_HOLDS_B S
1117         (Time_ID,
1118          Period_Type_ID,
1119          Org_ID,
1120          Supplier_ID,
1121          Invoice_ID,
1122          Hold_Date,
1123          Hold_Code,
1124          Held_By,
1125          Hold_Category,
1126          Release_Date,
1127          Released_By,
1128          Created_By,
1129          Creation_Date,
1130          Last_Updated_By,
1131          Last_Update_Date,
1132          Last_Update_Login)
1133   SELECT /*+ ordered parallel(AH) parallel(AI) use_hash(AI,AH) */
1134          TO_NUMBER(TO_CHAR(AH.Hold_Date,'J')),
1135          1,
1136          AH.Org_ID,
1137          AI.Supplier_ID,
1138          AH.Invoice_ID,
1139          TRUNC(AH.Hold_Date),
1140          AH.Hold_Lookup_Code,
1141          AH.Held_By,
1142         (CASE
1143             WHEN Hold_Lookup_Code IN ('DIST ACCT INVALID', 'ERV ACCT INVALID')
1144                  THEN 'ACCOUNT'
1145             WHEN Hold_Lookup_Code IN ('CANT FUNDS CHECK', 'INSUFFICIENT FUNDS')
1146                  THEN 'FUNDS'
1147             WHEN Hold_Lookup_Code IN ('AMOUNT', 'AWT ERROR', 'VENDOR',
1148                                       'NATURAL ACCOUNT TAX', 'PREPAID AMOUNT')
1149                  THEN 'INVOICE'
1150             WHEN Hold_Lookup_Code IN ('CANT CLOSE PO', 'CANT TRY PO CLOSE',
1151                                       'FINAL MATCHING', 'PO REQUIRED', 'MAX QTY ORD',
1152                                       'MAX QTY REC', 'MAX RATE AMOUNT', 'MAX SHIP AMOUNT',
1153                                       'MAX TOTAL AMOUNT', 'PRICE', 'QTY ORD', 'QTY REC',
1154                                       'QUANTITY', 'REC EXCEPTION', 'TAX DIFFERENCE')
1155                  THEN 'PO MATCHING'
1156             WHEN Hold_Lookup_Code IN ('DIST VARIANCE', 'TAX VARIANCE', 'TAX AMOUNT RANGE', 'LINE VARIANCE')
1157                  THEN 'VARIANCE'
1158             WHEN Hold_Lookup_Code IN ('NO RATE', 'VENDOR')
1159                  THEN 'MISCELLANEOUS'
1160          ELSE 'USER DEFINED'
1161          END) AS Hold_Category,
1162          DECODE(AH.Release_Lookup_Code, Null, Null,
1163                          AH.Last_Update_Date),
1164          DECODE(AH.Release_Lookup_Code, Null, Null,
1165                          AH.Last_Updated_By),
1166          g_fii_user_id Created_By,
1167          sysdate Creation_Date,
1168          g_fii_user_id Last_Updated_By,
1169          sysdate Last_Update_Date,
1170          g_fii_login_id Last_Update_Login
1171   FROM   AP_Holds_All AH,
1172          FII_AP_Invoice_B AI
1173   WHERE  AH.Invoice_ID = AI.Invoice_ID
1174   AND    AI.Cancel_Date IS NULL
1175   AND    AI.Invoice_Type NOT IN ('PREPAYMENT')
1176   AND    TRUNC(AH.Hold_Date) >= g_start_date
1177   AND    TRUNC(AH.Hold_Date) + 0 <= g_end_date + 0.99999;
1178 
1179   if g_debug_flag = 'Y' then
1180      FII_UTIL.put_line('Inserted '|| SQL%ROWCOUNT ||' records into FII_AP_INV_HOLDS_B');
1181      FII_UTIL.put_line('');
1182      FII_UTIL.stop_timer;
1183      FII_UTIL.print_timer('Duration');
1184   end if;
1185 
1186   COMMIT;
1187 
1188 
1189 EXCEPTION
1190    WHEN OTHERS THEN
1191       g_errbuf:=sqlerrm;
1192       g_retcode:= -1;
1193       g_exception_msg  := g_retcode || ':' || g_errbuf;
1194       FII_UTIL.put_line('Error occured while ' || g_state);
1195       FII_UTIL.put_line(g_exception_msg);
1196       RAISE;
1197 
1198 END;
1199 
1200 
1201 ------------------------------------------------------------------
1202 -- Procedure POPULATE_INV_BASE_SUM
1203 -- Purpose
1204 --   This POPULATE_INV_BASE_SUM routine inserts records into the
1205 --   FII AP Invoice Base summary table.
1206 ------------------------------------------------------------------
1207 
1208 PROCEDURE POPULATE_INV_BASE_SUM IS
1209 
1210   l_stmt  VARCHAR2(20000);
1211 
1212 BEGIN
1213 
1214   g_state := 'Deleting records from FII_AP_INVOICE_B that are already existing';
1215   /* For Initial Load we will truncate the data in the invoice base summary table
1216      and re-populate this table */
1217   TRUNCATE_TABLE('MLOG$_FII_AP_INVOICE_B');
1218   TRUNCATE_TABLE('FII_AP_INVOICE_B');
1219 
1220   g_state := 'Populating FII_AP_INVOICE_B FROM AP_INVOICES_ALL table';
1221   if g_debug_flag = 'Y' then
1222      FII_UTIL.put_line(g_state);
1223      FII_UTIL.start_timer;
1224      FII_UTIL.put_line('');
1225   end if;
1226 
1227   /* Enhancement 4227813: Manual invoice sources are defined by the profile option
1228      'FII: Manual Invoice Sources'.  Do dummy select in order to verify that the
1229      format of the profile option is valid.  Correct format is: 'Source1',..,'SourceN'*/
1230      g_manual_sources := upper(g_manual_sources);
1231      IF g_manual_sources IS NULL THEN g_manual_sources := ''''''; END IF;
1232      BEGIN
1233      execute immediate('SELECT 1 FROM (SELECT '' '' SOURCE FROM DUAL)
1234                         WHERE SOURCE IN (' || g_manual_sources || ')');
1235      EXCEPTION
1236         WHEN OTHERS THEN
1237         RAISE g_invalid_manual_source;
1238      END;
1239 
1240 
1241   /* Insert statement to insert the invoice records into the base summary
1242      table. We will insert the maximum due_date, maximum discount offered
1243      and the first hold date in this table. */
1244 
1245 
1246   INSERT /*+ append parallel(S) */ INTO FII_AP_INVOICE_B S
1247            (Org_ID,
1248             Supplier_ID,
1249             Invoice_ID,
1250             Invoice_Type,
1251             Invoice_Number,
1252             Invoice_Date,
1253             Invoice_Amount,
1254             Invoice_Currency_Code,
1255             Base_Currency_Code,
1256             Exchange_Date,
1257             Exchange_Rate,
1258             Exchange_Rate_Type,
1259             Entered_Date,
1260             Created_By,
1261             Payment_Currency_Code,
1262             Payment_Status_Flag,
1263             Payment_Cross_Rate,
1264             Fully_Paid_Date,
1265             Terms_ID,
1266             Source,
1267             E_Invoices_Flag,
1268             Cancel_Flag,
1269             Cancel_Date,
1270             Dist_Count,
1271             Base_Amount,
1272             Prim_Amount,
1273             Sec_Amount,
1274             Discount_Offered,
1275             Discount_Offered_B,
1276             Prim_Discount_Offered,
1277             Sec_Discount_Offered,
1278             Fully_Paid_Amount,
1279             Fully_Paid_Amount_B,
1280             Prim_Fully_Paid_Amount,
1281             Sec_Fully_Paid_Amount,
1282             Due_Date,
1283             Creation_Date,
1284             Last_Updated_By,
1285             Last_Update_Date,
1286             Last_Update_Login)
1287      SELECT /*+ leading(IB) parallel(IB) use_hash(PS,RATES,FRATES,AIP)
1288                  parallel(RATES) parallel(FRATES) */
1289             IB.Org_ID,
1290             IB.Supplier_ID,
1291             IB.Invoice_ID,
1292             IB.Invoice_Type,
1293             IB.Invoice_Number,
1294             IB.Invoice_Date,
1295             IB.Invoice_Amount,
1296             IB.Invoice_Currency_Code,
1297             IB.Base_Currency_Code,
1298             IB.Exchange_Date,
1299             IB.Exchange_Rate,
1300             IB.Exchange_Rate_Type,
1301             IB.Entered_Date,
1302             IB.Created_By,
1303             IB.Payment_Currency_Code,
1304             IB.Payment_Status_Flag,
1305             IB.Payment_Cross_Rate,
1306             DECODE(IB.Payment_Status_Flag, 'Y',
1307                    DECODE(AIP.Fully_Paid_Date, NULL, IB.Fully_Paid_Date,
1308                           DECODE(IB.Fully_Paid_Date, NULL, AIP.Fully_Paid_Date,
1309                                  GREATEST(AIP.Fully_Paid_Date, IB.Fully_Paid_Date))), NULL) Fully_Paid_Date,
1310             IB.Terms_ID,
1311             IB.Source,
1312             IB.E_Invoices_Flag,
1313             IB.Cancel_Flag,
1314             IB.Cancel_Date,
1315             IB.Dist_Count,
1316             ROUND((IB.Invoice_Amount * FRATES.Conversion_Rate)
1317                    /  Functional_MAU ) *  Functional_MAU  Base_Amount,
1318             ROUND(DECODE(IB.Invoice_Currency_Code, g_prim_currency, IB.Invoice_Amount,
1319                  ((IB.Invoice_Amount * FRATES.Conversion_Rate) * RATES.Prim_Conversion_Rate))
1320                    / g_primary_mau) * g_primary_mau Prim_Amount,
1321             ROUND(DECODE(IB.Invoice_Currency_Code, g_sec_currency, IB.Invoice_Amount,
1322                  ((IB.Invoice_Amount * FRATES.Conversion_Rate) * RATES.Sec_Conversion_Rate))
1323                    / g_secondary_mau) * g_secondary_mau Sec_Amount,
1324             PS.Discount_Amount_Available Discount_Offered,
1325             ROUND((PS.Discount_Amount_Available * FRATES.Conversion_Rate)
1326                   /  Functional_MAU ) * Functional_MAU  Discount_Offered_B,
1327             ROUND(DECODE(IB.Invoice_Currency_Code, g_prim_currency, PS.Discount_Amount_Available,
1328                  ((PS.Discount_Amount_Available * FRATES.Conversion_Rate)
1329                   * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau Prim_Discount_Offered,
1330             ROUND(DECODE(IB.Invoice_Currency_Code, g_sec_currency, PS.Discount_Amount_Available,
1331                  ((PS.Discount_Amount_Available * FRATES.Conversion_Rate)
1332                   * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau Sec_Discount_Offered,
1333             DECODE(IB.Payment_Status_Flag, 'Y',
1334                    NVL(AIP.Fully_Paid_Amount_IP,0) + NVL(IB.Fully_Paid_Amount_PP,0), NULL) Fully_Paid_Amount,
1335             ROUND((DECODE(IB.Payment_Status_Flag, 'Y',
1336                    NVL(AIP.Fully_Paid_Amount_IP,0) + NVL(IB.Fully_Paid_Amount_PP,0), NULL) * FRATES.Conversion_Rate)
1337                   / Functional_MAU) * Functional_MAU Fully_Paid_Amount_B,
1338             ROUND(DECODE(IB.Invoice_Currency_Code, g_prim_currency,
1339                          DECODE(IB.Payment_Status_Flag, 'Y',
1340                          NVL(AIP.Fully_Paid_Amount_IP,0) + NVL(IB.Fully_Paid_Amount_PP,0), NULL),
1341                        ((DECODE(IB.Payment_Status_Flag, 'Y',
1342                          NVL(AIP.Fully_Paid_Amount_IP,0) + NVL(IB.Fully_Paid_Amount_PP,0), NULL) * FRATES.Conversion_Rate)
1343                         * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau Prim_Fully_Paid_Amount,
1344             ROUND(DECODE(IB.Invoice_Currency_Code, g_sec_currency,
1345                          DECODE(IB.Payment_Status_Flag, 'Y',
1346                          NVL(AIP.Fully_Paid_Amount_IP,0) + NVL(IB.Fully_Paid_Amount_PP,0), NULL),
1347                        ((DECODE(IB.Payment_Status_Flag, 'Y',
1348                          NVL(AIP.Fully_Paid_Amount_IP,0) + NVL(IB.Fully_Paid_Amount_PP,0), NULL) * FRATES.Conversion_Rate)
1349                         * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau Sec_Fully_Paid_Amount,
1350             PS.Due_Date,
1351             IB.Creation_Date,
1352             IB.Last_Updated_By,
1353             IB.Last_Update_Date,
1354             IB.Last_Update_Login
1355      FROM
1356            (SELECT /*+ no_merge ordered use_hash(AI,AID) parallel(IB) parallel(AI) parallel(AID) */
1357                    AI.Org_ID,
1358                    AI.Vendor_ID Supplier_id,
1359                    AI.Invoice_ID,
1360                    AI.Invoice_Type_Lookup_Code Invoice_type,
1361                    AI.Invoice_Num Invoice_Number,
1362                    TRUNC(AI.Invoice_Date) Invoice_Date,
1363                    AI.Invoice_Amount,
1364                    AI.Invoice_Currency_Code,
1365                    ASP.Base_Currency_Code,
1366                    TRUNC(NVL(AI.Exchange_Date, AI.Invoice_Date)) Exchange_Date,
1367                    AI.Exchange_Rate,
1368                    NVL(AI.Exchange_Rate_Type, 'No Rate Type') Exchange_Rate_Type,
1369                    TRUNC(AI.Creation_Date) Entered_Date ,
1370                    AI.Created_By ,
1371                    AI.Payment_Currency_Code,
1372                    AI.Payment_Status_Flag,
1373                    AI.Payment_Cross_Rate,
1374                    Decode(AI.Payment_Status_Flag, 'Y',
1375                                TRUNC(MAX(CASE WHEN AID.Line_Type_Lookup_Code = 'PREPAY'
1376                                               --AND  AID.Reversal_Flag IS NULL
1377                                               AND NVL(AID.Reversal_Flag,'N') = 'N'
1378                                               AND  (AIL.Invoice_Includes_Prepay_Flag IS NULL OR AIL.Invoice_Includes_Prepay_Flag = 'N')
1379                                               THEN AID.Creation_Date
1380                                               ELSE NULL END)), NULL) Fully_Paid_Date,
1381                    Decode(AI.Payment_Status_Flag, 'Y',
1382                                SUM(CASE WHEN AID.Line_Type_Lookup_Code = 'PREPAY'
1383                                               --AND  AID.Reversal_Flag IS NULL
1384                                               AND NVL(AID.Reversal_Flag,'N') = 'N'
1385                                               AND  (AIL.Invoice_Includes_Prepay_Flag IS NULL OR AIL.Invoice_Includes_Prepay_Flag = 'N')
1386                                               THEN -1 * AID.Amount
1387                                               ELSE 0 END), 0) Fully_Paid_Amount_PP,
1388                    AI.Terms_ID,
1389                    AI.Source,
1390                    CASE WHEN g_manual_sources like '%''' || to_char(upper(AI.Source)) || '''%'
1391                         --upper(AI.Source) IN (g_manual_sources)
1392                         THEN 'N' ELSE 'Y' END E_Invoices_Flag,
1393                    Decode(AI.Cancelled_Date,Null,'N','Y') Cancel_Flag,
1394                    AI.Cancelled_Date Cancel_Date,
1395                    Count(Distinct AID.Invoice_Distribution_ID) Dist_Count,
1396                    sysdate Creation_Date,
1397                    g_fii_user_id Last_Updated_By,
1398                    sysdate Last_Update_Date,
1399                    g_fii_login_id Last_Update_Login
1400            FROM    AP_System_Parameters_All ASP,
1401                    AP_Invoices_All AI,
1402                    AP_Invoice_Distributions_All AID,
1403                    AP_Invoice_Lines_All AIL
1404            WHERE   AI.Invoice_ID = AIL.Invoice_ID (+)
1405            AND     AIL.Invoice_ID = AID.Invoice_ID (+)
1406            AND     AIL.Line_Number = AID.Invoice_Line_Number (+)
1407            AND     AI.Org_ID = ASP.Org_ID
1408            AND     AI.Invoice_Type_Lookup_Code NOT IN ('EXPENSE REPORT')
1409            AND     AI.Invoice_Amount <> 0
1410            AND     TRUNC(AI.Creation_Date) >= g_start_date
1411            AND     TRUNC(AI.Creation_Date) + 0 <= g_end_date + 0.99999
1412            GROUP BY AI.Org_ID,
1413                     AI.Vendor_ID,
1414                     AI.Invoice_ID,
1415                     AI.Invoice_Type_Lookup_Code,
1416                     AI.Invoice_Num,
1417                     AI.Invoice_Date,
1418                     AI.Invoice_Amount,
1419                     ASP.Base_Currency_Code,
1420                     AI.Base_Amount,
1421                     AI.Invoice_Currency_Code,
1422                     AI.Payment_Currency_Code,
1423                     AI.Exchange_Date,
1424                     AI.Exchange_Rate,
1425                     AI.Exchange_Rate_Type,
1426                     AI.Creation_Date,
1427                     AI.Created_By,
1428                     AI.Payment_Status_Flag,
1429                     AI.Payment_Cross_Rate,
1430                     AI.Terms_ID,
1431                     AI.Source,
1432                     AI.Cancelled_Date) IB,
1433            (SELECT /*+ no_merge parallel(PS) */
1434                    PS.Invoice_ID,
1435                    SUM(NVL(PS.Discount_Amount_Available,0)) Discount_Amount_Available,
1436                    TRUNC(MIN(PS.Due_Date)) Due_Date
1437             FROM   AP_Payment_Schedules_all PS
1438             GROUP  BY PS.Invoice_ID ) PS,
1439            (SELECT /*+ no_merge parallel(AIP) */
1440                    AIP.Invoice_id Invoice_ID,
1441                    TRUNC(MAX(AIP.Creation_Date)) Fully_Paid_Date,
1442                    SUM(AIP.Amount + NVL(AIP.Discount_Taken,0)) Fully_Paid_Amount_IP
1443             FROM   AP_Invoice_Payments_All AIP
1444             GROUP  BY AIP.Invoice_ID ) AIP,
1445             FII_AP_PS_Rates_Temp RATES,
1446             FII_AP_Func_Rates_Temp FRATES
1447      WHERE  IB.Invoice_ID = PS.Invoice_ID
1448      AND    IB.Invoice_ID = AIP.Invoice_ID (+)
1449      AND    IB.Invoice_Date = RATES.Trx_Date
1450      AND    IB.Base_Currency_Code = RATES.Functional_Currency
1451      AND    IB.Payment_Currency_Code = FRATES.From_Currency
1452      AND    IB.Exchange_Date = FRATES.Trx_Date
1453      AND    IB.Exchange_Rate_Type = FRATES.Conversion_Type
1454      AND    DECODE(IB.Exchange_Rate_Type,'User', IB.Exchange_Rate,1) =
1455                  DECODE(FRATES.Conversion_Type,'User', FRATES.Conversion_Rate,1)
1456      AND    IB.Base_Currency_Code = FRATES.To_Currency;
1457 
1458 
1459   if g_debug_flag = 'Y' then
1460      FII_UTIL.put_line('Inserted ' ||SQL%ROWCOUNT|| ' records into FII_AP_INVOICE_B');
1461      FII_UTIL.stop_timer;
1462      FII_UTIL.print_timer('Duration');
1463      FII_UTIL.put_line('');
1464   end if;
1465 
1466   COMMIT;
1467 
1468   FND_STATS.GATHER_TABLE_STATS(g_fii_schema,'FII_AP_INVOICE_B');
1469   EXECUTE IMMEDIATE 'ALTER SESSION ENABLE PARALLEL DML';
1470 
1471 
1472 EXCEPTION
1473    WHEN g_invalid_manual_source THEN
1474       g_retcode := -1;
1475       g_errbuf := fnd_message.get_string('FII', 'FII_AP_INVALID_MANUAL_SOURCE');
1476       RAISE;
1477    WHEN OTHERS THEN
1478       g_errbuf:=sqlerrm;
1479       g_retcode:= -1;
1480       g_exception_msg  := g_retcode || ':' || g_errbuf;
1481       FII_UTIL.put_line('Error occured while ' || g_state);
1482       FII_UTIL.put_line(g_exception_msg);
1483       RAISE;
1484 
1485 END;
1486 
1487 
1488 
1489 ------------------------------------------------------------------
1490 -- Procedure POPULATE_PS_PAYMENT_ACTION
1491 -- Purpose
1492 --   This POPULATE_PS_PAYMENT_ACTION routine inserts records into the
1493 --   FII AP Payment Schedule summary table all the payment and
1494 --   prepayment actions.
1495 ------------------------------------------------------------------
1496 
1497 PROCEDURE POPULATE_PS_PAYMENT_ACTION IS
1498 
1499 BEGIN
1500 
1501   g_state := 'Inside the procedure POPULATE_PS_PAYMENT_ACTION';
1502   if g_debug_flag = 'Y' then
1503      FII_UTIL.put_line('');
1504      FII_UTIL.put_line(g_state);
1505   end if;
1506 
1507   g_state := 'Populating Payment Creation records';
1508   if g_debug_flag = 'Y' then
1509      FII_UTIL.put_line(g_state);
1510      FII_UTIL.put_timestamp('Start Timestamp');
1511      FII_UTIL.start_timer;
1512      FII_UTIL.put_line('');
1513   end if;
1514 
1515   /* Insert statement to insert all the payment information into the summary
1516      table including the payments made for a prepayment invoice.
1517      We will record the creation date as the action date and not the check date
1518      for a payment. If we record the check date as the action date then the
1519      action date for a voided payment would be the same as the payment creation
1520      and the report would show wrong results for past periods.  */
1521 
1522 
1523   INSERT INTO FII_AP_PAY_SCHED_B b
1524         (Time_ID,
1525          Period_Type_ID,
1526          Action_Date,
1527          Action,
1528          Update_Sequence,
1529          Org_ID,
1530          Supplier_ID,
1531          Invoice_ID,
1532          Base_Currency_Code,
1533          Trx_Date,
1534          Payment_Num,
1535          Due_Date,
1536          Created_By,
1537          Amount_Remaining,
1538          Past_Due_Amount,
1539          Discount_Available,
1540          Discount_Taken,
1541          Discount_Lost,
1542          Payment_Amount,
1543          On_Time_Payment_Amt,
1544          Late_Payment_Amt,
1545          No_Days_Late,
1546          Due_Bucket1,
1547          Due_Bucket2,
1548          Due_Bucket3,
1549          Past_Due_Bucket1,
1550          Past_Due_Bucket2,
1551          Past_Due_Bucket3,
1552          Amount_Remaining_B,
1553          Past_Due_Amount_B,
1554          Discount_Available_B,
1555          Discount_Taken_B,
1556          Discount_Lost_B,
1557          Payment_Amount_B,
1558          On_Time_Payment_Amt_B,
1559          Late_Payment_Amt_B,
1560          Due_Bucket1_B,
1561          Due_Bucket2_B,
1562          Due_Bucket3_B,
1563          Past_Due_Bucket1_B,
1564          Past_Due_Bucket2_B,
1565          Past_Due_Bucket3_B,
1566          Prim_Amount_Remaining,
1567          Prim_Past_Due_Amount,
1568          Prim_Discount_Available,
1569          Prim_Discount_Taken,
1570          Prim_Discount_Lost,
1571          Prim_Payment_Amount,
1572          Prim_On_time_Payment_Amt,
1573          Prim_Late_Payment_Amt,
1574          Prim_Due_Bucket1,
1575          Prim_Due_Bucket2,
1576          Prim_Due_Bucket3,
1577          Prim_Past_Due_Bucket1,
1578          Prim_Past_Due_Bucket2,
1579          Prim_Past_Due_Bucket3,
1580          Sec_Amount_Remaining,
1581          Sec_Past_Due_Amount,
1582          Sec_Discount_Available,
1583          Sec_Discount_Taken,
1584          Sec_Discount_Lost,
1585          Sec_Payment_Amount,
1586          Sec_On_time_Payment_Amt,
1587          Sec_Late_Payment_Amt,
1588          Sec_Due_Bucket1,
1589          Sec_Due_Bucket2,
1590          Sec_Due_Bucket3,
1591          Sec_Past_Due_Bucket1,
1592          Sec_Past_Due_Bucket2,
1593          Sec_Past_Due_Bucket3,
1594          Check_ID,
1595          Check_Date,
1596          Payment_Method,
1597          Creation_Date,
1598          Last_Updated_By,
1599          Last_Update_Date,
1600          Last_Update_Login,
1601          Inv_Pymt_Flag,
1602          Unique_ID)
1603  SELECT /*+ MERGE(PSUM) use_nl(frates,rates) */
1604 	 TO_NUMBER(TO_CHAR(Action_Date,'J')) Time_ID,
1605          1 Period_Type_ID,
1606          Action_Date,
1607          Action,
1608          g_seq_id Update_Sequence,
1609          Org_ID,
1610          Supplier_ID,
1611          Invoice_ID,
1612          Base_Currency_Code,
1613          Invoice_Date,
1614          Payment_Num,
1615          Due_Date,
1616          Created_By,
1617          Amount_Remaining,
1618          Past_Due_Amount,
1619          Discount_Available,
1620          Discount_Taken,
1621          Discount_Lost,
1622          Payment_Amount,
1623          On_Time_Payment_Amt,
1624          Late_Payment_Amt,
1625          No_Days_Late,
1626          Due_Bucket1,
1627          Due_Bucket2,
1628          Due_Bucket3,
1629          Past_Due_Bucket1,
1630          Past_Due_Bucket2,
1631          Past_Due_Bucket3,
1632          ROUND((Amount_Remaining * Conversion_Rate) / Functional_MAU) * Functional_MAU Amount_Remaining_B,
1633          ROUND((Past_Due_Amount * Conversion_Rate) / Functional_MAU) * Functional_MAU Past_Due_Amount_B,
1634          ROUND((Discount_Available * Conversion_Rate) / Functional_MAU) * Functional_MAU Discount_Available_B,
1635          ROUND((Discount_Taken * Conversion_Rate) / Functional_MAU) * Functional_MAU Discount_Taken_B,
1636          ROUND((Discount_Lost * Conversion_Rate) / Functional_MAU) * Functional_MAU Discount_Lost_B,
1637          ROUND((Payment_Amount * Conversion_Rate) / Functional_MAU) * Functional_MAU Payment_Amount_B,
1638          ROUND((On_Time_Payment_Amt * Conversion_Rate) / Functional_MAU) * Functional_MAU On_Time_Payment_Amt_B,
1639          ROUND((Late_Payment_Amt * Conversion_Rate) / Functional_MAU) * Functional_MAU Last_Payment_Amt_B,
1640          ROUND((Due_Bucket1 * Conversion_Rate) / Functional_MAU) * Functional_MAU Due_Bucket1_B,
1641          ROUND((Due_Bucket2 * Conversion_Rate) / Functional_MAU) * Functional_MAU Due_Bucket2_B,
1642          ROUND((Due_Bucket3 * Conversion_Rate) / Functional_MAU) * Functional_MAU Due_Bucket3_B,
1643          ROUND((Past_Due_Bucket1 * Conversion_Rate) / Functional_MAU) * Functional_MAU Past_Due_Bucket1_B,
1644          ROUND((Past_Due_Bucket2 * Conversion_Rate) / Functional_MAU) * Functional_MAU Past_Due_Bucket2_B,
1645          ROUND((Past_Due_Bucket3 * Conversion_Rate) / Functional_MAU) * Functional_MAU Past_Due_Bucket3_B,
1646          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Amount_Remaining,
1647                           ((Amount_Remaining * Conversion_Rate) * RATES.Prim_Conversion_Rate))
1648                           / g_primary_mau) * g_primary_mau Prim_Amount_Remaining,
1649          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Past_Due_Amount,
1650                           ((Past_Due_Amount * Conversion_Rate) * RATES.Prim_Conversion_Rate))
1651                           / g_primary_mau) * g_primary_mau Prim_Past_Due_Amount,
1652          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Discount_Available,
1653                           ((Discount_Available * Conversion_Rate) * RATES.Prim_Conversion_Rate))
1654                           / g_primary_mau) * g_primary_mau Prim_Discount_Available,
1655          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Discount_Taken,
1656                           ((Discount_Taken * Conversion_Rate) * RATES.Prim_Conversion_Rate))
1657                           / g_primary_mau) * g_primary_mau Prim_Discount_Taken,
1658          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Discount_Lost,
1659                           ((Discount_Lost * Conversion_Rate) * RATES.Prim_Conversion_Rate))
1660                           / g_primary_mau) * g_primary_mau Prim_Discount_Lost,
1661          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Payment_Amount,
1662                           ((Payment_Amount * Conversion_Rate) * RATES.Prim_Conversion_Rate))
1663                           / g_primary_mau) * g_primary_mau Prim_Payment_Amount,
1664          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, On_Time_Payment_Amt,
1665                           ((On_Time_Payment_Amt * Conversion_Rate) * RATES.Prim_Conversion_Rate))
1666                           / g_primary_mau) * g_primary_mau Prim_On_Time_Payment_Amt,
1667          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Late_Payment_Amt,
1668                           ((Late_Payment_Amt * Conversion_Rate) * RATES.Prim_Conversion_Rate))
1669                           / g_primary_mau) * g_primary_mau Prim_Late_Payment_Amt,
1670          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Due_Bucket1,
1671                           ((Due_Bucket1 * Conversion_Rate) * RATES.Prim_Conversion_Rate))
1672                           / g_primary_mau) * g_primary_mau Prim_Due_Bucket1,
1673          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Due_Bucket2,
1674                           ((Due_Bucket2 * Conversion_Rate) * RATES.Prim_Conversion_Rate))
1675                           / g_primary_mau) * g_primary_mau Prim_Due_Bucket2,
1676          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Due_Bucket3,
1677                           ((Due_Bucket3 * Conversion_Rate) * RATES.Prim_Conversion_Rate))
1678                           / g_primary_mau) * g_primary_mau Prim_Due_Bucket3,
1679          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Past_Due_Bucket1,
1680                           ((Past_Due_Bucket1 * Conversion_Rate) * RATES.Prim_Conversion_Rate))
1681                           / g_primary_mau) * g_primary_mau Prim_Past_Due_Bucket1,
1682          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Past_Due_Bucket2,
1683                           ((Past_Due_Bucket2 * Conversion_Rate) * RATES.Prim_Conversion_Rate))
1684                           / g_primary_mau) * g_primary_mau Prim_Past_Due_Bucket2,
1685          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Past_Due_Bucket3,
1686                           ((Past_Due_Bucket3 * Conversion_Rate) * RATES.Prim_Conversion_Rate))
1687                           / g_primary_mau) * g_primary_mau Prim_Past_Due_Bucket3,
1688          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Amount_Remaining,
1689                           ((Amount_Remaining * Conversion_Rate) * RATES.Sec_Conversion_Rate))
1690                           / g_secondary_mau) * g_secondary_mau Sec_Amount_Remaining,
1691          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Past_Due_Amount,
1692                           ((Past_Due_Amount * Conversion_Rate) * RATES.Sec_Conversion_Rate))
1693                           / g_secondary_mau) * g_secondary_mau Sec_Past_Due_Amount,
1694          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Discount_Available,
1695                           ((Discount_Available * Conversion_Rate) * RATES.Sec_Conversion_Rate))
1696                           / g_secondary_mau) * g_secondary_mau Sec_Discount_Available,
1697          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Discount_Taken,
1698                           ((Discount_Taken * Conversion_Rate) * RATES.Sec_Conversion_Rate))
1699                           / g_secondary_mau) * g_secondary_mau Sec_Discount_Taken,
1700          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Discount_Lost,
1701                           ((Discount_Lost * Conversion_Rate) * RATES.Sec_Conversion_Rate))
1702                           / g_secondary_mau) * g_secondary_mau Sec_Discount_Lost,
1703          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Payment_Amount,
1704                           ((Payment_Amount * Conversion_Rate) * RATES.Sec_Conversion_Rate))
1705                           / g_secondary_mau) * g_secondary_mau Sec_Payment_Amount,
1706          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, On_Time_Payment_Amt,
1707                           ((On_Time_Payment_Amt * Conversion_Rate) * RATES.Sec_Conversion_Rate))
1708                           / g_secondary_mau) * g_secondary_mau Sec_On_Time_Payment_Amt,
1709          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Late_Payment_Amt,
1710                           ((Late_Payment_Amt * Conversion_Rate) * RATES.Sec_Conversion_Rate))
1711                           / g_secondary_mau) * g_secondary_mau Sec_Late_Payment_Amt,
1712          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Due_Bucket1,
1713                           ((Due_Bucket1 * Conversion_Rate) * RATES.Sec_Conversion_Rate))
1714                           / g_secondary_mau) * g_secondary_mau Sec_Due_Bucket1,
1715          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Due_Bucket2,
1716                           ((Due_Bucket2 * Conversion_Rate) * RATES.Sec_Conversion_Rate))
1717                           / g_secondary_mau) * g_secondary_mau Sec_Due_Bucket2,
1718          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Due_Bucket3,
1719                           ((Due_Bucket3 * Conversion_Rate) * RATES.Sec_Conversion_Rate))
1720                           / g_secondary_mau) * g_secondary_mau Sec_Due_Bucket3,
1721          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Past_Due_Bucket1,
1722                           ((Past_Due_Bucket1 * Conversion_Rate) * RATES.Sec_Conversion_Rate))
1723                           / g_secondary_mau) * g_secondary_mau Sec_Past_Due_Bucket1,
1724          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Past_Due_Bucket2,
1725                           ((Past_Due_Bucket2 * Conversion_Rate) * RATES.Sec_Conversion_Rate))
1726                           / g_secondary_mau) * g_secondary_mau Sec_Past_Due_Bucket2,
1727          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Past_Due_Bucket3,
1728                           ((Past_Due_Bucket3 * Conversion_Rate) * RATES.Sec_Conversion_Rate))
1729                           / g_secondary_mau) * g_secondary_mau Sec_Past_Due_Bucket3,
1730          Check_ID,
1731          Check_Date,
1732          Payment_Method,
1733          sysdate Creation_Date,
1734          g_fii_user_id Last_Updated_By,
1735          sysdate Last_Update_Date,
1736          g_fii_login_id Last_Update_Login,
1737          'Y' Inv_Pymt_Flag,
1738          Invoice_Payment_ID Unique_ID
1739   FROM
1740         (SELECT /*+ leading(aip) merge(aip) use_nl(ai) use_nl(PS) use_nl(AIP) use_nl(apc) */
1741                 TRUNC(AIP.Creation_Date) Action_Date,
1742 		DECODE(AI.Invoice_Type, 'PREPAYMENT', 'PREPAYMENT', 'PAYMENT') Action,
1743                 AI.Org_ID Org_ID,
1744                 AI.Supplier_ID Supplier_ID,
1745                 AI.Invoice_ID Invoice_ID,
1746                 AI.Invoice_Currency_Code Invoice_Currency_Code,
1747                 AI.Base_Currency_Code Base_Currency_Code,
1748                 AI.Invoice_Date Invoice_Date,
1749                 AI.Payment_Currency_Code Payment_Currency_Code,
1750                 AI.Exchange_Rate Exchange_Rate,
1751                 AI.Exchange_Date Exchange_Date,
1752                 AI.Exchange_Rate_Type Exchange_Rate_Type,
1753                 PS.Payment_Num Payment_Num,
1754                 TRUNC(PS.Due_Date) Due_Date,
1755                 AIP.Created_By Created_By,
1756                 -1 * (AIP.Amount + NVL(AIP.Discount_Taken,0)) Amount_Remaining,
1757                 DECODE(AI.Invoice_Type, 'PREPAYMENT', 0,
1758                    DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(AIP.Creation_Date)), -1,
1759                          -1 * (AIP.Amount + NVL(AIP.Discount_Taken,0)), 0)) Past_Due_Amount,
1760                 -1 * NVL(AIP.Discount_Taken,0) Discount_Available,
1761                 NVL(AIP.Discount_Taken,0) Discount_Taken,
1762                 0 Discount_Lost,
1763                 AIP.Amount Payment_Amount,
1764                 DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(AIP.Creation_Date)), -1, 0,
1765                                      AIP.Amount) On_Time_Payment_Amt,
1766                 DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(AIP.Creation_Date)), -1,
1767                                      AIP.Amount, 0) Late_Payment_Amt,
1768                 DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(AIP.Creation_Date)), -1,
1769                              (TRUNC(AIP.Creation_Date) - TRUNC(PS.Due_Date)), 0) No_Days_Late,
1770                CASE
1771                   WHEN (AI.Invoice_Type <> 'PREPAYMENT')
1772                    AND (TRUNC(PS.Due_Date) - TRUNC(AIP.Creation_Date)) >=  g_due_bucket1
1773                         THEN  -1 * (AIP.Amount + NVL(AIP.Discount_Taken,0))
1774                   ELSE  0
1775                 END Due_Bucket1,
1776                 CASE
1777                   WHEN (AI.Invoice_Type <> 'PREPAYMENT')
1778                    AND (TRUNC(PS.Due_Date) - TRUNC(AIP.Creation_Date)) <= g_due_bucket2
1779                    AND (TRUNC(PS.Due_Date) - TRUNC(AIP.Creation_Date)) >  g_due_bucket3
1780                         THEN  -1 * (AIP.Amount + NVL(AIP.Discount_Taken,0))
1781                   ELSE  0
1782                 END Due_Bucket2,
1783                 CASE
1784                   WHEN (AI.Invoice_Type <> 'PREPAYMENT')
1785                    AND (TRUNC(PS.Due_Date) - TRUNC(AIP.Creation_Date)) <= g_due_bucket3
1786                    AND (TRUNC(PS.Due_Date) - TRUNC(AIP.Creation_Date)) >= 0
1787                         THEN  -1 * (AIP.Amount + NVL(AIP.Discount_Taken,0))
1788                   ELSE  0
1789                 END Due_Bucket3,
1790                 CASE
1791                   WHEN (AI.Invoice_Type <> 'PREPAYMENT')
1792                    AND (TRUNC(AIP.Creation_Date) - TRUNC(PS.Due_Date)) >= g_past_bucket1
1793                         THEN  -1 * (AIP.Amount + NVL(AIP.Discount_Taken,0))
1794                   ELSE  0
1795                 END Past_Due_Bucket1,
1796                 CASE
1797                   WHEN (AI.Invoice_Type <> 'PREPAYMENT')
1798                    AND (TRUNC(AIP.Creation_Date) - TRUNC(PS.Due_Date)) <= g_past_bucket2
1799                    AND (TRUNC(AIP.Creation_Date) - TRUNC(PS.Due_Date)) >  g_past_bucket3
1800                         THEN  -1 * (AIP.Amount + NVL(AIP.Discount_Taken,0))
1801                   ELSE  0
1802                 END Past_Due_Bucket2,
1803                 CASE
1804                   WHEN (AI.Invoice_Type <> 'PREPAYMENT')
1805                    AND (TRUNC(AIP.Creation_Date) - TRUNC(PS.Due_Date)) <= g_past_bucket3
1806                    AND (TRUNC(AIP.Creation_Date) - TRUNC(PS.Due_Date)) >  0
1807                         THEN  -1 * (AIP.Amount + NVL(AIP.Discount_Taken,0))
1808                   ELSE  0
1809                 END Past_Due_Bucket3,
1810                 AIP.Check_ID Check_ID,
1811                 AC.Check_Date,
1812                 DECODE(IBY_SYS_PROF_B.Processing_Type,NULL,DECODE(AC.Payment_Method_Lookup_Code, 'EFT', 'E', 'WIRE', 'E', 'M')
1813                ,DECODE(IBY_SYS_PROF_B.Processing_Type, 'ELECTRONIC', 'E', 'M')) PAYMENT_METHOD,
1814                 AIP.Invoice_Payment_ID
1815          FROM   FII_AP_Invoice_B AI,
1816 		AP_Payment_Schedules_All PS,
1817                (SELECT /*+ leading(aip_pp) use_nl(aip_pp,pp) */ AIP_PP.Invoice_ID,
1818                        AIP_PP.Payment_Num,
1819                        AIP_PP.Creation_Date,
1820                        AIP_PP.Created_By,
1821                        AIP_PP.Amount,
1822                        AIP_PP.Discount_Taken,
1823                        AIP_PP.Check_ID,
1824                        AIP_PP.WH_Tax_Amount + NVL(SUM(CASE WHEN PP.Creation_Date IS NOT NULL
1825                                                            AND  PP.Creation_Date <= AIP_PP.Creation_Date
1826                                                            THEN PP.Prepay_Amount ELSE 0 END),0)  Prepay_WH_Tax_Amount,
1827                        AIP_PP.Invoice_Payment_ID
1828                 FROM
1829                       (SELECT /*+ use_nl(aip_wh,temp) */ AIP_WH.Invoice_ID,
1830                               AIP_WH.Payment_Num,
1831                               AIP_WH.Creation_Date,
1832                               AIP_WH.Created_By,
1833                               AIP_WH.Amount,
1834                               AIP_WH.Discount_Taken,
1835                               AIP_WH.Check_ID,
1836                               NVL(SUM(CASE WHEN TEMP.Creation_Date IS NOT NULL
1837                                            AND  TEMP.Creation_Date <= TRUNC(AIP_WH.Creation_Date)
1838                                            THEN TEMP.WH_Tax_Amount ELSE 0 END),0) WH_Tax_Amount,
1839                               AIP_WH.Invoice_Payment_ID
1840                        FROM AP_Invoice_Payments_All AIP_WH,
1841                             FII_AP_WH_Tax_T TEMP
1842                        WHERE AIP_WH.Invoice_ID BETWEEN g_start_range and g_end_range
1843                        AND   AIP_WH.Invoice_ID = TEMP.Invoice_ID (+)
1844                        AND   AIP_WH.Payment_Num = TEMP.Payment_Num (+)
1845                        GROUP BY AIP_WH.Invoice_ID,
1846                                 AIP_WH.Payment_Num,
1847                                 AIP_WH.Creation_Date,
1848                                 AIP_WH.Created_By,
1849                                 AIP_WH.Amount,
1850                                 AIP_WH.Discount_Taken,
1851                                 AIP_WH.Check_ID,
1852                                 AIP_WH.Invoice_Payment_ID) AIP_PP,
1853                        FII_AP_Prepay_T PP
1854                 WHERE AIP_PP.Invoice_ID = PP.Invoice_ID (+)
1855                 AND   AIP_PP.Payment_Num = PP.Payment_Num (+)
1856                 GROUP BY AIP_PP.Invoice_ID,
1857                          AIP_PP.Payment_Num,
1858                          AIP_PP.Creation_Date,
1859                          AIP_PP.Created_By,
1860                          AIP_PP.Amount,
1861                          AIP_PP.Discount_Taken,
1862                          AIP_PP.Check_ID,
1863                          AIP_PP.WH_Tax_Amount,
1864                          AIP_PP.Invoice_Payment_ID) AIP,
1865                 FII_AP_PAY_CHK_STG APC,
1866  								AP_Checks_All AC,
1867                 IBY_SYS_PMT_PROFILES_B IBY_SYS_PROF_B,--IBY CHANGE
1868                 IBY_ACCT_PMT_PROFILES_B IBY_ACCT_PROF_B--IBY CHANGE
1869          WHERE  AI.Invoice_ID = PS.Invoice_ID
1870          AND    AI.Cancel_Date IS NULL
1871          AND    AIP.Invoice_ID  = PS.Invoice_ID
1872          AND    AIP.Payment_Num = PS.Payment_Num
1873          AND    AIP.Check_ID    = AC.Check_ID
1874          AND    APC.Invoice_ID  = PS.Invoice_ID
1875          AND    APC.Payment_Num = PS.Payment_Num
1876          AND    AC.Payment_Profile_ID = IBY_ACCT_PROF_B.Payment_Profile_ID(+)--IBY CHANGE
1877          AND    IBY_ACCT_PROF_B.system_profile_code = IBY_SYS_PROF_B.system_profile_code(+)--IBY CHANGE
1878          AND    APC.Invp_Creation_Date <= TRUNC(AIP.Creation_Date)
1879          AND    AC.Void_Date   IS NULL
1880          HAVING SUM(APC.Payment_Amount) + AIP.Prepay_WH_Tax_Amount <> PS.Gross_Amount
1881          GROUP  BY AI.Org_ID, AI.Supplier_ID,
1882                    AI.Invoice_ID,
1883                    AI.Invoice_Currency_Code,
1884                    AI.Base_Currency_Code,
1885                    AI.Invoice_Date,
1886                    AI.Invoice_Type,
1887                    AI.Payment_Currency_Code,
1888                    AI.Exchange_Rate,
1889                    AI.Exchange_Date,
1890                    AI.Exchange_Rate_Type,
1891                    PS.Payment_Num,
1892                    PS.Due_Date,
1893                    AIP.Amount,
1894                    AIP.Created_By,
1895                    AIP.Check_ID,
1896                    AC.Check_Date,
1897                    IBY_SYS_PROF_B.Processing_Type,
1898                    AC.Payment_Method_Lookup_Code,
1899                   -- AC.PAYMENT_PROFILE_ID,
1900                    AIP.Creation_Date,
1901                    NVL(AIP.Discount_Taken,0),
1902                    PS.Gross_Amount,
1903                    AIP.Prepay_WH_Tax_Amount,
1904                    AIP.Invoice_Payment_ID) PSUM,
1905          FII_AP_PS_Rates_Temp   RATES,
1906          FII_AP_Func_Rates_Temp FRATES
1907   WHERE  FRATES.To_Currency   = PSUM.Base_Currency_Code
1908   AND    FRATES.From_Currency = PSUM.Payment_Currency_Code
1909   AND    FRATES.Trx_Date      = PSUM.Exchange_Date
1910   AND    DECODE(PSUM.Exchange_Rate_Type,'User', PSUM.Exchange_Rate,1) =
1911                  DECODE(FRATES.Conversion_Type,'User', FRATES.Conversion_Rate,1)
1912   AND    FRATES.Conversion_Type    = PSUM.Exchange_Rate_Type
1913   AND    RATES.Functional_Currency = PSUM.Base_Currency_Code
1914   AND    RATES.Trx_Date            = PSUM.Invoice_Date;
1915 
1916 
1917   if g_debug_flag = 'Y' then
1918      FII_UTIL.put_line('Inserted ' ||SQL%ROWCOUNT|| ' Payment records into FII_AP_PAY_SCHED_B');
1919      FII_UTIL.put_timestamp('End Timestamp');
1920      FII_UTIL.stop_timer;
1921      FII_UTIL.print_timer('Duration');
1922      FII_UTIL.put_line('');
1923   end if;
1924 
1925   commit;
1926 
1927 
1928 
1929 
1930   g_state := 'Populating Payment Creation records into temp table';
1931   if g_debug_flag = 'Y' then
1932      FII_UTIL.put_line(g_state);
1933      FII_UTIL.put_timestamp('Start Timestamp');
1934      FII_UTIL.start_timer;
1935      FII_UTIL.put_line('');
1936   end if;
1937 
1938 
1939   /* We will first insert the last payment of a payment schedule into a temp table
1940      because we want to adjust the discount available and discount lost for the last
1941      payment as the discounts can be taken after the discount dates and also more
1942      discounts can be taken. This adjustment will ensure that the sum of the
1943      discount available will be zero and the discount lost amount will be
1944      discount available - taken.
1945 
1946      Since we cannot insert and select from the same table at the same time we will
1947      select from the pay sched sum table and insert into the temp table  */
1948 
1949   INSERT INTO FII_AP_PAY_SCHED_TEMP
1950         (Time_ID,
1951          Period_Type_ID,
1952          Action_Date,
1953          Action,
1954          Update_Sequence,
1955          Org_ID,
1956          Supplier_ID,
1957          Invoice_ID,
1958          Base_Currency_Code,
1959          Trx_Date,
1960          Payment_Num,
1961          Due_Date,
1962          Created_By,
1963          Amount_Remaining,
1964          Past_Due_Amount,
1965          Discount_Available,
1966          Discount_Taken,
1967          Discount_Lost,
1968          Payment_Amount,
1969          On_Time_Payment_Amt,
1970          Late_Payment_Amt,
1971          No_Days_Late,
1972          Due_Bucket1,
1973          Due_Bucket2,
1974          Due_Bucket3,
1975          Past_Due_Bucket1,
1976          Past_Due_Bucket2,
1977          Past_Due_Bucket3,
1978          Fully_Paid_Date,
1979          Check_ID,
1980          Check_Date,
1981          Payment_Method,
1982          Inv_Pymt_Flag,
1983          Unique_ID)
1984   SELECT /*+ ordered use_nl(PS) index(ai, FII_AP_INVOICE_B_U1 )
1985              use_nl(ai)  use_nl(apc)*/
1986          TO_NUMBER(TO_CHAR(AIP.Creation_Date,'J')) Time_ID,
1987          1 Period_Type_ID,
1988          TRUNC(AIP.Creation_Date) Action_Date,
1989          DECODE(AI.Invoice_Type, 'PREPAYMENT', 'PREPAYMENT', 'PAYMENT') Action,
1990          g_seq_id Update_Sequence,
1991          AI.Org_ID Org_ID,
1992          AI.Supplier_ID Supplier_ID,
1993          AI.Invoice_ID Invoice_ID,
1994          AI.Base_Currency_Code Base_Currency_Code,
1995          AI.Invoice_Date Trx_Date,
1996          PS.Payment_Num Payment_Num,
1997          TRUNC(PS.Due_Date) Due_Date,
1998          AIP.Created_By Created_By,
1999          -1 * (AIP.Amount + NVL(AIP.Discount_Taken,0)) Amount_Remaining,
2000          DECODE(AI.Invoice_Type, 'PREPAYMENT', 0,
2001             DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(AIP.Creation_Date)), -1,
2002                   -1 * ((AIP.Amount + NVL(AIP.Discount_Taken,0))),
2003                     0)) Past_Due_Amount,
2004          -1 * NVL(DISC.Discount_Available,0) Discount_Available,
2005          NVL(AIP.Discount_Taken,0) Discount_Taken,
2006          GREATEST(NVL(PS.Discount_Amount_Available,0),
2007                   NVL(PS.Second_Disc_Amt_Available,0),
2008                   NVL(PS.Third_Disc_Amt_Available,0))
2009             - NVL(DISC.Discount_Taken,0) - NVL(DISC.Discount_Lost,0)
2010             - NVL(AIP.Discount_Taken,0) Discount_Lost,
2011          AIP.Amount Payment_Amount,
2012          DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(AIP.Creation_Date)), -1, 0, AIP.Amount) On_Time_Payment_Amt,
2013          DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(AIP.Creation_Date)), -1, AIP.Amount, 0) Late_Payment_Amt,
2014          DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(AIP.Creation_Date)), -1,
2015                       (TRUNC(AIP.Creation_Date) - TRUNC(PS.Due_Date)), 0) No_Days_Late,
2016          CASE
2017            WHEN (AI.Invoice_Type <> 'PREPAYMENT')
2018             AND (TRUNC(PS.Due_Date) - TRUNC(AIP.Creation_Date)) >= g_due_bucket1
2019                  THEN  -1 * ((AIP.Amount + NVL(AIP.Discount_Taken,0)))
2020            ELSE  0
2021          END Due_Bucket1,
2022          CASE
2023            WHEN (AI.Invoice_Type <> 'PREPAYMENT')
2024             AND (TRUNC(PS.Due_Date) - TRUNC(AIP.Creation_Date)) <= g_due_bucket2
2025             AND (TRUNC(PS.Due_Date) - TRUNC(AIP.Creation_Date)) >  g_due_bucket3
2026                  THEN  -1 * ((AIP.Amount + NVL(AIP.Discount_Taken,0)))
2027            ELSE  0
2028          END Due_Bucket2,
2029          CASE
2030            WHEN (AI.Invoice_Type <> 'PREPAYMENT')
2031             AND (TRUNC(PS.Due_Date) - TRUNC(AIP.Creation_Date)) <= g_due_bucket3
2032             AND (TRUNC(PS.Due_Date) - TRUNC(AIP.Creation_Date)) >= 0
2033                  THEN  -1 * ((AIP.Amount + NVL(AIP.Discount_Taken,0)))
2034            ELSE  0
2035          END Due_Bucket3,
2036          CASE
2037            WHEN (AI.Invoice_Type <> 'PREPAYMENT')
2038             AND (TRUNC(AIP.Creation_Date) - TRUNC(PS.Due_Date)) > = g_past_bucket1
2039                  THEN  -1 * ((AIP.Amount + NVL(AIP.Discount_Taken,0)))
2040            ELSE  0
2041          END Past_Due_Bucket1,
2042          CASE
2043            WHEN (AI.Invoice_Type <> 'PREPAYMENT')
2044             AND (TRUNC(AIP.Creation_Date) - TRUNC(PS.Due_Date)) <= g_past_bucket2
2045             AND (TRUNC(AIP.Creation_Date) - TRUNC(PS.Due_Date)) >  g_past_bucket3
2046                  THEN  -1 * ((AIP.Amount + NVL(AIP.Discount_Taken,0)))
2047            ELSE  0
2048          END Past_Due_Bucket2,
2049          CASE
2050            WHEN (AI.Invoice_Type <> 'PREPAYMENT')
2051             AND (TRUNC(AIP.Creation_Date) - TRUNC(PS.Due_Date)) <= g_past_bucket3
2052             AND (TRUNC(AIP.Creation_Date) - TRUNC(PS.Due_Date)) >  0
2053                  THEN  -1 * ((AIP.Amount + NVL(AIP.Discount_Taken,0)))
2054            ELSE  0
2055          END Past_Due_Bucket3,
2056          TRUNC(AIP.Creation_Date) Fully_Paid_Date,
2057          AIP.Check_ID Check_ID,
2058          AC.Check_Date,
2059        DECODE(IBY_SYS_PROF_B.Processing_Type,NULL,DECODE(AC.Payment_Method_Lookup_Code, 'EFT', 'E', 'WIRE', 'E', 'M')
2060       ,DECODE(IBY_SYS_PROF_B.Processing_Type, 'ELECTRONIC', 'E', 'M')) PAYMENT_METHOD,
2061            'Y' Inv_Pymt_Flag,
2062          AIP.Invoice_Payment_ID Unique_ID
2063   FROM
2064         (SELECT /*+ use_nl(PSUM) */
2065                 PS.Invoice_ID Invoice_ID,
2066                 PS.Payment_Num Payment_Num,
2067                 SUM(Discount_Available) Discount_Available,
2068                 SUM(Discount_Lost) Discount_Lost,
2069                 SUM(Discount_Taken) Discount_Taken
2070          FROM   FII_AP_Pay_Sched_B PSUM,
2071                 AP_Payment_Schedules_All PS
2072          WHERE  PS.Invoice_ID BETWEEN g_start_range and g_end_range
2073          AND    PS.Invoice_ID           = PSUM.Invoice_ID (+)
2074          AND    PS.Payment_Num          = PSUM.Payment_Num (+)
2075          AND    PSUM.Period_Type_ID (+) = 1
2076          GROUP  BY PS.Invoice_ID,
2077                    PS.Payment_Num) DISC,
2078         AP_Payment_Schedules_All PS,
2079         FII_AP_Invoice_B AI,
2080         (SELECT /*+  merge(aip_pp) use_nl(aip_pp, pp) */
2081                 AIP_PP.Invoice_ID,
2082                 AIP_PP.Payment_Num,
2083                 AIP_PP.Creation_Date,
2084                 AIP_PP.Created_By,
2085                 AIP_PP.Amount,
2086                 AIP_PP.Discount_Taken,
2087                 AIP_PP.Check_ID,
2088                 AIP_PP.WH_Tax_Amount + NVL(SUM(CASE WHEN PP.Creation_Date IS NOT NULL
2089                                                     AND  PP.Creation_Date <= AIP_PP.Creation_Date
2090                                                     THEN PP.Prepay_Amount ELSE 0 END),0) Prepay_WH_Tax_Amount,
2091                 AIP_PP.Invoice_Payment_ID
2092          FROM
2093                (SELECT /*+ use_nl(aip_wh,temp)  */ AIP_WH.Invoice_ID,
2094                        AIP_WH.Payment_Num,
2095                        AIP_WH.Creation_Date,
2096                        AIP_WH.Created_By,
2097                        AIP_WH.Amount,
2098                        AIP_WH.Discount_Taken,
2099                        AIP_WH.Check_ID,
2100                        NVL(SUM(CASE WHEN TEMP.Creation_Date IS NOT NULL
2101                                     AND  TEMP.Creation_Date <= TRUNC(AIP_WH.Creation_Date)
2102                                     THEN TEMP.WH_Tax_Amount ELSE 0 END),0) WH_Tax_Amount,
2103                        AIP_WH.Invoice_Payment_ID
2104                 FROM AP_Invoice_Payments_All AIP_WH,
2105                      FII_AP_WH_Tax_T TEMP
2106                 WHERE AIP_WH.Invoice_ID BETWEEN g_start_range and g_end_range
2107                 AND   AIP_WH.Invoice_ID = TEMP.Invoice_ID (+)
2108                 AND   AIP_WH.Payment_Num = TEMP.Payment_Num (+)
2109                 GROUP BY AIP_WH.Invoice_ID,
2110                          AIP_WH.Payment_Num,
2111                          AIP_WH.Creation_Date,
2112                          AIP_WH.Created_By,
2113                          AIP_WH.Amount,
2114                          AIP_WH.Discount_Taken,
2115                          AIP_WH.Check_ID,
2116                          AIP_WH.Invoice_Payment_ID) AIP_PP,
2117                 FII_AP_Prepay_T PP
2118          WHERE AIP_PP.Invoice_ID = PP.Invoice_ID (+)
2119          AND   AIP_PP.Payment_Num = PP.Payment_Num (+)
2120          GROUP BY AIP_PP.Invoice_ID,
2121                   AIP_PP.Payment_Num,
2122                   AIP_PP.Creation_Date,
2123                   AIP_PP.Created_By,
2124                   AIP_PP.Amount,
2125                   AIP_PP.Discount_Taken,
2126                   AIP_PP.Check_ID,
2127                   AIP_PP.WH_Tax_Amount,
2128                   AIP_PP.Invoice_Payment_ID) AIP,
2129          FII_AP_PAY_CHK_STG APC,
2130  	 AP_Checks_All AC,
2131           -- IBY_Payment_Profiles IBYPM
2132                 IBY_SYS_PMT_PROFILES_B IBY_SYS_PROF_B,--IBY CHANGE
2133                 IBY_ACCT_PMT_PROFILES_B IBY_ACCT_PROF_B--IBY CHANGE
2134   WHERE  AI.Invoice_ID = PS.Invoice_ID
2135   AND    AI.Cancel_Date IS NULL
2136   AND    AIP.Invoice_ID  = PS.Invoice_ID
2137   AND    AIP.Payment_Num = PS.Payment_Num
2138   AND    AIP.Check_ID    = AC.Check_ID
2139   AND    PS.Invoice_ID   = DISC.Invoice_ID
2140   AND    PS.Payment_Num  = DISC.Payment_Num
2141   AND    APC.Invoice_ID  = PS.Invoice_ID
2142   AND    APC.Payment_Num = PS.Payment_Num
2143   AND    AC.Payment_Profile_ID = IBY_ACCT_PROF_B.Payment_Profile_ID(+)--IBY CHANGE
2144   AND    IBY_ACCT_PROF_B.system_profile_code = IBY_SYS_PROF_B.system_profile_code(+)--IBY CHANGE
2145   AND    APC.Invp_Creation_Date <= TRUNC(AIP.Creation_Date)
2146   AND    AC.Void_Date   IS NULL
2147   HAVING SUM(APC.Payment_Amount) + AIP.Prepay_WH_Tax_Amount = PS.Gross_Amount
2148   GROUP  BY AI.Org_ID, AI.Supplier_ID,
2149             AI.Invoice_ID,
2150             AI.Base_Currency_Code,
2151             AI.Invoice_Date,
2152             AI.Invoice_Type,
2153             PS.Payment_Num,
2154             PS.Due_Date,
2155             AIP.Amount,
2156             AIP.Created_By,
2157             AIP.Check_ID,
2158             AC.Check_Date,
2159             IBY_SYS_PROF_B.Processing_Type,
2160             AC.Payment_Method_Lookup_Code,
2161            -- AC.PAYMENT_PROFILE_ID,
2162             AIP.Creation_Date,
2163             NVL(AIP.Discount_Taken,0),
2164             NVL(DISC.Discount_Available,0),
2165             NVL(DISC.Discount_Taken,0),
2166             NVL(DISC.Discount_Lost,0),
2167             NVL(PS.Discount_Amount_Available,0),
2168             NVL(PS.Second_Disc_Amt_Available,0),
2169             NVL(PS.Third_Disc_Amt_Available,0),
2170             PS.Gross_Amount,
2171             AIP.Prepay_WH_Tax_Amount,
2172             AIP.Invoice_Payment_ID;
2173 
2174 
2175   if g_debug_flag = 'Y' then
2176      FII_UTIL.put_line('Inserted ' ||SQL%ROWCOUNT|| ' Last Payment records into FII_AP_PAY_SCHED_TEMP ');
2177      FII_UTIL.put_timestamp('End Timestamp');
2178      FII_UTIL.stop_timer;
2179      FII_UTIL.print_timer('Duration');
2180      FII_UTIL.put_line('');
2181   end if;
2182 
2183   commit;
2184 
2185 
2186 
2187 
2188   g_state := 'Populating Last Payment Creation records from temp table';
2189   if g_debug_flag = 'Y' then
2190      FII_UTIL.put_line(g_state);
2191      FII_UTIL.put_timestamp('Start Timestamp');
2192      FII_UTIL.start_timer;
2193      FII_UTIL.put_line('');
2194   end if;
2195 
2196   INSERT INTO FII_AP_PAY_SCHED_B b
2197         (Time_ID,
2198          Period_Type_ID,
2199          Action_Date,
2200          Action,
2201          Update_Sequence,
2202          Org_ID,
2203          Supplier_ID,
2204          Invoice_ID,
2205          Base_Currency_Code,
2206          Trx_Date,
2207          Payment_Num,
2208          Due_Date,
2209          Created_By,
2210          Amount_Remaining,
2211          Past_Due_Amount,
2212          Discount_Available,
2213          Discount_Taken,
2214          Discount_Lost,
2215          Payment_Amount,
2216          On_Time_Payment_Amt,
2217          Late_Payment_Amt,
2218          No_Days_Late,
2219          Due_Bucket1,
2220          Due_Bucket2,
2221          Due_Bucket3,
2222          Past_Due_Bucket1,
2223          Past_Due_Bucket2,
2224          Past_Due_Bucket3,
2225          Amount_Remaining_B,
2226          Past_Due_Amount_B,
2227          Discount_Available_B,
2228          Discount_Taken_B,
2229          Discount_Lost_B,
2230          Payment_Amount_B,
2231          On_Time_Payment_Amt_B,
2232          Late_Payment_Amt_B,
2233          Due_Bucket1_B,
2234          Due_Bucket2_B,
2235          Due_Bucket3_B,
2236          Past_Due_Bucket1_B,
2237          Past_Due_Bucket2_B,
2238          Past_Due_Bucket3_B,
2239          Prim_Amount_Remaining,
2240          Prim_Past_Due_Amount,
2241          Prim_Discount_Available,
2242          Prim_Discount_Taken,
2243          Prim_Discount_Lost,
2244          Prim_Payment_Amount,
2245          Prim_On_time_Payment_Amt,
2246          Prim_Late_Payment_Amt,
2247          Prim_Due_Bucket1,
2248          Prim_Due_Bucket2,
2249          Prim_Due_Bucket3,
2250          Prim_Past_Due_Bucket1,
2251          Prim_Past_Due_Bucket2,
2252          Prim_Past_Due_Bucket3,
2253          Sec_Amount_Remaining,
2254          Sec_Past_Due_Amount,
2255          Sec_Discount_Available,
2256          Sec_Discount_Taken,
2257          Sec_Discount_Lost,
2258          Sec_Payment_Amount,
2259          Sec_On_time_Payment_Amt,
2260          Sec_Late_Payment_Amt,
2261          Sec_Due_Bucket1,
2262          Sec_Due_Bucket2,
2263          Sec_Due_Bucket3,
2264          Sec_Past_Due_Bucket1,
2265          Sec_Past_Due_Bucket2,
2266          Sec_Past_Due_Bucket3,
2267          Fully_Paid_Date,
2268          Check_ID,
2269          Check_Date,
2270          Payment_Method,
2271          Creation_Date,
2272          Last_Updated_By,
2273          Last_Update_Date,
2274          Last_Update_Login,
2275          Inv_Pymt_Flag,
2276          Unique_ID)
2277   SELECT /*+ ordered use_nl(RATES,FRATES) */
2278          TEMP.Time_ID,
2279          TEMP.Period_Type_ID,
2280          TEMP.Action_Date,
2281          TEMP.Action,
2282          TEMP.Update_Sequence,
2283          TEMP.Org_ID,
2284          TEMP.Supplier_ID,
2285          TEMP.Invoice_ID,
2286          TEMP.Base_Currency_Code,
2287          TEMP.Trx_Date,
2288          TEMP.Payment_Num,
2289          TEMP.Due_Date,
2290          TEMP.Created_By,
2291          TEMP.Amount_Remaining,
2292          TEMP.Past_Due_Amount,
2293          TEMP.Discount_Available,
2294          TEMP.Discount_Taken,
2295          TEMP.Discount_Lost,
2296          TEMP.Payment_Amount,
2297          TEMP.On_Time_Payment_Amt,
2298          TEMP.Late_Payment_Amt,
2299          TEMP.No_Days_Late,
2300          TEMP.Due_Bucket1,
2301          TEMP.Due_Bucket2,
2302          TEMP.Due_Bucket3,
2303          TEMP.Past_Due_Bucket1,
2304          TEMP.Past_Due_Bucket2,
2305          TEMP.Past_Due_Bucket3,
2306          ROUND((Amount_Remaining * Conversion_Rate) / Functional_MAU) * Functional_MAU Amount_Remaining_B,
2307          ROUND((Past_Due_Amount * Conversion_Rate) / Functional_MAU) * Functional_MAU Past_Due_Amount_B,
2308          ROUND((Discount_Available * Conversion_Rate) / Functional_MAU) * Functional_MAU Discount_Available_B,
2309          ROUND((Discount_Taken * Conversion_Rate) / Functional_MAU) * Functional_MAU,
2310          ROUND((Discount_Lost * Conversion_Rate) / Functional_MAU) * Functional_MAU,
2311          ROUND((Payment_Amount * Conversion_Rate) / Functional_MAU) * Functional_MAU,
2312          ROUND((On_Time_Payment_Amt * Conversion_Rate) / Functional_MAU) * Functional_MAU,
2313          ROUND((Late_Payment_Amt * Conversion_Rate) / Functional_MAU) * Functional_MAU,
2314          ROUND((Due_Bucket1 * Conversion_Rate) / Functional_MAU) * Functional_MAU Due_Bucket1_B,
2315          ROUND((Due_Bucket2 * Conversion_Rate) / Functional_MAU) * Functional_MAU Due_Bucket2_B,
2316          ROUND((Due_Bucket3 * Conversion_Rate) / Functional_MAU) * Functional_MAU Due_Bucket3_B,
2317          ROUND((Past_Due_Bucket1 * Conversion_Rate) / Functional_MAU) * Functional_MAU Past_Due_Bucket1_B,
2318          ROUND((Past_Due_Bucket2 * Conversion_Rate) / Functional_MAU) * Functional_MAU Past_Due_Bucket2_B,
2319          ROUND((Past_Due_Bucket3 * Conversion_Rate) / Functional_MAU) * Functional_MAU Past_Due_Bucket3_B,
2320          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, Amount_Remaining,
2321                           ((Amount_Remaining * Conversion_Rate) * RATES.Prim_Conversion_Rate))
2322                           / g_primary_mau) * g_primary_mau Prim_Amount_Remaining,
2323          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, Past_Due_Amount,
2324                           ((Past_Due_Amount * Conversion_Rate) * RATES.Prim_Conversion_Rate))
2325                           / g_primary_mau) * g_primary_mau Prim_Past_Due_Amount,
2326          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, Discount_Available,
2327                           ((Discount_Available * Conversion_Rate) * RATES.Prim_Conversion_Rate))
2328                           / g_primary_mau) * g_primary_mau Prim_Discount_Available,
2329          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, Discount_Taken,
2330                           ((Discount_Taken * Conversion_Rate) * RATES.Prim_Conversion_Rate))
2331                           / g_primary_mau) * g_primary_mau Prim_Discount_Taken,
2332          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, Discount_Lost,
2333                           ((Discount_Lost * Conversion_Rate) * RATES.Prim_Conversion_Rate))
2334                           / g_primary_mau) * g_primary_mau Prim_Discount_Lost,
2335          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, Payment_Amount,
2336                           ((Payment_Amount * Conversion_Rate) * RATES.Prim_Conversion_Rate))
2337                           / g_primary_mau) * g_primary_mau Prim_Payment_Amount,
2338          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, On_Time_Payment_Amt,
2339                           ((On_Time_Payment_Amt * Conversion_Rate) * RATES.Prim_Conversion_Rate))
2340                           / g_primary_mau) * g_primary_mau Prim_On_Time_Payment_Amt,
2341          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, Late_Payment_Amt,
2342                           ((Late_Payment_Amt * Conversion_Rate) * RATES.Prim_Conversion_Rate))
2343                           / g_primary_mau) * g_primary_mau Prim_Late_Payment_Amt,
2344          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, Due_Bucket1,
2345                           ((Due_Bucket1 * Conversion_Rate) * RATES.Prim_Conversion_Rate))
2346                           / g_primary_mau) * g_primary_mau Prim_Due_Bucket1,
2347          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, Due_Bucket2,
2348                           ((Due_Bucket2 * Conversion_Rate) * RATES.Prim_Conversion_Rate))
2349                           / g_primary_mau) * g_primary_mau Prim_Due_Bucket2,
2350          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, Due_Bucket3,
2351                           ((Due_Bucket3 * Conversion_Rate) * RATES.Prim_Conversion_Rate))
2352                           / g_primary_mau) * g_primary_mau Prim_Due_Bucket3,
2353          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, Past_Due_Bucket1,
2354                           ((Past_Due_Bucket1 * Conversion_Rate) * RATES.Prim_Conversion_Rate))
2355                           / g_primary_mau) * g_primary_mau Prim_Past_Due_Bucket1,
2356          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, Past_Due_Bucket2,
2357                           ((Past_Due_Bucket2 * Conversion_Rate) * RATES.Prim_Conversion_Rate))
2358                           / g_primary_mau) * g_primary_mau Prim_Past_Due_Bucket2,
2359          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, Past_Due_Bucket3,
2360                           ((Past_Due_Bucket3 * Conversion_Rate) * RATES.Prim_Conversion_Rate))
2361                           / g_primary_mau) * g_primary_mau Prim_Past_Due_Bucket3,
2362          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, Amount_Remaining,
2363                           ((Amount_Remaining * Conversion_Rate) * RATES.Sec_Conversion_Rate))
2364                           / g_secondary_mau) * g_secondary_mau Sec_Amount_Remaining,
2365          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, Past_Due_Amount,
2366                           ((Past_Due_Amount * Conversion_Rate) * RATES.Sec_Conversion_Rate))
2367                           / g_secondary_mau) * g_secondary_mau Sec_Past_Due_Amount,
2368          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, Discount_Available,
2369                           ((Discount_Available * Conversion_Rate) * RATES.Sec_Conversion_Rate))
2370                           / g_secondary_mau) * g_secondary_mau Sec_Discount_Available,
2371          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, Discount_Taken,
2372                           ((Discount_Taken * Conversion_Rate) * RATES.Sec_Conversion_Rate))
2373                           / g_secondary_mau) * g_secondary_mau Sec_Discount_Taken,
2374          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, Discount_Lost,
2375                           ((Discount_Lost * Conversion_Rate) * RATES.Sec_Conversion_Rate))
2376                           / g_secondary_mau) * g_secondary_mau Sec_Discount_Lost,
2377          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, Payment_Amount,
2378                           ((Payment_Amount * Conversion_Rate) * RATES.Sec_Conversion_Rate))
2379                           / g_secondary_mau) * g_secondary_mau Sec_Payment_Amount,
2380          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, On_Time_Payment_Amt,
2381                           ((On_Time_Payment_Amt * Conversion_Rate) * RATES.Sec_Conversion_Rate))
2382                           / g_secondary_mau) * g_secondary_mau Sec_On_Time_Payment_Amt,
2383          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, Late_Payment_Amt,
2384                           ((Late_Payment_Amt * Conversion_Rate) * RATES.Sec_Conversion_Rate))
2385                           / g_secondary_mau) * g_secondary_mau Sec_Late_Payment_Amt,
2386          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, Due_Bucket1,
2387                           ((Due_Bucket1 * Conversion_Rate) * RATES.Sec_Conversion_Rate))
2388                           / g_secondary_mau) * g_secondary_mau Sec_Due_Bucket1,
2389          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, Due_Bucket2,
2390                           ((Due_Bucket2 * Conversion_Rate) * RATES.Sec_Conversion_Rate))
2391                           / g_secondary_mau) * g_secondary_mau Sec_Due_Bucket2,
2392          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, Due_Bucket3,
2393                           ((Due_Bucket3 * Conversion_Rate) * RATES.Sec_Conversion_Rate))
2394                           / g_secondary_mau) * g_secondary_mau Sec_Due_Bucket3,
2395          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, Past_Due_Bucket1,
2396                           ((Past_Due_Bucket1 * Conversion_Rate) * RATES.Sec_Conversion_Rate))
2397                           / g_secondary_mau) * g_secondary_mau Sec_Past_Due_Bucket1,
2398          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, Past_Due_Bucket2,
2399                           ((Past_Due_Bucket2 * Conversion_Rate) * RATES.Sec_Conversion_Rate))
2400                           / g_secondary_mau) * g_secondary_mau Sec_Past_Due_Bucket2,
2401          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, Past_Due_Bucket3,
2402                           ((Past_Due_Bucket3 * Conversion_Rate) * RATES.Sec_Conversion_Rate))
2403                           / g_secondary_mau) * g_secondary_mau Sec_Past_Due_Bucket3,
2404          TEMP.Fully_Paid_Date,
2405          TEMP.Check_ID,
2406          TEMP.Check_Date,
2407          TEMP.Payment_Method,
2408          sysdate Creation_Date,
2409          g_fii_user_id Last_Updated_By,
2410          sysdate Last_Update_Date,
2411          g_fii_login_id Last_Update_Login,
2412          TEMP.Inv_Pymt_Flag,
2413          TEMP.Unique_ID
2414   FROM   FII_AP_Invoice_B AI,
2415          FII_AP_Pay_Sched_Temp TEMP,
2416          FII_AP_PS_Rates_Temp   RATES,
2417          FII_AP_Func_Rates_Temp FRATES
2418   WHERE  TEMP.Invoice_ID      = AI.Invoice_ID
2419   AND    AI.Invoice_ID BETWEEN g_start_range and g_end_range
2420   AND    FRATES.To_Currency   = TEMP.Base_Currency_Code
2421   AND    FRATES.From_Currency = AI.Payment_Currency_Code
2422   AND    FRATES.Trx_Date      = AI.Exchange_Date
2423   AND    DECODE(AI.Exchange_Rate_Type,'User', AI.Exchange_Rate,1) =
2424                  DECODE(FRATES.Conversion_Type,'User', FRATES.Conversion_Rate,1)
2425   AND    FRATES.Conversion_Type    = AI.Exchange_Rate_Type
2426   AND    RATES.Functional_Currency = TEMP.Base_Currency_Code
2427   AND    RATES.Trx_Date            = TEMP.Trx_Date;
2428 
2429 
2430   if g_debug_flag = 'Y' then
2431      FII_UTIL.put_line('Inserted ' ||SQL%ROWCOUNT|| ' Payment records into FII_AP_PAY_SCHED_B from Temp table');
2432      FII_UTIL.put_timestamp('End Timestamp');
2433      FII_UTIL.stop_timer;
2434      FII_UTIL.print_timer('Duration');
2435      FII_UTIL.put_line('');
2436   end if;
2437 
2438   commit;
2439 
2440   g_state := 'Populating Withholding Action records';
2441   if g_debug_flag = 'Y' then
2442      FII_UTIL.put_line(g_state);
2443      FII_UTIL.put_timestamp('Start Timestamp');
2444      FII_UTIL.start_timer;
2445      FII_UTIL.put_line('');
2446   end if;
2447 
2448   /* Insert statement to insert prepayment information into the summary tables.
2449      Discount information is not recorded as the discounts taken and lost are
2450      available in the payments to the prepayment invoice.
2451 
2452      For prepayments we will always assume that the payment is made on time and
2453      will populate the On_Time_Payment_Amt column with the payment amount */
2454 
2455 
2456 
2457   INSERT INTO FII_AP_PAY_SCHED_B b
2458         (Time_ID,
2459          Period_Type_ID,
2460          Action_Date,
2461          Action,
2462          Update_Sequence,
2463          Org_ID,
2464          Supplier_ID,
2465          Invoice_ID,
2466          Base_Currency_Code,
2467          Trx_Date,
2468          Payment_Num,
2469          Due_Date,
2470          Created_By,
2471          Amount_Remaining,
2472          Past_Due_Amount,
2473          Discount_Available,
2474          Discount_Taken,
2475          Discount_Lost,
2476          Payment_Amount,
2477          On_Time_Payment_Amt,
2478          Late_Payment_Amt,
2479          No_Days_Late,
2480          Due_Bucket1,
2481          Due_Bucket2,
2482          Due_Bucket3,
2483          Past_Due_Bucket1,
2484          Past_Due_Bucket2,
2485          Past_Due_Bucket3,
2486          Amount_Remaining_B,
2487          Past_Due_Amount_B,
2488          Discount_Available_B,
2489          Discount_Taken_B,
2490          Discount_Lost_B,
2491          Payment_Amount_B,
2492          On_Time_Payment_Amt_B,
2493          Late_Payment_Amt_B,
2494          Due_Bucket1_B,
2495          Due_Bucket2_B,
2496          Due_Bucket3_B,
2497          Past_Due_Bucket1_B,
2498          Past_Due_Bucket2_B,
2499          Past_Due_Bucket3_B,
2500          Prim_Amount_Remaining,
2501          Prim_Past_Due_Amount,
2502          Prim_Discount_Available,
2503          Prim_Discount_Taken,
2504          Prim_Discount_Lost,
2505          Prim_Payment_Amount,
2506          Prim_On_time_Payment_Amt,
2507          Prim_Late_Payment_Amt,
2508          Prim_Due_Bucket1,
2509          Prim_Due_Bucket2,
2510          Prim_Due_Bucket3,
2511          Prim_Past_Due_Bucket1,
2512          Prim_Past_Due_Bucket2,
2513          Prim_Past_Due_Bucket3,
2514          Sec_Amount_Remaining,
2515          Sec_Past_Due_Amount,
2516          Sec_Discount_Available,
2517          Sec_Discount_Taken,
2518          Sec_Discount_Lost,
2519          Sec_Payment_Amount,
2520          Sec_On_time_Payment_Amt,
2521          Sec_Late_Payment_Amt,
2522          Sec_Due_Bucket1,
2523          Sec_Due_Bucket2,
2524          Sec_Due_Bucket3,
2525          Sec_Past_Due_Bucket1,
2526          Sec_Past_Due_Bucket2,
2527          Sec_Past_Due_Bucket3,
2528          Creation_Date,
2529          Last_Updated_By,
2530          Last_Update_Date,
2531          Last_Update_Login)
2532   SELECT /*+  ordered use_nl(ps) use_nl(rates,frates) */
2533          TO_NUMBER(TO_CHAR(AID.Creation_Date,'J')) Time_ID,
2534          1 Period_Type_ID,
2535          TRUNC(AID.Creation_Date) Action_Date,
2536          DECODE(AID.Line_Type_Lookup_Code, 'AWT', 'WITHHOLDING', 'TAX') Action,
2537          g_seq_id Update_Sequence,
2538          AI.Org_ID Org_ID,
2539          AI.Supplier_ID Supplier_ID,
2540          AI.Invoice_ID Invoice_ID,
2541          AI.Base_Currency_Code Base_Currency_Code,
2542          AI.Invoice_Date Invoice_Date,
2543          PS.Payment_Num Payment_Num,
2544          TRUNC(PS.Due_Date) Due_Date,
2545          PS.Created_By Created_By,
2546          ROUND((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)))
2547                                    / NVL(FC.Minimum_Accountable_Unit, 0.01))
2548                             * NVL(FC.Minimum_Accountable_Unit, 0.01) Amount_Remaining,
2549 	 DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)), -1,
2550                 ROUND(SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount))
2551 		/NVL(FC.Minimum_Accountable_Unit, 0.01))
2552 		* NVL(FC.Minimum_Accountable_Unit, 0.01), 0) Past_Due_Amount,
2553          0 Discount_Available,
2554          0 Discount_Taken,
2555          0 Discount_Lost,
2556          0 Payment_Amount,
2557 	 0 On_Time_Payment_Amount,
2558 	 0 Late_Payment_Amt,
2559          DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)), -1,
2560 		(TRUNC(AID.Creation_Date) - TRUNC(PS.Due_Date)), 0) No_Days_Late,
2561          CASE
2562 	     WHEN (TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)) >= g_due_bucket1
2563 	     THEN ROUND((SUM(DECODE(AI.Invoice_Amount, 0, 0,PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)))
2564                                    / NVL(FC.Minimum_Accountable_Unit, 0.01))
2565                             * NVL(FC.Minimum_Accountable_Unit, 0.01)
2566 	     ELSE 0
2567 	 END  Due_Bucket1,
2568          CASE
2569 	     WHEN (TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)) <= g_due_bucket2
2570 	     AND (TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)) > g_due_bucket3
2571 	     THEN ROUND((SUM(DECODE(AI.Invoice_Amount, 0, 0,PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)))
2572                                    / NVL(FC.Minimum_Accountable_Unit, 0.01))
2573                             * NVL(FC.Minimum_Accountable_Unit, 0.01)
2574 	     ELSE 0
2575 	 END  Due_Bucket2,
2576          CASE
2577 	     WHEN (TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)) <= g_due_bucket3
2578 	     AND (TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)) >= 0
2579 	     THEN ROUND((SUM(DECODE(AI.Invoice_Amount, 0, 0,PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)))
2580                                    / NVL(FC.Minimum_Accountable_Unit, 0.01))
2581                             * NVL(FC.Minimum_Accountable_Unit, 0.01)
2582 	     ELSE 0
2583 	 END Due_Bucket3,
2584          CASE
2585 	     WHEN (TRUNC(AID.Creation_Date) - TRUNC(PS.Due_Date)) >= g_past_bucket1
2586 	     THEN ROUND((SUM(DECODE(AI.Invoice_Amount, 0, 0,PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)))
2587                                    / NVL(FC.Minimum_Accountable_Unit, 0.01))
2588                             * NVL(FC.Minimum_Accountable_Unit, 0.01)
2589 	     ELSE 0
2590 	 END Past_Due_Bucket1,
2591          CASE
2592 	     WHEN (TRUNC(AID.Creation_Date) - TRUNC(PS.Due_Date)) <= g_past_bucket2
2593 	     AND (TRUNC(AID.Creation_Date) - TRUNC(PS.Due_Date)) > g_past_bucket3
2594 	     THEN ROUND((SUM(DECODE(AI.Invoice_Amount, 0, 0,PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)))
2595                                    / NVL(FC.Minimum_Accountable_Unit, 0.01))
2596                             * NVL(FC.Minimum_Accountable_Unit, 0.01)
2597 	     ELSE 0
2598 	 END Past_Due_Bucket2,
2599          CASE
2600 	     WHEN (TRUNC(AID.Creation_Date) - TRUNC(PS.Due_Date)) <= g_past_bucket3
2601 	     AND (TRUNC(AID.Creation_Date) - TRUNC(PS.Due_Date)) > 0
2602 	     THEN ROUND((SUM(DECODE(AI.Invoice_Amount, 0, 0,PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)))
2603                                    / NVL(FC.Minimum_Accountable_Unit, 0.01))
2604                             * NVL(FC.Minimum_Accountable_Unit, 0.01)
2605 	     ELSE 0
2606 	 END Past_Due_Bucket3,
2607          ROUND(((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount))) * Conversion_Rate)
2608                            / NVL(FC.Minimum_Accountable_Unit, 0.01))
2609                       * NVL(FC.Minimum_Accountable_Unit, 0.01) Amount_Remaining_B,
2610 	 DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)), -1,
2611 	         ROUND(((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount))) * Conversion_Rate)
2612                            / NVL(FC.Minimum_Accountable_Unit, 0.01))
2613                       * NVL(FC.Minimum_Accountable_Unit, 0.01), 0)  Past_Due_Amount_B,
2614          0 Discount_Available_B,
2615          0 Discount_Taken_B,
2616          0 Discount_Lost_B,
2617 	 0 Payment_Amount_B,
2618 	 0 On_Time_Payment_Amt_B,
2619 	 0 Late_Payment_Amt_B,
2620          CASE
2621 	     WHEN (TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)) >= g_due_bucket1
2622 	     THEN ROUND(((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount))) * Conversion_Rate)
2623                            / NVL(FC.Minimum_Accountable_Unit, 0.01))
2624                       * NVL(FC.Minimum_Accountable_Unit, 0.01)
2625 	     ELSE 0
2626 	 END  Due_Bucket1_B,
2627          CASE
2628 	     WHEN (TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)) <= g_due_bucket2
2629 	     AND (TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)) > g_due_bucket3
2630 	     THEN ROUND(((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount))) * Conversion_Rate)
2631                            / NVL(FC.Minimum_Accountable_Unit, 0.01))
2632                       * NVL(FC.Minimum_Accountable_Unit, 0.01)
2633 	     ELSE 0
2634 	 END Due_Bucket2_B,
2635           CASE
2636 	     WHEN (TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)) <= g_due_bucket3
2637 	     AND (TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)) >= 0
2638 	     THEN ROUND(((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount))) * Conversion_Rate)
2639                            / NVL(FC.Minimum_Accountable_Unit, 0.01))
2640                       * NVL(FC.Minimum_Accountable_Unit, 0.01)
2641 	     ELSE 0
2642 	 END Due_Bucket3_B,
2643          CASE
2644 	     WHEN (TRUNC(AID.Creation_Date) - TRUNC(PS.Due_Date)) >= g_past_bucket1
2645 	     THEN ROUND(((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount))) * Conversion_Rate)
2646                            / NVL(FC.Minimum_Accountable_Unit, 0.01))
2647                       * NVL(FC.Minimum_Accountable_Unit, 0.01)
2648 	     ELSE 0
2649 	 END  Past_Due_Bucket1_B,
2650          CASE
2651 	     WHEN (TRUNC(AID.Creation_Date) - TRUNC(PS.Due_Date)) <= g_past_bucket2
2652 	     AND (TRUNC(AID.Creation_Date) - TRUNC(PS.Due_Date)) > g_past_bucket3
2653 	     THEN ROUND(((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount))) * Conversion_Rate)
2654                            / NVL(FC.Minimum_Accountable_Unit, 0.01))
2655                       * NVL(FC.Minimum_Accountable_Unit, 0.01)
2656 	     ELSE 0
2657 	 END Past_Due_Bucket2_B,
2658          CASE
2659 	     WHEN (TRUNC(AID.Creation_Date) - TRUNC(PS.Due_Date)) <= g_past_bucket3
2660 	     AND (TRUNC(AID.Creation_Date) - TRUNC(PS.Due_Date)) > 0
2661 	     THEN ROUND(((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount))) * Conversion_Rate)
2662                            / NVL(FC.Minimum_Accountable_Unit, 0.01))
2663                       * NVL(FC.Minimum_Accountable_Unit, 0.01)
2664 	     ELSE 0
2665 	 END Past_Due_Bucket3_B,
2666          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency,
2667                   SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)),
2668                (((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount))) * Conversion_Rate)
2669                 * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau Prim_Amount_Remaining,
2670 	 DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)), -1,
2671          	ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency,
2672                       SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)),
2673                     ((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)) * Conversion_Rate)
2674                 	* RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau, 0) Prim_Past_Due_Amount,
2675          0 Prim_Discount_Available,
2676          0 Prim_Discount_Taken,
2677          0 Prim_Discount_Lost,
2678 	 0 Prim_Payment_Amount,
2679 	 0 Prim_On_Time_Payment_Amt,
2680 	 0 Prim_Late_Payment_Amt,
2681          CASE
2682 	     WHEN (TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)) >= g_due_bucket1
2683 	     THEN ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency,
2684                         SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)),
2685                      (((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount))) * Conversion_Rate)
2686                 * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau
2687 	     ELSE 0
2688 	 END  Prim_Due_Bucket1,
2689          CASE
2690 	     WHEN (TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)) <= g_due_bucket2
2691 	     AND (TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)) > g_due_bucket3
2692 	     THEN ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency,
2693                         SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)),
2694                      (((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount))) * Conversion_Rate)
2695                 * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau
2696 	     ELSE 0
2697 	 END Prim_Due_Bucket2,
2698           CASE
2699 	     WHEN (TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)) <= g_due_bucket3
2700 	     AND (TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)) >= 0
2701 	     THEN ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency,
2702                         SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)),
2703                      (((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount))) * Conversion_Rate)
2704                 * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau
2705 	     ELSE 0
2706 	 END Prim_Due_Bucket3,
2707          CASE
2708 	     WHEN (TRUNC(AID.Creation_Date) - TRUNC(PS.Due_Date)) >= g_past_bucket1
2709 	     THEN ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency,
2710                         SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)),
2711                      (((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount))) * Conversion_Rate)
2712                 * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau
2713 	     ELSE 0
2714 	 END Prim_Past_Due_Bucket1,
2715          CASE
2716 	     WHEN (TRUNC(AID.Creation_Date) - TRUNC(PS.Due_Date)) <= g_past_bucket2
2717 	     AND (TRUNC(AID.Creation_Date) - TRUNC(PS.Due_Date)) > g_past_bucket3
2718 	     THEN ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency,
2719                         SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)),
2720                      (((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount))) * Conversion_Rate)
2721                 * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau
2722 	     ELSE 0
2723 	 END Prim_Past_Due_Bucket2,
2724          CASE
2725 	     WHEN (TRUNC(AID.Creation_Date) - TRUNC(PS.Due_Date)) <= g_past_bucket3
2726 	     AND (TRUNC(AID.Creation_Date) - TRUNC(PS.Due_Date)) > 0
2727 	     THEN ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency,
2728                         SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)),
2729                      (((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount))) * Conversion_Rate)
2730                 * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau
2731 	     ELSE 0
2732 	 END Prim_Past_Due_Bucket3,
2733          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency,
2734                SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)),
2735             (((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount))) * Conversion_Rate)
2736                 * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau Sec_Amount_Remaining,
2737 	 DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)), -1,
2738          	ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency,
2739                       SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)),
2740                     ((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)) * Conversion_Rate)
2741                 	* RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau, 0) Sec_Past_Due_Amount,
2742          0 Sec_Discount_Available,
2743          0 Sec_Discount_Taken,
2744          0 Sec_Discount_Lost,
2745 	 0 Sec_Payment_Amount,
2746 	 0 Sec_On_Time_Payment_Amt,
2747 	 0 Sec_Late_Payment_Amt,
2748          CASE
2749 	     WHEN (TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)) >= g_due_bucket1
2750 	     THEN ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency,
2751                         SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)),
2752                      (((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount))) * Conversion_Rate)
2753                 * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau
2754 	     ELSE 0
2755 	 END  Sec_Due_Bucket1,
2756          CASE
2757 	     WHEN (TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)) <= g_due_bucket2
2758 	     AND (TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)) > g_due_bucket3
2759 	     THEN ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency,
2760                         SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)),
2761                      (((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount))) * Conversion_Rate)
2762                 * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau
2763 	     ELSE 0
2764 	 END Sec_Due_Bucket2,
2765           CASE
2766 	     WHEN (TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)) <= g_due_bucket3
2767 	     AND (TRUNC(PS.Due_Date) - TRUNC(AID.Creation_Date)) >= 0
2768 	     THEN ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency,
2769                         SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)),
2770                      (((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount))) * Conversion_Rate)
2771                 * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau
2772 	     ELSE 0
2773 	 END Sec_Due_Bucket3,
2774          CASE
2775 	     WHEN (TRUNC(AID.Creation_Date) - TRUNC(PS.Due_Date)) >= g_past_bucket1
2776 	     THEN ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency,
2777                         SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)),
2778                      (((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount))) * Conversion_Rate)
2779                 * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau
2780 	     ELSE 0
2781 	 END Sec_Past_Due_Bucket1,
2782          CASE
2783 	     WHEN (TRUNC(AID.Creation_Date) - TRUNC(PS.Due_Date)) <= g_past_bucket2
2784 	     AND (TRUNC(AID.Creation_Date) - TRUNC(PS.Due_Date)) > g_past_bucket3
2785 	     THEN ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency,
2786                         SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)),
2787                      (((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount))) * Conversion_Rate)
2788                 * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau
2789 	     ELSE 0
2790 	 END Sec_Past_Due_Bucket2,
2791          CASE
2792 	     WHEN (TRUNC(AID.Creation_Date) - TRUNC(PS.Due_Date)) <= g_past_bucket3
2793 	     AND (TRUNC(AID.Creation_Date) - TRUNC(PS.Due_Date)) > 0
2794 	     THEN ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency,
2795                         SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount)),
2796                      (((SUM(DECODE(AI.Invoice_Amount, 0, 0, PS.Gross_Amount * AID.Amount / AI.Invoice_Amount))) * Conversion_Rate)
2797                 * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau
2798 	     ELSE 0
2799 	 END Sec_Past_Due_Bucket3,
2800          sysdate Creation_Date,
2801          g_fii_user_id Last_Updated_By,
2802          sysdate Last_Update_Date,
2803          g_fii_login_id Last_Update_Login
2804   FROM   FII_AP_Invoice_B AI,
2805          AP_Invoice_Distributions_All AID,
2806          AP_Invoice_Lines_All AIL,
2807 	 AP_Payment_Schedules_All PS,
2808          FII_AP_PS_Rates_Temp RATES,
2809          FII_AP_Func_Rates_Temp FRATES,
2810          FND_Currencies FC
2811   WHERE  PS.Invoice_ID = AI.Invoice_ID
2812   AND 	 AID.Invoice_ID = AI.Invoice_ID
2813   AND    AID.Invoice_ID = AIL.Invoice_ID
2814   AND    AID.Invoice_Line_Number = AIL.Line_Number
2815   AND    (AID.Line_Type_Lookup_Code IN ('AWT') OR (AID.Line_Type_Lookup_Code IN ('NONREC_TAX', 'REC_TAX') AND AID.Prepay_Distribution_ID IS NOT NULL))
2816   AND    (AIL.Invoice_Includes_Prepay_Flag IS NULL OR AIL.Invoice_Includes_Prepay_Flag = 'N')
2817   --AND    AID.Reversal_Flag IS NULL
2818   AND NVL(AID.Reversal_Flag,'N') = 'N'
2819   AND    AI.Invoice_Type NOT IN ('PREPAYMENT')
2820   AND    AI.Cancel_Date IS NULL
2821   AND    FRATES.To_Currency   = AI.Base_Currency_Code
2822   AND    FRATES.From_Currency = AI.Payment_Currency_Code
2823   AND    FRATES.Trx_Date      = AI.Exchange_Date
2824   AND    DECODE(AI.Exchange_Rate_Type,'User', AI.Exchange_Rate,1) =
2825                  DECODE(FRATES.Conversion_Type,'User', FRATES.Conversion_Rate,1)
2826   AND    FRATES.Conversion_Type    = AI.Exchange_Rate_Type
2827   AND    RATES.Functional_Currency = AI.Base_Currency_Code
2828   AND    RATES.Trx_Date            = AI.Invoice_Date
2829   AND    AI.Invoice_ID BETWEEN g_start_range and g_end_range
2830   AND    AI.Payment_Currency_Code = FC.Currency_Code
2831   GROUP BY	TO_NUMBER(TO_CHAR(AID.Creation_Date,'J')),
2832 		TRUNC(AID.Creation_Date),
2833 		AID.Line_Type_Lookup_Code,
2834                 AI.Invoice_Currency_Code,
2835         	AI.Base_Currency_Code,
2836         	AI.Invoice_Date,
2837 		AI.Org_ID,
2838 		AI.Supplier_ID,
2839 		AI.Invoice_ID,
2840 		PS.Payment_Num,
2841 		TRUNC(PS.Due_Date),
2842 		PS.Created_By,
2843 		AI.Invoice_Currency_Code,
2844 		AI.Payment_Currency_Code,
2845 		AI.Payment_Cross_Rate,
2846 		NVL(FC.Minimum_Accountable_Unit, 0.01),
2847 		AI.Invoice_Type,
2848         	RATES.Prim_Conversion_Rate,
2849         	RATES.Sec_Conversion_Rate,
2850         	Conversion_Rate;
2851 
2852 
2853 
2854   if g_debug_flag = 'Y' then
2855      FII_UTIL.put_line('Inserted ' ||SQL%ROWCOUNT|| ' Withholding records into FII_AP_PAY_SCHED_B');
2856      FII_UTIL.put_timestamp('End Timestamp');
2857      FII_UTIL.stop_timer;
2858      FII_UTIL.print_timer('Duration');
2859      FII_UTIL.put_line('');
2860   end if;
2861 
2862   commit;
2863 
2864   g_state := 'Populating Prepayment Action records';
2865   if g_debug_flag = 'Y' then
2866      FII_UTIL.put_line(g_state);
2867      FII_UTIL.put_timestamp('Start Timestamp');
2868      FII_UTIL.start_timer;
2869      FII_UTIL.put_line('');
2870   end if;
2871 
2872   INSERT INTO FII_AP_PAY_SCHED_B b
2873         (Time_ID,
2874          Period_Type_ID,
2875          Action_Date,
2876          Action,
2877          Update_Sequence,
2878          Org_ID,
2879          Supplier_ID,
2880          Invoice_ID,
2881          Base_Currency_Code,
2882          Trx_Date,
2883          Payment_Num,
2884          Due_Date,
2885          Created_By,
2886          Amount_Remaining,
2887          Past_Due_Amount,
2888          Discount_Available,
2889          Discount_Taken,
2890          Discount_Lost,
2891          Payment_Amount,
2892          On_Time_Payment_Amt,
2893          Late_Payment_Amt,
2894          No_Days_Late,
2895          Due_Bucket1,
2896          Due_Bucket2,
2897          Due_Bucket3,
2898          Past_Due_Bucket1,
2899          Past_Due_Bucket2,
2900          Past_Due_Bucket3,
2901          Amount_Remaining_B,
2902          Past_Due_Amount_B,
2903          Discount_Available_B,
2904          Discount_Taken_B,
2905          Discount_Lost_B,
2906          Payment_Amount_B,
2907          On_Time_Payment_Amt_B,
2908          Late_Payment_Amt_B,
2909          Due_Bucket1_B,
2910          Due_Bucket2_B,
2911          Due_Bucket3_B,
2912          Past_Due_Bucket1_B,
2913          Past_Due_Bucket2_B,
2914          Past_Due_Bucket3_B,
2915          Prim_Amount_Remaining,
2916          Prim_Past_Due_Amount,
2917          Prim_Discount_Available,
2918          Prim_Discount_Taken,
2919          Prim_Discount_Lost,
2920          Prim_Payment_Amount,
2921          Prim_On_time_Payment_Amt,
2922          Prim_Late_Payment_Amt,
2923          Prim_Due_Bucket1,
2924          Prim_Due_Bucket2,
2925          Prim_Due_Bucket3,
2926          Prim_Past_Due_Bucket1,
2927          Prim_Past_Due_Bucket2,
2928          Prim_Past_Due_Bucket3,
2929          Sec_Amount_Remaining,
2930          Sec_Past_Due_Amount,
2931          Sec_Discount_Available,
2932          Sec_Discount_Taken,
2933          Sec_Discount_Lost,
2934          Sec_Payment_Amount,
2935          Sec_On_time_Payment_Amt,
2936          Sec_Late_Payment_Amt,
2937          Sec_Due_Bucket1,
2938          Sec_Due_Bucket2,
2939          Sec_Due_Bucket3,
2940          Sec_Past_Due_Bucket1,
2941          Sec_Past_Due_Bucket2,
2942          Sec_Past_Due_Bucket3,
2943 	 Fully_Paid_Date,
2944 	 Check_ID,
2945 	 Check_Date,
2946 	 Payment_Method,
2947          Creation_Date,
2948          Last_Updated_By,
2949          Last_Update_Date,
2950          Last_Update_Login,
2951          Inv_Pymt_Flag,
2952          Unique_ID)
2953   SELECT /*+ use_nl(PS) use_nl(TEMP) */
2954 	 TO_NUMBER(TO_CHAR(TEMP.Creation_Date,'J')) Time_ID,
2955 	 1 Period_Type_ID,
2956 	 TEMP.Creation_Date Action_Date,
2957 	 'PAYMENT' Action,
2958 	 g_seq_id Update_Sequence,
2959 	 AI.Org_ID Org_ID,
2960 	 AI.Supplier_ID Supplier_ID,
2961 	 AI.Invoice_ID Invoice_ID,
2962 	 AI.Base_Currency_Code Base_Currency_Code,
2963 	 AI.Invoice_Date Invoice_Date,
2964 	 PS.Payment_Num Payment_Num,
2965 	 TRUNC(PS.Due_Date) Due_Date,
2966 	 PS.Created_By Created_By,
2967 	 -1 * ROUND(TEMP.Prepay_Amount
2968               / NVL(FC.Minimum_Accountable_Unit, 0.01)) * NVL(FC.Minimum_Accountable_Unit, 0.01) Amount_Remaining,
2969 	 -1 * DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)), -1,
2970 	        ROUND(TEMP.Prepay_Amount
2971 		    /NVL(FC.Minimum_Accountable_Unit, 0.01)) * NVL(FC.Minimum_Accountable_Unit, 0.01), 0) Past_Due_Amount,
2972          0 Discount_Available,
2973          0 Discount_Taken,
2974          0 Discount_Lost,
2975          ROUND(TEMP.Prepay_Amount
2976               / NVL(FC.Minimum_Accountable_Unit, 0.01)) * NVL(FC.Minimum_Accountable_Unit, 0.01) Payment_Amount,
2977        	 DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)), -1, 0,
2978 		  ROUND(TEMP.Prepay_Amount
2979                        / NVL(FC.Minimum_Accountable_Unit, 0.01)) * NVL(FC.Minimum_Accountable_Unit, 0.01)) On_Time_Payment_Amt,
2980 	 DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)), -1,
2981 	          ROUND(TEMP.Prepay_Amount
2982 		       / NVL(FC.Minimum_Accountable_Unit, 0.01)) * NVL(FC.Minimum_Accountable_Unit, 0.01), 0) Late_Payment_Amt,
2983          DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)), -1,
2984 		(TRUNC(TEMP.Creation_Date) - TRUNC(PS.Due_Date)), 0) No_Days_Late,
2985          CASE
2986 	     WHEN (TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)) >= g_due_bucket1
2987 	     THEN -1 * ROUND(TEMP.Prepay_Amount
2988                             / NVL(FC.Minimum_Accountable_Unit, 0.01)) * NVL(FC.Minimum_Accountable_Unit, 0.01)
2989 	     ELSE 0
2990 	 END  Due_Bucket1,
2991          CASE
2992 	     WHEN (TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)) <= g_due_bucket2
2993 	     AND (TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)) > g_due_bucket3
2994 	     THEN -1 * ROUND(TEMP.Prepay_Amount
2995                             / NVL(FC.Minimum_Accountable_Unit, 0.01)) * NVL(FC.Minimum_Accountable_Unit, 0.01)
2996 	     ELSE 0
2997 	 END  Due_Bucket2,
2998          CASE
2999 	     WHEN (TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)) <= g_due_bucket3
3000 	     AND (TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)) >= 0
3001 	     THEN -1 * ROUND(TEMP.Prepay_Amount
3002                             / NVL(FC.Minimum_Accountable_Unit, 0.01)) * NVL(FC.Minimum_Accountable_Unit, 0.01)
3003 	     ELSE 0
3004 	 END Due_Bucket3,
3005          CASE
3006 	     WHEN (TRUNC(TEMP.Creation_Date) - TRUNC(PS.Due_Date)) >= g_past_bucket1
3007 	     THEN -1 * ROUND(TEMP.Prepay_Amount
3008                             / NVL(FC.Minimum_Accountable_Unit, 0.01)) * NVL(FC.Minimum_Accountable_Unit, 0.01)
3009 	     ELSE 0
3010 	 END Past_Due_Bucket1,
3011          CASE
3012 	     WHEN (TRUNC(TEMP.Creation_Date) - TRUNC(PS.Due_Date)) <= g_past_bucket2
3013 	     AND (TRUNC(TEMP.Creation_Date) - TRUNC(PS.Due_Date)) > g_past_bucket3
3014 	     THEN -1 * ROUND(TEMP.Prepay_Amount
3015                             / NVL(FC.Minimum_Accountable_Unit, 0.01)) * NVL(FC.Minimum_Accountable_Unit, 0.01)
3016 	     ELSE 0
3017 	 END Past_Due_Bucket2,
3018          CASE
3019 	     WHEN (TRUNC(TEMP.Creation_Date) - TRUNC(PS.Due_Date)) <= g_past_bucket3
3020 	     AND (TRUNC(TEMP.Creation_Date) - TRUNC(PS.Due_Date)) > 0
3021 	     THEN -1 * ROUND(TEMP.Prepay_Amount
3022                             / NVL(FC.Minimum_Accountable_Unit, 0.01)) * NVL(FC.Minimum_Accountable_Unit, 0.01)
3023 	     ELSE 0
3024 	 END Past_Due_Bucket3,
3025          -1 * ROUND((TEMP.Prepay_Amount * Conversion_Rate)
3026                    / NVL(FC.Minimum_Accountable_Unit, 0.01)) * NVL(FC.Minimum_Accountable_Unit, 0.01) Amount_Remaining_B,
3027 	 -1 * DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)), -1,
3028 		   ROUND((TEMP.Prepay_Amount * Conversion_Rate)
3029                         / NVL(FC.Minimum_Accountable_Unit, 0.01)) * NVL(FC.Minimum_Accountable_Unit, 0.01), 0) Past_Due_Amount_B,
3030          0 Discount_Available_B,
3031          0 Discount_Taken_B,
3032          0 Discount_Lost_B,
3033          ROUND((TEMP.Prepay_Amount * Conversion_Rate)
3034                      / NVL(FC.Minimum_Accountable_Unit, 0.01)) * NVL(FC.Minimum_Accountable_Unit, 0.01) Payment_Amount_B,
3035 	 DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)), -1, 0,
3036          	ROUND((TEMP.Prepay_Amount * Conversion_Rate)
3037                      / NVL(FC.Minimum_Accountable_Unit, 0.01)) * NVL(FC.Minimum_Accountable_Unit, 0.01)) On_Time_Payment_Amt_B,
3038 	 DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)), -1,
3039                 ROUND((TEMP.Prepay_Amount * Conversion_Rate)
3040                      / NVL(FC.Minimum_Accountable_Unit, 0.01)) * NVL(FC.Minimum_Accountable_Unit, 0.01), 0) Late_Payment_Amt_B,
3041          CASE
3042 	     WHEN (TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)) >= g_due_bucket1
3043 	     THEN -1 * ROUND((TEMP.Prepay_Amount * Conversion_Rate)
3044                             / NVL(FC.Minimum_Accountable_Unit, 0.01)) * NVL(FC.Minimum_Accountable_Unit, 0.01)
3045 	     ELSE 0
3046 	 END  Due_Bucket1_B,
3047          CASE
3048 	     WHEN (TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)) <= g_due_bucket2
3049 	     AND (TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)) > g_due_bucket3
3050 	     THEN -1 * ROUND((TEMP.Prepay_Amount * Conversion_Rate)
3051                             / NVL(FC.Minimum_Accountable_Unit, 0.01)) * NVL(FC.Minimum_Accountable_Unit, 0.01)
3052 	     ELSE 0
3053 	 END Due_Bucket2_B,
3054           CASE
3055 	     WHEN (TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)) <= g_due_bucket3
3056 	     AND (TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)) >= 0
3057 	     THEN -1 * ROUND((TEMP.Prepay_Amount * Conversion_Rate)
3058                             / NVL(FC.Minimum_Accountable_Unit, 0.01)) * NVL(FC.Minimum_Accountable_Unit, 0.01)
3059 	     ELSE 0
3060 	 END Due_Bucket3_B,
3061          CASE
3062 	     WHEN (TRUNC(TEMP.Creation_Date) - TRUNC(PS.Due_Date)) >= g_past_bucket1
3063 	     THEN -1 * ROUND((TEMP.Prepay_Amount * Conversion_Rate)
3064                             / NVL(FC.Minimum_Accountable_Unit, 0.01)) * NVL(FC.Minimum_Accountable_Unit, 0.01)
3065 	     ELSE 0
3066 	 END  Past_Due_Bucket1_B,
3067          CASE
3068 	     WHEN (TRUNC(TEMP.Creation_Date) - TRUNC(PS.Due_Date)) <= g_past_bucket2
3069 	     AND (TRUNC(TEMP.Creation_Date) - TRUNC(PS.Due_Date)) > g_past_bucket3
3070 	     THEN -1 * ROUND((TEMP.Prepay_Amount * Conversion_Rate)
3071                             / NVL(FC.Minimum_Accountable_Unit, 0.01)) * NVL(FC.Minimum_Accountable_Unit, 0.01)
3072 	     ELSE 0
3073 	 END Past_Due_Bucket2_B,
3074          CASE
3075 	     WHEN (TRUNC(TEMP.Creation_Date) - TRUNC(PS.Due_Date)) <= g_past_bucket3
3076 	     AND (TRUNC(TEMP.Creation_Date) - TRUNC(PS.Due_Date)) > 0
3077 	     THEN -1 * ROUND((TEMP.Prepay_Amount * Conversion_Rate)
3078                             / NVL(FC.Minimum_Accountable_Unit, 0.01)) * NVL(FC.Minimum_Accountable_Unit, 0.01)
3079 	     ELSE 0
3080 	 END Past_Due_Bucket3_B,
3081          -1 * ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, TEMP.Prepay_Amount,
3082                          ((TEMP.Prepay_Amount * Conversion_Rate) * RATES.Prim_Conversion_Rate))
3083                    / g_primary_mau) * g_primary_mau Prim_Amount_Remaining,
3084 	 -1 * DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)), -1,
3085          	ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, TEMP.Prepay_Amount,
3086                            ((TEMP.Prepay_Amount * Conversion_Rate) * RATES.Prim_Conversion_Rate))
3087                      / g_primary_mau) * g_primary_mau, 0) Prim_Past_Due_Amount,
3088          0 Prim_Discount_Available,
3089          0 Prim_Discount_Taken,
3090          0 Prim_Discount_Lost,
3091          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, TEMP.Prepay_Amount,
3092                     ((TEMP.Prepay_Amount * Conversion_Rate) * RATES.Prim_Conversion_Rate))
3093               / g_primary_mau) * g_primary_mau Prim_Payment_Amount,
3094 	 DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)), -1, 0,
3095          		(ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, TEMP.Prepay_Amount,
3096                                     ((TEMP.Prepay_Amount * Conversion_Rate) * RATES.Prim_Conversion_Rate))
3097                               / g_primary_mau) * g_primary_mau)) Prim_On_Time_Payment_Amt,
3098 	 DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)), -1,
3099          	ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, TEMP.Prepay_Amount,
3100                            ((TEMP.Prepay_Amount * Conversion_Rate) * RATES.Prim_Conversion_Rate))
3101                      / g_primary_mau) * g_primary_mau, 0) Prim_Late_Payment_Amt,
3102          CASE
3103 	     WHEN (TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)) >= g_due_bucket1
3104 	     THEN -1 * ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, TEMP.Prepay_Amount,
3105                                   ((TEMP.Prepay_Amount * Conversion_Rate) * RATES.Prim_Conversion_Rate))
3106                             / g_primary_mau) * g_primary_mau
3107 	     ELSE 0
3108 	 END  Prim_Due_Bucket1,
3109          CASE
3110 	     WHEN (TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)) <= g_due_bucket2
3111 	     AND (TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)) > g_due_bucket3
3112 	     THEN -1 * ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, TEMP.Prepay_Amount,
3113                                   ((TEMP.Prepay_Amount * Conversion_Rate) * RATES.Prim_Conversion_Rate))
3114                             / g_primary_mau) * g_primary_mau
3115 	     ELSE 0
3116 	 END Prim_Due_Bucket2,
3117           CASE
3118 	     WHEN (TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)) <= g_due_bucket3
3119 	     AND (TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)) >= 0
3120 	     THEN -1 * ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, TEMP.Prepay_Amount,
3121                                   ((TEMP.Prepay_Amount * Conversion_Rate) * RATES.Prim_Conversion_Rate))
3122                             / g_primary_mau) * g_primary_mau
3123 	     ELSE 0
3124 	 END Prim_Due_Bucket3,
3125          CASE
3126 	     WHEN (TRUNC(TEMP.Creation_Date) - TRUNC(PS.Due_Date)) >= g_past_bucket1
3127 	     THEN -1 * ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, TEMP.Prepay_Amount,
3128                                   ((TEMP.Prepay_Amount * Conversion_Rate) * RATES.Prim_Conversion_Rate))
3129                             / g_primary_mau) * g_primary_mau
3130 	     ELSE 0
3131 	 END Prim_Past_Due_Bucket1,
3132          CASE
3133 	     WHEN (TRUNC(TEMP.Creation_Date) - TRUNC(PS.Due_Date)) <= g_past_bucket2
3134 	     AND (TRUNC(TEMP.Creation_Date) - TRUNC(PS.Due_Date)) > g_past_bucket3
3135 	     THEN -1 * ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, TEMP.Prepay_Amount,
3136                                   ((TEMP.Prepay_Amount * Conversion_Rate) * RATES.Prim_Conversion_Rate))
3137                             / g_primary_mau) * g_primary_mau
3138 	     ELSE 0
3139 	 END Prim_Past_Due_Bucket2,
3140          CASE
3141 	     WHEN (TRUNC(TEMP.Creation_Date) - TRUNC(PS.Due_Date)) <= g_past_bucket3
3142 	     AND (TRUNC(TEMP.Creation_Date) - TRUNC(PS.Due_Date)) > 0
3143 	     THEN -1 * ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency, TEMP.Prepay_Amount,
3144                                   ((TEMP.Prepay_Amount * Conversion_Rate) * RATES.Prim_Conversion_Rate))
3145                             / g_primary_mau) * g_primary_mau
3146 	     ELSE 0
3147 	 END Prim_Past_Due_Bucket3,
3148          -1 * ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, TEMP.Prepay_Amount,
3149                          ((TEMP.Prepay_Amount * Conversion_Rate) * RATES.Sec_Conversion_Rate))
3150                    / g_secondary_mau) * g_secondary_mau Sec_Amount_Remaining,
3151   	 -1 * DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)), -1,
3152 		ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, TEMP.Prepay_Amount,
3153                            ((TEMP.Prepay_Amount * Conversion_Rate) * RATES.Sec_Conversion_Rate))
3154                      / g_secondary_mau) * g_secondary_mau, 0) Sec_Past_Due_Amount,
3155          0 Sec_Discount_Available,
3156          0 Sec_Discount_Taken,
3157          0 Sec_Discount_Lost,
3158          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, TEMP.Prepay_Amount,
3159                     ((TEMP.Prepay_Amount * Conversion_Rate) * RATES.Sec_Conversion_Rate))
3160               / g_secondary_mau) * g_secondary_mau Sec_Payment_Amount,
3161          DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)), -1, 0,
3162               		(ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, TEMP.Prepay_Amount,
3163                                     ((TEMP.Prepay_Amount * Conversion_Rate) * RATES.Sec_Conversion_Rate))
3164                               / g_secondary_mau)) * g_secondary_mau) Sec_On_Time_Payment_Amt,
3165 	 DECODE(SIGN(TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)), -1,
3166          	ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, TEMP.Prepay_Amount,
3167                            ((TEMP.Prepay_Amount * Conversion_Rate) * RATES.Sec_Conversion_Rate))
3168                      / g_secondary_mau) * g_secondary_mau, 0) Sec_Late_Payment_Amt,
3169          CASE
3170 	     WHEN (TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)) >= g_due_bucket1
3171 	     THEN -1 * ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, TEMP.Prepay_Amount,
3172                                   ((TEMP.Prepay_Amount * Conversion_Rate) * RATES.Sec_Conversion_Rate))
3173                             / g_secondary_mau) * g_secondary_mau
3174 	     ELSE 0
3175 	 END  Sec_Due_Bucket1,
3176          CASE
3177 	     WHEN (TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)) <= g_due_bucket2
3178 	     AND (TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)) > g_due_bucket3
3179 	     THEN -1 * ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, TEMP.Prepay_Amount,
3180                                   ((TEMP.Prepay_Amount * Conversion_Rate) * RATES.Sec_Conversion_Rate))
3181                             / g_secondary_mau) * g_secondary_mau
3182 	     ELSE 0
3183 	 END Sec_Due_Bucket2,
3184           CASE
3185 	     WHEN (TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)) <= g_due_bucket3
3186 	     AND (TRUNC(PS.Due_Date) - TRUNC(TEMP.Creation_Date)) >= 0
3187 	     THEN -1 * ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, TEMP.Prepay_Amount,
3188                                   ((TEMP.Prepay_Amount * Conversion_Rate) * RATES.Sec_Conversion_Rate))
3189                             / g_secondary_mau) * g_secondary_mau
3190 	     ELSE 0
3191 	 END Sec_Due_Bucket3,
3192          CASE
3193 	     WHEN (TRUNC(TEMP.Creation_Date) - TRUNC(PS.Due_Date)) >= g_past_bucket1
3194 	     THEN -1 * ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, TEMP.Prepay_Amount,
3195                                   ((TEMP.Prepay_Amount * Conversion_Rate) * RATES.Sec_Conversion_Rate))
3196                             / g_secondary_mau) * g_secondary_mau
3197 	     ELSE 0
3198 	 END Sec_Past_Due_Bucket1,
3199          CASE
3200 	     WHEN (TRUNC(TEMP.Creation_Date) - TRUNC(PS.Due_Date)) <= g_past_bucket2
3201 	     AND (TRUNC(TEMP.Creation_Date) - TRUNC(PS.Due_Date)) > g_past_bucket3
3202 	     THEN -1 * ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, TEMP.Prepay_Amount,
3203                                   ((TEMP.Prepay_Amount * Conversion_Rate) * RATES.Sec_Conversion_Rate))
3204                             / g_secondary_mau) * g_secondary_mau
3205 	     ELSE 0
3206 	 END Sec_Past_Due_Bucket2,
3207          CASE
3208 	     WHEN (TRUNC(TEMP.Creation_Date) - TRUNC(PS.Due_Date)) <= g_past_bucket3
3209 	     AND (TRUNC(TEMP.Creation_Date) - TRUNC(PS.Due_Date)) > 0
3210 	     THEN -1 * ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency, TEMP.Prepay_Amount,
3211                                   ((TEMP.Prepay_Amount * Conversion_Rate) * RATES.Sec_Conversion_Rate))
3212                             / g_secondary_mau) * g_secondary_mau
3213 	     ELSE 0
3214 	 END Sec_Past_Due_Bucket3,
3215 	 CASE
3216 		WHEN TEMP.PP_WH_Tax_Pay_Amount = PS.Gross_Amount
3217 		THEN TEMP.Creation_Date ELSE NULL
3218 	 END Fully_Paid_Date,
3219 	 AC.Check_ID Check_ID,
3220 	 AC.Check_Date Check_Date,
3221    DECODE(IBY_SYS_PROF_B.Processing_Type,NULL,DECODE(AC.Payment_Method_Lookup_Code, 'EFT', 'E', 'WIRE', 'E', 'M')
3222    ,DECODE(IBY_SYS_PROF_B.Processing_Type, 'ELECTRONIC', 'E', 'M')) PAYMENT_METHOD,
3223 	 sysdate Creation_Date,
3224 	 g_fii_user_id Last_Updated_By,
3225 	 sysdate Last_Update_Date,
3226 	 g_fii_login_id Last_Update_Login,
3227          'N' Inv_Pymt_Flag,
3228          AC.Check_ID Unique_ID
3229   FROM   FII_AP_INVOICE_B AI,
3230 	 AP_Payment_Schedules_All PS,
3231         (SELECT /*+ use_nl(aps3,pp) */APS3.Invoice_ID,
3232                 APS3.Payment_Num,
3233                 APS3.Creation_Date,
3234                 APS3.Prepay_Amount,
3235                 APS3.Check_ID,
3236                 APS3.WH_Tax_Pay_Amount + NVL(SUM(CASE WHEN PP.Creation_Date IS NOT NULL
3237                                                       AND  PP.Creation_Date <= APS3.Creation_Date
3238                                                       THEN PP.Prepay_Amount ELSE 0 END),0) PP_WH_Tax_Pay_Amount
3239          FROM
3240                (SELECT /*+ use_nl(aps2,awts) */APS2.Invoice_ID,
3241                        APS2.Payment_Num,
3242                        APS2.Creation_Date,
3243                        APS2.Prepay_Amount,
3244                        APS2.Check_ID,
3245                        APS2.Pay_Amount + NVL(SUM(CASE WHEN AWTS.Creation_Date IS NOT NULL
3246                                                       AND  AWTS.Creation_Date <= APS2.Creation_Date
3247                                                       THEN AWTS.WH_Tax_Amount ELSE 0 END),0) WH_Tax_Pay_Amount
3248                 FROM
3249                       (SELECT /*+ use_nl(aps1, apc) */APS1.Invoice_ID,
3250                               APS1.Payment_Num,
3251                               APS1.Creation_Date,
3252 	                      APS1.Prepay_Amount,
3253 	                      APS1.Check_ID,
3254                               NVL(SUM(CASE WHEN APC.Invp_Creation_Date IS NOT NULL
3255                                            AND  APC.Invp_Creation_Date <= APS1.Creation_Date
3256                                            THEN APC.Payment_Amount ELSE 0 END),0) Pay_Amount
3257                        FROM FII_AP_Prepay_T APS1,
3258                             FII_AP_Pay_Chk_Stg APC
3259                        WHERE APS1.Invoice_ID BETWEEN g_start_range and g_end_range
3260                        AND   APS1.Invoice_ID = APC.Invoice_ID (+)
3261                        AND   APS1.Payment_Num = APC.Payment_Num (+)
3262                        Group By APS1.Invoice_ID,
3263                                 APS1.Payment_Num,
3264                                 APS1.Creation_Date,
3265                                 APS1.Prepay_Amount,
3266                                 APS1.Check_ID) APS2,
3267                        FII_AP_WH_Tax_T AWTS
3268                 WHERE APS2.Invoice_ID = AWTS.Invoice_ID (+)
3269                 AND   APS2.Payment_Num = AWTS.Payment_Num (+)
3270                 Group By APS2.Invoice_ID,
3271                          APS2.Payment_Num,
3272                          APS2.Creation_Date,
3273                          APS2.Prepay_Amount,
3274                          APS2.Check_ID,
3275                          APS2.Pay_Amount) APS3,
3276                 FII_AP_Prepay_T PP
3277          WHERE APS3.Invoice_ID = PP.Invoice_ID (+)
3278          AND   APS3.Payment_Num = PP.Payment_Num (+)
3279          Group By APS3.Invoice_ID,
3280                   APS3.Payment_Num,
3281                   APS3.Creation_Date,
3282                   APS3.Prepay_Amount,
3283                   APS3.Check_ID,
3284                   APS3.WH_Tax_Pay_Amount) TEMP,
3285 	 FII_AP_PS_Rates_Temp RATES,
3286 	 FII_AP_Func_Rates_Temp FRATES,
3287 	 AP_Checks_All AC,
3288           -- IBY_Payment_Profiles IBYPM--IBY CHANGE
3289                 IBY_SYS_PMT_PROFILES_B IBY_SYS_PROF_B,--IBY CHANGE
3290                 IBY_ACCT_PMT_PROFILES_B IBY_ACCT_PROF_B,--IBY CHANGE
3291 	 FND_Currencies FC
3292   WHERE  AI.Invoice_ID BETWEEN g_start_range and g_end_range
3293   AND    AI.Invoice_ID = TEMP.Invoice_ID
3294   AND	 TEMP.Invoice_ID = PS.Invoice_ID
3295   AND	 TEMP.Payment_Num = PS.Payment_Num
3296   AND	 TEMP.Check_ID = AC.Check_ID
3297   AND   AC.Payment_Profile_ID = IBY_ACCT_PROF_B.Payment_Profile_ID(+)--IBY CHANGE
3298   AND    IBY_ACCT_PROF_B.system_profile_code = IBY_SYS_PROF_B.system_profile_code(+)--IBY CHANGE
3299   AND    AI.Invoice_Type NOT IN ('PREPAYMENT')
3300   AND    AI.Cancel_Date IS NULL
3301   AND    FRATES.To_Currency   = AI.Base_Currency_Code
3302   AND    FRATES.From_Currency = AI.Payment_Currency_Code
3303   AND    FRATES.Trx_Date      = AI.Exchange_Date
3304   AND    DECODE(AI.Exchange_Rate_Type,'User', AI.Exchange_Rate,1) =
3305                  DECODE(FRATES.Conversion_Type,'User', FRATES.Conversion_Rate,1)
3306   AND    FRATES.Conversion_Type    = AI.Exchange_Rate_Type
3307   AND    RATES.Functional_Currency = AI.Base_Currency_Code
3308   AND    RATES.Trx_Date            = AI.Invoice_Date
3309   AND    AI.Payment_Currency_Code = FC.Currency_Code;
3310 
3311   if g_debug_flag = 'Y' then
3312      FII_UTIL.put_line('Inserted ' ||SQL%ROWCOUNT|| ' Prepayment records into FII_AP_PAY_SCHED_B ');
3313      FII_UTIL.put_timestamp('End Timestamp');
3314      FII_UTIL.stop_timer;
3315      FII_UTIL.print_timer('Duration');
3316      FII_UTIL.put_line('');
3317   end if;
3318 
3319   commit;
3320 
3321 
3322 
3323 
3324 EXCEPTION
3325    WHEN OTHERS THEN
3326       g_errbuf:=sqlerrm;
3327       g_retcode:= -1;
3328       g_exception_msg  := g_retcode || ':' || g_errbuf;
3329       FII_UTIL.put_line('Error occured while ' || g_state);
3330       FII_UTIL.put_line(g_exception_msg);
3331       RAISE;
3332 END;
3333 
3334 
3335 ------------------------------------------------------------------
3336 -- Procedure POPULATE_PS_DISCOUNT_ACTION
3337 -- Purpose
3338 --   This POPULATE_PS_DISCOUNT_ACTION routine inserts records into the
3339 --   FII AP Payment Schedule summary table all the discount actions.
3340 ------------------------------------------------------------------
3341 
3342 PROCEDURE POPULATE_PS_DISCOUNT_ACTION IS
3343 
3344 BEGIN
3345 
3346   g_state := 'Inside the procedure POPULATE_PS_DISC_ACTION';
3347   if g_debug_flag = 'Y' then
3348      FII_UTIL.put_line('');
3349      FII_UTIL.put_line(g_state);
3350   end if;
3351 
3352   g_state := 'Inserting the Payment Schedules Discount Action';
3353   if g_debug_flag = 'Y' then
3354      FII_UTIL.put_line('');
3355      FII_UTIL.put_line(g_state);
3356      FII_UTIL.put_timestamp('Start Timestamp');
3357      FII_UTIL.start_timer;
3358      FII_UTIL.put_line('');
3359   end if;
3360 
3361   /* For Discount and Due actions we will select the payment schedules whose
3362      discount or due date falls between the given from date and the least of
3363      to date or yesterday's date. This way we will not create wrong discount
3364      lost and past due amounts for due action */
3365 
3366   /* Inserting the Discount Date passed records into the summary table.
3367      We will insert only those payment schedules which have not been paid
3368      fully before the first discount date. This check is done by comparing
3369      the gross amount with the payment, prepayment and withheld amount */
3370 
3371   INSERT INTO FII_AP_PAY_SCHED_B b
3372         (Time_ID,
3373          Period_Type_ID,
3374          Action_Date,
3375          Action,
3376          Update_Sequence,
3377          Org_ID,
3378          Supplier_ID,
3379          Invoice_ID,
3380          Base_Currency_Code,
3381          Trx_Date,
3382          Payment_Num,
3383          Due_Date,
3384          Created_By,
3385          Amount_Remaining,
3386          Past_Due_Amount,
3387          Discount_Available,
3388          Discount_Taken,
3389          Discount_Lost,
3390          Payment_Amount,
3391          On_Time_Payment_Amt,
3392          Late_Payment_Amt,
3393          No_Days_Late,
3394          Due_Bucket1,
3395          Due_Bucket2,
3396          Due_Bucket3,
3397          Past_Due_Bucket1,
3398          Past_Due_Bucket2,
3399          Past_Due_Bucket3,
3400          Amount_Remaining_B,
3401          Past_Due_Amount_B,
3402          Discount_Available_B,
3403          Discount_Taken_B,
3404          Discount_Lost_B,
3405          Payment_Amount_B,
3406          On_Time_Payment_Amt_B,
3407          Late_Payment_Amt_B,
3408          Due_Bucket1_B,
3409          Due_Bucket2_B,
3410          Due_Bucket3_B,
3411          Past_Due_Bucket1_B,
3412          Past_Due_Bucket2_B,
3413          Past_Due_Bucket3_B,
3414          Prim_Amount_Remaining,
3415          Prim_Past_Due_Amount,
3416          Prim_Discount_Available,
3417          Prim_Discount_Taken,
3418          Prim_Discount_Lost,
3419          Prim_Payment_Amount,
3420          Prim_On_time_Payment_Amt,
3421          Prim_Late_Payment_Amt,
3422          Prim_Due_Bucket1,
3423          Prim_Due_Bucket2,
3424          Prim_Due_Bucket3,
3425          Prim_Past_Due_Bucket1,
3426          Prim_Past_Due_Bucket2,
3427          Prim_Past_Due_Bucket3,
3428          Sec_Amount_Remaining,
3429          Sec_Past_Due_Amount,
3430          Sec_Discount_Available,
3431          Sec_Discount_Taken,
3432          Sec_Discount_Lost,
3433          Sec_Payment_Amount,
3434          Sec_On_time_Payment_Amt,
3435          Sec_Late_Payment_Amt,
3436          Sec_Due_Bucket1,
3437          Sec_Due_Bucket2,
3438          Sec_Due_Bucket3,
3439          Sec_Past_Due_Bucket1,
3440          Sec_Past_Due_Bucket2,
3441          Sec_Past_Due_Bucket3,
3442          Creation_Date,
3443          Last_Updated_By,
3444          Last_Update_Date,
3445          Last_Update_Login,
3446          Unique_ID)
3447   SELECT /*+ leading(ai) index(AI,FII_AP_INVOICE_B_U1) index(FRATES) use_nl(ps)  */
3448          TO_NUMBER(TO_CHAR(PS.Discount_Date + 1,'J')) Time_ID,
3449          1 Period_Type_ID,
3450          TRUNC(PS.Discount_Date) + 1 Action_Date,
3451          'DISCOUNT' Action,
3452          g_seq_id Update_Sequence,
3453          AI.Org_ID Org_ID,
3454          AI.Supplier_ID Supplier_ID,
3455          AI.Invoice_ID Invoice_ID,
3456          AI.Base_Currency_Code Base_Currency_Code,
3457          AI.Invoice_Date Trx_Date,
3458          PS.Payment_Num Payment_Num,
3459          TRUNC(PS.Due_Date) Due_Date,
3460          PS.Created_By Created_By,
3461          0 Amount_Remaining,
3462          0 Past_Due_Amount,
3463          -1 * (NVL(PS.Discount_Amount_Available,0) - NVL(APC.Discount_Taken ,0)
3464             -  NVL(PS.Second_Disc_Amt_Available,0)) Discount_Available,
3465          0 Discount_Taken,
3466          NVL(PS.Discount_Amount_Available,0) - NVL(APC.Discount_Taken ,0)
3467             - NVL(PS.Second_Disc_Amt_Available,0) Discount_Lost,
3468          0 Payment_Amount,
3469          0 On_Time_Payment_Amt,
3470          0 Late_Payment_Amt,
3471          0 No_Days_Late,
3472          0 Due_Bucket1,
3473          0 Due_Bucket2,
3474          0 Due_Bucket3,
3475          0 Past_Due_Bucket1,
3476          0 Past_Due_Bucket2,
3477          0 Past_Due_Bucket3,
3478          0 Amount_Remaining_B,
3479          0 Past_Due_Amount_B,
3480          ROUND((-1 * (NVL(PS.Discount_Amount_Available,0) - NVL(APC.Discount_Taken ,0)
3481                        -  NVL(PS.Second_Disc_Amt_Available,0)) * Conversion_Rate)
3482                    / Functional_MAU) * Functional_MAU Discount_Available_B,
3483          0 Discount_Taken_B,
3484          ROUND(((NVL(PS.Discount_Amount_Available,0) - NVL(APC.Discount_Taken ,0)
3485                        -  NVL(PS.Second_Disc_Amt_Available,0)) * Conversion_Rate)
3486                        / Functional_MAU) * Functional_MAU Discount_Lost_B,
3487          0 Payment_Amount_B,
3488          0 On_Time_Payment_Amt_B,
3489          0 Late_Payment_Amt_B,
3490          0 Due_Bucket1_B,
3491          0 Due_Bucket2_B,
3492          0 Due_Bucket3_B,
3493          0 Past_Due_Bucket1_B,
3494          0 Past_Due_Bucket2_B,
3495          0 Past_Due_Bucket3_B,
3496          0 Prim_Amount_Remaining,
3497          0 Prim_Past_Due_Amount,
3498          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency,
3499                        -1 * (NVL(PS.Discount_Amount_Available,0) - NVL(APC.Discount_Taken ,0)
3500                        -  NVL(PS.Second_Disc_Amt_Available,0)),
3501                      ((-1 * (NVL(PS.Discount_Amount_Available,0) - NVL(APC.Discount_Taken ,0)
3502                        -  NVL(PS.Second_Disc_Amt_Available,0)) * Conversion_Rate)
3503              * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau Prim_Discount_Available,
3504          0 Prim_Discount_Taken,
3505          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency,
3506                         NVL(PS.Discount_Amount_Available,0) - NVL(APC.Discount_Taken ,0)
3507                         -  NVL(PS.Second_Disc_Amt_Available,0),
3508                      (((NVL(PS.Discount_Amount_Available,0) - NVL(APC.Discount_Taken ,0)
3509                        -  NVL(PS.Second_Disc_Amt_Available,0)) * Conversion_Rate)
3510              * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau Prim_Discount_Lost,
3511          0 Prim_Payment_Amount,
3512          0 Prim_On_Time_Payment_Amt,
3513          0 Prim_Late_Payment_Amt,
3514          0 Prim_Due_Bucket1,
3515          0 Prim_Due_Bucket2,
3516          0 Prim_Due_Bucket3,
3517          0 Prim_Past_Due_Bucket1,
3518          0 Prim_Past_Due_Bucket2,
3519          0 Prim_Past_Due_Bucket3,
3520          0 Sec_Amount_Remaining,
3521          0 Sec_Past_Due_Amount,
3522          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency,
3523                        -1 * (NVL(PS.Discount_Amount_Available,0) - NVL(APC.Discount_Taken ,0)
3524                        -  NVL(PS.Second_Disc_Amt_Available,0)),
3525                      ((-1 * (NVL(PS.Discount_Amount_Available,0) - NVL(APC.Discount_Taken ,0)
3526                        -  NVL(PS.Second_Disc_Amt_Available,0)) * Conversion_Rate)
3527              * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau Sec_Discount_Available,
3528          0 Sec_Discount_Taken,
3529          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency,
3530                        NVL(PS.Discount_Amount_Available,0) - NVL(APC.Discount_Taken ,0)
3531                        -  NVL(PS.Second_Disc_Amt_Available,0),
3532                     (((NVL(PS.Discount_Amount_Available,0) - NVL(APC.Discount_Taken ,0)
3533                        -  NVL(PS.Second_Disc_Amt_Available,0)) * Conversion_Rate)
3534              * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau Sec_Discount_Lost,
3535          0 Sec_Payment_Amount,
3536          0 Sec_On_Time_Payment_Amt,
3537          0 Sec_Late_Payment_Amt,
3538          0 Sec_Due_Bucket1,
3539          0 Sec_Due_Bucket2,
3540          0 Sec_Due_Bucket3,
3541          0 Sec_Past_Due_Bucket1,
3542          0 Sec_Past_Due_Bucket2,
3543          0 Sec_Past_Due_Bucket3,
3544          sysdate Creation_Date,
3545          g_fii_user_id Last_Updated_By,
3546          sysdate Last_Update_Date,
3547          g_fii_login_id Last_Update_Login,
3548          1 Unique_ID
3549   FROM   FII_AP_Invoice_B AI,
3550          FII_AP_PS_Rates_Temp   RATES,
3551          FII_AP_Func_Rates_Temp FRATES,
3552 	 AP_Payment_Schedules_All PS,
3553         (SELECT /*+ index(TEM,FII_AP_WH_TAX_T_N1) */ TEM.Invoice_ID,
3554                 TEM.Payment_Num,
3555                 SUM(NVL(TEM.WH_Tax_Amount, 0)) WH_Tax_Amount
3556          FROM   FII_AP_WH_Tax_T TEM
3557          WHERE  TEM.Invoice_id BETWEEN g_start_range and g_end_range
3558          AND    TEM.Invoice_Type <> 'PREPAYMENT'
3559          AND    TRUNC(TEM.Discount_Date) + 1 BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
3560          AND    TEM.Creation_Date < TRUNC(TEM.Discount_Date) + 1
3561          GROUP  BY TEM.Invoice_ID,
3562                    TEM.Payment_Num,
3563                    TEM.Discount_Date) TEMP,
3564         (SELECT /*+ index(prep,FII_AP_PREPAY_T_N1) */ PREP.Invoice_ID,
3565                 PREP.Payment_Num,
3566                 SUM(NVL(PREP.Prepay_Amount, 0)) Prepay_Amount
3567          FROM   FII_AP_Prepay_T PREP
3568          WHERE  PREP.Invoice_id BETWEEN g_start_range and g_end_range
3569          AND    TRUNC(PREP.Discount_Date) + 1 BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
3570          AND    PREP.Creation_Date < TRUNC(PREP.Discount_Date) + 1
3571          GROUP  BY PREP.Invoice_ID,
3572                    PREP.Payment_Num,
3573                    PREP.Discount_Date) PP,
3574         (SELECT /*+ index(PC,FII_AP_PAY_CHK_STG_N1) */ PC.Invoice_ID,
3575                 PC.Payment_Num,
3576                 SUM(NVL(PC.Discount_Taken, 0)) Discount_Taken,
3577                 SUM(NVL(PC.Payment_Amount, 0)) Payment_Amount
3578          FROM   FII_AP_PAY_CHK_STG PC
3579          WHERE  PC.Invoice_id BETWEEN g_start_range and g_end_range
3580          AND    PC.Invoice_Type <> 'PREPAYMENT'
3581          AND    TRUNC(PC.Discount_Date) + 1 BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
3582          AND    PC.Invp_Creation_Date < TRUNC(PC.Discount_Date) + 1
3583          GROUP  BY PC.Invoice_ID,
3584                    PC.Payment_Num,
3585                    PC.Discount_Date) APC
3586   WHERE  PS.Invoice_ID = AI.Invoice_ID
3587   AND    AI.Invoice_ID BETWEEN g_start_range and g_end_range
3588   AND    AI.Invoice_Type NOT IN ('PREPAYMENT')
3589   AND    AI.Cancel_Date IS NULL
3590   AND    FRATES.To_Currency   = AI.Base_Currency_Code
3591   AND    FRATES.From_Currency = AI.Payment_Currency_Code
3592   AND    FRATES.Trx_Date      = AI.Exchange_Date
3593   AND    DECODE(AI.Exchange_Rate_Type,'User', AI.Exchange_Rate,1) =
3594                  DECODE(FRATES.Conversion_Type,'User',FRATES.Conversion_Rate,1)
3595   AND    FRATES.Conversion_Type    = AI.Exchange_Rate_Type
3596   AND    RATES.Functional_Currency = AI.Base_Currency_Code
3597   AND    RATES.Trx_Date            = AI.Invoice_Date
3598   AND    TRUNC(PS.Discount_Date) + 1 BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
3599   AND    PS.Invoice_ID  = TEMP.Invoice_ID(+)
3600   AND    PS.Payment_Num = TEMP.Payment_Num(+)
3601   AND    PS.Invoice_ID  = PP.Invoice_ID(+)
3602   AND    PS.Payment_Num = PP.Payment_Num(+)
3603   AND    PS.Invoice_ID  = APC.Invoice_ID(+)
3604   AND    PS.Payment_Num = APC.Payment_Num(+)
3605   AND    ABS(NVL(APC.Payment_Amount,0) + NVL(TEMP.WH_Tax_Amount,0)
3606                                        + NVL(PP.Prepay_Amount,0)) < ABS(PS.Gross_Amount);
3607 
3608 
3609   if g_debug_flag = 'Y' then
3610      FII_UTIL.put_line('Inserted ' ||SQL%ROWCOUNT|| ' Discount records into FII_AP_PAY_SCHED_B');
3611      FII_UTIL.put_timestamp('End Timestamp');
3612      FII_UTIL.stop_timer;
3613      FII_UTIL.print_timer('Duration');
3614      FII_UTIL.put_line('');
3615   end if;
3616 
3617   commit;
3618 
3619   g_state := 'Inserting the Payment Schedules Second Discount Records';
3620   if g_debug_flag = 'Y' then
3621      FII_UTIL.put_line('');
3622      FII_UTIL.put_line(g_state);
3623      FII_UTIL.put_timestamp('Start Timestamp');
3624      FII_UTIL.start_timer;
3625      FII_UTIL.put_line('');
3626   end if;
3627 
3628 
3629   /* Inserting the Discount Date passed records into the summary table.
3630      We will insert only those payment schedules which have not be paid
3631      fully before the second discount date. This check is done by comparing
3632      the gross amount with the payment, prepayment and withheld amount */
3633 
3634   INSERT INTO FII_AP_PAY_SCHED_B b
3635         (Time_ID,
3636          Period_Type_ID,
3637          Action_Date,
3638          Action,
3639          Update_Sequence,
3640          Org_ID,
3641          Supplier_ID,
3642          Invoice_ID,
3643          Base_Currency_Code,
3644          Trx_Date,
3645          Payment_Num,
3646          Due_Date,
3647          Created_By,
3648          Amount_Remaining,
3649          Past_Due_Amount,
3650          Discount_Available,
3651          Discount_Taken,
3652          Discount_Lost,
3653          Payment_Amount,
3654          On_Time_Payment_Amt,
3655          Late_Payment_Amt,
3656          No_Days_Late,
3657          Due_Bucket1,
3658          Due_Bucket2,
3659          Due_Bucket3,
3660          Past_Due_Bucket1,
3661          Past_Due_Bucket2,
3662          Past_Due_Bucket3,
3663          Amount_Remaining_B,
3664          Past_Due_Amount_B,
3665          Discount_Available_B,
3666          Discount_Taken_B,
3667          Discount_Lost_B,
3668          Payment_Amount_B,
3669          On_Time_Payment_Amt_B,
3670          Late_Payment_Amt_B,
3671          Due_Bucket1_B,
3672          Due_Bucket2_B,
3673          Due_Bucket3_B,
3674          Past_Due_Bucket1_B,
3675          Past_Due_Bucket2_B,
3676          Past_Due_Bucket3_B,
3677          Prim_Amount_Remaining,
3678          Prim_Past_Due_Amount,
3679          Prim_Discount_Available,
3680          Prim_Discount_Taken,
3681          Prim_Discount_Lost,
3682          Prim_Payment_Amount,
3683          Prim_On_time_Payment_Amt,
3684          Prim_Late_Payment_Amt,
3685          Prim_Due_Bucket1,
3686          Prim_Due_Bucket2,
3687          Prim_Due_Bucket3,
3688          Prim_Past_Due_Bucket1,
3689          Prim_Past_Due_Bucket2,
3690          Prim_Past_Due_Bucket3,
3691          Sec_Amount_Remaining,
3692          Sec_Past_Due_Amount,
3693          Sec_Discount_Available,
3694          Sec_Discount_Taken,
3695          Sec_Discount_Lost,
3696          Sec_Payment_Amount,
3697          Sec_On_time_Payment_Amt,
3698          Sec_Late_Payment_Amt,
3699          Sec_Due_Bucket1,
3700          Sec_Due_Bucket2,
3701          Sec_Due_Bucket3,
3702          Sec_Past_Due_Bucket1,
3703          Sec_Past_Due_Bucket2,
3704          Sec_Past_Due_Bucket3,
3705          Creation_Date,
3706          Last_Updated_By,
3707          Last_Update_Date,
3708          Last_Update_Login,
3709          Unique_ID)
3710   SELECT /*+ leading(ai) index(AI,FII_AP_INVOICE_B_U1) index(FRATES) use_nl(ps)  */
3711          TO_NUMBER(TO_CHAR(PS.Second_Discount_Date + 1,'J')) Time_ID,
3712          1 Period_Type_ID,
3713          TRUNC(PS.Second_Discount_Date) + 1 Action_Date,
3714          'DISCOUNT' Action,
3715          g_seq_id Update_Sequence,
3716          AI.Org_ID Org_ID,
3717          AI.Supplier_ID Supplier_ID,
3718          AI.Invoice_ID Invoice_ID,
3719          AI.Base_Currency_Code Base_Currency_Code,
3720          AI.Invoice_Date Trx_Date,
3721          PS.Payment_Num Payment_Num,
3722          TRUNC(PS.Due_Date) Due_Date,
3723          PS.Created_By Created_By,
3724          0 Amount_Remaining,
3725          0 Past_Due_Amount,
3726          -1 * (NVL(PS.Second_Disc_Amt_Available,0) - NVL(DISC.Discount_Taken,0)
3727             -  NVL(PS.Third_Disc_Amt_Available,0)) Discount_Available,
3728          0 Discount_Taken,
3729          NVL(PS.Second_Disc_Amt_Available,0) - NVL(DISC.Discount_Taken,0)
3730             - NVL(PS.Third_Disc_Amt_Available,0) Discount_Lost,
3731          0 Payment_Amount,
3732          0 On_Time_Payment_Amt,
3733          0 Late_Payment_Amt,
3734          0 No_Days_Late,
3735          0 Due_Bucket1,
3736          0 Due_Bucket2,
3737          0 Due_Bucket3,
3738          0 Past_Due_Bucket1,
3739          0 Past_Due_Bucket2,
3740          0 Past_Due_Bucket3,
3741          0 Amount_Remaining_B,
3742          0 Past_Due_Amount_B,
3743          ROUND((-1 * (NVL(PS.Second_Disc_Amt_Available,0) - NVL(DISC.Discount_Taken,0)
3744                         -  NVL(PS.Third_Disc_Amt_Available,0)) * Conversion_Rate)
3745                    / Functional_MAU) * Functional_MAU Discount_Available_B,
3746          0 Discount_Taken_B,
3747          ROUND(((NVL(PS.Second_Disc_Amt_Available,0) - NVL(DISC.Discount_Taken,0)
3748                         -  NVL(PS.Third_Disc_Amt_Available,0)) * Conversion_Rate)
3749                    / Functional_MAU) * Functional_MAU Discount_Lost_B,
3750          0 Payment_Amount_B,
3751          0 On_Time_Payment_Amt_B,
3752          0 Late_Payment_Amt_B,
3753          0 Due_Bucket1_B,
3754          0 Due_Bucket2_B,
3755          0 Due_Bucket3_B,
3756          0 Past_Due_Bucket1_B,
3757          0 Past_Due_Bucket2_B,
3758          0 Past_Due_Bucket3_B,
3759          0 Prim_Amount_Remaining,
3760          0 Prim_Past_Due_Amount,
3761          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency,
3762                         -1 * (NVL(PS.Second_Disc_Amt_Available,0) - NVL(DISC.Discount_Taken,0)
3763                         -  NVL(PS.Third_Disc_Amt_Available,0)),
3764                       ((-1 * (NVL(PS.Second_Disc_Amt_Available,0) - NVL(DISC.Discount_Taken,0)
3765                         -  NVL(PS.Third_Disc_Amt_Available,0)) * Conversion_Rate)
3766              * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau Prim_Discount_Available,
3767          0 Prim_Discount_Taken,
3768          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency,
3769                         NVL(PS.Second_Disc_Amt_Available,0) - NVL(DISC.Discount_Taken,0)
3770                         -  NVL(PS.Third_Disc_Amt_Available,0),
3771                      (((NVL(PS.Second_Disc_Amt_Available,0) - NVL(DISC.Discount_Taken,0)
3772                         -  NVL(PS.Third_Disc_Amt_Available,0)) * Conversion_Rate)
3773              * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau Prim_Discount_Lost,
3774          0 Prim_Payment_Amount,
3775          0 Prim_On_Time_Payment_Amt,
3776          0 Prim_Late_Payment_Amt,
3777          0 Prim_Due_Bucket1,
3778          0 Prim_Due_Bucket2,
3779          0 Prim_Due_Bucket3,
3780          0 Prim_Past_Due_Bucket1,
3781          0 Prim_Past_Due_Bucket2,
3782          0 Prim_Past_Due_Bucket3,
3783          0 Sec_Amount_Remaining,
3784          0 Sec_Past_Due_Amount,
3785          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency,
3786                         -1 * (NVL(PS.Second_Disc_Amt_Available,0) - NVL(DISC.Discount_Taken,0)
3787                         -  NVL(PS.Third_Disc_Amt_Available,0)),
3788                       ((-1 * (NVL(PS.Second_Disc_Amt_Available,0) - NVL(DISC.Discount_Taken,0)
3789                         -  NVL(PS.Third_Disc_Amt_Available,0)) * Conversion_Rate)
3790              * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau Sec_Discount_Available,
3791          0 Sec_Discount_Taken,
3792          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency,
3793                         NVL(PS.Second_Disc_Amt_Available,0) - NVL(DISC.Discount_Taken,0)
3794                         -  NVL(PS.Third_Disc_Amt_Available,0),
3795                      (((NVL(PS.Second_Disc_Amt_Available,0) - NVL(DISC.Discount_Taken,0)
3796                         -  NVL(PS.Third_Disc_Amt_Available,0)) * Conversion_Rate)
3797              * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau Sec_Discount_Lost,
3798          0 Sec_Payment_Amount,
3799          0 Sec_On_Time_Payment_Amt,
3800          0 Sec_Late_Payment_Amt,
3801          0 Sec_Due_Bucket1,
3802          0 Sec_Due_Bucket2,
3803          0 Sec_Due_Bucket3,
3804          0 Sec_Past_Due_Bucket1,
3805          0 Sec_Past_Due_Bucket2,
3806          0 Sec_Past_Due_Bucket3,
3807          sysdate Creation_Date,
3808          g_fii_user_id Last_Updated_By,
3809          sysdate Last_Update_Date,
3810          g_fii_login_id Last_Update_Login,
3811          2 Unique_ID
3812   FROM   FII_AP_Invoice_B AI,
3813          FII_AP_PS_Rates_Temp   RATES,
3814          FII_AP_Func_Rates_Temp FRATES,
3815 	 AP_Payment_Schedules_All PS,
3816         (SELECT /*+ index(TEM,FII_AP_WH_TAX_T_N1) */ TEM.Invoice_ID,
3817                 TEM.Payment_Num,
3818                 SUM(NVL(TEM.WH_Tax_Amount, 0)) WH_Tax_Amount
3819          FROM   FII_AP_WH_Tax_T TEM
3820          WHERE  TEM.Invoice_id BETWEEN g_start_range and g_end_range
3821          AND    TEM.Invoice_Type <> 'PREPAYMENT'
3822          AND    TEM.Second_Discount_Date + 1 BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
3823          AND    TEM.Creation_Date < TEM.Second_Discount_Date + 1
3824          GROUP  BY TEM.Invoice_ID,
3825                    TEM.Payment_Num,
3826                    TEM.Second_Discount_Date) TEMP,
3827         (SELECT /*+ index(prep,FII_AP_PREPAY_T_N1) */ PREP.Invoice_ID,
3828                 PREP.Payment_Num,
3829                 SUM(NVL(PREP.Prepay_Amount, 0)) Prepay_Amount
3830          FROM   FII_AP_Prepay_T PREP
3831          WHERE  PREP.Invoice_id BETWEEN g_start_range and g_end_range
3832          AND    PREP.Second_Discount_Date + 1 BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
3833          AND    PREP.Creation_Date < PREP.Second_Discount_Date + 1
3834          GROUP  BY PREP.Invoice_ID,
3835                    PREP.Payment_Num,
3836                    PREP.Second_Discount_Date) PP,
3837         (SELECT /*+ index(PC,FII_AP_PAY_CHK_STG_N1) */ PC.Invoice_ID,
3838                 PC.Payment_Num,
3839                 SUM(NVL(PC.Discount_Taken, 0)) Discount_Taken,
3840                 SUM(NVL(PC.Payment_Amount, 0)) Payment_Amount
3841          FROM   FII_AP_PAY_CHK_STG PC
3842          WHERE  PC.Invoice_id BETWEEN g_start_range and g_end_range
3843          AND    PC.Invoice_Type <> 'PREPAYMENT'
3844          AND    PC.Second_Discount_Date + 1 BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
3845          AND    PC.Invp_Creation_Date < PC.Second_Discount_Date + 1
3846          GROUP  BY PC.Invoice_ID,
3847                    PC.Payment_Num,
3848                    PC.Second_Discount_Date) APC,
3849         (SELECT PC.Invoice_ID Invoice_ID,
3850                 PC.Payment_Num Payment_Num,
3851                 NVL(SUM(NVL(PC.Discount_Taken,0)),0) Discount_Taken
3852          FROM   FII_AP_PAY_CHK_STG PC
3853          WHERE  PC.Invoice_ID BETWEEN g_start_range and g_end_range
3854          AND    PC.Second_Discount_Date + 1 BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
3855          AND    PC.Invp_Creation_Date BETWEEN PC.Discount_Date + 1 AND PC.Second_Discount_Date
3856          GROUP  BY  PC.Invoice_ID,
3857                     PC.Payment_Num) DISC
3858   WHERE  PS.Invoice_ID = AI.Invoice_ID
3859   AND    AI.Invoice_ID BETWEEN g_start_range and g_end_range
3860   AND    AI.Invoice_Type NOT IN ('PREPAYMENT')
3861   AND    AI.Cancel_Date IS NULL
3862   AND    FRATES.To_Currency   = AI.Base_Currency_Code
3863   AND    FRATES.From_Currency = AI.Payment_Currency_Code
3864   AND    FRATES.Trx_Date      = AI.Exchange_Date
3865   AND    DECODE(AI.Exchange_Rate_Type,'User', AI.Exchange_Rate,1) =
3866                  DECODE(FRATES.Conversion_Type,'User', FRATES.Conversion_Rate,1)
3867   AND    FRATES.Conversion_Type    = AI.Exchange_Rate_Type
3868   AND    RATES.Functional_Currency = AI.Base_Currency_Code
3869   AND    RATES.Trx_Date            = AI.Invoice_Date
3870   AND    TRUNC(PS.Second_Discount_Date) + 1 BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
3871   AND    PS.Invoice_ID  = TEMP.Invoice_ID(+)
3872   AND    PS.Payment_Num = TEMP.Payment_Num(+)
3873   AND    PS.Invoice_ID  = PP.Invoice_ID(+)
3874   AND    PS.Payment_Num = PP.Payment_Num(+)
3875   AND    PS.Invoice_ID  = APC.Invoice_ID(+)
3876   AND    PS.Payment_Num = APC.Payment_Num(+)
3877   AND    PS.Invoice_ID  = DISC.Invoice_ID(+)
3878   AND    PS.Payment_Num = DISC.Payment_Num(+)
3879   AND    ABS(NVL(APC.Payment_Amount,0) + NVL(TEMP.WH_Tax_Amount,0)
3880                                        + NVL(PP.Prepay_Amount,0)) < ABS(PS.Gross_Amount);
3881 
3882 
3883   if g_debug_flag = 'Y' then
3884      FII_UTIL.put_line('Inserted ' ||SQL%ROWCOUNT|| ' Discount records into FII_AP_PAY_SCHED_B');
3885      FII_UTIL.put_timestamp('End Timestamp');
3886      FII_UTIL.stop_timer;
3887      FII_UTIL.print_timer('Duration');
3888      FII_UTIL.put_line('');
3889   end if;
3890 
3891   commit;
3892 
3893   g_state := 'Inserting the Payment Schedules Third Discount Records';
3894   if g_debug_flag = 'Y' then
3895      FII_UTIL.put_line('');
3896      FII_UTIL.put_line(g_state);
3897      FII_UTIL.put_timestamp('Start Timestamp');
3898      FII_UTIL.start_timer;
3899      FII_UTIL.put_line('');
3900   end if;
3901 
3902 
3903   /* Inserting the Discount Date passed records into the summary table.
3904      We will insert only those payment schedules which have not be paid
3905      fully before the third discount date. This check is done by comparing
3906      the gross amount with the payment, prepayment and withheld amount */
3907 
3908   INSERT INTO FII_AP_PAY_SCHED_B b
3909         (Time_ID,
3910          Period_Type_ID,
3911          Action_Date,
3912          Action,
3913          Update_Sequence,
3914          Org_ID,
3915          Supplier_ID,
3916          Invoice_ID,
3917          Base_Currency_Code,
3918          Trx_Date,
3919          Payment_Num,
3920          Due_Date,
3921          Created_By,
3922          Amount_Remaining,
3923          Past_Due_Amount,
3924          Discount_Available,
3925          Discount_Taken,
3926          Discount_Lost,
3927          Payment_Amount,
3928          On_Time_Payment_Amt,
3929          Late_Payment_Amt,
3930          No_Days_Late,
3931          Due_Bucket1,
3932          Due_Bucket2,
3933          Due_Bucket3,
3934          Past_Due_Bucket1,
3935          Past_Due_Bucket2,
3936          Past_Due_Bucket3,
3937          Amount_Remaining_B,
3938          Past_Due_Amount_B,
3939          Discount_Available_B,
3940          Discount_Taken_B,
3941          Discount_Lost_B,
3942          Payment_Amount_B,
3943          On_Time_Payment_Amt_B,
3944          Late_Payment_Amt_B,
3945          Due_Bucket1_B,
3946          Due_Bucket2_B,
3947          Due_Bucket3_B,
3948          Past_Due_Bucket1_B,
3949          Past_Due_Bucket2_B,
3950          Past_Due_Bucket3_B,
3951          Prim_Amount_Remaining,
3952          Prim_Past_Due_Amount,
3953          Prim_Discount_Available,
3954          Prim_Discount_Taken,
3955          Prim_Discount_Lost,
3956          Prim_Payment_Amount,
3957          Prim_On_time_Payment_Amt,
3958          Prim_Late_Payment_Amt,
3959          Prim_Due_Bucket1,
3960          Prim_Due_Bucket2,
3961          Prim_Due_Bucket3,
3962          Prim_Past_Due_Bucket1,
3963          Prim_Past_Due_Bucket2,
3964          Prim_Past_Due_Bucket3,
3965          Sec_Amount_Remaining,
3966          Sec_Past_Due_Amount,
3967          Sec_Discount_Available,
3968          Sec_Discount_Taken,
3969          Sec_Discount_Lost,
3970          Sec_Payment_Amount,
3971          Sec_On_time_Payment_Amt,
3972          Sec_Late_Payment_Amt,
3973          Sec_Due_Bucket1,
3974          Sec_Due_Bucket2,
3975          Sec_Due_Bucket3,
3976          Sec_Past_Due_Bucket1,
3977          Sec_Past_Due_Bucket2,
3978          Sec_Past_Due_Bucket3,
3979          Creation_Date,
3980          Last_Updated_By,
3981          Last_Update_Date,
3982          Last_Update_Login,
3983          Unique_ID)
3984   SELECT /*+ leading(ai) index(AI,FII_AP_INVOICE_B_U1) index(FRATES) use_nl(ps)  */
3985          TO_NUMBER(TO_CHAR(PS.Third_Discount_Date + 1,'J')) Time_ID,
3986          1 Period_Type_ID,
3987          TRUNC(PS.Third_Discount_Date) + 1 Action_Date,
3988          'DISCOUNT' Action,
3989          g_seq_id Update_Sequence,
3990          AI.Org_ID Org_ID,
3991          AI.Supplier_ID Supplier_ID,
3992          AI.Invoice_ID Invoice_ID,
3993          AI.Base_Currency_Code Base_Currency_Code,
3994          AI.Invoice_Date Trx_Date,
3995          PS.Payment_Num Payment_Num,
3996          TRUNC(PS.Due_Date) Due_Date,
3997          PS.Created_By Created_By,
3998          0 Amount_Remaining,
3999          0 Past_Due_Amount,
4000          -1 * (NVL(PS.Third_Disc_Amt_Available,0)
4001                      - NVL(DISC.Discount_Taken,0)) Discount_Available,
4002          0 Discount_Taken,
4003          NVL(PS.Third_Disc_Amt_Available,0)
4004                      - NVL(DISC.Discount_Taken,0) Discount_Lost,
4005          0 Payment_Amount,
4006          0 On_Time_Payment_Amt,
4007          0 Late_Payment_Amt,
4008          0 No_Days_Late,
4009          0 Due_Bucket1,
4010          0 Due_Bucket2,
4011          0 Due_Bucket3,
4012          0 Past_Due_Bucket1,
4013          0 Past_Due_Bucket2,
4014          0 Past_Due_Bucket3,
4015          0 Amount_Remaining_B,
4016          0 Past_Due_Amount_B,
4017          ROUND((-1 * (NVL(PS.Third_Disc_Amt_Available,0) - NVL(DISC.Discount_Taken,0)) * Conversion_Rate)
4018                        / Functional_MAU) * Functional_MAU Discount_Available_B,
4019          0 Discount_Taken_B,
4020          ROUND(((NVL(PS.Third_Disc_Amt_Available,0) - NVL(DISC.Discount_Taken,0)) * Conversion_Rate)
4021                        / Functional_MAU) * Functional_MAU Discount_Lost_B,
4022          0 Payment_Amount_B,
4023          0 On_Time_Payment_Amt_B,
4024          0 Late_Payment_Amt_B,
4025          0 Due_Bucket1_B,
4026          0 Due_Bucket2_B,
4027          0 Due_Bucket3_B,
4028          0 Past_Due_Bucket1_B,
4029          0 Past_Due_Bucket2_B,
4030          0 Past_Due_Bucket3_B,
4031          0 Prim_Amount_Remaining,
4032          0 Prim_Past_Due_Amount,
4033          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency,
4034              -1 * (NVL(PS.Third_Disc_Amt_Available,0) - NVL(DISC.Discount_Taken,0)),
4035            ((-1 * (NVL(PS.Third_Disc_Amt_Available,0) - NVL(DISC.Discount_Taken,0)) * Conversion_Rate)
4036              * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau Prim_Discount_Available,
4037          0 Prim_Discount_Taken,
4038          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency,
4039              NVL(PS.Third_Disc_Amt_Available,0) - NVL(DISC.Discount_Taken,0),
4040           (((NVL(PS.Third_Disc_Amt_Available,0) - NVL(DISC.Discount_Taken,0)) * Conversion_Rate)
4041              * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau Prim_Discount_Lost,
4042          0 Prim_Payment_Amount,
4043          0 Prim_On_Time_Payment_Amt,
4044          0 Prim_Late_Payment_Amt,
4045          0 Prim_Due_Bucket1,
4046          0 Prim_Due_Bucket2,
4047          0 Prim_Due_Bucket3,
4048          0 Prim_Past_Due_Bucket1,
4049          0 Prim_Past_Due_Bucket2,
4050          0 Prim_Past_Due_Bucket3,
4051          0 Sec_Amount_Remaining,
4052          0 Sec_Past_Due_Amount,
4053          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency,
4054              -1 * (NVL(PS.Third_Disc_Amt_Available,0) - NVL(DISC.Discount_Taken,0)),
4055            ((-1 * (NVL(PS.Third_Disc_Amt_Available,0) - NVL(DISC.Discount_Taken,0)) * Conversion_Rate)
4056              * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau Sec_Discount_Available,
4057          0 Sec_Discount_Taken,
4058          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency,
4059              NVL(PS.Third_Disc_Amt_Available,0) - NVL(DISC.Discount_Taken,0),
4060           (((NVL(PS.Third_Disc_Amt_Available,0) - NVL(DISC.Discount_Taken,0)) * Conversion_Rate)
4061              * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau Sec_Discount_Lost,
4062          0 Sec_Payment_Amount,
4063          0 Sec_On_Time_Payment_Amt,
4064          0 Sec_Late_Payment_Amt,
4065          0 Sec_Due_Bucket1,
4066          0 Sec_Due_Bucket2,
4067          0 Sec_Due_Bucket3,
4068          0 Sec_Past_Due_Bucket1,
4069          0 Sec_Past_Due_Bucket2,
4070          0 Sec_Past_Due_Bucket3,
4071          sysdate Creation_Date,
4072          g_fii_user_id Last_Updated_By,
4073          sysdate Last_Update_Date,
4074          g_fii_login_id Last_Update_Login,
4075          3 Unique_ID
4076   FROM   FII_AP_Invoice_B AI,
4077          FII_AP_PS_Rates_Temp   RATES,
4078          FII_AP_Func_Rates_Temp FRATES,
4079          AP_Payment_Schedules_All PS,
4080         (SELECT /*+ index(TEM,FII_AP_WH_TAX_T_N1) */ TEM.Invoice_ID,
4081                 TEM.Payment_Num,
4082                 SUM(NVL(TEM.WH_Tax_Amount, 0)) WH_Tax_Amount
4083          FROM   FII_AP_WH_Tax_T TEM
4084          WHERE  TEM.Invoice_id BETWEEN g_start_range and g_end_range
4085          AND    TEM.Invoice_Type <> 'PREPAYMENT'
4086          AND    TEM.Third_Discount_Date + 1 BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
4087          AND    TEM.Creation_Date < TEM.Third_Discount_Date + 1
4088          GROUP  BY TEM.Invoice_ID,
4089                    TEM.Payment_Num,
4090                    TEM.Third_Discount_Date) TEMP,
4091         (SELECT /*+ index(prep,FII_AP_PREPAY_T_N1) */ PREP.Invoice_ID,
4092                 PREP.Payment_Num,
4093                 SUM(NVL(PREP.Prepay_Amount, 0)) Prepay_Amount
4094          FROM   FII_AP_Prepay_T PREP
4095          WHERE  PREP.Invoice_id BETWEEN g_start_range and g_end_range
4096          AND    PREP.Third_Discount_Date + 1 BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
4097          AND    PREP.Creation_Date < PREP.Third_Discount_Date + 1
4098          GROUP  BY PREP.Invoice_ID,
4099                    PREP.Payment_Num,
4100                    PREP.Third_Discount_Date) PP,
4101         (SELECT /*+ index(PC,FII_AP_PAY_CHK_STG_N1) */ PC.Invoice_ID,
4102                 PC.Payment_Num,
4103                 SUM(NVL(PC.Discount_Taken, 0)) Discount_Taken,
4104                 SUM(NVL(PC.Payment_Amount, 0)) Payment_Amount
4105          FROM   FII_AP_PAY_CHK_STG PC
4106          WHERE  PC.Invoice_id BETWEEN g_start_range and g_end_range
4107          AND    PC.Invoice_Type <> 'PREPAYMENT'
4108          AND    PC.Third_Discount_Date + 1 BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
4109          AND    PC.Invp_Creation_Date < PC.Third_Discount_Date + 1
4110          GROUP  BY PC.Invoice_ID,
4111                    PC.Payment_Num,
4112                    PC.Third_Discount_Date) APC,
4113         (SELECT PC.Invoice_ID Invoice_ID,
4114                 PC.Payment_Num Payment_Num,
4115                 NVL(SUM(NVL(PC.Discount_Taken,0)),0) Discount_Taken
4116          FROM   FII_AP_PAY_CHK_STG PC
4117          WHERE  PC.Invoice_ID BETWEEN g_start_range and g_end_range
4118          AND    PC.Third_Discount_Date + 1 BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
4119          AND    PC.Invp_Creation_Date BETWEEN PC.Second_Discount_Date + 1
4120                                       AND     PC.Third_Discount_Date
4121          GROUP  BY  PC.Invoice_ID,
4122                     PC.Payment_Num) DISC
4123   WHERE  PS.Invoice_ID = AI.Invoice_ID
4124   AND    AI.Invoice_ID BETWEEN g_start_range and g_end_range
4125   AND    AI.Invoice_Type NOT IN ('PREPAYMENT')
4126   AND    AI.Cancel_Date IS NULL
4127   AND    FRATES.To_Currency   = AI.Base_Currency_Code
4128   AND    FRATES.From_Currency = AI.Payment_Currency_Code
4129   AND    FRATES.Trx_Date      = AI.Exchange_Date
4130   AND    DECODE(AI.Exchange_Rate_Type,'User', AI.Exchange_Rate,1) =
4131                  DECODE(FRATES.Conversion_Type,'User', FRATES.Conversion_Rate,1)
4132   AND    FRATES.Conversion_Type    = AI.Exchange_Rate_Type
4133   AND    RATES.Functional_Currency = AI.Base_Currency_Code
4134   AND    RATES.Trx_Date            = AI.Invoice_Date
4135   AND    TRUNC(PS.Third_Discount_Date) + 1 BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
4136   AND    PS.Invoice_ID  = TEMP.Invoice_ID(+)
4137   AND    PS.Payment_Num = TEMP.Payment_Num(+)
4138   AND    PS.Invoice_ID  = PP.Invoice_ID(+)
4139   AND    PS.Payment_Num = PP.Payment_Num(+)
4140   AND    PS.Invoice_ID  = APC.Invoice_ID(+)
4141   AND    PS.Payment_Num = APC.Payment_Num(+)
4142   AND    PS.Invoice_ID  = DISC.Invoice_ID(+)
4143   AND    PS.Payment_Num = DISC.Payment_Num(+)
4144   AND    ABS(NVL(APC.Payment_Amount,0) + NVL(TEMP.WH_Tax_Amount,0)
4145                                        + NVL(PP.Prepay_Amount,0)) < ABS(PS.Gross_Amount);
4146 
4147 
4148   if g_debug_flag = 'Y' then
4149      FII_UTIL.put_line('Inserted ' ||SQL%ROWCOUNT|| ' Discount records into FII_AP_PAY_SCHED_B');
4150      FII_UTIL.put_timestamp('End Timestamp');
4151      FII_UTIL.stop_timer;
4152      FII_UTIL.print_timer('Duration');
4153      FII_UTIL.put_line('');
4154   end if;
4155 
4156   commit;
4157 
4158 EXCEPTION
4159    WHEN OTHERS THEN
4160       g_errbuf:=sqlerrm;
4161       g_retcode:= -1;
4162       g_exception_msg  := g_retcode || ':' || g_errbuf;
4163       FII_UTIL.put_line('Error occured while ' || g_state);
4164       FII_UTIL.put_line(g_exception_msg);
4165       RAISE;
4166 END;
4167 
4168 
4169 
4170 ------------------------------------------------------------------
4171 -- Procedure POPULATE_PS_DUE_ACTION
4172 -- Purpose
4173 --   This POPULATE_PS_DUE_ACTION routine inserts records into the
4174 --   FII AP Payment Schedule summary table all the due actions.
4175 ------------------------------------------------------------------
4176 
4177 PROCEDURE POPULATE_PS_DUE_ACTION IS
4178 
4179 BEGIN
4180 
4181   g_state := 'Inside the procedure POPULATE_PS_DUE_ACTION';
4182   if g_debug_flag = 'Y' then
4183      FII_UTIL.put_line('');
4184      FII_UTIL.put_line(g_state);
4185   end if;
4186 
4187   g_state := 'Inserting the Payment Schedules Due Action';
4188   if g_debug_flag = 'Y' then
4189      FII_UTIL.put_line('');
4190      FII_UTIL.put_line(g_state);
4191      FII_UTIL.put_timestamp('Start Timestamp');
4192      FII_UTIL.start_timer;
4193      FII_UTIL.put_line('');
4194   end if;
4195 
4196 
4197   /* Inserting the Due Date passed records into the summary table.
4198      We will insert only those payment schedules which have not been paid
4199      fully before the due date. This check is done by comparing
4200      the gross amount with the payment, prepayment and withheld amount */
4201 
4202   INSERT INTO FII_AP_PAY_SCHED_B b
4203         (Time_ID,
4204          Period_Type_ID,
4205          Action_Date,
4206          Action,
4207          Update_Sequence,
4208          Org_ID,
4209          Supplier_ID,
4210          Invoice_ID,
4211          Base_Currency_Code,
4212          Trx_Date,
4213          Payment_Num,
4214          Due_Date,
4215          Created_By,
4216          Amount_Remaining,
4217          Past_Due_Amount,
4218          Discount_Available,
4219          Discount_Taken,
4220          Discount_Lost,
4221          Payment_Amount,
4222          On_Time_Payment_Amt,
4223          Late_Payment_Amt,
4224          No_Days_Late,
4225          Due_Bucket1,
4226          Due_Bucket2,
4227          Due_Bucket3,
4228          Past_Due_Bucket1,
4229          Past_Due_Bucket2,
4230          Past_Due_Bucket3,
4231          Amount_Remaining_B,
4232          Past_Due_Amount_B,
4233          Discount_Available_B,
4234          Discount_Taken_B,
4235          Discount_Lost_B,
4236          Payment_Amount_B,
4237          On_Time_Payment_Amt_B,
4238          Late_Payment_Amt_B,
4239          Due_Bucket1_B,
4240          Due_Bucket2_B,
4241          Due_Bucket3_B,
4242          Past_Due_Bucket1_B,
4243          Past_Due_Bucket2_B,
4244          Past_Due_Bucket3_B,
4245          Prim_Amount_Remaining,
4246          Prim_Past_Due_Amount,
4247          Prim_Discount_Available,
4248          Prim_Discount_Taken,
4249          Prim_Discount_Lost,
4250          Prim_Payment_Amount,
4251          Prim_On_time_Payment_Amt,
4252          Prim_Late_Payment_Amt,
4253          Prim_Due_Bucket1,
4254          Prim_Due_Bucket2,
4255          Prim_Due_Bucket3,
4256          Prim_Past_Due_Bucket1,
4257          Prim_Past_Due_Bucket2,
4258          Prim_Past_Due_Bucket3,
4259          Sec_Amount_Remaining,
4260          Sec_Past_Due_Amount,
4261          Sec_Discount_Available,
4262          Sec_Discount_Taken,
4263          Sec_Discount_Lost,
4264          Sec_Payment_Amount,
4265          Sec_On_time_Payment_Amt,
4266          Sec_Late_Payment_Amt,
4267          Sec_Due_Bucket1,
4268          Sec_Due_Bucket2,
4269          Sec_Due_Bucket3,
4270          Sec_Past_Due_Bucket1,
4271          Sec_Past_Due_Bucket2,
4272          Sec_Past_Due_Bucket3,
4273          Creation_Date,
4274          Last_Updated_By,
4275          Last_Update_Date,
4276          Last_Update_Login)
4277   SELECT /*+ leading(ai) index(AI,FII_AP_INVOICE_B_U1) use_nl(ps)  */
4278          TO_NUMBER(TO_CHAR(PS.Due_Date + 1,'J')) Time_ID,
4279          1 Period_Type_ID,
4280          TRUNC(PS.Due_Date) + 1 Action_Date,
4281          'DUE' Action,
4282          g_seq_id Update_Sequence,
4283          AI.Org_ID Org_ID,
4284          AI.Supplier_ID Supplier_ID,
4285          AI.Invoice_ID Invoice_ID,
4286          AI.Base_Currency_Code Base_Currency_Code,
4287          AI.Invoice_Date Trx_Date,
4288          PS.Payment_Num Payment_Num,
4289          TRUNC(PS.Due_Date) Due_Date,
4290          PS.Created_By Created_By,
4291          0 Amount_Remaining,
4292          PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4293                          - NVL(TEMP.WH_Tax_Amount,0)
4294                          - NVL(PP.Prepay_Amount,0) Past_Due_Amount,
4295          0 Discount_Available,
4296          0 Discount_Taken,
4297          0 Discount_Lost,
4298          0 Payment_Amount,
4299          0 On_Time_Payment_Amt,
4300          0 Late_Payment_Amt,
4301          0 No_Days_Late,
4302          0 Due_Bucket1,
4303          0 Due_Bucket2,
4304          -1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4305                                - NVL(TEMP.WH_Tax_Amount,0)
4306                                - NVL(PP.Prepay_Amount,0)) Due_Bucket3,
4307          0 Past_Due_Bucket1,
4308          0 Past_Due_Bucket2,
4309          PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4310                          - NVL(TEMP.WH_Tax_Amount,0)
4311                          - NVL(PP.Prepay_Amount,0) Past_Due_Bucket3,
4312          0 Amount_Remaining_B,
4313          ROUND(((PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4314                                  - NVL(TEMP.WH_Tax_Amount,0)
4315                                  - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
4316                        / Functional_MAU) * Functional_MAU Past_Due_Amount_B,
4317          0 Discount_Available_B,
4318          0 Discount_Taken_B,
4319          0 Discount_Lost_B,
4320          0 Payment_Amount_B,
4321          0 On_Time_Payment_Amt_B,
4322          0 Late_Payment_Amt_B,
4323          0 Due_Bucket1_B,
4324          0 Due_Bucket2_B,
4325          ROUND((-1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4326                                       - NVL(TEMP.WH_Tax_Amount,0)
4327                                       - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
4328                        / Functional_MAU) * Functional_MAU  Due_Bucket3_B,
4329          0 Past_Due_Bucket1_B,
4330          0 Past_Due_Bucket2_B,
4331          ROUND(((PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4332                                  - NVL(TEMP.WH_Tax_Amount,0)
4333                                  - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
4334                        / Functional_MAU) * Functional_MAU Past_Due_Bucket3_B,
4335          0 Prim_Amount_Remaining,
4336          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency,
4337                  (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4338                                   - NVL(TEMP.WH_Tax_Amount,0)
4339                                   - NVL(PP.Prepay_Amount,0)),
4340                (((PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4341                                   - NVL(TEMP.WH_Tax_Amount,0)
4342                                   - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
4343              * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau Prim_Past_Due_Amount,
4344          0 Prim_Discount_Available,
4345          0 Prim_Discount_Taken,
4346          0 Prim_Discount_Lost,
4347          0 Prim_Payment_Amount,
4348          0 Prim_On_Time_Payment_Amt,
4349          0 Prim_Late_Payment_Amt,
4350          0 Prim_Due_Bucket1,
4351          0 Prim_Due_Bucket2,
4352          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency,
4353                 (-1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4354                                        - NVL(TEMP.WH_Tax_Amount,0)
4355                                        - NVL(PP.Prepay_Amount,0))),
4356                ((-1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4357                                        - NVL(TEMP.WH_Tax_Amount,0)
4358                                        - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
4359              * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau  Prim_Due_Bucket3,
4360          0 Prim_Past_Due_Bucket1,
4361          0 Prim_Past_Due_Bucket2,
4362          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency,
4363                  (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4364                                   - NVL(TEMP.WH_Tax_Amount,0)
4365                                   - NVL(PP.Prepay_Amount,0)),
4366                (((PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4367                                   - NVL(TEMP.WH_Tax_Amount,0)
4368                                   - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
4369              * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau Prim_Past_Due_Bucket3,
4370          0 Sec_Amount_Remaining,
4371          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency,
4372                  (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4373                                   - NVL(TEMP.WH_Tax_Amount,0)
4374                                   - NVL(PP.Prepay_Amount,0)),
4375                (((PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4376                                   - NVL(TEMP.WH_Tax_Amount,0)
4377                                   - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
4378              * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau Sec_Past_Due_Amount,
4379          0 Sec_Discount_Available,
4380          0 Sec_Discount_Taken,
4381          0 Sec_Discount_Lost,
4382          0 Sec_Payment_Amount,
4383          0 Sec_On_Time_Payment_Amt,
4384          0 Sec_Late_Payment_Amt,
4385          0 Sec_Due_Bucket1,
4386          0 Sec_Due_Bucket2,
4387          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency,
4388                 (-1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4389                                        - NVL(TEMP.WH_Tax_Amount,0)
4390                                        - NVL(PP.Prepay_Amount,0))),
4391                ((-1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4392                                        - NVL(TEMP.WH_Tax_Amount,0)
4393                                        - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
4394              * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau Sec_Due_Bucket3,
4395          0 Sec_Past_Due_Bucket1,
4396          0 Sec_Past_Due_Bucket2,
4397          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency,
4398                  (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4399                                   - NVL(TEMP.WH_Tax_Amount,0)
4400                                   - NVL(PP.Prepay_Amount,0)),
4401                (((PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4402                                   - NVL(TEMP.WH_Tax_Amount,0)
4403                                   - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
4404              * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau Sec_Past_Due_Bucket3,
4405          sysdate Creation_Date,
4406          g_fii_user_id Last_Updated_By,
4407          sysdate Last_Update_Date,
4408          g_fii_login_id Last_Update_Login
4409   FROM   FII_AP_Invoice_B AI,
4410          FII_AP_PS_Rates_Temp   RATES,
4411          FII_AP_Func_Rates_Temp FRATES,
4412          AP_Payment_Schedules_All PS,
4413         (SELECT /*+ index(TEM,FII_AP_WH_TAX_T_N1) */ TEM.Invoice_ID Invoice_ID,
4414                 TEM.Payment_Num Payment_Num,
4415                 SUM(NVL(TEM.WH_Tax_Amount, 0)) WH_Tax_Amount
4416          FROM   FII_AP_WH_TAX_T TEM
4417          WHERE  TEM.Invoice_ID BETWEEN g_start_range and g_end_range
4418          AND    TEM.Invoice_Type <> 'PREPAYMENT'
4419          AND    TEM.Due_Date + 1 BETWEEN g_start_date AND LEAST(g_end_date, g_sysdate)
4420          AND    TEM.Due_Date >= TEM.Entered_Date
4421          AND    TRUNC(TEM.Creation_Date) < TRUNC(TEM.Due_Date) + 1
4422          GROUP BY TEM.Invoice_ID,
4423                   TEM.Payment_Num,
4424                   TEM.Due_Date) TEMP,
4425         (SELECT /*+ index(prep,FII_AP_PREPAY_T_N1) */ PREP.Invoice_ID Invoice_ID,
4426                 PREP.Payment_Num Payment_Num,
4427                 SUM(NVL(PREP.Prepay_Amount, 0)) Prepay_Amount
4428          FROM   FII_AP_Prepay_T PREP
4429          WHERE  PREP.Invoice_ID BETWEEN g_start_range and g_end_range
4430          AND    PREP.Due_Date + 1 BETWEEN g_start_date AND LEAST(g_end_date, g_sysdate)
4431          AND    PREP.Due_Date >= PREP.Entered_Date
4432          AND    TRUNC(PREP.Creation_Date) < TRUNC(PREP.Due_Date) + 1
4433          GROUP BY PREP.Invoice_ID,
4434                   PREP.Payment_Num,
4435                   PREP.Due_Date) PP,
4436         (SELECT /*+ index(PC,FII_AP_PAY_CHK_STG_N1) */ PC.Invoice_ID,
4437                 PC.Payment_Num,
4438                 SUM(NVL(PC.Payment_Amount, 0)) Payment_Amount
4439          FROM   FII_AP_PAY_CHK_STG PC
4440          WHERE  PC.Invoice_id BETWEEN g_start_range and g_end_range
4441          AND    PC.Invoice_Type <> 'PREPAYMENT'
4442          AND    PC.Due_Date + 1 BETWEEN g_start_date AND LEAST(g_end_date, g_sysdate)
4443          AND    PC.Due_Date >= PC.Entered_Date
4444          AND    PC.Invp_Creation_Date < PC.due_date + 1
4445          GROUP  BY PC.Invoice_ID,
4446                    PC.Payment_Num,
4447                    PC.Due_Date) APC
4448   WHERE  PS.Invoice_ID = AI.Invoice_ID
4449   AND    AI.Invoice_ID BETWEEN g_start_range and g_end_range
4450   AND    AI.Invoice_Type NOT IN ('PREPAYMENT')
4451   AND    AI.Cancel_Date IS NULL
4452   AND    FRATES.To_Currency   = AI.Base_Currency_Code
4453   AND    FRATES.From_Currency = AI.Payment_Currency_Code
4454   AND    FRATES.Trx_Date      = AI.Exchange_Date
4455   AND    DECODE(AI.Exchange_Rate_Type,'User', AI.Exchange_Rate,1) =
4456                  DECODE(FRATES.Conversion_Type,'User', FRATES.Conversion_Rate,1)
4457   AND    FRATES.Conversion_Type    = AI.Exchange_Rate_Type
4458   AND    RATES.Functional_Currency = AI.Base_Currency_Code
4459   AND    RATES.Trx_Date            = AI.Invoice_Date
4460   AND    TRUNC(PS.Due_Date) + 1 BETWEEN g_start_date
4461                             AND     LEAST(g_end_date,g_sysdate)
4462   AND    TRUNC(PS.Due_Date) >= AI.Entered_Date
4463   AND    PS.Invoice_ID  = TEMP.Invoice_ID(+)
4464   AND    PS.Payment_Num = TEMP.Payment_Num(+)
4465   AND    PS.Invoice_ID  = PP.Invoice_ID(+)
4466   AND    PS.Payment_Num = PP.Payment_Num(+)
4467   AND    PS.Invoice_ID  = APC.Invoice_ID(+)
4468   AND    PS.Payment_Num = APC.Payment_Num(+)
4469   AND    ABS(NVL(APC.Payment_Amount,0) + NVL(TEMP.WH_Tax_Amount,0)
4470                                        + NVL(PP.Prepay_Amount,0)) < ABS(PS.Gross_Amount);
4471 
4472 
4473   if g_debug_flag = 'Y' then
4474      FII_UTIL.put_line('Inserted ' ||SQL%ROWCOUNT|| ' Due records into FII_AP_PAY_SCHED_B');
4475      FII_UTIL.put_timestamp('End Timestamp');
4476      FII_UTIL.stop_timer;
4477      FII_UTIL.print_timer('Duration');
4478      FII_UTIL.put_line('');
4479   end if;
4480 
4481   commit;
4482 
4483 EXCEPTION
4484    WHEN OTHERS THEN
4485       g_errbuf:=sqlerrm;
4486       g_retcode:= -1;
4487       g_exception_msg  := g_retcode || ':' || g_errbuf;
4488       FII_UTIL.put_line('Error occured while ' || g_state);
4489       FII_UTIL.put_line(g_exception_msg);
4490       RAISE;
4491 END;
4492 
4493 
4494 ------------------------------------------------------------------
4495 -- Procedure POPULATE_PS_BUCKET_ACTION
4496 -- Purpose
4497 --   This POPULATE_PS_BUCKET_ACTION routine inserts records into the
4498 --   FII AP Payment Schedule summary table all the due bucket actions.
4499 ------------------------------------------------------------------
4500 
4501 PROCEDURE POPULATE_PS_BUCKET_ACTION IS
4502 
4503 BEGIN
4504 
4505   g_state := 'Inside the procedure POPULATE_PS_BUCKET_ACTION';
4506   if g_debug_flag = 'Y' then
4507      FII_UTIL.put_line('');
4508      FII_UTIL.put_line(g_state);
4509   end if;
4510 
4511   g_state := 'Inserting the Payment Schedules Due Bucket2 Action';
4512   if g_debug_flag = 'Y' then
4513      FII_UTIL.put_line('');
4514      FII_UTIL.put_line(g_state);
4515      FII_UTIL.put_timestamp('Start Timestamp');
4516      FII_UTIL.start_timer;
4517      FII_UTIL.put_line('');
4518   end if;
4519 
4520 
4521   /* Inserting the Due Bucket records into the summary table.
4522      We will insert only those payment schedules which have not been paid
4523      fully before the due bucket2. This check is done by comparing
4524      the gross amount with the payment, prepayment and withheld amount */
4525 
4526   INSERT INTO FII_AP_PAY_SCHED_B b
4527         (Time_ID,
4528          Period_Type_ID,
4529          Action_Date,
4530          Action,
4531          Update_Sequence,
4532          Org_ID,
4533          Supplier_ID,
4534          Invoice_ID,
4535          Base_Currency_Code,
4536          Trx_Date,
4537          Payment_Num,
4538          Due_Date,
4539          Created_By,
4540          Amount_Remaining,
4541          Past_Due_Amount,
4542          Discount_Available,
4543          Discount_Taken,
4544          Discount_Lost,
4545          Payment_Amount,
4546          On_Time_Payment_Amt,
4547          Late_Payment_Amt,
4548          No_Days_Late,
4549          Due_Bucket1,
4550          Due_Bucket2,
4551          Due_Bucket3,
4552          Past_Due_Bucket1,
4553          Past_Due_Bucket2,
4554          Past_Due_Bucket3,
4555          Amount_Remaining_B,
4556          Past_Due_Amount_B,
4557          Discount_Available_B,
4558          Discount_Taken_B,
4559          Discount_Lost_B,
4560          Payment_Amount_B,
4561          On_Time_Payment_Amt_B,
4562          Late_Payment_Amt_B,
4563          Due_Bucket1_B,
4564          Due_Bucket2_B,
4565          Due_Bucket3_B,
4566          Past_Due_Bucket1_B,
4567          Past_Due_Bucket2_B,
4568          Past_Due_Bucket3_B,
4569          Prim_Amount_Remaining,
4570          Prim_Past_Due_Amount,
4571          Prim_Discount_Available,
4572          Prim_Discount_Taken,
4573          Prim_Discount_Lost,
4574          Prim_Payment_Amount,
4575          Prim_On_time_Payment_Amt,
4576          Prim_Late_Payment_Amt,
4577          Prim_Due_Bucket1,
4578          Prim_Due_Bucket2,
4579          Prim_Due_Bucket3,
4580          Prim_Past_Due_Bucket1,
4581          Prim_Past_Due_Bucket2,
4582          Prim_Past_Due_Bucket3,
4583          Sec_Amount_Remaining,
4584          Sec_Past_Due_Amount,
4585          Sec_Discount_Available,
4586          Sec_Discount_Taken,
4587          Sec_Discount_Lost,
4588          Sec_Payment_Amount,
4589          Sec_On_time_Payment_Amt,
4590          Sec_Late_Payment_Amt,
4591          Sec_Due_Bucket1,
4592          Sec_Due_Bucket2,
4593          Sec_Due_Bucket3,
4594          Sec_Past_Due_Bucket1,
4595          Sec_Past_Due_Bucket2,
4596          Sec_Past_Due_Bucket3,
4597          Creation_Date,
4598          Last_Updated_By,
4599          Last_Update_Date,
4600          Last_Update_Login)
4601   SELECT /*+ leading(ai) index(AI,FII_AP_INVOICE_B_U1) use_nl(ps)  */
4602          TO_NUMBER(TO_CHAR((PS.Due_Date - g_due_bucket2),'J')) Time_ID,
4603          1 Period_Type_ID,
4604          (TRUNC(PS.Due_Date) - g_due_bucket2) Action_Date,
4605          'DUE BUCKET' Action,
4606          g_seq_id Update_Sequence,
4607          AI.Org_ID Org_ID,
4608          AI.Supplier_ID Supplier_ID,
4609          AI.Invoice_ID Invoice_ID,
4610          AI.Base_Currency_Code Base_Currency_Code,
4611          AI.Invoice_Date Trx_Date,
4612          PS.Payment_Num Payment_Num,
4613          TRUNC(PS.Due_Date) Due_Date,
4614          PS.Created_By Created_By,
4615          0 Amount_Remaining,
4616          0 Past_Due_Amount,
4617          0 Discount_Available,
4618          0 Discount_Taken,
4619          0 Discount_Lost,
4620          0 Payment_Amount,
4621          0 On_Time_Payment_Amt,
4622          0 Late_Payment_Amt,
4623          0 No_Days_Late,
4624          -1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4625                                - NVL(TEMP.WH_Tax_Amount,0)
4626                                - NVL(PP.Prepay_Amount,0)) Due_Bucket1,
4627          PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4628                          - NVL(TEMP.WH_Tax_Amount,0)
4629                          - NVL(PP.Prepay_Amount,0) Due_Bucket2,
4630          0 Due_Bucket3,
4631          0 Past_Due_Bucket1,
4632          0 Past_Due_Bucket2,
4633          0 Past_Due_Bucket3,
4634          0 Amount_Remaining_B,
4635          0 Past_Due_Amount_B,
4636          0 Discount_Available_B,
4637          0 Discount_Taken_B,
4638          0 Discount_Lost_B,
4639          0 Payment_Amount_B,
4640          0 On_Time_Payment_Amt_B,
4641          0 Late_Payment_Amt_B,
4642          ROUND((-1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4643                                       - NVL(TEMP.WH_Tax_Amount,0)
4644                                       - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
4645                        / Functional_MAU) * Functional_MAU  Due_Bucket1_B,
4646          ROUND(((PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4647                                  - NVL(TEMP.WH_Tax_Amount,0)
4648                                  - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
4649                        / Functional_MAU) * Functional_MAU Due_Bucket2_B,
4650          0 Due_Bucket3_B,
4651          0 Past_Due_Bucket1_B,
4652          0 Past_Due_Bucket2_B,
4653          0 Past_Due_Bucket3_B,
4654          0 Prim_Amount_Remaining,
4655          0 Prim_Past_Due_Amount,
4656          0 Prim_Discount_Available,
4657          0 Prim_Discount_Taken,
4658          0 Prim_Discount_Lost,
4659          0 Prim_Payment_Amount,
4660          0 Prim_On_Time_Payment_Amt,
4661          0 Prim_Late_Payment_Amt,
4662          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency,
4663                  -1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4664                                        - NVL(TEMP.WH_Tax_Amount,0)
4665                                        - NVL(PP.Prepay_Amount,0)),
4666                ((-1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4667                                        - NVL(TEMP.WH_Tax_Amount,0)
4668                                        - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
4669              * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau  Prim_Due_Bucket1,
4670          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency,
4671                   PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4672                                   - NVL(TEMP.WH_Tax_Amount,0)
4673                                   - NVL(PP.Prepay_Amount,0),
4674                (((PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4675                                   - NVL(TEMP.WH_Tax_Amount,0)
4676                                   - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
4677              * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau Prim_Due_Bucket2,
4678          0 Prim_Due_Bucket3,
4679          0 Prim_Past_Due_Bucket1,
4680          0 Prim_Past_Due_Bucket2,
4681          0 Prim_Past_Due_Bucket3,
4682          0 Sec_Amount_Remaining,
4683          0 Sec_Past_Due_Amount,
4684          0 Sec_Discount_Available,
4685          0 Sec_Discount_Taken,
4686          0 Sec_Discount_Lost,
4687          0 Sec_Payment_Amount,
4688          0 Sec_On_Time_Payment_Amt,
4689          0 Sec_Late_Payment_Amt,
4690          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency,
4691                  -1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4692                                        - NVL(TEMP.WH_Tax_Amount,0)
4693                                        - NVL(PP.Prepay_Amount,0)),
4694                ((-1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4695                                        - NVL(TEMP.WH_Tax_Amount,0)
4696                                        - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
4697              * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau Sec_Due_Bucket1,
4698          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency,
4699                   PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4700                                   - NVL(TEMP.WH_Tax_Amount,0)
4701                                   - NVL(PP.Prepay_Amount,0),
4702                (((PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4703                                   - NVL(TEMP.WH_Tax_Amount,0)
4704                                   - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
4705              * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau Sec_Due_Bucket2,
4706          0 Sec_Due_Bucket3,
4707          0 Sec_Past_Due_Bucket1,
4708          0 Sec_Past_Due_Bucket2,
4709          0 Sec_Past_Due_Bucket3,
4710          sysdate Creation_Date,
4711          g_fii_user_id Last_Updated_By,
4712          sysdate Last_Update_Date,
4713          g_fii_login_id Last_Update_Login
4714   FROM   FII_AP_Invoice_B AI,
4715          FII_AP_PS_Rates_Temp   RATES,
4716          FII_AP_Func_Rates_Temp FRATES,
4717          AP_Payment_Schedules_All PS,
4718         (SELECT /*+ index(TEM,FII_AP_WH_TAX_T_N1) */ TEM.Invoice_ID,
4719                 TEM.Payment_Num,
4720                 SUM(NVL(TEM.WH_Tax_Amount, 0)) WH_Tax_Amount
4721          FROM   FII_AP_WH_Tax_T TEM
4722          WHERE  TEM.Invoice_id BETWEEN g_start_range and g_end_range
4723          AND    TEM.Invoice_Type <> 'PREPAYMENT'
4724          AND   (TEM.Due_Date - g_due_bucket2) BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
4725          AND    TEM.Creation_Date < (TEM.Due_Date - g_due_bucket2)
4726          GROUP  BY TEM.Invoice_ID,
4727                    TEM.Payment_Num,
4728                    TEM.Due_Date) TEMP,
4729         (SELECT /*+ index(prep,FII_AP_PREPAY_T_N1) */ PREP.Invoice_ID,
4730                 PREP.Payment_Num,
4731                 SUM(NVL(PREP.Prepay_Amount, 0)) Prepay_Amount
4732          FROM   FII_AP_Prepay_T PREP
4733          WHERE  PREP.Invoice_id BETWEEN g_start_range and g_end_range
4734          AND   (PREP.Due_Date - g_due_bucket2) BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
4735          AND    PREP.Creation_Date < (PREP.Due_Date - g_due_bucket2)
4736          GROUP  BY PREP.Invoice_ID,
4737                    PREP.Payment_Num,
4738                    PREP.Due_Date) PP,
4739         (SELECT /*+ index(PC,FII_AP_PAY_CHK_STG_N1) */ PC.Invoice_ID,
4740                 PC.Payment_Num,
4741                 SUM(NVL(PC.Payment_Amount, 0)) Payment_Amount
4742          FROM   FII_AP_PAY_CHK_STG PC
4743          WHERE  PC.Invoice_id BETWEEN g_start_range and g_end_range
4744          AND    PC.Invoice_Type <> 'PREPAYMENT'
4745          AND   (PC.Due_Date - g_due_bucket2) BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
4746          AND    PC.Invp_Creation_Date < (PC.Due_Date - g_due_bucket2)
4747          GROUP  BY PC.Invoice_ID,
4748                    PC.Payment_Num,
4749                    PC.Due_Date) APC
4750   WHERE  PS.Invoice_ID = AI.Invoice_ID
4751   AND    AI.Invoice_ID BETWEEN g_start_range and g_end_range
4752   AND    AI.Invoice_Type NOT IN ('PREPAYMENT')
4753   AND    AI.Cancel_Date IS NULL
4754   AND    FRATES.To_Currency   = AI.Base_Currency_Code
4755   AND    FRATES.From_Currency = AI.Payment_Currency_Code
4756   AND    FRATES.Trx_Date      = AI.Exchange_Date
4757   AND    DECODE(AI.Exchange_Rate_Type,'User', AI.Exchange_Rate,1) =
4758                  DECODE(FRATES.Conversion_Type,'User', FRATES.Conversion_Rate,1)
4759   AND    FRATES.Conversion_Type    = AI.Exchange_Rate_Type
4760   AND    RATES.Functional_Currency = AI.Base_Currency_Code
4761   AND    RATES.Trx_Date            = AI.Invoice_Date
4762   AND   (TRUNC(PS.Due_Date) - AI.Entered_Date) > g_due_bucket2
4763   AND   (TRUNC(PS.Due_Date) - g_due_bucket2) BETWEEN g_start_date
4764                                                  AND     LEAST(g_end_date,g_sysdate)
4765   AND    PS.Invoice_ID  = TEMP.Invoice_ID(+)
4766   AND    PS.Payment_Num = TEMP.Payment_Num(+)
4767   AND    PS.Invoice_ID  = PP.Invoice_ID(+)
4768   AND    PS.Payment_Num = PP.Payment_Num(+)
4769   AND    PS.Invoice_ID  = APC.Invoice_ID(+)
4770   AND    PS.Payment_Num = APC.Payment_Num(+)
4771   AND    ABS(NVL(APC.Payment_Amount,0) + NVL(TEMP.WH_Tax_Amount,0)
4772                                        + NVL(PP.Prepay_Amount,0)) < ABS(PS.Gross_Amount);
4773 
4774 
4775   if g_debug_flag = 'Y' then
4776      FII_UTIL.put_line('Inserted ' ||SQL%ROWCOUNT|| ' Due Bucket2 records into FII_AP_PAY_SCHED_B');
4777      FII_UTIL.put_timestamp('End Timestamp');
4778      FII_UTIL.stop_timer;
4779      FII_UTIL.print_timer('Duration');
4780      FII_UTIL.put_line('');
4781      FII_UTIL.start_timer;
4782      FII_UTIL.put_line('');
4783   end if;
4784 
4785   commit;
4786 
4787   g_state := 'Inserting the Payment Schedules Due Bucket3 Action';
4788   if g_debug_flag = 'Y' then
4789      FII_UTIL.put_line('');
4790      FII_UTIL.put_line(g_state);
4791      FII_UTIL.put_timestamp('Start Timestamp');
4792      FII_UTIL.start_timer;
4793      FII_UTIL.put_line('');
4794   end if;
4795 
4796   /* Inserting the Due Bucket records into the summary table.
4797      We will insert only those payment schedules which have not been paid
4798      fully before the due bucket3. This check is done by comparing
4799      the gross amount with the payment, prepayment and withheld amount */
4800 
4801   INSERT INTO FII_AP_PAY_SCHED_B b
4802         (Time_ID,
4803          Period_Type_ID,
4804          Action_Date,
4805          Action,
4806          Update_Sequence,
4807          Org_ID,
4808          Supplier_ID,
4809          Invoice_ID,
4810          Base_Currency_Code,
4811          Trx_Date,
4812          Payment_Num,
4813          Due_Date,
4814          Created_By,
4815          Amount_Remaining,
4816          Past_Due_Amount,
4817          Discount_Available,
4818          Discount_Taken,
4819          Discount_Lost,
4820          Payment_Amount,
4821          On_Time_Payment_Amt,
4822          Late_Payment_Amt,
4823          No_Days_Late,
4824          Due_Bucket1,
4825          Due_Bucket2,
4826          Due_Bucket3,
4827          Past_Due_Bucket1,
4828          Past_Due_Bucket2,
4829          Past_Due_Bucket3,
4830          Amount_Remaining_B,
4831          Past_Due_Amount_B,
4832          Discount_Available_B,
4833          Discount_Taken_B,
4834          Discount_Lost_B,
4835          Payment_Amount_B,
4836          On_Time_Payment_Amt_B,
4837          Late_Payment_Amt_B,
4838          Due_Bucket1_B,
4839          Due_Bucket2_B,
4840          Due_Bucket3_B,
4841          Past_Due_Bucket1_B,
4842          Past_Due_Bucket2_B,
4843          Past_Due_Bucket3_B,
4844          Prim_Amount_Remaining,
4845          Prim_Past_Due_Amount,
4846          Prim_Discount_Available,
4847          Prim_Discount_Taken,
4848          Prim_Discount_Lost,
4849          Prim_Payment_Amount,
4850          Prim_On_time_Payment_Amt,
4851          Prim_Late_Payment_Amt,
4852          Prim_Due_Bucket1,
4853          Prim_Due_Bucket2,
4854          Prim_Due_Bucket3,
4855          Prim_Past_Due_Bucket1,
4856          Prim_Past_Due_Bucket2,
4857          Prim_Past_Due_Bucket3,
4858          Sec_Amount_Remaining,
4859          Sec_Past_Due_Amount,
4860          Sec_Discount_Available,
4861          Sec_Discount_Taken,
4862          Sec_Discount_Lost,
4863          Sec_Payment_Amount,
4864          Sec_On_time_Payment_Amt,
4865          Sec_Late_Payment_Amt,
4866          Sec_Due_Bucket1,
4867          Sec_Due_Bucket2,
4868          Sec_Due_Bucket3,
4869          Sec_Past_Due_Bucket1,
4870          Sec_Past_Due_Bucket2,
4871          Sec_Past_Due_Bucket3,
4872          Creation_Date,
4873          Last_Updated_By,
4874          Last_Update_Date,
4875          Last_Update_Login)
4876   SELECT /*+ leading(ai) index(AI,FII_AP_INVOICE_B_U1) use_nl(ps)  */
4877          TO_NUMBER(TO_CHAR((PS.Due_Date - g_due_bucket3),'J')) Time_ID,
4878          1 Period_Type_ID,
4879          (TRUNC(PS.Due_Date) - g_due_bucket3) Action_Date,
4880          'DUE BUCKET',
4881          g_seq_id Update_Sequence,
4882          AI.Org_ID Org_ID,
4883          AI.Supplier_ID Supplier_ID,
4884          AI.Invoice_ID Invoice_ID,
4885          AI.Base_Currency_Code Base_Currency_Code,
4886          AI.Invoice_Date Trx_Date,
4887          PS.Payment_Num Payment_Num,
4888          TRUNC(PS.Due_Date) Due_Date,
4889          PS.Created_By Created_By,
4890          0 Amount_Remaining,
4891          0 Past_Due_Amount,
4892          0 Discount_Available,
4893          0 Discount_Taken,
4894          0 Discount_Lost,
4895          0 Payment_Amount,
4896          0 On_Time_Payment_Amt,
4897          0 Late_Payment_Amt,
4898          0 No_Days_Late,
4899          0 Due_Bucket1,
4900          -1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4901                                - NVL(TEMP.WH_Tax_Amount,0)
4902                                - NVL(PP.Prepay_Amount,0)) Due_Bucket2,
4903          PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4904                          - NVL(TEMP.WH_Tax_Amount,0)
4905                          - NVL(PP.Prepay_Amount,0) Due_Bucket3,
4906          0 Past_Due_Bucket1,
4907          0 Past_Due_Bucket2,
4908          0 Past_Due_Bucket3,
4909          0 Amount_Remaining_B,
4910          0 Past_Due_Amount_B,
4911          0 Discount_Available_B,
4912          0 Discount_Taken_B,
4913          0 Discount_Lost_B,
4914          0 Payment_Amount_B,
4915          0 On_Time_Payment_Amt_B,
4916          0 Late_Payment_Amt_B,
4917          0 Due_Bucket1_B,
4918          ROUND((-1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4919                                       - NVL(TEMP.WH_Tax_Amount,0)
4920                                       - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
4921                        / Functional_MAU) * Functional_MAU  Due_Bucket2_B,
4922          ROUND(((PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4923                                  - NVL(TEMP.WH_Tax_Amount,0)
4924                                  - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
4925                        / Functional_MAU) * Functional_MAU Due_Bucket3_B,
4926          0 Past_Due_Bucket1_B,
4927          0 Past_Due_Bucket2_B,
4928          0 Past_Due_Bucket3_B,
4929          0 Prim_Amount_Remaining,
4930          0 Prim_Past_Due_Amount,
4931          0 Prim_Discount_Available,
4932          0 Prim_Discount_Taken,
4933          0 Prim_Discount_Lost,
4934          0 Prim_Payment_Amount,
4935          0 Prim_On_Time_Payment_Amt,
4936          0 Prim_Late_Payment_Amt,
4937          0 Prim_Due_Bucket1,
4938          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency,
4939                  -1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4940                                        - NVL(TEMP.WH_Tax_Amount,0)
4941                                        - NVL(PP.Prepay_Amount,0)),
4942                ((-1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4943                                        - NVL(TEMP.WH_Tax_Amount,0)
4944                                        - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
4945              * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau  Prim_Due_Bucket2,
4946          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency,
4947                   PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4948                                   - NVL(TEMP.WH_Tax_Amount,0)
4949                                   - NVL(PP.Prepay_Amount,0),
4950                (((PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4951                                   - NVL(TEMP.WH_Tax_Amount,0)
4952                                   - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
4953              * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau Prim_Due_Bucket3,
4954          0 Prim_Past_Due_Bucket1,
4955          0 Prim_Past_Due_Bucket2,
4956          0 Prim_Past_Due_Bucket3,
4957          0 Sec_Amount_Remaining,
4958          0 Sec_Past_Due_Amount,
4959          0 Sec_Discount_Available,
4960          0 Sec_Discount_Taken,
4961          0 Sec_Discount_Lost,
4962          0 Sec_Payment_Amount,
4963          0 Sec_On_Time_Payment_Amt,
4964          0 Sec_Late_Payment_Amt,
4965          0 Sec_Due_Bucket1,
4966          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency,
4967                  -1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4968                                        - NVL(TEMP.WH_Tax_Amount,0)
4969                                        - NVL(PP.Prepay_Amount,0)),
4970                ((-1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4971                                        - NVL(TEMP.WH_Tax_Amount,0)
4972                                        - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
4973              * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau Sec_Due_Bucket2,
4974          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency,
4975                   PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4976                                   - NVL(TEMP.WH_Tax_Amount,0)
4977                                   - NVL(PP.Prepay_Amount,0),
4978                (((PS.Gross_Amount - NVL(APC.Payment_Amount,0)
4979                                   - NVL(TEMP.WH_Tax_Amount,0)
4980                                   - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
4981              * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau Sec_Due_Bucket3,
4982          0 Sec_Past_Due_Bucket1,
4983          0 Sec_Past_Due_Bucket2,
4984          0 Sec_Past_Due_Bucket3,
4985          sysdate Creation_Date,
4986          g_fii_user_id Last_Updated_By,
4987          sysdate Last_Update_Date,
4988          g_fii_login_id Last_Update_Login
4989   FROM   FII_AP_Invoice_B AI,
4990          FII_AP_PS_Rates_Temp   RATES,
4991          FII_AP_Func_Rates_Temp FRATES,
4992          AP_Payment_Schedules_All PS,
4993         (SELECT /*+ index(TEM,FII_AP_WH_TAX_T_N1) */ TEM.Invoice_ID,
4994                 TEM.Payment_Num,
4995                 SUM(NVL(TEM.WH_Tax_Amount, 0)) WH_Tax_Amount
4996          FROM   FII_AP_WH_Tax_T TEM
4997          WHERE  TEM.Invoice_id BETWEEN g_start_range and g_end_range
4998          AND    TEM.Invoice_Type <> 'PREPAYMENT'
4999          AND   (TEM.Due_Date - g_due_bucket3) BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
5000          AND    TEM.Creation_Date < (TEM.Due_Date - g_due_bucket3)
5001          GROUP  BY TEM.Invoice_ID,
5002                    TEM.Payment_Num,
5003                    TEM.Due_Date) TEMP,
5004         (SELECT /*+ index(prep,FII_AP_PREPAY_T_N1) */ PREP.Invoice_ID,
5005                 PREP.Payment_Num,
5006                 SUM(NVL(PREP.Prepay_Amount, 0)) Prepay_Amount
5007          FROM   FII_AP_Prepay_T PREP
5008          WHERE  PREP.Invoice_id BETWEEN g_start_range and g_end_range
5009          AND   (PREP.Due_Date - g_due_bucket3) BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
5010          AND    PREP.Creation_Date < (PREP.Due_Date - g_due_bucket3)
5011          GROUP  BY PREP.Invoice_ID,
5012                    PREP.Payment_Num,
5013                    PREP.Due_Date) PP,
5014         (SELECT /*+ index(PC,FII_AP_PAY_CHK_STG_N1) */ PC.Invoice_ID,
5015                 PC.Payment_Num,
5016                 SUM(NVL(PC.Payment_Amount, 0)) Payment_Amount
5017          FROM   FII_AP_PAY_CHK_STG PC
5018          WHERE  PC.Invoice_id BETWEEN g_start_range and g_end_range
5019          AND    PC.Invoice_Type <> 'PREPAYMENT'
5020          AND   (PC.Due_Date - g_due_bucket3) BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
5021          AND    PC.Invp_Creation_Date < (PC.Due_Date - g_due_bucket3)
5022          GROUP  BY PC.Invoice_ID,
5023                    PC.Payment_Num,
5024                    PC.Due_Date) APC
5025   WHERE  PS.Invoice_ID = AI.Invoice_ID
5026   AND    AI.Invoice_ID BETWEEN g_start_range and g_end_range
5027   AND    AI.Invoice_Type NOT IN ('PREPAYMENT')
5028   AND    AI.Cancel_Date IS NULL
5029   AND    FRATES.To_Currency   = AI.Base_Currency_Code
5030   AND    FRATES.From_Currency = AI.Payment_Currency_Code
5031   AND    FRATES.Trx_Date      = AI.Exchange_Date
5032   AND    DECODE(AI.Exchange_Rate_Type,'User', AI.Exchange_Rate,1) =
5033                  DECODE(FRATES.Conversion_Type,'User', FRATES.Conversion_Rate,1)
5034   AND    FRATES.Conversion_Type    = AI.Exchange_Rate_Type
5035   AND    RATES.Functional_Currency = AI.Base_Currency_Code
5036   AND    RATES.Trx_Date            = AI.Invoice_Date
5037   AND   (TRUNC(PS.Due_Date) - AI.Entered_Date) > g_due_bucket3
5038   AND   (TRUNC(PS.Due_Date) - g_due_bucket3) BETWEEN g_start_date
5039                                                  AND     LEAST(g_end_date,g_sysdate)
5040   AND    PS.Invoice_ID  = TEMP.Invoice_ID(+)
5041   AND    PS.Payment_Num = TEMP.Payment_Num(+)
5042   AND    PS.Invoice_ID  = PP.Invoice_ID(+)
5043   AND    PS.Payment_Num = PP.Payment_Num(+)
5044   AND    PS.Invoice_ID  = APC.Invoice_ID(+)
5045   AND    PS.Payment_Num = APC.Payment_Num(+)
5046   AND    ABS(NVL(APC.Payment_Amount,0) + NVL(TEMP.WH_Tax_Amount,0)
5047                                        + NVL(PP.Prepay_Amount,0)) < ABS(PS.Gross_Amount);
5048 
5049 
5050   if g_debug_flag = 'Y' then
5051      FII_UTIL.put_line('Inserted ' ||SQL%ROWCOUNT|| ' Due Bucket3 records into FII_AP_PAY_SCHED_B');
5052      FII_UTIL.put_timestamp('End Timestamp');
5053      FII_UTIL.stop_timer;
5054      FII_UTIL.print_timer('Duration');
5055      FII_UTIL.put_line('');
5056   end if;
5057 
5058   commit;
5059 
5060 EXCEPTION
5061    WHEN OTHERS THEN
5062       g_errbuf:=sqlerrm;
5063       g_retcode:= -1;
5064       g_exception_msg  := g_retcode || ':' || g_errbuf;
5065       FII_UTIL.put_line('Error occured while ' || g_state);
5066       FII_UTIL.put_line(g_exception_msg);
5067       RAISE;
5068 END;
5069 
5070 
5071 ------------------------------------------------------------------
5072 -- Procedure POPULATE_PS_PAST_BUCKET_ACTION
5073 -- Purpose
5074 --   This POPULATE_PS_PAST_BUCKET_ACTION routine inserts records into the
5075 --   FII AP Payment Schedule summary table all the due bucket actions.
5076 ------------------------------------------------------------------
5077 
5078 PROCEDURE POPULATE_PS_PAST_BUCKET_ACTION IS
5079 
5080 BEGIN
5081 
5082   g_state := 'Inside the procedure POPULATE_PS_PAST_BUCKET_ACTION';
5083   if g_debug_flag = 'Y' then
5084      FII_UTIL.put_line('');
5085      FII_UTIL.put_line(g_state);
5086   end if;
5087 
5088   g_state := 'Inserting the Payment Schedules Past Due Bucket2 Action';
5089   if g_debug_flag = 'Y' then
5090      FII_UTIL.put_line('');
5091      FII_UTIL.put_line(g_state);
5092      FII_UTIL.put_timestamp('Start Timestamp');
5093      FII_UTIL.start_timer;
5094      FII_UTIL.put_line('');
5095   end if;
5096 
5097 
5098   /* Inserting the Past Due Bucket records into the summary table.
5099      We will insert only those payment schedules which have not been paid
5100      fully before the past due bucket2. This check is done by comparing
5101      the gross amount with the payment, prepayment and withheld amount */
5102 
5103   INSERT INTO FII_AP_PAY_SCHED_B b
5104         (Time_ID,
5105          Period_Type_ID,
5106          Action_Date,
5107          Action,
5108          Update_Sequence,
5109          Org_ID,
5110          Supplier_ID,
5111          Invoice_ID,
5112          Base_Currency_Code,
5113          Trx_Date,
5114          Payment_Num,
5115          Due_Date,
5116          Created_By,
5117          Amount_Remaining,
5118          Past_Due_Amount,
5119          Discount_Available,
5120          Discount_Taken,
5121          Discount_Lost,
5122          Payment_Amount,
5123          On_Time_Payment_Amt,
5124          Late_Payment_Amt,
5125          No_Days_Late,
5126          Due_Bucket1,
5127          Due_Bucket2,
5128          Due_Bucket3,
5129          Past_Due_Bucket1,
5130          Past_Due_Bucket2,
5131          Past_Due_Bucket3,
5132          Amount_Remaining_B,
5133          Past_Due_Amount_B,
5134          Discount_Available_B,
5135          Discount_Taken_B,
5136          Discount_Lost_B,
5137          Payment_Amount_B,
5138          On_Time_Payment_Amt_B,
5139          Late_Payment_Amt_B,
5140          Due_Bucket1_B,
5141          Due_Bucket2_B,
5142          Due_Bucket3_B,
5143          Past_Due_Bucket1_B,
5144          Past_Due_Bucket2_B,
5145          Past_Due_Bucket3_B,
5146          Prim_Amount_Remaining,
5147          Prim_Past_Due_Amount,
5148          Prim_Discount_Available,
5149          Prim_Discount_Taken,
5150          Prim_Discount_Lost,
5151          Prim_Payment_Amount,
5152          Prim_On_time_Payment_Amt,
5153          Prim_Late_Payment_Amt,
5154          Prim_Due_Bucket1,
5155          Prim_Due_Bucket2,
5156          Prim_Due_Bucket3,
5157          Prim_Past_Due_Bucket1,
5158          Prim_Past_Due_Bucket2,
5159          Prim_Past_Due_Bucket3,
5160          Sec_Amount_Remaining,
5161          Sec_Past_Due_Amount,
5162          Sec_Discount_Available,
5163          Sec_Discount_Taken,
5164          Sec_Discount_Lost,
5165          Sec_Payment_Amount,
5166          Sec_On_time_Payment_Amt,
5167          Sec_Late_Payment_Amt,
5168          Sec_Due_Bucket1,
5169          Sec_Due_Bucket2,
5170          Sec_Due_Bucket3,
5171          Sec_Past_Due_Bucket1,
5172          Sec_Past_Due_Bucket2,
5173          Sec_Past_Due_Bucket3,
5174          Creation_Date,
5175          Last_Updated_By,
5176          Last_Update_Date,
5177          Last_Update_Login)
5178   SELECT /*+ leading(ai) index(AI,FII_AP_INVOICE_B_U1) use_nl(ps)  */
5179          TO_NUMBER(TO_CHAR(PS.Due_Date + g_past_bucket3 + 1,'J')) Time_ID,
5180          1 Period_Type_ID,
5181          TRUNC(PS.Due_Date) + g_past_bucket3 + 1 Action_Date,
5182          'PAST BUCKET' Action,
5183          g_seq_id Update_Sequence,
5184          AI.Org_ID Org_ID,
5185          AI.Supplier_ID Supplier_ID,
5186          AI.Invoice_ID Invoice_ID,
5187          AI.Base_Currency_Code Base_Currency_Code,
5188          AI.Invoice_Date Trx_Date,
5189          PS.Payment_Num Payment_Num,
5190          TRUNC(PS.Due_Date) Due_Date,
5191          PS.Created_By Created_By,
5192          0 Amount_Remaining,
5193          0 Past_Due_Amount,
5194          0 Discount_Available,
5195          0 Discount_Taken,
5196          0 Discount_Lost,
5197          0 Payment_Amount,
5198          0 On_Time_Payment_Amt,
5199          0 Late_Payment_Amt,
5200          0 No_Days_Late,
5201          0 Due_Bucket1,
5202          0 Due_Bucket2,
5203          0 Due_Bucket3,
5204          0 Past_Due_Bucket1,
5205          PS.Gross_Amount - NVL(APC.Payment_Amount,0)
5206                          - NVL(TEMP.WH_Tax_Amount,0)
5207                          - NVL(PP.Prepay_Amount,0)  Past_Due_Bucket2,
5208          -1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
5209                                - NVL(TEMP.WH_Tax_Amount,0)
5210                                - NVL(PP.Prepay_Amount,0)) Past_Due_Bucket3,
5211          0 Amount_Remaining_B,
5212          0 Past_Due_Amount_B,
5213          0 Discount_Available_B,
5214          0 Discount_Taken_B,
5215          0 Discount_Lost_B,
5216          0 Payment_Amount_B,
5217          0 On_Time_Payment_Amt_B,
5218          0 Late_Payment_Amt_B,
5219          0 Due_Bucket1_B,
5220          0 Due_Bucket2_B,
5221          0 Due_Bucket3_B,
5222          0 Past_Due_Bucket1_B,
5223          ROUND(((PS.Gross_Amount - NVL(APC.Payment_Amount,0)
5224                                  - NVL(TEMP.WH_Tax_Amount,0)
5225                                  - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
5226                        / Functional_MAU) * Functional_MAU Past_Due_Bucket2_B,
5227          ROUND((-1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
5228                                       - NVL(TEMP.WH_Tax_Amount,0)
5229                                       - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
5230                        / Functional_MAU) * Functional_MAU Past_Due_Bucket3_B,
5231          0 Prim_Amount_Remaining,
5232          0 Prim_Past_Due_Amount,
5233          0 Prim_Discount_Available,
5234          0 Prim_Discount_Taken,
5235          0 Prim_Discount_Lost,
5236          0 Prim_Payment_Amount,
5237          0 Prim_On_Time_Payment_Amt,
5238          0 Prim_Late_Payment_Amt,
5239          0 Prim_Due_Bucket1,
5240          0 Prim_Due_Bucket2,
5241          0 Prim_Due_Bucket3,
5242          0 Prim_Past_Due_Bucket1,
5243          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency,
5244                   PS.Gross_Amount - NVL(APC.Payment_Amount,0)
5245                                   - NVL(TEMP.WH_Tax_Amount,0)
5246                                   - NVL(PP.Prepay_Amount,0),
5247                (((PS.Gross_Amount - NVL(APC.Payment_Amount,0)
5248                                   - NVL(TEMP.WH_Tax_Amount,0)
5249                                   - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
5250              * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau Prim_Past_Due_Bucket2,
5251          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency,
5252                  -1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
5253                                        - NVL(TEMP.WH_Tax_Amount,0)
5254                                        - NVL(PP.Prepay_Amount,0)),
5255                ((-1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
5256                                        - NVL(TEMP.WH_Tax_Amount,0)
5257                                        - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
5258              * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau Prim_Past_Due_Bucket3,
5259          0 Sec_Amount_Remaining,
5260          0 Sec_Past_Due_Amount,
5261          0 Sec_Discount_Available,
5262          0 Sec_Discount_Taken,
5263          0 Sec_Discount_Lost,
5264          0 Sec_Payment_Amount,
5265          0 Sec_On_Time_Payment_Amt,
5266          0 Sec_Late_Payment_Amt,
5267          0 Sec_Due_Bucket1,
5268          0 Sec_Due_Bucket2,
5269          0 Sec_Due_Bucket3,
5270          0 Sec_Past_Due_Bucket1,
5271          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency,
5272                   PS.Gross_Amount - NVL(APC.Payment_Amount,0)
5273                                   - NVL(TEMP.WH_Tax_Amount,0)
5274                                   - NVL(PP.Prepay_Amount,0),
5275                (((PS.Gross_Amount - NVL(APC.Payment_Amount,0)
5276                                   - NVL(TEMP.WH_Tax_Amount,0)
5277                                   - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
5278              * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau Sec_Past_Due_Bucket2,
5279          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency,
5280                  -1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
5281                                        - NVL(TEMP.WH_Tax_Amount,0)
5282                                        - NVL(PP.Prepay_Amount,0)),
5283                ((-1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
5284                                        - NVL(TEMP.WH_Tax_Amount,0)
5285                                        - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
5286              * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau Sec_Past_Due_Bucket3,
5287          sysdate Creation_Date,
5288          g_fii_user_id Last_Updated_By,
5289          sysdate Last_Update_Date,
5290          g_fii_login_id Last_Update_Login
5291   FROM   FII_AP_Invoice_B AI,
5292          FII_AP_PS_Rates_Temp   RATES,
5293          FII_AP_Func_Rates_Temp FRATES,
5294          AP_Payment_Schedules_All PS,
5295         (SELECT /*+ index(TEM,FII_AP_WH_TAX_T_N1) */ TEM.Invoice_ID,
5296                 TEM.Payment_Num,
5297                 SUM(NVL(TEM.WH_Tax_Amount, 0)) WH_Tax_Amount
5298          FROM   FII_AP_WH_Tax_T TEM
5299          WHERE  TEM.Invoice_id BETWEEN g_start_range and g_end_range
5300          AND    TEM.Invoice_Type <> 'PREPAYMENT'
5301          AND   (TEM.Due_Date + g_past_bucket3) + 1 BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
5302          AND    TEM.Creation_Date < (TEM.Due_Date + g_past_bucket3) + 1
5303          GROUP  BY TEM.Invoice_ID,
5304                    TEM.Payment_Num,
5305                    TEM.Due_Date) TEMP,
5306         (SELECT /*+ index(prep,FII_AP_PREPAY_T_N1) */ PREP.Invoice_ID,
5307                 PREP.Payment_Num,
5308                 SUM(NVL(PREP.Prepay_Amount, 0)) Prepay_Amount
5309          FROM   FII_AP_Prepay_T PREP
5310          WHERE  PREP.Invoice_id BETWEEN g_start_range and g_end_range
5311          AND   (PREP.Due_Date + g_past_bucket3) + 1 BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
5312          AND    PREP.Creation_Date < (PREP.Due_Date + g_past_bucket3) + 1
5313          GROUP  BY PREP.Invoice_ID,
5314                    PREP.Payment_Num,
5315                    PREP.Due_Date) PP,
5316         (SELECT /*+ index(PC,FII_AP_PAY_CHK_STG_N1) */ PC.Invoice_ID,
5317                 PC.Payment_Num,
5318                 SUM(NVL(PC.Payment_Amount, 0)) Payment_Amount
5319          FROM   FII_AP_PAY_CHK_STG PC
5320          WHERE  PC.Invoice_id BETWEEN g_start_range and g_end_range
5321          AND    PC.Invoice_Type <> 'PREPAYMENT'
5322          AND   (PC.Due_Date + g_past_bucket3) + 1 BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
5323          AND    PC.Invp_Creation_Date < (PC.Due_Date + g_past_bucket3) + 1
5324          GROUP  BY PC.Invoice_ID,
5325                    PC.Payment_Num,
5326                    PC.Due_Date) APC
5327   WHERE  PS.Invoice_ID = AI.Invoice_ID
5328   AND    AI.Invoice_ID BETWEEN g_start_range and g_end_range
5329   AND    AI.Invoice_Type NOT IN ('PREPAYMENT')
5330   AND    AI.Cancel_Date IS NULL
5331   AND    FRATES.To_Currency   = AI.Base_Currency_Code
5332   AND    FRATES.From_Currency = AI.Payment_Currency_Code
5333   AND    FRATES.Trx_Date      = AI.Exchange_Date
5334   AND    DECODE(AI.Exchange_Rate_Type,'User', AI.Exchange_Rate,1) =
5335                  DECODE(FRATES.Conversion_Type,'User', FRATES.Conversion_Rate,1)
5336   AND    FRATES.Conversion_Type    = AI.Exchange_Rate_Type
5337   AND    RATES.Functional_Currency = AI.Base_Currency_Code
5338   AND    RATES.Trx_Date            = AI.Invoice_Date
5339   AND   (TRUNC(PS.Due_Date) + g_past_bucket3 + 1) > AI.Entered_Date
5340   AND   (TRUNC(PS.Due_Date) + g_past_bucket3) + 1 BETWEEN g_start_date
5341                                                   AND     LEAST(g_end_date,g_sysdate)
5342   AND    PS.Invoice_ID  = TEMP.Invoice_ID(+)
5343   AND    PS.Payment_Num = TEMP.Payment_Num(+)
5344   AND    PS.Invoice_ID  = PP.Invoice_ID(+)
5345   AND    PS.Payment_Num = PP.Payment_Num(+)
5346   AND    PS.Invoice_ID  = APC.Invoice_ID(+)
5347   AND    PS.Payment_Num = APC.Payment_Num(+)
5348   AND    ABS(NVL(APC.Payment_Amount,0) + NVL(TEMP.WH_Tax_Amount,0)
5349                                        + NVL(PP.Prepay_Amount,0)) < ABS(PS.Gross_Amount);
5350 
5351 
5352   if g_debug_flag = 'Y' then
5353      FII_UTIL.put_line('Inserted ' ||SQL%ROWCOUNT|| ' Past Due Bucket2 records into FII_AP_PAY_SCHED_B');
5354      FII_UTIL.put_timestamp('End Timestamp');
5355      FII_UTIL.stop_timer;
5356      FII_UTIL.print_timer('Duration');
5357   end if;
5358 
5359   commit;
5360 
5361   g_state := 'Inserting the Payment Schedules Past Due Bucket1 Action';
5362   if g_debug_flag = 'Y' then
5363      FII_UTIL.put_line('');
5364      FII_UTIL.put_line(g_state);
5365      FII_UTIL.put_timestamp('Start Timestamp');
5366      FII_UTIL.start_timer;
5367      FII_UTIL.put_line('');
5368   end if;
5369 
5370 
5371   /* Inserting the Past Due Bucket records into the summary table.
5372      We will insert only those payment schedules which have not been paid
5373      fully before the past due bucket2. This check is done by comparing
5374      the gross amount with the payment, prepayment and withheld amount */
5375 
5376   INSERT INTO FII_AP_PAY_SCHED_B b
5377         (Time_ID,
5378          Period_Type_ID,
5379          Action_Date,
5380          Action,
5381          Update_Sequence,
5382          Org_ID,
5383          Supplier_ID,
5384          Invoice_ID,
5385          Base_Currency_Code,
5386          Trx_Date,
5387          Payment_Num,
5388          Due_Date,
5389          Created_By,
5390          Amount_Remaining,
5391          Past_Due_Amount,
5392          Discount_Available,
5393          Discount_Taken,
5394          Discount_Lost,
5395          Payment_Amount,
5396          On_Time_Payment_Amt,
5397          Late_Payment_Amt,
5398          No_Days_Late,
5399          Due_Bucket1,
5400          Due_Bucket2,
5401          Due_Bucket3,
5402          Past_Due_Bucket1,
5403          Past_Due_Bucket2,
5404          Past_Due_Bucket3,
5405          Amount_Remaining_B,
5406          Past_Due_Amount_B,
5407          Discount_Available_B,
5408          Discount_Taken_B,
5409          Discount_Lost_B,
5410          Payment_Amount_B,
5411          On_Time_Payment_Amt_B,
5412          Late_Payment_Amt_B,
5413          Due_Bucket1_B,
5414          Due_Bucket2_B,
5415          Due_Bucket3_B,
5416          Past_Due_Bucket1_B,
5417          Past_Due_Bucket2_B,
5418          Past_Due_Bucket3_B,
5419          Prim_Amount_Remaining,
5420          Prim_Past_Due_Amount,
5421          Prim_Discount_Available,
5422          Prim_Discount_Taken,
5423          Prim_Discount_Lost,
5424          Prim_Payment_Amount,
5425          Prim_On_time_Payment_Amt,
5426          Prim_Late_Payment_Amt,
5427          Prim_Due_Bucket1,
5428          Prim_Due_Bucket2,
5429          Prim_Due_Bucket3,
5430          Prim_Past_Due_Bucket1,
5431          Prim_Past_Due_Bucket2,
5432          Prim_Past_Due_Bucket3,
5433          Sec_Amount_Remaining,
5434          Sec_Past_Due_Amount,
5435          Sec_Discount_Available,
5436          Sec_Discount_Taken,
5437          Sec_Discount_Lost,
5438          Sec_Payment_Amount,
5439          Sec_On_time_Payment_Amt,
5440          Sec_Late_Payment_Amt,
5441          Sec_Due_Bucket1,
5442          Sec_Due_Bucket2,
5443          Sec_Due_Bucket3,
5444          Sec_Past_Due_Bucket1,
5445          Sec_Past_Due_Bucket2,
5446          Sec_Past_Due_Bucket3,
5447          Creation_Date,
5448          Last_Updated_By,
5449          Last_Update_Date,
5450          Last_Update_Login)
5451   SELECT /*+ leading(ai) index(AI,FII_AP_INVOICE_B_U1) use_nl(ps)  */
5452          TO_NUMBER(TO_CHAR(PS.Due_Date + g_past_bucket2 + 1,'J')) Time_ID,
5453          1 Period_Type_ID,
5454          TRUNC(PS.Due_Date) + g_past_bucket2 + 1 Action_Date,
5455          'PAST BUCKET' Action,
5456          g_seq_id Update_Sequence,
5457          AI.Org_ID Org_ID,
5458          AI.Supplier_ID Supplier_ID,
5459          AI.Invoice_ID Invoice_ID,
5460          AI.Base_Currency_Code Base_Currency_Code,
5461          AI.Invoice_Date Trx_Date,
5462          PS.Payment_Num Payment_Num,
5463          TRUNC(PS.Due_Date) Due_Date,
5464          PS.Created_By Created_By,
5465          0 Amount_Remaining,
5466          0 Past_Due_Amount,
5467          0 Discount_Available,
5468          0 Discount_Taken,
5469          0 Discount_Lost,
5470          0 Payment_Amount,
5471          0 On_Time_Payment_Amt,
5472          0 Late_Payment_Amt,
5473          0 No_Days_Late,
5474          0 Due_Bucket1,
5475          0 Due_Bucket2,
5476          0 Due_Bucket3,
5477          PS.Gross_Amount - NVL(APC.Payment_Amount,0)
5478                          - NVL(TEMP.WH_Tax_Amount,0)
5479                          - NVL(PP.Prepay_Amount,0) Past_Due_Bucket1,
5480          -1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
5481                                - NVL(TEMP.WH_Tax_Amount,0)
5482                                - NVL(PP.Prepay_Amount,0)) Past_Due_Bucket2,
5483          0 Past_Due_Bucket3,
5484          0 Amount_Remaining_B,
5485          0 Past_Due_Amount_B,
5486          0 Discount_Available_B,
5487          0 Discount_Taken_B,
5488          0 Discount_Lost_B,
5489          0 Payment_Amount_B,
5490          0 On_Time_Payment_Amt_B,
5491          0 Late_Payment_Amt_B,
5492          0 Due_Bucket1_B,
5493          0 Due_Bucket2_B,
5494          0 Due_Bucket3_B,
5495          ROUND(((PS.Gross_Amount - NVL(APC.Payment_Amount,0)
5496                                  - NVL(TEMP.WH_Tax_Amount,0)
5497                                  - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
5498                        / Functional_MAU) * Functional_MAU Past_Due_Bucket1_B,
5499          ROUND((-1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
5500                                       - NVL(TEMP.WH_Tax_Amount,0)
5501                                       - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
5502                        / Functional_MAU) * Functional_MAU Past_Due_Bucket2_B,
5503          0 Past_Due_Bucket3_B,
5504          0 Prim_Amount_Remaining,
5505          0 Prim_Past_Due_Amount,
5506          0 Prim_Discount_Available,
5507          0 Prim_Discount_Taken,
5508          0 Prim_Discount_Lost,
5509          0 Prim_Payment_Amount,
5510          0 Prim_On_Time_Payment_Amt,
5511          0 Prim_Late_Payment_Amt,
5512          0 Prim_Due_Bucket1,
5513          0 Prim_Due_Bucket2,
5514          0 Prim_Due_Bucket3,
5515          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency,
5516                   PS.Gross_Amount - NVL(APC.Payment_Amount,0)
5517                                   - NVL(TEMP.WH_Tax_Amount,0)
5518                                   - NVL(PP.Prepay_Amount,0),
5519                (((PS.Gross_Amount - NVL(APC.Payment_Amount,0)
5520                                   - NVL(TEMP.WH_Tax_Amount,0)
5521                                   - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
5522              * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau Prim_Past_Due_Bucket1,
5523          ROUND(DECODE(AI.Invoice_Currency_Code, g_prim_currency,
5524                  -1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
5525                                        - NVL(TEMP.WH_Tax_Amount,0)
5526                                        - NVL(PP.Prepay_Amount,0)),
5527                ((-1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
5528                                        - NVL(TEMP.WH_Tax_Amount,0)
5529                                        - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
5530              * RATES.Prim_Conversion_Rate)) / g_primary_mau) * g_primary_mau Prim_Past_Due_Bucket2,
5531          0 Prim_Past_Due_Bucket3,
5532          0 Sec_Amount_Remaining,
5533          0 Sec_Past_Due_Amount,
5534          0 Sec_Discount_Available,
5535          0 Sec_Discount_Taken,
5536          0 Sec_Discount_Lost,
5537          0 Sec_Payment_Amount,
5538          0 Sec_On_Time_Payment_Amt,
5539          0 Sec_Late_Payment_Amt,
5540          0 Sec_Due_Bucket1,
5541          0 Sec_Due_Bucket2,
5542          0 Sec_Due_Bucket3,
5543          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency,
5544                   PS.Gross_Amount - NVL(APC.Payment_Amount,0)
5545                                   - NVL(TEMP.WH_Tax_Amount,0)
5546                                   - NVL(PP.Prepay_Amount,0),
5547                (((PS.Gross_Amount - NVL(APC.Payment_Amount,0)
5548                                   - NVL(TEMP.WH_Tax_Amount,0)
5549                                   - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
5550              * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau Sec_Past_Due_Bucket1,
5551          ROUND(DECODE(AI.Invoice_Currency_Code, g_sec_currency,
5552                  -1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
5553                                        - NVL(TEMP.WH_Tax_Amount,0)
5554                                        - NVL(PP.Prepay_Amount,0)),
5555                ((-1 * (PS.Gross_Amount - NVL(APC.Payment_Amount,0)
5556                                        - NVL(TEMP.WH_Tax_Amount,0)
5557                                        - NVL(PP.Prepay_Amount,0)) * Conversion_Rate)
5558              * RATES.Sec_Conversion_Rate)) / g_secondary_mau) * g_secondary_mau Sec_Past_Due_Bucket2,
5559          0 Sec_Past_Due_Bucket3,
5560          sysdate Creation_Date,
5561          g_fii_user_id Last_Updated_By,
5562          sysdate Last_Update_Date,
5563          g_fii_login_id Last_Update_Login
5564   FROM   FII_AP_Invoice_B AI,
5565          FII_AP_PS_Rates_Temp   RATES,
5566          FII_AP_Func_Rates_Temp FRATES,
5567          AP_Payment_Schedules_All PS,
5568         (SELECT /*+ index(TEM,FII_AP_WH_TAX_T_N1) */ TEM.Invoice_ID,
5569                 TEM.Payment_Num,
5570                 SUM(NVL(TEM.WH_Tax_Amount, 0)) WH_Tax_Amount
5571          FROM   FII_AP_WH_Tax_T TEM
5572          WHERE  TEM.Invoice_id BETWEEN g_start_range and g_end_range
5573          AND    TEM.Invoice_Type <> 'PREPAYMENT'
5574          AND   (TEM.Due_Date + g_past_bucket2) + 1 BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
5575          AND    TEM.Creation_Date < (TEM.Due_Date + g_past_bucket2) + 1
5576          GROUP  BY TEM.Invoice_ID,
5577                    TEM.Payment_Num,
5578                    TEM.Due_Date) TEMP,
5579         (SELECT /*+ index(prep,FII_AP_PREPAY_T_N1) */ PREP.Invoice_ID,
5580                 PREP.Payment_Num,
5581                 SUM(NVL(PREP.Prepay_Amount, 0)) Prepay_Amount
5582          FROM   FII_AP_Prepay_T PREP
5583          WHERE  PREP.Invoice_id BETWEEN g_start_range and g_end_range
5584          AND   (PREP.Due_Date + g_past_bucket2) + 1 BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
5585          AND    PREP.Creation_Date < (PREP.Due_Date + g_past_bucket2) + 1
5586          GROUP  BY PREP.Invoice_ID,
5587                    PREP.Payment_Num,
5588                    PREP.Due_Date) PP,
5589         (SELECT /*+ index(PC,FII_AP_PAY_CHK_STG_N1) */ PC.Invoice_ID,
5590                 PC.Payment_Num,
5591                 SUM(NVL(PC.Payment_Amount, 0)) Payment_Amount
5592          FROM   FII_AP_PAY_CHK_STG PC
5593          WHERE  PC.Invoice_id BETWEEN g_start_range and g_end_range
5594          AND    PC.Invoice_Type <> 'PREPAYMENT'
5595          AND   (PC.Due_Date + g_past_bucket2) + 1 BETWEEN g_start_date AND LEAST(g_end_date,g_sysdate)
5596          AND    PC.Invp_Creation_Date < (PC.Due_Date + g_past_bucket2) + 1
5597          GROUP  BY PC.Invoice_ID,
5598                    PC.Payment_Num,
5599                    PC.Due_Date) APC
5600   WHERE  PS.Invoice_ID = AI.Invoice_ID
5601   AND    AI.Invoice_ID BETWEEN g_start_range and g_end_range
5602   AND    AI.Invoice_Type NOT IN ('PREPAYMENT')
5603   AND    AI.Cancel_Date IS NULL
5604   AND    FRATES.To_Currency   = AI.Base_Currency_Code
5605   AND    FRATES.From_Currency = AI.Payment_Currency_Code
5606   AND    FRATES.Trx_Date      = AI.Exchange_Date
5607   AND    DECODE(AI.Exchange_Rate_Type,'User', AI.Exchange_Rate,1) =
5608                  DECODE(FRATES.Conversion_Type,'User', FRATES.Conversion_Rate,1)
5609   AND    FRATES.Conversion_Type    = AI.Exchange_Rate_Type
5610   AND    RATES.Functional_Currency = AI.Base_Currency_Code
5611   AND    RATES.Trx_Date            = AI.Invoice_Date
5612   AND   (TRUNC(PS.Due_Date) + g_past_bucket2 + 1) > AI.Entered_Date
5613   AND   (TRUNC(PS.Due_Date) + g_past_bucket2) + 1 BETWEEN g_start_date
5614                                              AND     LEAST(g_end_date,g_sysdate)
5615   AND    PS.Invoice_ID = TEMP.Invoice_ID(+)
5616   AND    PS.Payment_Num = TEMP.Payment_Num(+)
5617   AND    PS.Invoice_ID = PP.Invoice_ID(+)
5618   AND    PS.Payment_Num = PP.Payment_Num(+)
5619   AND    PS.Invoice_ID = APC.Invoice_ID(+)
5620   AND    PS.Payment_Num = APC.Payment_Num(+)
5621   AND    ABS(NVL(APC.Payment_Amount,0) + NVL(TEMP.WH_Tax_Amount,0)
5622                                        + NVL(PP.Prepay_Amount,0)) < ABS(PS.Gross_Amount);
5623 
5624 
5625   if g_debug_flag = 'Y' then
5626      FII_UTIL.put_line('Inserted ' ||SQL%ROWCOUNT|| ' Past Due Bucket1 records into FII_AP_PAY_SCHED_B');
5627      FII_UTIL.put_timestamp('End Timestamp');
5628      FII_UTIL.stop_timer;
5629      FII_UTIL.print_timer('Duration');
5630      FII_UTIL.put_line('');
5631   end if;
5632 
5633   commit;
5634 
5635 
5636 EXCEPTION
5637    WHEN OTHERS THEN
5638       g_errbuf:=sqlerrm;
5639       g_retcode:= -1;
5640       g_exception_msg  := g_retcode || ':' || g_errbuf;
5641       FII_UTIL.put_line('Error occured while ' || g_state);
5642       FII_UTIL.put_line(g_exception_msg);
5643       RAISE;
5644 END;
5645 
5646 
5647 ------------------------------------------------------------------
5648 -- Procedure POPULATE_PAY_SCHED_SUM
5649 -- Purpose
5650 --   This POPULATE_PAY_SCHED_SUM routine inserts records into the
5651 --   FII AP Invoice Base summary table.
5652 ------------------------------------------------------------------
5653 
5654 PROCEDURE POPULATE_PAY_SCHED_SUM(
5655                                  P_Start_Range     IN   NUMBER,
5656                                  P_End_Range       IN   NUMBER) IS
5657 
5658 BEGIN
5659 
5660   g_state := 'Inside the procedure POPULATE_PAY_SCHED_SUM';
5661   if g_debug_flag = 'Y' then
5662      FII_UTIL.put_line('');
5663      FII_UTIL.put_line(g_state);
5664   end if;
5665 
5666   g_start_range := p_start_range;
5667   g_end_range   := p_end_range;
5668 
5669   if g_debug_flag = 'Y' then
5670     FII_UTIL.put_line('g_start_date is '|| g_start_date);
5671     FII_UTIL.put_line('g_end_date is '|| g_end_date);
5672     FII_UTIL.put_line('g_start_range is '|| g_start_range);
5673     FII_UTIL.put_line('g_end_range is '|| g_end_range);
5674   end if;
5675 
5676   SELECT fii_ap_pay_sched_b_s.nextval
5677   INTO   g_seq_id
5678   FROM   dual;
5679 
5680   g_state := 'Populating FII_AP_PAY_SCHED_B FROM AP_PAYMENT_SCHEDULES_ALL table';
5681   if g_debug_flag = 'Y' then
5682      FII_UTIL.put_line(g_state);
5683   end if;
5684 
5685   g_state := 'Populating Payment Schedules Creation records';
5686   if g_debug_flag = 'Y' then
5687      FII_UTIL.put_line(g_state);
5688      FII_UTIL.put_timestamp('Start Timestamp');
5689      FII_UTIL.start_timer;
5690      FII_UTIL.put_line('');
5691   end if;
5692 
5693   INSERT INTO FII_AP_PAY_SCHED_B b
5694         (Time_ID,
5695          Period_Type_ID,
5696          Action_Date,
5697          Action,
5698          Update_Sequence,
5699          Org_ID,
5700          Supplier_ID,
5701          Invoice_ID,
5702          Base_Currency_Code,
5703          Trx_Date,
5704          Payment_Num,
5705          Due_Date,
5706          Created_By,
5707          Amount_Remaining,
5708          Past_Due_Amount,
5709          Discount_Available,
5710          Discount_Taken,
5711          Discount_Lost,
5712          Payment_Amount,
5713          On_Time_Payment_Amt,
5714          Late_Payment_Amt,
5715          No_Days_Late,
5716          Due_Bucket1,
5717          Due_Bucket2,
5718          Due_Bucket3,
5719          Past_Due_Bucket1,
5720          Past_Due_Bucket2,
5721          Past_Due_Bucket3,
5722          Amount_Remaining_B,
5723          Past_Due_Amount_B,
5724          Discount_Available_B,
5725          Discount_Taken_B,
5726          Discount_Lost_B,
5727          Payment_Amount_B,
5728          On_Time_Payment_Amt_B,
5729          Late_Payment_Amt_B,
5730          Due_Bucket1_B,
5731          Due_Bucket2_B,
5732          Due_Bucket3_B,
5733          Past_Due_Bucket1_B,
5734          Past_Due_Bucket2_B,
5735          Past_Due_Bucket3_B,
5736          Prim_Amount_Remaining,
5737          Prim_Past_Due_Amount,
5738          Prim_Discount_Available,
5739          Prim_Discount_Taken,
5740          Prim_Discount_Lost,
5741          Prim_Payment_Amount,
5742          Prim_On_time_Payment_Amt,
5743          Prim_Late_Payment_Amt,
5744          Prim_Due_Bucket1,
5745          Prim_Due_Bucket2,
5746          Prim_Due_Bucket3,
5747          Prim_Past_Due_Bucket1,
5748          Prim_Past_Due_Bucket2,
5749          Prim_Past_Due_Bucket3,
5750          Sec_Amount_Remaining,
5751          Sec_Past_Due_Amount,
5752          Sec_Discount_Available,
5753          Sec_Discount_Taken,
5754          Sec_Discount_Lost,
5755          Sec_Payment_Amount,
5756          Sec_On_time_Payment_Amt,
5757          Sec_Late_Payment_Amt,
5758          Sec_Due_Bucket1,
5759          Sec_Due_Bucket2,
5760          Sec_Due_Bucket3,
5761          Sec_Past_Due_Bucket1,
5762          Sec_Past_Due_Bucket2,
5763          Sec_Past_Due_Bucket3,
5764          Creation_Date,
5765          Last_Updated_By,
5766          Last_Update_Date,
5767          Last_Update_Login)
5768   SELECT /*+ no_merge ordered index(PS,FII_AP_INVOICE_B_U1) index(RATES) use_nl(AI) */
5769          TO_NUMBER(TO_CHAR(Action_Date,'J')) Time_ID,
5770          1 Period_Type_ID,
5771          Action_Date,
5772          'CREATION' Action,
5773          g_seq_id Update_Sequence,
5774          Org_ID,
5775          Supplier_ID,
5776          Invoice_ID,
5777          Base_Currency_Code,
5778          Invoice_Date,
5779          Payment_Num,
5780          Due_Date,
5781          Created_By,
5782          Amount_Remaining,
5783          Past_Due_Amount,
5784          Discount_Available,
5785          Discount_Taken,
5786          Discount_Lost,
5787          Payment_Amount,
5788          On_Time_Payment_Amt,
5789          Late_Payment_Amt,
5790          No_Days_Late,
5791          Due_Bucket1,
5792          Due_Bucket2,
5793          Due_Bucket3,
5794          Past_Due_Bucket1,
5795          Past_Due_Bucket2,
5796          Past_Due_Bucket3,
5797          ROUND((Amount_Remaining * Conversion_Rate) / Functional_MAU) * Functional_MAU Amount_Remaining_B,
5798          ROUND((Past_Due_Amount * Conversion_Rate) / Functional_MAU) * Functional_MAU Past_Due_Amount_B,
5799          ROUND((Discount_Available * Conversion_Rate) / Functional_MAU) * Functional_MAU Discount_Available_B,
5800          ROUND((Discount_Taken * Conversion_Rate) / Functional_MAU) * Functional_MAU Discount_Taken_B,
5801          ROUND((Discount_Lost * Conversion_Rate) / Functional_MAU) * Functional_MAU Discount_Lost_B,
5802          ROUND((Payment_Amount * Conversion_Rate) / Functional_MAU) * Functional_MAU Payment_Amount_B,
5803          ROUND((On_Time_Payment_Amt * Conversion_Rate) / Functional_MAU) * Functional_MAU On_Time_Payment_Amt_B,
5804          ROUND((Late_Payment_Amt * Conversion_Rate) / Functional_MAU) * Functional_MAU Late_Payment_Amt_B,
5805          ROUND((Due_Bucket1 * Conversion_Rate) / Functional_MAU) * Functional_MAU Due_Bucket1_B,
5806          ROUND((Due_Bucket2 * Conversion_Rate) / Functional_MAU) * Functional_MAU Due_Bucket2_B,
5807          ROUND((Due_Bucket3 * Conversion_Rate) / Functional_MAU) * Functional_MAU Due_Bucket3_B,
5808          ROUND((Past_Due_Bucket1 * Conversion_Rate) / Functional_MAU) * Functional_MAU Past_Due_Bucket1_B,
5809          ROUND((Past_Due_Bucket2 * Conversion_Rate) / Functional_MAU) * Functional_MAU Past_Due_Bucket2_B,
5810          ROUND((Past_Due_Bucket3 * Conversion_Rate) / Functional_MAU) * Functional_MAU Past_Due_Bucket3_B,
5811          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Amount_Remaining,
5812                           ((Amount_Remaining * Conversion_Rate) * RATES.Prim_Conversion_Rate))
5813                           / g_primary_mau) * g_primary_mau Prim_Amount_Remaining,
5814          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Past_Due_Amount,
5815                           ((Past_Due_Amount * Conversion_Rate) * RATES.Prim_Conversion_Rate))
5816                           / g_primary_mau) * g_primary_mau Prim_Past_Due_Amount,
5817          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Discount_Available,
5818                           ((Discount_Available * Conversion_Rate) * RATES.Prim_Conversion_Rate))
5819                           / g_primary_mau) * g_primary_mau Prim_Discount_Available,
5820          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Discount_Taken,
5821                           ((Discount_Taken * Conversion_Rate) * RATES.Prim_Conversion_Rate))
5822                           / g_primary_mau) * g_primary_mau Prim_Discount_Taken,
5823          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Discount_Lost,
5824                           ((Discount_Lost * Conversion_Rate) * RATES.Prim_Conversion_Rate))
5825                           / g_primary_mau) * g_primary_mau Prim_Discount_Lost,
5826          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Payment_Amount,
5827                           ((Payment_Amount * Conversion_Rate) * RATES.Prim_Conversion_Rate))
5828                           / g_primary_mau) * g_primary_mau Prim_Payment_Amount,
5829          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, On_Time_Payment_Amt,
5830                           ((On_Time_Payment_Amt * Conversion_Rate) * RATES.Prim_Conversion_Rate))
5831                           / g_primary_mau) * g_primary_mau Prim_On_Time_Payment_Amt,
5832          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Late_Payment_Amt,
5833                           ((Late_Payment_Amt * Conversion_Rate) * RATES.Prim_Conversion_Rate))
5834                           / g_primary_mau) * g_primary_mau Prim_Late_Payment_Amt,
5835          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Due_Bucket1,
5836                           ((Due_Bucket1 * Conversion_Rate) * RATES.Prim_Conversion_Rate))
5837                           / g_primary_mau) * g_primary_mau Prim_Due_Bucket1,
5838          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Due_Bucket2,
5839                           ((Due_Bucket2 * Conversion_Rate) * RATES.Prim_Conversion_Rate))
5840                           / g_primary_mau) * g_primary_mau Prim_Due_Bucket2,
5841          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Due_Bucket3,
5842                           ((Due_Bucket3 * Conversion_Rate) * RATES.Prim_Conversion_Rate))
5843                           / g_primary_mau) * g_primary_mau Prim_Due_Bucket3,
5844          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Past_Due_Bucket1,
5845                           ((Past_Due_Bucket1 * Conversion_Rate) * RATES.Prim_Conversion_Rate))
5846                           / g_primary_mau) * g_primary_mau Prim_Past_Due_Bucket1,
5847          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Past_Due_Bucket2,
5848                           ((Past_Due_Bucket2 * Conversion_Rate) * RATES.Prim_Conversion_Rate))
5849                           / g_primary_mau) * g_primary_mau Prim_Past_Due_Bucket2,
5850          ROUND(DECODE(Invoice_Currency_Code, g_prim_currency, Past_Due_Bucket3,
5851                           ((Past_Due_Bucket3 * Conversion_Rate) * RATES.Prim_Conversion_Rate))
5852                           / g_primary_mau) * g_primary_mau Prim_Past_Due_Bucket3,
5853          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Amount_Remaining,
5854                           ((Amount_Remaining * Conversion_Rate) * RATES.Sec_Conversion_Rate))
5855                           / g_secondary_mau) * g_secondary_mau Sec_Amount_Remaining,
5856          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Past_Due_Amount,
5857                           ((Past_Due_Amount * Conversion_Rate) * RATES.Sec_Conversion_Rate))
5858                           / g_secondary_mau) * g_secondary_mau Sec_Past_Due_Amount,
5859          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Discount_Available,
5860                           ((Discount_Available * Conversion_Rate) * RATES.Sec_Conversion_Rate))
5861                           / g_secondary_mau) * g_secondary_mau Sec_Discount_Available,
5862          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Discount_Taken,
5863                           ((Discount_Taken * Conversion_Rate) * RATES.Sec_Conversion_Rate))
5864                           / g_secondary_mau) * g_secondary_mau Sec_Discount_Taken,
5865          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Discount_Lost,
5866                           ((Discount_Lost * Conversion_Rate) * RATES.Sec_Conversion_Rate))
5867                           / g_secondary_mau) * g_secondary_mau Sec_Discount_Lost,
5868          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Payment_Amount,
5869                           ((Payment_Amount * Conversion_Rate) * RATES.Sec_Conversion_Rate))
5870                           / g_secondary_mau) * g_secondary_mau Sec_Payment_Amount,
5871          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, On_Time_Payment_Amt,
5872                           ((On_Time_Payment_Amt * Conversion_Rate) * RATES.Sec_Conversion_Rate))
5873                           / g_secondary_mau) * g_secondary_mau Sec_On_Time_Payment_Amt,
5874          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Late_Payment_Amt,
5875                           ((Late_Payment_Amt * Conversion_Rate) * RATES.Sec_Conversion_Rate))
5876                           / g_secondary_mau) * g_secondary_mau Sec_Late_Payment_Amt,
5877          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Due_Bucket1,
5878                           ((Due_Bucket1 * Conversion_Rate) * RATES.Sec_Conversion_Rate))
5879                           / g_secondary_mau) * g_secondary_mau Sec_Due_Bucket1,
5880          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Due_Bucket2,
5881                           ((Due_Bucket2 * Conversion_Rate) * RATES.Sec_Conversion_Rate))
5882                           / g_secondary_mau) * g_secondary_mau Sec_Due_Bucket2,
5883          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Due_Bucket3,
5884                           ((Due_Bucket3 * Conversion_Rate) * RATES.Sec_Conversion_Rate))
5885                           / g_secondary_mau) * g_secondary_mau Sec_Due_Bucket3,
5886          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Past_Due_Bucket1,
5887                           ((Past_Due_Bucket1 * Conversion_Rate) * RATES.Sec_Conversion_Rate))
5888                           / g_secondary_mau) * g_secondary_mau Sec_Past_Due_Bucket1,
5889          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Past_Due_Bucket2,
5890                           ((Past_Due_Bucket2 * Conversion_Rate) * RATES.Sec_Conversion_Rate))
5891                           / g_secondary_mau) * g_secondary_mau Sec_Past_Due_Bucket2,
5892          ROUND(DECODE(Invoice_Currency_Code, g_sec_currency, Past_Due_Bucket3,
5893                           ((Past_Due_Bucket3 * Conversion_Rate) * RATES.Sec_Conversion_Rate))
5894                           / g_secondary_mau) * g_secondary_mau Sec_Past_Due_Bucket3,
5895          sysdate Creation_Date,
5896          g_fii_user_id Last_Updated_By,
5897          sysdate Last_Update_Date,
5898          g_fii_login_id Last_Update_Login
5899   FROM
5900         (SELECT AI.Entered_Date Action_Date,
5901                 AI.Org_Id Org_ID,
5902                 AI.Supplier_ID Supplier_ID,
5903                 AI.Invoice_Id Invoice_ID,
5904                 AI.Base_Currency_Code Base_Currency_Code,
5905                 AI.Invoice_Date Invoice_Date,
5906                 AI.Invoice_Currency_Code Invoice_Currency_Code,
5907                 AI.Payment_Currency_Code Payment_Currency_Code,
5908                 AI.Exchange_Rate Exchange_Rate,
5909                 AI.Exchange_Date Exchange_Date,
5910                 AI.Exchange_Rate_Type Exchange_Rate_Type,
5911                 PS.Payment_Num Payment_Num,
5912                 TRUNC(PS.Due_Date) Due_Date,
5913                 PS.Created_By Created_By,
5914                 PS.Gross_Amount Amount_Remaining,
5915                 DECODE(SIGN(TRUNC(PS.Due_Date) - AI.Entered_Date), -1,
5916                            PS.Gross_Amount, 0) Past_Due_Amount,
5917                 NVL(PS.Discount_Amount_Available,0) Discount_Available,
5918                 0 Discount_Taken,
5919                 0 Discount_Lost,
5920                 0 Payment_Amount,
5921                 0 On_Time_Payment_Amt,
5922                 0 Late_Payment_Amt,
5923                 0 No_Days_Late,
5924                 CASE
5925                   WHEN (TRUNC(PS.Due_Date) - AI.Entered_Date) >= g_due_bucket1
5926                         THEN PS.Gross_Amount
5927                   ELSE  0
5928                 END Due_Bucket1,
5929                 CASE
5930                   WHEN (TRUNC(PS.Due_Date) - AI.Entered_Date) <= g_due_bucket2
5931                    AND (TRUNC(PS.Due_Date) - AI.Entered_Date) >  g_due_bucket3
5932                         THEN PS.Gross_Amount
5933                   ELSE  0
5934                 END Due_Bucket2,
5935                 CASE
5936                   WHEN (TRUNC(PS.Due_Date) - AI.Entered_Date) <= g_due_bucket3
5937                    AND (TRUNC(PS.Due_Date) - AI.Entered_Date) >=  0
5938                         THEN PS.Gross_Amount
5939                   ELSE  0
5940                 END Due_Bucket3,
5941                 CASE
5942                   WHEN (AI.Entered_Date - TRUNC(PS.Due_Date)) >= g_past_bucket1
5943                         THEN PS.Gross_Amount
5944                   ELSE  0
5945                 END Past_Due_Bucket1,
5946                 CASE
5947                   WHEN (AI.Entered_Date - TRUNC(PS.Due_Date)) <= g_past_bucket2
5948                    AND (AI.Entered_Date - TRUNC(PS.Due_Date)) >  g_past_bucket3
5949                         THEN PS.Gross_Amount
5950                   ELSE  0
5951                 END Past_Due_Bucket2,
5952                 CASE
5953                   WHEN (AI.Entered_Date - TRUNC(PS.Due_Date)) <= g_past_bucket3
5954                    AND (AI.Entered_Date - TRUNC(PS.Due_Date)) > 0
5955                         THEN PS.Gross_Amount
5956                   ELSE  0
5957                 END Past_Due_Bucket3
5958          FROM   AP_Payment_Schedules_All PS,
5959                 FII_AP_Invoice_B AI
5960          WHERE  PS.Invoice_ID = AI.Invoice_ID
5961          AND    AI.Invoice_ID BETWEEN g_start_range and g_end_range
5962          AND    AI.Invoice_Type NOT IN ('PREPAYMENT')
5963          AND    AI.Cancel_Date IS NULL) PSUM,
5964          FII_AP_PS_Rates_Temp   RATES,
5965          FII_AP_Func_Rates_Temp FRATES
5966   WHERE  FRATES.To_Currency   = PSUM.Base_Currency_Code
5967   AND    FRATES.From_Currency = PSUM.Payment_Currency_Code
5968   AND    FRATES.Trx_Date      = PSUM.Exchange_Date
5969   AND    DECODE(PSUM.Exchange_Rate_Type,'User', PSUM.Exchange_Rate,1) =
5970                  DECODE(FRATES.Conversion_Type,'User', FRATES.Conversion_Rate,1)
5971   AND    FRATES.Conversion_Type    = PSUM.Exchange_Rate_Type
5972   AND    RATES.Functional_Currency = PSUM.Base_Currency_Code
5973   AND    RATES.Trx_Date            = PSUM.Invoice_Date;
5974 
5975 
5976   if g_debug_flag = 'Y' then
5977      FII_UTIL.put_line('Inserted ' ||SQL%ROWCOUNT|| ' Creation records into FII_AP_PAY_SCHED_B');
5978      FII_UTIL.put_timestamp('End Timestamp');
5979      FII_UTIL.stop_timer;
5980      FII_UTIL.print_timer('Duration');
5981      FII_UTIL.put_line('');
5982   end if;
5983 
5984   COMMIT;
5985 
5986   if g_debug_flag = 'Y' then
5987      FII_UTIL.put_line('Calling procedure POPULATE_PS_DISC_ACTION');
5988      FII_UTIL.put_line('');
5989   end if;
5990 
5991   POPULATE_PS_DISCOUNT_ACTION;
5992 
5993 
5994   if g_debug_flag = 'Y' then
5995      FII_UTIL.put_line('Calling procedure POPULATE_PS_PAYMENT_ACTION');
5996      FII_UTIL.put_line('');
5997   end if;
5998 
5999   POPULATE_PS_PAYMENT_ACTION;
6000 
6001 
6002   if g_debug_flag = 'Y' then
6003      FII_UTIL.put_line('Calling procedure POPULATE_PS_DUE_ACTION');
6004      FII_UTIL.put_line('');
6005   end if;
6006 
6007   POPULATE_PS_DUE_ACTION;
6008 
6009 
6010   if g_debug_flag = 'Y' then
6011      FII_UTIL.put_line('Calling procedure POPULATE_PS_BUCKET_ACTION');
6012      FII_UTIL.put_line('');
6013   end if;
6014 
6015   POPULATE_PS_BUCKET_ACTION;
6016 
6017 
6018   if g_debug_flag = 'Y' then
6019      FII_UTIL.put_line('Calling procedure POPULATE_PS_PAST_BUCKET_ACTION');
6020      FII_UTIL.put_line('');
6021   end if;
6022 
6023   POPULATE_PS_PAST_BUCKET_ACTION;
6024 
6025 EXCEPTION
6026    WHEN OTHERS THEN
6027       g_errbuf:=sqlerrm;
6028       g_retcode:= -1;
6029       g_exception_msg  := g_retcode || ':' || g_errbuf;
6030       FII_UTIL.put_line('Error occured while ' || g_state);
6031       FII_UTIL.put_line(g_exception_msg);
6032       RAISE;
6033 END;
6034 
6035 
6036 ------------------------------------------------------------------
6037 -- Procedure POPULATE_PS_BUCKET_COUNT
6038 -- Purpose
6039 --   This POPULATE_PS_BUCKET_COUNT routine inserts records into the
6040 --   FII AP Bucket Count table.
6041 --
6042 --   This procedure is divided into four steps.
6043 --
6044 --
6045 --
6046 ------------------------------------------------------------------
6047 
6048 PROCEDURE POPULATE_PS_BUCKET_COUNT IS
6049 
6050 BEGIN
6051 
6052   g_state := 'Inside the procedure POPULATE_PS_BUCKET_COUNT';
6053   if g_debug_flag = 'Y' then
6054      FII_UTIL.put_line('');
6055      FII_UTIL.put_line(g_state);
6056   end if;
6057 
6058 /* **************************************************************************
6059 Steps to populate FII_AP_AGING_BKTS_B and FII_AP_DUE_COUNTS_B:
6060 
6061 1.  To determine the action_date that a payment schedule enters
6062     and exits a bucket, if it enters a bucket at all, we need to know
6063     the creation date, due date,and fully paid date of each payment
6064     schedule.  The subquery Bucket_Calcs stores this information.
6065 
6066 2.  Each payment schedule can have at most 12 action dates.  Date 1 is
6067     when a payment schedule enters bucket 1, date 2 is when a payment
6068     schedule exits bucket 1, date 3 is when a payment schedule enters
6069     bucket 2, etc.  The subquery TMP stores the marker 1-12.
6070 
6071 3.  We join Bucket_Calcs and TMP to calculate up to 12 action dates.
6072     Depending on certain conditions, the entry date of a bucket will
6073     be either the creation date, the date a payment schedule moves
6074     buckets, or null.  The exit date of a bucket will be either the
6075     fully paid date, the date a payment schedule moves buckets, or
6076     null.  We also have a variable for each bucket.  For entry into a
6077     bucket we store a +1, for exit from a bucket we store a -1.  We do
6078     not keep rows with a null date.  This subquery is essentially
6079     FII_AP_AGING_BKTS_B at the payment schedule level.
6080 
6081 4.  To populate FII_AP_AGING_BKTS_B at the invoice level, group by
6082     invoice_id and action_date (SDate).  Then for each date in order
6083     from earliest to latest date, keep a cumulative sum for each
6084     bucket.  For example, let's say an invoice has two payment
6085     schedules.  The first goes into bucket 1 on 01-01-2004, the second
6086     goes into bucket 1 on 01-01-2004, the first exits bucket 1 on
6087     01-15-2004, the second exits bucket 1 on 01-31-2004:
6088 
6089     SDate            SUM(B1)   SB1
6090     01-01-2004        2         2
6091     01-15-2004       -1         1
6092     01-31-2004       -1         0
6093 
6094     To populate FII_AP_DUE_COUNTS_B, group by invoice_id and
6095     action_date and keep a cumulative sum for all due buckets and all
6096     past due buckets.
6097 
6098 5.  The most outer subquery looks at the cumulative sum of the
6099     previous subquery to determine when an invoice, not a payment
6100     schedule, enters and exits a bucket.  If the cumulative sum is
6101     increased from 0, then the invoice enters the bucket on that
6102     date.  If the cumulative sum is decreased to 0, then the invoice
6103     exits the bucket on that date.  In our example:
6104 
6105     SDate            Due_Bucket1_Cnt
6106     01-01-2004            1
6107     01-15-2004            0
6108     01-31-2004           -1
6109 
6110 6.  Populate FII_AP_AGING_BKTS_B with those records with non-zero Due
6111     Bucket or Past Due Bucket Counts.  Populate FII_AP_DUE_COUNTS_B
6112     with those records with non-zero Due or Past Due Counts.
6113 ************************************************************************** */
6114 
6115   g_state := 'Deleting records from FII_AP_INV_BUCKETS that are already existing';
6116   if g_debug_flag = 'Y' then
6117      FII_UTIL.put_line('');
6118      FII_UTIL.put_line(g_state);
6119   end if;
6120 
6121 
6122   /* For Initial Load we will truncate the data in the count table
6123      and re-populate this table */
6124   TRUNCATE_TABLE('MLOG$_FII_AP_AGING_BKTS_B');
6125   TRUNCATE_TABLE('MLOG$_FII_AP_DUE_COUNTS_B');
6126 
6127   TRUNCATE_TABLE('FII_AP_AGING_BKTS_B');
6128   TRUNCATE_TABLE('FII_AP_DUE_COUNTS_B');
6129 
6130   g_state := 'Populating FII_AP_AGING_BKTS_B and FII_AP_DUE_COUNTS_B table';
6131   if g_debug_flag = 'Y' then
6132      FII_UTIL.put_line(g_state);
6133      FII_UTIL.start_timer;
6134      FII_UTIL.put_line('');
6135   end if;
6136 
6137 
6138   INSERT /*+ append parallel(fii_ap_aging_bkts_b) parallel(fii_ap_due_counts_b) */ ALL
6139   WHEN (   Due_Bucket1_Cnt <> 0
6140         OR Due_Bucket2_Cnt <> 0
6141         OR Due_Bucket3_Cnt <> 0
6142         OR Past_Due_Bucket1_Cnt <> 0
6143         OR Past_Due_Bucket2_Cnt <> 0
6144         OR Past_Due_Bucket3_Cnt <> 0)
6145   THEN INTO FII_AP_AGING_BKTS_B(
6146 	Time_ID,
6147 	Period_Type_ID,
6148 	Org_ID,
6149 	Supplier_ID,
6150 	Invoice_ID,
6151 	Action_Date,
6152 	Due_Bucket1_Cnt,
6153 	Due_Bucket2_Cnt,
6154 	due_bucket3_Cnt,
6155 	Past_Due_Bucket3_Cnt,
6156 	Past_Due_Bucket2_Cnt,
6157 	Past_Due_Bucket1_Cnt,
6158 	Created_By,
6159 	Creation_Date,
6160 	Last_Updated_By,
6161 	Last_Update_Date,
6162 	Last_Update_Login)
6163   VALUES (
6164 	TO_NUMBER(TO_CHAR(Action_Date, 'J')),
6165 	1,
6166 	Org_ID,
6167 	Supplier_ID,
6168 	Invoice_ID,
6169 	Action_Date,
6170 	Due_Bucket1_Cnt,
6171 	Due_Bucket2_Cnt,
6172 	Due_Bucket3_Cnt,
6173 	Past_Due_Bucket3_Cnt,
6174 	Past_Due_Bucket2_Cnt,
6175 	Past_Due_Bucket1_Cnt,
6176 	g_fii_user_id,
6177 	sysdate,
6178 	g_fii_user_id,
6179 	sysdate,
6180 	g_fii_login_id)
6181   WHEN (   Due_Cnt <> 0
6182         OR Past_Due_Cnt <> 0)
6183   THEN INTO FII_AP_DUE_COUNTS_B (
6184 	Time_ID,
6185 	Period_Type_ID,
6186 	Org_ID,
6187 	Supplier_ID,
6188 	Invoice_ID,
6189 	Action_Date,
6190 	Due_Cnt,
6191 	Past_Due_Cnt,
6192 	Created_By,
6193 	Creation_Date,
6194 	Last_Updated_By,
6195 	Last_Update_Date,
6196 	Last_Update_Login)
6197   VALUES (
6198 	TO_NUMBER(TO_CHAR(Action_Date, 'J')),
6199 	1,
6200 	Org_ID,
6201 	Supplier_ID,
6202 	Invoice_ID,
6203 	Action_Date,
6204 	Due_Cnt,
6205 	Past_Due_Cnt,
6206 	g_fii_user_id,
6207 	sysdate,
6208 	g_fii_user_id,
6209 	sysdate,
6210 	g_fii_login_id)
6211   SELECT Org_ID,
6212          Supplier_ID,
6213          Invoice_ID,
6214          SDate Action_Date,
6215          CASE WHEN SB1 > 0
6216               AND NVL(lag(SB1) OVER (PARTITION BY Org_ID, Supplier_ID, Invoice_ID
6217                                      ORDER BY SDate), 0) = 0
6218               THEN 1
6219               WHEN SB1 = 0
6220               AND NVL(lag(SB1) OVER (PARTITION BY Org_ID, Supplier_ID, Invoice_ID
6221                                      ORDER BY SDate), 0) > 0 then -1
6222               ELSE 0
6223          END Due_Bucket1_Cnt,
6224          CASE WHEN SB2 > 0
6225               AND NVL(lag(SB2) OVER (PARTITION BY Org_ID, Supplier_ID, Invoice_ID
6226                                      ORDER BY SDate), 0) = 0
6227               THEN 1
6228               WHEN SB2 = 0
6229               AND NVL(lag(SB2) OVER (PARTITION BY Org_ID, Supplier_ID, Invoice_ID
6230                                      ORDER BY SDate), 0) > 0
6231               THEN -1
6232               ELSE 0
6233          END Due_Bucket2_Cnt,
6234          CASE WHEN SB3 > 0
6235               AND NVL(lag(SB3) OVER (PARTITION BY Org_ID, Supplier_ID, Invoice_ID
6236                                      ORDER BY SDate), 0) = 0
6237               THEN 1
6238               WHEN SB3 = 0
6239               AND NVL(lag(SB3) OVER (PARTITION BY Org_ID, Supplier_ID, Invoice_ID
6240                                      ORDER BY SDate), 0) > 0
6241               THEN -1
6242               ELSE 0
6243          END Due_Bucket3_Cnt,
6244          CASE WHEN SB4 > 0
6245               AND NVL(lag(SB4) OVER (PARTITION BY Org_ID, Supplier_ID, Invoice_ID
6246                                      ORDER BY SDate), 0) = 0
6247               THEN 1
6248               WHEN SB4 = 0
6249               AND NVL(lag(SB4) OVER(PARTITION BY Org_ID, Supplier_ID, Invoice_ID
6250                                     ORDER BY SDate), 0) > 0
6251               THEN -1
6252               ELSE 0
6253          END Past_Due_Bucket3_Cnt,
6254          CASE WHEN SB5 > 0
6255               AND NVL(lag(SB5) OVER (PARTITION BY Org_ID, Supplier_ID, Invoice_ID
6256                                      ORDER BY SDate), 0) = 0
6257               THEN 1
6258               WHEN SB5 = 0
6259               AND NVL(lag(SB5) OVER (PARTITION BY Org_ID, Supplier_ID, Invoice_ID
6260                                      ORDER BY SDate), 0) > 0
6261               THEN -1
6262               ELSE 0
6263          END Past_Due_Bucket2_Cnt,
6264          CASE WHEN SB6 > 0
6265               AND NVL(lag(SB6) OVER (PARTITION BY Org_ID, Supplier_ID, Invoice_ID
6266                                      ORDER BY SDate), 0) = 0
6267               THEN 1
6268               WHEN SB6 = 0
6269               AND NVL(lag(SB6) OVER (PARTITION BY Org_ID, Supplier_ID, Invoice_ID
6270                                      ORDER BY SDate), 0) > 0
6271               THEN -1
6272               ELSE 0
6273          END Past_Due_Bucket1_Cnt,
6274          CASE WHEN D > 0
6275               AND NVL(lag(D) OVER (PARTITION BY Org_ID, Supplier_ID, Invoice_ID
6276                                    ORDER BY SDate), 0) = 0
6277               THEN 1
6278               WHEN D = 0
6279               AND NVL(lag(D) OVER (PARTITION BY Org_ID, Supplier_ID, Invoice_ID
6280                                    ORDER BY SDate), 0) > 0
6281               THEN -1
6282               ELSE 0
6283          END Due_Cnt,
6284          CASE WHEN PD > 0
6285               AND nvl(lag(PD) OVER (PARTITION BY Org_ID, Supplier_ID, Invoice_ID
6286                                     ORDER BY SDate), 0) = 0
6287               THEN 1
6288               WHEN PD = 0
6289               AND NVL(lag(PD) OVER (PARTITION BY Org_ID, Supplier_ID, Invoice_ID
6290                                     ORDER BY SDate), 0) > 0
6291               THEN -1
6292               ELSE 0
6293          END Past_Due_Cnt
6294   FROM (SELECT Org_ID,
6295                Supplier_ID,
6296                Invoice_ID,
6297                SDate,
6298                SUM(SUM(B1)) OVER (PARTITION BY Org_ID, Supplier_ID, Invoice_ID
6299                                   ORDER BY SDate
6300                                   ROWS UNBOUNDED PRECEDING) SB1,
6301                SUM(SUM(B2)) OVER (PARTITION BY Org_ID, Supplier_ID, Invoice_ID
6302                                   ORDER BY SDate
6303                                   ROWS UNBOUNDED PRECEDING) SB2,
6304                SUM(SUM(B3)) OVER (PARTITION BY Org_ID, Supplier_ID, Invoice_ID
6305                                   ORDER BY SDate
6306                                   ROWS UNBOUNDED PRECEDING) SB3,
6307                SUM(SUM(B4)) OVER (PARTITION BY Org_ID, Supplier_ID, Invoice_ID
6308                                   ORDER BY SDate
6309                                   ROWS UNBOUNDED PRECEDING) SB4,
6310                SUM(SUM(B5)) OVER (PARTITION BY Org_ID, Supplier_ID, Invoice_ID
6311                                   ORDER BY SDate
6312                                   ROWS UNBOUNDED PRECEDING) SB5,
6313                SUM(SUM(B6)) OVER (PARTITION BY Org_ID, Supplier_ID, Invoice_ID
6314                                   ORDER BY SDate
6315                                   ROWS UNBOUNDED PRECEDING) SB6,
6316                SUM(SUM(B1+B2+B3)) OVER (PARTITION BY Org_ID, Supplier_ID, Invoice_ID
6317                                         ORDER BY SDate
6318                                         ROWS UNBOUNDED PRECEDING) D,
6319                SUM(SUM(B4+B5+B6)) OVER (PARTITION BY Org_ID, Supplier_ID, Invoice_ID
6320                                         ORDER BY SDate
6321                                         ROWS UNBOUNDED PRECEDING) PD
6322         FROM (
6323               SELECT Org_ID,
6324                      Supplier_ID,
6325                      Invoice_ID,
6326                      Payment_Num,
6327                      CASE TMP.Marker
6328                           WHEN 1 THEN CASE WHEN Creation_Date < Due_Date-g_due_bucket2 THEN Creation_Date END
6329                           WHEN 2 THEN CASE WHEN Creation_Date < Due_Date-g_due_bucket2
6330                                            AND nvl(Fully_Paid_Date, Due_Date-g_due_bucket2) <= trunc(sysdate)
6331                                            THEN least(nvl(Fully_Paid_Date, Due_Date-g_due_bucket2), Due_Date-g_due_bucket2) END
6332                           WHEN 3 THEN CASE WHEN Creation_Date < Due_Date-g_due_bucket3
6333                                            AND nvl(Fully_Paid_Date, Due_Date-g_due_bucket2) <= trunc(sysdate)
6334                                            AND nvl(Fully_Paid_Date, Due_Date-g_due_bucket3) >= Due_Date-g_due_bucket2
6335                                            THEN greatest(Creation_Date, Due_Date-g_due_bucket2) END
6336                           WHEN 4 THEN CASE WHEN Creation_Date < Due_Date-g_due_bucket3
6337                                            AND nvl(Fully_Paid_Date, Due_Date-g_due_bucket3) <= trunc(sysdate)
6338                                            AND nvl(Fully_Paid_Date, Due_Date-g_due_bucket3) >= Due_Date-g_due_bucket2
6339                                            THEN least(nvl(Fully_Paid_Date, Due_Date-g_due_bucket3), Due_Date-g_due_bucket3) END
6340                           WHEN 5 THEN CASE WHEN Creation_Date < Due_Date+1
6341                                            AND nvl(Fully_Paid_Date, Due_Date-g_due_bucket3) <= trunc(sysdate)
6342                                            AND nvl(Fully_Paid_Date, Due_Date) >= Due_Date-g_due_bucket3
6343                                            THEN greatest(Creation_Date, Due_Date-g_due_bucket3) END
6344                           WHEN 6 THEN CASE WHEN Creation_Date < Due_Date+1
6345                                            AND nvl(Fully_Paid_Date, Due_Date+1) <= trunc(sysdate)
6346                                            AND nvl(Fully_Paid_Date, Due_Date) >= Due_Date-g_due_bucket3
6347                                            THEN least(nvl(Fully_Paid_Date, Due_Date+1), Due_Date+1) END
6348                           WHEN 7 THEN CASE WHEN Creation_Date <= Due_Date+g_past_bucket3
6349                                            AND nvl(Fully_Paid_Date, Due_Date+1) <= trunc(sysdate)
6350                                            AND nvl(Fully_Paid_Date, Due_Date+g_past_bucket3) >= Due_Date+1
6351                                            THEN greatest(Creation_Date, Due_Date+1) END
6352                           WHEN 8 THEN CASE WHEN Creation_Date <= Due_Date+g_past_bucket3
6353                                            AND nvl(Fully_Paid_Date, Due_Date+g_past_bucket3+1) <= trunc(sysdate)
6354                                            AND nvl(Fully_Paid_Date, Due_Date+g_past_bucket3) >= Due_Date+1
6355                                            THEN least(nvl(Fully_Paid_Date, Due_Date+g_past_bucket3+1), Due_Date+g_past_bucket3+1) END
6356                           WHEN 9 THEN CASE WHEN Creation_Date <= Due_Date+g_past_bucket2
6357                                            AND nvl(Fully_Paid_Date, Due_Date+g_past_bucket3+1) <= trunc(sysdate)
6358                                            AND nvl(Fully_Paid_Date, Due_Date+g_past_bucket2) >= Due_Date+g_past_bucket3+1
6359                                            THEN greatest(Creation_Date, Due_Date+g_past_bucket3+1) END
6360                           WHEN 10 THEN CASE WHEN Creation_Date <= Due_Date+g_past_bucket2
6361                                             AND nvl(Fully_Paid_Date, Due_Date+g_past_bucket1) <= trunc(sysdate)
6362                                             AND nvl(Fully_Paid_Date, Due_Date+g_past_bucket2) >= Due_Date+g_past_bucket3+1
6363                                             THEN least(nvl(Fully_Paid_Date, Due_Date+g_past_bucket1), Due_Date+g_past_bucket1) END
6364                           WHEN 11 THEN CASE WHEN nvl(Fully_Paid_Date, Due_Date+g_past_bucket1) >= Due_Date+g_past_bucket1
6365                                             AND nvl(Fully_Paid_Date, Due_Date+g_past_bucket1) <= trunc(sysdate)
6366                                             THEN greatest(Creation_Date, Due_Date+g_past_bucket1) END
6367                           WHEN 12 THEN CASE WHEN nvl(Fully_Paid_Date, Due_Date+g_past_bucket1) >= Due_Date+g_past_bucket1
6368                                             THEN Fully_Paid_Date END
6369 	             END SDate,
6370 	             decode(TMP.Marker, 1, 1, 2, -1, 0) b1,
6371 	             decode(TMP.Marker, 3, 1, 4, -1, 0) b2,
6372                      decode(TMP.Marker, 5, 1, 6, -1, 0) b3,
6373                      decode(TMP.Marker, 7, 1, 8, -1, 0) b4,
6374                      decode(TMP.Marker, 9, 1, 10, -1, 0) b5,
6375                      decode(TMP.Marker, 11, 1, 12, -1, 0) b6
6376               FROM (SELECT /*+ parallel(ps) parallel(ai) */
6377 	                   AI.Org_ID,
6378                            AI.Supplier_ID,
6379                            PS.Invoice_ID,
6380                            PS.Payment_Num,
6381                            AI.Entered_Date Creation_Date,
6382                            trunc(PS.Due_Date) Due_Date,
6383                            CASE WHEN nvl(PAY.Payment_Amount, 0) + nvl(TEMP.WH_Tax_Amount, 0)
6384                                      + nvl(PP.Prepay_Amount, 0) = PS.Gross_Amount
6385                                 THEN CASE WHEN PAY.Payment_Date is not null
6386                                           AND PP.Payment_Date is not null
6387                                           THEN greatest(PAY.Payment_Date, PP.Payment_Date)
6388                                           ELSE nvl(PAY.Payment_Date, PP.Payment_Date) END
6389 	                        ELSE null END Fully_Paid_Date
6390                     FROM AP_Payment_Schedules_All PS,
6391 	                 FII_AP_INVOICE_B AI,
6392 	                (SELECT /*+ parallel(aip) */ AIP.Invoice_ID,
6393                                 AIP.Payment_Num,
6394                                 sum(AIP.Amount + nvl(AIP.Discount_Taken, 0)) Payment_Amount,
6395                                 trunc(max(AIP.Creation_Date)) Payment_Date
6396                          FROM AP_Invoice_Payments_ALL AIP
6397                	         GROUP BY AIP.Invoice_id, AIP.Payment_Num) PAY,
6398                         (SELECT /*+ parallel(t) */ Invoice_ID,
6399                                 Payment_Num,
6400                                 sum(WH_Tax_Amount) WH_Tax_Amount
6401                          FROM FII_AP_WH_Tax_T t
6402                          GROUP BY Invoice_ID, Payment_Num) TEMP,
6403                         (SELECT /*+ parallel(p) */ Invoice_ID,
6404                                 Payment_Num,
6405                                 sum(Prepay_Amount) Prepay_Amount,
6406                                 trunc(max(Creation_Date)) Payment_Date
6407                          FROM FII_AP_Prepay_T p
6408                          GROUP BY Invoice_ID, Payment_Num) PP
6409                     WHERE AI.Invoice_ID = PS.Invoice_ID
6410                     AND AI.Invoice_Type NOT IN ('PREPAYMENT')
6411                     AND AI.Cancel_Date IS NULL
6412                     AND PS.Invoice_ID = PAY.Invoice_ID (+)
6413                     AND PS.Payment_num = PAY.Payment_Num (+)
6414                     AND PS.Invoice_ID = TEMP.Invoice_ID (+)
6415                     AND PS.Payment_Num = TEMP.Payment_Num (+)
6416                     AND PS.Invoice_ID = PP.Invoice_ID (+)
6417                     AND PS.Payment_Num = PP.Payment_Num (+)) Bucket_Calcs,
6418                    (SELECT 1 marker FROM DUAL UNION ALL
6419                     SELECT 2 marker FROM DUAL UNION ALL
6420                     SELECT 3 marker FROM DUAL UNION ALL
6421                     SELECT 4 marker FROM DUAL UNION ALL
6422                     SELECT 5 marker FROM DUAL UNION ALL
6423                     SELECT 6 marker FROM DUAL UNION ALL
6424                     SELECT 7 marker FROM DUAL UNION ALL
6425                     SELECT 8 marker FROM DUAL UNION ALL
6426                     SELECT 9 marker FROM DUAL UNION ALL
6427                     SELECT 10 marker FROM DUAL UNION ALL
6428                     SELECT 11 marker FROM DUAL UNION ALL
6429                     SELECT 12 marker FROM DUAL) TMP)
6430         WHERE SDate IS NOT NULL
6431         GROUP BY Org_ID, Supplier_ID, Invoice_ID, SDate)
6432 
6433 COMMIT;
6434 
6435 
6436   if g_debug_flag = 'Y' then
6437      FII_UTIL.put_line('Inserted '|| SQL%ROWCOUNT ||' records into FII_AP_AGING_BKTS_B and  FII_AP_DUE_COUNTS_B');
6438      FII_UTIL.stop_timer;
6439      FII_UTIL.print_timer('Duration');
6440      FII_UTIL.put_line('');
6441   end if;
6442 
6443 --  g_state := 'Truncating temp tables used to populate FII_AP_AGING_BKTS_B and FII_AP_DUE_COUNTS_B';
6444 --  TRUNCATE_TABLE('FII_AP_BUCKET_CALCS');
6445 --  TRUNCATE_TABLE('FII_AP_PS_BUCKETS');
6446 --  TRUNCATE_TABLE('FII_AP_INDEX_TEMP');
6447 
6448 EXCEPTION
6449    WHEN OTHERS THEN
6450       g_errbuf:=sqlerrm;
6451       g_retcode:= -1;
6452       g_exception_msg  := g_retcode || ':' || g_errbuf;
6453       FII_UTIL.put_line('Error occured while ' || g_state);
6454       FII_UTIL.put_line(g_exception_msg);
6455       RAISE;
6456 
6457 END POPULATE_PS_BUCKET_COUNT;
6458 
6459 
6460 
6461 ------------------------------------------------------------------
6462 -- Procedure POPULATE_HOLD_HISTORY
6463 -- Purpose
6464 --   This POPULATE_HOLD_HISTORY routine inserts records into the
6465 --   FII Hold History summary table.
6466 ------------------------------------------------------------------
6467 
6468 PROCEDURE POPULATE_HOLD_HISTORY IS
6469 
6470 BEGIN
6471 
6472   g_state := 'Inside the procedure POPULATE_HOLD_HISTORY';
6473   if g_debug_flag = 'Y' then
6474      FII_UTIL.put_line('');
6475      FII_UTIL.put_line(g_state);
6476   end if;
6477 
6478   g_state := 'Deleting records from FII_AP_HOLD_HIST_B that are already existing';
6479   if g_debug_flag = 'Y' then
6480      FII_UTIL.put_line('');
6481      FII_UTIL.put_line(g_state);
6482   end if;
6483 
6484 
6485   /* For Initial Load we will truncate the data in the hold history table
6486      and re-populate this table */
6487   TRUNCATE_TABLE('MLOG$_FII_AP_HOLD_HIST_B');
6488   TRUNCATE_TABLE('FII_AP_HOLD_HIST_B');
6489 
6490 
6491   g_state := 'Populating FII_AP_HOLD_HIST_B FROM AP_HOLDS_ALL table';
6492   if g_debug_flag = 'Y' then
6493      FII_UTIL.put_line(g_state);
6494   end if;
6495 
6496   g_state := 'Populating Hold and Release Records';
6497   if g_debug_flag = 'Y' then
6498      FII_UTIL.put_line(g_state);
6499      FII_UTIL.start_timer;
6500      FII_UTIL.put_line('');
6501   end if;
6502 
6503   /* Only insert a 'H' record for the first hold in a series of overlapping
6504      holds.  The first part of the union does this by checking if this hold is
6505      the first ever for the invoice or if all holds made before this hold have
6506      been released before the hold was made.
6507 
6508      Only insert a 'R' record for the last release in a series of overlapping
6509      holds, if all holds have been released.  The second part of the union does
6510      this by checking if this hold is the very last to be released or if all holds
6511      released after it were held after this hold was released.
6512   */
6513 
6514   INSERT /*+ append parallel(HH) */ INTO FII_AP_HOLD_HIST_B HH
6515          (TIME_ID,
6516           PERIOD_TYPE_ID,
6517           ORG_ID,
6518           SUPPLIER_ID,
6519           INVOICE_ID,
6520           SEQ_ID,
6521           ACTION,
6522           ACTION_DATE,
6523           CREATED_BY,
6524           CREATION_DATE,
6525           LAST_UPDATED_BY,
6526           LAST_UPDATE_DATE,
6527           LAST_UPDATE_LOGIN)
6528   SELECT TO_NUMBER(TO_CHAR(H_R_DATE,'J')),
6529          1,
6530          ORG_ID,
6531          SUPPLIER_ID,
6532          INVOICE_ID,
6533          DECODE(REC_TYPE, 'H', FII_AP_HOLD_HIST_B_S.NEXTVAL, NULL),
6534          REC_TYPE,
6535          H_R_DATE,
6536          g_fii_user_id CREATED_BY,
6537          sysdate CREATION_DATE,
6538          g_fii_user_id LAST_UPDATED_BY,
6539          sysdate LAST_UPDATE_DATE,
6540          g_fii_login_id LAST_UPDATE_LOGIN
6541   FROM
6542     (SELECT /*+ use_hash(AI,AH) parallel(AI) parallel(AH) */
6543         DISTINCT AI.ORG_ID,
6544         AI.SUPPLIER_ID,
6545         AI.INVOICE_ID,
6546         AH.REC_TYPE,
6547         TRUNC(AH.H_R_DATE) H_R_DATE
6548      FROM FII_AP_INVOICE_B AI,
6549          (SELECT /*+ no_merge */
6550                  Invoice_ID,
6551                  TRUNC(Hold_Date) H_R_DATE,
6552                  'H' Rec_Type
6553           FROM (SELECT  /*+ parallel(h) */
6554                         Invoice_ID,
6555                         Hold_Date,
6556                         MAX(Release_Date) OVER (PARTITION BY Invoice_ID
6557                                                 ORDER BY Hold_Date ASC
6558                                                 ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) Max_Previous
6559                 FROM (SELECT  /*+ parallel(h) no_merge full(h) */
6560                               Invoice_ID,
6561                               Hold_Date,
6562                               DECODE(Release_Lookup_Code, NULL, g_sysdate, last_update_date) Release_Date
6563                       FROM    AP_Holds_All h
6564                       WHERE Hold_Date >= g_start_date
6565                       AND   Hold_Date+0 <= g_end_date  --workaround for pq-between bug
6566                       ) h
6567                )
6568           WHERE (Max_Previous IS NULL OR TRUNC(Hold_Date) > TRUNC(Max_Previous))
6569           UNION ALL
6570           SELECT Invoice_ID,
6571                  TRUNC(Release_Date) H_R_DATE,
6572                  'R' Rec_Type
6573           FROM (SELECT /*+ parallel(h) */
6574                        Invoice_ID,
6575                        Release_Date,
6576                        Release_Lookup_Code,
6577                        MIN(Hold_Date) OVER (PARTITION BY Invoice_ID
6578                                             ORDER BY Release_Date ASC
6579                                             ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING) Min_After
6580                 FROM (SELECT /*+ parallel(h) no_merge full(h) */
6581                              Invoice_ID,
6582                              Hold_Date,
6583                              DECODE(Release_Lookup_Code, NULL, g_sysdate, Last_Update_Date) Release_Date,
6584                              Release_Lookup_Code
6585                       FROM   AP_Holds_All h
6586                       WHERE  Hold_Date >= g_start_date
6587                       AND    Hold_Date+0 <= g_end_date  -- workaround for pq-between bug
6588                       ) h
6589                )
6590           WHERE (Min_After IS NULL OR TRUNC(Min_After) > TRUNC(Release_Date))
6591           AND   Release_Lookup_Code IS NOT NULL) AH --Filter release records for unreleased holds
6592      WHERE AI.Invoice_ID = AH.Invoice_ID
6593      AND AI.Cancel_Date IS NULL
6594      AND AI.Invoice_Type NOT IN ('PREPAYMENT'));
6595 
6596 
6597   if g_debug_flag = 'Y' then
6598      FII_UTIL.put_line('Inserted '|| SQL%ROWCOUNT ||' Hold and Release records into FII_AP_HOLD_HIST_B');
6599      FII_UTIL.stop_timer;
6600      FII_UTIL.print_timer('Duration');
6601      FII_UTIL.put_line('');
6602   end if;
6603 
6604   COMMIT;
6605 
6606   g_state := 'Updating Seq_ID for the Release Records';
6607   if g_debug_flag = 'Y' then
6608      FII_UTIL.put_line(g_state);
6609      FII_UTIL.start_timer;
6610      FII_UTIL.put_line('');
6611   end if;
6612 
6613   UPDATE FII_AP_Hold_Hist_B HH
6614   SET    Seq_ID = (SELECT HH1.Seq_ID
6615                    FROM   FII_AP_Hold_Hist_B HH1
6616                    WHERE  HH1.Action = 'H'
6617                    AND    HH1.Invoice_ID = HH.Invoice_ID
6618                    AND    HH1.Action_Date IN
6619                          (SELECT MIN(TRUNC(AH1.Hold_Date))
6620                           FROM   AP_Holds_ALL AH1, AP_Holds_ALL AH2
6621                           WHERE  AH1.Invoice_ID = HH.Invoice_ID
6622                           AND    AH2.Invoice_ID = HH.Invoice_ID
6623                           AND    TRUNC(AH2.Last_Update_Date) = HH.Action_Date
6624                           AND    AH2.Release_Lookup_Code IS NOT NULL
6625                           AND    TRUNC(AH1.Last_Update_Date) >= TRUNC(AH2.Hold_Date)
6626                           AND    AH1.Release_Lookup_Code IS NOT NULL
6627                           AND    TRUNC(AH1.Last_Update_Date)
6628                                      <= TRUNC(AH2.Last_Update_Date)))
6629   WHERE  HH.Action = 'R'
6630   AND    HH.Seq_ID IS NULL;
6631 
6632 
6633   if g_debug_flag = 'Y' then
6634      FII_UTIL.put_line('Updated '|| SQL%ROWCOUNT ||' Release records in the FII_AP_HOLD_HIST_B');
6635      FII_UTIL.stop_timer;
6636      FII_UTIL.print_timer('Duration');
6637      FII_UTIL.put_line('');
6638   end if;
6639 
6640 /* We currently do not use hold_count in any mv or detail report,
6641    so it is safe to remove this update.
6642 
6643   g_state := 'Updating the Hold Count on the Hold and Release records';
6644   if g_debug_flag = 'Y' then
6645      FII_UTIL.put_line(g_state);
6646      FII_UTIL.start_timer;
6647      FII_UTIL.put_line('');
6648   end if;
6649 
6650   UPDATE FII_AP_Hold_Hist_B HH
6651   SET Hold_Count = (SELECT DECODE(HH.Action,'H', COUNT(*), -1 * COUNT(*))
6652                     FROM   AP_Holds_ALL AH
6653                     WHERE  AH.Invoice_ID = HH.Invoice_ID
6654                     AND   (EXISTS (SELECT 'Hold Exists'
6655                                    FROM   FII_AP_Hold_Hist_B HH1
6656                                    WHERE  HH1.Invoice_ID = AH.Invoice_ID
6657                                    AND    HH1.Seq_ID = HH.Seq_ID
6658                                    AND    TRUNC(AH.Hold_Date) >= DECODE(HH.Action,'H',
6659                                                  HH.Action_Date, HH1.Action_Date)
6660                                    AND    AH.Release_Lookup_Code IS NOT NULL
6661                                    AND    TRUNC(AH.Last_Update_Date) <=
6662                                               DECODE(HH.Action,'H',HH1.Action_Date,
6663                                                                    HH.Action_Date)
6664                                    AND    HH1.Rowid <> HH.Rowid)
6665                     OR     NOT EXISTS (SELECT 'Release Exists'
6666                                        FROM   FII_AP_Hold_Hist_B HH2
6667                                        WHERE  HH2.Invoice_ID = AH.Invoice_ID
6668                                        AND    HH.Seq_ID = HH2.Seq_ID
6669                                        AND    HH2.Rowid <> HH.Rowid)))
6670   WHERE HH.Hold_Count IS NULL;
6671 
6672 
6673   if g_debug_flag = 'Y' then
6674      FII_UTIL.put_line('Updated '|| SQL%ROWCOUNT ||' Hold Counts in the FII_AP_HOLD_HIST_B');
6675      FII_UTIL.stop_timer;
6676      FII_UTIL.print_timer('Duration');
6677      FII_UTIL.put_line('');
6678   end if;
6679 */
6680 
6681 EXCEPTION
6682    WHEN OTHERS THEN
6683       g_errbuf:=sqlerrm;
6684       g_retcode:= -1;
6685       g_exception_msg  := g_retcode || ':' || g_errbuf;
6686       FII_UTIL.put_line('Error occured while ' || g_state);
6687       FII_UTIL.put_line(g_exception_msg);
6688       RAISE;
6689 
6690 END POPULATE_HOLD_HISTORY;
6691 
6692 
6693 
6694 -- ------------------------------------------------------------
6695 -- Public Functions and Procedures
6696 -- ------------------------------------------------------------
6697 
6698 --------------------------------------------------
6699 -- PROCEDURE WORKER
6700 --   This Worker routine Handles all functions involved in the
6701 --   Payment Schedule Summarization and populating FII_AP_PAY_SCHED_B
6702 --   summary table.
6703 
6704 ---------------------------------------------------
6705 PROCEDURE WORKER(Errbuf       IN OUT NOCOPY VARCHAR2,
6706                  Retcode      IN OUT NOCOPY VARCHAR2,
6707                  p_from_date  IN     VARCHAR2,
6708                  p_to_date    IN     VARCHAR2,
6709                  p_worker_no  IN     NUMBER
6710                 ) IS
6711 
6712   l_unassigned_cnt       NUMBER := 0;
6713   l_failed_cnt           NUMBER := 0;
6714   l_curr_unasgn_cnt      NUMBER := 0;
6715   l_curr_comp_cnt        NUMBER := 0;
6716   l_curr_tot_cnt         NUMBER := 0;
6717   l_count                NUMBER;
6718   l_start_range          NUMBER;
6719   l_end_range            NUMBER;
6720 
6721 BEGIN
6722   g_state := 'Inside the procedure WORKER';
6723   if g_debug_flag = 'Y' then
6724     FII_UTIL.put_line(g_state);
6725   end if;
6726 
6727   Errbuf := NULL;
6728   Retcode:= 0;
6729 
6730   EXECUTE IMMEDIATE 'ALTER SESSION ENABLE PARALLEL DML';
6731   EXECUTE IMMEDIATE 'ALTER SESSION SET MAX_DUMP_FILE_SIZE=UNLIMITED';
6732   EXECUTE IMMEDIATE 'ALTER SESSION SET SQL_TRACE TRUE';
6733   EXECUTE IMMEDIATE 'ALTER SESSION SET HASH_AREA_SIZE = 100000000';
6734   EXECUTE IMMEDIATE 'ALTER SESSION SET SORT_AREA_SIZE = 100000000';
6735 
6736   -- -----------------------------------------------
6737   -- Set up directory structure for child process
6738   -- because child process do not call setup routine
6739   -- from EDWCORE
6740   -- -----------------------------------------------
6741   CHILD_SETUP('FII_AP_INV_SUM_INIT_SUBWORKER'||p_worker_no);
6742 
6743   g_start_date := p_from_date;
6744 
6745 
6746 
6747   g_end_date   := p_to_date;
6748 
6749   if g_debug_flag = 'Y' then
6750      FII_UTIL.put_line(' ');
6751      FII_UTIL.put_timestamp;
6752      FII_UTIL.put_line('Worker '||p_worker_no||' Starting');
6753   end if;
6754 
6755   -- ------------------------------------------
6756   -- Initalization
6757   -- ------------------------------------------
6758   if g_debug_flag = 'Y' then
6759      FII_UTIL.put_line(' ');
6760      FII_UTIL.put_line('Initialization');
6761   end if;
6762 
6763   INIT;
6764 
6765   -- ------------------------------------------
6766   -- Loop thru job list
6767   -- -----------------------------------------
6768   g_state := 'Looping through job list';
6769   LOOP
6770     SELECT nvl(sum(decode(status,'UNASSIGNED',1,0)),0),
6771            nvl(sum(decode(status,'FAILED',1,0)),0),
6772            nvl(sum(decode(status,'UNASSIGNED',1, 0)),0),
6773            nvl(sum(decode(status,'COMPLETED', 1, 0)),0),
6774            count(*)
6775     INTO   l_unassigned_cnt,
6776            l_failed_cnt,
6777            l_curr_unasgn_cnt,
6778            l_curr_comp_cnt,
6779            l_curr_tot_cnt
6780     FROM   FII_AP_PS_WORK_JOBS;
6781     SELECT COUNT(1)
6782     INTO   l_unassigned_cnt
6783     FROM   FII_AP_PS_WORK_JOBS
6784     WHERE  status = 'UNASSIGNED'
6785     AND    rownum = 1;
6786     SELECT COUNT(1)
6787     INTO   l_failed_cnt
6788     FROM   FII_AP_PS_WORK_JOBS
6789     WHERE  status = 'FAILED'
6790     AND    rownum = 1;
6791     IF l_failed_cnt > 0 THEN
6792        if g_debug_flag = 'Y' then
6793           FII_UTIL.put_line('');
6794           FII_UTIL.put_line('Another worker have errored out.  Stop processing.');
6795        end if;
6796        EXIT;
6797     ELSIF l_unassigned_cnt = 0 THEN
6798           if g_debug_flag = 'Y' then
6799              FII_UTIL.put_line('');
6800              FII_UTIL.put_line('No more jobs left.  Terminating.');
6801           end if;
6802           EXIT;
6803     ELSIF l_unassigned_cnt > 0 THEN
6804           UPDATE FII_AP_PS_WORK_JOBS
6805           SET    status = 'IN PROCESS',
6806                  worker_number = p_worker_no
6807           WHERE  status = 'UNASSIGNED'
6808           AND    rownum < 2;
6809           l_count := sql%rowcount;
6810     END IF;
6811     COMMIT;
6812 
6813     -- -----------------------------------
6814     -- There could be rare situations where
6815     -- between Section 30 and Section 50
6816     -- the unassigned job gets taken by
6817     -- another worker.  So, if unassigned
6818     -- job no longer exist.  Do nothing.
6819       -- -----------------------------------
6820     IF l_count > 0 THEN
6821        BEGIN
6822          SELECT start_range,
6823                 end_range
6824          INTO   l_start_range,
6825                 l_end_range
6826          FROM   FII_AP_PS_WORK_JOBS jobs
6827          WHERE  jobs.worker_number = p_worker_no
6828          AND    jobs.status = 'IN PROCESS';
6829 
6830          ---------------------------------------------------------
6831          --Do summarization using the start_range and end_range
6832          ---------------------------------------------------------
6833          if g_debug_flag = 'Y' then
6834             FII_UTIL.start_timer;
6835          end if;
6836          POPULATE_PAY_SCHED_SUM(l_start_range,  l_end_range);
6837          if g_debug_flag = 'Y' then
6838             FII_UTIL.stop_timer;
6839             FII_UTIL.print_timer('Duration');
6840          end if;
6841          UPDATE FII_AP_PS_WORK_JOBS jobs
6842          SET    jobs.status = 'COMPLETED'
6843          WHERE  jobs.status = 'IN PROCESS'
6844          AND    jobs.worker_number = p_worker_no;
6845          COMMIT;
6846 
6847        EXCEPTION
6848          WHEN OTHERS THEN
6849               g_retcode := -1;
6850 
6851               UPDATE FII_AP_PS_WORK_JOBS
6852               SET  status = 'FAILED'
6853               WHERE  worker_number = p_worker_no
6854               AND   status = 'IN PROCESS';
6855 
6856               COMMIT;
6857               Raise;
6858        END;
6859     END IF; /* IF (l_count > 0) */
6860   END LOOP;
6861 
6862 EXCEPTION
6863   WHEN OTHERS THEN
6864        Retcode:= -1;
6865        Errbuf := '
6866 Error in Procedure: WORKER
6867 Message: '||sqlerrm;
6868        FII_UTIL.put_line(Errbuf);
6869 
6870         -------------------------------------------
6871         -------------------------------------------
6872         -- Update the WORKER_JOBS table to indicate
6873         -- this job has failed
6874         -------------------------------------------
6875         UPDATE FII_AP_PS_WORK_JOBS
6876         SET  status = 'FAILED'
6877         WHERE  worker_number = p_worker_no
6878         AND   status = 'IN PROCESS';
6879 
6880         COMMIT;
6881 
6882 END WORKER;
6883 
6884 
6885 -----------------------------------------------------------
6886 -- Procedure
6887 --   Collect()
6888 -- Purpose
6889 --   This Collect routine Handles all functions involved in the AP summarization
6890 --   and populating FII AP summary tables .
6891 
6892 -----------------------------------------------------------
6893 --  PROCEDURE COLLECT
6894 -----------------------------------------------------------
6895 Procedure Collect(Errbuf          IN OUT NOCOPY VARCHAR2,
6896                   Retcode         IN OUT NOCOPY VARCHAR2,
6897                   p_from_date     IN      VARCHAR2,
6898                   p_to_date       IN      VARCHAR2,
6899                   p_no_worker     IN      NUMBER
6900                   ) IS
6901   l_dir                VARCHAR2(400);
6902   TYPE WorkerList IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
6903   l_worker             WorkerList;
6904 
6905   l_start_date DATE;
6906   l_start_date_temp DATE;
6907 
6908   l_end_date DATE;
6909   l_period_from DATE;
6910   l_period_to DATE;
6911 BEGIN
6912   g_state := 'Inside the procedure COLLECT';
6913   if g_debug_flag = 'Y' then
6914     FII_UTIL.put_line(g_state);
6915   end if;
6916 
6917   Retcode := 0;
6918 
6919   ------------------------------------------------------
6920   -- Set default directory in case if the profile option
6921   -- BIS_DEBUG_LOG_DIRECTORY is not set up
6922   ------------------------------------------------------
6923   l_dir:=FII_UTIL.get_utl_file_dir;
6924   -- l_dir := '/sqlcom/log';
6925   ----------------------------------------------------------------
6926   -- FII_UTIL.initialize will get profile options FII_DEBUG_MODE
6927   -- and BIS_DEBUG_LOG_DIRECTORY and set up the directory where
6928   -- the log files and output files are written to
6929   ----------------------------------------------------------------
6930   FII_UTIL.initialize('FII_AP_INV_SUM_INIT.log','FII_AP_INV_SUM_INIT.out',l_dir, 'FII_AP_INV_SUM_INIT');
6931 
6932   -------------------------------------------------------------
6933   -- Check if FII: DBI Payables Operations Implementation profile
6934   -- is turned on.  If yes, continue, otherwise, error out.  User
6935   -- need to turn on this profile option before running this program
6936   ---------------------------------------------------------------
6937   IF g_oper_imp_prof_flag = 'N' THEN
6938       g_state := 'Checking Implementation profile option';
6939       FII_MESSAGE.write_log(
6940       msg_name    => 'FII_AP_DBI_OPER_IMP',
6941       token_num   => 0);
6942       g_retcode := -2;
6943 		g_errbuf := 'FII: DBI Payables Operations Implementation profile option is not turned on';
6944       RAISE G_IMP_NOT_SET;
6945   END IF;
6946 
6947   EXECUTE IMMEDIATE 'ALTER SESSION ENABLE PARALLEL DML';
6948   EXECUTE IMMEDIATE 'ALTER SESSION SET MAX_DUMP_FILE_SIZE=UNLIMITED';
6949   EXECUTE IMMEDIATE 'ALTER SESSION SET events ''10046 trace name context forever, level 8''';
6950   EXECUTE IMMEDIATE 'ALTER SESSION SET HASH_AREA_SIZE = 100000000';
6951   EXECUTE IMMEDIATE 'ALTER SESSION SET SORT_AREA_SIZE = 100000000';
6952 
6953   g_start_date := trunc(to_date(p_from_date, 'YYYY/MM/DD HH24:MI:SS'));
6954   g_end_date   := trunc(to_date(p_to_date, 'YYYY/MM/DD HH24:MI:SS')) + 1 - ONE_SECOND;
6955   g_no_worker := p_no_worker;
6956 
6957   g_state := 'Calling BIS_COLLECTION_UTILITIES.setup';
6958   IF(NOT BIS_COLLECTION_UTILITIES.setup('FII_AP_INV_SUM_INIT')) THEN
6959         raise_application_error(-20000, errbuf);
6960         return;
6961   END IF;
6962 
6963   IF g_exp_imp_prof_flag = 'Y' THEN
6964   ------------------------------------------------------------
6965   --Get timestamps used to maintain ap_dbi_log.
6966   --g_timestamp1 - current timestamp.
6967   --g_timestamp2 - last Payables Operation/Expenses load.
6968   --g_timestamp3 - last Payables Expenses load.
6969   -------------------------------------------------------------
6970     g_state := 'Defining timestamps to maintain ap_dbi_log.';
6971     IF g_debug_flag = 'Y' then
6972       FII_UTIL.put_line('');
6973       FII_UTIL.put_line(g_state);
6974       fii_util.put_line('');
6975     END IF;
6976 
6977     g_timestamp1 := BIS_COLLECTION_UTILITIES.G_Start_Date;
6978 
6979     BIS_COLLECTION_UTILITIES.get_last_refresh_dates('FII_AP_INV_DISTRIBUTIONS_B_L',
6980                                                     l_start_date, l_end_date,
6981                                                     l_period_from, l_period_to);
6982 
6983     BIS_COLLECTION_UTILITIES.get_last_refresh_dates('FII_AP_INV_DISTRIBUTIONS_B_I',
6984                                                     l_start_date_temp, l_end_date,
6985                                                     l_period_from, l_period_to);
6986 
6987     l_start_date := GREATEST(NVL(l_start_date, BIS_COMMON_PARAMETERS.Get_Global_Start_Date),
6988                              NVL(l_start_date_temp, BIS_COMMON_PARAMETERS.Get_Global_Start_Date));
6989     g_timestamp3 := l_start_date;
6990 
6991     BIS_COLLECTION_UTILITIES.get_last_refresh_dates('FII_AP_INV_SUM_INIT',
6992                                                     l_start_date_temp, l_end_date,
6993                                                     l_period_from, l_period_to);
6994 
6995     l_start_date := GREATEST(l_start_date,
6996                              NVL(l_start_date_temp, BIS_COMMON_PARAMETERS.Get_Global_Start_Date));
6997 
6998     BIS_COLLECTION_UTILITIES.get_last_refresh_dates('FII_AP_INV_SUM_INC',
6999                                                     l_start_date_temp, l_end_date,
7000                                                     l_period_from, l_period_to);
7001 
7002     l_start_date := GREATEST(l_start_date,
7003                              NVL(l_start_date_temp, BIS_COMMON_PARAMETERS.Get_Global_Start_Date));
7004 
7005     g_timestamp2 := l_start_date;
7006 
7007     g_act_part1 := MOD(TO_NUMBER(TO_CHAR(TRUNC(g_timestamp1), 'J')), 32);
7008     g_act_part2 := MOD(TO_NUMBER(TO_CHAR(TRUNC(g_timestamp1+1), 'J')), 32);
7009 
7010   ELSE --Payables Expenses is not implemented, so it is safe to truncate log table.
7011     g_state := 'Truncating AP_DBI_LOG.';
7012     EXECUTE IMMEDIATE('TRUNCATE TABLE ' || g_ap_schema || '.AP_DBI_LOG');
7013   END IF;
7014 
7015   if g_debug_flag = 'Y' then
7016    FII_UTIL.put_line('Current Load Timestamp is: ' || to_char(g_timestamp1, 'YYYY/MM/DD HH24:MI:SS'));
7017    FII_UTIL.put_line('Previous Payables Load Timestamp is: ' || to_char(g_timestamp2, 'YYYY/MM/DD HH24:MI:SS'));
7018    FII_UTIL.put_line('Previous Payables Expenses Load Timestamp is: ' || to_char(g_timestamp3, 'YYYY/MM/DD HH24:MI:SS'));
7019   end if;
7020 
7021   if g_debug_flag = 'Y' then
7022    FII_UTIL.put_line('-------------------------------------------------');
7023    FII_UTIL.put_line('Calling the Init procedure to initialize the global variables');
7024    FII_UTIL.put_line('-------------------------------------------------');
7025   end if;
7026 
7027   INIT;
7028 
7029   g_state := 'Truncating temp tables used to populate base tables';
7030   TRUNCATE_TABLE('FII_AP_PS_RATES_TEMP');
7031   TRUNCATE_TABLE('FII_AP_FUNC_RATES_TEMP');
7032   TRUNCATE_TABLE('FII_AP_PS_WORK_JOBS');
7033   TRUNCATE_TABLE('FII_AP_PAY_SCHED_TEMP');
7034   TRUNCATE_TABLE('FII_AP_WH_TAX_T');
7035   TRUNCATE_TABLE('FII_AP_PREPAY_T');
7036   TRUNCATE_TABLE('FII_AP_PAY_CHK_STG');
7037 
7038   COMMIT;
7039 
7040   if g_debug_flag = 'Y' then
7041    FII_UTIL.put_line('-------------------------------------------------');
7042    FII_UTIL.put_line('Calling the Insert_Rates procedure to insert the missing rate info');
7043    FII_UTIL.put_line('-------------------------------------------------');
7044   end if;
7045 
7046   INSERT_RATES;
7047 
7048   if g_debug_flag = 'Y' then
7049    FII_UTIL.put_line('-------------------------------------------------');
7050    FII_UTIL.put_line('Calling the Verify_Missing_Rates procedure');
7051    FII_UTIL.put_line('-------------------------------------------------');
7052   end if;
7053 
7054 
7055   IF (VERIFY_MISSING_RATES = -1) THEN
7056       g_retcode := -1;
7057       g_errbuf := fnd_message.get_string('FII', 'FII_MISS_EXCH_RATE_FOUND');
7058       RAISE G_MISSING_RATES;
7059 
7060 
7061   -----------------------------------------------------------------------
7062   -- If there are no missing exchange rate records, then we will insert
7063   -- records into the summary tables
7064   -----------------------------------------------------------------------
7065   ELSE
7066 
7067     if g_debug_flag = 'Y' then
7068        FII_UTIL.put_line('-------------------------------------------------');
7069        FII_UTIL.put_line('Calling procedure POPULATE_INV_BASE_SUM');
7070        FII_UTIL.put_line('-------------------------------------------------');
7071     end if;
7072 
7073     POPULATE_INV_BASE_SUM;
7074     g_retcode := 0;
7075 
7076     if g_debug_flag = 'Y' then
7077        FII_UTIL.put_line('-------------------------------------------------');
7078        FII_UTIL.put_line('Calling procedure POPULATE_HOLDS_SUM');
7079        FII_UTIL.put_line('-------------------------------------------------');
7080     end if;
7081 
7082     POPULATE_HOLDS_SUM;
7083     g_retcode := 0;
7084 
7085 
7086     /* Populating the prepayment and withholding amounts into the temp table */
7087     INSERT_WH_PREPAY_AMOUNT;
7088 
7089     /* Populating the payment and discount information into the temp table */
7090     INSERT_PAYMENT_CHECK_INFO;
7091 
7092     --------------------------------------------
7093     -- Register  jobs
7094     --------------------------------------------
7095     if g_debug_flag = 'Y' then
7096        FII_UTIL.put_line(' ');
7097        FII_UTIL.put_line('Populating Jobs Table');
7098        FII_UTIL.put_timestamp;
7099     end if;
7100 
7101     REGISTER_JOBS;
7102     COMMIT;
7103 
7104     g_state := 'Deleting records from FII_AP_PAY_SCHED_B that are already existing';
7105     /* For Initial Load we will truncate the data in the pay sched summary table
7106        and re-populate this table */
7107     TRUNCATE_TABLE('MLOG$_FII_AP_PAY_SCHED_B');
7108     TRUNCATE_TABLE('FII_AP_PAY_SCHED_B');
7109 
7110     FND_PROFILE.PUT('CONC_SINGLE_THREAD','N');
7111 
7112     --------------------------------------------------------
7113     -- Launch worker
7114     --------------------------------------------------------
7115     g_state := 'Launching Workers';
7116     if g_debug_flag = 'Y' then
7117        FII_UTIL.put_line('Launching Workers');
7118     end if;
7119 
7120    /* p_no_worker is the parameter user submitted to specify how many
7121       workers they want to submit */
7122 
7123     FOR i IN 1..p_no_worker
7124     LOOP
7125       l_worker(i) := LAUNCH_WORKER(i);
7126       if g_debug_flag = 'Y' then
7127          FII_UTIL.put_line('Worker '||i||' request id: '||l_worker(i));
7128       end if;
7129     END LOOP;
7130 
7131     COMMIT;
7132 
7133     MONITOR_WORKER;
7134 
7135     -------------------------------------------------------------------
7136     -- Analyze Payment schedule table after all workers have completed
7137     -- updating the AP_PAYMENT_SCHEDULES_ALL table
7138     -------------------------------------------------------------------
7139     FND_STATS.GATHER_TABLE_STATS(g_ap_schema,'AP_PAYMENT_SCHEDULES_ALL');
7140     EXECUTE IMMEDIATE 'ALTER SESSION ENABLE PARALLEL DML';
7141 
7142     FND_PROFILE.PUT('CONC_SINGLE_THREAD','Y');
7143 
7144     if g_debug_flag = 'Y' then
7145        FII_UTIL.put_line('-------------------------------------------------');
7146        FII_UTIL.put_line('Calling procedure POPULATE_PS_BUCKET_COUNT');
7147        FII_UTIL.put_line('-------------------------------------------------');
7148     end if;
7149 
7150     POPULATE_PS_BUCKET_COUNT;
7151     g_retcode := 0;
7152 
7153 
7154     if g_debug_flag = 'Y' then
7155        FII_UTIL.put_line('-------------------------------------------------');
7156        FII_UTIL.put_line('Calling procedure POPULATE_HOLD_HISTORY');
7157        FII_UTIL.put_line('-------------------------------------------------');
7158     end if;
7159 
7160     POPULATE_HOLD_HISTORY;
7161     g_retcode := 0;
7162 
7163 
7164     IF g_exp_imp_prof_flag = 'Y' THEN
7165       FOR i IN 0..31 LOOP --i represents the partition of ap_dbi_log.
7166 
7167         IF g_timestamp3 + 30 >= g_timestamp1 THEN --Copy records into Expense log table.
7168           INSERT INTO FII_AP_DBI_LOG_EXP_T(
7169                  Table_Name,
7170                  Operation_Flag,
7171                  Key_Value1_ID,
7172                  Key_Value2_ID,
7173                  Created_By,
7174                  Last_Updated_By,
7175                  Last_Update_Login,
7176                  Creation_Date,
7177                  Last_Update_Date)
7178           SELECT Table_Name,
7179                  Operation_Flag,
7180                  Key_Value1,
7181                  Key_Value2,
7182                  Created_By,
7183                  Last_Updated_By,
7184                  Last_Update_Login,
7185                  Creation_Date,
7186                  Last_Update_Date
7187           FROM AP_DBI_LOG
7188           WHERE Partition_ID = i
7189           AND Creation_Date >= g_timestamp2
7190           AND Creation_Date < g_timestamp1;
7191         END IF;
7192 
7193         IF NOT (i = g_act_part1 OR i = g_act_part2) THEN --This is a non-active partition.
7194           EXECUTE IMMEDIATE 'ALTER TABLE ' || g_ap_schema || '.AP_DBI_LOG TRUNCATE PARTITION P' || to_char(i);
7195         END IF;
7196 
7197       END LOOP;
7198 
7199     END IF;
7200 
7201     TRUNCATE_TABLE('FII_AP_DBI_LOG_PS_T');
7202 
7203 
7204     g_state := 'Truncating temp tables after used to populate base tables';
7205     TRUNCATE_TABLE('FII_AP_PAY_SCHED_TEMP');
7206     TRUNCATE_TABLE('FII_AP_WH_TAX_T');
7207     TRUNCATE_TABLE('FII_AP_PREPAY_T');
7208     TRUNCATE_TABLE('FII_AP_PS_WORK_JOBS');
7209     TRUNCATE_TABLE('FII_AP_PAY_CHK_STG');
7210 
7211   END IF;
7212 
7213 
7214   COMMIT;
7215 
7216   if g_debug_flag = 'Y' then
7217      FII_UTIL.put_line('return code is ' || retcode);
7218   end if;
7219   Retcode := g_retcode;
7220 
7221   g_state := 'Calling BIS_COLLECTION_UTILITIES.wrapup';
7222   BIS_COLLECTION_UTILITIES.wrapup(
7223       p_status => TRUE,
7224       p_period_from => g_start_date,
7225       p_period_to => g_end_date);
7226 
7227   BIS_COLLECTION_UTILITIES.deleteLogForObject('FII_AP_INV_SUM_INC');
7228 
7229 
7230 EXCEPTION
7231   WHEN OTHERS THEN
7232     g_retcode:= -1;
7233     retcode:=g_retcode;
7234     g_exception_msg  := g_retcode || ':' || g_errbuf;
7235     FII_UTIL.put_line('Error occured while ' || g_state);
7236     FII_UTIL.put_line(g_exception_msg);
7237 
7238 END;
7239 
7240 END FII_AP_INV_SUM_INIT;
7241