[Home] [Help]
PACKAGE BODY: APPS.CST_REVENUECOGSMATCH_PVT
Source
1 PACKAGE BODY CST_RevenueCogsMatch_PVT AS
2 /* $Header: CSTRCMVB.pls 120.83.12020000.21 2013/03/01 05:37:11 vnagasow ship $ */
3
4 G_PKG_NAME CONSTANT VARCHAR2(30) := 'CST_RevenueCogsMatch_PVT';
5 G_DEBUG CONSTANT VARCHAR2(1) := NVL(FND_PROFILE.VALUE('AFLOG_ENABLED'),'N');
6 G_LOG_HEAD CONSTANT VARCHAR2(40) := 'cst.plsql.'||G_PKG_NAME;
7 G_LOG_LEVEL CONSTANT NUMBER := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
8 g_module_name VARCHAR2(255) := G_LOG_HEAD;
9 g_mrp_debug CONSTANT VARCHAR2(1) := NVL(fnd_profile.value('MRP_DEBUG'),'N');
10
11 -- factional number representing 23 hours, 59 minutes and 59 seconds
12 EndDateNum CONSTANT NUMBER := 1-1/86400;
13
14 --BUG#7463298
15 g_ledger_id NUMBER := -1;
16
17 --------------------------------------------------------------------------------------
18 -- Local routines section : BEGIN
19 --------------------------------------------------------------------------------------
20 -- 1 set g_module_name
21 -- 2 call debug only with line
22 PROCEDURE debug
23 ( line IN VARCHAR2,
24 msg_prefix IN VARCHAR2 DEFAULT 'CST',
25 msg_module IN VARCHAR2 DEFAULT g_module_name,
26 msg_level IN NUMBER DEFAULT FND_LOG.LEVEL_STATEMENT);
27
28 -- Ensure the MMT transaction_date and the acct_period_id match
29 PROCEDURE ensure_mmt_per_and_date
30 (x_return_status OUT NOCOPY VARCHAR2,
31 x_msg_count OUT NOCOPY NUMBER,
32 x_msg_data OUT NOCOPY VARCHAR2);
33
34 --{BUG#7387575
35 PROCEDURE crrl_preparation
36 (p_batch_size IN NUMBER DEFAULT 1000
37 ,p_ledger_id IN NUMBER DEFAULT NULL);
38
39 PROCEDURE nb_req_active;
40
41 PROCEDURE updation_potential_crrl
42 (p_batch_size IN NUMBER DEFAULT 1000
43 ,p_ledger_id IN NUMBER);
44 --}
45
46 --
47 -- PROCEDURE check_program_running
48 -- p_prg_name IN VARCHAR2 program short name
49 -- p_app_id IN NUMBER program app id
50 -- p_ledger_id IN VARCHAR2 ledger parameter
51 -- x_running OUT NOCOPY VARCHAR2
52 -- Y if any of COGS program is running
53 -- N if the no COGS program is running
54 -- x_status OUT NOCOPY VARCHAR2
55 -- S if no exception found
56 -- E no program exist exception
57 -- U other exception
58 --
59 PROCEDURE check_program_running
60 ( p_prg_name IN VARCHAR2
61 , p_app_id IN NUMBER
62 , p_ledger_id IN NUMBER
63 , x_running OUT NOCOPY VARCHAR2
64 , x_status OUT NOCOPY VARCHAR2
65 , x_out_msg OUT NOCOPY VARCHAR2);
66
67 --
68 -- PROCEDURE check_eligible_lines
69 -- p_ledger_id IN NUMBER Ledger Id
70 -- x_status OUT NOCOPY VARCHAR2
71 -- x_out_msg OUT NOCOPY VARCHAR2
72 PROCEDURE check_eligible_lines
73 ( p_ledger_id IN NUMBER
74 , x_status OUT NOCOPY VARCHAR2
75 , x_out_msg OUT NOCOPY VARCHAR2);
76
77 -----------------------------------------------------------------------------
78 -- Start of comments --
79 -- --
80 -- PROCEDURE --
81 -- Match_RevenueCOGS This API is the outer wrapper for the concurrent --
82 -- request that matches COGS to revenue for OM lines. --
83 -- It is run in four phases, each followed by a --
84 -- commit: --
85 -- 1) Record any sales order issues and RMA receipts --
86 -- that have not yet been inserted into CRCML and --
87 -- CCE. --
88 -- 2) Process incoming revenue events and insert --
89 -- revenue recognition per period by OM line into --
90 -- CRRL. --
91 -- 3) Compare CRRL to CCE (via CRCML) and create new --
92 -- COGS recognition events where they don't match. --
93 -- 4) Cost all of the Cogs Recogntion Events that were --
94 -- just created in bulk. --
95 -- --
96 -- --
97 -- VERSION 1.0 --
98 -- --
99 -- PARAMETERS --
100 -- --
101 -- P_LOW_DATE Lower bound for the date range. --
102 -- P_HIGH_DATE Upper bound for the date range. --
103 -- P_PHASE Set to a number, this parameter indicates that only --
104 -- that phase # should be run. Otherwise all phases --
105 -- should be run. --
106 -- --
107 -- HISTORY: --
108 -- 04/20/05 Bryan Kuntz Created --
109 -- End of comments --
110 -----------------------------------------------------------------------------
111 PROCEDURE Match_RevenueCOGS(
115 p_phase IN NUMBER,
112 x_return_status OUT NOCOPY VARCHAR2,
113 x_dummy_out OUT NOCOPY NUMBER,
114 p_api_version IN NUMBER,
116 p_ledger_id IN NUMBER DEFAULT NULL, --BUG#5726230
117 p_low_date IN VARCHAR2,
118 p_high_date IN VARCHAR2,
119 p_gather_statistics IN VARCHAR2 DEFAULT 'N', --BUG#10627578
120 p_neg_req_id IN NUMBER DEFAULT NULL
121 ) IS
122
123 l_api_name CONSTANT VARCHAR2(30) := 'Match_RevenueCOGS';
124 l_api_version CONSTANT NUMBER := 1.0;
125 l_api_message VARCHAR2(1000);
126
127 l_return_status VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
128 l_dummy_status VARCHAR2(1); /* not sure why AR needs this, hopefully can remove it ... */
129 l_msg_count NUMBER := 0;
130 l_msg_data VARCHAR2(8000) := '';
131 l_stmt_num NUMBER := 0;
132
133 l_module CONSTANT VARCHAR2(100) := G_LOG_HEAD ||'.'||l_api_name;
134 l_unexpLog CONSTANT BOOLEAN := (FND_LOG.LEVEL_UNEXPECTED >= G_LOG_LEVEL) AND FND_LOG.TEST(FND_LOG.LEVEL_UNEXPECTED, l_module);
135 l_errorLog CONSTANT BOOLEAN := l_unexpLog and (FND_LOG.LEVEL_ERROR >= G_LOG_LEVEL);
136 l_eventLog CONSTANT BOOLEAN := l_errorLog and (FND_LOG.LEVEL_EVENT >= G_LOG_LEVEL);
137 l_procLog CONSTANT BOOLEAN := l_eventLog and (FND_LOG.LEVEL_PROCEDURE >= G_LOG_LEVEL);
138 l_stmtLog CONSTANT BOOLEAN := l_procLog and (FND_LOG.LEVEL_STATEMENT >= G_LOG_LEVEL);
139
140 program_exception EXCEPTION;
141 CONC_STATUS BOOLEAN; -- variable for concurrent request completion status
142
143 l_request_id NUMBER; -- FND global settings
144 l_user_id NUMBER;
145 l_login_id NUMBER;
146 l_pgm_app_id NUMBER;
147 l_pgm_id NUMBER;
148
149 l_schema VARCHAR2(30); -- variables for fnd_stats
150 l_status VARCHAR2(1);
151 l_industry VARCHAR2(1);
152
153 l_low_date DATE;
154 l_high_date DATE;
155 l_prg_name VARCHAR2(30);
156 x_running VARCHAR2(1);
157 x_out_msg VARCHAR2(2000);
158 l_control_id NUMBER;
159 already_running EXCEPTION;
160
161 BEGIN
162
163 -- Standard start of API savepoint
164 SAVEPOINT Match_RevenueCOGS_PVT;
165
166 l_stmt_num := 0;
167
168 debug('Match_RevenueCOGS+');
169 debug(' p_api_version:'||p_api_version);
170 debug(' p_phase : '|| p_phase);
171 debug(' p_low_date : '|| p_low_date);
172 debug(' p_high_date : '|| p_high_date);
173 debug(' p_ledger_id : '|| p_ledger_id);
174 debug(' p_neg_req_id: '|| p_neg_req_id);
175
176 -- Standard call to check for call compatibility
177 IF NOT FND_API.Compatible_API_Call (
178 l_api_version,
179 p_api_version,
180 l_api_name,
181 G_PKG_NAME ) THEN
182 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
183 END IF;
184
185 -- Initialize message list if p_init_msg_list is set to TRUE
186 FND_MSG_PUB.initialize;
187
188 -- Initialize API return status to success
189 x_return_status := FND_API.G_RET_STS_SUCCESS;
190
191 -- API Body
192
193 -- Populate WHO column variables
194 l_request_id := FND_GLOBAL.conc_request_id;
195 l_user_id := FND_GLOBAL.user_id;
196 l_login_id := FND_GLOBAL.login_id;
197 l_pgm_app_id := FND_GLOBAL.PROG_APPL_ID;
198 l_pgm_id := FND_GLOBAL.CONC_PROGRAM_ID;
199
200
201 -- Avoid multiple submission
202 IF p_phase = 1 THEN l_prg_name := 'CSTRCMCR1';
203 ELSIF p_phase = 2 THEN l_prg_name := 'CSTRCMCR';
204 ELSIF p_phase = 3 THEN l_prg_name := 'CSTRCMCR3';
205 END IF;
206
207 debug(' Program submitted :'||l_prg_name);
208
209 check_program_running( p_prg_name => l_prg_name
210 , p_app_id => 702
211 , p_ledger_id => p_ledger_id
212 , x_running => x_running
213 , x_status => x_return_status
214 , x_out_msg => x_out_msg);
215
216 debug(' x_running :' ||x_running);
217
218 IF x_return_status <> 'S' THEN
219 RAISE program_exception;
220 END IF;
221
222 IF x_running = 'Y' THEN
223 RAISE already_running;
224 END IF;
225
226
227
228
229 /********************************************************************************
230 * Phase 1 - Record any sales order issues and RMA receipts that have not yet *
231 * been inserted into the Revenue/COGS matching data model (CRCML and *
232 * CCE). *
233 ********************************************************************************/
234
235 IF ( p_phase = 1 ) THEN
236
237 --{BUG#7463298
238 IF p_ledger_id IS NOT NULL THEN
239 g_ledger_id := p_ledger_id;
240 END IF;
241 debug(' g_ledger_id : ' ||g_ledger_id);
242 --}
243
244 l_stmt_num := 10;
245 debug(l_stmt_num);
246
247 Insert_SoIssues(p_request_id => l_request_id,
248 p_user_id => l_user_id,
249 p_login_id => l_login_id,
250 p_pgm_app_id => l_pgm_app_id,
254 IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
251 p_pgm_id => l_pgm_id,
252 x_return_status => l_return_status);
253
255 IF l_errorLog THEN
256 FND_LOG.string(FND_LOG.LEVEL_ERROR,l_module||'.'||l_stmt_num,substr(SQLERRM,1,250));
257 END IF;
258 raise program_exception;
259 END IF;
260
261 l_stmt_num := 20;
262 debug(l_stmt_num);
263 insert_RmaReceipts(p_request_id => l_request_id,
264 p_user_id => l_user_id,
265 p_login_id => l_login_id,
266 p_pgm_app_id => l_pgm_app_id,
267 p_pgm_id => l_pgm_id,
268 x_return_status => l_return_status);
269
270 IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
271 IF l_errorLog THEN
272 FND_LOG.string(FND_LOG.LEVEL_ERROR,l_module||'.'||l_stmt_num,substr(SQLERRM,1,250));
273 END IF;
274 raise program_exception;
275 END IF;
276
277 IF l_eventLog THEN
278 FND_LOG.string(FND_LOG.LEVEL_EVENT,l_module||'.20'
279 ,'Completed phase 1 successfully.');
280 END IF;
281
282 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_success) THEN
283 FND_MESSAGE.set_name('BOM', 'CST_SUCCESSFUL_PHASE');
284 FND_MESSAGE.set_token('PHASE', '1');
285 FND_MSG_PUB.ADD;
286 END IF;
287
288 Print_MessageStack;
289
290 -- Commit phase 1 insertions
291 COMMIT;
292 SAVEPOINT Match_RevenueCOGS_PVT;
293
294 END IF; -- phase 1
295
296 /********************************************************************************
297 * Phase 2 - Process incoming revenue events and insert revenue recognition per *
298 * period by OM line into CRRL *
299 ********************************************************************************/
300
301 IF ( p_phase = 2 ) THEN
302
303 -- convert input parameters to dates
304 l_low_date := fnd_date.canonical_to_date(p_low_date);
305 l_high_date := trunc(fnd_date.canonical_to_date(p_high_date))+EndDateNum;
306 IF (l_low_date IS NULL) THEN
307 l_low_date := sysdate - (365*20); -- go back 20 years
308 END IF;
309 -- prevent the collection of future revenue recognition events
310 IF (l_high_date IS NULL OR l_high_date > sysdate) THEN
311 l_high_date := sysdate;
312 END IF;
313
314 -- Call AR's procedure to populate CRRL with revenue recognition event data
315 -- by OM line and accounting period.
316 l_stmt_num := 30;
317
318 IF l_stmtLog THEN
319 FND_LOG.string(FND_LOG.LEVEL_STATEMENT, l_module||'.'||l_stmt_num,
320 'Calling AR_MATCH_REV_COGS_GRP.populate_cst_tables with '||
321 'p_from_gl_date = '||l_low_date||','||
322 'p_to_gl_date = '||l_high_date||',');
323 END IF;
324
325 ar_match_rev_cogs_grp.populate_cst_tables (
326 p_api_version => 1,
327 p_from_gl_date => l_low_date,
328 p_to_gl_date => l_high_date,
329 --{BUG#7533570 add ledger_id
330 p_ledger_id => p_ledger_id,
331 --}
332 x_status => l_dummy_status,
333 x_return_status => l_return_status,
334 x_msg_count => l_msg_count,
335 x_msg_data => l_msg_data
336 );
337
338 IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
339 IF l_errorLog THEN
340 FND_LOG.string(FND_LOG.LEVEL_ERROR,l_module||'.'||l_stmt_num,substr(SQLERRM,1,250));
341 END IF;
342 raise program_exception;
343 END IF;
344
345 IF l_eventLog THEN
346 FND_LOG.string(FND_LOG.LEVEL_EVENT,l_module||'.20'
347 ,'Completed phase 2 successfully.');
348 END IF;
349
350 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_success) THEN
351 FND_MESSAGE.set_name('BOM', 'CST_SUCCESSFUL_PHASE');
352 FND_MESSAGE.set_token('PHASE', '2');
353 FND_MSG_PUB.ADD;
354 END IF;
355
356 Print_MessageStack;
357
358 --
359 -- -1 for all ledgers, this is unexpected as ledger is mandatory
360 -- nevertheless, we support the -1 concept for customer to make the ledger_id not madatory
361 -- otherwise control_id will be ledger_id
362 --
363 IF p_ledger_id IS NULL THEN
364 l_control_id := -1;
365 ELSE
366 l_control_id := p_ledger_id;
367 END IF;
368
369 -- Update the "Process Upto Date" in the revenue / COGS control table
370 UPDATE cst_revenue_cogs_control
371 SET last_process_upto_date = l_high_date,
372 last_update_date = sysdate,
373 last_updated_by = l_user_id,
374 last_update_login = l_login_id,
375 request_id = l_request_id
376 WHERE control_id = l_control_id;
377
378
379 IF (SQL%ROWCOUNT = 0) THEN
380 INSERT INTO cst_revenue_cogs_control (
381 CONTROL_ID,
382 LAST_PROCESS_UPTO_DATE,
383 LAST_UPDATE_DATE,
384 LAST_UPDATED_BY,
385 CREATION_DATE,
386 CREATED_BY,
387 LAST_UPDATE_LOGIN,
388 REQUEST_ID,
389 PROGRAM_APPLICATION_ID,
390 PROGRAM_ID,
394 sysdate,
391 PROGRAM_UPDATE_DATE)
392 VALUES ( l_control_id,
393 l_high_date,
395 l_user_id,
396 sysdate,
397 l_user_id,
398 l_login_id,
399 l_request_id,
400 l_pgm_app_id,
401 l_pgm_id,
402 sysdate);
403 END IF;
404
405 -- Commit phase 2 inserts and updates
406 COMMIT;
407 SAVEPOINT Match_RevenueCOGS_PVT;
408
409 -- Now we should gather statistics on CRRL since the above call to
410 -- ar_match_rev_cogs_grp.populate_cst_tables would have added a bunch of rows
411 -- with potentially_unmatched_flag = 'Y'. This column is a histogram, so we
412 -- need to gather stats before running phase 3, which relies heavily on this
413 -- column.
414 IF NOT FND_INSTALLATION.GET_APP_INFO('BOM', l_status, l_industry, l_schema) THEN
415 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
416 END IF;
417
418
419 --{BUG#10627578
420 IF p_gather_statistics = 'Y' THEN
421 IF l_schema IS NOT NULL THEN
422 l_stmt_num := 37;
423 FND_STATS.GATHER_TABLE_STATS(l_schema, 'CST_REVENUE_RECOGNITION_LINES');
424 END IF;
425 END IF;
426 --}
427 SAVEPOINT Match_RevenueCOGS_PVT; -- necessary because of commit within gather_table_stats
428
429 END IF; -- phase 2
430
431
432 IF ( p_phase = 3 ) THEN
433
434 /********************************************************************************
435 * Phase 3 - Compare CRRL to CCE (via CRCML) and create new COGS recognition *
436 * events where they don't match. *
437 ********************************************************************************/
438
439 -- Update all rows from OM so that any OLTP rows coming in during the
440 -- concurrent request will not get picked up in this run.
441 l_stmt_num := 40;
442
443
444 UPDATE cst_revenue_recognition_lines crrl
445 SET potentially_unmatched_flag = 'Y'
446 WHERE potentially_unmatched_flag = 'U'
447 AND ledger_id = p_ledger_id;
448
449
450 l_stmt_num := 50;
451 Create_CogsRecognitionEvents(p_request_id => l_request_id,
452 p_user_id => l_user_id,
453 p_login_id => l_login_id,
454 p_pgm_app_id => l_pgm_app_id,
455 p_pgm_id => l_pgm_id,
456 x_return_status => l_return_status,
457 p_ledger_id => p_ledger_id -- BUG#5726230
458 ,p_neg_req_id => p_neg_req_id); -- BUG#7387575
459
460 IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
461 IF l_errorLog THEN
462 FND_LOG.string(FND_LOG.LEVEL_ERROR,l_module||'.'||l_stmt_num,substr(SQLERRM,1,250));
463 END IF;
464 raise program_exception;
465 END IF;
466
467 -- Any rows marked as potentially mismatched would have been picked up for
468 -- comparison this time. For such rows to be mismatched at this point would
469 -- require a new initiating event. That event will reset the flag to 'Y' at
470 -- that time, so fow now we set it to NULL indicating it is matched.
471 l_stmt_num := 60;
472
473
474 IF l_eventLog THEN
475 FND_LOG.string(FND_LOG.LEVEL_EVENT,l_module||'.20'
476 ,'Completed phase 3 successfully.');
477 END IF;
478
479 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_success) THEN
480 FND_MESSAGE.set_name('BOM', 'CST_SUCCESSFUL_PHASE');
481 FND_MESSAGE.set_token('PHASE', '3');
482 FND_MSG_PUB.ADD;
483 END IF;
484
485 Print_MessageStack;
486
487 -- Commit phase 3 work
488 COMMIT;
489 SAVEPOINT Match_RevenueCOGS_PVT;
490
491 /********************************************************************************
492 * Phase 4 - Cost all of the Cogs Recogntion Events that were just created in *
493 * bulk. The cost manager would never be able to handle this. *
494 ********************************************************************************/
495
496 l_stmt_num := 70;
497 Cost_BulkCogsRecTxns(p_request_id => l_request_id,
498 p_user_id => l_user_id,
499 p_login_id => l_login_id,
500 p_pgm_app_id => l_pgm_app_id,
501 p_pgm_id => l_pgm_id,
502 x_return_status => l_return_status,
503 p_ledger_id => p_ledger_id --BUG5726230
504 ,p_neg_req_id => p_neg_req_id);--BUG7387575
505
506 IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
507 IF l_errorLog THEN
508 FND_LOG.string(FND_LOG.LEVEL_ERROR,l_module||'.'||l_stmt_num,substr(SQLERRM,1,250));
509 END IF;
510 raise program_exception;
511 END IF;
512
513 IF l_eventLog THEN
514 FND_LOG.string(FND_LOG.LEVEL_EVENT,l_module||'.20'
515 ,'Completed phase 4 successfully.');
516 END IF;
517
518 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_success) THEN
519 FND_MESSAGE.set_name('BOM', 'CST_SUCCESSFUL_PHASE');
520 FND_MESSAGE.set_token('PHASE', '4');
521 FND_MSG_PUB.ADD;
522 END IF;
523
527 -- Commit phase 4 work
524 -- Print the message stack to the log file
525 Print_MessageStack;
526
528 COMMIT;
529
530 END IF; -- phases 3 and 4
531
532
533 debug(' x_return_status : '||x_return_status);
534 debug(' x_dummy_out : '||x_dummy_out );
535 debug('Match_RevenueCOGS-');
536
537 EXCEPTION
538 WHEN already_running THEN
539 FND_FILE.PUT_LINE(FND_FILE.LOG, x_out_msg);
540 x_return_status := fnd_api.G_RET_STS_ERROR;
541
542 CONC_STATUS := FND_CONCURRENT.SET_COMPLETION_STATUS('ERROR',l_module||' failed to complete.');
543
544 Print_MessageStack;
545
546 WHEN program_exception THEN
547 x_return_status := fnd_api.g_ret_sts_error;
548
549 FND_MESSAGE.set_name('BOM', 'CST_PLSQL_ERROR');
550 FND_MESSAGE.set_token('PACKAGE', G_PKG_NAME);
551 FND_MESSAGE.set_token('PROCEDURE',l_api_name);
552 FND_MESSAGE.set_token('STATEMENT',to_char(l_stmt_num));
553 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_error) THEN
554 FND_MSG_PUB.ADD;
555 END IF;
556
557 IF l_errorLog THEN
558 FND_LOG.MESSAGE(FND_LOG.LEVEL_ERROR,l_module||'.'||l_stmt_num,TRUE);
559 END IF;
560
561 CONC_STATUS := FND_CONCURRENT.SET_COMPLETION_STATUS('ERROR',l_module||' failed to complete.');
562
563 DECLARE
564 l_cnt NUMBER;
565 l_data VARCHAR2(2000);
566 BEGIN
567 fnd_msg_pub.Count_And_Get
568 (p_encoded => 'T',
569 p_count => l_cnt,
570 p_data => l_data);
571 FND_FILE.put_line
572 (fnd_file.log,
573 'PROGRAM_EXCEPTION IN Match_RevenueCOGS at ('||l_stmt_num||'): '||
574 SUBSTRB(l_data,1,400) );
575 END;
576
577 Print_MessageStack;
578
579 WHEN OTHERS THEN
580
581 x_return_status := fnd_api.g_ret_sts_unexp_error ;
582
583 IF l_unexpLog THEN
584 FND_LOG.string(FND_LOG.LEVEL_UNEXPECTED,G_LOG_HEAD || '.'||l_api_name||'.'||l_stmt_num
585 ,substrb(SQLERRM,1,250));
586 END IF;
587
588 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
589 FND_MESSAGE.set_name('BOM', 'CST_UNEXP_ERROR');
590 FND_MESSAGE.set_token('PACKAGE', G_PKG_NAME);
591 FND_MESSAGE.set_token('PROCEDURE',l_api_name);
592 FND_MESSAGE.set_token('STATEMENT',to_char(l_stmt_num));
593 FND_MSG_PUB.ADD;
594 END IF;
595
596 FND_FILE.put_line(fnd_file.log, 'OTHERS EXCEPTION IN Match_RevenueCOGS:'||substrb(SQLERRM,1,250) );
597
598 CONC_STATUS := FND_CONCURRENT.SET_COMPLETION_STATUS('ERROR',l_module||' failed to complete.');
599
600 Print_MessageStack;
601
602 END Match_RevenueCOGS;
603
604
605 -----------------------------------------------------------------------------
606 -- Start of comments --
607 -- --
608 -- PROCEDURE --
609 -- Insert_SoIssues This procedure handles the insertion of sales order --
610 -- issue transactions in batch into the matching data --
611 -- model. Most sales orders will be inserted into the --
612 -- matching data model by the Cost Processor. Any that --
613 -- are not processed at that time (e.g. - OPM orgs) --
614 -- will be inserted here. --
615 -- --
616 -- VERSION 1.0 --
617 -- --
618 -- PARAMETERS --
619 -- X_RETURN_STATUS Success/Error/Unexplained error - 'S','E', or 'U' --
620 -- WHO columns --
621 -- --
622 -- HISTORY: --
623 -- 04/22/05 Bryan Kuntz Created using cursor --
624 -- End of comments --
625 -----------------------------------------------------------------------------
626
627 PROCEDURE Insert_SoIssues(
628 x_return_status OUT NOCOPY VARCHAR2,
629 p_request_id IN NUMBER,
630 p_user_id IN NUMBER,
631 p_login_id IN NUMBER,
632 p_pgm_app_id IN NUMBER,
633 p_pgm_id IN NUMBER
634 ) IS
635
636 l_api_name CONSTANT VARCHAR2(30) := 'Insert_SoIssues';
637 l_api_message VARCHAR2(100);
638 l_stmt_num NUMBER := 0;
639 l_return_status VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
640
641 l_module CONSTANT VARCHAR2(100) := G_LOG_HEAD ||'.'||l_api_name;
642 l_unexpLog CONSTANT BOOLEAN := (FND_LOG.LEVEL_UNEXPECTED >= G_LOG_LEVEL) AND FND_LOG.TEST(FND_LOG.LEVEL_UNEXPECTED, l_module);
643 l_errorLog CONSTANT BOOLEAN := l_unexpLog and (FND_LOG.LEVEL_ERROR >= G_LOG_LEVEL);
644 l_eventLog CONSTANT BOOLEAN := l_errorLog and (FND_LOG.LEVEL_EVENT >= G_LOG_LEVEL);
645 l_procLog CONSTANT BOOLEAN := l_eventLog and (FND_LOG.LEVEL_PROCEDURE >= G_LOG_LEVEL);
646 l_stmtLog CONSTANT BOOLEAN := l_procLog and (FND_LOG.LEVEL_STATEMENT >= G_LOG_LEVEL);
647
648 CURSOR c_sales_order_issues IS
649 SELECT mmt.trx_source_line_id cogs_om_line_id,
653 mmt.organization_id,
650 mmt.distribution_account_id cogs_acct_id,
651 mp.deferred_cogs_account def_cogs_acct_id,
652 mmt.transaction_id mmt_txn_id,
654 mmt.inventory_item_id,
655 mmt.transaction_date,
656 mmt.cost_group_id,
657 (-1*mmt.primary_quantity) quantity
658 FROM mtl_material_transactions mmt,
659 mtl_parameters mp,
660 mtl_secondary_inventories msi,
661 mtl_system_items_b item
662 ,cst_acct_info_v caiv --BUG#7463298
663 WHERE mmt.transaction_action_id in (1,7)
664 /* do not pick up physical SO issue in a drop shipment flow */
665 AND NVL(mmt.parent_transaction_id, -1) = DECODE(mmt.transaction_action_id,1,-1,mmt.parent_transaction_id)
666 AND mmt.transaction_type_id IN (33, 30)
667 AND mmt.transaction_source_type_id = 2
668 AND mmt.costed_flag = 'N'
669 AND mmt.COGS_RECOGNITION_PERCENT IS NULL
670 AND mmt.SO_ISSUE_ACCOUNT_TYPE = 2 /* deferred COGS */
671 AND mp.organization_id = mmt.organization_id
672 --{BUG#7463298
673 AND mp.organization_id = caiv.organization_id
674 AND nvl(mp.process_enabled_flag,'N') = 'N' /*BUG#9306124 - Ristricted this qry for Discrete orgs*/
675 AND DECODE(g_ledger_id,-1
676 ,caiv.ledger_id,g_ledger_id) = caiv.ledger_id
677 --}
678 AND mmt.subinventory_code = msi.secondary_inventory_name (+) /* Logical txn does not have sub code */
679 AND mmt.organization_id = msi.organization_id (+)
680 AND NVL(msi.asset_inventory,1) = 1
681 AND item.inventory_item_id = mmt.inventory_item_id
682 AND item.organization_id = mmt.organization_id
683 AND item.inventory_asset_flag = 'Y'
684 /* BUG#9306124 - Seperated query for Process orgs with this union(Start)*/
685 UNION ALL
686 SELECT mmt.trx_source_line_id cogs_om_line_id,
687 mmt.distribution_account_id cogs_acct_id,
688 mp.deferred_cogs_account def_cogs_acct_id,
689 mmt.transaction_id mmt_txn_id,
690 mmt.organization_id,
691 mmt.inventory_item_id,
692 mmt.transaction_date,
693 mmt.cost_group_id,
694 (-1*mmt.primary_quantity) quantity
695 FROM mtl_material_transactions mmt,
696 mtl_parameters mp,
697 mtl_secondary_inventories msi,
698 mtl_system_items_b item
699 ,cst_acct_info_v caiv --BUG#7463298
700 WHERE mmt.transaction_action_id in (1,7)
701 /* do not pick up physical SO issue in a drop shipment flow */
702 AND NVL(mmt.parent_transaction_id, -1) = DECODE(mmt.transaction_action_id,1,-1,mmt.parent_transaction_id)
703 AND mmt.transaction_type_id IN (33, 30)
704 AND mmt.transaction_source_type_id = 2
705 AND mmt.opm_costed_flag IS NOT NULL
706 AND mmt.COGS_RECOGNITION_PERCENT IS NULL
707 AND mmt.SO_ISSUE_ACCOUNT_TYPE = 2 /* deferred COGS */
708 AND mp.organization_id = mmt.organization_id
709 --{BUG#7463298
710 AND mp.organization_id = caiv.organization_id
711 AND nvl(mp.process_enabled_flag,'N') = 'Y' /*BUG#9306124 - Process org */
712 AND DECODE(g_ledger_id,-1
713 ,caiv.ledger_id,g_ledger_id) = caiv.ledger_id
714 --}
715 AND mmt.subinventory_code = msi.secondary_inventory_name (+) /* Logical txn does not have sub code */
716 AND mmt.organization_id = msi.organization_id (+)
717 AND NVL(msi.asset_inventory,1) = 1
718 AND item.inventory_item_id = mmt.inventory_item_id
719 AND item.organization_id = mmt.organization_id
720 AND item.inventory_asset_flag = 'Y'
721 /* BUG#9306124 - Seperated query for Process orgs with this union (End)*/
722 ;
723
724 l_so_count pls_integer := 0;
725
726 BEGIN
727
728 -- Standard start of API savepoint
729 SAVEPOINT Insert_SoIssues_PVT;
730
731 l_stmt_num := 0;
732
733 IF l_procLog THEN
734 FND_LOG.string(FND_LOG.LEVEL_PROCEDURE,l_module||'.begin',
735 'Entering '||G_PKG_NAME||'.'||l_api_name
736 );
737 END IF;
738
739 -- Initialize return values
740 x_return_status := FND_API.G_RET_STS_SUCCESS;
741
742 -- API Body
743
744 debug(' g_ledger_id : '||g_ledger_id);
745
746 -- Loop through all sales order issues that have not yet been inserted into the Deferred COGS data model.
747 -- Most of them would have been inserted by the cost processor during the course of normal processing.
748 l_stmt_num := 10;
749 <<all_sales_orders_loop>>
750 FOR cv_so_issues IN c_sales_order_issues LOOP
751
752 Insert_OneSoIssue(
753 p_api_version => 1.0,
754 p_user_id => p_user_id,
755 p_login_id => p_login_id,
756 p_request_id => p_request_id,
757 p_pgm_app_id => p_pgm_app_id,
758 p_pgm_id => p_pgm_id,
759 x_return_status => l_return_status,
760 p_cogs_om_line_id => cv_so_issues.cogs_om_line_id,
761 p_cogs_acct_id => cv_so_issues.cogs_acct_id,
762 p_def_cogs_acct_id => cv_so_issues.def_cogs_acct_id,
763 p_mmt_txn_id => cv_so_issues.mmt_txn_id,
767 p_cost_group_id => cv_so_issues.cost_group_id,
764 p_organization_id => cv_so_issues.organization_id,
765 p_item_id => cv_so_issues.inventory_item_id,
766 p_transaction_date => cv_so_issues.transaction_date,
768 p_quantity => cv_so_issues.quantity
769 );
770
771 IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
772 x_return_status := fnd_api.g_ret_sts_error;
773 FND_MESSAGE.set_name('BOM', 'CST_FAILED_DEFCOGS_SO_INSERT');
774 FND_MESSAGE.set_token('COGS_OM_LINE', to_char(cv_so_issues.cogs_om_line_id));
775 FND_MESSAGE.set_token('MMT_TXN_ID',to_char(cv_so_issues.mmt_txn_id));
776 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
777 FND_MSG_PUB.ADD;
778 END IF;
779 IF l_errorLog THEN
780 FND_LOG.message(FND_LOG.LEVEL_ERROR, l_module||'.10',TRUE);
781 END IF;
782 ELSE
783 l_so_count := l_so_count + 1;
784 END IF;
785
786 END LOOP all_sales_orders_loop;
787
788 IF l_eventLog THEN
789 l_api_message := 'Inserted '||to_char(l_so_count)||' sales order issues into CCE.';
790 FND_LOG.string(FND_LOG.LEVEL_EVENT, G_LOG_HEAD ||'.'|| l_api_name || '.10', l_api_message);
791 END IF;
792
793 -- End API Body
794
795 IF l_proclog THEN
796 fnd_log.string(FND_LOG.LEVEL_PROCEDURE,l_module||'.end',
797 'Exiting '||G_PKG_NAME||'.'||l_api_name||' with '||
798 'x_return_status = '||x_return_status
799 );
800 END IF;
801
802 EXCEPTION
803
804 WHEN OTHERS THEN
805 ROLLBACK TO Insert_SoIssues_PVT;
806 x_return_status := fnd_api.g_ret_sts_unexp_error ;
807
808 IF l_unexpLog THEN
809 FND_LOG.string(FND_LOG.LEVEL_UNEXPECTED,G_LOG_HEAD || '.'||l_api_name||l_stmt_num
810 ,'Insert_SoIssues ('||to_char(l_stmt_num)||') : '||substr(SQLERRM,1,200));
811 END IF;
812
813 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
814 FND_MESSAGE.set_name('BOM', 'CST_UNEXP_ERROR');
815 FND_MESSAGE.set_token('PACKAGE', G_PKG_NAME);
816 FND_MESSAGE.set_token('PROCEDURE',l_api_name);
817 FND_MESSAGE.set_token('STATEMENT',to_char(l_stmt_num));
818 FND_MSG_PUB.ADD;
819 END IF;
820
821 FND_FILE.put_line(fnd_file.log, 'OTHERS EXCEPTION IN Insert_SoIssues:'||substrb(SQLERRM,1,250) );
822
823
824 END Insert_SoIssues;
825
826 -----------------------------------------------------------------------------
827 -- Start of comments --
828 -- --
829 -- PROCEDURE --
830 -- Insert_RmaReceipts This procedure handles the insertion of RMA --
831 -- receipt transactions in batch into the matching --
832 -- data model. Most RMA receipts will be inserted --
833 -- by the Cost Processor. This bulk procedure will --
834 -- pick up the rest. --
835 -- --
836 -- VERSION 1.0 --
837 -- --
838 -- PARAMETERS --
839 -- X_RETURN_STATUS Success/Error/Unexplained error - 'S','E', or 'U' --
840 -- WHO columns --
841 -- --
842 -- HISTORY: --
843 -- 05/06/05 Bryan Kuntz Created --
844 -- End of comments --
845 -----------------------------------------------------------------------------
846
847 PROCEDURE Insert_RmaReceipts(
848 x_return_status OUT NOCOPY VARCHAR2,
849 p_request_id IN NUMBER,
850 p_user_id IN NUMBER,
851 p_login_id IN NUMBER,
852 p_pgm_app_id IN NUMBER,
853 p_pgm_id IN NUMBER
854 ) IS
855
856 l_api_name CONSTANT VARCHAR2(30) := 'Insert_RmaReceipts';
857 l_api_message VARCHAR2(1000);
858 l_module CONSTANT VARCHAR2(100) := G_LOG_HEAD ||'.'||l_api_name;
859 l_unexpLog CONSTANT BOOLEAN := (FND_LOG.LEVEL_UNEXPECTED >= G_LOG_LEVEL) AND FND_LOG.TEST(FND_LOG.LEVEL_UNEXPECTED, l_module);
860 l_errorLog CONSTANT BOOLEAN := l_unexpLog and (FND_LOG.LEVEL_ERROR >= G_LOG_LEVEL);
861 l_eventLog CONSTANT BOOLEAN := l_errorLog and (FND_LOG.LEVEL_EVENT >= G_LOG_LEVEL);
862 l_procLog CONSTANT BOOLEAN := l_eventLog and (FND_LOG.LEVEL_PROCEDURE >= G_LOG_LEVEL);
863 l_stmtLog CONSTANT BOOLEAN := l_procLog and (FND_LOG.LEVEL_STATEMENT >= G_LOG_LEVEL);
864
865 l_stmt_num NUMBER := 0;
866
867 CURSOR c_rma_receipts IS
868 SELECT /* LEADING(mmt) */
869 mmt.trx_source_line_id rma_om_line_id,
870 ool.reference_line_id cogs_om_line_id,
871 mmt.transaction_id mmt_txn_id,
872 mmt.transaction_date transaction_date,
873 (-1*mmt.primary_quantity) event_quantity,
874 cce.parent_event_id prior_event_id,
875 cce.cogs_percentage cogs_percentage,
876 sum(cce.event_quantity) prior_event_quantity
877 FROM mtl_material_transactions mmt,
878 oe_order_lines_all ool,
882 ,cst_acct_info_v caiv --BUG#7463298
879 cst_revenue_cogs_match_lines crcml,
880 cst_cogs_events cce,
881 mtl_system_items_b item
883 ,mtl_parameters mp --BUG#9306124
884 WHERE mmt.transaction_source_type_id = 12
885 AND mmt.transaction_action_id in (26,27) -- UT: see if this forces index use, otherwise take it out since repeated below
886 AND mmt.costed_flag = 'N'
887 AND mmt.cogs_recognition_percent IS NULL
888 AND mmt.trx_source_line_id = ool.line_id -- this line and the next will cause this query to
889 AND ool.reference_line_id = crcml.cogs_om_line_id -- return rows only if crcml has a row for the orig SO
890 AND (
891 ( mmt.transaction_action_id = 27
892 AND EXISTS
893 (SELECT NULL
894 FROM mtl_secondary_inventories msi
895 WHERE mmt.subinventory_code = msi.SECONDARY_INVENTORY_NAME
896 AND mmt.organization_id = msi.organization_id
897 AND msi.asset_inventory = 1)
898 -- Physical RMA should not have a logical RMA as child transaction
899 -- otherwise this drop ship return COGS is carried by the logical RMA
900 AND NOT EXISTS
901 (SELECT NULL
902 FROM mtl_material_transactions mmt_rma
903 WHERE mmt_rma.transaction_source_type_id = 12
904 AND mmt_rma.transaction_action_id = 26
905 AND mmt_rma.parent_transaction_id = mmt.transaction_id)
906 )
907 OR
908 ( mmt.transaction_action_id = 26)
909 )
910 AND item.inventory_item_id = mmt.inventory_item_id
911 AND item.organization_id = mmt.organization_id
912 AND item.inventory_asset_flag = 'Y'
913 --{BUG#7463298
914 AND mp.organization_id = mmt.organization_id
915 AND mp.organization_id = caiv.organization_id
916 AND nvl(mp.process_enabled_flag,'N') = 'N' /*BUG#9306124 - Ristricted this qry for Discrete orgs*/
917 AND DECODE(g_ledger_id,-1
918 ,caiv.ledger_id,g_ledger_id) = caiv.ledger_id
919 --}
920 AND crcml.cogs_om_line_id = cce.cogs_om_line_id
921 AND crcml.pac_cost_type_id is NULL --BUG 14656876
922 AND cce.event_date <= mmt.transaction_date
923 AND NOT EXISTS (SELECT NULL
924 FROM cst_cogs_events
925 WHERE DECODE(event_type,3,TRUNC(event_date),event_date) <= mmt.transaction_date
926 AND cogs_om_line_id = crcml.cogs_om_line_id
927 AND prior_event_id = cce.parent_event_id)
928 GROUP BY cce.parent_event_id , ool.reference_line_id,
929 mmt.transaction_id , cce.cogs_percentage,
930 mmt.trx_source_line_id, mmt.organization_id,
931 mmt.inventory_item_id , mmt.transaction_date,
932 mmt.primary_quantity
933
934 /* BUG#9306124 - Seperated query for Process orgs with this union (Start)*/
935 UNION ALL
936 SELECT /* LEADING(mmt) */
937 mmt.trx_source_line_id rma_om_line_id,
938 ool.reference_line_id cogs_om_line_id,
939 mmt.transaction_id mmt_txn_id,
940 mmt.transaction_date transaction_date,
941 (-1*mmt.primary_quantity) event_quantity,
942 cce.parent_event_id prior_event_id,
943 cce.cogs_percentage cogs_percentage,
944 sum(cce.event_quantity) prior_event_quantity
945 FROM mtl_material_transactions mmt,
946 oe_order_lines_all ool,
947 cst_revenue_cogs_match_lines crcml,
948 cst_cogs_events cce,
949 mtl_system_items_b item
950 ,cst_acct_info_v caiv --BUG#7463298
951 ,mtl_parameters mp --BUG#9306124
952 WHERE mmt.transaction_source_type_id = 12
953 AND mmt.transaction_action_id in (26,27) -- UT: see if this forces index use, otherwise take it out since repeated below
954 AND mmt.opm_costed_flag IS NOT NULL
955 AND mmt.cogs_recognition_percent IS NULL
956 AND mmt.trx_source_line_id = ool.line_id -- this line and the next will cause this query to
957 AND ool.reference_line_id = crcml.cogs_om_line_id -- return rows only if crcml has a row for the orig SO
958 AND (
959 ( mmt.transaction_action_id = 27
960 AND EXISTS
961 (SELECT NULL
962 FROM mtl_secondary_inventories msi
963 WHERE mmt.subinventory_code = msi.SECONDARY_INVENTORY_NAME
964 AND mmt.organization_id = msi.organization_id
965 AND msi.asset_inventory = 1)
966 -- Physical RMA should not have a logical RMA as child transaction
967 -- otherwise this drop ship return COGS is carried by the logical RMA
968 AND NOT EXISTS
969 (SELECT NULL
970 FROM mtl_material_transactions mmt_rma
971 WHERE mmt_rma.transaction_source_type_id = 12
972 AND mmt_rma.transaction_action_id = 26
973 AND mmt_rma.parent_transaction_id = mmt.transaction_id)
974 )
975 OR
976 ( mmt.transaction_action_id = 26)
977 )
978 AND item.inventory_item_id = mmt.inventory_item_id
979 AND item.organization_id = mmt.organization_id
980 AND item.inventory_asset_flag = 'Y'
981 --{BUG#7463298
982 AND mp.organization_id = mmt.organization_id
983 AND mp.organization_id = caiv.organization_id
987 --}
984 AND nvl(mp.process_enabled_flag,'N') = 'Y' /*BUG#9306124 - Process org */
985 AND DECODE(g_ledger_id,-1
986 ,caiv.ledger_id,g_ledger_id) = caiv.ledger_id
988 AND crcml.cogs_om_line_id = cce.cogs_om_line_id
989 AND cce.event_date <= mmt.transaction_date
990 AND NOT EXISTS (SELECT NULL
991 FROM cst_cogs_events
992 WHERE DECODE(event_type,3,TRUNC(event_date),event_date) <= mmt.transaction_date
993 AND cogs_om_line_id = crcml.cogs_om_line_id
994 AND prior_event_id = cce.parent_event_id)
995 GROUP BY cce.parent_event_id , ool.reference_line_id,
996 mmt.transaction_id , cce.cogs_percentage,
997 mmt.trx_source_line_id, mmt.organization_id,
998 mmt.inventory_item_id , mmt.transaction_date,
999 mmt.primary_quantity
1000 /* BUG#9306124 - Seperated query for Process orgs with this union (End)*/
1001 ORDER BY cogs_om_line_id, transaction_date;
1002
1003 l_rma_om_line_id_tbl number_table;
1004 l_cogs_om_line_id_tbl number_table;
1005 l_mmt_txn_id_tbl number_table;
1006 l_txn_date_tbl date_table;
1007 l_event_quantity_tbl number_table;
1008 l_prior_event_id_tbl number_table;
1009 l_prior_percent_tbl number_table;
1010 l_prior_event_qty_tbl number_table;
1011
1012 l_parent_event_id_tbl number_table;
1013 l_prior_event_id NUMBER;
1014 l_marker number_table;
1015
1016 l_rma_count pls_integer := 0;
1017 l_last_fetch BOOLEAN;
1018
1019 -- Inventory's API to insert MMT events returns these 3 parameters
1020 l_return_num NUMBER;
1021 l_error_code VARCHAR2(240);
1022 l_error_message VARCHAR2(2000);
1023 program_exception EXCEPTION;
1024
1025 -- The following stores the source code from an OE system parameter.
1026 l_source_code VARCHAR2(40);
1027
1028 BEGIN
1029 debug('Insert_RmaReceipts+');
1030 debug(' g_ledger_id :'||g_ledger_id);
1031
1032 -- Standard start of API savepoint
1033 SAVEPOINT Insert_RmaReceipts_PVT;
1034
1035 l_stmt_num := 0;
1036
1037 -- Initialize return values
1038 x_return_status := FND_API.G_RET_STS_SUCCESS;
1039
1040 -- API Body
1041
1042 l_last_fetch := FALSE; -- initialize boolean variable
1043
1044 OPEN c_rma_receipts;
1045
1046 <<all_rma_receipts_loop>>
1047 LOOP
1048 l_stmt_num := 10;
1049
1050 debug(l_stmt_num);
1051
1052 FETCH c_rma_receipts BULK COLLECT INTO
1053 l_rma_om_line_id_tbl ,
1054 l_cogs_om_line_id_tbl,
1055 l_mmt_txn_id_tbl ,
1056 l_txn_date_tbl ,
1057 l_event_quantity_tbl ,
1058 l_prior_event_id_tbl ,
1059 l_prior_percent_tbl ,
1060 l_prior_event_qty_tbl
1061 LIMIT C_max_bulk_fetch_size;
1062
1063 IF c_rma_receipts%NOTFOUND THEN
1064 l_last_fetch := TRUE;
1065 END IF;
1066
1067 IF (l_cogs_om_line_id_tbl.COUNT = 0 AND l_last_fetch) THEN
1068 CLOSE c_rma_receipts;
1069 EXIT all_rma_receipts_loop;
1070 END IF;
1071
1072 /* For each RMA Receipt, insert 2 rows in cce - one goes in
1073 * the string of events and the other is the quantity adjustment
1074 * to that parent event.
1075 */
1076
1077 l_stmt_num := 20;
1078 debug(l_stmt_num);
1079
1080 FOR i IN l_cogs_om_line_id_tbl.FIRST..l_cogs_om_line_id_tbl.LAST LOOP
1081 l_marker(i) := 1;
1082 IF ( (i > l_cogs_om_line_id_tbl.FIRST) AND
1083 (l_prior_event_id_tbl(i) = l_prior_event_id_tbl(i-1)) ) THEN
1084 l_prior_event_id := l_parent_event_id_tbl(i-1);
1085 /* Bug# 9716020
1086 If prior event is RMA then current RMA Place holder should deduct
1087 quantity of RMA Receipt from its Place holder
1088 */
1089 l_prior_event_qty_tbl(i) := l_prior_event_qty_tbl(i-1) + l_event_quantity_tbl(i-1);
1090 l_marker(i-1) := 0;
1091 ELSE
1092 l_prior_event_id := l_prior_event_id_tbl(i);
1093 END IF;
1094
1095 -- Insert the RMA marker
1096 INSERT INTO cst_cogs_events (
1097 event_id,
1098 cogs_om_line_id,
1099 event_date,
1100 mmt_transaction_id,
1101 cogs_percentage,
1102 prior_cogs_percentage,
1103 prior_event_id,
1104 event_type,
1105 event_om_line_id,
1106 event_quantity,
1107 costed,
1108 parent_event_id,
1109 -- WHO COLUMNS
1110 last_update_date,
1111 last_updated_by,
1112 creation_date,
1113 created_by,
1114 last_update_login,
1115 request_id,
1116 program_application_id,
1117 program_id,
1118 program_update_date)
1119 VALUES ( cst_cogs_events_s.nextval,
1120 l_cogs_om_line_id_tbl(i),
1121 l_txn_date_tbl(i),
1122 NULL, -- Quantity placeholder - no MMT transaction
1123 l_prior_percent_tbl(i),
1124 l_prior_percent_tbl(i),
1125 l_prior_event_id,
1126 RMA_RECEIPT_PLACEHOLDER,
1127 l_rma_om_line_id_tbl(i),
1131 -- WHO COLUMNS
1128 l_prior_event_qty_tbl(i),
1129 NULL, -- This event is a quantity placeholder, thus is never costed
1130 cst_cogs_events_s.currval,
1132 sysdate,
1133 p_user_id,
1134 sysdate,
1135 p_user_id,
1136 p_login_id,
1137 p_request_id,
1138 p_pgm_app_id,
1139 p_pgm_id,
1140 sysdate)
1141 RETURNING event_id INTO l_parent_event_id_tbl(i);
1142 END LOOP;
1143
1144 l_stmt_num := 30;
1145 debug(l_stmt_num);
1146 FORALL i IN l_cogs_om_line_id_tbl.FIRST..l_cogs_om_line_id_tbl.LAST
1147 -- Insert the RMA receipts as quantity events (as opposed to % events)
1148 INSERT INTO cst_cogs_events (
1149 event_id,
1150 cogs_om_line_id,
1151 event_date,
1152 mmt_transaction_id,
1153 cogs_percentage,
1154 prior_cogs_percentage,
1155 prior_event_id,
1156 event_type,
1157 event_om_line_id,
1158 event_quantity,
1159 costed,
1160 parent_event_id,
1161 -- WHO COLUMNS
1162 last_update_date,
1163 last_updated_by,
1164 creation_date,
1165 created_by,
1166 last_update_login,
1167 request_id,
1168 program_application_id,
1169 program_id,
1170 program_update_date)
1171 VALUES ( cst_cogs_events_s.nextval,
1172 l_cogs_om_line_id_tbl(i),
1173 l_txn_date_tbl(i),
1174 l_mmt_txn_id_tbl(i),
1175 l_prior_percent_tbl(i), -- COGS percentage
1176 l_prior_percent_tbl(i), -- prior COGS percentage
1177 NULL,
1178 RMA_RECEIPT,
1179 l_rma_om_line_id_tbl(i),
1180 l_event_quantity_tbl(i),
1181 'N',
1182 l_parent_event_id_tbl(i),
1183 -- WHO COLUMNS
1184 sysdate,
1185 p_user_id,
1186 sysdate,
1187 p_user_id,
1188 p_login_id,
1189 p_request_id,
1190 p_pgm_app_id,
1191 p_pgm_id,
1192 sysdate);
1193
1194 l_rma_count := l_rma_count + l_cogs_om_line_id_tbl.COUNT;
1195
1196 -- Mark the cogs percentage column in the MMT transaction, thus indicating that it has been added
1197 -- to the revenue / COGS matching data model.
1198 l_stmt_num := 40;
1199 debug(l_stmt_num);
1200 FORALL i IN l_cogs_om_line_id_tbl.FIRST..l_cogs_om_line_id_tbl.LAST
1201 UPDATE mtl_material_transactions
1202 SET cogs_recognition_percent = l_prior_percent_tbl(i),
1203 last_update_date = sysdate,
1204 last_updated_by = p_user_id,
1205 last_update_login = p_login_id,
1206 request_id = p_request_id,
1207 program_application_id = p_pgm_app_id,
1208 program_id = p_pgm_id,
1209 program_update_date = sysdate
1210 WHERE transaction_id = l_mmt_txn_id_tbl(i);
1211
1212
1213 /* If there are events after this RMA (that is, if it's backdated)
1214 * adjust the quantity for each of these events by either updating
1215 * CCE / MMT directly if the parent is uncosted, or inserting new
1216 * events in CCE and MMT in the case that the parent is costed.
1217 */
1218
1219 -- First insert this new event into the linked list by setting the prior event ID of the
1220 -- next event to this new one.
1221 l_stmt_num := 50;
1222 debug(l_stmt_num);
1223 FORALL i IN l_cogs_om_line_id_tbl.FIRST..l_cogs_om_line_id_tbl.LAST
1224 UPDATE cst_cogs_events
1225 SET PRIOR_EVENT_ID = l_parent_event_id_tbl(i),
1226 last_update_date = sysdate,
1227 last_updated_by = p_user_id,
1228 last_update_login = p_login_id,
1229 request_id = p_request_id
1230 WHERE cogs_om_line_id = l_cogs_om_line_id_tbl(i)
1231 AND prior_event_id = l_prior_event_id_tbl(i)
1232 AND DECODE(event_type,3,TRUNC(event_date),event_date) > l_txn_date_tbl(i)
1233 AND l_marker(i) = 1;
1234
1235 -- Now create quantity adjustment events for all future COGS Rec events
1236 -- First populate the global temp table with all future events that require
1237 -- quantity adjustment children.
1238
1239 l_stmt_num := 60;
1240 debug(l_stmt_num);
1241 FORALL i IN l_cogs_om_line_id_tbl.FIRST..l_cogs_om_line_id_tbl.LAST
1242 INSERT INTO cst_cogs_qty_adj_events_temp (
1243 adj_event_id,
1244 adj_mmt_txn_id,
1245 adj_cogs_om_line_id,
1246 adj_rma_om_line_id,
1247 adj_event_date,
1248 adj_new_cogs_percentage,
1249 adj_prior_cogs_percentage,
1250 adj_event_quantity,
1251 parent_event_id,
1252 inventory_item_id,
1253 primary_uom,
1254 organization_id,
1255 cost_group_id,
1256 cogs_acct_id,
1257 opm_org_flag,
1258 acct_period_id
1259 )
1260 SELECT cst_cogs_events_s.nextval,
1264 cce.cogs_om_line_id,
1261 decode(event_type, COGS_RECOGNITION_EVENT, mtl_material_transactions_s.nextval,
1262 COGS_REC_PERCENT_ADJUSTMENT, mtl_material_transactions_s.nextval,
1263 NULL),
1265 l_rma_om_line_id_tbl(i),
1266 cce.event_date,
1267 cogs_percentage, -- could also use cce.prior_cogs_percentage
1268 prior_cogs_percentage,
1269 l_event_quantity_tbl(i),
1270 event_id,
1271 crcml.inventory_item_id,
1272 msi.primary_uom_code,
1273 crcml.organization_id,
1274 crcml.cost_group_id,
1275 crcml.cogs_acct_id,
1276 nvl(mp.process_enabled_flag,'N'),
1277 oap.acct_period_id -- acct period ID, I should store this in CCE so I don't keep having to go back to OAP
1278 FROM cst_cogs_events cce,
1279 cst_revenue_cogs_match_lines crcml,
1280 mtl_parameters mp,
1281 org_acct_periods oap,
1282 cst_acct_info_v caiv,
1283 mtl_system_items msi
1284 WHERE cce.cogs_om_line_id = l_cogs_om_line_id_tbl(i)
1285 AND TRUNC(cce.event_date) > l_txn_date_tbl(i)
1286 AND cce.event_type IN (3,4)
1287 AND cce.event_id = cce.parent_event_id
1288 AND cce.cogs_om_line_id = crcml.cogs_om_line_id
1289 AND crcml.pac_cost_type_id IS NULL
1290 AND crcml.organization_id = mp.organization_id
1291 AND crcml.organization_id = oap.organization_id
1292 AND crcml.inventory_item_id = msi.inventory_item_id
1293 AND crcml.organization_id = msi.organization_id
1294 AND crcml.organization_id = caiv.organization_id
1295 AND inv_le_timezone_pub.get_le_day_time_for_ou(cce.event_date, caiv.operating_unit)
1296 BETWEEN oap.period_start_date AND oap.schedule_close_date+EndDateNum;
1297
1298 l_stmt_num := 70;
1299 debug(l_stmt_num);
1300 -- Now insert the quantity adjustment child events
1301 INSERT INTO cst_cogs_events (
1302 event_id,
1303 cogs_om_line_id,
1304 event_date,
1305 mmt_transaction_id,
1306 cogs_percentage,
1307 prior_cogs_percentage,
1308 prior_event_id,
1309 event_type,
1310 event_om_line_id,
1311 event_quantity,
1312 costed,
1313 parent_event_id,
1314 -- WHO COLUMNS
1315 last_update_date,
1316 last_updated_by,
1317 creation_date,
1318 created_by,
1319 last_update_login,
1320 request_id,
1321 program_application_id,
1322 program_id,
1323 program_update_date)
1324 SELECT adj_event_id,
1325 adj_cogs_om_line_id,
1326 adj_event_date,
1327 adj_mmt_txn_id,
1328 adj_new_cogs_percentage,
1329 adj_prior_cogs_percentage,
1330 NULL,
1331 COGS_REC_QTY_ADJUSTMENT,
1332 adj_rma_om_line_id,
1333 adj_event_quantity,
1334 decode(adj_mmt_txn_id, NULL, NULL, 'N'),
1335 parent_event_id,
1336 -- WHO COLUMNS
1337 sysdate,
1338 p_user_id,
1339 sysdate,
1340 p_user_id,
1341 p_login_id,
1342 p_request_id,
1343 p_pgm_app_id,
1344 p_pgm_id,
1345 sysdate
1346 FROM cst_cogs_qty_adj_events_temp;
1347
1348 -- Get the source code from the OE profile system parameter.
1349 -- It can be overridden by the user but most likely uses the default
1350 -- called 'ORDER ENTRY' and will most likely never change.
1351 l_stmt_num := 80;
1352 debug(l_stmt_num);
1353
1354 l_source_code := FND_PROFILE.VALUE('ONT_SOURCE_CODE'); -- borrowed from OEXVSCHB.pls
1355 debug(' l_source_code : '||l_source_code);
1356
1357 l_stmt_num := 90;
1358 debug(l_stmt_num);
1359
1360 -- Insert their quantity adjustments into MMT
1361 INSERT INTO MTL_COGS_RECOGNITION_TEMP (
1362 TRANSACTION_ID,
1363 LAST_UPDATE_DATE,
1364 LAST_UPDATED_BY,
1365 CREATION_DATE,
1366 CREATED_BY,
1367 INVENTORY_ITEM_ID,
1368 ORGANIZATION_ID,
1369 COST_GROUP_ID,
1370 TRANSACTION_TYPE_ID,
1371 TRANSACTION_ACTION_ID,
1372 TRANSACTION_SOURCE_TYPE_ID,
1373 TRANSACTION_SOURCE_ID,
1374 TRANSACTION_SOURCE_NAME,
1375 TRANSACTION_QUANTITY,
1376 TRANSACTION_UOM,
1377 PRIMARY_QUANTITY,
1378 TRANSACTION_DATE,
1379 ACCT_PERIOD_ID,
1380 DISTRIBUTION_ACCOUNT_ID,
1381 COSTED_FLAG,
1382 OPM_COSTED_FLAG,
1383 ACTUAL_COST,
1384 TRANSACTION_COST,
1385 PRIOR_COST,
1386 NEW_COST,
1387 TRX_SOURCE_LINE_ID,
1388 RMA_LINE_ID,
1389 LOGICAL_TRANSACTION,
1390 COGS_RECOGNITION_PERCENT)
1391 SELECT
1392 ccqa.adj_mmt_txn_id,
1393 sysdate,
1394 p_user_id,
1395 sysdate,
1396 p_user_id,
1400 10008,
1397 ccqa.inventory_item_id,
1398 ccqa.organization_id,
1399 ccqa.cost_group_id,
1401 36,
1402 2,
1403 mso.sales_order_id,
1404 mso.segment1,
1405 ccqa.adj_event_quantity,
1406 ccqa.primary_uom, -- Txn UOM
1407 ccqa.adj_event_quantity,
1408 ccqa.adj_event_date,
1409 ccqa.acct_period_id,
1410 ccqa.cogs_acct_id,
1411 decode(ccqa.opm_org_flag, 'N', 'N', NULL),
1412 decode(ccqa.opm_org_flag, 'Y', 'N', NULL),
1413 NULL, -- Actual Cost
1414 NULL, -- Txn Cost
1415 NULL, -- Prior Cost
1416 NULL, -- New Cost
1417 ccqa.adj_cogs_om_line_id,
1418 ccqa.adj_rma_om_line_id, -- RMA Line ID
1419 1, -- Logical Txn
1420 ccqa.adj_new_cogs_percentage
1421 FROM cst_cogs_qty_adj_events_temp ccqa,
1422 mtl_sales_orders mso,
1423 oe_order_lines_all ool,
1424 oe_order_headers_all ooh,
1425 oe_transaction_types_tl ott
1426 WHERE ool.line_id = ccqa.adj_cogs_om_line_id
1427 AND ool.header_id = ooh.header_id
1428 AND TO_CHAR(ooh.order_number) = mso.segment1
1429 AND ooh.order_type_id = ott.transaction_type_id
1430 AND ott.name = mso.segment2
1431 AND ott.language = (SELECT language_code
1432 FROM fnd_languages
1433 WHERE installed_flag = 'B')
1434 AND mso.segment3 = l_source_code
1435 AND ccqa.adj_mmt_txn_id IS NOT NULL;
1436
1437 -- Now Call Inventory API to populate MMT from the above global temp table
1438 l_stmt_num := 100;
1439 debug(l_stmt_num);
1440
1441 INV_LOGICAL_TRANSACTIONS_PUB.create_cogs_recognition(x_return_status => l_return_num,
1442 x_error_code => l_error_code,
1443 x_error_message => l_error_message);
1444
1445 IF (l_return_num <> 0) THEN
1446 x_return_status := fnd_api.g_ret_sts_error;
1447 FND_MESSAGE.set_name('BOM', 'CST_FAILED_COGSREC_MMT_INSERT');
1448 FND_MESSAGE.set_token('ERROR_CODE', l_error_code);
1449 FND_MESSAGE.set_token('ERROR_MESSAGE',substr(l_error_message,1,500));
1450 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
1451 FND_MSG_PUB.ADD;
1452 END IF;
1453 IF l_errorLog THEN
1454 FND_LOG.message(FND_LOG.LEVEL_ERROR, l_module||'.'||to_char(l_stmt_num),TRUE);
1455 END IF;
1456 raise program_exception;
1457 END IF;
1458
1459 l_stmt_num := 90;
1460 debug(l_stmt_num);
1461 commit; --delete from cst_cogs_qty_adj_events_temp;
1462 SAVEPOINT Insert_RmaReceipts_PVT;
1463
1464 END LOOP all_rma_receipts_loop;
1465
1466 IF l_eventLog THEN
1467 l_api_message := 'Inserted '||to_char(l_rma_count)||' RMA Receipts into CCE.';
1468 FND_LOG.string(FND_LOG.LEVEL_EVENT, G_LOG_HEAD ||'.'|| l_api_name || '.'||l_stmt_num, l_api_message);
1469 END IF;
1470
1471 -- End API Body
1472
1473 IF l_proclog THEN
1474 fnd_log.string(FND_LOG.LEVEL_PROCEDURE,l_module||'.end',
1475 'Exiting '||G_PKG_NAME||'.'||l_api_name||' with '||
1476 'x_return_status = '||x_return_status
1477 );
1478 END IF;
1479 debug(' x_return_status = '||x_return_status);
1480 debug('Insert_RmaReceipts-');
1481 EXCEPTION
1482 WHEN program_exception THEN
1483 ROLLBACK TO Insert_RmaReceipts_PVT;
1484 x_return_status := fnd_api.g_ret_sts_error;
1485
1486 WHEN OTHERS THEN
1487 ROLLBACK TO Insert_RmaReceipts_PVT;
1488 x_return_status := fnd_api.g_ret_sts_unexp_error ;
1489
1490 IF l_unexpLog THEN
1491 FND_LOG.string(FND_LOG.LEVEL_UNEXPECTED,G_LOG_HEAD || '.'||l_api_name||l_stmt_num
1492 ,'Insert_RmaReceipts ('||to_char(l_stmt_num)||') : '||substr(SQLERRM,1,200));
1493 END IF;
1494
1495 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
1496 FND_MESSAGE.set_name('BOM', 'CST_UNEXP_ERROR');
1497 FND_MESSAGE.set_token('PACKAGE', G_PKG_NAME);
1498 FND_MESSAGE.set_token('PROCEDURE',l_api_name);
1499 FND_MESSAGE.set_token('STATEMENT',to_char(l_stmt_num));
1500 FND_MSG_PUB.ADD;
1501 END IF;
1502 FND_FILE.put_line(fnd_file.log, 'OTHERS EXCEPTION IN Insert_RmaReceipts:'||substrb(SQLERRM,1,250) );
1503
1504 END Insert_RmaReceipts;
1505
1506 -----------------------------------------------------------------------------
1507 -- Start of comments --
1508 -- --
1509 -- PROCEDURE --
1510 -- Create_CogsRecognitionEvents --
1511 -- This procedure is the main procedure for phase 3 of the program --
1512 -- to Match COGS to Revenue. It compares the latest Revenue % with --
1513 -- the latest COGS percentage and, where different, creates new --
1514 -- COGS recognition events to bring the COGS percentage up to date. --
1515 -- --
1516 -- VERSION 1.0 --
1517 -- --
1518 -- PARAMETERS --
1522 -- HISTORY: --
1519 -- X_RETURN_STATUS Success/Error/Unexplained error - 'S','E', or 'U' --
1520 -- WHO columns --
1521 -- --
1523 -- 04/28/05 Bryan Kuntz Created --
1524 -- End of comments --
1525 -----------------------------------------------------------------------------
1526
1527 PROCEDURE Create_CogsRecognitionEvents(
1528 x_return_status OUT NOCOPY VARCHAR2,
1529 p_request_id IN NUMBER,
1530 p_user_id IN NUMBER,
1531 p_login_id IN NUMBER,
1532 p_pgm_app_id IN NUMBER,
1533 p_pgm_id IN NUMBER,
1534 p_ledger_id IN NUMBER DEFAULT NULL --BUG#5726230
1535 ,p_neg_req_id IN NUMBER DEFAULT NULL --BUG#7387575
1536 ) IS
1537
1538 l_api_name CONSTANT VARCHAR2(30) := 'Create_CogsRecognitionEvents';
1539 l_api_message VARCHAR2(1000);
1540 l_return_status VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
1541 l_stmt_num NUMBER := 0;
1542
1543 l_module CONSTANT VARCHAR2(100) := G_LOG_HEAD ||'.'||l_api_name;
1544 l_unexpLog CONSTANT BOOLEAN := (FND_LOG.LEVEL_UNEXPECTED >= G_LOG_LEVEL) AND FND_LOG.TEST(FND_LOG.LEVEL_UNEXPECTED, l_module);
1545 l_errorLog CONSTANT BOOLEAN := l_unexpLog and (FND_LOG.LEVEL_ERROR >= G_LOG_LEVEL);
1546 l_eventLog CONSTANT BOOLEAN := l_errorLog and (FND_LOG.LEVEL_EVENT >= G_LOG_LEVEL);
1547 l_procLog CONSTANT BOOLEAN := l_eventLog and (FND_LOG.LEVEL_PROCEDURE >= G_LOG_LEVEL);
1548 l_stmtLog CONSTANT BOOLEAN := l_procLog and (FND_LOG.LEVEL_STATEMENT >= G_LOG_LEVEL);
1549
1550 --{BUG#5726230
1551 -- Add cursor for ledger access
1552 CURSOR cu_ledger(p_ledger_id IN NUMBER)
1553 IS
1554 SELECT DISTINCT set_of_books_id
1555 FROM gl_sets_of_books
1556 WHERE set_of_books_id = NVL(p_ledger_id,set_of_books_id);
1557
1558
1559 -- CRRL.last_event_date corresponds to the Revenue Recognition date that AR pass to CST.
1560 -- Since AR does not store time component, we can assume that the recognition takes place at the end of the day.
1561 CURSOR c_mismatched_lines(p_sob_id NUMBER,
1562 p_process_acct_period_num NUMBER,
1563 p_cogs_acct_period_num NUMBER,
1564 p_neg_req_id NUMBER
1565 ) IS
1566
1567 SELECT crcml.cogs_om_line_id cogs_om_line_id,
1568 crrl.last_event_date + EndDateNum + clt.number_1 last_event_date,
1569 crrl.revenue_recognition_percent new_percentage,
1570 cce.cogs_percentage prior_percentage,
1571 cce.parent_event_id prior_event_id,
1572 sum(cce.event_quantity) event_quantity,
1573 crcml.cogs_acct_id cogs_acct_id,
1574 crcml.inventory_item_id inventory_item_id,
1575 msi.primary_uom_code primary_uom_code,
1576 crcml.organization_id organization_id,
1577 crcml.cost_group_id cost_group_id,
1578 nvl(mp.process_enabled_flag,'N') opm_org,
1579 (select oap.acct_period_id
1580 from org_acct_periods oap
1581 where oap.organization_id = crcml.organization_id
1582 and oap.period_name = gps.period_name) acct_period_id,
1583 clt.number_1 date_offset
1584 --{BUG#6809034 use CCE event_date if mmt trx after rev recog
1585 ,MAX(mmt.transaction_id) mmt_transaction_id
1586 ,MAX(mmt.transaction_date) mmt_transaction_date
1587 ,MAX(mmt.acct_period_id) mmt_period_id
1588 --}
1589 ,MAX(crrl.rowid) l_rowid
1590 FROM cst_revenue_cogs_match_lines crcml,
1591 cst_revenue_recognition_lines crrl,
1592 cst_cogs_events cce,
1593 mtl_parameters mp,
1594 gl_period_statuses gps,
1595 cst_lists_temp clt,
1596 mtl_system_items msi
1597 --{BUG#6809034
1598 ,mtl_material_transactions mmt
1599 --}
1600 WHERE crrl.ledger_id = p_sob_id
1601 AND crrl.acct_period_num = p_process_acct_period_num
1602 AND crrl.potentially_unmatched_flag = 'Y' -- Indexed column should substantially reduce the rows
1603 --{BUG#7387575
1604 AND DECODE(p_neg_req_id,NULL,
1605 NVL(crrl.request_id,-99),p_neg_req_id) = NVL(crrl.request_id,-99)
1606 --}
1607 AND crrl.revenue_om_line_id = crcml.revenue_om_line_id
1608 AND crcml.organization_id = mp.organization_id
1609 AND crcml.organization_id = msi.organization_id
1610 AND crcml.inventory_item_id = msi.inventory_item_id
1611 AND crcml.pac_cost_type_id IS NULL
1612 AND gps.application_id = 101
1613 AND gps.set_of_books_id = p_sob_id
1614 AND gps.effective_period_num = p_cogs_acct_period_num
1615 AND cce.event_date <= gps.end_date +EndDateNum + clt.number_1 -- gps1.end_date +EndDateNum + clt.number_1
1616 AND clt.VARCHAR_1 = 'TIMEZONE'
1617 AND clt.list_id = crcml.organization_id
1618 AND crcml.cogs_om_line_id = cce.cogs_om_line_id
1619 --{BUG#6809034
1623 from cst_cogs_events
1620 AND cce.mmt_transaction_id = mmt.transaction_id(+)
1621 -- retrieve cce which are not prior to any events -- sum of these event quantity is the quantity to recognize the cost
1622 AND NOT EXISTS (select 'X'
1624 where event_date <= gps.end_date + EndDateNum + clt.number_1 -- gps1.end_date + EndDateNum + clt.number_1
1625 and cogs_om_line_id = crcml.cogs_om_line_id
1626 and prior_event_id = cce.parent_event_id)
1627 --AND cce.cogs_percentage <> crrl.revenue_recognition_percent -- AR should only send the revenue info once
1628 GROUP BY cce.cogs_percentage,
1629 cce.parent_event_id,
1630 crcml.cogs_om_line_id,
1631 crcml.cogs_acct_id,
1632 crcml.inventory_item_id,
1633 crcml.cost_group_id,
1634 msi.primary_uom_code,
1635 crrl.last_event_date,
1636 crrl.revenue_recognition_percent,
1637 clt.number_1,
1638 crcml.organization_id,
1639 mp.process_enabled_flag,
1640 gps.period_name;
1641
1642
1643
1644
1645 CURSOR cu_period(p_ledger_id IN NUMBER) IS
1646 SELECT DISTINCT crrl.acct_period_num
1647 FROM cst_revenue_recognition_lines crrl
1648 WHERE crrl.ledger_id = p_ledger_id
1649 AND crrl.potentially_unmatched_flag = 'Y'
1650 ORDER BY crrl.acct_period_num ASC;
1651
1652
1653 l_per_num_tab DBMS_SQL.NUMBER_TABLE;
1654
1655
1656
1657
1658
1659 --l_revenue_acct_period_num NUMBER := NULL;
1660 l_cogs_acct_period_num NUMBER := NULL;
1661 --l_rev_cogs_period_num NUMBER := NULL; replaced by l_max_period_num
1662 l_max_period_num NUMBER := NULL;
1663 l_min_period_num NUMBER := NULL;
1664
1665 l_gl_period_status VARCHAR2(1);
1666 l_alternate_event_date DATE;
1667 l_dummy_date DATE;
1668 l_period_name gl_period_statuses.period_name%type;
1669
1670 l_last_fetch BOOLEAN;
1671 l_cce_count PLS_INTEGER := 0;
1672
1673 l_new_event_id_tbl number_table;
1674 l_new_mmt_txn_id_tbl number_table;
1675 l_cogs_om_line_id_tbl number_table;
1676 l_event_date_tbl date_table;
1677 l_date_offset_tbl number_table;
1678 l_new_percentage_tbl number_table;
1679 l_prior_percentage_tbl number_table;
1680 l_prior_event_id_tbl number_table;
1681 l_event_quantity_tbl number_table;
1682 l_cogs_acct_id_tbl number_table;
1683 l_item_id_tbl number_table;
1684 l_primary_uom_tbl char3_table;
1685 l_organization_id_tbl number_table;
1686 l_cost_group_id_tbl number_table;
1687 l_opm_org_flg_tbl flag_table;
1688 l_acct_period_id_tbl number_table;
1689 l_parent_event_id_tbl number_table;
1690
1691 l_adj_mmt_txn_id_tbl number_table;
1692 l_adj_event_id_tbl number_table;
1693 l_adj_prior_event_id_tbl number_table;
1694
1695 l_upd_rowid_tab DBMS_SQL.VARCHAR2_TABLE;
1696
1697 --BUG5726230
1698 l_ledger_id_tab number_table;
1699
1700 -- Inventory's API to insert MMT events returns these 3 parameters
1701 l_return_num NUMBER;
1702 l_error_code VARCHAR2(240);
1703 l_error_message VARCHAR2(2000);
1704 program_exception EXCEPTION;
1705
1706 -- The following stores the source code from an OE system parameter.
1707 l_source_code VARCHAR2(40);
1708
1709 --BUG#6809034
1710 l_mmt_transaction_id number_table;
1711 l_mmt_transaction_date date_table;
1712 l_mmt_period_id number_table;
1713 l_msg_count NUMBER;
1714 --}
1715
1716 --{BUG#7438582
1717 l_gl_adj_flag VARCHAR2(1);
1718 --}
1719
1720 l_sob NUMBER;
1721 l_loop NUMBER := 0;
1722 end_of_program EXCEPTION; --BUG#5726230
1723 no_crrl EXCEPTION;
1724 BEGIN
1725 debug('Create_CogsRecognitionEvents+');
1726
1727 -- Standard start of API savepoint
1728 SAVEPOINT Create_CogsRecEvents_PVT;
1729
1730 l_stmt_num := 0;
1731
1732 debug(' p_ledger_id :'||p_ledger_id);
1733
1734 -- Initialize return values
1735 x_return_status := FND_API.G_RET_STS_SUCCESS;
1736
1737 -- Populate offset for each organization that might be affected
1738
1739 l_dummy_date := sysdate;
1740
1741 l_stmt_num := 1;
1742
1743 DELETE FROM cst_lists_temp WHERE VARCHAR_1 = 'TIMEZONE';
1744
1745 -- ER 10030862
1746 check_eligible_lines(p_ledger_id, x_return_status,l_error_message);
1747
1748 IF (x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
1749 FND_FILE.PUT_LINE(FND_FILE.LOG, l_error_message);
1750 RAISE program_exception;
1751 END IF;
1752
1753 --
1754 --BUG#5726230 Restrict to a single ledger
1755 --VARCHAR_1 = 'TIMEZONE'
1756 INSERT
1757 INTO cst_lists_temp (
1758 list_id,
1759 number_1,
1760 varchar_1
1761 )
1762 SELECT organization_id,
1763 inv_le_timezone_pub.get_server_day_time_for_le(
1767 'TIMEZONE'
1764 l_dummy_date,
1765 legal_entity
1766 ) - l_dummy_date,
1768 FROM ( SELECT
1769 DISTINCT caiv.organization_id,
1770 caiv.legal_entity
1771 FROM cst_revenue_recognition_lines crrl,
1772 cst_revenue_cogs_match_lines crcml,
1773 cst_acct_info_v caiv
1774 WHERE crrl.potentially_unmatched_flag='Y'
1775 AND crcml.revenue_om_line_id =crrl.revenue_om_line_id
1776 AND DECODE(p_neg_req_id,NULL,NVL(crrl.request_id,-99),p_neg_req_id) = NVL(crrl.request_id,-99)
1777 -- BUG#7387575
1778 AND crcml.pac_cost_type_id IS NULL
1779 AND caiv.organization_id =crcml.organization_id
1780 AND caiv.ledger_id =p_ledger_id
1781 );
1782
1783 l_sob := p_ledger_id;
1784
1785
1786 OPEN cu_period(l_sob);
1787 FETCH cu_period BULK COLLECT INTO l_per_num_tab;
1788 CLOSE cu_period;
1789
1790
1791 l_stmt_num := 5;
1792 debug(l_stmt_num);
1793 debug( 'l_per_num_tab.COUNT :'||l_per_num_tab.COUNT);
1794
1795 IF l_per_num_tab.COUNT = 0 THEN
1796 RAISE no_crrl;
1797 END IF;
1798
1799 -- <<acct_period_loop>> -- loop in chronological order for each mismatched period in this ledger
1800 FOR j IN l_per_num_tab.FIRST .. l_per_num_tab.LAST LOOP
1801
1802 l_stmt_num := 10;
1803 debug(l_stmt_num);
1804
1805 -- For each ledger, find the minimum accounting period where there exists mismatched
1806 -- Revenue and COGS.
1807 -- BUG#6809034
1808 -- In the case of some AR recognition on Sales Order Issue in JAN-XX and some in FEB-XX
1809 -- and all Sales Order Issue in FEB-XX
1810 -- in CRRL has a mix of accting period and last event in JAN-XX and FEB-XX
1811 -- in CCE event date and CRCML for all Sales Order Issue are in FEB-XX
1812 -- min(crrl.acct_period_num) verifying the condition will be in JAN-XX period
1813 -- but the l_revenue_acct_period_num will be in both FEB-XX and JAN-XX
1814 -- and Sales Order Issue COGS Recognition will be using JAN-XX period but
1815 -- transaction date matching to AR last event date in JAN-XX and FEB-XX
1816 -- and the CRRL acct period will be only FEB-XX
1817
1818 l_stmt_num := 20;
1819 debug(l_stmt_num);
1820 debug('Determining the best cogs accounting period providing the ar period is '||l_per_num_tab(j));
1821 --
1822 -- Need to determine the cross GL period case and ship after revenue
1823 -- For example if ship in OCT
1824 -- Revenue in SEP
1825 -- the period to check is not SEP but OCT for COGS accounting
1826 -- though the driver would be revenue period is in SEP
1827 --
1828 SELECT MAX(glp.effective_period_num)
1829 ,MIN(glp.effective_period_num)
1830 INTO l_max_period_num --l_rev_cogs_period_num
1831 ,l_min_period_num
1832 FROM gl_period_statuses glp
1833 ,cst_revenue_recognition_lines crrl
1834 ,cst_revenue_cogs_match_lines crcml
1835 ,cst_cogs_events cce
1836 WHERE crrl.acct_period_num = l_per_num_tab(j)
1837 AND crrl.potentially_unmatched_flag = 'Y'
1838 AND crrl.revenue_om_line_id = crcml.revenue_om_line_id
1839 AND crrl.ledger_id = p_ledger_id
1840 AND crcml.cogs_om_line_id = cce.cogs_om_line_id
1841 AND cce.event_type = 1
1842 AND glp.application_id = 101
1843 AND glp.ledger_id = p_ledger_id
1844 AND glp.adjustment_period_flag = 'N'
1845 AND DECODE(SIGN(cce.event_date - crrl.last_event_date)
1846 , -1, crrl.last_event_date, TRUNC(cce.event_date))
1847 BETWEEN glp.start_date
1848 AND glp.end_date;
1849
1850 debug(l_stmt_num||'- COGS accounting period l_max_period_num period:'||l_max_period_num);
1851 debug(l_stmt_num||'- COGS accounting period l_min_period_num period:'||l_min_period_num);
1852
1853 l_stmt_num := 25;
1854 debug(l_stmt_num);
1855
1856 -- Check the GL period for cogs If it is closed, create these events in the next open period
1857 -- BUG#9809034
1858 -- Now suppose JAN-XX is still opened
1859 SELECT closing_status,
1860 period_name,
1861 adjustment_period_flag --BUG7438582
1862 INTO l_gl_period_status,
1863 l_period_name,
1864 l_gl_adj_flag
1865 FROM gl_period_statuses
1866 WHERE application_id = 101
1867 AND ledger_id = l_sob
1868 AND effective_period_num = l_max_period_num; --l_per_num_tab(j); the period to check is the cogs period
1869
1870 debug('AR period l_per_num_tab('||j||'):'||l_per_num_tab(j));
1871 debug('COGS l_max_period_num period :'||l_max_period_num);
1872 debug('l_period_name :'||l_period_name);
1873 debug('l_gl_period_status :'||l_gl_period_status);
1874 debug('l_gl_adj_flag :'||l_gl_adj_flag);
1875
1876 --
1877 -- If the GL period is closed, need to get the next open period
1878 -- BUG7438582 UTSTAR.COM
1879 -- Allow Cogs Reco in Futur not adjustment periods
1880 --
1881 IF ( l_gl_period_status <> 'O'
1882 AND (l_gl_period_status <> 'F' OR l_gl_adj_flag <> 'N')
1883 )
1884 THEN
1885
1886 l_stmt_num := 30;
1890 FROM gl_period_statuses
1887 debug(l_stmt_num);
1888 SELECT min(effective_period_num)
1889 INTO l_cogs_acct_period_num
1891 WHERE application_id = 101
1892 AND ledger_id = l_sob --l_sob.set_of_books_id
1893 AND effective_period_num > l_max_period_num --l_per_num_tab(j)
1894 AND (closing_status = 'O'
1895 --{BUG#7438582 UTSTAR - Allow Cogs Reco in Futur not adjustment periods
1896 OR (closing_status = 'F' AND adjustment_period_flag = 'N')
1897 );
1898 --}
1899 -- Use the start date of the next open period as the event date
1900 -- for all these new COGS Rec events.
1901 l_stmt_num := 40;
1902 debug(l_stmt_num||'- COGS accounting period l_cogs_acct_period_num period:'||l_cogs_acct_period_num);
1903 BEGIN
1904 l_stmt_num := 40;
1905 debug(l_stmt_num);
1906 SELECT start_date
1907 INTO l_alternate_event_date
1908 FROM gl_period_statuses
1909 WHERE application_id = 101
1910 AND ledger_id = l_sob --l_sob.set_of_books_id
1911 AND effective_period_num = l_cogs_acct_period_num;
1912
1913 l_max_period_num := l_cogs_acct_period_num;
1914 debug(l_stmt_num||'- COGS accounting period l_max_period_num period:'||l_max_period_num);
1915
1916 EXCEPTION
1917 WHEN NO_DATA_FOUND THEN
1918 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_error) THEN
1919 FND_MESSAGE.set_name('BOM', 'CST_NO_OPEN_GL_PERIOD');
1920 FND_MESSAGE.set_token('LEDGER', l_sob); -- to_char(l_sob.set_of_books_id)
1921 FND_MESSAGE.set_token('PERIOD_NAME',l_period_name);
1922 FND_MSG_PUB.ADD;
1923 debug('EXCEPTION- CST_NO_OPEN_GL_PERIOD l_sob:'||l_sob||' - l_period_name:'||l_period_name);
1924 IF l_errorLog THEN
1925 FND_LOG.MESSAGE(FND_LOG.LEVEL_ERROR,l_module||'.'||l_stmt_num,TRUE);
1926 END IF;
1927 END IF;
1928 raise no_data_found;
1929 END;
1930
1931 ELSE
1932 -- The period is open, so COGS and Revenue will be matched in this period
1933 -- and we can use the AR date as the event date (instead of alternate event date).
1934 --BUG#6809034
1935 -- If JAN-XX is still opened this is portion all code executed
1936 --
1937 l_cogs_acct_period_num := l_max_period_num; -- l_per_num_tab(j);
1938 l_alternate_event_date := NULL;
1939
1940
1941 END IF;
1942 --
1943 -- The l_cogs_acct_period_num is not sufficient to track the COGS recognition period
1944 -- As during 1 single run of COGS recognition more than 1 COGS accounting periods can be involved
1945 -- Ex: If OM 1 Shipment is on 15-APR and Revenue recognized in 03-May
1946 -- OM 2 Shipment is on 20-May and Revenue recognized in 04-May
1947 -- OM 3 Shipment is on 10-Jun and Revenue recognized in 05-May
1948 -- Customer is matching COGS on 11-Jun for the Period May
1949 -- Period Apr is closed and May, Jun are opened
1950 -- OM 1 Cogs is to recognized on 03-May Period May
1951 -- 2 COgs is to recognized on 04-May Period May
1952 -- 3 Cogs is to recognized on 10-Jun Period Jun
1953 -- The problem will be expand if customer leave all the GL periods opened
1954 -- We will use l_cogs_acct_period_num for selection of the COGS to match ONLY
1955 --
1956 debug('l_cogs_acct_period_num:'||l_cogs_acct_period_num);
1957 debug('l_alternate_event_date:'||l_alternate_event_date);
1958
1959 l_last_fetch := FALSE; -- initialize boolean variable
1960
1961
1962 -- Before selecting the cogs line matching to revenue, the determination of the accounting date and period num is required
1963 prepare_acct_dates
1964 (p_ledger_id => p_ledger_id
1965 ,p_low_per_num => l_min_period_num
1966 ,p_hig_per_num => l_max_period_num
1967 ,x_out_status => l_return_status
1968 ,x_out_msg => l_api_message);
1969
1970 debug('prepare_acct_dates x_out_status : '||l_return_status);
1971 debug('prepare_acct_dates l_api_message: '||l_api_message);
1972 IF l_return_status <> 'S' THEN
1973 RAISE end_of_program;
1974 END IF;
1975
1976
1977 OPEN c_mismatched_lines(l_sob,
1978 l_per_num_tab(j),
1979 l_cogs_acct_period_num,
1980 p_neg_req_id --BUG#7387575
1981 );
1982 <<mismatched_lines_loop>>
1983 LOOP
1984 l_stmt_num := 50;
1985 debug(l_stmt_num);
1986
1987 -- Fetch 1000 mismatched COGS and Revenue records for processing
1988 FETCH c_mismatched_lines BULK COLLECT INTO
1989 l_cogs_om_line_id_tbl,
1990 l_event_date_tbl,
1991 l_new_percentage_tbl,
1992 l_prior_percentage_tbl,
1993 l_prior_event_id_tbl,
1994 l_event_quantity_tbl,
1995 l_cogs_acct_id_tbl,
1996 l_item_id_tbl,
1997 l_primary_uom_tbl,
1998 l_organization_id_tbl,
1999 l_cost_group_id_tbl,
2000 l_opm_org_flg_tbl,
2001 l_acct_period_id_tbl,
2002 l_date_offset_tbl ,
2003 --{BUG#6809034
2007 --}
2004 l_mmt_transaction_id,
2005 l_mmt_transaction_date,
2006 l_mmt_period_id,
2008 l_upd_rowid_tab
2009 LIMIT C_max_bulk_fetch_size;
2010
2011 debug('COGS loop ('||l_loop||') count :'||l_cogs_om_line_id_tbl.COUNT);
2012
2013 IF c_mismatched_lines%NOTFOUND THEN
2014 l_last_fetch := TRUE;
2015 END IF;
2016
2017 IF (l_cogs_om_line_id_tbl.COUNT = 0 AND l_last_fetch) THEN
2018 EXIT mismatched_lines_loop;
2019 END IF;
2020
2021 l_stmt_num := 60;
2022 debug(l_stmt_num);
2023 -- create COGS events in cst_cogs_events
2024 FORALL i IN l_cogs_om_line_id_tbl.FIRST..l_cogs_om_line_id_tbl.LAST
2025 INSERT INTO cst_cogs_events (
2026 event_id,
2027 cogs_om_line_id,
2028 event_date,
2029 mmt_transaction_id,
2030 cogs_percentage,
2031 prior_cogs_percentage,
2032 prior_event_id,
2033 event_type,
2034 event_om_line_id,
2035 event_quantity,
2036 costed,
2037 parent_event_id,
2038 -- WHO COLUMNS
2039 last_update_date,
2040 last_updated_by,
2041 creation_date,
2042 created_by,
2043 last_update_login,
2044 request_id,
2045 program_application_id,
2046 program_id,
2047 program_update_date)
2048 VALUES(
2049 cst_cogs_events_s.nextval,
2050 l_cogs_om_line_id_tbl(i),
2051 --BUG#6809034 -- As JAN-XX is opened the transaction_date is AR recognition event date
2052 --But as the bug is only on the material transaction, this COGS event event_date remains
2053 --untouch to avoid breaking the current logic based on the CCE,CRCML,CRRL for CCE creation
2054 --If the problem is hitting the accounting unproper such as to much COGS recognized or not enough
2055 --this can be the starting point
2056 --nvl(l_alternate_event_date+EndDateNum+l_date_offset_tbl(i),l_event_date_tbl(i)),
2057 CST_RevenueCogsMatch_PVT.acct_date(l_mmt_transaction_date(i),l_event_date_tbl(i)),
2058 --l_event_date_tbl(i),
2059 --
2060 mtl_material_transactions_s.nextval,
2061 l_new_percentage_tbl(i),
2062 l_prior_percentage_tbl(i),
2063 l_prior_event_id_tbl(i),
2064 COGS_RECOGNITION_EVENT,
2065 NULL, -- event OM line ID
2066 l_event_quantity_tbl(i),
2067 'N',
2068 cst_cogs_events_s.currval,
2069 -- WHO COLUMNS
2070 sysdate,
2071 p_user_id,
2072 sysdate,
2073 p_user_id,
2074 p_login_id,
2075 p_request_id,
2076 p_pgm_app_id,
2077 p_pgm_id,
2078 sysdate
2079 )
2080 RETURNING event_id, mmt_transaction_id
2081 BULK COLLECT INTO l_new_event_id_tbl, l_new_mmt_txn_id_tbl;
2082
2083 l_cce_count := l_cce_count + l_new_event_id_tbl.COUNT;
2084 debug(' l_cce_count : '||l_cce_count);
2085 l_stmt_num := 70;
2086 debug(l_stmt_num);
2087 -- Insert MMT transactions into the global temp table
2088 FORALL i IN l_new_mmt_txn_id_tbl.FIRST .. l_new_mmt_txn_id_tbl.LAST
2089 INSERT INTO mtl_cogs_recognition_temp (
2090 TRANSACTION_ID,
2091 LAST_UPDATE_DATE,
2092 LAST_UPDATED_BY,
2093 CREATION_DATE,
2094 CREATED_BY,
2095 INVENTORY_ITEM_ID,
2096 ORGANIZATION_ID,
2097 COST_GROUP_ID,
2098 TRANSACTION_TYPE_ID,
2099 TRANSACTION_ACTION_ID,
2100 TRANSACTION_SOURCE_TYPE_ID,
2101 TRANSACTION_QUANTITY,
2102 TRANSACTION_UOM,
2103 PRIMARY_QUANTITY,
2104 TRANSACTION_DATE,
2105 ACCT_PERIOD_ID,
2106 DISTRIBUTION_ACCOUNT_ID,
2107 COSTED_FLAG,
2108 OPM_COSTED_FLAG,
2109 ACTUAL_COST,
2110 TRANSACTION_COST,
2111 PRIOR_COST,
2112 NEW_COST,
2113 TRX_SOURCE_LINE_ID,
2114 RMA_LINE_ID,
2115 LOGICAL_TRANSACTION,
2116 COGS_RECOGNITION_PERCENT,
2117 transaction_set_id -- BUG#7387575
2118 )
2119 VALUES (
2120 l_new_mmt_txn_id_tbl(i),
2121 sysdate,
2122 p_user_id,
2123 sysdate,
2124 p_user_id,
2125 l_item_id_tbl(i),
2126 l_organization_id_tbl(i),
2127 l_cost_group_id_tbl(i),
2128 10008,
2129 36,
2130 2,
2134 --BUG#6809034
2131 l_event_quantity_tbl(i),
2132 l_primary_uom_tbl(i), -- Txn UOM
2133 l_event_quantity_tbl(i),
2135 --nvl(l_alternate_event_date+EndDateNum+l_date_offset_tbl(i),l_event_date_tbl(i)),
2136 CST_RevenueCogsMatch_PVT.acct_date(l_mmt_transaction_date(i),l_event_date_tbl(i)),
2137 -- This inventory accounting period will be reconciled in ensure_mmt_per_and_date
2138 -- We can use the routine inv_period_id to correct here but performance should be better in ensure_mmt_per_and_date
2139 l_acct_period_id_tbl(i),
2140 --CST_RevenueCogsMatch_PVT.inv_period_id(l_mmt_transaction_date(i),l_event_date_tbl(i),p_ledger_id,l_organization_id_tbl(i),l_acct_period_id_tbl(i)),
2141 --}
2142 l_cogs_acct_id_tbl(i),
2143 decode(l_opm_org_flg_tbl(i), 'N', 'N', NULL),
2144 decode(l_opm_org_flg_tbl(i), 'Y', 'N', NULL),
2145 NULL, -- Actual Cost
2146 NULL, -- Txn Cost
2147 NULL, -- Prior Cost
2148 NULL, -- New Cost
2149 l_cogs_om_line_id_tbl(i),
2150 NULL, -- RMA Line ID
2151 1, -- Logical Txn
2152 l_new_percentage_tbl(i)
2153 ,p_neg_req_id --BUG#7387575
2154 );
2155
2156 -- Don't call Inventory's API to create MMT transactions from these temporary events
2157 -- until after the next insertion into the global temp table (below).
2158
2159 -- Adjustment Processing
2160 -- First adjust all future events by simply updating those directly that have not been costed yet
2161 l_stmt_num := 80;
2162 debug(l_stmt_num);
2163 FORALL i IN l_cogs_om_line_id_tbl.FIRST..l_cogs_om_line_id_tbl.LAST
2164 UPDATE cst_cogs_events
2165 SET PRIOR_COGS_PERCENTAGE = l_new_percentage_tbl(i),
2166 PRIOR_EVENT_ID = l_new_event_id_tbl(i),
2167 last_update_date = sysdate,
2168 last_updated_by = p_user_id,
2169 last_update_login = p_login_id,
2170 request_id = p_request_id
2171 WHERE cogs_om_line_id = l_cogs_om_line_id_tbl(i)
2172 AND prior_event_id = l_prior_event_id_tbl(i)
2173 AND event_id <> l_new_event_id_tbl(i)
2174 AND l_opm_org_flg_tbl(i) = 'N'
2175 AND costed = 'N';
2176
2177 -- Otherwise, for future events that are not directly updatable, insert adjustment events
2178 -- between the newly created events (above) and the future events.
2179 -- It is necessary to first insert these events into a global temp table because a placeholder
2180 -- is required for the update statement below (stmt 170).
2181 -- This portion is for Revenue Recognition adjustment activity for the bug#6809034 remain untouched
2182 l_stmt_num := 90;
2183 debug(l_stmt_num);
2184 FORALL i IN l_cogs_om_line_id_tbl.FIRST..l_cogs_om_line_id_tbl.LAST
2185 INSERT INTO cst_cogs_pct_adj_events_temp (
2186 adj_event_id,
2187 adj_mmt_txn_id,
2188 adj_cogs_om_line_id,
2189 adj_event_date,
2190 adj_new_cogs_percentage,
2191 adj_prior_cogs_percentage,
2192 adj_prior_event_id,
2193 adj_event_quantity,
2194 ftr_event_id,
2195 inventory_item_id,
2196 primary_uom,
2197 organization_id,
2198 cost_group_id,
2199 cogs_acct_id,
2200 opm_org_flag,
2201 acct_period_id
2202 )
2203 SELECT cst_cogs_events_s.nextval,
2204 mtl_material_transactions_s.nextval,
2205 cogs_om_line_id,
2206 event_date,
2207 l_prior_percentage_tbl(i), -- could also use cce.prior_cogs_percentage
2208 l_new_percentage_tbl(i),
2209 l_new_event_id_tbl(i),
2210 event_quantity,
2211 event_id,
2212 l_item_id_tbl(i),
2213 l_primary_uom_tbl(i),
2214 l_organization_id_tbl(i),
2215 l_cost_group_id_tbl(i),
2216 l_cogs_acct_id_tbl(i),
2217 l_opm_org_flg_tbl(i),
2218 oap.acct_period_id
2219 FROM cst_cogs_events cce,
2220 org_acct_periods oap
2221 WHERE cogs_om_line_id = l_cogs_om_line_id_tbl(i)
2222 AND prior_event_id = l_prior_event_id_tbl(i)
2223 AND event_id <> l_new_event_id_tbl(i)
2224 AND cce.event_date
2225 BETWEEN oap.period_start_date + l_date_offset_tbl(i)
2226 AND oap.schedule_close_date + EndDateNum + l_date_offset_tbl(i)
2227 AND oap.organization_id = l_organization_id_tbl(i);
2228
2229 -- Now load the events into CCE from the temp table
2230 l_stmt_num := 100;
2231 debug(l_stmt_num);
2235 event_date,
2232 INSERT INTO cst_cogs_events (
2233 event_id,
2234 cogs_om_line_id,
2236 mmt_transaction_id,
2237 cogs_percentage,
2238 prior_cogs_percentage,
2239 prior_event_id,
2240 event_type,
2241 event_om_line_id,
2242 event_quantity,
2243 costed,
2244 parent_event_id,
2245 -- WHO COLUMNS
2246 last_update_date,
2247 last_updated_by,
2248 creation_date,
2249 created_by,
2250 last_update_login,
2251 request_id,
2252 program_application_id,
2253 program_id,
2254 program_update_date)
2255 SELECT adj_event_id,
2256 adj_cogs_om_line_id,
2257 adj_event_date,
2258 adj_mmt_txn_id,
2259 adj_new_cogs_percentage,
2260 adj_prior_cogs_percentage,
2261 adj_prior_event_id,
2262 COGS_REC_PERCENT_ADJUSTMENT,
2263 NULL,
2264 adj_event_quantity,
2265 'N',
2266 adj_event_id,
2267 -- WHO COLUMNS
2268 sysdate,
2269 p_user_id,
2270 sysdate,
2271 p_user_id,
2272 p_login_id,
2273 p_request_id,
2274 p_pgm_app_id,
2275 p_pgm_id,
2276 sysdate
2277 FROM cst_cogs_pct_adj_events_temp;
2278
2279 l_cce_count := l_cce_count + SQL%ROWCOUNT;
2280 debug(' l_cce_count : '|| l_cce_count);
2281 l_stmt_num := 110;
2282 debug(l_stmt_num);
2283 -- Now create these events in Inventory's table
2284 -- Use the Inventory Temp Table for new MMT txns
2285 INSERT INTO MTL_COGS_RECOGNITION_TEMP (
2286 TRANSACTION_ID,
2287 LAST_UPDATE_DATE,
2288 LAST_UPDATED_BY,
2289 CREATION_DATE,
2290 CREATED_BY,
2291 INVENTORY_ITEM_ID,
2292 ORGANIZATION_ID,
2293 COST_GROUP_ID,
2294 TRANSACTION_TYPE_ID,
2295 TRANSACTION_ACTION_ID,
2296 TRANSACTION_SOURCE_TYPE_ID,
2297 TRANSACTION_QUANTITY,
2298 TRANSACTION_UOM,
2299 PRIMARY_QUANTITY,
2300 TRANSACTION_DATE,
2301 ACCT_PERIOD_ID,
2302 DISTRIBUTION_ACCOUNT_ID,
2303 COSTED_FLAG,
2304 OPM_COSTED_FLAG,
2305 ACTUAL_COST,
2306 TRANSACTION_COST,
2307 PRIOR_COST,
2308 NEW_COST,
2309 TRX_SOURCE_LINE_ID,
2310 LOGICAL_TRANSACTION,
2311 COGS_RECOGNITION_PERCENT
2312 ,transaction_set_id --BUG#7387575
2313 )
2314 SELECT
2315 adj_mmt_txn_id,
2316 sysdate,
2317 p_user_id,
2318 sysdate,
2319 p_user_id,
2320 inventory_item_id,
2321 organization_id,
2322 cost_group_id,
2323 10008,
2324 36,
2325 2,
2326 adj_event_quantity,
2327 primary_uom, -- Txn UOM
2328 adj_event_quantity,
2329 adj_event_date,
2330 acct_period_id,
2331 cogs_acct_id,
2332 decode(opm_org_flag, 'N', 'N', NULL),
2333 decode(opm_org_flag, 'Y', 'N', NULL),
2334 NULL, -- Actual Cost
2335 NULL, -- Txn Cost
2336 NULL, -- Prior Cost
2337 NULL, -- New Cost
2338 adj_cogs_om_line_id,
2339 1, -- Logical Txn
2340 adj_new_cogs_percentage
2341 ,p_neg_req_id --BUG#7387575
2342 FROM cst_cogs_pct_adj_events_temp;
2343
2344 -- Next update MTL_COGS_RECOGNITION_TEMP to populate transaction_source_id with the
2345 -- sales_order_id from mtl_sales_orders.
2346 -- Get the source code from the OE profile system parameter.
2347 -- It can be overridden by the user but most likely uses the default
2348 -- called 'ORDER ENTRY' and will most likely never change.
2349 l_stmt_num := 145;
2350 debug(l_stmt_num);
2351
2352 l_source_code := FND_PROFILE.VALUE('ONT_SOURCE_CODE'); -- borrowed from OEXVSCHB.pls
2353 debug(' l_source_code : '||l_source_code);
2354
2355 l_stmt_num := 150;
2356 debug(l_stmt_num);
2357
2358 UPDATE mtl_cogs_recognition_temp mcr
2359 SET (transaction_source_id, transaction_source_name) = (
2360 SELECT mkts.sales_order_id, mkts.segment1
2361 FROM mtl_sales_orders mkts,
2362 oe_order_lines_all ool,
2366 AND ool.header_id = ooh.header_id
2363 oe_order_headers_all ooh,
2364 oe_transaction_types_tl ott
2365 WHERE ool.line_id = mcr.trx_source_line_id
2367 AND to_char(ooh.order_number) = mkts.segment1
2368 AND ooh.order_type_id = ott.transaction_type_id
2369 AND ott.name = mkts.segment2
2370 AND ott.language = (select language_code
2371 from fnd_languages
2372 where installed_flag = 'B')
2373 AND mkts.segment3 = l_source_code);
2374
2375
2376
2377 --{BUG#6809034
2378 --All the effort has been put in the MMT transaction date, we now ensure the synchronization between
2379 -- transaction_date and acct_period_id before the call to INV transactions
2380 l_stmt_num := 155;
2381 debug(l_stmt_num);
2382
2383 ensure_mmt_per_and_date(x_return_status => l_return_status,
2384 x_msg_count => l_msg_count,
2385 x_msg_data => l_error_message);
2386
2387 IF l_return_status <> fnd_api.g_ret_sts_success THEN
2388 debug(FND_LOG.LEVEL_ERROR, l_module||'.'||to_char(l_stmt_num));
2389 raise program_exception;
2390 END IF;
2391 --}
2392
2393
2394 -- Now insert into MMT by calling INV API
2395 l_stmt_num := 160;
2396 debug(l_stmt_num);
2397
2398 INV_LOGICAL_TRANSACTIONS_PUB.create_cogs_recognition(x_return_status => l_return_num,
2399 x_error_code => l_error_code,
2400 x_error_message => l_error_message);
2401
2402 IF (l_return_num <> 0) THEN
2403 x_return_status := fnd_api.g_ret_sts_error;
2404 FND_MESSAGE.set_name('BOM', 'CST_FAILED_COGSREC_MMT_INSERT');
2405 FND_MESSAGE.set_token('ERROR_CODE', l_error_code);
2406 FND_MESSAGE.set_token('ERROR_MESSAGE',substr(l_error_message,1,500));
2407 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
2408 FND_MSG_PUB.ADD;
2409 END IF;
2410 IF l_errorLog THEN
2411 FND_LOG.message(FND_LOG.LEVEL_ERROR, l_module||'.'||to_char(l_stmt_num),TRUE);
2412 END IF;
2413 raise program_exception;
2414 END IF;
2415
2416 -- Update the prior event ID in the future event in CCE using the temp table CCPAET
2417 l_stmt_num := 170;
2418 debug(l_stmt_num);
2419
2420 UPDATE (
2421 SELECT cce.prior_event_id,
2422 cba.adj_event_id
2423 FROM cst_cogs_events cce,
2424 cst_cogs_pct_adj_events_temp cba
2425 WHERE cce.event_id = cba.ftr_event_id)
2426 SET prior_event_id = adj_event_id;
2427
2428 l_stmt_num := 180;
2429 debug(l_stmt_num);
2430
2431
2432 FORALL i IN l_upd_rowid_tab.FIRST .. l_upd_rowid_tab.LAST
2433 UPDATE cst_revenue_recognition_lines
2434 SET potentially_unmatched_flag = NULL,
2435 last_update_date = SYSDATE,
2436 last_updated_by = p_user_id,
2437 last_update_login = p_login_id,
2438 request_id = p_request_id,
2439 program_application_id = p_pgm_app_id,
2440 program_id = p_pgm_id,
2441 program_update_date = SYSDATE
2442 WHERE ROWID = l_upd_rowid_tab(i);
2443
2444
2445 COMMIT; --also deletes from cst_cogs_pct_adj_events_temp;
2446 SAVEPOINT Create_CogsRecEvents_PVT;
2447
2448 END LOOP mismatched_lines_loop; -- c_mismatched_lines bulk fetches
2449
2450 CLOSE c_mismatched_lines;
2451 -- Abnormal situation
2452 -- The line at Y should be in NULL
2453 -- "I" means investigation
2454 UPDATE cst_revenue_recognition_lines
2455 SET potentially_unmatched_flag = 'I',
2456 last_update_date = SYSDATE,
2457 last_updated_by = p_user_id,
2458 last_update_login = p_login_id,
2459 request_id = p_request_id,
2460 program_application_id = p_pgm_app_id,
2461 program_id = p_pgm_id,
2462 program_update_date = SYSDATE
2463 WHERE ledger_id = l_sob
2464 AND acct_period_num = l_per_num_tab(j)
2465 AND potentially_unmatched_flag = 'Y';
2466
2467
2468
2469
2470 IF l_stmtLog THEN
2471 FND_LOG.string(FND_LOG.LEVEL_STATEMENT, l_module||'.100',
2472 'Ledger.PerNum.InsertedEvents = '||l_sob||'.'|| --to_char(l_sob.set_of_books_id)||'.'||
2473 l_cogs_acct_period_num||'.'||l_cce_count);
2474 END IF;
2475
2476 -- Now update the mismatched revenue lines in CRRL in case a future SOB/period iteration fails
2477 l_stmt_num := 190;
2478 debug(l_stmt_num);
2479
2480 commit; -- Need to save this update as well
2481 SAVEPOINT Create_CogsRecEvents_PVT;
2482
2483 END LOOP; -- acct_period_loop;
2484
2488 END IF;
2485 IF l_eventLog THEN
2486 l_api_message := 'Inserted '||to_char(l_cce_count)||' new COGS Recognition Events into CCE.';
2487 FND_LOG.string(FND_LOG.LEVEL_EVENT, G_LOG_HEAD ||'.'|| l_api_name || '.10', l_api_message);
2489
2490 -- End API Body
2491
2492 IF l_proclog THEN
2493 fnd_log.string(FND_LOG.LEVEL_PROCEDURE,l_module||'.end',
2494 'Exiting '||G_PKG_NAME||'.'||l_api_name||' with '||
2495 'x_return_status = '||x_return_status
2496 );
2497 END IF;
2498
2499 debug(' x_return_status : '||x_return_status);
2500 debug('Create_CogsRecognitionEvents-');
2501
2502 EXCEPTION
2503 WHEN end_of_program THEN
2504 ROLLBACK TO Create_CogsRecEvents_PVT;
2505 -- Let the program end normally
2506 x_return_status := FND_API.G_RET_STS_SUCCESS;
2507 debug('EXCEPTION end_of_program Create_CogsRecognitionEvents '||l_stmt_num||' : no ledger found - p_ledger_id :'||p_ledger_id);
2508
2509 WHEN no_crrl THEN
2510 x_return_status := FND_API.G_RET_STS_SUCCESS;
2511 debug('EXCEPTION no_crrl for COGS to match to Revenue Create_CogsRecognitionEvents '||l_stmt_num||' : no CRRL found - p_ledger_id :'||p_ledger_id);
2512
2513 WHEN program_exception THEN
2514 ROLLBACK TO Create_CogsRecEvents_PVT;
2515 x_return_status := fnd_api.g_ret_sts_error;
2516 FND_FILE.put_line(fnd_file.log, 'program_exception EXCEPTION IN Create_CogsRecognitionEvents');
2517
2518 WHEN OTHERS THEN
2519 ROLLBACK TO Create_CogsRecEvents_PVT;
2520 x_return_status := fnd_api.g_ret_sts_unexp_error ;
2521
2522 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
2523 FND_MESSAGE.set_name('BOM', 'CST_UNEXP_ERROR');
2524 FND_MESSAGE.set_token('PACKAGE', G_PKG_NAME);
2525 FND_MESSAGE.set_token('PROCEDURE',l_api_name);
2526 FND_MESSAGE.set_token('STATEMENT',to_char(l_stmt_num));
2527 FND_MSG_PUB.ADD;
2528 END IF;
2529
2530 IF l_unexpLog THEN
2531 FND_LOG.string(FND_LOG.LEVEL_UNEXPECTED,G_LOG_HEAD || '.'||l_api_name||'.'||l_stmt_num
2532 ,'Create_CogsRecognitionEvents ('||to_char(l_stmt_num)||') : '||substr(SQLERRM,1,200));
2533 END IF;
2534 FND_FILE.put_line(fnd_file.log, 'OTHERS EXCEPTION IN Create_CogsRecognitionEvents:'||substrb(SQLERRM,1,250) );
2535
2536 END Create_CogsRecognitionEvents;
2537
2538
2539 -----------------------------------------------------------------------------
2540 -- Start of comments --
2541 -- --
2542 -- PROCEDURE --
2543 -- Insert_OneSoIssue This procedure is very similar to the --
2544 -- Insert_SoIssues() procedure above. It differs in that the --
2545 -- above procedure handles bulk inserts and is called during --
2546 -- one of the phases of the concurrent request, while this --
2547 -- version inserts one sales order at a time into the data --
2548 -- model, and is called from the Cost Processor. --
2549 -- --
2550 -- This procedure should only get called for issues out of --
2551 -- asset subinventories. --
2552 -- --
2553 -- VERSION 1.0 --
2554 -- --
2555 -- PARAMETERS --
2556 -- P_COGS_OM_LINE_ID Line_ID of the sales order issue from OM table --
2557 -- P_COGS_ACCT_ID GL Code Combination for the COGS account --
2558 -- P_DEF_COGS_ACCT_ID GCC for the deferred COGS account --
2559 -- P_MMT_TXN_ID Transaction ID from MMT table --
2560 -- P_ORGANIZATION_ID Organization ID --
2561 -- P_ITEM_ID Inventory Item ID --
2562 -- P_TRANSACTION_DATE Event Date --
2563 -- P_COGS_GROUP_ID Cost Group ID --
2564 -- P_QUANTITY Sales Order Issue Quantity as a POSITIVE value --
2565 -- --
2566 -- HISTORY: --
2567 -- 05/13/05 Bryan Kuntz Created --
2568 -- End of comments --
2569 -----------------------------------------------------------------------------
2570
2571 PROCEDURE Insert_OneSoIssue(
2572 p_api_version IN NUMBER,
2573 p_init_msg_list IN VARCHAR2 := FND_API.G_FALSE,
2574 p_commit IN VARCHAR2 := FND_API.G_FALSE,
2575 p_validation_level IN NUMBER := FND_API.G_VALID_LEVEL_FULL,
2576 p_user_id IN NUMBER,
2577 p_login_id IN NUMBER,
2578 p_request_id IN NUMBER,
2579 p_pgm_app_id IN NUMBER,
2580 p_pgm_id IN NUMBER,
2581 x_return_status OUT NOCOPY VARCHAR2,
2582 p_cogs_om_line_id IN NUMBER,
2583 p_cogs_acct_id IN NUMBER,
2584 p_def_cogs_acct_id IN NUMBER,
2585 p_mmt_txn_id IN NUMBER,
2586 p_organization_id IN NUMBER,
2587 p_item_id IN NUMBER,
2588 p_transaction_date IN DATE,
2589 p_cost_group_id IN NUMBER,
2593 l_api_name CONSTANT VARCHAR2(30) := 'Insert_OneSoIssue';
2590 p_quantity IN NUMBER
2591 ) IS
2592
2594 l_api_message VARCHAR2(1000);
2595 l_api_version CONSTANT NUMBER := 1.0;
2596 l_return_status VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
2597 l_msg_count NUMBER := 0;
2598 l_msg_data VARCHAR2(8000) := '';
2599 l_stmt_num NUMBER := 0;
2600
2601 l_module CONSTANT VARCHAR2(100) := G_LOG_HEAD ||'.'||l_api_name||'.';
2602 l_unexpLog CONSTANT BOOLEAN := (FND_LOG.LEVEL_UNEXPECTED >= G_LOG_LEVEL) AND FND_LOG.TEST(FND_LOG.LEVEL_UNEXPECTED, l_module);
2603 l_errorLog CONSTANT BOOLEAN := l_unexpLog and (FND_LOG.LEVEL_ERROR >= G_LOG_LEVEL);
2604 l_eventLog CONSTANT BOOLEAN := l_errorLog and (FND_LOG.LEVEL_EVENT >= G_LOG_LEVEL);
2605 l_procLog CONSTANT BOOLEAN := l_eventLog and (FND_LOG.LEVEL_PROCEDURE >= G_LOG_LEVEL);
2606 l_stmtLog CONSTANT BOOLEAN := l_procLog and (FND_LOG.LEVEL_STATEMENT >= G_LOG_LEVEL);
2607
2608 l_revenue_om_line_id NUMBER;
2609 l_parent_cce_id NUMBER;
2610 l_operating_unit_id NUMBER;
2611 l_ledger_id NUMBER;
2612
2613 program_exception EXCEPTION;
2614
2615 BEGIN
2616 debug('Insert_OneSoIssue_PVT+');
2617 debug(' p_api_version : '||p_api_version);
2618 debug(' p_cogs_om_line_id : '||p_cogs_om_line_id);
2619 debug(' p_cogs_acct_id : '||p_cogs_acct_id);
2620 debug(' p_def_cogs_acct_id: '||p_def_cogs_acct_id);
2621 debug(' p_mmt_txn_id : '||p_mmt_txn_id);
2622 debug(' p_organization_id : '||p_organization_id);
2623 debug(' p_item_id : '||p_item_id);
2624 debug(' p_transaction_date: '||to_char(p_transaction_date,'MM-DD-YYYY'));
2625 debug(' p_cost_group_id : '||p_cost_group_id);
2626 debug(' p_quantity : '||p_quantity);
2627
2628
2629 -- Standard start of API savepoint
2630 SAVEPOINT Insert_OneSoIssue_PVT;
2631
2632 l_stmt_num := 0;
2633 debug(l_stmt_num);
2634
2635 -- Standard call to check for call compatibility
2636 IF NOT FND_API.Compatible_API_Call (
2637 l_api_version,
2638 p_api_version,
2639 l_api_name,
2640 G_PKG_NAME ) THEN
2641 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2642 END IF;
2643
2644 -- Initialize message list if p_init_msg_list is set to TRUE
2645 IF FND_API.to_Boolean(p_init_msg_list) THEN
2646 FND_MSG_PUB.initialize;
2647 END IF;
2648
2649 -- Initialize API return status to success
2650 x_return_status := FND_API.G_RET_STS_SUCCESS;
2651
2652 -- API Body
2653
2654 -- initialize
2655 l_revenue_om_line_id := NULL;
2656 l_parent_cce_id := NULL;
2657
2658 l_stmt_num := 10;
2659 debug(l_stmt_num);
2660 -- get the operating unit
2661 SELECT decode(fpg.multi_org_flag,'Y',TO_NUMBER(hoi.org_information3),TO_NUMBER(NULL)),
2662 TO_NUMBER(hoi.org_information1)
2663 INTO l_operating_unit_id,
2664 l_ledger_id
2665 FROM hr_organization_information hoi,
2666 fnd_product_groups fpg
2667 WHERE hoi.organization_id = p_organization_id
2668 AND hoi.org_information_context = 'Accounting Information';
2669
2670 l_stmt_num := 20;
2671 debug(l_stmt_num);
2672 -- Using the sales order line ID, find the invoicable line ID by calling OM's view/API
2673 OE_COGS_GRP.get_revenue_event_line(p_shippable_line_id => p_cogs_om_line_id,
2674 x_revenue_event_line_id => l_revenue_om_line_id,
2675 x_return_status => l_return_status,
2676 x_msg_count => l_msg_count,
2677 x_msg_data => l_msg_data);
2678
2679 IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
2680 FND_MESSAGE.set_name('BOM', 'CST_NO_REVENUE_OM_LINE');
2681 FND_MESSAGE.set_token('COGS_LINE_ID', to_char(p_cogs_om_line_id));
2682 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_error) THEN
2683 FND_MSG_PUB.ADD;
2684 END IF;
2685
2686 debug(' CST_NO_REVENUE_OM_LINE for p_cogs_om_line_id : '||p_cogs_om_line_id);
2687
2688 IF l_errorLog THEN
2689 FND_LOG.MESSAGE(FND_LOG.LEVEL_ERROR,G_LOG_HEAD||'.'||l_api_name||'.20',TRUE);
2690 END IF;
2691 raise program_exception;
2692 END IF;
2693
2694 -- Insert a new row into CRCML - if it already has this row, the unique index will be violated (DUP_VAL_ON_INDEX)
2695 BEGIN
2696 l_stmt_num := 30;
2697 debug(l_stmt_num);
2698 INSERT INTO cst_revenue_cogs_match_lines (
2699 cogs_om_line_id,
2700 revenue_om_line_id,
2701 cogs_acct_id,
2702 deferred_cogs_acct_id,
2703 sales_order_issue_date,
2704 organization_id,
2705 inventory_item_id,
2706 operating_unit_id,
2707 ledger_id,
2708 cost_group_id,
2709 -- WHO COLUMNS
2710 last_update_date,
2711 last_updated_by,
2712 creation_date,
2713 created_by,
2714 last_update_login,
2715 request_id,
2716 program_application_id,
2717 program_id,
2718 program_update_date)
2719 VALUES (
2720 p_cogs_om_line_id,
2721 l_revenue_om_line_id,
2725 p_organization_id,
2722 p_cogs_acct_id,
2723 p_def_cogs_acct_id,
2724 p_transaction_date,
2726 p_item_id,
2727 l_operating_unit_id,
2728 l_ledger_id,
2729 p_cost_group_id,
2730 -- WHO COLUMNS
2731 sysdate,
2732 p_user_id,
2733 sysdate,
2734 p_user_id,
2735 p_login_id,
2736 p_request_id,
2737 p_pgm_app_id,
2738 p_pgm_id,
2739 sysdate
2740 );
2741 EXCEPTION
2742 WHEN DUP_VAL_ON_INDEX THEN
2743 l_stmt_num := 40;
2744 -- The existing event is the parent; this new sales order issue
2745 -- becomes the child of this existing one.
2746 SELECT event_id
2747 INTO l_parent_cce_id
2748 FROM cst_cogs_events
2749 WHERE cogs_om_line_id = p_cogs_om_line_id
2750 AND parent_event_id = event_id
2751 AND event_type = 1;
2752 END;
2753
2754 l_stmt_num := 50;
2755 debug(l_stmt_num);
2756 -- Now update CRRL to mark any lines that may be unmatched due to this SO insert
2757 UPDATE cst_revenue_recognition_lines
2758 SET potentially_unmatched_flag = 'Y',
2759 last_update_date = sysdate,
2760 last_updated_by = p_user_id,
2761 last_update_login = p_login_id,
2762 request_id = p_request_id
2763 WHERE revenue_om_line_id = l_revenue_om_line_id;
2764
2765 l_stmt_num := 60;
2766 debug(l_stmt_num);
2767 -- Insert the sales order issue as the first event in the linked list of COGS
2768 -- events with a recognition % of 0.
2769 INSERT INTO cst_cogs_events (
2770 event_id,
2771 cogs_om_line_id,
2772 event_date,
2773 mmt_transaction_id,
2774 cogs_percentage,
2775 prior_cogs_percentage,
2776 event_type,
2777 event_om_line_id,
2778 event_quantity,
2779 costed,
2780 parent_event_id,
2781 -- WHO COLUMNS
2782 last_update_date,
2783 last_updated_by,
2784 creation_date,
2785 created_by,
2786 last_update_login,
2787 request_id,
2788 program_application_id,
2789 program_id,
2790 program_update_date)
2791 VALUES (
2792 cst_cogs_events_s.nextval,
2793 p_cogs_om_line_id,
2794 p_transaction_date,
2795 p_mmt_txn_id,
2796 0, -- COGS %
2797 0, -- prior COGS %
2798 SO_ISSUE,
2799 p_cogs_om_line_id,
2800 p_quantity,
2801 'N',
2802 nvl(l_parent_cce_id,cst_cogs_events_s.currval),
2803 -- WHO COLUMNS
2804 sysdate,
2805 p_user_id,
2806 sysdate,
2807 p_user_id,
2808 p_login_id,
2809 p_request_id,
2810 p_pgm_app_id,
2811 p_pgm_id,
2812 sysdate
2813 );
2814
2815 l_stmt_num := 70;
2816 debug(l_stmt_num);
2817 -- Mark the cogs percentage column in the MMT transaction, thus indicating that it has been added
2818 -- to the revenue / COGS matching data model.
2819 UPDATE mtl_material_transactions
2820 SET cogs_recognition_percent = 0,
2821 last_update_date = sysdate,
2822 last_updated_by = p_user_id,
2823 last_update_login = p_login_id,
2824 request_id = p_request_id,
2825 program_application_id = p_pgm_app_id,
2826 program_id = p_pgm_id,
2827 program_update_date = sysdate
2828 WHERE transaction_id = p_mmt_txn_id;
2829
2830 -- End API Body
2831
2832 IF l_proclog THEN
2833 fnd_log.string(FND_LOG.LEVEL_PROCEDURE,l_module||'.end',
2834 'Exiting '||G_PKG_NAME||'.'||l_api_name||' with '||
2835 'x_return_status = '||x_return_status
2836 );
2837 END IF;
2838 debug(' x_return_status : '||x_return_status);
2839 debug('Insert_OneSoIssue-');
2840
2841 EXCEPTION
2842 WHEN program_exception THEN
2843 ROLLBACK TO Insert_OneSoIssue_PVT; /*Bug 7384398*/
2844 x_return_status := fnd_api.g_ret_sts_error;
2845 WHEN OTHERS THEN
2846 ROLLBACK TO Insert_OneSoIssue_PVT;
2847 x_return_status := fnd_api.g_ret_sts_unexp_error ;
2848
2849 IF l_unexpLog THEN
2850 FND_LOG.string(FND_LOG.LEVEL_UNEXPECTED,G_LOG_HEAD || '.'||l_api_name||'.'||l_stmt_num
2851 ,'Insert_OneSoIssue ('||to_char(l_stmt_num)||') : '||substr(SQLERRM,1,200));
2852 END IF;
2853
2854 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
2855 FND_MESSAGE.set_name('BOM', 'CST_UNEXP_ERROR');
2856 FND_MESSAGE.set_token('PACKAGE', G_PKG_NAME);
2857 FND_MESSAGE.set_token('PROCEDURE',l_api_name);
2858 FND_MESSAGE.set_token('STATEMENT',to_char(l_stmt_num));
2859 FND_MSG_PUB.ADD;
2860 END IF;
2861 FND_FILE.put_line(fnd_file.log, 'OTHERS EXCEPTION IN Insert_OneSoIssue:'||substrb(SQLERRM,1,250) );
2862 END Insert_OneSoIssue;
2863
2864 -----------------------------------------------------------------------------
2865 -- Start of comments --
2866 -- --
2870 -- above procedure handles bulk inserts and is called during one --
2867 -- PROCEDURE --
2868 -- Insert_OneRmaReceipt This procedure is very similar to the --
2869 -- Insert_RmaReceipts() procedure above. It differs in that the --
2871 -- of the phases of the concurrent request, while this version --
2872 -- inserts one RMA receipt at a time into the data model, and is --
2873 -- called from the Cost Processor. --
2874 -- --
2875 -- VERSION 1.0 --
2876 -- --
2877 -- PARAMETERS --
2878 -- P_RMA_OM_LINE_ID Line_ID of the RMA from OM table --
2879 -- P_COGS_OM_LINE_ID Line_ID of the Original Sales Order Issue --
2880 -- referrred to by this RMA Receipt. --
2881 -- P_MMT_TXN_ID Transaction ID from MMT table --
2882 -- P_ORGANIZATION_ID Organization ID --
2883 -- P_ITEM_ID Inventory Item ID --
2884 -- P_TRANSACTION_DATE Event Date --
2885 -- P_QUANTITY Event Quantity --
2886 -- X_COGS_PERCENTAGE Returns the % at which this RMA will be applied --
2887 -- to COGS. --
2888 -- --
2889 -- HISTORY: --
2890 -- 05/13/05 Bryan Kuntz Created --
2891 -- End of comments --
2892 -----------------------------------------------------------------------------
2893
2894 PROCEDURE Insert_OneRmaReceipt(
2895 p_api_version IN NUMBER,
2896 p_init_msg_list IN VARCHAR2 := FND_API.G_FALSE,
2897 p_commit IN VARCHAR2 := FND_API.G_FALSE,
2898 p_validation_level IN NUMBER := FND_API.G_VALID_LEVEL_FULL,
2899 p_user_id IN NUMBER,
2900 p_login_id IN NUMBER,
2901 p_request_id IN NUMBER,
2902 p_pgm_app_id IN NUMBER,
2903 p_pgm_id IN NUMBER,
2904 x_return_status OUT NOCOPY VARCHAR2,
2905 x_msg_count OUT NOCOPY NUMBER,
2906 x_msg_data OUT NOCOPY VARCHAR2,
2907 p_rma_om_line_id IN NUMBER,
2908 p_cogs_om_line_id IN NUMBER,
2909 p_mmt_txn_id IN NUMBER,
2910 p_organization_id IN NUMBER,
2911 p_item_id IN NUMBER,
2912 p_transaction_date IN DATE,
2913 p_quantity IN NUMBER,
2914 x_event_id OUT NOCOPY NUMBER,
2915 x_cogs_percentage OUT NOCOPY NUMBER
2916 ) IS
2917
2918 l_api_name CONSTANT VARCHAR2(30) := 'Insert_OneRmaReceipt';
2919 l_api_message VARCHAR2(1000);
2920 l_api_version CONSTANT NUMBER := 1.0;
2921 l_stmt_num NUMBER := 0;
2922
2923 l_module CONSTANT VARCHAR2(100) := G_LOG_HEAD ||'.'||l_api_name;
2924 l_unexpLog CONSTANT BOOLEAN := (FND_LOG.LEVEL_UNEXPECTED >= G_LOG_LEVEL) AND FND_LOG.TEST(FND_LOG.LEVEL_UNEXPECTED, l_module);
2925 l_errorLog CONSTANT BOOLEAN := l_unexpLog and (FND_LOG.LEVEL_ERROR >= G_LOG_LEVEL);
2926 l_eventLog CONSTANT BOOLEAN := l_errorLog and (FND_LOG.LEVEL_EVENT >= G_LOG_LEVEL);
2927 l_procLog CONSTANT BOOLEAN := l_eventLog and (FND_LOG.LEVEL_PROCEDURE >= G_LOG_LEVEL);
2928 l_stmtLog CONSTANT BOOLEAN := l_procLog and (FND_LOG.LEVEL_STATEMENT >= G_LOG_LEVEL);
2929
2930 l_parent_event_id NUMBER;
2931 l_prior_event_id NUMBER;
2932 l_cogs_percentage NUMBER;
2933 l_prior_event_quantity NUMBER;
2934
2935 -- Inventory's API to insert MMT events returns these 3 parameters
2936 l_return_num NUMBER;
2937 l_error_code VARCHAR2(240);
2938 l_error_message VARCHAR2(2000);
2939 program_exception EXCEPTION;
2940
2941 -- The following stores the source code from an OE system parameter.
2942 l_source_code VARCHAR2(40);
2943
2944 BEGIN
2945 debug('Insert_OneRmaReceipt_PVT+');
2946 debug(' p_api_version : '||p_api_version);
2947 debug(' p_rma_om_line_id : '||p_rma_om_line_id);
2948 debug(' p_cogs_om_line_id : '||p_cogs_om_line_id);
2949 debug(' p_mmt_txn_id : '||p_mmt_txn_id);
2950 debug(' p_organization_id : '||p_organization_id);
2951 debug(' p_item_id : '||p_item_id);
2952 debug(' p_transaction_date : '||to_char(p_transaction_date,'MM-DD-YYYY'));
2953 debug(' p_quantity : '||p_quantity);
2954 -- Standard start of API savepoint
2955 SAVEPOINT Insert_OneRmaReceipt_PVT;
2956
2957 l_stmt_num := 0;
2958 debug(l_stmt_num);
2959
2960 -- Standard call to check for call compatibility
2961 IF NOT FND_API.Compatible_API_Call (
2962 l_api_version,
2963 p_api_version,
2964 l_api_name,
2965 G_PKG_NAME ) THEN
2966 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2967 END IF;
2968
2969 -- Initialize message list if p_init_msg_list is set to TRUE
2970 IF FND_API.to_Boolean(p_init_msg_list) THEN
2971 FND_MSG_PUB.initialize;
2972 END IF;
2973
2974 -- Initialize API return status to success
2978
2975 x_return_status := FND_API.G_RET_STS_SUCCESS;
2976 x_msg_count := 0;
2977 x_msg_data := '';
2979
2980 -- API Body
2981
2982 x_event_id := NULL;
2983
2984 l_stmt_num := 10;
2985 debug(l_stmt_num);
2986 -- Retrieve basic information about the original sales order issue
2987 -- and the latest COGS recognition percentage for that sales order
2988 SELECT cce.parent_event_id prior_event_id,
2989 cce.cogs_percentage,
2990 sum(cce.event_quantity) prior_event_quantity
2991 INTO l_prior_event_id,
2992 l_cogs_percentage,
2993 l_prior_event_quantity
2994 FROM cst_cogs_events cce
2995 WHERE p_cogs_om_line_id = cce.cogs_om_line_id
2996 AND cce.event_date <= p_transaction_date
2997 AND cce.parent_event_id NOT IN (SELECT prior_event_id
2998 FROM cst_cogs_events
2999 WHERE event_date <= p_transaction_date
3000 AND cogs_om_line_id = p_cogs_om_line_id--changed <= to = for bug 13243263
3001 AND prior_event_id IS NOT NULL)
3002 GROUP BY cce.parent_event_id,
3003 cce.cogs_percentage;
3004
3005 /* For an RMA Receipt, insert 2 rows in cce - one goes in
3006 * the string of events and the other is the quantity adjustment
3007 * to that parent event.
3008 */
3009
3010 l_stmt_num := 20;
3011 debug(l_stmt_num);
3012 INSERT INTO cst_cogs_events (
3013 event_id,
3014 cogs_om_line_id,
3015 event_date,
3016 mmt_transaction_id,
3017 cogs_percentage,
3018 prior_cogs_percentage,
3019 prior_event_id,
3020 event_type,
3021 event_om_line_id,
3022 event_quantity,
3023 costed,
3024 parent_event_id,
3025 -- WHO COLUMNS
3026 last_update_date,
3027 last_updated_by,
3028 creation_date,
3029 created_by,
3030 last_update_login,
3031 request_id,
3032 program_application_id,
3033 program_id,
3034 program_update_date)
3035 VALUES ( cst_cogs_events_s.nextval,
3036 p_cogs_om_line_id,
3037 p_transaction_date,
3038 NULL, -- Quantity Placeholder event - no corresponding MMT txn
3039 l_cogs_percentage,
3040 l_cogs_percentage,
3041 l_prior_event_id,
3042 RMA_RECEIPT_PLACEHOLDER,
3043 p_rma_om_line_id,
3044 l_prior_event_quantity,
3045 NULL, -- RMA quantity placeholders are not costed
3046 cst_cogs_events_s.currval,
3047 -- WHO COLUMNS
3048 sysdate,
3049 p_user_id,
3050 sysdate,
3051 p_user_id,
3052 p_login_id,
3053 p_request_id,
3054 p_pgm_app_id,
3055 p_pgm_id,
3056 sysdate)
3057 RETURNING event_id INTO l_parent_event_id;
3058
3059 l_stmt_num := 30;
3060 debug(l_stmt_num);
3061 -- Now insert the actual RMA event
3062 INSERT INTO cst_cogs_events (
3063 event_id,
3064 cogs_om_line_id,
3065 event_date,
3066 mmt_transaction_id,
3067 cogs_percentage,
3068 prior_cogs_percentage,
3069 prior_event_id,
3070 event_type,
3071 event_om_line_id,
3072 event_quantity,
3073 costed,
3074 parent_event_id,
3075 -- WHO COLUMNS
3076 last_update_date,
3077 last_updated_by,
3078 creation_date,
3079 created_by,
3080 last_update_login,
3081 request_id,
3082 program_application_id,
3083 program_id,
3084 program_update_date)
3085 VALUES ( cst_cogs_events_s.nextval,
3086 p_cogs_om_line_id,
3087 p_transaction_date,
3088 p_mmt_txn_id,
3089 l_cogs_percentage,
3090 l_cogs_percentage,
3091 NULL,
3092 RMA_RECEIPT,
3093 p_rma_om_line_id,
3094 p_quantity,
3095 'N',
3096 l_parent_event_id,
3097 -- WHO COLUMNS
3098 sysdate,
3099 p_user_id,
3100 sysdate,
3101 p_user_id,
3102 p_login_id,
3103 p_request_id,
3104 p_pgm_app_id,
3105 p_pgm_id,
3106 sysdate)
3107 RETURNING event_id INTO x_event_id;
3108
3109 -- Mark the cogs percentage column in the MMT transaction, thus indicating that it has been added
3110 -- to the revenue / COGS matching data model.
3111 l_stmt_num := 40;
3112 debug(l_stmt_num);
3113 UPDATE mtl_material_transactions
3114 SET cogs_recognition_percent = l_cogs_percentage,
3115 last_update_date = sysdate,
3116 last_updated_by = p_user_id,
3117 last_update_login = p_login_id,
3118 request_id = p_request_id
3119 WHERE transaction_id = p_mmt_txn_id;
3120
3121 /* If there are events after this RMA (that is, if it's backdated)
3122 * adjust the quantity for each of these events by either updating
3123 * CCE / MMT directly if the parent is uncosted, or inserting new
3124 * events in CCE and MMT in the case that the parent is costed.
3125 */
3126
3127 -- First insert this new event into the linked list by setting the prior event ID of the
3128 -- next event to this new one.
3129 l_stmt_num := 50;
3130 debug(l_stmt_num);
3134 last_updated_by = p_user_id,
3131 UPDATE cst_cogs_events
3132 SET PRIOR_EVENT_ID = l_parent_event_id,
3133 last_update_date = sysdate,
3135 last_update_login = p_login_id,
3136 request_id = p_request_id
3137 WHERE cogs_om_line_id = p_cogs_om_line_id
3138 AND prior_event_id = l_prior_event_id
3139 AND event_date > p_transaction_date;
3140
3141 -- Now create quantity adjustment events for all future COGS Rec events
3142 -- This is a 2 step process, first insert into the global temp table
3143 l_stmt_num := 60;
3144 debug(l_stmt_num);
3145 INSERT INTO cst_cogs_qty_adj_events_temp (
3146 adj_event_id,
3147 adj_mmt_txn_id,
3148 adj_cogs_om_line_id,
3149 adj_rma_om_line_id,
3150 adj_event_date,
3151 adj_new_cogs_percentage,
3152 adj_prior_cogs_percentage,
3153 adj_event_quantity,
3154 parent_event_id,
3155 inventory_item_id,
3156 primary_uom,
3157 organization_id,
3158 cost_group_id,
3159 cogs_acct_id,
3160 opm_org_flag,
3161 acct_period_id
3162 )
3163 SELECT cst_cogs_events_s.nextval,
3164 decode(event_type, COGS_RECOGNITION_EVENT, mtl_material_transactions_s.nextval,
3165 COGS_REC_PERCENT_ADJUSTMENT, mtl_material_transactions_s.nextval,
3166 NULL),
3167 cce.cogs_om_line_id,
3168 p_rma_om_line_id,
3169 cce.event_date,
3170 cogs_percentage, -- could also use cce.prior_cogs_percentage
3171 prior_cogs_percentage,
3172 p_quantity,
3173 cce.event_id,
3174 crcml.inventory_item_id,
3175 msi.primary_uom_code,
3176 crcml.organization_id,
3177 crcml.cost_group_id,
3178 crcml.cogs_acct_id,
3179 nvl(mp.process_enabled_flag,'N'),
3180 oap.acct_period_id
3181 FROM cst_cogs_events cce,
3182 cst_revenue_cogs_match_lines crcml,
3183 mtl_parameters mp,
3184 org_acct_periods oap,
3185 cst_acct_info_v caiv,
3186 mtl_system_items msi
3187 WHERE cce.cogs_om_line_id = p_cogs_om_line_id
3188 AND cce.event_date > p_transaction_date
3189 AND cce.event_id = cce.parent_event_id
3190 AND cce.cogs_om_line_id = crcml.cogs_om_line_id
3191 AND crcml.pac_cost_type_id IS NULL
3192 AND crcml.organization_id = mp.organization_id
3193 AND crcml.organization_id = msi.organization_id
3194 AND crcml.inventory_item_id = msi.inventory_item_id
3195 AND crcml.organization_id = oap.organization_id
3196 AND crcml.organization_id = caiv.organization_id
3197 AND inv_le_timezone_pub.get_le_day_time_for_ou(cce.event_date, caiv.operating_unit)
3198 BETWEEN oap.period_start_date AND oap.schedule_close_date+EndDateNum;
3199
3200 -- Then insert from the global temp table into CCE
3201 l_stmt_num := 70;
3202 debug(l_stmt_num);
3203 INSERT INTO cst_cogs_events (
3204 event_id,
3205 cogs_om_line_id,
3206 event_date,
3207 mmt_transaction_id,
3208 cogs_percentage,
3209 prior_cogs_percentage,
3210 prior_event_id,
3211 event_type,
3212 event_om_line_id,
3213 event_quantity,
3214 costed,
3215 parent_event_id,
3216 -- WHO COLUMNS
3217 last_update_date,
3218 last_updated_by,
3219 creation_date,
3220 created_by,
3221 last_update_login,
3222 request_id,
3223 program_application_id,
3224 program_id,
3225 program_update_date)
3226 SELECT adj_event_id,
3227 adj_cogs_om_line_id,
3228 adj_event_date,
3229 adj_mmt_txn_id,
3230 adj_new_cogs_percentage,
3231 adj_prior_cogs_percentage,
3232 NULL,
3233 COGS_REC_QTY_ADJUSTMENT,
3234 adj_rma_om_line_id,
3235 adj_event_quantity,
3236 decode(adj_mmt_txn_id, NULL, NULL, 'N'),
3237 parent_event_id,
3238 -- WHO COLUMNS
3239 sysdate,
3240 p_user_id,
3241 sysdate,
3242 p_user_id,
3243 p_login_id,
3244 p_request_id,
3245 p_pgm_app_id,
3246 p_pgm_id,
3247 sysdate
3248 FROM cst_cogs_qty_adj_events_temp;
3249
3250 -- Get the source code from the OE profile system parameter.
3251 -- It can be overridden by the user but most likely uses the default
3252 -- called 'ORDER ENTRY' and will most likely never change.
3253 l_stmt_num := 80;
3254 debug(l_stmt_num);
3255 l_source_code := FND_PROFILE.VALUE('ONT_SOURCE_CODE'); -- borrowed from OEXVSCHB.pls
3256
3257 debug(' l_source_code : '||l_source_code);
3258 -- Insert MMT event here (from one global temp table to another)
3259 l_stmt_num := 90;
3260 debug(l_stmt_num);
3261
3262 INSERT INTO mtl_cogs_recognition_temp (
3263 TRANSACTION_ID,
3264 LAST_UPDATE_DATE,
3265 LAST_UPDATED_BY,
3266 CREATION_DATE,
3267 CREATED_BY,
3268 INVENTORY_ITEM_ID,
3269 ORGANIZATION_ID,
3270 COST_GROUP_ID,
3271 TRANSACTION_TYPE_ID,
3272 TRANSACTION_ACTION_ID,
3276 TRANSACTION_QUANTITY,
3273 TRANSACTION_SOURCE_TYPE_ID,
3274 TRANSACTION_SOURCE_ID,
3275 TRANSACTION_SOURCE_NAME,
3277 TRANSACTION_UOM,
3278 PRIMARY_QUANTITY,
3279 TRANSACTION_DATE,
3280 ACCT_PERIOD_ID,
3281 DISTRIBUTION_ACCOUNT_ID,
3282 COSTED_FLAG,
3283 OPM_COSTED_FLAG,
3284 ACTUAL_COST,
3285 TRANSACTION_COST,
3286 PRIOR_COST,
3287 NEW_COST,
3288 TRX_SOURCE_LINE_ID,
3289 RMA_LINE_ID,
3290 LOGICAL_TRANSACTION,
3291 COGS_RECOGNITION_PERCENT)
3292 SELECT
3293 ccqa.adj_mmt_txn_id,
3294 sysdate,
3295 p_user_id,
3296 sysdate,
3297 p_user_id,
3298 ccqa.inventory_item_id,
3299 ccqa.organization_id,
3300 ccqa.cost_group_id,
3301 10008,
3302 36,
3303 2,
3304 mso.sales_order_id,
3305 mso.segment1,
3306 ccqa.adj_event_quantity,
3307 ccqa.primary_uom, -- Txn UOM
3308 ccqa.adj_event_quantity,
3309 ccqa.adj_event_date,
3310 ccqa.acct_period_id,
3311 ccqa.cogs_acct_id,
3312 decode(ccqa.opm_org_flag, 'N', 'N', NULL),
3313 decode(ccqa.opm_org_flag, 'Y', 'N', NULL),
3314 NULL, -- Actual Cost
3315 NULL, -- Txn Cost
3316 NULL, -- Prior Cost
3317 NULL, -- New Cost
3318 ccqa.adj_cogs_om_line_id,
3319 ccqa.adj_rma_om_line_id, -- RMA Line ID
3320 1, -- Logical Txn
3321 ccqa.adj_new_cogs_percentage
3322 FROM cst_cogs_qty_adj_events_temp ccqa,
3323 mtl_sales_orders mso,
3324 oe_order_lines_all ool,
3325 oe_order_headers_all ooh,
3326 oe_transaction_types_tl ott
3327 WHERE ool.line_id = ccqa.adj_cogs_om_line_id
3328 AND ool.header_id = ooh.header_id
3329 AND TO_CHAR(ooh.order_number) = mso.segment1
3330 AND ooh.order_type_id = ott.transaction_type_id
3331 AND ott.name = mso.segment2
3332 AND ott.language = (SELECT language_code
3333 FROM fnd_languages
3334 WHERE installed_flag = 'B')
3335 AND mso.segment3 = l_source_code
3336 --{BUG#6909721
3337 -- Ensure the place holder COGS event is not inserted in MMT as not effect on Cost and Accounting
3338 AND ccqa.adj_mmt_txn_id IS NOT NULL;
3339 --}
3340
3341 -- Now insert into MMT by calling INV API
3342 l_stmt_num := 100;
3343 debug(l_stmt_num);
3344
3345 INV_LOGICAL_TRANSACTIONS_PUB.create_cogs_recognition(x_return_status => l_return_num,
3346 x_error_code => l_error_code,
3347 x_error_message => l_error_message);
3348
3349 IF (l_return_num <> 0) THEN
3350 x_return_status := fnd_api.g_ret_sts_error;
3351 FND_MESSAGE.set_name('BOM', 'CST_FAILED_COGSREC_MMT_INSERT');
3352 FND_MESSAGE.set_token('ERROR_CODE', l_error_code);
3353 FND_MESSAGE.set_token('ERROR_MESSAGE',substr(l_error_message,1,500));
3354 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
3355 FND_MSG_PUB.ADD;
3356 END IF;
3357 IF l_errorLog THEN
3358 FND_LOG.message(FND_LOG.LEVEL_ERROR, l_module||'.'||to_char(l_stmt_num),TRUE);
3359 END IF;
3360 raise program_exception;
3361 END IF;
3362
3363 --{BUG#12532039
3364 DELETE FROM cst_cogs_qty_adj_events_temp;
3365 --}
3366 --{BUG#13712426
3367 DELETE FROM mtl_cogs_recognition_temp;
3368 --}
3369
3370 -- Set the value of the return paramter
3371 x_cogs_percentage := l_cogs_percentage;
3372
3373 -- End API Body
3374
3375 FND_MSG_PUB.count_and_get
3376 ( p_count => x_msg_count
3377 , p_data => x_msg_data
3378 );
3379
3380 IF l_proclog THEN
3381 fnd_log.string(FND_LOG.LEVEL_PROCEDURE,l_module||'.end',
3382 'Exiting '||G_PKG_NAME||'.'||l_api_name||' with '||
3383 'x_return_status = '||x_return_status||','||
3384 'x_event_id = '||x_event_id||','||
3385 'x_cogs_percentage = '||x_cogs_percentage
3386 );
3387 END IF;
3388 debug(' x_msg_count : '||x_msg_count);
3389 debug(' x_msg_data : '||x_msg_data);
3390 debug('Insert_OneRmaReceipt-');
3391 EXCEPTION
3392 WHEN program_exception THEN
3393 ROLLBACK TO Insert_OneRmaReceipt_PVT;
3394 x_return_status := fnd_api.g_ret_sts_error;
3395 WHEN OTHERS THEN
3396 ROLLBACK TO Insert_OneRmaReceipt_PVT;
3397 x_return_status := fnd_api.g_ret_sts_unexp_error ;
3398
3399 IF l_unexpLog THEN
3400 FND_LOG.string(FND_LOG.LEVEL_UNEXPECTED,G_LOG_HEAD || '.'||l_api_name||'.'||l_stmt_num
3401 ,'Insert_OneRmaReceipt '||to_char(l_stmt_num)||' : '||substr(SQLERRM,1,200));
3402 END IF;
3403
3404 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
3405 FND_MESSAGE.set_name('BOM', 'CST_UNEXP_ERROR');
3406 FND_MESSAGE.set_token('PACKAGE', G_PKG_NAME);
3410 END IF;
3407 FND_MESSAGE.set_token('PROCEDURE',l_api_name);
3408 FND_MESSAGE.set_token('STATEMENT',to_char(l_stmt_num));
3409 FND_MSG_PUB.ADD;
3411
3412 FND_MSG_PUB.count_and_get
3413 ( p_count => x_msg_count
3414 , p_data => x_msg_data
3415 );
3416 FND_FILE.put_line(fnd_file.log, 'OTHERS EXCEPTION IN Insert_OneRmaReceipt:'||substrb(SQLERRM,1,250) );
3417 END Insert_OneRmaReceipt;
3418
3419 -----------------------------------------------------------------------------
3420 -- Start of comments --
3421 -- --
3422 -- PROCEDURE --
3423 -- Record_SoIssueCost --
3424 -- This procedure is called by the distribution processors to --
3425 -- record the outgoing cost of the item at the time of the sales --
3426 -- order issue. The logic is standard across cost methods so --
3427 -- it can be called for all perpetual and PAC types. --
3428 -- --
3429 -- VERSION 1.0 --
3430 -- --
3431 -- PARAMETERS --
3432 -- P_COGS_OM_LINE_ID OM Line_ID of the Sales Order issue --
3433 -- P_PAC_COST_TYPE_ID Periodic Cost Type, Leave NULL for perpetual --
3434 -- P_UNIT_MATERIAL_COST
3435 -- P_UNIT_MOH_COST
3436 -- P_UNIT_RESOURCE_COST
3437 -- P_UNIT_OP_COST
3438 -- P_UNIT_OVERHEAD_COST
3439 -- P_UNIT_COST
3440 -- P_TXN_QUANTITY
3441 -- --
3442 -- HISTORY: --
3443 -- 05/16/05 Bryan Kuntz Created --
3444 -- End of comments --
3445 -----------------------------------------------------------------------------
3446
3447 PROCEDURE Record_SoIssueCost(
3448 p_api_version IN NUMBER,
3449 p_init_msg_list IN VARCHAR2 := FND_API.G_FALSE,
3450 p_user_id IN NUMBER,
3451 p_login_id IN NUMBER,
3452 p_request_id IN NUMBER,
3453 p_pgm_app_id IN NUMBER,
3454 p_pgm_id IN NUMBER,
3455 x_return_status OUT NOCOPY VARCHAR2,
3456 x_msg_count OUT NOCOPY NUMBER,
3457 x_msg_data OUT NOCOPY VARCHAR2,
3458 p_cogs_om_line_id IN NUMBER,
3459 p_pac_cost_type_id IN NUMBER,
3460 p_unit_material_cost IN NUMBER,
3461 p_unit_moh_cost IN NUMBER,
3462 p_unit_resource_cost IN NUMBER,
3463 p_unit_op_cost IN NUMBER,
3464 p_unit_overhead_cost IN NUMBER,
3465 p_unit_cost IN NUMBER,
3466 p_txn_quantity IN NUMBER
3467 ) IS
3468
3469 l_api_name CONSTANT VARCHAR2(30) := 'Record_SoIssueCost';
3470 l_api_message VARCHAR2(1000);
3471 l_stmt_num NUMBER := 0;
3472 l_api_version CONSTANT NUMBER := 1.0;
3473
3474 l_return_status VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
3475
3476 l_module CONSTANT VARCHAR2(100) := G_LOG_HEAD ||'.'||l_api_name;
3477 l_unexpLog CONSTANT BOOLEAN := (FND_LOG.LEVEL_UNEXPECTED >= G_LOG_LEVEL) AND FND_LOG.TEST(FND_LOG.LEVEL_UNEXPECTED, l_module);
3478 l_errorLog CONSTANT BOOLEAN := l_unexpLog and (FND_LOG.LEVEL_ERROR >= G_LOG_LEVEL);
3479 l_eventLog CONSTANT BOOLEAN := l_errorLog and (FND_LOG.LEVEL_EVENT >= G_LOG_LEVEL);
3480 l_procLog CONSTANT BOOLEAN := l_eventLog and (FND_LOG.LEVEL_PROCEDURE >= G_LOG_LEVEL);
3481 l_stmtLog CONSTANT BOOLEAN := l_procLog and (FND_LOG.LEVEL_STATEMENT >= G_LOG_LEVEL);
3482
3483 BEGIN
3484
3485 -- Standard start of API savepoint
3486 SAVEPOINT Record_SoIssueCost_PVT;
3487
3488 l_stmt_num := 0;
3489
3490 IF l_procLog THEN
3491 FND_LOG.string(FND_LOG.LEVEL_PROCEDURE,l_module||'.begin',
3492 'Entering '||G_PKG_NAME||'.'||l_api_name||' with '||
3493 'p_api_version = '||p_api_version||','||
3494 'p_cogs_om_line_id = '||p_cogs_om_line_id||','||
3495 'p_pac_cost_type_id = '||p_pac_cost_type_id||','||
3496 'p_unit_material_cost = '||p_unit_material_cost||','||
3497 'p_unit_moh_cost = '||p_unit_moh_cost||','||
3498 'p_unit_resource_cost = '||p_unit_resource_cost||','||
3499 'p_unit_op_cost = '||p_unit_op_cost||','||
3500 'p_unit_overhead_cost = '||p_unit_overhead_cost||','||
3501 'p_unit_cost = '||p_unit_cost||','||
3502 'p_txn_quantity = '||p_txn_quantity
3503 );
3504 END IF;
3505
3506 -- Standard call to check for call compatibility
3507 IF NOT FND_API.Compatible_API_Call (
3508 l_api_version,
3509 p_api_version,
3510 l_api_name,
3511 G_PKG_NAME ) THEN
3512 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3513 END IF;
3514
3515 -- Initialize message list if p_init_msg_list is set to TRUE
3516 IF FND_API.to_Boolean(p_init_msg_list) THEN
3517 FND_MSG_PUB.initialize;
3518 END IF;
3519
3520 -- Initialize API return status to success
3521 x_return_status := FND_API.G_RET_STS_SUCCESS;
3522 x_msg_count := 0;
3523 x_msg_data := '';
3524
3525 -- API Body
3526
3530 -- already has a value and, if so, perform an average of the new parameters
3527 l_stmt_num := 10;
3528 -- It's possible that two or more MMT transactions get created for the same sales
3529 -- order line, that's why it is necessary to first check whether the quantity
3531 -- and whatever is already in the table already for these columns.
3532 UPDATE cst_revenue_cogs_match_lines crcml
3533 SET unit_material_cost = decode(original_shipped_qty, NULL, p_unit_material_cost,
3534 ((unit_material_cost*original_shipped_qty) +
3535 (p_unit_material_cost*p_txn_quantity)) /
3536 (original_shipped_qty + p_txn_quantity)),
3537 unit_moh_cost = decode(original_shipped_qty, NULL, p_unit_moh_cost,
3538 ((unit_moh_cost*original_shipped_qty) +
3539 (p_unit_moh_cost*p_txn_quantity)) /
3540 (original_shipped_qty + p_txn_quantity)),
3541 unit_resource_cost = decode(original_shipped_qty, NULL, p_unit_resource_cost,
3542 ((unit_resource_cost*original_shipped_qty) +
3543 (p_unit_resource_cost*p_txn_quantity)) /
3544 (original_shipped_qty + p_txn_quantity)),
3545 unit_op_cost = decode(original_shipped_qty, NULL, p_unit_op_cost,
3546 ((unit_op_cost*original_shipped_qty) +
3547 (p_unit_op_cost*p_txn_quantity)) /
3548 (original_shipped_qty + p_txn_quantity)),
3549 unit_overhead_cost = decode(original_shipped_qty, NULL, p_unit_overhead_cost,
3550 ((unit_overhead_cost*original_shipped_qty) +
3551 (p_unit_overhead_cost*p_txn_quantity)) /
3552 (original_shipped_qty + p_txn_quantity)),
3553 original_shipped_qty = nvl(original_shipped_qty,0) + p_txn_quantity,
3554 unit_cost = decode(original_shipped_qty, NULL, p_unit_cost,
3555 ((unit_cost*original_shipped_qty) +
3556 (p_unit_cost*p_txn_quantity)) /
3557 (original_shipped_qty + p_txn_quantity)),
3558 last_update_date = sysdate,
3559 last_updated_by = p_user_id,
3560 last_update_login = p_login_id,
3561 request_id = p_request_id,
3562 program_application_id = p_pgm_app_id,
3563 program_id = p_pgm_id,
3564 program_update_date = sysdate
3565 WHERE crcml.cogs_om_line_id = p_cogs_om_line_id
3566 AND nvl(pac_cost_type_id,0) = nvl(p_pac_cost_type_id,0);
3567
3568 -- End API Body
3569
3570 FND_MSG_PUB.count_and_get
3571 ( p_count => x_msg_count
3572 , p_data => x_msg_data
3573 , p_encoded => FND_API.g_false
3574 );
3575
3576 x_return_status := l_return_status;
3577
3578 IF l_proclog THEN
3579 fnd_log.string(fnd_log.level_procedure,l_module||'.end',
3580 'Exiting '||G_PKG_NAME||'.'||l_api_name||' with '||
3581 'x_return_status = '||x_return_status
3582 );
3583 END IF;
3584 EXCEPTION
3585
3586 WHEN OTHERS THEN
3587 ROLLBACK TO Record_SoIssueCost_PVT;
3588 x_return_status := fnd_api.g_ret_sts_unexp_error ;
3589
3590 IF l_unexpLog THEN
3591 FND_LOG.string(FND_LOG.LEVEL_UNEXPECTED,G_LOG_HEAD || '.'||l_api_name||'.'||l_stmt_num
3592 ,'Record_SoIssueCost '||to_char(l_stmt_num)||' : '||substr(SQLERRM,1,200));
3593 END IF;
3594
3595 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
3596 FND_MESSAGE.set_name('BOM', 'CST_UNEXP_ERROR');
3597 FND_MESSAGE.set_token('PACKAGE', G_PKG_NAME);
3598 FND_MESSAGE.set_token('PROCEDURE',l_api_name);
3599 FND_MESSAGE.set_token('STATEMENT',to_char(l_stmt_num));
3600 FND_MSG_PUB.ADD;
3601 END IF;
3602
3603 FND_MSG_PUB.count_and_get
3604 ( p_count => x_msg_count
3605 , p_data => x_msg_data
3606 , p_encoded => FND_API.g_false
3607 );
3608
3609 END Record_SoIssueCost;
3610
3611 -----------------------------------------------------------------------------
3612 -- Start of comments --
3613 -- --
3614 -- PROCEDURE --
3615 -- Process_RmaReceipt --
3616 -- This procedure is called by the distribution processors for --
3617 -- all perpetual cost methods to create the accounting entries --
3618 -- for RMAs that are linked to forward flow sales orders with --
3619 -- COGS deferral. --
3620 -- --
3621 -- VERSION 1.0 --
3622 -- --
3623 -- PARAMETERS --
3624 -- P_RMA_OM_LINE_ID OM Line ID of the RMA Receipt --
3625 -- P_COGS_OM_LINE_ID OM Line_ID of the Sales Order Issue, not the RMA --
3626 -- P_COST_TYPE_ID Cost Type if Periodic, Cost Method if perpetual --
3627 -- P_TXN_QUANTITY RMA Receipt quantity --
3628 -- P_COGS_PERCENTAGE Latest COGS Percentage reported for this OM line --
3629 -- --
3630 -- HISTORY: --
3631 -- 05/16/05 Bryan Kuntz Created --
3632 -- End of comments --
3636 x_return_status OUT NOCOPY VARCHAR2,
3633 -----------------------------------------------------------------------------
3634
3635 PROCEDURE Process_RmaReceipt(
3637 x_msg_count OUT NOCOPY NUMBER,
3638 x_msg_data OUT NOCOPY VARCHAR2,
3639 p_rma_om_line_id IN NUMBER,
3640 p_cogs_om_line_id IN NUMBER,
3641 p_cost_type_id IN NUMBER,
3642 p_txn_quantity IN NUMBER,
3643 p_cogs_percentage IN NUMBER,
3644 p_organization_id IN NUMBER,
3645 p_transaction_id IN NUMBER,
3646 p_item_id IN NUMBER,
3647 p_sob_id IN NUMBER,
3648 p_txn_date IN DATE,
3649 p_txn_src_id IN NUMBER,
3650 p_src_type_id IN NUMBER,
3651 p_pri_curr IN VARCHAR2,
3652 p_alt_curr IN VARCHAR2,
3653 p_conv_date IN DATE,
3654 p_conv_rate IN NUMBER,
3655 p_conv_type IN VARCHAR2,
3656 p_user_id IN NUMBER,
3657 p_login_id IN NUMBER,
3658 p_req_id IN NUMBER,
3659 p_prg_appl_id IN NUMBER,
3660 p_prg_id IN NUMBER
3661 ) IS
3662
3663 l_api_name CONSTANT VARCHAR2(30) := 'Process_RmaReceipt';
3664 l_api_message VARCHAR2(1000);
3665 l_stmt_num NUMBER := 0;
3666 l_return_status VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
3667 l_msg_count NUMBER := 0;
3668 l_msg_data VARCHAR2(8000) := '';
3669
3670 l_elemental_cost number_table;
3671 l_unit_cost NUMBER;
3672 l_def_cogs_acct_id NUMBER;
3673 l_cogs_acct_id NUMBER;
3674
3675 l_cogs_percentage NUMBER;
3676 l_cogs_credit_amount NUMBER;
3677 l_dcogs_credit_amount NUMBER;
3678
3679 l_rma_cce_id NUMBER := NULL;
3680
3681 l_err_num NUMBER;
3682 l_err_code VARCHAR2(240);
3683 l_err_msg VARCHAR2(240);
3684
3685 l_module CONSTANT VARCHAR2(100) := G_LOG_HEAD ||'.'||l_api_name;
3686 l_unexpLog CONSTANT BOOLEAN := (FND_LOG.LEVEL_UNEXPECTED >= G_LOG_LEVEL) AND FND_LOG.TEST(FND_LOG.LEVEL_UNEXPECTED, l_module);
3687 l_errorLog CONSTANT BOOLEAN := l_unexpLog and (FND_LOG.LEVEL_ERROR >= G_LOG_LEVEL);
3688 l_eventLog CONSTANT BOOLEAN := l_errorLog and (FND_LOG.LEVEL_EVENT >= G_LOG_LEVEL);
3689 l_procLog CONSTANT BOOLEAN := l_eventLog and (FND_LOG.LEVEL_PROCEDURE >= G_LOG_LEVEL);
3690 l_stmtLog CONSTANT BOOLEAN := l_procLog and (FND_LOG.LEVEL_STATEMENT >= G_LOG_LEVEL);
3691 program_exception EXCEPTION;
3692 l_accounting_created NUMBER;
3693
3694 BEGIN
3695
3696 -- Standard start of API savepoint
3697 SAVEPOINT Process_RmaReceipt_PVT;
3698
3699 l_stmt_num := 0;
3700
3701 IF l_proclog THEN
3702 fnd_log.string(FND_LOG.LEVEL_PROCEDURE,l_module||'.begin',
3703 'Entering '||G_PKG_NAME||'.'||l_api_name||' with '||
3704 'p_rma_om_line_id = '||p_rma_om_line_id||','||
3705 'p_cogs_om_line_id = '||p_cogs_om_line_id||','||
3706 'p_cost_type_id = '||p_cost_type_id||','||
3707 'p_txn_quantity = '||p_txn_quantity||','||
3708 'p_cogs_percentage = '||p_cogs_percentage||','||
3709 'p_organization_id = '||p_organization_id||','||
3710 'p_transaction_id = '||p_transaction_id||','||
3711 'p_item_id = '||p_item_id||','||
3712 'p_sob_id = '||p_sob_id||','||
3713 'p_txn_date = '||to_char(p_txn_date,'MM-DD-YYYY')||','||
3714 'p_txn_src_id = '||p_txn_src_id||','||
3715 'p_src_type_id = '||p_src_type_id||','||
3716 'p_pri_curr = '||p_pri_curr||','||
3717 'p_alt_curr = '||p_alt_curr||','||
3718 'p_conv_date = '||to_char(p_conv_date,'MM-DD-YYYY')||','||
3719 'p_conv_rate = '||p_conv_rate||','||
3720 'p_conv_type = '||p_conv_type
3721 );
3722 END IF;
3723
3724 -- Initialize API return status to success
3725 x_return_status := FND_API.G_RET_STS_SUCCESS;
3726 x_msg_count := 0;
3727 x_msg_data := '';
3728
3729 -- API Body
3730
3731 -- Initialize COGS percentage to passed value
3732 l_cogs_percentage := p_cogs_percentage;
3733
3734 l_stmt_num := 10;
3735 IF (l_cogs_percentage IS NULL) THEN
3736 /* Insert the RMA Receipt into the Revenue / COGS Matching data model *
3737 * retrieving the current cogs percentage in the process. */
3738
3739 Insert_OneRmaReceipt(
3740 p_api_version => 1,
3741 p_user_id => p_user_id,
3742 p_login_id => p_login_id,
3743 p_request_id => p_req_id,
3744 p_pgm_app_id => p_prg_appl_id,
3745 p_pgm_id => p_prg_id,
3746 x_return_status => l_return_status,
3747 x_msg_count => l_msg_count,
3748 x_msg_data => l_msg_data,
3749 p_rma_om_line_id => p_rma_om_line_id,
3750 p_cogs_om_line_id => p_cogs_om_line_id,
3751 p_mmt_txn_id => p_transaction_id,
3752 p_organization_id => p_organization_id,
3753 p_item_id => p_item_id,
3754 p_transaction_date => p_txn_date,
3755 p_quantity => p_txn_quantity,
3756 x_event_id => l_rma_cce_id,
3757 x_cogs_percentage => l_cogs_percentage
3758 );
3759
3763 FND_MESSAGE.set_token('COGS_OM_LINE', to_char(p_cogs_om_line_id));
3760 IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
3761 x_return_status := fnd_api.g_ret_sts_error;
3762 FND_MESSAGE.set_name('BOM', 'CST_FAILED_DEFCOGS_RMA_INSERT');
3764 FND_MESSAGE.set_token('MMT_TXN_ID',to_char(p_transaction_id));
3765 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
3766 FND_MSG_PUB.ADD;
3767 END IF;
3768 IF l_errorLog THEN
3769 FND_LOG.message(FND_LOG.LEVEL_ERROR, l_module||'.10',TRUE);
3770 END IF;
3771 raise program_exception;
3772 END IF;
3773
3774 END IF;
3775
3776 l_stmt_num := 20;
3777 /* Get the unit elemental costs and accounts for this sales order line ID */
3778 SELECT unit_material_cost,
3779 unit_moh_cost,
3780 unit_resource_cost,
3781 unit_op_cost,
3782 unit_overhead_cost,
3783 unit_cost,
3784 deferred_cogs_acct_id,
3785 cogs_acct_id
3786 INTO l_elemental_cost(1),
3787 l_elemental_cost(2),
3788 l_elemental_cost(3),
3789 l_elemental_cost(4),
3790 l_elemental_cost(5),
3791 l_unit_cost,
3792 l_def_cogs_acct_id,
3793 l_cogs_acct_id
3794 FROM cst_revenue_cogs_match_lines crcml
3795 WHERE cogs_om_line_id = p_cogs_om_line_id
3796 AND pac_cost_type_id IS NULL;
3797
3798 IF l_stmtLog THEN
3799 FND_LOG.string(FND_LOG.LEVEL_STATEMENT,l_module||'.20','Unit Material Cost = '||to_char(l_elemental_cost(1)));
3800 FND_LOG.string(FND_LOG.LEVEL_STATEMENT,l_module||'.20','Unit MOH Cost = '||to_char(l_elemental_cost(2)));
3801 FND_LOG.string(FND_LOG.LEVEL_STATEMENT,l_module||'.20','Unit Resource Cost = '||to_char(l_elemental_cost(3)));
3802 FND_LOG.string(FND_LOG.LEVEL_STATEMENT,l_module||'.20','Unit OSP Cost = '||to_char(l_elemental_cost(4)));
3803 FND_LOG.string(FND_LOG.LEVEL_STATEMENT,l_module||'.20','Unit OVHD Cost = '||to_char(l_elemental_cost(5)));
3804 FND_LOG.string(FND_LOG.LEVEL_STATEMENT,l_module||'.20','Unit Cost = '||to_char(l_unit_cost));
3805 FND_LOG.string(FND_LOG.LEVEL_STATEMENT,l_module||'.20','p_txn_quantity = '||to_char(p_txn_quantity));
3806 FND_LOG.string(FND_LOG.LEVEL_STATEMENT,l_module||'.20','l_cogs_percentage = '||to_char(l_cogs_percentage));
3807 END IF;
3808
3809 l_accounting_created := 0;
3810
3811 FOR i IN 1..5 LOOP
3812
3813 /* Bug# 9499282 */
3814 IF l_elemental_cost(i) IS NOT NULL THEN
3815
3816 l_stmt_num := 40;
3817 l_cogs_credit_amount := l_elemental_cost(i) * -1 * p_txn_quantity * l_cogs_percentage;
3818 l_dcogs_credit_amount := (l_elemental_cost(i) * -1 * p_txn_quantity) - l_cogs_credit_amount;
3819
3820 IF l_stmtLog THEN
3821 FND_LOG.string(FND_LOG.LEVEL_STATEMENT,l_module||'.40','Cost Element '||to_char(i)||' COGS Credit Amount = '||to_char(l_cogs_credit_amount));
3822 FND_LOG.string(FND_LOG.LEVEL_STATEMENT,l_module||'.40','Cost Element '||to_char(i)||' Deferred COGS Credit Amount = '||to_char(l_dcogs_credit_amount));
3823 END IF;
3824
3825 IF (l_cogs_credit_amount <> 0 ) THEN
3826 l_stmt_num := 50;
3827 -- Create COGS distribution
3828 CSTPACDP.insert_account(p_organization_id, p_transaction_id, p_item_id, -1 * l_cogs_credit_amount,
3829 p_txn_quantity, l_cogs_acct_id, p_sob_id, COGS_LINE_TYPE,
3830 i, NULL,
3831 p_txn_date, p_txn_src_id, p_src_type_id,
3832 p_pri_curr, p_alt_curr, p_conv_date, p_conv_rate, p_conv_type,
3833 1,p_user_id, p_login_id, p_req_id, p_prg_appl_id,p_prg_id,
3834 l_err_num, l_err_code, l_err_msg);
3835 l_accounting_created := l_accounting_created + 1;
3836 END IF;
3837
3838 IF (l_dcogs_credit_amount <> 0 ) THEN
3839 l_stmt_num := 60;
3840
3841 CSTPACDP.insert_account(p_organization_id, p_transaction_id, p_item_id, -1 * l_dcogs_credit_amount,
3842 p_txn_quantity, l_def_cogs_acct_id, p_sob_id, DEF_COGS_LINE_TYPE,
3843 i, NULL,
3844 p_txn_date, p_txn_src_id, p_src_type_id,
3845 p_pri_curr, p_alt_curr, p_conv_date, p_conv_rate, p_conv_type,
3846 1,p_user_id, p_login_id, p_req_id, p_prg_appl_id,p_prg_id,
3847 l_err_num, l_err_code, l_err_msg);
3848 l_accounting_created := l_accounting_created + 1;
3849 END IF;
3850 /* Bug# 9499282 */
3851 END IF; -- End of l_elemental_cost(i)
3852 END LOOP;
3853
3854 If(l_accounting_created = 0) then /*Create Dummy accounting */
3855 if(l_cogs_percentage > 0) then /*Dummy COGS*/
3856 CSTPACDP.insert_account(p_organization_id, p_transaction_id, p_item_id, 0,
3857 p_txn_quantity, l_cogs_acct_id, p_sob_id, COGS_LINE_TYPE,
3858 1, NULL,
3859 p_txn_date, p_txn_src_id, p_src_type_id,
3860 p_pri_curr, p_alt_curr, p_conv_date, p_conv_rate, p_conv_type,
3861 1,p_user_id, p_login_id, p_req_id, p_prg_appl_id,p_prg_id,
3862 l_err_num, l_err_code, l_err_msg);
3863 end if;
3864 if(l_cogs_percentage < 1) then /*Dummy Def COGS*/
3865 CSTPACDP.insert_account(p_organization_id, p_transaction_id, p_item_id, 0,
3866 p_txn_quantity, l_def_cogs_acct_id, p_sob_id, DEF_COGS_LINE_TYPE,
3867 1, NULL,
3868 p_txn_date, p_txn_src_id, p_src_type_id,
3869 p_pri_curr, p_alt_curr, p_conv_date, p_conv_rate, p_conv_type,
3873 End if;
3870 1,p_user_id, p_login_id, p_req_id, p_prg_appl_id,p_prg_id,
3871 l_err_num, l_err_code, l_err_msg);
3872 end if;
3874
3875 l_stmt_num := 70;
3876 -- Mark the RMA event in CCE as costed
3877 UPDATE cst_cogs_events
3878 SET costed = NULL
3879 WHERE mmt_transaction_id = p_transaction_id;
3880
3881 -- End API Body
3882
3883 FND_MSG_PUB.count_and_get
3884 ( p_count => x_msg_count
3885 , p_data => x_msg_data
3886 , p_encoded => FND_API.g_false
3887 );
3888
3889 IF l_proclog THEN
3890 fnd_log.string(FND_LOG.LEVEL_PROCEDURE,l_module||'.end',
3891 'Exiting '||G_PKG_NAME||'.'||l_api_name||' with '||
3892 'x_return_status = '||x_return_status
3893 );
3894 END IF;
3895
3896 EXCEPTION
3897
3898 WHEN program_exception THEN
3899 ROLLBACK TO Process_RmaReceipt_PVT;
3900 x_return_status := fnd_api.g_ret_sts_error ;
3901
3902 FND_MESSAGE.set_name('BOM', 'CST_PLSQL_ERROR');
3903 FND_MESSAGE.set_token('PACKAGE', G_PKG_NAME);
3904 FND_MESSAGE.set_token('PROCEDURE',l_api_name);
3905 FND_MESSAGE.set_token('STATEMENT',to_char(l_stmt_num));
3906 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_error) THEN
3907 FND_MSG_PUB.ADD;
3908 END IF;
3909
3910 IF l_errorLog THEN
3911 FND_LOG.MESSAGE(FND_LOG.LEVEL_ERROR,l_module||'.'||l_stmt_num,TRUE);
3912 END IF;
3913
3914 FND_MSG_PUB.count_and_get
3915 ( p_count => x_msg_count,
3916 p_data => x_msg_data,
3917 p_encoded => FND_API.g_false
3918 );
3919
3920 WHEN OTHERS THEN
3921 ROLLBACK TO Process_RmaReceipt_PVT;
3922 x_return_status := fnd_api.g_ret_sts_unexp_error ;
3923
3924 IF l_unexpLog THEN
3925 FND_LOG.string(FND_LOG.LEVEL_UNEXPECTED,l_module||'.'||l_stmt_num
3926 ,'Process_RmaReceipt '||to_char(l_stmt_num)||' : '||substr(SQLERRM,1,200));
3927 END IF;
3928
3929 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
3930 FND_MESSAGE.set_name('BOM', 'CST_UNEXP_ERROR');
3931 FND_MESSAGE.set_token('PACKAGE', G_PKG_NAME);
3932 FND_MESSAGE.set_token('PROCEDURE',l_api_name);
3933 FND_MESSAGE.set_token('STATEMENT',to_char(l_stmt_num));
3934 FND_MSG_PUB.ADD;
3935 END IF;
3936
3937 FND_MSG_PUB.count_and_get
3938 ( p_count => x_msg_count
3939 , p_data => x_msg_data
3940 , p_encoded => FND_API.g_false
3941 );
3942
3943 END Process_RmaReceipt;
3944
3945 -----------------------------------------------------------------------------
3946 -- Start of comments --
3947 -- --
3948 -- PROCEDURE --
3949 -- Process_CogsRecognitionTxn --
3950 -- This procedure is called by the distribution processors for --
3951 -- all perpetual cost methods to create the accounting entries --
3952 -- for COGS Recognition Events. --
3953 -- --
3954 -- VERSION 1.0 --
3955 -- --
3956 -- PARAMETERS --
3957 -- P_COGS_OM_LINE_ID OM Line_ID of the Sales Order issue --
3958 -- All other parameters are pretty standard for Cost Processing --
3959 -- --
3960 -- HISTORY: --
3961 -- 05/17/05 Bryan Kuntz Created --
3962 -- End of comments --
3963 -----------------------------------------------------------------------------
3964
3965 PROCEDURE Process_CogsRecognitionTxn(
3966 x_return_status OUT NOCOPY VARCHAR2,
3967 x_msg_count OUT NOCOPY NUMBER,
3968 x_msg_data OUT NOCOPY VARCHAR2,
3969 p_cogs_om_line_id IN NUMBER,
3970 p_transaction_id IN NUMBER,
3971 p_txn_quantity IN NUMBER,
3972 p_organization_id IN NUMBER,
3973 p_item_id IN NUMBER,
3974 p_sob_id IN NUMBER,
3975 p_txn_date IN DATE,
3976 p_txn_src_id IN NUMBER,
3977 p_src_type_id IN NUMBER,
3978 p_pri_curr IN VARCHAR2,
3979 p_alt_curr IN VARCHAR2,
3980 p_conv_date IN DATE,
3981 p_conv_rate IN NUMBER,
3982 p_conv_type IN VARCHAR2,
3983 p_user_id IN NUMBER,
3984 p_login_id IN NUMBER,
3985 p_req_id IN NUMBER,
3986 p_prg_appl_id IN NUMBER,
3987 p_prg_id IN NUMBER
3988 ) IS
3989
3990 l_api_name CONSTANT VARCHAR2(30) := 'Process_CogsRecognitionTxn';
3991 l_api_message VARCHAR2(1000);
3992 l_stmt_num NUMBER := 0;
3993 l_module CONSTANT VARCHAR2(100) := G_LOG_HEAD ||'.'||l_api_name;
3997 l_procLog CONSTANT BOOLEAN := l_eventLog and (FND_LOG.LEVEL_PROCEDURE >= G_LOG_LEVEL);
3994 l_unexpLog CONSTANT BOOLEAN := (FND_LOG.LEVEL_UNEXPECTED >= G_LOG_LEVEL) AND FND_LOG.TEST(FND_LOG.LEVEL_UNEXPECTED, l_module);
3995 l_errorLog CONSTANT BOOLEAN := l_unexpLog and (FND_LOG.LEVEL_ERROR >= G_LOG_LEVEL);
3996 l_eventLog CONSTANT BOOLEAN := l_errorLog and (FND_LOG.LEVEL_EVENT >= G_LOG_LEVEL);
3998 l_stmtLog CONSTANT BOOLEAN := l_procLog and (FND_LOG.LEVEL_STATEMENT >= G_LOG_LEVEL);
3999
4000 l_new_percentage NUMBER;
4001 l_prior_percentage NUMBER;
4002
4003 l_elemental_cost number_table;
4004 l_def_cogs_acct_id NUMBER;
4005 l_cogs_acct_id NUMBER;
4006
4007 l_adjustment_amount NUMBER;
4008
4009 l_err_num NUMBER;
4010 l_err_code VARCHAR2(240);
4011 l_err_msg VARCHAR2(240);
4012
4013 BEGIN
4014
4015 -- Standard start of API savepoint
4016 SAVEPOINT Process_CogsRecognitionTxn_PVT;
4017
4018 l_stmt_num := 0;
4019
4020 IF l_proclog THEN
4021 fnd_log.string(FND_LOG.LEVEL_PROCEDURE,l_module||'.begin',
4022 'Entering '||G_PKG_NAME||'.'||l_api_name||' with '||
4023 'p_cogs_om_line_id = '||p_cogs_om_line_id||','||
4024 'p_transaction_id = '||p_transaction_id||','||
4025 'p_txn_quantity = '||p_txn_quantity||','||
4026 'p_organization_id = '||p_organization_id||','||
4027 'p_item_id = '||p_item_id||','||
4028 'p_sob_id = '||p_sob_id||','||
4029 'p_txn_date = '||to_char(p_txn_date,'MM-DD-YYYY')||','||
4030 'p_txn_src_id = '||p_txn_src_id||','||
4031 'p_src_type_id = '||p_src_type_id||','||
4032 'p_pri_curr = '||p_pri_curr||','||
4033 'p_alt_curr = '||p_alt_curr||','||
4034 'p_conv_date = '||to_char(p_conv_date,'MM-DD-YYYY')||','||
4035 'p_conv_rate = '||p_conv_rate||','||
4036 'p_conv_type = '||p_conv_type
4037 );
4038 END IF;
4039
4040 -- Initialize API return status to success
4041 x_return_status := FND_API.G_RET_STS_SUCCESS;
4042 x_msg_count := 0;
4043 x_msg_data := '';
4044
4045 -- API Body
4046
4047 -- Retrieve the COGS percentage and prior COGS percentage for this
4048 -- event while marking it as costed
4049 l_stmt_num := 10;
4050 UPDATE cst_cogs_events
4051 SET costed = NULL
4052 WHERE mmt_transaction_id = p_transaction_id
4053 RETURNING cogs_percentage,
4054 prior_cogs_percentage
4055 INTO l_new_percentage,
4056 l_prior_percentage;
4057
4058
4059 -- Need the [elemental] unit costs, as well as the accounts from the
4060 -- original sales order to make the COGS adjustment
4061 l_stmt_num := 20;
4062 SELECT unit_material_cost,
4063 unit_moh_cost,
4064 unit_resource_cost,
4065 unit_op_cost,
4066 unit_overhead_cost,
4067 cogs_acct_id,
4068 deferred_cogs_acct_id
4069 INTO l_elemental_cost(1),
4070 l_elemental_cost(2),
4071 l_elemental_cost(3),
4072 l_elemental_cost(4),
4073 l_elemental_cost(5),
4074 l_cogs_acct_id,
4075 l_def_cogs_acct_id
4076 FROM cst_revenue_cogs_match_lines crcml
4077 WHERE cogs_om_line_id = p_cogs_om_line_id
4078 AND pac_cost_type_id IS NULL;
4079
4080 FOR i IN 1..5 LOOP
4081 l_stmt_num := 30;
4082 l_adjustment_amount := l_elemental_cost(i) * p_txn_quantity * (l_new_percentage - l_prior_percentage);
4083
4084 IF (l_adjustment_amount <> 0) THEN
4085 -- Dr. COGS
4086 l_stmt_num := 40;
4087 CSTPACDP.insert_account(p_organization_id, p_transaction_id, p_item_id, l_adjustment_amount,
4088 p_txn_quantity, l_cogs_acct_id, p_sob_id, COGS_LINE_TYPE,
4089 i, NULL,
4090 p_txn_date, p_txn_src_id, p_src_type_id,
4091 p_pri_curr, p_alt_curr, p_conv_date, p_conv_rate, p_conv_type,
4092 1,p_user_id, p_login_id, p_req_id, p_prg_appl_id,p_prg_id,
4093 l_err_num, l_err_code, l_err_msg);
4094
4095 l_stmt_num := 50;
4096 -- Cr. Deferred COGS
4097 CSTPACDP.insert_account(p_organization_id, p_transaction_id, p_item_id, -1 * l_adjustment_amount,
4098 -1 * p_txn_quantity, l_def_cogs_acct_id, p_sob_id, DEF_COGS_LINE_TYPE,
4099 i, NULL,
4100 p_txn_date, p_txn_src_id, p_src_type_id,
4101 p_pri_curr, p_alt_curr, p_conv_date, p_conv_rate, p_conv_type,
4102 1,p_user_id, p_login_id, p_req_id, p_prg_appl_id,p_prg_id,
4103 l_err_num, l_err_code, l_err_msg);
4104 END IF;
4105
4106 END LOOP;
4107
4108
4109 -- End API Body
4110
4111 FND_MSG_PUB.count_and_get
4112 ( p_count => x_msg_count
4113 , p_data => x_msg_data
4114 , p_encoded => FND_API.g_false
4115 );
4116
4117 IF l_proclog THEN
4118 fnd_log.string(FND_LOG.LEVEL_PROCEDURE,l_module||'.end',
4119 'Exiting '||G_PKG_NAME||'.'||l_api_name||' with '||
4120 'x_return_status = '||x_return_status
4121 );
4122 END IF;
4123
4124 EXCEPTION
4125 WHEN OTHERS THEN
4126 ROLLBACK TO Process_CogsRecognitionTxn_PVT;
4127 x_return_status := fnd_api.g_ret_sts_unexp_error ;
4128
4129 IF l_unexpLog THEN
4130 FND_LOG.string(FND_LOG.LEVEL_UNEXPECTED,G_LOG_HEAD || '.'||l_api_name||l_stmt_num
4131 ,'Process_CogsRecognitionTxn ('||to_char(l_stmt_num)||') : '||substr(SQLERRM,1,200));
4132 END IF;
4133
4137 FND_MESSAGE.set_token('PROCEDURE',l_api_name);
4134 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
4135 FND_MESSAGE.set_name('BOM', 'CST_UNEXP_ERROR');
4136 FND_MESSAGE.set_token('PACKAGE', G_PKG_NAME);
4138 FND_MESSAGE.set_token('STATEMENT',to_char(l_stmt_num));
4139 FND_MSG_PUB.ADD;
4140 END IF;
4141
4142 FND_MSG_PUB.count_and_get
4143 ( p_count => x_msg_count
4144 , p_data => x_msg_data
4145 , p_encoded => FND_API.g_false
4146 );
4147
4148 END Process_CogsRecognitionTxn;
4149
4150
4151 -----------------------------------------------------------------------------
4152 -- Start of comments --
4153 -- --
4154 -- PROCEDURE --
4155 -- Cost_BulkCogsRecTxns --
4156 -- This procedure is called in phase 4 of the concurrent request --
4157 -- to create the accounting distributions for all of the COGS --
4158 -- Recognition Events that were created during this run of the --
4159 -- concurrent request. --
4160 -- --
4161 -- VERSION 1.0 --
4162 -- --
4163 -- PARAMETERS --
4164 -- Standard return status and Who columns --
4165 -- --
4166 -- HISTORY: --
4167 -- 05/17/05 Bryan Kuntz Created --
4168 -- End of comments --
4169 -----------------------------------------------------------------------------
4170
4171 PROCEDURE Cost_BulkCogsRecTxns(
4172 x_return_status OUT NOCOPY VARCHAR2,
4173 p_request_id IN NUMBER,
4174 p_user_id IN NUMBER,
4175 p_login_id IN NUMBER,
4176 p_pgm_app_id IN NUMBER,
4177 p_pgm_id IN NUMBER,
4178 p_ledger_id IN NUMBER DEFAULT NULL --BUG5726230
4179 ,p_neg_req_id IN NUMBER DEFAULT NULL --BUG7387575
4180 ) IS
4181
4182 l_api_name CONSTANT VARCHAR2(30) := 'Cost_BulkCogsRecTxns';
4183 l_api_message VARCHAR2(1000);
4184 l_module CONSTANT VARCHAR2(100) := G_LOG_HEAD ||'.'||l_api_name;
4185 l_unexpLog CONSTANT BOOLEAN := (FND_LOG.LEVEL_UNEXPECTED >= G_LOG_LEVEL) AND FND_LOG.TEST(FND_LOG.LEVEL_UNEXPECTED, l_module);
4186 l_errorLog CONSTANT BOOLEAN := l_unexpLog and (FND_LOG.LEVEL_ERROR >= G_LOG_LEVEL);
4187 l_eventLog CONSTANT BOOLEAN := l_errorLog and (FND_LOG.LEVEL_EVENT >= G_LOG_LEVEL);
4188 l_procLog CONSTANT BOOLEAN := l_eventLog and (FND_LOG.LEVEL_PROCEDURE >= G_LOG_LEVEL);
4189 l_stmtLog CONSTANT BOOLEAN := l_procLog and (FND_LOG.LEVEL_STATEMENT >= G_LOG_LEVEL);
4190
4191 l_return_status VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
4192 l_stmt_num NUMBER := 0;
4193
4194 CURSOR c_uncosted_cogs_events(p_sob_id IN NUMBER) IS
4195 SELECT cce.cogs_om_line_id,
4196 cce.mmt_transaction_id,
4197 cce.cogs_percentage,
4198 cce.prior_cogs_percentage,
4199 cce.event_quantity,
4200 --{BUG#6980119
4201 -- cce.event_date,
4202 mmt.transaction_date,
4203 --}
4204 cce.event_id,
4205 cce.event_type,
4206 crcml.operating_unit_id,
4207 crcml.organization_id,
4208 crcml.cogs_acct_id,
4209 crcml.deferred_cogs_acct_id,
4210 crcml.inventory_item_id,
4211 crcml.unit_material_cost,
4212 crcml.unit_moh_cost,
4213 crcml.unit_resource_cost,
4214 crcml.unit_op_cost,
4215 crcml.unit_overhead_cost,
4216 crcml.unit_cost,
4217 sob.currency_code,
4218 NVL(mmt.transaction_source_id, -1)
4219 FROM cst_cogs_events cce,
4220 cst_revenue_cogs_match_lines crcml,
4221 mtl_parameters mp,
4222 gl_sets_of_books sob,
4223 mtl_material_transactions mmt -- joining here to use the index on costed_flag
4224 WHERE
4225 cce.mmt_transaction_id = mmt.transaction_id
4226 AND mmt.costed_flag = 'N'
4227 AND mmt.transaction_action_id = 36
4228 AND mmt.transaction_source_type_id = 2
4229 ---------------------------------------
4230 -- When the Cogs Recognition program reaches this point the MMT for COGS Regnition are commited to the DB
4231 -- The COGS Recognition program is still running and the Cost Manager should not start as it is incompatible
4232 -- with the Cogs Recognition program. Hence normally the MMT will not be picked up by the Cost Worker
4233 -- But in the case this phase of insertion into MTA stops by user action or by env issue
4234 -- Next time the Cost Manager could have launch the cost worker and during its run the COGS regnition
4235 -- program can start causing the MMT being costed by both program
4236 -- We add the condition transaction_group_id IS NULL as this is the first action the Cost Manager does
4237 -- stamping the transaction_group_id before assigning the job to cost worker and goes back to pending
4238 -- If the MMT is selected by Cost Manager, COGS recognition program should not pick it up
4242 AND cce.event_type IN (COGS_RECOGNITION_EVENT,
4239 ---------------------------------------
4240 AND mmt.transaction_group_id IS NULL --BUG#6730436
4241 AND cce.costed = 'N'
4243 COGS_REC_PERCENT_ADJUSTMENT,
4244 COGS_REC_QTY_ADJUSTMENT)
4245 AND crcml.cogs_om_line_id = cce.cogs_om_line_id
4246 AND crcml.pac_cost_type_id IS NULL
4247 AND crcml.ledger_id = p_sob_id
4248 AND crcml.original_shipped_qty IS NOT NULL -- indicator of whether the SO Issue was costed.
4249 AND crcml.organization_id = mp.organization_id
4250 AND nvl(mp.process_enabled_flag, 'N') = 'N' -- Cost only discrete orgs
4251 AND crcml.ledger_id = sob.set_of_books_id
4252 AND DECODE(p_neg_req_id,NULL,-99,p_neg_req_id)
4253 = DECODE(p_neg_req_id,NULL,-99,mmt.transaction_set_id); --BUG#7387575
4254
4255
4256 --{BUG#5726230
4257 CURSOR cu_ledger(p_ledger_id IN NUMBER) IS
4258 SELECT DISTINCT set_of_books_id
4259 FROM gl_sets_of_books
4260 WHERE set_of_books_id = NVL(p_ledger_id, set_of_books_id);
4261
4262 l_ledger_id_tab number_table;
4263 l_sob NUMBER;
4264 --}
4265 l_cogs_om_line_id_tbl number_table;
4266 l_mmt_txn_id_tbl number_table;
4267 l_cogs_percentage_tbl number_table;
4268 l_prior_percentage_tbl number_table;
4269 l_event_quantity_tbl number_table;
4270 l_event_date_tbl date_table;
4271 l_event_id_tbl number_table;
4272 l_event_type_tbl number_table;
4273 l_ou_id_tbl number_table;
4274 l_organization_id_tbl number_table;
4275 l_cogs_acct_id_tbl number_table;
4276 l_def_cogs_acct_id_tbl number_table;
4277 l_item_id_tbl number_table;
4278 l_e1_tbl number_table;
4279 l_e2_tbl number_table;
4280 l_e3_tbl number_table;
4281 l_e4_tbl number_table;
4282 l_e5_tbl number_table;
4283 l_unit_cost_tbl number_table;
4284 l_currency_code_tbl char15_table;
4285 l_ou_offset_tbl number_table;
4286 l_le_date_tbl date_table;
4287 l_transaction_source_id number_table;
4288
4289 l_last_fetch BOOLEAN;
4290 end_of_program EXCEPTION;
4291 BEGIN
4292
4293 -- Standard start of API savepoint
4294 SAVEPOINT Cost_BulkCogsRecTxns_PVT;
4295
4296 l_stmt_num := 0;
4297
4298 IF l_procLog THEN
4299 FND_LOG.string(FND_LOG.LEVEL_PROCEDURE,l_module||'.begin',
4300 'Entering '||G_PKG_NAME||'.'||l_api_name
4301 );
4302 END IF;
4303
4304 -- Initialize API return status to success
4305 x_return_status := FND_API.G_RET_STS_SUCCESS;
4306
4307 -- API Body
4308 -- Need to loop by set of books (ledger) because the call to the SLA
4309 -- API requires the ledger as an input parameter. So we'll have to
4310 -- cost the events ledger by ledger.
4311
4312 debug(' p_ledger_id :'||p_ledger_id);
4313
4314 --{BUG5726230
4315 OPEN cu_ledger(p_ledger_id);
4316 FETCH cu_ledger BULK COLLECT INTO l_ledger_id_tab;
4317 CLOSE cu_ledger;
4318
4319 IF l_ledger_id_tab.COUNT = 0 THEN
4320 RAISE end_of_program;
4321 END IF;
4322
4323 <<ledger_loop>>
4324 -- FOR l_sob_id IN (SELECT distinct set_of_books_id FROM gl_sets_of_books) LOOP
4325 FOR i IN l_ledger_id_tab.FIRST .. l_ledger_id_tab.LAST LOOP
4326 --}
4327 l_sob := l_ledger_id_tab(i);
4328 debug(' processing for the ledger:'||l_sob);
4329 l_last_fetch := FALSE; -- initialize boolean variable
4330
4331 OPEN c_uncosted_cogs_events(l_sob); --(l_sob_id.set_of_books_id);
4332
4333 <<uncosted_events_loop>>
4334 LOOP
4335 -- Get 1000 rows or less from the uncosted events cursor
4336 l_stmt_num := 10;
4337 FETCH c_uncosted_cogs_events BULK COLLECT INTO
4338 l_cogs_om_line_id_tbl,
4339 l_mmt_txn_id_tbl,
4340 l_cogs_percentage_tbl,
4341 l_prior_percentage_tbl,
4342 l_event_quantity_tbl,
4343 l_event_date_tbl,
4344 l_event_id_tbl,
4345 l_event_type_tbl,
4346 l_ou_id_tbl,
4347 l_organization_id_tbl,
4348 l_cogs_acct_id_tbl,
4349 l_def_cogs_acct_id_tbl,
4350 l_item_id_tbl,
4351 l_e1_tbl,
4352 l_e2_tbl,
4353 l_e3_tbl,
4354 l_e4_tbl,
4355 l_e5_tbl,
4356 l_unit_cost_tbl,
4357 l_currency_code_tbl,
4358 l_transaction_source_id
4359 LIMIT C_max_bulk_fetch_size;
4360
4361 IF c_uncosted_cogs_events%NOTFOUND THEN
4362 l_last_fetch := TRUE;
4363 END IF;
4364
4365 IF (l_cogs_om_line_id_tbl.COUNT = 0 AND l_last_fetch) THEN
4366 CLOSE c_uncosted_cogs_events;
4367 EXIT uncosted_events_loop;
4368 END IF;
4369
4370 -- Populate the legal entity dates table using each OU from above
4371 FOR i IN l_event_id_tbl.FIRST..l_event_id_tbl.LAST LOOP
4372
4373 IF NOT l_ou_offset_tbl.EXISTS(l_ou_id_tbl(i)) THEN
4374
4378 END IF;
4375 l_ou_offset_tbl(l_ou_id_tbl(i)) := INV_LE_TIMEZONE_PUB.get_le_day_time_for_ou(
4376 SYSDATE, l_ou_id_tbl(i)) - SYSDATE;
4377
4379
4380 l_le_date_tbl(i) := l_event_date_tbl(i) + l_ou_offset_tbl(l_ou_id_tbl(i));
4381
4382 END LOOP;
4383
4384 l_stmt_num := 20;
4385 -- To help create debits and credits in bulk, insert 2 simple rows in
4386 -- cst_lists_temp with opposite signs
4387 -- These insertions are inside the loop because the commit at stmt 80
4388 -- blows these away.
4389 -- Keeping these inserts inside the loop (instead of doing once outside
4390 -- the loop) in case the definition of the cst_lists_temp table ever
4391 -- changes from session to txn based. If the definition changes, there
4392 -- is no need to change it here since I'm deleting and reinserting each
4393 -- commit cycle.
4394 DELETE cst_lists_temp;
4395
4396 INSERT INTO cst_lists_temp (list_id)
4397 VALUES (1); -- Dr.
4398
4399 INSERT INTO cst_lists_temp (list_id)
4400 VALUES (-1); -- Cr.
4401
4402 -- The following statement will create elemental distributions for all source rows returned
4403 -- by the above cursor.
4404 l_stmt_num := 30;
4405 FORALL i IN l_event_id_tbl.FIRST..l_event_id_tbl.LAST
4406 INSERT INTO mtl_transaction_accounts mta (
4407 TRANSACTION_ID,
4408 REFERENCE_ACCOUNT,
4409 INVENTORY_ITEM_ID,
4410 ORGANIZATION_ID,
4411 TRANSACTION_DATE,
4412 TRANSACTION_SOURCE_ID,
4413 GL_BATCH_ID,
4414 ACCOUNTING_LINE_TYPE,
4415 BASE_TRANSACTION_VALUE,
4416 CONTRA_SET_ID,
4417 TRANSACTION_SOURCE_TYPE_ID,
4418 PRIMARY_QUANTITY,
4419 RATE_OR_AMOUNT,
4420 BASIS_TYPE,
4421 COST_ELEMENT_ID,
4422 INV_SUB_LEDGER_ID,
4423 -- WHO COLUMNS
4424 LAST_UPDATE_DATE,
4425 LAST_UPDATED_BY,
4426 CREATION_DATE,
4427 CREATED_BY,
4428 LAST_UPDATE_LOGIN,
4429 REQUEST_ID,
4430 PROGRAM_APPLICATION_ID,
4431 PROGRAM_ID,
4432 PROGRAM_UPDATE_DATE)
4433 SELECT l_mmt_txn_id_tbl(i),
4434 decode(clt.list_id, 1, l_cogs_acct_id_tbl(i), -1, l_def_cogs_acct_id_tbl(i), NULL),
4435 l_item_id_tbl(i),
4436 l_organization_id_tbl(i),
4437 l_event_date_tbl(i),
4438 l_transaction_source_id(i), -- txn_source_id is not necessary, this column is NOT NULL by mistake
4439 NULL, -- GL batch ID
4440 decode(clt.list_id, 1, COGS_LINE_TYPE, -1, DEF_COGS_LINE_TYPE, NULL),
4441 decode(fc.minimum_accountable_unit, NULL,
4442 ROUND(clt.list_id * l_event_quantity_tbl(i) * (l_cogs_percentage_tbl(i) - l_prior_percentage_tbl(i)) *
4443 decode(cce.cost_element_id, 1, l_e1_tbl(i), 2, l_e2_tbl(i), 3, l_e3_tbl(i),
4444 4, l_e4_tbl(i), 5, l_e5_tbl(i), 0), fc.precision),
4445 ROUND(clt.list_id * l_event_quantity_tbl(i) * (l_cogs_percentage_tbl(i) - l_prior_percentage_tbl(i)) *
4446 decode(cce.cost_element_id, 1, l_e1_tbl(i), 2, l_e2_tbl(i), 3, l_e3_tbl(i),
4447 4, l_e4_tbl(i), 5, l_e5_tbl(i), 0) / fc.minimum_accountable_unit)
4448 * fc.minimum_accountable_unit),
4449 1, -- contra set ID
4450 2, -- transaction source type ID for COGS Recognition Events
4451 clt.list_id * l_event_quantity_tbl(i),
4452 clt.list_id * (l_cogs_percentage_tbl(i) - l_prior_percentage_tbl(i)) *
4453 decode(cce.cost_element_id,
4454 1, l_e1_tbl(i),
4455 2, l_e2_tbl(i),
4456 3, l_e3_tbl(i),
4457 4, l_e4_tbl(i),
4458 5, l_e5_tbl(i),
4459 0),
4460 1, -- basis type
4461 cce.cost_element_id,
4462 cst_inv_sub_ledger_id_s.nextval,
4463 -- WHO COLUMNS
4464 sysdate,
4465 p_user_id,
4466 sysdate,
4467 p_user_id,
4468 p_login_id,
4469 p_request_id,
4470 p_pgm_app_id,
4471 p_pgm_id,
4472 sysdate
4473 FROM cst_cost_elements cce,
4474 cst_lists_temp clt,
4475 fnd_currencies fc
4476 WHERE fc.currency_code = l_currency_code_tbl(i)
4477 AND l_event_quantity_tbl(i) <> 0
4478 AND l_cogs_percentage_tbl(i) <> l_prior_percentage_tbl(i)
4479 AND nvl(decode(cce.cost_element_id,
4480 1, l_e1_tbl(i),
4481 2, l_e2_tbl(i),
4485 0),0) <> 0;
4482 3, l_e3_tbl(i),
4483 4, l_e4_tbl(i),
4484 5, l_e5_tbl(i),
4486
4487 l_stmt_num := 40;
4488 -- Update the costed flag in cst_cogs_events
4489 FORALL i IN l_event_id_tbl.FIRST..l_event_id_tbl.LAST
4490 UPDATE cst_cogs_events
4491 SET costed = NULL,
4492 last_update_date = sysdate,
4493 last_updated_by = p_user_id,
4494 last_update_login = p_login_id,
4495 request_id = p_request_id
4496 WHERE event_id = l_event_id_tbl(i);
4497
4498 l_stmt_num := 50;
4499 -- Update the costed flag in MMT
4500 FORALL i IN l_event_id_tbl.FIRST..l_event_id_tbl.LAST
4501 UPDATE mtl_material_transactions
4502 SET costed_flag = NULL,
4503 last_update_date = sysdate,
4504 last_updated_by = p_user_id,
4505 last_update_login = p_login_id,
4506 request_id = p_request_id,
4507 program_application_id = p_pgm_app_id,
4508 program_id = p_pgm_id,
4509 program_update_date = sysdate
4510 WHERE transaction_id = l_mmt_txn_id_tbl(i);
4511
4512 -- Pass events to SLA via a bulk insertion into their Global Temp Table
4513
4514 debug('COUNT MMT:'||SQL%ROWCOUNT);
4515
4516 l_stmt_num := 60;
4517 FORALL i IN l_event_id_tbl.FIRST..l_event_id_tbl.LAST
4518 INSERT INTO xla_events_int_gt
4519 (
4520 application_id,
4521 ledger_id,
4522 entity_code,
4523 source_id_int_1,
4524 source_id_int_2,
4525 source_id_int_3,
4526 event_class_code,
4527 event_type_code,
4528 event_date,
4529 event_status_code,
4530 security_id_int_1,
4531 security_id_int_2,
4532 transaction_date,
4533 reference_date_1,
4534 transaction_number
4535 )
4536 SELECT
4537 707,
4538 l_sob, --l_sob_id.set_of_books_id,
4539 'MTL_ACCOUNTING_EVENTS',
4540 l_mmt_txn_id_tbl(i),
4541 l_organization_id_tbl(i),
4542 2, -- source type ID for sales order related txns
4543 'SALES_ORDER',
4544 decode(l_event_type_tbl(i), COGS_RECOGNITION_EVENT , 'COGS_RECOGNITION',
4545 COGS_REC_PERCENT_ADJUSTMENT, 'COGS_RECOGNITION_ADJ',
4546 COGS_REC_QTY_ADJUSTMENT , 'COGS_RECOGNITION_ADJ',
4547 NULL),
4548 l_event_date_tbl(i),
4549 XLA_EVENTS_PUB_PKG.C_EVENT_UNPROCESSED,
4550 l_organization_id_tbl(i),
4551 l_ou_id_tbl(i),
4552 l_event_date_tbl(i),
4553 l_le_date_tbl(i),
4554 l_mmt_txn_id_tbl(i)
4555 FROM
4556 mtl_parameters mp,
4557 pjm_org_parameters pop,
4558 mtl_material_transactions mmt
4559 WHERE mmt.transaction_id = l_mmt_txn_id_tbl(i) --bug#14180892
4560 AND mp.organization_id = mmt.organization_id
4561 AND pop.organization_id (+) = mp.organization_id
4562 AND NVL(pop.pa_posting_flag,'N') <> 'Y'
4563
4564 AND EXISTS (SELECT NULL
4565 FROM mtl_transaction_accounts mta
4566 WHERE mta.transaction_id = mmt.transaction_id);
4567
4568
4569 debug('COUNT XLA:'||SQL%ROWCOUNT);
4570
4571
4572 l_stmt_num := 70;
4573 -- Call this API to instruct SLA to create these events from
4574 -- the data just populated in the global temp table.
4575 xla_events_pub_pkg.create_bulk_events(p_application_id => 707,
4576 p_ledger_id => l_sob, --l_sob_id.set_of_books_id,
4577 p_entity_type_code => 'MTL_ACCOUNTING_EVENTS',
4578 p_source_application_id => 401);
4579
4580 l_stmt_num := 80;
4581 commit; -- commit the processing of 1000 input rows at a time
4582 SAVEPOINT Cost_BulkCogsRecTxns_PVT;
4583
4584 END LOOP uncosted_events_loop;
4585 END LOOP ledger_loop;
4586
4587
4588 -- End API Body
4589
4590 IF l_proclog THEN
4591 fnd_log.string(FND_LOG.LEVEL_PROCEDURE,l_module||'.end',
4592 'Exiting '||G_PKG_NAME||'.'||l_api_name||' with '||
4593 'x_return_status = '||x_return_status
4594 );
4595 END IF;
4596
4597 EXCEPTION
4598 WHEN end_of_program THEN
4599 ROLLBACK TO Cost_BulkCogsRecTxns_PVT;
4600 -- Let the program end normally
4601 x_return_status := FND_API.G_RET_STS_SUCCESS;
4602 debug('EXCEPTION end_of_program - Cost_BulkCogsRecTxns '||l_stmt_num||' : no ledger found - p_ledger_id :'||p_ledger_id);
4603
4604 WHEN OTHERS THEN
4605 ROLLBACK TO Cost_BulkCogsRecTxns_PVT;
4606 x_return_status := fnd_api.g_ret_sts_unexp_error ;
4607
4608 IF l_unexpLog THEN
4609 FND_LOG.string(FND_LOG.LEVEL_UNEXPECTED,G_LOG_HEAD || '.'||l_api_name||l_stmt_num
4610 ,'Cost_BulkCogsRecTxns '||to_char(l_stmt_num)||' : '||substr(SQLERRM,1,200));
4611 END IF;
4612
4616 FND_MESSAGE.set_token('PROCEDURE',l_api_name);
4613 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
4614 FND_MESSAGE.set_name('BOM', 'CST_UNEXP_ERROR');
4615 FND_MESSAGE.set_token('PACKAGE', G_PKG_NAME);
4617 FND_MESSAGE.set_token('STATEMENT',to_char(l_stmt_num));
4618 FND_MSG_PUB.ADD;
4619 END IF;
4620
4621 END Cost_BulkCogsRecTxns;
4622
4623
4624 -----------------------------------------------------------------------------
4625 -- Start of comments --
4626 -- --
4627 -- PROCEDURE --
4628 -- Insert_PacSoIssue This is the PAC version of Insert_OneSoIssue(). --
4629 -- It creates a new row in CRCML for the given OM Line ID and --
4630 -- PAC cost type ID. The purpose is to record the SO issue --
4631 -- cost so that future related events (e.g. COGS Recognition) --
4632 -- can query this row and create accting with these amounts. --
4633 -- --
4634 -- VERSION 1.0 --
4635 -- --
4636 -- PARAMETERS --
4637 -- P_TRANSACTION_ID MMT Transaction ID --
4638 -- P_LAYER_ID PAC Cost Layer ID (CPIC, CPICD) --
4639 -- P_COST_TYPE_ID PAC Cost Type ID --
4640 -- P_COST_GROUP_ID PAC Cost Group ID --
4641 -- --
4642 -- HISTORY: --
4643 -- 06/27/05 Bryan Kuntz Created --
4644 -- End of comments --
4645 -----------------------------------------------------------------------------
4646 PROCEDURE Insert_PacSoIssue(
4647 p_api_version IN NUMBER,
4648 p_init_msg_list IN VARCHAR2 := FND_API.G_FALSE,
4649 p_commit IN VARCHAR2 := FND_API.G_FALSE,
4650 p_validation_level IN NUMBER := FND_API.G_VALID_LEVEL_FULL,
4651 x_return_status OUT NOCOPY VARCHAR2,
4652 x_msg_count OUT NOCOPY NUMBER,
4653 x_msg_data OUT NOCOPY VARCHAR2,
4654 p_transaction_id IN NUMBER,
4655 p_layer_id IN NUMBER,
4656 p_cost_type_id IN NUMBER,
4657 p_cost_group_id IN NUMBER,
4658 p_user_id IN NUMBER,
4659 p_login_id IN NUMBER,
4660 p_request_id IN NUMBER,
4661 p_pgm_app_id IN NUMBER,
4662 p_pgm_id IN NUMBER
4663 ) IS
4664
4665 l_api_name CONSTANT VARCHAR2(30) := 'Insert_PacSoIssue';
4666 l_api_version CONSTANT NUMBER := 1.0;
4667 l_stmt_num NUMBER := 0;
4668
4669 l_module CONSTANT VARCHAR2(100) := G_LOG_HEAD ||'.'||l_api_name||'.';
4670 l_unexpLog CONSTANT BOOLEAN := (FND_LOG.LEVEL_UNEXPECTED >= G_LOG_LEVEL) AND FND_LOG.TEST(FND_LOG.LEVEL_UNEXPECTED, l_module);
4671 l_errorLog CONSTANT BOOLEAN := l_unexpLog and (FND_LOG.LEVEL_ERROR >= G_LOG_LEVEL);
4672 l_eventLog CONSTANT BOOLEAN := l_errorLog and (FND_LOG.LEVEL_EVENT >= G_LOG_LEVEL);
4673 l_procLog CONSTANT BOOLEAN := l_eventLog and (FND_LOG.LEVEL_PROCEDURE >= G_LOG_LEVEL);
4674 l_stmtLog CONSTANT BOOLEAN := l_procLog and (FND_LOG.LEVEL_STATEMENT >= G_LOG_LEVEL);
4675
4676 l_cogs_om_line_id NUMBER;
4677
4678 l_mat_cost NUMBER;
4679 l_moh_cost NUMBER;
4680 l_res_cost NUMBER;
4681 l_osp_cost NUMBER;
4682 l_ovh_cost NUMBER;
4683 l_tot_cost NUMBER;
4684 l_cost NUMBER;/*bug 9146453*/
4685
4686 BEGIN
4687
4688 -- Standard start of API savepoint
4689 SAVEPOINT Insert_PacSoIssue_PVT;
4690
4691 l_stmt_num := 0;
4692
4693 IF l_proclog THEN
4694 fnd_log.string(FND_LOG.LEVEL_PROCEDURE,l_module||'.begin',
4695 'Entering '||G_PKG_NAME||'.'||l_api_name||' with '||
4696 'p_api_version = '||p_api_version||','||
4697 'p_transaction_id = '||p_transaction_id||','||
4698 'p_layer_id = '||p_layer_id||','||
4699 'p_cost_type_id = '||p_cost_type_id||','||
4700 'p_cost_group_id = '||p_cost_group_id
4701 );
4702 END IF;
4703
4704 -- Standard call to check for call compatibility
4705 IF NOT FND_API.Compatible_API_Call (
4706 l_api_version,
4707 p_api_version,
4708 l_api_name,
4709 G_PKG_NAME ) THEN
4710 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4711 END IF;
4712
4713 -- Initialize message list if p_init_msg_list is set to TRUE
4714 IF FND_API.to_Boolean(p_init_msg_list) THEN
4715 FND_MSG_PUB.initialize;
4716 END IF;
4717
4718 -- Initialize API return status to success
4719 x_return_status := FND_API.G_RET_STS_SUCCESS;
4720 x_msg_count := 0;
4721 x_msg_data := '';
4722
4723 -- API Body
4724
4725 l_stmt_num := 10;
4726 -- Get the OM Line ID of this sales order issue transaction
4727 -- but only if the event was inserted into the COGS Events
4728 -- table. For example, expense item shipments would not have
4729 -- been inserted into CCE, and should not be inserted into CRCML.
4730 BEGIN
4731 SELECT mmt.trx_source_line_id
4732 INTO l_cogs_om_line_id
4733 FROM mtl_material_transactions mmt,
4737 EXCEPTION
4734 cst_cogs_events cce
4735 WHERE mmt.transaction_id = p_transaction_id
4736 AND cce.mmt_transaction_id = mmt.transaction_id;
4738 WHEN NO_DATA_FOUND THEN
4739 -- Either no records is supposed to be in CCE (undeferred SOs,
4740 -- SOs for expense items), or the transaction has not been
4741 -- costed in perpetual and the Collect Order Management Transaction
4742 -- request has not pick up the transaction. For the latter, it is
4743 -- assummed that PAC process will be run again for this transaction
4744 -- after the transaction is costed (and hence CCE is created) in
4745 -- the future before the PAC period is closed.
4746 IF l_stmtLog THEN
4747 FND_LOG.string(FND_LOG.LEVEL_STATEMENT,l_module||'.10','No COGS Events found for transaction '||to_char(p_transaction_id));
4748 END IF;
4749 END;
4750
4751 l_stmt_num := 20;
4752
4753 /* Removed for Bug 9146453 */
4754
4755 /* SELECT NVL(SUM(DECODE(cost_element_id,1,NVL(actual_cost,0),0)),0),
4756 NVL(SUM(DECODE(cost_element_id,2,NVL(actual_cost,0),0)),0),
4757 NVL(SUM(DECODE(cost_element_id,3,NVL(actual_cost,0),0)),0),
4758 NVL(SUM(DECODE(cost_element_id,4,NVL(actual_cost,0),0)),0),
4759 NVL(SUM(DECODE(cost_element_id,5,NVL(actual_cost,0),0)),0)
4760 INTO l_mat_cost,
4761 l_moh_cost,
4762 l_res_cost,
4763 l_osp_cost,
4764 l_ovh_cost
4765 FROM mtl_pac_actual_cost_details
4766 WHERE transaction_id=p_transaction_id
4767 AND cost_type_id = p_cost_type_id; */
4768
4769 /* Added for Bug 9146453 */
4770 FOR i IN 1..5 LOOP
4771
4772 l_cost := NULL;
4773
4774 SELECT SUM(actual_cost)
4775 INTO l_cost
4776 FROM mtl_pac_actual_cost_details
4777 WHERE transaction_id=p_transaction_id
4778 AND cost_type_id = p_cost_type_id
4779 AND cost_element_id = i;
4780
4781 IF i = 1 THEN
4782 l_mat_cost := l_cost;
4783 ELSIF i = 2 THEN
4784 l_moh_cost := l_cost;
4785 ELSIF i = 3 THEN
4786 l_res_cost := l_cost;
4787 ELSIF i = 4 THEN
4788 l_osp_cost := l_cost;
4789 ELSIF i = 5 THEN
4790 l_ovh_cost := l_cost;
4791 END IF;
4792
4793 END LOOP;
4794
4795 /* Modified for Bug 9146453 */
4796 l_tot_cost := nvl(l_mat_cost,0) + nvl(l_moh_cost,0) + nvl(l_res_cost,0) + nvl(l_osp_cost,0) + nvl(l_ovh_cost,0);
4797
4798 IF l_stmtLog THEN
4799 FND_LOG.string(
4800 FND_LOG.LEVEL_STATEMENT,
4801 l_module||'.20',
4802 'MAT:'||l_mat_cost||' MOH:'||l_moh_cost||' RES:'||l_res_cost||
4803 ' OSP:'||l_osp_cost||' OVH:'||l_ovh_cost
4804 );
4805 END IF;
4806
4807 MERGE
4808 INTO cst_revenue_cogs_match_lines crcml
4809 USING (
4810 SELECT cogs_om_line_id,
4811 p_cost_type_id pac_cost_type_id,
4812 revenue_om_line_id,
4813 deferred_cogs_acct_id,
4814 cogs_acct_id,
4815 organization_id,
4816 inventory_item_id,
4817 operating_unit_id,
4818 ledger_id,
4819 sales_order_issue_date,
4820 original_shipped_qty
4821 FROM cst_revenue_cogs_match_lines
4822 WHERE cogs_om_line_id = l_cogs_om_line_id
4823 AND pac_cost_type_id IS NULL
4824 ) X
4825 ON (
4826 crcml.cogs_om_line_id = X.cogs_om_line_id
4827 AND crcml.pac_cost_type_id = X.pac_cost_type_id
4828 )
4829 WHEN MATCHED THEN
4830 UPDATE
4831 SET unit_material_cost = l_mat_cost, unit_moh_cost = l_moh_cost,
4832 unit_resource_cost = l_res_cost, unit_op_cost = l_osp_cost,
4833 unit_overhead_cost = l_ovh_cost, unit_cost = l_tot_cost,
4834 last_update_date = sysdate, last_updated_by = p_user_id,
4835 last_update_login = p_login_id, request_id = p_request_id,
4836 program_application_id = p_pgm_app_id, program_id = p_pgm_id,
4837 program_update_date = sysdate
4838
4839 WHEN NOT MATCHED THEN
4840 INSERT (
4841 COGS_OM_LINE_ID, PAC_COST_TYPE_ID, REVENUE_OM_LINE_ID,
4842 DEFERRED_COGS_ACCT_ID, COGS_ACCT_ID, ORGANIZATION_ID,
4843 INVENTORY_ITEM_ID, OPERATING_UNIT_ID, COST_GROUP_ID,
4844 LEDGER_ID, SALES_ORDER_ISSUE_DATE, UNIT_MATERIAL_COST,
4845 UNIT_MOH_COST, UNIT_RESOURCE_COST, UNIT_OP_COST,
4846 UNIT_OVERHEAD_COST, UNIT_COST, ORIGINAL_SHIPPED_QTY,
4847 LAST_UPDATE_DATE, LAST_UPDATED_BY, CREATION_DATE,
4848 CREATED_BY, LAST_UPDATE_LOGIN, REQUEST_ID,
4849 PROGRAM_APPLICATION_ID, PROGRAM_ID, PROGRAM_UPDATE_DATE
4850 )
4851 VALUES (
4852 X.cogs_om_line_id, X.pac_cost_type_id, X.revenue_om_line_id,
4853 X.deferred_cogs_acct_id,X.cogs_acct_id, X.organization_id,
4854 X.inventory_item_id, X.operating_unit_id, p_cost_group_id,
4855 X.ledger_id, X.sales_order_issue_date,l_mat_cost,
4856 l_moh_cost, l_res_cost, l_osp_cost,
4857 l_ovh_cost, l_tot_cost, X.original_shipped_qty,
4858 sysdate, p_user_id, sysdate,
4859 p_user_id, p_login_id, p_request_id,
4860 p_pgm_app_id, p_pgm_id, sysdate
4861 );
4862
4863 -- End API Body
4864
4868 'x_return_status = '||x_return_status
4865 IF l_procLog THEN
4866 fnd_log.string(FND_LOG.LEVEL_PROCEDURE,l_module||'.end',
4867 'Exiting '||G_PKG_NAME||'.'||l_api_name||' with '||
4869 );
4870 END IF;
4871
4872 FND_MSG_PUB.count_and_get
4873 ( p_count => x_msg_count
4874 , p_data => x_msg_data
4875 , p_encoded => FND_API.g_false
4876 );
4877
4878
4879
4880 EXCEPTION
4881
4882 WHEN OTHERS THEN
4883 ROLLBACK TO Insert_PacSoIssue_PVT;
4884 x_return_status := fnd_api.g_ret_sts_unexp_error ;
4885
4886 IF l_unexpLog THEN
4887 FND_LOG.string(FND_LOG.LEVEL_UNEXPECTED,G_LOG_HEAD || '.'||l_api_name||'.'||l_stmt_num
4888 ,'Insert_PacSoIssue ('||to_char(l_stmt_num)||') : '||substr(SQLERRM,1,200));
4889 END IF;
4890
4891 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
4892 FND_MESSAGE.set_name('BOM', 'CST_UNEXP_ERROR');
4893 FND_MESSAGE.set_token('PACKAGE', G_PKG_NAME);
4894 FND_MESSAGE.set_token('PROCEDURE',l_api_name);
4895 FND_MESSAGE.set_token('STATEMENT',to_char(l_stmt_num));
4896 FND_MSG_PUB.ADD;
4897 END IF;
4898
4899 FND_MSG_PUB.count_and_get
4900 ( p_count => x_msg_count
4901 , p_data => x_msg_data
4902 , p_encoded => FND_API.g_false
4903 );
4904
4905 END Insert_PacSoIssue;
4906
4907 -----------------------------------------------------------------------------
4908 -- Start of comments --
4909 -- --
4910 -- PROCEDURE --
4911 -- Process_PacRmaReceipt The PAC equivalent of Process_OneRmaRecipt() --
4912 -- This procedure creates the distributions for RMAs that refer --
4913 -- to an original sales order for which COGS was deferred. It --
4914 -- creates credits to Deferred COGS and COGS as appropriate. --
4915 -- --
4916 -- VERSION 1.0 --
4917 -- --
4918 -- PARAMETERS --
4919 -- P_AE_TXN_REC Transaction Record used throughout PAC processor --
4920 -- P_AE_CURR_REC Currency Record used throughout PAC processor --
4921 -- P_DR_FLAG Debit = True / Credit = False --
4922 -- P_COGS_OM_LINE_ID OM Line ID of the sales order to which this RMA refers
4923 -- L_AE_LINE_TBL Table where the distributions are built --
4924 -- --
4925 -- HISTORY: --
4926 -- 06/28/05 Bryan Kuntz Created --
4927 -- End of comments --
4928 -----------------------------------------------------------------------------
4929 PROCEDURE Process_PacRmaReceipt(
4930 p_ae_txn_rec IN CSTPALTY.cst_ae_txn_rec_type,
4931 p_ae_curr_rec IN CSTPALTY.cst_ae_curr_rec_type,
4932 p_dr_flag IN BOOLEAN,
4933 p_cogs_om_line_id IN NUMBER,
4934 l_ae_line_tbl IN OUT NOCOPY CSTPALTY.cst_ae_line_tbl_type,
4935 x_ae_err_rec OUT NOCOPY CSTPALTY.cst_ae_err_rec_type
4936 ) IS
4937
4938 l_api_name CONSTANT VARCHAR2(30) := 'Process_PacRmaReceipt';
4939 l_api_version CONSTANT NUMBER := 1.0;
4940 l_stmt_num NUMBER := 0;
4941 process_error EXCEPTION;
4942
4943 l_module CONSTANT VARCHAR2(100) := G_LOG_HEAD ||'.'||l_api_name||'.';
4944 l_unexpLog CONSTANT BOOLEAN := (FND_LOG.LEVEL_UNEXPECTED >= G_LOG_LEVEL) AND FND_LOG.TEST(FND_LOG.LEVEL_UNEXPECTED, l_module);
4945 l_errorLog CONSTANT BOOLEAN := l_unexpLog and (FND_LOG.LEVEL_ERROR >= G_LOG_LEVEL);
4946 l_eventLog CONSTANT BOOLEAN := l_errorLog and (FND_LOG.LEVEL_EVENT >= G_LOG_LEVEL);
4947 l_procLog CONSTANT BOOLEAN := l_eventLog and (FND_LOG.LEVEL_PROCEDURE >= G_LOG_LEVEL);
4948 l_stmtLog CONSTANT BOOLEAN := l_procLog and (FND_LOG.LEVEL_STATEMENT >= G_LOG_LEVEL);
4949
4950 l_ae_line_rec CSTPALTY.cst_ae_line_rec_type;
4951 l_err_rec CSTPALTY.cst_ae_err_rec_type;
4952
4953 l_elemental_cost number_table;
4954 l_def_cogs_acct_id NUMBER;
4955 l_cogs_acct_id NUMBER;
4956
4957 BEGIN
4958
4959 -- Standard start of API savepoint
4960 SAVEPOINT Process_PacRmaReceipt_PVT;
4961
4962 l_stmt_num := 0;
4963
4964 IF l_proclog THEN
4965 fnd_log.string(FND_LOG.LEVEL_PROCEDURE,l_module||'.begin',
4966 'Entering '||G_PKG_NAME||'.'||l_api_name||' with '||
4967 'p_cogs_om_line_id = '||p_cogs_om_line_id
4968 );
4969 END IF;
4970
4971 -- API Body
4972
4973 l_stmt_num := 10;
4974 /* Get the unit elemental costs and accounts for this sales order line ID */
4975 SELECT unit_material_cost,
4976 unit_moh_cost,
4977 unit_resource_cost,
4978 unit_op_cost,
4979 unit_overhead_cost,
4980 deferred_cogs_acct_id,
4981 cogs_acct_id
4982 INTO l_elemental_cost(1),
4983 l_elemental_cost(2),
4984 l_elemental_cost(3),
4985 l_elemental_cost(4),
4986 l_elemental_cost(5),
4987 l_def_cogs_acct_id,
4988 l_cogs_acct_id
4989 FROM cst_revenue_cogs_match_lines crcml
4990 WHERE cogs_om_line_id = p_cogs_om_line_id
4994 -- Create distributions for each cost element
4991 AND pac_cost_type_id = p_ae_txn_rec.cost_type_id;
4992
4993
4995 FOR i IN 1..5 LOOP
4996
4997 /* Added for Bug 9146453 */
4998 IF l_elemental_cost(i) IS NOT NULL THEN
4999
5000 -- COGS distribution = elemental cost * quantity * cogs percentage
5001
5002 l_ae_line_rec.transaction_value := abs(l_elemental_cost(i) * p_ae_txn_rec.primary_quantity * Nvl(p_ae_txn_rec.cogs_percentage,0)); /* Modified for Bug 9146453 */
5003
5004 IF l_stmtLog THEN
5005 FND_LOG.string(FND_LOG.LEVEL_STATEMENT,l_module||'.40','Cost Element '||to_char(i)||' COGS Credit Amount = '||to_char(l_ae_line_rec.transaction_value));
5006 END IF;
5007 /* Bug# 9499282 */
5008 IF (l_ae_line_rec.transaction_value <> 0 OR Nvl(p_ae_txn_rec.cogs_percentage,0) > 0) THEN
5009 -- Create COGS distribution
5010
5011 l_ae_line_rec.account := l_cogs_acct_id;
5012 l_ae_line_rec.resource_id := NULL;
5013 l_ae_line_rec.cost_element_id := i;
5014 l_ae_line_rec.ae_line_type := 35;
5015
5016 CSTPAPBR.insert_account (p_ae_txn_rec,
5017 p_ae_curr_rec,
5018 p_dr_flag,
5019 l_ae_line_rec,
5020 l_ae_line_tbl,
5021 l_err_rec);
5022 -- check error
5023 if(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) then
5024 raise process_error;
5025 end if;
5026
5027 END IF;
5028
5029 -- Deferred COGS distribution = (elemental cost * quantity) - COGS distribution
5030 l_ae_line_rec.transaction_value := abs(l_elemental_cost(i) * p_ae_txn_rec.primary_quantity) - l_ae_line_rec.transaction_value;
5031
5032 IF l_stmtLog THEN
5033 FND_LOG.string(FND_LOG.LEVEL_STATEMENT,l_module||'.40','Cost Element '||to_char(i)||' Deferred COGS Credit Amount = '||to_char(l_ae_line_rec.transaction_value));
5034 END IF;
5035
5036 /* Modified for Bug 9146453 */
5037 /* Bug# 9499282 */
5038 IF (l_ae_line_rec.transaction_value <> 0 OR Nvl(p_ae_txn_rec.cogs_percentage,0) < 1) THEN
5039 -- Create Deferred COGS distribution
5040
5041 l_ae_line_rec.account := l_def_cogs_acct_id;
5042 l_ae_line_rec.resource_id := NULL;
5043 l_ae_line_rec.cost_element_id := i;
5044 l_ae_line_rec.ae_line_type := 36;
5045
5046 CSTPAPBR.insert_account (p_ae_txn_rec,
5047 p_ae_curr_rec,
5048 p_dr_flag,
5049 l_ae_line_rec,
5050 l_ae_line_tbl,
5051 l_err_rec);
5052 -- check error
5053 if(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) then
5054 raise process_error;
5055 end if;
5056
5057 END IF;
5058 END IF; --l_elemental_cost(i) IS NOT NULL
5059
5060 END LOOP;
5061
5062 -- End API Body
5063
5064 IF l_proclog THEN
5065 fnd_log.string(FND_LOG.LEVEL_PROCEDURE,l_module||'.end',
5066 'Exiting '||G_PKG_NAME||'.'||l_api_name
5067 );
5068 END IF;
5069
5070 EXCEPTION
5071
5072 when process_error then
5073 ROLLBACK TO Process_PacRmaReceipt_PVT;
5074 x_ae_err_rec.l_err_num := l_err_rec.l_err_num;
5075 x_ae_err_rec.l_err_code := l_err_rec.l_err_code;
5076 x_ae_err_rec.l_err_msg := l_err_rec.l_err_msg;
5077
5078 IF l_errorLog THEN
5079 FND_LOG.string(FND_LOG.LEVEL_ERROR,G_LOG_HEAD || '.'||l_api_name||'.'||l_stmt_num
5080 ,'Process_PacRmaReceipt ('||to_char(l_stmt_num)||') : '||substr(l_err_rec.l_err_msg,1,200));
5081 END IF;
5082
5083 when others then
5084 ROLLBACK TO Process_PacRmaReceipt_PVT;
5085 x_ae_err_rec.l_err_num := SQLCODE;
5086 x_ae_err_rec.l_err_code := '';
5087 x_ae_err_rec.l_err_msg := 'CSTPAPBR.Process_PacRmaReceipt()' ||
5088 to_char(l_stmt_num) || substr(SQLERRM,1,180);
5089
5090 IF l_unexpLog THEN
5091 FND_LOG.string(FND_LOG.LEVEL_UNEXPECTED,G_LOG_HEAD || '.'||l_api_name||'.'||l_stmt_num
5092 ,'Process_PacRmaReceipt ('||to_char(l_stmt_num)||') : '||substr(SQLERRM,1,180));
5093 END IF;
5094
5095 END Process_PacRmaReceipt;
5096
5097 -----------------------------------------------------------------------------
5098 -- Start of comments --
5099 -- --
5100 -- PROCEDURE --
5101 -- Process_PacCogsRecTxn PAC equivalent of Process_CogsRecognitionTxn() --
5102 -- This procedure is called from the PAC distribution processor --
5103 -- to create the accounting entries for COGS Recognition events --
5104 -- --
5105 -- VERSION 1.0 --
5106 -- --
5107 -- PARAMETERS --
5108 -- P_AE_TXN_REC Transaction Record used throughout PAC processor --
5109 -- P_AE_CURR_REC Currency Record used throughout PAC processor --
5110 -- L_AE_LINE_TBL Table where the distributions are built --
5111 -- --
5112 -- HISTORY: --
5113 -- 06/28/05 Bryan Kuntz Created --
5114 -- End of comments --
5115 -----------------------------------------------------------------------------
5116 PROCEDURE Process_PacCogsRecTxn(
5120 x_ae_err_rec OUT NOCOPY CSTPALTY.cst_ae_err_rec_type
5117 p_ae_txn_rec IN CSTPALTY.cst_ae_txn_rec_type,
5118 p_ae_curr_rec IN CSTPALTY.cst_ae_curr_rec_type,
5119 l_ae_line_tbl IN OUT NOCOPY CSTPALTY.cst_ae_line_tbl_type,
5121 ) IS
5122
5123 l_api_name CONSTANT VARCHAR2(30) := 'Process_PacCogsRecTxn';
5124 l_api_version CONSTANT NUMBER := 1.0;
5125 l_stmt_num NUMBER := 0;
5126 process_error EXCEPTION;
5127
5128 l_module CONSTANT VARCHAR2(100) := G_LOG_HEAD ||'.'||l_api_name||'.';
5129 l_unexpLog CONSTANT BOOLEAN := (FND_LOG.LEVEL_UNEXPECTED >= G_LOG_LEVEL) AND FND_LOG.TEST(FND_LOG.LEVEL_UNEXPECTED, l_module);
5130 l_errorLog CONSTANT BOOLEAN := l_unexpLog and (FND_LOG.LEVEL_ERROR >= G_LOG_LEVEL);
5131 l_eventLog CONSTANT BOOLEAN := l_errorLog and (FND_LOG.LEVEL_EVENT >= G_LOG_LEVEL);
5132 l_procLog CONSTANT BOOLEAN := l_eventLog and (FND_LOG.LEVEL_PROCEDURE >= G_LOG_LEVEL);
5133 l_stmtLog CONSTANT BOOLEAN := l_procLog and (FND_LOG.LEVEL_STATEMENT >= G_LOG_LEVEL);
5134
5135 l_ae_line_rec CSTPALTY.cst_ae_line_rec_type;
5136 l_err_rec CSTPALTY.cst_ae_err_rec_type;
5137 l_dr_flag BOOLEAN;
5138
5139 l_elemental_cost number_table;
5140 l_def_cogs_acct_id NUMBER;
5141 l_cogs_acct_id NUMBER;
5142 l_new_percentage NUMBER;
5143 l_prior_percentage NUMBER;
5144
5145
5146
5147 BEGIN
5148
5149 -- Standard start of API savepoint
5150 SAVEPOINT Process_PacCogsRecTxn_PVT;
5151
5152 l_stmt_num := 0;
5153
5154 IF l_proclog THEN
5155 fnd_log.string(FND_LOG.LEVEL_PROCEDURE,l_module||'.begin',
5156 'Entering '||G_PKG_NAME||'.'||l_api_name
5157 );
5158 END IF;
5159
5160 -- API Body
5161
5162 l_stmt_num := 10;
5163 -- Get the unit elemental costs and accounts for this sales order line ID
5164 SELECT unit_material_cost,
5165 unit_moh_cost,
5166 unit_resource_cost,
5167 unit_op_cost,
5168 unit_overhead_cost,
5169 deferred_cogs_acct_id,
5170 cogs_acct_id
5171 INTO l_elemental_cost(1),
5172 l_elemental_cost(2),
5173 l_elemental_cost(3),
5174 l_elemental_cost(4),
5175 l_elemental_cost(5),
5176 l_def_cogs_acct_id,
5177 l_cogs_acct_id
5178 FROM cst_revenue_cogs_match_lines crcml
5179 WHERE cogs_om_line_id = p_ae_txn_rec.om_line_id
5180 AND pac_cost_type_id = p_ae_txn_rec.cost_type_id;
5181
5182 l_stmt_num := 20;
5183 -- Get the COGS percentage and prior COGS percentage for this event
5184 SELECT cogs_percentage,
5185 prior_cogs_percentage
5186 INTO l_new_percentage,
5187 l_prior_percentage
5188 FROM cst_cogs_events
5189 WHERE mmt_transaction_id = p_ae_txn_rec.transaction_id;
5190
5191
5192 -- Loop for each cost element
5193 FOR i IN 1..5 LOOP
5194 /* Added Nvl for Bug 9146453 */
5195 l_ae_line_rec.transaction_value := Nvl(l_elemental_cost(i),0) * p_ae_txn_rec.primary_quantity * (l_new_percentage - l_prior_percentage);
5196
5197 IF l_stmtLog THEN
5198 FND_LOG.string(FND_LOG.LEVEL_STATEMENT,l_module||'.20','Cost Element '||to_char(i)||' COGS Adjustment Amount = '||to_char(l_ae_line_rec.transaction_value));
5199 END IF;
5200
5201 IF (l_ae_line_rec.transaction_value <> 0) THEN
5202 -- Create COGS distribution (debit if percentage increase, credit otherwise)
5203
5204 l_ae_line_rec.account := l_cogs_acct_id;
5205 l_ae_line_rec.resource_id := NULL;
5206 l_ae_line_rec.cost_element_id := i;
5207 l_ae_line_rec.ae_line_type := 35;
5208
5209 l_dr_flag := (sign(l_ae_line_rec.transaction_value) > 0);
5210 l_ae_line_rec.transaction_value := abs(l_ae_line_rec.transaction_value);
5211
5212 CSTPAPBR.insert_account (p_ae_txn_rec,
5213 p_ae_curr_rec,
5214 l_dr_flag,
5215 l_ae_line_rec,
5216 l_ae_line_tbl,
5217 l_err_rec);
5218 -- check error
5219 if(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) then
5220 raise process_error;
5221 end if;
5222
5223 -- Create Deferred COGS distribution (credit if percentage increase, debit otherwise)
5224
5225 l_ae_line_rec.account := l_def_cogs_acct_id;
5226 l_ae_line_rec.ae_line_type := 36;
5227 l_dr_flag := not l_dr_flag;
5228
5229 CSTPAPBR.insert_account (p_ae_txn_rec,
5230 p_ae_curr_rec,
5231 l_dr_flag,
5232 l_ae_line_rec,
5233 l_ae_line_tbl,
5234 l_err_rec);
5235 -- check error
5236 if(l_err_rec.l_err_num<>0 and l_err_rec.l_err_num is not null) then
5237 raise process_error;
5238 end if;
5239
5240 END IF;
5241
5242 END LOOP;
5243
5244
5245 -- End API Body
5246
5247 IF l_proclog THEN
5248 fnd_log.string(FND_LOG.LEVEL_PROCEDURE,l_module||'.end',
5249 'Exiting '||G_PKG_NAME||'.'||l_api_name
5250 );
5251 END IF;
5252
5253
5254 EXCEPTION
5255
5256 when process_error then
5257 ROLLBACK TO Process_PacCogsRecTxn_PVT;
5258 x_ae_err_rec.l_err_num := l_err_rec.l_err_num;
5259 x_ae_err_rec.l_err_code := l_err_rec.l_err_code;
5260 x_ae_err_rec.l_err_msg := l_err_rec.l_err_msg;
5261
5262 IF l_errorLog THEN
5266
5263 FND_LOG.string(FND_LOG.LEVEL_ERROR,G_LOG_HEAD || '.'||l_api_name||'.'||l_stmt_num
5264 ,'Process_PacCogsRecTxn ('||to_char(l_stmt_num)||') : '||substr(l_err_rec.l_err_msg,1,200));
5265 END IF;
5267 when others then
5268 ROLLBACK TO Process_PacCogsRecTxn_PVT;
5269 x_ae_err_rec.l_err_num := SQLCODE;
5270 x_ae_err_rec.l_err_code := '';
5271 x_ae_err_rec.l_err_msg := 'CSTPAPBR.Process_PacCogsRecTxn(' ||
5272 to_char(l_stmt_num)||') -' || substr(SQLERRM,1,180);
5273
5274 IF l_unexpLog THEN
5275 FND_LOG.string(FND_LOG.LEVEL_UNEXPECTED,G_LOG_HEAD || '.'||l_api_name||'.'||l_stmt_num
5276 ,'Process_PacCogsRecTxn ('||to_char(l_stmt_num)||') : '||substr(SQLERRM,1,180));
5277 END IF;
5278
5279 END Process_PacCogsRecTxn;
5280
5281 -----------------------------------------------------------------------------
5282 -- Start of comments --
5283 -- --
5284 -- PROCEDURE --
5285 -- Print_MessageStack --
5286 -- This procedure is called from Match_RevenueCogs() to spit out --
5287 -- the contents of the message stack to the log file. --
5288 -- --
5289 -- VERSION 1.0 --
5290 -- --
5291 -- PARAMETERS --
5292 -- none --
5293 -- --
5294 -- HISTORY: --
5295 -- 05/17/05 Bryan Kuntz Created --
5296 -- End of comments --
5297 -----------------------------------------------------------------------------
5298 procedure Print_MessageStack IS
5299 l_msg_count NUMBER;
5300 l_msg_data VARCHAR2(8000);
5301 BEGIN
5302 FND_MSG_PUB.count_and_get
5303 ( p_count => l_msg_count,
5304 p_data => l_msg_data,
5305 p_encoded => FND_API.g_false
5306 );
5307 IF (l_msg_count > 1) THEN
5308 FOR i IN 1..l_msg_count LOOP
5309 l_msg_data := FND_MSG_PUB.get(i, FND_API.g_false);
5310 FND_FILE.PUT_LINE(FND_FILE.LOG, l_msg_data);
5311 END LOOP;
5312 ELSIF (l_msg_count = 1) THEN
5313 FND_FILE.PUT_LINE(FND_FILE.LOG, l_msg_data);
5314 END IF;
5315
5316 -- Reinitialize the message list
5317 FND_MSG_PUB.initialize;
5318
5319 END Print_MessageStack;
5320
5321 /*===========================================================================*/
5322 -- API name : Generate_DefCOGSXml
5323 -- Type : Private
5324 -- Function : Generate XML Data for Deferred COGS Report
5325 -- Report
5326 -- Pre-reqs : None.
5327 -- Parameters :
5328 -- in : p_cost_method in number
5329 -- : p_ledger_id in number (Only used in perpetual)
5330 -- : p_pac_legal_entity in number (Only used in PAC)
5331 -- : p_pac_cost_type in number (Only used in PAC)
5332 -- : p_pac_cost_group in number (Only used in PAC)
5333 -- : p_period_name in varchar2
5334 -- : p_sales_order_date_low in varchar2
5335 -- : p_sales_order_date_high in varchar2
5336 -- : p_all_lines in varchar2
5337 -- : p_api_version in number
5338 -- : p_amt_tolerance in number
5339 --
5340 -- out :
5341 -- : errcode OUT varchar2
5342 -- : errno OUT number
5343 --
5344 -- Version : Current version 1.0
5345 -- : Initial version 1.0
5346 -- History : 6/24/2005 David Gottlieb Created
5347 -- Notes : This Procedure is called by the Deferred COGS Report
5348 -- This is the wrapper procedure that calls the other
5349 -- procedures to generate XML data according to report parameters.
5350 -- End of comments
5351 /*===========================================================================*/
5352
5353 procedure Generate_DefCOGSXml (
5354 errcode out nocopy varchar2,
5355 err_code out nocopy number,
5356 p_cost_method in number,
5357 p_ledger_id in number,
5358 p_pac_legal_entity in number,
5359 p_pac_cost_type in number,
5360 p_pac_cost_group in number,
5361 p_period_name in varchar2,
5362 p_sales_order_date_low in varchar2,
5363 p_sales_order_date_high in varchar2,
5364 p_all_lines in varchar2,
5365 p_api_version in number,
5366 p_amt_tolerance in number DEFAULT 0) is
5367
5368 l_operating_unit number;
5369 l_sales_order_date_low date;
5370 l_sales_order_date_high date;
5371 l_qryCtx number;
5372 l_ref_cur sys_refcursor;
5373 l_xml_doc clob;
5374 l_amount number;
5378
5375 l_offset number;
5376 l_buffer varchar2(32767);
5377 l_length number;
5379 l_api_name constant varchar2(100) := 'Generate_DefCOGSXml';
5380 l_api_version constant number := 1.0;
5381
5382 l_return_status varchar2(1);
5383 l_msg_count number;
5384 l_msg_data varchar2(2000);
5385 l_stmt_num number;
5386 l_success boolean;
5387
5388 l_full_name constant varchar2(2000) := G_PKG_NAME || '.' || l_api_name;
5389 l_module constant varchar2(2000) := 'cst.plsql.' || l_full_name;
5390 l_uLog constant boolean := (fnd_log.LEVEL_UNEXPECTED >= G_LOG_LEVEL) AND
5391 fnd_log.test(fnd_log.LEVEL_UNEXPECTED, l_module);
5392 l_errorLog constant boolean := l_uLog and (fnd_log.LEVEL_ERROR >= G_LOG_LEVEL);
5393 l_eventLog constant boolean := l_errorLog and (fnd_log.LEVEL_EVENT >= G_LOG_LEVEL);
5394 l_pLog constant boolean := l_eventLog and (fnd_log.LEVEL_PROCEDURE >= G_LOG_LEVEL);
5395 /*Bug 7305146*/
5396 l_encoding VARCHAR2(20);
5397 l_xml_header VARCHAR2(100);
5398
5399 begin
5400
5401 EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_NUMERIC_CHARACTERS=''.,''';
5402
5403 -- Initialize variables
5404
5405 l_amount := 16383;
5406 l_offset := 1;
5407 l_return_status := fnd_api.g_ret_sts_success;
5408 l_msg_count := 0;
5409 if(p_cost_method = 1) then
5410 l_operating_unit := mo_global.get_current_org_id;
5411 else
5412 l_operating_unit := -1; /* Operating Unit not used in PAC */
5413 end if; /* p_cost_method = 1 */
5414
5415 -- Write the module name and user parameters to fnd log file
5416
5417 --IF l_pLog THEN
5418 -- FND_LOG.string(FND_LOG.LEVEL_PROCEDURE,
5419 debug('Generate_DefCOGSXml+');
5420 debug('p_cost_method = '||p_cost_method);
5421 debug('p_ledger_id = '||p_ledger_id);
5422 debug('l_operating_unit ='||l_operating_unit);
5423 debug('p_pac_legal_entity = '||p_pac_legal_entity);
5424 debug('p_pac_cost_type = '||p_pac_cost_type);
5425 debug('p_pac_cost_group = '||p_pac_cost_group);
5426 debug('p_period_name = '||p_period_name);
5427 debug('p_sales_order_date_low = '||p_sales_order_date_low);
5428 debug('p_sales_order_date_high = '||p_sales_order_date_high);
5429 debug('p_all_lines = '||p_all_lines);
5430 debug('p_api_version = '||p_api_version );
5431 debug('p_amt_tolerance = '||p_amt_tolerance);
5432 -- END IF;
5433
5434 -- Initialze variables for storing XML Data
5435
5436 dbms_lob.createtemporary(l_xml_doc, TRUE);
5437
5438 /*Bug 7305146*/
5439 l_encoding := fnd_profile.value('ICX_CLIENT_IANA_ENCODING');
5440 l_xml_header := '<?xml version="1.0" encoding="'|| l_encoding ||'"?>';
5441 DBMS_LOB.writeappend (l_xml_doc, length(l_xml_header), l_xml_header);
5442
5443 dbms_lob.writeappend (l_xml_doc, 8, '<REPORT>');
5444
5445 -- Initialize message stack
5446
5447 fnd_msg_pub.initialize;
5448
5449 -- Standard call to get message count and if count is 1, get message info.
5450
5451 fnd_msg_pub.Count_And_Get
5452 ( p_count => l_msg_count,
5453 p_data => l_msg_data
5454 );
5455
5456 -- Standard call to check the API version
5457
5458 if(not fnd_api.Compatible_API_Call( l_api_version,
5459 p_api_version,
5460 l_api_name,
5461 G_PKG_NAME)) then
5462 raise fnd_api.G_EXC_UNEXPECTED_ERROR;
5463 end if;
5464
5465 l_stmt_num := 10;
5466
5467 --set the date parameters if the user has not
5468
5469 if(p_sales_order_date_low is not null) then
5470 l_sales_order_date_low := trunc(fnd_date.canonical_to_date(p_sales_order_date_low));
5471 else
5472 l_sales_order_date_low := sysdate - 7200;
5473 end if;
5474
5475 if(p_sales_order_date_high is not null) then
5476 l_sales_order_date_high := trunc(fnd_date.canonical_to_date(p_sales_order_date_high));
5477 else
5478 l_sales_order_date_high := sysdate + 1;
5479 end if;
5480
5481 /*========================================================================*/
5482 -- Call to Procedure Add Parameters. To Add user entered Parameters to
5483 -- XML data
5484 /*========================================================================*/
5485
5486 Add_Parameters ( p_api_version => 1.0,
5487 p_init_msg_list => fnd_api.G_FALSE,
5488 p_validation_level => fnd_api.G_VALID_LEVEL_FULL,
5489 x_return_status => l_return_status,
5490 x_msg_count => l_msg_count,
5491 x_msg_data => l_msg_data,
5492 i_cost_method => p_cost_method,
5493 i_operating_unit => l_operating_unit,
5494 i_ledger_id => p_ledger_id,
5495 i_pac_legal_entity => p_pac_legal_entity,
5496 i_pac_cost_type => p_pac_cost_type,
5497 i_pac_cost_group => p_pac_cost_group,
5498 i_period_name => p_period_name,
5499 i_sales_order_date_low => p_sales_order_date_low,
5500 i_sales_order_date_high => p_sales_order_date_high,
5501 i_all_lines => p_all_lines,
5502 i_amt_tolerance => p_amt_tolerance,
5506
5503 x_xml_doc => l_xml_doc);
5504
5505 -- Standard call to check the return status from API called
5507 if(l_return_status <> fnd_api.G_RET_STS_SUCCESS) then
5508 raise fnd_api.G_EXC_UNEXPECTED_ERROR;
5509 end if;
5510
5511
5512 l_stmt_num := 20;
5513
5514 /*========================================================================*/
5515 -- Call to Procedure Add Parameters. To Add AP and PO data to XML data
5516 /*========================================================================*/
5517
5518 Add_DefCOGSData (p_api_version => 1.0,
5519 p_init_msg_list => FND_API.G_FALSE,
5520 p_validation_level => FND_API.G_VALID_LEVEL_FULL,
5521 x_return_status => l_return_status,
5522 x_msg_count => l_msg_count,
5523 x_msg_data => l_msg_data,
5524 i_cost_method => p_cost_method,
5525 i_operating_unit => l_operating_unit,
5526 i_ledger_id => p_ledger_id,
5527 i_pac_legal_entity => p_pac_legal_entity,
5528 i_pac_cost_type => p_pac_cost_type,
5529 i_pac_cost_group => p_pac_cost_group,
5530 i_period_name => p_period_name,
5531 i_sales_order_date_low => l_sales_order_date_low,
5532 i_sales_order_date_high => l_sales_order_date_high,
5533 i_all_lines => p_all_lines,
5534 i_amt_tolerance => p_amt_tolerance,
5535 x_xml_doc => l_xml_doc);
5536
5537 -- Standard call to check the return status from API called
5538
5539 if(l_return_status <> fnd_api.G_RET_STS_SUCCESS) then
5540 raise fnd_api.G_EXC_UNEXPECTED_ERROR;
5541 end if;
5542
5543 -- write the closing tag to the XML data
5544
5545 dbms_lob.writeappend (l_xml_doc, 9, '</REPORT>');
5546
5547 -- write xml data to the output file
5548
5549 l_length := nvl(dbms_lob.getlength(l_xml_doc),0);
5550 loop
5551 exit when l_length <= 0;
5552 dbms_lob.read (l_xml_doc, l_amount, l_offset, l_buffer);
5553 fnd_file.put(fnd_file.output, l_buffer);
5554 l_length := l_length - l_amount;
5555 l_offset := l_offset + l_amount;
5556 end loop;
5557
5558 dbms_xmlgen.closeContext(l_qryCtx);
5559
5560 -- Write the event log to fnd log file
5561
5562 debug(l_stmt_num||'-Completed writing to output file');
5563
5564 -- free temporary memory
5565
5566 dbms_lob.freetemporary(l_xml_doc);
5567
5568 l_success := fnd_concurrent.set_completion_status('NORMAL', 'Request Completed Successfully');
5569
5570 -- Write the module name to fnd log file
5571
5572 debug('errcode = '||errcode);
5573 debug('err_code = '||err_code);
5574 debug('Generate_DefCOGSXml-');
5575
5576 exception
5577 when fnd_api.G_EXC_ERROR then
5578 l_return_status := fnd_api.G_RET_STS_ERROR ;
5579 fnd_msg_pub.Count_And_Get
5580 ( p_count => l_msg_count,
5581 p_data => l_msg_data
5582 );
5583
5584 when fnd_api.G_EXC_UNEXPECTED_ERROR then
5585 fnd_msg_pub.Count_And_Get
5586 ( p_count => l_msg_count,
5587 p_data => l_msg_data
5588 );
5589
5590 cst_utility_pub.writelogmessages
5591 ( p_api_version => 1.0,
5592 p_msg_count => l_msg_count,
5593 p_msg_data => l_msg_data,
5594 x_return_status => l_return_status
5595 );
5596
5597 l_msg_data := substrb(SQLERRM,1,240);
5598 l_success := fnd_concurrent.set_completion_status('ERROR', l_msg_data);
5599
5600 when others then
5601 if(fnd_log.LEVEL_UNEXPECTED >= G_LOG_LEVEL) then
5602 fnd_log.string(fnd_log.LEVEL_UNEXPECTED,
5603 l_module || '.' || l_stmt_num,
5604 substrb(SQLERRM , 1 , 240));
5605 end if;
5606
5607 if(fnd_msg_pub.Check_Msg_Level(fnd_msg_pub.G_MSG_LVL_UNEXP_ERROR)) then
5608 fnd_msg_pub.Add_Exc_Msg(G_PKG_NAME,l_api_name);
5609 end if;
5610
5611 fnd_msg_pub.Count_And_Get
5612 ( p_count => l_msg_count,
5613 p_data => l_msg_data
5614 );
5615
5616 cst_utility_pub.writelogmessages
5617 ( p_api_version => 1.0,
5618 p_msg_count => l_msg_count,
5619 p_msg_data => l_msg_data,
5620 x_return_status => l_return_status
5621 );
5622
5623 l_msg_data := substrb(SQLERRM,1,240);
5624 l_success := fnd_concurrent.set_completion_status('ERROR', l_msg_data);
5625
5626 end Generate_DefCOGSXml;
5627
5628
5629 /*===========================================================================*/
5630 -- API name : add_parameters
5631 -- Type : Private
5632 -- Function : Generate XML data for Parameters and append it to
5633 -- output
5634 -- Pre-reqs : None.
5635 -- Parameters :
5636 -- in : p_api_version in number
5637 -- : p_init_msg_list in varchar2
5638 -- : p_validation_level in number
5639 -- : p_cost_method in number
5640 -- : p_operating_unit in number
5641 -- : p_ledger_id in number
5642 -- : p_pac_legal_entity in number
5646 -- : p_sales_order_date_low in varchar2
5643 -- : p_pac_cost_type in number
5644 -- : p_pac_cost_group in number
5645 -- : p_period_name in varchar2
5647 -- : p_sales_order_date_high in varchar2
5648 -- : p_all_lines in varchar2
5649 -- : i_amt_tolerance in number
5650 --
5651 -- out :
5652 -- : x_return_status out nocopy varchar2
5653 -- : x_msg_count out nocopy number
5654 -- : x_msg_data out nocopy varchar2
5655 --
5656 -- in out :
5657 -- : x_xml_doc in out nocopy clob
5658 --
5659 -- Version : Current version 1.0
5660 -- : Initial version 1.0
5661 -- History : 6/24/2005 David Gottlieb Created
5662 -- Notes : This Procedure is called by Generate_DefCOSXml
5663 -- procedure. The procedure generates XML data for the
5664 -- report parameters and appends it to the report
5665 -- output.
5666 -- End of comments
5667 /*===========================================================================*/
5668 procedure Add_Parameters (
5669 p_api_version in number,
5670 p_init_msg_list in varchar2,
5671 p_validation_level in number,
5672 x_return_status out nocopy varchar2,
5673 x_msg_count out nocopy number,
5674 x_msg_data out nocopy varchar2,
5675 i_cost_method in number,
5676 i_operating_unit in number,
5677 i_ledger_id in number,
5678 i_pac_legal_entity in number,
5679 i_pac_cost_type in number,
5680 i_pac_cost_group in number,
5681 i_period_name in varchar2,
5682 i_sales_order_date_low in varchar2,
5683 i_sales_order_date_high in varchar2,
5684 i_all_lines in varchar2,
5685 i_amt_tolerance in number,
5686 x_xml_doc in out nocopy clob) is
5687
5688 l_api_name constant varchar2(30) := 'ADD_PARAMETERS';
5689 l_api_version constant number := 1.0;
5690 l_full_name constant varchar2(2000) := G_PKG_NAME || '.' || l_api_name;
5691 l_module constant varchar2(2000) := 'cst.plsql.' || l_full_name;
5692
5693 l_ref_cur sys_refcursor;
5694 l_qryCtx number;
5695 l_xml_temp clob;
5696 l_age_option number;
5697 l_offset pls_integer;
5698 l_org_code CST_ORGANIZATION_DEFINITIONS.ORGANIZATION_CODE%TYPE;
5699 l_cost_type CST_COST_TYPES.COST_TYPE%TYPE;
5700 l_cost_group CST_COST_GROUPS.COST_GROUP%TYPE;
5701 l_period_name CST_PAC_PERIODS.PERIOD_NAME%TYPE;
5702 l_meaning FND_LOOKUPS.MEANING%TYPE;
5703 l_stmt_num number;
5704
5705 l_uLog constant boolean := (fnd_log.LEVEL_UNEXPECTED >= G_LOG_LEVEL) AND fnd_log.test(fnd_log.LEVEL_UNEXPECTED, l_module);
5706 l_errorLog constant boolean := l_uLog and (fnd_log.LEVEL_ERROR >= G_LOG_LEVEL);
5707 l_eventLog constant boolean := l_errorLog and (fnd_log.LEVEL_EVENT >= G_LOG_LEVEL);
5708 l_pLog constant boolean := l_eventLog and (fnd_log.LEVEL_PROCEDURE >= G_LOG_LEVEL);
5709
5710 begin
5711
5712 -- Write the module name to fnd log file
5713
5714 --IF l_pLog THEN
5715 --FND_LOG.string(FND_LOG.LEVEL_PROCEDURE,l_module||'.begin',
5716 debug('Add_Parameters+');
5717 debug('i_cost_method = '||i_cost_method);
5718 debug('i_operating_unit = '||i_operating_unit);
5719 debug('i_ledger_id = '||i_ledger_id);
5720 debug('i_pac_legal_entity = '||i_pac_legal_entity);
5721 debug('i_pac_cost_type = '||i_pac_cost_type);
5722 debug('i_pac_cost_group = '||i_pac_cost_group);
5723 debug('i_period_name = '||i_period_name);
5724 debug('i_sales_order_date_low = '||i_sales_order_date_low);
5725 debug('i_sales_order_date_high = '||i_sales_order_date_high);
5726 debug('i_all_lines = '||i_all_lines);
5727 debug('i_amt_tolerance = '||i_amt_tolerance);
5728
5729
5730 -- Standard call to check for call compatibility.
5731
5732 if(not fnd_api.Compatible_API_Call ( l_api_version,
5733 p_api_version,
5734 l_api_name,
5735 G_PKG_NAME )) then
5736 raise fnd_api.G_EXC_UNEXPECTED_ERROR;
5737 end if;
5738
5739 -- Initialize message list if p_init_msg_list is set to TRUE.
5740
5741 if(fnd_api.to_boolean( p_init_msg_list )) then
5742 fnd_msg_pub.initialize;
5743 end if;
5744
5745 -- Initialize API return status to success
5746
5747 x_return_status := fnd_api.G_RET_STS_SUCCESS;
5748
5749 -- Initialize temporary variable to hold xml data
5750
5751 dbms_lob.createtemporary(l_xml_temp, TRUE);
5752 l_offset := 21;
5753
5754 -- Get the proile value to determine the aging basis
5755
5756 fnd_profile.get('CST_ACCRUAL_AGE_IN_DAYS', l_age_option);
5757
5758 l_stmt_num := 10;
5759
5760 -- Open Ref Cursor to collect the report parameters
5761
5762 /* Perpetual */
5763 if(i_cost_method = 1) then
5764 open l_ref_cur for 'select :i_cost_method cost_method,
5765 haou.name operating_unit,
5766 gsb.name ledger,
5770 fl.meaning all_lines,
5767 :i_period_name period_name,
5768 :i_sales_order_date_low sales_order_date_from,
5769 :i_sales_order_date_high sales_order_date_to,
5771 :i_amt_tolerance tolerance_amount
5772 from hr_all_organization_units haou,
5773 gl_sets_of_books gsb,
5774 fnd_lookups fl
5775 where haou.organization_id = :i_operating_unit
5776 and gsb.set_of_books_id = :i_ledger_id
5777 and fl.lookup_code = :i_all_lines
5778 and fl.lookup_type = ''YES_NO'''
5779 using i_cost_method,
5780 i_period_name,
5781 i_sales_order_date_low,
5782 i_sales_order_date_high,
5783 i_amt_tolerance,
5784 i_operating_unit,
5785 i_ledger_id,
5786 i_all_lines;
5787
5788 /* Periodic */
5789 elsif(i_cost_method = 3) then
5790 SELECT cost_type
5791 INTO l_cost_type
5792 FROM cst_cost_types
5793 WHERE cost_type_id = i_pac_cost_type;
5794
5795 SELECT cost_group
5796 INTO l_cost_group
5797 FROM cst_cost_groups
5798 WHERE cost_group_id = i_pac_cost_group;
5799
5800 SELECT period_name
5801 INTO l_period_name
5802 FROM cst_pac_periods
5803 WHERE pac_period_id = TO_NUMBER(i_period_name);
5804
5805 SELECT meaning
5806 INTO l_meaning
5807 FROM fnd_lookups
5808 WHERE lookup_type = 'YES_NO'
5809 AND lookup_code = i_all_lines;
5810
5811 open l_ref_cur for 'select :i_cost_method cost_method,
5812 xle.name legal_entity,
5813 :l_cost_type cost_type,
5814 :l_cost_group cost_group,
5815 :l_period_name period_name,
5816 :i_sales_order_date_low sales_order_date_from,
5817 :i_sales_order_date_high sales_order_date_to,
5818 :l_meaning all_lines
5819 from xle_firstparty_information_v xle
5820 where xle.legal_entity_id = :i_pac_legal_entity'
5821 using i_cost_method,
5822 l_cost_type,
5823 l_cost_group,
5824 l_period_name,
5825 i_sales_order_date_low,
5826 i_sales_order_date_high,
5827 l_meaning,
5828 i_pac_legal_entity;
5829
5830 end if; /* p_cost_method = 1, p_cost_method = 3*/
5831
5832 -- create new context
5833
5834 l_stmt_num := 20;
5835
5836 l_qryCtx := DBMS_XMLGEN.newContext (l_ref_cur);
5837 dbms_xmlgen.setRowSetTag (l_qryCtx,'PARAMETERS');
5838 dbms_xmlgen.setRowTag (l_qryCtx,NULL);
5839
5840 l_stmt_num := 30;
5841
5842 -- get XML into the temporary clob variable
5843
5844 dbms_xmlgen.getXML (l_qryCtx, l_xml_temp, dbms_xmlgen.none);
5845
5846 -- remove the header (21 characters) and append the rest to xml output
5847
5848 if(dbms_xmlgen.getNumRowsProcessed(l_qryCtx) > 0) then
5849 dbms_lob.erase (l_xml_temp, l_offset,1);
5850 dbms_lob.append (x_xml_doc, l_xml_temp);
5851 end if;
5852
5853 -- close context and free memory
5854
5855 dbms_xmlgen.closeContext(l_qryCtx);
5856 close l_ref_cur;
5857
5858 dbms_lob.freetemporary(l_xml_temp);
5859
5860 -- Standard call to get message count and if count is 1, get message info.
5861
5862 fnd_msg_pub.Count_And_Get
5863 ( p_count => x_msg_count,
5864 p_data => x_msg_data
5865 );
5866
5867 -- Write the module name to fnd log file
5868
5869 debug( 'x_return_status = '||x_return_status );
5870 debug( 'x_msg_data ='||x_msg_data);
5871 debug('Add_Parameters-');
5872
5873 exception
5874 when fnd_api.G_EXC_ERROR then
5875 x_return_status := fnd_api.G_RET_STS_ERROR;
5876 fnd_msg_pub.Count_And_Get
5877 ( p_count => x_msg_count,
5878 p_data => x_msg_data
5879 );
5880
5881 when fnd_api.G_EXC_UNEXPECTED_ERROR then
5882 x_return_status := fnd_api.G_RET_STS_UNEXP_ERROR;
5883 fnd_msg_pub.Count_And_Get
5884 ( p_count => x_msg_count,
5885 p_data => x_msg_data
5886 );
5887
5888 when others then
5889 x_return_status := fnd_api.G_RET_STS_UNEXP_ERROR;
5890 if(fnd_log.LEVEL_UNEXPECTED >= G_LOG_LEVEL) then
5891 fnd_log.string(fnd_log.LEVEL_UNEXPECTED,
5892 l_module || '.' || l_stmt_num,
5893 substrb(SQLERRM , 1 , 240));
5894 end if;
5895
5896 if(fnd_msg_pub.Check_Msg_Level(fnd_msg_pub.G_MSG_LVL_UNEXP_ERROR)) then
5897 fnd_msg_pub.Add_Exc_Msg
5898 ( G_PKG_NAME,
5899 l_api_name
5900 );
5901 end if;
5902
5903 fnd_msg_pub.Count_And_Get
5904 ( p_count => x_msg_count,
5908 end Add_Parameters;
5905 p_data => x_msg_data
5906 );
5907
5909
5910
5911 /*===========================================================================*/
5912 -- API name : Add_DefCOGSData
5913 -- Type : Private
5914 -- Function : Generate XML data from sql query and append it to
5915 -- output
5916 -- Pre-reqs : None.
5917 -- Parameters :
5918 -- in : p_api_version in number
5919 -- : p_init_msg_list in varchar2
5920 -- : p_validation_level in number
5921 -- : i_cost_method in number
5922 -- : i_operating_unit in number
5923 -- : i_ledger_id in number
5924 -- : i_pac_legal_entity in number
5925 -- : i_pac_cost_type in number
5926 -- : i_pac_cost_group in number
5927 -- : i_period_name in varchar2
5928 -- : i_sales_order_date_low in date
5929 -- : i_sales_order_date_high in date
5930 -- : i_all_lines in varchar2
5931 -- : i_amt_tolerance in number
5932 --
5933 -- out : x_return_status out nocopy varchar2
5934 -- : x_msg_count out nocopy number
5935 -- : x_msg_data out nocopy varchar2
5936 --
5937 -- in out : x_xml_doc in out nocopy clob
5938 --
5939 -- Version : Current version 1.0
5940 -- : Initial version 1.0
5941 -- History : 6/24/2005 David Gottlieb Created
5942 -- Notes : This Procedure is called by Generate_DefCOGSXml
5943 -- procedure. The procedure generates XML data from
5944 -- sql query and appends it to the report output.
5945 -- End of comments
5946 /*===========================================================================*/
5947 procedure Add_DefCOGSData (
5948 p_api_version in number,
5949 p_init_msg_list in varchar2,
5950 p_validation_level in number,
5951 x_return_status out nocopy varchar2,
5952 x_msg_count out nocopy number,
5953 x_msg_data out nocopy varchar2,
5954 i_cost_method in number,
5955 i_operating_unit in number,
5956 i_ledger_id in number,
5957 i_pac_legal_entity in number,
5958 i_pac_cost_type in number,
5959 i_pac_cost_group in number,
5960 i_period_name in varchar2,
5961 i_sales_order_date_low in date,
5962 i_sales_order_date_high in date,
5963 i_all_lines in varchar2,
5964 i_amt_tolerance in number,
5965 x_xml_doc in out nocopy clob) is
5966
5967 l_api_name constant varchar2(100) := 'ADD_DEFCOGSDATA';
5968 l_api_version constant number := 1.0;
5969
5970 l_ref_cur sys_refcursor;
5971 l_qryCtx number;
5972 l_xml_temp clob;
5973 l_offset pls_integer;
5974 l_count number;
5975 l_stmt_num number;
5976 l_dummy_date date;
5977 l_date_offset number;
5978
5979 l_full_name constant varchar2(2000) := G_PKG_NAME || '.' || l_api_name;
5980 l_module constant varchar2(2000) := 'cst.plsql.' || l_full_name;
5981 l_uLog constant boolean := (fnd_log.LEVEL_UNEXPECTED >= G_LOG_LEVEL) AND fnd_log.test(fnd_log.LEVEL_UNEXPECTED, l_module);
5982 l_errorLog constant boolean := l_uLog AND (fnd_log.LEVEL_ERROR >= G_LOG_LEVEL);
5983 l_eventLog constant boolean := l_errorLog and (fnd_log.LEVEL_EVENT >= G_LOG_LEVEL);
5984 l_pLog constant boolean := l_eventLog and (fnd_log.LEVEL_PROCEDURE >= G_LOG_LEVEL);
5985
5986 begin
5987
5988 -- Write the module name to fnd log file
5989
5990 --IF l_pLog THEN
5991 --FND_LOG.string(FND_LOG.LEVEL_PROCEDURE,l_module||'.begin',
5992 debug('Add_DefCOGSData+');
5993 debug('i_cost_method = '||i_cost_method);
5994 debug('i_operating_unit = '||i_operating_unit);
5995 debug('i_ledger_id = '||i_ledger_id);
5996 debug('i_pac_legal_entity = '||i_pac_legal_entity);
5997 debug('i_pac_cost_type = '||i_pac_cost_type);
5998 debug('i_pac_cost_group = '||i_pac_cost_group);
5999 debug('i_period_name = '||i_period_name);
6000 debug('i_sales_order_date_low = '||to_char(i_sales_order_date_low,'DD-MON-YYYY HH24:MI:SS'));
6001 debug('i_sales_order_date_high = '||to_char(i_sales_order_date_high,'DD-MON-YYYY HH24:MI:SS'));
6002 debug('i_all_lines = '||i_all_lines);
6003 debug('i_amt_tolerance = '||i_amt_tolerance);
6004
6005 -- Standard call to check for call compatibility.
6006
6007 if(not fnd_api.Compatible_API_Call( l_api_version,
6008 p_api_version,
6009 l_api_name,
6010 G_PKG_NAME)) then
6011 raise fnd_api.G_EXC_UNEXPECTED_ERROR;
6012 end if;
6013
6014 -- Initialize message list if p_init_msg_list is set to TRUE.
6015
6016 if(fnd_api.to_boolean(p_init_msg_list)) then
6017 fnd_msg_pub.initialize;
6018 end if;
6019
6020 -- Initialize API return status to success
6021
6022 x_return_status := fnd_api.G_RET_STS_SUCCESS;
6023
6027 l_offset := 21;
6024 -- Initialize temporary variable to hold xml data
6025
6026 dbms_lob.createtemporary(l_xml_temp, TRUE);
6028
6029 -- open ref cur to fetch Deferred COGS data
6030
6031 l_stmt_num := 10;
6032
6033 if(i_cost_method = 1) then
6034 ----------------------------------------------------------------------------------------------
6035 -- Perpetual report
6036 ----------------------------------------------------------------------------------------------
6037
6038 l_dummy_date := SYSDATE;
6039 l_date_offset := inv_le_timezone_pub.get_le_day_time_for_ou(
6040 l_dummy_date,
6041 i_operating_unit
6042 ) - l_dummy_date;
6043
6044 open l_ref_cur for '
6045 with Z AS
6046 (
6047 SELECT /* index(srclines RA_CUSTOMER_TRX_LINES_N9) LEADING (Q, srclines) use_nl(Q, srclines) */
6048 Q.ORDER_NUMBER order_number,
6049 Q.booked_date /* maybe ordered_date? */ order_date,
6050 (select substrb(PARTY.PARTY_NAME,1,50)
6051 from HZ_PARTIES party
6052 where CUST_ACCT.PARTY_ID = PARTY.PARTY_ID) customer_name,
6053 Q.transactional_curr_code currency,
6054 Q.line_number sales_order_line,
6055 Q.REVENUE_OM_LINE_ID sales_order_line_id,
6056 Q.ITEM_TYPE_CODE item_type_code,
6057 msi.concatenated_segments item,
6058 srclines.line_number invoice_line,
6059 srclines.customer_trx_line_id,
6060 srclines.customer_trx_id,
6061 -------------------------
6062 sum(cce1.EVENT_QUANTITY) total_line_quantity,
6063 Q.COGS_BALANCE Earned_COGS,
6064 Q2.COGS_BALANCE Total_Earned_COGS,
6065 Q.DEF_COGS_BALANCE Deferred_COGS,
6066 Q2.DEF_COGS_BALANCE Total_Deferred_COGS,
6067 cogs_acct.concatenated_segments COGS_account,
6068 dcogs_acct.concatenated_segments Deferred_COGS_account
6069 FROM
6070 (
6071 SELECT /*+ leading(crcml), index(crcml CST_REV_COGS_MATCH_LINES_N2) */
6072 OOH.ORDER_NUMBER,
6073 OOH.BOOKED_DATE,
6074 OOH.transactional_curr_code,
6075 OOL.LINE_NUMBER,
6076 OOL.SOLD_TO_ORG_ID,
6077 OOL.ITEM_TYPE_CODE,
6078 CRCML.COGS_OM_LINE_ID,
6079 CRCML.REVENUE_OM_LINE_ID,
6080 CRCML.ORGANIZATION_ID,
6081 CRCML.inventory_item_id,
6082 CRCML.UNIT_COST,
6083 CRCML.COGS_ACCT_ID,
6084 CRCML.DEFERRED_COGS_ACCT_ID,
6085 sum(decode(mta.accounting_line_type, 35, MTA.BASE_TRANSACTION_VALUE,0)) COGS_BALANCE,
6086 sum(decode(mta.accounting_line_type, 36, MTA.BASE_TRANSACTION_VALUE,0)) DEF_COGS_BALANCE
6087 FROM
6088 CST_REVENUE_COGS_MATCH_LINES CRCML,
6089 CST_COGS_EVENTS CCE,
6090 GL_PERIOD_STATUSES GPS,
6091 OE_ORDER_LINES_ALL OOL,
6092 OE_ORDER_HEADERS_ALL OOH,
6093 MTL_TRANSACTION_ACCOUNTS MTA
6094 WHERE
6095 CRCML.SALES_ORDER_ISSUE_DATE BETWEEN :i_sales_order_date_low AND :i_sales_order_date_high
6096 AND CRCML.OPERATING_UNIT_ID = :i_operating_unit
6097 AND CRCML.PAC_COST_TYPE_ID IS NULL
6098 AND GPS.APPLICATION_ID = 101
6099 AND GPS.SET_OF_BOOKS_ID = :i_ledger_id
6100 AND GPS.PERIOD_NAME = :i_period_name
6101 AND CCE.EVENT_DATE <= GPS.END_DATE + (1-1/86400) - :l_date_offset
6102 AND CCE.COGS_OM_LINE_ID = CRCML.COGS_OM_LINE_ID
6103 AND OOL.HEADER_ID = OOH.HEADER_ID
6104 AND OOL.LINE_ID = CRCML.COGS_OM_LINE_ID
6105 AND mta.transaction_id (+) = cce.mmt_transaction_id
6106 GROUP BY
6107 OOH.ORDER_NUMBER,
6108 OOH.BOOKED_DATE,
6109 OOH.transactional_curr_code,
6110 OOL.LINE_NUMBER,
6111 OOL.SOLD_TO_ORG_ID,
6112 OOL.ITEM_TYPE_CODE,
6113 CRCML.COGS_OM_LINE_ID,
6114 CRCML.REVENUE_OM_LINE_ID,
6115 CRCML.ORGANIZATION_ID,
6116 CRCML.inventory_item_id,
6117 CRCML.UNIT_COST,
6118 CRCML.COGS_ACCT_ID,
6119 CRCML.DEFERRED_COGS_ACCT_ID
6120 ) Q,
6121 (
6122 SELECT /*+ leading(crcml), index(crcml CST_REV_COGS_MATCH_LINES_N2) */
6123 OOH.ORDER_NUMBER,
6124 OOH.BOOKED_DATE,
6125 OOH.transactional_curr_code,
6126 OOL.LINE_NUMBER,
6127 OOL.SOLD_TO_ORG_ID,
6128 OOL.ITEM_TYPE_CODE,
6129 CRCML.REVENUE_OM_LINE_ID,
6130 CRCML.ORGANIZATION_ID,
6131 CRCML.COGS_ACCT_ID,
6132 CRCML.DEFERRED_COGS_ACCT_ID,
6133 sum(decode(mta.accounting_line_type, 35, MTA.BASE_TRANSACTION_VALUE,0)) COGS_BALANCE,
6134 sum(decode(mta.accounting_line_type, 36, MTA.BASE_TRANSACTION_VALUE,0)) DEF_COGS_BALANCE
6135 FROM
6136 CST_REVENUE_COGS_MATCH_LINES CRCML,
6137 CST_COGS_EVENTS CCE,
6138 GL_PERIOD_STATUSES GPS,
6139 OE_ORDER_LINES_ALL OOL,
6140 OE_ORDER_HEADERS_ALL OOH,
6141 MTL_TRANSACTION_ACCOUNTS MTA
6142 WHERE
6143 CRCML.SALES_ORDER_ISSUE_DATE BETWEEN :i_sales_order_date_low AND :i_sales_order_date_high
6144 AND CRCML.OPERATING_UNIT_ID = :i_operating_unit
6145 AND CRCML.PAC_COST_TYPE_ID IS NULL
6149 AND CCE.EVENT_DATE <= GPS.END_DATE + (1-1/86400) - :l_date_offset
6146 AND GPS.APPLICATION_ID = 101
6147 AND GPS.SET_OF_BOOKS_ID = :i_ledger_id
6148 AND GPS.PERIOD_NAME = :i_period_name
6150 AND CCE.COGS_OM_LINE_ID = CRCML.COGS_OM_LINE_ID
6151 AND OOL.HEADER_ID = OOH.HEADER_ID
6152 AND OOL.LINE_ID = CRCML.COGS_OM_LINE_ID
6153 AND mta.transaction_id (+) = cce.mmt_transaction_id
6154 GROUP BY
6155 OOH.ORDER_NUMBER,
6156 OOH.BOOKED_DATE,
6157 OOH.transactional_curr_code,
6158 OOL.LINE_NUMBER,
6159 OOL.SOLD_TO_ORG_ID,
6160 OOL.ITEM_TYPE_CODE,
6161 CRCML.REVENUE_OM_LINE_ID,
6162 CRCML.ORGANIZATION_ID,
6163 CRCML.COGS_ACCT_ID,
6164 CRCML.DEFERRED_COGS_ACCT_ID
6165 ) Q2,
6166 MTL_SYSTEM_ITEMS_KFV MSI,
6167 gl_code_combinations_kfv cogs_acct,
6168 gl_code_combinations_kfv dcogs_acct,
6169 ra_customer_trx_lines_all srclines,
6170 HZ_CUST_ACCOUNTS cust_acct,
6171 cst_cogs_events cce1
6172 WHERE
6173 MSI.INVENTORY_ITEM_ID = Q.INVENTORY_ITEM_ID
6174 AND MSI.ORGANIZATION_ID = Q.ORGANIZATION_ID
6175 AND Q2.ORGANIZATION_ID = Q.ORGANIZATION_ID
6176 AND cogs_acct.code_combination_id = Q.cogs_acct_id
6177 AND Q2.cogs_acct_id = Q.cogs_acct_id
6178 AND dcogs_acct.code_combination_id = Q.deferred_cogs_acct_id
6179 AND Q2.deferred_cogs_acct_id = Q.deferred_cogs_acct_id
6180 AND Q.sold_to_org_id = cust_acct.CUST_ACCOUNT_ID (+)
6181 AND Q2.sold_to_org_id = Q.sold_to_org_id
6182 AND srclines.line_type (+) = ''LINE''
6183 AND srclines.interface_line_context (+) = ''ORDER ENTRY''
6184 AND srclines.interface_line_attribute6 (+) = to_char(Q.revenue_om_line_id)
6185 AND Q2.revenue_om_line_id = Q.revenue_om_line_id
6186 AND srclines.sales_order (+) = to_char(Q.order_number)
6187 AND Q2.order_number = Q.order_number
6188 AND cce1.cogs_om_line_id = Q.cogs_om_line_id
6189 AND cce1.event_type in (1,2)
6190 GROUP BY
6191 Q.ORDER_NUMBER,
6192 Q.booked_date,
6193 CUST_ACCT.PARTY_ID,
6194 Q.transactional_curr_code,
6195 Q.line_number,
6196 Q.REVENUE_OM_LINE_ID,
6197 Q.ITEM_TYPE_CODE,
6198 msi.concatenated_segments,
6199 srclines.line_number,
6200 srclines.customer_trx_line_id,
6201 srclines.customer_trx_id,
6202 Q.COGS_BALANCE,
6203 Q2.COGS_BALANCE,
6204 Q.DEF_COGS_BALANCE,
6205 Q2.DEF_COGS_BALANCE,
6206 cogs_acct.concatenated_segments,
6207 dcogs_acct.concatenated_segments
6208 )
6209 --Bug 12409271(Added following hint to start execution with Z and use Index join)
6210 SELECT /*+ Leading(Z) INDEX_JOIN(lines RA_CUSTOMER_TRX_LINES_U1 RA_CUSTOMER_TRX_LINES_N1)*/
6211 :i_all_lines all_lines,
6212 Z.order_number order_number,
6213 Z.order_date order_date,
6214 Z.customer_name customer,
6215 Decode(trx.TRX_NUMBER,Min(lines1.TRX_NUMBER),Decode(Z.INVOICE_LINE,Min(lines1.MIN_INV_LINE), Z.currency, NULL), NULL) currency,
6216 Z.sales_order_line order_line,
6217 Z.sales_order_line_id order_line_id,
6218 trx.trx_number invoice_number,
6219 Z.invoice_line invoice_line,
6220 Z.item item_number,
6221 ROUND(DECODE(Z.item_type_code, ''CONFIG'', lines2.total_earned_revenue,
6222 SUM (DECODE(dist.account_class, ''UNEARN'', 0,
6223 ''UNBILL'', 0, dist.acctd_amount ) ) )
6224 *DECODE(Z.earned_cogs, Z.total_earned_cogs,1,Z.earned_cogs/DECODE(Z.total_earned_cogs,0,1,Z.total_earned_cogs)), 2) earned_revenue,
6225 ROUND(DECODE(Z.item_type_code, ''CONFIG'', SUM (DECODE(dist.account_class, ''REV'', 0,
6226 ''UNBILL'', 0, lines2.total_earned_revenue) )
6227 *DECODE(Z.deferred_cogs, 0, 0, Z.total_deferred_cogs,1,Z.deferred_cogs/DECODE(Z.total_deferred_cogs,0,1,Z.total_deferred_cogs)),
6228 SUM (DECODE(dist.account_class, ''REV'', 0,
6229 ''UNBILL'', 0, dist.acctd_amount) )
6230 *DECODE(Z.deferred_cogs, Z.total_deferred_cogs,1,Z.deferred_cogs/DECODE(Z.total_deferred_cogs,0,1,Z.total_deferred_cogs))), 2) unearned_revenue,
6231 ROUND(DECODE(Z.item_type_code, ''CONFIG'', SUM (DECODE(dist.account_class, ''REV'', 0,
6232 ''UNEARN'', 0, lines2.total_earned_revenue) )
6233 *DECODE(Z.deferred_cogs, 0, 0, Z.total_deferred_cogs,1,Z.deferred_cogs/DECODE(Z.total_deferred_cogs,0,1,Z.total_deferred_cogs)),
6234 SUM (DECODE(dist.account_class, ''REV'', 0,
6235 ''UNEARN'', 0, dist.acctd_amount) )
6236 *DECODE(Z.deferred_cogs, Z.total_deferred_cogs,1,Z.deferred_cogs/DECODE(Z.total_deferred_cogs,0,1,Z.total_deferred_cogs))), 2) unbilled_revenue,
6237 Decode(trx.TRX_NUMBER,Min(lines1.TRX_NUMBER),Decode(Z.INVOICE_LINE,Min(lines1.MIN_INV_LINE),Z.total_line_quantity,NULL),NULL) order_quantity,
6238 Decode(trx.TRX_NUMBER,Min(lines1.TRX_NUMBER),Decode(Z.INVOICE_LINE,Min(lines1.MIN_INV_LINE),Z.Earned_COGS,NULL),NULL) earned_cogs,
6239 Decode(trx.TRX_NUMBER,Min(lines1.TRX_NUMBER),Decode(Z.INVOICE_LINE,Min(lines1.MIN_INV_LINE),Z.Deferred_COGS,NULL),NULL) deferred_cogs,
6240 Decode(trx.TRX_NUMBER,Min(lines1.TRX_NUMBER),Decode(Z.INVOICE_LINE,Min(lines1.MIN_INV_LINE),Z.COGS_account,NULL),NULL) cogs_account,
6241 Decode(trx.TRX_NUMBER,Min(lines1.TRX_NUMBER),Decode(Z.INVOICE_LINE,Min(lines1.MIN_INV_LINE),Z.Deferred_COGS_account,NULL),NULL) deferred_cogs_account
6242 FROM
6243 Z,
6244 GL_PERIOD_STATUSES GPS1,
6245 ra_cust_trx_line_gl_dist_all dist,
6246 ra_customer_trx_lines_all lines,
6250 WHERE rctla.INTERFACE_LINE_CONTEXT=''ORDER ENTRY''
6247 ra_customer_trx_all trx,
6248 (SELECT rctla.INTERFACE_LINE_ATTRIBUTE1,rctla.INTERFACE_LINE_ATTRIBUTE6,rcta.TRX_NUMBER,min(rctla.LINE_NUMBER) MIN_INV_LINE
6249 FROM RA_CUSTOMER_TRX_LINES_ALL rctla,RA_CUSTOMER_TRX_ALL rcta,z
6251 AND rctla.LINE_TYPE=''LINE''
6252 AND rctla.CUSTOMER_TRX_ID=rcta.CUSTOMER_TRX_ID
6253 AND RCTLA.SALES_ORDER = to_char(Z.ORDER_NUMBER)
6254 AND rctla.CUSTOMER_TRX_ID=(SELECT Min(rctla1.CUSTOMER_TRX_ID)
6255 FROM RA_CUSTOMER_TRX_LINES_ALL rctla1--,z
6256 WHERE rctla.INTERFACE_LINE_ATTRIBUTE6=rctla1.INTERFACE_LINE_ATTRIBUTE6
6257 AND rctla1.INTERFACE_LINE_CONTEXT=''ORDER ENTRY''
6258 AND RCTLA1.SALES_ORDER = RCTLA.SALES_ORDER --to_char(Z.ORDER_NUMBER)
6259 AND rctla1.LINE_TYPE=''LINE'')
6260 GROUP BY rctla.INTERFACE_LINE_ATTRIBUTE1,rctla.INTERFACE_LINE_ATTRIBUTE6,rcta.TRX_NUMBER) lines1,
6261 (SELECT rcta.CUSTOMER_TRX_ID,sum(rctlgda.ACCTD_AMOUNT) TOTAL_EARNED_REVENUE
6262 FROM RA_CUSTOMER_TRX_LINES_ALL rctla,RA_CUSTOMER_TRX_ALL rcta, RA_CUST_TRX_LINE_GL_DIST_ALL rctlgda,z
6263 WHERE rctla.INTERFACE_LINE_CONTEXT=''ORDER ENTRY''
6264 AND rctla.LINE_TYPE=''LINE''
6265 AND rctla.CUSTOMER_TRX_ID=rcta.CUSTOMER_TRX_ID
6266 AND RCTLA.SALES_ORDER = to_char(Z.ORDER_NUMBER)
6267 AND rctla.CUSTOMER_TRX_ID=(SELECT Min(rctla1.CUSTOMER_TRX_ID)
6268 FROM RA_CUSTOMER_TRX_LINES_ALL rctla1--,z
6269 WHERE rctla.INTERFACE_LINE_ATTRIBUTE6=rctla1.INTERFACE_LINE_ATTRIBUTE6
6270 AND rctla1.INTERFACE_LINE_CONTEXT=''ORDER ENTRY''
6271 AND RCTLA1.SALES_ORDER = RCTLA.SALES_ORDER --to_char(Z.ORDER_NUMBER)
6272 AND rctla1.LINE_TYPE=''LINE'')
6273 AND rctla.CUSTOMER_TRX_ID = rctlgda.CUSTOMER_TRX_ID
6274 AND rctla.customer_trx_line_id = rctlgda.customer_trx_line_id
6275 GROUP BY rcta.CUSTOMER_TRX_ID) lines2
6276 WHERE (Z.customer_trx_line_id = lines.customer_trx_line_id
6277 OR Z.customer_trx_line_id = lines.previous_customer_trx_line_id)
6278 AND Z.customer_trx_id = trx.customer_trx_id
6279 AND GPS1.APPLICATION_ID = 101
6280 AND GPS1.SET_OF_BOOKS_ID = :i_ledger_id
6281 AND GPS1.PERIOD_NAME = :i_period_name
6282 AND dist.customer_trx_line_id = lines.customer_trx_line_id
6283 AND dist.account_set_flag = ''N''
6284 AND dist.account_class IN (''REV'', ''UNEARN'', ''UNBILL'')
6285 AND dist.gl_date <= GPS1.END_DATE + (1-1/86400) -- or AS OF DATE
6286 AND to_char(Z.SALES_ORDER_LINE_ID)=LINES.INTERFACE_LINE_ATTRIBUTE6
6287 AND to_char(Z.SALES_ORDER_LINE_ID)=LINES1.INTERFACE_LINE_ATTRIBUTE6
6288 AND Z.CUSTOMER_TRX_ID=LINES2.CUSTOMER_TRX_ID
6289 GROUP BY
6290 Z.ORDER_NUMBER,
6291 Z.order_date,
6292 Z.customer_name,
6293 Z.currency,
6294 Z.sales_order_line,
6295 Z.sales_order_line_id,
6296 Z.CUSTOMER_TRX_ID,
6297 Z.item_type_code,
6298 lines2.total_earned_revenue,
6299 lines1.INTERFACE_LINE_ATTRIBUTE6,
6300 trx.trx_number,
6301 Z.invoice_line,
6302 Z.item,
6303 Z.total_line_quantity,
6304 Z.Earned_COGS,
6305 Z.Total_Earned_COGS,
6306 Z.Deferred_COGS,
6307 Z.Total_Deferred_COGS,
6308 Z.COGS_account,
6309 Z.Deferred_COGS_account
6310 HAVING :i_all_lines = ''Y''
6311 or ((decode(sum(lines.revenue_amount),0,1,
6312 round(sum(decode(dist.account_class, ''UNEARN'', 0,
6313 ''UNBILL'', 0, dist.amount)) /
6314 (sum(lines.revenue_amount) /
6315 count(dist.cust_trx_line_gl_dist_id)), 3))
6316 <>
6317 decode(z.earned_cogs, 0, decode(z.deferred_cogs,0,1,0),
6318 round((z.earned_cogs /
6319 decode(z.deferred_cogs + z.earned_cogs,0,1,
6320 z.deferred_cogs + z.earned_cogs)), 3)))
6321 AND z.deferred_cogs NOT BETWEEN -1*:i_amt_tolerance AND :i_amt_tolerance
6322 )
6323 UNION
6324 SELECT :i_all_lines all_lines,
6325 Z.order_number order_number,
6326 Z.order_date order_date,
6327 Z.customer_name customer,
6328 Z.currency currency,
6329 Z.sales_order_line order_line,
6330 Z.sales_order_line_id order_line_id,
6331 NULL invoice_number,
6332 NULL invoice_line,
6333 Z.item item_number,
6334 NULL earned_revenue,
6335 NULL unearned_revenue,
6336 NULL unbilled_revenue,
6337 Z.total_line_quantity order_quantity,
6338 Z.Earned_COGS earned_cogs,
6339 Z.Deferred_COGS deferred_cogs,
6340 Z.COGS_account cogs_account,
6341 Z.Deferred_COGS_account deferred_cogs_account
6342 FROM
6343 Z
6344 WHERE (Z.customer_trx_line_id IS NULL
6345 OR Z.customer_trx_id IS NULL)
6346 AND (:i_all_lines = ''Y''
6347 OR z.deferred_cogs NOT BETWEEN -1*:i_amt_tolerance AND :i_amt_tolerance)
6348 UNION
6349 SELECT :i_all_lines all_lines,
6350 Z.order_number order_number,
6351 Z.order_date order_date,
6352 Z.customer_name customer,
6353 Z.currency currency,
6354 Z.sales_order_line order_line,
6355 Z.sales_order_line_id order_line_id,
6356 NULL invoice_number,
6360 NULL unearned_revenue,
6357 NULL invoice_line,
6358 Z.item item_number,
6359 NULL earned_revenue,
6361 NULL unbilled_revenue,
6362 Z.total_line_quantity order_quantity,
6363 Z.Earned_COGS earned_cogs,
6364 Z.Deferred_COGS deferred_cogs,
6365 Z.COGS_account cogs_account,
6366 Z.Deferred_COGS_account deferred_cogs_account
6367 FROM
6368 Z
6369 WHERE (Z.customer_trx_line_id IS NOT NULL AND Z.customer_trx_id IS NOT NULL)
6370 AND NOT EXISTS
6371 (SELECT NULL
6372 FROM ra_customer_trx_lines_all ctl
6373 , ra_cust_trx_line_gl_dist_all ctlgd
6374 , gl_period_statuses g2
6375 WHERE (Z.customer_trx_line_id = ctl.customer_trx_line_id
6376 OR Z.customer_trx_line_id = ctl.previous_customer_trx_line_id)
6377 AND g2.application_id = 101
6378 AND g2.set_of_books_id = :i_ledger_id
6379 AND g2.period_name = :i_period_name
6380 AND ctl.customer_trx_line_id = ctlgd.customer_trx_line_id
6381 AND ctlgd.account_set_flag = ''N''
6382 AND ctlgd.account_class IN (''REV'', ''UNEARN'', ''UNBILL'')
6383 AND ctlgd.gl_date <= g2.END_DATE + (1-1/86400))
6384 AND (:i_all_lines = ''Y''
6385 OR z.deferred_cogs NOT BETWEEN -1*:i_amt_tolerance AND :i_amt_tolerance)
6386 ORDER BY order_number'
6387 using i_sales_order_date_low,
6388 i_sales_order_date_high,
6389 i_operating_unit,
6390 i_ledger_id,
6391 i_period_name,
6392 l_date_offset,
6393 i_sales_order_date_low,
6394 i_sales_order_date_high,
6395 i_operating_unit,
6396 i_ledger_id,
6397 i_period_name,
6398 l_date_offset,
6399 i_all_lines,
6400 i_ledger_id,
6401 i_period_name,
6402 i_all_lines,
6403 i_amt_tolerance,
6404 i_amt_tolerance,
6405 i_all_lines,
6406 i_all_lines,
6407 i_amt_tolerance,
6408 i_amt_tolerance,
6409 i_all_lines,
6410 i_ledger_id,
6411 i_period_name,
6412 i_all_lines,
6413 i_amt_tolerance,
6414 i_amt_tolerance;
6415
6416 elsif(i_cost_method = 3) then
6417 ----------------------------------------------------------------------------------------------
6418 -- PAC report
6419 ----------------------------------------------------------------------------------------------
6420 l_dummy_date := SYSDATE;
6421
6422 SELECT inv_le_timezone_pub.get_server_day_time_for_le(
6423 l_dummy_date,
6424 legal_entity
6425 ) - l_dummy_date
6426 INTO l_date_offset
6427 FROM cst_cost_groups
6428 WHERE cost_group_id = i_pac_cost_group;
6429
6430 open l_ref_cur for 'with Z AS
6431 (
6432 SELECT /* index(srclines RA_CUSTOMER_TRX_LINES_N9) LEADING (Q, srclines) use_nl(Q, srclines) */
6433 Q.ORDER_NUMBER order_number,
6434 Q.booked_date /* maybe ordered_date? */ order_date,
6435 (select substrb(PARTY.PARTY_NAME,1,50)
6436 from HZ_PARTIES party
6437 where CUST_ACCT.PARTY_ID = PARTY.PARTY_ID) customer_name,
6438 Q.transactional_curr_code currency,
6439 Q.line_number sales_order_line,
6440 msi.concatenated_segments item,
6441 srclines.line_number invoice_line,
6442 srclines.customer_trx_line_id,
6443 srclines.customer_trx_id,
6444 -------------------------
6445 sum(cce1.EVENT_QUANTITY) total_line_quantity,
6446 Q.COGS_BALANCE Earned_COGS,
6447 Q.DEF_COGS_BALANCE Deferred_COGS,
6448 cogs_acct.concatenated_segments COGS_account,
6449 dcogs_acct.concatenated_segments Deferred_COGS_account
6450 FROM
6451 (
6452 SELECT /* LEADING(CCGA) */
6453 OOH.ORDER_NUMBER,
6454 OOH.BOOKED_DATE,
6455 OOH.transactional_curr_code,
6456 OOL.LINE_NUMBER,
6457 OOL.SOLD_TO_ORG_ID,
6458 CRCML.COGS_OM_LINE_ID,
6459 CRCML.REVENUE_OM_LINE_ID,
6460 CRCML.ORGANIZATION_ID,
6461 CRCML.inventory_item_id,
6462 CRCML.UNIT_COST,
6463 CRCML.COGS_ACCT_ID,
6464 CRCML.DEFERRED_COGS_ACCT_ID,
6465 sum(decode(CAL.ae_line_type_code, 35, nvl(CAL.accounted_dr,0) - nvl(CAL.accounted_cr,0),0)) COGS_BALANCE,
6466 sum(decode(CAL.ae_line_type_code, 36, nvl(CAL.accounted_dr,0) - nvl(CAL.accounted_cr,0),0)) DEF_COGS_BALANCE
6467 FROM
6468 CST_REVENUE_COGS_MATCH_LINES CRCML,
6469 CST_COGS_EVENTS CCE,
6470 OE_ORDER_LINES_ALL OOL,
6471 OE_ORDER_HEADERS_ALL OOH,
6472 CST_COST_GROUP_ASSIGNMENTS CCGA,
6473 CST_PAC_PERIODS CPP,
6474 CST_AE_HEADERS CAH,
6475 CST_AE_LINES CAL
6476 WHERE
6477 CRCML.SALES_ORDER_ISSUE_DATE BETWEEN :i_sales_order_date_low AND :i_sales_order_date_high
6478 AND CRCML.ORGANIZATION_ID = CCGA.ORGANIZATION_ID
6479 AND CRCML.PAC_COST_TYPE_ID IS NULL -- Want to pick up all events, not just PAC-costed events
6480 AND CCGA.COST_GROUP_ID = :i_pac_cost_group
6481 AND CPP.PAC_PERIOD_ID = TO_NUMBER(:i_period_name)
6482 AND CCE.EVENT_DATE <= CPP.PERIOD_END_DATE + (1-1/86400) + :l_date_offset
6486 AND CCE.MMT_TRANSACTION_ID = CAH.ACCOUNTING_EVENT_ID (+)
6483 AND CCE.COGS_OM_LINE_ID = CRCML.COGS_OM_LINE_ID
6484 AND OOL.HEADER_ID = OOH.HEADER_ID
6485 AND OOL.LINE_ID = CRCML.COGS_OM_LINE_ID
6487 AND CAH.AE_HEADER_ID = CAL.AE_HEADER_ID (+)
6488 GROUP BY
6489 OOH.ORDER_NUMBER,
6490 OOH.BOOKED_DATE,
6491 OOH.transactional_curr_code,
6492 OOL.LINE_NUMBER,
6493 OOL.SOLD_TO_ORG_ID,
6494 CRCML.COGS_OM_LINE_ID,
6495 CRCML.REVENUE_OM_LINE_ID,
6496 CRCML.ORGANIZATION_ID,
6497 CRCML.inventory_item_id,
6498 CRCML.UNIT_COST,
6499 CRCML.COGS_ACCT_ID,
6500 CRCML.DEFERRED_COGS_ACCT_ID
6501 ) Q,
6502 MTL_SYSTEM_ITEMS_KFV MSI,
6503 gl_code_combinations_kfv cogs_acct,
6504 gl_code_combinations_kfv dcogs_acct,
6505 ra_customer_trx_lines_all srclines,
6506 HZ_CUST_ACCOUNTS cust_acct,
6507 cst_cogs_events cce1,
6508 CST_PAC_PERIODS CPP
6509 WHERE
6510 MSI.INVENTORY_ITEM_ID = Q.INVENTORY_ITEM_ID
6511 AND MSI.ORGANIZATION_ID = Q.ORGANIZATION_ID
6512 AND cogs_acct.code_combination_id = Q.cogs_acct_id
6513 AND dcogs_acct.code_combination_id = Q.deferred_cogs_acct_id
6514 AND Q.sold_to_org_id = cust_acct.CUST_ACCOUNT_ID (+)
6515 AND srclines.line_type (+) = ''LINE''
6516 AND srclines.interface_line_context (+) = ''ORDER ENTRY''
6517 AND srclines.interface_line_attribute6 (+) = to_char(Q.revenue_om_line_id)
6518 AND srclines.sales_order (+) = to_char(Q.order_number)
6519 AND cce1.cogs_om_line_id = Q.cogs_om_line_id
6520 AND CPP.PAC_PERIOD_ID = TO_NUMBER(:i_period_name)
6521 AND CCE1.EVENT_DATE <= CPP.PERIOD_END_DATE + (1-1/86400) + :l_date_offset
6522 AND cce1.event_type in (1,2)
6523 GROUP BY
6524 Q.ORDER_NUMBER,
6525 Q.booked_date,
6526 CUST_ACCT.PARTY_ID,
6527 Q.transactional_curr_code,
6528 Q.line_number,
6529 msi.concatenated_segments,
6530 srclines.line_number,
6531 srclines.customer_trx_line_id,
6532 srclines.customer_trx_id,
6533 Q.COGS_BALANCE,
6534 Q.DEF_COGS_BALANCE,
6535 cogs_acct.concatenated_segments,
6536 dcogs_acct.concatenated_segments
6537 )
6538 SELECT :i_all_lines all_lines,
6539 Z.order_number order_number,
6540 Z.order_date order_date,
6541 Z.customer_name customer,
6542 Z.currency currency,
6543 Z.sales_order_line order_line,
6544 trx.trx_number invoice_number,
6545 Z.invoice_line invoice_line,
6546 Z.item item_number,
6547 ROUND(SUM ( DECODE(dist.account_class, ''UNEARN'', 0,
6548 ''UNBILL'', 0, dist.acctd_amount) ), 2) earned_revenue,
6549 ROUND(SUM ( DECODE(dist.account_class, ''REV'', 0,
6550 ''UNBILL'', 0, dist.acctd_amount) ), 2) unearned_revenue,
6551 ROUND(SUM ( DECODE(dist.account_class, ''REV'', 0,
6552 ''UNEARN'', 0, dist.acctd_amount) ), 2) unbilled_revenue,
6553 Z.total_line_quantity order_quantity,
6554 Z.Earned_COGS earned_cogs,
6555 Z.Deferred_COGS deferred_cogs,
6556 Z.COGS_account cogs_account,
6557 Z.Deferred_COGS_account deferred_cogs_account
6558 FROM
6559 Z,
6560 cst_pac_periods cpp,
6561 ra_cust_trx_line_gl_dist_all dist,
6562 ra_customer_trx_lines_all lines,
6563 ra_customer_trx_all trx
6564 WHERE (Z.customer_trx_line_id = lines.customer_trx_line_id
6565 OR Z.customer_trx_line_id = lines.previous_customer_trx_line_id)
6566 AND Z.customer_trx_id = trx.customer_trx_id
6567 AND cpp.pac_period_id = TO_NUMBER(:i_period_name)
6568 AND dist.customer_trx_line_id = lines.customer_trx_line_id
6569 AND dist.account_set_flag = ''N''
6570 AND dist.account_class IN (''REV'', ''UNEARN'', ''UNBILL'')
6571 AND dist.gl_date <= cpp.period_end_date + (1-1/86400) -- or AS OF DATE
6572 GROUP BY
6573 Z.ORDER_NUMBER,
6574 Z.order_date,
6575 Z.customer_name,
6576 Z.currency,
6577 Z.sales_order_line,
6578 trx.trx_number,
6579 Z.invoice_line,
6580 Z.item,
6581 Z.total_line_quantity,
6582 Z.Earned_COGS,
6583 Z.Deferred_COGS,
6584 Z.COGS_account,
6585 Z.Deferred_COGS_account
6586 HAVING :i_all_lines = ''Y''
6587 or decode(sum(lines.revenue_amount),0,1,
6588 round(sum(decode(dist.account_class, ''UNEARN'', 0,
6589 ''UNBILL'', 0, dist.amount)) /
6590 (sum(lines.revenue_amount) /
6591 count(dist.cust_trx_line_gl_dist_id)), 3))
6592 <>
6593 decode(z.earned_cogs, 0, decode(z.deferred_cogs,0,1,0),
6594 round((z.earned_cogs /
6595 decode(z.deferred_cogs + z.earned_cogs,0,1,
6596 z.deferred_cogs + z.earned_cogs)), 3))
6597 UNION
6598 SELECT :i_all_lines all_lines,
6599 Z.order_number order_number,
6600 Z.order_date order_date,
6601 Z.customer_name customer,
6602 Z.currency currency,
6603 Z.sales_order_line order_line,
6604 NULL invoice_number,
6605 NULL invoice_line,
6606 Z.item item_number,
6607 NULL earned_revenue,
6611 Z.Earned_COGS earned_cogs,
6608 NULL unearned_revenue,
6609 NULL unbilled_revenue,
6610 Z.total_line_quantity order_quantity,
6612 Z.Deferred_COGS deferred_cogs,
6613 Z.COGS_account cogs_account,
6614 Z.Deferred_COGS_account deferred_cogs_account
6615 FROM
6616 Z
6617 WHERE Z.customer_trx_line_id IS NULL
6618 OR Z.customer_trx_id IS NULL
6619
6620 UNION
6621
6622 SELECT :i_all_lines all_lines,
6623 Z.order_number order_number,
6624 Z.order_date order_date,
6625 Z.customer_name customer,
6626 Z.currency currency,
6627 Z.sales_order_line order_line,
6628 NULL invoice_number,
6629 NULL invoice_line,
6630 Z.item item_number,
6631 NULL earned_revenue,
6632 NULL unearned_revenue,
6633 NULL unbilled_revenue,
6634 Z.total_line_quantity order_quantity,
6635 Z.Earned_COGS earned_cogs,
6636 Z.Deferred_COGS deferred_cogs,
6637 Z.COGS_account cogs_account,
6638 Z.Deferred_COGS_account deferred_cogs_account
6639 FROM
6640 Z
6641 WHERE (Z.customer_trx_line_id IS NOT NULL AND Z.customer_trx_id IS NOT NULL)
6642 AND NOT EXISTS
6643 (SELECT NULL
6644 FROM ra_customer_trx_lines_all ctl
6645 , ra_cust_trx_line_gl_dist_all ctlgd
6646 , CST_PAC_PERIODS CPP
6647 WHERE (Z.customer_trx_line_id = ctl.customer_trx_line_id
6648 OR Z.customer_trx_line_id = ctl.previous_customer_trx_line_id)
6649 AND CPP.PAC_PERIOD_ID = TO_NUMBER(:i_period_name)
6650 AND ctl.customer_trx_line_id = ctlgd.customer_trx_line_id
6651 AND ctlgd.account_set_flag = ''N''
6652 AND ctlgd.account_class IN (''REV'', ''UNEARN'', ''UNBILL'')
6653 AND ctlgd.gl_date <= cpp.period_end_date + (1-1/86400))
6654 AND (:i_all_lines = ''Y''
6655 OR z.deferred_cogs NOT BETWEEN -1*:i_amt_tolerance AND :i_amt_tolerance)'
6656 using
6657 i_sales_order_date_low,
6658 i_sales_order_date_high,
6659 i_pac_cost_group,
6660 i_period_name,
6661 l_date_offset,
6662 i_period_name,
6663 l_date_offset,
6664 i_all_lines,
6665 i_period_name,
6666 i_all_lines,
6667 i_all_lines,
6668 i_all_lines,
6669 i_period_name,
6670 i_all_lines,
6671 i_amt_tolerance,
6672 i_amt_tolerance;
6673
6674 end if; /* i_cost_method = 1, i_cost_method = 3 */
6675
6676 -- create new context
6677
6678 l_stmt_num := 20;
6679
6680 l_qryCtx := dbms_xmlgen.newContext(l_ref_cur);
6681 dbms_xmlgen.setRowSetTag(l_qryCtx,'DEF_COGS_DATA');
6682 dbms_xmlgen.setRowTag(l_qryCtx,'DEF_COGS');
6683
6684 -- get XML into the temporary clob variable
6685
6686 l_stmt_num := 70;
6687
6688 dbms_xmlgen.getXML(l_qryCtx, l_xml_temp, dbms_xmlgen.none);
6689
6690 -- remove the header (21 characters) and append the rest to xml output
6691
6692 l_count := dbms_xmlgen.getNumRowsProcessed(l_qryCtx);
6693
6694 if(dbms_xmlgen.getNumRowsProcessed(l_qryCtx) > 0) then
6695 dbms_lob.erase(l_xml_temp, l_offset,1);
6696 dbms_lob.append(x_xml_doc, l_xml_temp);
6697 end if;
6698
6699 -- close context and free memory
6700
6701 dbms_xmlgen.closeContext(l_qryCtx);
6702 close l_ref_cur;
6703 dbms_lob.freetemporary(l_xml_temp);
6704
6705 -- to add number of rows processed
6706
6707 dbms_lob.createtemporary(l_xml_temp, TRUE);
6708
6709 -- open ref cursor to get the number of rows processed
6710
6711 l_stmt_num := 80;
6712
6713 open l_ref_cur for ' select :l_count l_count
6714 from dual'
6715 using l_count;
6716
6717 -- create new context
6718
6719 l_stmt_num := 90;
6720
6721 l_qryCtx := DBMS_XMLGEN.newContext (l_ref_cur);
6722 dbms_xmlgen.setRowSetTag (l_qryCtx,'RECORD_NUM');
6723 dbms_xmlgen.setRowTag (l_qryCtx,NULL);
6724
6725 -- get XML to add the number of rows processed
6726
6727 l_stmt_num := 100;
6728
6729 dbms_xmlgen.getXML (l_qryCtx, l_xml_temp, dbms_xmlgen.none);
6730
6731 -- remove the header (21 characters) and append the rest to xml output
6732
6733 if(dbms_xmlgen.getNumRowsProcessed(l_qryCtx) > 0 ) then
6734 dbms_lob.erase (l_xml_temp, l_offset,1);
6735 dbms_lob.append (x_xml_doc, l_xml_temp);
6736 end if;
6737
6738 -- close context and free memory
6739
6740 dbms_xmlgen.closeContext(l_qryCtx);
6741 close l_ref_cur;
6742 dbms_lob.freetemporary(l_xml_temp);
6743
6744 -- Standard call to get message count and if count is 1, get message info.
6745
6746 fnd_msg_pub.Count_And_Get
6747 ( p_count => x_msg_count,
6748 p_data => x_msg_data
6749 );
6750
6751 -- Write the module name to fnd log file
6752 debug('x_return_status = '||x_return_status );
6753 debug('x_msg_count='||x_msg_count);
6754 debug('x_msg_data = '||x_msg_data);
6755 debug('Add_DefCOGSData-');
6756
6760 fnd_msg_pub.Count_And_Get
6757 exception
6758 when fnd_api.G_EXC_ERROR then
6759 x_return_status := fnd_api.G_RET_STS_ERROR;
6761 ( p_count => x_msg_count,
6762 p_data => x_msg_data
6763 );
6764
6765 when fnd_api.G_EXC_UNEXPECTED_ERROR then
6766 x_return_status := fnd_api.G_RET_STS_UNEXP_ERROR;
6767 fnd_msg_pub.Count_And_Get
6768 ( p_count => x_msg_count,
6769 p_data => x_msg_data
6770 );
6771
6772 when others then
6773 x_return_status := fnd_api.G_RET_STS_UNEXP_ERROR;
6774 if(fnd_log.LEVEL_UNEXPECTED >= G_LOG_LEVEL) then
6775 fnd_log.string(fnd_log.LEVEL_UNEXPECTED,
6776 l_module || '.' || l_stmt_num,
6777 substrb(SQLERRM , 1 , 240));
6778 end if;
6779
6780 if(fnd_msg_pub.Check_Msg_Level(fnd_msg_pub.G_MSG_LVL_UNEXP_ERROR)) then
6781 fnd_msg_pub.Add_Exc_Msg (G_PKG_NAME, l_api_name);
6782 end if;
6783
6784 fnd_msg_pub.Count_And_Get
6785 ( p_count => x_msg_count,
6786 p_data => x_msg_data
6787 );
6788
6789 end Add_DefCOGSData;
6790
6791
6792 PROCEDURE debug
6793 ( line IN VARCHAR2,
6794 msg_prefix IN VARCHAR2 DEFAULT 'CST',
6795 msg_module IN VARCHAR2 DEFAULT g_module_name,
6796 msg_level IN NUMBER DEFAULT FND_LOG.LEVEL_STATEMENT)
6797 IS
6798 l_msg_prefix VARCHAR2(64);
6799 l_msg_level NUMBER;
6800 l_msg_module VARCHAR2(256);
6801 l_beg_end_suffix VARCHAR2(15);
6802 l_org_cnt NUMBER;
6803 l_line VARCHAR2(32767);
6804 BEGIN
6805 l_line := SUBSTRB(line,1,500);
6806 l_msg_prefix := msg_prefix;
6807 l_msg_level := msg_level;
6808 l_msg_module := msg_module;
6809
6810 IF g_mrp_debug = 'Y' THEN
6811 fnd_file.put_line(fnd_file.log, 'CST_RevenueCogsMatch_PVT:'||l_line);
6812 ELSIF (INSTRB(upper(l_line), 'EXCEPTION') <> 0) THEN
6813 fnd_file.put_line(fnd_file.log, 'CST_RevenueCogsMatch_PVT:'||l_line);
6814 END IF;
6815
6816 IF (INSTRB(upper(l_line), 'EXCEPTION') <> 0) THEN
6817 l_msg_level := FND_LOG.LEVEL_EXCEPTION;
6818 END IF;
6819
6820 IF l_msg_level <> FND_LOG.LEVEL_EXCEPTION AND G_DEBUG = 'N' THEN
6821 RETURN;
6822 END IF;
6823
6824 IF ( l_msg_level >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
6825 FND_LOG.STRING(l_msg_level, l_msg_module, SUBSTRB(line,1,4000));
6826 END IF;
6827
6828 EXCEPTION
6829 WHEN OTHERS THEN RAISE;
6830 END debug;
6831
6832
6833
6834
6835
6836
6837 PROCEDURE ensure_mmt_per_and_date
6838 (x_return_status OUT NOCOPY VARCHAR2,
6839 x_msg_count OUT NOCOPY NUMBER,
6840 x_msg_data OUT NOCOPY VARCHAR2)
6841 IS
6842 CURSOR ctrx IS
6843 SELECT mmtt.rowid
6844 ,mmtt.transaction_id
6845 ,mmtt.transaction_date
6846 ,mmtt.acct_period_id
6847 ,oap.open_flag
6848 ,oap.period_start_date
6849 ,oap.schedule_close_date
6850 ,oap2.acct_period_id
6851 ,oap2.open_flag
6852 ,mmtt.organization_id
6853 FROM mtl_cogs_recognition_temp mmtt,
6854 org_acct_periods oap,
6855 (SELECT period_start_date,
6856 schedule_close_date,
6857 acct_period_id,
6858 organization_id,
6859 open_flag
6860 FROM org_acct_periods) oap2
6861 WHERE mmtt.acct_period_id = oap.acct_period_id
6862 AND mmtt.organization_id = oap.organization_id
6863 AND oap2.organization_id = mmtt.organization_id
6864 --
6865 --BUG#6873037 : Just check the inventory period mismatch
6866 --
6867 AND oap2.acct_period_id <> oap.acct_period_id
6868 AND mmtt.transaction_date BETWEEN oap2.period_start_date AND oap2.schedule_close_date + (1-1/86400) --BUG#13601677
6869 AND ( mmtt.transaction_date < oap.period_start_date OR
6870 mmtt.transaction_date > oap.schedule_close_date );
6871 --BUG#6873037
6872 -- No need to verify inventory period status
6873 --OR oap.open_flag = 'N');
6874 --
6875 l_mmtt_rowid_tab DBMS_SQL.VARCHAR2_TABLE;
6876 l_transaction_id_tab DBMS_SQL.NUMBER_TABLE;
6877 l_transaction_date_tab DBMS_SQL.DATE_TABLE;
6878 l_acct_period_id_tab DBMS_SQL.NUMBER_TABLE;
6879 l_open_flag DBMS_SQL.VARCHAR2_TABLE;
6880 l_period_start_date_tab DBMS_SQL.DATE_TABLE;
6881 l_schedule_close_date_tab DBMS_SQL.DATE_TABLE;
6882 l_good_acct_period_id_tab DBMS_SQL.NUMBER_TABLE;
6883 l_good_open_flag_tab DBMS_SQL.VARCHAR2_TABLE;
6884 l_organization_id_tab DBMS_SQL.VARCHAR2_TABLE;
6885
6886
6887 CURSOR next_inv_period(p_organization_id IN NUMBER
6888 ,p_transaction_date IN DATE)
6889 IS
6890 SELECT MIN(acct_period_id),
6891 MIN(period_start_date)
6892 FROM org_acct_periods
6893 WHERE organization_id = p_organization_id
6894 AND open_flag = 'Y'
6895 AND period_start_date >= p_transaction_date;
6896 l_next_acct_period_id NUMBER;
6897 l_next_transaction_date DATE;
6898
6899 l_upd_transaction_date_tab DBMS_SQL.DATE_TABLE;
6900 l_upd_acct_period_id_tab DBMS_SQL.NUMBER_TABLE;
6901 g_bulk_fetch_size NUMBER := 9999;
6902 l_last_fetch BOOLEAN := FALSE;
6903 l_msg_count NUMBER := 0;
6907 debug('ensure_mmt_per_and_date+');
6904 l_msg_data VARCHAR2(8000) := '';
6905 l_stmt_num NUMBER := 0;
6906 BEGIN
6908 SAVEPOINT ensure_mmt_per_and_date;
6909 x_return_status := fnd_api.g_ret_sts_success;
6910
6911 OPEN ctrx;
6912 LOOP
6913 l_stmt_num := 1;
6914 FETCH ctrx BULK COLLECT INTO
6915 l_mmtt_rowid_tab
6916 ,l_transaction_id_tab
6917 ,l_transaction_date_tab
6918 ,l_acct_period_id_tab
6919 ,l_open_flag
6920 ,l_period_start_date_tab
6921 ,l_schedule_close_date_tab
6922 ,l_good_acct_period_id_tab
6923 ,l_good_open_flag_tab
6924 ,l_organization_id_tab
6925 LIMIT g_bulk_fetch_size;
6926
6927 IF ctrx%NOTFOUND THEN
6928 l_last_fetch := TRUE;
6929 END IF;
6930
6931 IF l_last_fetch AND l_mmtt_rowid_tab.COUNT = 0 THEN
6932 EXIT;
6933 END IF;
6934
6935 FOR i IN l_mmtt_rowid_tab.FIRST .. l_mmtt_rowid_tab.LAST LOOP
6936 l_stmt_num := 2;
6937
6938 debug
6939 (l_stmt_num||'----------------------:
6940 l_transaction_id_tab :'||l_transaction_id_tab(i)||'
6941 l_transaction_date_tab :'||l_transaction_date_tab(i)||'
6942 l_acct_period_id_tab :'||l_acct_period_id_tab(i)||'
6943 l_open_flag :'||l_open_flag(i)||'
6944 l_period_start_date_tab :'||l_period_start_date_tab(i)||'
6945 l_schedule_close_date_tab:'||l_schedule_close_date_tab(i)||'
6946 l_good_acct_period_id_tab:'||l_good_acct_period_id_tab(i)||'
6947 l_good_open_flag_tab :'||l_good_open_flag_tab(i)||'
6948 l_organization_id_tab :'||l_organization_id_tab(i));
6949
6950 IF (l_acct_period_id_tab(i) <> l_good_acct_period_id_tab(i))
6951 THEN
6952 -- Update the acct_period_id with the l_good_acct_period_id_tab
6953 l_stmt_num := 21;
6954 l_upd_transaction_date_tab(i) := l_transaction_date_tab(i);
6955 l_upd_acct_period_id_tab(i) := l_good_acct_period_id_tab(i);
6956 debug(l_stmt_num||': l_upd_transaction_date_tab:'|| l_upd_transaction_date_tab(i));
6957 debug(l_stmt_num||': l_upd_acct_period_id_tab :'|| l_upd_acct_period_id_tab(i));
6958 ELSE
6959 -- Update the acct_period and transaction_date to the next opened inventory
6960 -- log a message
6961 l_stmt_num := 22;
6962 -- COGS transaction should not care about the status of inventory period as per design
6963 -- If GL is closed the problem will happen at GL post. Use needs to reopen GL period
6964 -- Not inventory Cst issue
6965 debug('Inventory period and COGS Inventory transaction date are in synchi but
6966 Inventory period has been closed');
6967 l_upd_transaction_date_tab(i) := l_transaction_date_tab(i);
6968 l_upd_acct_period_id_tab(i) := l_good_acct_period_id_tab(i);
6969
6970 END IF;
6971 END LOOP;
6972
6973 l_stmt_num := 3;
6974 debug(l_stmt_num||': UPDATING mtl_cogs_recognition_temp transaction_date and acct_period_id');
6975 FORALL i IN l_mmtt_rowid_tab.FIRST .. l_mmtt_rowid_tab.LAST
6976 UPDATE mtl_cogs_recognition_temp
6977 SET transaction_date = l_upd_transaction_date_tab(i)
6978 ,acct_period_id = l_upd_acct_period_id_tab(i)
6979 WHERE rowid = l_mmtt_rowid_tab(i);
6980 END LOOP;
6981 CLOSE ctrx;
6982 debug('ensure_mmt_per_and_date-');
6983 EXCEPTION
6984 WHEN OTHERS THEN
6985 --log a message
6986 ROLLBACK TO ensure_mmt_per_and_date;
6987 IF ctrx%ISOPEN THEN CLOSE ctrx; END IF;
6988 IF next_inv_period%ISOPEN THEN CLOSE next_inv_period; END IF;
6989 x_return_status := fnd_api.g_ret_sts_unexp_error ;
6990 debug('OTHERS EXCEPTION ensure_mmt_per_and_date:'||SQLERRM);
6991 fnd_message.set_name('BOM', 'CST_UNEXP_ERROR');
6992 fnd_message.set_token('PACKAGE', 'CST_REVENUECOGSMATCH_PVT');
6993 fnd_message.set_token('PROCEDURE','ensure_mmt_per_and_date');
6994 fnd_message.set_token('STATEMENT',to_char(l_stmt_num));
6995 fnd_msg_pub.ADD;
6996 fnd_msg_pub.count_and_get(
6997 p_encoded => fnd_api.g_false,
6998 p_count => x_msg_count,
6999 p_data => x_msg_data);
7000 END ensure_mmt_per_and_date;
7001
7002 --BUG#7387575
7003 -- Multi worker Development for performance
7004 -- Coordination of updation in CRRL
7005 --
7006 PROCEDURE crrl_preparation
7007 (p_batch_size IN NUMBER DEFAULT 1000
7008 ,p_ledger_id IN NUMBER DEFAULT NULL)
7009 IS
7010
7011 CURSOR c_no_ledger IS
7012 SELECT COUNT(*)
7013 , ledger_id
7014 FROM cst_revenue_recognition_lines crrl
7015 WHERE potentially_unmatched_flag = 'Y'
7016 GROUP BY ledger_id
7017 HAVING COUNT(*) > 0;
7018
7019 l_cnt_tab DBMS_SQL.NUMBER_TABLE;
7020 l_lgr_tab DBMS_SQL.NUMBER_TABLE;
7021 lafin EXCEPTION;
7022 BEGIN
7023 debug('crrl_preparation +');
7024 debug(' p_batch_size :'||p_batch_size);
7025 debug(' p_ledger_id :'||p_ledger_id);
7026 IF p_ledger_id IS NULL THEN
7027 OPEN c_no_ledger;
7028 FETCH c_no_ledger BULK COLLECT
7029 INTO l_cnt_tab
7030 ,l_lgr_tab;
7031 CLOSE c_no_ledger;
7032 IF l_lgr_tab.COUNT = 0 THEN
7033 debug('Nothing to run non potential unmatch');
7034 RAISE lafin;
7035 END IF;
7036 FOR i IN l_lgr_tab.FIRST .. l_lgr_tab.LAST LOOP
7037 debug(' Case ledger entered is null - determining ledger_id '||l_lgr_tab(i)||' and calling updation_potential_crrl');
7038 updation_potential_crrl
7039 (p_batch_size => p_batch_size
7040 ,p_ledger_id => l_lgr_tab(i));
7041 END LOOP;
7042 ELSE
7046 ,p_ledger_id => p_ledger_id);
7043 debug(' Case ledger entered is'||p_ledger_id||' calling updation_potential_crrl');
7044 updation_potential_crrl
7045 (p_batch_size => p_batch_size
7047 END IF;
7048 debug('crrl_preparation -');
7049 EXCEPTION
7050 WHEN lafin THEN NULL;
7051 WHEN OTHERS THEN
7052 debug('EXCEPTION OTHERS crrl_preparation:'||SQLERRM);
7053 RAISE;
7054 END crrl_preparation;
7055
7056
7057 ------------------
7058 -- PROCEDURE grouping cst_revenue_recognition_lines to be processed
7059 -- by a request
7060 -- Using the request_id column of the table CRRL
7061 -- This needs to be evaluated but for now we avoid schema changes
7062 -- At the end of the process the request_id in CRRL should back to the
7063 -- concurrent request submitting the process
7064 --------------------
7065 PROCEDURE updation_potential_crrl
7066 (p_batch_size IN NUMBER DEFAULT 1000
7067 ,p_ledger_id IN NUMBER)
7068 IS
7069 CURSOR c IS
7070 SELECT ROWID,
7071 revenue_om_line_id
7072 FROM cst_revenue_recognition_lines crrl
7073 WHERE potentially_unmatched_flag = 'Y'
7074 AND ledger_id = p_ledger_id
7075 ORDER BY revenue_om_line_id ASC;
7076
7077 l_rowid_tab DBMS_SQL.VARCHAR2_TABLE;
7078 l_upd_rowid_tab DBMS_SQL.VARCHAR2_TABLE;
7079 clear_tab DBMS_SQL.VARCHAR2_TABLE;
7080 l_romlid_tab DBMS_SQL.NUMBER_TABLE;
7081 l_oml_upd_tab DBMS_SQL.NUMBER_TABLE;
7082 l_last_fetch BOOLEAN := FALSE;
7083 l_last_om_id NUMBER := -9999;
7084 cnt NUMBER := 0;
7085 l_bulk_size NUMBER := 9999;
7086
7087 PROCEDURE update_one_set_crrl
7088 (p_rowid_tab IN DBMS_SQL.VARCHAR2_TABLE) IS
7089 l_gp_id NUMBER;
7090 BEGIN
7091 SELECT cst_cogs_events_s.nextval
7092 INTO l_gp_id
7093 FROM dual;
7094
7095 FORALL j IN p_rowid_tab.FIRST .. p_rowid_tab.LAST
7096 UPDATE cst_revenue_recognition_lines
7097 SET request_id = -1 * l_gp_id
7098 WHERE rowid = p_rowid_tab(j);
7099 COMMIT;
7100 debug(' CST_REV_REC_LINES updated with request_ID ='|| -1 * l_gp_id ||' for the ledger '||p_ledger_id ||
7101 ' Number of records CRRL updated is '|| l_upd_rowid_tab.COUNT);
7102
7103 INSERT INTO cst_lists_temp
7104 ( list_id
7105 ,number_1
7106 ,VARCHAR_1) VALUES (p_ledger_id
7107 , -1 * l_gp_id
7108 , 'INSERTED');
7109 debug(' CST_LISTS_TEMP.list_id = ledger_id : '||p_ledger_id);
7110 debug(' CST_LISTS_TEMP.number_1 = CRRL.request_id for process: '||-1 * l_gp_id);
7111 debug(' CST_LISTS_TEMP.varchar_1= STATUS of Request : INSERTED');
7112 END update_one_set_crrl;
7113
7114 PROCEDURE add_rowid
7115 ( p_rowid IN VARCHAR2
7116 ,x_rowid_tab IN OUT NOCOPY DBMS_SQL.VARCHAR2_TABLE)
7117 IS
7118 BEGIN
7119 x_rowid_tab(x_rowid_tab.COUNT+1) := p_rowid;
7120 END add_rowid;
7121
7122 BEGIN
7123 debug('updation_potential_crrl +');
7124 debug(' p_batch_size :'||p_batch_size);
7125 debug(' p_ledger_id :'||p_ledger_id);
7126
7127 OPEN c;
7128 LOOP
7129 FETCH c BULK COLLECT INTO
7130 l_rowid_tab ,
7131 l_romlid_tab
7132 LIMIT l_bulk_size;
7133
7134 cnt := 0;
7135
7136 IF c%NOTFOUND THEN
7137 l_last_fetch := TRUE;
7138 END IF;
7139
7140 IF (l_rowid_tab.COUNT = 0) AND (l_last_fetch) THEN
7141 IF l_upd_rowid_tab.COUNT > 0 THEN
7142 update_one_set_crrl(l_upd_rowid_tab);
7143 l_upd_rowid_tab := clear_tab;
7144 END IF;
7145 debug('COUNT = 0 and LAST FETCH ');
7146 EXIT;
7147 END IF;
7148
7149 FOR i IN l_rowid_tab.FIRST .. l_rowid_tab.LAST LOOP
7150 IF l_last_om_id <> l_romlid_tab(i) THEN
7151 l_last_om_id := l_romlid_tab(i);
7152 IF ( l_upd_rowid_tab.COUNT > p_batch_size
7153 AND i <> l_rowid_tab.LAST)
7154 THEN
7155 update_one_set_crrl(l_upd_rowid_tab);
7156 l_upd_rowid_tab := clear_tab;
7157 END IF;
7158 END IF;
7159 add_rowid(l_rowid_tab(i),l_upd_rowid_tab);
7160 cnt := cnt + 1;
7161 END LOOP;
7162
7163 END LOOP;
7164 CLOSE c;
7165 debug('updation_potential_crrl -');
7166 EXCEPTION
7167 WHEN OTHERS THEN
7168 debug('EXCEPTION OTHERS updation_potential_crrl:'||SQLERRM);
7169 RAISE;
7170 END updation_potential_crrl;
7171
7172
7173
7174 ---------------------
7175 -- Procedure Master ordonnancer of the COGS recognition programs
7176 --
7177 ---------------------
7178 PROCEDURE ordonnancer
7179 (errbuf OUT NOCOPY VARCHAR2
7180 ,retcode OUT NOCOPY VARCHAR2
7181 ,p_batch_size IN NUMBER DEFAULT 1000
7182 ,p_nb_worker IN NUMBER DEFAULT 4
7183 ,p_api_version IN NUMBER
7184 ,p_phase IN NUMBER
7185 ,p_low_date IN VARCHAR2
7186 ,p_high_date IN VARCHAR2
7187 ,p_ledger_id IN NUMBER DEFAULT NULL)
7188 IS
7189 CURSOR c_ins IS
7190 SELECT list_id -- ledger_id
7191 ,number_1 -- process_negative_request_id
7192 ,rowid
7193 FROM cst_lists_temp
7194 WHERE VARCHAR_1 = 'INSERTED';
7195
7196 CURSOR c_cpt IS
7197 SELECT COUNT(*)
7198 FROM cst_lists_temp
7199 WHERE VARCHAR_1 = 'SUBMITTED';
7200
7201 l_ledger_id NUMBER;
7202 l_neg_req_id NUMBER;
7203 l_rowid VARCHAR2(200);
7207 l_req_id NUMBER;
7204 l_nb_req_sub NUMBER := 0;
7205 l_from_date VARCHAR2(30);
7206 l_to_date VARCHAR2(30);
7208 BEGIN
7209 debug('Ordonnancer +');
7210 debug(' p_batch_size :'||p_batch_size);
7211 debug(' p_ledger_id :'||p_ledger_id);
7212 debug(' p_nb_worker :'||p_nb_worker);
7213 debug(' p_api_version:'||p_api_version);
7214 debug(' p_phase :'||p_phase);
7215 debug(' p_low_date :'||p_low_date);
7216 debug(' p_high_date :'||p_high_date);
7217 debug(' p_ledger_id :'||p_ledger_id);
7218 crrl_preparation
7219 (p_batch_size => p_batch_size
7220 ,p_ledger_id => p_ledger_id);
7221
7222 OPEN c_ins;
7223 LOOP
7224 FETCH c_ins INTO l_ledger_id
7225 ,l_neg_req_id
7226 ,l_rowid;
7227 EXIT WHEN c_ins%NOTFOUND;
7228 -----------------------
7229 -- submit_req_cogs_reco
7230 -----------------------
7231 l_req_id := FND_REQUEST.SUBMIT_REQUEST
7232 (application => 'BOM'
7233 ,program => 'CSTRCMCR3S'
7234 ,description => 'Phase 3 of the concurrent requests for matching COGS to revenue Child'
7235 ,start_time => NULL
7236 ,sub_request => FALSE
7237 ,argument1 => p_api_version
7238 ,argument2 => p_phase
7239 ,argument3 => l_ledger_id
7240 ,argument4 => p_low_date
7241 ,argument5 => p_high_date
7242 ,argument6 => l_neg_req_id);
7243 IF l_req_id > 0 THEN
7244 debug(' Submitted Concurrent request for CSTRCMCR3S with ledger_id '||l_ledger_id||
7245 ' and process_request_id '||l_neg_req_id);
7246 UPDATE cst_lists_temp
7247 SET number_2 = l_req_id
7248 ,VARCHAR_1 = 'SUBMITTED'
7249 WHERE ROWID = l_rowid;
7250 COMMIT;
7251 END IF;
7252 l_nb_req_sub := l_nb_req_sub + 1;
7253 debug(' Number of request launched before verification :'||l_nb_req_sub);
7254 IF l_nb_req_sub >= p_nb_worker THEN
7255 LOOP
7256 debug(' Verifying number of active requests');
7257 nb_req_active;
7258 SELECT COUNT(*)
7259 INTO l_nb_req_sub
7260 FROM cst_lists_temp
7261 WHERE VARCHAR_1 = 'SUBMITTED';
7262
7263 debug(' Number of request active after verification :'||l_nb_req_sub);
7264 IF l_nb_req_sub < p_nb_worker THEN
7265 EXIT;
7266 END IF;
7267 END LOOP;
7268 END IF;
7269 END LOOP;
7270 CLOSE c_ins;
7271
7272 debug('Ordonnancer -');
7273 EXCEPTION
7274 WHEN OTHERS THEN
7275 debug('EXCEPTION OTHERS Ordonnancer:'||SQLERRM);
7276 RAISE;
7277 END ordonnancer;
7278
7279
7280 ---------------------
7281 -- Procedure Checking active request
7282 ---------------------
7283 PROCEDURE nb_req_active
7284 IS
7285 CURSOR c_activity IS
7286 SELECT number_2
7287 ,rowid
7288 FROM cst_lists_temp
7289 WHERE varchar_1 = 'SUBMITTED';
7290
7291 l_req_id_tab DBMS_SQL.NUMBER_TABLE;
7292 l_rowid_tab DBMS_SQL.VARCHAR2_TABLE;
7293 l_res BOOLEAN;
7294 l_phase VARCHAR2(30);
7295 l_status VARCHAR2(30);
7296 l_dev_phase VARCHAR2(30);
7297 l_dev_status VARCHAR2(30);
7298 l_message VARCHAR2(2000);
7299 l_request_id NUMBER;
7300
7301 BEGIN
7302 debug('nb_req_active +');
7303 -- DBMS_LOCK.sleep (2);
7304 OPEN c_activity;
7305 FETCH c_activity BULK COLLECT INTO l_req_id_tab
7306 ,l_rowid_tab;
7307 CLOSE c_activity;
7308 FOR i IN l_req_id_tab.FIRST .. l_req_id_tab.LAST LOOP
7309 debug(' Status verification for the request '||l_req_id_tab(i));
7310 l_res := FND_CONCURRENT.GET_REQUEST_STATUS
7311 (request_id => l_req_id_tab(i)
7312 ,phase => l_phase
7313 ,status => l_status
7314 ,dev_phase => l_dev_phase
7315 ,dev_status => l_dev_status
7316 ,message => l_message);
7317 debug(' Request Status:'||l_dev_phase);
7318 IF l_dev_phase = 'COMPLETE' THEN
7319 IF l_dev_status = 'NORMAL' THEN
7320 debug('The process '||l_req_id_tab(i)||' completed successfully');
7321 debug(l_message);
7322 ELSIF l_dev_status = 'ERROR' THEN
7323 debug('The process '||l_req_id_tab(i)||' completed with error');
7324 debug(l_message);
7325 ELSIF l_dev_status = 'WARNING' THEN
7326 debug('The process '||l_req_id_tab(i)||' completed with warning');
7327 debug(l_message);
7328 ELSIF l_dev_status = 'CANCELLED' THEN
7329 debug('User has aborted the process '||l_req_id_tab(i));
7330 debug(l_message);
7331 ELSIF l_dev_status = 'TERMINATED' THEN
7332 debug('User has aborted the process '||l_req_id_tab(i));
7333 debug(l_message);
7334 END IF;
7335 --
7336 debug(' Updating the request as COMPLETED');
7337 UPDATE cst_lists_temp
7338 SET varchar_1 = l_dev_phase
7339 WHERE rowid = l_rowid_tab(i);
7340 END IF;
7341 END LOOP;
7342 debug('nb_req_active -');
7343 EXCEPTION
7344 WHEN OTHERS THEN
7345 debug('EXCEPTION OTHERS nb_req_active:'||SQLERRM);
7346 RAISE;
7347 END nb_req_active ;
7348
7349
7350 PROCEDURE check_program_running
7351 ( p_prg_name IN VARCHAR2
7352 , p_app_id IN NUMBER
7353 , p_ledger_id IN NUMBER
7354 , x_running OUT NOCOPY VARCHAR2
7358 /* CURSOR cu_program_id IS
7355 , x_status OUT NOCOPY VARCHAR2
7356 , x_out_msg OUT NOCOPY VARCHAR2)
7357 IS
7359 SELECT concurrent_program_id
7360 FROM fnd_concurrent_programs
7361 WHERE application_id = p_app_id
7362 AND concurrent_program_name = p_prg_name;
7363
7364 CURSOR cu_exec(p_prg_id IN NUMBER) IS
7365 SELECT NULL
7366 FROM fnd_concurrent_requests FCR
7367 WHERE FCR.program_application_id = 702
7368 AND FCR.concurrent_program_id = p_prg_id
7369 AND (FCR.argument3 = TO_CHAR(p_ledger_id) OR
7370 -- User submits a request for ledger A and CP for all ledger is running
7371 FCR.argument3 IS NULL)
7372 AND FCR.phase_code = 'R'
7373 AND FCR.request_id <> FND_GLOBAL.CONC_REQUEST_ID
7374 AND ROWNUM = 1;
7375
7376 -- User submits CP for all ledgers and CP for ledger A is running
7377 CURSOR cu_exec_all(p_prg_id IN NUMBER) IS
7378 SELECT NULL
7379 FROM fnd_concurrent_requests FCR
7380 WHERE FCR.program_application_id = 702
7381 AND FCR.concurrent_program_id = p_prg_id
7382 AND FCR.phase_code = 'R'
7383 AND FCR.request_id <> FND_GLOBAL.CONC_REQUEST_ID
7384 AND ROWNUM = 1; */
7385
7386 CURSOR cu_exec IS
7387 SELECT FCP.user_concurrent_program_name
7388 FROM fnd_concurrent_requests FCR, fnd_concurrent_programs_vl FCP
7389 WHERE FCR.program_application_id = p_app_id
7390 AND FCP.application_id = p_app_id
7391 AND (FCR.argument3 = TO_CHAR(p_ledger_id) OR
7392 -- User submits a request for ledger A and CP for all ledger is running
7393 FCR.argument3 IS NULL)
7394 AND FCR.phase_code = 'R'
7395 AND FCR.request_id <> FND_GLOBAL.CONC_REQUEST_ID
7396 AND FCR.concurrent_program_id = FCP.concurrent_program_id
7397 AND FCP.concurrent_program_name IN ('CSTRCMCR1','CSTRCMCR','CSTRCMCR3');
7398
7399 CURSOR cu_exec_all IS
7400 SELECT FCP.user_concurrent_program_name
7401 FROM fnd_concurrent_requests FCR, fnd_concurrent_programs_vl FCP
7402 WHERE FCR.program_application_id = p_app_id
7403 AND FCP.application_id = p_app_id
7404 AND FCR.phase_code = 'R'
7405 AND FCR.request_id <> FND_GLOBAL.CONC_REQUEST_ID
7406 AND FCR.concurrent_program_id = FCP.concurrent_program_id
7407 AND FCP.concurrent_program_name IN ('CSTRCMCR1','CSTRCMCR','CSTRCMCR3');
7408
7409 l_prg_id NUMBER;
7410 l_res VARCHAR2(1);
7411 l_prg_name VARCHAR2(100);
7412 no_program EXCEPTION;
7413
7414 BEGIN
7415 debug ('check_program_running(+)');
7416 debug (' p_prg_name : '||p_prg_name);
7417 debug (' p_app_id : '||p_app_id);
7418 debug (' p_ledger_id : '||p_ledger_id);
7419
7420 x_status := 'S';
7421 x_out_msg := '';
7422
7423 /*
7424 OPEN cu_program_id;
7425 FETCH cu_program_id INTO l_prg_id;
7426 IF cu_program_id%NOTFOUND THEN
7427 l_prg_id := -1;
7428 END IF;
7429 CLOSE cu_program_id;
7430
7431 IF l_prg_id = -1 THEN
7432 RAISE no_program;
7433 END IF;
7434 */
7435
7436 IF p_ledger_id IS NOT NULL THEN
7437 OPEN cu_exec;
7438 FETCH cu_exec INTO l_prg_name;
7439 IF cu_exec%NOTFOUND THEN
7440 --No program running
7441 x_running := 'N';
7442 x_out_msg := 'No COGS Program with the ledger_id '||p_ledger_id||' is running.';
7443 ELSE
7444 --Program is running
7445 x_running := 'Y';
7446 x_out_msg := 'Program '||l_prg_name||' with the ledger_id '||p_ledger_id||' is running.';
7447 END IF;
7448 CLOSE cu_exec;
7449
7450 ELSE
7451
7452 OPEN cu_exec_all;
7453 FETCH cu_exec_all INTO l_prg_name;
7454 IF cu_exec_all%NOTFOUND THEN
7455 --No program running
7456 x_running := 'N';
7457 x_out_msg := 'No COGS Program is running.';
7458 ELSE
7459 --Program is running
7460 x_running := 'Y';
7461 x_out_msg := 'Program '||l_prg_name||' is running ';
7462 END IF;
7463 CLOSE cu_exec_all;
7464
7465 END IF;
7466 debug(' x_running :' ||x_running);
7467 debug (line => 'check_program_running(-)');
7468 EXCEPTION
7469 WHEN no_program THEN
7470 x_status := 'E';
7471 x_out_msg := 'EXCEPTION PROGRAM DOES NOT EXIST FOR PRG_NAME :'||p_prg_name||' AND APP_ID :'||p_app_id;
7472 debug(x_out_msg);
7473 WHEN OTHERS THEN
7474 x_status := 'U';
7475 x_out_msg := 'EXCEPTION OTHERS:'||SQLERRM;
7476 debug(x_out_msg);
7477 END check_program_running;
7478
7479
7480 PROCEDURE check_eligible_lines
7481 ( p_ledger_id IN NUMBER
7482 , x_status OUT NOCOPY VARCHAR2
7483 , x_out_msg OUT NOCOPY VARCHAR2)
7484 IS
7485 CURSOR c_revenue_only_lines(p_ledger_id IN NUMBER) IS
7486 SELECT crrl.rowid l_rowid
7487 , 'P' flag
7488 FROM cst_revenue_recognition_lines CRRL
7489 WHERE CRRL.potentially_unmatched_flag = 'Y'
7490 AND CRRL.ledger_id = p_ledger_id
7491 AND (NOT EXISTS (SELECT NULL
7492 FROM cst_revenue_cogs_match_lines CRCML
7493 WHERE CRCML.revenue_om_line_id = CRRL.revenue_om_line_id
7494 AND CRCML.ledger_id = p_ledger_id)
7495 OR EXISTS
7496 (SELECT 1
7497 FROM mtl_transactions_interface mti , cst_revenue_cogs_match_lines crcml
7498 WHERE mti.trx_source_line_id = crcml.cogs_om_line_id
7499 AND crcml.revenue_om_line_id=crrl.revenue_om_line_id
7500 UNION
7501 SELECT 1
7505 UNION
7502 FROM mtl_material_transactions_temp mmtt, cst_revenue_cogs_match_lines crcml
7503 WHERE mmtt.trx_source_line_id = crcml.cogs_om_line_id
7504 AND crcml.revenue_om_line_id=crrl.revenue_om_line_id
7506 SELECT 1
7507 FROM mtl_material_transactions mmt, cst_revenue_cogs_match_lines crcml
7508 WHERE mmt.trx_source_line_id = crcml.cogs_om_line_id
7509 AND crcml.revenue_om_line_id=crrl.revenue_om_line_id
7510 AND mmt.cogs_recognition_percent is null
7511 AND mmt.so_issue_account_type = 2
7512 AND mmt.transaction_type_id = 33
7513 AND Nvl(mmt.logical_transactions_created,2)=2
7514 UNION
7515 SELECT 1
7516 FROM mtl_material_transactions mmt, cst_revenue_cogs_match_lines crcml
7517 WHERE mmt.trx_source_line_id = crcml.cogs_om_line_id
7518 AND crcml.revenue_om_line_id=crrl.revenue_om_line_id
7519 AND mmt.cogs_recognition_percent IS null
7520 AND mmt.so_issue_account_type = 2
7521 AND mmt.transaction_type_id = 30))
7522 UNION ALL
7523 SELECT crrl.rowid l_rowid
7524 , 'Y' flag
7525 FROM cst_revenue_recognition_lines CRRL
7526 WHERE CRRL.potentially_unmatched_flag = 'P'
7527 AND CRRL.ledger_id = p_ledger_id
7528 AND EXISTS (SELECT NULL
7529 FROM cst_revenue_cogs_match_lines CRCML
7530 WHERE CRCML.revenue_om_line_id = CRRL.revenue_om_line_id
7531 AND CRCML.ledger_id = p_ledger_id)
7532 and not exists
7533 (SELECT 1
7534 FROM mtl_transactions_interface mti , cst_revenue_cogs_match_lines crcml
7535 WHERE mti.trx_source_line_id = crcml.cogs_om_line_id
7536 AND crcml.revenue_om_line_id=crrl.revenue_om_line_id
7537 UNION
7538 SELECT 1
7539 FROM mtl_material_transactions_temp mmtt, cst_revenue_cogs_match_lines crcml
7540 WHERE mmtt.trx_source_line_id = crcml.cogs_om_line_id
7541 AND crcml.revenue_om_line_id=crrl.revenue_om_line_id
7542 UNION
7543 SELECT 1
7544 FROM mtl_material_transactions mmt, cst_revenue_cogs_match_lines crcml
7545 WHERE mmt.trx_source_line_id = crcml.cogs_om_line_id
7546 AND crcml.revenue_om_line_id=crrl.revenue_om_line_id
7547 AND mmt.cogs_recognition_percent is null
7548 AND mmt.so_issue_account_type = 2
7549 AND mmt.transaction_type_id = 33
7550 AND Nvl(mmt.logical_transactions_created,2)=2
7551 UNION
7552 SELECT 1
7553 FROM mtl_material_transactions mmt, cst_revenue_cogs_match_lines crcml
7554 WHERE mmt.trx_source_line_id = crcml.cogs_om_line_id
7555 AND crcml.revenue_om_line_id=crrl.revenue_om_line_id
7556 AND mmt.cogs_recognition_percent IS null
7557 AND mmt.so_issue_account_type = 2
7558 AND mmt.transaction_type_id = 30);
7559
7560
7561
7562 CURSOR c_prepare(p_ledger_id IN NUMBER) IS
7563 --
7564 -- Shipped after revenue
7565 -- Only the latest Revenue record at status unprocessed needs to be matched
7566 -- as revenue is cumulative
7567 --
7568 SELECT crrl.rowid l_rowid
7569 , DECODE(crrl.acct_period_num,latest_rev.mper,'Y',NULL) flag
7570 FROM cst_revenue_recognition_lines CRRL
7571 , cst_revenue_cogs_match_lines crcml
7572 , cst_cogs_events cce
7573 , gl_period_statuses glp
7574 , (SELECT MAX(crrl3.acct_period_num) mper
7575 , crrl3.revenue_om_line_id
7576 FROM cst_revenue_recognition_lines crrl3
7577 WHERE crrl3.potentially_unmatched_flag = 'Y'
7578 group by crrl3.revenue_om_line_id) latest_rev
7579 WHERE CRRL.potentially_unmatched_flag = 'Y'
7580 AND CRRL.ledger_id = p_ledger_id
7581 AND crrl.revenue_om_line_id = crcml.revenue_om_line_id
7582 AND crcml.cogs_om_line_id = cce.cogs_om_line_id
7583 AND cce.event_type = 1
7584 AND cce.event_date > crrl.last_event_date
7585 AND crrl.revenue_om_line_id = latest_rev.revenue_om_line_id
7586 AND glp.application_id = 101
7587 AND glp.ledger_id = p_ledger_id
7588 AND cce.event_date BETWEEN glp.start_date AND glp.end_date + EndDateNum
7589 AND crrl.acct_period_num < glp.effective_period_num;
7590
7591 l_rowid_tab DBMS_SQL.VARCHAR2_TABLE;
7592 l_flag_tab DBMS_SQL.VARCHAR2_TABLE;
7593 BEGIN
7594 debug ('check_eligible_lines +');
7595 debug (' p_ledger_id : '||p_ledger_id);
7596
7597 x_status := FND_API.G_RET_STS_SUCCESS;
7598 x_out_msg := '';
7599
7600 debug ('1');
7601
7602 OPEN c_revenue_only_lines(p_ledger_id);
7603 LOOP
7604 FETCH c_revenue_only_lines BULK COLLECT
7605 INTO l_rowid_tab
7606 , l_flag_tab
7607 LIMIT C_max_bulk_fetch_size;
7608 IF c_revenue_only_lines%NOTFOUND AND l_rowid_tab.Count=0 THEN
7609 EXIT;
7610 END IF;
7611 FORALL i IN l_rowid_tab.FIRST .. l_rowid_tab.LAST
7612 UPDATE cst_revenue_recognition_lines CRRL
7613 SET potentially_unmatched_flag = l_flag_tab(i)
7614 where CRRL.ROWID = l_rowid_tab(i);
7615 COMMIT;
7616 END LOOP;
7617 CLOSE c_revenue_only_lines;
7618
7619 debug ('2');
7620
7621 OPEN c_prepare(p_ledger_id);
7622 LOOP
7623 FETCH c_prepare BULK COLLECT
7624 INTO l_rowid_tab
7625 , l_flag_tab
7626 LIMIT C_max_bulk_fetch_size;
7627 IF c_prepare%NOTFOUND AND l_rowid_tab.Count=0 THEN
7628 EXIT;
7629 END IF;
7630 FORALL i IN l_rowid_tab.FIRST .. l_rowid_tab.LAST
7634 COMMIT;
7631 UPDATE cst_revenue_recognition_lines CRRL
7632 SET potentially_unmatched_flag = l_flag_tab(i)
7633 where CRRL.ROWID = l_rowid_tab(i);
7635 END LOOP;
7636 CLOSE c_prepare;
7637
7638 debug ('3');
7639
7640 debug ('check_eligible_lines -');
7641 EXCEPTION
7642 WHEN OTHERS THEN
7643 IF c_revenue_only_lines%ISOPEN THEN CLOSE c_revenue_only_lines; END IF;
7644 IF c_prepare%ISOPEN THEN CLOSE c_prepare; END IF;
7645 x_status := fnd_api.G_RET_STS_UNEXP_ERROR;
7646 x_out_msg := 'EXCEPTION OTHERS in check_eligible_lines:'||SQLERRM;
7647 debug(x_out_msg);
7648
7649 END check_eligible_lines;
7650
7651
7652 FUNCTION acct_date_per
7653 (p_ledger_id IN NUMBER
7654 ,p_closing_status IN VARCHAR2
7655 ,p_per_num IN NUMBER
7656 ,p_start_date IN DATE
7657 ,p_context IN VARCHAR2 DEFAULT 'DATE')
7658 RETURN NUMBER
7659 IS
7660 CURSOR c IS
7661 SELECT min(effective_period_num) per_num
7662 ,min(start_date) start_date
7663 FROM gl_period_statuses
7664 WHERE application_id = 101
7665 AND ledger_id = p_ledger_id
7666 AND effective_period_num > p_per_num
7667 AND ( closing_status = 'O' OR
7668 (closing_status = 'F' AND adjustment_period_flag = 'N'));
7669 l_per_num NUMBER;
7670 l_min_date DATE;
7671 l_per_res NUMBER;
7672 l_date_res DATE;
7673 l_return NUMBER;
7674 BEGIN
7675 debug('acct_date_per +');
7676 debug('p_ledger_id-p_closing_status-p_per_num-p_start_date-p_context:'||p_ledger_id||'-'||p_closing_status||'-'||p_per_num||'-'||p_start_date||'-'||p_context);
7677 IF p_context = 'DATE' THEN
7678 IF p_closing_status IN ('O','F') THEN
7679 l_date_res := p_start_date;
7680 ELSE
7681 OPEN c;
7682 FETCH c INTO l_per_num, l_min_date;
7683 IF c%NOTFOUND THEN
7684 l_date_res := p_start_date;
7685 ELSE
7686 l_date_res := l_min_date;
7687 END IF;
7688 CLOSE c;
7689 END IF;
7690 IF l_date_res IS NOT NULL THEN
7691 l_return := TO_NUMBER(TO_CHAR(l_date_res,'YYYYMMDD'));
7692 END IF;
7693 debug(' Date Resulting :'||l_return);
7694 ELSE
7695 -- Period_num
7696 IF p_closing_status IN ('O','F') THEN
7697 l_per_res := p_per_num;
7698 ELSE
7699 OPEN c;
7700 FETCH c INTO l_per_num, l_min_date;
7701 IF c%NOTFOUND THEN
7702 l_per_res := p_per_num;
7703 ELSE
7704 l_per_res := l_per_num;
7705 END IF;
7706 CLOSE c;
7707 END IF;
7708 l_return := l_per_res;
7709 debug(' Period Resulting :'||l_return);
7710 END IF;
7711 debug('acct_date_per -');
7712 RETURN l_return;
7713 END;
7714
7715
7716 PROCEDURE prepare_acct_dates
7717 (p_ledger_id IN NUMBER
7718 ,p_low_per_num IN NUMBER
7719 ,p_hig_per_num IN NUMBER
7720 ,x_out_status OUT NOCOPY VARCHAR2
7721 ,x_out_msg OUT NOCOPY VARCHAR2)
7722 IS
7723 CURSOR c_per
7724 (p_ledger_id IN NUMBER
7725 ,p_low_per_num IN NUMBER
7726 ,p_hig_per_num IN NUMBER)
7727 IS
7728 SELECT TO_NUMBER(TO_CHAR(start_date,'YYYYMMDD')) NUMBER_1
7729 ,TO_NUMBER(TO_CHAR(end_date ,'YYYYMMDD')) NUMBER_2
7730 ,DECODE(closing_status,'F'
7731 ,DECODE(adjustment_period_flag
7732 ,'Y','C','F')
7733 ,closing_status) VARCHAR_1
7734 ,CST_RevenueCogsMatch_PVT.acct_date_per
7735 (ledger_id
7736 ,closing_status
7737 ,effective_period_num
7738 ,start_date
7739 ,'PERIOD') LIST_ID
7740 ,CST_RevenueCogsMatch_PVT.acct_date_per
7741 (ledger_id
7742 ,closing_status
7743 ,effective_period_num
7744 ,start_date
7745 ,'DATE') NUMBER_3
7746 FROM gl_period_statuses
7747 WHERE ledger_id = p_ledger_id
7748 AND application_id = 101
7749 AND EFFECTIVE_PERIOD_NUM >= p_low_per_num
7750 AND EFFECTIVE_PERIOD_NUM <= p_hig_per_num;
7751 l_number_1_t DBMS_SQL.NUMBER_TABLE;
7752 l_number_2_t DBMS_SQL.NUMBER_TABLE;
7753 l_varchar_1_t DBMS_SQL.VARCHAR2_TABLE;
7754 l_list_id_t DBMS_SQL.NUMBER_TABLE;
7755 l_number_3_t DBMS_SQL.NUMBER_TABLE;
7756 BEGIN
7757 debug('prepare_acct_dates +');
7758 debug(' p_ledger_id :'||p_ledger_id);
7759 debug(' p_low_per_num :'||p_low_per_num);
7760 debug(' p_hig_per_num :'||p_hig_per_num);
7761
7762 x_out_status := 'S';
7763
7764 debug('1 Clear CST_LISTS_TEMP');
7765 DELETE FROM CST_LISTS_TEMP WHERE VARCHAR_1 <> 'TIMEZONE';
7766
7767 debug(' 3 opening cursor c_per');
7768 OPEN c_per(p_ledger_id, p_low_per_num, p_hig_per_num);
7769 FETCH c_per BULK COLLECT INTO
7770 l_number_1_t,
7771 l_number_2_t,
7772 l_varchar_1_t,
7773 l_list_id_t ,
7774 l_number_3_t;
7775 CLOSE c_per;
7776 debug(' 3 number of period found between period num '||p_low_per_num||' and '||p_hig_per_num||' is '||l_number_1_t.COUNT);
7777
7778 IF l_list_id_t.COUNT > 0 THEN
7779 debug('4 inserting in CST_LISTS_TEMP');
7780 FORALL i IN l_list_id_t.FIRST .. l_list_id_t.LAST
7781 INSERT INTO CST_LISTS_TEMP
7782 ( NUMBER_1 -- START_DATE YYYYMMDD
7783 ,NUMBER_2 -- END_DATE YYYYMMDD
7784 ,VARCHAR_1 -- CLOSING_STATUS
7785 ,LIST_ID -- EFFECTIVE_PERIOD_NUM
7786 ,NUMBER_3) -- ACCT_DATE YYYYMMDD
7787 VALUES
7788 ( l_number_1_t(i)
7789 ,l_number_2_t(i)
7790 ,l_varchar_1_t(i)
7794 debug('prepare_acct_dates -');
7791 ,l_list_id_t(i)
7792 ,l_number_3_t(i));
7793 END IF;
7795 EXCEPTION
7796 WHEN OTHERS THEN
7797 IF c_per%ISOPEN THEN CLOSE c_per; END IF;
7798 x_out_status := 'E';
7799 x_out_msg := 'OTHERS EXCEPTION in prepare_acct_dates '||SQLERRM;
7800 END;
7801
7802 FUNCTION acct_date
7803 (p_trx_date IN DATE
7804 ,p_rev_date IN DATE)
7805 RETURN DATE
7806 IS
7807 CURSOR c (p_date_num IN NUMBER) IS
7808 SELECT VARCHAR_1 closing_status
7809 , NUMBER_3 accounting_date
7810 , LIST_ID per_num
7811 FROM CST_LISTS_TEMP
7812 WHERE VARCHAR_1 <> 'TIMEZONE'
7813 AND p_date_num BETWEEN
7814 NUMBER_1 -- start_date
7815 AND NUMBER_2; -- end_date
7816 l_date DATE;
7817 l_date_num NUMBER;
7818 l_closing_status VARCHAR2(1);
7819 l_acct_date_num NUMBER;
7820 l_per_num NUMBER;
7821 BEGIN
7822 -- Shipping after Revenue
7823 IF p_trx_date > p_rev_date THEN
7824 l_date := p_trx_date;
7825 ELSE
7826 l_date := p_rev_date;
7827 END IF;
7828 l_date_num := TO_NUMBER(TO_CHAR(l_date,'YYYYMMDD'));
7829 OPEN c(l_date_num);
7830 FETCH c INTO l_closing_status
7831 ,l_acct_date_num
7832 ,l_per_num;
7833 IF c%NOTFOUND THEN
7834 l_acct_date_num := l_date_num;
7835 END IF;
7836 CLOSE c;
7837
7838 IF l_closing_status IN ('O','F') THEN
7839 RETURN l_date;
7840 ELSE
7841 IF l_acct_date_num IS NOT NULL THEN
7842 RETURN TO_DATE(l_acct_date_num,'YYYYMMDD')+EndDateNum;
7843 ELSE
7844 RETURN l_date;
7845 END IF;
7846 END IF;
7847 END;
7848
7849 FUNCTION acct_period
7850 (p_trx_date IN DATE
7851 ,p_rev_date IN DATE)
7852 RETURN NUMBER
7853 IS
7854 CURSOR c (p_date_num IN NUMBER) IS
7855 SELECT VARCHAR_1 closing_status
7856 , NUMBER_3 accounting_date
7857 , LIST_ID per_num
7858 FROM CST_LISTS_TEMP
7859 WHERE VARCHAR_1 <> 'TIMEZONE'
7860 AND p_date_num BETWEEN
7861 NUMBER_1 -- start_date
7862 AND NUMBER_2; -- end_date
7863 l_date DATE;
7864 l_date_num NUMBER;
7865 l_closing_status VARCHAR2(1);
7866 l_acct_date_num NUMBER;
7867 l_per_num NUMBER;
7868 BEGIN
7869 -- Shipping after Revenue
7870 IF p_trx_date > p_rev_date THEN
7871 l_date := p_trx_date;
7872 ELSE
7873 l_date := p_rev_date;
7874 END IF;
7875 l_date_num := TO_NUMBER(TO_CHAR(l_date,'YYYYMMDD'));
7876 OPEN c(l_date_num);
7877 FETCH c INTO l_closing_status
7878 ,l_acct_date_num
7879 ,l_per_num;
7880 IF c%NOTFOUND THEN
7881 l_per_num := NULL;
7882 ELSE
7883 l_per_num := l_acct_date_num;
7884 END IF;
7885 CLOSE c;
7886 RETURN l_per_num;
7887 END;
7888
7889 FUNCTION inv_period_id
7890 (p_trx_date IN DATE
7891 ,p_rev_date IN DATE
7892 ,p_ledger_id IN NUMBER
7893 ,p_org_id IN NUMBER
7894 ,p_acct_period_id IN NUMBER)
7895 RETURN NUMBER
7896 IS
7897 CURSOR c (p_date_num IN NUMBER
7898 ,p_org_id IN NUMBER
7899 ,p_ledger_id IN NUMBER)
7900 IS
7901 SELECT oap.acct_period_id
7902 FROM CST_LISTS_TEMP list
7903 , gl_period_statuses glp
7904 , org_acct_periods oap
7905 WHERE VARCHAR_1 <> 'TIMEZONE'
7906 AND p_date_num BETWEEN
7907 list.NUMBER_1 -- start_date
7908 AND list.NUMBER_2 -- end_date
7909 AND list.list_id = glp.effective_period_num
7910 AND glp.application_id = 101
7911 AND glp.ledger_id = p_ledger_id
7912 AND oap.organization_id = p_org_id
7913 AND glp.period_name = oap.period_name;
7914 l_return NUMBER;
7915 l_date DATE;
7916 l_date_num NUMBER;
7917 BEGIN
7918 -- Shipping after Revenue
7919 IF p_trx_date > p_rev_date THEN
7920 l_date := p_trx_date;
7921 ELSE
7922 l_date := p_rev_date;
7923 END IF;
7924 l_date_num := TO_NUMBER(TO_CHAR(l_date,'YYYYMMDD'));
7925 OPEN c(l_date_num
7926 ,p_org_id
7927 ,p_ledger_id);
7928 FETCH c INTO l_return;
7929 IF c%NOTFOUND THEN
7930 l_return := p_acct_period_id;
7931 END IF;
7932 CLOSE c;
7933 RETURN l_return;
7934 END;
7935
7936 END CST_RevenueCogsMatch_PVT;