DBA Data[Home] [Help]

PACKAGE BODY: APPS.OKL_REPORT_GENERATOR_PVT

Source


1 PACKAGE BODY OKL_REPORT_GENERATOR_PVT AS
2 /* $Header: OKLRRPTB.pls 120.35.12010000.12 2009/02/09 09:33:45 nikshah ship $ */
3 
4   -- Start of comments
5   --
6   -- Function Name   : populate_code_combinations
7   -- Description    : Populates the okl_code_cmbns_gt table
8   --
9   -- Business Rules  : Called from prepare_gross_rec_report before any other
10   --report call /*important */
11   -- Parameters       :
12   -- Version      : 1.0
13   -- History        : Ravindranath Gooty created.
14   --
15   -- End of comments
16   -- Package level variables
17   G_MODULE                  CONSTANT  VARCHAR2(255):= 'LEASE.ACCOUNTING.RECONCILIATION.OKL_REPORT_GENERATOR_PVT';
18   G_DEBUG_ENABLED           CONSTANT  VARCHAR2(10) := OKL_DEBUG_PUB.CHECK_LOG_ENABLED;
19   G_IS_DEBUG_STATEMENT_ON             BOOLEAN;
20 
21     CURSOR get_contracts_csr(
22               p_org_id          NUMBER
23              ,p_legal_entity_id NUMBER
24              ,p_ledger_id       NUMBER
25              ,p_start_date      DATE
26              ,p_end_date        DATE)
27     IS
28       -- Starting from OKL Transactions find all the Booking Transactions
29       --    in the input Ledger, Org Id, Le Id whose Transaction Date < p_end_date
30       -- Fetch all those khr_id
31       --  Using this khr_id confirm that there exists no Termination Transaction
32       --   on or before the Report Start Date
33       SELECT   chr.currency_code              currency_code
34               ,ou.name                        organization_name
35               ,chr.contract_number            contract_number
36               ,a.contract_id                  contract_id
37               ,ou.organization_id             org_id
38               ,a.bkg_transaction_date         bkg_transaction_date
39               ,KHR.PDT_ID                     PDT_ID
40       FROM
41       (
42         SELECT bkg_trx.khr_id                 contract_id
43               ,min(bkg_trx.transaction_date)  bkg_transaction_date
44          FROM  okl_trx_contracts_all          bkg_trx
45               ,okl_k_headers                  khr
46               ,okl_trx_types_b                try
47               ,okl_rep_products_gt            pdt_gt
48         WHERE  bkg_trx.tcn_type = 'BKG'
49           AND  bkg_trx.try_id =  try.id
50           AND  try.trx_type_class = 'BOOKING'
51           -- Booking Transaction should be related to the Input Ledger
52           AND  bkg_trx.set_of_books_id = p_ledger_id
53           -- Booking Transaction should belong the Operating Unit inputted
54           AND  bkg_trx.org_id = nvl( p_org_id, bkg_trx.org_id )
55           -- Booking Transaction should belong the Legal Entity inputted
56           AND  bkg_trx.legal_entity_id = nvl( p_legal_entity_id, bkg_trx.legal_entity_id )
57           AND  bkg_trx.khr_id = khr.id
58           -- Contracts product in context should be one of the Report Products
59           AND  khr.pdt_id = pdt_gt.product_id
60         GROUP BY  bkg_trx.khr_id
61         -- Booking Transaction Date should be less than the Report To Date
62         HAVING  MIN(bkg_trx.transaction_date ) <  p_end_date
63       ) a
64       ,okc_k_headers_all_b     CHR
65       ,okl_k_headers           KHR
66       ,hr_operating_units      ou
67     WHERE
68            a.contract_id = CHR.id
69       AND  KHR.ID = CHR.ID
70       AND  ou.organization_id = chr.authoring_org_id
71            -- Pick only those Contracts [iff in Expired/Terminated Status]
72            --  which got expired by a Termination Transaction
73            --  after the Report Start Date
74            -- Contract should not have occurbe before the Report Start Date
75       AND  p_start_date <=
76               nvl( ( SELECT  max(transaction_date) last_trm_trx_date
77                         FROM  okl_trx_contracts_all term_trx
78                        WHERE  term_trx.khr_id = a.contract_id
79                          AND  term_trx.tcn_type ='TMT'
80                          AND  trn_code = 'EXP'
81                     ),
82                     p_start_date + 1
83                  )
84     ; -- End of Cursor: get_contracts_csr
85 
86 
87   PROCEDURE write_to_log(
88               p_level                 IN VARCHAR2,
89               p_module                IN fnd_log_messages.module%TYPE,
90               msg                     IN VARCHAR2 )
91   AS
92     -- l_level: S - Statement, P- Procedure, B - Both
93   BEGIN
94     okl_debug_pub.log_debug(
95       p_level,
96       p_module,
97       msg);
98   END;
99 
100   PROCEDURE put_in_log(
101               p_debug_enabled         IN VARCHAR2,
102               is_debug_procedure_on   IN BOOLEAN,
103               is_debug_statement_on   IN BOOLEAN,
104               p_module                IN fnd_log_messages.module%TYPE,
105               p_level                 IN VARCHAR2,
106               msg                     IN VARCHAR2 )
107   AS
108     -- l_level: S - Statement, P- Procedure, B - Both
109   BEGIN
110     IF(p_debug_enabled='Y' AND is_debug_procedure_on AND p_level = 'P')
111     THEN
112         write_to_log(
113           p_level   => FND_LOG.LEVEL_PROCEDURE,
114           p_module  => p_module,
115           msg       => msg);
116     ELSIF (p_debug_enabled='Y' AND is_debug_statement_on AND
117           (p_level = 'S' OR p_level = 'B' ))
118     THEN
119         write_to_log(
120           p_level   => FND_LOG.LEVEL_STATEMENT,
121           p_module  => p_module,
122           msg       => msg);
123     END IF;
124     -- Log Each and Every Statement
125     FND_FILE.PUT_LINE(FND_FILE.LOG, MSG );
126   END put_in_log;
127 
128   -- Start of comments
129   --
130   -- Function Name   : populate_code_combinations
131   -- Description    : Populates the okl_code_cmbns_gt table
132   --
133   -- Business Rules  : Called from prepare_gross_rec_report before any other
134   --report call /*important */
135   -- Parameters       :
136   -- Version      : 1.0
137   -- History        : Ravindranath Gooty created.
138   --
139   -- End of comments
140 
141   PROCEDURE populate_code_combinations(
142               p_api_version   IN         NUMBER
143              ,p_init_msg_list IN         VARCHAR2
144              ,x_return_status OUT NOCOPY VARCHAR2
145              ,x_msg_count     OUT NOCOPY NUMBER
146              ,x_msg_data      OUT NOCOPY VARCHAR2
147              ,p_report_id     IN         NUMBER
148              ,p_ledger_id     IN         NUMBER)
149   IS
150     l_segment   VARCHAR2(50);
151     l_report_id NUMBER := p_report_id;
152 
153     -- Cursor to identifiy what is the column Name for the Natural Account Segment
154     CURSOR c_get_segment_num(p_ledger_id NUMBER)
155     IS
156       SELECT s.application_column_name    segment_col_name
157       FROM   fnd_id_flex_segments         s,
158              fnd_segment_attribute_values sav,
159              gl_ledgers_public_v          glp --,
160       WHERE  s.application_id = 101 -- GL Application ID
161       AND    s.id_flex_code = 'GL#'
162       AND    s.id_flex_num = glp.chart_of_accounts_id --COA ID
163       AND    s.enabled_flag = 'Y'
164       AND    s.application_column_name = sav.application_column_name
165       AND    sav.application_id = 101
166       AND    sav.id_flex_code = 'GL#'
167       AND    sav.id_flex_num = glp.chart_of_accounts_id
168       AND    sav.attribute_value = 'Y'
169       AND    sav.segment_attribute_type = 'GL_ACCOUNT'
170       AND    glp.ledger_id = p_ledger_id;
171     -----------------------------------------------------------------
172     -- Declare Process Variable
173     -----------------------------------------------------------------
174     l_api_version      CONSTANT NUMBER         := 1;
175     l_api_name         CONSTANT VARCHAR2(30)   := 'populate_code_combinations';
176     l_return_status    VARCHAR2(1)             := OKL_API.G_RET_STS_SUCCESS;
177     -- Debug related parameters
178     l_module CONSTANT fnd_log_messages.module%TYPE := G_MODULE || l_api_name;
179     l_debug_enabled       VARCHAR2(10);
180     is_debug_procedure_on BOOLEAN;
181     is_debug_statement_on BOOLEAN;
182     -- Local Variables
183     l_query_string        VARCHAR2(4000);
184   BEGIN
185     l_debug_enabled := okl_debug_pub.check_log_enabled;
186     -- check for logging on PROCEDURE level
187     is_debug_procedure_on := okl_debug_pub.check_log_on(l_module,FND_LOG.LEVEL_PROCEDURE);
188     -- check for logging on STATEMENT level
189     is_debug_statement_on := okl_debug_pub.check_log_on(l_module,FND_LOG.LEVEL_STATEMENT);
190     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'P',
191                'begin debug OKLRRPTB.pls call ' || l_api_name);
192     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'P',
193                'BEGIN API OKL_REPORT_GENERATOR_PVT.' || l_api_name );
194     l_return_status := okl_api.g_ret_sts_success;
195     -- The Report Definition stores the Range of Natural Account Segments.
196     -- Using those prepare a list of Code Combinations and populate it into the
197     -- OKL Account Code Combinations GT Table
198     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
199       'Execute the Cursor c_get_segment_num' || p_ledger_id );
200     -- Given a Ledger find out the Segment Column Number which stores the Natural Account Segment
201     OPEN c_get_segment_num(p_ledger_id => p_ledger_id);
202     FETCH c_get_segment_num
203       INTO l_segment;
204     CLOSE c_get_segment_num;
205     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
206       'After Execute the Cursor c_get_segment_num' || p_ledger_id );
207     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
208       'Segment Column Name=' || l_segment );
209     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
210       'Ledger Id=' || p_ledger_id );
211     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
212       'Report Id=' || p_report_id );
213 
214     -- Prepare the Query String
215     l_query_string :=
216          'INSERT INTO okl_code_cmbns_gt ('
217        ||' ccid'
218        ||' ,account_number'
219        ||')'
220        ||' SELECT  cc.code_combination_id'
221        ||' ,' || l_segment || ' ' -- segment3
222        ||' FROM  gl_code_combinations cc'
223        ||' ,gl_ledgers_public_v gl'
224        ||' WHERE  cc.chart_of_accounts_id = gl.chart_of_accounts_id'
225        ||'  AND cc.enabled_flag =  ' || '''' || 'Y' || ''''
226        ||'  AND gl.ledger_id =  ' || p_ledger_id
227        ||'  AND EXISTS'
228        ||' ('
229        ||'  SELECT  sg_frm_fvl.flex_value segment_range_from'
230        ||'  ,sg_to_fvl.flex_value  segment_range_to'
231        ||'  FROM  fnd_flex_values_vl     sg_frm_fvl'
232        ||'  ,fnd_flex_values_vl     sg_to_fvl'
233        ||'  ,okl_report_acc_params  acc_params'
234        ||'  WHERE  sg_frm_fvl.flex_value_id = acc_params.segment_range_from'
235        ||'   AND  sg_to_fvl.flex_value_id  = acc_params.segment_range_to'
236        ||'   AND  acc_params.report_id = ' || p_report_id || ' '
237        ||'   AND  cc.' || l_segment || ' >= sg_frm_fvl.flex_value'
238        ||'   AND  cc.' || l_segment || ' <= sg_to_fvl.flex_value'
239        ||')';
240     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
241       'Query String=' );
242     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
243       l_query_string );
244     -- Dynamically Execute the
245     EXECUTE IMMEDIATE l_query_string;
246     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
247       'Executed the Dynamically Binded Query' );
248     -- Set the Return Status and return back
249     x_return_status := l_return_status;
250   EXCEPTION
251     WHEN OTHERS THEN
252       IF c_get_segment_num%ISOPEN
253       THEN
254         CLOSE c_get_segment_num;
255       END IF;
256       x_return_status := okl_api.g_ret_sts_unexp_error;
257       okl_api.set_message(p_app_name     => g_app_name,
258                           p_msg_name     => g_unexpected_error,
259                           p_token1       => g_sqlcode_token,
260                           p_token1_value => SQLCODE,
261                           p_token2       => g_sqlerrm_token,
262                           p_token2_value => SQLERRM);
263   END populate_code_combinations;
264 
265   -- Start of comments
266   --
267   -- Function Name   : populate_code_combinations
268   -- Description    : Populates the okl_code_cmbns_gt table
269   --
270   -- Business Rules  : Called from prepare_gross_rec_report before any other
271   --report call /*important */
272   -- Parameters       :
273   -- Version      : 1.0
274   -- History        : Ravindranath Gooty created.
275   --
276   -- End of comments
277   PROCEDURE populate_products(
278               p_api_version   IN         NUMBER
279              ,p_init_msg_list IN         VARCHAR2
280              ,x_return_status OUT NOCOPY VARCHAR2
281              ,x_msg_count     OUT NOCOPY NUMBER
282              ,x_msg_data      OUT NOCOPY VARCHAR2
283              ,p_report_id     IN         NUMBER
284              ,p_org_id        IN         NUMBER )
285   IS
286     -----------------------------------------------------------------
287     -- Declare Process Variable
288     -----------------------------------------------------------------
289     CURSOR is_pdt_or_bc_present_csr( p_report_id NUMBER )
290     IS
291       SELECT  DISTINCT 'Y'  flag
292         FROM  okl_report_parameters params
293        WHERE  params.report_id = p_report_id
294          AND  params.parameter_type_code IN
295               ( 'BOOK_CLASSIFICATION', 'PRODUCT' );
296 
297     -- Cursor to fetch all the products present in the System
298     CURSOR get_all_pdts_from_sys_csr( p_report_id NUMBER )
299     IS
300       SELECT  pdt.NAME       product_name
301              ,pdt.id         product_id
302         FROM  okl_products   pdt;
303 
304 
305     -- The following Cursor will fetch the product if
306     --  a. The Products book classification is mentioned in the Report Definition
307     --  b. The Product is directly mentioned in the Report Definition
308     CURSOR get_pdts_from_report_csr(
309             p_report_id       NUMBER)
310     IS
311       SELECT  pdt.NAME       product_name
312              ,pdt.id         product_id
313         FROM  okl_products              pdt
314              ,okl_ae_tmpt_sets_all      aes
315              ,okl_st_gen_tmpt_sets_all  gts
316        WHERE  pdt.aes_id = aes.id
317          AND  aes.gts_id = gts.id
318          AND  gts.deal_type
319               IN
320                (
321                   SELECT  params.param_char_value1
322                     FROM  okl_report_parameters params
323                    WHERE  params.report_id = p_report_id
324                      AND  params.parameter_type_code = 'BOOK_CLASSIFICATION'
325                )
326    UNION
327      -- Append list of products from the report too
328       SELECT  pdt.NAME       product_name
329              ,pdt.id         product_id
330         FROM  okl_report_parameters  params
331              ,okl_products           pdt
332        WHERE  params.report_id = p_report_id
333          AND  params.parameter_type_code = 'PRODUCT'
334          AND  params.param_num_value1 = pdt.id;
335 
336     TYPE rep_pdts_tbl_type IS TABLE OF okl_rep_products_gt%ROWTYPE
337       INDEX BY BINARY_INTEGER;
338     l_report_pdts_tbl    rep_pdts_tbl_type;
339 
340     l_api_version      CONSTANT NUMBER         := 1;
341     l_api_name         CONSTANT VARCHAR2(30)   := 'populate_products';
342     l_return_status    VARCHAR2(1)             := OKL_API.G_RET_STS_SUCCESS;
343     -- Debug related parameters
344     l_module CONSTANT fnd_log_messages.module%TYPE := G_MODULE || l_api_name;
345     l_debug_enabled       VARCHAR2(10);
346     is_debug_procedure_on BOOLEAN;
347     is_debug_statement_on BOOLEAN;
348     -- Local Variables
349     l_report_id            NUMBER := p_report_id;
350     l_bc_or_pdts_mentioned VARCHAR2(1);
351     l_pdt_count            NUMBER;
352   BEGIN
353     l_debug_enabled := okl_debug_pub.check_log_enabled;
354     -- check for logging on PROCEDURE level
355     is_debug_procedure_on := okl_debug_pub.check_log_on(l_module,FND_LOG.LEVEL_PROCEDURE);
356     -- check for logging on STATEMENT level
357     is_debug_statement_on := okl_debug_pub.check_log_on(l_module,FND_LOG.LEVEL_STATEMENT);
358     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'P',
359                'begin debug OKLRRPTB.pls call ' || l_api_name);
360     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'P',
361                'BEGIN API OKL_REPORT_GENERATOR_PVT.' || l_api_name );
362     l_return_status := okl_api.g_ret_sts_success;
363 
364     -- Logic:
365     --  Check whether atleast one product or Book Classification is mentioned on the
366     --   Report Definition or not
367     --  If Mentioned:
368     --    Fetch only those products from the System which are either mentioned
369     --     directly on the report definition or
370     --    The products Book Classification is mentioned in the Report Definition
371     --  If Not mentioned:
372     --    Fetch all products from the System
373 
374     l_bc_or_pdts_mentioned := 'N';
375     FOR t_rec IN is_pdt_or_bc_present_csr( p_report_id => p_report_id )
376     LOOP
377       l_bc_or_pdts_mentioned := t_rec.flag;
378       put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
379         'Case when Book Classification or Product is Mentioned' );
380     END LOOP;
381 
382     -- Initialize the l_pdt_count
383     l_pdt_count := 1;
384     IF l_bc_or_pdts_mentioned = 'Y'
385     THEN
386       put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
387         '!!! Fetching All products from System based on BC !!! ' );
388       FOR t_rec IN get_pdts_from_report_csr(p_report_id  => p_report_id )
389       LOOP
390         l_report_pdts_tbl(l_pdt_count).product_name := t_rec.product_name;
391         l_report_pdts_tbl(l_pdt_count).product_id   := t_rec.product_id;
392         put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'P',
393           'Product Name ' || t_rec.product_name ||
394           'Product Id ' || t_rec.product_id );
395         -- Increment the  l_pdt_count
396         l_pdt_count := l_pdt_count +1;
397       END LOOP;
398     ELSE
399       put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
400         '!!! Fetching All products from System Without any restriction !!! ' );
401       FOR t_rec IN get_all_pdts_from_sys_csr(p_report_id  => p_report_id )
402       LOOP
403         l_report_pdts_tbl(l_pdt_count).product_name := t_rec.product_name;
404         l_report_pdts_tbl(l_pdt_count).product_id   := t_rec.product_id;
405         put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'P',
406           'Product Name ' || t_rec.product_name ||
407           'Product Id ' || t_rec.product_id );
408         -- Increment the  l_pdt_count
409         l_pdt_count := l_pdt_count +1;
410       END LOOP;
411     END IF;
412 
413     -- Finally insert the list of Products figured out
414     FORALL i IN l_report_pdts_tbl.FIRST .. l_report_pdts_tbl.LAST
415       INSERT INTO okl_rep_products_gt VALUES l_report_pdts_tbl(i);
416 
417     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
418       'End: populate_products' );
419     -- Set the Return Status and return back
420     x_return_status := l_return_status;
421   EXCEPTION
422     WHEN OTHERS THEN
423       x_return_status := okl_api.g_ret_sts_unexp_error;
424       okl_api.set_message(p_app_name     => g_app_name,
425                           p_msg_name     => g_unexpected_error,
426                           p_token1       => g_sqlcode_token,
427                           p_token1_value => SQLCODE,
428                           p_token2       => g_sqlerrm_token,
429                           p_token2_value => SQLERRM);
430   END populate_products;
431 
432   -- Start of comments
433   --
434   -- Function Name   : populate_code_combinations
435   -- Description    : Populates the okl_code_cmbns_gt table
436   --
437   -- Business Rules  : Called from prepare_gross_rec_report before any other
438   --report call /*important */
439   -- Parameters       :
440   -- Version      : 1.0
441   -- History        : Ravindranath Gooty created.
442   --
443   -- End of comments
444 
445   PROCEDURE populate_trx_data(
446               p_api_version   IN         NUMBER
447              ,p_init_msg_list IN         VARCHAR2
448              ,x_return_status OUT NOCOPY VARCHAR2
449              ,x_msg_count     OUT NOCOPY NUMBER
450              ,x_msg_data      OUT NOCOPY VARCHAR2
451              ,p_report_id     IN         NUMBER
452              ,p_ledger_id     IN         NUMBER
453              ,p_start_date    IN         DATE
454              ,p_end_date      IN         DATE
455              ,p_org_id        IN         NUMBER
456              ,p_le_id         IN         NUMBER)
457   IS
458     l_report_id NUMBER := p_report_id;
459     -----------------------------------------------------------------
460     -- Declare Process Variable
461     -----------------------------------------------------------------
462     l_api_version      CONSTANT NUMBER         := 1;
463     l_api_name         CONSTANT VARCHAR2(30)   := 'populate_trx_data';
464     l_return_status    VARCHAR2(1)             := OKL_API.G_RET_STS_SUCCESS;
465     -- Debug related parameters
466     l_module CONSTANT fnd_log_messages.module%TYPE := G_MODULE || l_api_name;
467     l_debug_enabled       VARCHAR2(10);
468     is_debug_procedure_on BOOLEAN;
469     is_debug_statement_on BOOLEAN;
470     -- Local Variables
471     l_query_string        VARCHAR2(4000);
472     l_trace_time          DATE;
473   BEGIN
474     l_debug_enabled := okl_debug_pub.check_log_enabled;
475     -- check for logging on PROCEDURE level
476     is_debug_procedure_on := okl_debug_pub.check_log_on(l_module,FND_LOG.LEVEL_PROCEDURE);
477     -- check for logging on STATEMENT level
478     is_debug_statement_on := okl_debug_pub.check_log_on(l_module,FND_LOG.LEVEL_STATEMENT);
479     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'P',
480                'begin debug OKLRRPTB.pls call ' || l_api_name);
481     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
482       'Begin: ' || l_api_name ||'(+)' );
483     l_return_status := okl_api.g_ret_sts_success;
484     -- By now we have the List of Eligible Products and Code Combinations Available
485     -- in the corresponding _GT tables
486     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'P',
487       'Input Parameters ' ||
488       ' p_report_id=' || p_report_id ||
489       ' p_ledger_id=' || p_ledger_id ||
490       ' p_start_date=' || p_start_date ||
491       ' p_end_date=' || p_end_date ||
492       ' p_org_id=' || p_org_id ||
493       ' p_le_id=' ||  p_le_id );
494 
495     -- Start the stop watch
496     l_trace_time := SYSDATE;
497     -------------------------------------------------------------------------------------
498     -- Queries to Populate the Lease Accounting Transactions
499     -------------------------------------------------------------------------------------
500     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
501       'Before Inserting the Lease Accounting Transactions GT Table Start Time:'
502       || TO_CHAR(l_trace_time, 'DD-MON-YYY HH:MM:SS') );
503     INSERT INTO okl_g_reports_gt
504                 (
505                   -- String Formatted Columns
506                    value1_text
507                   ,value2_text
508                   ,value3_text
509                   ,value4_text
510                   ,value5_text
511                   ,value6_text
512                   ,value7_text
513                   ,value8_text
514                   ,value9_text
515                   ,value10_text
516                   ,value11_text
517                   ,value12_text
518                   ,value13_text
519                   ,value14_text
520                   ,value15_text
521                   ,value16_text
522                   ,value17_text
523                   ,value18_text
524                   -- Number Formatted Columns
525                   ,value1_num
526                   ,value2_num
527                   ,value3_num
528                   ,value4_num
529                   ,value5_num
530                   ,value6_num
531                   ,value7_num
532                   ,value8_num
533                   ,value9_num
534                   ,value10_num
535                   ,value11_num
536                   ,value12_num
537                   ,value13_num
538                   -- Date Formatted Columns
539                   ,value1_date
540                   ,value2_date
541                   ,value4_date
542                 )
543       SELECT -- String Formatted Columns
544              trx_detail_type_code        --value1_text
545             ,trx_number                  --value2_text
546             ,trx_type_name               --value3_text
547             ,trx_application_name        --value4_text
548             ,contract_number             --value5_text
549             ,asset_number                --value6_text
550             ,book_classification         --value7_text
551             ,product_name                --value8_text
552             ,trx_sty_name                --value9_text
553             ,trx_sty_purpose             --value10_text
554             ,currency_code               --value11_text
555             ,trx_period_name             --value12_text -- Populated Only for Posted Entries
556             ,trx_account_name            --value13_text
557             ,trx_operating_unit_name     --value14_text
558             ,trx_legal_entity_name       --value15_text
559             ,trx_ledger_name             --value16_text
560             ,trx_activity_code           --value17_text
561             ,trx_event_name              --value18_text
562             -- Number Formatted Columns
563             ,trx_dr_amount               --value1_num
564             ,trx_cr_amount               --value2_num
565             ,trx_net_amount              --value3_num
566             ,trx_acc_dr_ccid             --value4_num
567             ,trx_acc_cr_ccid             --value5_num
568             ,trx_ledger_id               --value6_num
569             ,trx_operating_unit_id       --value7_num
570             ,trx_legal_entity_id         --value8_num
571             ,trx_khr_id                  --value9_num
572             ,txl_asset_id                --value10_num
573             ,trx_pdt_id                  --value11_num
574             ,trx_sty_id                  --value12_num
575             ,trx_application_id          --value13_num
576             -- Date Formatted Columns
577             ,trx_date                    --value1_date
578             ,gl_date                     --value2_date
579             ,accrual_date                --value4_date
580         FROM
581        (
582          -- Query Segment to fetch the UnAccounted Transactions with No Accounting Events
583          -- Bug 6835659: Modified the Report Extraction to fetch the Transactions
584          --  of Non-Accounting Transaction Types
585          SELECT 'TRX_UNACCOUNTED_NO_EVENT'                trx_detail_type_code
586                 ,trx.trx_number                           trx_number
587                 ,try.name                                 trx_type_name
588                 ,NULL                                     trx_event_name
589                 ,'Leasing and Finance Management'                       trx_application_name
590                 ,chr.contract_number                      contract_number
591                 ,NULL                                     asset_number
592                 ,gts.deal_type                            book_classification -- Fetching Code need to change to fetch Meaning
593                 ,pdt.NAME                                 product_name
594                 ,sty.NAME                                 trx_sty_name
595                 ,sty.styb_purpose_meaning                 trx_sty_purpose
596                 ,trx.currency_code                        currency_code
597                 ,NULL                                     trx_account_name
598                 ,ou.name                                  trx_operating_unit_name
599                 ,le.name                                  trx_legal_entity_name
600                 ,ledger.name                              trx_ledger_name
601                 ,rtryp.add_substract_code                 trx_activity_code
602                 ,NULL                                     trx_period_name
603                 -- Number Format Columns
604                 ,DECODE(nvl(dist.cr_dr_flag, 'D')
605                           ,'D', dist.amount, NULL)        trx_dr_amount
606                 ,DECODE(nvl(dist.cr_dr_flag, 'D')
607                           ,'C', dist.amount, NULL)        trx_cr_amount
608                 ,DECODE(rtryp.add_substract_code
609                          ,'ADD',       txl.amount
610                          ,'SUBTRACT', txl.amount * -1
611                        )                                  trx_net_amount
612                 -- Note that in ATS Mode only distributions can fetch you the CCID
613                 -- In AMB mode, the Un-Accounted Distributions may not have the Code Combinations ID
614                 ,DECODE(nvl(dist.cr_dr_flag, 'D')
615                         ,'D', dist.code_combination_id
616                         , NULL)                           trx_acc_dr_ccid
617                 ,DECODE(nvl(dist.cr_dr_flag, 'D')
618                         ,'C', dist.code_combination_id
619                         , NULL)                           trx_acc_cr_ccid
620                 ,trx.set_of_books_id                      trx_ledger_id
621                 ,trx.org_id                               trx_operating_unit_id
622                 ,trx.legal_entity_id                      trx_legal_entity_id
623                 ,trx.khr_id                               trx_khr_id
624                 ,txl.kle_id                               txl_asset_id
625                 ,khr.pdt_id                               trx_pdt_id
626                 ,txl.sty_id                               trx_sty_id
627                 ,540                                      trx_application_id
628                 -- DATE Format Columns
629                 ,trx.transaction_date                     trx_date
630                 ,dist.gl_date                             gl_date
631                 -- Additional Columns
632                 ,trx.id                                   trx_id
633                 ,txl.id                                   trx_txl_id
634                 ,trx.try_id                               trx_try_id
635                 ,rep.report_id                            trx_report_id
636                 ,dist.id                                  trx_dist_id
637                 -- Occurance date of the Transaction, not the Transaction Effective Date
638                 ,trx.transaction_date                     trx_occ_date
639                 ,trx.date_accrual                         accrual_date
640                 ,'Lease'                                  source
641           FROM   okl_trx_contracts_all     trx
642                 ,okl_txl_cntrct_lns_all    txl
643                 ,okl_trns_acc_dstrs_all    dist
644                 ,okl_trx_types_v           try
645                 ,okl_strm_type_v           sty
646                 ,okc_k_headers_all_b       chr
647                 ,okl_k_headers             khr
648                 ,okl_products              pdt
649                 ,okl_ae_tmpt_sets_all      aes
650                 ,okl_st_gen_tmpt_sets_all  gts
651                 ,okl_reports_b             rep
652                 ,okl_report_trx_params     rtryp
653                 ,okl_rep_products_gt       pdt_gt
654                 -- To fetch Names
655                 ,hr_operating_units        ou
656                 ,xle_entity_profiles       le
657                 ,gl_ledgers_v              ledger
658           WHERE  trx.id = txl.tcn_id
659             AND  trx.try_id = try.id
660             AND  try.accounting_event_class_code IS NOT NULL
661             AND  txl.sty_id   = sty.id -- May be we dont need outer join here
662             AND  dist.source_id = txl.id
663             AND  dist.source_table = 'OKL_TXL_CNTRCT_LNS'
664             -- Restrict to only one Distribution Line
665             -- In ATS Mode restrict the distribution to Debit Only
666             -- In AMB Mode only one dist. will be created in OKL, hence consider that as Debit
667             AND  nvl(dist.cr_dr_flag, 'D') = 'D'
668             AND  trx.khr_id = chr.id
669             AND  chr.id = khr.id
670             AND  khr.pdt_id = pdt.id
671             AND  pdt.aes_id = aes.id
672             AND  aes.gts_id = gts.id
673             -- Pick the Distribution which doesnot have the Accounting Event Stamped on It
674             AND  dist.accounting_event_id IS NULL
675             -- Transaction should have occured in the Start and End date of the Context
676             AND  trx.transaction_date >= p_start_date
677             AND  trx.transaction_date <= p_end_date
678             -- Add Predicates based on the report
679             AND  rep.report_id = p_report_id
680             AND  rtryp.report_id = rep.report_id
681             AND  try.id = rtryp.try_id
682             AND  nvl(nvl(rtryp.sty_id,txl.sty_id), - 1 )  = nvl(txl.sty_id, -1)
683             -- Products restriction
684             AND  pdt_gt.product_id = pdt.id
685             -- Org., Ledger and Legal Entity Id restriction
686             AND trx.org_id = NVL( p_org_id, trx.org_id )
687             AND trx.legal_entity_id = NVL(p_le_id, trx.legal_entity_id )
688             AND trx.set_of_books_id = p_ledger_id -- Ledger is Mandatory Input Param
689             -- Predicates to fetch the Names
690             AND ou.organization_id = trx.org_id
691             AND le.legal_entity_id = trx.legal_entity_id
692             AND ledger.ledger_id = trx.set_of_books_id
693             --End of Query Segment to fetch the UnAccounted Transactions
694       UNION ALL
695          -- Query Segment to fetch the UnAccounted Transactions
696          -- from OLM only. Imp Predicates: XLA_EVENTS.event_status_code in ( 'I', 'U' )
697          SELECT 'TRX_UNACCOUNTED'                         trx_detail_type_code
698                 ,trx.trx_number                           trx_number
699                 ,try.name                                 trx_type_name
700                 ,xvl.name                                 trx_event_name
701                 ,'Leasing and Finance Management'                       trx_application_name
702                 ,chr.contract_number                      contract_number
703                 ,NULL                                     asset_number
704                 ,gts.deal_type                            book_classification -- Fetching Code need to change to fetch Meaning
705                 ,pdt.NAME                                 product_name
706                 ,sty.NAME                                 trx_sty_name
707                 ,sty.styb_purpose_meaning                 trx_sty_purpose
708                 ,trx.currency_code                        currency_code
709                 ,NULL                                     trx_account_name
710                 ,ou.name                                  trx_operating_unit_name
711                 ,le.name                                  trx_legal_entity_name
712                 ,ledger.name                              trx_ledger_name
713                 ,rtryp.add_substract_code                 trx_activity_code
714                 ,NULL                                     trx_period_name
715                 -- Number Format Columns
716                 ,DECODE(nvl(dist.cr_dr_flag, 'D')
717                           ,'D', dist.amount, NULL)        trx_dr_amount
718                 ,DECODE(nvl(dist.cr_dr_flag, 'D')
719                           ,'C', dist.amount, NULL)        trx_cr_amount
720                 ,DECODE(rtryp.add_substract_code
721                          ,'ADD',       txl.amount
722                          ,'SUBTRACT', txl.amount * -1
723                        )                                  trx_net_amount
724                 -- Note that in ATS Mode only distributions can fetch you the CCID
725                 -- In AMB mode, the Un-Accounted Distributions may not have the Code Combinations ID
726                 ,DECODE(nvl(dist.cr_dr_flag, 'D')
727                         ,'D', dist.code_combination_id
728                         , NULL)                           trx_acc_dr_ccid
729                 ,DECODE(nvl(dist.cr_dr_flag, 'D')
730                         ,'C', dist.code_combination_id
731                         , NULL)                           trx_acc_cr_ccid
732                 ,trx.set_of_books_id                      trx_ledger_id
733                 ,trx.org_id                               trx_operating_unit_id
734                 ,trx.legal_entity_id                      trx_legal_entity_id
735                 ,trx.khr_id                               trx_khr_id
736                 ,txl.kle_id                               txl_asset_id
737                 ,khr.pdt_id                               trx_pdt_id
738                 ,txl.sty_id                               trx_sty_id
739                 ,540                                      trx_application_id
740                 -- DATE Format Columns
741                 ,trx.transaction_date                     trx_date
742                 ,dist.gl_date                             gl_date
743                 -- Additional Columns
744                 ,trx.id                                   trx_id
745                 ,txl.id                                   trx_txl_id
746                 ,trx.try_id                               trx_try_id
747                 ,rep.report_id                            trx_report_id
748                 ,dist.id                                  trx_dist_id
749                 -- Occurance date of the Transaction, not the Transaction Effective Date
750                 ,trx.transaction_date                     trx_occ_date
751                 ,trx.date_accrual                         accrual_date
752                 ,'Lease'                                  source
753           FROM   okl_trx_contracts_all     trx
754                 ,okl_txl_cntrct_lns_all    txl
755                 ,okl_trns_acc_dstrs_all    dist
756                 ,okl_trx_types_v           try
757                 ,okl_strm_type_v           sty
758                 ,okc_k_headers_all_b       chr
759                 ,okl_k_headers             khr
760                 ,okl_products              pdt
761                 ,okl_ae_tmpt_sets_all      aes
762                 ,okl_st_gen_tmpt_sets_all  gts
763                 ,okl_reports_b             rep
764                 ,okl_report_trx_params     rtryp
765                 ,okl_rep_products_gt       pdt_gt
766                 -- To fetch Names
767                 ,hr_operating_units        ou
768                 ,xle_entity_profiles       le
769                 ,gl_ledgers_v              ledger
770                 -- XLA Entities
771                 ,xla_events                xe
772                 ,xla_event_types_vl        xvl
773           WHERE  trx.id = txl.tcn_id
774             AND  trx.try_id = try.id
775             AND  try.accounting_event_class_code IS NOT NULL
776             AND  txl.sty_id   = sty.id -- May be we dont need outer join here
777             AND  dist.source_id = txl.id
778             AND  dist.source_table = 'OKL_TXL_CNTRCT_LNS'
779             -- Restrict to only one Distribution Line
780             -- In ATS Mode restrict the distribution to Debit Only
781             -- In AMB Mode only one dist. will be created in OKL, hence consider that as Debit
782             AND  nvl(dist.cr_dr_flag, 'D') = 'D'
783             AND  trx.khr_id = chr.id
784             AND  chr.id = khr.id
785             AND  khr.pdt_id = pdt.id
786             AND  pdt.aes_id = aes.id
787             AND  aes.gts_id = gts.id
788             AND  xe.event_id = dist.accounting_event_id -- Distribution have the Acc. Event Stamp on it
789             AND  xe.application_id = 540 -- Lease
790             AND  xe.event_status_code IN ( 'U', 'I' ) -- Un Accounted Or Errored out
791             AND  xe.application_id = xvl.application_id
792             AND  xvl.event_type_code = xe.event_type_code
793             -- Transaction should have occured in the Start and End date of the Context
794             AND  trx.transaction_date >= p_start_date
795             AND  trx.transaction_date <= p_end_date
796             -- Add Predicates based on the report
797             AND  rep.report_id = p_report_id
798             AND  rtryp.report_id = rep.report_id
799             AND  try.id = rtryp.try_id
800             AND  nvl(nvl(rtryp.sty_id,txl.sty_id), - 1 )  = nvl(txl.sty_id, -1)
801             -- Products restriction
802             AND  pdt_gt.product_id = pdt.id
803             -- Org., Ledger and Legal Entity Id restriction
804             AND trx.org_id = NVL( p_org_id, trx.org_id )
805             AND trx.legal_entity_id = NVL(p_le_id, trx.legal_entity_id )
806             AND trx.set_of_books_id = p_ledger_id -- Ledger is Mandatory Input Param
807             -- Predicates to fetch the Names
808             AND ou.organization_id = trx.org_id
809             AND le.legal_entity_id = trx.legal_entity_id
810             AND ledger.ledger_id = trx.set_of_books_id
811             --End of Query Segment to fetch the UnAccounted Transactions
812       UNION ALL
813         -- Query Segment to fetch the Unposted Accounting Transactions
814         -- from OLM only. Imp Predicates: Accounting Event status in P
815         -- xh.gl_transfer_status_code <> 'Y'
816         SELECT 'TRX_UNPOSTED'                           trx_detail_type_code
817               ,trx.trx_number                           trx_number
818               ,try.name                                 trx_type_name
819               ,xvl.name                                 trx_event_name
820               ,'Leasing and Finance Management'                       trx_application_name
821               ,chr.contract_number                      contract_number
822               ,NULL                                     asset_number
823               ,gts.deal_type                            book_classification -- Fetching Code need to change to fetch Meaning
824               ,pdt.NAME                                 product_name
825               ,sty.NAME                                 trx_sty_name
826               ,sty.styb_purpose_meaning                 trx_sty_purpose
827               ,trx.currency_code                        currency_code
828               ,glcc.concatenated_segments               trx_account_name
829               ,ou.name                                  trx_operating_unit_name
830               ,le.name                                  trx_legal_entity_name
831               ,ledger.name                              trx_ledger_name
832               ,rtryp.add_substract_code                 trx_activity_code
833               ,NULL                                     trx_period_name
834               -- Number Format Columns
835               ,DECODE(nvl(dist.cr_dr_flag, 'D')
836                         ,'D', dist.amount, NULL)        trx_dr_amount
837               ,DECODE(nvl(dist.cr_dr_flag, 'D')
838                         ,'C', dist.amount, NULL)        trx_cr_amount
839               ,DECODE(rtryp.add_substract_code
840                        ,'ADD',       txl.amount
841                        ,'SUBTRACT', txl.amount * -1
842                      )                                  trx_net_amount
843               -- Its safe to fetch the CCID from XLA itself instead of the OKL Dist. table
844               ,DECODE(xl.entered_cr
845                        ,NULL, xl.code_combination_id    -- When Credit Amount is Null, it means Debit Distribution
846                        ,NULL
847                      )                                  trx_acc_dr_ccid
848               ,DECODE(xl.entered_dr
849                        ,NULL, xl.code_combination_id    -- When Debit Amount is Null, it means Credit Distribution
850                        ,NULL
851                      )
852               ,trx.set_of_books_id                      trx_ledger_id
853               ,trx.org_id                               trx_operating_unit_id
854               ,trx.legal_entity_id                      trx_legal_entity_id
855               ,trx.khr_id                               trx_khr_id
856               ,txl.kle_id                               txl_asset_id
857               ,khr.pdt_id                               trx_pdt_id
858               ,txl.sty_id                               trx_sty_id
859               ,540                                      trx_application_id
860               -- DATE Format Columns
861               ,trx.transaction_date                     trx_date
862               ,dist.gl_date                             gl_date
863               -- Additional Columns
864               ,trx.id                                   trx_id
865               ,txl.id                                   trx_txl_id
866               ,trx.try_id                               trx_try_id
867               ,rep.report_id                            trx_report_id
868               ,dist.id                                  trx_dist_id
869               -- Occurance date of the Transaction, not the Transaction Effective Date
870               ,trx.transaction_date                     trx_occ_date
871               ,trx.date_accrual                         accrual_date
872               ,'Lease'                                  source
873         FROM    okl_trx_contracts_all     trx
874                ,okl_txl_cntrct_lns_all    txl
875                ,okl_trns_acc_dstrs_all    dist
876                ,okl_trx_types_v           try
877                ,okl_strm_type_v           sty
878                ,okc_k_headers_all_b       chr
879                ,okl_k_headers             khr
880                ,okl_products              pdt
881                ,okl_ae_tmpt_sets_all      aes
882                ,okl_st_gen_tmpt_sets_all  gts
883                -- SLA Entities
884                ,xla_distribution_links    xd
885                ,xla_ae_headers            xh
886                ,xla_ae_lines              xl
887                ,xla_events                xe
888                ,xla_event_types_vl        xvl
889                -- OLM Reconciliation Report Definitions Table
890                ,okl_reports_b             rep
891                ,okl_report_trx_params     rtryp
892                ,okl_rep_products_gt       pdt_gt
893                -- To fetch Names
894                ,gl_code_combinations_kfv  glcc
895                ,hr_operating_units        ou
896                ,xle_entity_profiles       le
897                ,gl_ledgers_v              ledger
898         WHERE  trx.id = txl.tcn_id
899           AND  trx.try_id = try.id
900           AND  try.accounting_event_class_code IS NOT NULL
901           AND  txl.sty_id   = sty.id -- May be we dont need outer join here
902           AND  dist.source_id = txl.id
903           AND  dist.source_table = 'OKL_TXL_CNTRCT_LNS'
904           -- Restrict to only one Distribution Line based on the Transaction Activity Code Add/Substract
905           -- If its Add Consider only Debit, else if its Substract consider Credit
906           AND
907           (
908             DECODE(xl.entered_cr, NULL, 'DEBIT_DIST', 'CREDIT_DIST' ) =
909                  DECODE(rtryp.add_substract_code,
910                  'ADD', 'DEBIT_DIST', 'SUBTRACT', 'CREDIT_DIST' )
911             OR
912             DECODE(xl.entered_dr, NULL, 'CREDIT_DIST', 'DEBIT_DIST' ) =
913                 DECODE(rtryp.add_substract_code,
914                 'ADD', 'DEBIT_DIST', 'SUBTRACT', 'CREDIT_DIST' )
915           )
916           AND  dist.posted_yn = 'Y'
917           AND  trx.khr_id = chr.id
918           AND  chr.id = khr.id
919           AND  khr.pdt_id = pdt.id
920           AND  pdt.aes_id = aes.id
921           AND  aes.gts_id = gts.id
922           -- OKL to SLA Link predicates
923           AND  dist.accounting_event_id = xe.event_id
924           AND  xe.application_id = xvl.application_id
925           AND  xvl.event_type_code = xe.event_type_code
926           AND  xd.event_id = dist.accounting_event_id
927           AND  dist.id = xd.source_distribution_id_num_1
928           AND  xd.application_id = 540
929           AND  xd.ae_header_id = xh.ae_header_id
930           AND  xl.ae_header_id = xh.ae_header_id
931           AND  xl.ledger_id = p_ledger_id
932           AND  xd.ae_line_num = xl.ae_line_num --XD,XH,XL END
933           -- Important Predicate: XLA Distribution Links should not have the GL Import Link ID
934           AND  xh.gl_transfer_status_code <> 'Y' -- Not Imported to GL Yet
935           -- Transaction should have occured in the Start and End date of the Context
936           AND  trx.transaction_date >= p_start_date
937           AND  trx.transaction_date <= p_end_date
938           -- Add Predicates based on the report
939           AND  rep.report_id = p_report_id
940           AND  rtryp.report_id = rep.report_id
941           AND  try.id = rtryp.try_id
942           AND  nvl(nvl(rtryp.sty_id,txl.sty_id), - 1 )  = nvl(txl.sty_id, -1)
943           -- Products restriction
944           AND  pdt_gt.product_id = pdt.id
945           -- Org., Ledger and Legal Entity Id restriction
946           AND trx.org_id = NVL( p_org_id, trx.org_id )
947           AND trx.legal_entity_id = NVL(p_le_id, trx.legal_entity_id )
948           AND trx.set_of_books_id = p_ledger_id -- Ledger is Mandatory Input Param
949           -- Predicates to fetch the Names
950           AND ou.organization_id = trx.org_id
951           AND le.legal_entity_id = trx.legal_entity_id
952           AND ledger.ledger_id = trx.set_of_books_id
953           AND glcc.code_combination_id = xl.code_combination_id
954         -- End of Query Segment to fetch the Unposted Acc. Entries from OLM
955       UNION ALL
956         -- Query Segment to find the Journal Entries from GL, either Posted or Unposted
957         -- If Unposted Detail Type will be TRX_UNPOSTED else if posted it will be TRX_POSTED
958         SELECT DECODE( gh.status
959                          ,'U',  'TRX_UNPOSTED'  -- Unposted Transactions
960                          ,'P',  'TRX_POSTED'    -- Posted Transactions
961                      )                                  trx_detail_type_code
962               ,trx.trx_number                           trx_number
963               ,try.name                                 trx_type_name
964               ,xvl.name                                 trx_event_name
965               ,'Leasing and Finance Management'                       trx_application_name
966               ,chr.contract_number                      contract_number
967               ,NULL                                     asset_number
968               ,gts.deal_type                            book_classification -- Fetching Code need to change to fetch Meaning
969               ,pdt.NAME                                 product_name
970               ,sty.NAME                                 trx_sty_name
971               ,sty.styb_purpose_meaning                 trx_sty_purpose
972               ,trx.currency_code                        currency_code
973               ,glcc.concatenated_segments               trx_account_name
974               ,ou.name                                  trx_operating_unit_name
975               ,le.name                                  trx_legal_entity_name
976               ,ledger.name                              trx_ledger_name
977               ,rtryp.add_substract_code                 trx_activity_code
978               ,gh.period_name                           trx_period_name
979               -- Number Format Columns
980               ,xl.entered_dr                            trx_dr_amount
981               ,xl.entered_cr                            trx_cr_amount
982               ,DECODE(rtryp.add_substract_code
983                        ,'ADD',       txl.amount
984                        ,'SUBTRACT', txl.amount * -1
985                      )                                  trx_net_amount
986               -- Its safe to fetch the CCID from XLA itself instead of the OKL Dist. table
987               ,DECODE(xl.entered_cr
988                        ,NULL, xl.code_combination_id    -- When Credit Amount is Null, it means Debit Distribution
989                        ,NULL
990                      )                                  trx_acc_dr_ccid
991               ,DECODE(xl.entered_dr
992                        ,NULL, xl.code_combination_id    -- When Debit Amount is Null, it means Credit Distribution
993                        ,NULL
994                      )                                  trx_acc_cr_ccid
995               ,trx.set_of_books_id                      trx_ledger_id
996               ,trx.org_id                               trx_operating_unit_id
997               ,trx.legal_entity_id                      trx_legal_entity_id
998               ,trx.khr_id                               trx_khr_id
999               ,txl.kle_id                               txl_asset_id
1000               ,khr.pdt_id                               trx_pdt_id
1001               ,txl.sty_id                               trx_sty_id
1002               ,540                                      trx_application_id
1003               -- DATE Format Columns
1004               ,trx.transaction_date                     trx_date
1005               ,dist.gl_date                             gl_date
1006               -- Additional Columns
1007               ,trx.id                                   trx_id
1008               ,txl.id                                   trx_txl_id
1009               ,trx.try_id                               trx_try_id
1010               ,rep.report_id                            trx_report_id
1011               ,dist.id                                  trx_dist_id
1012               -- Occurance date of the Transaction, not the Transaction Effective Date
1013               ,trx.transaction_date                     trx_occ_date
1014               ,trx.date_accrual                         accrual_date
1015               ,'Lease'                                  source
1016         FROM    okl_trx_contracts_all     trx
1017                ,okl_txl_cntrct_lns_all    txl
1018                ,okl_trns_acc_dstrs_all    dist
1019                ,okl_trx_types_v           try
1020                ,okl_strm_type_v           sty
1021                ,okc_k_headers_all_b       chr
1022                ,okl_k_headers             khr
1023                ,okl_products              pdt
1024                ,okl_ae_tmpt_sets_all      aes
1025                ,okl_st_gen_tmpt_sets_all  gts
1026                -- SLA Entities
1027                ,xla_distribution_links    xd
1028                ,xla_ae_headers            xh
1029                ,xla_ae_lines              xl
1030                ,xla_events                xe
1031                ,xla_event_types_vl        xvl
1032                -- GL Tables: Import Reference, GL Header and Lines
1033                ,gl_import_references      gi
1034                ,gl_je_headers             gh
1035                ,gl_je_lines               gl
1036                -- OKL Report Definition Tables
1037                ,okl_reports_b             rep
1038                ,okl_report_trx_params     rtryp
1039                ,okl_rep_products_gt       pdt_gt
1040                -- To fetch Names
1041                ,gl_code_combinations_kfv  glcc
1042                ,hr_operating_units        ou
1043                ,xle_entity_profiles       le
1044                ,gl_ledgers_v              ledger
1045         WHERE  trx.id = txl.tcn_id
1046           AND  trx.try_id = try.id
1047           AND  try.accounting_event_class_code IS NOT NULL
1048           AND  txl.sty_id   = sty.id -- May be we dont need outer join here
1049           AND  dist.source_id = txl.id
1050           AND  dist.source_table = 'OKL_TXL_CNTRCT_LNS'
1051           -- Restrict to only one Distribution Line based on the Transaction Activity Code Add/Substract
1052           -- If its Add Consider only Debit, else if its Substract consider Credit
1053           AND
1054           (
1055             DECODE(xl.entered_cr, NULL, 'DEBIT_DIST', 'CREDIT_DIST' ) =
1056                  DECODE(rtryp.add_substract_code,
1057                  'ADD', 'DEBIT_DIST', 'SUBTRACT', 'CREDIT_DIST' )
1058             OR
1059             DECODE(xl.entered_dr, NULL, 'CREDIT_DIST', 'DEBIT_DIST' ) =
1060                 DECODE(rtryp.add_substract_code,
1061                 'ADD', 'DEBIT_DIST', 'SUBTRACT', 'CREDIT_DIST' )
1062           )
1063           AND  dist.posted_yn = 'Y'
1064           AND  trx.khr_id = chr.id
1065           AND  chr.id = khr.id
1066           AND  khr.pdt_id = pdt.id
1067           AND  pdt.aes_id = aes.id
1068           AND  aes.gts_id = gts.id
1069           -- OKL to SLA Link predicates
1070           AND  dist.posted_yn = 'Y'
1071           AND  dist.accounting_event_id = xe.event_id
1072           AND  xe.application_id = xvl.application_id
1073           AND  xvl.event_type_code = xe.event_type_code
1074           AND  xd.event_id = dist.accounting_event_id --XE,XD,XH,XL BEGIN
1075           AND  dist.id = xd.source_distribution_id_num_1
1076           AND  xd.application_id = 540
1077           AND  xd.ae_header_id = xh.ae_header_id
1078           AND  xl.ae_header_id = xh.ae_header_id
1079           AND  xd.ae_line_num = xl.ae_line_num --XD,XH,XL END
1080           -- From SLA to GL Tables Link
1081           AND  gi.gl_sl_link_id = xl.gl_sl_link_id --GL TABLES JOIN START
1082           AND  xl.ledger_id = gl.ledger_id
1083           AND  xl.ledger_id = p_ledger_id
1084           AND  gi.gl_sl_link_table = xl.gl_sl_link_table
1085           AND  gi.je_header_id = gh.je_header_id
1086           AND  gh.je_header_id = gl.je_header_id
1087           AND  gi.je_line_num = gl.je_line_num
1088           AND  gh.je_source = 'Lease'
1089           -- Important Predicate: gl_je_headers.status can be either Posted
1090           -- There can be even Unposted Entries
1091           AND  gh.status IN ( 'U', 'P' ) -- Unposted or Posted Entries
1092           -- Transaction should have occured in the Start and End date of the Context
1093           AND  trx.transaction_date >= p_start_date
1094           AND  trx.transaction_date <= p_end_date
1095           -- Add Predicates based on the report
1096           AND  rep.report_id = p_report_id
1097           AND  rtryp.report_id = rep.report_id
1098           AND  try.id = rtryp.try_id
1099           AND  nvl(nvl(rtryp.sty_id,txl.sty_id), - 1 )  = nvl(txl.sty_id, -1)
1100           -- Products restriction
1101           AND  pdt_gt.product_id = pdt.id
1102           -- Org., Ledger and Legal Entity Id restriction
1103           AND trx.org_id = NVL( p_org_id, trx.org_id )
1104           AND trx.legal_entity_id = NVL(p_le_id, trx.legal_entity_id )
1105           AND trx.set_of_books_id = p_ledger_id -- Ledger is Mandatory Input Param
1106           -- Predicates to fetch the Names
1107           AND ou.organization_id = trx.org_id
1108           AND le.legal_entity_id = trx.legal_entity_id
1109           AND ledger.ledger_id = trx.set_of_books_id
1110           AND glcc.code_combination_id = xl.code_combination_id
1111         -- End of Query Segment to fetch Unposted or Posted Acc. Entries from GL
1112     );
1113     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
1114       'After Inserting the Lease Transactions Data in GT Table End Time:'
1115       || TO_CHAR(SYSDATE, 'DD-MON-YYY HH:MM:SS') );
1116     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
1117       'Time taken for Populating Lease Transactions ' || ( SYSDATE - l_trace_time ) * 86400 || ' Seconds' );
1118 
1119 
1120     ---------------------------------------------------------------------------
1121     -- Query Segments to Populate the Receivables Transactions Data
1122     ---------------------------------------------------------------------------
1123     -- Start the stop watch
1124     l_trace_time := SYSDATE;
1125     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
1126       'Before Inserting the Receivables Invoice Transactions Data in GT Table Start Time:'
1127       || TO_CHAR(l_trace_time, 'DD-MON-YYY HH:MM:SS') );
1128     INSERT INTO okl_g_reports_gt
1129                 (
1130                   -- String Formatted Columns
1131                    value1_text
1132                   ,value2_text
1133                   ,value3_text
1134                   ,value4_text
1135                   ,value5_text
1136                   ,value6_text
1137                   ,value7_text
1138                   ,value8_text
1139                   ,value9_text
1140                   ,value10_text
1141                   ,value11_text
1142                   ,value12_text
1143                   ,value13_text
1144                   ,value14_text
1145                   ,value15_text
1146                   ,value16_text
1147                   ,value17_text
1148                   ,value18_text
1149                   -- Number Formatted Columns
1150                   ,value1_num
1151                   ,value2_num
1152                   ,value3_num
1153                   ,value4_num
1154                   ,value5_num
1155                   ,value6_num
1156                   ,value7_num
1157                   ,value8_num
1158                   ,value9_num
1159                   ,value10_num
1160                   ,value11_num
1161                   ,value12_num
1162                   ,value13_num
1163                   -- Date Formatted Columns
1164                   ,value1_date
1165                   ,value2_date
1166                 )
1167       SELECT -- String Formatted Columns
1168              trx_detail_type_code        --value1_text
1169             ,trx_number                  --value2_text
1170             ,trx_type_name               --value3_text
1171             ,trx_application_name        --value4_text
1172             ,contract_number             --value5_text
1173             ,asset_number                --value6_text
1174             ,book_classification         --value7_text
1175             ,product_name                --value8_text
1176             ,trx_sty_name                --value9_text
1177             ,trx_sty_purpose             --value10_text
1178             ,currency_code               --value11_text
1179             ,trx_period_name             --value12_text -- Populated Only for Posted Entries
1180             ,trx_account_name            --value13_text
1181             ,trx_operating_unit_name     --value14_text
1182             ,trx_legal_entity_name       --value15_text
1183             ,trx_ledger_name             --value16_text
1184             ,trx_activity_code           --value17_text
1185             ,trx_event_name              --value18_text
1186             -- Number Formatted Columns
1187             ,trx_dr_amount               --value1_num
1188             ,trx_cr_amount               --value2_num
1189             ,trx_net_amount              --value3_num
1190             ,trx_acc_dr_ccid             --value4_num
1191             ,trx_acc_cr_ccid             --value5_num
1192             ,trx_ledger_id               --value6_num
1193             ,trx_operating_unit_id       --value7_num
1194             ,trx_legal_entity_id         --value8_num
1195             ,trx_khr_id                  --value9_num
1196             ,txl_asset_id                --value10_num
1197             ,trx_pdt_id                  --value11_num
1198             ,trx_sty_id                  --value12_num
1199             ,trx_application_id          --value13_num
1200             -- Date Formatted Columns
1201             ,trx_date                    --value1_date
1202             ,gl_date                     --value2_date
1203        FROM
1204       (
1205         -------------------------------------------------------------------------------------
1206         -- Query segments to fetch OKL-AR Invoice Transactions from OKL, Not moved to even AR
1207         -------------------------------------------------------------------------------------
1208         -- Query Segment to find the UnAccounted Receivables Invoice Transactions from OKL
1209         SELECT  'TRX_UNACCOUNTED_NO_EVENT'                trx_detail_type_code
1210                 ,tai.trx_number                           trx_number
1211                 ,try.name                                 trx_type_name
1212                 ,NULL                                     trx_event_name
1213                 ,'Receivables'                            trx_application_name
1214                 ,chr.contract_number                      contract_number
1215                 ,NULL                                     asset_number
1216                 ,gts.deal_type                            book_classification -- Fetching Code need to change to fetch Meaning
1217                 ,pdt.NAME                                 product_name
1218                 ,sty.NAME                                 trx_sty_name
1219                 ,sty.styb_purpose_meaning                 trx_sty_purpose
1220                 ,tai.currency_code                        currency_code
1221                 ,NULL                                     trx_account_name
1222                 ,ou.name                                  trx_operating_unit_name
1223                 ,le.name                                  trx_legal_entity_name
1224                 ,ledger.name                              trx_ledger_name
1225                 ,rtryp.add_substract_code                 trx_activity_code
1226                 ,NULL                                     trx_period_name
1227                 -- Number Format Columns
1228                 ,NULL                                     trx_dr_amount
1229                 ,NULL                                     trx_cr_amount
1230                 ,DECODE(rtryp.add_substract_code
1231                          ,'ADD',       txd.amount
1232                          ,'SUBTRACT',  txd.amount * -1
1233                        )                                  trx_net_amount
1234                 -- Note that in ATS Mode only distributions can fetch you the CCID
1235                 -- In AMB mode, the Un-Accounted Distributions may not have the Code Combinations ID
1236                 ,NULL                                     trx_acc_dr_ccid
1237                 ,NULL                                     trx_acc_cr_ccid
1238                 ,tai.set_of_books_id                      trx_ledger_id
1239                 ,tai.org_id                               trx_operating_unit_id
1240                 ,tai.legal_entity_id                      trx_legal_entity_id
1241                 ,txd.khr_id                               trx_khr_id
1242                 ,txd.kle_id                               txl_asset_id
1243                 ,khr.pdt_id                               trx_pdt_id
1244                 ,txd.sty_id                               trx_sty_id
1245                 ,222                                      trx_application_id
1246                 -- DATE Format Columns
1247                 ,tai.transaction_date                     trx_date
1248                 ,NULL                                     gl_date  -- Invoice Date
1249                 -- Additional Columns
1250                 ,tai.id                                   trx_id
1251                 ,til.id                                   trx_txl_id
1252                 ,tai.try_id                               trx_try_id
1253                 ,rep.report_id                            trx_report_id
1254                 ,NULL                                     trx_dist_id
1255                 -- Occurance date of the Transaction, not the Transaction Effective Date
1256                 ,tai.transaction_date                     trx_occ_date
1257                 ,'Receivables'                            source
1258           FROM  okl_txd_ar_ln_dtls_b           txd
1259                ,okl_txl_ar_inv_lns_b           til
1260                ,okl_trx_ar_invoices_b          tai
1261                ,okc_k_headers_all_b            chr
1262                ,okl_k_headers                  khr
1263                ,okl_products                   pdt
1264                ,okl_ae_tmpt_sets_all           aes
1265                ,okl_st_gen_tmpt_sets_all       gts
1266                ,okl_trx_types_v                try
1267                ,okl_strm_type_v                sty
1268                -- OLM Reconciliation Report Entities
1269                ,okl_reports_b                  rep
1270                ,okl_report_trx_params          rtryp
1271                ,okl_rep_products_gt            pdt_gt
1272                -- To fetch Names
1273                ,hr_operating_units             ou
1274                ,xle_entity_profiles            le
1275                ,gl_ledgers_v                   ledger
1276           WHERE -- OKL to AR Application Predicates
1277                 txd.til_id_details = til.id -- Trx. Detail to Trx. Line
1278             AND til.tai_id = tai.id         -- Trx. Line to Trx. Header
1279             AND txd.khr_id = chr.id         -- Trx. Header to Contract
1280             -- OKL_ARINTF_PVT picks
1281             -- Possible values found: CANCELED, ERROR, SUBMITTED, PROCESSED, WORKING
1282             -- When the Trx. Status is SUBMITTED, then it means that the Trx. is just in OKL
1283             -- When the Trx. Status is PROCESSED, then it means that the Trx. may
1284             --   a. have moved to AR Import Tables but not yet imported successfully.
1285             --   b. Have been moved to AR Import Tables successfully..
1286             AND  (
1287                     tai.trx_status_code = 'SUBMITTED'
1288                   OR (
1289                          tai.trx_status_code = 'PROCESSED'
1290                      AND NOT EXISTS
1291                          (
1292                             SELECT 1
1293                               FROM ra_customer_trx_lines_all  rcl
1294                              WHERE txd.id = rcl.interface_line_attribute14
1295                                AND chr.contract_number = rcl.interface_line_attribute6
1296                          ) -- Close: Not Exists
1297                     ) -- Close: Only for Processed
1298                  ) -- Close: For both Submitted/Processed
1299             -- Notes: In R12 Found that the OKL_ARINTF_PVT picks only submitted trx.s and moves it to AR
1300             AND chr.id     = khr.id
1301             AND khr.pdt_id = pdt.id
1302             AND pdt.aes_id = aes.id
1303             AND aes.gts_id = gts.id
1304             AND tai.try_id = try.id
1305             AND txd.sty_id = sty.id
1306             -- Transaction should have occured in the Start and End date of the Context
1307             AND tai.transaction_date >= p_start_date
1308             AND tai.transaction_date <= p_end_date
1309             -- Add Predicates based on the report
1310             AND  rep.report_id = p_report_id
1311             AND  rtryp.report_id = rep.report_id
1312             AND  try.id = rtryp.try_id
1313             AND  nvl(nvl(rtryp.sty_id,txd.sty_id), - 1 )  = nvl(txd.sty_id, -1)
1314             -- Products restriction
1315             AND  pdt_gt.product_id = pdt.id
1316             -- Org., Ledger and Legal Entity Id restriction
1317             AND tai.org_id = NVL( p_org_id, tai.org_id )
1318             AND tai.legal_entity_id = NVL(p_le_id, tai.legal_entity_id )
1319             AND tai.set_of_books_id = p_ledger_id -- Ledger is Mandatory Input Param
1320             -- Predicates to fetch the Names
1321             AND ou.organization_id = tai.org_id
1322             AND le.legal_entity_id = tai.legal_entity_id
1323             AND ledger.ledger_id = tai.set_of_books_id
1324       UNION ALL
1325         -------------------------------------------------------------------------
1326         -- Query segments to fetch Invoice Transactions from AR related to OLM Contracts
1327         -------------------------------------------------------------------------
1328         -- Query Segment to find the UnAccounted Receivables Invoice Transactions from Receivables
1329         SELECT  'TRX_UNACCOUNTED'                         trx_detail_type_code
1330                 ,rct.trx_number                           trx_number
1331                 ,try.name                                 trx_type_name
1332                 ,xvl.name                                 trx_event_name
1333                 ,'Receivables'                            trx_application_name
1334                 ,chr.contract_number                      contract_number
1335                 ,NULL                                     asset_number
1336                 ,gts.deal_type                            book_classification -- Fetching Code need to change to fetch Meaning
1337                 ,pdt.NAME                                 product_name
1338                 ,sty.NAME                                 trx_sty_name
1339                 ,sty.styb_purpose_meaning                 trx_sty_purpose
1340                 ,tai.currency_code                        currency_code
1341                 ,NULL                                     trx_account_name
1342                 ,ou.name                                  trx_operating_unit_name
1343                 ,le.name                                  trx_legal_entity_name
1344                 ,ledger.name                              trx_ledger_name
1345                 ,rtryp.add_substract_code                 trx_activity_code
1346                 ,NULL                                     trx_period_name
1347                 -- Number Format Columns
1348                 ,NULL                                     trx_dr_amount
1349                 ,NULL                                     trx_cr_amount
1350                 ,DECODE(rtryp.add_substract_code
1351                          ,'ADD',       txd.amount
1352                          ,'SUBTRACT',  txd.amount * -1
1353                        )                                  trx_net_amount
1354                 -- Note that in ATS Mode only distributions can fetch you the CCID
1355                 -- In AMB mode, the Un-Accounted Distributions may not have the Code Combinations ID
1356                 ,NULL                                     trx_acc_dr_ccid
1357                 ,NULL                                     trx_acc_cr_ccid
1358                 ,tai.set_of_books_id                      trx_ledger_id
1359                 ,tai.org_id                               trx_operating_unit_id
1360                 ,tai.legal_entity_id                      trx_legal_entity_id
1361                 ,txd.khr_id                               trx_khr_id
1362                 ,txd.kle_id                               txl_asset_id
1363                 ,khr.pdt_id                               trx_pdt_id
1364                 ,txd.sty_id                               trx_sty_id
1365                 ,222                                      trx_application_id
1366                 -- DATE Format Columns
1367                 ,tai.transaction_date                     trx_date
1368                 ,rct.trx_date                             gl_date  -- Invoice Date
1369                 -- Additional Columns
1370                 ,tai.id                                   trx_id
1371                 ,til.id                                   trx_txl_id
1372                 ,tai.try_id                               trx_try_id
1373                 ,rep.report_id                            trx_report_id
1374                 ,NULL                                     trx_dist_id
1375                 -- Occurance date of the Transaction, not the Transaction Effective Date
1376                 ,tai.transaction_date                     trx_occ_date
1377                 ,'Receivables'                            source
1378           FROM  okl_txd_ar_ln_dtls_b           txd
1379                ,okl_txl_ar_inv_lns_b           til
1380                ,okl_trx_ar_invoices_b          tai
1381                ,okc_k_headers_all_b            chr
1382                ,okl_k_headers                  khr
1383                ,okl_products                   pdt
1384                ,okl_ae_tmpt_sets_all           aes
1385                ,okl_st_gen_tmpt_sets_all       gts
1386                ,okl_trx_types_v                try
1387                ,okl_strm_type_v                sty
1388                -- AR Tables
1389                ,ra_customer_trx_lines_all      rcl
1390                ,ra_customer_trx_all            rct
1391                ,ra_cust_trx_line_gl_dist_all   rad
1392                -- XLA Tables
1393                ,xla_events                     xe
1394                ,xla_event_types_vl             xvl
1395                -- OLM Reconciliation Report Entities
1396                ,okl_reports_b                  rep
1397                ,okl_report_trx_params          rtryp
1398                ,okl_rep_products_gt            pdt_gt
1399                -- To fetch Names
1400                ,hr_operating_units             ou
1401                ,xle_entity_profiles            le
1402                ,gl_ledgers_v                   ledger
1403           WHERE -- OKL to AR Application Predicates
1404                 txd.til_id_details = til.id -- Trx. Detail to Trx. Line
1405             AND til.tai_id = tai.id         -- Trx. Line to Trx. Header
1406             AND txd.khr_id = chr.id         -- Trx. Header to Contract
1407             AND chr.id     = khr.id
1408             AND khr.pdt_id = pdt.id
1409             AND pdt.aes_id = aes.id
1410             AND aes.gts_id = gts.id
1411             AND tai.try_id = try.id
1412             AND txd.sty_id = sty.id
1413             -- Transaction should have occured in the Start and End date of the Context
1414             AND tai.transaction_date >= p_start_date
1415             AND tai.transaction_date <= p_end_date
1416             -- Add Predicates based on the report
1417             AND  rep.report_id = p_report_id
1418             AND  rtryp.report_id = rep.report_id
1419             AND  try.id = rtryp.try_id
1420             AND  nvl(nvl(rtryp.sty_id,txd.sty_id), - 1 )  = nvl(txd.sty_id, -1)
1421             -- Products restriction
1422             AND  pdt_gt.product_id = pdt.id
1423             -- Org., Ledger and Legal Entity Id restriction
1424             AND tai.org_id = NVL( p_org_id, tai.org_id )
1425             AND tai.legal_entity_id = NVL(p_le_id, tai.legal_entity_id )
1426             AND tai.set_of_books_id = p_ledger_id -- Ledger is Mandatory Input Param
1427             -- Predicates to fetch the Names
1428             AND ou.organization_id = tai.org_id
1429             AND le.legal_entity_id = tai.legal_entity_id
1430             AND ledger.ledger_id = tai.set_of_books_id
1431             -- OKL to AR Predicates
1432             AND txd.id = rcl.interface_line_attribute14
1433             AND chr.contract_number = rcl.interface_line_attribute6
1434             AND rct.customer_trx_id = rcl.customer_trx_id
1435             AND rct.org_id = nvl(p_org_id, rct.org_id)
1436             AND rcl.customer_trx_line_id = rad.customer_trx_line_id
1437             AND rct.customer_trx_id = rad.customer_trx_id
1438             -- AR to XLA Relations
1439             AND rad.event_id = xe.event_id
1440             AND xe.application_id = 222
1441             AND xe.event_status_code IN ( 'U', 'I' )
1442             AND xe.application_id = xvl.application_id
1443             AND xvl.event_type_code = xe.event_type_code
1444       UNION ALL
1445         -- Query Segment to find the UnPosted Receivables Invoice Transactions from Receivables
1446         -- And hence the Dr/Cr CCId are from XLA only
1447         -- Important Predicate again is xh.gl_transfer_status_code <> 'Y'
1448         SELECT  'TRX_UNPOSTED'                            trx_detail_type_code
1449                 ,rct.trx_number                           trx_number
1450                 ,try.name                                 trx_type_name
1451                 ,xvl.name                                 trx_event_name
1452                 ,'Receivables'                            trx_application_name
1453                 ,chr.contract_number                      contract_number
1454                 ,NULL                                     asset_number
1455                 ,gts.deal_type                            book_classification -- Fetching Code need to change to fetch Meaning
1456                 ,pdt.NAME                                 product_name
1457                 ,sty.NAME                                 trx_sty_name
1458                 ,sty.styb_purpose_meaning                 trx_sty_purpose
1459                 ,tai.currency_code                        currency_code
1460                 ,NULL                                     trx_account_name
1461                 ,ou.name                                  trx_operating_unit_name
1462                 ,le.name                                  trx_legal_entity_name
1463                 ,ledger.name                              trx_ledger_name
1464                 ,rtryp.add_substract_code                 trx_activity_code
1465                 ,NULL                                     trx_period_name
1466                 -- Number Format Columns
1467                 ,xl.entered_dr                            trx_dr_amount
1468                 ,xl.entered_cr                            trx_cr_amount
1469                 ,DECODE(rtryp.add_substract_code
1470                          ,'ADD',      txd.amount
1471                          ,'SUBTRACT', txd.amount * -1
1472                        )                                  trx_net_amount
1473                 -- Its safe to fetch the CCID from XLA itself instead of the OKL Dist. table
1474                 ,DECODE(xl.entered_cr
1475                          ,NULL, xl.code_combination_id    -- When Credit Amount is Null, it means Debit Distribution
1476                          ,NULL
1477                        )                                  trx_acc_dr_ccid
1478                 ,DECODE(xl.entered_dr
1479                          ,NULL, xl.code_combination_id    -- When Debit Amount is Null, it means Credit Distribution
1480                          ,NULL
1481                        )                                  trx_acc_cr_ccid
1482                 ,tai.set_of_books_id                      trx_ledger_id
1483                 ,tai.org_id                               trx_operating_unit_id
1484                 ,tai.legal_entity_id                      trx_legal_entity_id
1485                 ,txd.khr_id                               trx_khr_id
1486                 ,txd.kle_id                               txl_asset_id
1487                 ,khr.pdt_id                               trx_pdt_id
1488                 ,txd.sty_id                               trx_sty_id
1489                 ,222                                      trx_application_id
1490                 -- DATE Format Columns
1491                 ,tai.transaction_date                     trx_date
1492                 ,rct.trx_date                             gl_date  -- Invoice Date
1493                 -- Additional Columns
1494                 ,tai.id                                   trx_id
1495                 ,til.id                                   trx_txl_id
1496                 ,tai.try_id                               trx_try_id
1497                 ,rep.report_id                            trx_report_id
1498                 ,NULL                                     trx_dist_id
1499                 -- Occurance date of the Transaction, not the Transaction Effective Date
1500                 ,tai.transaction_date                     trx_occ_date
1501                 ,'Receivables'                            source
1502           FROM  okl_txd_ar_ln_dtls_b           txd
1503                ,okl_txl_ar_inv_lns_b           til
1504                ,okl_trx_ar_invoices_b          tai
1505                ,okc_k_headers_all_b            chr
1506                ,okl_k_headers                  khr
1507                ,okl_products                   pdt
1508                ,okl_ae_tmpt_sets_all           aes
1509                ,okl_st_gen_tmpt_sets_all       gts
1510                ,okl_trx_types_v                try
1511                ,okl_strm_type_v                sty
1512                -- AR Tables
1513                ,ra_customer_trx_lines_all      rcl
1514                ,ra_customer_trx_all            rct
1515                ,ra_cust_trx_line_gl_dist_all   rad
1516                -- XLA Tables
1517                ,xla_events                     xe
1518                ,xla_event_types_vl             xvl
1519                ,xla_distribution_links         xd
1520                ,xla_ae_headers                 xh
1521                ,xla_ae_lines                   xl
1522                -- OLM Reconciliation Report Entities
1523                ,okl_reports_b                  rep
1524                ,okl_report_trx_params          rtryp
1525                ,okl_rep_products_gt            pdt_gt
1526                -- To fetch Names
1527                ,hr_operating_units             ou
1528                ,xle_entity_profiles            le
1529                ,gl_ledgers_v                   ledger
1530           WHERE -- OKL to AR Application Predicates
1531                 txd.til_id_details = til.id -- Trx. Detail to Trx. Line
1532             AND til.tai_id = tai.id         -- Trx. Line to Trx. Header
1533             AND txd.khr_id = chr.id         -- Trx. Header to Contract
1534             AND chr.id     = khr.id
1535             AND khr.pdt_id = pdt.id
1536             AND pdt.aes_id = aes.id
1537             AND aes.gts_id = gts.id
1538             AND tai.try_id = try.id
1539             AND txd.sty_id = sty.id
1540             -- Transaction should have occured in the Start and End date of the Context
1541             AND tai.transaction_date >= p_start_date
1542             AND tai.transaction_date <= p_end_date
1543             -- Add Predicates based on the report
1544             AND  rep.report_id = p_report_id
1545             AND  rtryp.report_id = rep.report_id
1546             AND  try.id = rtryp.try_id
1547             AND  nvl(nvl(rtryp.sty_id,txd.sty_id), - 1 )  = nvl(txd.sty_id, -1)
1548             -- Products restriction
1549             AND  pdt_gt.product_id = pdt.id
1550             -- Org., Ledger and Legal Entity Id restriction
1551             AND tai.org_id = NVL( p_org_id, tai.org_id )
1552             AND tai.legal_entity_id = NVL(p_le_id, tai.legal_entity_id )
1553             AND tai.set_of_books_id = p_ledger_id -- Ledger is Mandatory Input Param
1554             -- Predicates to fetch the Names
1555             AND ou.organization_id = tai.org_id
1556             AND le.legal_entity_id = tai.legal_entity_id
1557             AND ledger.ledger_id = tai.set_of_books_id
1558             -- OKL to AR Predicates
1559             AND txd.id = rcl.interface_line_attribute14
1560             AND chr.contract_number = rcl.interface_line_attribute6
1561             AND rct.customer_trx_id = rcl.customer_trx_id
1562             AND rct.org_id = nvl(p_org_id, rct.org_id )
1563             AND rcl.customer_trx_line_id = rad.customer_trx_line_id
1564             AND rct.customer_trx_id = rad.customer_trx_id
1565             -- AR to XLA Relations
1566             AND rad.event_id = xe.event_id
1567             AND xe.application_id = 222
1568             AND xe.event_status_code = 'P'
1569             AND xe.application_id = xvl.application_id
1570             AND xvl.event_type_code = xe.event_type_code
1571             AND xd.event_id = rad.event_id
1572             AND xd.source_distribution_id_num_1 = rad.cust_trx_line_gl_dist_id
1573             AND xd.application_id = 222
1574             AND xd.ae_header_id = xh.ae_header_id
1575             AND xl.ae_header_id = xh.ae_header_id
1576             AND xl.ledger_id = p_ledger_id
1577             AND xd.ae_line_num = xl.ae_line_num --XD,XH,XL END
1578             -- Important Predicate: XLA Distribution Links should not have the GL Import Link ID
1579             AND  xh.gl_transfer_status_code <> 'Y' -- Not Imported to GL Yet
1580             -- Restrict to only one Distribution Line based on the Transaction Activity Code Add/Substract
1581             -- If its Add Consider only Debit, else if its Substract consider Credit
1582             AND
1583             (
1584               DECODE(xl.entered_cr, NULL, 'DEBIT_DIST', 'CREDIT_DIST' ) =
1585                    DECODE(rtryp.add_substract_code,
1586                    'ADD', 'DEBIT_DIST', 'SUBTRACT', 'CREDIT_DIST' )
1587               OR
1588               DECODE(xl.entered_dr, NULL, 'CREDIT_DIST', 'DEBIT_DIST' ) =
1589                   DECODE(rtryp.add_substract_code,
1590                   'ADD', 'DEBIT_DIST', 'SUBTRACT', 'CREDIT_DIST' )
1591             )
1592       UNION ALL
1593         -- Query Segment to find the Imported Receivables Invoice Transactions from GL
1594         -- Note that that the Acc. Entry may or may not be posted into GL.
1595         -- Important Predicate again is xla_ae_lines.gl_sl_link_id IS NOT NULL
1596         SELECT   DECODE( gh.status
1597                          ,'U',  'TRX_UNPOSTED'  -- Unposted Transactions
1598                          ,'P',  'TRX_POSTED'    -- Posted Transactions
1599                      )                                    trx_detail_type_code
1600                 ,rct.trx_number                           trx_number
1601                 ,try.name                                 trx_type_name
1602                 ,xvl.name                                 trx_event_name
1603                 ,'Receivables'                            trx_application_name
1604                 ,chr.contract_number                      contract_number
1605                 ,NULL                                     asset_number
1606                 ,gts.deal_type                            book_classification -- Fetching Code need to change to fetch Meaning
1607                 ,pdt.NAME                                 product_name
1608                 ,sty.NAME                                 trx_sty_name
1609                 ,sty.styb_purpose_meaning                 trx_sty_purpose
1610                 ,tai.currency_code                        currency_code
1611                 ,glcc.concatenated_segments               trx_account_name
1612                 ,ou.name                                  trx_operating_unit_name
1613                 ,le.name                                  trx_legal_entity_name
1614                 ,ledger.name                              trx_ledger_name
1615                 ,rtryp.add_substract_code                 trx_activity_code
1616                 ,gh.period_name                           trx_period_name
1617                 -- Number Format Columns
1618                 ,xl.entered_dr                            trx_dr_amount
1619                 ,xl.entered_cr                            trx_cr_amount
1620                 ,DECODE(rtryp.add_substract_code
1621                          ,'ADD',      txd.amount
1622                          ,'SUBTRACT', txd.amount * -1
1623                        )                                  trx_net_amount
1624                 -- Its safe to fetch the CCID from XLA itself instead of the OKL Dist. table
1625                 ,DECODE(xl.entered_cr
1626                          ,NULL, xl.code_combination_id    -- When Credit Amount is Null, it means Debit Distribution
1627                          ,NULL
1628                        )                                  trx_acc_dr_ccid
1629                 ,DECODE(xl.entered_dr
1630                          ,NULL, xl.code_combination_id    -- When Debit Amount is Null, it means Credit Distribution
1631                          ,NULL
1632                        )
1633                 ,tai.set_of_books_id                      trx_ledger_id
1634                 ,tai.org_id                               trx_operating_unit_id
1635                 ,tai.legal_entity_id                      trx_legal_entity_id
1636                 ,txd.khr_id                               trx_khr_id
1637                 ,txd.kle_id                               txl_asset_id
1638                 ,khr.pdt_id                               trx_pdt_id
1639                 ,txd.sty_id                               trx_sty_id
1640                 ,222                                      trx_application_id
1641                 -- DATE Format Columns
1642                 ,tai.transaction_date                     trx_date
1643                 ,rct.trx_date                             gl_date  -- Invoice Date
1644                 -- Additional Columns
1645                 ,tai.id                                   trx_id
1646                 ,til.id                                   trx_txl_id
1647                 ,tai.try_id                               trx_try_id
1648                 ,rep.report_id                            trx_report_id
1649                 ,NULL                                     trx_dist_id
1650                 -- Occurance date of the Transaction, not the Transaction Effective Date
1651                 ,tai.transaction_date                     trx_occ_date
1652                 ,'Receivables'                            source
1653           FROM  okl_txd_ar_ln_dtls_b           txd
1654                ,okl_txl_ar_inv_lns_b           til
1655                ,okl_trx_ar_invoices_b          tai
1656                ,okc_k_headers_all_b            chr
1657                ,okl_k_headers                  khr
1658                ,okl_products                   pdt
1659                ,okl_ae_tmpt_sets_all           aes
1660                ,okl_st_gen_tmpt_sets_all       gts
1661                ,okl_trx_types_v                try
1662                ,okl_strm_type_v                sty
1663                -- AR Tables
1664                ,ra_customer_trx_lines_all      rcl
1665                ,ra_customer_trx_all            rct
1666                ,ra_cust_trx_line_gl_dist_all   rad
1667                -- XLA Tables
1668                ,xla_events                     xe
1669                ,xla_event_types_vl             xvl
1670                ,xla_distribution_links         xd
1671                ,xla_ae_headers                 xh
1672                ,xla_ae_lines                   xl
1673                -- GL Tables: Import Reference, GL Header and Lines
1674                ,gl_import_references           gi
1675                ,gl_je_headers                  gh
1676                ,gl_je_lines                    gl
1677                ,gl_code_combinations_kfv       glcc
1678                -- OLM Reconciliation Report Entities
1679                ,okl_reports_b                  rep
1680                ,okl_report_trx_params          rtryp
1681                ,okl_rep_products_gt            pdt_gt
1682                -- To fetch Names
1683                ,hr_operating_units             ou
1684                ,xle_entity_profiles            le
1685                ,gl_ledgers_v                   ledger
1686           WHERE -- OKL to AR Application Predicates
1687                 txd.til_id_details = til.id -- Trx. Detail to Trx. Line
1688             AND til.tai_id = tai.id         -- Trx. Line to Trx. Header
1689             AND txd.khr_id = chr.id         -- Trx. Header to Contract
1690             AND chr.id     = khr.id
1691             AND khr.pdt_id = pdt.id
1692             AND pdt.aes_id = aes.id
1693             AND aes.gts_id = gts.id
1694             AND tai.try_id = try.id
1695             AND txd.sty_id = sty.id
1696             -- Transaction should have occured in the Start and End date of the Context
1697             AND tai.transaction_date >= p_start_date
1698             AND tai.transaction_date <= p_end_date
1699             -- Add Predicates based on the report
1700             AND  rep.report_id = p_report_id
1701             AND  rtryp.report_id = rep.report_id
1702             AND  try.id = rtryp.try_id
1703             AND  nvl(nvl(rtryp.sty_id,txd.sty_id), - 1 )  = nvl(txd.sty_id, -1)
1704             -- Products restriction
1705             AND  pdt_gt.product_id = pdt.id
1706             -- Org., Ledger and Legal Entity Id restriction
1707             AND tai.org_id = NVL( p_org_id, tai.org_id )
1708             AND tai.legal_entity_id = NVL(p_le_id, tai.legal_entity_id )
1709             AND tai.set_of_books_id = p_ledger_id -- Ledger is Mandatory Input Param
1710             -- Predicates to fetch the Names
1711             AND ou.organization_id = tai.org_id
1712             AND le.legal_entity_id = tai.legal_entity_id
1713             AND ledger.ledger_id = tai.set_of_books_id
1714             -- OKL to AR Predicates
1715             AND txd.id = rcl.interface_line_attribute14
1716             AND chr.contract_number = rcl.interface_line_attribute6
1717             AND rct.customer_trx_id = rcl.customer_trx_id
1718             AND rct.org_id = nvl(p_org_id, rct.org_id )
1719             AND rcl.customer_trx_line_id = rad.customer_trx_line_id
1720             AND rct.customer_trx_id = rad.customer_trx_id
1721             -- AR to XLA Relations
1722             AND rad.event_id = xe.event_id
1723             AND xe.application_id = 222
1724             AND xe.event_status_code = 'P'
1725             AND xe.application_id = xvl.application_id
1726             AND xvl.event_type_code = xe.event_type_code
1727             AND xd.event_id = rad.event_id
1728             AND xd.source_distribution_id_num_1 = rad.cust_trx_line_gl_dist_id
1729             AND xd.application_id = 222
1730             AND xd.ae_header_id = xh.ae_header_id
1731             AND xl.ae_header_id = xh.ae_header_id
1732             AND xl.ledger_id = p_ledger_id
1733             AND glcc.code_combination_id = xl.code_combination_id
1734             AND xd.ae_line_num = xl.ae_line_num --XD,XH,XL END
1735             -- Restrict to only one Distribution Line based on the Transaction Activity Code Add/Substract
1736             -- If its Add Consider only Debit, else if its Substract consider Credit
1737             AND
1738             (
1739               DECODE(xl.entered_cr, NULL, 'DEBIT_DIST', 'CREDIT_DIST' ) =
1740                    DECODE(rtryp.add_substract_code,
1741                    'ADD', 'DEBIT_DIST', 'SUBTRACT', 'CREDIT_DIST' )
1742               OR
1743               DECODE(xl.entered_dr, NULL, 'CREDIT_DIST', 'DEBIT_DIST' ) =
1744                   DECODE(rtryp.add_substract_code,
1745                   'ADD', 'DEBIT_DIST', 'SUBTRACT', 'CREDIT_DIST' )
1746             )
1747             -- From SLA to GL Tables Link
1748             -- Important Predicate: XLA Distribution Links should have the GL Import Link ID
1749             AND  gi.gl_sl_link_id = xl.gl_sl_link_id --GL TABLES JOIN START
1750             AND  gl.ledger_id = xl.ledger_id
1751             AND  gi.gl_sl_link_table = xl.gl_sl_link_table
1752             AND  gi.je_header_id = gh.je_header_id
1753             AND  gh.je_header_id = gl.je_header_id
1754             AND  gi.je_line_num = gl.je_line_num
1755             --  AND  gl.code_combination_id = cc.ccid
1756             AND  gh.je_source = 'Receivables'
1757             -- Important Predicate: gl_je_headers.status can be either Posted
1758             -- There can be even Unposted Entries
1759             AND  gh.status IN ( 'U', 'P' ) -- Unposted or Posted Entries
1760     );
1761     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
1762       'After Inserting the Receivables Invoice Transactions Data in GT Table End Time:'
1763       || TO_CHAR(SYSDATE, 'DD-MON-YYY HH:MM:SS') );
1764     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
1765       'Time taken for Populating Receivables Invoice Transactions '
1766        || ( SYSDATE - l_trace_time ) * 86400 || ' Seconds' );
1767 
1768     ---------------------------------------------------------------------------
1769     -- Query Segments to Populate the Receivables Adjustments Transactions Data
1770     ---------------------------------------------------------------------------
1771     -- Start the stop watch
1772     l_trace_time := SYSDATE;
1773     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
1774       'Before Inserting the Receivables Adjustments Transactions Data in GT Table Start Time:'
1775       || TO_CHAR(l_trace_time, 'DD-MON-YYY HH:MM:SS') );
1776     INSERT INTO okl_g_reports_gt
1777                 (
1778                   -- String Formatted Columns
1779                    value1_text
1780                   ,value2_text
1781                   ,value3_text
1782                   ,value4_text
1783                   ,value5_text
1784                   ,value6_text
1785                   ,value7_text
1786                   ,value8_text
1787                   ,value9_text
1788                   ,value10_text
1789                   ,value11_text
1790                   ,value12_text
1791                   ,value13_text
1792                   ,value14_text
1793                   ,value15_text
1794                   ,value16_text
1795                   ,value17_text
1796                   ,value18_text
1797                   -- Number Formatted Columns
1798                   ,value1_num
1799                   ,value2_num
1800                   ,value3_num
1801                   ,value4_num
1802                   ,value5_num
1803                   ,value6_num
1804                   ,value7_num
1805                   ,value8_num
1806                   ,value9_num
1807                   ,value10_num
1808                   ,value11_num
1809                   ,value12_num
1810                   ,value13_num
1811                   -- Date Formatted Columns
1812                   ,value1_date
1813                   ,value2_date
1814                 )
1815       SELECT -- String Formatted Columns
1816              trx_detail_type_code        --value1_text
1817             ,trx_number                  --value2_text
1818             ,trx_type_name               --value3_text
1819             ,trx_application_name        --value4_text
1820             ,contract_number             --value5_text
1821             ,asset_number                --value6_text
1822             ,book_classification         --value7_text
1823             ,product_name                --value8_text
1824             ,trx_sty_name                --value9_text
1825             ,trx_sty_purpose             --value10_text
1826             ,currency_code               --value11_text
1827             ,trx_period_name             --value12_text -- Populated Only for Posted Entries
1828             ,trx_account_name            --value13_text
1829             ,trx_operating_unit_name     --value14_text
1830             ,trx_legal_entity_name       --value15_text
1831             ,trx_ledger_name             --value16_text
1832             ,trx_activity_code           --value17_text
1833             ,trx_event_name              --value18_text
1834             -- Number Formatted Columns
1835             ,trx_dr_amount               --value1_num
1836             ,trx_cr_amount               --value2_num
1837             ,trx_net_amount              --value3_num
1838             ,trx_acc_dr_ccid             --value4_num
1839             ,trx_acc_cr_ccid             --value5_num
1840             ,trx_ledger_id               --value6_num
1841             ,trx_operating_unit_id       --value7_num
1842             ,trx_legal_entity_id         --value8_num
1843             ,trx_khr_id                  --value9_num
1844             ,txl_asset_id                --value10_num
1845             ,trx_pdt_id                  --value11_num
1846             ,trx_sty_id                  --value12_num
1847             ,trx_application_id          --value13_num
1848             -- Date Formatted Columns
1849             ,trx_date                    --value1_date
1850             ,gl_date                     --value2_date
1851         FROM
1852        (
1853         -- Query Segment to find the OLM-AR Adjustment Transactions
1854         -- which are not yet moved to AR even.
1855         -- Important predicate ajl.receivables_adjustment_id IS NULL
1856         SELECT  'TRX_UNACCOUNTED_NO_EVENT'                trx_detail_type_code
1857                 ,chr.contract_number || adj.adjustment_reason_code
1858                                                           trx_number
1859                 ,try.name                                 trx_type_name
1860                 ,NULL                                     trx_event_name
1861                 ,'Receivables'                            trx_application_name
1862                 ,chr.contract_number                      contract_number
1863                 ,NULL                                     asset_number
1864                 ,gts.deal_type                            book_classification -- Fetching Code need to change to fetch Meaning
1865                 ,pdt.NAME                                 product_name
1866                 ,sty.NAME                                 trx_sty_name
1867                 ,sty.styb_purpose_meaning                 trx_sty_purpose
1868                 ,tai.currency_code                        currency_code
1869                 ,NULL                                     trx_account_name
1870                 ,ou.name                                  trx_operating_unit_name
1871                 ,NULL                                     trx_legal_entity_name
1872                 ,ledger.name                              trx_ledger_name
1873                 ,rtryp.add_substract_code                 trx_activity_code
1874                 ,NULL                                     trx_period_name
1875                 -- Number Format Columns
1876                 ,NULL                                     trx_dr_amount
1877                 ,NULL                                     trx_cr_amount
1878                 ,DECODE(rtryp.add_substract_code
1879                          ,'ADD',       ajl.amount
1880                          ,'SUBTRACT',  ajl.amount * -1
1881                        )                                  trx_net_amount
1882                 -- Note that in ATS Mode only distributions can fetch you the CCID
1883                 -- In AMB mode, the Un-Accounted Distributions may not have the Code Combinations ID
1884                 ,NULL                                     trx_acc_dr_ccid
1885                 ,NULL                                     trx_acc_cr_ccid
1886                 ,tai.set_of_books_id                     trx_ledger_id
1887                 ,adj.org_id                               trx_operating_unit_id
1888                 ,NULL                                     trx_legal_entity_id
1889                 ,ajl.khr_id                               trx_khr_id
1890                 ,ajl.kle_id                               txl_asset_id
1891                 ,khr.pdt_id                               trx_pdt_id
1892                 ,ajl.sty_id                               trx_sty_id
1893                 ,222                                      trx_application_id
1894                 -- DATE Format Columns
1895                 ,adj.transaction_date                     trx_date
1896                 ,adj.gl_date                              gl_date  -- Invoice Date
1897                 -- Additional Columns
1898                 ,adj.id                                   trx_id
1899                 ,ajl.id                                   trx_txl_id
1900                 ,adj.try_id                               trx_try_id
1901                 ,rep.report_id                            trx_report_id
1902                 ,NULL                                     trx_dist_id
1903                 -- Occurance date of the Transaction, not the Transaction Effective Date
1904                 ,adj.transaction_date                     trx_occ_date
1905                 ,'Receivables - Adjustments'              source
1906           FROM  -- OKL AR Adjustment Entities
1907                 okl_trx_ar_adjsts_all_b        adj
1908                ,okl_txl_adjsts_lns_all_b       ajl
1909                ,okl_txl_ar_inv_lns_b           til
1910                ,okl_trx_ar_invoices_b          tai
1911                -- OKL Entities
1912                ,okc_k_headers_all_b            chr
1913                ,okl_k_headers                  khr
1914                ,okl_products                   pdt
1915                ,okl_ae_tmpt_sets_all           aes
1916                ,okl_st_gen_tmpt_sets_all       gts
1917                ,okl_trx_types_v                try
1918                ,okl_strm_type_v                sty
1919                -- OLM Reconciliation Report Entities
1920                ,okl_reports_b                  rep
1921                ,okl_report_trx_params          rtryp
1922                ,okl_rep_products_gt            pdt_gt
1923                -- To fetch Names
1924                ,hr_operating_units             ou
1925                ,gl_ledgers_v                   ledger
1926           WHERE -- OKL - AR Adjustment Application Predicates
1927                 adj.id     = ajl.adj_id
1928             AND ajl.til_id = til.id
1929             -- Important Predicate
1930             AND ajl.receivables_adjustment_id IS NULL
1931             -- The above predicate ensures that the OLM-AR Adjustment Trx. is not yet moved to AR even.
1932             AND til.tai_id = tai.id
1933             AND ajl.khr_id = khr.id
1934             AND chr.id     = khr.id
1935             AND khr.pdt_id = pdt.id
1936             AND pdt.aes_id = aes.id
1937             AND aes.gts_id = gts.id
1938             AND adj.try_id = try.id
1939             AND ajl.sty_id = sty.id
1940             -- Transaction should have occured in the Start and End date of the Context
1941             AND adj.transaction_date >= p_start_date
1942             AND adj.transaction_date <= p_end_date
1943             -- Add Predicates based on the report
1944             AND  rep.report_id = p_report_id
1945             AND  rtryp.report_id = rep.report_id
1946             AND  try.id = rtryp.try_id
1947             AND  nvl(nvl(rtryp.sty_id,ajl.sty_id), - 1 )  = nvl(ajl.sty_id, -1)
1948             -- Products restriction
1949             AND  pdt_gt.product_id = pdt.id
1950             -- Org., Ledger and Legal Entity Id restriction
1951             AND adj.org_id = NVL( p_org_id, adj.org_id )
1952             AND tai.set_of_books_id = p_ledger_id -- Ledger is Mandatory Input Param
1953             -- Predicates to fetch the Names
1954             AND ou.organization_id = adj.org_id
1955             AND ledger.ledger_id = tai.set_of_books_id
1956       UNION ALL
1957         -- Query Segment to find the UnAccounted Receivables Invoice Transactions from Receivables
1958         SELECT  'TRX_UNACCOUNTED'                         trx_detail_type_code
1959                 ,radj.adjustment_number                   trx_number
1960                 ,try.name                                 trx_type_name
1961                 ,xvl.name                                 trx_event_name
1962                 ,'Receivables'                            trx_application_name
1963                 ,chr.contract_number                      contract_number
1964                 ,NULL                                     asset_number
1965                 ,gts.deal_type                            book_classification -- Fetching Code need to change to fetch Meaning
1966                 ,pdt.NAME                                 product_name
1967                 ,sty.NAME                                 trx_sty_name
1968                 ,sty.styb_purpose_meaning                 trx_sty_purpose
1969                 ,tai.currency_code                        currency_code
1970                 ,NULL                                     trx_account_name
1971                 ,ou.name                                  trx_operating_unit_name
1972                 ,NULL                                     trx_legal_entity_name
1973                 ,ledger.name                              trx_ledger_name
1974                 ,rtryp.add_substract_code                 trx_activity_code
1975                 ,NULL                                     trx_period_name
1976                 -- Number Format Columns
1977                 ,NULL                                     trx_dr_amount
1978                 ,NULL                                     trx_cr_amount
1979                 ,DECODE(rtryp.add_substract_code
1980                          ,'ADD',       ajl.amount
1981                          ,'SUBTRACT',  ajl.amount * -1
1982                        )                                  trx_net_amount
1983                 -- Note that in ATS Mode only distributions can fetch you the CCID
1984                 -- In AMB mode, the Un-Accounted Distributions may not have the Code Combinations ID
1985                 ,NULL                                     trx_acc_dr_ccid
1986                 ,NULL                                     trx_acc_cr_ccid
1987                 ,radj.set_of_books_id                     trx_ledger_id
1988                 ,adj.org_id                               trx_operating_unit_id
1989                 ,NULL                                     trx_legal_entity_id
1990                 ,ajl.khr_id                               trx_khr_id
1991                 ,ajl.kle_id                               txl_asset_id
1992                 ,khr.pdt_id                               trx_pdt_id
1993                 ,ajl.sty_id                               trx_sty_id
1994                 ,222                                      trx_application_id
1995                 -- DATE Format Columns
1996                 ,adj.transaction_date                     trx_date
1997                 ,adj.gl_date                              gl_date  -- Invoice Date
1998                 -- Additional Columns
1999                 ,adj.id                                   trx_id
2000                 ,ajl.id                                   trx_txl_id
2001                 ,adj.try_id                               trx_try_id
2002                 ,rep.report_id                            trx_report_id
2003                 ,NULL                                     trx_dist_id
2004                 -- Occurance date of the Transaction, not the Transaction Effective Date
2005                 ,adj.transaction_date                     trx_occ_date
2006                 ,'Receivables - Adjustments'              source
2007           FROM  -- OKL AR Adjustment Entities
2008                 okl_trx_ar_adjsts_all_b        adj
2009                ,okl_txl_adjsts_lns_all_b       ajl
2010                ,okl_txl_ar_inv_lns_b           til
2011                ,okl_trx_ar_invoices_b          tai
2012                -- OKL Entities
2013                ,okc_k_headers_all_b            chr
2014                ,okl_k_headers                  khr
2015                ,okl_products                   pdt
2016                ,okl_ae_tmpt_sets_all           aes
2017                ,okl_st_gen_tmpt_sets_all       gts
2018                ,okl_trx_types_v                try
2019                ,okl_strm_type_v                sty
2020                -- AR Tables
2021                ,ar_adjustments_all             radj
2022                -- XLA Tables
2023                ,xla_events                     xe
2024                ,xla_event_types_vl             xvl
2025                -- OLM Reconciliation Report Entities
2026                ,okl_reports_b                  rep
2027                ,okl_report_trx_params          rtryp
2028                ,okl_rep_products_gt            pdt_gt
2029                -- To fetch Names
2030                ,hr_operating_units             ou
2031                ,gl_ledgers_v                   ledger
2032           WHERE -- OKL - AR Adjustment Application Predicates
2033                 adj.id     = ajl.adj_id
2034             AND ajl.til_id = til.id
2035             AND til.tai_id = tai.id
2036             AND ajl.khr_id = khr.id
2037             AND chr.id     = khr.id
2038             AND khr.pdt_id = pdt.id
2039             AND pdt.aes_id = aes.id
2040             AND aes.gts_id = gts.id
2041             AND adj.try_id = try.id
2042             AND ajl.sty_id = sty.id
2043             -- Transaction should have occured in the Start and End date of the Context
2044             AND adj.transaction_date >= p_start_date
2045             AND adj.transaction_date <= p_end_date
2046             -- Add Predicates based on the report
2047             AND  rep.report_id = p_report_id
2048             AND  rtryp.report_id = rep.report_id
2049             AND  try.id = rtryp.try_id
2050             AND  nvl(nvl(rtryp.sty_id,ajl.sty_id), - 1 )  = nvl(ajl.sty_id, -1)
2051             -- Products restriction
2052             AND  pdt_gt.product_id = pdt.id
2053             -- Org., Ledger and Legal Entity Id restriction
2054             AND adj.org_id = NVL( p_org_id, adj.org_id )
2055             AND radj.set_of_books_id = p_ledger_id -- Ledger is Mandatory Input Param
2056             -- Predicates to fetch the Names
2057             AND ou.organization_id = adj.org_id
2058             AND ledger.ledger_id = radj.set_of_books_id
2059             -- OKL to AR Predicates
2060             AND ajl.receivables_adjustment_id = radj.adjustment_id
2061             -- AR to XLA Relations
2062             AND radj.event_id = xe.event_id
2063             AND xe.application_id = 222
2064             AND xe.event_status_code IN ( 'U', 'I' )
2065             AND xe.application_id = xvl.application_id
2066             AND xvl.event_type_code = xe.event_type_code
2067       UNION ALL
2068         -- Query Segment to find the  Receivables Adjustment Transactions in SLA
2069         -- Only Adjustment Transactions that are not transferred to GL will be queried up
2070         --   by this segment.
2071         -- And hence the Dr/Cr CCId are from XLA only
2072         -- Important Predicate again is xh.gl_transfer_status_code <> 'Y'
2073         SELECT  'TRX_UNPOSTED'                            trx_detail_type_code
2074                 ,radj.adjustment_number                   trx_number
2075                 ,try.name                                 trx_type_name
2076                 ,xvl.name                                 trx_event_name
2077                 ,'Receivables'                            trx_application_name
2078                 ,chr.contract_number                      contract_number
2079                 ,NULL                                     asset_number
2080                 ,gts.deal_type                            book_classification -- Fetching Code need to change to fetch Meaning
2081                 ,pdt.NAME                                 product_name
2082                 ,sty.NAME                                 trx_sty_name
2083                 ,sty.styb_purpose_meaning                 trx_sty_purpose
2084                 ,tai.currency_code                        currency_code
2085                 ,NULL                                     trx_account_name
2086                 ,ou.name                                  trx_operating_unit_name
2087                 ,NULL                                     trx_legal_entity_name
2088                 ,ledger.name                              trx_ledger_name
2089                 ,rtryp.add_substract_code                 trx_activity_code
2090                 ,NULL                                     trx_period_name
2091                 -- Number Format Columns
2092                 ,xl.entered_dr                            trx_dr_amount
2093                 ,xl.entered_cr                            trx_cr_amount
2094                 ,DECODE(rtryp.add_substract_code
2095                          ,'ADD',      NVL(xl.entered_dr, xl.entered_cr)
2096                          ,'SUBTRACT', NVL(xl.entered_dr, xl.entered_cr) * -1
2097                        )                                  trx_net_amount
2098                 -- Its safe to fetch the CCID from XLA itself instead of the OKL Dist. table
2099                 ,DECODE(xl.entered_cr
2100                          ,NULL, xl.code_combination_id    -- When Credit Amount is Null, it means Debit Distribution
2101                          ,NULL
2102                        )                                  trx_acc_dr_ccid
2103                 ,DECODE(xl.entered_dr
2104                          ,NULL, xl.code_combination_id    -- When Debit Amount is Null, it means Credit Distribution
2105                          ,NULL
2106                        )                                  trx_acc_cr_ccid
2107                 ,radj.set_of_books_id                     trx_ledger_id
2108                 ,adj.org_id                               trx_operating_unit_id
2109                 ,NULL                                     trx_legal_entity_id
2110                 ,ajl.khr_id                               trx_khr_id
2111                 ,ajl.kle_id                               txl_asset_id
2112                 ,khr.pdt_id                               trx_pdt_id
2113                 ,ajl.sty_id                               trx_sty_id
2114                 ,222                                      trx_application_id
2115                 -- DATE Format Columns
2116                 ,adj.transaction_date                     trx_date
2117                 ,adj.gl_date                              gl_date  -- Invoice Date
2118                 -- Additional Columns
2119                 ,adj.id                                   trx_id
2120                 ,ajl.id                                   trx_txl_id
2121                 ,adj.try_id                               trx_try_id
2122                 ,rep.report_id                            trx_report_id
2123                 ,NULL                                     trx_dist_id
2124                 -- Occurance date of the Transaction, not the Transaction Effective Date
2125                 ,adj.transaction_date                     trx_occ_date
2126                 ,'Receivables - Adjustments'              source
2127           FROM  -- OKL AR Adjustment Entities
2128                 okl_trx_ar_adjsts_all_b        adj
2129                ,okl_txl_adjsts_lns_all_b       ajl
2130                ,okl_txl_ar_inv_lns_b           til
2131                ,okl_trx_ar_invoices_b          tai
2132                -- OKL Entities
2133                ,okc_k_headers_all_b            chr
2134                ,okl_k_headers                  khr
2135                ,okl_products                   pdt
2136                ,okl_ae_tmpt_sets_all           aes
2137                ,okl_st_gen_tmpt_sets_all       gts
2138                ,okl_trx_types_v                try
2139                ,okl_strm_type_v                sty
2140                -- AR Tables
2141                ,ar_adjustments_all             radj
2142                ,ar_distributions_all           rdist
2143                -- OLM Reconciliation Report Entities
2144                ,okl_reports_b                  rep
2145                ,okl_report_trx_params          rtryp
2146                ,okl_rep_products_gt            pdt_gt
2147                -- To fetch Names
2148                ,hr_operating_units             ou
2149                ,gl_ledgers_v                   ledger
2150                -- XLA Tables
2151                ,xla_events                     xe
2152                ,xla_event_types_vl             xvl
2153                ,xla_distribution_links         xd
2154                ,xla_ae_headers                 xh
2155                ,xla_ae_lines                   xl
2156           WHERE -- OKL - AR Adjustment Application Predicates
2157                 adj.id     = ajl.adj_id
2158             AND ajl.til_id = til.id
2159             AND til.tai_id = tai.id
2160             AND ajl.khr_id = khr.id
2161             AND chr.id     = khr.id
2162             AND khr.pdt_id = pdt.id
2163             AND pdt.aes_id = aes.id
2164             AND aes.gts_id = gts.id
2165             AND adj.try_id = try.id
2166             AND ajl.sty_id = sty.id
2167             -- Transaction should have occured in the Start and End date of the Context
2168             AND adj.transaction_date >= p_start_date
2169             AND adj.transaction_date <= p_end_date
2170             -- Add Predicates based on the report
2171             AND  rep.report_id = p_report_id
2172             AND  rtryp.report_id = rep.report_id
2173             AND  try.id = rtryp.try_id
2174             AND  nvl(nvl(rtryp.sty_id,ajl.sty_id), - 1 )  = nvl(ajl.sty_id, -1)
2175             -- Products restriction
2176             AND  pdt_gt.product_id = pdt.id
2177             -- Org., Ledger and Legal Entity Id restriction
2178             AND adj.org_id = NVL( p_org_id, adj.org_id )
2179             AND radj.set_of_books_id = p_ledger_id -- Ledger is Mandatory Input Param
2180             -- Predicates to fetch the Names
2181             AND ou.organization_id = adj.org_id
2182             AND ledger.ledger_id = radj.set_of_books_id
2183             -- OKL to AR Predicates
2184             AND ajl.receivables_adjustment_id = radj.adjustment_id
2185             AND radj.adjustment_id = rdist.source_id
2186             AND rdist.source_type = 'ADJ'  -- Assumption
2187             AND rdist.source_table = 'ADJ' -- Assumption
2188             -- AR to XLA Relations
2189             AND radj.event_id = xe.event_id
2190             AND xe.application_id = 222
2191             AND xe.event_status_code IN ( 'U', 'I' )
2192             AND xe.application_id = xvl.application_id
2193             AND xvl.event_type_code = xe.event_type_code
2194             AND xd.event_id = radj.event_id
2195             AND xd.application_id = 222
2196             AND xd.source_distribution_type = 'AR_DISTRIBUTIONS_ALL'
2197             AND xd.source_distribution_id_num_1 = rdist.line_id
2198             AND xd.ae_header_id = xh.ae_header_id
2199             AND xl.ae_header_id = xh.ae_header_id
2200             AND xl.ledger_id = p_ledger_id
2201             AND xd.ae_line_num = xl.ae_line_num --XD,XH,XL END
2202             -- Important Predicate: XLA Distribution Links should not have the GL Import Link ID
2203             AND  xh.gl_transfer_status_code <> 'Y' -- Not Imported to GL Yet
2204             -- Restrict to only one Distribution Line based on the Transaction Activity Code Add/Substract
2205             -- If its Add Consider only Debit, else if its Substract consider Credit
2206             AND
2207             (
2208               DECODE(xl.entered_cr, NULL, 'DEBIT_DIST', 'CREDIT_DIST' ) =
2209                    DECODE(rtryp.add_substract_code,
2210                    'ADD', 'DEBIT_DIST', 'SUBTRACT', 'CREDIT_DIST' )
2211               OR
2212               DECODE(xl.entered_dr, NULL, 'CREDIT_DIST', 'DEBIT_DIST' ) =
2213                   DECODE(rtryp.add_substract_code,
2214                   'ADD', 'DEBIT_DIST', 'SUBTRACT', 'CREDIT_DIST' )
2215             )
2216       UNION ALL
2217         -- Query Segment to find the  Receivables Adjustment Transactions in GL#
2218         -- Note that: These Adjustment Transactions may or may not be Posted in GL#,
2219         --  but got imported in GL# though.
2220         SELECT  DECODE( gh.status
2221                          ,'U',  'TRX_UNPOSTED'  -- Unposted Transactions
2222                          ,'P',  'TRX_POSTED'    -- Posted Transactions
2223                      )                                    trx_detail_type_code
2224                 ,radj.adjustment_number                   trx_number
2225                 ,try.name                                 trx_type_name
2226                 ,xvl.name                                 trx_event_name
2227                 ,'Receivables'                            trx_application_name
2228                 ,chr.contract_number                      contract_number
2229                 ,NULL                                     asset_number
2230                 ,gts.deal_type                            book_classification -- Fetching Code need to change to fetch Meaning
2231                 ,pdt.NAME                                 product_name
2232                 ,sty.NAME                                 trx_sty_name
2233                 ,sty.styb_purpose_meaning                 trx_sty_purpose
2234                 ,tai.currency_code                        currency_code
2235                 ,NULL                                     trx_account_name
2236                 ,ou.name                                  trx_operating_unit_name
2237                 ,NULL                                     trx_legal_entity_name
2238                 ,ledger.name                              trx_ledger_name
2239                 ,rtryp.add_substract_code                 trx_activity_code
2240                 ,NULL                                     trx_period_name
2241                 -- Number Format Columns
2242                 ,xl.entered_dr                            trx_dr_amount
2243                 ,xl.entered_cr                            trx_cr_amount
2244                 ,DECODE(rtryp.add_substract_code
2245                          ,'ADD',      NVL(xl.entered_dr, xl.entered_cr)
2246                          ,'SUBTRACT', NVL(xl.entered_dr, xl.entered_cr) * -1
2247                        )                                  trx_net_amount
2248                 -- Its safe to fetch the CCID from XLA itself instead of the OKL Dist. table
2249                 ,DECODE(xl.entered_cr
2250                          ,NULL, xl.code_combination_id    -- When Credit Amount is Null, it means Debit Distribution
2251                          ,NULL
2252                        )                                  trx_acc_dr_ccid
2253                 ,DECODE(xl.entered_dr
2254                          ,NULL, xl.code_combination_id    -- When Debit Amount is Null, it means Credit Distribution
2255                          ,NULL
2256                        )                                  trx_acc_cr_ccid
2257                 ,radj.set_of_books_id                     trx_ledger_id
2258                 ,adj.org_id                               trx_operating_unit_id
2259                 ,NULL                                     trx_legal_entity_id
2260                 ,ajl.khr_id                               trx_khr_id
2261                 ,ajl.kle_id                               txl_asset_id
2262                 ,khr.pdt_id                               trx_pdt_id
2263                 ,ajl.sty_id                               trx_sty_id
2264                 ,222                                      trx_application_id
2265                 -- DATE Format Columns
2266                 ,adj.transaction_date                     trx_date
2267                 ,adj.gl_date                              gl_date  -- Invoice Date
2268                 -- Additional Columns
2269                 ,adj.id                                   trx_id
2270                 ,ajl.id                                   trx_txl_id
2271                 ,adj.try_id                               trx_try_id
2272                 ,rep.report_id                            trx_report_id
2273                 ,NULL                                     trx_dist_id
2274                 -- Occurance date of the Transaction, not the Transaction Effective Date
2275                 ,adj.transaction_date                     trx_occ_date
2276                 ,'Receivables - Adjustments'              source
2277           FROM  -- OKL AR Adjustment Entities
2278                 okl_trx_ar_adjsts_all_b        adj
2279                ,okl_txl_adjsts_lns_all_b       ajl
2280                ,okl_txl_ar_inv_lns_b           til
2281                ,okl_trx_ar_invoices_b          tai
2282                -- OKL Entities
2283                ,okc_k_headers_all_b            chr
2284                ,okl_k_headers                  khr
2285                ,okl_products                   pdt
2286                ,okl_ae_tmpt_sets_all           aes
2287                ,okl_st_gen_tmpt_sets_all       gts
2288                ,okl_trx_types_v                try
2289                ,okl_strm_type_v                sty
2290                -- AR Tables
2291                ,ar_adjustments_all             radj
2292                ,ar_distributions_all           rdist
2293                -- OLM Reconciliation Report Entities
2294                ,okl_reports_b                  rep
2295                ,okl_report_trx_params          rtryp
2296                ,okl_rep_products_gt            pdt_gt
2297                -- To fetch Names
2298                ,hr_operating_units             ou
2299                ,gl_ledgers_v                   ledger
2300                -- XLA Tables
2301                ,xla_events                     xe
2302                ,xla_event_types_vl             xvl
2303                ,xla_distribution_links         xd
2304                ,xla_ae_headers                 xh
2305                ,xla_ae_lines                   xl
2306                -- GL Tables: Import Reference, GL Header and Lines
2307                ,gl_import_references           gi
2308                ,gl_je_headers                  gh
2309                ,gl_je_lines                    gl
2310                ,gl_code_combinations_kfv       glcc
2311           WHERE -- OKL - AR Adjustment Application Predicates
2312                 adj.id     = ajl.adj_id
2313             AND ajl.til_id = til.id
2314             AND til.tai_id = tai.id
2315             AND ajl.khr_id = khr.id
2316             AND chr.id     = khr.id
2317             AND khr.pdt_id = pdt.id
2318             AND pdt.aes_id = aes.id
2319             AND aes.gts_id = gts.id
2320             AND adj.try_id = try.id
2321             AND ajl.sty_id = sty.id
2322             -- Transaction should have occured in the Start and End date of the Context
2323             AND adj.transaction_date >= p_start_date
2324             AND adj.transaction_date <= p_end_date
2325             -- Add Predicates based on the report
2326             AND  rep.report_id = p_report_id
2327             AND  rtryp.report_id = rep.report_id
2328             AND  try.id = rtryp.try_id
2329             AND  nvl(nvl(rtryp.sty_id,ajl.sty_id), - 1 )  = nvl(ajl.sty_id, -1)
2330             -- Products restriction
2331             AND  pdt_gt.product_id = pdt.id
2332             -- Org., Ledger and Legal Entity Id restriction
2333             AND adj.org_id = NVL( p_org_id, adj.org_id )
2334             AND radj.set_of_books_id = p_ledger_id -- Ledger is Mandatory Input Param
2335             -- Predicates to fetch the Names
2336             AND ou.organization_id = adj.org_id
2337             AND ledger.ledger_id = radj.set_of_books_id
2338             -- OKL to AR Predicates
2339             AND ajl.receivables_adjustment_id = radj.adjustment_id
2340             AND radj.adjustment_id = rdist.source_id
2341             AND rdist.source_type = 'ADJ'  -- Assumption
2342             AND rdist.source_table = 'ADJ' -- Assumption
2343             -- AR to XLA Relations
2344             AND radj.event_id = xe.event_id
2345             AND xe.application_id = 222
2346             AND xe.event_status_code IN ( 'U', 'I' )
2347             AND xe.application_id = xvl.application_id
2348             AND xvl.event_type_code = xe.event_type_code
2349             AND xd.event_id = radj.event_id
2350             AND xd.application_id = 222
2351             AND xd.source_distribution_type = 'AR_DISTRIBUTIONS_ALL'
2352             AND xd.source_distribution_id_num_1 = rdist.line_id
2353             AND xd.ae_header_id = xh.ae_header_id
2354             AND xl.ae_header_id = xh.ae_header_id
2355             AND xl.ledger_id = p_ledger_id
2356             AND xd.ae_line_num = xl.ae_line_num --XD,XH,XL END
2357             -- Restrict to only one Distribution Line based on the Transaction Activity Code Add/Substract
2358             -- If its Add Consider only Debit, else if its Substract consider Credit
2359             AND
2360             (
2361               DECODE(xl.entered_cr, NULL, 'DEBIT_DIST', 'CREDIT_DIST' ) =
2362                    DECODE(rtryp.add_substract_code,
2363                    'ADD', 'DEBIT_DIST', 'SUBTRACT', 'CREDIT_DIST' )
2364               OR
2365               DECODE(xl.entered_dr, NULL, 'CREDIT_DIST', 'DEBIT_DIST' ) =
2366                   DECODE(rtryp.add_substract_code,
2367                   'ADD', 'DEBIT_DIST', 'SUBTRACT', 'CREDIT_DIST' )
2368             )
2369             -- From SLA to GL Tables Link
2370             -- Important Predicate: XLA Distribution Links should have the GL Import Link ID
2371             AND  gi.gl_sl_link_id = xl.gl_sl_link_id --GL TABLES JOIN START
2372             AND  gl.ledger_id = xl.ledger_id
2373             AND  gi.gl_sl_link_table = xl.gl_sl_link_table
2374             AND  gi.je_header_id = gh.je_header_id
2375             AND  gh.je_header_id = gl.je_header_id
2376             AND  gi.je_line_num = gl.je_line_num
2377             --  AND  gl.code_combination_id = cc.ccid
2378             AND  gh.je_source = 'Receivables'
2379             -- Important Predicate: gl_je_headers.status can be either Posted
2380             -- There can be even Unposted Entries
2381             AND  gh.status IN ( 'U', 'P' ) -- Unposted or Posted Entries
2382       );
2383     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
2384       'After Inserting the Receivables Adjustment Transactions Data in GT Table End Time:'
2385       || TO_CHAR(SYSDATE, 'DD-MON-YYY HH:MM:SS') );
2386     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
2387       'Time taken for Populating Receivables Adjustment Transactions '
2388        || ( SYSDATE - l_trace_time ) * 86400 || ' Seconds' );
2389     ---------------------------------------------------------------------------
2390     -- Query Segments to Populate the Payables Invoice Transactions Data
2391     ---------------------------------------------------------------------------
2392     -- Start the stop watch
2393     l_trace_time := SYSDATE;
2394     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
2395       'Before Inserting the Payables Invoice Transactions Data in GT Table Start Time:'
2396       || TO_CHAR(l_trace_time, 'DD-MON-YYY HH:MM:SS') );
2397     INSERT INTO okl_g_reports_gt
2398                 (
2399                   -- String Formatted Columns
2400                    value1_text
2401                   ,value2_text
2402                   ,value3_text
2403                   ,value4_text
2404                   ,value5_text
2405                   ,value6_text
2406                   ,value7_text
2407                   ,value8_text
2408                   ,value9_text
2409                   ,value10_text
2410                   ,value11_text
2411                   ,value12_text
2412                   ,value13_text
2413                   ,value14_text
2414                   ,value15_text
2415                   ,value16_text
2416                   ,value17_text
2417                   ,value18_text
2418                   -- Number Formatted Columns
2419                   ,value1_num
2420                   ,value2_num
2421                   ,value3_num
2422                   ,value4_num
2423                   ,value5_num
2424                   ,value6_num
2425                   ,value7_num
2426                   ,value8_num
2427                   ,value9_num
2428                   ,value10_num
2429                   ,value11_num
2430                   ,value12_num
2431                   ,value13_num
2432                   -- Date Formatted Columns
2433                   ,value1_date
2434                   ,value2_date
2435                 )
2436       SELECT -- String Formatted Columns
2437              trx_detail_type_code        --value1_text
2438             ,trx_number                  --value2_text
2439             ,trx_type_name               --value3_text
2440             ,trx_application_name        --value4_text
2441             ,contract_number             --value5_text
2442             ,asset_number                --value6_text
2443             ,book_classification         --value7_text
2444             ,product_name                --value8_text
2445             ,trx_sty_name                --value9_text
2446             ,trx_sty_purpose             --value10_text
2447             ,currency_code               --value11_text
2448             ,trx_period_name             --value12_text -- Populated Only for Posted Entries
2449             ,trx_account_name            --value13_text
2450             ,trx_operating_unit_name     --value14_text
2451             ,trx_legal_entity_name       --value15_text
2452             ,trx_ledger_name             --value16_text
2453             ,trx_activity_code           --value17_text
2454             ,trx_event_name              --value18_text
2455             -- Number Formatted Columns
2456             ,trx_dr_amount               --value1_num
2457             ,trx_cr_amount               --value2_num
2458             ,trx_net_amount              --value3_num
2459             ,trx_acc_dr_ccid             --value4_num
2460             ,trx_acc_cr_ccid             --value5_num
2461             ,trx_ledger_id               --value6_num
2462             ,trx_operating_unit_id       --value7_num
2463             ,trx_legal_entity_id         --value8_num
2464             ,trx_khr_id                  --value9_num
2465             ,txl_asset_id                --value10_num
2466             ,trx_pdt_id                  --value11_num
2467             ,trx_sty_id                  --value12_num
2468             ,trx_application_id          --value13_num
2469             -- Date Formatted Columns
2470             ,trx_date                    --value1_date
2471             ,gl_date                     --value2_date
2472         FROM
2473        (
2474         ---------------------------------------------------------------------------------------
2475         -- Query Segment to find the OKL-AP Invoice Transactions which are not moved to AP Yet.
2476         ---------------------------------------------------------------------------------------
2477         SELECT  'TRX_UNACCOUNTED_NO_EVENT'                trx_detail_type_code
2478                 ,tap.invoice_number                       trx_number
2479                 ,try.name                                 trx_type_name
2480                 ,NULL                                     trx_event_name
2481                 ,'Payables'                               trx_application_name
2482                 ,chr.contract_number                      contract_number
2483                 ,NULL                                     asset_number
2484                 ,gts.deal_type                            book_classification -- Fetching Code need to change to fetch Meaning
2485                 ,pdt.NAME                                 product_name
2486                 ,sty.NAME                                 trx_sty_name
2487                 ,sty.styb_purpose_meaning                 trx_sty_purpose
2488                 ,tap.currency_code                        currency_code
2489                 ,NULL                                     trx_account_name
2490                 ,ou.name                                  trx_operating_unit_name
2491                 ,le.name                                  trx_legal_entity_name
2492                 ,ledger.name                              trx_ledger_name
2493                 ,rtryp.add_substract_code                 trx_activity_code
2494                 ,NULL                                     trx_period_name
2495                 -- Number Format Columns
2496                 ,NULL                                     trx_dr_amount
2497                 ,NULL                                     trx_cr_amount
2498                 ,DECODE(rtryp.add_substract_code
2499                          ,'ADD',       tpl.amount
2500                          ,'SUBTRACT',  tpl.amount * -1
2501                        )                                  trx_net_amount
2502                 -- Note that in ATS Mode only distributions can fetch you the CCID
2503                 -- In AMB mode, the Un-Accounted Distributions may not have the Code Combinations ID
2504                 ,NULL                                     trx_acc_dr_ccid
2505                 ,NULL                                     trx_acc_cr_ccid
2506                 ,tap.set_of_books_id                      trx_ledger_id
2507                 ,tap.org_id                               trx_operating_unit_id
2508                 ,tap.legal_entity_id                      trx_legal_entity_id
2509                 ,tpl.khr_id                               trx_khr_id
2510                 ,tpl.kle_id                               txl_asset_id
2511                 ,khr.pdt_id                               trx_pdt_id
2512                 ,tpl.sty_id                               trx_sty_id
2513                 ,200                                      trx_application_id
2514                 -- DATE Format Columns
2515                 ,tap.transaction_date                     trx_date
2516                 ,NULL                                     gl_date  -- Invoice Date
2517                 -- Additional Columns
2518                 ,tap.id                                   trx_id
2519                 ,tpl.id                                   trx_txl_id
2520                 ,tap.try_id                               trx_try_id
2521                 ,rep.report_id                            trx_report_id
2522                 ,NULL                                     trx_dist_id
2523                 -- Occurance date of the Transaction, not the Transaction Effective Date
2524                 ,tap.transaction_date                     trx_occ_date
2525                 ,'Payables'                               source
2526           FROM
2527                 okl_txl_ap_inv_lns_all_b       tpl
2528                ,okl_trx_ap_invs_all_b          tap
2529                ,okc_k_headers_all_b            chr
2530                ,okl_k_headers                  khr
2531                ,okl_products                   pdt
2532                ,okl_ae_tmpt_sets_all           aes
2533                ,okl_st_gen_tmpt_sets_all       gts
2534                ,okl_trx_types_v                try
2535                ,okl_strm_type_v                sty
2536                -- OLM Reconciliation Report Entities
2537                ,okl_reports_b                  rep
2538                ,okl_report_trx_params          rtryp
2539                ,okl_rep_products_gt            pdt_gt
2540                -- To fetch Names
2541                ,hr_operating_units             ou
2542                ,xle_entity_profiles            le
2543                ,gl_ledgers_v                   ledger
2544           WHERE
2545                 -- OKL to AP Application Predicates
2546                 tpl.tap_id = tap.id
2547             --  Restrict to only transactions which are not yet moved to AP
2548             -- Observation: Only Funding requests need to be in APPROVED status
2549             -- For others Entered and Approved can be used for processing to move to AP
2550             AND (
2551                   (  tap.FUNDING_TYPE_CODE IS NULL AND
2552                        NVL(tap.trx_status_code, 'ENTERED') IN ( 'ENTERED', 'APPROVED' ) )
2553                   OR
2554                   (  tap.FUNDING_TYPE_CODE IS NOT NULL AND
2555                        NVL(tap.trx_status_code, 'APPROVED') in ( 'APPROVED')
2556                   )
2557                 )
2558             AND tpl.khr_id = khr.id
2559             AND chr.id     = khr.id
2560             AND khr.pdt_id = pdt.id
2561             AND pdt.aes_id = aes.id
2562             AND aes.gts_id = gts.id
2563             AND tap.try_id = try.id
2564             AND tpl.sty_id = sty.id
2565             -- Transaction should have occured in the Start and End date of the Context
2566             AND tap.transaction_date >= p_start_date
2567             AND tap.transaction_date <= p_end_date
2568             -- Add Predicates based on the report
2569             AND  rep.report_id = p_report_id
2570             AND  rtryp.report_id = rep.report_id
2571             AND  try.id = rtryp.try_id
2572             AND  nvl(nvl(rtryp.sty_id,tpl.sty_id), - 1 )  = nvl(tpl.sty_id, -1)
2573             -- Products restriction
2574             AND  pdt_gt.product_id = pdt.id
2575             -- Org., Ledger and Legal Entity Id restriction
2576             AND tap.org_id = NVL( p_org_id, tap.org_id )
2577             AND tap.legal_entity_id = NVL(p_le_id, tap.legal_entity_id )
2578             AND tap.set_of_books_id = p_ledger_id -- Ledger is Mandatory Input Param
2579             -- Predicates to fetch the Names
2580             AND ou.organization_id = tap.org_id
2581             AND le.legal_entity_id = tap.legal_entity_id
2582             AND ledger.ledger_id = tap.set_of_books_id
2583      UNION ALL
2584         -- Query Segment to find the UnAccounted Payables Invoice Transactions from Payables
2585         --
2586         -- Logic: Starting from okl_txl_ap lines go to the Invoice Line
2587         --        in AP and its Invoice Distributions. Invoice Distributions stamps the
2588         --        Accounting Event Id.
2589         -- Assumption: For a Given OKL AP Inv Lines, we assume that there is a one-one mapping
2590         --             to Invoice Line and to its Distribution.
2591         -- The Potential issue may be with the above assumption itself, as we found
2592         -- multiple Distributions for a given Invoice Line Id. Hence, used the
2593         -- LINE_TYPE_LOOKUP_CODE = 'ITEM' predicate. Not sure about this though !!
2594         SELECT  'TRX_UNACCOUNTED'                         trx_detail_type_code
2595                 ,inv.invoice_num                          trx_number
2596                 ,try.name                                 trx_type_name
2597                 ,xvl.name                                 trx_event_name
2598                 ,'Payables'                               trx_application_name
2599                 ,chr.contract_number                      contract_number
2600                 ,NULL                                     asset_number
2601                 ,gts.deal_type                            book_classification -- Fetching Code need to change to fetch Meaning
2602                 ,pdt.NAME                                 product_name
2603                 ,sty.NAME                                 trx_sty_name
2604                 ,sty.styb_purpose_meaning                 trx_sty_purpose
2605                 ,tap.currency_code                        currency_code
2606                 ,NULL                                     trx_account_name
2607                 ,ou.name                                  trx_operating_unit_name
2608                 ,le.name                                  trx_legal_entity_name
2609                 ,ledger.name                              trx_ledger_name
2610                 ,rtryp.add_substract_code                 trx_activity_code
2611                 ,NULL                                     trx_period_name
2612                 -- Number Format Columns
2613                 ,NULL                                     trx_dr_amount
2614                 ,NULL                                     trx_cr_amount
2615                 ,DECODE(rtryp.add_substract_code
2616                          ,'ADD',       tpl.amount
2617                          ,'SUBTRACT',  tpl.amount * -1
2618                        )                                  trx_net_amount
2619                 -- Note that in ATS Mode only distributions can fetch you the CCID
2620                 -- In AMB mode, the Un-Accounted Distributions may not have the Code Combinations ID
2621                 ,NULL                                     trx_acc_dr_ccid
2622                 ,NULL                                     trx_acc_cr_ccid
2623                 ,tap.set_of_books_id                      trx_ledger_id
2624                 ,tap.org_id                               trx_operating_unit_id
2625                 ,tap.legal_entity_id                      trx_legal_entity_id
2626                 ,tpl.khr_id                               trx_khr_id
2627                 ,tpl.kle_id                               txl_asset_id
2628                 ,khr.pdt_id                               trx_pdt_id
2629                 ,tpl.sty_id                               trx_sty_id
2630                 ,200                                      trx_application_id
2631                 -- DATE Format Columns
2632                 ,tap.transaction_date                     trx_date
2633                 ,inv.invoice_date                         gl_date  -- Invoice Date
2634                 -- Additional Columns
2635                 ,tap.id                                   trx_id
2636                 ,tpl.id                                   trx_txl_id
2637                 ,tap.try_id                               trx_try_id
2638                 ,rep.report_id                            trx_report_id
2639                 ,invdist.invoice_distribution_id          trx_dist_id
2640                 -- Occurance date of the Transaction, not the Transaction Effective Date
2641                 ,tap.transaction_date                     trx_occ_date
2642                 ,'Payables'                               source
2643           FROM
2644                 okl_txl_ap_inv_lns_all_b       tpl
2645                ,okl_trx_ap_invs_all_b          tap
2646                ,okc_k_headers_all_b            chr
2647                ,okl_k_headers                  khr
2648                ,okl_products                   pdt
2649                ,okl_ae_tmpt_sets_all           aes
2650                ,okl_st_gen_tmpt_sets_all       gts
2651                ,okl_trx_types_v                try
2652                ,okl_strm_type_v                sty
2653                -- AP Tables
2654                ,ap_invoices_all                inv
2655                ,ap_invoice_lines_all           lin
2656                ,ap_invoice_distributions_all   invdist
2657                -- XLA Tables
2658                ,xla_events                     xe
2659                ,xla_event_types_vl             xvl
2660                -- OLM Reconciliation Report Entities
2661                ,okl_reports_b                  rep
2662                ,okl_report_trx_params          rtryp
2663                ,okl_rep_products_gt            pdt_gt
2664                -- To fetch Names
2665                ,hr_operating_units             ou
2666                ,xle_entity_profiles            le
2667                ,gl_ledgers_v                   ledger
2668           WHERE
2669                 -- OKL to AP Application Predicates
2670                 tpl.tap_id = tap.id
2671             AND tpl.khr_id = khr.id
2672             AND chr.id     = khr.id
2673             AND khr.pdt_id = pdt.id
2674             AND pdt.aes_id = aes.id
2675             AND aes.gts_id = gts.id
2676             AND tap.try_id = try.id
2677             AND tpl.sty_id = sty.id
2678             -- Transaction should have occured in the Start and End date of the Context
2679             AND tap.transaction_date >= p_start_date
2680             AND tap.transaction_date <= p_end_date
2681             -- Add Predicates based on the report
2682             AND  rep.report_id = p_report_id
2683             AND  rtryp.report_id = rep.report_id
2684             AND  try.id = rtryp.try_id
2685             AND  nvl(nvl(rtryp.sty_id,tpl.sty_id), - 1 )  = nvl(tpl.sty_id, -1)
2686             -- Products restriction
2687             AND  pdt_gt.product_id = pdt.id
2688             -- Org., Ledger and Legal Entity Id restriction
2689             AND tap.org_id = NVL( p_org_id, tap.org_id )
2690             AND tap.legal_entity_id = NVL(p_le_id, tap.legal_entity_id )
2691             AND tap.set_of_books_id = p_ledger_id -- Ledger is Mandatory Input Param
2692             -- Predicates to fetch the Names
2693             AND ou.organization_id = tap.org_id
2694             AND le.legal_entity_id = tap.legal_entity_id
2695             AND ledger.ledger_id = tap.set_of_books_id
2696             -- OKL to AP Invoice Lines Predicates
2697             AND invdist.line_type_lookup_code = 'ITEM' -- Need to verify
2698             AND invdist.invoice_id = lin.invoice_id
2699             AND invdist.invoice_line_number = lin.line_number
2700             AND lin.application_id = 540
2701             AND lin.product_table = 'OKL_TXL_AP_INV_LNS_ALL_B'
2702             AND lin.reference_key1 = tpl.id
2703             AND lin.invoice_id = inv.invoice_id
2704             -- AP to XLA Relations
2705            AND  invdist.accounting_event_id = xe.event_id
2706            AND  xe.application_id = 200 -- Payables
2707            AND  xe.event_status_code IN ( 'U', 'I' ) -- Un Accounted Or Errored out
2708            AND xe.application_id = xvl.application_id
2709            AND xvl.event_type_code = xe.event_type_code
2710 
2711       UNION ALL
2712         -- Query Segment to find the Unposted Payables Invoice Transactions from Payables
2713         --
2714         -- Logic: Starting from okl_txl_ap lines go to the Invoice Line
2715         --        in AP and its Invoice Distributions. Invoice Distributions stamps the
2716         --        Accounting Event Id.
2717         -- Assumption: For a Given OKL AP Inv Lines, we assume that there is a one-one mapping
2718         --             to Invoice Line and to its Distribution.
2719         -- The Potential issue may be with the above assumption itself, as we found
2720         -- multiple Distributions for a given Invoice Line Id. Hence, used the
2721         -- LINE_TYPE_LOOKUP_CODE = 'ITEM' predicate. Not sure about this though !!
2722         -- Important Predicate: xh.gl_transfer_status_code <> 'Y'
2723         SELECT  'TRX_UNPOSTED'                            trx_detail_type_code
2724                 ,inv.invoice_num                          trx_number
2725                 ,try.name                                 trx_type_name
2726                 ,xvl.name                                 trx_event_name
2727                 ,'Payables'                               trx_application_name
2728                 ,chr.contract_number                      contract_number
2729                 ,NULL                                     asset_number
2730                 ,gts.deal_type                            book_classification -- Fetching Code need to change to fetch Meaning
2731                 ,pdt.NAME                                 product_name
2732                 ,sty.NAME                                 trx_sty_name
2733                 ,sty.styb_purpose_meaning                 trx_sty_purpose
2734                 ,tap.currency_code                        currency_code
2735                 ,NULL                                     trx_account_name
2736                 ,ou.name                                  trx_operating_unit_name
2737                 ,le.name                                  trx_legal_entity_name
2738                 ,ledger.name                              trx_ledger_name
2739                 ,rtryp.add_substract_code                 trx_activity_code
2740                 ,NULL                                     trx_period_name
2741                 -- Number Format Columns
2742                 ,xl.entered_dr                            trx_dr_amount
2743                 ,xl.entered_cr                            trx_cr_amount
2744                 ,DECODE(rtryp.add_substract_code
2745                          ,'ADD',      tpl.amount
2746                          ,'SUBTRACT', tpl.amount * -1
2747                        )                                  trx_net_amount
2748                 -- Its safe to fetch the CCID from XLA itself instead of the OKL Dist. table
2749                 ,DECODE(xl.entered_cr
2750                          ,NULL, xl.code_combination_id    -- When Credit Amount is Null, it means Debit Distribution
2751                          ,NULL
2752                        )                                  trx_acc_dr_ccid
2753                 ,DECODE(xl.entered_dr
2754                          ,NULL, xl.code_combination_id    -- When Debit Amount is Null, it means Credit Distribution
2755                          ,NULL
2756                        )                                  trx_acc_cr_ccid
2757                 ,tap.set_of_books_id                      trx_ledger_id
2758                 ,tap.org_id                               trx_operating_unit_id
2759                 ,tap.legal_entity_id                      trx_legal_entity_id
2760                 ,tpl.khr_id                               trx_khr_id
2761                 ,tpl.kle_id                               txl_asset_id
2762                 ,khr.pdt_id                               trx_pdt_id
2763                 ,tpl.sty_id                               trx_sty_id
2764                 ,200                                      trx_application_id
2765                 -- DATE Format Columns
2766                 ,tap.transaction_date                     trx_date
2767                 ,inv.invoice_date                         gl_date  -- Invoice Date
2768                 -- Additional Columns
2769                 ,tap.id                                   trx_id
2770                 ,tpl.id                                   trx_txl_id
2771                 ,tap.try_id                               trx_try_id
2772                 ,rep.report_id                            trx_report_id
2773                 ,invdist.invoice_distribution_id          trx_dist_id
2774                 -- Occurance date of the Transaction, not the Transaction Effective Date
2775                 ,tap.transaction_date                     trx_occ_date
2776                 ,'Payables'                               source
2777           FROM
2778                 okl_txl_ap_inv_lns_all_b       tpl
2779                ,okl_trx_ap_invs_all_b          tap
2780                ,okc_k_headers_all_b            chr
2781                ,okl_k_headers                  khr
2782                ,okl_products                   pdt
2783                ,okl_ae_tmpt_sets_all           aes
2784                ,okl_st_gen_tmpt_sets_all       gts
2785                ,okl_trx_types_v                try
2786                ,okl_strm_type_v                sty
2787                -- AP Tables
2788                ,ap_invoices_all                inv
2789                ,ap_invoice_lines_all           lin
2790                ,ap_invoice_distributions_all   invdist
2791                -- XLA Tables
2792                ,xla_events                     xe
2793                ,xla_event_types_vl             xvl
2794                ,xla_distribution_links         xd
2795                ,xla_ae_headers                 xh
2796                ,xla_ae_lines                   xl
2797                -- OLM Reconciliation Report Entities
2798                ,okl_reports_b                  rep
2799                ,okl_report_trx_params          rtryp
2800                ,okl_rep_products_gt            pdt_gt
2801                -- To fetch Names
2802                ,hr_operating_units             ou
2803                ,xle_entity_profiles            le
2804                ,gl_ledgers_v                   ledger
2805           WHERE
2806                 -- OKL to AP Application Predicates
2807                 tpl.tap_id = tap.id
2808             AND tpl.khr_id = khr.id
2809             AND chr.id     = khr.id
2810             AND khr.pdt_id = pdt.id
2811             AND pdt.aes_id = aes.id
2812             AND aes.gts_id = gts.id
2813             AND tap.try_id = try.id
2814             AND tpl.sty_id = sty.id
2815             -- Transaction should have occured in the Start and End date of the Context
2816             AND tap.transaction_date >= p_start_date
2817             AND tap.transaction_date <= p_end_date
2818             -- Add Predicates based on the report
2819             AND  rep.report_id = p_report_id
2820             AND  rtryp.report_id = rep.report_id
2821             AND  try.id = rtryp.try_id
2822             AND  nvl(nvl(rtryp.sty_id,tpl.sty_id), - 1 )  = nvl(tpl.sty_id, -1)
2823             -- Products restriction
2824             AND  pdt_gt.product_id = pdt.id
2825             -- Org., Ledger and Legal Entity Id restriction
2826             AND tap.org_id = NVL( p_org_id, tap.org_id )
2827             AND tap.legal_entity_id = NVL(p_le_id, tap.legal_entity_id )
2828             AND tap.set_of_books_id = p_ledger_id -- Ledger is Mandatory Input Param
2829             -- Predicates to fetch the Names
2830             AND ou.organization_id = tap.org_id
2831             AND le.legal_entity_id = tap.legal_entity_id
2832             AND ledger.ledger_id = tap.set_of_books_id
2833             -- OKL to AP Invoice Lines Predicates
2834             AND invdist.line_type_lookup_code = 'ITEM' -- Need to verify
2835             AND invdist.invoice_id = lin.invoice_id
2836             AND invdist.invoice_line_number = lin.line_number
2837             AND lin.application_id = 540
2838             AND lin.product_table = 'OKL_TXL_AP_INV_LNS_ALL_B'
2839             AND lin.reference_key1 = tpl.id
2840             AND lin.invoice_id = inv.invoice_id
2841             -- AP to XLA Relations
2842             AND  invdist.accounting_event_id = xe.event_id
2843             AND  xe.application_id = 200 -- Payables
2844             AND  xe.event_status_code = 'P' -- Pick Only processed XLA Events
2845             AND xe.application_id = xvl.application_id
2846             AND xvl.event_type_code = xe.event_type_code
2847             AND  xd.event_id = xe.event_id
2848             AND  xd.application_id = 200 -- Payables Application
2849             AND  xd.source_distribution_id_num_1 = invdist.invoice_distribution_id
2850             AND  xd.source_distribution_type = 'AP_INV_DIST'
2851             AND  xd.ae_header_id = xh.ae_header_id
2852             AND  xl.ae_header_id = xh.ae_header_id
2853             AND  xl.ledger_id = p_ledger_id
2854             AND  xd.ae_line_num = xl.ae_line_num --XD,XH,XL END
2855             -- Important Predicate: XLA Distribution Links should not have the GL Import Link ID
2856             AND  xh.gl_transfer_status_code <> 'Y' -- Not Imported to GL Yet
2857             -- Restrict to only one Distribution Line based on the Transaction Activity Code Add/Substract
2858             -- If its Add Consider only Debit, else if its Substract consider Credit
2859             AND
2860             (
2861               DECODE(xl.entered_cr, NULL, 'DEBIT_DIST', 'CREDIT_DIST' ) =
2862                    DECODE(rtryp.add_substract_code,
2863                    'ADD', 'DEBIT_DIST', 'SUBTRACT', 'CREDIT_DIST' )
2864               OR
2865               DECODE(xl.entered_dr, NULL, 'CREDIT_DIST', 'DEBIT_DIST' ) =
2866                   DECODE(rtryp.add_substract_code,
2867                   'ADD', 'DEBIT_DIST', 'SUBTRACT', 'CREDIT_DIST' )
2868            )
2869       UNION ALL
2870         -- Query Segment to find the Imported Payables Invoice Transactions from GL
2871         -- Note that Imported means either Posted or not posted.
2872         -- Logic: Starting from okl_txl_ap lines go to the Invoice Line
2873         --        in AP and its Invoice Distributions. Invoice Distributions stamps the
2874         --        Accounting Event Id.
2875         -- Assumption: For a Given OKL AP Inv Lines, we assume that there is a one-one mapping
2876         --             to Invoice Line and to its Distribution.
2877         -- The Potential issue may be with the above assumption itself, as we found
2878         -- multiple Distributions for a given Invoice Line Id. Hence, used the
2879         -- LINE_TYPE_LOOKUP_CODE = 'ITEM' predicate. Not sure about this though !!
2880         -- Important Predicate: xl.gl_sl_link_id = gl.gl_sl_link_id
2881         SELECT  DECODE( gh.status
2882                          ,'U',  'TRX_UNPOSTED'  -- Unposted Transactions
2883                          ,'P',  'TRX_POSTED'    -- Posted Transactions
2884                      )                                    trx_detail_type_code
2885                 ,inv.invoice_num                          trx_number
2886                 ,try.name                                 trx_type_name
2887                 ,xvl.name                                 trx_event_name
2888                 ,'Payables'                               trx_application_name
2889                 ,chr.contract_number                      contract_number
2890                 ,NULL                                     asset_number
2891                 ,gts.deal_type                            book_classification -- Fetching Code need to change to fetch Meaning
2892                 ,pdt.NAME                                 product_name
2893                 ,sty.NAME                                 trx_sty_name
2894                 ,sty.styb_purpose_meaning                 trx_sty_purpose
2895                 ,tap.currency_code                        currency_code
2896                 ,NULL                                     trx_account_name
2897                 ,ou.name                                  trx_operating_unit_name
2898                 ,le.name                                  trx_legal_entity_name
2899                 ,ledger.name                              trx_ledger_name
2900                 ,rtryp.add_substract_code                 trx_activity_code
2901                 ,gh.period_name                           trx_period_name
2902                 -- Number Format Columns
2903                 ,xl.entered_dr                            trx_dr_amount
2904                 ,xl.entered_cr                            trx_cr_amount
2905                 ,DECODE(rtryp.add_substract_code
2906                          ,'ADD',      tpl.amount
2907                          ,'SUBTRACT', tpl.amount * -1
2908                        )                                  trx_net_amount
2909                 -- Its safe to fetch the CCID from XLA itself instead of the OKL Dist. table
2910                 ,DECODE(xl.entered_cr
2911                          ,NULL, xl.code_combination_id    -- When Credit Amount is Null, it means Debit Distribution
2912                          ,NULL
2913                        )                                  trx_acc_dr_ccid
2914                 ,DECODE(xl.entered_dr
2915                          ,NULL, xl.code_combination_id    -- When Debit Amount is Null, it means Credit Distribution
2916                          ,NULL
2917                        )                                  trx_acc_cr_ccid
2918                 ,tap.set_of_books_id                      trx_ledger_id
2919                 ,tap.org_id                               trx_operating_unit_id
2920                 ,tap.legal_entity_id                      trx_legal_entity_id
2921                 ,tpl.khr_id                               trx_khr_id
2922                 ,tpl.kle_id                               txl_asset_id
2923                 ,khr.pdt_id                               trx_pdt_id
2924                 ,tpl.sty_id                               trx_sty_id
2925                 ,200                                      trx_application_id
2926                 -- DATE Format Columns
2927                 ,tap.transaction_date                     trx_date
2928                 ,inv.invoice_date                         gl_date  -- Invoice Date
2929                 -- Additional Columns
2930                 ,tap.id                                   trx_id
2931                 ,tpl.id                                   trx_txl_id
2932                 ,tap.try_id                               trx_try_id
2933                 ,rep.report_id                            trx_report_id
2934                 ,invdist.invoice_distribution_id          trx_dist_id
2935                 -- Occurance date of the Transaction, not the Transaction Effective Date
2936                 ,tap.transaction_date                     trx_occ_date
2937                 ,'Payables'                               source
2938           FROM
2939                 okl_txl_ap_inv_lns_all_b       tpl
2940                ,okl_trx_ap_invs_all_b          tap
2941                ,okc_k_headers_all_b            chr
2942                ,okl_k_headers                  khr
2943                ,okl_products                   pdt
2944                ,okl_ae_tmpt_sets_all           aes
2945                ,okl_st_gen_tmpt_sets_all       gts
2946                ,okl_trx_types_v                try
2947                ,okl_strm_type_v                sty
2948                -- AP Tables
2949                ,ap_invoices_all                inv
2950                ,ap_invoice_lines_all           lin
2951                ,ap_invoice_distributions_all   invdist
2952                -- XLA Tables
2953                ,xla_events                     xe
2954                ,xla_event_types_vl             xvl
2955                ,xla_distribution_links         xd
2956                ,xla_ae_headers                 xh
2957                ,xla_ae_lines                   xl
2958                -- GL Tables: Import Reference, GL Header and Lines
2959                ,gl_import_references           gi
2960                ,gl_je_headers                  gh
2961                ,gl_je_lines                    gl
2962                -- OLM Reconciliation Report Entities
2963                ,okl_reports_b                  rep
2964                ,okl_report_trx_params          rtryp
2965                ,okl_rep_products_gt            pdt_gt
2966                -- To fetch Names
2967                ,hr_operating_units             ou
2968                ,xle_entity_profiles            le
2969                ,gl_ledgers_v                   ledger
2970           WHERE
2971                 -- OKL to AP Application Predicates
2972                 tpl.tap_id = tap.id
2973             AND tpl.khr_id = khr.id
2974             AND chr.id     = khr.id
2975             AND khr.pdt_id = pdt.id
2976             AND pdt.aes_id = aes.id
2977             AND aes.gts_id = gts.id
2978             AND tap.try_id = try.id
2979             AND tpl.sty_id = sty.id
2980             -- Transaction should have occured in the Start and End date of the Context
2981             AND tap.transaction_date >= p_start_date
2982             AND tap.transaction_date <= p_end_date
2983             -- Add Predicates based on the report
2984             AND  rep.report_id = p_report_id
2985             AND  rtryp.report_id = rep.report_id
2986             AND  try.id = rtryp.try_id
2987             AND  nvl(nvl(rtryp.sty_id,tpl.sty_id), - 1 )  = nvl(tpl.sty_id, -1)
2988             -- Products restriction
2989             AND  pdt_gt.product_id = pdt.id
2990             -- Org., Ledger and Legal Entity Id restriction
2991             AND tap.org_id = NVL( p_org_id, tap.org_id )
2992             AND tap.legal_entity_id = NVL(p_le_id, tap.legal_entity_id )
2993             AND tap.set_of_books_id = p_ledger_id -- Ledger is Mandatory Input Param
2994             -- Predicates to fetch the Names
2995             AND ou.organization_id = tap.org_id
2996             AND le.legal_entity_id = tap.legal_entity_id
2997             AND ledger.ledger_id = tap.set_of_books_id
2998             -- OKL to AP Invoice Lines Predicates
2999             AND invdist.line_type_lookup_code = 'ITEM' -- Need to verify
3000             AND invdist.invoice_id = lin.invoice_id
3001             AND invdist.invoice_line_number = lin.line_number
3002             AND lin.application_id = 540
3003             AND lin.product_table = 'OKL_TXL_AP_INV_LNS_ALL_B'
3004             AND lin.reference_key1 = tpl.id
3005             AND lin.invoice_id = inv.invoice_id
3006             -- AP to XLA Relations
3007             AND  invdist.accounting_event_id = xe.event_id
3008             AND  xe.application_id = 200 -- Payables
3009             AND  xe.event_status_code = 'P' -- Pick Only processed XLA Events
3010             AND xe.application_id = xvl.application_id
3011             AND xvl.event_type_code = xe.event_type_code
3012             AND  xd.event_id = xe.event_id
3013             AND  xd.application_id = 200 -- Payables Application
3014             AND  xd.source_distribution_id_num_1 = invdist.invoice_distribution_id
3015             AND  xd.source_distribution_type = 'AP_INV_DIST'
3016             AND  xd.ae_header_id = xh.ae_header_id
3017             AND  xl.ae_header_id = xh.ae_header_id
3018             AND  xl.ledger_id = p_ledger_id
3019             AND  xd.ae_line_num = xl.ae_line_num --XD,XH,XL END
3020             -- Restrict to only one Distribution Line based on the Transaction Activity Code Add/Substract
3021             -- If its Add Consider only Debit, else if its Substract consider Credit
3022             AND
3023             (
3024               DECODE(xl.entered_cr, NULL, 'DEBIT_DIST', 'CREDIT_DIST' ) =
3025                    DECODE(rtryp.add_substract_code,
3026                    'ADD', 'DEBIT_DIST', 'SUBTRACT', 'CREDIT_DIST' )
3027               OR
3028               DECODE(xl.entered_dr, NULL, 'CREDIT_DIST', 'DEBIT_DIST' ) =
3029                   DECODE(rtryp.add_substract_code,
3030                   'ADD', 'DEBIT_DIST', 'SUBTRACT', 'CREDIT_DIST' )
3031             )
3032             -- From SLA to GL Tables Link
3033             -- Important Predicate: XLA Distribution Links should have the GL Import Link ID
3034             AND  gi.gl_sl_link_id = xl.gl_sl_link_id
3035             AND  gl.ledger_id = xl.ledger_id
3036             AND  gi.gl_sl_link_table = xl.gl_sl_link_table
3037             AND  gi.je_header_id = gh.je_header_id
3038             AND  gh.je_header_id = gl.je_header_id
3039             AND  gi.je_line_num = gl.je_line_num
3040             --  AND  gl.code_combination_id = cc.ccid
3041             AND  gh.je_source = 'Payables'
3042             -- Important Predicate: gl_je_headers.status can be either Posted Or Unposted
3043             AND  gh.status IN ( 'U', 'P' ) -- Unposted or Posted Entries
3044     );
3045     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3046       'After Inserting the Payables Invoice Transactions Data in GT Table End Time:'
3047       || TO_CHAR(SYSDATE, 'DD-MON-YYY HH:MM:SS') );
3048     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3049       'Time taken for Populating Payables Invoice Transactions ' || ( SYSDATE - l_trace_time ) * 86400 || ' Seconds' );
3050     -- Notes:
3051     --   The above Query segments fetch the following
3052     --   1. UnAccounted Entries: Means Trx.s are in corresponding applications itself
3053     --   2. Unposted Entries: Means, Trx. entries reached SLA, but may not be imported to GL
3054     --                       (Or) Trx. entries reached SLA, and also imported to GL but not posted
3055     --   3. Posted Entries: Means, Trx. entries reached SLA, got Imported to GL and got posted in GL too
3056     -- As functionally we need further split-up of (3), posted entries into
3057     --         "Posted into Non Specified Accounts"
3058     --   (Or)  "Posted into Non Specified Period"
3059     --  Note that "Posted into Non Specified Accounts" take precedence
3060     --    on the "Posted into Non Specified Period"
3061     --   As by now the okl_g_reports_gt table has all the TRX_POSTED [Posted Transactions]
3062     --   Update the entries which are not posted into the specified Accounts
3063     -- Logic:
3064     -- Step 1:
3065     --  For each record with detail type as TRX_POSTED in okl_g_reports_gt
3066     --  check whether the Code Combination does not exists in the okl_code_cmbns_gt
3067     --  If it doesnot exist, update the detail type from TRX_POSTED to TRX_POST_NON_SPEC_ACCOUNTS
3068     UPDATE okl_g_reports_gt
3069        SET value1_text   -- trx_detail_type_code
3070               = 'TRX_POST_NON_SPEC_ACCOUNTS'
3071      WHERE value1_text   -- trx_detail_type_code
3072               = 'TRX_POSTED'
3073        AND NVL( value4_num  -- trx_acc_dr_ccid
3074                ,value5_num  -- trx_acc_cr_ccid
3075               )
3076            NOT IN
3077            (
3078              SELECT ccid
3079                FROM okl_code_cmbns_gt
3080            );
3081     -- Step 2:
3082     --  And then, take rest of the records and,
3083     --  For each record with detail type as TRX_POSTED now in okl_g_reports_gt
3084     --  check whether the GL_DATE is not from the period_from_date and period_to_date
3085     --  If so, update the detail type from TRX_POSTED to TRX_POST_NON_SPEC_PERIOD
3086     UPDATE okl_g_reports_gt
3087        SET value1_text   -- trx_detail_type_code
3088               = 'TRX_POST_NON_SPEC_PERIOD'
3089      WHERE  value1_text   -- trx_detail_type_code
3090               = 'TRX_POSTED'
3091       AND ( value2_date -- GL_DATE
3092              < p_start_date
3093        OR  value2_date -- GL_DATE
3094              > p_end_date
3095       );
3096 
3097     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'P',
3098       'After Inserting the Trx. Data in GT Table Start Time:'
3099       || TO_CHAR(SYSDATE, 'DD-MON-YYY HH:MM:SS') );
3100     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3101       'End: ' || l_api_name ||'(-)' );
3102     -- Set the Return Status and return back
3103     x_return_status := l_return_status;
3104   EXCEPTION
3105     WHEN OTHERS THEN
3106       x_return_status := okl_api.g_ret_sts_unexp_error;
3107       okl_api.set_message(p_app_name     => g_app_name,
3108                           p_msg_name     => g_unexpected_error,
3109                           p_token1       => g_sqlcode_token,
3110                           p_token1_value => SQLCODE,
3111                           p_token2       => g_sqlerrm_token,
3112                           p_token2_value => SQLERRM);
3113   END populate_trx_data;
3114 
3115 
3116   -- Start of comments
3117   --
3118   -- Function Name  : process_strm_bal_details.
3119   -- Description    : Procedure to Calculate Contract Streams Opening and Closing Balance
3120   -- Business Rules :
3121   -- Parameters     :
3122   -- Version        : 1.0
3123   -- History        : RGOOTY created.
3124   -- End of comments
3125   PROCEDURE process_strm_bal_details(
3126                p_api_version     IN         NUMBER
3127               ,p_init_msg_list   IN         VARCHAR2
3128               ,x_return_status   OUT NOCOPY VARCHAR2
3129               ,x_msg_count       OUT NOCOPY NUMBER
3130               ,x_msg_data        OUT NOCOPY VARCHAR2
3131               ,p_report_id       IN         NUMBER
3132               ,p_start_date      IN         DATE
3133               ,p_end_date        IN         DATE
3134               ,p_ledger_id       IN         NUMBER
3135               ,p_org_id          IN         NUMBER
3136               ,p_le_id           IN         NUMBER)
3137   IS
3138     TYPE khr_tbl_type IS TABLE OF get_contracts_csr%ROWTYPE
3139       INDEX BY BINARY_INTEGER;
3140     l_khr_tbl   khr_tbl_type;
3141 
3142     -- Cursor to find the Activity Code on the Reconciliation Report Definition
3143     CURSOR get_activity_code_csr( p_report_id NUMBER)
3144     IS
3145       SELECT  activity_code  activity_code
3146         FROM  okl_reports_b rep
3147        WHERE  rep.report_id = p_report_id;
3148     ------------------------------------------------------------------------
3149     --  Important: Logic to find the Stream Balances as on a particular Date
3150     --  Inputs   :
3151     --      p_date : Represents date on which the Unbilled receivables
3152     --                    balance has to be calculated.
3153     --
3154     --  Predicates:
3155     --   a. Restrict the Streams to the ones mentioned in the Report Params.
3156     --   b. Streams should be Active
3157     --   c. If Stream is in Current Status, date of current <= p_date
3158     --   d. If Stream is in History Status, date of current <= p_date AND date of History > p_date
3159     --   e. If Stream is billed, date_billed > p_date
3160     ------------------------------------------------------------------------
3161     CURSOR strm_bal_csr(
3162               p_date         DATE
3163              ,p_khr_id       NUMBER
3164              ,p_activity     VARCHAR2)
3165     IS
3166       SELECT  stm.khr_id                khr_id
3167              ,SUM(se.amount)            unbilled_amount
3168         FROM  okl_streams_rep_v               stm
3169              ,okl_strm_elements         se
3170              ,okl_strm_type_v           sty
3171        WHERE
3172               stm.khr_id = p_khr_id
3173          AND  se.stm_id = stm.id
3174          AND sty.id = stm.sty_id
3175               -- Stream Type should be setup as the Balancing Stream in Report Definition
3176          AND  stm.sty_id IN
3177               (
3178                  SELECT  sty_id
3179                    FROM  okl_report_stream_params  rsp
3180                   WHERE  rsp.report_id = p_report_id
3181               )
3182          AND  stm.say_code <>'WORKING'
3183               -- Only Active Streams are used by down stream processes like Billing/Accrual
3184          AND  stm.active_yn = 'Y'
3185               -- No need to Consider WORK Streams as they are never used by Billing/Accrual
3186          AND  stm.say_code <> 'WORK'
3187          AND
3188          (     -- If Stream is in Current Status, date_current should <= p_date
3189               (
3190                      stm.say_code = 'CURR'
3191                  AND stm.date_current <= p_date
3192               )
3193            OR -- If Stream is Historized, p_date should be in between
3194               --   date_current and date_history
3195               (
3196                      stm.say_code = 'HIST'
3197                  AND stm.date_current <= p_date
3198                  AND stm.date_history > p_date
3199               )
3200          )
3201         AND
3202         (
3203               -- Case 1: Activity Code is NULL, hence no other predicates
3204               --          should be considered
3205                   p_activity IS NULL
3206               -- Case 2: Activity Code is UNBILL, then fetch only those
3207               --          streams which arenot billed untill that date
3208           OR  (
3209                   p_activity = 'UNBILL'
3210                   -- Stream Billed Date should be later than the p_date
3211                AND NVL(se.date_billed, p_date ) >= p_date
3212               ) -- End Case 3: Activity Code is UNBILL
3213               -- Case 3:
3214               --   ACTIVITY Code is UNACCRUED.
3215               --   Consider only Un Accrued Streams as on the p_date.
3216               --   Assumption: Stream Element Date has to be considered as the
3217               --                Streams Accrual Date
3218           OR (
3219                    -- Case: Activity Code is UNACCRUED
3220                    p_activity = 'UNACCRUED'
3221                AND
3222                ( -- Case: UNACCURED (AND)
3223                    (
3224                      -- Stream was Un-Accrued till date, so considered this
3225                      NVL( se.accrued_yn, 'N' ) = 'N'
3226                    ) -- Accrued YN = N
3227                  OR
3228                   (
3229                       se.accrued_yn = 'Y'
3230                   AND se.stream_element_date > p_date
3231                    )
3232                 ) -- End Case: UNACCURED (AND)
3233               ) -- End Case 3: Activity Code is UNACCRUED
3234         ) -- End of AND Clause based on Activity Codes
3235       GROUP BY stm.khr_id;
3236 
3237     -- Local Variables
3238     l_report_id NUMBER := p_report_id;
3239     -----------------------------------------------------------------
3240     -- Declare Process Variable
3241     -----------------------------------------------------------------
3242     l_api_version      CONSTANT NUMBER         := 1;
3243     l_api_name         CONSTANT VARCHAR2(30)   := 'process_strm_bal_details';
3244     l_return_status    VARCHAR2(1)             := OKL_API.G_RET_STS_SUCCESS;
3245     -- Debug related parameters
3246     l_module CONSTANT fnd_log_messages.module%TYPE := G_MODULE || l_api_name;
3247     l_debug_enabled       VARCHAR2(10);
3248     is_debug_procedure_on BOOLEAN;
3249     is_debug_statement_on BOOLEAN;
3250     -- Local Variables
3251     khr_idx               NUMBER;
3252     l_trace_time          DATE;
3253     l_strm_bal_tbl        reports_gt_tbl_type;
3254     l_activity_code       VARCHAR2(30);
3255   BEGIN
3256     l_debug_enabled := okl_debug_pub.check_log_enabled;
3257     -- check for logging on PROCEDURE level
3258     is_debug_procedure_on := okl_debug_pub.check_log_on(l_module,FND_LOG.LEVEL_PROCEDURE);
3259     -- check for logging on STATEMENT level
3260     is_debug_statement_on := okl_debug_pub.check_log_on(l_module,FND_LOG.LEVEL_STATEMENT);
3261     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'P',
3262                'begin debug OKLRRPTB.pls call ' || l_api_name);
3263     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3264       'Begin: ' || l_api_name ||'(+)' );
3265     l_return_status := okl_api.g_ret_sts_success;
3266     -- By now we have the List of Eligible Products and Code Combinations Available
3267     -- in the corresponding _GT tables
3268     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'P',
3269       'Input Parameters ' ||
3270       ' p_report_id=' || p_report_id ||
3271       ' p_ledger_id=' || p_ledger_id ||
3272       ' p_start_date=' || p_start_date ||
3273       ' p_end_date=' || p_end_date ||
3274       ' p_org_id=' || p_org_id ||
3275       ' p_le_id=' ||  p_le_id );
3276 
3277     -------------------------------------------------------------------
3278     -- First find out all the eligible Contracts, and then find
3279     --  Opening Balance and Closing Balance for each of such Contract
3280     -------------------------------------------------------------------
3281     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3282       'Before executing get_contracts_csr:' || TO_CHAR(l_trace_time, 'DD-MON-YYY HH:MM:SS') );
3283     -- Start the stop watch
3284     l_trace_time := SYSDATE;
3285     khr_idx := 0;
3286     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3287       'Need to find Balances for the following Contracts ' );
3288     FOR t_rec IN get_contracts_csr(
3289               p_org_id          => p_org_id
3290              ,p_legal_entity_id => p_le_id
3291              ,p_ledger_id       => p_ledger_id
3292              ,p_start_date      => p_start_date
3293              ,p_end_date        => p_end_date)
3294     LOOP
3295       khr_idx := khr_idx + 1;
3296       l_khr_tbl(khr_idx).currency_code     := t_rec.currency_code;
3297       l_khr_tbl(khr_idx).organization_name := t_rec.organization_name;
3298       l_khr_tbl(khr_idx).contract_number   := t_rec.contract_number;
3299       l_khr_tbl(khr_idx).contract_id       := t_rec.contract_id;
3300       l_khr_tbl(khr_idx).org_id            := t_rec.org_id;
3301       l_khr_tbl(khr_idx).bkg_transaction_date := t_rec.bkg_transaction_date;
3302       put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3303         'Operating Unit:' || l_khr_tbl(khr_idx).organization_name ||
3304         'Contract Number:' || l_khr_tbl(khr_idx).contract_number );
3305     END LOOP;
3306     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3307       'After Executing and Populating Contracts' || ( SYSDATE - l_trace_time ) * 86400 || ' Seconds' );
3308     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3309       'Number of Contracts Fetched ' || TO_NUMBER(khr_idx) );
3310 
3311     ---------------------------------------------------------------------------------
3312     -- Now, Loop on all the above contracts and find the Opening and Closing Balance
3313     ---------------------------------------------------------------------------------
3314     IF l_khr_tbl.COUNT > 0
3315     THEN
3316 
3317       -- First up, fetch the Activity Code on the Report Definition
3318       -- Stream Balances cursor is generic and it can calculate the
3319       --   Unbilled Receivables and also the Un-Accrued amount as on a particular date
3320       -- Hence, we will pass the Activity Code to Stream Balances Cursor
3321       FOR t_rec IN get_activity_code_csr( p_report_id => p_report_id )
3322       LOOP
3323         l_activity_code := t_rec.activity_code;
3324       END LOOP;
3325       put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3326         ' ACTIVITY_CODE on the Report Definition= ' || l_activity_code );
3327       -- Reuse the khr_idx
3328       khr_idx := 0;
3329       FOR i IN l_khr_tbl.FIRST .. l_khr_tbl.LAST
3330       LOOP
3331         put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3332           'Fetching Balance for ' || TO_NUMBER(i) || ' Contract ' || l_khr_tbl(i).contract_number  );
3333         ------------------------------------------------------------------------
3334         -- Code snippet to store the Contract details and its Opening Balance
3335         ------------------------------------------------------------------------
3336         -- Increment the Index
3337         khr_idx := khr_idx + 1;
3338         -- First populate the Record structure
3339         l_strm_bal_tbl(khr_idx).value1_text  := 'OKL_STR_OPEN';
3340         l_strm_bal_tbl(khr_idx).value11_text := l_khr_tbl(i).currency_code;
3341         l_strm_bal_tbl(khr_idx).value9_num   := l_khr_tbl(i).contract_id;
3342         l_strm_bal_tbl(khr_idx).value5_text  := l_khr_tbl(i).contract_number;
3343         l_strm_bal_tbl(khr_idx).value14_text := l_khr_tbl(i).organization_name;
3344         l_strm_bal_tbl(khr_idx).value7_num   := l_khr_tbl(i).org_id;
3345         l_strm_bal_tbl(khr_idx).value3_num   := 0;
3346         -- Start the stop watch
3347         l_trace_time := SYSDATE;
3348 
3349         -- For secondary rep txn, set the security policy for streams. MG Uptake
3350         IF g_representation_type = 'SECONDARY' THEN
3351           OKL_STREAMS_SEC_PVT.SET_REPO_STREAMS;
3352         END IF;
3353         -- Now, calculate the Opening Balance on the p_start_date
3354         FOR t_rec IN strm_bal_csr(
3355                        p_date     => p_start_date
3356                       ,p_khr_id   => l_khr_tbl(i).contract_id
3357                       ,p_activity => l_activity_code)
3358         LOOP
3359           l_strm_bal_tbl(khr_idx).value3_num := nvl(t_rec.unbilled_amount,0);
3360         END LOOP;
3361         put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3362           'Opening Balance - Time Take' || ( SYSDATE - l_trace_time ) * 86400 || ' Seconds' );
3363         ------------------------------------------------------------------------
3364         -- Code snippet to store the Contract details and its Closing Balance
3365         ------------------------------------------------------------------------
3366         -- Increment the Index
3367         khr_idx := khr_idx + 1;
3368         -- First populate the Record structure
3369         l_strm_bal_tbl(khr_idx).value1_text  := 'OKL_STR_CLOSE';
3370         l_strm_bal_tbl(khr_idx).value11_text := l_khr_tbl(i).currency_code;
3371         l_strm_bal_tbl(khr_idx).value9_num   := l_khr_tbl(i).contract_id;
3372         l_strm_bal_tbl(khr_idx).value5_text  := l_khr_tbl(i).contract_number;
3373         l_strm_bal_tbl(khr_idx).value14_text := l_khr_tbl(i).organization_name;
3374         l_strm_bal_tbl(khr_idx).value7_num   := l_khr_tbl(i).org_id;
3375         l_strm_bal_tbl(khr_idx).value3_num   := 0;
3376         -- Start the stop watch
3377         l_trace_time := SYSDATE;
3378         FOR t_rec IN strm_bal_csr(
3379                        p_date   => p_end_date
3380                       ,p_khr_id => l_khr_tbl(i).contract_id
3381                       ,p_activity => l_activity_code)
3382         LOOP
3383           -- Damn Important
3384           --  Negating the Closing Balance, as the Layout checks for
3385           -- Contracts Opening Balance + Transaction Amount + Closing Balance = 0
3386           -- Hence, taken the impact of negating the closing balance here
3387           l_strm_bal_tbl(khr_idx).value3_num := -1 * nvl(t_rec.unbilled_amount,0);
3388         END LOOP;
3389 
3390         -- For secondary rep txn, reset the security policy for streams. MG Uptake
3391         IF g_representation_type = 'SECONDARY' THEN
3392           OKL_STREAMS_SEC_PVT.RESET_REPO_STREAMS;
3393         END IF;
3394 
3395         put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3396           'Closing Balance - Time Take' || ( SYSDATE - l_trace_time ) * 86400 || ' Seconds' );
3397       END LOOP;
3398     END IF; -- IF l_khr_tbl.COUNT > 0
3399     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3400       ' ******** Fetched Closing and Opening Balances for all Contracts ********' );
3401     IF l_strm_bal_tbl.COUNT > 0
3402     THEN
3403       put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3404         'Before Call to Bulk Insert ' || TO_CHAR(SYSDATE, 'DD-MON-YYY HH:MM:SS') );
3405       -- Finally insert the list of Products figured out
3406       FORALL i IN l_strm_bal_tbl.FIRST .. l_strm_bal_tbl.LAST
3407         INSERT INTO okl_g_reports_gt VALUES l_strm_bal_tbl(i);
3408       put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3409         'After Call to Bulk Insert ' || TO_CHAR(SYSDATE, 'DD-MON-YYY HH:MM:SS') );
3410     END IF; -- IF l_strm_bal_tbl.COUNT > 0
3411 
3412     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3413       'End: ' || l_api_name ||'(-)' );
3414     -- Set the Return Status and return back
3415     x_return_status := l_return_status;
3416 
3417   EXCEPTION
3418     WHEN OTHERS THEN
3419       IF get_contracts_csr%ISOPEN
3420       THEN
3421         CLOSE get_contracts_csr;
3422       END IF;
3423       IF strm_bal_csr%ISOPEN
3424       THEN
3425         CLOSE strm_bal_csr;
3426       END IF;
3427       -- If any exception was thrown, then reset the reporting streams - MG uptake
3428       IF g_representation_type = 'SECONDARY' THEN
3429         OKL_STREAMS_SEC_PVT.RESET_REPO_STREAMS;
3430       END IF;
3431       x_return_status := okl_api.g_ret_sts_error;
3432   END process_strm_bal_details;
3433 
3434 
3435   -- Start of comments
3436   --
3437   -- Function Name   : populate_acc_data
3438   -- Description     : API to populate the GL Journal Accounting Details
3439   --
3440   -- Business Rules  : Called from prepare_gross_rec_report
3441   -- Parameters      :
3442   -- Version         : 1.0
3443   -- History         : Ravindranath Gooty created.
3444   -- End of comments
3445 
3446   PROCEDURE populate_acc_data(
3447               p_api_version   IN         NUMBER
3448              ,p_init_msg_list IN         VARCHAR2
3449              ,x_return_status OUT NOCOPY VARCHAR2
3450              ,x_msg_count     OUT NOCOPY NUMBER
3451              ,x_msg_data      OUT NOCOPY VARCHAR2
3452              ,p_report_id     IN         NUMBER
3453              ,p_ledger_id     IN         NUMBER
3454              ,p_start_date    IN         DATE
3455              ,p_end_date      IN         DATE
3456              ,p_org_id        IN         NUMBER
3457              ,p_le_id         IN         NUMBER)
3458   IS
3459     CURSOR get_period_dtls_csr(
3460              p_ledger_id    NUMBER
3461             ,p_period_from  VARCHAR2
3462     )
3463     IS
3464       SELECT  gl.period_set_name   period_set_name
3465              ,per.period_type      period_type
3466         FROM  gl_ledgers           gl
3467              ,gl_periods           per
3468        WHERE  gl.ledger_id        = p_ledger_id
3469          AND  per.period_set_name = gl.period_set_name
3470          AND  per.period_name     = p_period_from;
3471 
3472     l_report_id NUMBER := p_report_id;
3473     -----------------------------------------------------------------
3474     -- Declare Process Variable
3475     -----------------------------------------------------------------
3476     l_api_version      CONSTANT NUMBER         := 1;
3477     l_api_name         CONSTANT VARCHAR2(30)   := 'populate_acc_data';
3478     l_return_status    VARCHAR2(1)             := OKL_API.G_RET_STS_SUCCESS;
3479     -- Debug related parameters
3480     l_module CONSTANT fnd_log_messages.module%TYPE := G_MODULE || l_api_name;
3481     l_debug_enabled       VARCHAR2(10);
3482     is_debug_procedure_on BOOLEAN;
3483     is_debug_statement_on BOOLEAN;
3484     -- Local Variables
3485     l_query_string        VARCHAR2(4000);
3486     l_trace_time          DATE;
3487     l_period_set_name     VARCHAR2(15);
3488     l_period_type         VARCHAR2(15);
3489   BEGIN
3490     l_debug_enabled := okl_debug_pub.check_log_enabled;
3491     -- check for logging on PROCEDURE level
3492     is_debug_procedure_on := okl_debug_pub.check_log_on(l_module,FND_LOG.LEVEL_PROCEDURE);
3493     -- check for logging on STATEMENT level
3494     is_debug_statement_on := okl_debug_pub.check_log_on(l_module,FND_LOG.LEVEL_STATEMENT);
3495     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'P',
3496                'begin debug OKLRRPTB.pls call ' || l_api_name);
3497     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3498       'Begin: ' || l_api_name ||'(+)' );
3499     l_return_status := okl_api.g_ret_sts_success;
3500     -- By now we have the List of Eligible Products and Code Combinations Available
3501     -- in the corresponding _GT tables
3502     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3503       'Input Parameters ' ||
3504       ' p_report_id=' || p_report_id ||
3505       ' p_ledger_id=' || p_ledger_id ||
3506       ' p_start_date=' || p_start_date ||
3507       ' p_end_date=' || p_end_date ||
3508       ' p_org_id=' || p_org_id ||
3509       ' p_le_id=' ||  p_le_id );
3510 
3511     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3512       'Before Executing the Cursor get_period_dtls_csr p_ledger_id=' || p_ledger_id
3513       || ' p_period_from= ' || p_gl_period_from );
3514     FOR t_rec IN get_period_dtls_csr(
3515                    p_ledger_id    => p_ledger_id
3516                   ,p_period_from  => p_gl_period_from )
3517     LOOP
3518       l_period_set_name := t_rec.period_set_name;
3519       l_period_type     := t_rec.period_type;
3520     END LOOP;
3521     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3522       'After executing the Cursor get_period_dtls_csr l_period_set_name=' || l_period_set_name
3523       || ' l_period_type=' || l_period_type );
3524 
3525     -------------------------------------------------------------------------------------
3526     -- Queries to Populate Manual Posted GL Journal Entries from GL Application
3527     -------------------------------------------------------------------------------------
3528     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3529       'Before Inserting the Accounting Journals From GL Start Time:'
3530       || TO_CHAR(l_trace_time, 'DD-MON-YYY HH:MM:SS') );
3531 
3532     -- Start the stop watch
3533     l_trace_time := SYSDATE;
3534     -- Actual Insert Statement
3535     INSERT INTO okl_g_reports_gt
3536                 (
3537                   -- String Formatted Columns
3538                    value1_text
3539                   ,value2_text
3540                   ,value3_text
3541                   ,value4_text
3542                   ,value5_text
3543                   ,value6_text
3544                   ,value7_text
3545                   ,value8_text
3546                   ,value9_text
3547                   ,value10_text
3548                   ,value11_text
3549                   ,value12_text
3550                   ,value13_text
3551                   ,value14_text
3552                   ,value15_text
3553                   ,value16_text
3554                   ,value17_text
3555                   ,value18_text
3556                   -- Number Formatted Columns
3557                   ,value1_num
3558                   ,value2_num
3559                   ,value3_num
3560                   ,value4_num
3561                   ,value5_num
3562                   ,value6_num
3563                   ,value7_num
3564                   ,value8_num
3565                   ,value9_num
3566                   ,value10_num
3567                   ,value11_num
3568                   ,value12_num
3569                   ,value13_num
3570                   -- Date Formatted Columns
3571                   ,value1_date
3572                   ,value2_date
3573                 )
3574       SELECT -- String Formatted Columns
3575              gl_detail_type_code         --value1_text
3576             ,trx_number                  --value2_text
3577             ,trx_type_name               --value3_text
3578             ,trx_application_name        --value4_text
3579             ,contract_number             --value5_text
3580             ,asset_number                --value6_text
3581             ,book_classification         --value7_text
3582             ,product_name                --value8_text
3583             ,trx_sty_name                --value9_text
3584             ,trx_sty_purpose             --value10_text
3585             ,currency_code               --value11_text
3586             ,trx_period_name             --value12_text -- Populated Only for Posted Entries
3587             ,trx_account_name            --value13_text
3588             ,trx_operating_unit_name     --value14_text
3589             ,NULL                        --value15_text -- Legal Entity Name
3590             ,trx_ledger_name             --value16_text
3591             ,trx_activity_code           --value17_text  -- Activity Code Add/Subtract for Dr/Cr
3592             ,trx_event_name              --value18_text
3593             -- Number Formatted Columns
3594             ,trx_dr_amount               --value1_num
3595             ,trx_cr_amount               --value2_num
3596             ,trx_net_amount              --value3_num
3597             ,code_combination_id         --value4_num
3598             ,code_combination_id         --value5_num
3599             ,trx_ledger_id               --value6_num
3600             ,trx_operating_unit_id       --value7_num
3601             ,trx_try_id                  --value8_num -- Transaction Type ID
3602             ,trx_khr_id                  --value9_num
3603             ,txl_asset_id                --value10_num
3604             ,trx_pdt_id                  --value11_num
3605             ,trx_sty_id                  --value12_num
3606             ,trx_application_id          --value13_num
3607             -- Date Formatted Columns
3608             ,trx_date                    --value1_date
3609             ,gl_date                     --value2_date
3610         FROM
3611       (
3612         -- Query Segment to find the Manual Posted Journal Entries in GL
3613         SELECT 'GL_ACC_OTHER_APPS'                      gl_detail_type_code
3614               ,gh.currency_code                         currency_code
3615               ,app.application_name                     trx_application_name
3616               ,glcc.concatenated_segments               trx_account_name
3617               ,gh.je_source                             trx_event_name
3618               ,NULL                                     trx_type_name
3619               ,NULL                                     trx_sty_name
3620               ,gh.name                                  trx_number
3621               ,NULL                                     contract_number
3622               ,NULL                                     asset_number
3623               ,NULL                                     book_classification
3624               ,NULL                                     product_name
3625               ,NULL                                     trx_sty_purpose
3626               ,NULL                                     trx_operating_unit_name
3627               ,ledger.name                              trx_ledger_name
3628               ,gh.period_name                           trx_period_name
3629               ,gl.code_combination_id                   code_combination_id
3630               -- Number Format Columns
3631               ,gl.entered_dr                            trx_dr_amount
3632               ,gl.entered_cr                            trx_cr_amount
3633               ,( NVL(gl.entered_dr,0) - NVL(gl.entered_cr,0) )
3634                                                         trx_net_amount
3635               ,NULL                                     trx_activity_code
3636               ,gl.ledger_id                             trx_ledger_id
3637               ,NULL                                     trx_operating_unit_id
3638               ,NULL                                     trx_khr_id
3639               ,NULL                                     txl_asset_id
3640               ,NULL                                     trx_pdt_id
3641               ,NULL                                     trx_sty_id
3642               ,101                                      trx_application_id
3643               -- DATE Format Columns
3644               ,gh.default_effective_date                trx_date
3645               ,NULL                                     gl_date
3646               -- Additional Columns
3647               ,NULL                                     trx_id
3648               ,NULL                                     trx_txl_id
3649               ,NULL                                     trx_try_id
3650               ,NULL                                     trx_dist_id
3651         FROM    -- GL Tables: Import Reference, GL Header and Lines
3652                 gl_je_lines               gl
3653                ,gl_je_headers             gh
3654                ,gl_code_combinations_kfv  glcc
3655                -- Code Combination GT Table
3656                ,okl_code_cmbns_gt         cc
3657                ,gl_ledgers                ledger
3658                ,fnd_application_vl        app
3659         WHERE
3660                -- Restrict the Code Combinations to the one setup on the Report
3661                gl.code_combination_id = cc.ccid AND
3662                -- GL Tables
3663                gl.ledger_id        = p_ledger_id
3664           AND  gh.je_header_id     = gl.je_header_id
3665           AND  gh.ledger_id        = gl.ledger_id
3666           AND  gh.je_source        = 'Manual'
3667           AND  gh.status           =  'P'  -- Pick Only Posted Journals
3668           AND  glcc.code_combination_id = gl.code_combination_id
3669                -- Predicates to fetch Names
3670           AND  ledger.ledger_id    = gl.ledger_id
3671           AND  app.application_id  = 101 -- GL Appliation Id
3672           AND  gh.default_effective_date >= p_start_date
3673           AND  gh.default_effective_date <= p_end_date
3674           -- End of Query Segment to fetch Manual Posted Journal Entries from GL Application
3675     );
3676     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3677       'After Inserting the Accounting Journals From GL End Time:'
3678       || TO_CHAR(SYSDATE, 'DD-MON-YYY HH:MM:SS') );
3679     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3680       'Time taken for Populating Accounting Journals from GL Application '
3681        || ( SYSDATE - l_trace_time ) * 86400 || ' Seconds' );
3682 
3683 
3684     -------------------------------------------------------------------------------------
3685     -- Queries to Populate Posted GL Journal Entry details from Non OLM, AR, AP, FA App.
3686     -------------------------------------------------------------------------------------
3687     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3688       'Before Inserting the Accounting Journals From Non GL, OLM, AR, AP, FA Applications:'
3689       || TO_CHAR(l_trace_time, 'DD-MON-YYY HH:MM:SS') );
3690 
3691     -- Start the stop watch
3692     l_trace_time := SYSDATE;
3693     -- Actual Insert Statement
3694     INSERT INTO okl_g_reports_gt
3695                 (
3696                   -- String Formatted Columns
3697                    value1_text
3698                   ,value2_text
3699                   ,value3_text
3700                   ,value4_text
3701                   ,value5_text
3702                   ,value6_text
3703                   ,value7_text
3704                   ,value8_text
3705                   ,value9_text
3706                   ,value10_text
3707                   ,value11_text
3708                   ,value12_text
3709                   ,value13_text
3710                   ,value14_text
3711                   ,value15_text
3712                   ,value16_text
3713                   ,value17_text
3714                   ,value18_text
3715                   -- Number Formatted Columns
3716                   ,value1_num
3717                   ,value2_num
3718                   ,value3_num
3719                   ,value4_num
3720                   ,value5_num
3721                   ,value6_num
3722                   ,value7_num
3723                   ,value8_num
3724                   ,value9_num
3725                   ,value10_num
3726                   ,value11_num
3727                   ,value12_num
3728                   ,value13_num
3729                   -- Date Formatted Columns
3730                   ,value1_date
3731                   ,value2_date
3732                 )
3733       SELECT -- String Formatted Columns
3734              gl_detail_type_code         --value1_text
3735             ,trx_number                  --value2_text
3736             ,trx_type_name               --value3_text
3737             ,trx_application_name        --value4_text
3738             ,contract_number             --value5_text
3739             ,asset_number                --value6_text
3740             ,book_classification         --value7_text
3741             ,product_name                --value8_text
3742             ,trx_sty_name                --value9_text
3743             ,trx_sty_purpose             --value10_text
3744             ,currency_code               --value11_text
3745             ,trx_period_name             --value12_text -- Populated Only for Posted Entries
3746             ,trx_account_name            --value13_text
3747             ,trx_operating_unit_name     --value14_text
3748             ,NULL                        --value15_text -- Legal Entity Name
3749             ,trx_ledger_name             --value16_text
3750             ,trx_activity_code           --value17_text  -- Activity Code Add/Subtract for Dr/Cr
3751             ,trx_event_name              --value18_text
3752             -- Number Formatted Columns
3753             ,trx_dr_amount               --value1_num
3754             ,trx_cr_amount               --value2_num
3755             ,trx_net_amount              --value3_num
3756             ,code_combination_id         --value4_num
3757             ,code_combination_id         --value5_num
3758             ,trx_ledger_id               --value6_num
3759             ,trx_operating_unit_id       --value7_num
3760             ,trx_try_id                  --value8_num -- Transaction Type ID
3761             ,trx_khr_id                  --value9_num
3762             ,txl_asset_id                --value10_num
3763             ,trx_pdt_id                  --value11_num
3764             ,trx_sty_id                  --value12_num
3765             ,trx_application_id          --value13_num
3766             -- Date Formatted Columns
3767             ,trx_date                    --value1_date
3768             ,gl_date                     --value2_date
3769         FROM
3770       (
3771         -- Query Segment to find the Posted Journal Entries from
3772         --  applications other than GL, OLM, FA, AR and AP
3773         SELECT 'GL_ACC_OTHER_APPS'                      gl_detail_type_code
3774               ,gh.currency_code                         currency_code
3775               ,app.application_name                     trx_application_name
3776               ,glcc.concatenated_segments               trx_account_name
3777               ,xvl.name                                 trx_event_name
3778               ,NULL                                     trx_type_name
3779               ,NULL                                     trx_sty_name
3780               ,xte.transaction_number                   trx_number
3781               ,NULL                                     contract_number
3782               ,NULL                                     asset_number
3783               ,NULL                                     book_classification
3784               ,NULL                                     product_name
3785               ,NULL                                     trx_sty_purpose
3786               ,NULL                                     trx_operating_unit_name
3787               ,ledger.name                              trx_ledger_name
3788               ,gh.period_name                           trx_period_name
3789               ,xl.code_combination_id                   code_combination_id
3790               -- Number Format Columns
3791               ,xl.entered_dr                            trx_dr_amount
3792               ,xl.entered_cr                            trx_cr_amount
3793               ,( NVL(xl.entered_dr,0) - NVL(xl.entered_cr,0) )
3794                                                         trx_net_amount
3795               ,NULL                                     trx_activity_code
3796               ,xl.ledger_id                             trx_ledger_id
3797               ,NULL                                     trx_operating_unit_id
3798               ,NULL                                     trx_khr_id
3799               ,NULL                                     txl_asset_id
3800               ,NULL                                     trx_pdt_id
3801               ,NULL                                     trx_sty_id
3802               ,xl.application_id                        trx_application_id
3803               -- DATE Format Columns
3804               ,xe.transaction_date                      trx_date
3805               ,NULL                                     gl_date
3806               -- Additional Columns
3807               ,NULL                                     trx_id
3808               ,NULL                                     trx_txl_id
3809               ,NULL                                     trx_try_id
3810               ,NULL                                     trx_dist_id
3811         FROM    -- GL Tables: Import Reference, GL Header and Lines
3812                 gl_je_lines               gl
3813                ,gl_je_headers             gh
3814                ,gl_code_combinations_kfv  glcc
3815                ,gl_import_references      gi
3816                -- Code Combination GT Table
3817                ,okl_code_cmbns_gt         cc
3818                -- SLA Entities
3819                ,xla_ae_lines              xl
3820                ,xla_ae_headers            xh
3821                ,xla_events                xe
3822                ,xla_event_types_vl        xvl
3823                ,xla_transaction_entities  xte
3824                ,gl_ledgers                ledger
3825                ,fnd_application_vl        app
3826         WHERE
3827                -- Restrict the Code Combinations to the one setup on the Report
3828                gl.code_combination_id = cc.ccid AND
3829                -- GL Tables
3830                gl.ledger_id        = p_ledger_id
3831           AND  gh.je_header_id     = gl.je_header_id
3832           AND  gh.ledger_id        = gl.ledger_id
3833           AND  gh.status           =  'P'  -- Pick Only Posted Journals
3834           AND  glcc.code_combination_id = gl.code_combination_id
3835           AND  gi.je_header_id     = gh.je_header_id
3836           AND  gi.je_line_num      = gl.je_line_num
3837                -- GL to XLA Relations
3838           AND  xl.gl_sl_link_id    = gi.gl_sl_link_id
3839           AND  xl.gl_sl_link_table = gi.gl_sl_link_table
3840           AND  xl.ledger_id        = gl.ledger_id
3841                -- XLA Predicates
3842           AND  xl.ae_header_id     = xh.ae_header_id
3843           AND  xe.event_id         = xh.event_id
3844           AND  xe.application_id
3845                NOT IN
3846                  (  540  -- Leasing and Finance Management
3847                    ,200  -- Payables
3848                    ,222  -- Receivables
3849                  )
3850           AND  xe.application_id   = xvl.application_id
3851           AND  xvl.event_type_code = xe.event_type_code
3852           AND  xte.entity_id       = xe.entity_id
3853           AND  xte.application_id  = xe.application_id
3854           -- Predicates to fetch the Names
3855           AND  ledger.ledger_id    = xl.ledger_id
3856           AND  app.application_id  = xe.application_id
3857           -- Restrict the Journal Entries to be in between Start and End Dates
3858           AND  gh.default_effective_date >= p_start_date
3859           AND  gh.default_effective_date <= p_end_date
3860           -- End of Query Segment to fetch Posted Acc. Entries in GL
3861           --  from applications other than GL, OLM, FA, AR and AP
3862       );
3863     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3864       'After Inserting the Accounting Journals From Non GL, OLM, AR, AP, FA Applications End Time:'
3865       || TO_CHAR(SYSDATE, 'DD-MON-YYY HH:MM:SS') );
3866     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3867       'Time taken for Populating Accounting Journals in GL from Non GL, OLM, AR, AP, FA Applications '
3868        || ( SYSDATE - l_trace_time ) * 86400 || ' Seconds' );
3869 
3870     -------------------------------------------------------------------------------------
3871     -- Queries to Populate Posted GL Journal Entry details from OLM, AR, AP
3872     --  OLM - All Transactions. AR - Invoice, Adjustment, Credit Memo
3873     --  AP - Payables Invoices
3874     -------------------------------------------------------------------------------------
3875     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
3876       'Before Inserting the Accounting Journals in GT Table Start Time:'
3877       || TO_CHAR(l_trace_time, 'DD-MON-YYY HH:MM:SS') );
3878 
3879     -- Start the stop watch
3880     l_trace_time := SYSDATE;
3881     -- Actual Insert Statement
3882     INSERT INTO okl_g_reports_gt
3883                 (
3884                   -- String Formatted Columns
3885                    value1_text
3886                   ,value2_text
3887                   ,value3_text
3888                   ,value4_text
3889                   ,value5_text
3890                   ,value6_text
3891                   ,value7_text
3892                   ,value8_text
3893                   ,value9_text
3894                   ,value10_text
3895                   ,value11_text
3896                   ,value12_text
3897                   ,value13_text
3898                   ,value14_text
3899                   ,value15_text
3900                   ,value16_text
3901                   ,value17_text
3902                   ,value18_text
3903                   -- Number Formatted Columns
3904                   ,value1_num
3905                   ,value2_num
3906                   ,value3_num
3907                   ,value4_num
3908                   ,value5_num
3909                   ,value6_num
3910                   ,value7_num
3911                   ,value8_num
3912                   ,value9_num
3913                   ,value10_num
3914                   ,value11_num
3915                   ,value12_num
3916                   ,value13_num
3917                   ,value15_num
3918                   -- Date Formatted Columns
3919                   ,value1_date
3920                   ,value2_date
3921                 )
3922       SELECT -- String Formatted Columns
3923              gl_detail_type_code         --value1_text
3924             ,trx_number                  --value2_text
3925             ,trx_type_name               --value3_text
3926             ,trx_application_name        --value4_text
3927             ,contract_number             --value5_text
3928             ,asset_number                --value6_text
3929             ,book_classification         --value7_text
3930             ,product_name                --value8_text
3931             ,trx_sty_name                --value9_text
3932             ,trx_sty_purpose             --value10_text
3933             ,currency_code               --value11_text
3934             ,trx_period_name             --value12_text -- Populated Only for Posted Entries
3935             ,trx_account_name            --value13_text
3936             ,trx_operating_unit_name     --value14_text
3937             ,NULL                        --value15_text -- Legal Entity Name
3938             ,trx_ledger_name             --value16_text
3939             ,trx_activity_code           --value17_text  -- Activity Code Add/Subtract for Dr/Cr
3940             ,trx_event_name              --value18_text
3941             -- Number Formatted Columns
3942             ,trx_dr_amount               --value1_num
3943             ,trx_cr_amount               --value2_num
3944             ,trx_net_amount              --value3_num
3945             ,code_combination_id         --value4_num
3946             ,code_combination_id         --value5_num
3947             ,trx_ledger_id               --value6_num
3948             ,trx_operating_unit_id       --value7_num
3949             ,trx_try_id                  --value8_num -- Transaction Type ID
3950             ,trx_khr_id                  --value9_num
3951             ,txl_asset_id                --value10_num
3952             ,trx_pdt_id                  --value11_num
3953             ,trx_sty_id                  --value12_num
3954             ,trx_application_id          --value13_num
3955             ,trx_xla_event_id            --value15_num
3956             -- Date Formatted Columns
3957             ,trx_date                    --value1_date
3958             ,gl_date                     --value2_date
3959         FROM
3960       (
3961         -- Query Segment to find the Posted Journal Entries from GL from Lease Contracts
3962         SELECT 'GL_ACC_OLM_ENTRIES'                     gl_detail_type_code
3963               ,gh.currency_code                         currency_code
3964               ,app.application_name                     trx_application_name
3965               ,glcc.concatenated_segments               trx_account_name
3966               ,xvl.name                                 trx_event_name
3967               ,try.name                                 trx_type_name
3968               ,sty.NAME                                 trx_sty_name
3969               ,trx.trx_number                           trx_number
3970               ,chr.contract_number                      contract_number
3971               ,NULL                                     asset_number
3972               ,gts.deal_type                            book_classification
3973               ,pdt.NAME                                 product_name
3974               ,sty.styb_purpose_meaning                 trx_sty_purpose
3975               ,ou.name                                  trx_operating_unit_name
3976               ,ledger.name                              trx_ledger_name
3977               ,gh.period_name                           trx_period_name
3978               ,xl.code_combination_id                   code_combination_id
3979               -- Number Format Columns
3980               ,xl.entered_dr                            trx_dr_amount
3981               ,xl.entered_cr                            trx_cr_amount
3982               ,( NVL(xl.entered_dr,0) - NVL(xl.entered_cr,0) )
3983                                                         trx_net_amount
3984               ,DECODE( xl.entered_dr, NULL, 'SUBTRACT', 'ADD' )
3985                                                         trx_activity_code
3986               ,xl.ledger_id                             trx_ledger_id
3987               ,trx.org_id                               trx_operating_unit_id
3988               ,trx.khr_id                               trx_khr_id
3989               ,txl.kle_id                               txl_asset_id
3990               ,khr.pdt_id                               trx_pdt_id
3991               ,txl.sty_id                               trx_sty_id
3992               ,xl.application_id                        trx_application_id
3993               ,xe.event_id                              trx_xla_event_id
3994               -- DATE Format Columns
3995               ,trx.transaction_date                     trx_date
3996               ,dist.gl_date                             gl_date
3997               -- Additional Columns
3998               ,trx.id                                   trx_id
3999               ,txl.id                                   trx_txl_id
4000               ,trx.try_id                               trx_try_id
4001               ,dist.id                                  trx_dist_id
4002         FROM    -- GL Tables: Import Reference, GL Header and Lines
4003                 gl_je_lines               gl
4004                ,gl_je_headers             gh
4005                ,gl_code_combinations_kfv  glcc
4006                ,gl_import_references      gi
4007                -- Code Combination GT Table
4008                ,okl_code_cmbns_gt         cc
4009                -- SLA Entities
4010                ,xla_ae_lines              xl
4011                ,xla_ae_headers            xh
4012                ,xla_distribution_links    xd
4013                ,xla_events                xe
4014                ,xla_event_types_vl        xvl
4015               -- OLM Entities
4016                ,okl_trns_acc_dstrs_all    dist
4017                ,okl_txl_cntrct_lns_all    txl
4018                ,okl_trx_contracts_all     trx
4019                ,okl_trx_types_v           try
4020                ,okl_strm_type_v           sty
4021                ,okc_k_headers_all_b       chr
4022                ,okl_k_headers             khr
4023                ,okl_products              pdt
4024                ,okl_ae_tmpt_sets_all      aes
4025                ,okl_st_gen_tmpt_sets_all  gts
4026                -- To fetch Names
4027                ,hr_operating_units        ou
4028                ,gl_ledgers                ledger
4029                ,fnd_application_vl        app
4030         WHERE
4031                -- Restrict the Code Combinations to the one setup on the Report
4032                gl.code_combination_id = cc.ccid AND
4033                -- GL Tables
4034                gl.ledger_id        = p_ledger_id
4035           AND  gh.je_header_id     = gl.je_header_id
4036           AND  gh.ledger_id        = gl.ledger_id
4037           AND  gh.je_source        = 'Lease'
4038           AND  gh.status           =  'P'  -- Pick Only Posted Journals
4039           AND  glcc.code_combination_id = gl.code_combination_id
4040           AND  gi.je_header_id     = gh.je_header_id
4041           AND  gi.je_line_num      = gl.je_line_num
4042                -- GL to XLA Relations
4043           AND  xl.gl_sl_link_id    = gi.gl_sl_link_id
4044           AND  xl.gl_sl_link_table = gi.gl_sl_link_table
4045           AND  xl.ledger_id        = gl.ledger_id
4046                -- XLA Predicates
4047           AND  xl.ae_header_id     = xh.ae_header_id
4048           AND  xd.application_id   = 540  -- Restrict to Lease Journals
4049           AND  xd.ae_header_id     = xh.ae_header_id
4050           AND  xd.ae_line_num      = xl.ae_line_num
4051           AND  xe.event_id         = xd.event_id
4052           AND  xe.application_id   = xvl.application_id
4053           AND  xvl.event_type_code = xe.event_type_code
4054                -- XLA to OLM Predicates
4055           AND  xd.event_id         = dist.accounting_event_id
4056           AND  dist.id             = xd.source_distribution_id_num_1
4057           AND  dist.posted_yn      = 'Y'
4058                -- OLM Predicates
4059           AND  dist.source_table   = 'OKL_TXL_CNTRCT_LNS'
4060           AND  dist.source_id      = txl.id
4061           AND  trx.id              = txl.tcn_id
4062           AND  trx.try_id          = try.id
4063           AND  txl.sty_id          = sty.id
4064           AND  trx.khr_id          = chr.id
4065           AND  chr.id              = khr.id
4066           AND  khr.pdt_id          = pdt.id
4067           AND  pdt.aes_id          = aes.id
4068           AND  aes.gts_id          = gts.id
4069           -- Predicates to fetch the Names
4070           AND  ou.organization_id  = trx.org_id
4071           AND  ledger.ledger_id    = trx.set_of_books_id
4072           AND  app.application_id  = xe.application_id
4073           -- Restrict the Journal Entries to be in between Start and End Dates
4074           AND  gh.default_effective_date >= p_start_date
4075           AND  gh.default_effective_date <= p_end_date
4076           -- End of Query Segment to fetch Posted Acc. Entries from GL
4077     );
4078     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
4079       'After Inserting the Lease Accounting Journals Data in GT Table End Time:'
4080       || TO_CHAR(SYSDATE, 'DD-MON-YYY HH:MM:SS') );
4081     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
4082       'Time taken for Populating Lease Accounting Journals '
4083        || ( SYSDATE - l_trace_time ) * 86400 || ' Seconds' );
4084 
4085     -------------------------------------------------------------------------------------
4086     -- Queries to Populate Posted GL Journal Entry details
4087     --  related to AR Invoice Transactions
4088     -------------------------------------------------------------------------------------
4089     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
4090       'Before Inserting the Receivables Invoice Accounting Journals in GT Table Start Time:'
4091       || TO_CHAR(l_trace_time, 'DD-MON-YYY HH:MM:SS') );
4092 
4093     -- Start the stop watch
4094     l_trace_time := SYSDATE;
4095     -- Actual Insert Statement
4096     INSERT INTO okl_g_reports_gt
4097                 (
4098                   -- String Formatted Columns
4099                    value1_text
4100                   ,value2_text
4101                   ,value3_text
4102                   ,value4_text
4103                   ,value5_text
4104                   ,value6_text
4105                   ,value7_text
4106                   ,value8_text
4107                   ,value9_text
4108                   ,value10_text
4109                   ,value11_text
4110                   ,value12_text
4111                   ,value13_text
4112                   ,value14_text
4113                   ,value15_text
4114                   ,value16_text
4115                   ,value17_text
4116                   ,value18_text
4117                   -- Number Formatted Columns
4118                   ,value1_num
4119                   ,value2_num
4120                   ,value3_num
4121                   ,value4_num
4122                   ,value5_num
4123                   ,value6_num
4124                   ,value7_num
4125                   ,value8_num
4126                   ,value9_num
4127                   ,value10_num
4128                   ,value11_num
4129                   ,value12_num
4130                   ,value13_num
4131                   ,value15_num
4132                   -- Date Formatted Columns
4133                   ,value1_date
4134                   ,value2_date
4135                 )
4136       SELECT -- String Formatted Columns
4137              gl_detail_type_code         --value1_text
4138             ,trx_number                  --value2_text
4139             ,trx_type_name               --value3_text
4140             ,trx_application_name        --value4_text
4141             ,contract_number             --value5_text
4142             ,asset_number                --value6_text
4143             ,book_classification         --value7_text
4144             ,product_name                --value8_text
4145             ,trx_sty_name                --value9_text
4146             ,trx_sty_purpose             --value10_text
4147             ,currency_code               --value11_text
4148             ,trx_period_name             --value12_text -- Populated Only for Posted Entries
4149             ,trx_account_name            --value13_text
4150             ,trx_operating_unit_name     --value14_text
4151             ,NULL                        --value15_text -- Legal Entity Name
4152             ,trx_ledger_name             --value16_text
4153             ,trx_activity_code           --value17_text  -- Activity Code Add/Subtract for Dr/Cr
4154             ,trx_event_name              --value18_text
4155             -- Number Formatted Columns
4156             ,trx_dr_amount               --value1_num
4157             ,trx_cr_amount               --value2_num
4158             ,trx_net_amount              --value3_num
4159             ,code_combination_id         --value4_num
4160             ,code_combination_id         --value5_num
4161             ,trx_ledger_id               --value6_num
4162             ,trx_operating_unit_id       --value7_num
4163             ,trx_try_id                  --value8_num -- Transaction Type ID
4164             ,trx_khr_id                  --value9_num
4165             ,txl_asset_id                --value10_num
4166             ,trx_pdt_id                  --value11_num
4167             ,trx_sty_id                  --value12_num
4168             ,trx_application_id          --value13_num
4169             ,trx_xla_event_id            --value15_num
4170             -- Date Formatted Columns
4171             ,trx_date                    --value1_date
4172             ,gl_date                     --value2_date
4173         FROM
4174       (
4175         -- Query Segment to find the Posted Journal Entries from GL
4176         --  On behalf of OLM Lease Contract Receivables Invoice
4177         SELECT 'GL_ACC_OLM_ENTRIES'                     gl_detail_type_code
4178               ,gh.currency_code                         currency_code
4179               ,app.application_name                     trx_application_name
4180               ,glcc.concatenated_segments               trx_account_name
4181               ,xvl.name                                 trx_event_name
4182               ,try.name                                 trx_type_name
4183               ,sty.NAME                                 trx_sty_name
4184               ,rct.trx_number                           trx_number
4185               ,chr.contract_number                      contract_number
4186               ,NULL                                     asset_number
4187               ,gts.deal_type                            book_classification
4188               ,pdt.NAME                                 product_name
4189               ,sty.styb_purpose_meaning                 trx_sty_purpose
4190               ,ou.name                                  trx_operating_unit_name
4191               ,ledger.name                              trx_ledger_name
4192               ,gh.period_name                           trx_period_name
4193               ,xl.code_combination_id                   code_combination_id
4194               -- Number Format Columns
4195               ,DECODE( xl.entered_cr
4196                        ,NULL, txd.amount
4197                        ,0 )                             trx_dr_amount
4198               ,DECODE( xl.entered_dr
4199                        ,NULL, txd.amount
4200                        ,0 )                             trx_cr_amount
4201               ,DECODE( xl.entered_cr, NULL, txd.amount, txd.amount * -1 )
4202                                                         trx_net_amount
4203               ,DECODE( xl.entered_dr, NULL, 'SUBTRACT', 'ADD' )
4204                                                         trx_activity_code
4205               ,xl.ledger_id                             trx_ledger_id
4206               ,tai.org_id                               trx_operating_unit_id
4207               ,tai.khr_id                               trx_khr_id
4208               ,txd.kle_id                               txl_asset_id
4209               ,khr.pdt_id                               trx_pdt_id
4210               ,txd.sty_id                               trx_sty_id
4211               ,xl.application_id                        trx_application_id
4212               ,xe.event_id                              trx_xla_event_id
4213               -- DATE Format Columns
4214               ,tai.transaction_date                     trx_date
4215               ,rad.gl_date                              gl_date
4216               -- Additional Columns
4217               ,tai.id                                   trx_id
4218               ,txd.id                                   trx_txl_id
4219               ,tai.try_id                               trx_try_id
4220               ,NULL                                     trx_dist_id
4221         FROM    -- GL Tables: Import Reference, GL Header and Lines
4222                 gl_je_lines               gl
4223                ,gl_je_headers             gh
4224                ,gl_code_combinations_kfv  glcc
4225                ,gl_import_references      gi
4226                -- Code Combination GT Table
4227                ,okl_code_cmbns_gt         cc
4228                -- SLA Entities
4229                ,xla_ae_lines              xl
4230                ,xla_ae_headers            xh
4231                ,xla_distribution_links    xd
4232                ,xla_events                xe
4233                ,xla_event_types_vl        xvl
4234                -- AR Tables
4235                ,ra_cust_trx_line_gl_dist_all  rad
4236                ,ra_customer_trx_lines_all     rcl
4237                ,ra_customer_trx_all           rct
4238                -- OLM Tables
4239                ,okl_txd_ar_ln_dtls_b      txd
4240                ,okl_txl_ar_inv_lns_b      til
4241                ,okl_trx_ar_invoices_b     tai
4242                ,okc_k_headers_all_b       chr
4243                ,okl_k_headers             khr
4244                ,okl_products              pdt
4245                ,okl_ae_tmpt_sets_all      aes
4246                ,okl_st_gen_tmpt_sets_all  gts
4247                ,okl_trx_types_v           try
4248                ,okl_strm_type_v           sty
4249                -- To fetch Names
4250                ,hr_operating_units        ou
4251                ,gl_ledgers                ledger
4252                ,fnd_application_vl        app
4253         WHERE
4254                -- Restrict the Code Combinations to the one setup on the Report
4255                gl.code_combination_id = cc.ccid AND
4256                -- GL Tables
4257                gl.ledger_id        = p_ledger_id
4258           AND  gh.je_header_id     = gl.je_header_id
4259           AND  gh.ledger_id        = gl.ledger_id
4260           AND  gh.je_source        = 'Receivables'
4261           AND  gh.status           =  'P'  -- Pick Only Posted Journals
4262           AND  glcc.code_combination_id = gl.code_combination_id
4263           AND  gi.je_header_id     = gh.je_header_id
4264           AND  gi.je_line_num      = gl.je_line_num
4265                -- GL to XLA Relations
4266           AND  xl.gl_sl_link_id    = gi.gl_sl_link_id
4267           AND  xl.gl_sl_link_table = gi.gl_sl_link_table
4268           AND  xl.ledger_id        = gl.ledger_id
4269                -- XLA Predicates
4270           AND  xl.ae_header_id     = xh.ae_header_id
4271           AND  xd.application_id   = 222  -- Restrict to Receivables Journals
4272           AND  xd.ae_header_id     = xh.ae_header_id
4273           AND  xd.ae_line_num      = xl.ae_line_num
4274           AND  xe.event_id         = xd.event_id
4275           AND  xe.event_status_code = 'P'
4276           AND  xe.application_id   = xvl.application_id
4277           AND  xvl.event_type_code = xe.event_type_code
4278                -- XLA to AR Predicates
4279           AND  rad.event_id = xd.event_id
4280           AND  rad.cust_trx_line_gl_dist_id = xd.source_distribution_id_num_1
4281                -- AR Predicates
4282           AND  rcl.customer_trx_line_id = rad.customer_trx_line_id
4283           AND  rct.customer_trx_id = rcl.customer_trx_id
4284                -- OKL to AR Predicates
4285           AND  rcl.interface_line_attribute14 = txd.id
4286           AND  rcl.interface_line_attribute6 =  chr.contract_number
4287                -- OKL Predicates
4288           AND  txd.til_id_details = til.id -- Trx. Detail to Trx. Line
4289           AND  til.tai_id = tai.id         -- Trx. Line to Trx. Header
4290           AND  txd.khr_id = chr.id         -- Trx. Header to Contract
4291           AND  tai.try_id = try.id
4292           AND  txd.sty_id = sty.id
4293           AND  chr.id     = khr.id
4294           AND  khr.pdt_id = pdt.id
4295           AND  pdt.aes_id = aes.id
4296           AND  aes.gts_id = gts.id
4297           -- Predicates to fetch the Names
4298           AND  ou.organization_id  = tai.org_id
4299           AND  ledger.ledger_id    = tai.set_of_books_id
4300           AND  app.application_id  = xe.application_id
4301           AND  gh.default_effective_date >= p_start_date
4302           AND  gh.default_effective_date <= p_end_date
4303           -- End of Query Segment to fetch Posted Acc. Entries from GL
4304           --  from Receivables for a Lease Contract Invoice Transactions
4305     );
4306     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
4307       'After Inserting the Receivables Invoice Accounting Journals Data in GT Table End Time:'
4308       || TO_CHAR(SYSDATE, 'DD-MON-YYY HH:MM:SS') );
4309     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
4310       'Time taken for Populating Receivables Invoice Accounting Journals '
4311        || ( SYSDATE - l_trace_time ) * 86400 || ' Seconds' );
4312 
4313 
4314     -------------------------------------------------------------------------------------
4315     -- Queries to Populate Posted GL Journal Entry details
4316     --  related to AR Adjustment Transactions
4317     -------------------------------------------------------------------------------------
4318     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
4319       'Before Inserting the Receivables Adjustment Accounting Journals in GT Table Start Time:'
4320       || TO_CHAR(l_trace_time, 'DD-MON-YYY HH:MM:SS') );
4321 
4322     -- Start the stop watch
4323     l_trace_time := SYSDATE;
4324     -- Actual Insert Statement
4325     INSERT INTO okl_g_reports_gt
4326                 (
4327                   -- String Formatted Columns
4328                    value1_text
4329                   ,value2_text
4330                   ,value3_text
4331                   ,value4_text
4332                   ,value5_text
4333                   ,value6_text
4334                   ,value7_text
4335                   ,value8_text
4336                   ,value9_text
4337                   ,value10_text
4338                   ,value11_text
4339                   ,value12_text
4340                   ,value13_text
4341                   ,value14_text
4342                   ,value15_text
4343                   ,value16_text
4344                   ,value17_text
4345                   ,value18_text
4346                   -- Number Formatted Columns
4347                   ,value1_num
4348                   ,value2_num
4349                   ,value3_num
4350                   ,value4_num
4351                   ,value5_num
4352                   ,value6_num
4353                   ,value7_num
4354                   ,value8_num
4355                   ,value9_num
4356                   ,value10_num
4357                   ,value11_num
4358                   ,value12_num
4359                   ,value13_num
4360                   ,value15_num
4361                   -- Date Formatted Columns
4362                   ,value1_date
4363                   ,value2_date
4364                 )
4365       SELECT -- String Formatted Columns
4366              gl_detail_type_code         --value1_text
4367             ,trx_number                  --value2_text
4368             ,trx_type_name               --value3_text
4369             ,trx_application_name        --value4_text
4370             ,contract_number             --value5_text
4371             ,asset_number                --value6_text
4372             ,book_classification         --value7_text
4373             ,product_name                --value8_text
4374             ,trx_sty_name                --value9_text
4375             ,trx_sty_purpose             --value10_text
4376             ,currency_code               --value11_text
4377             ,trx_period_name             --value12_text -- Populated Only for Posted Entries
4378             ,trx_account_name            --value13_text
4379             ,trx_operating_unit_name     --value14_text
4380             ,NULL                        --value15_text -- Legal Entity Name
4381             ,trx_ledger_name             --value16_text
4382             ,trx_activity_code           --value17_text  -- Activity Code Add/Subtract for Dr/Cr
4383             ,trx_event_name              --value18_text
4384             -- Number Formatted Columns
4385             ,trx_dr_amount               --value1_num
4386             ,trx_cr_amount               --value2_num
4387             ,trx_net_amount              --value3_num
4388             ,code_combination_id         --value4_num
4389             ,code_combination_id         --value5_num
4390             ,trx_ledger_id               --value6_num
4391             ,trx_operating_unit_id       --value7_num
4392             ,trx_try_id                  --value8_num -- Transaction Type ID
4393             ,trx_khr_id                  --value9_num
4394             ,txl_asset_id                --value10_num
4395             ,trx_pdt_id                  --value11_num
4396             ,trx_sty_id                  --value12_num
4397             ,trx_application_id          --value13_num
4398             ,trx_xla_event_id            --value15_num
4399             -- Date Formatted Columns
4400             ,trx_date                    --value1_date
4401             ,gl_date                     --value2_date
4402         FROM
4403       (
4404         -- Query Segment to find the Posted Journal Entries from GL
4405         --  On behalf of OLM Lease Contract Receivables Adjustments
4406         SELECT 'GL_ACC_OLM_ENTRIES'                     gl_detail_type_code
4407               ,gh.currency_code                         currency_code
4408               ,app.application_name                     trx_application_name
4409               ,glcc.concatenated_segments               trx_account_name
4410               ,xvl.name                                 trx_event_name
4411               ,try.name                                 trx_type_name
4412               ,sty.NAME                                 trx_sty_name
4413               ,radj.adjustment_number                   trx_number
4414               ,chr.contract_number                      contract_number
4415               ,NULL                                     asset_number
4416               ,gts.deal_type                            book_classification
4417               ,pdt.NAME                                 product_name
4418               ,sty.styb_purpose_meaning                 trx_sty_purpose
4419               ,ou.name                                  trx_operating_unit_name
4420               ,ledger.name                              trx_ledger_name
4421               ,gh.period_name                           trx_period_name
4422               ,xl.code_combination_id                   code_combination_id
4423               -- Number Format Columns
4424               ,xl.entered_dr                            trx_dr_amount
4425               ,xl.entered_cr                            trx_cr_amount
4426               ,( NVL(xl.entered_dr,0) - NVL(xl.entered_cr,0) )
4427                                                         trx_net_amount
4428               ,DECODE( xl.entered_dr, NULL, 'SUBTRACT', 'ADD' )
4429                                                         trx_activity_code
4430               ,xl.ledger_id                             trx_ledger_id
4431               ,adj.org_id                               trx_operating_unit_id
4432               ,ajl.khr_id                               trx_khr_id
4433               ,ajl.kle_id                               txl_asset_id
4434               ,khr.pdt_id                               trx_pdt_id
4435               ,ajl.sty_id                               trx_sty_id
4436               ,xl.application_id                        trx_application_id
4437               ,xe.event_id                              trx_xla_event_id
4438               -- DATE Format Columns
4439               ,adj.transaction_date                     trx_date
4440               ,adj.gl_date                              gl_date
4441               -- Additional Columns
4442               ,adj.id                                   trx_id
4443               ,ajl.id                                   trx_txl_id
4444               ,adj.try_id                               trx_try_id
4445               ,NULL                                     trx_dist_id
4446         FROM    -- GL Tables: Import Reference, GL Header and Lines
4447                 gl_je_lines               gl
4448                ,gl_je_headers             gh
4449                ,gl_code_combinations_kfv  glcc
4450                ,gl_import_references      gi
4451                -- Code Combination GT Table
4452                ,okl_code_cmbns_gt         cc
4453                -- SLA Entities
4454                ,xla_ae_lines              xl
4455                ,xla_ae_headers            xh
4456                ,xla_distribution_links    xd
4457                ,xla_events                xe
4458                ,xla_event_types_vl        xvl
4459                -- AR Tables
4460                ,ar_distributions_all      rdist
4461                ,ar_adjustments_all        radj
4462                -- OLM Tables
4463                ,okl_txl_adjsts_lns_all_b  ajl
4464                ,okl_trx_ar_adjsts_all_b   adj
4465                ,okc_k_headers_all_b       chr
4466                ,okl_k_headers             khr
4467                ,okl_products              pdt
4468                ,okl_ae_tmpt_sets_all      aes
4469                ,okl_st_gen_tmpt_sets_all  gts
4470                ,okl_trx_types_v           try
4471                ,okl_strm_type_v           sty
4472                -- To fetch Names
4473                ,hr_operating_units        ou
4474                ,gl_ledgers                ledger
4475                ,fnd_application_vl        app
4476         WHERE
4477                -- Restrict the Code Combinations to the one setup on the Report
4478                gl.code_combination_id = cc.ccid AND
4479                -- GL Tables
4480                gl.ledger_id        = p_ledger_id
4481           AND  gh.je_header_id     = gl.je_header_id
4482           AND  gh.ledger_id        = gl.ledger_id
4483           AND  gh.je_source        = 'Receivables'
4484           AND  gh.status           =  'P'  -- Pick Only Posted Journals
4485           AND  glcc.code_combination_id = gl.code_combination_id
4486           AND  gi.je_header_id     = gh.je_header_id
4487           AND  gi.je_line_num      = gl.je_line_num
4488                -- GL to XLA Relations
4489           AND  xl.gl_sl_link_id    = gi.gl_sl_link_id
4490           AND  xl.gl_sl_link_table = gi.gl_sl_link_table
4491           AND  xl.ledger_id        = gl.ledger_id
4492                -- XLA Predicates
4493           AND  xl.ae_header_id     = xh.ae_header_id
4494           AND  xd.application_id   = 222  -- Restrict to Receivables Journals
4495           AND  xd.ae_header_id     = xh.ae_header_id
4496           AND  xd.ae_line_num      = xl.ae_line_num
4497           AND  xe.event_id         = xd.event_id
4498           AND  xe.event_status_code = 'P'
4499           AND  xe.application_id   = xvl.application_id
4500           AND  xvl.event_type_code = xe.event_type_code
4501                -- XLA to AR Predicates
4502           AND  xd.event_id = radj.event_id
4503           AND  xd.source_distribution_type = 'AR_DISTRIBUTIONS_ALL'
4504           AND  xd.source_distribution_id_num_1 = rdist.line_id
4505                -- AR Predicates
4506           AND  rdist.source_type = 'ADJ'  -- Assumption
4507           AND  radj.adjustment_id = rdist.source_id
4508                -- OKL to AR Predicates
4509           AND  ajl.receivables_adjustment_id = radj.adjustment_id
4510                -- OKL Predicates
4511           AND  adj.id     = ajl.adj_id
4512           AND  adj.try_id = try.id
4513           AND  ajl.sty_id = sty.id
4514           AND  ajl.khr_id = khr.id
4515           AND  chr.id     = khr.id
4516           AND  khr.pdt_id = pdt.id
4517           AND  pdt.aes_id = aes.id
4518           AND  aes.gts_id = gts.id
4519           -- Predicates to fetch the Names
4520           AND  ou.organization_id  = adj.org_id
4521           AND  ledger.ledger_id    = xl.ledger_id
4522           AND  app.application_id  = xe.application_id
4523           AND  gh.default_effective_date >= p_start_date
4524           AND  gh.default_effective_date <= p_end_date
4525           -- End of Query Segment to fetch Posted Acc. Entries from GL
4526           --  from Receivables for a Lease Contract Receivable Adjustment
4527     );
4528     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
4529       'After Inserting the Receivables Adjustment Accounting Journals Data in GT Table End Time:'
4530       || TO_CHAR(SYSDATE, 'DD-MON-YYY HH:MM:SS') );
4531     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
4532       'Time taken for Populating Receivables Adjustment Accounting Journals '
4533        || ( SYSDATE - l_trace_time ) * 86400 || ' Seconds' );
4534 
4535     -------------------------------------------------------------------------------------
4536     -- Queries to Populate Posted GL Journal Entry details
4537     --  related to AP Invoice Transactions
4538     -------------------------------------------------------------------------------------
4539     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
4540       'Before Inserting the Payables Invoice Accounting Journals in GT Table Start Time:'
4541       || TO_CHAR(l_trace_time, 'DD-MON-YYY HH:MM:SS') );
4542 
4543     -- Start the stop watch
4544     l_trace_time := SYSDATE;
4545     -- Actual Insert Statement
4546     INSERT INTO okl_g_reports_gt
4547                 (
4548                   -- String Formatted Columns
4549                    value1_text
4550                   ,value2_text
4551                   ,value3_text
4552                   ,value4_text
4553                   ,value5_text
4554                   ,value6_text
4555                   ,value7_text
4556                   ,value8_text
4557                   ,value9_text
4558                   ,value10_text
4559                   ,value11_text
4560                   ,value12_text
4561                   ,value13_text
4562                   ,value14_text
4563                   ,value15_text
4564                   ,value16_text
4565                   ,value17_text
4566                   ,value18_text
4567                   -- Number Formatted Columns
4568                   ,value1_num
4569                   ,value2_num
4570                   ,value3_num
4571                   ,value4_num
4572                   ,value5_num
4573                   ,value6_num
4574                   ,value7_num
4575                   ,value8_num
4576                   ,value9_num
4577                   ,value10_num
4578                   ,value11_num
4579                   ,value12_num
4580                   ,value13_num
4581                   ,value15_num
4582                   -- Date Formatted Columns
4583                   ,value1_date
4584                   ,value2_date
4585                 )
4586       SELECT -- String Formatted Columns
4587              gl_detail_type_code         --value1_text
4588             ,trx_number                  --value2_text
4589             ,trx_type_name               --value3_text
4590             ,trx_application_name        --value4_text
4591             ,contract_number             --value5_text
4592             ,asset_number                --value6_text
4593             ,book_classification         --value7_text
4594             ,product_name                --value8_text
4595             ,trx_sty_name                --value9_text
4596             ,trx_sty_purpose             --value10_text
4597             ,currency_code               --value11_text
4598             ,trx_period_name             --value12_text -- Populated Only for Posted Entries
4599             ,trx_account_name            --value13_text
4600             ,trx_operating_unit_name     --value14_text
4601             ,NULL                        --value15_text -- Legal Entity Name
4602             ,trx_ledger_name             --value16_text
4603             ,trx_activity_code           --value17_text  -- Activity Code Add/Subtract for Dr/Cr
4604             ,trx_event_name              --value18_text
4605             -- Number Formatted Columns
4606             ,trx_dr_amount               --value1_num
4607             ,trx_cr_amount               --value2_num
4608             ,trx_net_amount              --value3_num
4609             ,code_combination_id         --value4_num
4610             ,code_combination_id         --value5_num
4611             ,trx_ledger_id               --value6_num
4612             ,trx_operating_unit_id       --value7_num
4613             ,trx_try_id                  --value8_num -- Transaction Type ID
4614             ,trx_khr_id                  --value9_num
4615             ,txl_asset_id                --value10_num
4616             ,trx_pdt_id                  --value11_num
4617             ,trx_sty_id                  --value12_num
4618             ,trx_application_id          --value13_num
4619             ,trx_xla_event_id            --value15_num
4620             -- Date Formatted Columns
4621             ,trx_date                    --value1_date
4622             ,gl_date                     --value2_date
4623         FROM
4624       (
4625         -- Query Segment to find the Posted Journal Entries from GL
4626         --  On behalf of OLM Lease Contract Receivables Adjustments
4627         SELECT 'GL_ACC_OLM_ENTRIES'                     gl_detail_type_code
4628               ,gh.currency_code                         currency_code
4629               ,app.application_name                     trx_application_name
4630               ,glcc.concatenated_segments               trx_account_name
4631               ,xvl.name                                 trx_event_name
4632               ,try.name                                 trx_type_name
4633               ,sty.name                                 trx_sty_name
4634               ,inv.invoice_num                          trx_number
4635               ,chr.contract_number                      contract_number
4636               ,NULL                                     asset_number
4637               ,gts.deal_type                            book_classification
4638               ,pdt.NAME                                 product_name
4639               ,sty.styb_purpose_meaning                 trx_sty_purpose
4640               ,ou.name                                  trx_operating_unit_name
4641               ,ledger.name                              trx_ledger_name
4642               ,gh.period_name                           trx_period_name
4643               ,xl.code_combination_id                   code_combination_id
4644               -- Number Format Columns
4645               ,xl.entered_dr                            trx_dr_amount
4646               ,xl.entered_cr                            trx_cr_amount
4647               ,( NVL(xl.entered_dr,0) - NVL(xl.entered_cr,0) )
4648                                                         trx_net_amount
4649               ,DECODE( xl.entered_dr, NULL, 'SUBTRACT', 'ADD' )
4650                                                         trx_activity_code
4651               ,xl.ledger_id                             trx_ledger_id
4652               ,tap.org_id                               trx_operating_unit_id
4653               ,tpl.khr_id                               trx_khr_id
4654               ,tpl.kle_id                               txl_asset_id
4655               ,khr.pdt_id                               trx_pdt_id
4656               ,tpl.sty_id                               trx_sty_id
4657               ,xl.application_id                        trx_application_id
4658               ,xe.event_id                              trx_xla_event_id
4659               -- DATE Format Columns
4660               ,inv.invoice_date                         trx_date
4661               ,inv.invoice_date                         gl_date
4662               -- Additional Columns
4663               ,tap.id                                   trx_id
4664               ,tpl.id                                   trx_txl_id
4665               ,tap.try_id                               trx_try_id
4666               ,invdist.invoice_distribution_id          trx_dist_id
4667         FROM    -- GL Tables: Import Reference, GL Header and Lines
4668                 gl_je_lines               gl
4669                ,gl_je_headers             gh
4670                ,gl_code_combinations_kfv  glcc
4671                ,gl_import_references      gi
4672                -- Code Combination GT Table
4673                ,okl_code_cmbns_gt         cc
4674                -- SLA Entities
4675                ,xla_ae_lines              xl
4676                ,xla_ae_headers            xh
4677                ,xla_distribution_links    xd
4678                ,xla_events                xe
4679                ,xla_event_types_vl        xvl
4680                -- AP Tables
4681                ,ap_invoice_distributions_all   invdist
4682                ,ap_invoice_lines_all      lin
4683                ,ap_invoices_all           inv
4684                -- OLM Tables
4685                ,okl_txl_ap_inv_lns_all_b  tpl
4686                ,okl_trx_ap_invs_all_b     tap
4687                ,okc_k_headers_all_b       chr
4688                ,okl_k_headers             khr
4689                ,okl_products              pdt
4690                ,okl_ae_tmpt_sets_all      aes
4691                ,okl_st_gen_tmpt_sets_all  gts
4692                ,okl_trx_types_v           try
4693                ,okl_strm_type_v           sty
4694                -- To fetch Names
4695                ,hr_operating_units        ou
4696                ,gl_ledgers                ledger
4697                ,fnd_application_vl        app
4698         WHERE
4699                -- Restrict the Code Combinations to the one setup on the Report
4700                gl.code_combination_id = cc.ccid AND
4701                -- GL Tables
4702                gl.ledger_id        = p_ledger_id
4703           AND  gh.je_header_id     = gl.je_header_id
4704           AND  gh.ledger_id        = gl.ledger_id
4705           AND  gh.je_source        = 'Payables'
4706           AND  gh.status           =  'P'  -- Pick Only Posted Journals
4707           AND  glcc.code_combination_id = gl.code_combination_id
4708           AND  gi.je_header_id     = gh.je_header_id
4709           AND  gi.je_line_num      = gl.je_line_num
4710                -- GL to XLA Relations
4711           AND  xl.gl_sl_link_id    = gi.gl_sl_link_id
4712           AND  xl.gl_sl_link_table = gi.gl_sl_link_table
4713           AND  xl.ledger_id        = gl.ledger_id
4714                -- XLA Predicates
4715           AND  xl.ae_header_id     = xh.ae_header_id
4716           AND  xd.application_id   = 200  -- Restrict to Payables Journals
4717           AND  xd.ae_header_id     = xh.ae_header_id
4718           AND  xd.ae_line_num      = xl.ae_line_num
4719           AND  xe.event_id         = xd.event_id
4720           AND  xe.event_status_code = 'P'
4721           AND  xe.application_id   = xvl.application_id
4722           AND  xvl.event_type_code = xe.event_type_code
4723                -- XLA to AP Predicates
4724           AND  xd.event_id                 = invdist.accounting_event_id
4725           AND  xd.source_distribution_type = 'AP_INV_DIST'
4726           AND  xd.source_distribution_id_num_1 = invdist.invoice_distribution_id
4727                -- AP Predicates
4728           AND invdist.line_type_lookup_code = 'ITEM' -- Need to verify
4729           AND invdist.invoice_line_number = lin.line_number
4730           AND invdist.invoice_id = lin.invoice_id
4731           AND lin.invoice_id     = inv.invoice_id
4732                -- OKL to AP Predicates
4733           AND lin.application_id = 540
4734           AND lin.product_table  = 'OKL_TXL_AP_INV_LNS_ALL_B'
4735           AND lin.reference_key1 = tpl.id
4736                -- OKL Predicates
4737           AND  tap.id     = tpl.tap_id
4738           AND  tap.try_id = try.id
4739           AND  tpl.sty_id = sty.id
4740           AND  tpl.khr_id = khr.id
4741           AND  chr.id     = khr.id
4742           AND  khr.pdt_id = pdt.id
4743           AND  pdt.aes_id = aes.id
4744           AND  aes.gts_id = gts.id
4745           -- Predicates to fetch the Names
4746           AND  ou.organization_id  = tap.org_id
4747           AND  ledger.ledger_id    = xl.ledger_id
4748           AND  app.application_id  = xe.application_id
4749           AND  gh.default_effective_date >= p_start_date
4750           AND  gh.default_effective_date <= p_end_date
4751           -- End of Query Segment to fetch Posted Acc. Entries from GL
4752           --  from Payables for a Lease Contract Payables Invoice Transaction
4753     );
4754     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
4755       'After Inserting the Payables Invoice Accounting Journals Data in GT Table End Time:'
4756       || TO_CHAR(SYSDATE, 'DD-MON-YYY HH:MM:SS') );
4757     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
4758       'Time taken for Populating Payables Invoice  Accounting Journals '
4759        || ( SYSDATE - l_trace_time ) * 86400 || ' Seconds' );
4760 
4761    -- Important: Step 1
4762    --  Till Now we have fetched all the Journal Entries that can be linked
4763    --   back to an OLM Transaction.
4764    --  For the Reconciliation Report, we need to verify each and every Journal
4765    --   Entry fetched and verify the following criteria:
4766    --   a. If the Journal Entry is related to Transaction, Stream Type combination
4767    --        not specified in the Report. Move the GL Type to GL_ACC_NON_SPEC_CRITERIA
4768    --   b. If its a Debit Journal, find that the Corresponding Transaction
4769    --        has to be mentioned in report definition with "Add" flag
4770    --        and for Credit Journal, Trx. should be having "Subtract" flag
4771    --        Otherwsie also, change this Journal Type to GL_ACC_NON_SPEC_CRITERIA
4772    --  c. If the Journal is for a Contract whose product is not present in
4773    --        okl_g_products_gt, change this Journal Type to GL_ACC_NON_SPEC_CRITERIA
4774    -- Step 1:
4775    UPDATE  okl_g_reports_gt gt
4776       SET  value1_text = 'GL_ACC_NON_SPEC_CRITERIA'
4777     WHERE  value1_text = 'GL_ACC_OLM_ENTRIES'
4778            -- Need to Cover Two Cases:
4779            --  Case 1: When Only the Transaction Type is mentioned
4780            --  Case 2: When Combination of Transaction and Stream Type is mentioned
4781       AND
4782       (
4783           -- Step 1: Case (a)
4784            NOT EXISTS
4785            (
4786               SELECT  1
4787                 FROM  okl_report_trx_params trep
4788                WHERE  trep.try_id = gt.value8_num  -- trx_try_id
4789                  AND  NVL( trep.sty_id,
4790                            gt.value12_num -- Stream Type Id
4791                           ) = gt.value12_num -- Stream Type Id
4792                  -- Step 1: Case (b)
4793                  AND  trep.add_substract_code = value17_text -- Debit/Credit or Add/Subtract
4794                  AND  trep.report_id = p_report_id
4795            )
4796            -- Step 1: Case (C)
4797            OR value11_num -- Product Id
4798               NOT IN
4799              ( SELECT product_id
4800                  FROM okl_rep_products_gt pdt_gt
4801              )
4802       );
4803 
4804    -- Important: Step 2
4805    -- Even if the Journal hasn't moved to GL_ACC_NON_SPEC_CRITERIA, means Journal
4806    --  is in the specified Criteria, check the Transaction Date
4807    -- If ithe Transaction Date is not lying in between the start and End Date
4808    --   change this Journal Type to GL_ACC_NON_SPEC_PERIOD
4809    -- Step 2:
4810    UPDATE  okl_g_reports_gt gt
4811       SET  value1_text = 'GL_ACC_NON_SPEC_PERIOD'
4812           ,value12_text -- trx_period_name
4813            = (
4814                  SELECT  per.period_name
4815                    FROM  gl_periods per
4816                   WHERE  per.period_set_name = l_period_set_name
4817                     AND  per.START_DATE <= value1_date
4818                     AND  per.end_date >=  value1_date
4819                     AND  per.adjustment_period_flag = 'N'
4820                     AND  period_type = l_period_type
4821              )
4822     WHERE  value1_text = 'GL_ACC_OLM_ENTRIES'
4823       AND
4824       (
4825            p_start_date > value1_date -- Transaction Date
4826         OR p_end_date   < value1_date
4827       );
4828 
4829     -------------------------------------------------------------------------------------
4830     -- Queries to Populate Posted GL Journal Entry details from AR and AP Applications
4831     --  which are not related to a Lease Contract
4832     -- As part of the Query segment above for GL_ACC_OLM_ENTRIES, we have fetched Journals
4833     --  from AR and AP which are related to OLM.
4834     -- Now we fetch from AR and AP all the Events, which are not fetched as part of above
4835     --  query segment, but got posted in GL from these applications.
4836     -------------------------------------------------------------------------------------
4837     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
4838       'Before Inserting the Accounting Journals AR and AP Applications but not related to OLM:'
4839       || TO_CHAR(l_trace_time, 'DD-MON-YYY HH:MM:SS') );
4840     -- Start the stop watch
4841     l_trace_time := SYSDATE;
4842     -- Actual Insert Statement
4843     INSERT INTO okl_g_reports_gt
4844                 (
4845                   -- String Formatted Columns
4846                    value1_text
4847                   ,value2_text
4848                   ,value3_text
4849                   ,value4_text
4850                   ,value5_text
4851                   ,value6_text
4852                   ,value7_text
4853                   ,value8_text
4854                   ,value9_text
4855                   ,value10_text
4856                   ,value11_text
4857                   ,value12_text
4858                   ,value13_text
4859                   ,value14_text
4860                   ,value15_text
4861                   ,value16_text
4862                   ,value17_text
4863                   ,value18_text
4864                   -- Number Formatted Columns
4865                   ,value1_num
4866                   ,value2_num
4867                   ,value3_num
4868                   ,value4_num
4869                   ,value5_num
4870                   ,value6_num
4871                   ,value7_num
4872                   ,value8_num
4873                   ,value9_num
4874                   ,value10_num
4875                   ,value11_num
4876                   ,value12_num
4877                   ,value13_num
4878                   ,value15_num
4879                   -- Date Formatted Columns
4880                   ,value1_date
4881                   ,value2_date
4882                 )
4883       SELECT -- String Formatted Columns
4884              gl_detail_type_code         --value1_text
4885             ,trx_number                  --value2_text
4886             ,trx_type_name               --value3_text
4887             ,trx_application_name        --value4_text
4888             ,contract_number             --value5_text
4889             ,asset_number                --value6_text
4890             ,book_classification         --value7_text
4891             ,product_name                --value8_text
4892             ,trx_sty_name                --value9_text
4893             ,trx_sty_purpose             --value10_text
4894             ,currency_code               --value11_text
4895             ,trx_period_name             --value12_text -- Populated Only for Posted Entries
4896             ,trx_account_name            --value13_text
4897             ,trx_operating_unit_name     --value14_text
4898             ,NULL                        --value15_text -- Legal Entity Name
4899             ,trx_ledger_name             --value16_text
4900             ,trx_activity_code           --value17_text  -- Activity Code Add/Subtract for Dr/Cr
4901             ,trx_event_name              --value18_text
4902             -- Number Formatted Columns
4903             ,trx_dr_amount               --value1_num
4904             ,trx_cr_amount               --value2_num
4905             ,trx_net_amount              --value3_num
4906             ,code_combination_id         --value4_num
4907             ,code_combination_id         --value5_num
4908             ,trx_ledger_id               --value6_num
4909             ,trx_operating_unit_id       --value7_num
4910             ,trx_try_id                  --value8_num -- Transaction Type ID
4911             ,trx_khr_id                  --value9_num
4912             ,txl_asset_id                --value10_num
4913             ,trx_pdt_id                  --value11_num
4914             ,trx_sty_id                  --value12_num
4915             ,trx_application_id          --value13_num
4916             ,trx_xla_event_id            --value15_num
4917             -- Date Formatted Columns
4918             ,trx_date                    --value1_date
4919             ,gl_date                     --value2_date
4920         FROM
4921       (
4922         -- Query Segment to find the Posted Journal Entries from
4923         --  AR and AP applications but not related to Lease
4924         SELECT 'GL_ACC_OTHER_APPS'                      gl_detail_type_code
4925               ,gh.currency_code                         currency_code
4926               ,app.application_name                     trx_application_name
4927               ,glcc.concatenated_segments               trx_account_name
4928               ,xvl.name                                 trx_event_name
4929               ,NULL                                     trx_type_name
4930               ,NULL                                     trx_sty_name
4931               ,xte.transaction_number                   trx_number
4932               ,NULL                                     contract_number
4933               ,NULL                                     asset_number
4934               ,NULL                                     book_classification
4935               ,NULL                                     product_name
4936               ,NULL                                     trx_sty_purpose
4937               ,NULL                                     trx_operating_unit_name
4938               ,ledger.name                              trx_ledger_name
4939               ,gh.period_name                           trx_period_name
4940               ,xl.code_combination_id                   code_combination_id
4941               -- Number Format Columns
4942               ,xl.entered_dr                            trx_dr_amount
4943               ,xl.entered_cr                            trx_cr_amount
4944               ,( NVL(xl.entered_dr,0) - NVL(xl.entered_cr,0) )
4945                                                         trx_net_amount
4946               ,NULL                                     trx_activity_code
4947               ,xl.ledger_id                             trx_ledger_id
4948               ,NULL                                     trx_operating_unit_id
4949               ,NULL                                     trx_khr_id
4950               ,NULL                                     txl_asset_id
4951               ,NULL                                     trx_pdt_id
4952               ,NULL                                     trx_sty_id
4953               ,xl.application_id                        trx_application_id
4954               ,xe.event_id                              trx_xla_event_id
4955               -- DATE Format Columns
4956               ,xe.transaction_date                      trx_date
4957               ,NULL                                     gl_date
4958               -- Additional Columns
4959               ,NULL                                     trx_id
4960               ,NULL                                     trx_txl_id
4961               ,NULL                                     trx_try_id
4962               ,NULL                                     trx_dist_id
4963         FROM    -- GL Tables: Import Reference, GL Header and Lines
4964                 gl_je_lines               gl
4965                ,gl_je_headers             gh
4966                ,gl_code_combinations_kfv  glcc
4967                ,gl_import_references      gi
4968                -- Code Combination GT Table
4969                ,okl_code_cmbns_gt         cc
4970                -- SLA Entities
4971                ,xla_ae_lines              xl
4972                ,xla_ae_headers            xh
4973                ,xla_events                xe
4974                ,xla_event_types_vl        xvl
4975                ,xla_transaction_entities  xte
4976                ,gl_ledgers                ledger
4977                ,fnd_application_vl        app
4978         WHERE
4979                -- Restrict the Code Combinations to the one setup on the Report
4980                gl.code_combination_id = cc.ccid AND
4981                -- GL Tables
4982                gl.ledger_id        = p_ledger_id
4983           AND  gh.je_header_id     = gl.je_header_id
4984           AND  gh.ledger_id        = gl.ledger_id
4985           AND  gh.status           =  'P'  -- Pick Only Posted Journals
4986           AND  glcc.code_combination_id = gl.code_combination_id
4987           AND  gi.je_header_id     = gh.je_header_id
4988           AND  gi.je_line_num      = gl.je_line_num
4989                -- GL to XLA Relations
4990           AND  xl.gl_sl_link_id    = gi.gl_sl_link_id
4991           AND  xl.gl_sl_link_table = gi.gl_sl_link_table
4992           AND  xl.ledger_id        = gl.ledger_id
4993                -- XLA Predicates
4994           AND  xl.ae_header_id     = xh.ae_header_id
4995           AND  xe.event_id         = xh.event_id
4996           AND  xe.application_id   = xvl.application_id
4997           AND  xvl.event_type_code = xe.event_type_code
4998           AND  xte.entity_id       = xe.entity_id
4999           AND  xte.application_id  = xe.application_id
5000           -- Important: Fetch Only Accounting Events from AR and AP Only
5001           AND  xe.application_id
5002                IN
5003                  (  200  -- Payables
5004                    ,222  -- Receivables
5005                  )
5006           --  These Accounting Events should not have been fetched
5007           --  as part of Entries related to Lease fetched above
5008           AND  xe.event_id
5009                NOT IN
5010                  (
5011                     SELECT  DISTINCT gt.value15_num -- trx_xla_event_id
5012                       FROM  okl_g_reports_gt  gt
5013                      WHERE  gt.value1_text IN
5014                             (
5015                               'GL_ACC_OLM_ENTRIES'
5016                              ,'GL_ACC_NON_SPEC_CRITERIA'
5017                              ,'GL_ACC_NON_SPEC_PERIOD'
5018                             )
5019                  )
5020           -- Predicates to fetch the Names
5021           AND  ledger.ledger_id    = xl.ledger_id
5022           AND  app.application_id  = xe.application_id
5023           -- Restrict the Journal Entries to be in between Start and End Dates
5024           AND  gl.period_name IN
5025                (
5026                  SELECT  per.period_name
5027                    FROM  gl_periods per
5028                   WHERE  per.period_set_name = l_period_set_name
5029                     AND  per.START_DATE >= p_start_date
5030                     AND  per.end_date <=  p_end_date
5031                     AND  per.adjustment_period_flag = 'N'
5032                     AND  period_type = l_period_type
5033                )
5034           -- End of Query Segment to fetch Posted Acc. Entries in GL
5035           --  from applications other than GL, OLM, FA, AR and AP
5036       );
5037     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
5038       'After Inserting the Accounting Journals AR and AP Applications but not related to OLM:'
5039       || TO_CHAR(SYSDATE, 'DD-MON-YYY HH:MM:SS') );
5040     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
5041       'Time taken for Populating Accounting Journals AR and AP Applications but not related to OLM '
5042        || ( SYSDATE - l_trace_time ) * 86400 || ' Seconds' );
5043 
5044     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'P',
5045       'End: ' || l_api_name ||'(-)' );
5046     -- Set the Return Status and return back
5047     x_return_status := l_return_status;
5048   EXCEPTION
5049     WHEN OTHERS
5050     THEN
5051       x_return_status := okl_api.g_ret_sts_unexp_error;
5052       okl_api.set_message(p_app_name     => g_app_name,
5053                           p_msg_name     => g_unexpected_error,
5054                           p_token1       => g_sqlcode_token,
5055                           p_token1_value => SQLCODE,
5056                           p_token2       => g_sqlerrm_token,
5057                           p_token2_value => SQLERRM);
5058   END populate_acc_data;
5059 
5060 
5061   -- Start of comments
5062   --
5063   -- Function Name   : populate_gl_balances
5064   -- Description     : API to populate the GL Opening and Closing Balances
5065   --
5066   -- Business Rules  : Called from prepare_gross_rec_report
5067   -- Parameters      :
5068   -- Version         : 1.0
5069   -- History         : Ravindranath Gooty created.
5070   -- End of comments
5071 
5072   PROCEDURE populate_gl_balances(
5073               p_api_version   IN         NUMBER
5074              ,p_init_msg_list IN         VARCHAR2
5075              ,x_return_status OUT NOCOPY VARCHAR2
5076              ,x_msg_count     OUT NOCOPY NUMBER
5077              ,x_msg_data      OUT NOCOPY VARCHAR2
5078              ,p_ledger_id     IN         NUMBER
5079              ,p_period_from   IN         VARCHAR2
5080              ,p_period_to     IN         VARCHAR2)
5081   IS
5082     -----------------------------------------------------------------
5083     -- Declare Process Variable
5084     -----------------------------------------------------------------
5085     l_api_version      CONSTANT NUMBER         := 1;
5086     l_api_name         CONSTANT VARCHAR2(30)   := 'populate_gl_balances';
5087     l_return_status    VARCHAR2(1)             := OKL_API.G_RET_STS_SUCCESS;
5088     -- Debug related parameters
5089     l_module CONSTANT fnd_log_messages.module%TYPE := G_MODULE || l_api_name;
5090     l_debug_enabled       VARCHAR2(10);
5091     is_debug_procedure_on BOOLEAN;
5092     is_debug_statement_on BOOLEAN;
5093     -- Local Variables
5094     l_trace_time          DATE;
5095   BEGIN
5096     l_debug_enabled := okl_debug_pub.check_log_enabled;
5097     -- check for logging on PROCEDURE level
5098     is_debug_procedure_on := okl_debug_pub.check_log_on(l_module,FND_LOG.LEVEL_PROCEDURE);
5099     -- check for logging on STATEMENT level
5100     is_debug_statement_on := okl_debug_pub.check_log_on(l_module,FND_LOG.LEVEL_STATEMENT);
5101     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'P',
5102                'begin debug OKLRRPTB.pls call ' || l_api_name);
5103     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
5104       'Begin: ' || l_api_name ||'(+)' );
5105     l_return_status := okl_api.g_ret_sts_success;
5106     -- By now we have the List of Eligible Products and Code Combinations Available
5107     -- in the corresponding _GT tables
5108     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
5109       'Input Parameters ' ||
5110       ' p_ledger_id   =' || p_ledger_id  ||
5111       ' p_period_from =' || p_period_from ||
5112       ' p_period_to   =' || p_period_to );
5113 
5114     -------------------------------------------------------------------------------------
5115     -- Query segment to Insert the GL Opening Balances ...
5116     -------------------------------------------------------------------------------------
5117     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
5118       'Before Inserting the GL Opening Balances Start Time:'
5119       || TO_CHAR(l_trace_time, 'DD-MON-YYY HH:MM:SS') );
5120 
5121     -- Start the stop watch
5122     l_trace_time := SYSDATE;
5123     -- Actual Insert Statement
5124     INSERT INTO okl_g_reports_gt
5125                 (
5126                   -- String Formatted Columns
5127                    value1_text
5128                   ,value11_text
5129                   ,value12_text
5130                   ,value13_text
5131                   -- Number Formatted Columns
5132                   ,value1_num
5133                   ,value2_num
5134                 )
5135       SELECT -- String Formatted Columns
5136              'GL_OPENING_BALANCE'       --value1_text
5137             ,currency_code              --value11_text
5138             ,period_name                --value12_text
5139             ,account_number             --value13_text -- Stores the Segment Number
5140             -- Number Formatted Columns
5141             ,SUM(opening_bal_trx_curr)  --value1_num
5142             ,SUM(opening_bal_led_curr)  --value2_num
5143         FROM
5144       (
5145         -- Query Segment to fetch the GL Balances for Non-Ledger Currency
5146         SELECT  cc_gt.account_number          account_number -- Stores the Segment Number
5147                ,cc_gt.ccid                    ccid
5148                ,bal.currency_code             currency_code
5149                ,bal.period_name               period_name
5150                ,( begin_balance_dr
5151                   - begin_balance_cr )        opening_bal_trx_curr
5152                ,( begin_balance_dr_beq
5153                   - begin_balance_cr_beq )    opening_bal_led_curr
5154           FROM  gl_balances                   bal
5155                ,gl_ledgers                    led
5156                ,okl_code_cmbns_gt             cc_gt
5157          WHERE  led.ledger_id   = p_ledger_id
5158            AND  bal.ledger_id   = led.ledger_id
5159            -- opening Balance as on Period Start
5160            AND  bal.translated_flag IS NULL
5161            AND  bal.currency_code <> 'STAT'
5162            AND  bal.period_name = p_period_from
5163            AND  bal.actual_flag = 'A'
5164            AND  bal.currency_code <> led.currency_code
5165            AND  bal.currency_code <> 'STAT'
5166            AND  bal.code_combination_id = cc_gt.ccid
5167       UNION ALL
5168         -- Query Segment to fetch the GL Balances for Ledger Currency
5169         -- Logic: GL Balances store thes Accumulated Balance of all Currencies
5170         --        in the Ledger Currency. Hence need to deduct the non-ledger
5171         --        currency Total from the GL Balance
5172         SELECT  account_number              account_number -- Stores the Segment Number
5173                ,ccid                        ccid
5174                ,currency_code               currency_code
5175                ,period_name                 period_name
5176                ,nvl(opening_bal_led_curr,0) opening_bal_trx_curr
5177                ,nvl(opening_bal_led_curr,0) opening_bal_led_curr
5178           FROM
5179           (
5180             SELECT  cc_gt.account_number      account_number -- Segment Number
5181                    ,cc_gt.ccid                ccid
5182                    ,bal.currency_code         currency_code
5183                    ,bal.period_name           period_name
5184                    ,( begin_balance_dr
5185                       - begin_balance_cr )    opening_bal_led_curr
5186               FROM  gl_balances               bal
5187                    ,gl_ledgers                led
5188                    ,okl_code_cmbns_gt         cc_gt
5189              WHERE  led.ledger_id   = p_ledger_id
5190                AND  bal.ledger_id   = led.ledger_id
5191                -- opening Balance as on Period Start
5192                AND  bal.translated_flag IS NULL
5193                AND  bal.currency_code <> 'STAT'
5194                AND  bal.period_name = p_period_from
5195                AND  bal.actual_flag = 'A'
5196                AND  bal.currency_code = led.currency_code
5197                AND  bal.code_combination_id = cc_gt.ccid
5198           )
5199     )
5200     GROUP BY -- String Formatted Columns
5201        'GL_OPENING_BALANCE'
5202       ,currency_code
5203       ,period_name
5204       ,account_number
5205     ; -- End of Open Balances Query
5206 
5207    -- Logic:
5208    --  Till now, we have fetched the GL Opening Balances for both the
5209    --   Ledger Currency and Non-Ledger currencies.
5210    --  But the Ledger Currency Balances in GL is sum of Balances in all the
5211    --    Transactional Currencies too. Hence, need to deduct the Sum of
5212    --    Opening Balances for all non Ledger Currencies from the Ledger Currencies
5213    --    if exists.
5214    UPDATE okl_g_reports_gt lc
5215        SET  value1_num =
5216             value1_num -
5217              NVL(
5218               (
5219                 SELECT  SUM(value2_num) -- Sum of Balance in Ledger Currency
5220                   FROM  okl_g_reports_gt nlc
5221                  WHERE  nlc.value1_text = 'GL_OPENING_BALANCE'
5222                     AND nlc.value12_text = lc.value12_text -- Period Name
5223                     AND nlc.value13_text = lc.value13_text -- Account Number
5224                     AND nlc.value11_text <> lc.value11_text
5225                ), 0 )
5226            ,value2_num =
5227             value2_num -
5228              NVL(
5229                (
5230                 SELECT  SUM(value2_num) -- Sum of Balance in Ledger Currency
5231                   FROM  okl_g_reports_gt nlc
5232                  WHERE  nlc.value1_text = 'GL_OPENING_BALANCE'
5233                     AND nlc.value12_text = lc.value12_text -- Period Name
5234                     AND nlc.value13_text = lc.value13_text -- Account Number
5235                     AND nlc.value11_text <> lc.value11_text
5236                ), 0 )
5237      -- Pick only the Ledger Currency GL Opening Balances
5238      WHERE value1_text = 'GL_OPENING_BALANCE' --value1_text
5239        AND value11_text IN
5240        (
5241           SELECT  currency_code
5242             FROM  gl_ledgers
5243            WHERE  ledger_id = p_ledger_id
5244        );
5245 
5246    --Bug:7535993 by nikshah
5247    --If no records found for OPENING BALANCE in GL
5248    --for natural account, enter 0 amount with ledger currency
5249     INSERT INTO okl_g_reports_gt
5250                 (
5251                   -- String Formatted Columns
5252                    value1_text
5253                   ,value11_text
5254                   ,value12_text
5255                   ,value13_text
5256                   -- Number Formatted Columns
5257                   ,value1_num
5258                   ,value2_num
5259                 )
5260       SELECT -- String Formatted Columns
5261              'GL_OPENING_BALANCE'       --value1_text
5262             ,(select currency_code
5263               from gl_ledgers
5264               where ledger_id = p_ledger_id)              --value11_text
5265             ,p_period_from                --value12_text
5266             ,cgt.account_number           --value13_text -- Stores the Segment Number
5267             ,0  --value1_num
5268             ,0  --value2_num
5269       FROM (select distinct account_number
5270             from okl_code_cmbns_gt) cgt
5271       WHERE cgt.account_number not in (select value13_text
5272                                        from okl_g_reports_gt
5273                                        where value1_text = 'GL_OPENING_BALANCE'
5274                                        );
5275 
5276     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
5277       'After Inserting the GL Opening Balances End Time:'
5278       || TO_CHAR(SYSDATE, 'DD-MON-YYY HH:MM:SS') );
5279     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
5280       'Time taken for Populating GL Opening Balances '
5281        || ( SYSDATE - l_trace_time ) * 86400 || ' Seconds' );
5282 
5283     -------------------------------------------------------------------------------------
5284     -- Query segment to Insert the GL Period Activity ... In case of income report
5285     -------------------------------------------------------------------------------------
5286     IF p_report_type_code = 'INCOME' THEN
5287 
5288     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
5289       'Before Inserting the GL Period Activity Start Time:'
5290       || TO_CHAR(l_trace_time, 'DD-MON-YYY HH:MM:SS') );
5291 
5292     -- Start the stop watch
5293     l_trace_time := SYSDATE;
5294     -- Actual Insert Statement
5295 
5296     INSERT INTO okl_g_reports_gt
5297                 (
5298                   -- String Formatted Columns
5299                    value1_text
5300                   ,value11_text
5301                   ,value12_text
5302                   ,value13_text
5303                   -- Number Formatted Columns
5304                   ,value1_num
5305                   ,value2_num
5306                 )
5307       SELECT -- String Formatted Columns
5308              'GL_PERIOD_ACTIVITY'       --value1_text
5309             ,currency_code              --value11_text
5310             ,period_name                --value12_text
5311             ,account_number             --value13_text -- Stores the Segment Number
5312             ,SUM(period_activity_trx_curr)  --value1_num
5313             ,SUM(period_activity_led_curr)  --value2_num
5314         FROM
5315       (
5316         -- Query Segment to fetch the GL Closing Balances for Non-Ledger Currency
5317         SELECT  cc_gt.account_number          account_number -- Stores the Segment Number
5318                ,cc_gt.ccid                    ccid
5319                ,bal.currency_code             currency_code
5320                ,bal.period_name               period_name
5321                ,period_net_dr -
5322                 period_net_cr                 period_activity_trx_curr
5323                ,period_net_dr_beq -
5324                 period_net_cr_beq             period_activity_led_curr
5325           FROM  gl_balances                   bal
5326                ,gl_ledgers                    led
5327                ,okl_code_cmbns_gt             cc_gt
5328                ,gl_period_statuses        gps
5329          WHERE  led.ledger_id   = p_ledger_id
5330            AND  bal.ledger_id   = led.ledger_id
5331            AND  gps.application_id = 101
5332            and  gps.ledger_id = led.ledger_id
5333            and  bal.period_name = gps.period_name
5334            and  gps.effective_period_num >= ( select pf.effective_period_num
5335                                               from   gl_period_statuses pf
5336                                               where  pf.application_id = 101
5337                                                 and  pf.ledger_id = p_ledger_id
5338                                                 and  pf.period_name = p_period_from
5339                                                 and  rownum < 2
5340                                             )
5341            and  gps.effective_period_num <= ( select pt.effective_period_num
5342                                               from   gl_period_statuses pt
5343                                               where  pt.application_id = 101
5344                                                 and  pt.ledger_id = p_ledger_id
5345                                                 and  pt.period_name = p_period_to
5346                                                 and  rownum < 2
5347                                              )
5348            AND  bal.actual_flag = 'A'
5349            AND  bal.translated_flag IS NULL
5350            AND  bal.currency_code <> 'STAT'
5351            AND  bal.currency_code <> led.currency_code
5352            AND  bal.currency_code <> 'STAT'
5353            AND  bal.code_combination_id = cc_gt.ccid
5354       UNION ALL
5355         -- Query Segment to fetch the GL Closing Balances for Ledger Currency
5356         -- Logic: GL Balances store thes Accumulated Balance of all Currencies
5357         --        in the Ledger Currency. Hence need to deduct the non-ledger
5358         --        currency Total from the GL Balance
5359         SELECT  account_number              account_number -- Stores the Segment Number
5360                ,ccid                        ccid
5361                ,currency_code               currency_code
5362                ,period_name                 period_name
5363                ,nvl(period_activity_led_curr,0) period_activity_trx_curr
5364                ,nvl(period_activity_led_curr,0) period_activity_led_curr
5365           FROM
5366           (
5367             SELECT  cc_gt.account_number      account_number -- Segment Number
5368                    ,cc_gt.ccid                ccid
5369                    ,bal.currency_code         currency_code
5370                    ,bal.period_name           period_name
5371                    ,period_net_dr -
5372                     period_net_cr             period_activity_led_curr
5373               FROM  gl_balances               bal
5374                    ,gl_ledgers                led
5375                    ,okl_code_cmbns_gt         cc_gt
5376                    ,gl_period_statuses        gps
5377              WHERE  led.ledger_id   = p_ledger_id
5378                AND  gps.application_id = 101
5379                and  gps.ledger_id = led.ledger_id
5380                and  bal.period_name = gps.period_name
5381                and  gps.effective_period_num >= ( select pf.effective_period_num
5382                                                   from   gl_period_statuses pf
5383                                                   where  pf.application_id = 101
5384                                                     and  pf.ledger_id = p_ledger_id
5385                                                     and  pf.period_name = p_period_from
5386                                                     and  rownum < 2
5387                                                 )
5388                and  gps.effective_period_num <= ( select pt.effective_period_num
5389                                                   from   gl_period_statuses pt
5390                                                   where  pt.application_id = 101
5391                                                     and  pt.ledger_id = p_ledger_id
5392                                                     and  pt.period_name = p_period_to
5393                                                     and  rownum < 2
5394                                                  )
5395                AND  bal.ledger_id   = led.ledger_id
5396                AND  bal.translated_flag IS NULL
5397                AND  bal.currency_code <> 'STAT'
5398                AND  bal.actual_flag = 'A'
5399                AND  bal.currency_code = led.currency_code
5400                AND  bal.code_combination_id = cc_gt.ccid
5401           )
5402     )
5403     GROUP BY -- String Formatted Columns
5404        'GL_PERIOD_ACTIVITY'
5405       ,currency_code
5406       ,period_name
5407       ,account_number
5408     ;
5409 
5410    -- Logic:
5411    --  Till now, we have fetched the GL Period Activity for both the
5412    --   Ledger Currency and Non-Ledger currencies.
5413    --  But the Ledger Currency Balances in GL is sum of Balances in all the
5414    --    Transactional Currencies too. Hence, need to deduct the Sum of
5415    --    Period Activity for all non Ledger Currencies from the Ledger Currencies
5416    --    if exists.
5417    UPDATE okl_g_reports_gt lc
5418        SET  value1_num =
5419             value1_num -
5420              NVL(
5421               (
5422                 SELECT  SUM(value2_num) -- Sum of Balance in Ledger Currency
5423                   FROM  okl_g_reports_gt nlc
5424                  WHERE  nlc.value1_text = 'GL_PERIOD_ACTIVITY'
5425                     AND nlc.value12_text = lc.value12_text -- Period Name
5426                     AND nlc.value13_text = lc.value13_text -- Account Number
5427                     AND nlc.value11_text <> lc.value11_text
5428                ), 0)
5429            ,value2_num =
5430             value2_num -
5431              NVL(
5432               (
5433                 SELECT  SUM(value2_num) -- Sum of Balance in Ledger Currency
5434                   FROM  okl_g_reports_gt nlc
5435                  WHERE  nlc.value1_text = 'GL_PERIOD_ACTIVITY'
5436                     AND nlc.value12_text = lc.value12_text -- Period Name
5437                     AND nlc.value13_text = lc.value13_text -- Account Number
5438                     AND nlc.value11_text <> lc.value11_text
5439               ), 0 )
5440      -- Pick only the Ledger Currency GL Opening Balances
5441      WHERE value1_text = 'GL_PERIOD_ACTIVITY' --value1_text
5442        AND value11_text IN
5443        (
5444           SELECT  currency_code
5445             FROM  gl_ledgers
5446            WHERE  ledger_id = p_ledger_id
5447        );
5448 
5449     --Bug:7535993 by nikshah
5450     --If no records found for Period Activity in GL
5451     --for natural account, enter 0 amount with ledger currency
5452     INSERT INTO okl_g_reports_gt
5453                 (
5454                   -- String Formatted Columns
5455                    value1_text
5456                   ,value11_text
5457                   ,value12_text
5458                   ,value13_text
5459                   -- Number Formatted Columns
5460                   ,value1_num
5461                   ,value2_num
5462                 )
5463       SELECT -- String Formatted Columns
5464              'GL_PERIOD_ACTIVITY'       --value1_text
5465             ,(select currency_code
5466               from gl_ledgers
5467               where ledger_id = p_ledger_id)              --value11_text
5468             ,null                --value12_text
5469             ,cgt.account_number             --value13_text -- Stores the Segment Number
5470             -- Number Formatted Columns
5471             ,0  --value1_num
5472             ,0  --value2_num
5473       FROM (select distinct account_number
5474             from okl_code_cmbns_gt) cgt
5475       WHERE cgt.account_number not in (select value13_text
5476                                        from okl_g_reports_gt
5477                                        where value1_text = 'GL_PERIOD_ACTIVITY'
5478                                        );
5479 
5480     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
5481       'After Inserting the GL Period Activity End Time:'
5482       || TO_CHAR(SYSDATE, 'DD-MON-YYY HH:MM:SS') );
5483     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
5484       'Time taken for Populating GL Period Activity '
5485        || ( SYSDATE - l_trace_time ) * 86400 || ' Seconds' );
5486 
5487    END IF; --End if of period activity for report type INCOME
5488     -------------------------------------------------------------------------------------
5489     -- Query segment to Insert the GL Closing Balances ...
5490     -------------------------------------------------------------------------------------
5491     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
5492       'Before Inserting the GL Closing Balances Start Time:'
5493       || TO_CHAR(l_trace_time, 'DD-MON-YYY HH:MM:SS') );
5494 
5495     -- Start the stop watch
5496     l_trace_time := SYSDATE;
5497     -- Actual Insert Statement
5498     INSERT INTO okl_g_reports_gt
5499                 (
5500                   -- String Formatted Columns
5501                    value1_text
5502                   ,value11_text
5503                   ,value12_text
5504                   ,value13_text
5505                   -- Number Formatted Columns
5506                   ,value1_num
5507                   ,value2_num
5508                 )
5509       SELECT -- String Formatted Columns
5510              'GL_CLOSING_BALANCE'       --value1_text
5511             ,currency_code              --value11_text
5512             ,period_name                --value12_text
5513             ,account_number             --value13_text -- Stores the Segment Number
5514             -- Number Formatted Columns
5515             -- Populating the Closing Balance asa Negative Amount
5516             --  for XML Publisher Layout Publisher
5517             -- In the Layout sum( closing balance + gl accounts ) = 0 hence
5518             --  negated the Closing Balance
5519             ,SUM(closing_bal_trx_curr) * -1  --value1_num
5520             ,SUM(closing_bal_led_curr) * -1  --value2_num
5521         FROM
5522       (
5523         -- Query Segment to fetch the GL Closing Balances for Non-Ledger Currency
5524         SELECT  cc_gt.account_number          account_number -- Stores the Segment Number
5525                ,cc_gt.ccid                    ccid
5526                ,bal.currency_code             currency_code
5527                ,bal.period_name               period_name
5528                ,(
5529                   ( period_net_dr
5530                     + begin_balance_dr
5531                   )
5532                   -
5533                   ( period_net_cr
5534                     + begin_balance_cr
5535                   )
5536                 )                              closing_bal_trx_curr
5537                ,(
5538                   ( period_net_dr_beq
5539                     + begin_balance_dr_beq
5540                   )
5541                   -
5542                   ( period_net_cr_beq
5543                     + begin_balance_cr_beq
5544                   )
5545                  )                            closing_bal_led_curr
5546           FROM  gl_balances                   bal
5547                ,gl_ledgers                    led
5548                ,okl_code_cmbns_gt             cc_gt
5549          WHERE  led.ledger_id   = p_ledger_id
5550            AND  bal.ledger_id   = led.ledger_id
5551            -- Closing Balance as on Period End
5552            AND  bal.period_name = p_period_to
5553            AND  bal.actual_flag = 'A'
5554            AND  bal.translated_flag IS NULL
5555            AND  bal.currency_code <> 'STAT'
5556            AND  bal.currency_code <> led.currency_code
5557            AND  bal.currency_code <> 'STAT'
5558            AND  bal.code_combination_id = cc_gt.ccid
5559       UNION ALL
5560         -- Query Segment to fetch the GL Closing Balances for Ledger Currency
5561         -- Logic: GL Balances store thes Accumulated Balance of all Currencies
5562         --        in the Ledger Currency. Hence need to deduct the non-ledger
5563         --        currency Total from the GL Balance
5564         SELECT  account_number              account_number -- Stores the Segment Number
5565                ,ccid                        ccid
5566                ,currency_code               currency_code
5567                ,period_name                 period_name
5568                ,nvl(closing_bal_led_curr,0) closing_bal_trx_curr
5569                ,nvl(closing_bal_led_curr,0) closing_bal_led_curr
5570           FROM
5571           (
5572             SELECT  cc_gt.account_number      account_number -- Segment Number
5573                    ,cc_gt.ccid                ccid
5574                    ,bal.currency_code         currency_code
5575                    ,bal.period_name           period_name
5576                    ,(
5577                       ( period_net_dr
5578                         + begin_balance_dr
5579                       )
5580                       -
5581                       ( period_net_cr
5582                         + begin_balance_cr
5583                       )
5584                     )                         closing_bal_led_curr
5585               FROM  gl_balances               bal
5586                    ,gl_ledgers                led
5587                    ,okl_code_cmbns_gt         cc_gt
5588              WHERE  led.ledger_id   = p_ledger_id
5589                AND  bal.ledger_id   = led.ledger_id
5590                -- Closing Balance as on Period End
5591                AND  bal.period_name = p_period_to
5592                AND  bal.translated_flag IS NULL
5593                AND  bal.currency_code <> 'STAT'
5594                AND  bal.actual_flag = 'A'
5595                AND  bal.currency_code = led.currency_code
5596                AND  bal.code_combination_id = cc_gt.ccid
5597           )
5598     )
5599     GROUP BY -- String Formatted Columns
5600        'GL_CLOSING_BALANCE'
5601       ,currency_code
5602       ,period_name
5603       ,account_number
5604     ;
5605 
5606    -- Logic:
5607    --  Till now, we have fetched the GL Closing Balances for both the
5608    --   Ledger Currency and Non-Ledger currencies.
5609    --  But the Ledger Currency Balances in GL is sum of Balances in all the
5610    --    Transactional Currencies too. Hence, need to deduct the Sum of
5611    --    Opening Balances for all non Ledger Currencies from the Ledger Currencies
5612    --    if exists.
5613    UPDATE okl_g_reports_gt lc
5614        SET  value1_num =
5615             value1_num -
5616              NVL(
5617               (
5618                 SELECT  SUM(value2_num) -- Sum of Balance in Ledger Currency
5619                   FROM  okl_g_reports_gt nlc
5620                  WHERE  nlc.value1_text = 'GL_CLOSING_BALANCE'
5621                     AND nlc.value12_text = lc.value12_text -- Period Name
5622                     AND nlc.value13_text = lc.value13_text -- Account Number
5623                     AND nlc.value11_text <> lc.value11_text
5624                ), 0)
5625            ,value2_num =
5626             value2_num -
5627              NVL(
5628               (
5629                 SELECT  SUM(value2_num) -- Sum of Balance in Ledger Currency
5630                   FROM  okl_g_reports_gt nlc
5631                  WHERE  nlc.value1_text = 'GL_CLOSING_BALANCE'
5632                     AND nlc.value12_text = lc.value12_text -- Period Name
5633                     AND nlc.value13_text = lc.value13_text -- Account Number
5634                     AND nlc.value11_text <> lc.value11_text
5635               ), 0 )
5636      -- Pick only the Ledger Currency GL Opening Balances
5637      WHERE value1_text = 'GL_CLOSING_BALANCE' --value1_text
5638        AND value11_text IN
5639        (
5640           SELECT  currency_code
5641             FROM  gl_ledgers
5642            WHERE  ledger_id = p_ledger_id
5643        );
5644 
5645     --Bug:7535993 by nikshah
5646     --If no records found for CLOSING BALANCE in GL
5647     --for natural account, enter 0 amount with ledger currency
5648     INSERT INTO okl_g_reports_gt
5649                 (
5650                   -- String Formatted Columns
5651                    value1_text
5652                   ,value11_text
5653                   ,value12_text
5654                   ,value13_text
5655                   -- Number Formatted Columns
5656                   ,value1_num
5657                   ,value2_num
5658                 )
5659       SELECT -- String Formatted Columns
5660              'GL_CLOSING_BALANCE'       --value1_text
5661             ,(select currency_code
5662               from gl_ledgers
5663               where ledger_id = p_ledger_id)              --value11_text
5664             ,p_period_to                --value12_text
5665             ,cgt.account_number             --value13_text -- Stores the Segment Number
5666             -- Number Formatted Columns
5667             ,0  --value1_num
5668             ,0  --value2_num
5669       FROM (select distinct account_number
5670             from okl_code_cmbns_gt) cgt
5671       WHERE cgt.account_number not in (select value13_text
5672                                        from okl_g_reports_gt
5673                                        where value1_text = 'GL_CLOSING_BALANCE'
5674                                        );
5675 
5676     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
5677       'After Inserting the GL Closing Balances End Time:'
5678       || TO_CHAR(SYSDATE, 'DD-MON-YYY HH:MM:SS') );
5679     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
5680       'Time taken for Populating GL Balances '
5681        || ( SYSDATE - l_trace_time ) * 86400 || ' Seconds' );
5682 
5683     -- Requirement:
5684     --   Always need to show the GL Closing and Opening Balances, when there are entries
5685     --   in the Streams Closing and Opening Balances. So that the Report generated fillay
5686     --   shows the GL and Stream Balance headers.
5687     -- Case 1: Insert GL Closing Balance record for which we have Streams Closing Balance
5688     INSERT INTO okl_g_reports_gt
5689                 (
5690                   -- String Formatted Columns
5691                    value1_text
5692                   ,value11_text
5693                   ,value12_text
5694                   ,value13_text
5695                   -- Number Formatted Columns
5696                   ,value1_num
5697                   ,value2_num
5698                 )
5699        SELECT -- String Formatted Columns
5700            'GL_CLOSING_BALANCE'       --value1_text
5701           ,value11_text               --value11_text -- Currency Code
5702           ,NULL                       --value12_text
5703           ,NULL                       --value13_text
5704           ,0                          --value1_num
5705           ,0                          --value2_num
5706         FROM okl_g_reports_gt
5707        WHERE value1_text IN ( 'OKL_STR_CLOSE' )
5708          AND value11_text
5709          NOT IN
5710            (
5711               SELECT  DISTINCT value11_text
5712                 FROM  okl_g_reports_gt
5713                WHERE  value1_text = 'GL_CLOSING_BALANCE'
5714            )
5715        GROUP BY 'GL_CLOSING_BALANCE'
5716                ,value11_text;
5717 
5718 
5719     -- Case 2: Insert GL Opening Balance record for which we have Streams Opening Balance
5720     INSERT INTO okl_g_reports_gt
5721                 (
5722                   -- String Formatted Columns
5723                    value1_text
5724                   ,value11_text
5725                   ,value12_text
5726                   ,value13_text
5727                   -- Number Formatted Columns
5728                   ,value1_num
5729                   ,value2_num
5730                 )
5731        SELECT -- String Formatted Columns
5732            'GL_OPENING_BALANCE'       --value1_text
5733           ,value11_text               --value11_text -- Currency Code
5734           ,NULL                       --value12_text
5735           ,NULL                       --value13_text
5736           ,0                          --value1_num
5737           ,0                          --value2_num
5738         FROM okl_g_reports_gt
5739        WHERE value1_text IN ( 'OKL_STR_OPEN' )
5740          AND value11_text
5741          NOT IN
5742            (
5743               SELECT  DISTINCT value11_text
5744                 FROM  okl_g_reports_gt
5745                WHERE  value1_text = 'GL_OPENING_BALANCE'
5746            )
5747        GROUP BY 'GL_OPENING_BALANCE'
5748                ,value11_text;
5749     -- Case 3: Insert Stream Closing Balance record for which we have GL Closing Balance
5750     INSERT INTO okl_g_reports_gt
5751                 (
5752                   -- String Formatted Columns
5753                    value1_text
5754                   ,value11_text
5755                   ,value5_text  -- Contract Number
5756                   ,value14_text -- Organization Name
5757                   -- Number Formatted Columns
5758                   ,value3_num -- Stream Balances Amount
5759                 )
5760        SELECT -- String Formatted Columns
5761            'OKL_STR_CLOSE'       --value1_text
5762           ,value11_text               --value11_text -- Currency Code
5763           ,NULL                       --value5_text
5764           ,NULL                       --value14_text
5765           ,0                          --value3_num
5766         FROM okl_g_reports_gt
5767        WHERE value1_text IN ( 'GL_CLOSING_BALANCE' )
5768          AND value11_text
5769          NOT IN
5770            (
5771               SELECT  DISTINCT value11_text
5772                 FROM  okl_g_reports_gt
5773                WHERE  value1_text = 'OKL_STR_CLOSE'
5774            )
5775        GROUP BY 'OKL_STR_CLOSE'
5776                 ,value11_text;
5777 
5778     -- Case 4: Insert Stream Opening Balance record for which we have GL Opening Balance
5779     INSERT INTO okl_g_reports_gt
5780                 (
5781                   -- String Formatted Columns
5782                    value1_text
5783                   ,value11_text
5784                   ,value5_text  -- Contract Number
5785                   ,value14_text -- Organization Name
5786                   -- Number Formatted Columns
5787                   ,value3_num -- Stream Balances Amount
5788                 )
5789        SELECT -- String Formatted Columns
5790            'OKL_STR_OPEN'       --value1_text
5791           ,value11_text               --value11_text -- Currency Code
5792           ,NULL                       --value5_text
5793           ,NULL                       --value14_text
5794           ,0                          --value3_num
5795         FROM okl_g_reports_gt
5796        WHERE value1_text IN ( 'GL_OPENING_BALANCE' )
5797          AND value11_text
5798          NOT IN
5799            (
5800               SELECT  DISTINCT value11_text
5801                 FROM  okl_g_reports_gt
5802                WHERE  value1_text = 'OKL_STR_OPEN'
5803            )
5804        GROUP BY 'OKL_STR_OPEN'
5805                 ,value11_text;
5806 
5807     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'P',
5808       'End: ' || l_api_name ||'(-)' );
5809     -- Set the Return Status and return back
5810     x_return_status := l_return_status;
5811   EXCEPTION
5812     WHEN OTHERS
5813     THEN
5814       x_return_status := okl_api.g_ret_sts_unexp_error;
5815       okl_api.set_message(p_app_name     => g_app_name,
5816                           p_msg_name     => g_unexpected_error,
5817                           p_token1       => g_sqlcode_token,
5818                           p_token1_value => SQLCODE,
5819                           p_token2       => g_sqlerrm_token,
5820                           p_token2_value => SQLERRM);
5821   END populate_gl_balances;
5822 
5823   -- Start of comments
5824   --
5825   -- Function Name   : validate_orgs_access.
5826   -- Description     : API to validate that User has access to all Orgs
5827   --                    whose Journals can impact the Ledger inputted
5828   --
5829   -- Business Rules  :
5830   -- Parameters      : p_ledger_id
5831   -- Version         : 1.0
5832   -- History         : Ravindranath Gooty created.
5833   --
5834   -- End of comments
5835   FUNCTION validate_orgs_access( p_ledger_id NUMBER )
5836     RETURN BOOLEAN
5837   IS
5838     -- Cursor Declarations
5839     CURSOR c_orgs_using_ledger_csr( p_ledger_id NUMBER )
5840     IS
5841      SELECT  organization_id        org_id
5842             ,NAME                   org_name
5843        FROM  hr_operating_units     hr
5844       WHERE  hr.set_of_books_id = p_ledger_id;
5845     -----------------------------------------------------------------
5846     -- Declare Process Variable
5847     -----------------------------------------------------------------
5848     l_api_version      CONSTANT NUMBER         := 1;
5849     l_api_name         CONSTANT VARCHAR2(30)   := 'validate_orgs_access';
5850     l_return_status    VARCHAR2(1)             := OKL_API.G_RET_STS_SUCCESS;
5851     -- Debug related parameters
5852     l_module CONSTANT fnd_log_messages.module%TYPE := G_MODULE || l_api_name;
5853     l_debug_enabled       VARCHAR2(10);
5854     is_debug_procedure_on BOOLEAN;
5855     is_debug_statement_on BOOLEAN;
5856     -- Local Variable Declarations
5857     l_error_out            VARCHAR2(1);
5858     l_check_access         VARCHAR2(1);
5859 
5860     TYPE rep_gt_tbl_type IS TABLE OF okl_g_reports_gt%ROWTYPE
5861       INDEX BY BINARY_INTEGER;
5862     l_invalid_orgs_tbl    rep_gt_tbl_type;
5863     i                     NUMBER;
5864   BEGIN
5865     l_debug_enabled := okl_debug_pub.check_log_enabled;
5866     -- check for logging on PROCEDURE level
5867     is_debug_procedure_on := okl_debug_pub.check_log_on(l_module,FND_LOG.LEVEL_PROCEDURE);
5868     -- check for logging on STATEMENT level
5869     is_debug_statement_on := okl_debug_pub.check_log_on(l_module,FND_LOG.LEVEL_STATEMENT);
5870     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'P',
5871                'begin debug OKLRRPTB.pls call ' || l_api_name);
5872     -- Constraint:
5873     --  The Report shouldnot be generated if the user doesnot have access to atleast
5874     --  one Operating Unit, whose Journal Entries can impact the Ledger inputted for the
5875     --  Report
5876     --   Eg. If say User U1 has access to OU1, OU2 but the Ledger L1 has been associated
5877     --       Orgs OU1, OU2, OU3, OU4, OU5, then report should not be generated.
5878     -- Initialize the Error Out flag to 'N'
5879     l_error_out := 'N';
5880     i := 1;
5881     FOR t_rec IN c_orgs_using_ledger_csr( p_ledger_id => p_ledger_id )
5882     LOOP
5883       -- Check whether user has access to this Operating Unit
5884       l_check_access := MO_GLOBAL.check_access( p_org_id => t_rec.org_id );
5885       IF l_check_access = 'Y'
5886       THEN
5887         put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
5888           'User has access to Operating Unit ID ' || t_rec.org_id );
5889       ELSE
5890         -- User doesnot have access to the Operating Unit, hence error out
5891         l_error_out := 'Y';
5892         l_invalid_orgs_tbl(i).value1_text := 'INVALID_ORG';
5893         l_invalid_orgs_tbl(i).value2_text := t_rec.org_name;
5894         l_invalid_orgs_tbl(i).value1_num  := t_rec.org_id;
5895         put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
5896           'User doesnot have has access to Operating Unit ID ' || t_rec.org_id
5897           || 'Org Name' || t_rec.org_name);
5898         -- Increment Index i
5899         i := i + 1;
5900       END IF;
5901     END LOOP;
5902 
5903     IF l_error_out = 'Y'
5904     THEN
5905       -- Finally insert the list of Invalid Org Names to print it on the Report
5906       FORALL i IN l_invalid_orgs_tbl.FIRST .. l_invalid_orgs_tbl.LAST
5907         INSERT INTO okl_g_reports_gt VALUES l_invalid_orgs_tbl(i);
5908 
5909       put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'P',
5910           'Returning FALSE. End API OKL_REPORT_GENERATOR_PVT.' || l_api_name );
5911       RETURN FALSE;
5912     END IF;
5913     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'P',
5914           'End API OKL_REPORT_GENERATOR_PVT.' || l_api_name );
5915     -- Return TRUE and Exit
5916     RETURN TRUE;
5917   EXCEPTION
5918     WHEN OTHERS
5919     THEN
5920       l_return_status := okl_api.g_ret_sts_unexp_error;
5921       -- Set the oracle error message
5922       okl_api.set_message(p_app_name     => okc_api.g_app_name,
5923                           p_msg_name     => g_unexpected_error,
5924                           p_token1       => g_sqlcode_token,
5925                           p_token1_value => SQLCODE,
5926                           p_token2       => g_sqlerrm_token,
5927                           p_token2_value => SQLERRM);
5928 
5929       RETURN FALSE;
5930   END validate_orgs_access;
5931 
5932 
5933   -- Start of comments
5934   --
5935   -- Function Name   : POPULATE_STRM_TO_TRX_DATA
5936   -- Description    : Populates streams to transaction and vice versa data for
5937   --                  Income Reconciliation Report
5938   -- Business Rules  : Called from generate_gross_inv_recon_rpt
5939   -- Parameters       :
5940   -- Version      : 1.0
5941   -- History        : NIKSHAH created.
5942   --
5943   -- End of comments
5944 PROCEDURE POPULATE_STRM_TO_TRX_DATA
5945              (p_api_version   IN         NUMBER
5946              ,p_init_msg_list IN         VARCHAR2
5947              ,x_return_status OUT NOCOPY VARCHAR2
5948              ,x_msg_count     OUT NOCOPY NUMBER
5949              ,x_msg_data      OUT NOCOPY VARCHAR2
5950              ,p_report_id     IN         NUMBER
5951              ,p_ledger_id     IN         NUMBER
5952              ,p_start_date    IN         DATE
5953              ,p_end_date      IN         DATE
5954              ,p_org_id        IN         NUMBER
5955              ,p_le_id         IN         NUMBER
5956              )IS
5957 
5958   L_EXPECTED_STREAMS CONSTANT VARCHAR2(30) := 'EXPECTED_STREAMS';
5959   L_STRMS_WITHOUT_TRX CONSTANT VARCHAR2(30) := 'STRMS_WITHOUT_TRX';
5960   L_TRX_SPEC_STRMS_ASE CONSTANT VARCHAR2(30) := 'TRX_SPEC_STRMS_ASE';
5961   L_NON_SPEC_STRMS_NASE CONSTANT VARCHAR2(30) := 'NON_SPEC_STRMS_NASE';
5962   L_TRX_SPEC_STRMS_NASE CONSTANT VARCHAR2(30) := 'TRX_SPEC_STRMS_NASE';
5963   L_TRX_ADD_ACC_EVENTS CONSTANT VARCHAR2(30) := 'TRX_ADD_ACC_EVENTS';
5964   L_NON_SPEC_STRMS_ASE CONSTANT VARCHAR2(30) := 'NON_SPEC_STRMS_ASE';
5965   L_TRX_NON_SPEC_STRMS CONSTANT VARCHAR2(30) := 'TRX_NON_SPEC_STRMS';
5966 
5967   l_api_version      CONSTANT NUMBER         := 1;
5968   l_api_name         CONSTANT VARCHAR2(30)   := 'POPULATE_STRM_TO_TRX_DATA';
5969   l_return_status    VARCHAR2(1)             := OKL_API.G_RET_STS_SUCCESS;
5970 
5971   -- Debug related parameters
5972   l_module CONSTANT fnd_log_messages.module%TYPE := G_MODULE || l_api_name;
5973   l_debug_enabled       VARCHAR2(10);
5974   is_debug_procedure_on BOOLEAN;
5975   is_debug_statement_on BOOLEAN;
5976 
5977   l_start_date     DATE :=  p_start_date;
5978   l_end_date     DATE :=  p_end_date;
5979 
5980   l_s_khr_id NUMBER;
5981   l_s_kle_id NUMBER;
5982   l_s_sty_id NUMBER;
5983 
5984   l_no_single_trx_exists BOOLEAN := FALSE;
5985   l_trx_exists BOOLEAN := FALSE;
5986   l_stream_currency_exists BOOLEAN := FALSE;
5987   l_is_spec_strm_type BOOLEAN := FALSE;
5988 
5989   l_trx_date            DATE;
5990   l_trace_time          DATE;
5991 
5992   l_count NUMBER := 1;
5993   l_accrual_count NUMBER := 1;
5994   l_reverse_count NUMBER := 1;
5995   l_non_elig_count NUMBER := 1;
5996   l_report_count NUMBER;
5997   l_stream_amount_count NUMBER := 0;
5998   l_no_of_streams_defined NUMBER := 0;
5999 
6000   --Cursor to see if there are any stream types defined for a report
6001   CURSOR C_CHECK_STREAMS_DEFINED
6002   IS
6003   SELECT STY_ID
6004   FROM   okl_report_stream_params
6005   WHERE  report_id = p_report_id;
6006 
6007   --Cursor to fetch all accruable streams for a  product
6008   CURSOR C_ALL_ACCRUABLE_STREAMS (p_pdt_id NUMBER)
6009   IS
6010   SELECT styy.id STY_ID
6011   FROM OKL_PROD_STRM_TYPES PSYY,
6012        OKL_STRM_TYPE_V STYY
6013   WHERE psyy.sty_id = styy.id
6014     AND psyy.accrual_yn = 'Y'
6015     AND psyy.pdt_id = p_pdt_id;
6016 
6017   --Cursor to fetch stream type IDs which are not specified in the template
6018   CURSOR C_STREAM_TYPES_NOT_SPECIFIED
6019   IS
6020   SELECT param.STY_ID
6021   FROM   okl_report_trx_params param,
6022          okl_trx_types_b trx
6023   WHERE  report_id = p_report_id
6024     AND  trx.id = param.try_id
6025     AND  trx.AEP_CODE = 'ACCRUAL'
6026   MINUS
6027   SELECT STY_ID
6028   FROM   okl_report_stream_params
6029   WHERE  report_id = p_report_id;
6030 
6031   --Cursor to fetch all products
6032   CURSOR C_GET_PRODUCTS
6033   IS
6034   SELECT product_id
6035   FROM okl_rep_products_gt;
6036 
6037   --Cursor to fetch all streams for given contract number and specified streams
6038   CURSOR C_MASTER_STREAMS_SPECIFIED
6039          (p_khr_id NUMBER)
6040   IS
6041   SELECT OU.NAME OPERATING_UNIT,
6042          PRO.NAME PROD_NAME,
6043          KHR.CONTRACT_NUMBER,
6044          KLE.NAME ASSET_NUMBER,
6045          STY.NAME STREAM_TYPE_NAME,
6046          STY.STYB_PURPOSE_MEANING STREAM_PURPOSE,
6047          STE.STREAM_ELEMENT_DATE,
6048          STE.AMOUNT,
6049          KHR.CURRENCY_CODE,
6050          STE.SE_LINE_NUMBER,
6051          OU.ORGANIZATION_ID ORG_ID,
6052          PRO.ID PDT_ID,
6053          KHR.ID KHR_ID,
6054          KLE.ID KLE_ID,
6055          STY.ID STY_ID,
6056          (SELECT  per.period_name
6057           FROM  gl_periods per
6058           WHERE  per.period_set_name = (SELECT gl.period_set_name
6059                                                                FROM gl_ledgers gl
6060                                                                WHERE ledger_id = p_ledger_id)
6061             AND  per.START_DATE <= STE.STREAM_ELEMENT_DATE
6062             AND  per.end_date >=  STE.STREAM_ELEMENT_DATE
6063             AND  per.adjustment_period_flag = 'N'
6064             AND  period_type = (SELECT gl.accounted_period_type
6065                                              FROM gl_ledgers gl
6066                                              WHERE ledger_id = p_ledger_id)
6067             ) STREAM_PERIOD
6068   FROM   OKL_STRM_TYPE_V STY,
6069          okl_streams_rep_v STM,
6070          OKL_STRM_ELEMENTS STE,
6071          OKC_K_HEADERS_ALL_B KHR,
6072          OKL_K_HEADERS KH,
6073          OKC_K_LINES_V KLE,
6074          OKL_PRODUCTS_V PRO,
6075          HR_OPERATING_UNITS OU
6076   WHERE  STY.ID = STM.STY_ID
6077     AND  STM.ID = STE.STM_ID
6078     AND  STM.KHR_ID = KHR.ID
6079 --    AND  STM.ACTIVE_YN = 'Y'
6080     AND  KLE.ID = STM.KLE_ID
6081     AND  KLE.DNZ_CHR_ID = KHR.ID
6082     AND  KH.ID = KHR.ID
6083     AND  KH.PDT_ID = PRO.ID
6084     AND  KHR.ORG_ID = OU.ORGANIZATION_ID
6085     AND  STY.ID IN ( SELECT styy.id
6086                      FROM OKL_PROD_STRM_TYPES PSYY,
6087                           OKL_STRM_TYPE_V STYY
6088                      WHERE psyy.sty_id = sty.id
6089                        AND psyy.accrual_yn = 'Y'
6090                        and psyy.pdt_id = KH.PDT_ID
6091                      UNION
6092                      SELECT  sty_id
6093                      FROM  okl_report_stream_params  rsp
6094                      WHERE  rsp.report_id = p_report_id
6095                      )
6096     and KH.ID = p_khr_id
6097     AND  STM.ID IN ( SELECT MAX(ID)
6098                      FROM ( SELECT STM.STY_ID, STM.ID, MAX(DATE_CURRENT)
6099                             FROM   OKL_STRM_TYPE_V STY,
6100                                    okl_streams_rep_v STM,
6101                                    OKL_STRM_ELEMENTS STE,
6102                                    OKC_K_HEADERS_ALL_B KHR,
6103                                    OKL_K_HEADERS KH,
6104                                    OKC_K_LINES_V KLE,
6105                                    OKL_PRODUCTS_V PRO,
6106                                    HR_OPERATING_UNITS OU
6107                             WHERE  STY.ID = STM.STY_ID
6108                               AND  STM.ID = STE.STM_ID
6109                               AND  STM.KHR_ID = KHR.ID
6110                               AND  STM.DATE_CURRENT <= p_end_date
6111                               AND  NVL(STM.date_history,  p_end_date) between p_start_date  and p_end_date
6112                               AND  KHR.ID = p_khr_id
6113                               AND  KLE.ID = STM.KLE_ID
6114                               AND  KLE.DNZ_CHR_ID = KHR.ID
6115                               AND  KH.ID = KHR.ID
6116                               AND  KH.PDT_ID = PRO.ID
6117                               AND  KHR.ORG_ID = OU.ORGANIZATION_ID
6118                             GROUP BY STM.STY_ID, STM.ID
6119                             ORDER BY 3 DESC
6120                            )
6121                      GROUP BY STY_ID
6122                    )
6123   ORDER BY KHR.ID, KLE.ID, STY.ID, STE.SE_LINE_NUMBER;
6124 
6125   --Cursor to fetch all streams for given contract number and all accruable streams
6126   CURSOR C_MASTER_STREAMS_ALL
6127          (p_khr_id NUMBER)
6128   IS
6129   SELECT OU.NAME OPERATING_UNIT,
6130          PRO.NAME PROD_NAME,
6131          KHR.CONTRACT_NUMBER,
6132          KLE.NAME ASSET_NUMBER,
6133          STY.NAME STREAM_TYPE_NAME,
6134          STY.STYB_PURPOSE_MEANING STREAM_PURPOSE,
6135          STE.STREAM_ELEMENT_DATE,
6136          STE.AMOUNT,
6137          KHR.CURRENCY_CODE,
6138          STE.SE_LINE_NUMBER,
6139          OU.ORGANIZATION_ID ORG_ID,
6140          PRO.ID PDT_ID,
6141          KHR.ID KHR_ID,
6142          KLE.ID KLE_ID,
6143          STY.ID STY_ID,
6144          (SELECT  per.period_name
6145           FROM  gl_periods per
6146           WHERE  per.period_set_name = (SELECT gl.period_set_name
6147                                         FROM gl_ledgers gl
6148                                         WHERE ledger_id = p_ledger_id)
6149             AND  per.START_DATE <= STE.STREAM_ELEMENT_DATE
6150             AND  per.end_date >=  STE.STREAM_ELEMENT_DATE
6151             AND  per.adjustment_period_flag = 'N'
6152             AND  period_type = (SELECT gl.accounted_period_type
6153                                 FROM gl_ledgers gl
6154                                 WHERE ledger_id = p_ledger_id)
6155             ) STREAM_PERIOD
6156   FROM   OKL_STRM_TYPE_V STY,
6157          okl_streams_rep_v STM,
6158          OKL_STRM_ELEMENTS STE,
6159          OKC_K_HEADERS_ALL_B KHR,
6160          OKL_K_HEADERS KH,
6161          OKC_K_LINES_V KLE,
6162          OKL_PRODUCTS_V PRO,
6163          HR_OPERATING_UNITS OU
6164   WHERE  STY.ID = STM.STY_ID
6165     AND  STM.ID = STE.STM_ID
6166     AND  STM.KHR_ID = KHR.ID
6167 --    AND  STM.ACTIVE_YN = 'Y'
6168     AND  KLE.ID = STM.KLE_ID
6169     AND  KLE.DNZ_CHR_ID = KHR.ID
6170     AND  KH.ID = KHR.ID
6171     AND  KH.PDT_ID = PRO.ID
6172     AND  KHR.ORG_ID = OU.ORGANIZATION_ID
6173     AND  STY.ID IN ( SELECT styy.id
6174                      FROM OKL_PROD_STRM_TYPES PSYY,
6175                           OKL_STRM_TYPE_V STYY
6176                      WHERE psyy.sty_id = sty.id
6177                        AND psyy.accrual_yn = 'Y'
6178                        and psyy.pdt_id = KH.PDT_ID
6179                      UNION
6180                      SELECT  sty_id
6181                      FROM  okl_report_stream_params  rsp
6182                      WHERE  rsp.report_id = p_report_id
6183                      )
6184     and KH.ID = p_khr_id
6185     AND  STM.ID IN ( SELECT MAX(ID)
6186                      FROM ( SELECT STM.STY_ID, STM.ID, MAX(DATE_CURRENT)
6187                             FROM   OKL_STRM_TYPE_V STY,
6188                                    okl_streams_rep_v STM,
6189                                    OKL_STRM_ELEMENTS STE,
6190                                    OKC_K_HEADERS_ALL_B KHR,
6191                                    OKL_K_HEADERS KH,
6192                                    OKC_K_LINES_V KLE,
6193                                    OKL_PRODUCTS_V PRO,
6194                                    HR_OPERATING_UNITS OU
6195                             WHERE  STY.ID = STM.STY_ID
6196                               AND  STM.ID = STE.STM_ID
6197                               AND  STM.KHR_ID = KHR.ID
6198                               AND  STM.DATE_CURRENT <= p_end_date
6199                               AND  NVL(STM.date_history,  p_end_date) between p_start_date  and p_end_date
6200                               AND  KHR.ID = p_khr_id --BIND CONTRACT IDS WHICH ARE ELIGIBLE BASED ON PRODUCTS
6201                               AND  KLE.ID = STM.KLE_ID
6202                               AND  KLE.DNZ_CHR_ID = KHR.ID
6203                               AND  KH.ID = KHR.ID
6204                               AND  KH.PDT_ID = PRO.ID
6205                               AND  KHR.ORG_ID = OU.ORGANIZATION_ID
6206                             GROUP BY STM.STY_ID, STM.ID
6207                             ORDER BY 3 DESC
6208                            )
6209                      GROUP BY STY_ID
6210                    )
6211   ORDER BY KHR.ID, KLE.ID, STY.ID, STE.SE_LINE_NUMBER;
6212 
6213   --Get all transaction lines for a particular contract
6214   CURSOR C_TRANSACTION_LINES
6215          (p_khr_id NUMBER)
6216   IS
6217   SELECT * FROM
6218 (
6219   SELECT TCN.ID TCN_ID,
6220          TCL.ID TCL_ID,
6221          TCN.TRX_NUMBER,
6222          TCL.KHR_ID,
6223          TCL.STY_ID,
6224          STY.NAME STREAM_TYPE,
6225          CLE.NAME ASSET_NUMBER,
6226          STY.STYB_PURPOSE_MEANING STREAM_PURPOSE,
6227          TCL.AMOUNT,
6228          TCN.CURRENCY_CODE,
6229          TCN.ORG_ID,
6230          OU.NAME OPERATING_UNIT,
6231          TCN.PRODUCT_NAME,
6232          TCL.KLE_ID,
6233          TCN.TRANSACTION_DATE,
6234          NULL REVERSAL_DATE,
6235          ACCRUAL_ACTIVITY,
6236          TCN.DATE_ACCRUAL,
6237          TCN.DATE_TRANSACTION_OCCURRED,
6238          TCL.LINE_NUMBER,
6239          TTY.ID TRY_ID,
6240          TTY.NAME TRANSACTION_TYPE,
6241          (SELECT  per.period_name
6242           FROM  gl_periods per
6243           WHERE  per.period_set_name = (SELECT gl.period_set_name
6244                                         FROM gl_ledgers gl
6245                                         WHERE ledger_id = p_ledger_id)
6246             AND  per.START_DATE <= TCN.TRANSACTION_DATE
6247             AND  per.end_date >=  TCN.TRANSACTION_DATE
6248             AND  per.adjustment_period_flag = 'N'
6249             AND  period_type = (SELECT gl.accounted_period_type
6250                                 FROM gl_ledgers gl
6251                                 WHERE ledger_id = p_ledger_id)
6252             ) TRANSACTION_PERIOD,
6253          NULL REVERSAL_PERIOD
6254   FROM   OKL_TRX_CONTRACTS_ALL TCN,
6255          OKL_TXL_CNTRCT_LNS_ALL TCL,
6256          OKL_TRX_TYPES_V TTY,
6257          OKL_STRM_TYPE_V STY,
6258          HR_OPERATING_UNITS OU,
6259          OKC_K_LINES_V CLE
6260   WHERE  TCN.ID = TCL.TCN_ID
6261     AND  TCN.TRY_ID = TTY.ID
6262     AND  TCL.STY_ID = STY.ID
6263     AND  TCL.KHR_ID = p_khr_id
6264     AND  TCN.KHR_ID = TCL.KHR_ID
6265     AND  CLE.ID (+) = TCL.KLE_ID
6266 --
6267     AND  TCN.SET_OF_BOOKS_ID = p_ledger_id
6268     AND  TTY.ID IN (SELECT TRY_ID FROM  OKL_REPORT_TRX_PARAMS where report_id = p_report_id)
6269     AND  TCN.ACCRUAL_ACTIVITY <> 'REVERSAL'
6270     AND OU.ORGANIZATION_ID = TCN.ORG_ID
6271     AND  NVL(TCN.SOURCE_TRX_ID,-1) NOT IN (SELECT ID
6272                                            FROM OKL_TRX_CONTRACTS_ALL RBK_TRX
6273                                            WHERE RBK_TRX.KHR_ID = TCN.KHR_ID
6274                                              AND  RBK_TRX.ID = TCN.SOURCE_TRX_ID
6275                                              AND TCN_TYPE in('TRBK','ALT'))
6276 
6277   UNION ALL
6278 
6279   SELECT TCN.ID TCN_ID,
6280          TCL.ID TCL_ID,
6281          TCN.TRX_NUMBER,
6282          TCL.KHR_ID,
6283          TCL.STY_ID,
6284          STY.NAME STREAM_TYPE,
6285          CLE.NAME ASSET_NUMBER,
6286          STY.STYB_PURPOSE_MEANING STREAM_PURPOSE,
6287          TCL.AMOUNT,
6288          TCN.CURRENCY_CODE,
6289          TCN.ORG_ID,
6290          OU.NAME OPERATING_UNIT,
6291          TCN.PRODUCT_NAME,
6292          TCL.KLE_ID,
6293          TCN.TRANSACTION_DATE,
6294          TCN.TRANSACTION_REVERSAL_DATE REVERSAL_DATE,
6295          ACCRUAL_ACTIVITY,
6296          TCN.DATE_ACCRUAL,
6297          TCN.DATE_TRANSACTION_OCCURRED,
6298          TCL.LINE_NUMBER,
6299          TTY.ID TRY_ID,
6300          TTY.NAME TRANSACTION_TYPE,
6301          (SELECT  per.period_name
6302           FROM  gl_periods per
6303           WHERE  per.period_set_name = (SELECT gl.period_set_name
6304                                         FROM gl_ledgers gl
6305                                         WHERE ledger_id = p_ledger_id)
6306             AND  per.START_DATE <= TCN.TRANSACTION_DATE
6307             AND  per.end_date >=  TCN.TRANSACTION_DATE
6308             AND  per.adjustment_period_flag = 'N'
6309             AND  period_type = (SELECT gl.accounted_period_type
6310                                 FROM gl_ledgers gl
6311                                 WHERE ledger_id = p_ledger_id)
6312             ) TRANSACTION_PERIOD,
6313          (SELECT  per.period_name
6314           FROM  gl_periods per
6315           WHERE  per.period_set_name = (SELECT gl.period_set_name
6316                                         FROM gl_ledgers gl
6317                                         WHERE ledger_id = p_ledger_id)
6318             AND  per.START_DATE <= TCN.TRANSACTION_REVERSAL_DATE
6319             AND  per.end_date >=  TCN.TRANSACTION_REVERSAL_DATE
6320             AND  per.adjustment_period_flag = 'N'
6321             AND  period_type = (SELECT gl.accounted_period_type
6322                                 FROM gl_ledgers gl
6323                                 WHERE ledger_id = p_ledger_id)
6324             ) REVERSAL_PERIOD
6325   FROM   OKL_TRX_CONTRACTS_ALL TCN,
6326          OKL_TXL_CNTRCT_LNS_ALL TCL,
6327          OKL_TRX_TYPES_V TTY,
6328          OKL_STRM_TYPE_V STY,
6329          HR_OPERATING_UNITS OU,
6330          OKC_K_LINES_V CLE
6331   WHERE  TCN.ID = TCL.TCN_ID
6332     AND  TCN.TRY_ID = TTY.ID
6333     AND  TCL.KHR_ID = p_khr_id
6334     AND  TCL.STY_ID = STY.ID
6335     AND  TCN.KHR_ID = TCL.KHR_ID
6336     AND  CLE.ID (+) = TCL.KLE_ID
6337 --
6338     AND  TCN.SET_OF_BOOKS_ID = p_ledger_id
6339     AND  TTY.ID IN (SELECT TRY_ID FROM  OKL_REPORT_TRX_PARAMS where report_id = p_report_id)
6340     AND  TCN.ACCRUAL_ACTIVITY = 'REVERSAL'
6341     AND  OU.ORGANIZATION_ID = TCN.ORG_ID
6342     AND  NVL(TCN.SOURCE_TRX_ID,-1) NOT IN (SELECT ID
6343                                            FROM OKL_TRX_CONTRACTS_ALL RBK_TRX
6344                                            WHERE RBK_TRX.KHR_ID = TCN.KHR_ID
6345                                              AND  RBK_TRX.ID = TCN.SOURCE_TRX_ID
6346                                              AND TCN_TYPE in ('TRBK','ALT'))
6347   )
6348   ORDER BY TCN_ID, KHR_ID, KLE_ID, STY_ID, DATE_ACCRUAL, TRANSACTION_DATE, LINE_NUMBER;
6349 
6350   --Cursor to get adjustment transactions for all stream types
6351   --and specified transaction types
6352   CURSOR C_GET_ADJUSTMENT_TRANSACTIONS(p_product_id NUMBER)
6353   IS
6354   SELECT ou.name operating_unit,
6355          prod.name Product,
6356          khr.Contract_Number,
6357          kle.name Asset_Number,
6358          st.name stream_type,
6359          st.styb_purpose_meaning Stream_purpose,
6360          tty.NAME TRANSACTION_TYPE,
6361          ce.Stream_Element_Date stream_element_date,
6362          acc_trx.transaction_date,
6363          acc_trx.trx_number,
6364          khr.CURRENCY_CODE,
6365          (ce.Amount - Nvl(pe.Amount,ce.Amount)) Delta_Amt,
6366          DECODE(Sign(ce.Stream_Element_Date - to_date(p_start_date)),
6367                 - 1,'N',
6368                 DECODE(Sign(to_date(p_end_date) - ce.Stream_Element_Date),
6369                        - 1,'N',
6370                        'Y')) In_rep_Period,
6371          ou.organization_id org_id,
6372          prod.id  product_id,
6373          c.khr_Id khr_Id,
6374          c.kle_Id kle_Id,
6375          c.Sty_Id Sty_Id,
6376          ce.se_Line_Number line_number
6377   FROM   okl_streams_rep_v c,
6378          okl_streams_rep_v p,
6379          Okl_strm_Type_v st,
6380          Okl_strm_Elements ce,
6381          Okl_strm_Elements pe,
6382          Okc_k_Headers_All_b khr,
6383          Okc_k_lines_v kle,
6384          hr_operating_units ou,
6385          okl_products prod,
6386          okl_k_headers chr,
6387          okl_trx_contracts_all rbk_trx,
6388          okl_trx_contracts_all acc_trx,
6389          okl_rep_products_gt pdt_gt,
6390          OKL_TRX_TYPES_V TTY
6391   WHERE  c.Link_Hist_Stream_Id = p.Id
6392     AND  c.Sty_Id = st.Id
6393     AND  c.Date_Current >= p.Date_Current
6394     AND  c.kle_Id = p.kle_Id
6395     AND  ce.stm_Id = c.Id
6396     AND  pe.stm_Id = p.Id
6397     AND  ce.Stream_Element_Date = pe.Stream_Element_Date
6398     AND  ce.se_Line_Number = pe.se_Line_Number
6399     AND  c.khr_Id = khr.Id
6400     AND  khr.sts_Code IN ('BOOKED','TERMINATED')
6401     AND  Nvl(p.Date_History,p_end_date) BETWEEN p_start_date
6402                                             AND p_end_date
6403     AND  Nvl(c.Date_History,p_end_date) BETWEEN p_start_date
6404                                             AND p_end_date
6405     AND  c.Date_Current <= p_end_date
6406     AND  Nvl(ce.Accrued_yn,'N') = 'Y'
6407     AND  pe.Accrued_yn = 'Y'
6408     AND  kle.id = c.kle_id
6409     AND  kle.id = p.kle_id
6410     AND  khr.org_id = ou.organization_id
6411     AND  chr.id = khr.id
6412     AND  chr.pdt_id = prod.id
6413     AND  rbk_trx.khr_id = khr.id
6414     AND  rbk_trx.id = c.trx_id
6415     AND  rbk_trx.pdt_id = prod.id
6416     AND  acc_trx.source_trx_id = rbk_trx.id
6417     AND  acc_trx.khr_id  = rbk_trx.khr_id
6418     AND  acc_trx.pdt_id = rbk_trx.pdt_id
6419     AND  acc_trx.khr_id = khr.id
6420     AND  acc_trx.pdt_id = prod.id
6421     AND  acc_trx.source_trx_type = 'TCN'
6422     AND  prod.id = pdt_gt.product_id
6423     AND  acc_trx.SET_OF_BOOKS_ID = p_ledger_id
6424     AND  rbk_trx.SET_OF_BOOKS_ID = p_ledger_id
6425     AND  TTY.ID IN (SELECT TRY_ID FROM OKL_REPORT_TRX_PARAMS WHERE report_id = p_report_id)
6426     AND  TTY.ID = ACC_TRX.TRY_ID
6427     AND  prod.id = p_product_id
6428     AND  (ce.Amount - Nvl(pe.Amount,ce.Amount)) <> 0
6429   order by org_id,product_id,khr_id,kle_id,sty_id,line_number;
6430 
6431   TYPE strm_tbl_type IS TABLE OF C_MASTER_STREAMS_SPECIFIED%ROWTYPE INDEX BY BINARY_INTEGER;
6432   TYPE trx_tbl_type IS TABLE OF C_TRANSACTION_LINES%ROWTYPE INDEX BY BINARY_INTEGER;
6433   TYPE sty_id_tbl_type IS TABLE OF OKL_REPORT_TRX_PARAMS.STY_ID%TYPE INDEX BY BINARY_INTEGER;
6434 
6435   l_master_strm_tbl strm_tbl_type;
6436   l_eligible_strm_tbl strm_tbl_type;
6437   l_non_eligible_strm_tbl strm_tbl_type;
6438 
6439   l_master_trx_tbl trx_tbl_type;
6440   l_subset_trx_tbl trx_tbl_type;
6441 
6442   l_exp_streams_tbl reports_gt_tbl_type;
6443 
6444   l_reports_tbl reports_gt_tbl_type;
6445   l_non_spec_sty_id_tbl sty_id_tbl_type;
6446   l_spec_sty_id_tbl sty_id_tbl_type;
6447   l_pdt_sty_id_tbl sty_id_tbl_type;
6448 
6449   BEGIN
6450     l_debug_enabled := okl_debug_pub.check_log_enabled;
6451     -- check for logging on PROCEDURE level
6452     is_debug_procedure_on := okl_debug_pub.check_log_on(l_module,FND_LOG.LEVEL_PROCEDURE);
6453     -- check for logging on STATEMENT level
6454     is_debug_statement_on := okl_debug_pub.check_log_on(l_module,FND_LOG.LEVEL_STATEMENT);
6455     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'P',
6456                'begin debug OKLRRPTB.pls call ' || l_api_name);
6457     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'P',
6458       'Input Parameters ' ||
6459       ' p_report_id=' || p_report_id ||
6460       ' p_ledger_id=' || p_ledger_id ||
6461       ' p_start_date=' || p_start_date ||
6462       ' p_end_date=' || p_end_date ||
6463       ' p_org_id=' || p_org_id ||
6464       ' p_le_id=' ||  p_le_id );
6465     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
6466       'Begin: ' || l_api_name ||'(+)' );
6467 
6468     -- For secondary rep txn, set the security policy for streams. MG Uptake
6469     IF g_representation_type = 'SECONDARY' THEN
6470       OKL_STREAMS_SEC_PVT.SET_REPO_STREAMS;
6471     END IF;
6472 
6473     -- Start the stop watch
6474     l_trace_time := SYSDATE;
6475     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
6476       'Starting the processing of streams to transactions and transactions to streams for income: '
6477       || TO_CHAR(l_trace_time, 'DD-MON-YYY HH:MM:SS') );
6478 
6479     l_return_status := okl_api.g_ret_sts_success;
6480 
6481     l_report_count := 1;
6482 
6483     --Check and fetch streams defined on template
6484     OPEN C_CHECK_STREAMS_DEFINED;
6485     FETCH C_CHECK_STREAMS_DEFINED BULK COLLECT INTO l_spec_sty_id_tbl;
6486     CLOSE C_CHECK_STREAMS_DEFINED;
6487 
6488     --Get the list of stream types which are not specified
6489     OPEN C_STREAM_TYPES_NOT_SPECIFIED;
6490     FETCH C_STREAM_TYPES_NOT_SPECIFIED BULK COLLECT INTO l_non_spec_sty_id_tbl;
6491     CLOSE C_STREAM_TYPES_NOT_SPECIFIED;
6492 
6493     l_no_of_streams_defined  := l_spec_sty_id_tbl.COUNT;
6494 
6495     --Iterate to get adjustment transanctions for each product
6496     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
6497       'Getting adjustment transactions for each product');
6498     FOR pdt_rec IN C_GET_PRODUCTS
6499     LOOP
6500       --If no streams are specified, then take all the accruable streams
6501       --defined on product
6502       IF l_no_of_streams_defined = 0 THEN
6503         l_pdt_sty_id_tbl.delete;
6504         OPEN C_ALL_ACCRUABLE_STREAMS(pdt_rec.product_id);
6505         FETCH C_ALL_ACCRUABLE_STREAMS BULK COLLECT INTO l_pdt_sty_id_tbl;
6506         CLOSE C_ALL_ACCRUABLE_STREAMS;
6507       ELSE
6508         l_pdt_sty_id_tbl := l_spec_sty_id_tbl;
6509       END IF;
6510 
6511       --If there are any eligible streams for getting adjustment then
6512       --get those otherwise skip the product
6513       IF l_pdt_sty_id_tbl.COUNT > 0 THEN
6514         FOR adj_rec IN C_GET_ADJUSTMENT_TRANSACTIONS(pdt_rec.product_id)
6515         LOOP
6516           l_is_spec_strm_type := FALSE;
6517           --Check whether adjustment entry is belonging to specified stream
6518           FOR i IN l_pdt_sty_id_tbl.FIRST..l_pdt_sty_id_tbl.LAST
6519           LOOP
6520             IF adj_rec.STY_ID = l_pdt_sty_id_tbl(i) THEN
6521               l_is_spec_strm_type := TRUE;
6522               EXIT;
6523             END IF;
6524           END LOOP;
6525 
6526           --If it is not belonging to specified stream,
6527           --Check whether adjustment entry is belonging to non specified stream
6528           IF NOT l_is_spec_strm_type THEN
6529             FOR i IN l_non_spec_sty_id_tbl.FIRST..l_non_spec_sty_id_tbl.LAST
6530             LOOP
6531               IF adj_rec.STY_ID = l_non_spec_sty_id_tbl(i) THEN
6532                 l_is_spec_strm_type := TRUE;
6533                 EXIT;
6534               END IF;
6535             END LOOP;
6536           END IF;
6537 
6538           --If adjustment is belonging to either specified or non-specified
6539           --it should be taken into consideration
6540           --And fill bucket TRX_SPEC_STRMS_NASE
6541           IF l_is_spec_strm_type THEN
6542             l_reports_tbl(l_report_count).value1_text := L_TRX_SPEC_STRMS_NASE;
6543             l_reports_tbl(l_report_count).value2_text := adj_rec.TRX_NUMBER;
6544             l_reports_tbl(l_report_count).value1_date := adj_rec.TRANSACTION_DATE;
6545             l_reports_tbl(l_report_count).value3_date := adj_rec.STREAM_ELEMENT_DATE;
6546             l_reports_tbl(l_report_count).value7_num := adj_rec.ORG_ID;
6547             l_reports_tbl(l_report_count).value8_text := adj_rec.PRODUCT;
6548             l_reports_tbl(l_report_count).value9_text := adj_rec.STREAM_TYPE;
6549             l_reports_tbl(l_report_count).value10_text := adj_rec.STREAM_PURPOSE;
6550             l_reports_tbl(l_report_count).value5_text := adj_rec.contract_number;
6551             l_reports_tbl(l_report_count).value6_text := adj_rec.asset_number;
6552             l_reports_tbl(l_report_count).value11_text := adj_rec.CURRENCY_CODE;
6553             l_reports_tbl(l_report_count).value14_text := adj_rec.OPERATING_UNIT;
6554             l_reports_tbl(l_report_count).value9_num := adj_rec.KHR_ID;
6555             l_reports_tbl(l_report_count).value10_num := adj_rec.KLE_ID;
6556             l_reports_tbl(l_report_count).value12_num := adj_rec.STY_ID;
6557             l_reports_tbl(l_report_count).value3_text := adj_rec.TRANSACTION_TYPE;
6558             l_reports_tbl(l_report_count).value3_num := adj_rec.DELTA_AMT;
6559             l_report_count := l_report_count + 1;
6560 
6561             --If adjustment is of non specified period then also fill bucket
6562             -- NON_SPEC_STRMS_NASE
6563             IF adj_rec.IN_REP_PERIOD = 'N' THEN
6564               l_reports_tbl(l_report_count) := l_reports_tbl(l_report_count - 1);
6565               l_reports_tbl(l_report_count).value1_text := L_NON_SPEC_STRMS_NASE;
6566               l_report_count := l_report_count + 1;
6567             END IF;
6568           END IF;
6569         END LOOP; -- adjustment transactions
6570       END IF;
6571     END LOOP; --products
6572 
6573     IF l_no_of_streams_defined > 0 THEN
6574       put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
6575         'Streams are defined on this template, it will consider only these');
6576     ELSE
6577       put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
6578         'Streams are NOT defined on this template, it will consider accruable streams defined on product');
6579     END IF;
6580 
6581     --Iterate through each eligible contract, and fill respective bucket
6582     FOR khr_rec IN get_contracts_csr(NULL,NULL,p_ledger_id,p_start_date,p_end_date)
6583     LOOP
6584       put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
6585         'Starting for contract: ' || khr_rec.contract_number );
6586 
6587       --Purge all plsql tables
6588       l_master_strm_tbl.delete;
6589       l_eligible_strm_tbl.delete;
6590       l_non_eligible_strm_tbl.delete;
6591       l_master_trx_tbl.delete;
6592       l_subset_trx_tbl.delete;
6593 
6594       --If number of streams defined is > 0, then take those into consideration
6595       --Else consider all accruable streams defined in the product for this
6596       --     particular contract
6597       IF l_no_of_streams_defined > 0 THEN
6598         OPEN C_MASTER_STREAMS_SPECIFIED(khr_rec.contract_id);
6599         FETCH C_MASTER_STREAMS_SPECIFIED BULK COLLECT INTO l_master_strm_tbl;
6600         CLOSE C_MASTER_STREAMS_SPECIFIED;
6601       ELSE
6602         OPEN C_MASTER_STREAMS_ALL(khr_rec.contract_id);
6603         FETCH C_MASTER_STREAMS_ALL BULK COLLECT INTO l_master_strm_tbl;
6604         CLOSE C_MASTER_STREAMS_ALL;
6605 
6606         l_spec_sty_id_tbl.delete;
6607         OPEN C_ALL_ACCRUABLE_STREAMS(khr_rec.pdt_id);
6608         FETCH C_ALL_ACCRUABLE_STREAMS BULK COLLECT INTO l_spec_sty_id_tbl;
6609         CLOSE C_ALL_ACCRUABLE_STREAMS;
6610       END IF;
6611 
6612       put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
6613         '  This Contract is having total stream elements: ' || l_master_strm_tbl.COUNT );
6614 
6615       l_count := 1;
6616       l_non_elig_count := 1;
6617 
6618       --
6619       --At this time, we got all stream elements and out of them
6620       --identify eligible streams falling into reporting period
6621       --and put those in another plsql table
6622       IF l_master_strm_tbl.COUNT > 0 THEN
6623         FOR i IN l_master_strm_tbl.FIRST..l_master_strm_tbl.LAST
6624         LOOP
6625           l_is_spec_strm_type := FALSE;
6626           --Check whether stream is specified or not
6627           IF l_spec_sty_id_tbl.COUNT > 0 THEN
6628             FOR j in l_spec_sty_id_tbl.FIRST..l_spec_sty_id_tbl.LAST
6629             LOOP
6630               IF l_spec_sty_id_tbl(j) = l_master_strm_tbl(i).STY_ID THEN
6631                 l_is_spec_strm_type := TRUE;
6632                 EXIT;
6633               END IF;
6634             END LOOP;
6635           END IF;
6636 
6637           --If it is within reporting period and specified stream
6638           --Then put it into eligible stream PLSQL table
6639           --Also create EXPECTED_STREAMS bucket
6640           IF l_master_strm_tbl(i).STREAM_ELEMENT_DATE BETWEEN p_start_date AND p_end_date AND
6641              l_is_spec_strm_type
6642           THEN
6643             l_eligible_strm_tbl(l_count):= l_master_strm_tbl(i);
6644             l_stream_currency_exists := FALSE;
6645             IF l_exp_streams_tbl.COUNT > 0 THEN
6646               FOR j IN l_exp_streams_tbl.FIRST..l_exp_streams_tbl.LAST
6647               LOOP
6648                 IF l_exp_streams_tbl(j).value11_text = l_master_strm_tbl(i).currency_code THEN
6649                   l_stream_currency_exists := TRUE;
6650                   l_exp_streams_tbl(j).value3_num := l_exp_streams_tbl(j).value3_num + l_master_strm_tbl(i).AMOUNT;
6651                   EXIT;
6652                 END IF;
6653               END LOOP;
6654             END IF;
6655 
6656             IF NOT l_stream_currency_exists THEN
6657               put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
6658                  '  Creating expected stream amount total line for currency: ' || l_master_strm_tbl(i).currency_code);
6659               l_stream_amount_count := l_stream_amount_count + 1;
6660               l_exp_streams_tbl(l_stream_amount_count).value3_num := l_master_strm_tbl(i).AMOUNT;
6661               l_exp_streams_tbl(l_stream_amount_count).value11_text := l_master_strm_tbl(i).currency_code;
6662               l_exp_streams_tbl(l_stream_amount_count).value1_text := L_EXPECTED_STREAMS;
6663             END IF;
6664 
6665             l_count := l_count + 1;
6666           --If it is outside reporting period and specified stream
6667           --Then put it into non eligible stream PLSQL table
6668           ELSIF l_master_strm_tbl(i).STREAM_ELEMENT_DATE NOT BETWEEN p_start_date AND p_end_date AND
6669                 l_is_spec_strm_type
6670           THEN
6671             l_non_eligible_strm_tbl(l_non_elig_count):= l_master_strm_tbl(i);
6672             l_non_elig_count := l_non_elig_count + 1;
6673           END IF;
6674         END LOOP;
6675       END IF;
6676       l_count := 1;
6677 
6678       put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
6679                 '  Total specified stream elements falling in specified period: ' || l_eligible_strm_tbl.COUNT);
6680       put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
6681                 '  Total specified stream elements falling in non specified period: ' || l_non_eligible_strm_tbl.COUNT);
6682 
6683       IF l_exp_streams_tbl.COUNT > 0 THEN
6684         put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
6685                    '  Total expected stream activity so far: ' || l_exp_streams_tbl(l_stream_amount_count).value3_num);
6686       END IF;
6687 
6688       --Get all transactions for a contract for specified transaction types
6689       OPEN C_TRANSACTION_LINES(khr_rec.contract_id);
6690       FETCH C_TRANSACTION_LINES BULK COLLECT INTO l_master_trx_tbl;
6691       CLOSE C_TRANSACTION_LINES;
6692 
6693       put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
6694         '  This contract is having total transaction lines: ' || l_master_trx_tbl.COUNT);
6695 
6696       IF l_master_trx_tbl.COUNT = 0 THEN
6697         l_no_single_trx_exists := TRUE;
6698       ELSE
6699         l_no_single_trx_exists := FALSE;
6700       END IF;
6701 
6702       IF l_eligible_strm_tbl.COUNT > 0 THEN
6703         l_s_khr_id := -1;
6704         l_s_kle_id := -1;
6705         l_s_sty_id := -1;
6706 
6707         --Iterate through each eligible stream and identify respective bucket
6708         FOR i IN l_eligible_strm_tbl.FIRST..l_eligible_strm_tbl.LAST
6709         LOOP
6710           --If any of khr_id, kle_id and sty_id change then populate
6711           --subset of master trx table
6712           IF l_s_khr_id <> l_eligible_strm_tbl(i).KHR_ID OR
6713              l_s_kle_id <> l_eligible_strm_tbl(i).KLE_ID OR
6714              l_s_sty_id <> l_eligible_strm_tbl(i).STY_ID
6715           THEN
6716             l_s_khr_id := l_eligible_strm_tbl(i).KHR_ID;
6717             l_s_kle_id := l_eligible_strm_tbl(i).KLE_ID;
6718             l_s_sty_id := l_eligible_strm_tbl(i).STY_ID;
6719 
6720             --Identify transactions which relate to non-specified period streams
6721 
6722             put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
6723                   '  Identify transactions relating non-specified period streams ');
6724             IF l_subset_trx_tbl.COUNT > 0 AND l_non_eligible_strm_tbl.COUNT > 0 THEN
6725               FOR j IN l_subset_trx_tbl.FIRST..l_subset_trx_tbl.LAST
6726               LOOP
6727                 --If transaction or reversal date is within reporting period then consider
6728                 --For each accrual reversal transaction, there will be 2 transactions
6729                 --  1) Actual Accrual (TRANSACTION_DATE)
6730                 --  2) Accrual Reversal (TRANSACTION_REVERSAL_DATE)
6731                 --So for each reversal transaction, we need to pull two transactions
6732                 --and then compare actual accrual date and accrual reversal date
6733                 --against reporting period dates
6734                 --This logic is similar across INCOME REPORT in this procedure
6735                 IF l_subset_trx_tbl(j).TRANSACTION_DATE BETWEEN l_start_date AND l_end_date OR
6736                    NVL(l_subset_trx_tbl(j).REVERSAL_DATE, l_start_date-1) BETWEEN l_start_date AND l_end_date
6737                 THEN
6738                   FOR k IN l_non_eligible_strm_tbl.FIRST..l_non_eligible_strm_tbl.LAST
6739                   LOOP
6740                     IF l_subset_trx_tbl(j).LINE_NUMBER = l_non_eligible_strm_tbl(k).SE_LINE_NUMBER AND
6741                        l_subset_trx_tbl(j).KHR_ID = l_non_eligible_strm_tbl(k).KHR_ID AND
6742                        l_subset_trx_tbl(j).KLE_ID = l_non_eligible_strm_tbl(k).KLE_ID AND
6743                        l_subset_trx_tbl(j).STY_ID = l_non_eligible_strm_tbl(k).STY_ID
6744                     THEN
6745                       IF l_reports_tbl.exists(l_report_count) THEN
6746                         l_report_count := l_report_count + 1;
6747                       END IF;
6748                       l_reports_tbl(l_report_count).value1_text := L_NON_SPEC_STRMS_ASE;
6749                       l_reports_tbl(l_report_count).value7_num := l_non_eligible_strm_tbl(k).ORG_ID;
6750                       l_reports_tbl(l_report_count).value8_text := l_non_eligible_strm_tbl(k).PROD_NAME;
6751                       l_reports_tbl(l_report_count).value9_text := l_non_eligible_strm_tbl(k).STREAM_TYPE_NAME;
6752                       l_reports_tbl(l_report_count).value10_text := l_non_eligible_strm_tbl(k).STREAM_PURPOSE;
6753                       l_reports_tbl(l_report_count).value5_text := l_non_eligible_strm_tbl(k).CONTRACT_NUMBER;
6754                       l_reports_tbl(l_report_count).value6_text := l_non_eligible_strm_tbl(k).ASSET_NUMBER;
6755                       l_reports_tbl(l_report_count).value3_date := l_non_eligible_strm_tbl(k).STREAM_ELEMENT_DATE;
6756                       l_reports_tbl(l_report_count).value11_text := l_non_eligible_strm_tbl(k).CURRENCY_CODE;
6757                       l_reports_tbl(l_report_count).value19_text := l_non_eligible_strm_tbl(k).STREAM_PERIOD;
6758                       l_reports_tbl(l_report_count).value14_text := l_non_eligible_strm_tbl(k).OPERATING_UNIT;
6759                       l_reports_tbl(l_report_count).value9_num := l_non_eligible_strm_tbl(k).KHR_ID;
6760                       l_reports_tbl(l_report_count).value10_num := l_non_eligible_strm_tbl(k).KLE_ID;
6761                       l_reports_tbl(l_report_count).value12_num := l_non_eligible_strm_tbl(k).STY_ID;
6762 
6763                       l_reports_tbl(l_report_count).value3_text := l_subset_trx_tbl(j).TRANSACTION_TYPE;
6764                       l_reports_tbl(l_report_count).value2_text := l_subset_trx_tbl(j).TRX_NUMBER;
6765                       l_reports_tbl(l_report_count).value18_text := l_subset_trx_tbl(j).ACCRUAL_ACTIVITY;
6766                       l_reports_tbl(l_report_count).value4_date := l_subset_trx_tbl(j).DATE_ACCRUAL;
6767                       l_reports_tbl(l_report_count).value14_num := l_subset_trx_tbl(j).TCN_ID;
6768 
6769                       IF l_subset_trx_tbl(j).ACCRUAL_ACTIVITY = 'REVERSAL' AND
6770                          NVL(l_subset_trx_tbl(j).REVERSAL_DATE, l_start_date - 1) BETWEEN l_start_date AND l_end_date
6771                       THEN
6772                         l_reports_tbl(l_report_count).value3_num := l_non_eligible_strm_tbl(k).AMOUNT * -1;
6773                         l_reports_tbl(l_report_count).value1_date := l_subset_trx_tbl(j).REVERSAL_DATE;
6774                         l_reports_tbl(l_report_count).value12_text := l_subset_trx_tbl(j).REVERSAL_PERIOD;
6775                       ELSIF l_subset_trx_tbl(j).ACCRUAL_ACTIVITY <> 'REVERSAL' AND
6776                             l_subset_trx_tbl(j).TRANSACTION_DATE BETWEEN l_start_date AND l_end_date
6777                       THEN
6778                         l_reports_tbl(l_report_count).value3_num := l_non_eligible_strm_tbl(k).AMOUNT;
6779                         l_reports_tbl(l_report_count).value1_date := l_subset_trx_tbl(j).TRANSACTION_DATE;
6780                         l_reports_tbl(l_report_count).value12_text := l_subset_trx_tbl(j).TRANSACTION_PERIOD;
6781                       END IF;
6782 
6783                       IF l_subset_trx_tbl(j).ACCRUAL_ACTIVITY = 'REVERSAL' AND
6784                          l_subset_trx_tbl(j).TRANSACTION_DATE BETWEEN l_start_date AND l_end_date
6785                       THEN
6786                         l_report_count := l_report_count + 1;
6787                         l_reports_tbl(l_report_count) := l_reports_tbl(l_report_count-1);
6788                         l_reports_tbl(l_report_count).value3_num := l_non_eligible_strm_tbl(k).AMOUNT;
6789                         l_reports_tbl(l_report_count).value1_date := l_subset_trx_tbl(j).TRANSACTION_DATE;
6790                         l_reports_tbl(l_report_count).value12_text := l_subset_trx_tbl(j).TRANSACTION_PERIOD;
6791                         l_reports_tbl(l_report_count).value18_text := 'ACCRUAL';
6792                       END IF;
6793                       l_report_count := l_report_count + 1;
6794                       EXIT;
6795                     END IF;
6796                   END LOOP;
6797                 END IF;
6798               END LOOP;
6799             END IF;
6800 
6801             --Delete subset of transaction table
6802             l_subset_trx_tbl.delete;
6803 
6804             l_count := 1;
6805             l_accrual_count := 1;
6806             l_reverse_count := 1;
6807 
6808             --Out of master transaction table which comprises of all periods,
6809             --Get subset of transaction lines
6810             --Subset of transaction lines consists of only those transaction lines
6811             --whose contract, asset and stream type are same.
6812             --Moreover, generate line number to compare further against stream element
6813             --line number
6814             IF NOT l_no_single_trx_exists THEN
6815               FOR j IN l_master_trx_tbl.FIRST..l_master_trx_tbl.LAST
6816               LOOP
6817                 IF l_master_trx_tbl(j).KHR_ID = l_s_khr_id AND
6818                      l_master_trx_tbl(j).KLE_ID = l_s_kle_id AND
6819                      l_master_trx_tbl(j).STY_ID = l_s_sty_id
6820                 THEN
6821                    l_subset_trx_tbl(l_count) := l_master_trx_tbl(j);
6822                    l_subset_trx_tbl(l_count).LINE_NUMBER := l_accrual_count;
6823                    l_accrual_count := l_accrual_count + 1;
6824                    l_count := l_count + 1;
6825                 END IF;
6826 
6827                 --From these master transaction lines, if transaction date
6828                 --or reversal date is within reporting
6829                 --period, then put it into respective buckets
6830                 l_trx_exists := FALSE;
6831                 l_trx_date := l_master_trx_tbl(j).TRANSACTION_DATE;
6832                 IF (l_master_trx_tbl(j).TRANSACTION_DATE between l_start_date AND l_end_date) OR
6833                    (NVL(l_master_trx_tbl(j).REVERSAL_DATE,l_start_date - 1) between l_start_date AND l_end_date)
6834                 THEN
6835                   IF l_reports_tbl.exists(l_report_count) THEN
6836                     l_report_count := l_report_count + 1;
6837                   END IF;
6838                   l_trx_exists := TRUE;
6839                 END IF;
6840 
6841                 IF l_trx_exists THEN
6842                   l_reports_tbl(l_report_count).value7_num := l_master_trx_tbl(j).ORG_ID;
6843                   l_reports_tbl(l_report_count).value8_text := l_master_trx_tbl(j).PRODUCT_NAME;
6844                   l_reports_tbl(l_report_count).value9_text := l_master_trx_tbl(j).STREAM_TYPE;
6845                   l_reports_tbl(l_report_count).value10_text := l_master_trx_tbl(j).STREAM_PURPOSE;
6846                   l_reports_tbl(l_report_count).value5_text := khr_rec.CONTRACT_NUMBER;
6847                   l_reports_tbl(l_report_count).value6_text := l_master_trx_tbl(j).ASSET_NUMBER;
6848                   l_reports_tbl(l_report_count).value3_num := l_master_trx_tbl(j).AMOUNT;
6849                   l_reports_tbl(l_report_count).value11_text := l_master_trx_tbl(j).CURRENCY_CODE;
6850                   l_reports_tbl(l_report_count).value14_text := l_master_trx_tbl(j).OPERATING_UNIT;
6851                   l_reports_tbl(l_report_count).value9_num := l_master_trx_tbl(j).KHR_ID;
6852                   l_reports_tbl(l_report_count).value10_num := l_master_trx_tbl(j).KLE_ID;
6853                   l_reports_tbl(l_report_count).value12_num := l_master_trx_tbl(j).STY_ID;
6854 
6855                   l_reports_tbl(l_report_count).value3_text := l_master_trx_tbl(j).TRANSACTION_TYPE;
6856                   l_reports_tbl(l_report_count).value2_text := l_master_trx_tbl(j).TRX_NUMBER;
6857                   l_reports_tbl(l_report_count).value18_text := l_master_trx_tbl(j).ACCRUAL_ACTIVITY;
6858                   l_reports_tbl(l_report_count).value4_date := l_master_trx_tbl(j).DATE_ACCRUAL;
6859                   l_reports_tbl(l_report_count).value14_num := l_master_trx_tbl(j).TCN_ID;
6860 
6861                   --Identify whether transaction of corresponding stream type is
6862                   --specified or not on the template
6863                   l_is_spec_strm_type := FALSE;
6864                   IF l_spec_sty_id_tbl.COUNT > 0 THEN
6865                     FOR k IN l_spec_sty_id_tbl.FIRST..l_spec_sty_id_tbl.LAST
6866                     LOOP
6867                       IF l_master_trx_tbl(j).STY_ID = l_spec_sty_id_tbl(k) THEN
6868                         l_is_spec_strm_type := TRUE;
6869                         EXIT;
6870                       END IF;
6871                     END LOOP;
6872                   END IF;
6873                   --Based on specified or not, put it in different buckets
6874 
6875                   IF l_master_trx_tbl(j).ACCRUAL_ACTIVITY <> 'REVERSAL' THEN
6876                     IF l_is_spec_strm_type THEN
6877                       l_reports_tbl(l_report_count).value1_text := L_TRX_SPEC_STRMS_ASE;
6878                     ELSE
6879                       l_reports_tbl(l_report_count).value1_text := L_TRX_NON_SPEC_STRMS;
6880                     END IF;
6881                     l_reports_tbl(l_report_count).value1_date := l_master_trx_tbl(j).TRANSACTION_DATE;
6882                   ELSE
6883                     IF l_master_trx_tbl(j).TRANSACTION_DATE between l_start_date AND l_end_date THEN
6884                       IF l_is_spec_strm_type THEN
6885                         l_reports_tbl(l_report_count).value1_text := L_TRX_SPEC_STRMS_ASE;
6886                       ELSE
6887                         l_reports_tbl(l_report_count).value1_text := L_TRX_NON_SPEC_STRMS;
6888                       END IF;
6889                       l_reports_tbl(l_report_count).value18_text := 'ACCRUAL';
6890                       l_reports_tbl(l_report_count).value1_date := l_master_trx_tbl(j).TRANSACTION_DATE;
6891                     ELSE
6892                       IF l_is_spec_strm_type THEN
6893                         l_reports_tbl(l_report_count).value1_text := L_TRX_ADD_ACC_EVENTS;
6894                       ELSE
6895                         l_reports_tbl(l_report_count).value1_text := L_TRX_NON_SPEC_STRMS;
6896                         l_reports_tbl(l_report_count).value3_num := l_master_trx_tbl(j).AMOUNT * -1;
6897                       END IF;
6898                       l_reports_tbl(l_report_count).value18_text := 'REVERSAL';
6899                       l_reports_tbl(l_report_count).value1_date := l_master_trx_tbl(j).REVERSAL_DATE;
6900                     END IF;
6901                     IF (l_master_trx_tbl(j).TRANSACTION_DATE between l_start_date AND l_end_date) AND
6902                        (NVL(l_master_trx_tbl(j).REVERSAL_DATE,l_start_date - 1) between l_start_date AND l_end_date)
6903                     THEN
6904                       l_report_count := l_report_count + 1;
6905                       l_reports_tbl(l_report_count) := l_reports_tbl(l_report_count - 1);
6906                       IF l_is_spec_strm_type THEN
6907                         l_reports_tbl(l_report_count).value1_text := L_TRX_ADD_ACC_EVENTS;
6908                       ELSE
6909                         l_reports_tbl(l_report_count).value1_text := L_TRX_NON_SPEC_STRMS;
6910                         l_reports_tbl(l_report_count).value3_num := l_master_trx_tbl(j).AMOUNT * -1;
6911                       END IF;
6912                       l_reports_tbl(l_report_count).value18_text := 'REVERSAL';
6913                     END IF;
6914                   END IF;
6915                   l_report_count := l_report_count + 1;
6916                 END IF;
6917 
6918               END LOOP;
6919             END IF; --End if of l_no_single_trx_exists
6920           END IF; --End if of khr_id, kle_id and sty_id inequality
6921 
6922           --Populate transaction details for different bucket
6923           IF l_subset_trx_tbl.COUNT > 0 THEN --this means transaction exist
6924             --Search for non-reversal transactions and put it in transaction bucket
6925             l_trx_exists := FALSE;
6926             FOR j IN l_subset_trx_tbl.FIRST..l_subset_trx_tbl.LAST
6927             LOOP
6928               IF l_subset_trx_tbl(j).LINE_NUMBER = l_eligible_strm_tbl(i).SE_LINE_NUMBER
6929               THEN
6930                 l_trx_date := l_subset_trx_tbl(j).TRANSACTION_DATE;
6931                 IF l_trx_date <= l_end_date THEN
6932                   l_trx_exists := TRUE;
6933                 END IF;
6934                 EXIT;
6935               END IF;
6936             END LOOP; --End looping of l_subset_trx_tbl
6937 
6938             --If transaction doesn't exist, then put it into STRMS_WITHOUT_TRX bucket
6939             IF NOT l_trx_exists THEN
6940               l_reports_tbl(l_report_count).value1_text := L_STRMS_WITHOUT_TRX;
6941               l_reports_tbl(l_report_count).value7_num := l_eligible_strm_tbl(i).ORG_ID;
6942               l_reports_tbl(l_report_count).value8_text := l_eligible_strm_tbl(i).PROD_NAME;
6943               l_reports_tbl(l_report_count).value9_text := l_eligible_strm_tbl(i).STREAM_TYPE_NAME;
6944               l_reports_tbl(l_report_count).value10_text := l_eligible_strm_tbl(i).STREAM_PURPOSE;
6945               l_reports_tbl(l_report_count).value5_text := l_eligible_strm_tbl(i).CONTRACT_NUMBER;
6946               l_reports_tbl(l_report_count).value6_text := l_eligible_strm_tbl(i).ASSET_NUMBER;
6947               l_reports_tbl(l_report_count).value3_date := l_eligible_strm_tbl(i).STREAM_ELEMENT_DATE;
6948               l_reports_tbl(l_report_count).value3_num := l_eligible_strm_tbl(i).AMOUNT;
6949               l_reports_tbl(l_report_count).value11_text := l_eligible_strm_tbl(i).CURRENCY_CODE;
6950               l_reports_tbl(l_report_count).value19_text := l_eligible_strm_tbl(i).STREAM_PERIOD;
6951               l_reports_tbl(l_report_count).value14_text := l_eligible_strm_tbl(i).OPERATING_UNIT;
6952               l_reports_tbl(l_report_count).value9_num := l_eligible_strm_tbl(i).KHR_ID;
6953               l_reports_tbl(l_report_count).value10_num := l_eligible_strm_tbl(i).KLE_ID;
6954               l_reports_tbl(l_report_count).value12_num := l_eligible_strm_tbl(i).STY_ID;
6955               l_report_count := l_report_count + 1;
6956             END IF;
6957           ELSE --No transaction exists
6958             l_reports_tbl(l_report_count).value1_text := L_STRMS_WITHOUT_TRX;
6959             l_reports_tbl(l_report_count).value7_num := l_eligible_strm_tbl(i).ORG_ID;
6960             l_reports_tbl(l_report_count).value8_text := l_eligible_strm_tbl(i).PROD_NAME;
6961             l_reports_tbl(l_report_count).value9_text := l_eligible_strm_tbl(i).STREAM_TYPE_NAME;
6962             l_reports_tbl(l_report_count).value10_text := l_eligible_strm_tbl(i).STREAM_PURPOSE;
6963             l_reports_tbl(l_report_count).value5_text := l_eligible_strm_tbl(i).CONTRACT_NUMBER;
6964             l_reports_tbl(l_report_count).value6_text := l_eligible_strm_tbl(i).ASSET_NUMBER;
6965             l_reports_tbl(l_report_count).value3_date := l_eligible_strm_tbl(i).STREAM_ELEMENT_DATE;
6966             l_reports_tbl(l_report_count).value3_num := l_eligible_strm_tbl(i).AMOUNT;
6967             l_reports_tbl(l_report_count).value11_text := l_eligible_strm_tbl(i).CURRENCY_CODE;
6968             l_reports_tbl(l_report_count).value19_text := l_eligible_strm_tbl(i).STREAM_PERIOD;
6969             l_reports_tbl(l_report_count).value14_text := l_eligible_strm_tbl(i).OPERATING_UNIT;
6970             l_reports_tbl(l_report_count).value9_num := l_eligible_strm_tbl(i).KHR_ID;
6971             l_reports_tbl(l_report_count).value10_num := l_eligible_strm_tbl(i).KLE_ID;
6972             l_reports_tbl(l_report_count).value12_num := l_eligible_strm_tbl(i).STY_ID;
6973             l_report_count := l_report_count + 1;
6974           END IF; --End if of l_subset_trx_tbl.COUNT > 0
6975         END LOOP; -- End loop of l_eligible_strm_tbl
6976       END IF; --End if of l_eligible_strm_tbl
6977 
6978       put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
6979                   '  Now identify bucket related to transactions of non specified period streams - Outside loop');
6980       IF l_subset_trx_tbl.COUNT > 0 AND l_non_eligible_strm_tbl.COUNT > 0 THEN
6981         FOR j IN l_subset_trx_tbl.FIRST..l_subset_trx_tbl.LAST
6982         LOOP
6983           IF l_subset_trx_tbl(j).TRANSACTION_DATE BETWEEN l_start_date AND l_end_date OR
6984              NVL(l_subset_trx_tbl(j).REVERSAL_DATE, l_start_date-1) BETWEEN l_start_date AND l_end_date
6985           THEN
6986             FOR k IN l_non_eligible_strm_tbl.FIRST..l_non_eligible_strm_tbl.LAST
6987             LOOP
6988               IF l_subset_trx_tbl(j).LINE_NUMBER = l_non_eligible_strm_tbl(k).SE_LINE_NUMBER AND
6989                 l_subset_trx_tbl(j).KHR_ID = l_non_eligible_strm_tbl(k).KHR_ID AND
6990                 l_subset_trx_tbl(j).KLE_ID = l_non_eligible_strm_tbl(k).KLE_ID AND
6991                 l_subset_trx_tbl(j).STY_ID = l_non_eligible_strm_tbl(k).STY_ID
6992               THEN
6993                 IF l_reports_tbl.exists(l_report_count) THEN
6994                   l_report_count := l_report_count + 1;
6995                 END IF;
6996                 l_reports_tbl(l_report_count).value1_text := L_NON_SPEC_STRMS_ASE;
6997                 l_reports_tbl(l_report_count).value7_num := l_non_eligible_strm_tbl(k).ORG_ID;
6998                 l_reports_tbl(l_report_count).value8_text := l_non_eligible_strm_tbl(k).PROD_NAME;
6999                 l_reports_tbl(l_report_count).value9_text := l_non_eligible_strm_tbl(k).STREAM_TYPE_NAME;
7000                 l_reports_tbl(l_report_count).value10_text := l_non_eligible_strm_tbl(k).STREAM_PURPOSE;
7001                 l_reports_tbl(l_report_count).value5_text := l_non_eligible_strm_tbl(k).CONTRACT_NUMBER;
7002                 l_reports_tbl(l_report_count).value6_text := l_non_eligible_strm_tbl(k).ASSET_NUMBER;
7003                 l_reports_tbl(l_report_count).value3_date := l_non_eligible_strm_tbl(k).STREAM_ELEMENT_DATE;
7004                 l_reports_tbl(l_report_count).value11_text := l_non_eligible_strm_tbl(k).CURRENCY_CODE;
7005                 l_reports_tbl(l_report_count).value19_text := l_non_eligible_strm_tbl(k).STREAM_PERIOD;
7006                 l_reports_tbl(l_report_count).value14_text := l_non_eligible_strm_tbl(k).OPERATING_UNIT;
7007                 l_reports_tbl(l_report_count).value9_num := l_non_eligible_strm_tbl(k).KHR_ID;
7008                 l_reports_tbl(l_report_count).value10_num := l_non_eligible_strm_tbl(k).KLE_ID;
7009                 l_reports_tbl(l_report_count).value12_num := l_non_eligible_strm_tbl(k).STY_ID;
7010 
7011                 l_reports_tbl(l_report_count).value3_text := l_subset_trx_tbl(j).TRANSACTION_TYPE;
7012                 l_reports_tbl(l_report_count).value2_text := l_subset_trx_tbl(j).TRX_NUMBER;
7013                 l_reports_tbl(l_report_count).value18_text := l_subset_trx_tbl(j).ACCRUAL_ACTIVITY;
7014                 l_reports_tbl(l_report_count).value4_date := l_subset_trx_tbl(j).DATE_ACCRUAL;
7015                 l_reports_tbl(l_report_count).value14_num := l_subset_trx_tbl(j).TCN_ID;
7016 
7017                 IF l_subset_trx_tbl(j).ACCRUAL_ACTIVITY = 'REVERSAL' AND
7018                    NVL(l_subset_trx_tbl(j).REVERSAL_DATE, l_start_date-1) BETWEEN l_start_date AND l_end_date
7019                 THEN
7020                   l_reports_tbl(l_report_count).value3_num := l_non_eligible_strm_tbl(k).AMOUNT * -1;
7021                   l_reports_tbl(l_report_count).value1_date := l_subset_trx_tbl(j).REVERSAL_DATE;
7022                   l_reports_tbl(l_report_count).value12_text := l_subset_trx_tbl(j).REVERSAL_PERIOD;
7023                 ELSIF l_subset_trx_tbl(j).ACCRUAL_ACTIVITY <> 'REVERSAL' AND
7024                       l_subset_trx_tbl(j).TRANSACTION_DATE BETWEEN l_start_date AND l_end_date
7025                 THEN
7026                   l_reports_tbl(l_report_count).value3_num := l_non_eligible_strm_tbl(k).AMOUNT;
7027                   l_reports_tbl(l_report_count).value1_date := l_subset_trx_tbl(j).TRANSACTION_DATE;
7028                   l_reports_tbl(l_report_count).value12_text := l_subset_trx_tbl(j).TRANSACTION_PERIOD;
7029                 END IF;
7030 
7031                 IF l_subset_trx_tbl(j).ACCRUAL_ACTIVITY = 'REVERSAL' AND
7032                    l_subset_trx_tbl(j).TRANSACTION_DATE BETWEEN l_start_date AND l_end_date AND
7033                    NVL(l_subset_trx_tbl(j).REVERSAL_DATE, l_start_date-1) BETWEEN l_start_date AND l_end_date
7034                 THEN
7035                   l_report_count := l_report_count + 1;
7036                   l_reports_tbl(l_report_count) := l_reports_tbl(l_report_count-1);
7037                   l_reports_tbl(l_report_count).value3_num := l_non_eligible_strm_tbl(k).AMOUNT;
7038                   l_reports_tbl(l_report_count).value1_date := l_subset_trx_tbl(j).TRANSACTION_DATE;
7039                   l_reports_tbl(l_report_count).value12_text := l_subset_trx_tbl(j).TRANSACTION_PERIOD;
7040                   l_reports_tbl(l_report_count).value18_text := 'ACCRUAL';
7041                 END IF;
7042                 l_report_count := l_report_count + 1;
7043                 EXIT;
7044               END IF;
7045             END LOOP;
7046           END IF;
7047         END LOOP;
7048       END IF;
7049 
7050     END LOOP; --End loop of get_contracts_csr
7051 
7052     -- For secondary rep txn, reset the security policy for streams. MG Uptake
7053     IF g_representation_type = 'SECONDARY' THEN
7054       OKL_STREAMS_SEC_PVT.RESET_REPO_STREAMS;
7055     END IF;
7056 
7057     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
7058                 '  Inserting records in okl_g_reports_gt table for expected sum of stream elements amount');
7059 
7060     FORALL i IN l_exp_streams_tbl.FIRST..l_exp_streams_tbl.LAST
7061       INSERT INTO okl_g_reports_gt VALUES l_exp_streams_tbl(i);
7062 
7063     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
7064                 '  Inserting all records into okl_g_reports_gt');
7065 
7066     --At this point of time we will have details of all buckets, so just bulk insert into GT table
7067     FORALL i IN l_reports_tbl.FIRST..l_reports_tbl.LAST
7068       INSERT INTO okl_g_reports_gt VALUES l_reports_tbl(i);
7069 
7070     --Now whatever allotted in NON_SPEC_STRMS_ASE bucket,
7071     --we want to populate same in NON_SPEC_PERIOD_TRX_SPEC_PERIOD bucket
7072     --if it was accounted and posted
7073     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
7074                 '   Populating NON_SPEC_PERIOD_TRX_SPEC_PERIOD bucket based on NON_SPEC_STRMS_ASE bucket');
7075     INSERT INTO okl_g_reports_gt
7076       (  value1_text --Bucket name
7077         ,value2_text --trx number
7078         ,value3_text --trx_type name
7079         ,value4_text  --application name
7080         ,value5_text --contract number
7081         ,value6_text --asset number
7082         ,value7_text --book classification
7083         ,value8_text --product name
7084         ,value9_text --trx sty name
7085         ,value10_text --trx_sty_purpose
7086         ,value11_text --currency code
7087         ,value12_text --trx period name
7088         ,value13_text --trx account name
7089         ,value14_text --trx operating unit
7090         ,value15_text --legal entity name
7091         ,value16_text --trx ledger name
7092         ,value17_text --add subtract activity
7093         ,value18_text --trx event name
7094         -- Number Formatted Columns
7095         ,value1_num --trx dr amt
7096         ,value2_num --trx cr amt
7097         ,value3_num --trx net amt
7098         ,value4_num --code combination id
7099         ,value5_num --code combination id
7100         ,value6_num --trx ledger id
7101         ,value7_num --Org ID
7102         ,value8_num --TRY ID
7103         ,value9_num --KHR ID
7104         ,value10_num --asset Id
7105         ,value11_num --pdt id
7106         ,value12_num --sty id
7107         ,value13_num --application id
7108         ,value15_num --xla event id
7109         -- Date Formatted Columns
7110         ,value1_date --trx date
7111         ,value2_date --gl date
7112         ,value4_date --accrual date
7113       )
7114 
7115     SELECT   gl_detail_type_code         --value1_text
7116             ,trx_number                  --value2_text
7117             ,trx_type_name               --value3_text
7118             ,trx_application_name        --value4_text
7119             ,contract_number             --value5_text
7120             ,asset_number                --value6_text
7121             ,book_classification         --value7_text
7122             ,product_name                --value8_text
7123             ,trx_sty_name                --value9_text
7124             ,trx_sty_purpose             --value10_text
7125             ,currency_code               --value11_text
7126             ,trx_period_name             --value12_text -- Populated Only for Posted Entries
7127             ,trx_account_name            --value13_text
7128             ,trx_operating_unit_name     --value14_text
7129             ,NULL                        --value15_text -- Legal Entity Name
7130             ,trx_ledger_name             --value16_text
7131             ,trx_activity_code           --value17_text  -- Activity Code Add/Subtract for Dr/Cr
7132             ,trx_event_name              --value18_text
7133             -- Number Formatted Columns
7134             ,trx_dr_amount               --value1_num
7135             ,trx_cr_amount               --value2_num
7136             ,trx_net_amount              --value3_num
7137             ,code_combination_id         --value4_num
7138             ,code_combination_id         --value5_num
7139             ,trx_ledger_id               --value6_num
7140             ,trx_operating_unit_id       --value7_num
7141             ,trx_try_id                  --value8_num -- Transaction Type ID
7142             ,trx_khr_id                  --value9_num
7143             ,txl_asset_id                --value10_num
7144             ,trx_pdt_id                  --value11_num
7145             ,trx_sty_id                  --value12_num
7146             ,trx_application_id          --value13_num
7147             ,trx_xla_event_id            --value15_num
7148             -- Date Formatted Columns
7149             ,trx_date                    --value1_date
7150             ,gl_date                     --value2_date
7151             ,accrual_date                --value4_date
7152         FROM
7153         (
7154           SELECT 'NON_SPEC_PERIOD_TRX_SPEC_PERIOD'        gl_detail_type_code
7155                 ,gh.currency_code                         currency_code
7156                 ,app.application_name                     trx_application_name
7157                 ,glcc.concatenated_segments               trx_account_name
7158                 ,xvl.name                                 trx_event_name
7159                 ,try.name                                 trx_type_name
7160                 ,sty.NAME                                 trx_sty_name
7161                 ,trx.trx_number                           trx_number
7162                 ,chr.contract_number                      contract_number
7163                 ,KLE.NAME                                     asset_number
7164                 ,gts.deal_type                            book_classification
7165                 ,pdt.NAME                                 product_name
7166                 ,sty.styb_purpose_meaning                 trx_sty_purpose
7167                 ,ou.name                                  trx_operating_unit_name
7168                 ,ledger.name                              trx_ledger_name
7169                 ,gh.period_name                           trx_period_name
7170                 ,xl.code_combination_id                   code_combination_id
7171                 -- Number Format Columns
7172                 ,xl.entered_dr                            trx_dr_amount
7173                 ,xl.entered_cr                            trx_cr_amount
7174                 ,( NVL(xl.entered_dr,0) - NVL(xl.entered_cr,0) )
7175                                                           trx_net_amount
7176                 ,DECODE( xl.entered_dr, NULL, 'SUBTRACT', 'ADD' )
7177                                                           trx_activity_code
7178                 ,xl.ledger_id                             trx_ledger_id
7179                 ,trx.org_id                               trx_operating_unit_id
7180                 ,trx.khr_id                               trx_khr_id
7181                 ,txl.kle_id                               txl_asset_id
7182                 ,khr.pdt_id                               trx_pdt_id
7183                 ,txl.sty_id                               trx_sty_id
7184                 ,xl.application_id                        trx_application_id
7185                 ,xe.event_id                              trx_xla_event_id
7186                 -- DATE Format Columns
7187                 ,trx.transaction_date                     trx_date
7188                 ,dist.gl_date                             gl_date
7189                 ,trx.date_accrual                         accrual_date
7190                 -- Additional Columns
7191                 ,trx.id                                   trx_id
7192                 ,txl.id                                   trx_txl_id
7193                 ,trx.try_id                               trx_try_id
7194                 ,dist.id                                  trx_dist_id
7195           FROM    -- GL Tables: Import Reference, GL Header and Lines
7196                 gl_je_lines               gl
7197                ,gl_je_headers             gh
7198                ,gl_code_combinations_kfv  glcc
7199                ,gl_import_references      gi
7200                -- Code Combination GT Table
7201                ,okl_code_cmbns_gt         cc
7202                -- SLA Entities
7203                ,xla_ae_lines              xl
7204                ,xla_ae_headers            xh
7205                ,xla_distribution_links    xd
7206                ,xla_events                xe
7207                ,xla_event_types_vl        xvl
7208               -- OLM Entities
7209                ,okl_trns_acc_dstrs_all    dist
7210                ,okl_txl_cntrct_lns_all    txl
7211                ,okl_trx_contracts_all     trx
7212                ,okl_trx_types_v           try
7213                ,okl_strm_type_v           sty
7214                ,okc_k_headers_all_b       chr
7215                ,okl_k_headers             khr
7216                ,okl_products              pdt
7217                ,okl_ae_tmpt_sets_all      aes
7218                ,okl_st_gen_tmpt_sets_all  gts
7219                -- To fetch Names
7220                ,hr_operating_units        ou
7221                ,gl_ledgers                ledger
7222                ,fnd_application_vl        app
7223                ,okc_k_lines_v        kle
7224                ,okl_report_trx_params trep
7225           WHERE
7226                  -- Restrict the Code Combinations to the one setup on the Report
7227                  gl.code_combination_id = cc.ccid AND
7228                  -- GL Tables
7229                  gl.ledger_id        = p_ledger_id
7230             AND  gh.je_header_id     = gl.je_header_id
7231             AND  gh.ledger_id        = gl.ledger_id
7232             AND  gh.je_source        = 'Lease'
7233             AND  gh.status           =  'P'  -- Pick Only Posted Journals
7234             AND  glcc.code_combination_id = gl.code_combination_id
7235             AND  gi.je_header_id     = gh.je_header_id
7236             AND  gi.je_line_num      = gl.je_line_num
7237                  -- GL to XLA Relations
7238             AND  xl.gl_sl_link_id    = gi.gl_sl_link_id
7239             AND  xl.gl_sl_link_table = gi.gl_sl_link_table
7240             AND  xl.ledger_id        = gl.ledger_id
7241                  -- XLA Predicates
7242             AND  xl.ae_header_id     = xh.ae_header_id
7243             AND  xd.application_id   = 540  -- Restrict to Lease Journals
7244             AND  xd.ae_header_id     = xh.ae_header_id
7245             AND  xd.ae_line_num      = xl.ae_line_num
7246             AND  xe.event_id         = xd.event_id
7247             AND  xe.application_id   = xvl.application_id
7248             AND  xvl.event_type_code = xe.event_type_code
7249                  -- XLA to OLM Predicates
7250             AND  xd.event_id         = dist.accounting_event_id
7251             AND  dist.id             = xd.source_distribution_id_num_1
7252             AND  dist.posted_yn      = 'Y'
7253                  -- OLM Predicates
7254             AND  dist.source_table   = 'OKL_TXL_CNTRCT_LNS'
7255             AND  dist.source_id      = txl.id
7256             AND  trx.id              = txl.tcn_id
7257             AND  trx.try_id          = try.id
7258             AND  txl.sty_id          = sty.id
7259             AND  trx.khr_id          = chr.id
7260             AND  chr.id              = khr.id
7261             AND  khr.pdt_id          = pdt.id
7262             AND  pdt.aes_id          = aes.id
7263             AND  aes.gts_id          = gts.id
7264             -- Predicates to fetch the Names
7265             AND  ou.organization_id  = trx.org_id
7266             AND  ledger.ledger_id    = trx.set_of_books_id
7267             AND  app.application_id  = xe.application_id
7268 	    AND  kle.id = txl.kle_id
7269             -- Restrict the Journal Entries to be in between Start and End Dates
7270             AND  dist.gl_date between p_start_date AND p_end_date
7271             AND  trx.transaction_date not between p_start_date AND p_end_date
7272             AND  trep.try_id = trx.try_id  -- trx_try_id
7273             AND  NVL( trep.sty_id, txl.sty_id) = txl.sty_id -- Stream Type Id
7274             AND  DECODE( xl.entered_dr, NULL, 'SUBTRACT', 'ADD' ) = trep.add_substract_code -- Debit/Credit or Add/Subtract
7275             AND  trep.report_id = p_report_id
7276         );
7277     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
7278       'Completed the processing of streams to transactions and transactions to streams for income:'
7279       || TO_CHAR(SYSDATE, 'DD-MON-YYY HH:MM:SS') );
7280     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
7281       'Time taken for Income Report Processing (STRMS <-> TRANSACTIONS): '
7282        || ( SYSDATE - l_trace_time ) * 86400 || ' Seconds' );
7283 
7284     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
7285       'End: ' || l_api_name ||'(-)' );
7286     -- Set the Return Status and return back
7287     x_return_status := l_return_status;
7288 
7289   EXCEPTION
7290     WHEN OTHERS THEN
7291       IF C_CHECK_STREAMS_DEFINED%ISOPEN THEN
7292         CLOSE C_CHECK_STREAMS_DEFINED;
7293       END IF;
7294       IF C_ALL_ACCRUABLE_STREAMS%ISOPEN THEN
7295         CLOSE C_ALL_ACCRUABLE_STREAMS;
7296       END IF;
7297       IF C_STREAM_TYPES_NOT_SPECIFIED%ISOPEN THEN
7298         CLOSE C_STREAM_TYPES_NOT_SPECIFIED;
7299       END IF;
7300       IF C_GET_PRODUCTS%ISOPEN THEN
7301         CLOSE C_GET_PRODUCTS;
7302       END IF;
7303       IF C_MASTER_STREAMS_SPECIFIED%ISOPEN THEN
7304         CLOSE C_MASTER_STREAMS_SPECIFIED;
7305       END IF;
7306       IF C_MASTER_STREAMS_ALL%ISOPEN THEN
7307         CLOSE C_MASTER_STREAMS_ALL;
7308       END IF;
7309       IF C_TRANSACTION_LINES%ISOPEN THEN
7310         CLOSE C_TRANSACTION_LINES;
7311       END IF;
7312       IF C_GET_ADJUSTMENT_TRANSACTIONS%ISOPEN THEN
7313         CLOSE C_GET_ADJUSTMENT_TRANSACTIONS;
7314       END IF;
7315       IF GET_CONTRACTS_CSR%ISOPEN THEN
7316         CLOSE GET_CONTRACTS_CSR;
7317       END IF;
7318       -- If any exception was thrwon before resetting reporting streams, then do that
7319       IF g_representation_type = 'SECONDARY' THEN
7320         OKL_STREAMS_SEC_PVT.RESET_REPO_STREAMS;
7321       END IF;
7322 
7323       x_return_status := okl_api.g_ret_sts_error;
7324 END POPULATE_STRM_TO_TRX_DATA;
7325 
7326   -- Start of comments
7327   --
7328   -- Function Name   :  generate_gross_inv_recon_rpt.
7329   -- Description    : Main Function called by CP
7330   --
7331   -- Business Rules  :
7332   -- Parameters       :
7333   -- Version      : 1.0
7334   -- History        : Ravindranath Gooty created.
7335   --
7336   -- End of comments
7337   FUNCTION generate_gross_inv_recon_rpt
7338   RETURN BOOLEAN IS
7339     -- Cursor to fetch teh Operating Unit
7340     CURSOR get_param_num_value( p_report_id NUMBER , p_parameter_type_code VARCHAR2)
7341     IS
7342       SELECT  param_num_value1
7343         FROM  okl_report_parameters oup
7344        WHERE  oup.report_id = p_report_id
7345          AND  oup.parameter_type_code = p_parameter_type_code ;
7346 
7347     -- Cursor to fetch GL Period From Date
7348     CURSOR get_period_from_date IS
7349       SELECT gl.start_date from_date
7350       FROM   gl_period_statuses    gl
7351       WHERE  gl.application_id = 540
7352       AND    gl.set_of_books_id = p_ledger_id
7353       AND    gl.period_name = p_gl_period_from;
7354 
7355     -- Cursor to fetch GL Period To Date
7356     CURSOR get_period_to_date IS
7357       SELECT gl.end_date to_date
7358       FROM   gl_period_statuses    gl
7359       WHERE  gl.application_id = 540
7360       AND    gl.set_of_books_id = p_ledger_id
7361       AND    gl.period_name = p_gl_period_to;
7362 
7363     --Cursor to fetch representation type of ledger
7364     CURSOR C_GET_LEGER_REPRENSENTATION(p_ledger_id NUMBER) IS
7365       SELECT NVL(LEDGER_CATEGORY_CODE,'PRIMARY') LEDGER_CATEGORY_CODE
7366       FROM   GL_LEDGERS
7367       WHERE  LEDGER_ID = p_ledger_id;
7368 
7369     l_init_msg_list VARCHAR2(1);
7370     l_msg_count     NUMBER;
7371     l_msg_data      VARCHAR2(4000);
7372     l_start_date    DATE;
7373     l_end_date      DATE;
7374 
7375     -----------------------------------------------------------------
7376     -- Declare Process Variable
7377     -----------------------------------------------------------------
7378     l_api_version      CONSTANT NUMBER         := 1;
7379     l_api_name         CONSTANT VARCHAR2(30)   := 'generate_gross_inv_recon_rpt';
7380     l_return_status    VARCHAR2(1)             := OKL_API.G_RET_STS_SUCCESS;
7381     -- Debug related parameters
7382     l_module CONSTANT fnd_log_messages.module%TYPE := G_MODULE || l_api_name;
7383     l_debug_enabled       VARCHAR2(10);
7384     is_debug_procedure_on BOOLEAN;
7385     is_debug_statement_on BOOLEAN;
7386     -- Local Variable declaration
7387     l_org_id              NUMBER;
7388     l_le_id               NUMBER;
7389     l_proceed_flag        BOOLEAN := TRUE;
7390   BEGIN
7391     l_debug_enabled := okl_debug_pub.check_log_enabled;
7392     -- check for logging on PROCEDURE level
7393     is_debug_procedure_on := okl_debug_pub.check_log_on(l_module,FND_LOG.LEVEL_PROCEDURE);
7394     -- check for logging on STATEMENT level
7395     is_debug_statement_on := okl_debug_pub.check_log_on(l_module,FND_LOG.LEVEL_STATEMENT);
7396     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'P',
7397                'begin debug OKLRRPTB.pls call ' || l_api_name);
7398     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'P',
7399                'BEGIN API OKL_REPORT_GENERATOR_PVT.generate_gross_inv_recon_rpt');
7400     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
7401       'Start of the Main Logic');
7402     l_init_msg_list := OKL_API.G_FALSE;
7403     l_return_status := OKL_API.G_RET_STS_SUCCESS;
7404 
7405     --Get representation type of the ledger
7406     OPEN C_GET_LEGER_REPRENSENTATION(p_ledger_id);
7407     FETCH C_GET_LEGER_REPRENSENTATION INTO g_representation_type;
7408     CLOSE C_GET_LEGER_REPRENSENTATION;
7409 
7410     -- First up, validate whether user has access to run the Report Or not
7411     l_proceed_flag :=  validate_orgs_access( p_ledger_id => p_ledger_id );
7412 
7413     IF l_proceed_flag = FALSE
7414     THEN
7415       put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
7416         'Validation Failed !!! Erroring Out !!!!');
7417       INSERT INTO OKL_G_REPORTS_GT ( VALUE1_TEXT )
7418         VALUES ('OKL_RECON_REP_NO_ACCESS' );
7419       RETURN TRUE; -- Always return TRUE
7420     END IF;
7421     -- Fetch the Operating Unit
7422     FOR t_rec IN get_param_num_value(
7423                   p_report_id => p_report_id
7424                  ,p_parameter_type_code => 'OPERATING_UNIT')
7425     LOOP
7426       l_org_id := t_rec.param_num_value1;
7427     END LOOP;
7428     -- Fetch the Legal Entity Setup on the Report Definition
7429     FOR t_rec IN get_param_num_value(
7430                   p_report_id => p_report_id
7431                  ,p_parameter_type_code => 'LEGAL_ENTITY')
7432     LOOP
7433       l_le_id := t_rec.param_num_value1;
7434     END LOOP;
7435 
7436     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
7437       'Before calling populate_code_combinations: p_report_id=' || p_report_id
7438       || 'p_ledger_id= ' || p_ledger_id );
7439     populate_code_combinations(
7440        p_api_version    => l_api_version
7441       ,p_init_msg_list  => l_init_msg_list
7442       ,x_return_status  => l_return_status
7443       ,x_msg_count      => l_msg_count
7444       ,x_msg_data       => l_msg_data
7445       ,p_report_id      => p_report_id
7446       ,p_ledger_id      => p_ledger_id);
7447     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
7448       'After call to the populate_code_combinations ' || l_return_status );
7449     IF (l_return_status = okl_api.g_ret_sts_unexp_error) THEN
7450       RAISE okl_api.g_exception_unexpected_error;
7451     ELSIF (l_return_status = okl_api.g_ret_sts_error) THEN
7452       RAISE okl_api.g_exception_error;
7453     END IF;
7454 
7455     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
7456       'Before calling populate_products: p_report_id=' || p_report_id
7457       || 'l_org_id= ' || l_org_id );
7458     populate_products(
7459        p_api_version    => l_api_version
7460       ,p_init_msg_list  => l_init_msg_list
7461       ,x_return_status  => l_return_status
7462       ,x_msg_count      => l_msg_count
7463       ,x_msg_data       => l_msg_data
7464       ,p_report_id      => p_report_id
7465       ,p_org_id         => l_org_id );
7466     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
7467       'After call to the populate_products ' || l_return_status );
7468     IF (l_return_status = okl_api.g_ret_sts_unexp_error) THEN
7469       RAISE okl_api.g_exception_unexpected_error;
7470     ELSIF (l_return_status = okl_api.g_ret_sts_error) THEN
7471       RAISE okl_api.g_exception_error;
7472     END IF;
7473 
7474     -- Retreive the GL period from and period to dates.
7475     OPEN get_period_from_date;
7476     FETCH get_period_from_date
7477       INTO l_start_date;
7478     IF get_period_from_date%NOTFOUND
7479     THEN
7480       RAISE okl_api.g_exception_error;
7481     END IF;
7482     CLOSE get_period_from_date;
7483 
7484     OPEN get_period_to_date;
7485     FETCH get_period_to_date
7486       INTO l_end_date;
7487     IF get_period_to_date%NOTFOUND
7488     THEN
7489       RAISE okl_api.g_exception_error;
7490     END IF;
7491     CLOSE get_period_to_date;
7492 
7493     ---------------------------------------------------------------------------
7494     -- Invoking the API to Populate Transaction Details Originated from
7495     --  OLM, AR, AP Applications on behalf of a Lease Contract
7496     -- Transaction may be at any stage of its life, Un Accounted, Accounted,
7497     --   Not Imported in GL, Imported in GL, Not Posted in GL, Posted in GL
7498     ---------------------------------------------------------------------------
7499     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
7500       'Before call to the populate_trx_data ' );
7501     populate_trx_data(
7502        p_api_version   => l_api_version
7503       ,p_init_msg_list => l_init_msg_list
7504       ,x_return_status => l_return_status
7505       ,x_msg_count     => l_msg_count
7506       ,x_msg_data      => l_msg_data
7507       ,p_report_id     => p_report_id
7508       ,p_ledger_id     => p_ledger_id
7509       ,p_start_date    => l_start_date
7510       ,p_end_date      => l_end_date
7511       ,p_org_id        => l_org_id
7512       ,p_le_id         => l_le_id);
7513     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
7514       'After call to the populate_trx_data ' || l_return_status );
7515     IF (l_return_status = okl_api.g_ret_sts_unexp_error) THEN
7516       RAISE okl_api.g_exception_unexpected_error;
7517     ELSIF (l_return_status = okl_api.g_ret_sts_error) THEN
7518       RAISE okl_api.g_exception_error;
7519     END IF;
7520 
7521     ------------------------------------------------------------
7522     -- Call the API to Calculate the Stream Balances
7523     ------------------------------------------------------------
7524 
7525     IF p_report_type_code <> 'INCOME' THEN
7526       put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
7527         'Before call to the  process_strm_bal_details' );
7528       process_strm_bal_details(
7529          p_api_version     => l_api_version
7530         ,p_init_msg_list   => l_init_msg_list
7531         ,x_return_status   => l_return_status
7532         ,x_msg_count       => l_msg_count
7533         ,x_msg_data        => l_msg_data
7534         ,p_report_id       => p_report_id
7535         ,p_start_date      => l_start_date
7536         ,p_end_date        => l_end_date
7537         ,p_ledger_id       => p_ledger_id
7538         ,p_org_id          => l_org_id
7539         ,p_le_id           => l_le_id);
7540       put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
7541         'After call to the process_strm_bal_details ' || l_return_status );
7542       IF (l_return_status = okl_api.g_ret_sts_unexp_error) THEN
7543         RAISE okl_api.g_exception_unexpected_error;
7544       ELSIF (l_return_status = okl_api.g_ret_sts_error) THEN
7545         RAISE okl_api.g_exception_error;
7546       END IF;
7547     END IF;
7548 
7549     ------------------------------------------------------------
7550     -- Call the API to Populate the GL Journal Details
7551     --  for Accounting Drill downs and Comparision
7552     ------------------------------------------------------------
7553     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
7554       'Before call to the  populate_acc_data' );
7555     populate_acc_data(
7556        p_api_version     => l_api_version
7557       ,p_init_msg_list   => l_init_msg_list
7558       ,x_return_status   => l_return_status
7559       ,x_msg_count       => l_msg_count
7560       ,x_msg_data        => l_msg_data
7561       ,p_report_id       => p_report_id
7562       ,p_start_date      => l_start_date
7563       ,p_end_date        => l_end_date
7564       ,p_ledger_id       => p_ledger_id
7565       ,p_org_id          => l_org_id
7566       ,p_le_id           => l_le_id);
7567     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
7568       'After call to the populate_acc_data ' || l_return_status );
7569     IF (l_return_status = okl_api.g_ret_sts_unexp_error) THEN
7570       RAISE okl_api.g_exception_unexpected_error;
7571     ELSIF (l_return_status = okl_api.g_ret_sts_error) THEN
7572       RAISE okl_api.g_exception_error;
7573     END IF;
7574 
7575     ---------------------------------------------------------------------------
7576     -- Invoking the API to Populate the GL Opening Balances as on Period From
7577     --   and GL Closing Balances as on Period To both in Transaction and
7578     --   Functional Currency
7579     ---------------------------------------------------------------------------
7580     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
7581       'Before call to the populate_gl_balances ' );
7582     populate_gl_balances(
7583        p_api_version   => l_api_version
7584       ,p_init_msg_list => l_init_msg_list
7585       ,x_return_status => l_return_status
7586       ,x_msg_count     => l_msg_count
7587       ,x_msg_data      => l_msg_data
7588       ,p_ledger_id     => p_ledger_id
7589       ,p_period_from   => p_gl_period_from
7590       ,p_period_to     => p_gl_period_to );
7591     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
7592       'After call to the populate_gl_balances ' || l_return_status );
7593     IF (l_return_status = okl_api.g_ret_sts_unexp_error) THEN
7594       RAISE okl_api.g_exception_unexpected_error;
7595     ELSIF (l_return_status = okl_api.g_ret_sts_error) THEN
7596       RAISE okl_api.g_exception_error;
7597     END IF;
7598 
7599     ------------------------------------------------------------
7600     -- If report type is INCOME then call the API to reconcile
7601     -- between streams to transactions
7602     ------------------------------------------------------------
7603     IF p_report_type_code = 'INCOME' THEN
7604       put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
7605         'Before call to the  POPULATE_STRM_TO_TRX_DATA' );
7606       POPULATE_STRM_TO_TRX_DATA(
7607          p_api_version     => l_api_version
7608         ,p_init_msg_list   => l_init_msg_list
7609         ,x_return_status   => l_return_status
7610         ,x_msg_count       => l_msg_count
7611         ,x_msg_data        => l_msg_data
7612         ,p_report_id       => p_report_id
7613         ,p_ledger_id       => p_ledger_id
7614         ,p_start_date      => l_start_date
7615         ,p_end_date        => l_end_date
7616         ,p_org_id          => l_org_id
7617         ,p_le_id           => l_le_id);
7618       put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'S',
7619         'After call to the POPULATE_STRM_TO_TRX_DATA ' || l_return_status );
7620       IF (l_return_status = okl_api.g_ret_sts_unexp_error) THEN
7621         RAISE okl_api.g_exception_unexpected_error;
7622       ELSIF (l_return_status = okl_api.g_ret_sts_error) THEN
7623         RAISE okl_api.g_exception_error;
7624       END IF;
7625     END IF;
7626 
7627 
7628     put_in_log(l_debug_enabled,is_debug_procedure_on,is_debug_statement_on,l_module, 'P',
7629           'End API OKL_REPORT_GENERATOR_PVT.generate_gross_inv_recon_rpt');
7630     RETURN TRUE;
7631   EXCEPTION
7632     WHEN OTHERS
7633     THEN
7634       l_return_status := okl_api.g_ret_sts_unexp_error;
7635       -- Set the oracle error message
7636       okl_api.set_message(p_app_name     => okc_api.g_app_name,
7637                           p_msg_name     => g_unexpected_error,
7638                           p_token1       => g_sqlcode_token,
7639                           p_token1_value => SQLCODE,
7640                           p_token2       => g_sqlerrm_token,
7641                           p_token2_value => SQLERRM);
7642 
7643       RETURN FALSE;
7644 
7645   END generate_gross_inv_recon_rpt;
7646 
7647 END OKL_REPORT_GENERATOR_PVT;