DBA Data[Home] [Help]

PACKAGE BODY: APPS.CSTPPWAS

Source


1 PACKAGE BODY CSTPPWAS AS
2 /* $Header: CSTPWASB.pls 120.45.12020000.3 2012/12/19 12:04:06 penpan ship $ */
3 
4 G_MAX_RECORDS CONSTANT NUMBER := 1000;
5 G_PKG_NAME CONSTANT VARCHAR2(30) := 'CSTPPWAS';
6 G_LOG_LEVEL CONSTANT NUMBER := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
7 
8 /*---------------------------------------------------------------------------*
9 |  PRIVATE PROCEDURE                                                         |
10 |       insert_wip_costs                                                     |
11 *----------------------------------------------------------------------------*/
12 PROCEDURE insert_wip_costs (
13         p_pac_period_id           IN          NUMBER,
14         p_prior_period_id         IN          NUMBER,
15         p_cost_group_id           IN          NUMBER,
16         p_cost_type_id            IN          NUMBER,
17         p_item_id                 IN          NUMBER,
18         p_entity_id               IN          NUMBER,
19         p_line_id                 IN          NUMBER,
20         p_txn_id                  IN          NUMBER,
21         p_net_qty                 IN          NUMBER,
22         p_completed_assembly_qty  IN          NUMBER,
23         p_user_id                 IN          NUMBER,
24         p_final_completion_flag   IN          NUMBER,
25         p_start_date              IN          DATE,
26         p_end_date                IN          DATE,
27         p_login_id                IN          NUMBER,
28         p_request_id              IN          NUMBER,
29         p_prog_id                 IN          NUMBER DEFAULT -1,
30         p_prog_app_id             IN          NUMBER DEFAULT -1,
31         x_err_num                 OUT NOCOPY  NUMBER,
32         x_err_code                OUT NOCOPY  VARCHAR2,
33         x_err_msg                 OUT NOCOPY  VARCHAR2)
34 IS
35 l_stmt_num                      NUMBER;
36 l_err_num                       NUMBER;
37 l_err_code                      VARCHAR2(240);
38 l_err_msg                       VARCHAR2(240);
39 
40 l_transaction_action_id         NUMBER; --Aded for R12 PAC enhancement
41 l_transaction_source_type_id    NUMBER; --Aded for R12 PAC enhancement
42 l_net_qty                       NUMBER; --Aded for R12 PAC enhancement
43 l_primary_quantity              NUMBER; --Aded for R12 PAC enhancement
44 l_details                       NUMBER; --Aded for R12 PAC enhancement
45 l_job                           NUMBER;
46 l_prior_completed_units         NUMBER := 0;
47 
48 l_api_name            CONSTANT VARCHAR2(30) := 'insert_wip_costs';
49 l_full_name           CONSTANT VARCHAR2(60) := G_PKG_NAME || '.' || l_api_name;
50 l_module              CONSTANT VARCHAR2(60) := 'cst.plsql.' || l_full_name;
51 
52 l_uLog  CONSTANT BOOLEAN := FND_LOG.LEVEL_UNEXPECTED >= G_LOG_LEVEL AND FND_LOG.TEST (FND_LOG.LEVEL_UNEXPECTED, l_module);
53 l_errorLog CONSTANT BOOLEAN := l_uLog AND (FND_LOG.LEVEL_ERROR >= G_LOG_LEVEL);
54 l_exceptionLog CONSTANT BOOLEAN := l_errorLog AND (FND_LOG.LEVEL_EXCEPTION >= G_LOG_LEVEL);
55 l_eventLog CONSTANT BOOLEAN := l_exceptionLog AND (FND_LOG.LEVEL_EVENT >= G_LOG_LEVEL);
56 l_pLog CONSTANT BOOLEAN := l_eventLog AND (FND_LOG.LEVEL_PROCEDURE >= G_LOG_LEVEL);
57 l_sLog CONSTANT BOOLEAN := l_pLog AND (FND_LOG.LEVEL_STATEMENT >= G_LOG_LEVEL);
58 
59 BEGIN
60 
61      IF (l_pLog) THEN
62 
63            FND_LOG.STRING (FND_LOG.LEVEL_PROCEDURE,
64                            l_module || '.begin',
65                            l_api_name || ' <<< Parameters:
66                            p_pac_period_id = ' || p_pac_period_id || '
67                            p_prior_period_id = ' || p_prior_period_id || '
68                            p_item_id = ' || p_item_id || '
69                            p_entity_id = ' || p_entity_id || '
70                            p_line_id = ' || p_line_id || '
71                            p_net_qty = ' || p_net_qty || '
72                            p_completed_assembly_qty = ' || p_completed_assembly_qty ||'
73                            p_final_completion_flag = ' || p_final_completion_flag ||'
74                            p_txn_id = ' || p_txn_id );
75      END IF;
76 
77        ----------------------------------------------------------------------
78        -- Initialize Variables
79        ----------------------------------------------------------------------
80        l_stmt_num := 0;
81        l_err_num := 0;
82        l_err_code := '';
83        l_err_msg := '';
84        l_details := 0;
85        l_job  := 0;
86 
87        --------------------------------------------------------------------------
88        -- Get the transaction_action_id, transaction_source_id and primary_qty
89        --------------------------------------------------------------------------
90        l_stmt_num := 10;
91        SELECT mmt.transaction_action_id,
92               mmt.transaction_source_type_id ,
93               mmt.primary_quantity
94        INTO   l_transaction_action_id,
95               l_transaction_source_type_id,
96               l_primary_quantity
97        FROM   mtl_material_transactions mmt
98        WHERE  mmt.transaction_id = p_txn_id;
99 
100        ------------------------------------------------------------
101        -- In case Asembly Returns cancels Assembly completions then
102        -- Get the completed Assembly Units in this period
103        ------------------------------------------------------------
104        l_stmt_num := 20;
105        IF (p_net_qty = 0 AND p_final_completion_flag = 1 AND l_transaction_action_id = 31 AND l_transaction_source_type_id =5) THEN
106             l_net_qty := p_completed_assembly_qty;
107        ELSE
108            l_net_qty := p_net_qty;
109        END IF;
110 
111       -------------------------------------------------------------------------------------------
112       -- Assembly return transaction needs to relieved at prior period Avg of Relieved cost
113       -- In case of p_net_qty = 0 and final_completion transaction exists
114       -------------------------------------------------------------------------------------------
115        IF ( p_net_qty = 0 AND p_final_completion_flag = 1 AND
116             l_transaction_action_id = 32 AND l_transaction_source_type_id = 5)  THEN-- Assembly Return
117           -----------------------------------------------------
118           -- PAC prior period exists
119           -----------------------------------------------------
120           l_stmt_num := 30;
121 
122 	                                    /* Check if any assembly units were relieved before this period */
123 	  SELECT COUNT(wppb.PAC_PERIOD_ID), Max(Nvl(relieved_assembly_units,0))
124 	    INTO l_job, l_prior_completed_units
125 	    FROM WIP_PAC_PERIOD_BALANCES wppb
126 	   WHERE wppb.WIP_ENTITY_ID = p_entity_id
127 	     AND wppb.PAC_PERIOD_ID = p_prior_period_id
128 	     AND NVL(wppb.line_id,-99) = decode(wppb.wip_entity_type, 4, -99, NVL(p_line_id,-99));
129 
130 	    -- Statement level log message for FND logging
131           IF (l_sLog) THEN
132               FND_LOG.STRING (FND_LOG.LEVEL_STATEMENT,
133                               l_module || '.'||l_stmt_num,
134                               'l_transaction_action_id :' || l_transaction_action_id || ','||
135                               'l_transaction_source_type_id :'  || l_transaction_source_type_id || ','||
136                               'l_net_qty :' ||  l_net_qty || ','||
137                               'l_job :'  || l_job);
138           END IF;
139 
140           l_stmt_num := 35;
141           IF ( p_prior_period_id <> -1  AND l_job <> 0 AND l_prior_completed_units <> 0) THEN
142 
143                INSERT ALL
144                 WHEN pp_pl_material_out <> 0 THEN
145                    -- Previous Level  and Material cost element
146                    INTO mtl_pac_txn_cost_details
147                        (pac_period_id,
148                         cost_group_id,
149                         cost_type_id ,
150                         inventory_item_id,
151                         transaction_id,
152                         cost_element_id,
153                         level_type,
154                         transaction_cost,
155                         wip_variance, -- New Column
156                         last_update_date,
157                         last_updated_by,
158                         creation_date,
159                         created_by,
160                         request_id,
161                         program_application_id,
162                         program_id,
163                         program_update_date,
164                         last_update_login)
165                   VALUES(p_pac_period,
166                          p_cost_group,
167                          p_cost_type,
168                          p_item,
169                          p_txn,
170                          1,
171                          2,
172                          pp_pl_material_out,
173                          0, -- New column value
174                          SYSDATE,
175                          p_user,
176                          SYSDATE,
177                          p_user,
178                          p_request,
179                          p_prog_app,
180                          p_prog,
181                          SYSDATE,
182                          p_login)
183                 -- Previous Level and Material Overhead cost element
184                 WHEN pp_pl_material_overhead_out <> 0 THEN
185                    INTO mtl_pac_txn_cost_details
186                        (pac_period_id,
187                         cost_group_id,
188                         cost_type_id ,
189                         inventory_item_id,
190                         transaction_id,
191                         cost_element_id,
192                         level_type,
193                         transaction_cost,
194                         wip_variance, -- New Column
195                         last_update_date,
196                         last_updated_by,
197                         creation_date,
198                         created_by,
199                         request_id,
200                         program_application_id,
201                         program_id,
202                         program_update_date,
203                         last_update_login)
204                   VALUES(p_pac_period,
205                          p_cost_group,
206                          p_cost_type,
207                          p_item,
208                          p_txn,
209                          2,
210                          2,
211                          pp_pl_material_overhead_out,
212                          0, -- New column value
213                          SYSDATE,
214                          p_user,
215                          SYSDATE,
216                          p_user,
217                          p_request,
218                          p_prog_app,
219                          p_prog,
220                          SYSDATE,
221                          p_login)
222                    -- Previous Level and Resource cost element
223                    WHEN pp_pl_resource_out <> 0 THEN
224                    INTO mtl_pac_txn_cost_details
225                        (pac_period_id,
226                         cost_group_id,
227                         cost_type_id ,
228                         inventory_item_id,
229                         transaction_id,
230                         cost_element_id,
231                         level_type,
232                         transaction_cost,
233                         wip_variance, -- New Column
234                         last_update_date,
235                         last_updated_by,
236                         creation_date,
237                         created_by,
238                         request_id,
239                         program_application_id,
240                         program_id,
241                         program_update_date,
242                         last_update_login)
243                   VALUES(p_pac_period,
244                          p_cost_group,
245                          p_cost_type,
246                          p_item,
247                          p_txn,
248                          3,
249                          2,
250                          pp_pl_resource_out,
251                          0, -- New column value
252                          SYSDATE,
253                          p_user,
254                          SYSDATE,
255                          p_user,
256                          p_request,
257                          p_prog_app,
258                          p_prog,
259                          SYSDATE,
260                          p_login)
261                    -- Previous Level and Outsideprocessing cost element
262                    WHEN pp_pl_outside_processing_out <> 0 THEN
263                    INTO mtl_pac_txn_cost_details
264                        (pac_period_id,
265                         cost_group_id,
266                         cost_type_id ,
267                         inventory_item_id,
268                         transaction_id,
269                         cost_element_id,
270                         level_type,
271                         transaction_cost,
272                         wip_variance, -- New Column
273                         last_update_date,
274                         last_updated_by,
275                         creation_date,
276                         created_by,
277                         request_id,
278                         program_application_id,
279                         program_id,
280                         program_update_date,
281                         last_update_login)
282                   VALUES(p_pac_period,
283                          p_cost_group,
284                          p_cost_type,
285                          p_item,
286                          p_txn,
287                          4,
288                          2,
289                          pp_pl_outside_processing_out,
290                          0, -- New column value
291                          SYSDATE,
292                          p_user,
293                          SYSDATE,
294                          p_user,
295                          p_request,
296                          p_prog_app,
297                          p_prog,
298                          SYSDATE,
299                          p_login)
300                    -- Previous Level and Overhead cost element
301                    WHEN pp_pl_overhead_out <> 0 THEN
302                    INTO mtl_pac_txn_cost_details
303                        (pac_period_id,
304                         cost_group_id,
305                         cost_type_id ,
306                         inventory_item_id,
307                         transaction_id,
308                         cost_element_id,
309                         level_type,
310                         transaction_cost,
311                         wip_variance, -- New Column
312                         last_update_date,
313                         last_updated_by,
314                         creation_date,
315                         created_by,
316                         request_id,
317                         program_application_id,
318                         program_id,
319                         program_update_date,
320                         last_update_login)
321                   VALUES(p_pac_period,
322                          p_cost_group,
323                          p_cost_type,
324                          p_item,
325                          p_txn,
326                          5,
327                          2,
328                          pp_pl_overhead_out,
329                          0, -- New column value
330                          SYSDATE,
331                          p_user,
332                          SYSDATE,
333                          p_user,
334                          p_request,
335                          p_prog_app,
336                          p_prog,
337                          SYSDATE,
338                          p_login)
339                    -- This Level and Resource cost element
340                    WHEN pp_tl_resource_out <> 0 THEN
341                    INTO mtl_pac_txn_cost_details
342                        (pac_period_id,
343                         cost_group_id,
344                         cost_type_id ,
345                         inventory_item_id,
346                         transaction_id,
347                         cost_element_id,
348                         level_type,
349                         transaction_cost,
350                         wip_variance, -- New Column
351                         last_update_date,
352                         last_updated_by,
353                         creation_date,
354                         created_by,
355                         request_id,
356                         program_application_id,
357                         program_id,
358                         program_update_date,
359                         last_update_login)
360                   VALUES(p_pac_period,
361                          p_cost_group,
362                          p_cost_type,
363                          p_item,
364                          p_txn,
365                          3,
366                          1,
367                          pp_tl_resource_out,
368                          0, -- New column value
369                          SYSDATE,
370                          p_user,
371                          SYSDATE,
372                          p_user,
373                          p_request,
374                          p_prog_app,
375                          p_prog,
376                          SYSDATE,
377                          p_login)
378                    -- This Level and Overhead  cost element
379                    WHEN pp_tl_overhead_out <> 0 THEN
380                    INTO mtl_pac_txn_cost_details
381                        (pac_period_id,
382                         cost_group_id,
383                         cost_type_id ,
384                         inventory_item_id,
385                         transaction_id,
386                         cost_element_id,
387                         level_type,
388                         transaction_cost,
389                         wip_variance, -- New Column
390                         last_update_date,
391                         last_updated_by,
392                         creation_date,
393                         created_by,
394                         request_id,
395                         program_application_id,
396                         program_id,
397                         program_update_date,
398                         last_update_login)
399                   VALUES(p_pac_period,
400                          p_cost_group,
401                          p_cost_type,
402                          p_item,
403                          p_txn,
404                          5,
405                          1,
406                          pp_tl_overhead_out,
407                          0, -- New column value
408                          SYSDATE,
409                          p_user,
410                          SYSDATE,
411                          p_user,
412                          p_request,
413                          p_prog_app,
414                          p_prog,
415                          SYSDATE,
416                          p_login)
417                    -- This Level and Outsideprocessing cost element
418                    WHEN pp_tl_outside_processing_out <> 0 THEN
419                         INTO mtl_pac_txn_cost_details
420                        (pac_period_id,
421                         cost_group_id,
422                         cost_type_id ,
423                         inventory_item_id,
424                         transaction_id,
425                         cost_element_id,
426                         level_type,
427                         transaction_cost,
428                         wip_variance, -- New Column
429                         last_update_date,
430                         last_updated_by,
431                         creation_date,
432                         created_by,
433                         request_id,
434                         program_application_id,
435                         program_id,
436                         program_update_date,
437                         last_update_login)
438                   VALUES(p_pac_period,
439                          p_cost_group,
440                          p_cost_type,
441                          p_item,
442                          p_txn,
443                          4,
444                          1,
445                          pp_tl_outside_processing_out,
446                          0, -- New column value
447                          SYSDATE,
448                          p_user,
449                          SYSDATE,
450                          p_user,
451                          p_request,
452                          p_prog_app,
453                          p_prog,
454                          SYSDATE,
455                          p_login)
456                   -- Create 0 TL Material instead of 0 PL Material when there is no non-zero cost details
457                   WHEN (pp_pl_material_out = 0 AND pp_pl_material_overhead_out = 0 AND pp_pl_resource_out = 0 AND
458                         pp_pl_outside_processing_out = 0 AND pp_pl_overhead_out = 0 AND pp_tl_resource_out = 0 AND
459                         pp_tl_outside_processing_out = 0 AND pp_tl_overhead_out = 0) THEN
460                   INTO mtl_pac_txn_cost_details
461                        (pac_period_id,
462                        cost_group_id,
463                        cost_type_id ,
464                        inventory_item_id,
465                        transaction_id,
466                        cost_element_id,
467                        level_type,
468                        transaction_cost,
469                        last_update_date,
470                        last_updated_by,
471                        creation_date,
472                        created_by,
473                        request_id,
474                        program_application_id,
475                        program_id,
476                        program_update_date,
477                        last_update_login)
478                VALUES (p_pac_period,
479                        p_cost_group,
480                        p_cost_type,
481                        p_item,
482                        p_txn,
483                        1, -- Material Cost Element
484                        1, -- This Level
485                        0,-- Zero Cost
486                        sysdate,
487                        p_user,
488                        sysdate,
489                        p_user,
490                        p_request,
491                        p_prog_app,
492                        p_prog,
493                        sysdate,
494                        p_login)
495 
496                   SELECT p_pac_period_id p_pac_period,
497                          p_cost_group_id p_cost_group,
498                          p_cost_type_id  p_cost_type,
499                          p_item_id p_item,
500                          p_txn_id p_txn,
501                          SUM(nvl(wppb.pl_material_out/
502                                      decode(nvl(relieved_assembly_units,0),0,1,relieved_assembly_units),0)) pp_pl_material_out,
503                          SUM(nvl(wppb.pl_material_overhead_out/
504                                      decode(nvl(relieved_assembly_units,0),0,1,relieved_assembly_units),0)) pp_pl_material_overhead_out,
505                          SUM(nvl(wppb.pl_resource_out/
506                                      decode(nvl(relieved_assembly_units,0),0,1,relieved_assembly_units),0)) pp_pl_resource_out,
507                          SUM(nvl(wppb.pl_outside_processing_out/
508                                      decode(nvl(relieved_assembly_units,0),0,1,relieved_assembly_units),0)) pp_pl_outside_processing_out,
509                          SUM(nvl(wppb.pl_overhead_out/
510                                      decode(nvl(relieved_assembly_units,0),0,1,relieved_assembly_units),0)) pp_pl_overhead_out,
511                          SUM(nvl(wppb.tl_resource_out/
512                                      decode(nvl(relieved_assembly_units,0),0,1,relieved_assembly_units),0)) pp_tl_resource_out,
513                          SUM(nvl(wppb.tl_outside_processing_out/
514                                      decode(nvl(relieved_assembly_units,0),0,1,relieved_assembly_units),0)) pp_tl_outside_processing_out,
515                          SUM(nvl(wppb.tl_overhead_out/
516                                      decode(nvl(relieved_assembly_units,0),0,1,relieved_assembly_units),0)) pp_tl_overhead_out,
517                          p_user_id p_user,
518                          p_login_id p_login,
519                          p_request_id p_request,
520                          p_prog_app_id p_prog_app,
521                          p_prog_id p_prog
522                   FROM   wip_pac_period_balances wppb
523                   WHERE  wppb.pac_period_id = p_prior_period_id
524                   AND    wppb.cost_group_id = p_cost_group_id
525                   AND    wppb.wip_entity_id = p_entity_id
526                   AND    NVL(wppb.line_id,-99) = decode(wppb.wip_entity_type, 4, -99, NVL(p_line_id,-99));
527           ELSE
528 /*               INSERT INTO mtl_pac_txn_cost_details
529                     (pac_period_id,
530                      cost_group_id,
531                      cost_type_id ,
532                      inventory_item_id,
533                      transaction_id,
534                      cost_element_id,
535                      level_type,
536                      transaction_cost,
537                      last_update_date,
538                      last_updated_by,
539                      creation_date,
540                      created_by,
541                      request_id,
542                      program_application_id,
543                      program_id,
544                      program_update_date,
545                      last_update_login)
546                VALUES(p_pac_period_id,
547                       p_cost_group_id,
548                       p_cost_type_id,
549                       p_item_id,
550                       p_txn_id,
551                       1, -- Material Cost Element
552                       1, -- This Level
553                       0,-- Zero Cost
554                       sysdate,
555                       p_user_id,
556                       sysdate,
557                       p_user_id,
558                       p_request_id,
559                       p_prog_app_id,
560                       p_prog_id,
561                       sysdate,
562                       p_login_id);*/
563 
564 	   l_net_qty := p_completed_assembly_qty;
565 
566            INSERT ALL
567                -- Previous Level and Material cost element
568                WHEN (pl_material_temp <> 0 OR pl_material_temp_var <> 0) THEN
569                INTO mtl_pac_txn_cost_details
570                     (pac_period_id,
571                      cost_group_id,
572                      cost_type_id,
573                      inventory_item_id,
574                      transaction_id,
575                      cost_element_id,
576                      level_type,
577                      transaction_cost,
578                      wip_variance, -- New Column
579                      last_update_date,
580                      last_updated_by,
581                      creation_date,
582                      created_by,
583                      request_id,
584                      program_application_id,
585                      program_id,
586                      program_update_date,
587                      last_update_login)
588                VALUES(p_pac_period,
589                       p_cost_group,
590                       p_cost_type,
591                       p_item,
592                       p_txn,
593                       1,
594                       2,
595                       pl_material_temp,
596                       pl_material_temp_var,-- New column value
597                       sysdate,
598                       p_user,
599                       sysdate,
600                       p_user,
601                       p_request,
602                       p_prog_app,
603                       p_prog,
604                       sysdate,
605                       p_login )
606                -- Previous Level and Material Overhead cost element
607                WHEN (pl_material_overhead_temp <> 0 OR pl_material_overhead_temp_var <> 0) THEN
608                INTO mtl_pac_txn_cost_details
609                     (pac_period_id,
610                      cost_group_id,
611                      cost_type_id,
612                      inventory_item_id,
613                      transaction_id,
614                      cost_element_id,
615                      level_type,
616                      transaction_cost,
617                      wip_variance, -- New Column
618                      last_update_date,
619                      last_updated_by,
620                      creation_date,
621                      created_by,
622                      request_id,
623                      program_application_id,
624                      program_id,
625                      program_update_date,
626                      last_update_login)
627                VALUES(p_pac_period,
628                       p_cost_group,
629                       p_cost_type,
630                       p_item,
631                       p_txn,
632                       2,
633                       2,
634                       pl_material_overhead_temp,
635                       pl_material_overhead_temp_var,-- New column value
636                       sysdate,
637                       p_user,
638                       sysdate,
639                       p_user,
640                       p_request,
641                       p_prog_app,
642                       p_prog,
643                       sysdate,
644                       p_login )
645                -- Previous Level and Resource cost element
646                WHEN (pl_resource_temp <> 0 OR pl_resource_temp_var <> 0) THEN
647                INTO mtl_pac_txn_cost_details
648                     (pac_period_id,
649                      cost_group_id,
650                      cost_type_id,
651                      inventory_item_id,
652                      transaction_id,
653                      cost_element_id,
654                      level_type,
655                      transaction_cost,
656                      wip_variance, -- New Column
657                      last_update_date,
658                      last_updated_by,
659                      creation_date,
660                      created_by,
661                      request_id,
662                      program_application_id,
663                      program_id,
664                      program_update_date,
665                      last_update_login)
666               VALUES (p_pac_period,
667                       p_cost_group,
668                       p_cost_type,
669                       p_item,
670                       p_txn,
671                       3,
672                       2,
673                       pl_resource_temp,
674                       pl_resource_temp_var,-- New column value
675                       sysdate,
676                       p_user,
677                       sysdate,
678                       p_user,
679                       p_request,
680                       p_prog_app,
681                       p_prog,
682                       sysdate,
683                       p_login )
684                -- Previous Level and Outside Processing cost element
685                WHEN (pl_outside_processing_temp <>0 OR pl_outside_processing_temp_var <> 0) THEN
686                INTO mtl_pac_txn_cost_details
687                     (pac_period_id,
688                      cost_group_id,
689                      cost_type_id,
690                      inventory_item_id,
691                      transaction_id,
692                      cost_element_id,
693                      level_type,
694                      transaction_cost,
695                      wip_variance, -- New Column
696                      last_update_date,
697                      last_updated_by,
698                      creation_date,
699                      created_by,
700                      request_id,
701                      program_application_id,
702                      program_id,
703                      program_update_date,
704                      last_update_login)
705               VALUES (p_pac_period,
706                       p_cost_group,
707                       p_cost_type,
708                       p_item,
709                       p_txn,
710                       4,
711                       2,
712                       pl_outside_processing_temp,
713                       pl_outside_processing_temp_var,-- New column value
714                       sysdate,
715                       p_user,
716                       sysdate,
717                       p_user,
718                       p_request,
719                       p_prog_app,
720                       p_prog,
721                       sysdate,
722                       p_login )
723                -- Previous Level and Overhead cost element
724                WHEN (pl_overhead_temp <>0 OR pl_overhead_temp_var <> 0)  THEN
725                INTO mtl_pac_txn_cost_details
726                     (pac_period_id,
727                      cost_group_id,
728                      cost_type_id,
729                      inventory_item_id,
730                      transaction_id,
731                      cost_element_id,
732                      level_type,
733                      transaction_cost,
734                      wip_variance, -- New Column
735                      last_update_date,
736                      last_updated_by,
737                      creation_date,
738                      created_by,
739                      request_id,
740                      program_application_id,
741                      program_id,
742                      program_update_date,
743                      last_update_login)
744               VALUES (p_pac_period,
745                       p_cost_group,
746                       p_cost_type,
747                       p_item,
748                       p_txn,
749                       5,
750                       2,
751                       pl_overhead_temp,
752                       pl_overhead_temp_var,-- New column value
753                       sysdate,
754                       p_user,
755                       sysdate,
756                       p_user,
757                       p_request,
758                       p_prog_app,
759                       p_prog,
760                       sysdate,
761                       p_login )
762                -- This level and Resource Cost Element
763                WHEN (tl_resource_temp <> 0) THEN
764                INTO mtl_pac_txn_cost_details
765                     (pac_period_id,
766                      cost_group_id,
767                      cost_type_id ,
768                      inventory_item_id,
769                      transaction_id,
770                      cost_element_id,
771                      level_type,
772                      transaction_cost,
773                      last_update_date,
774                      last_updated_by,
775                      creation_date,
776                      created_by,
777                      request_id,
778                      program_application_id,
779                      program_id,
780                      program_update_date,
781                      last_update_login)
782                VALUES(p_pac_period,
783                       p_cost_group,
784                       p_cost_type,
785                       p_item,
786                       p_txn,
787                       3,
788                       1,
789                       tl_resource_temp,
790                       sysdate,
791                       p_user,
792                       sysdate,
793                       p_user,
794                       p_request,
795                       p_prog_app,
796                       p_prog,
797                       sysdate,
798                       p_login)
799                -- This Level and Outside Processing cost element
800                WHEN tl_outside_processing_temp <>0 THEN
801                INTO mtl_pac_txn_cost_details
802                     (pac_period_id,
803                      cost_group_id,
804                      cost_type_id ,
805                      inventory_item_id,
806                      transaction_id,
807                      cost_element_id,
808                      level_type,
809                      transaction_cost,
810                      last_update_date,
811                      last_updated_by,
812                      creation_date,
813                      created_by,
814                      request_id,
815                      program_application_id,
816                      program_id,
817                      program_update_date,
818                      last_update_login)
819                VALUES(p_pac_period,
820                       p_cost_group,
821                       p_cost_type,
822                       p_item,
823                       p_txn,
824                       4,
825                       1,
826                       tl_outside_processing_temp,
827                       sysdate,
828                       p_user,
829                       sysdate,
830                       p_user,
831                       p_request,
832                       p_prog_app,
833                       p_prog,
834                       sysdate,
835                       p_login)
836                -- This Level and Overhead cost element
837                WHEN tl_overhead_temp <> 0 THEN
838                INTO mtl_pac_txn_cost_details
839                     (pac_period_id,
840                      cost_group_id,
841                      cost_type_id ,
842                      inventory_item_id,
843                      transaction_id,
844                      cost_element_id,
845                      level_type,
846                      transaction_cost,
847                      last_update_date,
848                      last_updated_by,
849                      creation_date,
850                      created_by,
851                      request_id,
852                      program_application_id,
853                      program_id,
854                      program_update_date,
855                      last_update_login)
856                VALUES(p_pac_period,
857                       p_cost_group,
858                       p_cost_type,
859                       p_item,
860                       p_txn,
861                       5,
862                       1,
863                       tl_overhead_temp,
864                       sysdate,
865                       p_user,
866                       sysdate,
867                       p_user,
868                       p_request,
869                       p_prog_app,
870                       p_prog,
871                       sysdate,
872                       p_login)
873                -- Create 0 TL Material instead of 0 PL Material when there is no non-zero cost details
874                WHEN (pl_material_temp = 0 AND pl_material_overhead_temp = 0 AND pl_resource_temp = 0 AND
875                      pl_outside_processing_temp = 0 AND pl_overhead_temp = 0 AND tl_resource_temp = 0 AND
876                      tl_outside_processing_temp = 0 AND tl_overhead_temp = 0) THEN
877                INTO mtl_pac_txn_cost_details
878                     (pac_period_id,
879                      cost_group_id,
880                      cost_type_id ,
881                      inventory_item_id,
882                      transaction_id,
883                      cost_element_id,
884                      level_type,
885                      transaction_cost,
886                      last_update_date,
887                      last_updated_by,
888                      creation_date,
889                      created_by,
890                      request_id,
891                      program_application_id,
892                      program_id,
893                      program_update_date,
894                      last_update_login)
895                VALUES(p_pac_period,
896                       p_cost_group,
897                       p_cost_type,
898                       p_item,
899                       p_txn,
900                       1, -- Material Cost Element
901                       1, -- This Level
902                       0,-- Zero Cost
903                       sysdate,
904                       p_user,
905                       sysdate,
906                       p_user,
907                       p_request,
908                       p_prog_app,
909                       p_prog,
910                       sysdate,
911                       p_login)
912 
913             SELECT  p_pac_period_id p_pac_period,
914                     p_cost_group_id p_cost_group,
915                     p_cost_type_id  p_cost_type,
916                     p_item_id p_item,
917                     p_txn_id p_txn,
918                     NVL(SUM(wppb.pl_material_temp)/
919                                     decode(l_net_qty,0,1,l_net_qty),0) pl_material_temp,
920                     NVL(SUM(wppb.pl_material_overhead_temp)/
921                                     decode(l_net_qty,0,1,l_net_qty),0) pl_material_overhead_temp,
922                     NVL(SUM(wppb.pl_resource_temp)/
923                                     decode(l_net_qty,0,1,l_net_qty),0) pl_resource_temp,
924                     NVL(SUM(wppb.pl_outside_processing_temp)/
925                                     decode(l_net_qty,0,1,l_net_qty),0) pl_outside_processing_temp,
926                     NVL(SUM(wppb.pl_overhead_temp)/
927                                     decode(l_net_qty,0,1,l_net_qty),0) pl_overhead_temp,
928                     NVL(SUM(wppb.tl_resource_temp)/
929                                     decode(l_net_qty,0,1,l_net_qty),0) tl_resource_temp,
930                     NVL(SUM(wppb.tl_outside_processing_temp)/
931                                     decode(l_net_qty,0,1,l_net_qty),0) tl_outside_processing_temp,
932                     NVL(SUM(wppb.tl_overhead_temp)/
933                                     decode(l_net_qty,0,1,l_net_qty),0) tl_overhead_temp,
934                     -- All temp Variance Columns
935                     NVL(SUM(wppb.pl_material_temp_var)*l_primary_quantity/
936                                     decode(l_net_qty,0,1,l_net_qty),0) pl_material_temp_var,
937                     NVL(SUM(wppb.pl_material_overhead_temp_var)*l_primary_quantity/
938                                     decode(l_net_qty,0,1,l_net_qty),0) pl_material_overhead_temp_var,
939                     NVL(SUM(wppb.pl_resource_temp_var)*l_primary_quantity/
940                                     decode(l_net_qty,0,1,l_net_qty),0) pl_resource_temp_var,
941                     NVL(SUM(wppb.pl_outside_processing_temp_var)*l_primary_quantity/
942                                     decode(l_net_qty,0,1,l_net_qty),0) pl_outside_processing_temp_var,
943                     NVL(SUM(wppb.pl_overhead_temp_var)*l_primary_quantity/
944                                     decode(l_net_qty,0,1,l_net_qty),0) pl_overhead_temp_var,
945                     p_user_id p_user,
946                     p_login_id p_login,
947                     p_request_id p_request,
948                     p_prog_app_id p_prog_app,
949                     p_prog_id p_prog
950             FROM    wip_pac_period_balances wppb
951             WHERE   wppb.pac_period_id = p_pac_period_id
952             AND     wppb.cost_group_id = p_cost_group_id
953             AND     wppb.wip_entity_id = p_entity_id
954             AND     NVL(wppb.line_id,-99) = decode(wppb.wip_entity_type, 4, -99, NVL(p_line_id,-99));
955 
956           END IF; -- End of IF p_prior_period_id <> -1
957 
958       ELSE
959           --------------------------------------------------
960           --
961           --------------------------------------------------
962            l_stmt_num := 40;
963            INSERT ALL
964                -- Previous Level and Material cost element
965                WHEN (pl_material_temp <> 0 OR pl_material_temp_var <> 0) THEN
966                INTO mtl_pac_txn_cost_details
967                     (pac_period_id,
968                      cost_group_id,
969                      cost_type_id,
970                      inventory_item_id,
971                      transaction_id,
972                      cost_element_id,
973                      level_type,
974                      transaction_cost,
975                      wip_variance, -- New Column
976                      last_update_date,
977                      last_updated_by,
978                      creation_date,
979                      created_by,
980                      request_id,
981                      program_application_id,
982                      program_id,
983                      program_update_date,
984                      last_update_login)
985                VALUES(p_pac_period,
986                       p_cost_group,
987                       p_cost_type,
988                       p_item,
989                       p_txn,
990                       1,
991                       2,
992                       pl_material_temp,
993                       pl_material_temp_var,-- New column value
994                       sysdate,
995                       p_user,
996                       sysdate,
997                       p_user,
998                       p_request,
999                       p_prog_app,
1000                       p_prog,
1001                       sysdate,
1002                       p_login )
1003                -- Previous Level and Material Overhead cost element
1004                WHEN (pl_material_overhead_temp <> 0 OR pl_material_overhead_temp_var <> 0) THEN
1005                INTO mtl_pac_txn_cost_details
1006                     (pac_period_id,
1007                      cost_group_id,
1008                      cost_type_id,
1009                      inventory_item_id,
1010                      transaction_id,
1011                      cost_element_id,
1012                      level_type,
1013                      transaction_cost,
1014                      wip_variance, -- New Column
1015                      last_update_date,
1016                      last_updated_by,
1017                      creation_date,
1018                      created_by,
1019                      request_id,
1020                      program_application_id,
1021                      program_id,
1022                      program_update_date,
1023                      last_update_login)
1024                VALUES(p_pac_period,
1025                       p_cost_group,
1026                       p_cost_type,
1027                       p_item,
1028                       p_txn,
1029                       2,
1030                       2,
1031                       pl_material_overhead_temp,
1032                       pl_material_overhead_temp_var,-- New column value
1033                       sysdate,
1034                       p_user,
1035                       sysdate,
1036                       p_user,
1037                       p_request,
1038                       p_prog_app,
1039                       p_prog,
1040                       sysdate,
1041                       p_login )
1042                -- Previous Level and Resource cost element
1043                WHEN (pl_resource_temp <> 0 OR pl_resource_temp_var <> 0) THEN
1044                INTO mtl_pac_txn_cost_details
1045                     (pac_period_id,
1046                      cost_group_id,
1047                      cost_type_id,
1048                      inventory_item_id,
1049                      transaction_id,
1050                      cost_element_id,
1051                      level_type,
1052                      transaction_cost,
1053                      wip_variance, -- New Column
1054                      last_update_date,
1055                      last_updated_by,
1056                      creation_date,
1057                      created_by,
1058                      request_id,
1059                      program_application_id,
1060                      program_id,
1061                      program_update_date,
1062                      last_update_login)
1063               VALUES (p_pac_period,
1064                       p_cost_group,
1065                       p_cost_type,
1066                       p_item,
1067                       p_txn,
1068                       3,
1069                       2,
1070                       pl_resource_temp,
1071                       pl_resource_temp_var,-- New column value
1072                       sysdate,
1073                       p_user,
1074                       sysdate,
1075                       p_user,
1076                       p_request,
1077                       p_prog_app,
1078                       p_prog,
1079                       sysdate,
1080                       p_login )
1081                -- Previous Level and Outside Processing cost element
1082                WHEN (pl_outside_processing_temp <>0 OR pl_outside_processing_temp_var <> 0) THEN
1083                INTO mtl_pac_txn_cost_details
1084                     (pac_period_id,
1085                      cost_group_id,
1086                      cost_type_id,
1087                      inventory_item_id,
1088                      transaction_id,
1089                      cost_element_id,
1090                      level_type,
1091                      transaction_cost,
1092                      wip_variance, -- New Column
1093                      last_update_date,
1094                      last_updated_by,
1095                      creation_date,
1096                      created_by,
1097                      request_id,
1098                      program_application_id,
1099                      program_id,
1100                      program_update_date,
1101                      last_update_login)
1102               VALUES (p_pac_period,
1103                       p_cost_group,
1104                       p_cost_type,
1105                       p_item,
1106                       p_txn,
1107                       4,
1108                       2,
1109                       pl_outside_processing_temp,
1110                       pl_outside_processing_temp_var,-- New column value
1111                       sysdate,
1112                       p_user,
1113                       sysdate,
1114                       p_user,
1115                       p_request,
1116                       p_prog_app,
1117                       p_prog,
1118                       sysdate,
1119                       p_login )
1120                -- Previous Level and Overhead cost element
1121                WHEN (pl_overhead_temp <>0 OR pl_overhead_temp_var <> 0)  THEN
1122                INTO mtl_pac_txn_cost_details
1123                     (pac_period_id,
1124                      cost_group_id,
1125                      cost_type_id,
1126                      inventory_item_id,
1127                      transaction_id,
1128                      cost_element_id,
1129                      level_type,
1130                      transaction_cost,
1131                      wip_variance, -- New Column
1132                      last_update_date,
1133                      last_updated_by,
1134                      creation_date,
1135                      created_by,
1136                      request_id,
1137                      program_application_id,
1138                      program_id,
1139                      program_update_date,
1140                      last_update_login)
1141               VALUES (p_pac_period,
1142                       p_cost_group,
1143                       p_cost_type,
1144                       p_item,
1145                       p_txn,
1146                       5,
1147                       2,
1148                       pl_overhead_temp,
1149                       pl_overhead_temp_var,-- New column value
1150                       sysdate,
1151                       p_user,
1152                       sysdate,
1153                       p_user,
1154                       p_request,
1155                       p_prog_app,
1156                       p_prog,
1157                       sysdate,
1158                       p_login )
1159                -- This level and Resource Cost Element
1160                WHEN (tl_resource_temp <> 0) THEN
1161                INTO mtl_pac_txn_cost_details
1162                     (pac_period_id,
1163                      cost_group_id,
1164                      cost_type_id ,
1165                      inventory_item_id,
1166                      transaction_id,
1167                      cost_element_id,
1168                      level_type,
1169                      transaction_cost,
1170                      last_update_date,
1171                      last_updated_by,
1172                      creation_date,
1173                      created_by,
1174                      request_id,
1175                      program_application_id,
1176                      program_id,
1177                      program_update_date,
1178                      last_update_login)
1179                VALUES(p_pac_period,
1180                       p_cost_group,
1181                       p_cost_type,
1182                       p_item,
1183                       p_txn,
1184                       3,
1185                       1,
1186                       tl_resource_temp,
1187                       sysdate,
1188                       p_user,
1189                       sysdate,
1190                       p_user,
1191                       p_request,
1192                       p_prog_app,
1193                       p_prog,
1194                       sysdate,
1195                       p_login)
1196                -- This Level and Outside Processing cost element
1197                WHEN tl_outside_processing_temp <>0 THEN
1198                INTO mtl_pac_txn_cost_details
1199                     (pac_period_id,
1200                      cost_group_id,
1201                      cost_type_id ,
1202                      inventory_item_id,
1203                      transaction_id,
1204                      cost_element_id,
1205                      level_type,
1206                      transaction_cost,
1207                      last_update_date,
1208                      last_updated_by,
1209                      creation_date,
1210                      created_by,
1211                      request_id,
1212                      program_application_id,
1213                      program_id,
1214                      program_update_date,
1215                      last_update_login)
1216                VALUES(p_pac_period,
1217                       p_cost_group,
1218                       p_cost_type,
1219                       p_item,
1220                       p_txn,
1221                       4,
1222                       1,
1223                       tl_outside_processing_temp,
1224                       sysdate,
1225                       p_user,
1226                       sysdate,
1227                       p_user,
1228                       p_request,
1229                       p_prog_app,
1230                       p_prog,
1231                       sysdate,
1232                       p_login)
1233                -- This Level and Overhead cost element
1234                WHEN tl_overhead_temp <> 0 THEN
1235                INTO mtl_pac_txn_cost_details
1236                     (pac_period_id,
1237                      cost_group_id,
1238                      cost_type_id ,
1239                      inventory_item_id,
1240                      transaction_id,
1241                      cost_element_id,
1242                      level_type,
1243                      transaction_cost,
1244                      last_update_date,
1245                      last_updated_by,
1246                      creation_date,
1247                      created_by,
1248                      request_id,
1249                      program_application_id,
1250                      program_id,
1251                      program_update_date,
1252                      last_update_login)
1253                VALUES(p_pac_period,
1254                       p_cost_group,
1255                       p_cost_type,
1256                       p_item,
1257                       p_txn,
1258                       5,
1259                       1,
1260                       tl_overhead_temp,
1261                       sysdate,
1262                       p_user,
1263                       sysdate,
1264                       p_user,
1265                       p_request,
1266                       p_prog_app,
1267                       p_prog,
1268                       sysdate,
1269                       p_login)
1270                -- Create 0 TL Material instead of 0 PL Material when there is no non-zero cost details
1271                WHEN (pl_material_temp = 0 AND pl_material_overhead_temp = 0 AND pl_resource_temp = 0 AND
1272                      pl_outside_processing_temp = 0 AND pl_overhead_temp = 0 AND tl_resource_temp = 0 AND
1273                      tl_outside_processing_temp = 0 AND tl_overhead_temp = 0) THEN
1274                INTO mtl_pac_txn_cost_details
1275                     (pac_period_id,
1276                      cost_group_id,
1277                      cost_type_id ,
1278                      inventory_item_id,
1279                      transaction_id,
1280                      cost_element_id,
1281                      level_type,
1282                      transaction_cost,
1283                      last_update_date,
1284                      last_updated_by,
1285                      creation_date,
1286                      created_by,
1287                      request_id,
1288                      program_application_id,
1289                      program_id,
1290                      program_update_date,
1291                      last_update_login)
1292                VALUES(p_pac_period,
1293                       p_cost_group,
1294                       p_cost_type,
1295                       p_item,
1296                       p_txn,
1297                       1, -- Material Cost Element
1298                       1, -- This Level
1299                       0,-- Zero Cost
1300                       sysdate,
1301                       p_user,
1302                       sysdate,
1303                       p_user,
1304                       p_request,
1305                       p_prog_app,
1306                       p_prog,
1307                       sysdate,
1308                       p_login)
1309 
1310             SELECT  p_pac_period_id p_pac_period,
1311                     p_cost_group_id p_cost_group,
1312                     p_cost_type_id  p_cost_type,
1313                     p_item_id p_item,
1314                     p_txn_id p_txn,
1315                     NVL(SUM(wppb.pl_material_temp)/
1316                                     decode(l_net_qty,0,1,l_net_qty),0) pl_material_temp,
1317                     NVL(SUM(wppb.pl_material_overhead_temp)/
1318                                     decode(l_net_qty,0,1,l_net_qty),0) pl_material_overhead_temp,
1319                     NVL(SUM(wppb.pl_resource_temp)/
1320                                     decode(l_net_qty,0,1,l_net_qty),0) pl_resource_temp,
1321                     NVL(SUM(wppb.pl_outside_processing_temp)/
1322                                     decode(l_net_qty,0,1,l_net_qty),0) pl_outside_processing_temp,
1323                     NVL(SUM(wppb.pl_overhead_temp)/
1324                                     decode(l_net_qty,0,1,l_net_qty),0) pl_overhead_temp,
1325                     NVL(SUM(wppb.tl_resource_temp)/
1326                                     decode(l_net_qty,0,1,l_net_qty),0) tl_resource_temp,
1327                     NVL(SUM(wppb.tl_outside_processing_temp)/
1328                                     decode(l_net_qty,0,1,l_net_qty),0) tl_outside_processing_temp,
1329                     NVL(SUM(wppb.tl_overhead_temp)/
1330                                     decode(l_net_qty,0,1,l_net_qty),0) tl_overhead_temp,
1331                     -- All temp Variance Columns
1332                     NVL(SUM(wppb.pl_material_temp_var)*l_primary_quantity/
1333                                     decode(l_net_qty,0,1,l_net_qty),0) pl_material_temp_var,
1334                     NVL(SUM(wppb.pl_material_overhead_temp_var)*l_primary_quantity/
1335                                     decode(l_net_qty,0,1,l_net_qty),0) pl_material_overhead_temp_var,
1336                     NVL(SUM(wppb.pl_resource_temp_var)*l_primary_quantity/
1337                                     decode(l_net_qty,0,1,l_net_qty),0) pl_resource_temp_var,
1338                     NVL(SUM(wppb.pl_outside_processing_temp_var)*l_primary_quantity/
1339                                     decode(l_net_qty,0,1,l_net_qty),0) pl_outside_processing_temp_var,
1340                     NVL(SUM(wppb.pl_overhead_temp_var)*l_primary_quantity/
1341                                     decode(l_net_qty,0,1,l_net_qty),0) pl_overhead_temp_var,
1342                     p_user_id p_user,
1343                     p_login_id p_login,
1344                     p_request_id p_request,
1345                     p_prog_app_id p_prog_app,
1346                     p_prog_id p_prog
1347             FROM    wip_pac_period_balances wppb
1348             WHERE   wppb.pac_period_id = p_pac_period_id
1349             AND     wppb.cost_group_id = p_cost_group_id
1350             AND     wppb.wip_entity_id = p_entity_id
1351             AND     NVL(wppb.line_id,-99) = decode(wppb.wip_entity_type, 4, -99, NVL(p_line_id,-99));
1352  END IF;
1353 
1354   IF (l_pLog) THEN
1355    FND_LOG.STRING (FND_LOG.LEVEL_PROCEDURE,
1356                    l_module || '.end',
1357                    l_api_name || ' >>>');
1358   END IF;
1359 
1360 EXCEPTION
1361         WHEN OTHERS THEN
1362                 IF (l_uLog) THEN
1363                    FND_LOG.STRING (FND_LOG.LEVEL_UNEXPECTED,
1364                                    l_module || '.' || l_stmt_num,
1365                                    SQLERRM);
1366                 END IF;
1367                 ROLLBACK;
1368                 fnd_file.put_line(fnd_file.log,' Exception in Insert_wip_costs');
1369                 x_err_num := SQLCODE;
1370                 x_err_code := NULL;
1371                 x_err_msg := SUBSTR('CSTPPWAS.insert_wip_costs('
1372                                 || to_char(l_stmt_num)
1373                                 || '): '
1374                                 ||SQLERRM,1,240);
1375 END insert_wip_costs;
1376 
1377 /*---------------------------------------------------------------------------*
1378 |  PRIVATE PROCEDURE:  Relief_BOM_Quantity                                   |
1379 |                                                                            |
1380 |  Design: Ray, Vinayak, Srinath and Subbu                                   |
1381 |                                                                            |
1382 |  Description:                                                              |
1383 |       This Procedure relieves the Material costs based on Predefined       |
1384 |       Materials                                                            |
1385 |                                                                            |
1386 |  Logic:                                                                    |
1387 |     The first cursor gets the operation sequence number for a given job.   |
1388 |                                                                            |
1389 |     Second cursor gets all the components at that operations.              |
1390 |                                                                            |
1391 |    Check any record exists in table CST_PAC_REQ_OPER_COST_DETAILS (cprocd) |
1392 |                                                                            |
1393 |    IF (no record exists in cprocd) THEN                                    |
1394 |       FOR  each cost elements  LOOP                                        |
1395 |                                                                            |
1396 |       END LOOP;                                                            |
1397 |    ELSE                                                                    |
1398 |       Third cursor to get the cost elements from cprocd                    |
1399 |        Relieve material costs accordingly                                  |
1400 |       END of Third cursor                                                  |
1401 |   END IF;                                                                  |
1402 |  END of Second Cursor                                                      |
1403 | End of First Cursor                                                        |
1404 |   UPDATE table WIP_PAC_PERIOD_BALANCES (wppb)                              |
1405 |    Check the job balance in this period for each cost element.             |
1406 |   If the value is negative then put these values in VAR columns            |
1407 |   END;                                                                     |
1408 *----------------------------------------------------------------------------*/
1409 PROCEDURE Relief_BOM_Quantity (
1410         p_pac_period_id               IN        NUMBER,
1411         p_prior_period_id             IN        NUMBER,
1412         p_cost_group_id               IN        NUMBER,
1413         p_cost_type_id                IN        NUMBER,
1414         p_entity_id                   IN        NUMBER,
1415         p_line_id                     IN        NUMBER,
1416         p_net_qty                     IN        NUMBER,
1417         p_final_completion_flag       IN        NUMBER,
1418         p_scrap                       IN        NUMBER,
1419         p_op_seq                      IN        NUMBER,
1420         p_start_date                  IN        DATE,
1421         p_end_date                    IN        DATE,
1422         p_login_id                    IN        NUMBER,
1423         p_user_id                     IN        NUMBER,
1424         p_request_id                  IN        NUMBER,
1425         p_prog_id                     IN        NUMBER DEFAULT -1,
1426         p_prog_app_id                 IN        NUMBER DEFAULT -1,
1427         x_err_num                     OUT NOCOPY     NUMBER,
1428         x_err_code                    OUT NOCOPY     VARCHAR2,
1429         x_err_msg                     OUT NOCOPY     VARCHAR2)
1430 IS
1431 
1432 l_lot_size                  NUMBER; -- Lot based materials project for R12
1433 l_include_comp_yield        NUMBER; -- Component yield enhancement changes in R12
1434 l_org_id                    NUMBER;
1435 l_repetitive_schedule_id    NUMBER;
1436 
1437 CURSOR c_wip_opseq IS
1438        SELECT wppb.operation_seq_num operation_seq_num
1439        FROM   WIP_PAC_PERIOD_BALANCES wppb
1440        WHERE  wppb.pac_period_id = p_pac_period_id
1441        AND    wppb.cost_group_id = p_cost_group_id
1442        AND    wppb.wip_entity_id =  p_entity_id
1443        AND    NVL(wppb.line_id, -99) = nvl(p_line_id,-99)
1444        AND    wppb.operation_seq_num <= decode(p_scrap,1,p_op_seq,
1445                                                          wppb.operation_seq_num);
1446 
1447 --Added decode for Lot based materials project for R12
1448 --Divide the value of qpa by comp_yield_factor
1449 CURSOR c_wro(c_op_sequence NUMBER) IS
1450        SELECT wro.inventory_item_id component,
1451               Decode(wro.basis_type,
1452                           2, (wro.quantity_per_assembly / l_lot_size),
1453                           wro.quantity_per_assembly) / decode(l_include_comp_yield,
1454                                                               1, nvl(wro.component_yield_factor,1),
1455                                                               1) quantity_per_assembly
1456        FROM   WIP_REQUIREMENT_OPERATIONS wro
1457        WHERE  wro.wip_entity_id = p_entity_id
1458        AND    nvl(wro.repetitive_schedule_id ,-99) = nvl(l_repetitive_schedule_id,-99)
1459        AND    wro.operation_seq_num = c_op_sequence
1460        AND    wro.wip_supply_type NOT IN (4,5,6);
1461 
1462 CURSOR c_cost_element(op_sequence_num NUMBER,component NUMBER) IS
1463        SELECT cost_element_id cst_ele_id
1464        FROM   CST_PAC_REQ_OPER_COST_DETAILS cprocd
1465        WHERE  cprocd.pac_period_id = p_pac_period_id
1466        AND    cprocd.cost_group_id = p_cost_group_id
1467        AND    cprocd.wip_entity_id = p_entity_id
1468        AND    nvl(cprocd.line_id, -99) = nvl(p_line_id,-99)
1469        AND    cprocd.operation_seq_num = op_sequence_num
1470        AND    cprocd.inventory_item_id = component;
1471 
1472 type t_cst_element_cost is table of number
1473              index by binary_integer;
1474 
1475 l_op_relieved_comp_cost t_cst_element_cost;
1476 l_job_balance t_cst_element_cost;
1477 l_prior_relieved_comp_cost t_cst_element_cost;
1478 
1479 l_applied_qty               NUMBER;
1480 l_record_exists             NUMBER;
1481 l_current_period_cost       NUMBER;
1482 l_avl_relieve_qty           NUMBER;
1483 l_avl_relieve_value         NUMBER;
1484 l_relieved_qty              NUMBER;
1485 l_skip_below_process        NUMBER;
1486 l_prior_relieved_value      NUMBER;
1487 l_prior_relieved_qty        NUMBER;
1488 l_required_qty              NUMBER;
1489 l_assembly_return_cnt       NUMBER;
1490 l_prior_completed_units     NUMBER := 0;
1491 l_entity_type               NUMBER;
1492 l_stmt_num                  NUMBER;
1493 l_err_num                   NUMBER;
1494 l_err_code                  VARCHAR2(240);
1495 l_err_msg                   VARCHAR2(240);
1496 
1497 l_api_name            CONSTANT VARCHAR2(30) := 'Relief_BOM_Quantity';
1498 l_full_name           CONSTANT VARCHAR2(60) := G_PKG_NAME || '.' || l_api_name;
1499 l_module              CONSTANT VARCHAR2(60) := 'cst.plsql.' || l_full_name;
1500 
1501 l_uLog  CONSTANT BOOLEAN := FND_LOG.LEVEL_UNEXPECTED >= G_LOG_LEVEL AND FND_LOG.TEST (FND_LOG.LEVEL_UNEXPECTED, l_module);
1502 l_errorLog CONSTANT BOOLEAN := l_uLog AND (FND_LOG.LEVEL_ERROR >= G_LOG_LEVEL);
1503 l_exceptionLog CONSTANT BOOLEAN := l_errorLog AND (FND_LOG.LEVEL_EXCEPTION >= G_LOG_LEVEL);
1504 l_eventLog CONSTANT BOOLEAN := l_exceptionLog AND (FND_LOG.LEVEL_EVENT >= G_LOG_LEVEL);
1505 l_pLog CONSTANT BOOLEAN := l_eventLog AND (FND_LOG.LEVEL_PROCEDURE >= G_LOG_LEVEL);
1506 l_sLog CONSTANT BOOLEAN := l_pLog AND (FND_LOG.LEVEL_STATEMENT >= G_LOG_LEVEL);
1507 
1508 BEGIN
1509 
1510   IF (l_pLog) THEN
1511 
1512         FND_LOG.STRING (FND_LOG.LEVEL_PROCEDURE,
1513                         l_module || '.begin',
1514                         l_api_name || ' <<< Parameters:
1515                         p_pac_period_id = ' || p_pac_period_id || '
1516                         p_prior_period_id = ' || p_prior_period_id || '
1517                         p_entity_id = ' || p_entity_id || '
1518                         p_line_id = ' || p_line_id || '
1519                         p_net_qty = ' || p_net_qty ||'
1520                         p_final_completion_flag = ' || p_final_completion_flag ||'
1521                         p_scrap = ' || p_scrap ||'
1522                         p_op_seq = ' || p_op_seq );
1523   END IF;
1524 
1525   l_stmt_num := 0;
1526   -- Lot based materials project for R12, get the lot size of job
1527   l_lot_size := 1;
1528 
1529   ------------------------------------------------------
1530   -- Get the Organization id for Component yield project
1531   -- Get the entity type for LotBased project
1532   ------------------------------------------------------
1533   SELECT entity_type,
1534          organization_id
1535   INTO   l_entity_type,
1536          l_org_id
1537   FROM   wip_entities
1538   WHERE  wip_entity_id = p_entity_id;
1539 
1540   IF (l_entity_type <> 2) THEN -- Exclude repetitive schedules.
1541 
1542       SELECT nvl(start_quantity,1) -- to avoid divide by zero error
1543       INTO   l_lot_size
1544       FROM   wip_discrete_jobs
1545       WHERE  wip_entity_id = p_entity_id;
1546   ELSE
1547       -- Get the repetitive_schedule_id for a wip entity id and line id
1548       SELECT wrs.repetitive_schedule_id
1549       INTO   l_repetitive_schedule_id
1550       FROM   wip_repetitive_schedules wrs
1551       WHERE  wrs.wip_entity_id = p_entity_id
1552       AND    wrs.line_id = p_line_id;
1553   END IF;
1554 
1555   ---------------------------------------------------------------------------
1556   -- Get the value of Include Component yield flag, which will determine
1557   -- whether to include or not component yield factor in quantity per
1558   -- assembly
1559   ---------------------------------------------------------------------------
1560   SELECT  nvl(include_component_yield, 1)
1561   INTO    l_include_comp_yield
1562   FROM    wip_parameters
1563   WHERE   organization_id = l_org_id;
1564 
1565 -------------------------------------
1566 -- Intialize job balance PL/SQL table
1567 -------------------------------------
1568 FOR cost_element in 1..5 LOOP
1569     l_job_balance(cost_element) := 0;
1570 END LOOP;
1571 
1572 FOR op_seq_rec IN c_wip_opseq LOOP
1573     ---------------------------------------------------
1574     -- Intialize Operation-Component level PL/SQL table
1575     ---------------------------------------------------
1576     l_stmt_num := 10;
1577     FOR cost_element in 1..5 LOOP
1578         l_op_relieved_comp_cost(cost_element) := 0;
1579         l_prior_relieved_comp_cost(cost_element) := 0;
1580     END LOOP;
1581 
1582     FOR comp_rec IN c_wro(op_seq_rec.operation_seq_num) LOOP
1583         -------------------------------------------------------
1584         -- Check record count for this Job, Component in cprocd
1585         -------------------------------------------------------
1586         l_stmt_num := 20;
1587         SELECT COUNT(*)
1588         INTO   l_record_exists
1589         FROM   CST_PAC_REQ_OPER_COST_DETAILS cprocd
1590         WHERE  cprocd.pac_period_id = p_pac_period_id
1591         AND    cprocd.cost_group_id = p_cost_group_id
1592         AND    cprocd.wip_entity_id = p_entity_id
1593         AND    nvl(cprocd.line_id, -99) = nvl(p_line_id,-99)
1594         AND    cprocd.operation_seq_num = op_seq_rec.operation_seq_num
1595         AND    cprocd.inventory_item_id = comp_rec.component
1596         AND    ROWNUM < 2;
1597 
1598         --------------------------------------------------------------------
1599         -- If no record exists in cprocd, but there is completion/return txn
1600         --------------------------------------------------------------------
1601         IF (l_record_exists = 0 ) THEN
1602            ------------------------------------------------------------
1603            -- Repeat for 5 cost elements if no record exists in cprocd
1604            ------------------------------------------------------------
1605            l_stmt_num := 30;
1606            FOR i IN 1..5 LOOP
1607 
1608             /* Added condition (p_net_qty >= 0) for Bug 13833164.
1609 	       p_net_qty < 0, is the case of net Assembly returns.
1610 	       Assembly return uses previous period costs for calculation of cost.
1611                For components that have never been issued, this would be zero, hece
1612 	       we donot add anything to l_op_relieved_comp_cost and l_job_balance.
1613 	     */
1614             IF (p_net_qty >= 0) THEN
1615 
1616             l_current_period_cost  := 0;
1617             l_avl_relieve_qty := 0;
1618             -----------------------------------------------------
1619             -- No Final completion exists in this PAC Period
1620             -----------------------------------------------------
1621             IF (NVL(p_final_completion_flag,0) <> 1) THEN
1622                -------------------------------------------------------------------
1623                -- Calculate required Qty
1624                -------------------------------------------------------------------
1625                l_stmt_num := 40;
1626                l_avl_relieve_qty  :=  p_net_qty * comp_rec.quantity_per_assembly;
1627                ----------------------------------------------
1628                -- Get the component cost from current period
1629                ----------------------------------------------
1630                BEGIN
1631 
1632                    SELECT   SUM(NVL(cpicd.item_cost,0))
1633                    INTO     l_current_period_cost
1634                    FROM     cst_pac_item_costs cpic,
1635                             cst_pac_item_cost_details cpicd
1636                    WHERE    cpic.pac_period_id = p_pac_period_id
1637                    AND      cpic.cost_group_id = p_cost_group_id
1638                    AND      cpic.inventory_item_id = comp_rec.component
1639                    AND      cpic.cost_layer_id = cpicd.cost_layer_id
1640                    AND      cpicd.cost_element_id = i
1641                    GROUP BY cpicd.cost_element_id;
1642                EXCEPTION
1643                    WHEN NO_DATA_FOUND THEN
1644                      l_current_period_cost  := 0;
1645                END;
1646             END IF;
1647             -----------------------------------------------------------------------
1648             -- Add period cost to PL/SQL table. This table value will be used
1649             -- while updating WPPB table at l_stmt_num := 270
1650             ------------------------------------------------------------------------
1651             l_stmt_num := 50;
1652             /* Bug 10627017: Added Nvl to the 2 formulas below, in case the item does not have pac costs */
1653 	    l_op_relieved_comp_cost(i) := l_op_relieved_comp_cost(i) + Nvl(l_current_period_cost * l_avl_relieve_qty, 0);
1654             -- This is used to find out balance to be relieved from this job in this period
1655             l_job_balance(i) := l_job_balance(i) + Nvl(l_current_period_cost * l_avl_relieve_qty,0);
1656 
1657             -- Statement level log message for FND logging
1658             IF (l_sLog) THEN
1659                 FND_LOG.STRING (FND_LOG.LEVEL_STATEMENT,
1660                                 l_module || '.'||l_stmt_num,
1661 				'Operation Seq :' || op_seq_rec.operation_seq_num || ','||
1662 				'Component :' || comp_rec.component || ','||
1663 				'Cost Element :' || i || ','||
1664                                 'l_record_exists :' || l_record_exists || ','||
1665                                 'l_current_period_cost :' || l_current_period_cost || ','||
1666                                 'l_avl_relieve_qty :' || l_avl_relieve_qty || ','||
1667                                 'l_op_relieved_comp_cost :'  || l_op_relieved_comp_cost(i) || ','||
1668                                 'l_job_balance :'  || l_job_balance(i));
1669             END IF;
1670 
1671             ------------------------------------------------------
1672             -- Insert a record into CST_PAC_REQ_OPER_COST_DETAILS
1673             ------------------------------------------------------
1674             l_stmt_num := 60;
1675             INSERT INTO CST_PAC_REQ_OPER_COST_DETAILS
1676                     (pac_period_id,
1677                      cost_group_id,
1678                      wip_entity_id,
1679                      line_id,
1680                      inventory_item_id,
1681                      cost_element_id,
1682                      operation_seq_num,
1683                      applied_value,
1684                      applied_quantity,
1685                      relieved_value,
1686                      relieved_quantity,
1687                      comp_variance,
1688                      Temp_Relieved_value,
1689                      -- who Columns
1690                      last_update_date,
1691                      last_updated_by,
1692                      creation_date,
1693                      created_by,
1694                      request_id,
1695                      program_application_id,
1696                      program_id,
1697                      program_update_date,
1698                      last_update_login
1699                     )
1700             VALUES (p_pac_period_id,
1701                     p_cost_group_id,
1702                     p_entity_id,
1703                     p_line_id,
1704                     comp_rec.component,
1705                     i,
1706                     op_seq_rec.operation_seq_num,
1707                     0,
1708                     0,
1709                     l_current_period_cost * l_avl_relieve_qty,
1710                     l_avl_relieve_qty,
1711                     0,
1712                     l_current_period_cost * l_avl_relieve_qty,
1713                     sysdate,
1714                     p_user_id,
1715                     sysdate,
1716                     p_user_id,
1717                     p_request_id,
1718                     p_prog_app_id,
1719                     p_prog_id,
1720                     sysdate,
1721                     p_login_id);
1722 
1723              END IF; -- p_net_qty >= 0
1724            END LOOP;
1725     ---------------------------------------------------------------------------------------
1726     -- If record exists in cprocd table and there is(are) Assembly completion/return txn(s)
1727     ---------------------------------------------------------------------------------------
1728     ELSE
1729         -------------------------------------------------------------------------------------------
1730         -- Loop through the Cost elements for which already record is there in cprocd for Component
1731         -------------------------------------------------------------------------------------------
1732         FOR cstelement_rec in c_cost_element(op_seq_rec.operation_seq_num,comp_rec.component ) LOOP
1733 
1734             l_applied_qty := 0;
1735             l_avl_relieve_value := 0;
1736             l_avl_relieve_qty := 0;
1737             l_prior_relieved_value := 0;
1738             l_prior_relieved_qty := 0;
1739             l_skip_below_process := 0;
1740             -------------------------------------------------------------
1741             -- Calculate the Required Quantity
1742             -------------------------------------------------------------
1743             l_stmt_num := 70;
1744             l_required_qty := p_net_qty * comp_rec.quantity_per_assembly;
1745 
1746                -- Statement level log message for FND logging
1747                IF (l_sLog) THEN
1748                    FND_LOG.STRING (FND_LOG.LEVEL_STATEMENT,
1749                                    l_module || '.'||l_stmt_num,
1750   				   'Operation Seq :' || op_seq_rec.operation_seq_num || ','||
1751 				   'Component :' || comp_rec.component || ','||
1752                                    'Cost Element :' || cstelement_rec.cst_ele_id || ','||
1753                                    'p_net_qty :' || p_net_qty || ','||
1754                                    'l_required_qty :' || l_required_qty || ','||
1755                                    'quantity_per_assembly :'|| comp_rec.quantity_per_assembly ||','||
1756                                    'l_skip_below_process :'|| l_skip_below_process ||','||
1757                                    'p_final_completion_flag :'  || p_final_completion_flag);
1758                END IF;
1759 
1760 
1761             ------------------------------------------------------
1762             -- p_net_qty < 0 then get Avg of Prior Relieved Value
1763             ------------------------------------------------------
1764             IF (p_net_qty < 0) THEN
1765                BEGIN
1766                l_stmt_num := 80;
1767                SELECT nvl(relieved_value,0),
1768                      decode(nvl(relieved_quantity, 0),
1769                                 0,1,
1770                                 nvl(relieved_quantity, 0))
1771                INTO   l_avl_relieve_value,
1772                       l_avl_relieve_qty
1773                FROM   CST_PAC_REQ_OPER_COST_DETAILS cprocd
1774                WHERE  cprocd.pac_period_id = p_prior_period_id
1775                AND    cprocd.cost_group_id = p_cost_group_id
1776                AND    cprocd.wip_entity_id = p_entity_id
1777                AND    nvl(cprocd.line_id, -99) = nvl(p_line_id,-99)
1778                AND    cprocd.operation_seq_num = op_seq_rec.operation_seq_num
1779                AND    cprocd.inventory_item_id = comp_rec.component
1780                AND    cprocd.cost_element_id = cstelement_rec.cst_ele_id;
1781                EXCEPTION
1782                WHEN NO_DATA_FOUND THEN
1783                  l_avl_relieve_value := 0;
1784                  l_avl_relieve_qty := 1; --To avoid zero Division error
1785                END;
1786                ----------------------------------------------------------
1787                -- Calculate to be Relived value in case of p_net_qty < 0
1788                ----------------------------------------------------------
1789                l_stmt_num := 90;
1790                l_avl_relieve_value := (l_avl_relieve_value/l_avl_relieve_qty) * l_required_qty;
1791                l_avl_relieve_qty := l_required_qty;
1792                ------------------------------------------------------------------
1793                -- Add the Component Relieve value to the Operation Relieve value
1794                ------------------------------------------------------------------
1795                /* Bug 10627017: Added Nvl to the 2 formulas below */
1796                l_op_relieved_comp_cost(cstelement_rec.cst_ele_id) :=
1797                                                   l_op_relieved_comp_cost(cstelement_rec.cst_ele_id) +
1798                                                   Nvl(l_avl_relieve_value,0);
1799                -- This is used to find out balance to be relieved from this job in this period
1800                -- For more Assembly returns than Assembly completions then sign of p_net_qty is negative.
1801                -- So making sign to opposite sign.
1802                l_job_balance(cstelement_rec.cst_ele_id) := l_job_balance(cstelement_rec.cst_ele_id) + Nvl(l_avl_relieve_value,0) * (-1);
1803 
1804                -- Statement level log message for FND logging
1805                IF (l_sLog) THEN
1806                    FND_LOG.STRING (FND_LOG.LEVEL_STATEMENT,
1807                                    l_module || '.'||l_stmt_num,
1808                                    'l_avl_relieve_value :' || l_avl_relieve_value || ','||
1809                                    'l_avl_relieve_qty :'  || l_avl_relieve_qty || ','||
1810                                    'l_op_relieved_comp_cost :' || l_op_relieved_comp_cost(cstelement_rec.cst_ele_id) || ','||
1811                                    'l_job_balance :'  || l_job_balance(cstelement_rec.cst_ele_id));
1812                END IF;
1813 
1814             ELSE -- p_net_qty > 0 OR  p_net_qty = 0
1815 
1816             -------------------------------------------------------------------------------------
1817             -- p_net_qty = 0, Assembly completions cancels Assembly returns
1818             -------------------------------------------------------------------------------------
1819             l_stmt_num := 100;
1820             IF ( p_net_qty = 0) THEN
1821 
1822                --------------------------------------
1823                --Check Final completion exists or not
1824                --------------------------------------
1825                IF (p_final_completion_flag = 1) THEN
1826 
1827                    ------------------------------------------------
1828                    -- Get the Assembly return qty in this period
1829                    ------------------------------------------------
1830                    l_stmt_num := 110;
1831                    SELECT sum(primary_quantity)
1832                    INTO   l_assembly_return_cnt
1833                    FROM   mtl_material_transactions mmt
1834                    WHERE  mmt.transaction_source_id =  p_entity_id
1835                    AND    mmt.transaction_action_id = 32
1836                    AND    mmt.transaction_source_type_id = 5
1837                    AND    nvl(mmt.repetitive_line_id,-99) = nvl(p_line_id,-99)
1838                    AND    mmt.transaction_date BETWEEN TRUNC(p_start_date)
1839                                                    AND  (TRUNC(p_end_date) + 0.99999);
1840 
1841 
1842                   IF l_assembly_return_cnt <> 0 THEN
1843 
1844 		    /* Check if any assembly units were relieved before this period */
1845 		    SELECT Max(Nvl(relieved_assembly_units,0))
1846                       INTO l_prior_completed_units
1847 		      FROM wip_pac_period_balances wppb
1848 		     WHERE wppb.wip_entity_id = p_entity_id
1849 		       AND wppb.pac_period_id = p_pac_period_id
1850 		       AND wppb.cost_type_id = p_cost_type_id
1851 		       AND nvl(wppb.line_id, -99) = nvl(p_line_id,-99)
1852 		       AND wppb.cost_group_id = p_cost_group_id;
1853 
1854                      END IF;
1855 
1856 		   -------------------------------------------------------------------
1857                    -- Get the Avg of Prior Relieved Value.
1858                    -------------------------------------------------------------------
1859                    l_stmt_num := 120;
1860                    BEGIN
1861                        SELECT nvl(Relieved_Value,0),
1862                               decode(sign(nvl(Relieved_quantity,0)),
1863                                      0,1,
1864                                      Relieved_quantity)
1865                        INTO   l_prior_relieved_value,
1866                               l_prior_relieved_qty
1867                        FROM   CST_PAC_REQ_OPER_COST_DETAILS cprocd
1868                        WHERE  cprocd.wip_entity_id = p_entity_id
1869                        AND    nvl(cprocd.line_id, -99) = nvl(p_line_id,-99)
1870                        AND    cprocd.operation_seq_num = op_seq_rec.operation_seq_num
1871                        AND    cprocd.inventory_item_id = comp_rec.component
1872                        AND    cprocd.cost_element_id = cstelement_rec.cst_ele_id
1873                        AND    cprocd.cost_group_id = p_cost_group_id
1874                        AND    cprocd.pac_period_id = p_prior_period_id;
1875                    EXCEPTION
1876                        WHEN NO_DATA_FOUND THEN
1877                             l_prior_relieved_value := 0;
1878                             l_prior_relieved_qty := 1; -- To avoid Zero division error
1879                    END;
1880                    ----------------------------------------------------------------------------
1881                    -- Add the Net value = Avg of Prior Relieved Value * Assmebly return qty * qpa
1882                    -- to PL/SQL table. This used to update the WPPB's TEMP column value
1883                    -- and cprocd's Temp_Relieved_value
1884                    -----------------------------------------------------------------------------
1885                    /* Bug 10627017: Added Nvl to the formulas below */
1886                    l_prior_relieved_value := Nvl((l_prior_relieved_value / l_prior_relieved_qty) *
1887                                                                        l_assembly_return_cnt *
1888                                                                        comp_rec.quantity_per_assembly,0);
1889                    l_prior_relieved_comp_cost(cstelement_rec.cst_ele_id) := l_prior_relieved_comp_cost(cstelement_rec.cst_ele_id) +
1890                                                                             (-1) * Nvl(l_prior_relieved_value,0);
1891                    -- This is used to find out balance to be relieved from this job in this period
1892                    l_job_balance(cstelement_rec.cst_ele_id) := l_job_balance(cstelement_rec.cst_ele_id) + (-1) * Nvl(l_prior_relieved_value,0);
1893 
1894                    -- Statement level log message for FND logging
1895                    IF (l_sLog) THEN
1896                        FND_LOG.STRING (FND_LOG.LEVEL_STATEMENT,
1897                                        l_module || '.'||l_stmt_num,
1898                                        'l_assembly_return_cnt :' || l_assembly_return_cnt || ','||
1899                                        'l_prior_relieved_value :'  || l_prior_relieved_value || ','||
1900                                        'l_prior_relieved_comp_cost :' ||  l_prior_relieved_comp_cost(cstelement_rec.cst_ele_id) || ','||
1901                                        'l_job_balance :'  || l_job_balance(cstelement_rec.cst_ele_id));
1902                    END IF;
1903                ELSE
1904                -------------------------------------------------------------------
1905                -- No final completion then relieve at average of prior completions
1906                -------------------------------------------------------------------
1907                l_stmt_num := 130;
1908                    BEGIN
1909                        SELECT nvl(relieved_value,0),
1910                               decode(nvl(relieved_quantity, 0),
1911                                       0,1,
1912                                      nvl(relieved_quantity, 0))
1913                        INTO   l_avl_relieve_value,
1914                               l_avl_relieve_qty
1915                        FROM   CST_PAC_REQ_OPER_COST_DETAILS cprocd
1916                        WHERE  cprocd.pac_period_id = p_prior_period_id
1917                        AND    cprocd.cost_group_id = p_cost_group_id
1918                        AND    cprocd.wip_entity_id = p_entity_id
1919                        AND    nvl(cprocd.line_id, -99) = nvl(p_line_id,-99)
1920                        AND    cprocd.operation_seq_num = op_seq_rec.operation_seq_num
1921                        AND    cprocd.inventory_item_id = comp_rec.component
1922                        AND    cprocd.cost_element_id = cstelement_rec.cst_ele_id;
1923                    EXCEPTION
1924                        WHEN NO_DATA_FOUND THEN
1925                          l_avl_relieve_value := 0;
1926                          l_avl_relieve_qty := 1; --To avoid zero Division error
1927                    END;
1928                    ---------------------------------------------------------------------
1929                    -- Calculate to be Relived value = average of prior completions
1930                    -- make l_avl_relieve_qty to zero so subsequent cprocd's relived_qty
1931                    -- column update will not changed in this case
1932                    -- Update the new variable l_skip_below_process = 1
1933                    -- So that we can skip the
1934                    ---------------------------------------------------------------------
1935                    l_stmt_num := 140;
1936                    l_avl_relieve_value := (l_avl_relieve_value/l_avl_relieve_qty);
1937                    l_avl_relieve_qty := 0;
1938                    l_skip_below_process := 1;
1939 
1940                    ------------------------------------------------------------------
1941                    -- Add the Component Relieve value to the Operation Relieve value
1942                    ------------------------------------------------------------------
1943                    l_stmt_num := 150;
1944                    /* Bug 10627017: Added Nvl to the 2 formulas below */
1945                    l_op_relieved_comp_cost(cstelement_rec.cst_ele_id) :=
1946                                                       l_op_relieved_comp_cost(cstelement_rec.cst_ele_id) +
1947                                                       Nvl(l_avl_relieve_value,0);
1948                    -- This is used to find out balance to be relieved from this job in this period
1949                    l_job_balance(cstelement_rec.cst_ele_id) := l_job_balance(cstelement_rec.cst_ele_id) + Nvl(l_avl_relieve_value,0);
1950 
1951                    -- Statement level log message for FND logging
1952                    IF (l_sLog) THEN
1953                        FND_LOG.STRING (FND_LOG.LEVEL_STATEMENT,
1954                                        l_module || '.'||l_stmt_num,
1955                                        'l_skip_below_process :' || l_skip_below_process || ','||
1956                                        'l_avl_relieve_value :'  || l_avl_relieve_value || ','||
1957                                        'l_op_relieved_comp_cost :' ||  l_op_relieved_comp_cost(cstelement_rec.cst_ele_id) || ','||
1958                                        'l_job_balance :'  || l_job_balance(cstelement_rec.cst_ele_id));
1959                    END IF;
1960                END IF;
1961 
1962             END IF; --End of IF ( p_net_qty = 0)
1963 
1964             -----------------------------------------------------------------------------
1965             -- Check to skip the below process or not. In case of p_net_qty = 0 and
1966             --  no final completion then l_skip_below_process = 1, below part is  skipped
1967             -- in all other cases below IF will be executed
1968             -----------------------------------------------------------------------------
1969             IF (l_skip_below_process <> 1 ) THEN
1970             ------------------------------------------------------------------------
1971             -- Get the Available to Relieve Value and Available to Relieve Quantity
1972             -- applied quantity and relieved quantity
1973             ------------------------------------------------------------------------
1974             l_stmt_num := 160;
1975             SELECT (nvl(applied_value,0) - nvl(relieved_value,0) - nvl(comp_variance,0)),
1976                     nvl(applied_quantity, 0),
1977                    (nvl(applied_quantity, 0) - nvl(relieved_quantity, 0)),
1978                    nvl(relieved_quantity, 0)
1979             INTO   l_avl_relieve_value,
1980                    l_applied_qty,
1981                    l_avl_relieve_qty,
1982                    l_relieved_qty
1983             FROM   CST_PAC_REQ_OPER_COST_DETAILS cprocd
1984             WHERE  cprocd.pac_period_id = p_pac_period_id
1985             AND    cprocd.cost_group_id = p_cost_group_id
1986             AND    cprocd.wip_entity_id = p_entity_id
1987             AND    nvl(cprocd.line_id, -99) = nvl(p_line_id,-99)
1988             AND    cprocd.operation_seq_num = op_seq_rec.operation_seq_num
1989             AND    cprocd.inventory_item_id = comp_rec.component
1990             AND    cprocd.cost_element_id = cstelement_rec.cst_ele_id;
1991 
1992             -- Statement level log message for FND logging
1993             IF (l_sLog) THEN
1994                 FND_LOG.STRING (FND_LOG.LEVEL_STATEMENT,
1995                                 l_module || '.'||l_stmt_num,
1996                                 'l_avl_relieve_value :' ||l_avl_relieve_value || ','||
1997                                 'l_applied_qty :'  || l_applied_qty || ','||
1998                                 'l_avl_relieve_qty :' ||  l_avl_relieve_qty || ','||
1999                                 'l_relieved_qty :'  || l_relieved_qty);
2000             END IF;
2001 
2002             ---------------------------------------------------------------------
2003             -- Available to Relieve Value and Quantity are zero,
2004             -- So use Current Periodic Cost for Ordinary completions
2005             -- If Final completion exists in this period then Relieve Zero Value
2006             ---------------------------------------------------------------------
2007             l_stmt_num := 170;
2008             IF (l_avl_relieve_value = 0 and l_avl_relieve_qty = 0) THEN
2009                -------------------------------------------------------------------------------------------
2010                -- Final Completion exists in this period, Ignore Final Completion in case of p_net_qty < 0
2011                -------------------------------------------------------------------------------------------
2012                l_stmt_num := 180;
2013                 IF (p_final_completion_flag = 1 AND p_net_qty >= 0) THEN
2014                    -----------------------------------------------------
2015                    -- Relieve Zero values
2016                    -----------------------------------------------------
2017                    l_avl_relieve_value := 0;
2018                    --------------------------------------------------------
2019                    -- Calculate the Relieve Qty in case of Final completion
2020                    --------------------------------------------------------
2021                    IF ( (l_required_qty > 0 AND l_applied_qty > l_required_qty) OR
2022                         (l_required_qty < 0 AND l_applied_qty < l_required_qty)) THEN
2023 
2024                         l_avl_relieve_qty := l_applied_qty - l_relieved_qty;
2025                         -- Statement level log message for FND logging
2026                         IF (l_sLog) THEN
2027                            FND_LOG.STRING (FND_LOG.LEVEL_STATEMENT,
2028                                            l_module || '.'||l_stmt_num,
2029                                            'l_avl_relieve_value :' ||l_avl_relieve_value || ','||
2030                                            'l_avl_relieve_qty :'  || l_avl_relieve_qty);
2031                         END IF;
2032                    END IF;
2033                 ELSE
2034                   l_stmt_num := 190;
2035                   SELECT SUM(NVL(cpicd.item_cost,0))
2036                   INTO   l_current_period_cost
2037                   FROM   cst_pac_item_costs cpic,
2038                          cst_pac_item_cost_details cpicd
2039                   WHERE  cpic.pac_period_id = p_pac_period_id
2040                   AND    cpic.cost_group_id = p_cost_group_id
2041                   AND    cpic.inventory_item_id = comp_rec.component
2042                   AND    cpic.cost_layer_id = cpicd.cost_layer_id
2043                   AND    cpicd.cost_element_id = cstelement_rec.cst_ele_id;
2044 
2045                   l_avl_relieve_value := l_current_period_cost * l_required_qty;
2046                   l_avl_relieve_qty := l_required_qty;
2047                    -- Statement level log message for FND logging
2048                    IF (l_sLog) THEN
2049                        FND_LOG.STRING (FND_LOG.LEVEL_STATEMENT,
2050                                        l_module || '.'||l_stmt_num,
2051                                        'l_avl_relieve_value :' ||l_avl_relieve_value || ','||
2052                                        'l_avl_relieve_qty :'  || l_avl_relieve_qty);
2053                    END IF;
2054                 END IF; -- End of IF at l_stmt_num := 180
2055             --------------------------------------------------------------------------
2056             -- Available to Relieve Value and Quantity are negative OR
2057             -- Available to Relieve Value and Quantity are positive
2058             -- So use WIP Avg Cost for Ordinary Completions
2059             -- If Final completion exists in this period then Relieve Available Value
2060             --------------------------------------------------------------------------
2061             ELSIF ((l_avl_relieve_value < 0 AND l_avl_relieve_qty < 0) OR
2062                   (l_avl_relieve_value > 0 and l_avl_relieve_qty > 0)) THEN
2063 
2064                --------------------------------------------------------------------------------------------
2065                -- Final Completion exists in this period, Ignore Final Completion in case of p_net_qty <0
2066                --------------------------------------------------------------------------------------------
2067                l_stmt_num := 200;
2068                IF (p_final_completion_flag = 1 AND p_net_qty >= 0 ) THEN
2069 
2070                   l_avl_relieve_value := l_avl_relieve_value;
2071                   --------------------------------------------------------
2072                   -- Calculate the Relieve Qty in case of Final completion
2073                   --------------------------------------------------------
2074                   IF ( (l_required_qty > 0 AND l_applied_qty > l_required_qty) OR
2075                        (l_required_qty < 0 AND l_applied_qty < l_required_qty)) THEN
2076 
2077                         l_avl_relieve_qty := l_applied_qty - l_relieved_qty;
2078                         -- Statement level log message for FND logging
2079                         IF (l_sLog) THEN
2080                            FND_LOG.STRING (FND_LOG.LEVEL_STATEMENT,
2081                                            l_module || '.'||l_stmt_num,
2082                                            'l_avl_relieve_value :' ||l_avl_relieve_value || ','||
2083                                            'l_avl_relieve_qty :'  || l_avl_relieve_qty);
2084                         END IF;
2085                   END IF;
2086 
2087                ELSE -- Ordinary Completion Only
2088                  ----------------------------------------------------------------
2089                  -- Required Quantity is less than or Equal to Available Quantity
2090                  ----------------------------------------------------------------
2091                  l_stmt_num := 210;
2092                  -- Added Modified new condition
2093                  IF  (l_required_qty = 0 OR
2094                      (l_required_qty > 0 and l_avl_relieve_qty > 0 and l_required_qty <= l_avl_relieve_qty) OR
2095                      (l_required_qty < 0 and l_avl_relieve_qty < 0 and l_required_qty >= l_avl_relieve_qty)) THEN
2096 
2097                      l_avl_relieve_value := (l_avl_relieve_value/l_avl_relieve_qty) *  l_required_qty;
2098                      l_avl_relieve_qty := l_required_qty;
2099 
2100                      -- Statement level log message for FND logging
2101                      IF (l_sLog) THEN
2102                         FND_LOG.STRING (FND_LOG.LEVEL_STATEMENT,
2103                                         l_module || '.'||l_stmt_num,
2104                                         'l_avl_relieve_value :' ||l_avl_relieve_value || ','||
2105                                         'l_avl_relieve_qty :'  || l_avl_relieve_qty);
2106                      END IF;
2107                  ELSE -- Required Quantity is more than Available Quantity
2108                     ---------------------------------------------------
2109                     -- Get the Current Periodic Cost
2110                     ---------------------------------------------------
2111                     l_stmt_num := 220;
2112                     SELECT SUM(NVL(cpicd.item_cost,0))
2113                     INTO   l_current_period_cost
2114                     FROM   cst_pac_item_costs cpic,
2115                            cst_pac_item_cost_details cpicd
2116                     WHERE  cpic.pac_period_id = p_pac_period_id
2117                     AND    cpic.cost_group_id = p_cost_group_id
2118                     AND    cpic.inventory_item_id = comp_rec.component
2119                     AND    cpic.cost_layer_id = cpicd.cost_layer_id
2120                     AND    cpicd.cost_element_id = cstelement_rec.cst_ele_id;
2121 
2122                     ------------------------------------------------------------------------------------------------------
2123                     -- Relieve Value = Available to Relieve Value + (Required qty - Available to Relieve qty ) * PWAC Cost
2124                     -- Required qty = p_net_qty * comp_rec.quantity_per_assembly
2125                     ------------------------------------------------------------------------------------------------------
2126                     l_avl_relieve_value := l_avl_relieve_value + (l_required_qty - l_avl_relieve_qty) *
2127                                                                   l_current_period_cost;
2128                     l_avl_relieve_qty := l_required_qty;
2129 
2130                     -- Statement level log message for FND logging
2131                      IF (l_sLog) THEN
2132                         FND_LOG.STRING (FND_LOG.LEVEL_STATEMENT,
2133                                         l_module || '.'||l_stmt_num,
2134                                         'l_avl_relieve_value :' ||l_avl_relieve_value || ','||
2135                                         'l_avl_relieve_qty :'  || l_avl_relieve_qty);
2136                      END IF;
2137                  END IF; -- end of IF l_stmt_num := 210
2138 
2139                END IF; -- End of Final Completion exists in this period, End of l_stmt_num := 200
2140             ------------------------------------------------------------------------------------------
2141             -- Available to Relieve Value and Available to Relieve Quantity both are opposite in signs
2142             ------------------------------------------------------------------------------------------
2143             ELSIF ((l_avl_relieve_value < 0 AND l_avl_relieve_qty >= 0) OR
2144                    (l_avl_relieve_value > 0 AND l_avl_relieve_qty <= 0)) THEN
2145                     --------------------------------------------------------------------------------------------
2146                     -- Final Completion exists in this period, Ignore Final Completion in case of p_net_qty < 0
2147                     --------------------------------------------------------------------------------------------
2148                     l_stmt_num := 240;
2149                     IF (p_final_completion_flag = 1 AND p_net_qty >= 0) THEN
2150                         -------------------------------------------
2151                         -- Relieve Available Value from Job
2152                         -------------------------------------------
2153                         l_avl_relieve_value := l_avl_relieve_value;
2154                         --------------------------------------------------------
2155                         -- Calculate the Relieve Qty in case of Final completion
2156                         --------------------------------------------------------
2157                         IF ( (l_required_qty > 0 AND l_applied_qty > l_required_qty) OR
2158                              (l_required_qty < 0 AND l_applied_qty < l_required_qty)) THEN
2159 
2160                              l_avl_relieve_qty := l_applied_qty - l_relieved_qty;
2161                              -- Statement level log message for FND logging
2162                              IF (l_sLog) THEN
2163                                  FND_LOG.STRING (FND_LOG.LEVEL_STATEMENT,
2164                                                  l_module || '.'||l_stmt_num,
2165                                                  'l_avl_relieve_value :' ||l_avl_relieve_value || ','||
2166                                                  'l_avl_relieve_qty :'  || l_avl_relieve_qty);
2167                              END IF;
2168                         END IF;
2169                     ELSE
2170                         -------------------------------------------------------------
2171                         -- Ordinary Cmpletions Only, So get the Current Periodic Cost
2172                         -------------------------------------------------------------
2173                         l_stmt_num := 250;
2174                         SELECT SUM(NVL(cpicd.item_cost,0))
2175                         INTO   l_current_period_cost
2176                         FROM   cst_pac_item_costs cpic,
2177                                cst_pac_item_cost_details cpicd
2178                         WHERE  cpic.pac_period_id = p_pac_period_id
2179                         AND    cpic.cost_group_id = p_cost_group_id
2180                         AND    cpic.inventory_item_id = comp_rec.component
2181                         AND    cpic.cost_layer_id = cpicd.cost_layer_id
2182                         AND    cpicd.cost_element_id = cstelement_rec.cst_ele_id;
2183 
2184                        l_avl_relieve_value := l_current_period_cost * l_required_qty;
2185                        l_avl_relieve_qty := l_required_qty;
2186 
2187                        -- Statement level log message for FND logging
2188                        IF (l_sLog) THEN
2189                            FND_LOG.STRING (FND_LOG.LEVEL_STATEMENT,
2190                                            l_module || '.'||l_stmt_num,
2191                                            'l_avl_relieve_value :' ||l_avl_relieve_value || ','||
2192                                            'l_avl_relieve_qty :'  || l_avl_relieve_qty);
2193                        END IF;
2194                     END IF; -- End of IF at l_stmt_num := 240
2195             END IF; -- End of IF at l_stmt_num := 170
2196 
2197             ------------------------------------------------------------------
2198             -- Add the Component Relieve value to the Operation Relieve value
2199             ------------------------------------------------------------------
2200             l_stmt_num := 250;
2201             /* Bug 10627017: Added Nvl to the 2 formulas below */
2202             l_op_relieved_comp_cost(cstelement_rec.cst_ele_id) :=
2203                                                l_op_relieved_comp_cost(cstelement_rec.cst_ele_id) +
2204                                                Nvl(l_avl_relieve_value,0);
2205             -- This is used to find out balance to be relieved from this job in this period
2206 	    IF p_net_qty = 0 AND p_final_completion_flag = 1 AND l_prior_completed_units = 0 THEN
2207 	       l_job_balance(cstelement_rec.cst_ele_id) := 0;
2208             ELSE
2209 	       l_job_balance(cstelement_rec.cst_ele_id) := l_job_balance(cstelement_rec.cst_ele_id) + Nvl(l_avl_relieve_value,0);
2210             END IF;
2211 
2212             -- Statement level log message for FND logging
2213             IF (l_sLog) THEN
2214                 FND_LOG.STRING (FND_LOG.LEVEL_STATEMENT,
2215                                 l_module || '.'||l_stmt_num,
2216                                 'l_avl_relieve_value :' ||l_avl_relieve_value || ','||
2217                                 'l_avl_relieve_qty :'  || l_avl_relieve_qty);
2218             END IF;
2219            END IF; -- End of IF l_skip_below_process <> 1
2220 
2221          END IF; -- End of IF p_net_qty < 0
2222 
2223             ------------------------------------------------------
2224             -- Update cprocd table with Calculated Relieved value
2225             ------------------------------------------------------
2226             l_stmt_num := 260;
2227             UPDATE CST_PAC_REQ_OPER_COST_DETAILS cprocd
2228             SET    cprocd.Relieved_value = nvl(cprocd.Relieved_Value,0) +
2229                                                 decode(p_net_qty,
2230                                                        0,decode(p_final_completion_flag,
2231                                                                  0, 0,
2232                                                                  NULL,0,
2233                                                                  Decode(l_prior_completed_units, 0, 0,
2234                                                                  l_avl_relieve_value)),
2235                                                         l_avl_relieve_value),
2236                    cprocd.Temp_Relieved_value = l_avl_relieve_value + (-1) * l_prior_relieved_value,
2237                    -- Same as TEMP column in WPPB table. This will be used if Total Job value is -ve then
2238                    -- Update the Comp_variance = Comp_variance + Temp_Relieved_value at the end
2239                    cprocd.Relieved_quantity = nvl(cprocd.Relieved_quantity,0) + l_avl_relieve_qty,
2240                    cprocd.last_update_date = SYSDATE,
2241                    cprocd.last_updated_by = p_user_id,
2242                    cprocd.last_update_login = p_login_id,
2243                    cprocd.request_id = p_request_id,
2244                    cprocd.program_application_id = p_prog_app_id,
2245                    cprocd.program_id = p_prog_id,
2246                    cprocd.program_update_date = SYSDATE
2247             WHERE  cprocd.wip_entity_id = p_entity_id
2248             AND    nvl(cprocd.line_id, -99) = nvl(p_line_id,-99)
2249             AND    cprocd.operation_seq_num = op_seq_rec.operation_seq_num
2250             AND    cprocd.inventory_item_id = comp_rec.component
2251             AND    cprocd.cost_element_id = cstelement_rec.cst_ele_id
2252             AND    cprocd.cost_group_id = p_cost_group_id
2253             AND    cprocd.pac_period_id = p_pac_period_id;
2254 
2255             -- Statement level log message for FND logging
2256             IF (l_sLog) THEN
2257                 FND_LOG.STRING (FND_LOG.LEVEL_STATEMENT,
2258                                 l_module || '.'||l_stmt_num,
2259                                 'l_avl_relieve_value :' ||l_avl_relieve_value || ','||
2260                                 'l_prior_relieved_value :' ||l_prior_relieved_value || ','||
2261                                 'l_avl_relieve_qty :'  || l_avl_relieve_qty);
2262             END IF;
2263          END LOOP; -- End of Cost Element Loop
2264 
2265      END IF;  -- End of l_record_exists check
2266 
2267   END LOOP; -- End of Component Loop
2268 
2269      -----------------------------------------------------------------------------------
2270      -- Update the wip_pac_period_balances with relieved values
2271      -----------------------------------------------------------------------------------
2272      -- OUT columns will not be updated if p_net_qty = 0 and p_final_completion_flag <>1
2273      -- In this case only TEMP columns are updated
2274      ------------------------------------------------------------------------------------
2275      l_stmt_num := 270;
2276      UPDATE wip_pac_period_balances wppb
2277      SET    wppb.pl_material_out = wppb.pl_material_out + decode(p_net_qty,0,
2278                                                                    decode(p_final_completion_flag,
2279                                                                            0,0,
2280                                                                            NULL,0,
2281                                                                            Decode(l_prior_completed_units, 0, 0,
2282 									   l_op_relieved_comp_cost(1))),
2283                                                                    l_op_relieved_comp_cost(1)),
2284             wppb.pl_material_temp = wppb.pl_material_temp + l_op_relieved_comp_cost(1)
2285                                                           + decode(p_net_qty,0,
2286                                                                    decode(p_final_completion_flag,
2287                                                                           1, l_prior_relieved_comp_cost(1),
2288                                                                           0),
2289                                                                 0),
2290 
2291             wppb.pl_material_overhead_out = wppb.pl_material_overhead_out + decode(p_net_qty,
2292                                                                                    0,decode(p_final_completion_flag,
2293                                                                                                0,0,
2294                                                                                                NULL,0,
2295                                                                                                Decode(l_prior_completed_units, 0, 0,
2296                                                                                                l_op_relieved_comp_cost(2))),
2297                                                                                    l_op_relieved_comp_cost(2)),
2298             wppb.pl_material_overhead_temp = wppb.pl_material_overhead_temp + l_op_relieved_comp_cost(2)
2299                                                           + decode(p_net_qty,0,
2300                                                                    decode(p_final_completion_flag,
2301                                                                           1, l_prior_relieved_comp_cost(2),
2302                                                                           0),0),
2303 
2304             wppb.pl_resource_out = wppb.pl_resource_out +  decode(p_net_qty,
2305                                                                      0,decode(p_final_completion_flag,
2306                                                                                         0,0,
2307                                                                                         NULL,0,
2308                                                                                         Decode(l_prior_completed_units, 0, 0,
2309                                                                                         l_op_relieved_comp_cost(3))),
2310                                                                      l_op_relieved_comp_cost(3)),
2311             wppb.pl_resource_temp = wppb.pl_resource_temp + l_op_relieved_comp_cost(3)
2312                                                           + decode(p_net_qty,0,
2313                                                                    decode(p_final_completion_flag,
2314                                                                           1, l_prior_relieved_comp_cost(3),
2315                                                                           0), 0),
2316 
2317             wppb.pl_outside_processing_out = wppb.pl_outside_processing_out + decode(p_net_qty,
2318                                                                                      0,decode(p_final_completion_flag,
2319                                                                                               0,0,
2320                                                                                               NULL,0,
2321                                                                                               Decode(l_prior_completed_units, 0, 0,
2322                                                                                               l_op_relieved_comp_cost(4))),
2323                                                                                      l_op_relieved_comp_cost(4)),
2324             wppb.pl_outside_processing_temp = wppb.pl_outside_processing_temp + l_op_relieved_comp_cost(4)
2325                                                           + decode(p_net_qty,0,
2326                                                                    decode(p_final_completion_flag,
2327                                                                           1, l_prior_relieved_comp_cost(4),
2328                                                                           0), 0),
2329 
2330             wppb.pl_overhead_out = wppb.pl_overhead_out +  decode(p_net_qty,
2331                                                                   0,decode(p_final_completion_flag,
2332                                                                            0,0,
2333                                                                            NULL,0,
2334                                                                            Decode(l_prior_completed_units, 0, 0,
2335                                                                            l_op_relieved_comp_cost(5))),
2336                                                                   l_op_relieved_comp_cost(5)),
2337             wppb.pl_overhead_temp = wppb.pl_overhead_temp + l_op_relieved_comp_cost(5)
2338                                                           + decode(p_net_qty,0,
2339                                                                    decode(p_final_completion_flag,
2340                                                                           1, l_prior_relieved_comp_cost(5),
2341                                                                           0),0),
2342 
2343             wppb.last_update_date = SYSDATE,
2344             wppb.last_updated_by = p_user_id,
2345             wppb.last_update_login = p_login_id,
2346             wppb.request_id = p_request_id,
2347             wppb.program_application_id = p_prog_app_id,
2348             wppb.program_id = p_prog_id,
2349             wppb.program_update_date = SYSDATE
2350 
2351      WHERE  wppb.wip_entity_id = p_entity_id
2352      AND    wppb.pac_period_id = p_pac_period_id
2353      AND    wppb.cost_type_id = p_cost_type_id
2354      AND    nvl(wppb.line_id, -99) = nvl(p_line_id,-99)
2355      AND    wppb.cost_group_id = p_cost_group_id
2356      AND    wppb.operation_seq_num = op_seq_rec.operation_seq_num;
2357 
2358      END LOOP; -- End of Operation Sequence Loop
2359 
2360      -----------------------------------------------------------------------------
2361      -- Check Cost Element balance to be relieved from the job are Negative or Not
2362      -----------------------------------------------------------------------------
2363 
2364      IF (l_job_balance(1) < 0) THEN
2365 
2366             l_stmt_num := 280;
2367             UPDATE CST_PAC_REQ_OPER_COST_DETAILS cprocd
2368             SET    cprocd.Comp_variance = nvl(cprocd.Comp_variance,0) + nvl(cprocd.Temp_Relieved_value,0),
2369                    cprocd.Relieved_value = nvl(cprocd.Relieved_value,0) - nvl(cprocd.Temp_Relieved_value,0),
2370                    cprocd.last_update_date = SYSDATE,
2371                    cprocd.last_updated_by = p_user_id,
2372                    cprocd.last_update_login = p_login_id,
2373                    cprocd.request_id = p_request_id,
2374                    cprocd.program_application_id = p_prog_app_id,
2375                    cprocd.program_id = p_prog_id,
2376                    cprocd.program_update_date = SYSDATE
2377             WHERE  cprocd.cost_group_id = p_cost_group_id
2378             AND    cprocd.pac_period_id = p_pac_period_id
2379             AND    cprocd.wip_entity_id = p_entity_id
2380             AND    nvl(cprocd.line_id, -99) = nvl(p_line_id,-99)
2381             AND    cprocd.cost_element_id = 1;  -- Material Cost Element
2382 
2383             l_stmt_num := 290;
2384             UPDATE WIP_PAC_PERIOD_BALANCES wppb
2385                    -- New column to store the variance in case if total job balance is negative
2386                    -- This column is not storing accumlated value
2387             SET    wppb.pl_material_temp_var = wppb.pl_material_temp,
2388                    --   This column is  storing accumlated values of variance
2389                    wppb.pl_material_var = nvl(wppb.pl_material_var,0) + nvl(wppb.pl_material_temp,0),
2390                    --Subtract the variance column value from OUT column
2391                    wppb.pl_material_out = wppb.pl_material_out - wppb.pl_material_temp,
2392                    -- Make TEMP value to Zero
2393                    wppb.pl_material_temp = 0,
2394 
2395                    wppb.last_update_date = SYSDATE,
2396                    wppb.last_updated_by = p_user_id,
2397                    wppb.last_update_login = p_login_id,
2398                    wppb.request_id = p_request_id,
2399                    wppb.program_application_id = p_prog_app_id,
2400                    wppb.program_id = p_prog_id,
2401                    wppb.program_update_date = SYSDATE
2402             WHERE  wppb.cost_group_id = p_cost_group_id
2403             AND    wppb.pac_period_id = p_pac_period_id
2404             AND    wppb.cost_type_id = p_cost_type_id
2405             AND    wppb.wip_entity_id = p_entity_id
2406             AND    nvl(wppb.line_id, -99) = nvl(p_line_id,-99);
2407 
2408             -- Statement level log message for FND logging
2409             IF (l_sLog) THEN
2410                 FND_LOG.STRING (FND_LOG.LEVEL_STATEMENT,
2411                                 l_module || '.'||l_stmt_num,
2412                                 ' Negative Value in Job of Cost element = 1');
2413             END IF;
2414      END IF;
2415 
2416      IF (l_job_balance(2) < 0) THEN
2417 
2418             l_stmt_num := 300;
2419             UPDATE CST_PAC_REQ_OPER_COST_DETAILS cprocd
2420             SET    cprocd.Comp_variance = nvl(cprocd.Comp_variance,0) + cprocd.Temp_Relieved_value,
2421                    cprocd.Relieved_value = cprocd.Relieved_value - cprocd.Temp_Relieved_value,
2422                    cprocd.last_update_date = SYSDATE,
2423                    cprocd.last_updated_by = p_user_id,
2424                    cprocd.last_update_login = p_login_id,
2425                    cprocd.request_id = p_request_id,
2426                    cprocd.program_application_id = p_prog_app_id,
2427                    cprocd.program_id = p_prog_id,
2428                    cprocd.program_update_date = SYSDATE
2429             WHERE  cprocd.cost_group_id = p_cost_group_id
2430             AND    cprocd.pac_period_id = p_pac_period_id
2431             AND    cprocd.wip_entity_id = p_entity_id
2432             AND    nvl(cprocd.line_id, -99) = nvl(p_line_id,-99)
2433             AND    cprocd.cost_element_id = 2;  -- Material Overhead Cost Element
2434 
2435             l_stmt_num := 310;
2436             UPDATE wip_pac_period_balances wppb
2437                     -- New column to store the variance in case total job balance is negative
2438             SET    wppb.pl_material_overhead_temp_var = wppb.pl_material_overhead_temp,
2439                    --   This column is  storing accumlated values of variance
2440                    wppb.pl_material_overhead_var = nvl(wppb.pl_material_overhead_var,0) + nvl(wppb.pl_material_overhead_temp,0),
2441                    --Subtract the variance column value from OUT column
2442                    wppb.pl_material_overhead_out = wppb.pl_material_overhead_out - wppb.pl_material_overhead_temp,
2443                    -- Make TEMP value to Zero
2444                    wppb.pl_material_overhead_temp = 0,
2445                    wppb.last_update_date = SYSDATE,
2446                    wppb.last_updated_by = p_user_id,
2447                    wppb.last_update_login = p_login_id,
2448                    wppb.request_id = p_request_id,
2449                    wppb.program_application_id = p_prog_app_id,
2450                    wppb.program_id = p_prog_id,
2451                    wppb.program_update_date = SYSDATE
2452             WHERE  wppb.cost_group_id = p_cost_group_id
2453             AND    wppb.pac_period_id = p_pac_period_id
2454             AND    wppb.cost_type_id = p_cost_type_id
2455             AND    wppb.wip_entity_id = p_entity_id
2456             AND    nvl(wppb.line_id, -99) = nvl(p_line_id,-99);
2457 
2458             -- Statement level log message for FND logging
2459             IF (l_sLog) THEN
2460                 FND_LOG.STRING (FND_LOG.LEVEL_STATEMENT,
2461                                 l_module || '.'||l_stmt_num,
2462                                 ' Negative Value in Job for Cost element = 2');
2463             END IF;
2464      END IF;
2465 
2466      IF (l_job_balance(3) < 0) THEN
2467 
2468             l_stmt_num := 320;
2469             UPDATE CST_PAC_REQ_OPER_COST_DETAILS cprocd
2470             SET    cprocd.Comp_variance = nvl(cprocd.Comp_variance,0) + cprocd.Temp_Relieved_value,
2471                    cprocd.Relieved_value = cprocd.Relieved_value - cprocd.Temp_Relieved_value,
2472                    cprocd.last_update_date = SYSDATE,
2473                    cprocd.last_updated_by = p_user_id,
2474                    cprocd.last_update_login = p_login_id,
2475                    cprocd.request_id = p_request_id,
2476                    cprocd.program_application_id = p_prog_app_id,
2477                    cprocd.program_id = p_prog_id,
2478                    cprocd.program_update_date = SYSDATE
2479             WHERE  cprocd.cost_group_id = p_cost_group_id
2480             AND    cprocd.pac_period_id = p_pac_period_id
2481             AND    cprocd.wip_entity_id = p_entity_id
2482             AND    nvl(cprocd.line_id, -99) = nvl(p_line_id,-99)
2483             AND    cprocd.cost_element_id = 3;  -- Resource Cost Element
2484 
2485             l_stmt_num := 330;
2486             UPDATE wip_pac_period_balances wppb
2487                    -- New column to store the variance in case if total job balance is negative
2488             SET    wppb.pl_resource_temp_var = wppb.pl_resource_temp,
2489                    --   This column is  storing accumlated values of variance
2490                    wppb.pl_resource_var = nvl(wppb.pl_resource_var,0) + nvl(wppb.pl_resource_temp,0),
2491                    --Subtract the variance column value from OUT column
2492                    wppb.pl_resource_out = wppb.pl_resource_out - wppb.pl_resource_temp,
2493                    -- Make TEMP value to Zero
2494                    wppb.pl_resource_temp = 0,
2495                    wppb.last_update_date = SYSDATE,
2496                    wppb.last_updated_by = p_user_id,
2497                    wppb.last_update_login = p_login_id,
2498                    wppb.request_id = p_request_id,
2499                    wppb.program_application_id = p_prog_app_id,
2500                    wppb.program_id = p_prog_id,
2501                    wppb.program_update_date = SYSDATE
2502             WHERE  wppb.cost_group_id = p_cost_group_id
2503             AND    wppb.pac_period_id = p_pac_period_id
2504             AND    wppb.cost_type_id = p_cost_type_id
2505             AND    wppb.wip_entity_id = p_entity_id
2506             AND    nvl(wppb.line_id, -99) = nvl(p_line_id,-99);
2507 
2508             -- Statement level log message for FND logging
2509             IF (l_sLog) THEN
2510                 FND_LOG.STRING (FND_LOG.LEVEL_STATEMENT,
2511                                 l_module || '.'||l_stmt_num,
2512                                 ' Negative Value in Job for Cost element = 3');
2513             END IF;
2514      END IF;
2515 
2516      IF (l_job_balance(4) < 0) THEN
2517 
2518             l_stmt_num := 340;
2519             UPDATE CST_PAC_REQ_OPER_COST_DETAILS cprocd
2520             SET    cprocd.Comp_variance = nvl(cprocd.Comp_variance,0) + cprocd.Temp_Relieved_value,
2521                    cprocd.Relieved_value = cprocd.Relieved_value - cprocd.Temp_Relieved_value,
2522                    cprocd.last_update_date = SYSDATE,
2523                    cprocd.last_updated_by = p_user_id,
2524                    cprocd.last_update_login = p_login_id,
2525                    cprocd.request_id = p_request_id,
2526                    cprocd.program_application_id = p_prog_app_id,
2527                    cprocd.program_id = p_prog_id,
2528                    cprocd.program_update_date = SYSDATE
2529             WHERE  cprocd.cost_group_id = p_cost_group_id
2530             AND    cprocd.pac_period_id = p_pac_period_id
2531             AND    cprocd.wip_entity_id = p_entity_id
2532             AND    nvl(cprocd.line_id, -99) = nvl(p_line_id,-99)
2533             AND    cprocd.cost_element_id = 4;  -- Outside Processing Cost Element
2534 
2535             l_stmt_num := 350;
2536             UPDATE wip_pac_period_balances wppb
2537                    -- New column to store the varaince in case total job balance is negative
2538             SET    wppb.pl_outside_processing_temp_var = wppb.pl_outside_processing_temp,
2539                    --   This column is  storing accumlated values of variance
2540                    wppb.pl_outside_processing_var = nvl(wppb.pl_outside_processing_var,0) + nvl(wppb.pl_outside_processing_temp,0),
2541                    --Subtract the variance column value from OUT column
2542                    wppb.pl_outside_processing_out = wppb.pl_outside_processing_out - wppb.pl_outside_processing_temp,
2543                    -- Make TEMP value to Zero
2544                    wppb.pl_outside_processing_temp = 0,
2545                    wppb.last_update_date = SYSDATE,
2546                    wppb.last_updated_by = p_user_id,
2547                    wppb.last_update_login = p_login_id,
2548                    wppb.request_id = p_request_id,
2549                    wppb.program_application_id = p_prog_app_id,
2550                    wppb.program_id = p_prog_id,
2551                    wppb.program_update_date = SYSDATE
2552             WHERE  wppb.cost_group_id = p_cost_group_id
2553             AND    wppb.pac_period_id = p_pac_period_id
2554             AND    wppb.cost_type_id = p_cost_type_id
2555             AND    wppb.wip_entity_id = p_entity_id
2556             AND    nvl(wppb.line_id, -99) = nvl(p_line_id,-99);
2557 
2558             -- Statement level log message for FND logging
2559             IF (l_sLog) THEN
2560                 FND_LOG.STRING (FND_LOG.LEVEL_STATEMENT,
2561                                 l_module || '.'||l_stmt_num,
2562                                 ' Negative Value in Job for Cost element = 4');
2563             END IF;
2564      END IF;
2565 
2566      IF (l_job_balance(5) < 0) THEN
2567 
2568             l_stmt_num := 360;
2569             UPDATE CST_PAC_REQ_OPER_COST_DETAILS cprocd
2570             SET    cprocd.Comp_variance = nvl(cprocd.Comp_variance,0) + cprocd.Temp_Relieved_value,
2571                    cprocd.Relieved_value = cprocd.Relieved_value - cprocd.Temp_Relieved_value,
2572                    cprocd.last_update_date = SYSDATE,
2573                    cprocd.last_updated_by = p_user_id,
2574                    cprocd.last_update_login = p_login_id,
2575                    cprocd.request_id = p_request_id,
2576                    cprocd.program_application_id = p_prog_app_id,
2577                    cprocd.program_id = p_prog_id,
2578                    cprocd.program_update_date = SYSDATE
2579             WHERE  cprocd.cost_group_id = p_cost_group_id
2580             AND    cprocd.pac_period_id = p_pac_period_id
2581             AND    cprocd.wip_entity_id = p_entity_id
2582             AND    nvl(cprocd.line_id, -99) = nvl(p_line_id,-99)
2583             AND    cprocd.cost_element_id = 5;  -- Overhead Processing Cost Element
2584 
2585             l_stmt_num := 370;
2586             UPDATE wip_pac_period_balances wppb
2587                    -- New column to store the varaince in case total job balance is negative
2588             SET    wppb.pl_overhead_temp_var = wppb.pl_overhead_temp,
2589                    --   This column is  storing accumlated values of variance
2590                    wppb.pl_overhead_var = nvl(wppb.pl_overhead_var,0) + nvl(wppb.pl_overhead_temp,0),
2591                    --Subtract the variance column value from OUT column
2592                    wppb.pl_overhead_out = wppb.pl_overhead_out - wppb.pl_overhead_temp,
2593                    -- Make TEMP value to Zero
2594                    wppb.pl_overhead_temp = 0,
2595                    wppb.last_update_date = SYSDATE,
2596                    wppb.last_updated_by = p_user_id,
2597                    wppb.last_update_login = p_login_id,
2598                    wppb.request_id = p_request_id,
2599                    wppb.program_application_id = p_prog_app_id,
2600                    wppb.program_id = p_prog_id,
2601                    wppb.program_update_date = SYSDATE
2602             WHERE  wppb.cost_group_id = p_cost_group_id
2603             AND    wppb.pac_period_id = p_pac_period_id
2604             AND    wppb.cost_type_id = p_cost_type_id
2605             AND    wppb.wip_entity_id = p_entity_id
2606             AND    nvl(wppb.line_id, -99) = nvl(p_line_id,-99);
2607 
2608             -- Statement level log message for FND logging
2609             IF (l_sLog) THEN
2610                 FND_LOG.STRING (FND_LOG.LEVEL_STATEMENT,
2611                                 l_module || '.'||l_stmt_num,
2612                                 ' Negative Value in Job for Cost element = 5');
2613             END IF;
2614      END IF;
2615 
2616   IF (l_pLog) THEN
2617    FND_LOG.STRING (FND_LOG.LEVEL_PROCEDURE,
2618                    l_module || '.end',
2619                    l_api_name || ' >>>');
2620   END IF;
2621 
2622   EXCEPTION
2623   WHEN OTHERS THEN
2624         fnd_file.put_line(fnd_file.log,' Exception '||to_char(l_stmt_num));
2625         IF (l_uLog) THEN
2626            FND_LOG.STRING (FND_LOG.LEVEL_UNEXPECTED,
2627                            l_module || '.' || l_stmt_num,
2628                            SQLERRM);
2629         END IF;
2630         ROLLBACK;
2631         x_err_num := SQLCODE;
2632         x_err_code := NULL;
2633         x_err_msg := SUBSTR('CSTPPWAS.Relief_BOM_Quantity('
2634                                 || to_char(l_stmt_num)
2635                                 || '): '
2636                                 ||SQLERRM,1,240);
2637 END Relief_BOM_Quantity;
2638 
2639 /*---------------------------------------------------------------------------*
2640 |  PRIVATE PROCEDURE                                                         |
2641 |       process_net_relief                                                     |
2642 *----------------------------------------------------------------------------*/
2643 PROCEDURE process_net_relief (
2644                                 p_pac_period_id             IN    NUMBER,
2645                                 p_prior_period_id           IN    NUMBER,
2646                                 p_cost_group_id             IN    NUMBER,
2647                                 p_cost_type_id              IN    NUMBER,
2648                                 p_legal_entity              IN    NUMBER,
2649                                 p_cost_method               IN    NUMBER,
2650                                 p_cost_layer_id             IN    NUMBER,
2651                                 p_qty_layer_id              IN    NUMBER,
2652                                 p_pac_rates_id              IN    NUMBER,
2653                                 p_entity_type               IN    NUMBER,
2654                                 p_org_id                    IN    NUMBER,
2655                                 p_entity_id                 IN    NUMBER,
2656                                 p_final_completion_flag     IN    NUMBER,
2657                                 p_material_relief_algorithm IN    NUMBER,
2658                                 p_line_id                   IN    NUMBER DEFAULT NULL,
2659                                 p_net_qty                   IN    NUMBER,
2660                                 p_start_date                IN    DATE,
2661                                 p_end_date                  IN    DATE,
2662                                 p_scrap                     IN    NUMBER DEFAULT -1,
2663                                 p_op_seq                    IN    NUMBER DEFAULT NULL,
2664                                 p_master_org_id             IN    NUMBER,
2665                                 p_uom_control               IN    NUMBER,
2666                                 p_user_id                   IN    NUMBER,
2667                                 p_login_id                  IN    NUMBER,
2668                                 p_request_id                IN    NUMBER,
2669                                 p_prog_id                   IN    NUMBER DEFAULT -1,
2670                                 p_prog_app_id               IN    NUMBER DEFAULT -1,
2671                                 p_txn_category              IN    NUMBER,
2672                                 x_err_num                   OUT NOCOPY        NUMBER,
2673                                 x_err_code                  OUT NOCOPY        VARCHAR2,
2674                                 x_err_msg                   OUT NOCOPY        VARCHAR2)
2675 IS
2676 CURSOR c_scrap_txn IS
2677         SELECT  mmt.transaction_id txn_id,
2678                 mmt.inventory_item_id item_id,
2679                 mmt.primary_quantity pri_qty,
2680                 mmt.organization_id org_id,
2681                 mmt.subinventory_code subinv,
2682                 mmt.transaction_action_id txn_action_id,
2683                 mmt.transaction_source_type_id txn_src_type_id
2684         FROM    mtl_material_transactions mmt
2685         WHERE   mmt.transaction_date BETWEEN TRUNC(p_start_date)
2686                                      AND (TRUNC(p_end_date) + 0.99999)
2687         AND     mmt.transaction_source_type_id = 5
2688         AND     mmt.transaction_source_id = p_entity_id
2689         AND     NVL(mmt.repetitive_line_id,-99) = NVL(p_line_id,-99)
2690         AND     mmt.transaction_action_id  = 30
2691         AND     NVL(mmt.operation_seq_num, -1) = NVL(p_op_seq,-1)
2692         ORDER BY mmt.primary_quantity DESC, mmt.transaction_id; -- minimize the occurences of negative periodic inventory quantity
2693 
2694 CURSOR c_assy_txn IS
2695         SELECT  mmt.transaction_id txn_id,
2696                 mmt.inventory_item_id item_id,
2697                 mmt.primary_quantity pri_qty,
2698                 mmt.organization_id org_id,
2699                 mmt.subinventory_code subinv,
2700                 mmt.transaction_action_id txn_action_id,
2701                 mmt.transaction_source_type_id txn_src_type_id
2702         FROM    mtl_material_transactions mmt
2703         WHERE   mmt.transaction_date BETWEEN TRUNC(p_start_date)
2704                                    AND (TRUNC(p_end_date) + 0.99999)
2705         AND     mmt.transaction_source_type_id = 5
2706         AND     mmt.transaction_source_id = p_entity_id
2707         AND     NVL(mmt.repetitive_line_id,-99) = NVL(p_line_id,-99)
2708         AND     mmt.transaction_action_id IN (31,32)
2709         ORDER BY mmt.transaction_action_id, mmt.transaction_id; -- minimize the occurences of negative periodic inventory quantity
2710 
2711 l_uom_conv_rate                 NUMBER;
2712 l_conv_net_qty                  NUMBER;
2713 l_item_id                       NUMBER;
2714 l_org_id                        NUMBER;
2715 l_stmt_num                      NUMBER;
2716 l_err_num                       NUMBER;
2717 l_err_code                      VARCHAR2(240);
2718 l_err_msg                       VARCHAR2(240);
2719 l_exp_item                      NUMBER;
2720 l_exp_flag                      NUMBER;
2721 l_wip_assy_hook                 NUMBER;
2722 cst_process_error               EXCEPTION;
2723 /* Added new local variables for R12 PAC enhancements */
2724 l_assembly_return_cnt          NUMBER;
2725 l_net_qty                      NUMBER;
2726 l_completed_assembly_qty       NUMBER;
2727 l_net_completion               NUMBER;
2728 l_prior_completed_units        NUMBER := 0;
2729 
2730 /* Start bug6847717 */
2731 l_net_tl_resource_value           NUMBER;
2732 l_net_tl_overhead_value           NUMBER;
2733 l_net_tl_osp_value                NUMBER;
2734 l_net_pl_material_value           NUMBER;
2735 l_net_pl_moh_value                NUMBER;
2736 l_net_pl_resource_value           NUMBER;
2737 l_net_pl_osp_value                NUMBER;
2738 l_net_pl_overhead_value           NUMBER;
2739 /* End bug6847717 */
2740 
2741 l_api_name            CONSTANT VARCHAR2(30) := 'process_net_relief';
2742 l_full_name           CONSTANT VARCHAR2(60) := G_PKG_NAME || '.' || l_api_name;
2743 l_module              CONSTANT VARCHAR2(60) := 'cst.plsql.' || l_full_name;
2744 
2745 l_uLog  CONSTANT BOOLEAN := FND_LOG.LEVEL_UNEXPECTED >= G_LOG_LEVEL AND FND_LOG.TEST (FND_LOG.LEVEL_UNEXPECTED, l_module);
2746 l_errorLog CONSTANT BOOLEAN := l_uLog AND (FND_LOG.LEVEL_ERROR >= G_LOG_LEVEL);
2747 l_exceptionLog CONSTANT BOOLEAN := l_errorLog AND (FND_LOG.LEVEL_EXCEPTION >= G_LOG_LEVEL);
2748 l_eventLog CONSTANT BOOLEAN := l_exceptionLog AND (FND_LOG.LEVEL_EVENT >= G_LOG_LEVEL);
2749 l_pLog CONSTANT BOOLEAN := l_eventLog AND (FND_LOG.LEVEL_PROCEDURE >= G_LOG_LEVEL);
2750 l_sLog CONSTANT BOOLEAN := l_pLog AND (FND_LOG.LEVEL_STATEMENT >= G_LOG_LEVEL);
2751 
2752 BEGIN
2753 
2754         IF (l_pLog) THEN
2755          FND_LOG.STRING (FND_LOG.LEVEL_PROCEDURE,
2756                          l_module || '.begin',
2757                          l_api_name || ' <<<');
2758 
2759         FND_LOG.STRING (FND_LOG.LEVEL_PROCEDURE,
2760                         l_module || '.begin',
2761                         l_api_name || ' <<< Parameters:
2762                         p_cost_layer_id = ' || p_cost_layer_id || '
2763                         p_qty_layer_id = ' || p_qty_layer_id || '
2764                         p_entity_type = ' || p_entity_type || '
2765                         p_org_id = ' || p_org_id || '
2766                         p_entity_id = ' || p_entity_id ||'
2767                         p_final_completion_flag = ' || p_final_completion_flag ||'
2768                         p_material_relief_algorithm = ' || p_material_relief_algorithm ||'
2769                         p_line_id = ' || p_line_id ||'
2770                         p_net_qty = ' || p_net_qty ||'
2771                         p_scrap = ' || p_scrap ||'
2772                         p_op_seq = ' || p_op_seq ||'
2773                         p_txn_category = ' || p_txn_category);
2774         END IF;
2775         ----------------------------------------------------------------------
2776         -- Initialize Variables
2777         ----------------------------------------------------------------------
2778 
2779         l_err_num := 0;
2780         l_err_code := '';
2781         l_err_msg := '';
2782         l_wip_assy_hook := -1;
2783 
2784        IF (p_scrap = 1 AND p_entity_type <> 4) THEN
2785 
2786            SELECT SUM(mmt.primary_quantity)
2787            INTO   l_net_completion
2788            FROM   mtl_material_transactions mmt
2789            WHERE  mmt.transaction_source_id = p_entity_id
2790            AND    mmt.transaction_date BETWEEN TRUNC(p_start_date)
2791                                         AND (TRUNC(p_end_date) + 0.99999)
2792            AND    mmt.transaction_source_type_id = 5
2793            AND    mmt.transaction_action_id IN (31,32);
2794 
2795        END IF;
2796 
2797         ----------------------------------------------------------------------
2798         -- This proc will be called for those entities/line that have a record
2799         -- in wppb.
2800         -- There is no need to check/create Create WIP BAL Rec as such
2801         -- a record will always exist when this proc is called.
2802         -- This is becasue a completion/scrap/return always required
2803         -- a preceding move transactions.  The move transactions
2804         -- will create the WIPBAL record in the process_wipresovhd
2805         -- procedure.  For CFMs, the rec will be created while processing
2806         -- the res_ovhds or material component issue txns.
2807         ----------------------------------------------------------------------
2808 
2809 
2810 
2811         ----------------------------------------------------------------------
2812         -- Reset the Temp columns
2813         ----------------------------------------------------------------------
2814 
2815         l_stmt_num := 0;
2816 
2817         UPDATE  wip_pac_period_balances wppb
2818         SET     tl_resource_temp = 0,
2819                 tl_overhead_temp = 0,
2820                 tl_outside_processing_temp = 0,
2821                 pl_material_temp = 0,
2822                 pl_material_overhead_temp = 0,
2823                 pl_resource_temp = 0,
2824                 pl_outside_processing_temp = 0,
2825                 pl_overhead_temp = 0
2826         WHERE   wppb.pac_period_id = p_pac_period_id
2827         AND     wppb.cost_group_id = p_cost_group_id
2828         AND     wppb.wip_entity_id = p_entity_id
2829         AND     NVL(wppb.line_id,-99) = decode(p_entity_type, 4, -99, NVL(p_line_id,-99));
2830 
2831         ----------------------------------------------------------------------
2832         -- Relieve Costs
2833         ----------------------------------------------------------------------
2834 
2835 
2836           --------------------------------------------------------------------
2837           -- Check whether CFM  completion/return/scrap.
2838           -- For non-scheduled CFMs, Each transaction will be Unique
2839           -- WIP entity.
2840           -- For CFMs, We do not distinguish between net completion and return.
2841           -- The Variance column has no meaning for CFM entities.
2842           -- Flush/Relieve all costs from the entity as:
2843           -- IN-OUT-VAR
2844           --------------------------------------------------------------------
2845 
2846           IF (p_entity_type = 4) THEN
2847 
2848             l_stmt_num := 10;
2849 
2850             UPDATE wip_pac_period_balances wppb
2851             SET (tl_resource_out,
2852                  tl_resource_temp,
2853                  tl_outside_processing_out,
2854                  tl_outside_processing_temp,
2855                  tl_overhead_out,
2856                  tl_overhead_temp,
2857                  pl_material_out,
2858                  pl_material_temp,
2859                  pl_material_overhead_out,
2860                  pl_material_overhead_temp,
2861                  pl_resource_out,
2862                  pl_resource_temp,
2863                  pl_outside_processing_out,
2864                  pl_outside_processing_temp,
2865                  pl_overhead_out,
2866                  pl_overhead_temp
2867                 ) =
2868             (SELECT
2869             (NVL(wppb.tl_resource_in,0)-NVL(wppb.tl_resource_out,0)
2870                                         -NVL(wppb.tl_resource_var,0)),
2871             (NVL(wppb.tl_resource_in,0)-NVL(wppb.tl_resource_out,0)
2872                                         -NVL(wppb.tl_resource_var,0)),
2873             (NVL(wppb.tl_outside_processing_in,0)
2874                                 - NVL(wppb.tl_outside_processing_out,0)
2875                                 - NVL(wppb.tl_outside_processing_var,0)),
2876             (NVL(wppb.tl_outside_processing_in,0)
2877                                 - NVL(wppb.tl_outside_processing_out,0)
2878                                 - NVL(wppb.tl_outside_processing_var,0)),
2879             (NVL(wppb.tl_overhead_in,0)-NVL(wppb.tl_overhead_out,0)
2880                                 - NVL(wppb.tl_overhead_var,0)),
2881             (NVL(wppb.tl_overhead_in,0)-NVL(wppb.tl_overhead_out,0)
2882                                 - NVL(wppb.tl_overhead_var,0)),
2883             (NVL(wppb.pl_material_in,0)-NVL(wppb.pl_material_out,0)
2884                                 - NVL(wppb.pl_material_var,0)),
2885             (NVL(wppb.pl_material_in,0)-NVL(wppb.pl_material_out,0)
2886                                 - NVL(wppb.pl_material_var,0)),
2887             (NVL(wppb.pl_material_overhead_in,0)
2888                                 - NVL(wppb.pl_material_overhead_out,0)
2889                                 - NVL(wppb.pl_material_overhead_var,0)),
2890             (NVL(wppb.pl_material_overhead_in,0)
2891                                 - NVL(wppb.pl_material_overhead_out,0)
2892                                 - NVL(wppb.pl_material_overhead_var,0)),
2893             (NVL(wppb.pl_resource_in,0)-NVL(wppb.pl_resource_out,0)
2894                                 - NVL(wppb.pl_resource_var,0)),
2895             (NVL(wppb.pl_resource_in,0)-NVL(wppb.pl_resource_out,0)
2896                                 - NVL(wppb.pl_resource_var,0)),
2897             (NVL(wppb.pl_outside_processing_in,0)
2898                                 - NVL(wppb.pl_outside_processing_out,0)
2899                                 - NVL(wppb.pl_outside_processing_var,0)),
2900             (NVL(wppb.pl_outside_processing_in,0)
2901                                 - NVL(wppb.pl_outside_processing_out,0)
2902                                 - NVL(wppb.pl_outside_processing_var,0)),
2903             (NVL(wppb.pl_overhead_in,0)-NVL(wppb.pl_overhead_out,0)
2904                                 - NVL(wppb.pl_overhead_var,0)),
2905             (NVL(wppb.pl_overhead_in,0)-NVL(wppb.pl_overhead_out,0)
2906                                 - NVL(wppb.pl_overhead_var,0))
2907              FROM wip_pac_period_balances wppb2,
2908                   wip_flow_schedules wfs
2909              WHERE wppb2.pac_period_id = p_pac_period_id
2910              AND   wppb2.cost_group_id = p_cost_group_id
2911              AND   wppb2.wip_entity_id = p_entity_id
2912              AND   wppb2.operation_seq_num = wppb.operation_seq_num
2913              AND   wfs.wip_entity_id = p_entity_id
2914             )
2915             WHERE wppb.pac_period_id = p_pac_period_id
2916             AND wppb.cost_group_id = p_cost_group_id
2917             AND wppb.wip_entity_id = p_entity_id
2918             AND wppb.operation_seq_num <=
2919                 decode(p_scrap,1,nvl(p_op_seq,wppb.operation_seq_num),wppb.operation_seq_num);
2920           ELSE -- If not CFM completion/return
2921               /* material Completion Algo is based on BOM */
2922               /* This algo relieves the material costs based on the BOM */
2923               l_stmt_num := 20;
2924 
2925               IF ( p_material_relief_algorithm = 0 ) THEN
2926                 Relief_BOM_Quantity(p_pac_period_id          => p_pac_period_id,
2927                                     p_prior_period_id        => p_prior_period_id,
2928                                     p_cost_group_id          => p_cost_group_id ,
2929                                     p_cost_type_id           => p_cost_type_id,
2930                                     p_entity_id              => p_entity_id,
2931                                     p_line_id                => p_line_id,
2932                                     p_net_qty                => p_net_qty,
2933                                     p_final_completion_flag  => p_final_completion_flag,
2934                                     p_scrap                  => p_scrap,
2935                                     p_op_seq                 => p_op_seq,
2936                                     p_start_date             => p_start_date,
2937                                     p_end_date               => p_end_date,
2938                                     p_login_id               => p_login_id,
2939                                     p_user_id                => p_user_id,
2940                                     p_request_id             => p_request_id,
2941                                     p_prog_id                => p_prog_id,
2942                                     p_prog_app_id            => p_prog_app_id,
2943                                     x_err_num                => x_err_num,
2944                                     x_err_code               => x_err_code,
2945                                     x_err_msg                => x_err_msg );
2946                  /* Calculation TL resource, Overhead and OSP */
2947                  /* They always will be relieved based on actuals */
2948                  IF (p_net_qty > 0) THEN
2949                          l_stmt_num := 25;
2950 
2951 	       /* Bug6847717: Calculate the total value across op_seq */
2952 	       SELECT NVL(SUM(NVL(wppb.tl_resource_in,0)-
2953                           NVL(wppb.tl_resource_out,0)
2954                           - NVL(wppb.tl_resource_var,0)),0) net_tl_resource_value,
2955 	              NVL(SUM(NVL(wppb.tl_overhead_in,0)-
2956                           NVL(wppb.tl_overhead_out,0)
2957                           - NVL(wppb.tl_overhead_var,0)),0) net_tl_overhead_value,
2958                       NVL(SUM(NVL(wppb.tl_outside_processing_in,0)-
2959                           NVL(wppb.tl_outside_processing_out,0)
2960                           - NVL(wppb.tl_outside_processing_var,0)),0) net_tl_osp_value
2961                  INTO
2962                  l_net_tl_resource_value,
2963                  l_net_tl_overhead_value,
2964                  l_net_tl_osp_value
2965                FROM wip_pac_period_balances wppb
2966                 WHERE wppb.pac_period_id = p_pac_period_id
2967                   AND wppb.cost_group_id = p_cost_group_id
2968                   AND wppb.wip_entity_id = p_entity_id
2969                   AND NVL(wppb.line_id,-99) = NVL(p_line_id,-99)
2970                   AND wppb.operation_seq_num <=
2971                       decode(p_scrap,1,p_op_seq,wppb.operation_seq_num);
2972 
2973                          /* Final completion exists */
2974                          IF p_final_completion_flag = 1 THEN
2975 
2976                               UPDATE wip_pac_period_balances wppb
2977                               SET (tl_resource_out,
2978                                    tl_resource_temp,
2979                                    tl_outside_processing_out,
2980                                    tl_outside_processing_temp,
2981                                    tl_overhead_out,
2982                                    tl_overhead_temp
2983                                   ) =
2984                                   (SELECT
2985                                        NVL(wppb.tl_resource_out,0) +
2986 				       decode(SIGN(SIGN(NVL(wppb.tl_resource_in,0)
2987 						       -NVL(wppb.tl_resource_out,0)
2988 						       -NVL(tl_resource_var,0))
2989 						       +SIGN(l_net_tl_resource_value)
2990 						       +2
2991 						       ),
2992                                                 1,
2993 						(NVL(wppb.tl_resource_in,0)-NVL(wppb.tl_resource_out,0)
2994                                                            - NVL(tl_resource_var,0)),
2995                                                 0
2996                                              ),
2997 				       decode(SIGN(SIGN(NVL(wppb.tl_resource_in,0)
2998 						       -NVL(wppb.tl_resource_out,0)
2999 						       -NVL(tl_resource_var,0))
3000 						       +SIGN(l_net_tl_resource_value)
3001 						       +2
3002 						       ),
3003                                                 1,
3004                                                 (NVL(wppb.tl_resource_in,0)-NVL(wppb.tl_resource_out,0)
3005                                                           - NVL(tl_resource_var,0)),
3006                                                 0
3007                                              ),
3008                                        NVL(tl_outside_processing_out,0) +
3009 				       decode(SIGN(SIGN(NVL(wppb.tl_outside_processing_in,0)
3010 						       -NVL(wppb.tl_outside_processing_out,0)
3011 						       -NVL(wppb.tl_outside_processing_var,0))
3012 						       +SIGN(l_net_tl_osp_value)
3013 						       +2
3014 						       ),
3015                                                  1,
3016                                                  (NVL(wppb.tl_outside_processing_in,0)
3017                                                       - NVL(wppb.tl_outside_processing_out,0)
3018                                                       - NVL(wppb.tl_outside_processing_var,0)),
3019                                                  0
3020                                             ),
3021 				       decode(SIGN(SIGN(NVL(wppb.tl_outside_processing_in,0)
3022 			                               -NVL(wppb.tl_outside_processing_out,0)
3023                         			       -NVL(wppb.tl_outside_processing_var,0))
3024 						       +SIGN(l_net_tl_osp_value)
3025 						       +2
3026 						       ),
3027                                                1,
3028                                                (NVL(wppb.tl_outside_processing_in,0)
3029                                                   - NVL(wppb.tl_outside_processing_out,0)
3030                                                   - NVL(wppb.tl_outside_processing_var,0)),
3031                                                0
3032                                             ),
3033                                        NVL(tl_overhead_out,0) +
3034 				       decode(SIGN(SIGN(NVL(wppb.tl_overhead_in,0)
3035 						       -NVL(wppb.tl_overhead_out,0)
3036 						       -NVL(wppb.tl_overhead_var,0))
3037 						       +SIGN(l_net_tl_overhead_value)
3038 						       +2
3039 						       ),
3040                                               1,
3041                                              (NVL(wppb.tl_overhead_in,0)-NVL(wppb.tl_overhead_out,0)
3042                                                         - NVL(wppb.tl_overhead_var,0)),
3043                                              0
3044                                             ),
3045 				       decode(SIGN(SIGN(NVL(wppb.tl_overhead_in,0)
3046 						       -NVL(wppb.tl_overhead_out,0)
3047 						       -NVL(wppb.tl_overhead_var,0))
3048 						       +SIGN(l_net_tl_overhead_value)
3049 						       +2
3050 						       ),
3051                                               1,
3052                                               (NVL(wppb.tl_overhead_in,0)-NVL(wppb.tl_overhead_out,0)
3053                                                         - NVL(wppb.tl_overhead_var,0)),
3054                                               0
3055                                              )
3056                                   FROM  wip_pac_period_balances wppb2
3057                                   WHERE wppb2.pac_period_id =p_pac_period_id
3058                                   AND   wppb2.cost_group_id = p_cost_group_id
3059                                   AND   wppb2.wip_entity_id = p_entity_id
3060                                   AND   NVL(wppb2.line_id,-99) = NVL(p_line_id,-99)
3061                                   AND   wppb2.operation_seq_num = wppb.operation_seq_num
3062                                )
3063                               WHERE wppb.pac_period_id = p_pac_period_id
3064                               AND   wppb.cost_group_id = p_cost_group_id
3065                               AND   wppb.wip_entity_id = p_entity_id
3066                               AND   NVL(wppb.line_id,-99) = NVL(p_line_id,-99)
3067                               AND   wppb.operation_seq_num <= wppb.operation_seq_num;
3068                         ELSE    /* No Final completion exists */
3069                               UPDATE wip_pac_period_balances wppb
3070                               SET (tl_resource_out,
3071                                    tl_resource_temp,
3072                                    tl_outside_processing_out,
3073                                    tl_outside_processing_temp,
3074                                    tl_overhead_out,
3075                                    tl_overhead_temp
3076                                   ) =
3077                                   (SELECT
3078                                        NVL(wppb.tl_resource_out,0) +
3079 				       decode(SIGN(SIGN(NVL(wppb.tl_resource_in,0)
3080 						       -NVL(wppb.tl_resource_out,0)
3081 						       -NVL(tl_resource_var,0))
3082 						       +SIGN(l_net_tl_resource_value)
3083 						       +2
3084 						       ),
3085                                                1,
3086                                                (NVL(wppb.tl_resource_in,0)-NVL(wppb.tl_resource_out,0)
3087                                                         - NVL(tl_resource_var,0))*
3088                                                decode(SIGN(NVL(wppb2.operation_completed_units,0) -
3089                                                        NVL(wppb2.relieved_assembly_units,0) - nvl(unrelieved_scrap_quantity,0)),
3090                                                         0,  1,
3091                                                        -1,  1,
3092                                                         p_net_qty/ (NVL(wppb2.operation_completed_units,0) -
3093                                                        NVL(wppb2.relieved_assembly_units,0) - nvl(wppb2.unrelieved_scrap_quantity,0))
3094                                                      ),
3095                                               0
3096                                              ),
3097 				       decode(SIGN(SIGN(NVL(wppb.tl_resource_in,0)
3098 						       -NVL(wppb.tl_resource_out,0)
3099 						       -NVL(tl_resource_var,0))
3100 						       +SIGN(l_net_tl_resource_value)
3101 						       +2
3102 						       ),
3103                                                1,
3104                                               (NVL(wppb.tl_resource_in,0)-NVL(wppb.tl_resource_out,0)
3105                                                         - NVL(tl_resource_var,0))*
3106                                                     decode(SIGN(NVL(wppb2.operation_completed_units,0) -
3107                                                             NVL(wppb2.relieved_assembly_units,0) -
3108                                                             nvl(unrelieved_scrap_quantity,0)),
3109                                                              0,  1,
3110                                                              -1, 1,
3111                                                              p_net_qty/ (NVL(wppb2.operation_completed_units,0) -
3112                                                                 NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0))
3113                                                           ),
3114                                                0
3115                                             ),
3116                                        NVL(tl_outside_processing_out,0) +
3117 				       decode(SIGN(SIGN(NVL(wppb.tl_outside_processing_in,0)
3118 						       -NVL(wppb.tl_outside_processing_out,0)
3119 						       -NVL(wppb.tl_outside_processing_var,0))
3120 						       +SIGN(l_net_tl_osp_value)
3121 						       +2
3122 						       ),
3123                                                 1,
3124                                                 (NVL(wppb.tl_outside_processing_in,0)
3125                                                  - NVL(wppb.tl_outside_processing_out,0)
3126                                                  - NVL(wppb.tl_outside_processing_var,0))*
3127                                                         decode(SIGN(NVL(wppb2.operation_completed_units,0) -
3128                                                                 NVL(wppb2.relieved_assembly_units,0) -
3129                                                                 NVL(wppb2.unrelieved_scrap_quantity,0)),
3130                                                                 0,  1,
3131                                                                 -1, 1,
3132                                                                  p_net_qty/ (NVL(wppb2.operation_completed_units,0) -
3133                                                                     NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0))
3134                                                               ),
3135                                                0
3136                                             ),
3137 				       decode(SIGN(SIGN(NVL(wppb.tl_outside_processing_in,0)
3138 						       -NVL(wppb.tl_outside_processing_out,0)
3139 						       -NVL(wppb.tl_outside_processing_var,0))
3140 						       +SIGN(l_net_tl_osp_value)
3141 						       +2
3142 						       ),
3143                                         1,
3144                                         (NVL(wppb.tl_outside_processing_in,0)
3145                                                         - NVL(wppb.tl_outside_processing_out,0)
3146                                                         - NVL(wppb.tl_outside_processing_var,0))*
3147                                         decode(SIGN(NVL(wppb2.operation_completed_units,0) -
3148                                                 NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0)),
3149                                           0,  1,
3150                                           -1, 1,
3151                                           p_net_qty/ (NVL(wppb2.operation_completed_units,0) -
3152                                                     NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0))
3153                                         ),
3154                                         0
3155                                       ),
3156                                        NVL(tl_overhead_out,0) +
3157 				       decode(SIGN(SIGN(NVL(wppb.tl_overhead_in,0)
3158 						       -NVL(wppb.tl_overhead_out,0)
3159 						       -NVL(wppb.tl_overhead_var,0))
3160 						       +SIGN(l_net_tl_overhead_value)
3161 						       +2
3162 						       ),
3163                                         1,
3164                                         (NVL(wppb.tl_overhead_in,0)-NVL(wppb.tl_overhead_out,0)
3165                                                         - NVL(wppb.tl_overhead_var,0))*
3166                                         decode(SIGN(NVL(wppb2.operation_completed_units,0) -
3167                                                 NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0)),
3168                                           0,  1,
3169                                           -1, 1,
3170                                           p_net_qty/ (NVL(wppb2.operation_completed_units,0) -
3171                                                     NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0))
3172                                         ),
3173                                         0
3174                                       ),
3175 				       decode(SIGN(SIGN(NVL(wppb.tl_overhead_in,0)
3176 						       -NVL(wppb.tl_overhead_out,0)
3177 						       -NVL(wppb.tl_overhead_var,0))
3178 						       +SIGN(l_net_tl_overhead_value)
3179 						       +2
3180 						       ),
3181                                         1,
3182                                         (NVL(wppb.tl_overhead_in,0)-NVL(wppb.tl_overhead_out,0)
3183                                                         - NVL(wppb.tl_overhead_var,0))*
3184                                         decode(SIGN(NVL(wppb2.operation_completed_units,0) -
3185                                                 NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0)),
3186                                              0,  1,
3187                                           -1, 1,
3188                                           p_net_qty/ (NVL(wppb2.operation_completed_units,0) -
3189                                                     NVL(wppb2.relieved_assembly_units,0) - nvl(wppb2.unrelieved_scrap_quantity,0))
3190                                         ),
3191                                         0
3192                                       )
3193                                        FROM wip_pac_period_balances wppb2
3194                                        WHERE wppb2.pac_period_id = p_pac_period_id
3195                                        AND   wppb2.cost_group_id = p_cost_group_id
3196                                        AND   wppb2.wip_entity_id = p_entity_id
3197                                        AND   NVL(wppb2.line_id,-99) = NVL(p_line_id,-99)
3198                                        AND   wppb2.operation_seq_num = wppb.operation_seq_num
3199                               )
3200                               WHERE wppb.pac_period_id = p_pac_period_id
3201                               AND wppb.cost_group_id = p_cost_group_id
3202                               AND wppb.wip_entity_id = p_entity_id
3203                               AND NVL(wppb.line_id,-99) = NVL(p_line_id,-99)
3204                               AND wppb.operation_seq_num <=
3205                                         decode(p_scrap,1,p_op_seq,wppb.operation_seq_num);
3206                         END IF;
3207                    ELSIF (p_net_qty < 0) THEN
3208                        l_stmt_num := 28;
3209 
3210 		       /* Bug6847717: Calculate the total value across op_seq */
3211 		       SELECT NVL(SUM(NVL(wppb.tl_resource_out,0)),0) net_tl_resource_value,
3212 		              NVL(SUM(NVL(wppb.tl_overhead_out,0)),0) net_tl_overhead_value,
3213 		              NVL(SUM(NVL(wppb.tl_outside_processing_out,0)),0) net_tl_osp_value
3214 		       INTO
3215 		       l_net_tl_resource_value,
3216 		       l_net_tl_overhead_value,
3217 		       l_net_tl_osp_value
3218 		       FROM wip_pac_period_balances wppb
3219 		       WHERE wppb.pac_period_id = p_prior_period_id
3220 		         AND wppb.cost_group_id = p_cost_group_id
3221 	                 AND wppb.wip_entity_id = p_entity_id
3222                          AND NVL(wppb.line_id,-99) = NVL(p_line_id,-99)
3223                          AND wppb.operation_seq_num <=
3224                              decode(p_scrap,1,p_op_seq,wppb.operation_seq_num);
3225 
3226                        UPDATE wip_pac_period_balances wppb
3227                        SET (tl_resource_out,
3228                             tl_resource_temp,
3229                             tl_outside_processing_out,
3230                             tl_outside_processing_temp,
3231                             tl_overhead_out,
3232                             tl_overhead_temp) =
3233                              (SELECT
3234                                NVL(wppb.tl_resource_out,0) +
3235 			       decode(SIGN(SIGN(NVL(wppb2.tl_resource_out,0))
3236 					   +SIGN(l_net_tl_resource_value)
3237 					   +2
3238 					   ),
3239                                         1,
3240                                         NVL(wppb2.tl_resource_out,0) *
3241                                             p_net_qty/decode(NVL(wppb2.relieved_assembly_units,0),
3242                                                                 0,1,wppb2.relieved_assembly_units),
3243                                       0),
3244 			       decode(SIGN(SIGN(NVL(wppb2.tl_resource_out,0))
3245 					   +SIGN(l_net_tl_resource_value)
3246 					   +2
3247 					   ),
3248                                         1,
3249                                         NVL(wppb2.tl_resource_out,0) *
3250                                                p_net_qty/decode(NVL(wppb2.relieved_assembly_units,0),
3251                                                                 0,1,wppb2.relieved_assembly_units),
3252                                         0),
3253                                NVL(wppb.tl_outside_processing_out,0) +
3254 			       decode(SIGN(SIGN(NVL(wppb2.tl_outside_processing_out,0))
3255 					   +SIGN(l_net_tl_osp_value)
3256 					   +2
3257 					   ),
3258                                         1,
3259                                         NVL(wppb2.tl_outside_processing_out,0)*
3260                                                 p_net_qty/decode(NVL(wppb2.relieved_assembly_units,0),
3261                                                                 0,1,wppb2.relieved_assembly_units),
3262                                      0),
3263 			       decode(SIGN(SIGN(NVL(wppb2.tl_outside_processing_out,0))
3264 					   +SIGN(l_net_tl_osp_value)
3265 					   +2
3266 					   ),
3267                                         1,
3268                                         NVL(wppb2.tl_outside_processing_out,0)*
3269                                                 p_net_qty/decode(NVL(wppb2.relieved_assembly_units,0),
3270                                                                 0,1,wppb2.relieved_assembly_units),
3271                                     0),
3272                                NVL(wppb.tl_overhead_out,0) +
3273 	                       decode(SIGN(SIGN(NVL(wppb2.tl_overhead_out,0))
3274 			      	           +SIGN(l_net_tl_overhead_value)
3275 				           +2
3276 					   ),
3277                                         1,
3278                                         NVL(wppb2.tl_overhead_out,0) *
3279                                                 p_net_qty/decode(NVL(wppb2.relieved_assembly_units,0),
3280                                                                 0,1,wppb2.relieved_assembly_units),
3281                                     0),
3282 	                       decode(SIGN(SIGN(NVL(wppb2.tl_overhead_out,0))
3283 			      	           +SIGN(l_net_tl_overhead_value)
3284 				           +2
3285 					   ),
3286                                         1,
3287                                         NVL(wppb2.tl_overhead_out,0) *
3288                                                 p_net_qty/decode(NVL(wppb2.relieved_assembly_units,0),
3289                                                                 0,1,wppb2.relieved_assembly_units),
3290                                     0)
3291                                FROM  wip_pac_period_balances wppb2
3292                                WHERE wppb2.pac_period_id = p_prior_period_id
3293                                AND   wppb2.cost_group_id = p_cost_group_id
3294                                AND   wppb2.wip_entity_id = p_entity_id
3295                                AND   NVL(wppb2.line_id,-99) = NVL(p_line_id,-99)
3296                                AND   wppb2.operation_seq_num = wppb.operation_seq_num
3297                               )
3298                               WHERE  wppb.pac_period_id = p_pac_period_id
3299                               AND    wppb.cost_group_id = p_cost_group_id
3300                               AND    wppb.wip_entity_id = p_entity_id
3301                               AND    NVL(wppb.line_id,-99) = NVL(p_line_id,-99)
3302                               AND    wppb.operation_seq_num <=
3303                                         decode(p_scrap,1,p_op_seq,wppb.operation_seq_num);
3304                    ELSE /* p_net_qty=0 */
3305                        /* Final Completion, Flush out everything from the Job for TL columns */
3306                        l_stmt_num := 30;
3307 
3308 		       /* Bug6847717: Calculate the total value across op_seq */
3309 		       SELECT NVL(SUM(NVL(wppb.tl_resource_out,0)),0) net_tl_resource_value,
3310 		              NVL(SUM(NVL(wppb.tl_overhead_out,0)),0) net_tl_overhead_value,
3311 		              NVL(SUM(NVL(wppb.tl_outside_processing_out,0)),0) net_tl_osp_value
3312 		       INTO
3313 		       l_net_tl_resource_value,
3314 		       l_net_tl_overhead_value,
3315 		       l_net_tl_osp_value
3316 		       FROM wip_pac_period_balances wppb
3317 		       WHERE wppb.pac_period_id = p_prior_period_id
3318 		         AND wppb.cost_group_id = p_cost_group_id
3319 	                 AND wppb.wip_entity_id = p_entity_id
3320                          AND NVL(wppb.line_id,-99) = NVL(p_line_id,-99)
3321 	                 AND wppb.operation_seq_num <=
3322                              decode(p_scrap,1,p_op_seq,wppb.operation_seq_num);
3323 
3324                        IF p_final_completion_flag = 1 THEN
3325 
3326                             SELECT sum(primary_quantity)
3327                             INTO   l_assembly_return_cnt
3328                             FROM   mtl_material_transactions mmt
3329                             WHERE  mmt.transaction_source_id =  p_entity_id
3330                             AND    mmt.transaction_action_id = 32
3331                             AND    nvl(mmt.repetitive_line_id,-99) = nvl(p_line_id,-99)
3332                             AND    mmt.transaction_date BETWEEN TRUNC(p_start_date)
3333                                                              AND  (TRUNC(p_end_date) + 0.99999);
3334 
3335                             IF l_assembly_return_cnt <> 0 THEN
3336 		              SELECT Max(Nvl(relieved_assembly_units,0))
3337                                INTO l_prior_completed_units
3338 		               FROM wip_pac_period_balances wppb
3339 		              WHERE wppb.wip_entity_id = p_entity_id
3340 		                AND wppb.pac_period_id = p_pac_period_id
3341 		                AND wppb.cost_type_id = p_cost_type_id
3342 		                AND nvl(wppb.line_id, -99) = nvl(p_line_id,-99)
3343 		                AND wppb.cost_group_id = p_cost_group_id;
3344                             END IF;
3345 
3346 			      -- If this period is not the first period
3347                             IF p_prior_period_id <> -1 AND l_prior_completed_units <> 0 then
3348 
3349                                      l_stmt_num := 32;
3350                                      UPDATE wip_pac_period_balances wppb
3351                                       SET (tl_resource_out,
3352                                            tl_resource_temp,
3353                                            tl_outside_processing_out,
3354                                            tl_outside_processing_temp,
3355                                            tl_overhead_out,
3356                                            tl_overhead_temp
3357                                          ) =
3358                                       (SELECT
3359                                        NVL(wppb.tl_resource_out,0) +
3360                                        decode(SIGN(SIGN(NVL(wppb2.tl_resource_out,0))
3361                                                    +SIGN(l_net_tl_resource_value)
3362                                                    +2
3363                                                    ),
3364                                                   1,
3365                                                  NVL(wppb2.tl_resource_out,0) *
3366                                                       l_assembly_return_cnt/decode(NVL(wppb2.relieved_assembly_units,0),
3367                                                                         0,1,wppb2.relieved_assembly_units),
3368                                               0),
3369                                        decode(SIGN(SIGN(NVL(wppb2.tl_resource_out,0))
3370                                                    +SIGN(l_net_tl_resource_value)
3371                                                    +2
3372                                                    ),
3373                                                 1,  NVL(wppb2.tl_resource_out,0) *
3374                                                    l_assembly_return_cnt/decode(NVL(wppb2.relieved_assembly_units,0),
3375                                                                         0,1,wppb2.relieved_assembly_units),
3376                                                0),
3377                                        NVL(wppb.tl_outside_processing_out,0) +
3378                                        decode(SIGN(SIGN(NVL(wppb2.tl_outside_processing_out,0))
3379                                                    +SIGN(l_net_tl_osp_value)
3380                                                    +2
3381                                                    ),
3382                                                 1,
3383                                                  NVL(wppb2.tl_outside_processing_out,0)*
3384                                                  l_assembly_return_cnt/decode(NVL(wppb2.relieved_assembly_units,0),
3385                                                                         0,1,wppb2.relieved_assembly_units),
3386                                              0),
3387                                        decode(SIGN(SIGN(NVL(wppb2.tl_outside_processing_out,0))
3388                                                    +SIGN(l_net_tl_osp_value)
3389                                                    +2
3390                                                    ),
3391                                                 1,
3392                                                 NVL(wppb2.tl_outside_processing_out,0)*
3393                                                         l_assembly_return_cnt/decode(NVL(wppb2.relieved_assembly_units,0),
3394                                                                         0,1,wppb2.relieved_assembly_units),
3395                                             0),
3396                                        NVL(wppb.tl_overhead_out,0) +
3397                                        decode(SIGN(SIGN(NVL(wppb2.tl_overhead_out,0))
3398                                                    +SIGN(l_net_tl_overhead_value)
3399                                                    +2
3400                                                    ),
3401                                                 1,
3402                                                 NVL(wppb2.tl_overhead_out,0) *
3403                                                    l_assembly_return_cnt/decode(NVL(wppb2.relieved_assembly_units,0),
3404                                                                         0,1,wppb2.relieved_assembly_units),
3405                                             0),
3406                                        decode(SIGN(SIGN(NVL(wppb2.tl_overhead_out,0))
3407                                                    +SIGN(l_net_tl_overhead_value)
3408                                                    +2
3409                                                    ),
3410                                                 1,
3411                                                 NVL(wppb2.tl_overhead_out,0) *
3412                                                         l_assembly_return_cnt/decode(NVL(wppb2.relieved_assembly_units,0),
3413                                                                         0,1,wppb2.relieved_assembly_units),
3414                                             0)
3415                                        FROM  wip_pac_period_balances wppb2
3416                                        WHERE wppb2.pac_period_id = p_prior_period_id
3417                                        AND   wppb2.cost_group_id = p_cost_group_id
3418                                        AND   wppb2.wip_entity_id = p_entity_id
3419                                        AND   NVL(wppb2.line_id,-99) = NVL(p_line_id,-99)
3420                                        AND   wppb2.operation_seq_num = wppb.operation_seq_num
3421                                       )
3422                                       WHERE
3423                                       wppb.pac_period_id = p_pac_period_id
3424                                       AND wppb.cost_group_id = p_cost_group_id
3425                                       AND wppb.wip_entity_id = p_entity_id
3426                                       AND NVL(wppb.line_id,-99) = NVL(p_line_id,-99)
3427                                       AND wppb.operation_seq_num <=
3428                                                 decode(p_scrap,1,p_op_seq,wppb.operation_seq_num);
3429 
3430                            END IF; /*  p_prior_period_id <> -1 AND l_prior_completed_unit <> 0 */
3431                               l_stmt_num := 34;
3432                               UPDATE wip_pac_period_balances wppb
3433                               SET (tl_resource_out,
3434                                    tl_resource_temp,
3435                                    tl_outside_processing_out,
3436                                    tl_outside_processing_temp,
3437                                    tl_overhead_out,
3438                                    tl_overhead_temp
3439                                  ) =
3440                               (SELECT
3441                                NVL(wppb.tl_resource_out,0) +
3442                               decode(SIGN(SIGN(NVL(wppb2.tl_resource_in,0)
3443                                                -NVL(wppb2.tl_resource_out,0)
3444                                                -NVL(wppb2.tl_resource_var,0))
3445                                           +SIGN(l_net_tl_resource_value)
3446                                           +2
3447                                           ),
3448                                 1,
3449                                 Decode(l_prior_completed_units, 0, 0,
3450 				(NVL(wppb2.tl_resource_in,0)-NVL(wppb2.tl_resource_out,0)
3451                                                 - NVL(wppb2.tl_resource_var,0))),
3452                                0
3453                               ),
3454                               decode(SIGN(SIGN(NVL(wppb2.tl_resource_in,0)
3455                                                -NVL(wppb2.tl_resource_out,0)
3456                                                -NVL(wppb2.tl_resource_var,0))
3457                                           +SIGN(l_net_tl_resource_value)
3458                                           +2
3459                                           ),
3460                                 1,
3461                                 (NVL(wppb2.tl_resource_in,0)-NVL(wppb2.tl_resource_out,0)
3462                                                 - NVL(wppb2.tl_resource_var,0))
3463                                 ,0
3464                               ),
3465                                NVL(wppb.tl_outside_processing_out,0) +
3466                               decode(SIGN(SIGN(NVL(wppb2.tl_outside_processing_in,0)
3467                                                 - NVL(wppb2.tl_outside_processing_out,0)
3468                                                 - NVL(wppb2.tl_outside_processing_var,0))
3469                                           +SIGN(l_net_tl_osp_value)
3470                                           +2
3471                                           ),
3472                                 1,
3473                                 Decode(l_prior_completed_units, 0, 0,
3474                                 (NVL(wppb2.tl_outside_processing_in,0)
3475                                                 - NVL(wppb2.tl_outside_processing_out,0)
3476                                                 - NVL(wppb2.tl_outside_processing_var,0)))
3477                                 ,0
3478                               ),
3479                               decode(SIGN(SIGN(NVL(wppb2.tl_outside_processing_in,0)
3480                                                 - NVL(wppb2.tl_outside_processing_out,0)
3481                                                 - NVL(wppb2.tl_outside_processing_var,0))
3482                                           +SIGN(l_net_tl_osp_value)
3483                                           +2
3484                                           ),
3485                                 1,
3486                                 (NVL(wppb2.tl_outside_processing_in,0)
3487                                                 - NVL(wppb2.tl_outside_processing_out,0)
3488                                                 - NVL(wppb2.tl_outside_processing_var,0)),
3489                                 0
3490                               ),
3491                                NVL(tl_overhead_out,0) +
3492                               decode(SIGN(SIGN(NVL(wppb2.tl_overhead_in,0)
3493                                                -NVL(wppb2.tl_overhead_out,0)
3494                                                -NVL(wppb2.tl_overhead_var,0))
3495                                           +SIGN(l_net_tl_overhead_value)
3496                                           +2
3497                                           ),
3498                                 1,
3499                                 Decode(l_prior_completed_units, 0, 0,
3500                                 (NVL(wppb2.tl_overhead_in,0)-NVL(wppb2.tl_overhead_out,0)
3501                                                 - NVL(wppb2.tl_overhead_var,0))),
3502                                 0
3503                               ),
3504                               decode(SIGN(SIGN(NVL(wppb2.tl_overhead_in,0)
3505                                                -NVL(wppb2.tl_overhead_out,0)
3506                                                -NVL(wppb2.tl_overhead_var,0))
3507                                           +SIGN(l_net_tl_overhead_value)
3508                                           +2
3509                                           ),
3510                                 1,
3511                                 (NVL(wppb2.tl_overhead_in,0)-NVL(wppb2.tl_overhead_out,0)
3512                                                 - NVL(wppb2.tl_overhead_var,0)),
3513                                 0
3514                               )
3515                                FROM wip_pac_period_balances wppb2
3516                                WHERE wppb2.pac_period_id = p_pac_period_id
3517                                AND   wppb2.cost_group_id = p_cost_group_id
3518                                AND   wppb2.wip_entity_id = p_entity_id
3519                                AND   NVL(wppb2.line_id,-99) = NVL(p_line_id,-99)
3520                                AND   wppb2.operation_seq_num = wppb.operation_seq_num
3521                               )
3522                               WHERE
3523                               wppb.pac_period_id = p_pac_period_id
3524                               AND wppb.cost_group_id = p_cost_group_id
3525                               AND wppb.wip_entity_id = p_entity_id
3526                               AND NVL(wppb.line_id,-99) = NVL(p_line_id,-99)
3527                               AND wppb.operation_seq_num <=
3528                                         decode(p_scrap,1,p_op_seq,wppb.operation_seq_num);
3529 
3530                        ELSE  /* No Final Completion */
3531                               l_stmt_num := 36;
3532                               UPDATE wip_pac_period_balances wppb
3533                               SET    (
3534                                        tl_resource_temp,
3535                                        tl_outside_processing_temp,
3536                                        tl_overhead_temp
3537                                      )
3538                                     =
3539                                      ( SELECT DECODE(
3540 						SIGN(SIGN(NVL(tl_resource_out,0))
3541 						     +SIGN(l_net_tl_resource_value)
3542 						     +2
3543 						     ),
3544                                                 1,
3545                                                 NVL(tl_resource_out,0) /
3546                                                 DECODE(NVL(relieved_assembly_units,0),0,1,relieved_assembly_units),
3547                                                 0
3548                                               ),
3549                                               DECODE(
3550                                                 SIGN(SIGN(NVL(tl_outside_processing_out,0))
3551                                                      +SIGN(l_net_tl_osp_value)
3552                                                      +2
3553                                                      ),
3554                                                 1,
3555                                                 NVL(tl_outside_processing_out,0) /
3556                                                 DECODE(NVL(relieved_assembly_units,0),0,1,relieved_assembly_units),
3557                                                 0
3558                                               ),
3559                                               DECODE(
3560                                                 SIGN(SIGN(NVL(tl_overhead_out,0))
3561                                                      +SIGN(l_net_tl_overhead_value)
3562                                                      +2
3563                                                      ),
3564                                                 1,
3565                                                 NVL(tl_overhead_out,0) /
3566                                                 DECODE(NVL(relieved_assembly_units,0),0,1,relieved_assembly_units),
3567                                                 0
3568                                               )
3569                                        FROM   wip_pac_period_balances
3570                                        WHERE  pac_period_id = p_prior_period_id
3571                                        AND    cost_group_id = p_cost_group_id
3572                                        AND    wip_entity_id = p_entity_id
3573                                        AND    NVL(line_id,-99) = NVL(p_line_id,-99)
3574                                        AND    operation_seq_num = wppb.operation_seq_num
3575                                      )
3576                               WHERE  pac_period_id = p_pac_period_id
3577                               AND    cost_group_id = p_cost_group_id
3578                               AND    wip_entity_id = p_entity_id
3579                               AND    NVL(line_id,-99) = NVL(p_line_id,-99)
3580                               AND    operation_seq_num <= decode(p_scrap,1,p_op_seq,wppb.operation_seq_num);
3581                     END IF; /* Final Completion Check */
3582                   END IF; /* End of P_net_qty */
3583            ELSE -- Actuals Logic
3584             l_stmt_num := 38;
3585             IF (p_net_qty > 0) THEN  -- completion are more than returns
3586 
3587 	       /* Bug6847717: Calculate the total value across op_seq */
3588 	       SELECT NVL(SUM(NVL(wppb.tl_resource_in,0)-
3589                           NVL(wppb.tl_resource_out,0)
3590                           - NVL(wppb.tl_resource_var,0)),0) net_tl_resource_value,
3591 	              NVL(SUM(NVL(wppb.tl_overhead_in,0)-
3592                           NVL(wppb.tl_overhead_out,0)
3593                           - NVL(wppb.tl_overhead_var,0)),0) net_tl_overhead_value,
3594                       NVL(SUM(NVL(wppb.tl_outside_processing_in,0)-
3595                           NVL(wppb.tl_outside_processing_out,0)
3596                           - NVL(wppb.tl_outside_processing_var,0)),0) net_tl_osp_value,
3597                       NVL(SUM(NVL(wppb.pl_material_in,0)-
3598                           NVL(wppb.pl_material_out,0)
3599                           - NVL(wppb.pl_material_var,0)),0) net_pl_material_value,
3600                       NVL(SUM(NVL(wppb.pl_material_overhead_in,0)-
3601                           NVL(wppb.pl_material_overhead_out,0)
3602                           - NVL(wppb.pl_material_overhead_var,0)),0) net_pl_moh_value,
3603                       NVL(SUM(NVL(wppb.pl_resource_in,0)-
3604                           NVL(wppb.pl_resource_out,0)
3605                           - NVL(wppb.pl_resource_var,0)),0) net_pl_resource_value,
3606                       NVL(SUM(NVL(wppb.pl_outside_processing_in,0)-
3607                           NVL(wppb.pl_outside_processing_out,0)
3608                           - NVL(wppb.pl_outside_processing_var,0)),0) net_pl_osp_value,
3609                       NVL(SUM(NVL(wppb.pl_overhead_in,0)-
3610                           NVL(wppb.pl_overhead_out,0)
3611                           - NVL(wppb.pl_overhead_var,0)),0) net_pl_overhead_value
3612                  INTO
3613                  l_net_tl_resource_value,
3614                  l_net_tl_overhead_value,
3615                  l_net_tl_osp_value,
3616                  l_net_pl_material_value,
3617                  l_net_pl_moh_value,
3618                  l_net_pl_resource_value,
3619                  l_net_pl_osp_value,
3620                  l_net_pl_overhead_value
3621                FROM wip_pac_period_balances wppb
3622                 WHERE wppb.pac_period_id = p_pac_period_id
3623                   AND wppb.cost_group_id = p_cost_group_id
3624                   AND wppb.wip_entity_id = p_entity_id
3625                   AND NVL(wppb.line_id,-99) = NVL(p_line_id,-99)
3626                   AND wppb.operation_seq_num <=
3627                       decode(p_scrap,1,p_op_seq,wppb.operation_seq_num);
3628 
3629               IF p_final_completion_flag = 1 THEN     /* Final completion exists */
3630             l_stmt_num := 40;
3631                       UPDATE wip_pac_period_balances wppb
3632                       SET (tl_resource_out,
3633                            tl_resource_temp,
3634                            tl_outside_processing_out,
3635                            tl_outside_processing_temp,
3636                            tl_overhead_out,
3637                            tl_overhead_temp,
3638                            pl_material_out,
3639                            pl_material_temp,
3640                            pl_material_overhead_out,
3641                            pl_material_overhead_temp,
3642                            pl_resource_out,
3643                            pl_resource_temp,
3644                            pl_outside_processing_out,
3645                            pl_outside_processing_temp,
3646                            pl_overhead_out,
3647                            pl_overhead_temp
3648                         ) =
3649                       (SELECT
3650                        NVL(wppb.tl_resource_out,0) +
3651                        decode(SIGN(SIGN(NVL(wppb.tl_resource_in,0)
3652                                         -NVL(wppb.tl_resource_out,0)
3653                                         -NVL(tl_resource_var,0))
3654                                    +SIGN(l_net_tl_resource_value)
3655                                    +2
3656                                    ),
3657                                  1,
3658                                  (NVL(wppb.tl_resource_in,0)-NVL(wppb.tl_resource_out,0)
3659                                         - NVL(tl_resource_var,0)),
3660                                   0
3661                              ),
3662                        decode(SIGN(SIGN(NVL(wppb.tl_resource_in,0)
3663                                         -NVL(wppb.tl_resource_out,0)
3664                                         -NVL(tl_resource_var,0))
3665                                    +SIGN(l_net_tl_resource_value)
3666                                    +2
3667                                    ),
3668                                    1,
3669                                    (NVL(wppb.tl_resource_in,0)-NVL(wppb.tl_resource_out,0)
3670                                         - NVL(tl_resource_var,0)),
3671                                  0
3672                              ),
3673                        NVL(tl_outside_processing_out,0) +
3674                        decode(SIGN(SIGN(NVL(wppb.tl_outside_processing_in,0)
3675                                         - NVL(wppb.tl_outside_processing_out,0)
3676                                         - NVL(wppb.tl_outside_processing_var,0))
3677                                    +SIGN(l_net_tl_osp_value)
3678                                    +2
3679                                    ),
3680                                 1,
3681                                 (NVL(wppb.tl_outside_processing_in,0)
3682                                         - NVL(wppb.tl_outside_processing_out,0)
3683                                         - NVL(wppb.tl_outside_processing_var,0)),
3684                                 0
3685                              ),
3686                        decode(SIGN(SIGN(NVL(wppb.tl_outside_processing_in,0)
3687                                         - NVL(wppb.tl_outside_processing_out,0)
3688                                         - NVL(wppb.tl_outside_processing_var,0))
3689                                    +SIGN(l_net_tl_osp_value)
3690                                    +2
3691                                    ),
3692                                 1,
3693                                 (NVL(wppb.tl_outside_processing_in,0)
3694                                         - NVL(wppb.tl_outside_processing_out,0)
3695                                         - NVL(wppb.tl_outside_processing_var,0)),
3696                                 0
3697                              ),
3698                        NVL(tl_overhead_out,0) +
3699                        decode(SIGN(SIGN(NVL(wppb.tl_overhead_in,0)
3700                                         -NVL(wppb.tl_overhead_out,0)
3701                                         -NVL(wppb.tl_overhead_var,0))
3702                                    +SIGN(l_net_tl_overhead_value)
3703                                    +2
3704                                    ),
3705                                   1,
3706                                    (NVL(wppb.tl_overhead_in,0)-NVL(wppb.tl_overhead_out,0)
3707                                       - NVL(wppb.tl_overhead_var,0)),
3708                                   0
3709                              ),
3710                        decode(SIGN(SIGN(NVL(wppb.tl_overhead_in,0)
3711                                         -NVL(wppb.tl_overhead_out,0)
3712                                         -NVL(wppb.tl_overhead_var,0))
3713                                    +SIGN(l_net_tl_overhead_value)
3714                                    +2
3715                                    ),
3716                                1,
3717                                 (NVL(wppb.tl_overhead_in,0)-NVL(wppb.tl_overhead_out,0)
3718                                             - NVL(wppb.tl_overhead_var,0)),
3719                                0
3720                              ),
3721                        NVL(pl_material_out,0) +
3722                        decode(SIGN(SIGN(NVL(wppb.pl_material_in,0)
3723                                         -NVL(wppb.pl_material_out,0)
3724                                         -NVL(wppb.pl_material_var,0))
3725                                    +SIGN(l_net_pl_material_value)
3726                                    +2
3727                                    ),
3728                                1,
3729                                 (NVL(wppb.pl_material_in,0) - NVL(wppb.pl_material_out,0) - nvl(wppb.pl_material_in_apull,0)
3730                                         - NVL(wppb.pl_material_var,0)) + nvl(wppb.pl_material_in_apull,0),
3731                                0
3732                               ),
3733                        decode(SIGN(SIGN(NVL(wppb.pl_material_in,0)
3734                                         -NVL(wppb.pl_material_out,0)
3735                                         -NVL(wppb.pl_material_var,0))
3736                                    +SIGN(l_net_pl_material_value)
3737                                    +2
3738                                    ),
3739                                1,
3740                                 (NVL(wppb.pl_material_in,0)-NVL(wppb.pl_material_out,0)-nvl(wppb.pl_material_in_apull,0)
3741                                         - NVL(wppb.pl_material_var,0)) + nvl(wppb.pl_material_in_apull,0),
3742                                0
3743                             ),
3744                        NVL(pl_material_overhead_out,0) +
3745                        decode(SIGN(SIGN(NVL(wppb.pl_material_overhead_in,0)
3746                                         - NVL(wppb.pl_material_overhead_out,0)
3747                                         - NVL(wppb.pl_material_overhead_var,0))
3748                                    +SIGN(l_net_pl_moh_value)
3749                                    +2
3750                                    ),
3751                               1,
3752                                (NVL(wppb.pl_material_overhead_in,0)
3753                                         - NVL(wppb.pl_material_overhead_out,0) - nvl(wppb.pl_material_overhead_in_apull,0)
3754                                         - NVL(wppb.pl_material_overhead_var,0)) + nvl(wppb.pl_material_overhead_in_apull,0),
3755                               0
3756                              ),
3757                        decode(SIGN(SIGN(NVL(wppb.pl_material_overhead_in,0)
3758                                         - NVL(wppb.pl_material_overhead_out,0)
3759                                         - NVL(wppb.pl_material_overhead_var,0))
3760                                    +SIGN(l_net_pl_moh_value)
3761                                    +2
3762                                    ),
3763                                 1,
3764                                  (NVL(wppb.pl_material_overhead_in,0)
3765                                         - NVL(wppb.pl_material_overhead_out,0) - nvl(wppb.pl_material_overhead_in_apull,0)
3766                                         - NVL(wppb.pl_material_overhead_var,0)) + nvl(wppb.pl_material_overhead_in_apull,0),
3767                                 0
3768                              ),
3769                        NVL(pl_resource_out,0) +
3770                        decode(SIGN(SIGN(NVL(wppb.pl_resource_in,0)
3771 					-NVL(wppb.pl_resource_out,0)
3772                                         -NVL(wppb.pl_resource_var,0))
3773                                    +SIGN(l_net_pl_resource_value)
3774                                    +2
3775                                    ),
3776                                 1,
3777                                 (NVL(wppb.pl_resource_in,0)-NVL(wppb.pl_resource_out,0) - nvl(wppb.pl_resource_in_apull,0)
3778                                         - NVL(wppb.pl_resource_var,0)) + nvl(wppb.pl_resource_in_apull,0),
3779                                 0
3780                              ),
3781                        decode(SIGN(SIGN(NVL(wppb.pl_resource_in,0)
3782 					-NVL(wppb.pl_resource_out,0)
3783                                         -NVL(wppb.pl_resource_var,0))
3784                                    +SIGN(l_net_pl_resource_value)
3785                                    +2
3786                                    ),
3787                                 1,
3788                                  (NVL(wppb.pl_resource_in,0)-NVL(wppb.pl_resource_out,0) - nvl(wppb.pl_resource_in_apull,0)
3789                                         - NVL(wppb.pl_resource_var,0)) + nvl(wppb.pl_resource_in_apull,0),
3790                                 0
3791                              ),
3792                        NVL(pl_outside_processing_out,0) +
3793                        decode(SIGN(SIGN(NVL(wppb.pl_outside_processing_in,0)
3794                                         - NVL(wppb.pl_outside_processing_out,0)
3795                                         - NVL(wppb.pl_outside_processing_var,0))
3796                                    +SIGN(l_net_pl_osp_value)
3797                                    +2
3798                                    ),
3799                                 1,
3800                                  (NVL(wppb.pl_outside_processing_in,0)
3801                                         - NVL(wppb.pl_outside_processing_out,0)-nvl(wppb.pl_outside_processing_in_apull,0)
3802                                         - NVL(wppb.pl_outside_processing_var,0)) + nvl(wppb.pl_outside_processing_in_apull,0),
3803                                 0
3804                              ),
3805                        decode(SIGN(SIGN(NVL(wppb.pl_outside_processing_in,0)
3806                                         - NVL(wppb.pl_outside_processing_out,0)
3807                                         - NVL(wppb.pl_outside_processing_var,0))
3808                                    +SIGN(l_net_pl_osp_value)
3809                                    +2
3810                                    ),
3811                                 1,
3812                                  (NVL(wppb.pl_outside_processing_in,0)
3813                                         - NVL(wppb.pl_outside_processing_out,0)-nvl(wppb.pl_outside_processing_in_apull,0)
3814                                         - NVL(wppb.pl_outside_processing_var,0)) + nvl(wppb.pl_outside_processing_in_apull,0),
3815                                 0
3816                              ),
3817                        NVL(pl_overhead_out,0) +
3818                        decode(SIGN(SIGN(NVL(wppb.pl_overhead_in,0)
3819 					-NVL(wppb.pl_overhead_out,0)
3820                                         -NVL(wppb.pl_overhead_var,0))
3821                                    +SIGN(l_net_pl_overhead_value)
3822                                    +2
3823 				   ),
3824                                 1,
3825                                  (NVL(wppb.pl_overhead_in,0)-NVL(wppb.pl_overhead_out,0)-nvl(wppb.pl_overhead_in_apull,0)
3826                                         - NVL(wppb.pl_overhead_var,0)) + nvl(wppb.pl_overhead_in_apull,0),
3827                                 0
3828                              ),
3829                        decode(SIGN(SIGN(NVL(wppb.pl_overhead_in,0)
3830 					-NVL(wppb.pl_overhead_out,0)
3831                                         -NVL(wppb.pl_overhead_var,0))
3832                                    +SIGN(l_net_pl_overhead_value)
3833                                    +2
3834 				   ),
3835                                 1,
3836                                  (NVL(wppb.pl_overhead_in,0)-NVL(wppb.pl_overhead_out,0)-nvl(wppb.pl_overhead_in_apull,0)
3837                                         - NVL(wppb.pl_overhead_var,0)) + nvl(wppb.pl_overhead_in_apull,0),
3838                                 0
3839                              )
3840                        FROM  wip_pac_period_balances wppb2
3841                        WHERE wppb2.pac_period_id = p_pac_period_id
3842                        AND   wppb2.cost_group_id = p_cost_group_id
3843                        AND   wppb2.wip_entity_id = p_entity_id
3844                        AND   NVL(wppb2.line_id,-99) = NVL(p_line_id,-99)
3845                        AND   wppb2.operation_seq_num = wppb.operation_seq_num
3846                       )
3847                       WHERE wppb.pac_period_id = p_pac_period_id
3848                       AND   wppb.cost_group_id = p_cost_group_id
3849                       AND   wppb.wip_entity_id = p_entity_id
3850                       AND   NVL(wppb.line_id,-99) = NVL(p_line_id,-99)
3851                       AND   wppb.operation_seq_num <= wppb.operation_seq_num;
3852               ELSE     /* No Final completion exists */
3853                       l_stmt_num := 42;
3854                       UPDATE wip_pac_period_balances wppb
3855                       SET (tl_resource_out,
3856                         tl_resource_temp,
3857                         tl_outside_processing_out,
3858                         tl_outside_processing_temp,
3859                         tl_overhead_out,
3860                         tl_overhead_temp,
3861                         pl_material_out,
3862                         pl_material_temp,
3863 			scrap_pull_material,  -- Added by Bug#4717026
3864                         pl_material_overhead_out,
3865                         pl_material_overhead_temp,
3866                         scrap_pull_material_overhead,  -- Added by Bug#4717026
3867                         pl_resource_out,
3868                         pl_resource_temp,
3869                 	scrap_pull_resource,  -- Added by Bug#4717026
3870                         pl_outside_processing_out,
3871                         pl_outside_processing_temp,
3872               		scrap_pull_outside_processing,  -- Added by Bug#4717026
3873                         pl_overhead_out,
3874                         pl_overhead_temp,
3875 			scrap_pull_overhead  -- Added by Bug#4717026
3876                         ) =
3877                       (SELECT
3878                        NVL(wppb.tl_resource_out,0) +
3879                       decode(SIGN(SIGN(NVL(wppb2.tl_resource_in,0)
3880 				       -NVL(wppb2.tl_resource_out,0)
3881                                        -NVL(wppb2.tl_resource_var,0))
3882 				  +SIGN(l_net_tl_resource_value)
3883                                   +2
3884                                   ),
3885                         1,
3886                         (NVL(wppb2.tl_resource_in,0)-NVL(wppb2.tl_resource_out,0)
3887                                         - NVL(wppb2.tl_resource_var,0))*
3888                         decode(SIGN(NVL(wppb2.operation_completed_units,0) -
3889                                 NVL(wppb2.relieved_assembly_units,0) - nvl(wppb2.unrelieved_scrap_quantity,0)),
3890                               0,  1,
3891                              -1, 1,
3892                               p_net_qty/ (NVL(wppb2.operation_completed_units,0) -
3893                                     NVL(wppb2.relieved_assembly_units,0) - nvl(wppb2.unrelieved_scrap_quantity,0))
3894                         ),
3895                         0
3896                       ),
3897                       decode(SIGN(SIGN(NVL(wppb2.tl_resource_in,0)
3898 				       -NVL(wppb2.tl_resource_out,0)
3899                                        -NVL(wppb2.tl_resource_var,0))
3900 				  +SIGN(l_net_tl_resource_value)
3901                                   +2
3902                                   ),
3903                         1,
3904                         (NVL(wppb2.tl_resource_in,0)-NVL(wppb2.tl_resource_out,0)
3905                                         - NVL(wppb2.tl_resource_var,0))*
3906                         decode(SIGN(NVL(wppb2.operation_completed_units,0) -
3907                                 NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0)),
3908                               0,  1,
3909                              -1, 1,
3910                               p_net_qty/ (NVL(wppb2.operation_completed_units,0) -
3911                                     NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0))
3912                         ),
3913                         0
3914                       ),
3915                        NVL(tl_outside_processing_out,0) +
3916                       decode(SIGN(SIGN(NVL(wppb2.tl_outside_processing_in,0)
3917                                         - NVL(wppb2.tl_outside_processing_out,0)
3918                                         - NVL(wppb2.tl_outside_processing_var,0))
3919 				  +SIGN(l_net_tl_osp_value)
3920        				  +2
3921 				  ),
3922                         1,
3923                         (NVL(wppb2.tl_outside_processing_in,0)
3924                                         - NVL(wppb2.tl_outside_processing_out,0)
3925                                         - NVL(wppb2.tl_outside_processing_var,0))*
3926                         decode(SIGN(NVL(wppb2.operation_completed_units,0) -
3927                                 NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0)),
3928                              0,  1,
3929                             -1, 1,
3930                              p_net_qty/ (NVL(wppb2.operation_completed_units,0) -
3931                                     NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0))
3932                         ),
3933                         0
3934                       ),
3935                       decode(SIGN(SIGN(NVL(wppb2.tl_outside_processing_in,0)
3936                                         - NVL(wppb2.tl_outside_processing_out,0)
3937                                         - NVL(wppb2.tl_outside_processing_var,0))
3938 				  +SIGN(l_net_tl_osp_value)
3939        				  +2
3940 				  ),
3941                         1,
3942                         (NVL(wppb2.tl_outside_processing_in,0)
3943                                         - NVL(wppb2.tl_outside_processing_out,0)
3944                                         - NVL(wppb2.tl_outside_processing_var,0))*
3945                         decode(SIGN(NVL(wppb2.operation_completed_units,0) -
3946                                 NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0)),
3947                            0,  1,
3948                           -1, 1,
3949                            p_net_qty/ (NVL(wppb2.operation_completed_units,0) -
3950                                     NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0))
3951                         ),
3952                         0
3953                       ),
3954                        NVL(tl_overhead_out,0) +
3955                       decode(SIGN(SIGN(NVL(wppb2.tl_overhead_in,0)
3956 				       -NVL(wppb2.tl_overhead_out,0)
3957                                        -NVL(wppb2.tl_overhead_var,0))
3958 				  +SIGN(l_net_tl_overhead_value)
3959 				  +2
3960 				  ),
3961                         1,
3962                         (NVL(wppb2.tl_overhead_in,0)-NVL(wppb2.tl_overhead_out,0)
3963                                         - NVL(wppb2.tl_overhead_var,0))*
3964                         decode(SIGN(NVL(wppb2.operation_completed_units,0) -
3965                                 NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0)),
3966                            0,  1,
3967                           -1, 1,
3968                            p_net_qty/ (NVL(wppb2.operation_completed_units,0) -
3969                                     NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0))
3970                         ),
3971                         0
3972                       ),
3973                       decode(SIGN(SIGN(NVL(wppb2.tl_overhead_in,0)
3974 				       -NVL(wppb2.tl_overhead_out,0)
3975                                        -NVL(wppb2.tl_overhead_var,0))
3976 				  +SIGN(l_net_tl_overhead_value)
3977 				  +2
3978 				  ),
3979                         1,
3980                         (NVL(wppb2.tl_overhead_in,0)-NVL(wppb2.tl_overhead_out,0)
3981                                         - NVL(wppb2.tl_overhead_var,0))*
3982                         decode(SIGN(NVL(wppb2.operation_completed_units,0) -
3983                                 NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0)),
3984                              0,  1,
3985                             -1, 1,
3986                              p_net_qty/ (NVL(wppb2.operation_completed_units,0) -
3987                                     NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0))
3988                         ),
3989                         0
3990                       ),
3991                        NVL(pl_material_out,0) +
3992                       decode(SIGN(SIGN(NVL(wppb2.pl_material_in,0)
3993 				       -NVL(wppb2.pl_material_out,0)
3994                                        -NVL(wppb2.pl_material_var,0))
3995 				  +SIGN(l_net_pl_material_value)
3996 				  +2
3997 				  ),
3998                         1,
3999                         (NVL(wppb2.pl_material_in,0)-NVL(wppb2.pl_material_out,0)- nvl(wppb2.pl_material_in_apull,0)
4000                                         - NVL(wppb2.pl_material_var,0)+ nvl(wppb.scrap_pull_material,0))*
4001                         decode(SIGN(NVL(wppb2.operation_completed_units,0) -
4002                                 NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0)),
4003                            0,  1,
4004                           -1, 1,
4005                            p_net_qty/ (NVL(wppb2.operation_completed_units,0) -
4006                                     NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0))
4007                         ) +   decode(p_scrap,1,
4008 						decode(SIGN(nvl(wppb.relieved_scrap_qty,0) + nvl(l_net_completion,0)),
4009 						0,1,
4010 						-1,1,
4011 						p_net_qty /(nvl(wppb.relieved_scrap_qty,0) + nvl(l_net_completion,0))) * nvl(wppb.pl_material_in_apull,0),    --bug 14537919
4012 		            nvl(wppb.pl_material_in_apull,0) - nvl(wppb.scrap_pull_material,0)),
4013                         0
4014                       ),
4015                       decode(SIGN(SIGN(NVL(wppb2.pl_material_in,0)
4016 				       -NVL(wppb2.pl_material_out,0)
4017                                        -NVL(wppb2.pl_material_var,0))
4018 				  +SIGN(l_net_pl_material_value)
4019 				  +2
4020 				  ),
4021                         1,
4022                         (NVL(wppb2.pl_material_in,0)- NVL(wppb2.pl_material_out,0)- nvl(wppb2.pl_material_in_apull,0)
4023                                         - NVL(wppb2.pl_material_var,0)+ nvl(wppb2.scrap_pull_material,0))*
4024                         decode(SIGN(NVL(wppb2.operation_completed_units,0) -
4025                                 NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0)),
4026                            0,  1,
4027                           -1, 1,
4028                            p_net_qty/ (NVL(wppb2.operation_completed_units,0) -
4029                                     NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0))
4030                         ) +  decode(p_scrap,1,
4031 						decode(SIGN(nvl(wppb2.relieved_scrap_qty,0) + nvl(l_net_completion,0)),
4032 						0,1,
4033 						-1,1,
4034 						p_net_qty/(nvl(wppb2.relieved_scrap_qty,0) + nvl(l_net_completion,0))) * nvl(wppb2.pl_material_in_apull,0),
4035 
4036 		            nvl(wppb2.pl_material_in_apull,0) - nvl(wppb2.scrap_pull_material,0)),
4037                         0
4038                       ),
4039                      NVL(wppb.scrap_pull_material,0) +
4040           	      decode(p_scrap,1,
4041 				  decode(SIGN(nvl(wppb2.relieved_scrap_qty,0) + nvl(l_net_completion,0)),
4042 				  0,1,
4043 				  -1,1,
4044 				  p_net_qty/(nvl(wppb2.relieved_scrap_qty,0) + nvl(l_net_completion,0))) * nvl(wppb2.pl_material_in_apull,0),
4045 					   0),
4046                        NVL(pl_material_overhead_out,0) +
4047                       decode(SIGN(SIGN(NVL(wppb2.pl_material_overhead_in,0)
4048                                         - NVL(wppb2.pl_material_overhead_out,0)
4049                                         - NVL(wppb2.pl_material_overhead_var,0))
4050 				  +SIGN(l_net_pl_moh_value)
4051 				  +2
4052 				  ),
4053                         1,
4054                         (NVL(wppb2.pl_material_overhead_in,0)
4055                                         - NVL(wppb2.pl_material_overhead_out,0)- nvl(wppb2.pl_material_overhead_in_apull,0)
4056                                         - NVL(wppb2.pl_material_overhead_var,0)+  nvl(wppb2.scrap_pull_material_overhead,0))*
4057                         decode(SIGN(NVL(wppb2.operation_completed_units,0) -
4058                                 NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0)),
4059                              0,  1,
4060                             -1, 1,
4061                              p_net_qty/ (NVL(wppb2.operation_completed_units,0) -
4062                                     NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0))
4063                         ) + decode(p_scrap,1,
4064 						decode(SIGN(nvl(wppb.relieved_scrap_qty,0) + nvl(l_net_completion,0)),
4065 						0,1,
4066 						-1,1,
4067 						p_net_qty/(nvl(wppb.relieved_scrap_qty,0) + nvl(l_net_completion,0))) * nvl(wppb.pl_material_overhead_in_apull,0),
4068 		            nvl(wppb.pl_material_overhead_in_apull,0) - nvl(wppb2.scrap_pull_material_overhead,0)),
4069                         0
4070                       ),
4071                       decode(SIGN(SIGN(NVL(wppb2.pl_material_overhead_in,0)
4072                                         - NVL(wppb2.pl_material_overhead_out,0)
4073                                         - NVL(wppb2.pl_material_overhead_var,0))
4074 				  +SIGN(l_net_pl_moh_value)
4075 				  +2
4076 				  ),
4077                         1,
4078                         (NVL(wppb2.pl_material_overhead_in,0)
4079                                         - NVL(wppb2.pl_material_overhead_out,0)-nvl(wppb2.pl_material_overhead_in_apull,0)
4080                                         - NVL(wppb2.pl_material_overhead_var,0)+  nvl(wppb2.scrap_pull_material_overhead,0))*
4081                         decode(SIGN(NVL(wppb2.operation_completed_units,0) -
4082                                 NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0)),
4083                              0,  1,
4084                             -1, 1,
4085                              p_net_qty/ (NVL(wppb2.operation_completed_units,0) -
4086                                     NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0))
4087                         ) + decode(p_scrap,1,
4088 						decode(SIGN(nvl(wppb2.relieved_scrap_qty,0) + nvl(l_net_completion,0)),
4089 						0,1,
4090 						-1,1,
4091 						p_net_qty/(nvl(wppb2.relieved_scrap_qty,0) + nvl(l_net_completion,0))) * nvl(wppb2.pl_material_overhead_in_apull,0),
4092 		            nvl(wppb2.pl_material_overhead_in_apull,0) - nvl(wppb2.scrap_pull_material_overhead,0)),
4093                         0
4094                       ),
4095                   NVL(wppb.scrap_pull_material_overhead,0) +
4096   	          decode(p_scrap,1,
4097 			  decode(SIGN(nvl(wppb2.relieved_scrap_qty,0) + nvl(l_net_completion,0)),
4098 			  0,1,
4099 			  -1,1,
4100 			  p_net_qty/(nvl(wppb2.relieved_scrap_qty,0) + nvl(l_net_completion,0))) * nvl(wppb2.pl_material_overhead_in_apull,0),
4101 			   0),
4102                        NVL(pl_resource_out,0) +
4103                       decode(SIGN(SIGN(NVL(wppb2.pl_resource_in,0)
4104 					-NVL(wppb2.pl_resource_out,0)
4105                                         - NVL(wppb2.pl_resource_var,0))
4106 				  +SIGN(l_net_pl_resource_value)
4107 				  +2
4108 				  ),
4109                         1,
4110                         (NVL(wppb2.pl_resource_in,0)- NVL(wppb2.pl_resource_out,0)- nvl(wppb2.pl_resource_in_apull,0)
4111                                         - NVL(wppb2.pl_resource_var,0) + NVL(wppb2.scrap_pull_resource,0) )*
4112                         decode(SIGN(NVL(wppb2.operation_completed_units,0) -
4113                                 NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0)),
4114                            0,  1,
4115                           -1, 1,
4116                            p_net_qty/ (NVL(wppb2.operation_completed_units,0) -
4117                                     NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0))
4118                         ) + decode(p_scrap,1,
4119 						decode(SIGN(nvl(wppb2.relieved_scrap_qty,0) + nvl(l_net_completion,0)),
4120 						0,1,
4121 						-1,1,
4122 						p_net_qty/(nvl(wppb2.relieved_scrap_qty,0) + nvl(l_net_completion,0))) * nvl(wppb2.pl_resource_in_apull,0),
4123 		             nvl(wppb2.pl_resource_in_apull,0) - NVL(wppb2.scrap_pull_resource,0)),
4124                         0
4125                       ),
4126                       decode(SIGN(SIGN(NVL(wppb2.pl_resource_in,0)
4127 					-NVL(wppb2.pl_resource_out,0)
4128                                         - NVL(wppb2.pl_resource_var,0))
4129 				  +SIGN(l_net_pl_resource_value)
4130 				  +2
4131 				  ),
4132                         1,
4133                         (NVL(wppb2.pl_resource_in,0)- NVL(wppb2.pl_resource_out,0)- nvl(wppb2.pl_resource_in_apull,0)
4134                                         - NVL(wppb2.pl_resource_var,0) + NVL(wppb2.scrap_pull_resource,0))*
4135                         decode(SIGN(NVL(wppb2.operation_completed_units,0) -
4136                                 NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0)),
4137                            0,  1,
4138                           -1, 1,
4139                            p_net_qty/ (NVL(wppb2.operation_completed_units,0) -
4140                                     NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0))
4141                         ) + decode(p_scrap,1,
4142 						decode(SIGN(nvl(wppb2.relieved_scrap_qty,0) + nvl(l_net_completion,0)),
4143 						0,1,
4144 						-1,1,
4145 						p_net_qty/(nvl(wppb2.relieved_scrap_qty,0) + nvl(l_net_completion,0))) * nvl(wppb2.pl_resource_in_apull,0),
4146 		             nvl(wppb2.pl_resource_in_apull,0) - NVL(wppb2.scrap_pull_resource,0)),
4147                         0
4148                       ),
4149 		  NVL(wppb.scrap_pull_resource,0) +
4150                	      decode(p_scrap,1,
4151 					  decode(SIGN(nvl(wppb2.relieved_scrap_qty,0) + nvl(l_net_completion,0)),
4152 					  0,1,
4153 					  -1,1,
4154 					  p_net_qty/(nvl(wppb2.relieved_scrap_qty,0) + nvl(l_net_completion,0))) * nvl(wppb2.pl_resource_in_apull,0),
4155 					  0),
4156                        NVL(pl_outside_processing_out,0) +
4157                       decode(SIGN(SIGN(NVL(wppb2.pl_outside_processing_in,0)
4158                                         - NVL(wppb2.pl_outside_processing_out,0)
4159                                         - NVL(wppb2.pl_outside_processing_var,0)
4160 					+ NVL(wppb.scrap_pull_outside_processing,0))
4161 				  +SIGN(l_net_pl_osp_value)
4162 				  +2
4163 				  ),
4164                         1,
4165                         (NVL(wppb2.pl_outside_processing_in,0)
4166                                         - NVL(wppb2.pl_outside_processing_out,0)- nvl(wppb2.pl_outside_processing_in_apull,0)
4167                                         - NVL(wppb2.pl_outside_processing_var,0) + NVL(wppb2.scrap_pull_resource,0))*
4168                         decode(SIGN(NVL(wppb2.operation_completed_units,0) -
4169                                 NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0)),
4170                              0,  1,
4171                             -1, 1,
4172                              p_net_qty/ (NVL(wppb2.operation_completed_units,0) -
4173                                     NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0))
4174                         ) + decode(p_scrap,1,
4175 						decode(SIGN(nvl(wppb2.relieved_scrap_qty,0) + nvl(l_net_completion,0)),
4176 						0,1,
4177 						-1,1,
4178 						p_net_qty/(nvl(wppb2.relieved_scrap_qty,0) + nvl(l_net_completion,0))) * nvl(wppb2.pl_outside_processing_in_apull,0),
4179 		             nvl(wppb2.pl_outside_processing_in_apull,0) - NVL(wppb2.scrap_pull_outside_processing,0)),
4180                         0
4181                       ),
4182                       decode(SIGN(SIGN(NVL(wppb.pl_outside_processing_in,0)
4183                                         - NVL(wppb.pl_outside_processing_out,0)
4184                                         - NVL(wppb.pl_outside_processing_var,0))
4185 				  +SIGN(l_net_pl_osp_value)
4186 				  +2
4187 				  ),
4188                         1,
4189                         (NVL(wppb.pl_outside_processing_in,0)
4190                                         - NVL(wppb.pl_outside_processing_out,0)-nvl(wppb.pl_outside_processing_in_apull,0)
4191                                         - NVL(wppb.pl_outside_processing_var,0) + NVL(wppb.scrap_pull_outside_processing,0))*
4192                         decode(SIGN(NVL(wppb2.operation_completed_units,0) -
4193                                 NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0)),
4194                            0, 1,
4195                           -1, 1,
4196                            p_net_qty/ (NVL(wppb2.operation_completed_units,0) -
4197                                     NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0))
4198                         ) + decode(p_scrap,1,
4199 						decode(SIGN(nvl(wppb2.relieved_scrap_qty,0) + nvl(l_net_completion,0)),
4200 						0,1,
4201 						-1,1,
4202 						p_net_qty/(nvl(wppb2.relieved_scrap_qty,0) + nvl(l_net_completion,0))) * nvl(wppb2.pl_outside_processing_in_apull,0),
4203 		             nvl(wppb2.pl_outside_processing_in_apull,0) - NVL(wppb2.scrap_pull_outside_processing,0)),
4204                         0
4205                       ),
4206 		      NVL(scrap_pull_outside_processing,0) +
4207 	              decode(p_scrap,1,
4208 				  decode(SIGN(nvl(wppb2.relieved_scrap_qty,0) + nvl(l_net_completion,0)),
4209 				  0,1,
4210 				  -1,1,
4211 				  p_net_qty/(nvl(wppb2.relieved_scrap_qty,0) + nvl(l_net_completion,0))) * nvl(wppb2.pl_outside_processing_in_apull,0),
4212 				  0),
4213                        NVL(pl_overhead_out,0) +
4214                       decode(SIGN(SIGN(NVL(wppb.pl_overhead_in,0)
4215 					-NVL(wppb.pl_overhead_out,0)
4216                                         -NVL(wppb.pl_overhead_var,0))
4217 				  +SIGN(l_net_pl_overhead_value)
4218 				  +2
4219 				  ),
4220                         1,
4221                         (NVL(wppb2.pl_overhead_in,0)-NVL(wppb2.pl_overhead_out,0)-nvl(wppb2.pl_overhead_in_apull,0)
4222                                         - NVL(wppb2.pl_overhead_var,0) + NVL(wppb2.scrap_pull_overhead,0) )*
4223                         decode(SIGN(NVL(wppb2.operation_completed_units,0) -
4224                                 NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0)),
4225                            0, 1,
4226                           -1, 1,
4227                            p_net_qty/ (NVL(wppb2.operation_completed_units,0) -
4228                                     NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0))
4229                         ) + decode(p_scrap,1,
4230 						decode(SIGN(nvl(relieved_scrap_qty,0) + nvl(l_net_completion,0)),
4231 						0,1,
4232 						-1,1,
4233 						p_net_qty/(nvl(relieved_scrap_qty,0) + nvl(l_net_completion,0))) * nvl(wppb2.pl_overhead_in_apull,0),
4234 
4235 		             nvl(wppb2.pl_overhead_in_apull,0) -  NVL(wppb2.scrap_pull_overhead,0)),
4236                         0
4237                       ),
4238                       decode(SIGN(SIGN(NVL(wppb2.pl_overhead_in,0)
4239 					-NVL(wppb2.pl_overhead_out,0)
4240                                         -NVL(wppb2.pl_overhead_var,0))
4241 				  +SIGN(l_net_pl_overhead_value)
4242 				  +2
4243 				  ),
4244                         1,
4245                         (NVL(wppb2.pl_overhead_in,0)-NVL(wppb2.pl_overhead_out,0)-nvl(wppb2.pl_overhead_in_apull,0)
4246                                         - NVL(wppb2.pl_overhead_var,0) + NVL(wppb2.scrap_pull_overhead,0) )*
4247                         decode(SIGN(NVL(wppb2.operation_completed_units,0) -
4248                                 NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0)),
4249                            0, 1,
4250                           -1, 1,
4251                            p_net_qty/ (NVL(wppb2.operation_completed_units,0) -
4252                                     NVL(wppb2.relieved_assembly_units,0)- nvl(wppb2.unrelieved_scrap_quantity,0))
4253                         ) + decode(p_scrap,1,
4254 						decode(SIGN(nvl(relieved_scrap_qty,0) + nvl(l_net_completion,0)),
4255 						0,1,
4256 						-1,1,
4257 						p_net_qty/(nvl(relieved_scrap_qty,0) + nvl(l_net_completion,0))) * nvl(wppb2.pl_overhead_in_apull,0),
4258 		             nvl(wppb2.pl_overhead_in_apull,0) -  NVL(wppb2.scrap_pull_overhead,0)),
4259                         0
4260                       ),
4261 		       NVL(wppb.scrap_pull_overhead,0) +
4262               	      decode(p_scrap,1,
4263 					  decode(SIGN(nvl(wppb2.relieved_scrap_qty,0) + nvl(l_net_completion,0)),
4264 					  0,1,
4265 					  -1,1,
4266 					  p_net_qty/(nvl(wppb2.relieved_scrap_qty,0) + nvl(l_net_completion,0))) * nvl(wppb2.pl_overhead_in_apull,0),
4267 					  0)
4268                        FROM wip_pac_period_balances wppb2
4269                        WHERE wppb2.pac_period_id = p_pac_period_id
4270                        AND   wppb2.cost_group_id = p_cost_group_id
4271                        AND   wppb2.wip_entity_id = p_entity_id
4272                        AND   NVL(wppb2.line_id,-99) = NVL(p_line_id,-99)
4273                        AND   wppb2.operation_seq_num = wppb.operation_seq_num
4274                       )
4275                       WHERE wppb.pac_period_id = p_pac_period_id
4276                       AND   wppb.cost_group_id = p_cost_group_id
4277                       AND   wppb.wip_entity_id = p_entity_id
4278                       AND   NVL(wppb.line_id,-99) = NVL(p_line_id,-99)
4279                       AND   wppb.operation_seq_num <=
4280                                 decode(p_scrap,1,p_op_seq,wppb.operation_seq_num);
4281                 END IF;
4282             ELSIF p_net_qty < 0 THEN -- This is a net return
4283               l_stmt_num := 44;
4284 
4285 	      /* Bug6847717: Calculate the total value across op_seq */
4286 	       SELECT NVL(SUM(NVL(wppb.tl_resource_out,0)),0) net_tl_resource_value,
4287 	              NVL(SUM(NVL(wppb.tl_overhead_out,0)),0) net_tl_overhead_value,
4288                       NVL(SUM(NVL(wppb.tl_outside_processing_out,0)),0) net_tl_osp_value,
4289                       NVL(SUM(NVL(wppb.pl_material_out,0)),0) net_pl_material_value,
4290                       NVL(SUM(NVL(wppb.pl_material_overhead_out,0)),0) net_pl_moh_value,
4291                       NVL(SUM(NVL(wppb.pl_resource_out,0)),0) net_pl_resource_value,
4292                       NVL(SUM(NVL(wppb.pl_outside_processing_out,0)),0) net_pl_osp_value,
4293 		      NVL(SUM(NVL(wppb.pl_overhead_out,0)),0) net_pl_overhead_value
4294                  INTO
4295                  l_net_tl_resource_value,
4296                  l_net_tl_overhead_value,
4297                  l_net_tl_osp_value,
4298                  l_net_pl_material_value,
4299                  l_net_pl_moh_value,
4300                  l_net_pl_resource_value,
4301                  l_net_pl_osp_value,
4302                  l_net_pl_overhead_value
4303                FROM wip_pac_period_balances wppb
4304                 WHERE wppb.pac_period_id = p_prior_period_id
4305                   AND wppb.cost_group_id = p_cost_group_id
4306                   AND wppb.wip_entity_id = p_entity_id
4307                   AND NVL(wppb.line_id,-99) = NVL(p_line_id,-99)
4308                   AND wppb.operation_seq_num <=
4309                       decode(p_scrap,1,p_op_seq,wppb.operation_seq_num);
4310 
4311               UPDATE wip_pac_period_balances wppb
4312               SET (tl_resource_out,
4313                         tl_resource_temp,
4314                         tl_outside_processing_out,
4315                         tl_outside_processing_temp,
4316                         tl_overhead_out,
4317                         tl_overhead_temp,
4318                         pl_material_out,
4319                         pl_material_temp,
4320                         pl_material_overhead_out,
4321                         pl_material_overhead_temp,
4322                         pl_resource_out,
4323                         pl_resource_temp,
4324                         pl_outside_processing_out,
4325                         pl_outside_processing_temp,
4326                         pl_overhead_out,
4327                         pl_overhead_temp
4328                 ) =
4329               (SELECT
4330                NVL(wppb.tl_resource_out,0) +
4331               decode(SIGN(SIGN(NVL(wppb2.tl_resource_out,0))
4332 			  +SIGN(l_net_tl_resource_value)
4333 			  +2
4334 			  ),
4335                 1,
4336                 NVL(wppb2.tl_resource_out,0) *
4337                         p_net_qty/decode(NVL(wppb2.relieved_assembly_units,0),
4338                                         0,1,wppb2.relieved_assembly_units),
4339                 0),
4340               decode(SIGN(SIGN(NVL(wppb2.tl_resource_out,0))
4341 			  +SIGN(l_net_tl_resource_value)
4342 			  +2
4343 			  ),
4344                 1,
4345                 NVL(wppb2.tl_resource_out,0) *
4346                         p_net_qty/decode(NVL(wppb2.relieved_assembly_units,0),
4347                                         0,1,wppb2.relieved_assembly_units),
4348                 0),
4349                NVL(wppb.tl_outside_processing_out,0) +
4350                decode(SIGN(SIGN(NVL(wppb2.tl_outside_processing_out,0))
4351 			   +SIGN(l_net_tl_osp_value)
4352 			   +2
4353 			   ),
4354                 1,
4355                 NVL(wppb2.tl_outside_processing_out,0)*
4356                         p_net_qty/decode(NVL(wppb2.relieved_assembly_units,0),
4357                                         0,1,wppb2.relieved_assembly_units),
4358                 0),
4359                decode(SIGN(SIGN(NVL(wppb2.tl_outside_processing_out,0))
4360 			   +SIGN(l_net_tl_osp_value)
4361 			   +2
4362 			   ),
4363                 1,
4364                 NVL(wppb2.tl_outside_processing_out,0)*
4365                         p_net_qty/decode(NVL(wppb2.relieved_assembly_units,0),
4366                                         0,1,wppb2.relieved_assembly_units),
4367                 0),
4368                NVL(wppb.tl_overhead_out,0) +
4369                decode(SIGN(SIGN(NVL(wppb2.tl_overhead_out,0))
4370 			   +SIGN(l_net_tl_overhead_value)
4371 			   +2
4372 			   ),
4373                 1,
4374                 NVL(wppb2.tl_overhead_out,0) *
4375                         p_net_qty/decode(NVL(wppb2.relieved_assembly_units,0),
4376                                         0,1,wppb2.relieved_assembly_units),
4377                 0),
4378                decode(SIGN(SIGN(NVL(wppb2.tl_overhead_out,0))
4379 			   +SIGN(l_net_tl_overhead_value)
4380 			   +2
4381 			   ),
4382                 1,
4383                 NVL(wppb2.tl_overhead_out,0) *
4384                         p_net_qty/decode(NVL(wppb2.relieved_assembly_units,0),
4385                                         0,1,wppb2.relieved_assembly_units),
4386                 0),
4387                NVL(wppb.pl_material_out,0) +
4388                decode(SIGN(SIGN(NVL(wppb2.pl_material_out,0))
4389 			   +SIGN(l_net_pl_material_value)
4390 			   +2
4391 			   ),
4392                 1,
4393                 NVL(wppb2.pl_material_out,0) *
4394                         p_net_qty/decode(NVL(wppb2.relieved_assembly_units,0),
4395                                         0,1,wppb2.relieved_assembly_units),
4396                 0),
4397                decode(SIGN(SIGN(NVL(wppb2.pl_material_out,0))
4398 			   +SIGN(l_net_pl_material_value)
4399 			   +2
4400 			   ),
4401                 1,
4402                 NVL(wppb2.pl_material_out,0) *
4403                         p_net_qty/decode(NVL(wppb2.relieved_assembly_units,0),
4404                                         0,1,wppb2.relieved_assembly_units),
4405                 0),
4406                NVL(wppb.pl_material_overhead_out,0) +
4407                decode(SIGN(SIGN(NVL(wppb2.pl_material_overhead_out,0))
4408 			   +SIGN(l_net_pl_moh_value)
4409 			   +2
4410 			   ),
4411                 1,
4412                 NVL(wppb2.pl_material_overhead_out,0) *
4413                         p_net_qty/decode(NVL(wppb2.relieved_assembly_units,0),
4414                                         0,1,wppb2.relieved_assembly_units),
4415                 0),
4416                decode(SIGN(SIGN(NVL(wppb2.pl_material_overhead_out,0))
4417 			   +SIGN(l_net_pl_moh_value)
4418 			   +2
4419 			   ),
4420                 1,
4421                 NVL(wppb2.pl_material_overhead_out,0) *
4422                         p_net_qty/decode(NVL(wppb2.relieved_assembly_units,0),
4423                                         0,1,wppb2.relieved_assembly_units),
4424                 0),
4425                NVL(wppb.pl_resource_out,0) +
4426                decode(SIGN(SIGN(NVL(wppb2.pl_resource_out,0))
4427 			   +SIGN(l_net_pl_resource_value)
4428 			   +2
4429 			   ),
4430                 1,
4431                 NVL(wppb2.pl_resource_out,0) *
4432                         p_net_qty/decode(NVL(wppb2.relieved_assembly_units,0),
4433                                         0,1,wppb2.relieved_assembly_units),
4434                 0),
4435                decode(SIGN(SIGN(NVL(wppb2.pl_resource_out,0))
4436 			   +SIGN(l_net_pl_resource_value)
4437 			   +2
4438 			   ),
4439                 1,
4440                 NVL(wppb2.pl_resource_out,0) *
4441                         p_net_qty/decode(NVL(wppb2.relieved_assembly_units,0),
4442                                         0,1,wppb2.relieved_assembly_units),
4443                 0),
4444                NVL(wppb.pl_outside_processing_out,0) +
4445                decode(SIGN(SIGN(NVL(wppb2.pl_outside_processing_out,0))
4446 			   +SIGN(l_net_pl_osp_value)
4447 			   +2
4448 			   ),
4449                 1,
4450                 NVL(wppb2.pl_outside_processing_out,0) *
4451                         p_net_qty/decode(NVL(wppb2.relieved_assembly_units,0),
4452                                         0,1,wppb2.relieved_assembly_units),
4453                 0),
4454                decode(SIGN(SIGN(NVL(wppb2.pl_outside_processing_out,0))
4455 			   +SIGN(l_net_pl_osp_value)
4456 			   +2
4457 			   ),
4458                 1,
4459                 NVL(wppb2.pl_outside_processing_out,0) *
4460                         p_net_qty/decode(NVL(wppb2.relieved_assembly_units,0),
4461                                         0,1,wppb2.relieved_assembly_units),
4462                 0),
4463                NVL(wppb.pl_overhead_out,0) +
4464                decode(SIGN(SIGN(NVL(wppb2.pl_overhead_out,0))
4465 			   +SIGN(l_net_pl_overhead_value)
4466 			   +2
4467 			   ),
4468                 1,
4469                 NVL(wppb2.pl_overhead_out,0) *
4470                         p_net_qty/decode(NVL(wppb2.relieved_assembly_units,0),
4471                                         0,1,wppb2.relieved_assembly_units),
4472                 0),
4473                decode(SIGN(SIGN(NVL(wppb2.pl_overhead_out,0))
4474 			   +SIGN(l_net_pl_overhead_value)
4475 			   +2
4476 			   ),
4477                 1,
4478                 NVL(wppb2.pl_overhead_out,0) *
4479                         p_net_qty/decode(NVL(wppb2.relieved_assembly_units,0),
4480                                         0,1,wppb2.relieved_assembly_units),
4481                 0)
4482                FROM  wip_pac_period_balances wppb2
4483                WHERE wppb2.pac_period_id = p_prior_period_id
4484                AND   wppb2.cost_group_id = p_cost_group_id
4485                AND   wppb2.wip_entity_id = p_entity_id
4486                AND   NVL(wppb2.line_id,-99) = NVL(p_line_id,-99)
4487                AND   wppb2.operation_seq_num = wppb.operation_seq_num
4488               )
4489               WHERE  wppb.pac_period_id = p_pac_period_id
4490               AND    wppb.cost_group_id = p_cost_group_id
4491               AND    wppb.wip_entity_id = p_entity_id
4492               AND    NVL(wppb.line_id,-99) = NVL(p_line_id,-99)
4493               AND    wppb.operation_seq_num <=
4494                         decode(p_scrap,1,p_op_seq,wppb.operation_seq_num);
4495             ELSE
4496               l_stmt_num := 46;
4497               -- This is net cancellation between completions and returns
4498 
4499 	       /* Bug6847717: Calculate the total value across op_seq */
4500 	       SELECT NVL(SUM(NVL(wppb.tl_resource_out,0)),0) net_tl_resource_value,
4501 	              NVL(SUM(NVL(wppb.tl_overhead_out,0)),0) net_tl_overhead_value,
4502                       NVL(SUM(NVL(wppb.tl_outside_processing_out,0)),0) net_tl_osp_value,
4503                       NVL(SUM(NVL(wppb.pl_material_out,0)),0) net_pl_material_value,
4504                       NVL(SUM(NVL(wppb.pl_material_overhead_out,0)),0) net_pl_moh_value,
4505                       NVL(SUM(NVL(wppb.pl_resource_out,0)),0) net_pl_resource_value,
4506                       NVL(SUM(NVL(wppb.pl_outside_processing_out,0)),0) net_pl_osp_value,
4507 		      NVL(SUM(NVL(wppb.pl_overhead_out,0)),0) net_pl_overhead_value,
4508                       NVL(MAX(relieved_assembly_units),0)
4509 		 INTO
4510                  l_net_tl_resource_value,
4511                  l_net_tl_overhead_value,
4512                  l_net_tl_osp_value,
4513                  l_net_pl_material_value,
4514                  l_net_pl_moh_value,
4515                  l_net_pl_resource_value,
4516                  l_net_pl_osp_value,
4517                  l_net_pl_overhead_value,
4518                  l_prior_completed_units
4519 	       FROM wip_pac_period_balances wppb
4520                 WHERE wppb.pac_period_id = p_prior_period_id
4521                   AND wppb.cost_group_id = p_cost_group_id
4522                   AND wppb.wip_entity_id = p_entity_id
4523                   AND NVL(wppb.line_id,-99) = NVL(p_line_id,-99)
4524                   AND wppb.operation_seq_num <=
4525                       decode(p_scrap,1,p_op_seq,wppb.operation_seq_num);
4526 
4527               IF p_final_completion_flag = 1 THEN
4528 
4529                      l_stmt_num := 48;
4530                      SELECT sum(primary_quantity)
4531                      INTO   l_assembly_return_cnt
4532                      FROM   mtl_material_transactions mmt
4533                      WHERE  mmt.transaction_source_id =  p_entity_id
4534                      AND    mmt.transaction_action_id = 32
4535                      AND    nvl(mmt.repetitive_line_id,-99) = nvl(p_line_id,-99)
4536                      AND    mmt.transaction_date BETWEEN TRUNC(p_start_date)
4537                                                      AND  (TRUNC(p_end_date) + 0.99999);
4538 
4539                   IF (p_prior_period_id <> -1 AND l_prior_completed_units <> 0) then
4540 
4541                      l_stmt_num := 50;
4542                      UPDATE wip_pac_period_balances wppb
4543                       SET    (  tl_resource_out,
4544                                 tl_outside_processing_out,
4545                                 tl_overhead_out,
4546                                 pl_material_out,
4547                                 pl_material_overhead_out,
4548                                 pl_resource_out,
4549                                 pl_outside_processing_out,
4550                                 pl_overhead_out
4551                         ) =
4552                       (SELECT
4553                        NVL(wppb.tl_resource_out,0) +
4554                       decode(SIGN(SIGN(NVL(wppb2.tl_resource_out,0))
4555 				  +SIGN(l_net_tl_resource_value)
4556 				  +2
4557 				  ),
4558                         1,
4559                         NVL(wppb2.tl_resource_out,0) *
4560                                 l_assembly_return_cnt/decode(NVL(wppb2.relieved_assembly_units,0),
4561                                                       0,1,wppb2.relieved_assembly_units),
4562                         0),
4563                        NVL(wppb.tl_outside_processing_out,0) +
4564                        decode(SIGN(SIGN(NVL(wppb2.tl_outside_processing_out,0))
4565 				   +SIGN(l_net_tl_osp_value)
4566 				   +2
4567 				   ),
4568                         1,
4569                         NVL(wppb2.tl_outside_processing_out,0)*
4570                            l_assembly_return_cnt/decode(NVL(wppb2.relieved_assembly_units,0),
4571                                                 0,1,wppb2.relieved_assembly_units),
4572                         0),
4573                        NVL(wppb.tl_overhead_out,0) +
4574                        decode(SIGN(SIGN(NVL(wppb2.tl_overhead_out,0))
4575 				   +SIGN(l_net_tl_overhead_value)
4576 				   +2
4577 				   ),
4578                         1,
4579                         NVL(wppb2.tl_overhead_out,0) *
4580                            l_assembly_return_cnt/decode(NVL(wppb2.relieved_assembly_units,0),
4581                                                 0,1,wppb2.relieved_assembly_units),
4582                         0),
4583                        NVL(wppb.pl_material_out,0) +
4584                        decode(SIGN(SIGN(NVL(wppb2.pl_material_out,0))
4585 				   +SIGN(l_net_pl_material_value)
4586 				   +2
4587 				   ),
4588                         1,
4589                         NVL(wppb2.pl_material_out,0) *
4590                            l_assembly_return_cnt/decode(NVL(wppb2.relieved_assembly_units,0),
4591                                                 0,1,wppb2.relieved_assembly_units),
4592                         0),
4593                        NVL(wppb.pl_material_overhead_out,0) +
4594                        decode(SIGN(SIGN(NVL(wppb2.pl_material_overhead_out,0))
4595 				   +SIGN(l_net_pl_moh_value)
4596 			 	   +2
4597 				   ),
4598                         1,
4599                         NVL(wppb2.pl_material_overhead_out,0) *
4600                            l_assembly_return_cnt/decode(NVL(wppb2.relieved_assembly_units,0),
4601                                                          0,1,wppb2.relieved_assembly_units),
4602                         0),
4603                        NVL(wppb.pl_resource_out,0) +
4604                        decode(SIGN(SIGN(NVL(wppb2.pl_resource_out,0))
4605 				   +SIGN(l_net_pl_resource_value)
4606 				   +2
4607 			 	   ),
4608                         1,
4609                         NVL(wppb2.pl_resource_out,0) *
4610                            l_assembly_return_cnt/decode(NVL(wppb2.relieved_assembly_units,0),
4611                                                      0,1,wppb2.relieved_assembly_units),
4612                         0),
4613                        NVL(wppb.pl_outside_processing_out,0) +
4614                        decode(SIGN(SIGN(NVL(wppb2.pl_outside_processing_out,0))
4615 				   +SIGN(l_net_pl_osp_value)
4616 				   +2
4617 				   ),
4618                         1,
4619                         NVL(wppb2.pl_outside_processing_out,0) *
4620                            l_assembly_return_cnt/decode(NVL(wppb2.relieved_assembly_units,0),
4621                                                 0,1,wppb2.relieved_assembly_units),
4622                         0),
4623                        NVL(wppb.pl_overhead_out,0) +
4624                        decode(SIGN(SIGN(NVL(wppb2.pl_overhead_out,0))
4625 				   +SIGN(l_net_pl_overhead_value)
4626 				   +2
4627 				   ),
4628                         1,
4629                         NVL(wppb2.pl_overhead_out,0) *
4630                            l_assembly_return_cnt/decode(NVL(wppb2.relieved_assembly_units,0),
4631                                                        0,1,wppb2.relieved_assembly_units),
4632                         0)
4633                        FROM  wip_pac_period_balances wppb2
4634                        WHERE wppb2.pac_period_id = p_prior_period_id
4635                        AND   wppb2.cost_group_id = p_cost_group_id
4636                        AND   wppb2.wip_entity_id = p_entity_id
4637                        AND   NVL(wppb2.line_id,-99) = NVL(p_line_id,-99)
4638                        AND   wppb2.operation_seq_num = wppb.operation_seq_num
4639                       )
4640                       WHERE  wppb.pac_period_id = p_pac_period_id
4641                       AND    wppb.cost_group_id = p_cost_group_id
4642                       AND    wppb.wip_entity_id = p_entity_id
4643                       AND    NVL(wppb.line_id,-99) = NVL(p_line_id,-99)
4644                       AND    wppb.operation_seq_num <=
4645                                 decode(p_scrap,1,p_op_seq,wppb.operation_seq_num);
4646 
4647                  END IF;  /* end of p_prior_period_id <> -1 AND l_prior_completed_units <> 0 */
4648                       l_stmt_num := 52;
4649                       UPDATE wip_pac_period_balances wppb
4650                       SET ( tl_resource_out,
4651                             tl_resource_temp,
4652                             tl_outside_processing_out,
4653                             tl_outside_processing_temp,
4654                             tl_overhead_out,
4655                             tl_overhead_temp,
4656                             pl_material_out,
4657                             pl_material_temp,
4658                             pl_material_overhead_out,
4659                             pl_material_overhead_temp,
4660                             pl_resource_out,
4661                             pl_resource_temp,
4662                             pl_outside_processing_out,
4663                             pl_outside_processing_temp,
4664                             pl_overhead_out,
4665                             pl_overhead_temp
4666                         ) =
4667                       (SELECT
4668                        NVL(wppb.tl_resource_out,0) +
4669                       decode(SIGN(SIGN(NVL(wppb.tl_resource_in,0)
4670 					-NVL(wppb.tl_resource_out,0)
4671                                         -NVL(tl_resource_var,0))
4672 				  +SIGN(l_net_tl_resource_value)
4673 				  +2
4674 				  ),
4675                         1,
4676                         Decode(l_prior_completed_units, 0, 0,
4677                         (NVL(wppb.tl_resource_in,0)-NVL(wppb.tl_resource_out,0)
4678                                         - NVL(tl_resource_var,0))),
4679                        0
4680                       ),
4681                       decode(SIGN(SIGN(NVL(wppb.tl_resource_in,0)
4682 					-NVL(wppb.tl_resource_out,0)
4683                                         -NVL(tl_resource_var,0))
4684 				  +SIGN(l_net_tl_resource_value)
4685 				  +2
4686 				  ),
4687                         1,
4688                         (NVL(wppb.tl_resource_in,0)-NVL(wppb.tl_resource_out,0)
4689                                         - NVL(tl_resource_var,0))
4690                         ,0
4691                       ),
4692                        NVL(tl_outside_processing_out,0) +
4693                       decode(SIGN(SIGN(NVL(wppb.tl_outside_processing_in,0)
4694                                         - NVL(wppb.tl_outside_processing_out,0)
4695                                         - NVL(wppb.tl_outside_processing_var,0))
4696 				  +SIGN(l_net_tl_osp_value)
4697 				  +2
4698 				  ),
4699                         1,
4700                         Decode(l_prior_completed_units, 0, 0,
4701                         (NVL(wppb.tl_outside_processing_in,0)
4702                                         - NVL(wppb.tl_outside_processing_out,0)
4703                                         - NVL(wppb.tl_outside_processing_var,0)))
4704                         ,0
4705                       ),
4706                       decode(SIGN(SIGN(NVL(wppb.tl_outside_processing_in,0)
4707                                         - NVL(wppb.tl_outside_processing_out,0)
4708                                         - NVL(wppb.tl_outside_processing_var,0))
4709 				  +SIGN(l_net_tl_osp_value)
4710 				  +2
4711 				  ),
4712                         1,
4713                         (NVL(wppb.tl_outside_processing_in,0)
4714                                         - NVL(wppb.tl_outside_processing_out,0)
4715                                         - NVL(wppb.tl_outside_processing_var,0)),
4716                         0
4717                       ),
4718                        NVL(tl_overhead_out,0) +
4719                       decode(SIGN(SIGN(NVL(wppb.tl_overhead_in,0)
4720 					-NVL(wppb.tl_overhead_out,0)
4721                                         -NVL(wppb.tl_overhead_var,0))
4722 				  +SIGN(l_net_tl_overhead_value)
4723 				  +2
4724 				  ),
4725                         1,
4726                         Decode(l_prior_completed_units, 0, 0,
4727                         (NVL(wppb.tl_overhead_in,0)-NVL(wppb.tl_overhead_out,0)
4728                                         - NVL(wppb.tl_overhead_var,0))),
4729                         0
4730                       ),
4731                       decode(SIGN(SIGN(NVL(wppb.tl_overhead_in,0)
4732 					-NVL(wppb.tl_overhead_out,0)
4733                                         -NVL(wppb.tl_overhead_var,0))
4734 				  +SIGN(l_net_tl_overhead_value)
4735 				  +2
4736 				  ),
4737                         1,
4738                         (NVL(wppb.tl_overhead_in,0)-NVL(wppb.tl_overhead_out,0)
4739                                         - NVL(wppb.tl_overhead_var,0)),
4740                         0
4741                       ),
4742                        NVL(pl_material_out,0) +
4743                       decode(SIGN(SIGN(NVL(wppb.pl_material_in,0)
4744 					-NVL(wppb.pl_material_out,0)
4745                                         -NVL(wppb.pl_material_var,0))
4746 				  +SIGN(l_net_pl_material_value)
4747 				  +2
4748 				  ),
4749                         1,
4750                         Decode(l_prior_completed_units, 0, 0,
4751                         (NVL(wppb.pl_material_in,0)-NVL(wppb.pl_material_out,0)- nvl(wppb.pl_material_in_apull,0)
4752                                         - NVL(wppb.pl_material_var,0))
4753                          + nvl(wppb.pl_material_in_apull,0)),
4754                         0
4755                       ),
4756                       decode(SIGN(SIGN(NVL(wppb.pl_material_in,0)
4757 					-NVL(wppb.pl_material_out,0)
4758                                         -NVL(wppb.pl_material_var,0))
4759 				  +SIGN(l_net_pl_material_value)
4760 				  +2
4761 				  ),
4762                         1,
4763                         (NVL(wppb.pl_material_in,0)-NVL(wppb.pl_material_out,0)-nvl(wppb.pl_material_in_apull,0)
4764                                         - NVL(wppb.pl_material_var,0)) + nvl(wppb.pl_material_in_apull,0),
4765                         0
4766                       ),
4767                        NVL(pl_material_overhead_out,0) +
4768                       decode(SIGN(SIGN(NVL(wppb.pl_material_overhead_in,0)
4769                                         - NVL(wppb.pl_material_overhead_out,0)
4770                                         - NVL(wppb.pl_material_overhead_var,0))
4771 				  +SIGN(l_net_pl_moh_value)
4772 				  +2
4773 				  ),
4774                         1,
4775                         Decode(l_prior_completed_units, 0, 0,
4776                         (NVL(wppb.pl_material_overhead_in,0)
4777                                         - NVL(wppb.pl_material_overhead_out,0)-nvl(wppb.pl_material_overhead_in_apull,0)
4778                                         - NVL(wppb.pl_material_overhead_var,0))
4779                          + nvl(wppb.pl_material_overhead_in_apull,0)),
4780                         0
4781                       ),
4782                       decode(SIGN(SIGN(NVL(wppb.pl_material_overhead_in,0)
4783                                         - NVL(wppb.pl_material_overhead_out,0)
4784                                         - NVL(wppb.pl_material_overhead_var,0))
4785 				  +SIGN(l_net_pl_moh_value)
4786 				  +2
4787 				  ),
4788                         1,
4789                         (NVL(wppb.pl_material_overhead_in,0)
4790                                         - NVL(wppb.pl_material_overhead_out,0)-nvl(wppb.pl_material_overhead_in_apull,0)
4791                                         - NVL(wppb.pl_material_overhead_var,0))
4792                         + nvl(wppb.pl_material_overhead_in_apull,0),
4793                         0
4794                       ),
4795                        NVL(pl_resource_out,0) +
4796                       decode(SIGN(SIGN(NVL(wppb.pl_resource_in,0)
4797 					-NVL(wppb.pl_resource_out,0)
4798                                         -NVL(wppb.pl_resource_var,0))
4799 				  +SIGN(l_net_pl_resource_value)
4800 				  +2
4801 				  ),
4802                         1,
4803                         Decode(l_prior_completed_units, 0, 0,
4804                         (NVL(wppb.pl_resource_in,0)-NVL(wppb.pl_resource_out,0)-nvl(wppb.pl_resource_in_apull,0)
4805                                         - NVL(wppb.pl_resource_var,0))
4806                          + nvl(wppb.pl_resource_in_apull,0)),
4807                         0
4808                       ),
4809                       decode(SIGN(SIGN(NVL(wppb.pl_resource_in,0)
4810 					-NVL(wppb.pl_resource_out,0)
4811                                         -NVL(wppb.pl_resource_var,0))
4812 				  +SIGN(l_net_pl_resource_value)
4813 				  +2
4814 				  ),
4815                         1,
4816                         (NVL(wppb.pl_resource_in,0)-NVL(wppb.pl_resource_out,0)-nvl(wppb.pl_resource_in_apull,0)
4817                                         - NVL(wppb.pl_resource_var,0)) + nvl(wppb.pl_resource_in_apull,0),
4818                         0
4819                       ),
4820                        NVL(pl_outside_processing_out,0) +
4821                       decode(SIGN(SIGN(NVL(wppb.pl_outside_processing_in,0)
4822                                         - NVL(wppb.pl_outside_processing_out,0)
4823                                         - NVL(wppb.pl_outside_processing_var,0))
4824 				  +SIGN(l_net_pl_osp_value)
4825 				  +2
4826 				  ),
4827                         1,
4828                         Decode(l_prior_completed_units, 0, 0,
4829                         (NVL(wppb.pl_outside_processing_in,0)
4830                                         - NVL(wppb.pl_outside_processing_out,0)-nvl(wppb.pl_outside_processing_in_apull,0)
4831                                         - NVL(wppb.pl_outside_processing_var,0))
4832                          + nvl(wppb.pl_outside_processing_in_apull,0)),
4833                         0
4834                       ),
4835                       decode(SIGN(SIGN(NVL(wppb.pl_outside_processing_in,0)
4836                                         - NVL(wppb.pl_outside_processing_out,0)
4837                                         - NVL(wppb.pl_outside_processing_var,0))
4838 				  +SIGN(l_net_pl_osp_value)
4839 				  +2
4840 				  ),
4841                         1,
4842                         (NVL(wppb.pl_outside_processing_in,0)
4843                                         - NVL(wppb.pl_outside_processing_out,0)-nvl(wppb.pl_outside_processing_in_apull,0)
4844                                         - NVL(wppb.pl_outside_processing_var,0)) +
4845                                         nvl(wppb.pl_outside_processing_in_apull,0),
4846                         0
4847                       ),
4848                        NVL(pl_overhead_out,0) +
4849                       decode(SIGN(SIGN(NVL(wppb.pl_overhead_in,0)
4850 					-NVL(wppb.pl_overhead_out,0)
4851                                         -NVL(wppb.pl_overhead_var,0))
4852 				  +SIGN(l_net_pl_overhead_value)
4853 				  +2
4854 				  ),
4855                         1,
4856                         Decode(l_prior_completed_units, 0, 0,
4857                         (NVL(wppb.pl_overhead_in,0)-NVL(wppb.pl_overhead_out,0)-nvl(wppb.pl_overhead_in_apull,0)
4858                                         - NVL(wppb.pl_overhead_var,0))
4859                          + nvl(wppb.pl_overhead_in_apull,0)),
4860                          0
4861                       ),
4862                       decode(SIGN(SIGN(NVL(wppb.pl_overhead_in,0)
4863 					-NVL(wppb.pl_overhead_out,0)
4864                                         -NVL(wppb.pl_overhead_var,0))
4865 				  +SIGN(l_net_pl_overhead_value)
4866 				  +2
4867 				  ),
4868                         1,
4869                         (NVL(wppb.pl_overhead_in,0)-NVL(wppb.pl_overhead_out,0)-nvl(wppb.pl_overhead_in_apull,0)
4870                                         - NVL(wppb.pl_overhead_var,0))
4871                         + nvl(wppb.pl_overhead_in_apull,0),
4872                         0
4873                       )
4874                        FROM wip_pac_period_balances wppb2
4875                        WHERE wppb2.pac_period_id = p_pac_period_id
4876                        AND   wppb2.cost_group_id = p_cost_group_id
4877                        AND   wppb2.wip_entity_id = p_entity_id
4878                        AND   NVL(wppb2.line_id,-99) = NVL(p_line_id,-99)
4879                        AND   wppb2.operation_seq_num = wppb.operation_seq_num
4880                       )
4881                       WHERE
4882                       wppb.pac_period_id = p_pac_period_id
4883                       AND wppb.cost_group_id = p_cost_group_id
4884                       AND wppb.wip_entity_id = p_entity_id
4885                       AND NVL(wppb.line_id,-99) = NVL(p_line_id,-99)
4886                       AND wppb.operation_seq_num <=
4887                                 decode(p_scrap,1,p_op_seq,wppb.operation_seq_num);
4888             ELSE
4889                       l_stmt_num := 54;
4890                       UPDATE wip_pac_period_balances wppb
4891                       SET    (
4892                                tl_resource_temp,
4893                                tl_outside_processing_temp,
4894                                tl_overhead_temp,
4895                                pl_material_temp,
4896                                pl_material_overhead_temp,
4897                                pl_resource_temp,
4898                                pl_outside_processing_temp,
4899                                pl_overhead_temp
4900                              )
4901                       =      (
4902                                SELECT DECODE(
4903                                         SIGN(SIGN(NVL(tl_resource_out,0))
4904 					     +SIGN(l_net_tl_resource_value)
4905 					     +2
4906 					     ),
4907                                         1,
4908                                         NVL(tl_resource_out,0) /
4909                                         DECODE(NVL(relieved_assembly_units,0),0,1,relieved_assembly_units),
4910                                         0
4911                                       ),
4912                                       DECODE(
4913                                         SIGN(SIGN(NVL(tl_outside_processing_out,0))
4914 					     +SIGN(l_net_tl_osp_value)
4915 					     +2
4916 					     ),
4917                                         1,
4918                                         NVL(tl_outside_processing_out,0) /
4919                                         DECODE(NVL(relieved_assembly_units,0),0,1,relieved_assembly_units),
4920                                         0
4921                                       ),
4922                                       DECODE(
4923                                         SIGN(SIGN(NVL(tl_overhead_out,0))
4924 					     +SIGN(l_net_tl_overhead_value)
4925 					     +2
4926 					     ),
4927                                         1,
4928                                         NVL(tl_overhead_out,0) /
4929                                         DECODE(NVL(relieved_assembly_units,0),0,1,relieved_assembly_units),
4930                                         0
4931                                       ),
4932                                       DECODE(
4933                                         SIGN(SIGN(NVL(pl_material_out,0))
4934 					     +SIGN(l_net_pl_material_value)
4935 					     +2
4936 					     ),
4937                                         1,
4938                                         NVL(pl_material_out,0) /
4939                                         DECODE(NVL(relieved_assembly_units,0),0,1,relieved_assembly_units),
4940                                         0
4941                                       ),
4942                                       DECODE(
4943                                         SIGN(SIGN(NVL(pl_material_overhead_out,0))
4944 					     +SIGN(l_net_pl_moh_value)
4945 					     +2
4946 					     ),
4947                                         1,
4948                                         NVL(pl_material_overhead_out,0) /
4949                                         DECODE(NVL(relieved_assembly_units,0),0,1,relieved_assembly_units),
4950                                         0
4951                                       ),
4952                                       DECODE(
4953                                         SIGN(SIGN(NVL(pl_resource_out,0))
4954 					     +SIGN(l_net_pl_resource_value)
4955 					     +2
4956 					     ),
4957                                         1,
4958                                         NVL(pl_resource_out,0) /
4959                                         DECODE(NVL(relieved_assembly_units,0),0,1,relieved_assembly_units),
4960                                         0
4961                                       ),
4962                                       DECODE(
4963                                         SIGN(SIGN(NVL(pl_outside_processing_out,0))
4964 					     +SIGN(l_net_pl_osp_value)
4965 					     +2
4966 					     ),
4967                                         1,
4968                                         NVL(pl_outside_processing_out,0) /
4969                                         DECODE(NVL(relieved_assembly_units,0),0,1,relieved_assembly_units),
4970                                         0
4971                                       ),
4972                                       DECODE(
4973                                         SIGN(SIGN(NVL(pl_overhead_out,0))
4974 					     +SIGN(l_net_pl_overhead_value)
4975 					     +2
4976 					     ),
4977                                         1,
4978                                         NVL(pl_overhead_out,0) /
4979                                         DECODE(NVL(relieved_assembly_units,0),0,1,relieved_assembly_units),
4980                                         0
4981                                       )
4982                                FROM   wip_pac_period_balances
4983                                WHERE  pac_period_id = p_prior_period_id
4984                                AND    cost_group_id = p_cost_group_id
4985                                AND    wip_entity_id = p_entity_id
4986                                AND    NVL(line_id,-99) = NVL(p_line_id,-99)
4987                                AND    operation_seq_num = wppb.operation_seq_num
4988                              )
4989                       WHERE  pac_period_id = p_pac_period_id
4990                       AND    cost_group_id = p_cost_group_id
4991                       AND    wip_entity_id = p_entity_id
4992                       AND    NVL(line_id,-99) = NVL(p_line_id,-99)
4993                       AND    operation_seq_num <= decode(p_scrap,1,p_op_seq,wppb.operation_seq_num);
4994              END IF; -- end of p_final_completion_flag = 1
4995             END IF; -- Check whether p_net_qty is > 0, < 0 or = 0
4996          END IF; /* End of p_material_relief_algorithm */
4997 
4998             ------------------------------------------------------------------
4999             -- Update wppb.RELIEVED_ASSEMBLY_UNITS
5000             ------------------------------------------------------------------
5001             l_stmt_num := 56;
5002             UPDATE  wip_pac_period_balances wppb
5003             SET     wppb.relieved_assembly_units =
5004                     NVL(wppb.relieved_assembly_units,0) + p_net_qty
5005             WHERE   wppb.pac_period_id = p_pac_period_id
5006             AND     wppb.cost_group_id = p_cost_group_id
5007             AND     wppb.wip_entity_id = p_entity_id
5008             AND     NVL(wppb.line_id,-99) = NVL(p_line_id,-99)
5009             AND     wppb.operation_seq_num <= decode(p_scrap,1,p_op_seq,
5010                                                                wppb.operation_seq_num);
5011           END IF; -- check for cfm
5012 
5013         ----------------------------------------------------------------------
5014         -- Load transaction costs in MPTCD
5015         -- Costs to be loaded into MPTCD must be in the
5016         -- pri uom of the master item organization
5017         -- The costs stored in the temp column and p_net_qty are based
5018         -- on the organization's pri UOM which may be different from
5019         -- the item master org's pri UOM. So, we will convert both
5020         -- costs and quantity if the uom control is not 1 i.e. not at
5021         -- the item master org level
5022         ----------------------------------------------------------------------
5023 
5024         l_uom_conv_rate := 1;
5025 
5026         IF (p_uom_control <> 1) THEN
5027 
5028                 l_stmt_num := 60;
5029 
5030                 SELECT  NVL(we.primary_item_id,-1),
5031                         we.organization_id
5032                 INTO    l_item_id,
5033                         l_org_id
5034                 FROM    wip_entities we
5035                 WHERE   we.wip_entity_id = p_entity_id;
5036 
5037              IF (l_item_id <> -1) THEN
5038                 l_stmt_num := 65;
5039                 CSTPPINV.get_um_rate (  i_txn_org_id          => l_org_id,
5040                                         i_master_org_id       => p_master_org_id,
5041                                         i_txn_cost_group_id   => -1,
5042                                         i_txfr_cost_group_id  => -2,
5043                                         i_txn_action_id       => -3,
5044                                         i_item_id             => l_item_id,
5045                                         i_uom_control         => p_uom_control,
5046                                         i_user_id             => p_user_id,
5047                                         i_login_id            => p_login_id,
5048                                         i_request_id          => p_request_id,
5049                                         i_prog_id             => p_prog_id,
5050                                         i_prog_appl_id        => p_prog_app_id,
5051                                         o_um_rate             => l_uom_conv_rate,
5052                                         o_err_num             => l_err_num,
5053                                         o_err_code            => l_err_code,
5054                                         o_err_msg             => l_err_msg);
5055 
5056                 IF (l_err_num <> 0) THEN
5057 
5058                         l_err_msg := SUBSTR('UOM conv error wip_entity: '
5059                                                 ||TO_CHAR(p_entity_id)
5060                                                 ||':'
5061                                                 ||l_err_msg,1,240);
5062                          RAISE CST_PROCESS_ERROR;
5063 
5064                 END IF;
5065 
5066              ELSE
5067                l_stmt_num := 67;
5068 
5069                l_uom_conv_rate :=1;
5070 
5071              END IF;
5072 
5073         END IF; -- check for uom control level
5074 
5075         l_stmt_num := 70;
5076         IF ( p_net_qty = 0 and p_final_completion_flag = 1 ) then
5077 
5078            SELECT sum(primary_quantity)
5079            INTO   l_completed_assembly_qty
5080            FROM   mtl_material_transactions mmt
5081            WHERE  mmt.transaction_source_id = p_entity_id
5082            AND    nvl(mmt.repetitive_line_id,-99) = nvl(p_line_id,-99)
5083            AND    mmt.transaction_action_id  = 31
5084            AND    mmt.transaction_date BETWEEN TRUNC(p_start_date)
5085                                        AND (TRUNC(p_end_date) + 0.99999);
5086         END IF;
5087 
5088        l_conv_net_qty := p_net_qty * l_uom_conv_rate;
5089        -- Using same variable for conversion value aslo
5090        l_completed_assembly_qty := l_completed_assembly_qty * l_uom_conv_rate;
5091 
5092         IF (p_scrap = 1) THEN -- Its a scrap txn
5093 
5094           FOR c_txn_rec IN c_scrap_txn LOOP
5095 
5096                 l_stmt_num := 75;
5097 
5098                 insert_wip_costs
5099                         (p_pac_period_id          => p_pac_period_id,
5100                          p_prior_period_id        => p_prior_period_id,
5101                          p_cost_group_id          => p_cost_group_id,
5102                          p_cost_type_id           => p_cost_type_id,
5103                          p_item_id                => c_txn_rec.item_id,
5104                          p_entity_id              => p_entity_id,
5105                          p_line_id                => p_line_id,
5106                          p_txn_id                 => c_txn_rec.txn_id,
5107                          p_net_qty                => l_conv_net_qty,
5108                          p_completed_assembly_qty => NULL,
5109                          p_final_completion_flag  => NULL,
5110                          p_start_date             => p_start_date,
5111                          p_end_date               => p_end_date,
5112                          p_user_id                => p_user_id,
5113                          p_login_id               => p_login_id,
5114                          p_request_id             => p_request_id,
5115                          p_prog_id                => p_prog_id,
5116                          p_prog_app_id            => p_prog_app_id,
5117                          x_err_num                => l_err_num,
5118                          x_err_code               => l_err_code,
5119                          x_err_msg                => l_err_msg);
5120 
5121                 IF (l_err_num <> 0) THEN
5122 
5123                         l_err_msg := SUBSTR('Txn_id: '
5124                                                 ||TO_CHAR(c_txn_rec.txn_id)
5125                                                 ||':'
5126                                                 ||l_err_msg,1,240);
5127                          RAISE CST_PROCESS_ERROR;
5128 
5129                 END IF;
5130 
5131                 l_stmt_num := 80;
5132 
5133                 check_expense_flags (
5134                                 p_item_id    => c_txn_rec.item_id,
5135                                 p_subinv     => c_txn_rec.subinv,
5136                                 p_org_id     => c_txn_rec.org_id,
5137                                 x_exp_item   => l_exp_item,
5138                                 x_exp_flag   => l_exp_flag,
5139                                 x_err_num    => l_err_num,
5140                                 x_err_code   => l_err_code,
5141                                 x_err_msg    => l_err_msg);
5142 
5143                 IF (l_err_num <> 0) THEN
5144 
5145                         l_err_msg := SUBSTR('Item_id: '
5146                                                 ||TO_CHAR(c_txn_rec.item_id)
5147                                                 ||':'
5148                                                 ||l_err_msg,1,240);
5149 
5150                         RAISE CST_PROCESS_ERROR;
5151 
5152                 END IF;
5153 
5154                 l_stmt_num := 85;
5155 
5156                 CSTPPWAC.cost_processor
5157                         (i_legal_entity         => p_legal_entity,
5158                          i_pac_period_id        => p_pac_period_id,
5159                          i_org_id               => p_org_id,
5160                          i_cost_group_id        => p_cost_group_id,
5161                          i_txn_cost_group_id    => NULL,
5162                          i_txfr_cost_group_id   => NULL,
5163                          i_cost_type_id         => p_cost_type_id,
5164                          i_cost_method          => p_cost_method,
5165                          i_process_group        => 1,
5166                          i_txn_id               => c_txn_rec.txn_id,
5167                          i_qty_layer_id         => p_qty_layer_id,
5168                          i_cost_layer_id        => p_cost_layer_id,
5169                          i_pac_rates_id         => p_pac_rates_id,
5170                          i_item_id              => c_txn_rec.item_id,
5171                          i_txn_qty              => c_txn_rec.pri_qty *
5172                                                         l_uom_conv_rate,
5173                          i_txn_action_id        => c_txn_rec.txn_action_id,
5174                          i_txn_src_type_id      => c_txn_rec.txn_src_type_id,
5175                          i_fob_point            => NULL,
5176                          i_exp_item             => l_exp_item,
5177                          i_exp_flag             => l_exp_flag,
5178                          i_cost_hook_used       => -1,
5179                          i_user_id              => p_user_id,
5180                          i_login_id             => p_login_id,
5181                          i_req_id               => p_request_id,
5182                          i_prg_appl_id          => p_prog_app_id,
5183                          i_prg_id               => p_prog_id,
5184                          i_txn_category         => p_txn_category,
5185                          o_err_num              => l_err_num,
5186                          o_err_code             => l_err_code,
5187                          o_err_msg              => l_err_msg);
5188 
5189                   IF (l_err_num <> 0) THEN
5190 
5191                         l_err_msg := SUBSTR('Txn_id: '
5192                                                 ||TO_CHAR(c_txn_rec.txn_id)
5193                                                 ||':'
5194                                                 ||l_err_msg,1,240);
5195                         RAISE CST_PROCESS_ERROR;
5196 
5197                   END IF;
5198 
5199 		-- ================================================================
5200                 -- FP 12.0-12.1.3 Bug 9280484 fix: Update periodic primary quantity
5201 	        -- with converted UOM transaction quantity
5202 	        -- ================================================================
5203                 UPDATE MTL_MATERIAL_TRANSACTIONS
5204                    SET PERIODIC_PRIMARY_QUANTITY = c_txn_rec.pri_qty *  l_uom_conv_rate
5205                  WHERE TRANSACTION_ID = c_txn_rec.txn_id;
5206 
5207           END LOOP; --scrap_txn_loop
5208 
5209         ELSE -- Its is an assembly completion/return txn
5210 
5211           l_stmt_num := 90;
5212 
5213           FOR c_txn_rec IN c_assy_txn LOOP
5214                 l_stmt_num := 95;
5215 
5216                   insert_wip_costs
5217                         (p_pac_period_id         => p_pac_period_id,
5218                          p_prior_period_id       => p_prior_period_id,
5219                          p_cost_group_id         => p_cost_group_id,
5220                          p_cost_type_id          => p_cost_type_id,
5221                          p_item_id               => c_txn_rec.item_id,
5222                          p_entity_id             => p_entity_id,
5223                          p_line_id               => p_line_id,
5224                          p_txn_id                => c_txn_rec.txn_id,
5225                          p_net_qty               => l_conv_net_qty,
5226                          p_completed_assembly_qty => l_completed_assembly_qty,
5227                          p_final_completion_flag => p_final_completion_flag,
5228                          p_start_date            => p_start_date,
5229                          p_end_date              => p_end_date,
5230                          p_user_id               => p_user_id,
5231                          p_login_id              => p_login_id,
5232                          p_request_id            => p_request_id,
5233                          p_prog_id               => p_prog_id,
5234                          p_prog_app_id           => p_prog_app_id,
5235                          x_err_num               => l_err_num,
5236                          x_err_code              => l_err_code,
5237                          x_err_msg               => l_err_msg);
5238 
5239                 IF (l_err_num <> 0) THEN
5240 
5241                         l_err_msg := SUBSTR('Txn_id: '
5242                                                 ||TO_CHAR(c_txn_rec.txn_id)
5243                                                 ||':'
5244                                                 ||l_err_msg,1,240);
5245 
5246                          RAISE CST_PROCESS_ERROR;
5247 
5248                 END IF;
5249 
5250                 l_stmt_num := 100;
5251 
5252                 check_expense_flags (
5253                                 p_item_id    => c_txn_rec.item_id,
5254                                 p_subinv     => c_txn_rec.subinv,
5255                                 p_org_id     => c_txn_rec.org_id,
5256                                 x_exp_item   => l_exp_item,
5257                                 x_exp_flag   => l_exp_flag,
5258                                 x_err_num    => l_err_num,
5259                                 x_err_code   => l_err_code,
5260                                 x_err_msg    => l_err_msg);
5261 
5262                 IF (l_err_num <> 0) THEN
5263 
5264                         l_err_msg := SUBSTR('Item_id: '
5265                                                 ||TO_CHAR(c_txn_rec.item_id)
5266                                                 ||':'
5267                                                 ||l_err_msg,1,240);
5268                         RAISE CST_PROCESS_ERROR;
5269 
5270                 END IF;
5271 
5272                 l_stmt_num := 105;
5273 
5274                 CSTPPWAC.cost_processor
5275                         (i_legal_entity         => p_legal_entity,
5276                          i_pac_period_id        => p_pac_period_id,
5277                          i_org_id               => p_org_id,
5278                          i_cost_group_id        => p_cost_group_id,
5279                          i_txn_cost_group_id    => NULL,
5280                          i_txfr_cost_group_id   => NULL,
5281                          i_cost_type_id         => p_cost_type_id,
5282                          i_cost_method          => p_cost_method,
5283                          i_process_group        => 1,
5284                          i_txn_id               => c_txn_rec.txn_id,
5285                          i_qty_layer_id         => p_qty_layer_id,
5286                          i_cost_layer_id        => p_cost_layer_id,
5287                          i_pac_rates_id         => p_pac_rates_id,
5288                          i_item_id              => c_txn_rec.item_id,
5289                          i_txn_qty              => c_txn_rec.pri_qty *
5290                                                         l_uom_conv_rate,
5291                          i_txn_action_id        => c_txn_rec.txn_action_id,
5292                          i_txn_src_type_id      => c_txn_rec.txn_src_type_id,
5293                          i_fob_point            => NULL,
5294                          i_exp_item             => l_exp_item,
5295                          i_exp_flag             => l_exp_flag,
5296                          i_cost_hook_used       => -1,
5297                          i_user_id              => p_user_id,
5298                          i_login_id             => p_login_id,
5299                          i_req_id               => p_request_id,
5300                          i_prg_appl_id          => p_prog_app_id,
5301                          i_prg_id               => p_prog_id,
5302                          i_txn_category         => p_txn_category,
5303                          o_err_num              => l_err_num,
5304                          o_err_code             => l_err_code,
5305                          o_err_msg              => l_err_msg);
5306 
5307                   IF (l_err_num <> 0) THEN
5308 
5309                         l_err_msg := SUBSTR('Txn_id: '
5310                                                 ||TO_CHAR(c_txn_rec.txn_id)
5311                                                 ||':'
5312                                                 ||l_err_msg,1,240);
5313                         RAISE CST_PROCESS_ERROR;
5314                 END IF;
5315 
5316 	      -- =================================================================
5317               -- FP 12.0-12.1.3 Bug 9280484 fix: Update periodic primary quantity
5318 	      -- with converted UOM transaction quantity
5319 	      -- =================================================================
5320               UPDATE MTL_MATERIAL_TRANSACTIONS
5321                  SET PERIODIC_PRIMARY_QUANTITY = c_txn_rec.pri_qty *  l_uom_conv_rate
5322                WHERE TRANSACTION_ID = c_txn_rec.txn_id;
5323 
5324           END LOOP; --assy_txn_loop
5325         END IF; -- check for p_scrap
5326 
5327   IF (l_pLog) THEN
5328    FND_LOG.STRING (FND_LOG.LEVEL_PROCEDURE,
5329                    l_module || '.end',
5330                    l_api_name || ' >>>');
5331   END IF;
5332 
5333 EXCEPTION
5334 
5335         WHEN CST_PROCESS_ERROR THEN
5336                 IF (l_exceptionLog) THEN
5337                    FND_LOG.STRING (FND_LOG.LEVEL_EXCEPTION,
5338                                    l_module || '.' || l_stmt_num,
5339                                    l_err_msg);
5340                 END IF;
5341                 x_err_num := l_err_num;
5342                 x_err_code := l_err_code;
5343                 x_err_msg := l_err_msg;
5344 
5345         WHEN OTHERS THEN
5346                 IF (l_uLog) THEN
5347                    FND_LOG.STRING (FND_LOG.LEVEL_UNEXPECTED,
5348                                    l_module || '.' || l_stmt_num,
5349                                    SQLERRM);
5350                 END IF;
5351                 ROLLBACK;
5352                 x_err_num := SQLCODE;
5353                 x_err_code := NULL;
5354                 x_err_msg := SUBSTR('CSTPPWAS.process_net_relief('
5355                                 || to_char(l_stmt_num)
5356                                 || '): '
5357                                 ||SQLERRM,1,240);
5358 
5359 END process_net_relief;
5360 
5361 /*---------------------------------------------------------------------------*
5362 |  PUBLIC PROCEDURE                                                          |
5363 |       process_nonreworkassembly_txns                                       |
5364 |   called for items that have nonrework completion/return/scrap txns        |
5365 |   in the period                                                            |
5366 *----------------------------------------------------------------------------*/
5367 PROCEDURE process_nonreworkassembly_txns(
5368        p_pac_period_id                  IN      NUMBER,
5369        p_start_date                     IN      DATE,
5370        p_end_date                       IN      DATE,
5371        p_prior_period_id                IN      NUMBER,
5372        p_item_id                        IN      NUMBER,
5373        p_cost_group_id                  IN      NUMBER,
5374        p_cost_type_id                   IN      NUMBER,
5375        p_legal_entity                   IN      NUMBER,
5376        p_cost_method                    IN      NUMBER,
5377        p_pac_rates_id                   IN      NUMBER,
5378        p_master_org_id                  IN      NUMBER,
5379        p_material_relief_algorithm      IN      NUMBER,
5380        p_uom_control                    IN      NUMBER,
5381        p_low_level_code                 IN      NUMBER,
5382        p_user_id                        IN      NUMBER,
5383        p_login_id                       IN      NUMBER,
5384        p_request_id                     IN      NUMBER,
5385        p_prog_id                        IN      NUMBER DEFAULT -1,
5386        p_prog_app_id                    IN      NUMBER DEFAULT -1,
5387        x_err_num                        OUT NOCOPY      NUMBER,
5388        x_err_code                       OUT NOCOPY      VARCHAR2,
5389        x_err_msg                        OUT NOCOPY      VARCHAR2)
5390 IS
5391 
5392 l_cost_method_hook              NUMBER;
5393 l_cost_layer_id                 NUMBER;
5394 l_qty_layer_id                  NUMBER;
5395 l_open_flag                     VARCHAR2(1);
5396 l_stmt_num                      NUMBER;
5397 l_pri_uom_code                  VARCHAR2(3);
5398 l_err_num                       NUMBER;
5399 l_err_code                      VARCHAR2(240);
5400 l_err_msg                       VARCHAR2(240);
5401 l_exp_flag                      NUMBER;
5402 l_exp_item                      NUMBER;
5403 cst_process_error               EXCEPTION;
5404 l_final_completion_count        NUMBER;
5405 
5406 l_api_name            CONSTANT VARCHAR2(30) := 'process_nonreworkassembly_txns';
5407 l_full_name           CONSTANT VARCHAR2(60) := G_PKG_NAME || '.' || l_api_name;
5408 l_module              CONSTANT VARCHAR2(60) := 'cst.plsql.' || l_full_name;
5409 
5410 l_uLog  CONSTANT BOOLEAN := FND_LOG.LEVEL_UNEXPECTED >= G_LOG_LEVEL AND FND_LOG.TEST (FND_LOG.LEVEL_UNEXPECTED, l_module);
5411 l_errorLog CONSTANT BOOLEAN := l_uLog AND (FND_LOG.LEVEL_ERROR >= G_LOG_LEVEL);
5412 l_exceptionLog CONSTANT BOOLEAN := l_errorLog AND (FND_LOG.LEVEL_EXCEPTION >= G_LOG_LEVEL);
5413 l_eventLog CONSTANT BOOLEAN := l_exceptionLog AND (FND_LOG.LEVEL_EVENT >= G_LOG_LEVEL);
5414 l_pLog CONSTANT BOOLEAN := l_eventLog AND (FND_LOG.LEVEL_PROCEDURE >= G_LOG_LEVEL);
5415 l_sLog CONSTANT BOOLEAN := l_pLog AND (FND_LOG.LEVEL_STATEMENT >= G_LOG_LEVEL);
5416 
5417 ------------------------------------------------------------------------------
5418 -- All relieves are based on the concept of net relieves. i.e.
5419 -- Net Relief = Completion - Return
5420 -- Job information is also built on the concept of net units earned i.e.
5421 -- if you complete 10 units from Op 10 and Return 5 units to Op 30 then,
5422 -- the net relief logic uses:
5423 -- net_qty = 10-5 = 5
5424 -- Resource earned at Op 10 = 10
5425 -- Resource earned at Op 30 = 5
5426 -- Its possible then the completion is done at period P1 where the PAC
5427 -- rates (say for a resource) were lower than the period (P2) in which the
5428 -- net return was done.   Net return is always done based on prior period's
5429 -- values.
5430 ------------------------------------------------------------------------------
5431 
5432 ------------------------------------------------------------------------------
5433 -- This cursor will give the net scrap qty for all wip_entity/line that :-
5434 --      1. Had scrap transaction(s) in this period
5435 --      2. AND entity belongs to one of the memeber organizations
5436 --      3. AND entity has a record in wppb i.e. has some value
5437 --      4. AND entity is a non-rework job
5438 ------------------------------------------------------------------------------
5439 
5440        CURSOR  c_non_rework_entity_scrap  IS
5441        SELECT  mmt.transaction_source_id entity_id,
5442                mmt.organization_id org_id,
5443                we.entity_type entity_type,
5444                mmt.repetitive_line_id line_id,
5445                mmt.operation_seq_num op_seq,
5446                SUM(mmt.primary_quantity) net_scrap
5447        FROM    mtl_material_transactions mmt,
5448                cst_cost_group_assignments ccga,
5449                wip_entities we
5450        WHERE   mmt.inventory_item_id = p_item_id
5451        AND     mmt.transaction_date BETWEEN TRUNC(p_start_date)
5452                                     AND (TRUNC(p_end_date) + 0.99999)
5453        AND     mmt.transaction_source_type_id = 5
5454        AND     mmt.transaction_action_id  = 30
5455        AND     mmt.organization_id = ccga.organization_id
5456        AND     ccga.cost_group_id = p_cost_group_id
5457        AND     we.wip_entity_id = mmt.transaction_source_id
5458        AND     we.organization_id = mmt.organization_id
5459        AND     NOT EXISTS  (SELECT 1
5460                             FROM   mtl_material_transactions mmt1
5461                             WHERE  mmt1.inventory_item_id = we.primary_item_id
5462                             AND    mmt1.transaction_source_id = we.wip_entity_id
5463                             AND    mmt1.organization_id = we.organization_id
5464                             AND    mmt1.transaction_source_type_id = 5
5465                             AND    mmt1.transaction_action_id in (1,27,33,34)
5466                             AND    mmt1.transaction_date BETWEEN TRUNC(p_start_date)
5467                                                       AND     (TRUNC (p_end_date) + 0.99999))
5468        GROUP BY
5469                mmt.transaction_source_id,
5470                mmt.organization_id,
5471                we.entity_type,
5472                mmt.repetitive_line_id,
5473                mmt.operation_seq_num
5474         ORDER BY
5475                 SUM(mmt.primary_quantity) DESC, -- minimize the occurences of negative periodic inventory quantity
5476                 mmt.transaction_source_id;      -- a consistent tie breaker
5477 
5478 
5479 ------------------------------------------------------------------------------
5480 -- This cursor will give the net compl/ret qty for all wip_entity/line that :-
5481 --      1. Had completion/return transaction(s) in this period
5482 --      2. AND entity belongs to one of the memeber organizations
5483 --      3. AND entity has a record in wppb i.e. has some value
5484 --      4. AND entity is a non-rework job
5485 ------------------------------------------------------------------------------
5486 
5487        CURSOR   c_non_rework_entity_complete  IS
5488        SELECT   mmt.transaction_source_id entity_id,
5489                 mmt.organization_id org_id,
5490                 we.entity_type entity_type,
5491                 mmt.repetitive_line_id line_id,
5492                 SUM(mmt.primary_quantity) net_completion
5493        FROM     mtl_material_transactions mmt,
5494                 cst_cost_group_assignments ccga,
5495                 wip_entities we
5496        WHERE    mmt.inventory_item_id = p_item_id
5497        AND      mmt.transaction_date BETWEEN TRUNC(p_start_date)
5498                                     AND (TRUNC(p_end_date) + 0.99999)
5499        AND      mmt.transaction_source_type_id = 5
5500        AND      mmt.transaction_action_id IN (31,32)
5501        AND      mmt.organization_id = ccga.organization_id
5502        AND      ccga.cost_group_id = p_cost_group_id
5503        AND      we.wip_entity_id = mmt.transaction_source_id
5504        AND      we.organization_id = mmt.organization_id
5505        AND      NOT EXISTS (SELECT 1
5506                             FROM   mtl_material_transactions mmt1
5507                             WHERE  mmt1.inventory_item_id = we.primary_item_id
5508                             AND    mmt1.transaction_source_id = we.wip_entity_id
5509                             AND    mmt1.organization_id = we.organization_id
5510                             AND    mmt1.transaction_source_type_id = 5
5511                             AND    mmt1.transaction_action_id in (1,27,33,34)
5512                             AND    mmt1.transaction_date BETWEEN TRUNC(p_start_date)
5513                                                              AND (TRUNC (p_end_date) + 0.99999))
5514         /* R12 PAC Enhancements for China and Taiwan: Exclude eAM entities as rebuildable jobs can be
5515            completed only in expense subinventories at 0 cost so they should not be costed and no
5516            distributions created */
5517         AND     we.entity_type not in (6,7)
5518        GROUP BY
5519                mmt.transaction_source_id,
5520                mmt.organization_id,
5521                we.entity_type,
5522                mmt.repetitive_line_id
5523        ORDER BY
5524                 SUM(mmt.primary_quantity) DESC, -- minimize the occurences of negative periodic inventory quantity
5525                 mmt.transaction_source_id;      -- a consistent tie breaker
5526 
5527 
5528 BEGIN
5529 
5530        IF (l_pLog) THEN
5531         FND_LOG.STRING (FND_LOG.LEVEL_PROCEDURE,
5532                         l_module || '.begin',
5533                         l_api_name || ' <<< Parameters:
5534                         p_pac_period_id = ' || p_pac_period_id || '
5535                         p_prior_period_id = ' || p_prior_period_id || '
5536                         p_item_id = ' || p_item_id || '
5537                         p_low_level_code = ' || p_low_level_code || '
5538 			p_material_relief_algorithm = '|| p_material_relief_algorithm || '
5539                         p_cost_type_id = ' || p_cost_type_id );
5540 
5541        END IF;
5542 
5543        ----------------------------------------------------------------------
5544        -- Initialize Variables
5545        ----------------------------------------------------------------------
5546 
5547        l_err_num := 0;
5548        l_err_code := '';
5549        l_err_msg := '';
5550        l_cost_layer_id := 0;
5551        l_qty_layer_id := 0;
5552        l_cost_method_hook := -1;
5553 
5554        ----------------------------------------------------------------------
5555        -- Check and Create layer for the assembly, if required
5556        ----------------------------------------------------------------------
5557 
5558        l_stmt_num := 5;
5559        CSTPPCLM.layer_id (
5560                        i_pac_period_id => p_pac_period_id,
5561                        i_legal_entity  => p_legal_entity,
5562                        i_item_id => p_item_id,
5563                        i_cost_group_id => p_cost_group_id,
5564                        o_cost_layer_id => l_cost_layer_id,
5565                        o_quantity_layer_id => l_qty_layer_id,
5566                        o_err_num => l_err_num,
5567                        o_err_code => l_err_code,
5568                        o_err_msg  => l_err_msg);
5569 
5570        IF (l_err_num <> 0) THEN
5571                RAISE CST_PROCESS_ERROR;
5572        END IF;
5573 
5574 
5575        IF (l_cost_layer_id = 0 AND l_qty_layer_id = 0) THEN
5576        l_stmt_num := 10;
5577        CSTPPCLM.create_layer (
5578                        i_pac_period_id  => p_pac_period_id,
5579                        i_legal_entity   => p_legal_entity,
5580                        i_item_id        => p_item_id,
5581                        i_cost_group_id  => p_cost_group_id,
5582                        i_user_id        => p_user_id,
5583                        i_login_id       => p_login_id,
5584                        i_request_id     => p_request_id,
5585                        i_prog_id        => p_prog_id,
5586                        i_prog_appl_id   => p_prog_app_id,
5587                        o_cost_layer_id  => l_cost_layer_id,
5588                        o_quantity_layer_id => l_qty_layer_id,
5589                        o_err_num        => l_err_num,
5590                        o_err_code       => l_err_code,
5591                        o_err_msg        => l_err_msg);
5592 
5593                IF (l_err_num <> 0) THEN
5594                        RAISE CST_PROCESS_ERROR;
5595                END IF;
5596        END IF; -- Check Create Layer
5597 
5598 
5599       FOR c_ent_rec IN c_non_rework_entity_scrap LOOP
5600 
5601            fnd_file.put_line(fnd_file.log,' Scrap_Qty Update << ');
5602            l_stmt_num := 15;
5603 
5604            UPDATE WIP_PAC_PERIOD_BALANCES
5605            SET    relieved_scrap_qty = nvl(relieved_scrap_qty,0) + c_ent_rec.net_scrap
5606            WHERE  wip_entity_id =  c_ent_rec.entity_id
5607            AND    nvl(line_id,-99) = decode(wip_entity_type, 4, -99, nvl(c_ent_rec.line_id,-99))
5608            AND    operation_seq_num <= c_ent_rec.op_seq
5609            AND    cost_type_id =  p_cost_type_id
5610            AND    pac_period_id = p_pac_period_id
5611            AND    cost_group_id = p_cost_group_id;
5612 
5613       END LOOP;
5614 
5615 
5616 
5617 
5618     --------------------------------------------------------------------------
5619     -- Process scrap transactions for non-rework jobs
5620     -- If number of records exceeds threshold value, insert them into
5621     -- CST_PAC_PERIOD_BALANCES and clear the PL/SQL tables.
5622     --------------------------------------------------------------------------
5623 
5624        FOR c_ent_rec IN c_non_rework_entity_scrap LOOP
5625        l_stmt_num := 20;
5626         IF (CSTPPINV.l_item_id_tbl.COUNT >= G_MAX_RECORDS AND p_cost_method <> 4) THEN
5627           CSTPPWAC.insert_into_cppb(i_pac_period_id   =>  p_pac_period_id,
5628                                     i_cost_group_id   =>  p_cost_group_id,
5629                                     i_txn_category    =>  4,        /* Non-rework completions */
5630                                     i_user_id         =>  p_user_id,
5631                                     i_login_id        =>  p_login_id,
5632                                     i_request_id      =>  p_request_id,
5633                                     i_prog_id         =>  p_prog_id,
5634                                     i_prog_appl_id    =>  p_prog_app_id,
5635                                     o_err_num         =>  l_err_num,
5636                                     o_err_code        =>  l_err_code,
5637                                     o_err_msg         =>  l_err_msg);
5638           IF (l_err_num <> 0) THEN
5639               l_err_msg := SUBSTR('CSTPPWAS.process_nonreworkassembly_txns('
5640                                 ||TO_CHAR(l_stmt_num)
5641                                 ||'):'
5642                                 ||l_err_msg,1,240);
5643               RAISE CST_PROCESS_ERROR;
5644           END IF;
5645         END IF;
5646 
5647        l_stmt_num := 25;
5648        process_net_relief       (
5649                p_pac_period_id             => p_pac_period_id,
5650                p_prior_period_id           => p_prior_period_id,
5651                p_cost_group_id             => p_cost_group_id,
5652                p_cost_type_id              => p_cost_type_id,
5653                p_legal_entity              => p_legal_entity,
5654                p_cost_method               => p_cost_method,
5655                p_cost_layer_id             => l_cost_layer_id,
5656                p_qty_layer_id              => l_qty_layer_id,
5657                p_pac_rates_id              => p_pac_rates_id,
5658                p_entity_type               => c_ent_rec.entity_type,
5659                p_org_id                    => c_ent_rec.org_id,
5660                p_entity_id                 => c_ent_rec.entity_id,
5661                p_line_id                   => c_ent_rec.line_id,
5662                p_net_qty                   =>  c_ent_rec.net_scrap,
5663                p_start_date                => p_start_date,
5664                p_end_date                  => p_end_date,
5665                p_scrap                     => 1,
5666                p_op_seq                    => c_ent_rec.op_seq,
5667                p_final_completion_flag     => NULL,
5668                p_material_relief_algorithm => p_material_relief_algorithm,
5669                p_master_org_id             => p_master_org_id,
5670                p_uom_control               => p_uom_control,
5671                p_user_id                   => p_user_id,
5672                p_login_id                  => p_login_id,
5673                p_request_id                => p_request_id,
5674                p_prog_id                   => p_prog_id,
5675                p_prog_app_id               => p_prog_app_id,
5676                p_txn_category              => 4,  -- txn_category = 4 for non-rework completions/scrap
5677                x_err_num                   => l_err_num,
5678                x_err_code                  => l_err_code,
5679                x_err_msg                   => l_err_msg);
5680 
5681                IF (l_err_num <>0) THEN
5682                        l_err_msg := SUBSTR('scrap entity/line: '
5683                                        ||TO_CHAR(c_ent_rec.entity_id)
5684                                        ||'/'
5685                                        ||TO_CHAR(c_ent_rec.line_id)
5686                                        ||':'
5687                                        ||l_err_msg,1,240);
5688                        RAISE CST_PROCESS_ERROR;
5689                END IF;
5690        END LOOP; --c_non_rework_entity_scrap
5691 
5692     --------------------------------------------------------------------------
5693     -- Process completions transactions for non-rework jobs
5694     -- If number of records exceeds threshold value, insert them into
5695     -- CST_PAC_PERIOD_BALANCES and clear the PL/SQL tables.
5696     --------------------------------------------------------------------------
5697 
5698        l_stmt_num := 30;
5699        FOR c_ent_rec IN c_non_rework_entity_complete LOOP
5700        l_stmt_num := 35;
5701 
5702        SELECT count(*)
5703        INTO   l_final_completion_count
5704        FROM   mtl_material_transactions mmt
5705        WHERE  mmt.transaction_source_id = c_ent_rec.entity_id
5706        AND    nvl(mmt.repetitive_line_id,-99) = nvl( c_ent_rec.line_id,-99)
5707        /* Bug 12703830 */
5708        AND    mmt.transaction_action_id = 31
5709        AND    mmt.final_completion_flag = 'Y'
5710        AND    mmt.transaction_date BETWEEN TRUNC(p_start_date)
5711                                        AND (TRUNC(p_end_date) + 0.99999)
5712        AND    ROWNUM < 2;
5713 
5714         IF (CSTPPINV.l_item_id_tbl.COUNT >= G_MAX_RECORDS AND p_cost_method <> 4) THEN
5715           l_stmt_num := 40;
5716           CSTPPWAC.insert_into_cppb(i_pac_period_id   =>  p_pac_period_id,
5717                                     i_cost_group_id   =>  p_cost_group_id,
5718                                     i_txn_category    =>  4,   /* Non-rework completions */
5719                                     i_user_id         =>  p_user_id,
5720                                     i_login_id        =>  p_login_id,
5721                                     i_request_id      =>  p_request_id,
5722                                     i_prog_id         =>  p_prog_id,
5723                                     i_prog_appl_id    =>  p_prog_app_id,
5724                                     o_err_num         =>  l_err_num,
5725                                     o_err_code        =>  l_err_code,
5726                                     o_err_msg         =>  l_err_msg);
5727           IF (l_err_num <> 0) THEN
5728               l_err_msg := SUBSTR('CSTPPWAS.process_nonreworkassembly_txns('
5729                                 ||TO_CHAR(l_stmt_num)
5730                                 ||'):'
5731                                 ||l_err_msg,1,240);
5732               RAISE CST_PROCESS_ERROR;
5733           END IF;
5734         END IF;
5735 
5736        l_stmt_num := 45;
5737        process_net_relief       (
5738                p_pac_period_id              => p_pac_period_id,
5739                p_prior_period_id            => p_prior_period_id,
5740                p_cost_group_id              => p_cost_group_id,
5741                p_cost_type_id               => p_cost_type_id,
5742                p_legal_entity               => p_legal_entity,
5743                p_cost_method                => p_cost_method,
5744                p_cost_layer_id              => l_cost_layer_id,
5745                p_qty_layer_id               => l_qty_layer_id,
5746                p_pac_rates_id               => p_pac_rates_id,
5747                p_entity_type                => c_ent_rec.entity_type,
5748                p_org_id                     => c_ent_rec.org_id,
5749                p_entity_id                  => c_ent_rec.entity_id,
5750                p_line_id                    => c_ent_rec.line_id,
5751                p_net_qty                    => c_ent_rec.net_completion,
5752                p_start_date                 => p_start_date,
5753                p_end_date                   => p_end_date,
5754                p_scrap                      => -1,
5755                p_op_seq                     => NULL,
5756                p_final_completion_flag      => l_final_completion_count,
5757                p_material_relief_algorithm  => p_material_relief_algorithm,
5758                p_master_org_id              => p_master_org_id,
5759                p_uom_control                => p_uom_control,
5760                p_user_id                    => p_user_id,
5761                p_login_id                   => p_login_id,
5762                p_request_id                 => p_request_id,
5763                p_prog_id                    => p_prog_id,
5764                p_prog_app_id                => p_prog_app_id,
5765                p_txn_category               => 4,  -- txn_category = 4 for non-rework completions/scrap
5766                x_err_num                    => l_err_num,
5767                x_err_code                   => l_err_code,
5768                x_err_msg                    => l_err_msg);
5769 
5770                IF (l_err_num <>0) THEN
5771                  l_err_msg := SUBSTR('cmpl entity/line: '
5772                                  ||TO_CHAR(c_ent_rec.entity_id)
5773                                  ||'/'
5774                                  ||TO_CHAR(c_ent_rec.line_id)
5775                                  ||':'
5776                                  ||l_err_msg,1,240);
5777                  RAISE CST_PROCESS_ERROR;
5778                END IF;
5779        END LOOP; --c_non_rework_entity_complete
5780 
5781       -- Flush the remaining records from PL/SQL tables.
5782       l_stmt_num := 50;
5783       IF (CSTPPINV.l_item_id_tbl.COUNT > 0 AND p_cost_method <> 4) THEN
5784           CSTPPWAC.insert_into_cppb(i_pac_period_id   =>  p_pac_period_id,
5785                                     i_cost_group_id   =>  p_cost_group_id,
5786                                     i_txn_category    =>  4,  /* Non-rework completions */
5787                                     i_user_id         =>  p_user_id,
5788                                     i_login_id        =>  p_login_id,
5789                                     i_request_id      =>  p_request_id,
5790                                     i_prog_id         =>  p_prog_id,
5791                                     i_prog_appl_id    =>  p_prog_app_id,
5792                                     o_err_num         =>  l_err_num,
5793                                     o_err_code        =>  l_err_code,
5794                                     o_err_msg         =>  l_err_msg);
5795 
5796           IF (l_err_num <> 0) THEN
5797               l_err_msg := SUBSTR('CSTPPWAS.process_nonreworkassembly_txns('
5798                                 ||TO_CHAR(l_stmt_num)
5799                                 ||'):'
5800                                 ||l_err_msg,1,240);
5801               RAISE CST_PROCESS_ERROR;
5802           END IF;
5803        END IF;
5804 
5805        IF (p_cost_method <> 4) THEN
5806 
5807 	 l_stmt_num := 55;
5808          /* Bug 11834257: Use update_item_cppb instead of update_cppb */
5809          CSTPPWAC.update_item_cppb(i_pac_period_id  =>  p_pac_period_id,
5810                               i_cost_group_id  =>  p_cost_group_id,
5811                               i_txn_category   =>  4,    /* Non-rework Completions */
5812                               i_item_id         =>  p_item_id,
5813                               i_user_id        =>  p_user_id,
5814                               i_login_id       =>  p_login_id,
5815                               i_request_id     =>  p_request_id,
5816                               i_prog_id        =>  p_prog_id,
5817                               i_prog_appl_id   =>  p_prog_app_id,
5818                               o_err_num        =>  l_err_num,
5819                               o_err_code       =>  l_err_code,
5820                               o_err_msg        =>  l_err_msg);
5821 
5822 	 /*CSTPPWAC.update_cppb(i_pac_period_id  =>  p_pac_period_id,
5823                               i_cost_group_id  =>  p_cost_group_id,
5824                               i_txn_category   =>  4,    /* Non-rework Completions */
5825                             /*i_low_level_code =>  p_low_level_code,
5826                               i_user_id        =>  p_user_id,
5827                               i_login_id       =>  p_login_id,
5828                               i_request_id     =>  p_request_id,
5829                               i_prog_id        =>  p_prog_id,
5830                               i_prog_appl_id   =>  p_prog_app_id,
5831                               o_err_num        =>  l_err_num,
5832                               o_err_code       =>  l_err_code,
5833                               o_err_msg        =>  l_err_msg);*/
5834 
5835 
5836          IF (l_err_num <> 0) THEN
5837             l_err_msg := SUBSTR('CSTPPWAS.process_reworkassembly_txns('
5838                                   || TO_CHAR(l_stmt_num)
5839                                   ||'):'
5840                                   ||l_err_msg,1,240);
5841              RAISE CST_PROCESS_ERROR;
5842          END IF;
5843        END IF;
5844 
5845   IF (l_pLog) THEN
5846    FND_LOG.STRING (FND_LOG.LEVEL_PROCEDURE,
5847                    l_module || '.end',
5848                    l_api_name || ' >>>');
5849   END IF;
5850 
5851 EXCEPTION
5852 
5853        WHEN CST_PROCESS_ERROR THEN
5854                IF (l_exceptionLog) THEN
5855                   FND_LOG.STRING (FND_LOG.LEVEL_EXCEPTION,
5856                                   l_module || '.' || l_stmt_num,
5857                                   l_err_msg);
5858                END IF;
5859                x_err_num := l_err_num;
5860                x_err_code := l_err_code;
5861                x_err_msg := SUBSTR(l_err_msg,1,240);
5862 
5863        WHEN OTHERS THEN
5864                IF (l_uLog) THEN
5865                   FND_LOG.STRING (FND_LOG.LEVEL_UNEXPECTED,
5866                                   l_module || '.' || l_stmt_num,
5867                                   SQLERRM);
5868                END IF;
5869                ROLLBACK;
5870                x_err_num := SQLCODE;
5871                x_err_code := NULL;
5872                x_err_msg := SUBSTR('CSTPPWAS.process_nonreworkassembly_txns ('
5873                                || to_char(l_stmt_num)
5874                                || '): '
5875                                ||SQLERRM,1,240);
5876 
5877 END process_nonreworkassembly_txns;
5878 
5879 /*---------------------------------------------------------------------------*
5880 |  PUBLIC PROCEDURE                                                          |
5881 |       process_reworkassembly_txns                                          |
5882 |  Called for items that have rework completion/return/scrap txns            |
5883 |  in the period                                                             |
5884 *----------------------------------------------------------------------------*/
5885 PROCEDURE process_reworkassembly_txns(
5886        p_pac_period_id                  IN      NUMBER,
5887        p_start_date                     IN      DATE,
5888        p_end_date                       IN      DATE,
5889        p_prior_period_id                IN      NUMBER,
5890        p_item_id                        IN      NUMBER,
5891        p_cost_group_id                  IN      NUMBER,
5892        p_cost_type_id                   IN      NUMBER,
5893        p_legal_entity                   IN      NUMBER,
5894        p_cost_method                    IN      NUMBER,
5895        p_pac_rates_id                   IN      NUMBER,
5896        p_master_org_id                  IN      NUMBER,
5897        p_material_relief_algorithm      IN      NUMBER,
5898        p_uom_control                    IN      NUMBER,
5899        p_low_level_code                 IN      NUMBER,
5900        p_user_id                        IN      NUMBER,
5901        p_login_id                       IN      NUMBER,
5902        p_request_id                     IN      NUMBER,
5903        p_prog_id                        IN      NUMBER DEFAULT -1,
5904        p_prog_app_id                    IN      NUMBER DEFAULT -1,
5905        x_err_num                        OUT NOCOPY      NUMBER,
5906        x_err_code                       OUT NOCOPY      VARCHAR2,
5907        x_err_msg                        OUT NOCOPY      VARCHAR2)
5908 IS
5909 l_cost_method_hook              NUMBER;
5910 l_cost_layer_id                 NUMBER;
5911 l_qty_layer_id                  NUMBER;
5912 l_open_flag                     VARCHAR2(1);
5913 l_stmt_num                      NUMBER;
5914 l_pri_uom_code                  VARCHAR2(3);
5915 l_err_num                       NUMBER;
5916 l_err_code                      VARCHAR2(240);
5917 l_err_msg                       VARCHAR2(240);
5918 l_exp_flag                      NUMBER;
5919 l_exp_item                      NUMBER;
5920 cst_process_error               EXCEPTION;
5921 l_final_completion_count        NUMBER;
5922 l_co_txns_count                 NUMBER;
5923 
5924 l_api_name            CONSTANT VARCHAR2(30) := 'process_reworkassembly_txns';
5925 l_full_name           CONSTANT VARCHAR2(60) := G_PKG_NAME || '.' || l_api_name;
5926 l_module              CONSTANT VARCHAR2(60) := 'cst.plsql.' || l_full_name;
5927 
5928 l_uLog  CONSTANT BOOLEAN := FND_LOG.LEVEL_UNEXPECTED >= G_LOG_LEVEL AND FND_LOG.TEST (FND_LOG.LEVEL_UNEXPECTED, l_module);
5929 l_errorLog CONSTANT BOOLEAN := l_uLog AND (FND_LOG.LEVEL_ERROR >= G_LOG_LEVEL);
5930 l_exceptionLog CONSTANT BOOLEAN := l_errorLog AND (FND_LOG.LEVEL_EXCEPTION >= G_LOG_LEVEL);
5931 l_eventLog CONSTANT BOOLEAN := l_exceptionLog AND (FND_LOG.LEVEL_EVENT >= G_LOG_LEVEL);
5932 l_pLog CONSTANT BOOLEAN := l_eventLog AND (FND_LOG.LEVEL_PROCEDURE >= G_LOG_LEVEL);
5933 l_sLog CONSTANT BOOLEAN := l_pLog AND (FND_LOG.LEVEL_STATEMENT >= G_LOG_LEVEL);
5934 
5935 ------------------------------------------------------------------------------
5936 -- This cursor will get all the WIP Component txns where this item
5937 -- is issued to a WIP entity building itself.(Non Standard Job-Rework)
5938 ------------------------------------------------------------------------------
5939 
5940        CURSOR   c_rework_issue_txns IS
5941        SELECT   mmt.transaction_id txn_id,
5942                 mmt.transaction_source_id entity_id,
5943                 mmt.repetitive_line_id line_id,
5944                 mmt.primary_quantity pri_qty,
5945                 mmt.inventory_item_id item_id,
5946                 mmt.operation_seq_num op_seq,
5947                 mmt.organization_id org_id
5948        FROM     mtl_material_transactions mmt,
5949                 wip_entities we,
5950                 cst_cost_group_assignments ccga /* bug3930450 - Added ccga join to process txns for a particular cost group */
5951        WHERE    mmt.transaction_date BETWEEN TRUNC(p_start_date)
5952                                     AND (TRUNC(p_end_date) + 0.99999)
5953        AND      ccga.cost_group_id = p_cost_group_id /* bug3930450 */
5954        AND      ccga.organization_id = mmt.organization_id /* bug3930450 */
5955        AND      mmt.transaction_source_type_id = 5
5956        AND      mmt.transaction_action_id IN (1,27,33,34)
5957        AND      mmt.inventory_item_id = p_item_id
5958        AND      we.wip_entity_id = mmt.transaction_source_id
5959        AND      we.organization_id = mmt.organization_id
5960        AND      NVL(we.primary_item_id,-1) = mmt.inventory_item_id;
5961 
5962 -----------------------------------------------------------------------------
5963 -- This cursor will give the net scrap qty for all wip_entity/line that :-
5964 --      1. Had scrap transaction(s) in this period
5965 --      2. AND entity belongs to one of the memeber organizations
5966 --      3. AND entity has a record in wppb i.e. has some value
5967 --      4. AND entity is a rework job
5968 ------------------------------------------------------------------------------
5969 
5970        CURSOR   c_rework_entity_scrap  IS
5971        SELECT   mmt.transaction_source_id entity_id,
5972                 mmt.organization_id org_id,
5973                 we.entity_type entity_type,
5974                 mmt.repetitive_line_id line_id,
5975                 mmt.operation_seq_num op_seq,
5976                 SUM(mmt.primary_quantity) net_scrap
5977        FROM     mtl_material_transactions mmt,
5978                 cst_cost_group_assignments ccga,
5979                 wip_entities we
5980        WHERE    mmt.inventory_item_id = p_item_id
5981        AND      mmt.transaction_date BETWEEN TRUNC(p_start_date)
5982                                     AND (TRUNC(p_end_date) + 0.99999)
5983        AND      mmt.transaction_source_type_id = 5
5984        AND      mmt.transaction_action_id  = 30
5985        AND      mmt.organization_id = ccga.organization_id
5986        AND      ccga.cost_group_id = p_cost_group_id
5987        AND      we.wip_entity_id = mmt.transaction_source_id
5988        AND      we.organization_id = mmt.organization_id
5989        AND      EXISTS (SELECT  1
5990                         FROM    mtl_material_transactions mmt1
5991                         WHERE   mmt1.inventory_item_id = we.primary_item_id
5992                         AND     mmt1.transaction_source_id = we.wip_entity_id
5993                         AND     mmt1.organization_id = we.organization_id
5994                         AND     mmt1.transaction_source_type_id = 5
5995                         AND     mmt1.transaction_action_id in (1,27,33,34)
5996                         AND     mmt1.transaction_date BETWEEN TRUNC(p_start_date)
5997                                            AND    (TRUNC (p_end_date) + 0.99999))
5998        GROUP BY
5999                mmt.transaction_source_id,
6000                mmt.organization_id,
6001                we.entity_type,
6002                mmt.repetitive_line_id,
6003                mmt.operation_seq_num
6004         ORDER BY
6005                 SUM(mmt.primary_quantity) DESC, -- minimize the occurences of negative periodic inventory quantity
6006                 mmt.transaction_source_id;      -- a consistent tie breaker
6007 
6008 -----------------------------------------------------------------------------
6009 -- This cursor will give the net compl/ret qty for all wip_entity/line that :-
6010 --      1. Had completion/return transaction(s) in this period
6011 --      2. AND entity belongs to one of the memeber organizations
6012 --      3. AND entity has a record in wppb i.e. has some value
6013 --      4. AND entity is a rework job
6014 ------------------------------------------------------------------------------
6015 
6016        CURSOR   c_rework_entity_complete  IS
6017        SELECT   mmt.transaction_source_id entity_id,
6018                 mmt.organization_id org_id,
6019                 we.entity_type entity_type,
6020                 mmt.repetitive_line_id line_id,
6021                 SUM(mmt.primary_quantity) net_completion
6022        FROM     mtl_material_transactions mmt,
6023                 cst_cost_group_assignments ccga,
6024                 wip_entities we
6025        WHERE    mmt.inventory_item_id = p_item_id
6026        AND      mmt.transaction_date BETWEEN TRUNC(p_start_date)
6027                                     AND (TRUNC(p_end_date) + 0.99999)
6028        AND      mmt.transaction_source_type_id = 5
6029        AND      mmt.transaction_action_id IN (31,32)
6030        AND      mmt.organization_id = ccga.organization_id
6031        AND      ccga.cost_group_id = p_cost_group_id
6032        AND      we.wip_entity_id = mmt.transaction_source_id
6033        AND      we.organization_id = mmt.organization_id
6034        AND      EXISTS(SELECT   1
6035                        FROM     mtl_material_transactions mmt1
6036                        WHERE    mmt1.inventory_item_id = we.primary_item_id
6037                        AND      mmt1.transaction_source_id = we.wip_entity_id
6038                        AND      mmt1.organization_id = we.organization_id
6039                        AND      mmt1.transaction_source_type_id = 5
6040                        AND      mmt1.transaction_action_id in (1,27,33,34)
6041                        AND      mmt1.transaction_date BETWEEN TRUNC(p_start_date)
6042                                                     AND (TRUNC (p_end_date) + 0.99999))
6043         /* Exclude eAM entities as only rebuildable jobs can be completed only
6044         in expense subinventories at 0 cost so they should not be costed and no
6045         distributions created */
6046         AND     we.entity_type not in (6,7)
6047        GROUP BY
6048                mmt.transaction_source_id,
6049                mmt.organization_id,
6050                we.entity_type,
6051                mmt.repetitive_line_id
6052        ORDER BY
6053                 SUM(mmt.primary_quantity) DESC, -- minimize the occurences of negative periodic inventory quantity
6054                 mmt.transaction_source_id;      -- a consistent tie breaker
6055 
6056 BEGIN
6057 
6058        IF (l_pLog) THEN
6059 
6060         FND_LOG.STRING (FND_LOG.LEVEL_PROCEDURE,
6061                         l_module || '.begin',
6062                         l_api_name || ' <<< Parameters:
6063                         p_pac_period_id = ' || p_pac_period_id || '
6064                         p_prior_period_id = ' || p_prior_period_id || '
6065                         p_item_id = ' || p_item_id || '
6066                         p_low_level_code = ' || p_low_level_code || '
6067 			p_material_relief_algorithm = '|| p_material_relief_algorithm || '
6068                         p_cost_type_id = ' || p_cost_type_id );
6069         END IF;
6070 
6071        ----------------------------------------------------------------------
6072        -- Initialize Variables
6073        ----------------------------------------------------------------------
6074 
6075        l_err_num := 0;
6076        l_err_code := '';
6077        l_err_msg := '';
6078        l_cost_layer_id := 0;
6079        l_qty_layer_id := 0;
6080        l_cost_method_hook := -1;
6081        l_co_txns_count := 0;
6082        ----------------------------------------------------------------------
6083        -- Check and Create layer for the assembly, if required
6084        ----------------------------------------------------------------------
6085 
6086        l_stmt_num := 5;
6087 
6088        CSTPPCLM.layer_id (
6089                        i_pac_period_id => p_pac_period_id,
6090                        i_legal_entity  => p_legal_entity,
6091                        i_item_id => p_item_id,
6092                        i_cost_group_id => p_cost_group_id,
6093                        o_cost_layer_id => l_cost_layer_id,
6094                        o_quantity_layer_id => l_qty_layer_id,
6095                        o_err_num => l_err_num,
6096                        o_err_code => l_err_code,
6097                        o_err_msg  => l_err_msg);
6098 
6099        IF (l_err_num <> 0) THEN
6100                RAISE CST_PROCESS_ERROR;
6101        END IF;
6102 
6103 
6104        IF (l_cost_layer_id = 0 AND l_qty_layer_id = 0) THEN
6105 
6106        l_stmt_num := 10;
6107 
6108        CSTPPCLM.create_layer (
6109                        i_pac_period_id => p_pac_period_id,
6110                        i_legal_entity  => p_legal_entity,
6111                        i_item_id        => p_item_id,
6112                        i_cost_group_id => p_cost_group_id,
6113                        i_user_id        => p_user_id,
6114                        i_login_id       => p_login_id,
6115                        i_request_id     => p_request_id,
6116                        i_prog_id        => p_prog_id,
6117                        i_prog_appl_id   => p_prog_app_id,
6118                        o_cost_layer_id  => l_cost_layer_id,
6119                        o_quantity_layer_id => l_qty_layer_id,
6120                        o_err_num        => l_err_num,
6121                        o_err_code       => l_err_code,
6122                        o_err_msg        => l_err_msg);
6123 
6124                IF (l_err_num <> 0) THEN
6125                        RAISE CST_PROCESS_ERROR;
6126                END IF;
6127        END IF; -- Check Create Layer
6128 
6129        l_stmt_num := 15;
6130        FOR c_rework_rec IN c_rework_issue_txns LOOP
6131 
6132         IF (CSTPPINV.l_item_id_tbl.COUNT >= G_MAX_RECORDS AND p_cost_method <> 4) THEN
6133           l_stmt_num := 20;
6134           CSTPPWAC.insert_into_cppb(i_pac_period_id  =>  p_pac_period_id,
6135                                     i_cost_group_id  =>  p_cost_group_id,
6136                                     i_txn_category   =>  6,      /* Rework Issues */
6137                                     i_user_id        =>  p_user_id,
6138                                     i_login_id       =>  p_login_id,
6139                                     i_request_id     =>  p_request_id,
6140                                     i_prog_id        =>  p_prog_id,
6141                                     i_prog_appl_id   =>  p_prog_app_id,
6142                                     o_err_num        =>  l_err_num,
6143                                     o_err_code       =>  l_err_code,
6144                                     o_err_msg        =>  l_err_msg);
6145 
6146           IF (l_err_num <> 0) THEN
6147              l_err_msg := SUBSTR('CSTPPWAS.process_reworkassembly_txns('
6148                                 ||TO_CHAR(l_stmt_num)
6149                                 ||'):'
6150                                 ||l_err_msg,1,240);
6151               RAISE CST_PROCESS_ERROR;
6152           END IF;
6153         END IF;
6154 
6155        l_stmt_num := 25;
6156        CSTPPWMT.charge_wip_material (
6157                        p_pac_period_id              => p_pac_period_id,
6158                        p_cost_group_id              => p_cost_group_id,
6159                        p_txn_id                     => c_rework_rec.txn_id,
6160                        p_exp_item                   => NULL,
6161                        p_exp_flag                   => NULL,
6162                        p_legal_entity               => p_legal_entity,
6163                        p_cost_type_id               => p_cost_type_id,
6164                        p_cost_method                => p_cost_method,
6165                        p_pac_rates_id               => p_pac_rates_id,
6166                        p_master_org_id              => p_master_org_id,
6167                        p_material_relief_algorithm  => p_material_relief_algorithm,
6168                        p_uom_control                => p_uom_control,
6169                        p_user_id                    => p_user_id,
6170                        p_login_id                   => p_login_id,
6171                        p_request_id                 => p_request_id,
6172                        p_prog_id                    => p_prog_id,
6173                        p_prog_app_id                => p_prog_app_id,
6174                        p_txn_category               => 6,        /* Rework issues */
6175                        x_cost_method_hook           => l_cost_method_hook,
6176                        x_err_num                    => l_err_num,
6177                        x_err_code                   => l_err_code,
6178                        x_err_msg                    => l_err_msg);
6179 
6180                IF (l_err_num <>0) THEN
6181                        l_err_msg := SUBSTR('Rewrk_txn_id: '
6182                                        ||TO_CHAR(c_rework_rec.txn_id)
6183                                        ||':'
6184                                        ||l_err_msg,1,240);
6185                        RAISE CST_PROCESS_ERROR;
6186                END IF;
6187        END LOOP; --c_rework_issue_txns
6188 
6189        l_stmt_num := 30;
6190        IF (CSTPPINV.l_item_id_tbl.COUNT > 0 AND p_cost_method <> 4) THEN
6191 
6192         CSTPPWAC.insert_into_cppb(i_pac_period_id  =>  p_pac_period_id,
6193                                   i_cost_group_id  =>  p_cost_group_id,
6194                                   i_txn_category   =>  6,  /* Rework Issues */
6195                                   i_user_id        =>  p_user_id,
6196                                   i_login_id       =>  p_login_id,
6197                                   i_request_id     =>  p_request_id,
6198                                   i_prog_id        =>  p_prog_id,
6199                                   i_prog_appl_id   =>  p_prog_app_id,
6200                                   o_err_num        =>  l_err_num,
6201                                   o_err_code       =>  l_err_code,
6202                                   o_err_msg        =>  l_err_msg);
6203 
6204         IF (l_err_num <> 0) THEN
6205            l_err_msg := SUBSTR('CSTPPWAS.process_reworkassembly_txns('
6206                               ||TO_CHAR(l_stmt_num)
6207                               ||'):'
6208                               ||l_err_msg,1,240);
6209             RAISE CST_PROCESS_ERROR;
6210         END IF;
6211        END IF;
6212 
6213        IF (p_cost_method <> 4) THEN
6214           l_stmt_num := 35;
6215           /* Bug 11834257: Use update_item_cppb instead of update_cppb */
6216          CSTPPWAC.update_item_cppb(i_pac_period_id  =>  p_pac_period_id,
6217                               i_cost_group_id  =>  p_cost_group_id,
6218                               i_txn_category   =>  6,          /* Rework Issues */
6219                               i_item_id         =>  p_item_id,
6220                               i_user_id        =>  p_user_id,
6221                               i_login_id       =>  p_login_id,
6222                               i_request_id     =>  p_request_id,
6223                               i_prog_id        =>  p_prog_id,
6224                               i_prog_appl_id   =>  p_prog_app_id,
6225                               o_err_num        =>  l_err_num,
6226                               o_err_code       =>  l_err_code,
6227                               o_err_msg        =>  l_err_msg);
6228 
6229           /*CSTPPWAC.update_cppb(i_pac_period_id   =>  p_pac_period_id,
6230                                i_cost_group_id   =>  p_cost_group_id,
6231                                i_txn_category    =>  6,          /* Rework Issues */
6232                              /*i_low_level_code  =>  p_low_level_code,
6233                                i_user_id         =>  p_user_id,
6234                                i_login_id        =>  p_login_id,
6235                                i_request_id      =>  p_request_id,
6236                                i_prog_id         =>  p_prog_id,
6237                                i_prog_appl_id    =>  p_prog_app_id,
6238                                o_err_num         =>  l_err_num,
6239                                o_err_code        =>  l_err_code,
6240                                o_err_msg         =>  l_err_msg);*/
6241 
6242           IF (l_err_num <> 0) THEN
6243              l_err_msg := SUBSTR('CSTPPWAS.process_reworkassembly_txns('
6244                                    || TO_CHAR(l_stmt_num)
6245                                    ||'):'
6246                                    ||l_err_msg,1,240);
6247               RAISE CST_PROCESS_ERROR;
6248           END IF;
6249        END IF;
6250 
6251        ----------------------------------------------------------------------
6252        -- Relieve each entity's scrap/scrap_return cost
6253        ----------------------------------------------------------------------
6254 
6255        l_stmt_num := 40;
6256 
6257        FOR c_ent_rec IN c_rework_entity_scrap LOOP
6258 
6259           l_co_txns_count := l_co_txns_count + 1;
6260 
6261           IF (CSTPPINV.l_item_id_tbl.COUNT >= G_MAX_RECORDS AND p_cost_method <> 4) THEN
6262 
6263             CSTPPWAC.insert_into_cppb(i_pac_period_id  =>  p_pac_period_id,
6264                                       i_cost_group_id  =>  p_cost_group_id,
6265                                       i_txn_category   =>  7,   /* Rework Completions */
6266                                       i_user_id        =>  p_user_id,
6267                                       i_login_id       =>  p_login_id,
6268                                       i_request_id     =>  p_request_id,
6269                                       i_prog_id        =>  p_prog_id,
6270                                       i_prog_appl_id   =>  p_prog_app_id,
6271                                       o_err_num        =>  l_err_num,
6272                                       o_err_code       =>  l_err_code,
6273                                       o_err_msg        =>  l_err_msg);
6274             IF (l_err_num <> 0) THEN
6275                l_err_msg := SUBSTR('CSTPPWAS.process_reworkassembly_txns('
6276                                   ||TO_CHAR(l_stmt_num)
6277                                   ||'):'
6278                                   ||l_err_msg,1,240);
6279                 RAISE CST_PROCESS_ERROR;
6280             END IF;
6281           END IF;
6282 
6283           l_stmt_num := 45;
6284           process_net_relief    (
6285                   p_pac_period_id              => p_pac_period_id,
6286                   p_prior_period_id            => p_prior_period_id,
6287                   p_cost_group_id              => p_cost_group_id,
6288                   p_cost_type_id               => p_cost_type_id,
6289                   p_legal_entity               => p_legal_entity,
6290                   p_cost_method                => p_cost_method,
6291                   p_cost_layer_id              => l_cost_layer_id,
6292                   p_qty_layer_id               => l_qty_layer_id,
6293                   p_pac_rates_id               => p_pac_rates_id,
6294                   p_entity_type                => c_ent_rec.entity_type,
6295                   p_org_id                     => c_ent_rec.org_id,
6296                   p_entity_id                  => c_ent_rec.entity_id,
6297                   p_line_id                    => c_ent_rec.line_id,
6298                   p_net_qty                    =>  c_ent_rec.net_scrap,
6299                   p_start_date                 => p_start_date,
6300                   p_end_date                   => p_end_date,
6301                   p_scrap                      => 1,
6302                   p_op_seq                     => c_ent_rec.op_seq,
6303                   p_final_completion_flag      => NULL,
6304                   p_material_relief_algorithm  => p_material_relief_algorithm,
6305                   p_master_org_id              => p_master_org_id,
6306                   p_uom_control                => p_uom_control,
6307                   p_user_id                    => p_user_id,
6308                   p_login_id                   => p_login_id,
6309                   p_request_id                 => p_request_id,
6310                   p_prog_id                    => p_prog_id,
6311                   p_prog_app_id                => p_prog_app_id,
6312                   p_txn_category               => 7, /* rework completions/scrap */
6313                   x_err_num                    => l_err_num,
6314                   x_err_code                   => l_err_code,
6315                   x_err_msg                    => l_err_msg);
6316 
6317            IF (l_err_num <> 0) THEN
6318 
6319                    l_err_msg := SUBSTR('scrap entity/line: '
6320                                    ||TO_CHAR(c_ent_rec.entity_id)
6321                                    ||'/'
6322                                    ||TO_CHAR(c_ent_rec.line_id)
6323                                    ||':'
6324                                    ||l_err_msg,1,240);
6325                    RAISE CST_PROCESS_ERROR;
6326 
6327            END IF;
6328         END LOOP; --c_rework_entity_scrap
6329 
6330        ----------------------------------------------------------------------
6331        -- Relieve each entity's completion/return cost
6332        ----------------------------------------------------------------------
6333         l_stmt_num := 50;
6334 
6335         FOR c_ent_rec IN c_rework_entity_complete LOOP
6336 
6337            l_co_txns_count := l_co_txns_count + 1;
6338 
6339            SELECT count(*)
6340            INTO   l_final_completion_count
6341            FROM   mtl_material_transactions mmt
6342            WHERE  mmt.transaction_source_id = c_ent_rec.entity_id
6343            AND    nvl(mmt.repetitive_line_id,-99) = nvl( c_ent_rec.line_id,-99)
6344            /* Bug 12703830 */
6345            AND    mmt.transaction_action_id = 31
6346            AND    mmt.final_completion_flag = 'Y'
6347            AND    mmt.transaction_date BETWEEN TRUNC(p_start_date)
6348                                          AND (TRUNC(p_end_date) + 0.99999)
6349            AND    ROWNUM < 2;
6350 
6351             IF (CSTPPINV.l_item_id_tbl.COUNT >= G_MAX_RECORDS AND p_cost_method <> 4) THEN
6352              CSTPPWAC.insert_into_cppb(i_pac_period_id  =>  p_pac_period_id,
6353                                        i_cost_group_id  =>  p_cost_group_id,
6354                                        i_txn_category   =>  7,   /* Rework completions */
6355                                        i_user_id        =>  p_user_id,
6356                                        i_login_id       =>  p_login_id,
6357                                        i_request_id     =>  p_request_id,
6358                                        i_prog_id        =>  p_prog_id,
6359                                        i_prog_appl_id   =>  p_prog_app_id,
6360                                        o_err_num        =>  l_err_num,
6361                                        o_err_code       =>  l_err_code,
6362                                        o_err_msg        =>  l_err_msg);
6363 
6364              IF (l_err_num <> 0) THEN
6365                 l_err_msg := SUBSTR('CSTPPWAS.process_reworkassembly_txns('
6366                                    ||TO_CHAR(l_stmt_num)
6367                                    ||'):'
6368                                    ||l_err_msg,1,240);
6369                  RAISE CST_PROCESS_ERROR;
6370              END IF;
6371             END IF;
6372 
6373            l_stmt_num := 55;
6374            process_net_relief   (
6375                    p_pac_period_id              => p_pac_period_id,
6376                    p_prior_period_id            => p_prior_period_id,
6377                    p_cost_group_id              => p_cost_group_id,
6378                    p_cost_type_id               => p_cost_type_id,
6379                    p_legal_entity               => p_legal_entity,
6380                    p_cost_method                => p_cost_method,
6381                    p_cost_layer_id              => l_cost_layer_id,
6382                    p_qty_layer_id               => l_qty_layer_id,
6383                    p_pac_rates_id               => p_pac_rates_id,
6384                    p_entity_type                => c_ent_rec.entity_type,
6385                    p_org_id                     => c_ent_rec.org_id,
6386                    p_entity_id                  => c_ent_rec.entity_id,
6387                    p_line_id                    => c_ent_rec.line_id,
6388                    p_net_qty                    => c_ent_rec.net_completion,
6389                    p_start_date                 => p_start_date,
6390                    p_end_date                   => p_end_date,
6391                    p_scrap                      => -1,
6392                    p_op_seq                     => NULL,
6393                    p_final_completion_flag      => l_final_completion_count,
6394                    p_material_relief_algorithm  => p_material_relief_algorithm,
6395                    p_master_org_id              => p_master_org_id,
6396                    p_uom_control                => p_uom_control,
6397                    p_user_id                    => p_user_id,
6398                    p_login_id                   => p_login_id,
6399                    p_request_id                 => p_request_id,
6400                    p_prog_id                    => p_prog_id,
6401                    p_prog_app_id                => p_prog_app_id,
6402                    p_txn_category               => 7, /* rework completions/scrap */
6403                    x_err_num                    => l_err_num,
6404                    x_err_code                   => l_err_code,
6405                    x_err_msg                    => l_err_msg);
6406 
6407            IF (l_err_num <>0) THEN
6408 
6409               l_err_msg := SUBSTR('cmpl entity/line: '
6410                               ||TO_CHAR(c_ent_rec.entity_id)
6411                               ||'/'
6412                               ||TO_CHAR(c_ent_rec.line_id)
6413                               ||':'
6414                               ||l_err_msg,1,240);
6415               RAISE CST_PROCESS_ERROR;
6416 
6417            END IF;
6418 
6419         END LOOP; --c_rework_entity_complete
6420 
6421         /* Insert into cppb */
6422         l_stmt_num := 60;
6423          IF (CSTPPINV.l_item_id_tbl.COUNT > 0 AND p_cost_method <> 4) THEN
6424 
6425           CSTPPWAC.insert_into_cppb(i_pac_period_id  =>  p_pac_period_id,
6426                                     i_cost_group_id  =>  p_cost_group_id,
6427                                     i_txn_category   =>  7,  /* Rework completions */
6428                                     i_user_id        =>  p_user_id,
6429                                     i_login_id       =>  p_login_id,
6430                                     i_request_id     =>  p_request_id,
6431                                     i_prog_id        =>  p_prog_id,
6432                                     i_prog_appl_id   =>  p_prog_app_id,
6433                                     o_err_num        =>  l_err_num,
6434                                     o_err_code       =>  l_err_code,
6435                                     o_err_msg        =>  l_err_msg);
6436           IF (l_err_num <> 0) THEN
6437              l_err_msg := SUBSTR('CSTPPWAS.process_reworkassembly_txns('
6438                                 ||TO_CHAR(l_stmt_num)
6439                                 ||'):'
6440                                 ||l_err_msg,1,240);
6441               RAISE CST_PROCESS_ERROR;
6442           END IF;
6443          END IF;
6444 
6445         /* Calculate Periodic Cost if cost method is not ILIFO and there
6446            have is atleast one cost owned transaction for rework assemblies */
6447 
6448          IF (p_cost_method <> 4 AND l_co_txns_count > 0) THEN
6449              l_stmt_num := 65;
6450              CSTPPWAC.calculate_periodic_cost(i_pac_period_id   =>  p_pac_period_id,
6451                                               i_cost_group_id   =>  p_cost_group_id,
6452                                               i_cost_type_id    =>  p_cost_type_id,
6453                                               i_low_level_code  =>  p_low_level_code,
6454                                               i_item_id         =>  p_item_id, /* 11834257 */
6455                                               /*i_item_id         =>  NULL, /* Used only by PACP */
6456                                               i_user_id         =>  p_user_id,
6457                                               i_login_id        =>  p_login_id,
6458                                               i_request_id      =>  p_request_id,
6459                                               i_prog_id         =>  p_prog_id,
6460                                               i_prog_appl_id    =>  p_prog_app_id,
6461                                               o_err_num         =>  l_err_num,
6462                                               o_err_code        =>  l_err_code,
6463                                               o_err_msg         =>  l_err_msg);
6464 
6465              IF (l_err_num <> 0) THEN
6466                 l_err_msg := SUBSTR('CSTPPWAS.process_reworkassembly_txns('
6467                                    ||TO_CHAR(l_stmt_num)
6468                                    ||'):'
6469                                    ||l_err_msg,1,240);
6470                  RAISE CST_PROCESS_ERROR;
6471              END IF;
6472 
6473              /* Update cppb */
6474              l_stmt_num := 70;
6475              /* Bug 11834257: Use update_item_cppb instead of update_cppb */
6476              CSTPPWAC.update_item_cppb(i_pac_period_id  =>  p_pac_period_id,
6477                                   i_cost_group_id  =>  p_cost_group_id,
6478                                   i_txn_category   =>  7,       /* Rework Completions */
6479                                   i_item_id         =>  p_item_id,
6480                                   i_user_id        =>  p_user_id,
6481                                   i_login_id       =>  p_login_id,
6482                                   i_request_id     =>  p_request_id,
6483                                   i_prog_id        =>  p_prog_id,
6484                                   i_prog_appl_id   =>  p_prog_app_id,
6485                                   o_err_num        =>  l_err_num,
6486                                   o_err_code       =>  l_err_code,
6487                                   o_err_msg        =>  l_err_msg);
6488 
6489              /*CSTPPWAC.update_cppb(i_pac_period_id   =>  p_pac_period_id,
6490                                   i_cost_group_id   =>  p_cost_group_id,
6491                                   i_txn_category    =>  7,       /* Rework Completions */
6492                                 /*i_low_level_code  =>  p_low_level_code,
6493                                   i_user_id         =>  p_user_id,
6494                                   i_login_id        =>  p_login_id,
6495                                   i_request_id      =>  p_request_id,
6496                                   i_prog_id         =>  p_prog_id,
6497                                   i_prog_appl_id    =>  p_prog_app_id,
6498                                   o_err_num         =>  l_err_num,
6499                                   o_err_code        =>  l_err_code,
6500                                   o_err_msg         =>  l_err_msg);*/
6501 
6502 
6503              IF (l_err_num <> 0) THEN
6504                 l_err_msg := SUBSTR('CSTPPWAS.process_reworkassembly_txns('
6505                                    ||TO_CHAR(l_stmt_num)
6506                                    ||'):'
6507                                    ||l_err_msg,1,240);
6508                  RAISE CST_PROCESS_ERROR;
6509              END IF;
6510           END IF;
6511 
6512   IF (l_pLog) THEN
6513    FND_LOG.STRING (FND_LOG.LEVEL_PROCEDURE,
6514                    l_module || '.end',
6515                    l_api_name || ' >>>');
6516   END IF;
6517 
6518 EXCEPTION
6519 
6520        WHEN CST_PROCESS_ERROR THEN
6521                IF (l_exceptionLog) THEN
6522                   FND_LOG.STRING (FND_LOG.LEVEL_EXCEPTION,
6523                                   l_module || '.' || l_stmt_num,
6524                                   l_err_msg);
6525                END IF;
6526                x_err_num := l_err_num;
6527                x_err_code := l_err_code;
6528                x_err_msg := SUBSTR(l_err_msg,1,240);
6529 
6530        WHEN OTHERS THEN
6531                IF (l_uLog) THEN
6532                   FND_LOG.STRING (FND_LOG.LEVEL_UNEXPECTED,
6533                                   l_module || '.' || l_stmt_num,
6534                                   SQLERRM);
6535                END IF;
6536                ROLLBACK;
6537                x_err_num := SQLCODE;
6538                x_err_code := NULL;
6539                x_err_msg := SUBSTR('CSTPPWAS.process_reworkassembly_txns('
6540                                || to_char(l_stmt_num)
6541                                || '): '
6542                                ||SQLERRM,1,240);
6543 END process_reworkassembly_txns;
6544 
6545 /*---------------------------------------------------------------------------*
6546 |  PUBLIC PROCEDURE                                                  |
6547 |       check_expense_flags                                                  |
6548 |   utility procedure to return item and expense flags               |
6549 |                                                                            |
6550 *----------------------------------------------------------------------------*/
6551 
6552 PROCEDURE check_expense_flags(
6553        p_item_id                IN      NUMBER,
6554        p_subinv                 IN      VARCHAR2,
6555        p_org_id                 IN      NUMBER,
6556        x_exp_item               OUT NOCOPY      NUMBER,
6557        x_exp_flag               OUT NOCOPY      NUMBER,
6558        x_err_num                OUT NOCOPY      NUMBER,
6559        x_err_code               OUT NOCOPY      VARCHAR2,
6560        x_err_msg                OUT NOCOPY      VARCHAR2)
6561 IS
6562 
6563 l_stmt_num                      NUMBER;
6564 l_err_num                       NUMBER;
6565 l_err_code                      VARCHAR2(240);
6566 l_err_msg                       VARCHAR2(240);
6567 l_exp_item                      NUMBER;
6568 l_exp_flag                      NUMBER;
6569 
6570 l_api_name            CONSTANT VARCHAR2(30) := 'check_expense_flags';
6571 l_full_name           CONSTANT VARCHAR2(60) := G_PKG_NAME || '.' || l_api_name;
6572 l_module              CONSTANT VARCHAR2(60) := 'cst.plsql.' || l_full_name;
6573 
6574 l_uLog  CONSTANT BOOLEAN := FND_LOG.LEVEL_UNEXPECTED >= G_LOG_LEVEL AND FND_LOG.TEST (FND_LOG.LEVEL_UNEXPECTED, l_module);
6575 l_errorLog CONSTANT BOOLEAN := l_uLog AND (FND_LOG.LEVEL_ERROR >= G_LOG_LEVEL);
6576 l_exceptionLog CONSTANT BOOLEAN := l_errorLog AND (FND_LOG.LEVEL_EXCEPTION >= G_LOG_LEVEL);
6577 l_eventLog CONSTANT BOOLEAN := l_exceptionLog AND (FND_LOG.LEVEL_EVENT >= G_LOG_LEVEL);
6578 l_pLog CONSTANT BOOLEAN := l_eventLog AND (FND_LOG.LEVEL_PROCEDURE >= G_LOG_LEVEL);
6579 l_sLog CONSTANT BOOLEAN := l_pLog AND (FND_LOG.LEVEL_STATEMENT >= G_LOG_LEVEL);
6580 
6581 BEGIN
6582 
6583        IF (l_pLog) THEN
6584         FND_LOG.STRING (FND_LOG.LEVEL_PROCEDURE,
6585                         l_module || '.begin',
6586                         l_api_name || ' <<< Parameters:
6587                         p_item_id = ' || p_item_id );
6588 
6589        END IF;
6590        ----------------------------------------------------------------------
6591        -- Initialize Variables
6592        ----------------------------------------------------------------------
6593 
6594        l_err_num        := 0;
6595        l_err_code       := '';
6596        l_err_msg        := '';
6597        l_exp_item       := 0;
6598        l_exp_flag       := 0;
6599 
6600        ----------------------------------------------------------------------
6601        -- Check Item flag
6602        ----------------------------------------------------------------------
6603 
6604        l_stmt_num := 5;
6605 
6606        SELECT  decode(inventory_asset_flag,'Y',0,1)
6607        INTO    l_exp_item
6608        FROM    mtl_system_items msi
6609        WHERE   inventory_item_id = p_item_id
6610        AND     organization_id = p_org_id;
6611 
6612        ----------------------------------------------------------------------
6613        -- Check Item flag
6614        ----------------------------------------------------------------------
6615 
6616        l_stmt_num := 10;
6617 
6618        IF (p_subinv IS NULL) THEN
6619                l_exp_flag := l_exp_item;
6620        ELSE
6621                SELECT  decode(l_exp_item,1,1,decode(asset_inventory,1,0,1))
6622                INTO    l_exp_flag
6623                FROM    mtl_secondary_inventories msi
6624                WHERE   secondary_inventory_name = p_subinv
6625                AND     organization_id = p_org_id;
6626        END IF;
6627 
6628        l_stmt_num := 15;
6629 
6630        x_exp_item := l_exp_item;
6631        x_exp_flag := l_exp_flag;
6632 
6633   IF (l_pLog) THEN
6634    FND_LOG.STRING (FND_LOG.LEVEL_PROCEDURE,
6635                    l_module || '.end',
6636                    l_api_name || ' >>>');
6637   END IF;
6638 
6639 EXCEPTION
6640 
6641        WHEN OTHERS THEN
6642                IF (l_uLog) THEN
6643                   FND_LOG.STRING (FND_LOG.LEVEL_UNEXPECTED,
6644                                   l_module || '.' || l_stmt_num,
6645                                   SQLERRM);
6646                END IF;
6647                ROLLBACK;
6648                x_err_num := SQLCODE;
6649                x_err_code := NULL;
6650                x_err_msg := SUBSTR('CSTPPWAS.check_expense_flags('
6651                                || to_char(l_stmt_num)
6652                                || '): '
6653                                ||SQLERRM,1,240);
6654 
6655 END check_expense_flags;
6656 
6657 END cstppwas;