DBA Data[Home] [Help]

PACKAGE BODY: APPS.ENG_CHANGE_LIFECYCLE_UTIL

Source


1 PACKAGE BODY ENG_CHANGE_LIFECYCLE_UTIL AS
2 /* $Header: ENGULCMB.pls 120.28 2007/07/04 10:59:55 sdarbha ship $ */
3 
4   -- ---------------------------------------------------------------------------
5   -- Global variables and constants
6   -- ---------------------------------------------------------------------------
7   G_PKG_NAME                VARCHAR2(30) := 'ENG_CHANGE_LIFECYCLE_UTIL';
8 
9   -- For Debug
10   g_debug_flag            BOOLEAN       := FALSE ;
11   g_output_dir            VARCHAR2(240) := NULL ;
12   g_debug_filename        VARCHAR2(200) := NULL ;
13   g_debug_errmesg         VARCHAR2(240);
14   G_BO_IDENTIFIER         VARCHAR2(30) := 'ENG_CHANGE_LC_UTIL';
15   G_ERRFILE_PATH_AND_NAME VARCHAR2(10000);
16   g_profile_debug_option  VARCHAR2(10) ;
17   g_profile_debug_level   VARCHAR2(10) ;
18 
19 
20   -- ---------------------------------------------------------------------------
21   -- Global cursors
22   -- ---------------------------------------------------------------------------
23 
24 
25   /********************************************************************
26   * Debug APIs    : Open_Debug_Session, Close_Debug_Session,
27   *                 Write_Debug
28   * Parameters IN :
29   * Parameters OUT:
30   * Purpose       : These procedures are for test and debug
31   *********************************************************************/
32 
33   ----------------------------------------------------------
34   -- Internal procedure to open Debug Session.            --
35   ----------------------------------------------------------
36   -- Open_Debug_Session
37   PROCEDURE Open_Debug_Session
38   (  p_output_dir IN VARCHAR2 := NULL
39   ,  p_file_name  IN VARCHAR2 := NULL
40   )
41   IS
42 
43     CURSOR c_get_utl_file_dir IS
44        SELECT VALUE
45         FROM V$PARAMETER
46         WHERE NAME = 'utl_file_dir';
47 
48     --local variables
49     l_found                NUMBER;
50 
51     l_log_output_dir       VARCHAR2(512);
52     l_log_return_status    VARCHAR2(99);
53     l_errbuff              VARCHAR2(2000);
54 
55   BEGIN
56 
57 
58     -- Ignore open_debug_session call if package debugging mode is already ON
59     IF ( g_debug_flag AND Error_Handler.Get_Debug = 'Y' ) THEN
60       RETURN ;
61     END IF ;
62 
63     l_found := 0 ;
64     Error_Handler.initialize();
65     Error_Handler.set_bo_identifier(G_BO_IDENTIFIER);
66 
67     ---------------------------------------------------------------------------------
68     -- Open_Debug_Session should set the value
69     -- appropriately, so that when the Debug Session is successfully opened :
70     -- will return Error_Handler.Get_Debug = 'Y', else Error_Handler.Get_Debug = 'N'
71     ---------------------------------------------------------------------------------
72 
73     IF p_output_dir IS NOT NULL THEN
74         g_output_dir := p_output_dir ;
75     END IF;
76 
77     IF p_file_name IS NOT NULL THEN
78         g_debug_filename := p_file_name ;
79     END IF;
80 
81     OPEN c_get_utl_file_dir;
82     FETCH c_get_utl_file_dir INTO l_log_output_dir;
83 
84     IF c_get_utl_file_dir%FOUND THEN
85 
86       IF g_output_dir IS NOT NULL
87       THEN
88          l_found := INSTR(l_log_output_dir, g_output_dir);
89          IF l_found = 0
90          THEN
91              g_output_dir := NULL ;
92          END IF;
93       END IF;
94 
95       ------------------------------------------------------
96       -- Trim to get only the first directory in the list --
97       ------------------------------------------------------
98       IF INSTR(l_log_output_dir,',') <> 0 THEN
99         l_log_output_dir := SUBSTR(l_log_output_dir, 1, INSTR(l_log_output_dir, ',') - 1);
100       END IF;
101 
102 
103       IF g_output_dir IS NULL
104       THEN
105          g_output_dir := l_log_output_dir ;
106       END IF ;
107 
108 
109       IF g_debug_filename IS NULL
110       THEN
111           g_debug_filename := G_BO_IDENTIFIER ||'_' || to_char(sysdate, 'DDMONYYYY_HH24MISS')||'.log';
112       END IF ;
113 
114       -----------------------------------------------------------------------
115       -- To open the Debug Session to write the Debug Log.                 --
116       -- This sets Debug value so that Error_Handler.Get_Debug returns 'Y' --
117       -----------------------------------------------------------------------
118       Error_Handler.Open_Debug_Session(
119         p_debug_filename   => g_debug_filename
120        ,p_output_dir       => g_output_dir
121        ,x_return_status    => l_log_return_status
122        ,x_error_mesg       => l_errbuff
123        );
124 
125       FND_FILE.put_line(FND_FILE.LOG, 'Log file location --> '||l_log_output_dir||'/'||g_debug_filename ||' created with status '|| l_log_return_status);
126 
127       IF (l_log_return_status <> FND_API.G_RET_STS_SUCCESS)
128       THEN
129          FND_FILE.put_line(FND_FILE.LOG, 'Unable to open error log file. Error => '||l_errbuff) ;
130       END IF;
131 
132     END IF; --IF c_get_utl_file_dir%FOUND THEN
133     -- Bug : 4099546
134     CLOSE c_get_utl_file_dir;
135 
136     -- Set Global Debug Flag
137 
138     g_debug_flag := TRUE ;
139 
140   EXCEPTION
141       WHEN OTHERS THEN
142          g_debug_errmesg := Substr(To_Char(SQLCODE)||'/'||SQLERRM,1,240);
143          FND_FILE.put_line(FND_FILE.LOG, G_PKG_NAME || ' Open_Debug_Session LOGGING SQL ERROR => '||g_debug_errmesg);
144          g_debug_flag := FALSE;
145   END Open_Debug_Session ;
146 
147 
148    -----------------------------------------------------------
149    -- Open the Debug Session, conditionally if the profile: --
150    -- INV Debug Trace is set to TRUE                        --
151    -----------------------------------------------------------
152   PROCEDURE Check_And_Open_Debug_Session
153   (  p_debug_flag IN VARCHAR2
154   ,  p_output_dir IN VARCHAR2 := NULL
155   ,  p_file_name  IN VARCHAR2 := NULL
156   )
157   IS
158 
159 
160   BEGIN
161     ----------------------------------------------------------------
162     -- Open the Debug Log Session, p_debug_flag is TRUE or
163     -- if Profile is set to TRUE: INV_DEBUG_TRACE Yes, INV_DEBUG_LEVEL 20
164     ----------------------------------------------------------------
165     g_profile_debug_option := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'), TO_CHAR(0));
166     g_profile_debug_level := NVL(FND_PROFILE.VALUE('INV_DEBUG_LEVEL'), TO_CHAR(0));
167 
168     IF (g_profile_debug_option = '1' AND TO_NUMBER(g_profile_debug_level) >= 20)
169        OR FND_API.to_Boolean(p_debug_flag)
170     THEN
171 
172        ----------------------------------------------------------------------------------
173        -- Opens Error_Handler debug session, only if Debug session is not already open.
174        -- Suggested by RFAROOK, so that multiple debug sessions are not open PER
175        -- Concurrent Request.
176        ----------------------------------------------------------------------------------
177        IF (Error_Handler.Get_Debug <> 'Y') THEN
178 
179 FND_FILE.put_line(FND_FILE.LOG, G_PKG_NAME || ' Error_Handler.Get_Debug is not Y, calling Open_Debug_Session  ');
180          Open_Debug_Session(p_output_dir => p_output_dir, p_file_name => p_file_name) ;
181        END IF;
182 
183     END IF;
184 
185   EXCEPTION
186       WHEN OTHERS THEN
187          g_debug_errmesg := Substr(To_Char(SQLCODE)||'/'||SQLERRM,1,240);
188          FND_FILE.put_line(FND_FILE.LOG, G_PKG_NAME || ' Check_And_Open_Debug_Session LOGGING SQL ERROR => '||g_debug_errmesg);
189          g_debug_flag := FALSE;
190   END Check_And_Open_Debug_Session;
191 
192   -- Close Debug_Session
193   PROCEDURE Close_Debug_Session
194   IS
195   BEGIN
196 
197        -----------------------------------------------------------------------------
198        -- Close Error_Handler debug session, only if Debug session is already open.
199        -----------------------------------------------------------------------------
200        IF (Error_Handler.Get_Debug = 'Y') THEN
201 
202 FND_FILE.put_line(FND_FILE.LOG, G_PKG_NAME || ' Error_Handler.Get_Debug is not Y, calling Close_Debug_Session  ');
203          Error_Handler.Close_Debug_Session;
204 
205        END IF;
206 
207        g_debug_flag := FALSE;
208 
209   EXCEPTION
210       WHEN OTHERS THEN
211          g_debug_errmesg := Substr(To_Char(SQLCODE)||'/'||SQLERRM,1,240);
212          FND_FILE.put_line(FND_FILE.LOG, G_PKG_NAME || ' Close_Debug_Session LOGGING SQL ERROR => '||g_debug_errmesg);
213          g_debug_flag := FALSE;
214 
215   END Close_Debug_Session;
216 
217   -- Test Debug
218   PROCEDURE Write_Debug
219   (  p_debug_message      IN  VARCHAR2 )
220   IS
221   BEGIN
222       -- Sometimes Error_Handler.Write_Debug would not write
223       -- the debug message properly
224       -- So as workaround, I added special developer debug mode here
225       -- to write debug message forcedly
226       IF (TO_NUMBER(g_profile_debug_level) = 999)
227       THEN
228         FND_FILE.put_line(FND_FILE.LOG
229                         , G_PKG_NAME
230                           || '['||TO_CHAR(SYSDATE,'DD-MON-YYYY HH24:MI:SS')||'] '
231                           || p_debug_message
232                          );
233 
234       END IF ;
235 
236       Error_Handler.Write_Debug('['||TO_CHAR(SYSDATE,'DD-MON-YYYY HH24:MI:SS')||'] '|| p_debug_message);
237 
238   EXCEPTION
239       WHEN OTHERS THEN
240          g_debug_errmesg := Substr(To_Char(SQLCODE)||'/'||SQLERRM,1,240);
241          FND_FILE.put_line(FND_FILE.LOG, G_PKG_NAME || ' Write_Debug LOGGING SQL ERROR => '||g_debug_errmesg);
242          g_debug_flag := FALSE;
243   END Write_Debug;
244 
245 
246   PROCEDURE Get_Debug_Mode
247   (   p_item_type         IN  VARCHAR2
248    ,  p_item_key          IN  VARCHAR2
249    ,  x_debug_flag        OUT NOCOPY BOOLEAN
250    ,  x_output_dir        OUT NOCOPY VARCHAR2
251    ,  x_debug_filename    OUT NOCOPY VARCHAR2
252   )
253   IS
254 
255       l_debug_flag VARCHAR2(1) ;
256 
257   BEGIN
258 
259       -- Get Debug Flag
260       l_debug_flag := WF_ENGINE.GetItemAttrText
261                               (  p_item_type
262                                , p_item_key
263                                , '.DEBUG_FLAG'
264                                );
265 
266       IF FND_API.to_Boolean( l_debug_flag ) THEN
267          x_debug_flag := TRUE ;
268       END IF;
269 
270       -- Get Debug Output Directory
271       x_output_dir  := WF_ENGINE.GetItemAttrText
272                               (  p_item_type
273                                , p_item_key
274                                , '.DEBUG_OUTPUT_DIR'
275                                );
276 
277       -- Get Debug File Name
278       x_debug_filename := WF_ENGINE.GetItemAttrText
279                               (  p_item_type
280                                , p_item_key
281                                , '.DEBUG_FILE_NAME'
282                                );
283 
284   EXCEPTION
285       WHEN OTHERS THEN
286          g_debug_errmesg := Substr(To_Char(SQLCODE)||'/'||SQLERRM,1,240);
287          g_debug_flag := FALSE;
288 
289 
290   END Get_Debug_Mode ;
291 
292 
293   /********************************************************************
294   * API Type      : Local APIs
295   *********************************************************************/
296   FUNCTION CheckChangeObjApplication
297   ( p_change_id         IN     NUMBER
298   , p_appl_id           IN     NUMBER
299   )
300   RETURN NUMBER
301   IS
302       l_change_obj_appl_id  NUMBER ;
303 
304       CURSOR  c_change_appl_rec (p_change_id NUMBER
305                                 ,p_appl_id   NUMBER)
306       IS
307          SELECT eec.change_id,
308                 eec.change_mgmt_type_code ,
309                 changecategory.base_change_mgmt_type_code,
310                 type_appl.application_id
311           FROM ENG_ENGINEERING_CHANGES eec,
312                ENG_CHANGE_ORDER_TYPES ChangeCategory,
313                ENG_CHANGE_TYPE_APPLICATIONS type_appl
314           WHERE type_appl.change_type_id = ChangeCategory.change_order_type_id
315           and type_appl.application_id = p_appl_id
316           AND ChangeCategory.type_classification = 'CATEGORY'
317           AND ChangeCategory.change_mgmt_type_code = eec.change_mgmt_type_code
318           AND eec.change_id = p_change_id  ;
319 
320   BEGIN
321 
322       FOR l_rec IN c_change_appl_rec (p_change_id => p_change_id ,
323                                       p_appl_id => p_appl_id)
324       LOOP
325           l_change_obj_appl_id :=  l_rec.application_id  ;
326       END LOOP ;
327 
328       RETURN l_change_obj_appl_id ;
329 
330   END CheckChangeObjApplication ;
331 
332 
333 
334   -- Internal utility procedure to check if the header is CO and on its last
335   -- implement phase
336   PROCEDURE Is_CO_On_Last_Imp_Phase
337   (
338     p_change_id                 IN   NUMBER
339    ,p_api_caller                IN   VARCHAR2
340    ,x_is_co_last_phase          OUT  NOCOPY  VARCHAR2
341    --,x_curr_status_code          OUT  NOCOPY  NUMBER
342    --,x_last_status_code          OUT  NOCOPY  NUMBER
343    ,x_auto_demote_status        OUT  NOCOPY  NUMBER
344   )
345   IS
346 
347     l_fnd_user_id        NUMBER := TO_NUMBER(FND_PROFILE.VALUE('USER_ID'));
348     l_fnd_login_id       NUMBER := TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID'));
349 
350     l_cm_type_code       eng_engineering_changes.CHANGE_MGMT_TYPE_CODE%TYPE;
351     l_base_cm_type_code  eng_change_order_types.BASE_CHANGE_MGMT_TYPE_CODE%TYPE;
352 
353     l_next_status_code   NUMBER;
354 
355     --l_curr_status_code   NUMBER;
356 
357     l_last_status_code   NUMBER;
358     l_max_phase_sn       eng_lifecycle_statuses.sequence_number%TYPE;
359 
360     l_last_imp_flag      VARCHAR2(1) := 'N';
361 
362 
363   BEGIN
364     -- Standard Start of API savepoint
365     --SAVEPOINT  Is_CO_On_Last_Imp_Phase;
366 
367     -- Default return value
368     x_is_co_last_phase := 'F';
369     --x_last_status_code := NULL;
370     --x_curr_status_code := NULL;
371 
372     -- Write debug message if debug mode is on
373     IF g_debug_flag THEN
374        Write_Debug('ENG_CHANGE_LIFECYCLE_UTIL.Is_CO_On_Last_Imp_Phase log');
375        Write_Debug('-----------------------------------------------------');
376        Write_Debug('p_change_id         : ' || p_change_id );
377        Write_Debug('-----------------------------------------------------');
378        Write_Debug('Initializing return status... ' );
379     END IF;
380 
381     -- FND_PROFILE package is not available for workflow (WF),
382     -- therefore manually set WHO column values
383     IF p_api_caller = 'WF' THEN
384       l_fnd_user_id := G_ENG_WF_USER_ID;
385       l_fnd_login_id := G_ENG_WF_LOGIN_ID;
386     ELSIF p_api_caller = 'CP' THEN
387       l_fnd_user_id := G_ENG_CP_USER_ID;
388       l_fnd_login_id := G_ENG_CP_LOGIN_ID;
389     END IF;
390 
391     -- Real code starts here -----------------------------------------------
392 
393     -- Get the change header's cm type, and promote_status_code
394     SELECT eec.change_mgmt_type_code, eec.promote_status_code, --eec.status_code
395            ecot.base_change_mgmt_type_code
396       INTO l_cm_type_code, l_next_status_code, --l_curr_status_code
397            l_base_cm_type_code
398       FROM eng_engineering_changes eec,
399            eng_change_order_types ecot
400       WHERE eec.change_id = p_change_id
401         AND ecot.change_order_type_id = eec.change_order_type_id;
402 
403     --x_curr_status_code := l_curr_status_code;
404 
405     -- If the change header is of type ECO and the current running phase
406     -- is the last implement phase, return true, otherwise false
407     IF (l_base_cm_type_code = G_ENG_ECO OR l_next_status_code IS NOT NULL) THEN
408 
409       IF g_debug_flag THEN
410         Write_Debug('Info: change category is CO, and header promote_status_code is not null' );
411       END IF;
412 
413       -- Get the sequence_number of the last phase
414       -- Note that only phase of IMPLEMENT type can be the last phase
415       SELECT max(sequence_number)
416         INTO l_max_phase_sn
417         FROM eng_lifecycle_statuses
418         WHERE entity_name = G_ENG_CHANGE
419           AND entity_id1 = p_change_id
420           AND active_flag = 'Y';
421 
422       -- Get the sequence number of the last phase
423       SELECT status_code
424         INTO l_last_status_code
425         FROM eng_lifecycle_statuses
426         WHERE entity_name = G_ENG_CHANGE
427           AND entity_id1 = p_change_id
428           AND active_flag = 'Y'
429           AND sequence_number = l_max_phase_sn;
430       /*
431       x_last_status_code := l_last_status_code;
432       */
433 
434       IF ( l_next_status_code = l_last_status_code ) THEN
435         x_is_co_last_phase := 'T';
436       END IF;
437 
438     END IF;
439 
440 
441     -- Standard ending code ------------------------------------------------
442     IF g_debug_flag THEN
443       Write_Debug('x_is_co_last_phase        : ' || x_is_co_last_phase );
444       --Write_Debug('x_curr_status_code        : ' || x_curr_status_code );
445       --Write_Debug('x_last_status_code        : ' || x_last_status_code );
446       Write_Debug('x_auto_demote_status      : ' || x_auto_demote_status );
447       Write_Debug('Finish. End Of procedure: Is_CO_On_Last_Imp_Phase') ;
448     END IF;
449 
450   END Is_CO_On_Last_Imp_Phase;
451 
452 
453   -- Internal utility procedure to check if the header is CO and its last
454   -- implement phase has been used
455   PROCEDURE Is_CO_Last_Imp_Phase_Used
456   (
457     p_change_id                 IN   NUMBER
458    ,x_is_used                   OUT  NOCOPY  VARCHAR2
459    ,x_last_status_type          OUT  NOCOPY  NUMBER
460    ,x_last_status_code          OUT  NOCOPY  NUMBER
461   )
462   IS
463     l_cm_type_code       eng_engineering_changes.CHANGE_MGMT_TYPE_CODE%TYPE;
464     l_base_cm_type_code  eng_change_order_types.BASE_CHANGE_MGMT_TYPE_CODE%TYPE;
465     l_max_phase_sn       eng_lifecycle_statuses.sequence_number%TYPE;
466     l_start_date         eng_lifecycle_statuses.start_date%TYPE;
467   BEGIN
468     -- Standard Start of API savepoint
469     --SAVEPOINT  Is_CO_Last_Imp_Phase_Used;
470 
471     -- Default return value
472     x_is_used := 'F';
473     x_last_status_type := NULL;
474     x_last_status_code := NULL;
475 
476     -- Write debug message if debug mode is on
477     IF g_debug_flag THEN
478        Write_Debug('ENG_CHANGE_LIFECYCLE_UTIL.Is_CO_Last_Imp_Phase_Used log');
479        Write_Debug('-----------------------------------------------------');
480        Write_Debug('p_change_id         : ' || p_change_id );
481        Write_Debug('-----------------------------------------------------');
482        Write_Debug('Initializing return status... ' );
483     END IF;
484 
485     -- Real code starts here -----------------------------------------------
486 
487     -- Get the change header's cm type, and promote_status_code
488     SELECT eec.change_mgmt_type_code, ecot.base_change_mgmt_type_code
489       INTO l_cm_type_code, l_base_cm_type_code
490       FROM eng_engineering_changes eec,
491            eng_change_order_types ecot
492       WHERE eec.change_id = p_change_id
493         AND ecot.change_order_type_id = eec.change_order_type_id;
494 
495     IF (l_base_cm_type_code = G_ENG_ECO) THEN
496 
497       -- Get the sequence_number of the last phase
498       -- Note that only phase of IMPLEMENT type can be the last phase
499       SELECT max(sequence_number)
500         INTO l_max_phase_sn
501         FROM eng_lifecycle_statuses
502         WHERE entity_name = G_ENG_CHANGE
503           AND entity_id1 = p_change_id
504           AND active_flag = 'Y';
505 
506       -- Get the start_date of the last phase (to see if it's been used)
507       SELECT status_code, start_date
508         INTO x_last_status_code, l_start_date
509         FROM eng_lifecycle_statuses
510         WHERE entity_name = G_ENG_CHANGE
511           AND entity_id1 = p_change_id
512           AND active_flag = 'Y'
513           AND sequence_number = l_max_phase_sn
514           AND rownum = 1;
515 
516       IF ( l_start_date IS NOT NULL ) THEN
517         x_is_used := 'T';
518       END IF;
519 
520       -- Get the status_type of the last phase
521       SELECT status_type
522         INTO x_last_status_type
523         FROM eng_change_statuses
524         WHERE status_code = x_last_status_code;
525 
526     END IF;
527 
528 
529     -- Standard ending code ------------------------------------------------
530     IF g_debug_flag THEN
531       Write_Debug('x_is_used            : ' || x_is_used );
532       Write_Debug('x_last_status_type   : ' || x_last_status_type );
533       Write_Debug('x_last_status_code   : ' || x_last_status_code );
534       Write_Debug('Finish. End Of procedure: Is_CO_Last_Imp_Phase_Used') ;
535     END IF;
536 
537   END Is_CO_Last_Imp_Phase_Used;
538 
539 
540 
541   -- Internal procedure to return if a co has active revised items
542   -- active revised items are defined as those with status other than
543   -- 5(cancelled) or 6(implemented)
544   PROCEDURE Has_Active_RevItem
545   (
546     p_change_id                 IN   NUMBER
547    ,x_found                     OUT  NOCOPY VARCHAR2
548   )
549   IS
550     l_rev_item_seq_id    NUMBER;
551     CURSOR c_activeRevItem IS
552       SELECT revised_item_sequence_id
553         FROM eng_revised_items eri
554         WHERE eri.change_id = p_change_id
555           AND eri.status_type NOT IN (G_ENG_CANCELLED, G_ENG_IMPLEMENTED);
556   BEGIN
557     OPEN c_activeRevItem;
558       LOOP
559         FETCH c_activeRevItem INTO l_rev_item_seq_id;
560         IF (c_activeRevItem%FOUND)
561         THEN
562           x_found := 'Y';
563         ELSE
564           x_found := 'N';
565         END IF;
566         EXIT;
567       END LOOP;
568     CLOSE c_activeRevItem;
569 
570   END Has_Active_RevItem;
571 
572   -- Internal utility procedure to update header approval status
573   -- together with launching associated workflow
574   PROCEDURE Update_Header_Appr_Status
575   (
576     p_api_version               IN   NUMBER                             --
577    ,p_init_msg_list             IN   VARCHAR2 := FND_API.G_FALSE        --
578    ,p_commit                    IN   VARCHAR2 := FND_API.G_FALSE        --
579    ,p_validation_level          IN   NUMBER   := FND_API.G_VALID_LEVEL_FULL
580    ,p_debug                     IN   VARCHAR2 := FND_API.G_FALSE        --
581    ,p_output_dir                IN   VARCHAR2 := NULL
582    ,p_debug_filename            IN   VARCHAR2 := NULL
583    ,x_return_status             OUT  NOCOPY  VARCHAR2                   --
584    ,x_msg_count                 OUT  NOCOPY  NUMBER                     --
585    ,x_msg_data                  OUT  NOCOPY  VARCHAR2                   --
586    ,p_change_id                 IN   NUMBER                             -- header's change_id
587    ,p_status_code               IN   NUMBER
588    ,p_appr_status               IN   NUMBER                             -- header approval status
589    ,p_route_status              IN   VARCHAR2                           -- workflow routing status (for document types)
590    ,p_api_caller                IN   VARCHAR2 := 'UI'                   -- must
591    ,p_bypass                    IN   VARCHAR2 := 'N'                    -- flag to bypass phase type check
592    ,x_sfa_line_items_exists     OUT  NOCOPY  VARCHAR2
593   )
594   IS
595     l_api_name           CONSTANT VARCHAR2(30)  := 'Update_Header_Appr_Status';
596     l_api_version        CONSTANT NUMBER := 1.0;
597 
598     l_return_status      VARCHAR2(1);
599     l_msg_count          NUMBER;
600     l_msg_data           VARCHAR2(2000);
601 
602     l_fnd_user_id        NUMBER := TO_NUMBER(FND_PROFILE.VALUE('USER_ID'));
603     l_fnd_login_id       NUMBER := TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID'));
604 
605     l_updated            BOOLEAN      := FALSE ;
606 
607     l_phase_sn           eng_lifecycle_statuses.sequence_number%TYPE;
608     l_max_appr_phase_sn  eng_lifecycle_statuses.sequence_number%TYPE;
609 
610     l_status_type        eng_engineering_changes.status_type%TYPE;
611 
612     l_wf_item_key        wf_item_activity_statuses.item_key%TYPE := NULL;
613 
614     l_cm_type_code       eng_engineering_changes.change_mgmt_type_code%TYPE;
615     l_base_cm_type_code  eng_change_order_types.BASE_CHANGE_MGMT_TYPE_CODE%TYPE;
616 
617     l_param_list         WF_PARAMETER_LIST_T := WF_PARAMETER_LIST_T();
618 
619     l_appl_id            NUMBER ;
620 
621 
622     l_doc_lc_object_flag BOOLEAN := FALSE ;
623 
624   BEGIN
625     -- Standard Start of API savepoint
626     --SAVEPOINT  Update_Header_Appr_Status;
627     -- Standard call to check for call compatibility
628     IF NOT FND_API.Compatible_API_Call ( l_api_version
629                                         ,p_api_version
630                                         ,l_api_name
631                                         ,G_PKG_NAME )
632     THEN
633       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
634     END IF;
635     -- Initialize message list if p_init_msg_list is set to TRUE.
636     IF FND_API.to_Boolean( p_init_msg_list ) THEN
637        FND_MSG_PUB.initialize;
638     END IF;
639 
640     -- For Test/Debug
641     Check_And_Open_Debug_Session(p_debug, p_output_dir, p_debug_filename) ;
642     -- R12 Comment out
643     -- IF FND_API.to_Boolean( p_debug ) THEN
644     --     Open_Debug_Session(p_output_dir, p_debug_filename ) ;
645     -- END IF;
646 
647     -- Write debug message if debug mode is on
648     IF g_debug_flag THEN
649        Write_Debug('ENG_CHANGE_LIFECYCLE_UTIL.Update_Header_Appr_Status log');
650        Write_Debug('-----------------------------------------------------');
651        Write_Debug('p_change_id         : ' || p_change_id );
652        Write_Debug('p_status_code       : ' || p_status_code );
653        Write_Debug('p_appr_status       : ' || p_appr_status );
654        Write_Debug('p_api_caller        : ' || p_api_caller );
655        Write_Debug('p_bypass            : ' || p_bypass );
656        Write_Debug('-----------------------------------------------------');
657        Write_Debug('Initializing return status... ' );
658     END IF;
659 
660     -- Initialize API return status to success
661     x_return_status := FND_API.G_RET_STS_SUCCESS;
662 
663     -- FND_PROFILE package is not available for workflow (WF),
664     -- therefore manually set WHO column values
665     IF p_api_caller = 'WF' THEN
666       l_fnd_user_id := G_ENG_WF_USER_ID;
667       l_fnd_login_id := G_ENG_WF_LOGIN_ID;
668     ELSIF p_api_caller = 'CP' THEN
669       l_fnd_user_id := G_ENG_CP_USER_ID;
670       l_fnd_login_id := G_ENG_CP_LOGIN_ID;
671     END IF;
672 
673 
674     -- Real code starts here -----------------------------------------------
675 
676     -- get phase type
677     SELECT status_type
678       INTO l_status_type
679       FROM eng_change_statuses
680       WHERE status_code = p_status_code;
681 
682      -- Select cm type and base type code for upcoming API calls
683         SELECT eec.change_mgmt_type_code, ecot.base_change_mgmt_type_code
684           INTO l_cm_type_code, l_base_cm_type_code
685           FROM eng_engineering_changes eec,
686                eng_change_order_types ecot
687           WHERE eec.change_id = p_change_id
688           AND ecot.change_order_type_id = eec.change_order_type_id;
689 
690     l_doc_lc_object_flag := ENG_DOCUMENT_UTIL.Is_Dom_Document_Lifecycle
691                                ( p_change_id => p_change_id
692                                , p_base_change_mgmt_type_code => l_base_cm_type_code
693                                )  ;
694     -- update header approval status only for phase of type APPROVAL or REVIEW for Doc
695     IF ( l_status_type = G_ENG_APPROVED OR p_bypass = 'Y'
696          OR (l_doc_lc_object_flag AND l_status_type = G_ENG_REVIEWED)) THEN
697       IF g_debug_flag THEN
698          Write_Debug('Phase type is APPROVAL or p_bypass = ''Y'' (for NIR)');
699       END IF;
700 
701       -- update header approval status
702       IF (p_appr_status = G_ENG_APPR_REQUESTED ) THEN
703         UPDATE eng_engineering_changes
704           SET approval_status_type = p_appr_status,
705               approval_request_date = sysdate,
706               approval_date = null,
707               last_update_date = sysdate,
708               last_updated_by = l_fnd_user_id,
709               last_update_login = l_fnd_login_id
710           WHERE change_id = p_change_id;
711         l_updated := TRUE;
712         IF g_debug_flag THEN
713           Write_Debug('After updating eng_engineering_changes.approval_* columns.');
714           Write_Debug('  Row count = ' || SQL%ROWCOUNT);
715         END IF;
716         IF g_debug_flag THEN
717            Write_Debug('After: updating header approval columns FOR APPROVAL REQUESTED');
718         END IF;
719 
720       ELSIF (p_appr_status = G_ENG_APPR_APPROVED) THEN
721         -- Update header to approved only for the last phase of type APPROVAL
722 
723         -- Get the current phase's sequence number
724         SELECT sequence_number
725           INTO l_phase_sn
726           FROM eng_lifecycle_statuses
727           WHERE entity_name = G_ENG_CHANGE
728             AND entity_id1 = p_change_id
729             AND status_code = p_status_code
730             AND active_flag = 'Y'
731             AND rownum = 1;
732 
733         -- Get the sequence number of the last phase of type APPROVAL
734         SELECT max(lcs.sequence_number)
735           INTO l_max_appr_phase_sn
736           FROM eng_lifecycle_statuses lcs,
737                eng_change_statuses chs
738           WHERE lcs.entity_name = G_ENG_CHANGE
739             AND lcs.entity_id1 = p_change_id
740             AND lcs.active_flag = 'Y'
741             AND chs.status_code = lcs.status_code
742             AND chs.status_type = G_ENG_APPROVED;
743 
744         -- Check if the specified phase is the last phase of type APPROVAL
745         -- Update header approval status if so
746         -- Note for case: p_api_caller is null: previous if condition
747         -- garrantees l_status_type = G_ENG_APPROVED
748         IF ( l_phase_sn = l_max_appr_phase_sn OR (p_api_caller IS NULL) ) THEN
749           IF g_debug_flag THEN
750             Write_Debug('Current phase is the last of such type');
751           END IF;
752 
753           UPDATE eng_engineering_changes
754             SET approval_status_type = p_appr_status,
755                 approval_date = sysdate,
756                 last_update_date = sysdate,
757                 last_updated_by = l_fnd_user_id,
758                 last_update_login = l_fnd_login_id
759             WHERE change_id = p_change_id;
760             l_updated := TRUE;
761           IF g_debug_flag THEN
762             Write_Debug('After updating eng_engineering_changes.approval_* columns.');
763             Write_Debug('  Row count = ' || SQL%ROWCOUNT);
764           END IF;
765           IF g_debug_flag THEN
766              Write_Debug('After: updating header approval columns FOR APPROVED');
767           END IF;
768         END IF;
769 
770       ELSIF (   p_appr_status = G_ENG_APPR_REJECTED
771              OR p_appr_status = G_ENG_APPR_PROC_ERR
772              OR p_appr_status = G_ENG_APPR_TIME_OUT)
773       THEN
774         UPDATE eng_engineering_changes
775           SET approval_status_type = p_appr_status,
776               approval_date = null,
777               last_update_date = sysdate,
778               last_updated_by = l_fnd_user_id,
779               last_update_login = l_fnd_login_id
780           WHERE change_id = p_change_id;
781         l_updated := TRUE;
782         IF g_debug_flag THEN
783           Write_Debug('After updating eng_engineering_changes.approval_* columns.');
784           Write_Debug('  Row count = ' || SQL%ROWCOUNT);
785         END IF;
786         IF g_debug_flag THEN
787            Write_Debug('After: updating header approval columns FOR REJECTED, PROC_ERR, TIME_OUT');
788         END IF;
789 
790        ELSIF  (l_doc_lc_object_flag AND l_status_type = G_ENG_REVIEWED)
791        THEN
792         l_updated := TRUE;
793 	ELSE
794         UPDATE eng_engineering_changes
795           SET approval_status_type = p_appr_status,
796               approval_request_date = null,
797               approval_date = null,
798               last_update_date = sysdate,
799               last_updated_by = l_fnd_user_id,
800               last_update_login = l_fnd_login_id
801           WHERE change_id = p_change_id;
802         l_updated := TRUE;
803         IF g_debug_flag THEN
804           Write_Debug('After updating eng_engineering_changes.approval_* columns.');
805           Write_Debug('  Row count = ' || SQL%ROWCOUNT);
806         END IF;
807         IF g_debug_flag THEN
808            Write_Debug('After: updating header approval columns FOR other approval types');
809         END IF;
810       END IF;
811 
812       -- Since in some cases header approval status won't be updated
813       -- using l_updated flag to skip follow-up code here
814       IF (l_updated = TRUE)
815       THEN
816         -- Select cm type and base type code for upcoming API calls
817         /*SELECT eec.change_mgmt_type_code, ecot.base_change_mgmt_type_code
818           INTO l_cm_type_code, l_base_cm_type_code
819           FROM eng_engineering_changes eec,
820                eng_change_order_types ecot
821           WHERE eec.change_id = p_change_id
822           AND ecot.change_order_type_id = eec.change_order_type_id;*/
823 
824 
825         --
826         -- R12B
827         -- Document Lifecycle Support
828         -- Check if this change object is Document LC Change Object
829         --
830        /* l_doc_lc_object_flag := ENG_DOCUMENT_UTIL.Is_Dom_Document_Lifecycle
831                                ( p_change_id => p_change_id
832                                , p_base_change_mgmt_type_code => l_base_cm_type_code
833                                )  ;*/
834 
835         -- In case of Document LC Change Object, the Approval Status change and
836         -- the event should not be raised
837         IF (NOT l_doc_lc_object_flag)
838         THEN
839             IF g_debug_flag THEN
840                Write_Debug('Before: Approval Status Change WF and Event');
841             END IF;
842 
843 
844             -- Skip workflow notification if approval status is set back to not_submitted
845             IF (p_appr_status <> G_ENG_NOT_SUBMITTED)
846             THEN
847 
848 
849               -- Launch header approval status change workflow
850               IF g_debug_flag THEN
851                  Write_Debug('Before: Launch header approval status change workflow');
852               END IF;
853 
854               Eng_Workflow_Util.StartWorkflow
855               (  p_api_version       => 1.0
856               ,  p_init_msg_list     => FND_API.G_FALSE
857               ,  p_commit            => FND_API.G_FALSE
858               ,  p_validation_level  => FND_API.G_VALID_LEVEL_FULL
859               ,  x_return_status     => l_return_status
860               ,  x_msg_count         => l_msg_count
861               ,  x_msg_data          => l_msg_data
862               ,  p_item_type         => Eng_Workflow_Util.G_CHANGE_ACTION_ITEM_TYPE
863               ,  x_item_key          => l_wf_item_key
864               ,  p_process_name      => Eng_Workflow_Util.G_APPROVAL_STATUS_CHANGE_PROC
865               ,  p_change_id         => p_change_id
866               ,  p_wf_user_id        => l_fnd_user_id
867               ,  p_route_id          => 0 --l_wf_route_id
868               ,  p_debug             => p_debug --FND_API.G_FALSE
869               ,  p_output_dir        => p_output_dir
870               ,  p_debug_filename    => NULL  --p_debug_filename
871               ) ;
872 
873               IF g_debug_flag THEN
874                  Write_Debug('After: Launch header approval status change workflow: ' || l_return_status );
875               END IF;
876 
877               IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS )
878               THEN
879                 x_return_status := l_return_status;
880                 x_msg_count := l_msg_count;
881                 x_msg_data := l_msg_data;
882                 --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_WF_API');
883                 --#FND_MSG_PUB.Add;
884                 RAISE FND_API.G_EXC_ERROR;
885               END IF;
886               l_wf_item_key := NULL;
887               IF g_debug_flag THEN
888                  Write_Debug('Successful: Launch header approval status change workflow');
889               END IF;
890             END IF;
891 
892 
893             -- Raise the approval status change business event
894             -- Adding event parameters to the list
895             ENG_CHANGE_BES_UTIL.Raise_Appr_Status_Change_Event
896             ( p_change_id         => p_change_id
897              ,p_appr_status       => p_appr_status
898              ,p_wf_route_status   => p_route_status
899              );
900 
901 
902         END IF ; -- Check IF (NOT l_doc_lc_object_flag)
903 
904      --   R12C Enhancement
905      --   When the NIR is demoted to Approval status from a later phase or when the workflow in the Approval phase is aborted
906      --   The Line items have to go through the Item Approval process again
907      --   All the Rejected line items have to be reset to Submitted for Approval / Open
908      --   Approved Items can anyhow be rejected later if required by other workflow assignees
909      IF p_appr_status <> G_ENG_APPR_APPROVED AND p_appr_status <> G_ENG_APPR_REJECTED THEN
910           ENG_NIR_UTIL_PKG.Update_Line_Items_App_St(p_change_id, 3, x_sfa_line_items_exists); -- Reset it to SFA
911      END IF;
912 
913         -- Other calls and updates triggered by the header approval status update
914         -- call item part request API if applicable
915          IF (l_base_cm_type_code = G_ENG_NEW_ITEM_REQ
916 		AND p_appr_status in (G_ENG_APPR_REJECTED, G_ENG_APPR_TIME_OUT) ) THEN
917           IF g_debug_flag THEN
918              Write_Debug('Before: calling new item request API');
919              Write_Debug('  p_change_id =   ' || p_change_id);
920              Write_Debug('  p_appr_status = ' || p_appr_status);
921           END IF;
922          ENG_NIR_UTIL_PKG.set_nir_item_approval_status
923           ( p_change_id          => p_change_id
924            ,p_approval_status    => p_appr_status
925            ,x_return_status      => l_return_status
926            ,x_msg_count          => l_msg_count
927            ,x_msg_data           => l_msg_data
928            );
929 
930           IF g_debug_flag THEN
931              Write_Debug('After: calling new item request API: ' || l_return_status) ;
932           END IF;
933 
934           IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS )
935           THEN
936             x_return_status := l_return_status;
937             x_msg_count := l_msg_count;
938             x_msg_data := l_msg_data;
939             --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_PS_API');
940             --#FND_MESSAGE.Set_Token('OBJECT_NAME', 'ENG_NIR_UTIL_PKG.set_nir_item_approval_status');
941             --#FND_MSG_PUB.Add;
942             RAISE FND_API.G_EXC_ERROR;
943           END IF;
944           l_wf_item_key := NULL;
945           IF g_debug_flag THEN
946              Write_Debug('Successful: calling new item request API');
947           END IF;
948         END IF;
949 
950         -- Update attachment approval if needed
951         IF g_debug_flag THEN
952           Write_Debug('Before: calling update DM attachment approval/review API');
953         END IF;
954 
955         -- obsolete for bug 3429353 fix
956         -- IF ( p_appr_status <> G_ENG_APPR_APPROVED
957         --    AND (l_base_cm_type_code = G_ENG_ATTACHMENT_APPR
958         --         OR l_base_cm_type_code = G_ENG_ATTACHMENT_REVW)
959         --    )
960         --
961 
962         -- Added condition to call DM attachment approval/review API
963         -- if approval status is REJECTED for Bug4187851
964         -- in order to support
965         -- 6. Document status will get changed to Rejected as soon as the first
966         -- rejection happens in any of the approval workflow in the lifecycle.
967         -- then we will leave the final approval update to
968         -- Update_Lifecycle_States procedure final implement phase handling
969         -- Note:
970         -- This call will be moved to BES Subscription in future release
971         -- In the meantime, we put the call to minimize the code impact
972 
973         IF (   l_base_cm_type_code = G_ENG_ATTACHMENT_APPR
974             OR l_base_cm_type_code = G_ENG_ATTACHMENT_REVW )
975            AND p_appr_status = G_ENG_APPR_REJECTED
976         THEN
977           IF g_debug_flag THEN
978             Write_Debug('In: calling DM attachment approval/review API');
979             Write_Debug('p_workflow_status : ' || p_status_code );
980             Write_Debug('p_approval_status : ' || p_appr_status );
981           END IF;
982           ENG_ATTACHMENT_IMPLEMENTATION.Update_Attachment_Status
983           (
984             p_api_version         => 1.0
985            ,p_init_msg_list       => FND_API.G_FALSE
986            ,p_commit              => FND_API.G_FALSE
987            ,p_validation_level    => p_validation_level
988            ,p_debug               => p_debug --FND_API.G_FALSE
989            ,p_output_dir          => p_output_dir
990            ,p_debug_filename      => NULL  --p_debug_filename
991            ,x_return_status       => l_return_status
992            ,x_msg_count           => l_msg_count
993            ,x_msg_data            => l_msg_data
994            ,p_change_id           => p_change_id
995            ,p_workflow_status     => p_route_status
996            ,p_approval_status     => p_appr_status
997            ,p_api_caller          => p_api_caller
998           );
999 
1000           IF g_debug_flag THEN
1001               Write_Debug('After: calling update DM attachment approval/review API: ' || l_return_status) ;
1002           END IF;
1003 
1004 
1005           IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS ) THEN
1006             x_return_status := l_return_status;
1007             x_msg_count := l_msg_count;
1008             x_msg_data := l_msg_data;
1009             --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_PS_API');
1010             --#FND_MESSAGE.Set_Token('OBJECT_NAME', 'ENG_ATTACHMENT_IMPLEMENTATION.Update_Attachment_Status');
1011             --#FND_MSG_PUB.Add;
1012             RAISE FND_API.G_EXC_ERROR;
1013           END IF;
1014         END IF;
1015 
1016 
1017         -- AMW/ICM Application to Application integration calls
1018         -- triggered by the header approval status update
1019         -- call ENG_ICMDB_APIS_UTIL.Update_Approval_Status if applicable
1020 
1021         l_appl_id := CheckChangeObjApplication( p_change_id => p_change_id ,
1022                                                 p_appl_id => 242 -- AMW Applciation ID
1023                                               )  ;
1024 
1025         IF  (l_appl_id IS NOT NULL)
1026         THEN
1027           IF g_debug_flag THEN
1028              Write_Debug('Before: calling ENG_ICMDB_APIS_UTIL.Update_Approval_Status');
1029              Write_Debug('  p_change_id =   ' || p_change_id);
1030              Write_Debug('  p_base_change_mgmt_type_code =   ' || l_base_cm_type_code);
1031              Write_Debug('  p_appr_status = ' || p_appr_status);
1032              Write_Debug('  p_workflow_status_code =   ' || p_route_status);
1033           END IF;
1034 
1035           ENG_ICMDB_APIS_UTIL.Update_Approval_Status
1036           ( p_change_id => p_change_id
1037            ,p_base_change_mgmt_type_code => l_base_cm_type_code
1038            ,p_new_approval_status_cde  => p_appr_status
1039            ,p_workflow_status_code => p_route_status
1040            ,x_return_status      => l_return_status
1041            ,x_msg_count          => l_msg_count
1042            ,x_msg_data           => l_msg_data
1043            );
1044 
1045           IF g_debug_flag THEN
1046              Write_Debug('After: calling ENG_ICMDB_APIS_UTIL.Update_Approval_Status: ' || l_return_status) ;
1047           END IF;
1048 
1049           IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS )
1050           THEN
1051             x_return_status := l_return_status;
1052             x_msg_count := l_msg_count;
1053             x_msg_data := l_msg_data;
1054             --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_PS_API');
1055             --#FND_MESSAGE.Set_Token('OBJECT_NAME', 'ENG_NIR_UTIL_PKG.set_nir_item_approval_status');
1056             --#FND_MSG_PUB.Add;
1057             RAISE FND_API.G_EXC_ERROR;
1058           END IF;
1059 
1060           IF g_debug_flag THEN
1061              Write_Debug('Successful: ENG_ICMDB_APIS_UTIL.Update_Approval_Status');
1062           END IF;
1063 
1064         END IF; -- if ( Change Object is AMW Appl)
1065 
1066         IF g_debug_flag THEN
1067           Write_Debug('After: calling update DM attachment approval/review API');
1068         END IF;
1069 
1070 
1071         --
1072         -- R12B
1073         -- Document Lifecycle Support
1074         -- triggered by the header approval status update
1075         -- call ENG_DOCUMENT_UTIL.Update_Approval_Status if applicable
1076 
1077         IF ( l_doc_lc_object_flag)
1078         THEN
1079             IF g_debug_flag THEN
1080                Write_Debug('Before: calling ENG_DOCUMENT_UTIL.Update_Approval_Status');
1081                Write_Debug('  p_change_id =   ' || p_change_id);
1082                Write_Debug('  p_base_change_mgmt_type_code =   ' || l_base_cm_type_code);
1083                Write_Debug('  p_appr_status = ' || p_appr_status);
1084                Write_Debug('  p_workflow_status_code =   ' || p_route_status);
1085             END IF;
1086 
1087 
1088             ENG_DOCUMENT_UTIL.Update_Approval_Status
1089             ( p_api_version         => 1.0
1090              ,p_init_msg_list       => FND_API.G_FALSE
1091              ,p_commit              => FND_API.G_FALSE
1092              ,p_validation_level    => p_validation_level
1093              ,p_debug               => FND_API.G_FALSE
1094              ,p_output_dir          => p_output_dir
1095              ,p_debug_filename      => p_debug_filename
1096              ,x_return_status       => l_return_status     --
1097              ,x_msg_count           => l_msg_count         --
1098              ,x_msg_data            => l_msg_data          --
1099              ,p_change_id           => p_change_id         -- header's change_id
1100              ,p_approval_status     => p_appr_status       -- header approval status
1101              ,p_wf_route_status     => p_route_status      -- workflow routing status (for document types)
1102              ,p_api_caller          => p_api_caller        -- Optionnal for future use
1103             );
1104 
1105             IF g_debug_flag THEN
1106                Write_Debug('After: calling ENG_DOCUMENT_UTIL.Update_Approval_Status: ' || l_return_status) ;
1107             END IF;
1108 
1109             IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS )
1110             THEN
1111               x_return_status := l_return_status;
1112               x_msg_count := l_msg_count;
1113               x_msg_data := l_msg_data;
1114               RAISE FND_API.G_EXC_ERROR;
1115             END IF;
1116 
1117             IF g_debug_flag THEN
1118                Write_Debug('Successful: ENG_DOCUMENT_UTIL.Update_Approval_Status');
1119             END IF;
1120 
1121         END IF; -- if ( Change Object is Documet LC Object)
1122 
1123 
1124       END IF; -- if (l_updated = true)
1125 
1126     END IF; -- if (p_status_code's phase type is APPROVAL
1127 
1128 
1129 
1130     -- Standard ending code ------------------------------------------------
1131     IF FND_API.To_Boolean ( p_commit ) THEN
1132       COMMIT WORK;
1133     END IF;
1134 
1135     FND_MSG_PUB.Count_And_Get
1136     ( p_count        =>      x_msg_count,
1137       p_data         =>      x_msg_data );
1138 
1139     IF g_debug_flag THEN
1140       Write_Debug('Finish. End Of procedure: Update_Header_Appr_Status') ;
1141     END IF;
1142 
1143     IF FND_API.to_Boolean( p_debug ) THEN
1144       Close_Debug_Session;
1145     END IF;
1146 
1147   EXCEPTION
1148     WHEN FND_API.G_EXC_ERROR THEN
1149       --ROLLBACK TO Update_Header_Appr_Status;
1150       x_return_status := FND_API.G_RET_STS_ERROR;
1151       FND_MSG_PUB.Count_And_Get
1152       ( p_count        =>      x_msg_count
1153        ,p_data         =>      x_msg_data );
1154       IF g_debug_flag THEN
1155         Write_Debug('Rollback and Finish with expected error.') ;
1156       END IF;
1157       IF FND_API.to_Boolean( p_debug ) THEN
1158         Close_Debug_Session;
1159       END IF;
1160     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1161       --ROLLBACK TO Update_Header_Appr_Status;
1162       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1163       FND_MSG_PUB.Count_And_Get
1164       ( p_count        =>      x_msg_count
1165        ,p_data         =>      x_msg_data );
1166       IF g_debug_flag THEN
1167         Write_Debug('Rollback and Finish with unexpected error.') ;
1168       END IF;
1169       IF FND_API.to_Boolean( p_debug ) THEN
1170         Close_Debug_Session;
1171       END IF;
1172     WHEN OTHERS THEN
1173       --ROLLBACK TO Update_Header_Appr_Status;
1174       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1175       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )
1176       THEN
1177         FND_MSG_PUB.Add_Exc_Msg (  G_PKG_NAME, l_api_name );
1178       END IF;
1179       FND_MSG_PUB.Count_And_Get
1180       ( p_count        =>      x_msg_count
1181        ,p_data         =>      x_msg_data );
1182       IF g_debug_flag THEN
1183         Write_Debug('Rollback and Finish with other error.') ;
1184       END IF;
1185       IF FND_API.to_Boolean( p_debug ) THEN
1186         Close_Debug_Session;
1187       END IF;
1188 
1189   END Update_Header_Appr_Status;
1190 
1191   PROCEDURE Update_Header_Appr_Status
1192   (
1193     p_api_version               IN   NUMBER                             --
1194    ,p_init_msg_list             IN   VARCHAR2 := FND_API.G_FALSE        --
1195    ,p_commit                    IN   VARCHAR2 := FND_API.G_FALSE        --
1196    ,p_validation_level          IN   NUMBER   := FND_API.G_VALID_LEVEL_FULL
1197    ,p_debug                     IN   VARCHAR2 := FND_API.G_FALSE        --
1198    ,p_output_dir                IN   VARCHAR2 := NULL
1199    ,p_debug_filename            IN   VARCHAR2 := NULL
1200    ,x_return_status             OUT  NOCOPY  VARCHAR2                   --
1201    ,x_msg_count                 OUT  NOCOPY  NUMBER                     --
1202    ,x_msg_data                  OUT  NOCOPY  VARCHAR2                   --
1203    ,p_change_id                 IN   NUMBER                             -- header's change_id
1204    ,p_status_code               IN   NUMBER
1205    ,p_appr_status               IN   NUMBER                             -- header approval status
1206    ,p_route_status              IN   VARCHAR2                           -- workflow routing status (for document types)
1207    ,p_api_caller                IN   VARCHAR2 := 'UI'                   -- must
1208    ,p_bypass                    IN   VARCHAR2 := 'N'                    -- flag to bypass phase type check
1209   )
1210   IS
1211      l_sfa_line_items_exists VARCHAR2(1);
1212   BEGIN
1213       Update_Header_Appr_Status
1214       (
1215         p_api_version               =>  p_api_version
1216        ,p_init_msg_list             =>  p_init_msg_list
1217        ,p_commit                    =>  p_commit
1218        ,p_validation_level          =>  p_validation_level
1219        ,p_debug                     =>  p_debug
1220        ,p_output_dir                =>  p_output_dir
1221        ,p_debug_filename            =>  p_debug_filename
1222        ,x_return_status             =>  x_return_status
1223        ,x_msg_count                 =>  x_msg_count
1224        ,x_msg_data                  =>  x_msg_data
1225        ,p_change_id                 =>  p_change_id
1226        ,p_status_code               =>  p_status_code
1227        ,p_appr_status               =>  p_appr_status
1228        ,p_route_status              =>  p_route_status
1229        ,p_api_caller                =>  p_api_caller
1230        ,p_bypass                    =>  p_bypass
1231        ,x_sfa_line_items_exists     => l_sfa_line_items_exists
1232       );
1233   END Update_Header_Appr_Status;
1234 
1235 
1236   -- R12B
1237   -- Internal procedure to automatically launch line 'Not Started' workflow if necessary
1238   -- when line workflow is defined and Start After Status is specified phase.
1239   --
1240   PROCEDURE Start_Line_Workflow
1241   (
1242     p_api_version               IN   NUMBER                             --
1243    ,p_init_msg_list             IN   VARCHAR2 := FND_API.G_FALSE        --
1244    ,p_commit                    IN   VARCHAR2 := FND_API.G_FALSE        --
1245    ,p_validation_level          IN   NUMBER   := FND_API.G_VALID_LEVEL_FULL
1246    ,p_debug                     IN   VARCHAR2 := FND_API.G_FALSE        --
1247    ,p_output_dir                IN   VARCHAR2 := NULL
1248    ,p_debug_filename            IN   VARCHAR2 := NULL
1249    ,x_return_status             OUT  NOCOPY  VARCHAR2                   --
1250    ,x_msg_count                 OUT  NOCOPY  NUMBER                     --
1251    ,x_msg_data                  OUT  NOCOPY  VARCHAR2                   --
1252    ,p_change_id                 IN   NUMBER                             -- header's change_id
1253    ,p_new_status_code           IN   NUMBER                             -- new status code to be promoted to
1254    ,p_cur_status_code           IN   NUMBER                             -- curre status code
1255    ,p_api_caller                IN   VARCHAR2 := NULL                   -- can also be 'WF', or other internal procedure names
1256    ,p_action_type               IN   VARCHAR2 := NULL                   -- or PROMOTE, DEMOTE
1257   )
1258   IS
1259     l_api_name           CONSTANT VARCHAR2(30)  := 'Start_Line_Workflow';
1260     l_api_version        CONSTANT NUMBER := 1.0;
1261 
1262     l_return_status      VARCHAR2(1);
1263     l_msg_count          NUMBER;
1264     l_msg_data           VARCHAR2(2000);
1265 
1266     l_api_caller         VARCHAR2(2) := NULL;
1267     l_fnd_user_id        NUMBER := TO_NUMBER(FND_PROFILE.VALUE('USER_ID'));
1268     l_fnd_login_id       NUMBER := TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID'));
1269 
1270 
1271     l_change_line_id     NUMBER ;
1272     l_line_wf_route_id   NUMBER ;
1273     l_line_wf_item_key   wf_item_activity_statuses.item_key%TYPE := NULL;
1274 
1275 
1276     -- Select Line Workflow to be started for the passed status_code
1277     CURSOR c_line_wf ( c_change_id       IN NUMBER
1278                      , c_new_status_code IN NUMBER
1279                      , c_cur_status_code IN NUMBER
1280                      )
1281     IS
1282         SELECT DISTINCT route.route_id wf_route_id
1283              , line.change_line_id
1284         FROM eng_change_routes route,
1285              eng_change_statuses s,
1286              eng_change_lines line,
1287              eng_lifecycle_statuses line_start_after,
1288              eng_lifecycle_statuses new_status,
1289              eng_lifecycle_statuses cur_status
1290         WHERE route.status_code = Eng_Workflow_Util.G_RT_NOT_STARTED
1291         AND route.route_id = line.route_id
1292         AND s.status_type <> G_ENG_COMPLETED
1293         AND s.status_type <> G_ENG_IMPLEMENTED
1294         AND s.status_type <> G_ENG_CANCELLED
1295         AND s.status_code = line.status_code
1296         AND line.start_after_status_code = c_new_status_code
1297         AND line.change_id = c_change_id
1298         AND line_start_after.sequence_number <= new_status.sequence_number
1299         AND line_start_after.sequence_number > cur_status.sequence_number
1300         AND line_start_after.entity_name = G_ENG_CHANGE
1301         AND line_start_after.entity_id1 = c_change_id
1302         AND line_start_after.status_code = line.start_after_status_code
1303         AND line_start_after.active_flag = 'Y'
1304         AND new_status.entity_name =  G_ENG_CHANGE
1305         AND new_status.entity_id1 = c_change_id
1306         AND new_status.status_code = c_new_status_code
1307         AND new_status.active_flag = 'Y'
1308         AND cur_status.entity_name = G_ENG_CHANGE
1309         AND cur_status.entity_id1 = c_change_id
1310         AND cur_status.status_code = c_cur_status_code
1311         AND cur_status.active_flag = 'Y' ;
1312 
1313   BEGIN
1314     -- Standard Start of API savepoint
1315     --SAVEPOINT Start_Line_Workflow;
1316     -- Standard call to check for call compatibility
1317     IF NOT FND_API.Compatible_API_Call ( l_api_version
1318                                         ,p_api_version
1319                                         ,l_api_name
1320                                         ,G_PKG_NAME )
1321     THEN
1322       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1323     END IF;
1324     -- Initialize message list if p_init_msg_list is set to TRUE.
1325     IF FND_API.to_Boolean( p_init_msg_list ) THEN
1326        FND_MSG_PUB.initialize;
1327     END IF;
1328 
1329     -- For Test/Debug
1330     Check_And_Open_Debug_Session(p_debug, p_output_dir, p_debug_filename) ;
1331     -- R12 Comment out
1332     -- IF FND_API.to_Boolean( p_debug ) THEN
1333     --     Open_Debug_Session(p_output_dir, p_debug_filename ) ;
1334     -- END IF;
1335 
1336     -- Write debug message if debug mode is on
1337     IF g_debug_flag THEN
1338        Write_Debug('ENG_CHANGE_LIFECYCLE_UTIL.Start_Line_Workflow log');
1339        Write_Debug('-----------------------------------------------------');
1340        Write_Debug('p_change_id         : ' || p_change_id );
1341        Write_Debug('p_new_status_code       : ' || p_new_status_code );
1342        Write_Debug('p_cur_status_code       : ' || p_cur_status_code);
1343        Write_Debug('-----------------------------------------------------');
1344        Write_Debug('Initializing return status... ' );
1345     END IF;
1346 
1347     -- Initialize API return status to success
1348     x_return_status := FND_API.G_RET_STS_SUCCESS;
1349 
1350     -- FND_PROFILE package is not available for workflow (WF),
1351     -- therefore manually set WHO column values
1352     IF p_api_caller = 'WF' THEN
1353       l_fnd_user_id := G_ENG_WF_USER_ID;
1354       l_fnd_login_id := G_ENG_WF_LOGIN_ID;
1355     ELSIF p_api_caller = 'CP' THEN
1356       l_fnd_user_id := G_ENG_CP_USER_ID;
1357       l_fnd_login_id := G_ENG_CP_LOGIN_ID;
1358     END IF;
1359 
1360     -- Real code starts here -----------------------------------------------
1361 
1362     -- Start phase workflow only for PROMOTE action
1363     -- (When demoting to a new phase, do not auto-start new phase workflow)
1364     IF g_debug_flag THEN
1365        Write_Debug('Before: handling new phase workflow');
1366     END IF;
1367 
1368     IF (p_action_type = G_ENG_DEMOTE) THEN
1369       IF g_debug_flag THEN
1370          Write_Debug('  Branch: p_action_type = DEMOTE, do nothing');
1371       END IF;
1372 
1373     ELSE  --G_ENG_PROMOTE
1374 
1375       IF g_debug_flag THEN
1376          Write_Debug('  Branch: p_action_type is not DEMOTE, check if Line workflow auto-start is needed');
1377       END IF;
1378 
1379 
1380       FOR l_rec IN c_line_wf (c_change_id => p_change_id ,
1381                               c_new_status_code => p_new_status_code ,
1382                               c_cur_status_code => p_cur_status_code
1383                               )
1384       LOOP
1385 
1386         l_change_line_id     := l_rec.change_line_id  ;
1387         l_line_wf_route_id   := l_rec.wf_route_id  ;
1388         l_line_wf_item_key   := NULL;
1389 
1390         IF g_debug_flag THEN
1391           Write_Debug('calling Eng_Workflow_Util.StartWorkflow API for Line') ;
1392           Write_Debug('l_change_line_id: '  || TO_CHAR(l_change_line_id) ) ;
1393           Write_Debug('l_line_wf_route_id: '  || TO_CHAR(l_line_wf_route_id) ) ;
1394         END IF;
1395 
1396         -- start phase-level workflow
1397         Eng_Workflow_Util.StartWorkflow
1398         (  p_api_version       => 1.0
1399         ,  p_init_msg_list     => FND_API.G_FALSE
1400         ,  p_commit            => FND_API.G_FALSE
1401         ,  p_validation_level  => FND_API.G_VALID_LEVEL_FULL
1402         ,  x_return_status     => l_return_status
1403         ,  x_msg_count         => l_msg_count
1404         ,  x_msg_data          => l_msg_data
1405         ,  p_item_type         => Eng_Workflow_Util.G_CHANGE_ROUTE_ITEM_TYPE
1406         ,  x_item_key          => l_line_wf_item_key
1407         ,  p_process_name      => Eng_Workflow_Util.G_ROUTE_AGENT_PROC
1408         ,  p_change_id         => p_change_id
1409         ,  p_change_line_id    => l_change_line_id
1410         ,  p_wf_user_id        => l_fnd_user_id
1411         ,  p_route_id          => l_line_wf_route_id
1412         ,  p_debug             => p_debug --FND_API.G_FALSE
1413         ,  p_output_dir        => p_output_dir
1414         ,  p_debug_filename    => NULL
1415         ) ;
1416 
1417         IF g_debug_flag THEN
1418           Write_Debug('After: calling Eng_Workflow_Util.StartWorkflow API for ' || TO_CHAR(l_change_line_id) || l_return_status) ;
1419           Write_Debug('l_line_wf_item_key: '  || l_line_wf_item_key) ;
1420         END IF;
1421 
1422         IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS )
1423         THEN
1424           x_return_status := l_return_status;
1425           x_msg_count := l_msg_count;
1426           x_msg_data := l_msg_data;
1427         ELSE
1428           IF g_debug_flag THEN
1429              Write_Debug('Successful: calling workflow routing agent');
1430           END IF;
1431         END IF;
1432 
1433       END LOOP ; -- c_line_wf loop
1434 
1435     END IF; -- if (action = PROMOTE)
1436 
1437     -- Standard ending code ------------------------------------------------
1438     IF FND_API.To_Boolean ( p_commit ) THEN
1439       COMMIT WORK;
1440     END IF;
1441 
1442     FND_MSG_PUB.Count_And_Get
1443     ( p_count        =>      x_msg_count,
1444       p_data         =>      x_msg_data );
1445 
1446     IF g_debug_flag THEN
1447       Write_Debug('Finish. End Of procedure: Start_Line_Workflow');
1448     END IF;
1449 
1450     IF FND_API.to_Boolean( p_debug ) THEN
1451       Close_Debug_Session;
1452     END IF;
1453 
1454   EXCEPTION
1455     WHEN FND_API.G_EXC_ERROR THEN
1456       --ROLLBACK TO Start_Line_Workflow;
1457       x_return_status := FND_API.G_RET_STS_ERROR;
1458       FND_MSG_PUB.Count_And_Get
1459       ( p_count        =>      x_msg_count
1460        ,p_data         =>      x_msg_data );
1461       IF g_debug_flag THEN
1462         Write_Debug('Rollback and Finish with expected error.') ;
1463       END IF;
1464       IF FND_API.to_Boolean( p_debug ) THEN
1465         Close_Debug_Session;
1466       END IF;
1467     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1468       --ROLLBACK TO Start_Line_Workflow;
1469       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1470       FND_MSG_PUB.Count_And_Get
1471       ( p_count        =>      x_msg_count
1472        ,p_data         =>      x_msg_data );
1473       IF g_debug_flag THEN
1474         Write_Debug('Rollback and Finish with unexpected error.') ;
1475       END IF;
1476       IF FND_API.to_Boolean( p_debug ) THEN
1477         Close_Debug_Session;
1478       END IF;
1479     WHEN OTHERS THEN
1480       --ROLLBACK TO Start_Line_Workflow;
1481       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1482       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )
1483       THEN
1484         FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME, l_api_name );
1485       END IF;
1486       FND_MSG_PUB.Count_And_Get
1487       ( p_count        =>      x_msg_count
1488        ,p_data         =>      x_msg_data );
1489       IF g_debug_flag THEN
1490         Write_Debug('Rollback and Finish with other error.') ;
1491       END IF;
1492       IF FND_API.to_Boolean( p_debug ) THEN
1493         Close_Debug_Session;
1494       END IF;
1495   END Start_Line_Workflow ;
1496 
1497 
1498 
1499 
1500   -- Internal procedure to automatically launch workflow if necessary
1501   -- (i.e., when workflow is defined) for the specified phase
1502   -- Note that this procedure may also submit the concurrent program for
1503   -- implementing ECO as well!!!
1504   PROCEDURE Start_WF_OnlyIf_Necessary
1505   (
1506     p_api_version               IN   NUMBER                             --
1507    ,p_init_msg_list             IN   VARCHAR2 := FND_API.G_FALSE        --
1508    ,p_commit                    IN   VARCHAR2 := FND_API.G_FALSE        --
1509    ,p_validation_level          IN   NUMBER   := FND_API.G_VALID_LEVEL_FULL
1510    ,p_debug                     IN   VARCHAR2 := FND_API.G_FALSE        --
1511    ,p_output_dir                IN   VARCHAR2 := NULL
1512    ,p_debug_filename            IN   VARCHAR2 := NULL
1513    ,x_return_status             OUT  NOCOPY  VARCHAR2                   --
1514    ,x_msg_count                 OUT  NOCOPY  NUMBER                     --
1515    ,x_msg_data                  OUT  NOCOPY  VARCHAR2                   --
1516    ,p_change_id                 IN   NUMBER                             -- header's change_id
1517    ,p_status_code               IN   NUMBER                             -- new phase
1518    ,p_status_type               IN   NUMBER                             -- new phase type
1519    ,p_sequence_number           IN   NUMBER                             -- new phase sequence number
1520    ,p_imp_eco_flag              IN   VARCHAR2 := 'N'                    -- flag for implementECO
1521    ,p_api_caller                IN   VARCHAR2 := NULL                   -- can also be 'WF', or other internal procedure names
1522    ,p_action_type               IN   VARCHAR2 := NULL                   -- or PROMOTE, DEMOTE
1523    ,p_comment                   IN   VARCHAR2 := NULL                   -- only used for co promote-to-implement action
1524    ,p_skip_wf                   IN   VARCHAR2 := 'N'                    -- used for eco's last implement phase
1525   )
1526   IS
1527     l_api_name           CONSTANT VARCHAR2(30)  := 'Start_WF_OnlyIf_Necessary';
1528     l_api_version        CONSTANT NUMBER := 1.0;
1529 
1530     l_return_status      VARCHAR2(1);
1531     l_msg_count          NUMBER;
1532     l_msg_data           VARCHAR2(2000);
1533 
1534     l_api_caller         VARCHAR2(2) := NULL;
1535     l_fnd_user_id        NUMBER := TO_NUMBER(FND_PROFILE.VALUE('USER_ID'));
1536     l_fnd_login_id       NUMBER := TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID'));
1537 
1538     l_action_id          eng_change_actions.action_id%TYPE;
1539 
1540     l_wf_route_id        eng_lifecycle_statuses.change_wf_route_id%TYPE;
1541     l_wf_route_temp_id   eng_lifecycle_statuses.change_wf_route_template_id%TYPE;
1542     l_wf_item_key        wf_item_activity_statuses.item_key%TYPE := NULL;
1543 
1544     l_chg_notice         eng_engineering_changes.change_notice%TYPE;
1545     l_org_id             eng_engineering_changes.organization_id%TYPE;
1546     l_request_id         NUMBER;
1547 
1548     l_min_appr_sn        eng_lifecycle_statuses.sequence_number%TYPE;
1549     l_doc_lc_object_flag BOOLEAN := FALSE ;
1550 
1551 
1552   BEGIN
1553     -- Standard Start of API savepoint
1554     --SAVEPOINT Start_WF_OnlyIf_Necessary;
1555     -- Standard call to check for call compatibility
1556     IF NOT FND_API.Compatible_API_Call ( l_api_version
1557                                         ,p_api_version
1558                                         ,l_api_name
1559                                         ,G_PKG_NAME )
1560     THEN
1561       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1562     END IF;
1563     -- Initialize message list if p_init_msg_list is set to TRUE.
1564     IF FND_API.to_Boolean( p_init_msg_list ) THEN
1565        FND_MSG_PUB.initialize;
1566     END IF;
1567 
1568     -- For Test/Debug
1569     Check_And_Open_Debug_Session(p_debug, p_output_dir, p_debug_filename) ;
1570     -- R12 Comment out
1571     -- IF FND_API.to_Boolean( p_debug ) THEN
1572     --     Open_Debug_Session(p_output_dir, p_debug_filename ) ;
1573     -- END IF;
1574 
1575     -- Write debug message if debug mode is on
1576     IF g_debug_flag THEN
1577        Write_Debug('ENG_CHANGE_LIFECYCLE_UTIL.Start_WF_OnlyIf_Necessary log');
1578        Write_Debug('-----------------------------------------------------');
1579        Write_Debug('p_change_id         : ' || p_change_id );
1580        Write_Debug('p_status_code       : ' || p_status_code );
1581        Write_Debug('-----------------------------------------------------');
1582        Write_Debug('Initializing return status... ' );
1583     END IF;
1584 
1585     -- Initialize API return status to success
1586     x_return_status := FND_API.G_RET_STS_SUCCESS;
1587 
1588     -- FND_PROFILE package is not available for workflow (WF),
1589     -- therefore manually set WHO column values
1590     IF p_api_caller = 'WF' THEN
1591       l_fnd_user_id := G_ENG_WF_USER_ID;
1592       l_fnd_login_id := G_ENG_WF_LOGIN_ID;
1593     ELSIF p_api_caller = 'CP' THEN
1594       l_fnd_user_id := G_ENG_CP_USER_ID;
1595       l_fnd_login_id := G_ENG_CP_LOGIN_ID;
1596     END IF;
1597 
1598     -- Real code starts here -----------------------------------------------
1599 
1600     -- Log action log only for calls not coming from the UI
1601     -- (implying automatic promotion/demotion),
1602     -- but not from Init_Lifecycle procedure
1603     -- Also: skip logging action and status change workflow in case of special
1604     -- ECO implement phase
1605     IF (    (p_api_caller IS NULL OR p_api_caller <> 'UI')
1606          AND p_action_type IS NOT NULL
1607          AND(p_imp_eco_flag IS NULL OR p_imp_eco_flag <> 'Y')
1608          )
1609     THEN
1610 
1611       IF g_debug_flag THEN
1612          Write_Debug('Before: saving action log');
1613       END IF;
1614       l_action_id := 0;
1615       -- create new action log
1616       ENG_CHANGE_ACTIONS_UTIL.Create_Change_Action
1617       ( p_api_version           => 1.0
1618       , p_init_msg_list         => FND_API.G_FALSE        --
1619       , p_commit                => FND_API.G_FALSE        --
1620       , p_validation_level      => FND_API.G_VALID_LEVEL_FULL
1621       , p_debug                 => p_debug --FND_API.G_FALSE
1622       , p_output_dir            => p_output_dir
1623       , p_debug_filename        => NULL
1624       , x_return_status         => l_return_status
1625       , x_msg_count             => l_msg_count
1626       , x_msg_data              => l_msg_data
1627       , p_action_type           => p_action_type
1628       , p_object_name           => G_ENG_CHANGE
1629       , p_object_id1            => p_change_id
1630       , p_object_id2            => NULL
1631       , p_object_id3            => NULL
1632       , p_object_id4            => NULL
1633       , p_object_id5            => NULL
1634       , p_parent_action_id      => -1
1635       , p_status_code           => p_status_code
1636       , p_action_date           => SYSDATE
1637       , p_change_description    => NULL
1638       , p_user_id               => l_fnd_user_id
1639       , p_api_caller            => p_api_caller
1640       , x_change_action_id      => l_action_id
1641       );
1642       IF g_debug_flag THEN
1643          Write_Debug('After: saving action log: ' || l_return_status) ;
1644          Write_Debug('l_action_id       : ' || l_action_id );
1645       END IF;
1646       IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS )
1647       THEN
1648         x_return_status := l_return_status;
1649         x_msg_count := l_msg_count;
1650         x_msg_data := l_msg_data;
1651         --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_PS_API');
1652         --#FND_MESSAGE.Set_Token('OBJECT_NAME', 'ENG_CHANGE_ACTIONS_UTIL.Create_Change_Action');
1653         --#FND_MSG_PUB.Add;
1654         RAISE FND_API.G_EXC_ERROR;
1655       END IF;
1656       IF g_debug_flag THEN
1657          Write_Debug('Successful: saving action log');
1658       END IF;
1659 
1660 
1661       -- R12B
1662       -- In case of Document LC Change Object
1663       -- We don't need to start workflow and riase event
1664       l_doc_lc_object_flag := ENG_DOCUMENT_UTIL.Is_Dom_Document_Lifecycle
1665                              ( p_change_id => p_change_id
1666                              , p_base_change_mgmt_type_code => NULL
1667                              )  ;
1668 
1669       IF (NOT l_doc_lc_object_flag)
1670       THEN
1671 
1672           ENG_CHANGE_BES_UTIL.Raise_Status_Change_Event
1673           ( p_change_id         => p_change_id
1674            ,p_status_code       => p_status_code
1675            ,p_action_type       => p_action_type
1676            ,p_action_id         => l_action_id
1677            );
1678 
1679 
1680           -- Force commit to make sure workflow picks up the latest phase
1681           COMMIT WORK;
1682 
1683           -- launch the standard action workflow for new action if needed
1684           IF g_debug_flag THEN
1685             Write_Debug('Before: calling status change workflow API');
1686           END IF;
1687 
1688           Eng_Workflow_Util.StartWorkflow
1689           (  p_api_version       => 1.0
1690           ,  p_init_msg_list     => FND_API.G_FALSE
1691           ,  p_commit            => FND_API.G_FALSE
1692           ,  p_validation_level  => FND_API.G_VALID_LEVEL_FULL
1693           ,  x_return_status     => l_return_status
1694           ,  x_msg_count         => l_msg_count
1695           ,  x_msg_data          => l_msg_data
1696           ,  p_item_type         => Eng_Workflow_Util.G_CHANGE_ACTION_ITEM_TYPE
1697           ,  x_item_key          => l_wf_item_key
1698           ,  p_process_name      => Eng_Workflow_Util.G_STATUS_CHANGE_PROC
1699           ,  p_change_id         => p_change_id
1700           ,  p_action_id         => l_action_id
1701           ,  p_wf_user_id        => l_fnd_user_id
1702           ,  p_route_id          => 0 --l_wf_route_id
1703           ,  p_debug             => p_debug --FND_API.G_FALSE
1704           ,  p_output_dir        => p_output_dir
1705           ,  p_debug_filename    => NULL
1706           ) ;
1707           -- note that the returned wf item_key won't be saved on the ENG side
1708           IF g_debug_flag THEN
1709             Write_Debug('After: calling status change workflow API: ' || l_return_status) ;
1710           END IF;
1711 
1712           IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS )
1713           THEN
1714             x_return_status := l_return_status;
1715             x_msg_count := l_msg_count;
1716             x_msg_data := l_msg_data;
1717             --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_WF_API');
1718             --#FND_MSG_PUB.Add;
1719             --#RAISE FND_API.G_EXC_ERROR;
1720           END IF;
1721           l_wf_item_key := NULL;
1722 
1723           IF g_debug_flag THEN
1724             Write_Debug('Successful: calling status change workflow API');
1725           END IF;
1726 
1727        END IF ;   -- IF (NOT l_doc_lc_object_flag)
1728 
1729     END IF; -- if (not calling from UI)
1730 
1731     -- Start phase workflow only for PROMOTE action
1732     -- (When demoting to a new phase, do not auto-start new phase workflow)
1733     IF g_debug_flag THEN
1734        Write_Debug('Before: handling new phase workflow');
1735     END IF;
1736 
1737     IF (p_action_type = G_ENG_DEMOTE) THEN
1738       IF g_debug_flag THEN
1739          Write_Debug('  Branch: p_action_type = DEMOTE, do nothing');
1740       END IF;
1741 
1742     ELSE  --G_ENG_PROMOTE
1743       IF g_debug_flag THEN
1744          Write_Debug('  Branch: p_action_type is not DEMOTE, check if workflow auto-start is needed');
1745       END IF;
1746       -- Check if automatic wf start is needed
1747       SELECT change_wf_route_id, change_wf_route_template_id
1748         INTO l_wf_route_id, l_wf_route_temp_id
1749         FROM eng_lifecycle_statuses
1750         WHERE entity_name = G_ENG_CHANGE
1751           AND entity_id1 = p_change_id
1752           AND status_code = p_status_code
1753           AND active_flag = 'Y'
1754           AND rownum = 1;
1755 
1756 
1757       IF (p_skip_wf <> 'Y'   -- fix for bug 3479509 design change of launching wf after concurrent program
1758           AND l_wf_route_id IS NOT NULL )
1759       THEN
1760         IF g_debug_flag THEN
1761            Write_Debug('Auto-starting workflow is needed, calling workflow routing agent');
1762         END IF;
1763 
1764         -- start phase-level workflow
1765         Eng_Workflow_Util.StartWorkflow
1766         (  p_api_version       => 1.0
1767         ,  p_init_msg_list     => FND_API.G_FALSE
1768         ,  p_commit            => FND_API.G_FALSE
1769         ,  p_validation_level  => FND_API.G_VALID_LEVEL_FULL
1770         ,  x_return_status     => l_return_status
1771         ,  x_msg_count         => l_msg_count
1772         ,  x_msg_data          => l_msg_data
1773         ,  p_item_type         => Eng_Workflow_Util.G_CHANGE_ROUTE_ITEM_TYPE
1774         ,  x_item_key          => l_wf_item_key
1775         ,  p_process_name      => Eng_Workflow_Util.G_ROUTE_AGENT_PROC
1776         ,  p_change_id         => p_change_id
1777         ,  p_wf_user_id        => l_fnd_user_id
1778         ,  p_route_id          => l_wf_route_id
1779         ,  p_debug             => p_debug --FND_API.G_FALSE
1780         ,  p_output_dir        => p_output_dir
1781         ,  p_debug_filename    => NULL
1782         ) ;
1783 
1784         IF g_debug_flag THEN
1785           Write_Debug('After: calling Eng_Workflow_Util.StartWorkflow API: ' || l_return_status) ;
1786         END IF;
1787 
1788 
1789         IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS )
1790         THEN
1791           x_return_status := l_return_status;
1792           x_msg_count := l_msg_count;
1793           x_msg_data := l_msg_data;
1794           --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_WF_API');
1795           --#FND_MSG_PUB.Add;
1796           --#RAISE FND_API.G_EXC_ERROR;
1797         ELSE
1798           IF g_debug_flag THEN
1799              Write_Debug('Successful: calling workflow routing agent');
1800           END IF;
1801         END IF;
1802         l_wf_item_key := NULL;
1803 
1804       -- If phase workflow is defined by wf template id, but wf instance id
1805       -- has not been populated, do nothing
1806       ELSIF (p_skip_wf <> 'Y'   -- fix for bug 3479509 design change of launching wf after concurrent program
1807              AND l_wf_Route_id IS NULL AND l_wf_route_temp_id IS NOT NULL ) THEN
1808         IF g_debug_flag THEN
1809            Write_Debug('Only phase workflow template id is defined,');
1810            Write_Debug('Workflow instance id needs to be populated before the phase workflow can be started');
1811         END IF;
1812 
1813       -- If p_skip_wf is true, or there is no workflow defined for the phase at all
1814       ELSE
1815         IF g_debug_flag THEN
1816            Write_Debug('Auto-starting workflow is not needed, do post workflow update right away');
1817         END IF;
1818 
1819         -- determine API caller - 'UI' should be used only once in case promotion/demote recursion occurs
1820         IF (p_api_caller = 'WF' OR p_api_caller = 'CP') THEN
1821           l_api_caller := p_api_caller;
1822         ELSE
1823           l_api_caller := NULL;
1824         END IF;
1825 
1826         -- Do post workflow update right away
1827         Update_Lifecycle_States
1828         (
1829           p_api_version        => 1.0
1830          ,p_init_msg_list      => FND_API.G_FALSE
1831          ,p_commit             => FND_API.G_FALSE
1832          ,p_validation_level   => p_validation_level
1833          ,p_debug              => FND_API.G_FALSE
1834          ,p_output_dir         => p_output_dir
1835          ,p_debug_filename     => p_debug_filename
1836          ,x_return_status      => l_return_status
1837          ,x_msg_count          => l_msg_count
1838          ,x_msg_data           => l_msg_data
1839          ,p_change_id          => p_change_id
1840          ,p_api_caller         => l_api_caller
1841          ,p_wf_route_id        => NULL
1842          ,p_route_status       => NULL
1843          ,p_comment            => p_comment
1844         );
1845 
1846         IF g_debug_flag THEN
1847           Write_Debug('After: calling Update_Lifecycle_States API: ' || l_return_status) ;
1848         END IF;
1849 
1850 
1851         IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS ) THEN
1852           x_return_status := l_return_status;
1853           x_msg_count := l_msg_count;
1854           x_msg_data := l_msg_data;
1855           --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_PS_API');
1856           --#FND_MESSAGE.Set_Token('OBJECT_NAME', 'Update_Lifecycle_States');
1857           --#FND_MSG_PUB.Add;
1858           RAISE FND_API.G_EXC_ERROR;
1859         END IF;
1860 
1861       END IF; -- if (phase workflow instance id and workflow template id)
1862 
1863     END IF; -- if (action = PROMOTE)
1864 
1865     -- Standard ending code ------------------------------------------------
1866     IF FND_API.To_Boolean ( p_commit ) THEN
1867       COMMIT WORK;
1868     END IF;
1869 
1870     FND_MSG_PUB.Count_And_Get
1871     ( p_count        =>      x_msg_count,
1872       p_data         =>      x_msg_data );
1873 
1874     IF g_debug_flag THEN
1875       Write_Debug('Finish. End Of procedure: Start_WF_OnlyIf_Necessary');
1876     END IF;
1877 
1878     IF FND_API.to_Boolean( p_debug ) THEN
1879       Close_Debug_Session;
1880     END IF;
1881 
1882   EXCEPTION
1883     WHEN FND_API.G_EXC_ERROR THEN
1884       --ROLLBACK TO Start_WF_OnlyIf_Necessary;
1885       x_return_status := FND_API.G_RET_STS_ERROR;
1886       FND_MSG_PUB.Count_And_Get
1887       ( p_count        =>      x_msg_count
1888        ,p_data         =>      x_msg_data );
1889       IF g_debug_flag THEN
1890         Write_Debug('Rollback and Finish with expected error.') ;
1891       END IF;
1892       IF FND_API.to_Boolean( p_debug ) THEN
1893         Close_Debug_Session;
1894       END IF;
1895     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1896       --ROLLBACK TO Start_WF_OnlyIf_Necessary;
1897       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1898       FND_MSG_PUB.Count_And_Get
1899       ( p_count        =>      x_msg_count
1900        ,p_data         =>      x_msg_data );
1901       IF g_debug_flag THEN
1902         Write_Debug('Rollback and Finish with unexpected error.') ;
1903       END IF;
1904       IF FND_API.to_Boolean( p_debug ) THEN
1905         Close_Debug_Session;
1906       END IF;
1907     WHEN OTHERS THEN
1908       --ROLLBACK TO Start_WF_OnlyIf_Necessary;
1909       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1910       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )
1911       THEN
1912         FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME, l_api_name );
1913       END IF;
1914       FND_MSG_PUB.Count_And_Get
1915       ( p_count        =>      x_msg_count
1916        ,p_data         =>      x_msg_data );
1917       IF g_debug_flag THEN
1918         Write_Debug('Rollback and Finish with other error.') ;
1919       END IF;
1920       IF FND_API.to_Boolean( p_debug ) THEN
1921         Close_Debug_Session;
1922       END IF;
1923   END Start_WF_OnlyIf_Necessary;
1924 
1925 
1926   -- Internal procedure for promotion of change header (inc. revItems)
1927   PROCEDURE Promote_Header
1928   (
1929     p_api_version               IN   NUMBER                             --
1930    ,p_init_msg_list             IN   VARCHAR2 := FND_API.G_FALSE        --
1931    ,p_commit                    IN   VARCHAR2 := FND_API.G_FALSE        --
1932    ,p_validation_level          IN   NUMBER   := FND_API.G_VALID_LEVEL_FULL
1933    ,p_debug                     IN   VARCHAR2 := FND_API.G_FALSE        --
1934    ,p_output_dir                IN   VARCHAR2 := NULL
1935    ,p_debug_filename            IN   VARCHAR2 := NULL
1936    ,x_return_status             OUT  NOCOPY  VARCHAR2                   --
1937    ,x_msg_count                 OUT  NOCOPY  NUMBER                     --
1938    ,x_msg_data                  OUT  NOCOPY  VARCHAR2                   --
1939    ,p_change_id                 IN   NUMBER                             -- header's change_id
1940    ,p_status_code               IN   NUMBER                             -- new phase
1941    ,p_update_ri_flag            IN   VARCHAR2 := 'Y'                    -- can also be 'N'
1942    ,p_api_caller                IN   VARCHAR2 := NULL                   -- can also be 'WF'
1943    ,p_comment                   IN   VARCHAR2 := NULL                   -- only used for co promote-to-implement action
1944   )
1945   IS
1946     l_api_name           CONSTANT VARCHAR2(30)  := 'Promote_Header';
1947     l_api_version        CONSTANT NUMBER := 1.0;
1948 
1949     l_fnd_user_id        NUMBER := TO_NUMBER(FND_PROFILE.VALUE('USER_ID'));
1950     l_fnd_login_id       NUMBER := TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID'));
1951 
1952     l_is_imp_phase_used  VARCHAR2(1) := 'F';
1953 
1954     l_last_status_type   NUMBER;
1955     l_last_status_code   NUMBER;
1956 
1957     l_return_status      VARCHAR2(1);
1958     l_msg_count          NUMBER;
1959     l_msg_data           VARCHAR2(2000);
1960 
1961     l_curr_status_code   eng_engineering_changes.status_code%TYPE;
1962 
1963     l_curr_phase_sn      eng_lifecycle_statuses.sequence_number%TYPE;
1964     l_new_phase_sn       eng_lifecycle_statuses.sequence_number%TYPE;
1965     l_max_sn             eng_lifecycle_statuses.sequence_number%TYPE;
1966     l_phase_sn           eng_lifecycle_statuses.sequence_number%TYPE;
1967 
1968     l_new_status_type    eng_change_statuses.status_type%TYPE;
1969     l_ri_status_type     eng_change_statuses.status_type%TYPE;
1970 
1971     l_skip_promotion     VARCHAR2(1) := 'N';
1972 
1973     -- Select status_type of all lines of the header
1974     CURSOR c_lines IS
1975       SELECT s.status_type,
1976              l.complete_before_status_code,
1977              l.required_flag,
1978              l.change_type_id ,
1979              route.status_code wf_status_code
1980         FROM eng_change_routes route,
1981              eng_change_statuses s,
1982              eng_change_lines l
1983         WHERE s.status_code = l.status_code
1984         AND route.route_id(+) = l.route_id
1985         AND l.change_id = p_change_id ;
1986 
1987     l_line_status_type   eng_change_statuses.status_type%TYPE;
1988     l_status_code        eng_change_lines.complete_before_status_code%TYPE;
1989     l_required_flag      eng_change_lines.required_flag%TYPE := NULL;
1990     l_change_type_id     NUMBER ;
1991     l_wf_status_code     eng_change_routes.status_code%TYPE;
1992 
1993     -- Select all revised items
1994     CURSOR c_revItems IS
1995       SELECT status_code
1996         FROM eng_revised_items
1997         WHERE change_id = p_change_id
1998       FOR UPDATE;
1999 
2000 
2001     l_ri_status_code      eng_change_lines.status_code%TYPE;
2002     -- revItem's status_code's sequence_number
2003     l_ri_phase_sn         eng_lifecycle_statuses.sequence_number%TYPE;
2004 
2005     l_last_imp_flag       VARCHAR2(1) := 'N';
2006     l_cm_type_code        eng_engineering_changes.CHANGE_MGMT_TYPE_CODE%TYPE;
2007     l_base_cm_type_code   eng_change_order_types.BASE_CHANGE_MGMT_TYPE_CODE%TYPE;
2008     l_imp_eco_flag        VARCHAR2(1) := 'N';
2009 
2010     --l_co_type_id          eng_engineering_changes.CHANGE_ORDER_TYPE_ID%TYPE;
2011     l_auto_prop_flag      eng_type_org_properties.AUTO_PROPAGATE_FLAG%TYPE;
2012     l_change_notice       eng_engineering_changes.change_notice%TYPE;
2013     l_hierarchy_name      per_organization_structures.name%TYPE;
2014     l_org_name            org_organization_definitions.organization_name%TYPE;
2015     l_row_cnt             NUMBER := 0;
2016 
2017     l_doc_lc_object_flag  BOOLEAN := FALSE ;
2018 
2019 
2020     CURSOR c_orgProp IS
2021       SELECT op.auto_propagate_flag,
2022              ec.change_notice,
2023              pos.name,
2024              ood.name organization_name
2025         FROM eng_type_org_properties op,
2026              eng_engineering_changes ec,
2027              per_organization_structures pos,
2028              hr_all_organization_units_tl ood
2029         WHERE ec.change_id = p_change_id
2030           --AND ec.PLM_OR_ERP_CHANGE = 'PLM'
2031           AND op.change_type_id = ec.change_order_type_id
2032           AND op.organization_id = ec.organization_id
2033           AND op.propagation_status_code = p_status_code
2034           AND ec.hierarchy_id IS NOT NULL
2035           AND ec.organization_id IS NOT NULL
2036           AND pos.organization_structure_id(+) = ec.hierarchy_id
2037           AND ood.organization_id(+) = ec.organization_id
2038           AND ood.LANGUAGE = USERENV('LANG')
2039           -- R12 UT: Added where clause to not autopropagated if propagation has
2040           -- been initiated to any of the organizations manually or by the
2041           -- TTM process.
2042           AND NOT EXISTS (SELECT 1
2043                             FROM eng_change_obj_relationships
2044                            WHERE change_id = ec.change_id
2045                              AND object_to_name = 'ENG_CHANGE'
2046                              AND relationship_code = 'PROPAGATED_TO');
2047 
2048     --Bug No: 4767315
2049     --returns Y if there exists no revised items which are not cancelled for the CO
2050     CURSOR c_no_revisedItem IS
2051       SELECT 'Y'
2052       FROM DUAL
2053       WHERE not exists (SELECT 1
2054                         FROM eng_revised_items
2055                         WHERE change_id=p_change_id
2056                         AND status_code <> 5
2057                         );
2058 
2059     l_request_id          NUMBER := 0;
2060 
2061     l_action_id           eng_change_actions.action_id%TYPE;
2062 
2063     l_wf_status           eng_lifecycle_statuses.workflow_status%TYPE;
2064     l_wf_route_id         eng_lifecycle_statuses.change_wf_route_id%TYPE;
2065     l_new_route_id        eng_lifecycle_statuses.change_wf_route_id%TYPE;
2066 
2067     l_found_rev_item      VARCHAR2(1) := 'N';
2068 
2069     l_skip_wf             VARCHAR2(1) := 'N';
2070 
2071     l_skip        VARCHAR2(1) := 'N';
2072 
2073     l_eco_approval_status VARCHAR2(1); -- Bug 3769329
2074 
2075   BEGIN
2076 
2077     -- Standard Start of API savepoint
2078     SAVEPOINT Promote_Header;
2079     -- Standard call to check for call compatibility
2080     IF NOT FND_API.Compatible_API_Call ( l_api_version
2081                                         ,p_api_version
2082                                         ,l_api_name
2083                                         ,G_PKG_NAME )
2084     THEN
2085       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2086     END IF;
2087     -- Initialize message list if p_init_msg_list is set to TRUE.
2088     IF FND_API.to_Boolean( p_init_msg_list ) THEN
2089        FND_MSG_PUB.initialize;
2090     END IF;
2091 
2092 
2093     -- For Test/Debug
2094     Check_And_Open_Debug_Session(p_debug, p_output_dir, p_debug_filename) ;
2095 
2096     -- R12 Comment out
2097     -- IF FND_API.to_Boolean( p_debug ) THEN
2098     --     Open_Debug_Session(p_output_dir, p_debug_filename ) ;
2099     -- END IF;
2100 
2101 
2102 
2103     -- Write debug message if debug mode is on
2104     IF g_debug_flag THEN
2105        Write_Debug('ENG_CHANGE_LIFECYCLE_UTIL.Promote_Header log');
2106        Write_Debug('-----------------------------------------------------');
2107        Write_Debug('p_change_id         : ' || p_change_id );
2108        Write_Debug('p_status_code       : ' || p_status_code );
2109        Write_Debug('p_api_caller        : ' || p_api_caller );
2110        Write_Debug('-----------------------------------------------------');
2111        Write_Debug('Initializing return status... ' );
2112     END IF;
2113 
2114     -- Initialize API return status to success
2115     x_return_status := FND_API.G_RET_STS_SUCCESS;
2116 
2117     -- FND_PROFILE package is not available for workflow (WF),
2118     -- therefore manually set WHO column values
2119     IF p_api_caller = 'WF' THEN
2120       l_fnd_user_id := G_ENG_WF_USER_ID;
2121       l_fnd_login_id := G_ENG_WF_LOGIN_ID;
2122     ELSIF p_api_caller = 'CP' THEN
2123       l_fnd_user_id := G_ENG_CP_USER_ID;
2124       l_fnd_login_id := G_ENG_CP_LOGIN_ID;
2125     END IF;
2126 
2127     -- Real code starts here -----------------------------------------------
2128 
2129     -- First check if the header is CO and last implement phase has been used
2130     Is_CO_Last_Imp_Phase_Used
2131     ( p_change_id          => p_change_id
2132      ,x_is_used            => l_is_imp_phase_used
2133      ,x_last_status_type   => l_last_status_type
2134      ,x_last_status_code   => l_last_status_code
2135      );
2136 
2137     -- If so, refresh the last implement phase before promotion
2138     IF ( l_is_imp_phase_used = 'T' ) THEN
2139 
2140       -- Refresh workflow id and its status if applicable
2141       IF g_debug_flag THEN
2142         Write_Debug('Before: calling Refresh_WF_Route procedure ');
2143       END IF;
2144       Refresh_WF_Route
2145       ( p_api_version         => 1.0
2146        ,p_init_msg_list       => FND_API.G_FALSE
2147        ,p_commit              => FND_API.G_FALSE        --
2148        ,p_validation_level    => p_validation_level
2149        ,p_debug               => FND_API.G_FALSE
2150        ,p_output_dir          => p_output_dir
2151        ,p_debug_filename      => p_debug_filename
2152        ,x_return_status       => l_return_status
2153        ,x_msg_count           => l_msg_count
2154        ,x_msg_data            => l_msg_data
2155        ,p_change_id           => p_change_id
2156        ,p_status_code         => l_last_status_code
2157        ,p_wf_route_id         => NULL
2158        ,p_api_caller          => p_api_caller
2159        );
2160       IF g_debug_flag THEN
2161         Write_Debug('After: calling Refresh_WF_Route procedure: ' || l_return_status) ;
2162       END IF;
2163 
2164       -- Update the remaining columns of the phase row
2165       UPDATE eng_lifecycle_statuses
2166         SET start_date = null,
2167             completion_date = null,
2168             last_update_date = sysdate,
2169             last_updated_by = l_fnd_user_id,
2170             last_update_login = l_fnd_login_id
2171         WHERE entity_name = G_ENG_CHANGE
2172           AND entity_id1 = p_change_id
2173           AND status_code = l_last_status_code
2174           AND active_flag = 'Y'
2175           AND rownum = 1;
2176 
2177     END IF;
2178     -- Finished checking and refreshing the ECO final implement phase
2179 
2180     -- Get the sequence number for the current phase of the change header
2181     SELECT sequence_number, status_code
2182       INTO l_curr_phase_sn, l_curr_status_code
2183       FROM eng_lifecycle_statuses
2184       WHERE entity_name = G_ENG_CHANGE
2185         AND entity_id1 = p_change_id
2186         AND status_code = ( SELECT status_code
2187                               FROM eng_engineering_changes
2188                               WHERE change_id = p_change_id)
2189         AND active_flag = 'Y'
2190         AND rownum = 1;
2191 
2192     -- Get the sequence number for the new phase of the change header
2193     SELECT sequence_number
2194       INTO l_new_phase_sn
2195       FROM eng_lifecycle_statuses
2196       WHERE entity_name = G_ENG_CHANGE
2197         AND entity_id1 = p_change_id
2198         AND status_code = p_status_code
2199         AND active_flag = 'Y'
2200         AND rownum = 1;
2201 
2202     -- Get the status_type of the new phase
2203     SELECT status_type
2204       INTO l_new_status_type
2205       FROM eng_change_statuses
2206       WHERE status_code = p_status_code
2207         AND rownum = 1;
2208 
2209     -- Get the max sequence number in the lifecycle
2210     SELECT max(sequence_number)
2211       INTO l_max_sn
2212       FROM eng_lifecycle_statuses
2213       WHERE entity_name = G_ENG_CHANGE
2214         AND entity_id1 = p_change_id
2215         AND active_flag = 'Y';
2216 
2217     IF g_debug_flag THEN
2218        Write_Debug('Before comparing l_new_phase_sn = l_curr_phase_sn');
2219        Write_Debug('l_new_phase_sn      : ' || l_new_phase_sn );
2220        Write_Debug('l_curr_phase_sn     : ' || l_curr_phase_sn );
2221     END IF;
2222 
2223     -- Sanity check to make sure the new phase is after the current phase
2224     IF l_new_phase_sn <= l_curr_phase_sn THEN
2225       FND_MESSAGE.Set_Name('ENG','ENG_OBJ_STATE_CHANGED');
2226       FND_MSG_PUB.Add;
2227       RAISE FND_API.G_EXC_ERROR;
2228     END IF;
2229 
2230 
2231     --
2232     -- Check for all open line/tasks complete_before_status_code
2233     -- R12B Change Line will support complete_before_status_code
2234     -- without required_flag
2235     --
2236     IF g_debug_flag THEN
2237        Write_Debug('Before checking open line/tasks'' complete_before_status_code');
2238     END IF;
2239     OPEN c_lines;
2240       LOOP
2241         FETCH c_lines
2242           INTO l_line_status_type,
2243                l_status_code,  -- line's complete_before_status_code
2244                l_required_flag,
2245                l_change_type_id,
2246                l_wf_status_code ;
2247 
2248         EXIT WHEN c_lines%NOTFOUND;
2249 
2250 
2251         -- R12B, In case of Line, assuming Required Flag is always NULL
2252         IF ( ( l_required_flag IS NULL OR l_required_flag = 'Y' )
2253              AND l_status_code IS NOT NULL
2254              AND l_line_status_type <> G_ENG_COMPLETED
2255              AND l_line_status_type <> G_ENG_IMPLEMENTED
2256              AND l_line_status_type <> G_ENG_CANCELLED )
2257         THEN
2258 
2259           IF g_debug_flag THEN
2260             Write_Debug('Found one open line or mandatory task... ');
2261             Write_Debug('  line status type = ' || l_line_status_type );
2262             Write_Debug('  l_wf_status_code = ' || l_wf_status_code );
2263             Write_Debug('  l_change_type_id = ' || l_change_type_id );
2264 
2265             IF ( l_required_flag IS NULL OR l_required_flag = 'Y' ) THEN
2266               Write_Debug('  line/task required flag is NULL or Y ');
2267             END IF;
2268 
2269           END IF;
2270 
2271 
2272           --
2273           -- R12B
2274           -- There is no additional logic required so far for the Line Status check
2275           --
2276           -- Get the sequence_number for line/task's complete_before_status_code
2277           SELECT sequence_number
2278             INTO l_phase_sn
2279             FROM eng_lifecycle_statuses
2280             WHERE entity_name = G_ENG_CHANGE
2281               AND entity_id1 = p_change_id
2282               AND status_code = l_status_code
2283               AND active_flag = 'Y'
2284               AND rownum = 1;
2285 
2286           -- If open line's complete_before_status_code is behind the new phase's
2287           -- status_code, raise error
2288           IF (l_phase_sn <= l_new_phase_sn ) THEN
2289             FND_MESSAGE.Set_Name('ENG','ENG_EXIST_LINE_COMP_BF');
2290             FND_MSG_PUB.Add;
2291             RAISE FND_API.G_EXC_ERROR;
2292           END IF;
2293 
2294         END IF;
2295       END LOOP;
2296     CLOSE c_lines;
2297 
2298 
2299     IF g_debug_flag THEN
2300        Write_Debug('After checking open line/tasks'' complete_before_status_code');
2301     END IF;
2302 
2303 
2304     -- Removing last sequence check on implement phase for bug 3446436
2305     -- Replacing it by simple implement phase type check
2306     IF (l_new_status_type = G_ENG_IMPLEMENTED) THEN
2307       -- New phase type is implementation
2308       IF g_debug_flag THEN
2309          Write_Debug('Branch: New phase type is implementation.');
2310       END IF;
2311 
2312       -- Check if the new phase is the last in the lifecycle definition
2313       -- and set the flag if so
2314       IF g_debug_flag THEN
2315          Write_Debug('Before comparing l_new_phase_sn = l_max_sn');
2316       END IF;
2317       IF (l_new_phase_sn = l_max_sn) THEN
2318         -- New phase is the last lifecycle phase
2319         IF g_debug_flag THEN
2320            Write_Debug('Branch: New phase is the last implement phase');
2321         END IF;
2322         -- Set the flag
2323         l_last_imp_flag := 'Y';
2324       END IF;
2325 
2326     -- Fix for bug 3731977
2327     -- Get the change header's cm type and base cm type
2328     SELECT eec.change_mgmt_type_code, ecot.base_change_mgmt_type_code, eec.approval_status_type
2329       INTO l_cm_type_code, l_base_cm_type_code, l_eco_approval_status
2330       FROM eng_engineering_changes eec,
2331            eng_change_order_types ecot
2332       WHERE eec.change_id = p_change_id
2333         AND ecot.change_order_type_id = eec.change_order_type_id;
2334 
2335       -- If there are still open lines or mandatory tasks, raise error message
2336       OPEN c_lines;
2337         LOOP
2338           FETCH c_lines INTO l_line_status_type,
2339                              l_status_code,
2340                              l_required_flag,
2341                              l_change_type_id,
2342                              l_wf_status_code ;
2343 
2344           EXIT WHEN c_lines%NOTFOUND;
2345           IF ( (l_required_flag IS NULL OR l_required_flag = 'Y')
2346                -- line: NULL; task: 'Y'
2347                AND l_line_status_type <> G_LINE_COMPLETED
2348                AND l_line_status_type <> G_ENG_IMPLEMENTED
2349                AND l_line_status_type <> G_LINE_CANCELLED
2350                --   R12.C Enhancement : Added following condition because NIRs can have different line statuses
2351                AND l_base_cm_type_code <> G_ENG_NEW_ITEM_REQ)
2352           THEN
2353 
2354             IF g_debug_flag THEN
2355               Write_Debug('Found one mandatory open line/task... ');
2356               Write_Debug('  line status type = ' || l_line_status_type );
2357               IF ( l_required_flag IS NULL OR l_required_flag = 'Y' ) THEN
2358                 Write_Debug('  line required flag is NULL or Y' );
2359               END IF;
2360             END IF;
2361 
2362             --
2363             -- R12B
2364             -- There is no additional logic required so far for the Line Status check
2365             --
2366             -- In case of the wf triggered session
2367             IF ( p_api_caller = 'WF' ) THEN
2368               l_skip_promotion := 'Y';
2369             ELSE
2370               -- Stop and return error message
2371               FND_MESSAGE.Set_Name('ENG','ENG_EXIST_ACTIVE_LINES');
2372               FND_MSG_PUB.Add;
2373               RAISE FND_API.G_EXC_ERROR;
2374             END IF;
2375 
2376           END IF;
2377         END LOOP;
2378       CLOSE c_lines;
2379       IF g_debug_flag THEN
2380          Write_Debug('After open mandatory lines check');
2381       END IF;
2382 
2383     END IF; -- end of if (phase type = implementation)
2384 
2385 
2386     --Bug No:4767315
2387     --Skip implementation if no active or implemented revised items exist for a change order.
2388     open c_no_revisedItem;
2389     fetch c_no_revisedItem into l_skip;
2390     close c_no_revisedItem;
2391 
2392 
2393 
2394     -- Skip promoting CO to implement phase w/o active revised item
2395     IF (l_base_cm_type_code = G_ENG_ECO
2396         AND l_last_imp_flag = 'Y'
2397         AND l_skip = 'Y'
2398         )
2399     THEN
2400       -- Do not promote CO to implement phase in order to comply with the ERP behavior
2401       l_skip_promotion := 'Y';
2402       IF g_debug_flag THEN
2403         Write_Debug('Do not promote CO to implement phase to comply with ERP');
2404       END IF;
2405       -- Raise error message to the caller
2406       FND_MESSAGE.Set_Name('ENG','ENG_IMP_STOP_WO_ACT_REV_ITEM');
2407       FND_MSG_PUB.Add;
2408       RAISE FND_API.G_EXC_ERROR;
2409 
2410     END IF;
2411 
2412 
2413     -- Fix for bug 4767315 - end
2414     -- Fix for Bug 3769329
2415     IF (l_base_cm_type_code = G_ENG_ECO
2416         AND l_last_imp_flag = 'Y'
2417         AND l_eco_approval_status = Eng_Workflow_Util.G_REJECTED
2418         )
2419     THEN
2420       -- Do not promote CO to implement phase in order to comply with the ERP approval status validation
2421       l_skip_promotion := 'Y';
2422       IF g_debug_flag THEN
2423         Write_Debug('Do not promote CO to implement phase to comply with ERP approval status validation');
2424       END IF;
2425       -- Raise error message to the caller
2426       FND_MESSAGE.Set_Name('ENG','ENG_IMP_STOP_APPR_REJECTED');
2427       FND_MSG_PUB.Add;
2428       RAISE FND_API.G_EXC_ERROR;
2429 
2430     END IF;
2431     -- End of fix for 3769329
2432 
2433     IF (l_skip_promotion = 'Y') THEN
2434       IF g_debug_flag THEN
2435          Write_Debug('Branch: skipping auto promotion');
2436       END IF;
2437     ELSE
2438 
2439       -- If the change header is of type ECO and the new phase is of type IMPLEMENTED,
2440       -- then do not update change header and revised items' status_code (phase)
2441       -- only save the new phase into the header's promote_status_code column
2442       -- actual promotion will be taken care of later in the concurrent program
2443       -- as a special case
2444       IF (l_base_cm_type_code = G_ENG_ECO AND l_last_imp_flag = 'Y') THEN
2445         l_imp_eco_flag := 'Y';
2446         l_skip_wf := 'Y';   -- fix for bug 3479509 design change of launching wf after concurrent program
2447 
2448         -- save new phase into header's promote_status_code column
2449         UPDATE eng_engineering_changes
2450           SET promote_status_code = p_status_code,
2451               last_update_date = sysdate,
2452               last_updated_by = l_fnd_user_id,
2453               last_update_login = l_fnd_login_id
2454           WHERE change_id = p_change_id;
2455 
2456         IF g_debug_flag THEN
2457            Write_Debug('Skipped: Updating header and revised items to the new phase');
2458            Write_Debug('But: last <implement> phase saved into promote_status_code column');
2459         END IF;
2460         -- Fix for 3479509 by commenting out code
2461         -- Behavior changed to leaving completion_date of previous phase and
2462         -- start_date of implement phase blank in case of CO promotion to
2463         -- implement phase
2464         /*
2465         -- Update start_date of the implement phase
2466         UPDATE eng_lifecycle_statuses
2467           SET start_date = sysdate,
2468               completion_date = null,
2469               last_update_date = sysdate,
2470               last_updated_by = l_fnd_user_id,
2471               last_update_login = l_fnd_login_id
2472           WHERE entity_name = G_ENG_CHANGE
2473             AND entity_id1 = p_change_id
2474             AND status_code = p_status_code
2475             AND active_flag = 'Y';
2476         */
2477         -- Fix for 3479509 - end
2478 
2479         -- If the CO's last implement type phase is already used, refresh its workflow id and status
2480         SELECT workflow_status, change_wf_route_id
2481         INTO l_wf_status, l_wf_route_id
2482         FROM eng_lifecycle_statuses
2483         WHERE entity_name = G_ENG_CHANGE
2484         AND entity_id1 = p_change_id
2485         AND status_code = p_status_code
2486         AND active_flag = 'Y'
2487         AND rownum = 1;
2488 
2489 
2490         IF (l_wf_route_id IS NOT NULL AND l_wf_status <> Eng_Workflow_Util.G_RT_NOT_STARTED)
2491         THEN
2492 
2493           -- Get a new workflow route_id
2494           Eng_Change_Route_Util.REFRESH_ROUTE
2495           ( X_NEW_ROUTE_ID   => l_new_route_id,
2496             P_ROUTE_ID       => l_wf_route_id,
2497             P_USER_ID        => l_fnd_user_id,
2498             P_API_CALLER     => p_api_caller
2499           );
2500 
2501           -- refresh imp phase row
2502           UPDATE eng_lifecycle_statuses
2503             SET change_wf_route_id = l_new_route_id,
2504                 workflow_status = Eng_Workflow_Util.G_RT_NOT_STARTED,
2505                 last_update_date = sysdate,
2506                 last_updated_by = l_fnd_user_id,
2507                 last_update_login = l_fnd_login_id
2508             WHERE entity_name = G_ENG_CHANGE
2509               AND entity_id1 = p_change_id
2510               AND status_code = p_status_code
2511               AND active_flag = 'Y';
2512 
2513         END IF;
2514 
2515         --
2516         -- Bug 4967289 Fix
2517         -- Call ENG_ATTACHMENT_IMPLEMENTATION.Validate_floating_version
2518         --Removed this code from here as it is not required here Attachment impl is taking care of it.
2519        /* IF g_debug_flag THEN
2520             Write_Debug('Calling ENG_ATTACHMENT_IMPLEMENTATION.Validate_floating_version ');
2521         END IF;
2522 
2523         ENG_ATTACHMENT_IMPLEMENTATION.Validate_floating_version
2524         ( p_api_version          => 1.0
2525          ,x_return_status        =>    l_return_status
2526          ,x_msg_count            =>    l_msg_count
2527          ,x_msg_data             =>    l_msg_data
2528          ,p_change_id            =>    p_change_id
2529          ,p_rev_item_seq_id      =>    NULL
2530         );
2531 
2532         IF g_debug_flag THEN
2533             Write_Debug('After: calling ENG_ATTACHMENT_IMPLEMENTATION.Validate_floating_version API: ' || l_return_status) ;
2534         END IF;*/
2535 
2536         IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS ) THEN
2537             x_return_status := l_return_status;
2538             x_msg_count := l_msg_count;
2539             x_msg_data := l_msg_data;
2540             --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_PS_API');
2541             --#FND_MESSAGE.Set_Token('OBJECT_NAME', 'ENG_ECO_UTIL.Propagate_ECO');
2542             --#FND_MSG_PUB.Add;
2543             RAISE FND_API.G_EXC_ERROR;
2544         END IF;
2545         -- End of Bug4967289 Fix
2546 
2547       ELSE
2548         -- Normal phase promotion
2549         -- Complete the current phase
2550         UPDATE eng_lifecycle_statuses
2551           SET completion_date = sysdate,
2552               last_update_date = sysdate,
2553               last_updated_by = l_fnd_user_id,
2554               last_update_login = l_fnd_login_id
2555           WHERE entity_name = G_ENG_CHANGE
2556             AND entity_id1 = p_change_id
2557             AND status_code = l_curr_status_code
2558             AND active_flag = 'Y';
2559         IF g_debug_flag THEN
2560           Write_Debug('After updating eng_lifecycle_statuses.completion_date.');
2561           Write_Debug('  Row count = ' || SQL%ROWCOUNT);
2562         END IF;
2563         -- Sanity check, only one record can qualify the condition
2564         IF SQL%ROWCOUNT <> 1 THEN
2565           FND_MESSAGE.Set_Name('ENG','ENG_NOT_EXACTLY_ONE_RECORD');
2566           FND_MSG_PUB.Add;
2567           RAISE FND_API.G_EXC_ERROR;
2568         END IF;
2569         IF g_debug_flag THEN
2570            Write_Debug('After: Completing the current phase and promote to the next phase');
2571         END IF;
2572 
2573         -- Promote change header to the new phase
2574         UPDATE eng_engineering_changes
2575           SET status_code = p_status_code,
2576               promote_status_code = null,
2577               status_type = l_new_status_type,
2578               last_update_date = sysdate,
2579               last_updated_by = l_fnd_user_id,
2580               last_update_login = l_fnd_login_id
2581           WHERE change_id = p_change_id;
2582         IF g_debug_flag THEN
2583           Write_Debug('After updating eng_engineering_changes.');
2584           Write_Debug('  Row count = ' || SQL%ROWCOUNT);
2585         END IF;
2586 
2587         IF g_debug_flag THEN
2588           Write_Debug('Before updateing revised items.');
2589         END IF;
2590         IF ( p_update_ri_flag = 'Y' ) THEN
2591           IF g_debug_flag THEN
2592             Write_Debug('In updateing revised items.');
2593           END IF;
2594 
2595           -- Promote revised items to the new phase
2596           OPEN c_revItems;
2597           LOOP
2598             FETCH c_revItems INTO l_ri_status_code;
2599             EXIT WHEN c_revItems%NOTFOUND;
2600 
2601             IF g_debug_flag THEN
2602               Write_Debug('In Cursor c_revItems to update revised items.');
2603             END IF;
2604 
2605             -- Get the status_type of the revised item phase
2606             SELECT status_type
2607               INTO l_ri_status_type
2608               FROM eng_change_statuses
2609               WHERE status_code = l_ri_status_code
2610                 AND rownum = 1;
2611 
2612             -- Update only those which are open
2613             -- and whose phase is behind the new promoting phase
2614             IF (l_ri_status_type <> G_ENG_IMPLEMENTED
2615                 AND l_ri_status_type <> G_ENG_CANCELLED) THEN
2616 
2617               -- Get the sequence_number of the revised item phase
2618               -- Note: moved this query inside this IF block because cancel
2619               --       status is not in the regular lifecycle phase definitions
2620               SELECT sequence_number
2621                 INTO l_ri_phase_sn
2622                 FROM eng_lifecycle_statuses
2623                 WHERE entity_name = G_ENG_CHANGE
2624                   AND entity_id1 = p_change_id
2625                   AND status_code = l_ri_status_code
2626                   AND active_flag = 'Y'
2627                   AND rownum = 1;
2628 
2629               -- Only promote those revised items whose phase are lower than
2630               -- the new header promotion phase
2631               IF (l_ri_phase_sn < l_new_phase_sn) THEN
2632                 UPDATE eng_revised_items
2633                   SET status_code = p_status_code,
2634                       status_type = l_new_status_type,
2635                       last_update_date = sysdate,
2636                       last_updated_by = l_fnd_user_id,
2637                       last_update_login = l_fnd_login_id
2638                   WHERE CURRENT OF c_revItems;
2639                 IF g_debug_flag THEN
2640                   Write_Debug('After updating eng_revised_items.');
2641                   Write_Debug('  Row count = ' || SQL%ROWCOUNT);
2642                 END IF;
2643               END IF; -- ri_phase < new_header_phase
2644             END IF; -- not imp or cancelled
2645 
2646           END LOOP;
2647           CLOSE c_revItems;
2648         END IF; -- p_update_ri_flag = 'Y'
2649 
2650         IF g_debug_flag THEN
2651            Write_Debug('Done: Updating header and revised items to the new phase');
2652         END IF;
2653 
2654         -- Update the new phase's start_date
2655         UPDATE eng_lifecycle_statuses
2656           SET start_date = sysdate,
2657               last_update_date = sysdate,
2658               last_updated_by = l_fnd_user_id,
2659               last_update_login = l_fnd_login_id
2660           WHERE entity_name = G_ENG_CHANGE
2661             AND entity_id1 = p_change_id
2662             AND status_code = p_status_code
2663             AND active_flag = 'Y';
2664         IF g_debug_flag THEN
2665           Write_Debug('After updating eng_lifecycle_statuses.start_date.');
2666           Write_Debug('  Row count = ' || SQL%ROWCOUNT);
2667         END IF;
2668 
2669       END IF;
2670 
2671 
2672       -- auto propagate if necessary
2673       l_row_cnt := 0;
2674       OPEN c_orgProp;
2675         LOOP
2676           FETCH c_orgProp
2677             INTO l_auto_prop_flag,
2678                  l_change_notice,
2679                  l_hierarchy_name,
2680                  l_org_name;
2681           EXIT WHEN c_orgProp%NOTFOUND;
2682 
2683           l_row_cnt := l_row_cnt + 1;
2684           -- verify the uniqueness of the record
2685           IF (l_row_cnt > 1) THEN
2686             IF g_debug_flag THEN
2687               Write_Debug('Error: more than one propagation policy is found');
2688             END IF;
2689           END IF;
2690 
2691           IF g_debug_flag THEN
2692             Write_Debug('one record for propagation policy is found');
2693             Write_Debug('l_auto_prop_flag         : ' || l_auto_prop_flag );
2694             Write_Debug('l_change_notice          : ' || l_change_notice );
2695             Write_Debug('l_hierarchy_name         : ' || l_hierarchy_name );
2696             Write_Debug('l_org_name               : ' || l_org_name );
2697           END IF;
2698 
2699           IF ( l_auto_prop_flag = 'Y') THEN
2700             IF g_debug_flag THEN
2701               Write_Debug('which needs auto propagation');
2702             END IF;
2703 
2704             ENG_ECO_UTIL.Propagate_ECO
2705             (
2706               p_api_version          =>    1.0
2707              ,p_init_msg_list        =>    FND_API.G_FALSE
2708              ,p_commit               =>    FND_API.G_FALSE
2709              ,p_validation_level     =>    FND_API.G_VALID_LEVEL_FULL
2710              ,p_debug                =>    p_debug --FND_API.G_FALSE
2711              ,p_output_dir           =>    p_output_dir
2712              ,p_debug_filename       =>    NULL  --p_debug_filename
2713              ,x_return_status        =>    l_return_status
2714              ,x_msg_count            =>    l_msg_count
2715              ,x_msg_data             =>    l_msg_data
2716              ,p_change_id            =>    p_change_id
2717              ,p_change_notice        =>    l_change_notice
2718              ,p_hierarchy_name       =>    l_hierarchy_name
2719              ,p_org_name             =>    l_org_name
2720              ,x_request_id           =>    l_request_id
2721             );
2722 
2723             IF g_debug_flag THEN
2724               Write_Debug('After: calling propagate_eco API: ' || l_return_status) ;
2725             END IF;
2726 
2727             IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS ) THEN
2728               x_return_status := l_return_status;
2729               x_msg_count := l_msg_count;
2730               x_msg_data := l_msg_data;
2731               --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_PS_API');
2732               --#FND_MESSAGE.Set_Token('OBJECT_NAME', 'ENG_ECO_UTIL.Propagate_ECO');
2733               --#FND_MSG_PUB.Add;
2734               RAISE FND_API.G_EXC_ERROR;
2735             END IF;
2736 
2737             IF g_debug_flag THEN
2738               Write_Debug('Successful: calling propagate_eco API');
2739               Write_Debug('l_request_id       : ' || l_request_id );
2740             END IF;
2741 
2742             IF g_debug_flag THEN
2743                Write_Debug('Before: saving action log');
2744             END IF;
2745             l_action_id := 0;
2746             -- create new action log
2747             -- Bug Fix: 3547844
2748             -- In case of Auto-Propgation Action Log
2749             -- Who column is Workflow
2750             ENG_CHANGE_ACTIONS_UTIL.Create_Change_Action
2751             ( p_api_version           => 1.0
2752             , p_init_msg_list         => FND_API.G_FALSE        --
2753             , p_commit                => FND_API.G_FALSE        --
2754             , p_validation_level      => FND_API.G_VALID_LEVEL_FULL
2755             , p_debug                 => p_debug --FND_API.G_FALSE
2756             , p_output_dir            => p_output_dir
2757             , p_debug_filename        => NULL
2758             , x_return_status         => l_return_status
2759             , x_msg_count             => l_msg_count
2760             , x_msg_data              => l_msg_data
2761             , p_action_type           => ENG_CHANGE_ACTIONS_UTIL.G_ACT_PROPAGATE
2762             , p_object_name           => G_ENG_CHANGE
2763             , p_object_id1            => p_change_id
2764             , p_object_id2            => NULL
2765             , p_object_id3            => NULL
2766             , p_object_id4            => NULL
2767             , p_object_id5            => NULL
2768             , p_parent_action_id      => -1
2769             , p_status_code           => NULL
2770             , p_action_date           => SYSDATE
2771             , p_change_description    => NULL
2772             , p_user_id               => G_ENG_WF_USER_ID
2773             , p_api_caller            => 'WF'
2774             , x_change_action_id      => l_action_id
2775             );
2776 
2777 
2778             IF g_debug_flag THEN
2779                Write_Debug('After: saving action log: ' || l_return_status) ;
2780                Write_Debug('l_action_id       : ' || l_action_id );
2781             END IF;
2782 
2783             IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS )
2784             THEN
2785               x_return_status := l_return_status;
2786               x_msg_count := l_msg_count;
2787               x_msg_data := l_msg_data;
2788               --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_PS_API');
2789               --#FND_MESSAGE.Set_Token('OBJECT_NAME', 'ENG_CHANGE_ACTIONS_UTIL.Create_Change_Action');
2790               --#FND_MSG_PUB.Add;
2791               RAISE FND_API.G_EXC_ERROR;
2792             END IF;
2793 
2794             IF g_debug_flag THEN
2795                Write_Debug('Successful: saving action log');
2796             END IF;
2797 
2798           ELSE
2799             IF g_debug_flag THEN
2800               Write_Debug('which does not need auto propagation');
2801             END IF;
2802           END IF;
2803 
2804         END LOOP;
2805       CLOSE c_orgProp;
2806       IF g_debug_flag THEN
2807          Write_Debug('After checking propagation policy');
2808       END IF;
2809 
2810 
2811       -- R12B
2812       -- In case of Document LC Change Object, call ENG_DOCUMENT_UTIL.Change_Doc_LC_Phase
2813       -- to validate and sync Document LC Phase
2814       l_doc_lc_object_flag := ENG_DOCUMENT_UTIL.Is_Dom_Document_Lifecycle
2815                              ( p_change_id => p_change_id
2816                              , p_base_change_mgmt_type_code => l_base_cm_type_code
2817                              ) ;
2818 
2819       IF (l_doc_lc_object_flag)
2820       THEN
2821           IF g_debug_flag THEN
2822              Write_Debug('Before: calling ENG_DOCUMENT_UTIL.Change_Doc_LC_Phase');
2823              Write_Debug('  p_change_id =   ' || p_change_id);
2824              Write_Debug('  p_status_code = ' || p_status_code);
2825              Write_Debug('  p_action_type =   ' || G_ENG_PROMOTE);
2826           END IF;
2827 
2828           ENG_DOCUMENT_UTIL.Change_Doc_LC_Phase
2829           ( p_api_version         => 1.0
2830            ,p_init_msg_list       => FND_API.G_FALSE
2831            ,p_commit              => FND_API.G_FALSE
2832            ,p_validation_level    => p_validation_level
2833            ,p_debug               => FND_API.G_FALSE
2834            ,p_output_dir          => p_output_dir
2835            ,p_debug_filename      => p_debug_filename
2836            ,x_return_status       => l_return_status
2837            ,x_msg_count           => l_msg_count
2838            ,x_msg_data            => l_msg_data
2839            ,p_change_id           => p_change_id
2840            ,p_lc_phase_code       => p_status_code
2841            ,p_action_type         =>  G_ENG_PROMOTE
2842            ,p_api_caller          => p_api_caller
2843           ) ;
2844 
2845 
2846           IF g_debug_flag THEN
2847              Write_Debug('After: calling ENG_DOCUMENT_UTIL.Change_Doc_LC_Phase: ' || l_return_status) ;
2848           END IF;
2849 
2850           IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS )
2851           THEN
2852             x_return_status := l_return_status;
2853             x_msg_count := l_msg_count;
2854             x_msg_data := l_msg_data;
2855             RAISE FND_API.G_EXC_ERROR;
2856           END IF;
2857 
2858           IF g_debug_flag THEN
2859              Write_Debug('Successful: ENG_DOCUMENT_UTIL.Change_Doc_LC_Phase');
2860           END IF;
2861 
2862       END IF ; -- if ( Change Object is Documet LC Object)
2863       --
2864       -- End of R12B Document LC Change Object Enh Change
2865       --
2866 
2867 
2868 
2869       -- Start workflow for new phase if necessary
2870       Start_WF_OnlyIf_Necessary
2871       ( p_api_version       => 1.0
2872        ,p_init_msg_list     => FND_API.G_FALSE
2873        ,p_commit            => FND_API.G_FALSE
2874        ,p_validation_level  => p_validation_level
2875        ,p_debug             => FND_API.G_FALSE
2876        ,p_output_dir        => p_output_dir
2877        ,p_debug_filename    => p_debug_filename
2878        ,x_return_status     => l_return_status
2879        ,x_msg_count         => l_msg_count
2880        ,x_msg_data          => l_msg_data
2881        ,p_change_id         => p_change_id
2882        ,p_status_code       => p_status_code
2883        ,p_status_type       => l_new_status_type
2884        ,p_sequence_number   => l_new_phase_sn
2885        ,p_imp_eco_flag      => l_imp_eco_flag
2886        ,p_api_caller        => p_api_caller
2887        ,p_action_type       => G_ENG_PROMOTE
2888        ,p_comment           => p_comment        -- only used for co promote-to-implement action
2889        ,p_skip_wf           => l_skip_wf        -- fix for bug 3479509 design change of launching wf after concurrent program
2890       );
2891 
2892       IF g_debug_flag THEN
2893          Write_Debug('After call to procedure Start_WF_OnlyIf_Necessary');
2894       END IF;
2895 
2896       IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS ) THEN
2897         x_return_status := l_return_status;
2898         x_msg_count := l_msg_count;
2899         x_msg_data := l_msg_data;
2900         --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_WF_API');
2901         --#FND_MSG_PUB.Add;
2902         RAISE FND_API.G_EXC_ERROR;
2903       END IF;
2904 
2905 
2906       --
2907       -- R12B
2908       -- Change Line and Line Workflow Enhancement
2909       -- Kick off Change Line Workflow if Start After Status is defined in
2910       -- Change Line and the Change Line has a Workflow "Not Started"
2911       --
2912       --
2913       -- The associated Line Workflow will be automatically started once
2914       -- the Header Workflow Status reaches the status specified in 'Start After Status'
2915       -- if the Line Workflow is still Not Started.
2916       --
2917       -- Start workflow for new phase if necessary
2918       IF (NOT l_doc_lc_object_flag AND l_skip_wf <> 'Y')
2919       THEN
2920 
2921           IF g_debug_flag THEN
2922              Write_Debug('calling procedure Start_Line_Workflow');
2923           END IF;
2924 
2925           Start_Line_Workflow
2926           ( p_api_version           => 1.0
2927            ,p_init_msg_list         => FND_API.G_FALSE
2928            ,p_commit                => FND_API.G_FALSE
2929            ,p_validation_level      => p_validation_level
2930            ,p_debug                 => FND_API.G_FALSE
2931            ,p_output_dir            => p_output_dir
2932            ,p_debug_filename        => p_debug_filename
2933            ,x_return_status         => l_return_status
2934            ,x_msg_count             => l_msg_count
2935            ,x_msg_data              => l_msg_data
2936            ,p_change_id             => p_change_id
2937            ,p_new_status_code       => p_status_code
2938            ,p_cur_status_code       => l_curr_status_code
2939            ,p_api_caller            => p_api_caller
2940            ,p_action_type           => G_ENG_PROMOTE
2941           );
2942 
2943           IF g_debug_flag THEN
2944              Write_Debug('After call to procedure Start_Line_Workflow');
2945           END IF;
2946 
2947           IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS ) THEN
2948             x_return_status := l_return_status;
2949             x_msg_count := l_msg_count;
2950             x_msg_data := l_msg_data;
2951             RAISE FND_API.G_EXC_ERROR;
2952           END IF;
2953       END IF ;
2954       --
2955       -- End of R12B Change Line and Line Workflow Enhancement
2956       --
2957 
2958     END IF; -- if (p_skip_promotion='Y')
2959 
2960 
2961     -- Standard ending code ------------------------------------------------
2962     IF FND_API.To_Boolean ( p_commit ) THEN
2963       COMMIT WORK;
2964     END IF;
2965 
2966     FND_MSG_PUB.Count_And_Get
2967     ( p_count        =>      x_msg_count,
2968       p_data         =>      x_msg_data );
2969 
2970     IF g_debug_flag THEN
2971       Write_Debug('Finish. End Of procedure: Promote_Header');
2972     END IF;
2973 
2974     IF FND_API.to_Boolean( p_debug ) THEN
2975       Close_Debug_Session;
2976     END IF;
2977 
2978   EXCEPTION
2979     WHEN FND_API.G_EXC_ERROR THEN
2980       --ROLLBACK TO Promote_Header;
2981       x_return_status := FND_API.G_RET_STS_ERROR;
2982       FND_MSG_PUB.Count_And_Get
2983       ( p_count        =>      x_msg_count
2984        ,p_data         =>      x_msg_data );
2985       IF g_debug_flag THEN
2986         Write_Debug('Rollback and Finish with expected error.') ;
2987       END IF;
2988       IF FND_API.to_Boolean( p_debug ) THEN
2989         Close_Debug_Session;
2990       END IF;
2991     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2992       --ROLLBACK TO Promote_Header;
2993       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2994       FND_MSG_PUB.Count_And_Get
2995       ( p_count        =>      x_msg_count
2996        ,p_data         =>      x_msg_data );
2997       IF g_debug_flag THEN
2998         Write_Debug('Rollback and Finish with unexpected error.') ;
2999       END IF;
3000       IF FND_API.to_Boolean( p_debug ) THEN
3001         Close_Debug_Session;
3002       END IF;
3003     WHEN OTHERS THEN
3004       --ROLLBACK TO Promote_Header;
3005       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3006       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )
3007       THEN
3008         FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME, l_api_name );
3009       END IF;
3010       FND_MSG_PUB.Count_And_Get
3011       ( p_count        =>      x_msg_count
3012        ,p_data         =>      x_msg_data );
3013       IF g_debug_flag THEN
3014         Write_Debug('Rollback and Finish with other error.') ;
3015       END IF;
3016       IF FND_API.to_Boolean( p_debug ) THEN
3017         Close_Debug_Session;
3018       END IF;
3019   END Promote_Header;
3020 
3021   -- Internal procedure for demotion of change header (inc. revItems)
3022   -- Note that even though this procedure shares the same argument list
3023   -- as Promote_Header procedure, the internal logic is quite different,
3024   -- so it is written as a seperate procedure for easier understanding.
3025   PROCEDURE Demote_Header
3026   (
3027     p_api_version               IN   NUMBER                             --
3028    ,p_init_msg_list             IN   VARCHAR2 := FND_API.G_FALSE        --
3029    ,p_commit                    IN   VARCHAR2 := FND_API.G_FALSE        --
3030    ,p_validation_level          IN   NUMBER   := FND_API.G_VALID_LEVEL_FULL
3031    ,p_debug                     IN   VARCHAR2 := FND_API.G_FALSE        --
3032    ,p_output_dir                IN   VARCHAR2 := NULL
3033    ,p_debug_filename            IN   VARCHAR2 := NULL
3034    ,x_return_status             OUT  NOCOPY  VARCHAR2                   --
3035    ,x_msg_count                 OUT  NOCOPY  NUMBER                     --
3036    ,x_msg_data                  OUT  NOCOPY  VARCHAR2                   --
3037    ,p_change_id                 IN   NUMBER                             -- header's change_id
3038    ,p_status_code               IN   NUMBER                             -- new phase
3039    ,p_update_ri_flag            IN   VARCHAR2 := 'Y'                    -- can also be 'N'
3040    ,p_api_caller                IN   VARCHAR2 := NULL                   -- can also be 'WF'
3041    ,x_sfa_line_items_exists     OUT NOCOPY VARCHAR2
3042   )
3043   IS
3044     l_api_name           CONSTANT VARCHAR2(30)  := 'Demote_Header';
3045     l_api_version        CONSTANT NUMBER := 1.0;
3046 
3047     l_fnd_user_id        NUMBER := TO_NUMBER(FND_PROFILE.VALUE('USER_ID'));
3048     l_fnd_login_id       NUMBER := TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID'));
3049 
3050     l_curr_appr_status   eng_engineering_changes.approval_status_type%TYPE;
3051 
3052     l_is_imp_phase_used  VARCHAR2(1) := 'F';
3053 
3054     l_last_status_type   NUMBER;
3055     l_last_status_code   NUMBER;
3056 
3057     l_return_status      VARCHAR2(1);
3058     l_msg_count          NUMBER;
3059     l_msg_data           VARCHAR2(2000);
3060 
3061     l_curr_phase_sn      eng_lifecycle_statuses.sequence_number%TYPE;
3062     l_new_phase_sn       eng_lifecycle_statuses.sequence_number%TYPE;
3063 
3064     l_new_status_type    eng_change_statuses.status_type%TYPE;
3065 
3066     l_change_mgmt_type_code eng_engineering_changes.change_mgmt_type_code%TYPE;
3067 
3068     -- Cursor to get all lifecycle phases between (inclusive) [demotionStatus, currentStatus]
3069     CURSOR c_lcStatuses IS
3070       SELECT *
3071         FROM eng_lifecycle_statuses
3072         WHERE entity_name = G_ENG_CHANGE
3073           AND entity_id1 = p_change_id
3074           AND active_flag = 'Y'
3075           AND sequence_number >= l_new_phase_sn
3076           AND sequence_number <= l_curr_phase_sn
3077       FOR UPDATE;
3078 
3079     l_temp_status_type   NUMBER;
3080     l_has_approval_phase VARCHAR2(1) := 'F';  -- in between curr and demotion phases
3081 
3082     -- Cursor to get all lifecycle phases between (inclusive) [demotionStatus, maxStatus]
3083     CURSOR c_lcStatusesToMax IS
3084       SELECT *
3085         FROM eng_lifecycle_statuses
3086         WHERE entity_name = G_ENG_CHANGE
3087           AND entity_id1 = p_change_id
3088           AND active_flag = 'Y'
3089           AND sequence_number >= l_new_phase_sn
3090       FOR UPDATE;
3091     l_lcStatuses_row     eng_lifecycle_statuses%ROWTYPE;
3092     l_old_iter_num       eng_lifecycle_statuses.iteration_number%TYPE := -1;
3093     l_old_row_id         NUMBER;
3094     l_new_row_id         NUMBER;
3095     l_new_route_id       NUMBER;
3096 
3097     CURSOR c_statusProp IS
3098       SELECT *
3099         FROM eng_status_properties
3100         WHERE change_lifecycle_status_id = l_old_row_id
3101       FOR UPDATE;
3102     l_status_prop_row   eng_status_properties%ROWTYPE;
3103 
3104     CURSOR c_revItems IS
3105       SELECT status_code
3106         FROM eng_revised_items
3107         WHERE change_id = p_change_id
3108       FOR UPDATE;
3109     l_ri_status_code      eng_change_lines.status_code%TYPE;
3110     -- revItem's status_code's sequence_number
3111     l_ri_phase_sn         eng_lifecycle_statuses.sequence_number%TYPE;
3112     l_ri_status_type     eng_change_statuses.status_type%TYPE;
3113 
3114 
3115     l_base_cm_type_code  VARCHAR2(30) ;
3116     l_doc_lc_object_flag BOOLEAN := FALSE ;
3117     l_old_status_code NUMBER;
3118     l_old_status_type NUMBER;
3119 
3120   BEGIN
3121 select status_type, status_code into l_old_status_code, l_old_status_type from eng_engineering_changes where change_id = p_change_id;
3122     -- Standard Start of API savepoint
3123     SAVEPOINT Demote_Header;
3124     -- Standard call to check for call compatibility
3125     IF NOT FND_API.Compatible_API_Call ( l_api_version
3126                                         ,p_api_version
3127                                         ,l_api_name
3128                                         ,G_PKG_NAME )
3129     THEN
3130       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3131     END IF;
3132     -- Initialize message list if p_init_msg_list is set to TRUE.
3133     IF FND_API.to_Boolean( p_init_msg_list ) THEN
3134        FND_MSG_PUB.initialize;
3135     END IF;
3136 
3137     -- For Test/Debug
3138     Check_And_Open_Debug_Session(p_debug, p_output_dir, p_debug_filename) ;
3139     -- R12 Comment out
3140     -- IF FND_API.to_Boolean( p_debug ) THEN
3141     --     Open_Debug_Session(p_output_dir, p_debug_filename ) ;
3142     -- END IF;
3143 
3144     -- Write debug message if debug mode is on
3145     IF g_debug_flag THEN
3146        Write_Debug('ENG_CHANGE_LIFECYCLE_UTIL.Demote_Header log');
3147        Write_Debug('-----------------------------------------------------');
3148        Write_Debug('p_change_id         : ' || p_change_id );
3149        Write_Debug('p_status_code       : ' || p_status_code );
3150        Write_Debug('p_api_caller        : ' || p_api_caller );
3151        Write_Debug('-----------------------------------------------------');
3152        Write_Debug('Initializing return status... ' );
3153     END IF;
3154 
3155     -- Initialize API return status to success
3156     x_return_status := FND_API.G_RET_STS_SUCCESS;
3157 
3158     -- FND_PROFILE package is not available for workflow (WF),
3159     -- therefore manually set WHO column values
3160     IF p_api_caller = 'WF' THEN
3161       l_fnd_user_id := G_ENG_WF_USER_ID;
3162       l_fnd_login_id := G_ENG_WF_LOGIN_ID;
3163     ELSIF p_api_caller = 'CP' THEN
3164       l_fnd_user_id := G_ENG_CP_USER_ID;
3165       l_fnd_login_id := G_ENG_CP_LOGIN_ID;
3166     END IF;
3167 
3168     -- Real code starts here -----------------------------------------------
3169 
3170     -- Get the current header approval status
3171     SELECT approval_status_type
3172       INTO l_curr_appr_status
3173       FROM eng_engineering_changes
3174       where change_id = p_change_id;
3175 
3176     -- First check if the header is CO and last implement phase has been used
3177     Is_CO_Last_Imp_Phase_Used
3178     ( p_change_id          => p_change_id
3179      ,x_is_used            => l_is_imp_phase_used
3180      ,x_last_status_type   => l_last_status_type
3181      ,x_last_status_code   => l_last_status_code
3182      );
3183 
3184     -- If so, adjust header phase to implemented phase before demotion
3185     IF ( l_is_imp_phase_used = 'T' ) THEN
3186       UPDATE eng_engineering_changes
3187         SET status_type = l_last_status_type,
3188             status_code = l_last_status_code,
3189             promote_status_code = NULL
3190         WHERE change_id = p_change_id;
3191 
3192     END IF;
3193 
3194     -- Get the sequence number for the current phase of the change header
3195     SELECT sequence_number
3196       INTO l_curr_phase_sn
3197       FROM eng_lifecycle_statuses
3198       WHERE entity_name = G_ENG_CHANGE
3199         AND entity_id1 = p_change_id
3200         AND active_flag = 'Y'
3201         AND status_code = ( SELECT status_code
3202                               FROM eng_engineering_changes
3203                               WHERE change_id = p_change_id)
3204         AND rownum = 1;
3205 
3206     -- Get the sequence number for the new phase of the change header
3207     SELECT sequence_number
3208       INTO l_new_phase_sn
3209       FROM eng_lifecycle_statuses
3210       WHERE entity_name = G_ENG_CHANGE
3211         AND entity_id1 = p_change_id
3212         AND status_code = p_status_code
3213         AND active_flag = 'Y'
3214         AND rownum = 1;
3215 
3216     IF g_debug_flag THEN
3217        Write_Debug('Before comparing l_new_phase_sn >= l_curr_phase_sn');
3218     END IF;
3219 
3220     -- Sanity check to make sure the new phase is after the current phase
3221     IF ( l_new_phase_sn >= l_curr_phase_sn ) THEN
3222       FND_MESSAGE.Set_Name('ENG','ENG_OBJ_STATE_CHANGED');
3223       FND_MSG_PUB.Add;
3224       RAISE FND_API.G_EXC_ERROR;
3225     END IF;
3226 
3227     -- Finish the current phase before demotion
3228     UPDATE eng_lifecycle_statuses
3229       SET completion_date = sysdate,
3230           last_update_date = sysdate,
3231           last_updated_by = l_fnd_user_id,
3232           last_update_login = l_fnd_login_id
3233       WHERE entity_name = G_ENG_CHANGE
3234         AND entity_id1 = p_change_id
3235         AND active_flag = 'Y'
3236         AND status_code = ( SELECT status_code
3237                               FROM eng_engineering_changes
3238                               WHERE change_id = p_change_id)
3239         AND sequence_number = l_curr_phase_sn
3240         AND rownum = 1;
3241     IF g_debug_flag THEN
3242       Write_Debug('After updating eng_lifecycle_statuses.completion_date.');
3243       Write_Debug('  Row count = ' || SQL%ROWCOUNT);
3244     END IF;
3245 
3246 
3247     -- obsolete all the lifecycle phases in the range of [demotionStatus, currStatus]
3248     OPEN c_lcStatuses;
3249       LOOP
3250         FETCH c_lcStatuses INTO l_lcStatuses_row;
3251         EXIT WHEN c_lcStatuses%NOTFOUND;
3252 
3253         -- l_old_iter_num initialization and sanity check
3254         /*
3255         IF (l_old_iter_num = -1) THEN
3256           -- make sure the original iteration number is not null
3257           IF (l_lcStatuses_row.iteration_number IS NULL) THEN
3258             FND_MESSAGE.Set_Name('ENG','ENG_OBJECT_CANT_BE_NULL');
3259             FND_MESSAGE.Set_Token('OBJECT_NAME', 'ITERATION_NUMBER');
3260             FND_MSG_PUB.Add;
3261             RAISE FND_API.G_EXC_ERROR;
3262           END IF;
3263           l_old_iter_num := l_lcStatuses_row.iteration_number;
3264         ELSE
3265           IF ( l_lcStatuses_row.iteration_number <> l_old_iter_num ) THEN
3266             -- Stop and return error message
3267             IF g_debug_flag THEN
3268               Write_Debug('Error: inconsistent lifecycle phase sequence numbers');
3269               Write_Debug('l_lcStatuses_row.iteration_number : '|| l_lcStatuses_row.iteration_number);
3270               Write_Debug('l_old_iter_num     : ' || l_old_iter_num );
3271             END IF;
3272             FND_MESSAGE.Set_Name('ENG','ENG_SN_INCONSISTENT');
3273             FND_MSG_PUB.Add;
3274             RAISE FND_API.G_EXC_ERROR;
3275           END IF;
3276         END IF;
3277         */
3278 
3279         -- Check existence of approval phase between curr and demotion phase
3280         SELECT status_type
3281           INTO l_temp_status_type
3282           FROM eng_change_statuses
3283           WHERE status_code = l_lcStatuses_row.STATUS_CODE;
3284         IF (l_temp_status_type = G_ENG_APPROVED)
3285         THEN
3286           l_has_approval_phase := 'T';
3287         END IF;
3288 
3289         -- Obsolete (but not delete) the old phase row
3290         UPDATE eng_lifecycle_statuses
3291           SET active_flag = 'N',
3292               last_update_date = sysdate,
3293               last_updated_by = l_fnd_user_id,
3294               last_update_login = l_fnd_login_id
3295           WHERE CURRENT OF c_lcStatuses;
3296 
3297         -- First get the new unique index id value for the new row (also save the old id)
3298         l_old_row_id := l_lcStatuses_row.change_lifecycle_status_id;
3299         SELECT eng_lifecycle_statuses_s.nextval
3300           INTO l_new_row_id
3301           FROM DUAL;
3302 
3303         -- Secondly get the new wf_route_id if needed
3304         IF (l_lcStatuses_row.change_wf_route_id IS NULL) THEN
3305           l_new_route_id := NULL;
3306         ELSE
3307           -- Get a new workflow route_id
3308           Eng_Change_Route_Util.REFRESH_ROUTE
3309           ( X_NEW_ROUTE_ID   => l_new_route_id,
3310             P_ROUTE_ID       => l_lcStatuses_row.change_wf_route_id,
3311             P_USER_ID        => l_fnd_user_id,
3312             P_API_CALLER     => p_api_caller
3313           );
3314         END IF;
3315 
3316         -- Prepare for the new phase row
3317         l_lcStatuses_row.change_lifecycle_status_id := l_new_row_id;
3318         l_lcStatuses_row.change_wf_route_id := l_new_route_id;
3319         l_lcStatuses_row.active_flag := 'Y';
3320         l_lcStatuses_row.creation_date := sysdate;
3321         l_lcStatuses_row.created_by := l_fnd_user_id;
3322         l_lcStatuses_row.last_update_date := sysdate;
3323         l_lcStatuses_row.last_updated_by := l_fnd_user_id;
3324         l_lcStatuses_row.last_update_login := l_fnd_login_id;
3325         -- populate wf_status based upon wf availibility of the phase
3326         IF ( l_lcStatuses_row.change_wf_route_id IS NULL ) THEN
3327           l_lcStatuses_row.workflow_status := null;
3328         ELSE
3329           l_lcStatuses_row.workflow_status := Eng_Workflow_Util.G_RT_NOT_STARTED;
3330         END IF;
3331         -- populate start_date only if it is the newly demoted phase
3332         IF ( l_lcStatuses_row.status_code = p_status_code ) THEN
3333           l_lcStatuses_row.start_date := sysdate;
3334         ELSE
3335         l_lcStatuses_row.start_date := null;
3336         END IF;
3337         -- populate completion_date to null for all new phase rows
3338         l_lcStatuses_row.completion_date := null;
3339 
3340 
3341         -- Before insertion, update all the corresponding rows in eng_status_properties table
3342         OPEN c_statusProp;
3343           LOOP
3344             FETCH c_statusProp INTO l_status_prop_row;
3345             EXIT WHEN c_statusProp%NOTFOUND;
3346               UPDATE eng_status_properties
3347                 SET change_lifecycle_status_id = l_new_row_id,
3348                     last_update_date = sysdate,
3349                     last_updated_by = l_fnd_user_id,
3350                     last_update_login = l_fnd_login_id
3351                 WHERE current of c_statusProp;
3352             END LOOP;
3353         CLOSE c_statusProp;
3354         IF g_debug_flag THEN
3355           Write_Debug('After updating eng_status_properties row''s change_lifecycle_status_id');
3356         END IF;
3357 
3358         -- Insert the new phase row
3359         /* Only Oracle 9.2+ supports this directly row insertion feature
3360         INSERT INTO eng_lifecycle_statuses
3361           VALUES l_lcStatuses_row;
3362         */
3363         INSERT INTO eng_lifecycle_statuses
3364                     ( CHANGE_LIFECYCLE_STATUS_ID,
3365                       ENTITY_NAME,
3366                       ENTITY_ID1,
3367                       ENTITY_ID2,
3368                       ENTITY_ID3,
3369                       ENTITY_ID4,
3370                       ENTITY_ID5,
3371                       SEQUENCE_NUMBER,
3372                       STATUS_CODE,
3373                       START_DATE,
3374                       COMPLETION_DATE,
3375                       CHANGE_WF_ROUTE_ID,
3376                       CHANGE_WF_ROUTE_TEMPLATE_ID,
3377                       AUTO_PROMOTE_STATUS,
3378                       AUTO_DEMOTE_STATUS,
3379                       WORKFLOW_STATUS,
3380                       CHANGE_EDITABLE_FLAG,
3381                       CREATION_DATE,
3382                       CREATED_BY,
3383                       LAST_UPDATE_DATE,
3384                       LAST_UPDATED_BY,
3385                       LAST_UPDATE_LOGIN,
3386                       ITERATION_NUMBER,
3387                       ACTIVE_FLAG,
3388                       WF_SIG_POLICY )
3389           VALUES
3390                     ( l_lcStatuses_row.CHANGE_LIFECYCLE_STATUS_ID,
3391                       l_lcStatuses_row.ENTITY_NAME,
3392                       l_lcStatuses_row.ENTITY_ID1,
3393                       l_lcStatuses_row.ENTITY_ID2,
3394                       l_lcStatuses_row.ENTITY_ID3,
3395                       l_lcStatuses_row.ENTITY_ID4,
3396                       l_lcStatuses_row.ENTITY_ID5,
3397                       l_lcStatuses_row.SEQUENCE_NUMBER,
3398                       l_lcStatuses_row.STATUS_CODE,
3399                       l_lcStatuses_row.START_DATE,
3400                       l_lcStatuses_row.COMPLETION_DATE,
3401                       l_lcStatuses_row.CHANGE_WF_ROUTE_ID,
3402                       l_lcStatuses_row.CHANGE_WF_ROUTE_TEMPLATE_ID,
3403                       l_lcStatuses_row.AUTO_PROMOTE_STATUS,
3404                       l_lcStatuses_row.AUTO_DEMOTE_STATUS,
3405                       l_lcStatuses_row.WORKFLOW_STATUS,
3406                       l_lcStatuses_row.CHANGE_EDITABLE_FLAG,
3407                       l_lcStatuses_row.CREATION_DATE,
3408                       l_lcStatuses_row.CREATED_BY,
3409                       l_lcStatuses_row.LAST_UPDATE_DATE,
3410                       l_lcStatuses_row.LAST_UPDATED_BY,
3411                       l_lcStatuses_row.LAST_UPDATE_LOGIN,
3412                       l_lcStatuses_row.ITERATION_NUMBER,
3413                       l_lcStatuses_row.ACTIVE_FLAG,
3414                       l_lcStatuses_row.WF_SIG_POLICY );
3415 
3416       END LOOP;
3417     CLOSE c_lcStatuses;
3418 
3419     -- Update all phase rows from the demotion phase all the way to the last phase
3420     OPEN c_lcStatusesToMax;
3421       LOOP
3422         FETCH c_lcStatusesToMax INTO l_lcStatuses_row;
3423         EXIT WHEN c_lcStatusesToMax%NOTFOUND;
3424 
3425         -- make sure the original iteration number is not null
3426         IF (l_lcStatuses_row.iteration_number IS NULL) THEN
3427           FND_MESSAGE.Set_Name('ENG','ENG_OBJECT_CANT_BE_NULL');
3428           FND_MESSAGE.Set_Token('OBJECT_NAME', 'ITERATION_NUMBER');
3429           FND_MSG_PUB.Add;
3430           RAISE FND_API.G_EXC_ERROR;
3431         END IF;
3432         l_old_iter_num := l_lcStatuses_row.iteration_number;
3433 
3434         -- Increment sequence number of the active phases
3435         UPDATE eng_lifecycle_statuses
3436           SET iteration_number = l_old_iter_num + 1,
3437               last_update_date = sysdate,
3438               last_updated_by = l_fnd_user_id,
3439               last_update_login = l_fnd_login_id
3440           WHERE CURRENT OF c_lcStatusesToMax;
3441 
3442       END LOOP;
3443     CLOSE c_lcStatusesToMax;
3444 
3445     -- Get the status_type of the new phase
3446     SELECT status_type
3447       INTO l_new_status_type
3448       FROM eng_change_statuses
3449       WHERE status_code = p_status_code;
3450 
3451     -- Demote change header record
3452     UPDATE eng_engineering_changes
3453       SET status_code = p_status_code,
3454           promote_status_code = null,
3455           status_type = l_new_status_type,
3456           last_update_date = sysdate,
3457           last_updated_by = l_fnd_user_id,
3458           last_update_login = l_fnd_login_id
3459       WHERE change_id = p_change_id;
3460     IF g_debug_flag THEN
3461       Write_Debug('After updating eng_engineering_changes.');
3462       Write_Debug('  Row count = ' || SQL%ROWCOUNT);
3463     END IF;
3464 
3465     -- Fix for bug 3775865: Reset header approval status
3466     IF (l_has_approval_phase = 'T' AND l_curr_appr_status <> G_ENG_NOT_SUBMITTED)
3467     THEN
3468 
3469       IF g_debug_flag THEN
3470         Write_Debug('Before: calling Update_Header_Appr_Status');
3471       END IF;
3472 
3473       -- Update change header approval status
3474       -- Launch header approval status change workflow
3475       Update_Header_Appr_Status
3476       (
3477         p_api_version               =>  1.0
3478        ,p_init_msg_list             =>  FND_API.G_FALSE
3479        ,p_commit                    =>  FND_API.G_FALSE
3480        ,p_validation_level          =>  FND_API.G_VALID_LEVEL_FULL
3481        ,p_debug                     =>  FND_API.G_FALSE
3482        ,p_output_dir                =>  p_output_dir
3483        ,p_debug_filename            =>  p_debug_filename
3484        ,x_return_status             =>  l_return_status
3485        ,x_msg_count                 =>  l_msg_count
3486        ,x_msg_data                  =>  l_msg_data
3487        ,p_change_id                 =>  p_change_id
3488        ,p_status_code               =>  p_status_code
3489        ,p_appr_status               =>  G_ENG_NOT_SUBMITTED
3490        ,p_route_status              =>  NULL
3491        ,p_api_caller                =>  p_api_caller
3492        ,p_bypass                    =>  'Y'
3493        ,x_sfa_line_items_exists     => x_sfa_line_items_exists
3494       );
3495 
3496       IF g_debug_flag THEN
3497         Write_Debug('After: calling Update_Header_Appr_Status: ' || l_return_status) ;
3498       END IF;
3499 
3500 
3501       IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS )
3502       THEN
3503         x_return_status := l_return_status;
3504         x_msg_count := l_msg_count;
3505         x_msg_data := l_msg_data;
3506         RAISE FND_API.G_EXC_ERROR;
3507       END IF;
3508       IF g_debug_flag THEN
3509         Write_Debug('After: calling Update_Header_Appr_Status');
3510       END IF;
3511 
3512     END IF;
3513     -- Fix for bug 3775865 - end
3514 
3515     IF ( p_update_ri_flag = 'Y' ) THEN
3516       -- Demote revised items to the new phase
3517       OPEN c_revItems;
3518       LOOP
3519         FETCH c_revItems INTO l_ri_status_code;
3520         EXIT WHEN c_revItems%NOTFOUND;
3521 
3522         IF g_debug_flag THEN
3523           Write_Debug('In Cursor c_revItems to update revised items.');
3524         END IF;
3525 
3526         -- Get the status_type of the revised item phase
3527         SELECT status_type
3528           INTO l_ri_status_type
3529           FROM eng_change_statuses
3530           WHERE status_code = l_ri_status_code
3531             AND rownum = 1;
3532 
3533         IF (l_ri_status_type <> G_ENG_IMPLEMENTED
3534             AND l_ri_status_type <> G_ENG_CANCELLED) THEN
3535 
3536           -- Get the sequence_number of the revised item phase
3537           -- Note: moved this query inside this IF block because cancel
3538           --       status is not in the regular lifecycle phase definitions
3539           SELECT sequence_number
3540             INTO l_ri_phase_sn
3541             FROM eng_lifecycle_statuses
3542             WHERE entity_name = G_ENG_CHANGE
3543               AND entity_id1 = p_change_id
3544               AND status_code = l_ri_status_code
3545               AND active_flag = 'Y'
3546               AND rownum = 1;
3547 
3548           -- Update only those which are active
3549           -- and whose phase is ahead of the new demotion phase
3550           IF (l_ri_phase_sn > l_new_phase_sn) THEN
3551             UPDATE eng_revised_items
3552               SET status_code = p_status_code,
3553                   status_type = l_new_status_type,
3554                   last_update_date = sysdate,
3555                   last_updated_by = l_fnd_user_id,
3556                   last_update_login = l_fnd_login_id
3557               WHERE CURRENT OF c_revItems;
3558           END IF;
3559         END IF; -- revItem not implmented or cancelled
3560       END LOOP;
3561       CLOSE c_revItems;
3562     END IF; -- p_update_ri_flag = 'Y'
3563 
3564 
3565     -- R12B
3566     -- In case of Document LC Change Object, call ENG_DOCUMENT_UTIL.Change_Doc_LC_Phase
3567     -- to validate and sync Document LC Phase
3568     l_doc_lc_object_flag := ENG_DOCUMENT_UTIL.Is_Dom_Document_Lifecycle
3569                            ( p_change_id => p_change_id
3570                            , p_base_change_mgmt_type_code => l_base_cm_type_code
3571                            )  ;
3572 
3573     IF (l_doc_lc_object_flag)
3574     THEN
3575         IF g_debug_flag THEN
3576            Write_Debug('Before: calling ENG_DOCUMENT_UTIL.Change_Doc_LC_Phase');
3577            Write_Debug('  p_change_id =   ' || p_change_id);
3578            Write_Debug('  p_base_change_mgmt_type_code =   ' || l_base_cm_type_code);
3579            Write_Debug('  p_status_code = ' || p_status_code);
3580            Write_Debug('  p_action_type =   ' || G_ENG_DEMOTE);
3581         END IF;
3582 
3583         ENG_DOCUMENT_UTIL.Change_Doc_LC_Phase
3584         ( p_api_version         => 1.0
3585          ,p_init_msg_list       => FND_API.G_FALSE
3586          ,p_commit              => FND_API.G_FALSE
3587          ,p_validation_level    => p_validation_level
3588          ,p_debug               => FND_API.G_FALSE
3589          ,p_output_dir          => p_output_dir
3590          ,p_debug_filename      => p_debug_filename
3591          ,x_return_status       => l_return_status
3592          ,x_msg_count           => l_msg_count
3593          ,x_msg_data            => l_msg_data
3594          ,p_change_id           => p_change_id
3595          ,p_lc_phase_code       => p_status_code
3596          ,p_action_type         => G_ENG_DEMOTE
3597          ,p_api_caller          => p_api_caller
3598         ) ;
3599 
3600 
3601         IF g_debug_flag THEN
3602            Write_Debug('After: calling ENG_DOCUMENT_UTIL.Change_Doc_LC_Phase: ' || l_return_status) ;
3603         END IF;
3604 
3605         IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS )
3606         THEN
3607           x_return_status := l_return_status;
3608           x_msg_count := l_msg_count;
3609           x_msg_data := l_msg_data;
3610           RAISE FND_API.G_EXC_ERROR;
3611         END IF;
3612 
3613         IF g_debug_flag THEN
3614            Write_Debug('Successful: ENG_DOCUMENT_UTIL.Change_Doc_LC_Phase');
3615         END IF;
3616 
3617     END IF ; -- if ( Change Object is Documet LC Object)
3618 
3619 
3620 
3621     -- Start workflow for new phase if necessary
3622     Start_WF_OnlyIf_Necessary
3623     ( p_api_version       => 1.0
3624      ,p_init_msg_list     => FND_API.G_FALSE
3625      ,p_commit            => FND_API.G_FALSE
3626      ,p_validation_level  => p_validation_level
3627      ,p_debug             => FND_API.G_FALSE
3628      ,p_output_dir        => p_output_dir
3629      ,p_debug_filename    => p_debug_filename
3630      ,x_return_status     => l_return_status
3631      ,x_msg_count         => l_msg_count
3632      ,x_msg_data          => l_msg_data
3633      ,p_change_id         => p_change_id
3634      ,p_status_code       => p_status_code
3635      ,p_status_type       => l_new_status_type
3636      ,p_sequence_number   => l_new_phase_sn
3637      --,p_imp_eco_flag      => 'N'
3638      ,p_api_caller        => p_api_caller
3639      ,p_action_type       => G_ENG_DEMOTE
3640     );
3641 
3642 
3643       IF g_debug_flag THEN
3644         Write_Debug('After: calling Start_WF_OnlyIf_Necessary: ' || l_return_status) ;
3645       END IF;
3646 
3647 
3648     IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS ) THEN
3649       x_return_status := l_return_status;
3650       x_msg_count := l_msg_count;
3651       x_msg_data := l_msg_data;
3652       --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_WF_API');
3653       --#FND_MSG_PUB.Add;
3654       RAISE FND_API.G_EXC_ERROR;
3655     END IF;
3656 
3657     -- Standard ending code ------------------------------------------------
3658     IF FND_API.To_Boolean ( p_commit ) THEN
3659       COMMIT WORK;
3660     END IF;
3661 
3662     FND_MSG_PUB.Count_And_Get
3663     ( p_count        =>      x_msg_count,
3664       p_data         =>      x_msg_data );
3665 
3666     IF g_debug_flag THEN
3667       Write_Debug('Finish. End Of procedure: Demote_Header');
3668     END IF;
3669 
3670     IF FND_API.to_Boolean( p_debug ) THEN
3671       Close_Debug_Session;
3672     END IF;
3673 
3674   EXCEPTION
3675     WHEN FND_API.G_EXC_ERROR THEN
3676       --ROLLBACK TO Demote_Header;
3677       x_return_status := FND_API.G_RET_STS_ERROR;
3678       FND_MSG_PUB.Count_And_Get
3679       ( p_count        =>      x_msg_count
3680        ,p_data         =>      x_msg_data );
3681       IF g_debug_flag THEN
3682         Write_Debug('Rollback and Finish with expected error.') ;
3683       END IF;
3684       IF FND_API.to_Boolean( p_debug ) THEN
3685         Close_Debug_Session;
3686       END IF;
3687     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3688       --ROLLBACK TO Demote_Header;
3689       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3690       FND_MSG_PUB.Count_And_Get
3691       ( p_count        =>      x_msg_count
3692        ,p_data         =>      x_msg_data );
3693       IF g_debug_flag THEN
3694         Write_Debug('Rollback and Finish with unexpected error.') ;
3695       END IF;
3696       IF FND_API.to_Boolean( p_debug ) THEN
3697         Close_Debug_Session;
3698       END IF;
3699     WHEN OTHERS THEN
3700       --ROLLBACK TO Demote_Header;
3701       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3702       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )
3703       THEN
3704         FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME, l_api_name );
3705       END IF;
3706       FND_MSG_PUB.Count_And_Get
3707       ( p_count        =>      x_msg_count
3708        ,p_data         =>      x_msg_data );
3709       IF g_debug_flag THEN
3710         Write_Debug('Rollback and Finish with other error.') ;
3711       END IF;
3712       IF FND_API.to_Boolean( p_debug ) THEN
3713         Close_Debug_Session;
3714       END IF;
3715   END Demote_Header;
3716 
3717   PROCEDURE Demote_Header
3718   (
3719     p_api_version               IN   NUMBER                             --
3720    ,p_init_msg_list             IN   VARCHAR2 := FND_API.G_FALSE        --
3721    ,p_commit                    IN   VARCHAR2 := FND_API.G_FALSE        --
3722    ,p_validation_level          IN   NUMBER   := FND_API.G_VALID_LEVEL_FULL
3723    ,p_debug                     IN   VARCHAR2 := FND_API.G_FALSE        --
3724    ,p_output_dir                IN   VARCHAR2 := NULL
3725    ,p_debug_filename            IN   VARCHAR2 := NULL
3726    ,x_return_status             OUT  NOCOPY  VARCHAR2                   --
3727    ,x_msg_count                 OUT  NOCOPY  NUMBER                     --
3728    ,x_msg_data                  OUT  NOCOPY  VARCHAR2                   --
3729    ,p_change_id                 IN   NUMBER                             -- header's change_id
3730    ,p_status_code               IN   NUMBER                             -- new phase
3731    ,p_update_ri_flag            IN   VARCHAR2 := 'Y'                    -- can also be 'N'
3732    ,p_api_caller                IN   VARCHAR2 := NULL                   -- can also be 'WF'
3733   )
3734   IS
3735      l_sfa_line_items_exists VARCHAR2(1);
3736   BEGIN
3737      Demote_Header
3738              ( p_api_version        => p_api_version
3739               ,p_init_msg_list      => p_init_msg_list
3740               ,p_commit             => p_commit
3741               ,p_validation_level   => p_validation_level
3742               ,p_debug              => p_debug
3743               ,p_output_dir         => p_output_dir
3744               ,p_debug_filename     => p_debug_filename
3745               ,x_return_status      => x_return_status
3746               ,x_msg_count          => x_msg_count
3747               ,x_msg_data           => x_msg_data
3748               ,p_change_id          => p_change_id
3749               ,p_status_code        => p_status_code
3750               ,p_update_ri_flag     => p_update_ri_flag
3751               ,p_api_caller         => p_api_caller
3752               ,x_sfa_line_items_exists => l_sfa_line_items_exists
3753              );
3754   END Demote_Header;
3755 
3756   -- Internal procedure for promotion of a revised item
3757   PROCEDURE Promote_Revised_Item
3758   (
3759     p_api_version               IN   NUMBER                             --
3760    ,p_init_msg_list             IN   VARCHAR2 := FND_API.G_FALSE        --
3761    ,p_commit                    IN   VARCHAR2 := FND_API.G_FALSE        --
3762    ,p_validation_level          IN   NUMBER   := FND_API.G_VALID_LEVEL_FULL
3763    ,p_debug                     IN   VARCHAR2 := FND_API.G_FALSE        --
3764    ,p_output_dir                IN   VARCHAR2 := NULL
3765    ,p_debug_filename            IN   VARCHAR2 := NULL
3766    ,x_return_status             OUT  NOCOPY  VARCHAR2                   --
3767    ,x_msg_count                 OUT  NOCOPY  NUMBER                     --
3768    ,x_msg_data                  OUT  NOCOPY  VARCHAR2                   --
3769    ,p_change_id                 IN   NUMBER                             -- header's change_id
3770    ,p_object_id2                IN   NUMBER                             -- revised item sequence id
3771    ,p_status_code               IN   NUMBER                             -- new phase
3772    ,p_api_caller                IN   VARCHAR2 := NULL                   -- can also be 'WF'
3773   )
3774   IS
3775     l_api_name           CONSTANT VARCHAR2(30)  := 'Promote_Revised_Item';
3776     l_api_version        CONSTANT NUMBER := 1.0;
3777 
3778     l_fnd_user_id        NUMBER := TO_NUMBER(FND_PROFILE.VALUE('USER_ID'));
3779     l_fnd_login_id       NUMBER := TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID'));
3780 
3781     l_return_status      VARCHAR2(1);
3782     l_msg_count          NUMBER;
3783     l_msg_data           VARCHAR2(2000);
3784 
3785     l_ri_phase_sn        eng_lifecycle_statuses.sequence_number%TYPE;
3786     l_new_phase_sn       eng_lifecycle_statuses.sequence_number%TYPE;
3787     l_max_sn             eng_lifecycle_statuses.sequence_number%TYPE;
3788     l_phase_sn           eng_lifecycle_statuses.sequence_number%TYPE;
3789 
3790     l_last_imp_flag       VARCHAR2(1) := 'N';
3791 
3792     l_new_status_type    eng_change_statuses.status_type%TYPE;
3793     l_ri_status_code     eng_change_lines.status_code%TYPE;
3794     l_ri_status_type     eng_change_statuses.status_type%TYPE;
3795 
3796     l_chg_notice         eng_engineering_changes.change_notice%TYPE;
3797     l_org_id             eng_engineering_changes.organization_id%TYPE;
3798     l_request_id         NUMBER;
3799 
3800     l_row_cnt            NUMBER := 0;
3801   BEGIN
3802     -- Standard Start of API savepoint
3803     SAVEPOINT Promote_Revised_Item;
3804     -- Standard call to check for call compatibility
3805     IF NOT FND_API.Compatible_API_Call ( l_api_version
3806                                         ,p_api_version
3807                                         ,l_api_name
3808                                         ,G_PKG_NAME )
3809     THEN
3810       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3811     END IF;
3812     -- Initialize message list if p_init_msg_list is set to TRUE.
3813     IF FND_API.to_Boolean( p_init_msg_list ) THEN
3814        FND_MSG_PUB.initialize;
3815     END IF;
3816 
3817     -- For Test/Debug
3818     Check_And_Open_Debug_Session(p_debug, p_output_dir, p_debug_filename) ;
3819     -- R12 Comment out
3820     -- IF FND_API.to_Boolean( p_debug ) THEN
3821     --     Open_Debug_Session(p_output_dir, p_debug_filename ) ;
3822     -- END IF;
3823 
3824     -- Write debug message if debug mode is on
3825     IF g_debug_flag THEN
3826        Write_Debug('ENG_CHANGE_LIFECYCLE_UTIL.Promote_Revised_Item log');
3827        Write_Debug('-----------------------------------------------------');
3828        Write_Debug('p_change_id         : ' || p_change_id );
3829        Write_Debug('p_object_id2        : ' || p_object_id2 );
3830        Write_Debug('p_status_code       : ' || p_status_code );
3831        Write_Debug('p_api_caller        : ' || p_api_caller );
3832        Write_Debug('-----------------------------------------------------');
3833        Write_Debug('Initializing return status... ' );
3834     END IF;
3835 
3836     -- Initialize API return status to success
3837     x_return_status := FND_API.G_RET_STS_SUCCESS;
3838 
3839     -- FND_PROFILE package is not available for workflow (WF),
3840     -- therefore manually set WHO column values
3841     IF p_api_caller = 'WF' THEN
3842       l_fnd_user_id := G_ENG_WF_USER_ID;
3843       l_fnd_login_id := G_ENG_WF_LOGIN_ID;
3844     ELSIF p_api_caller = 'CP' THEN
3845       l_fnd_user_id := G_ENG_CP_USER_ID;
3846       l_fnd_login_id := G_ENG_CP_LOGIN_ID;
3847     END IF;
3848 
3849     -- Real code starts here -----------------------------------------------
3850 
3851     -- Get the sequence number for the current phase of the revised item
3852     SELECT sequence_number, status_code
3853       INTO l_ri_phase_sn, l_ri_status_code
3854       FROM eng_lifecycle_statuses
3855       WHERE entity_name = G_ENG_CHANGE
3856         AND entity_id1 = p_change_id
3857         AND status_code = ( SELECT status_code
3858                               FROM eng_revised_items
3859                               WHERE revised_item_sequence_id = p_object_id2)
3860         AND active_flag = 'Y'
3861         AND rownum = 1;
3862 
3863     -- Get the sequence number for the new phase of the revised item
3864     SELECT sequence_number
3865       INTO l_new_phase_sn
3866       FROM eng_lifecycle_statuses
3867       WHERE entity_name = G_ENG_CHANGE
3868         AND entity_id1 = p_change_id
3869         AND status_code = p_status_code
3870         AND active_flag = 'Y'
3871         AND rownum = 1;
3872 
3873     -- Get the status_type of the new phase
3874     SELECT status_type
3875       INTO l_new_status_type
3876       FROM eng_change_statuses
3877       WHERE status_code = p_status_code
3878         AND rownum = 1;
3879 
3880     -- Get the max sequence number in the lifecycle
3881     SELECT max(sequence_number)
3882       INTO l_max_sn
3883       FROM eng_lifecycle_statuses
3884       WHERE entity_name = G_ENG_CHANGE
3885         AND entity_id1 = p_change_id
3886         AND active_flag = 'Y';
3887 
3888     IF g_debug_flag THEN
3889        Write_Debug('Before comparing l_new_phase_sn = l_ri_phase_sn');
3890        Write_Debug('l_new_phase_sn      : ' || l_new_phase_sn );
3891        Write_Debug('l_ri_phase_sn       : ' || l_ri_phase_sn );
3892     END IF;
3893 
3894     -- Sanity check to make sure the new phase is after the current phase
3895     IF l_new_phase_sn <= l_ri_phase_sn THEN
3896       FND_MESSAGE.Set_Name('ENG','ENG_OBJ_STATE_CHANGED');
3897       FND_MSG_PUB.Add;
3898       RAISE FND_API.G_EXC_ERROR;
3899     END IF;
3900 
3901     IF g_debug_flag THEN
3902        Write_Debug('Before comparing l_new_phase_sn = l_max_sn');
3903     END IF;
3904 
3905     -- Check if the new phase is the last in the lifecycle definition
3906     IF (l_new_phase_sn = l_max_sn) THEN
3907       -- New phase is the last lifecycle phase
3908       IF g_debug_flag THEN
3909          Write_Debug('Branch: New phase is the last lifecycle phase');
3910       END IF;
3911 
3912       -- Sanity check: the new phase must be of status_type = 'IMPLEMENTED'
3913       IF (l_new_status_type <> G_ENG_IMPLEMENTED) THEN
3914         FND_MESSAGE.Set_Name('ENG','ENG_LAST_PHASE_NOT_IMP');
3915         FND_MSG_PUB.Add;
3916         RAISE FND_API.G_EXC_ERROR;
3917       END IF;
3918 
3919       -- Set the flag
3920       l_last_imp_flag := 'Y';
3921 
3922     ELSE
3923       -- New phase is not the last lifecycle phase
3924       IF g_debug_flag THEN
3925          Write_Debug('Branch: New phase is not the last lifecycle phase');
3926       END IF;
3927     END IF;
3928 
3929     -- Make sure the revised item is not implemented or cancelled
3930     -- Get the status_type of the revised item phase
3931     SELECT status_type
3932       INTO l_ri_status_type
3933       FROM eng_change_statuses
3934       WHERE status_code = l_ri_status_code
3935         AND rownum = 1;
3936 
3937     -- Update only those which are still active
3938     IF (l_ri_status_type = G_ENG_IMPLEMENTED
3939         OR l_ri_status_type = G_ENG_CANCELLED)
3940     THEN
3941       FND_MESSAGE.Set_Name('ENG','ENG_REVITEM_IMP_OR_CNCL');
3942       FND_MSG_PUB.Add;
3943       RAISE FND_API.G_EXC_ERROR;
3944     ELSE
3945       -- Case: promoting revised item to the last implement phase
3946       IF ( l_last_imp_flag = 'Y' ) THEN
3947 
3948 
3949         SELECT change_notice, organization_id
3950           INTO l_chg_notice, l_org_id
3951           FROM eng_engineering_changes
3952           WHERE change_id = p_change_id;
3953         -- If so, submit concurrent program to implement the eco and all its revised items
3954         -- as the lifecycle has already reach the last phase for implementation
3955         IF g_debug_flag THEN
3956           Write_Debug('Before: calling ENG_ECO_UTIL.Implement_ECO (for revised item)');
3957           Write_Debug('  l_chg_notice = ' || l_chg_notice);
3958           Write_Debug('  l_org_id     = ' || l_org_id);
3959         END IF;
3960 
3961         -- Call concurrent program to implement revised item
3962         ENG_ECO_UTIL.Implement_ECO
3963         ( p_api_version          =>    1.0
3964          ,p_init_msg_list        =>    FND_API.G_FALSE
3965          ,p_commit               =>    FND_API.G_FALSE
3966          ,p_validation_level     =>    p_validation_level
3967          ,p_debug                =>    p_debug --FND_API.G_FALSE
3968          ,p_output_dir           =>    p_output_dir
3969          ,p_debug_filename       =>    NULL --p_debug_filename
3970          ,x_return_status        =>    l_return_status
3971          ,x_msg_count            =>    l_msg_count
3972          ,x_msg_data             =>    l_msg_data
3973          ,p_change_id            =>    p_change_id
3974          ,p_change_notice        =>    l_chg_notice
3975          ,p_rev_item_seq_id      =>    p_object_id2
3976          ,p_org_id               =>    l_org_id
3977          ,x_request_id           =>    l_request_id
3978         );
3979 
3980         IF g_debug_flag THEN
3981           Write_Debug('After: calling ENG_ECO_UTIL.Implement_ECO (for revised item): ' || l_return_status);
3982         END IF;
3983 
3984         IF (l_return_status <>  FND_API.G_RET_STS_SUCCESS) THEN
3985           x_return_status := l_return_status;
3986           x_msg_count := l_msg_count;
3987           x_msg_data := l_msg_data;
3988           --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_IMP_ECO');
3989           --#FND_MSG_PUB.Add;
3990           RAISE FND_API.G_EXC_ERROR;
3991 
3992         ELSIF (l_request_id = 0) THEN
3993           x_return_status := l_return_status;
3994           x_msg_count := l_msg_count;
3995           x_msg_data := l_msg_data;
3996           FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CONCURRENT_PRGM');
3997           FND_MESSAGE.Set_Token('OBJECT_NAME', 'ENG_ECO_UTIL.Implement_ECO(for revised item)');
3998           FND_MSG_PUB.Add;
3999           RAISE FND_API.G_EXC_ERROR;
4000         END IF;
4001 
4002         IF g_debug_flag THEN
4003           Write_Debug('Successful: calling ENG_ECO_UTIL.Implement_ECO (for revised item)');
4004           Write_Debug('l_request_id       : ' || l_request_id );
4005         END IF;
4006 
4007         -- update parent revised item
4008         UPDATE eng_revised_items
4009           SET status_type = G_ENG_IMP_IN_PROGRESS,
4010               last_update_date = sysdate,
4011               last_updated_by = l_fnd_user_id,
4012               last_update_login = l_fnd_login_id,
4013               Implementation_req_id = l_request_id
4014           WHERE revised_item_sequence_id = p_object_id2;
4015         IF g_debug_flag THEN
4016           Write_Debug('After updating eng_revised_items (parent level).');
4017           Write_Debug('  Row count = ' || SQL%ROWCOUNT);
4018         END IF;
4019 
4020         -- update active children revised items
4021         UPDATE eng_revised_items
4022           SET status_type = G_ENG_IMP_IN_PROGRESS,
4023               last_update_date = sysdate,
4024               last_updated_by = l_fnd_user_id,
4025               last_update_login = l_fnd_login_id,
4026               Implementation_req_id = l_request_id
4027           WHERE parent_revised_item_seq_id = p_object_id2
4028             AND status_type NOT IN (G_ENG_IMPLEMENTED, G_ENG_CANCELLED);
4029         IF g_debug_flag THEN
4030           Write_Debug('After updating eng_revised_items (child level).');
4031           Write_Debug('  Row count = ' || SQL%ROWCOUNT);
4032         END IF;
4033 
4034       -- Case: promoting revised item to a phase that's not the last implement phase
4035       ELSE
4036         -- update parent revised item
4037         UPDATE eng_revised_items
4038           SET status_code = p_status_code,
4039               status_type = l_new_status_type,
4040               last_update_date = sysdate,
4041               last_updated_by = l_fnd_user_id,
4042               last_update_login = l_fnd_login_id
4043           WHERE revised_item_sequence_id = p_object_id2;
4044         IF g_debug_flag THEN
4045           Write_Debug('After updating eng_revised_items (parent level).');
4046           Write_Debug('  Row count = ' || SQL%ROWCOUNT);
4047         END IF;
4048 
4049         -- update active children revised items
4050      /*   UPDATE eng_revised_items
4051           SET status_code = p_status_code,
4052               status_type = l_new_status_type,
4053               last_update_date = sysdate,
4054               last_updated_by = l_fnd_user_id,
4055               last_update_login = l_fnd_login_id
4056           WHERE parent_revised_item_seq_id = p_object_id2
4057             AND status_type NOT IN (G_ENG_IMPLEMENTED, G_ENG_CANCELLED);*/
4058         IF g_debug_flag THEN
4059           Write_Debug('After updating eng_revised_items (child level).');
4060           Write_Debug('  Row count = ' || SQL%ROWCOUNT);
4061         END IF;
4062       END IF;
4063 
4064     END IF;
4065 
4066     IF g_debug_flag THEN
4067        Write_Debug('Done: Updating revised items to the new phase');
4068     END IF;
4069 
4070 
4071     -- Standard ending code ------------------------------------------------
4072     IF FND_API.To_Boolean ( p_commit ) THEN
4073       COMMIT WORK;
4074     END IF;
4075 
4076     FND_MSG_PUB.Count_And_Get
4077     ( p_count        =>      x_msg_count,
4078       p_data         =>      x_msg_data );
4079 
4080     IF g_debug_flag THEN
4081       Write_Debug('Finish. End Of procedure: Promote_Revised_Item');
4082     END IF;
4083 
4084     IF FND_API.to_Boolean( p_debug ) THEN
4085       Close_Debug_Session;
4086     END IF;
4087 
4088   EXCEPTION
4089     WHEN FND_API.G_EXC_ERROR THEN
4090       --ROLLBACK TO Promote_Revised_Item;
4091       x_return_status := FND_API.G_RET_STS_ERROR;
4092       FND_MSG_PUB.Count_And_Get
4093       ( p_count        =>      x_msg_count
4094        ,p_data         =>      x_msg_data );
4095       IF g_debug_flag THEN
4096         Write_Debug('Rollback and Finish with expected error.') ;
4097       END IF;
4098       IF FND_API.to_Boolean( p_debug ) THEN
4099         Close_Debug_Session;
4100       END IF;
4101     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4102       --ROLLBACK TO Promote_Revised_Item;
4103       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4104       FND_MSG_PUB.Count_And_Get
4105       ( p_count        =>      x_msg_count
4106        ,p_data         =>      x_msg_data );
4107       IF g_debug_flag THEN
4108         Write_Debug('Rollback and Finish with unexpected error.') ;
4109       END IF;
4110       IF FND_API.to_Boolean( p_debug ) THEN
4111         Close_Debug_Session;
4112       END IF;
4113     WHEN OTHERS THEN
4114       --ROLLBACK TO Promote_Revised_Item;
4115       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4116       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )
4117       THEN
4118         FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME, l_api_name );
4119       END IF;
4120       FND_MSG_PUB.Count_And_Get
4121       ( p_count        =>      x_msg_count
4122        ,p_data         =>      x_msg_data );
4123       IF g_debug_flag THEN
4124         Write_Debug('Rollback and Finish with other error.') ;
4125       END IF;
4126       IF FND_API.to_Boolean( p_debug ) THEN
4127         Close_Debug_Session;
4128       END IF;
4129   END Promote_Revised_Item;
4130 
4131 
4132   -- Internal procedure for demotion of change header (inc. revItems)
4133   PROCEDURE Demote_Revised_Item
4134   (
4135     p_api_version               IN   NUMBER                             --
4136    ,p_init_msg_list             IN   VARCHAR2 := FND_API.G_FALSE        --
4137    ,p_commit                    IN   VARCHAR2 := FND_API.G_FALSE        --
4138    ,p_validation_level          IN   NUMBER   := FND_API.G_VALID_LEVEL_FULL
4139    ,p_debug                     IN   VARCHAR2 := FND_API.G_FALSE        --
4140    ,p_output_dir                IN   VARCHAR2 := NULL
4141    ,p_debug_filename            IN   VARCHAR2 := NULL
4142    ,x_return_status             OUT  NOCOPY  VARCHAR2                   --
4143    ,x_msg_count                 OUT  NOCOPY  NUMBER                     --
4144    ,x_msg_data                  OUT  NOCOPY  VARCHAR2                   --
4145    ,p_change_id                 IN   NUMBER                             -- header's change_id
4146    ,p_object_id2                IN   NUMBER                             -- revised item sequence id
4147    ,p_status_code               IN   NUMBER                             -- new phase
4148    ,p_api_caller                IN   VARCHAR2 := NULL                   -- can also be 'WF'
4149   )
4150   IS
4151     l_api_name           CONSTANT VARCHAR2(30)  := 'Demote_Revised_Item';
4152     l_api_version        CONSTANT NUMBER := 1.0;
4153 
4154     l_fnd_user_id        NUMBER := TO_NUMBER(FND_PROFILE.VALUE('USER_ID'));
4155     l_fnd_login_id       NUMBER := TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID'));
4156 
4157     l_api_caller         VARCHAR2(2) := NULL;
4158 
4159     l_return_status      VARCHAR2(1);
4160     l_msg_count          NUMBER;
4161     l_msg_data           VARCHAR2(2000);
4162 
4163     l_new_phase_sn       eng_lifecycle_statuses.sequence_number%TYPE;
4164 
4165     l_new_status_type    eng_change_statuses.status_type%TYPE;
4166 
4167     l_ri_status_code      eng_change_lines.status_code%TYPE;
4168     -- revItem's status_code's sequence_number
4169     l_ri_phase_sn         eng_lifecycle_statuses.sequence_number%TYPE;
4170     l_ri_status_type     eng_change_statuses.status_type%TYPE;
4171 
4172     l_curr_phase_sn      eng_lifecycle_statuses.sequence_number%TYPE;
4173 
4174   BEGIN
4175     -- Standard Start of API savepoint
4176     SAVEPOINT Demote_Revised_Item;
4177     -- Standard call to check for call compatibility
4178     IF NOT FND_API.Compatible_API_Call ( l_api_version
4179                                         ,p_api_version
4180                                         ,l_api_name
4181                                         ,G_PKG_NAME )
4182     THEN
4183       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4184     END IF;
4185     -- Initialize message list if p_init_msg_list is set to TRUE.
4186     IF FND_API.to_Boolean( p_init_msg_list ) THEN
4187        FND_MSG_PUB.initialize;
4188     END IF;
4189 
4190 
4191     -- For Test/Debug
4192     Check_And_Open_Debug_Session(p_debug, p_output_dir, p_debug_filename) ;
4193     -- R12 Comment out
4194     -- IF FND_API.to_Boolean( p_debug ) THEN
4195     --     Open_Debug_Session(p_output_dir, p_debug_filename ) ;
4196     -- END IF;
4197 
4198     -- Write debug message if debug mode is on
4199     IF g_debug_flag THEN
4200        Write_Debug('ENG_CHANGE_LIFECYCLE_UTIL.Demote_Revised_Item log');
4201        Write_Debug('-----------------------------------------------------');
4202        Write_Debug('p_change_id         : ' || p_change_id );
4203        Write_Debug('p_object_id2        : ' || p_object_id2 );
4204        Write_Debug('p_status_code       : ' || p_status_code );
4205        Write_Debug('p_api_caller        : ' || p_api_caller );
4206        Write_Debug('-----------------------------------------------------');
4207        Write_Debug('Initializing return status... ' );
4208     END IF;
4209 
4210     -- Initialize API return status to success
4211     x_return_status := FND_API.G_RET_STS_SUCCESS;
4212 
4213     -- FND_PROFILE package is not available for workflow (WF),
4214     -- therefore manually set WHO column values
4215     IF p_api_caller = 'WF' THEN
4216       l_fnd_user_id := G_ENG_WF_USER_ID;
4217       l_fnd_login_id := G_ENG_WF_LOGIN_ID;
4218     ELSIF p_api_caller = 'CP' THEN
4219       l_fnd_user_id := G_ENG_CP_USER_ID;
4220       l_fnd_login_id := G_ENG_CP_LOGIN_ID;
4221     END IF;
4222 
4223     -- Real code starts here -----------------------------------------------
4224     -- Get the sequence number for the current phase of the change header
4225     SELECT sequence_number, status_code
4226       INTO l_ri_phase_sn, l_ri_status_code
4227       FROM eng_lifecycle_statuses
4228       WHERE entity_name = G_ENG_CHANGE
4229         AND entity_id1 = p_change_id
4230         AND active_flag = 'Y'
4231         AND status_code = ( SELECT status_code
4232                               FROM eng_revised_items
4233                               WHERE revised_item_sequence_id = p_object_id2)
4234         AND rownum = 1;
4235 
4236     -- Get the sequence number for the new phase of the change header
4237     SELECT sequence_number
4238       INTO l_new_phase_sn
4239       FROM eng_lifecycle_statuses
4240       WHERE entity_name = G_ENG_CHANGE
4241         AND entity_id1 = p_change_id
4242         AND status_code = p_status_code
4243         AND active_flag = 'Y'
4244         AND rownum = 1;
4245 
4246     IF g_debug_flag THEN
4247        Write_Debug('Before comparing l_new_phase_sn >= l_ri_phase_sn');
4248     END IF;
4249 
4250     -- Sanity check to make sure the new phase is after the current phase
4251     IF l_new_phase_sn >= l_ri_phase_sn THEN
4252       FND_MESSAGE.Set_Name('ENG','ENG_OBJ_STATE_CHANGED');
4253       FND_MSG_PUB.Add;
4254       RAISE FND_API.G_EXC_ERROR;
4255     END IF;
4256 
4257     -- Get the status_type of the new phase
4258     SELECT status_type
4259       INTO l_new_status_type
4260       FROM eng_change_statuses
4261       WHERE status_code = p_status_code;
4262 
4263     -- Make sure the revised item is not implemented or cancelled
4264     -- Get the status_type of the revised item phase
4265     SELECT status_type
4266       INTO l_ri_status_type
4267       FROM eng_change_statuses
4268       WHERE status_code = l_ri_status_code
4269         AND rownum = 1;
4270 
4271     -- Update only those which are still active
4272     IF (l_ri_status_type = G_ENG_IMPLEMENTED
4273         OR l_ri_status_type = G_ENG_CANCELLED)
4274     THEN
4275       FND_MESSAGE.Set_Name('ENG','ENG_REVITEM_IMP_OR_CNCL');
4276       FND_MSG_PUB.Add;
4277       RAISE FND_API.G_EXC_ERROR;
4278     ELSE
4279       -- update parent revised item
4280       UPDATE eng_revised_items
4281         SET status_code = p_status_code,
4282             status_type = l_new_status_type,
4283             last_update_date = sysdate,
4284             last_updated_by = l_fnd_user_id,
4285             last_update_login = l_fnd_login_id
4286         WHERE revised_item_sequence_id = p_object_id2;
4287       IF g_debug_flag THEN
4288         Write_Debug('After updating eng_revised_items (parent level).');
4289         Write_Debug('  Row count = ' || SQL%ROWCOUNT);
4290       END IF;
4291 
4292       -- update active children revised items
4293       UPDATE eng_revised_items
4294         SET status_code = p_status_code,
4295             status_type = l_new_status_type,
4296             last_update_date = sysdate,
4297             last_updated_by = l_fnd_user_id,
4298             last_update_login = l_fnd_login_id
4299         WHERE parent_revised_item_seq_id = p_object_id2
4300           AND status_type NOT IN (G_ENG_IMPLEMENTED, G_ENG_CANCELLED);
4301       IF g_debug_flag THEN
4302         Write_Debug('After updating eng_revised_items (child level).');
4303         Write_Debug('  Row count = ' || SQL%ROWCOUNT);
4304       END IF;
4305 
4306 
4307       -- Get the sequence number for the current phase of the change header
4308       SELECT sequence_number
4309         INTO l_curr_phase_sn
4310         FROM eng_lifecycle_statuses
4311         WHERE entity_name = G_ENG_CHANGE
4312           AND entity_id1 = p_change_id
4313           AND active_flag = 'Y'
4314           AND status_code = ( SELECT status_code
4315                                 FROM eng_engineering_changes
4316                                 WHERE change_id = p_change_id)
4317           AND rownum = 1;
4318 
4319       IF g_debug_flag THEN
4320          Write_Debug('After: getting header phase sequence number');
4321          Write_Debug('l_curr_phase_sn       : ' || l_curr_phase_sn );
4322       END IF;
4323       -- if the header's current phase is higher than the revised item's new phase
4324       -- demotion of revised item will also trigger demotion of header
4325       IF ( l_new_phase_sn < l_curr_phase_sn ) THEN
4326         IF g_debug_flag THEN
4327            Write_Debug('Demoting header is needed ');
4328            Write_Debug('Before: calling Demote_Header procedure');
4329         END IF;
4330 
4331         -- determine API caller - 'UI' should be used only once
4332         IF (p_api_caller = 'WF' OR p_api_caller = 'CP') THEN
4333           l_api_caller := p_api_caller;
4334         ELSE
4335           l_api_caller := NULL;
4336         END IF;
4337 
4338         Demote_Header
4339         ( p_api_version        => 1.0
4340          ,p_init_msg_list      => FND_API.G_FALSE
4341          ,p_commit             => FND_API.G_FALSE
4342          ,p_validation_level   => p_validation_level
4343          ,p_debug              => FND_API.G_FALSE
4344          ,p_output_dir         => p_output_dir
4345          ,p_debug_filename     => p_debug_filename
4346          ,x_return_status      => l_return_status
4347          ,x_msg_count          => l_msg_count
4348          ,x_msg_data           => l_msg_data
4349          ,p_change_id          => p_change_id
4350          ,p_status_code        => p_status_code
4351          ,p_update_ri_flag     => 'N'
4352          ,p_api_caller         => l_api_caller
4353         );
4354 
4355         IF g_debug_flag THEN
4356            Write_Debug('After: calling Demote_Header procedure: ' || l_return_status);
4357         END IF;
4358 
4359         IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS ) THEN
4360           x_return_status := l_return_status;
4361           x_msg_count := l_msg_count;
4362           x_msg_data := l_msg_data;
4363           --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_PS_API');
4364           --#FND_MESSAGE.Set_Token('OBJECT_NAME', 'Demote_Header');
4365           --#FND_MSG_PUB.Add;
4366           RAISE FND_API.G_EXC_ERROR;
4367         END IF;
4368 
4369         IF g_debug_flag THEN
4370            Write_Debug('Successful: calling Demote_Header procedure');
4371         END IF;
4372       END IF;
4373 
4374     END IF;
4375 
4376     IF g_debug_flag THEN
4377        Write_Debug('Done: Updating revised items to the new phase');
4378     END IF;
4379 
4380     -- Standard ending code ------------------------------------------------
4381     IF FND_API.To_Boolean ( p_commit ) THEN
4382       COMMIT WORK;
4383     END IF;
4384 
4385     FND_MSG_PUB.Count_And_Get
4386     ( p_count        =>      x_msg_count,
4387       p_data         =>      x_msg_data );
4388 
4389     IF g_debug_flag THEN
4390       Write_Debug('Finish. End Of procedure: Demote_Revised_Item');
4391     END IF;
4392 
4393     IF FND_API.to_Boolean( p_debug ) THEN
4394       Close_Debug_Session;
4395     END IF;
4396 
4397   EXCEPTION
4398     WHEN FND_API.G_EXC_ERROR THEN
4399       --ROLLBACK TO Demote_Revised_Item;
4400       x_return_status := FND_API.G_RET_STS_ERROR;
4401       FND_MSG_PUB.Count_And_Get
4402       ( p_count        =>      x_msg_count
4403        ,p_data         =>      x_msg_data );
4404       IF g_debug_flag THEN
4405         Write_Debug('Rollback and Finish with expected error.') ;
4406       END IF;
4407       IF FND_API.to_Boolean( p_debug ) THEN
4408         Close_Debug_Session;
4409       END IF;
4410     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4411       --ROLLBACK TO Demote_Revised_Item;
4412       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4413       FND_MSG_PUB.Count_And_Get
4414       ( p_count        =>      x_msg_count
4415        ,p_data         =>      x_msg_data );
4416       IF g_debug_flag THEN
4417         Write_Debug('Rollback and Finish with unexpected error.') ;
4418       END IF;
4419       IF FND_API.to_Boolean( p_debug ) THEN
4420         Close_Debug_Session;
4421       END IF;
4422     WHEN OTHERS THEN
4423       --ROLLBACK TO Demote_Revised_Item;
4424       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4425       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )
4426       THEN
4427         FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME, l_api_name );
4428       END IF;
4429       FND_MSG_PUB.Count_And_Get
4430       ( p_count        =>      x_msg_count
4431        ,p_data         =>      x_msg_data );
4432       IF g_debug_flag THEN
4433         Write_Debug('Rollback and Finish with other error.') ;
4434       END IF;
4435       IF FND_API.to_Boolean( p_debug ) THEN
4436         Close_Debug_Session;
4437       END IF;
4438   END Demote_Revised_Item;
4439 
4440 
4441   -- Interface procedure for combining promotion/demotion procedures
4442   -- Note that this procedure can ONLY be called directly from UI
4443   PROCEDURE Change_Phase
4444   (
4445     p_api_version               IN   NUMBER                             --
4446    ,p_init_msg_list             IN   VARCHAR2 := FND_API.G_FALSE        --
4447    ,p_commit                    IN   VARCHAR2 := FND_API.G_FALSE        --
4448    ,p_validation_level          IN   NUMBER   := FND_API.G_VALID_LEVEL_FULL
4449    ,p_debug                     IN   VARCHAR2 := FND_API.G_FALSE        --
4450    ,p_output_dir                IN   VARCHAR2 := NULL
4451    ,p_debug_filename            IN   VARCHAR2 := NULL
4452    ,x_return_status             OUT  NOCOPY  VARCHAR2                   --
4453    ,x_msg_count                 OUT  NOCOPY  NUMBER                     --
4454    ,x_msg_data                  OUT  NOCOPY  VARCHAR2                   --
4455    ,p_object_name               IN   VARCHAR2 := 'ENG_CHANGE'
4456    ,p_change_id                 IN   NUMBER                             -- header's change_id
4457    ,p_object_id2                IN   NUMBER   := NULL                   -- revised item seq id
4458    ,p_status_code               IN   NUMBER                             -- new phase
4459    ,p_update_ri_flag            IN   VARCHAR2 := 'Y'                    -- can also be 'N'
4460    ,p_api_caller                IN   VARCHAR2 := 'UI'
4461    ,p_action_type               IN   VARCHAR2 := G_ENG_PROMOTE          -- promote/demote
4462    ,p_comment                   IN   VARCHAR2 := NULL                   -- only used for co promote-to-implement action
4463    ,x_sfa_line_items_exists     OUT NOCOPY VARCHAR2
4464   )
4465   IS
4466     l_api_name           CONSTANT VARCHAR2(30)  := 'Change_Phase';
4467     l_api_version        CONSTANT NUMBER := 1.0;
4468 
4469     l_return_status      VARCHAR2(1);
4470     l_msg_count          NUMBER;
4471     l_msg_data           VARCHAR2(2000);
4472 
4473     l_curr_status_code   eng_engineering_changes.status_code%TYPE;
4474     l_curr_status_type   eng_engineering_changes.status_type%TYPE;
4475     l_wf_route_id        eng_lifecycle_statuses.change_wf_route_id%TYPE;
4476     l_wf_status          eng_lifecycle_statuses.workflow_status%TYPE;
4477   BEGIN
4478     -- Standard Start of API savepoint
4479     SAVEPOINT Change_Phase;
4480     -- Standard call to check for call compatibility
4481     IF NOT FND_API.Compatible_API_Call ( l_api_version
4482                                         ,p_api_version
4483                                         ,l_api_name
4484                                         ,G_PKG_NAME )
4485     THEN
4486       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4487     END IF;
4488     -- Initialize message list if p_init_msg_list is set to TRUE.
4489     IF FND_API.to_Boolean( p_init_msg_list ) THEN
4490        FND_MSG_PUB.initialize;
4491     END IF;
4492 
4493 
4494     -- For Test/Debug
4495     Check_And_Open_Debug_Session(p_debug, p_output_dir, p_debug_filename) ;
4496     -- R12 Comment out
4497     -- IF FND_API.to_Boolean( p_debug ) THEN
4498     --     Open_Debug_Session(p_output_dir, p_debug_filename ) ;
4499     -- END IF;
4500 
4501     -- Write debug message if debug mode is on
4502     IF g_debug_flag THEN
4503        Write_Debug('ENG_CHANGE_LIFECYCLE_UTIL.Change_Phase log');
4504        Write_Debug('-----------------------------------------------------');
4505        Write_Debug('p_object_name       : ' || p_object_name );
4506        Write_Debug('p_change_id         : ' || p_change_id );
4507        Write_Debug('p_object_id2        : ' || p_object_id2 );
4508        Write_Debug('p_status_code       : ' || p_status_code );
4509        Write_Debug('p_api_caller        : ' || p_api_caller );
4510        Write_Debug('p_action_type       : ' || p_action_type );
4511        Write_Debug('-----------------------------------------------------');
4512        Write_Debug('Initializing return status... ' );
4513     END IF;
4514 
4515     -- Initialize API return status to success
4516     x_return_status := FND_API.G_RET_STS_SUCCESS;
4517 
4518 
4519     -- Real code starts here -----------------------------------------------
4520     IF (p_object_name = G_ENG_CHANGE) THEN
4521       -- Get the current phase of the change header
4522       SELECT status_code, status_type
4523         INTO l_curr_status_code, l_curr_status_type
4524         FROM eng_engineering_changes
4525         WHERE change_id = p_change_id;
4526 
4527       -- Get the workflow route id and status for the current phase
4528       SELECT change_wf_route_id, workflow_status
4529         INTO l_wf_route_id, l_wf_status
4530         FROM eng_lifecycle_statuses
4531         WHERE entity_name = G_ENG_CHANGE
4532           AND entity_id1 = p_change_id
4533           AND status_code = l_curr_status_code
4534           AND active_flag = 'Y'
4535           AND rownum = 1;
4536 
4537       -- (x If the current phase is of type APPROVAL x), and the
4538       -- workflow is available on the current phase but it is still running,
4539       -- raise error message.
4540       IF (--l_curr_status_type = G_ENG_APPROVED AND
4541           l_wf_route_id IS NOT NULL
4542           AND l_wf_status = Eng_Workflow_Util.G_RT_IN_PROGRESS)
4543       THEN
4544         FND_MESSAGE.Set_Name('ENG','ENG_OBJ_STATE_CHANGED');
4545         FND_MSG_PUB.Add;
4546         RAISE FND_API.G_EXC_ERROR;
4547       END IF;
4548 
4549       IF ( p_action_type = G_ENG_PROMOTE ) THEN
4550         Promote_Header
4551         ( p_api_version        => 1.0
4552          ,p_init_msg_list      => FND_API.G_FALSE
4553          ,p_commit             => FND_API.G_FALSE
4554          ,p_validation_level   => p_validation_level
4555          ,p_debug              => FND_API.G_FALSE
4556          ,p_output_dir         => p_output_dir
4557          ,p_debug_filename     => p_debug_filename
4558          ,x_return_status      => l_return_status
4559          ,x_msg_count          => l_msg_count
4560          ,x_msg_data           => l_msg_data
4561          ,p_change_id          => p_change_id
4562          ,p_status_code        => p_status_code
4563          ,p_update_ri_flag     => p_update_ri_flag
4564          ,p_api_caller         => p_api_caller
4565          ,p_comment            => p_comment
4566         );
4567 
4568         IF g_debug_flag THEN
4569            Write_Debug('After: calling Promote_Header procedure: ' || l_return_status);
4570         END IF;
4571 
4572 
4573 
4574         IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS ) THEN
4575           x_return_status := l_return_status;
4576           x_msg_count := l_msg_count;
4577           x_msg_data := l_msg_data;
4578           --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_PS_API');
4579           --#FND_MESSAGE.Set_Token('OBJECT_NAME', 'Promote_Header');
4580           --#FND_MSG_PUB.Add;
4581           RAISE FND_API.G_EXC_ERROR;
4582         END IF;
4583       ELSIF ( p_action_type = G_ENG_DEMOTE ) THEN
4584 
4585         Demote_Header
4586         ( p_api_version        => 1.0
4587          ,p_init_msg_list      => FND_API.G_FALSE
4588          ,p_commit             => FND_API.G_FALSE
4589          ,p_validation_level   => p_validation_level
4590          ,p_debug              => FND_API.G_FALSE
4591          ,p_output_dir         => p_output_dir
4592          ,p_debug_filename     => p_debug_filename
4593          ,x_return_status      => l_return_status
4594          ,x_msg_count          => l_msg_count
4595          ,x_msg_data           => l_msg_data
4596          ,p_change_id          => p_change_id
4597          ,p_status_code        => p_status_code
4598          ,p_api_caller         => p_api_caller
4599          ,x_sfa_line_items_exists => x_sfa_line_items_exists
4600         );
4601 
4602 
4603         IF g_debug_flag THEN
4604            Write_Debug('After: calling Demote_Header procedure: ' || l_return_status);
4605         END IF;
4606 
4607 
4608         IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS ) THEN
4609           x_return_status := l_return_status;
4610           x_msg_count := l_msg_count;
4611           x_msg_data := l_msg_data;
4612           --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_PS_API');
4613           --#FND_MESSAGE.Set_Token('OBJECT_NAME', 'Demote_Header');
4614           --#FND_MSG_PUB.Add;
4615           RAISE FND_API.G_EXC_ERROR;
4616         END IF;
4617       ELSE
4618         IF g_debug_flag THEN
4619           Write_Debug('Error: action is neither promotion nor demotion') ;
4620         END IF;
4621       END IF;
4622     -- revised item
4623     ELSIF (p_object_name = G_ENG_REVISED_ITEM) THEN
4624 
4625       IF ( p_action_type = G_ENG_PROMOTE ) THEN
4626         Promote_Revised_Item
4627         ( p_api_version        => 1.0
4628          ,p_init_msg_list      => FND_API.G_FALSE
4629          ,p_commit             => FND_API.G_FALSE
4630          ,p_validation_level   => p_validation_level
4631          ,p_debug              => FND_API.G_FALSE
4632          ,p_output_dir         => p_output_dir
4633          ,p_debug_filename     => p_debug_filename
4634          ,x_return_status      => l_return_status
4635          ,x_msg_count          => l_msg_count
4636          ,x_msg_data           => l_msg_data
4637          ,p_change_id          => p_change_id
4638          ,p_object_id2         => p_object_id2
4639          ,p_status_code        => p_status_code
4640          ,p_api_caller         => p_api_caller
4641         );
4642 
4643 
4644         IF g_debug_flag THEN
4645            Write_Debug('After: calling Promote_Revised_Item procedure: ' || l_return_status);
4646         END IF;
4647 
4648 
4649 
4650         IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS ) THEN
4651           x_return_status := l_return_status;
4652           x_msg_count := l_msg_count;
4653           x_msg_data := l_msg_data;
4654           --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_PS_API');
4655           --#FND_MESSAGE.Set_Token('OBJECT_NAME', 'Promote_Revised_Item');
4656           --#FND_MSG_PUB.Add;
4657           RAISE FND_API.G_EXC_ERROR;
4658         END IF;
4659 
4660       ELSIF ( p_action_type = G_ENG_DEMOTE ) THEN
4661         Demote_Revised_Item
4662         ( p_api_version        => 1.0
4663          ,p_init_msg_list      => FND_API.G_FALSE
4664          ,p_commit             => FND_API.G_FALSE
4665          ,p_validation_level   => p_validation_level
4666          ,p_debug              => FND_API.G_FALSE
4667          ,p_output_dir         => p_output_dir
4668          ,p_debug_filename     => p_debug_filename
4669          ,x_return_status      => l_return_status
4670          ,x_msg_count          => l_msg_count
4671          ,x_msg_data           => l_msg_data
4672          ,p_change_id          => p_change_id
4673          ,p_object_id2         => p_object_id2
4674          ,p_status_code        => p_status_code
4675          ,p_api_caller         => p_api_caller
4676         );
4677 
4678         IF g_debug_flag THEN
4679            Write_Debug('After: calling Demote_Revised_Item procedure: ' || l_return_status);
4680         END IF;
4681 
4682 
4683         IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS ) THEN
4684           x_return_status := l_return_status;
4685           x_msg_count := l_msg_count;
4686           x_msg_data := l_msg_data;
4687           --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_PS_API');
4688           --#FND_MESSAGE.Set_Token('OBJECT_NAME', 'Demote_Revised_Item');
4689           --#FND_MSG_PUB.Add;
4690           RAISE FND_API.G_EXC_ERROR;
4691         END IF;
4692       ELSE
4693         IF g_debug_flag THEN
4694           Write_Debug('Error: action is neither promotion nor demotion') ;
4695         END IF;
4696       END IF;
4697     ELSE
4698       IF g_debug_flag THEN
4699         Write_Debug('Error: p_object_name is neither ENG_CHANGE nor ENG_REVISED_ITEM') ;
4700       END IF;
4701     END IF;
4702 
4703     -- Standard ending code ------------------------------------------------
4704     IF FND_API.To_Boolean ( p_commit ) THEN
4705       COMMIT WORK;
4706     END IF;
4707 
4708     FND_MSG_PUB.Count_And_Get
4709     ( p_count        =>      x_msg_count,
4710       p_data         =>      x_msg_data );
4711 
4712     IF g_debug_flag THEN
4713       Write_Debug('Finish. End Of procedure: Change_Phase');
4714     END IF;
4715 
4716     IF FND_API.to_Boolean( p_debug ) THEN
4717       Close_Debug_Session;
4718     END IF;
4719 
4720   EXCEPTION
4721     WHEN FND_API.G_EXC_ERROR THEN
4722       --ROLLBACK TO Change_Phase;
4723       x_return_status := FND_API.G_RET_STS_ERROR;
4724       FND_MSG_PUB.Count_And_Get
4725       ( p_count        =>      x_msg_count
4726        ,p_data         =>      x_msg_data );
4727       IF g_debug_flag THEN
4728         Write_Debug('Rollback and Finish with expected error.') ;
4729       END IF;
4730       IF FND_API.to_Boolean( p_debug ) THEN
4731         Close_Debug_Session;
4732       END IF;
4733     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4734       --ROLLBACK TO Change_Phase;
4735       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4736       FND_MSG_PUB.Count_And_Get
4737       ( p_count        =>      x_msg_count
4738        ,p_data         =>      x_msg_data );
4739       IF g_debug_flag THEN
4740         Write_Debug('Rollback and Finish with unexpected error.') ;
4741       END IF;
4742       IF FND_API.to_Boolean( p_debug ) THEN
4743         Close_Debug_Session;
4744       END IF;
4745     WHEN OTHERS THEN
4746       --ROLLBACK TO Change_Phase;
4747       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4748       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )
4749       THEN
4750         FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME, l_api_name );
4751       END IF;
4752       FND_MSG_PUB.Count_And_Get
4753       ( p_count        =>      x_msg_count
4754        ,p_data         =>      x_msg_data );
4755       IF g_debug_flag THEN
4756         Write_Debug('Rollback and Finish with other error.') ;
4757       END IF;
4758       IF FND_API.to_Boolean( p_debug ) THEN
4759         Close_Debug_Session;
4760       END IF;
4761 
4762   END Change_Phase;
4763 
4764   PROCEDURE Change_Phase
4765   (
4766     p_api_version               IN   NUMBER                             --
4767    ,p_init_msg_list             IN   VARCHAR2 := FND_API.G_FALSE        --
4768    ,p_commit                    IN   VARCHAR2 := FND_API.G_FALSE        --
4769    ,p_validation_level          IN   NUMBER   := FND_API.G_VALID_LEVEL_FULL
4770    ,p_debug                     IN   VARCHAR2 := FND_API.G_FALSE        --
4771    ,p_output_dir                IN   VARCHAR2 := NULL
4772    ,p_debug_filename            IN   VARCHAR2 := NULL
4773    ,x_return_status             OUT  NOCOPY  VARCHAR2                   --
4774    ,x_msg_count                 OUT  NOCOPY  NUMBER                     --
4775    ,x_msg_data                  OUT  NOCOPY  VARCHAR2                   --
4776    ,p_object_name               IN   VARCHAR2 := 'ENG_CHANGE'
4777    ,p_change_id                 IN   NUMBER                             -- header's change_id
4778    ,p_object_id2                IN   NUMBER   := NULL                   -- revised item seq id
4779    ,p_status_code               IN   NUMBER                             -- new phase
4780    ,p_update_ri_flag            IN   VARCHAR2 := 'Y'                    -- can also be 'N'
4781    ,p_api_caller                IN   VARCHAR2 := 'UI'
4782    ,p_action_type               IN   VARCHAR2 := G_ENG_PROMOTE          -- promote/demote
4783    ,p_comment                   IN   VARCHAR2 := NULL                   -- only used for co promote-to-implement action
4784   )
4785   IS
4786      l_sfa_line_items_exists VARCHAR2(1);
4787   BEGIN
4788           Change_Phase
4789           (
4790             p_api_version       => p_api_version
4791             ,p_init_msg_list     => p_init_msg_list
4792             ,p_commit            => p_commit
4793             ,p_validation_level  => p_validation_level
4794             ,p_debug             => p_debug
4795             ,p_output_dir        => p_output_dir
4796             ,p_debug_filename    => p_debug_filename
4797             ,x_return_status     => x_return_status
4798             ,x_msg_count         => x_msg_count
4799             ,x_msg_data          => x_msg_data
4800             ,p_object_name       => p_object_name
4801             ,p_change_id         => p_change_id
4802             ,p_object_id2        => p_object_id2
4803             ,p_status_code       => p_status_code
4804             ,p_update_ri_flag    => p_update_ri_flag
4805             ,p_api_caller        => p_api_caller
4806             ,p_action_type       => p_action_type
4807             ,p_comment           => p_comment
4808             ,x_sfa_line_items_exists => l_sfa_line_items_exists
4809           );
4810   END Change_Phase;
4811 
4812 
4813 
4814 
4815   -- Procedure to be called by WF to update lifecycle states of the change header,
4816   -- revised items, tasks and lines lifecycle states
4817   PROCEDURE Update_Lifecycle_States
4818   (
4819     p_api_version               IN   NUMBER                             --
4820    ,p_init_msg_list             IN   VARCHAR2 := FND_API.G_FALSE        --
4821    ,p_commit                    IN   VARCHAR2 := FND_API.G_FALSE        --
4822    ,p_validation_level          IN   NUMBER   := FND_API.G_VALID_LEVEL_FULL
4823    ,p_debug                     IN   VARCHAR2 := FND_API.G_FALSE        --
4824    ,p_output_dir                IN   VARCHAR2 := NULL
4825    ,p_debug_filename            IN   VARCHAR2 := NULL
4826    ,x_return_status             OUT  NOCOPY  VARCHAR2                   --
4827    ,x_msg_count                 OUT  NOCOPY  NUMBER                     --
4828    ,x_msg_data                  OUT  NOCOPY  VARCHAR2                   --
4829    ,p_change_id                 IN   NUMBER
4830    ,p_status_code               IN   NUMBER   := NULL -- passed only by WF call for p_route_status = IN_PROGRESS or CP for imp failure
4831    ,p_api_caller                IN   VARCHAR2 := NULL                   -- or 'WF'
4832    ,p_wf_route_id               IN   NUMBER
4833    ,p_route_status              IN   VARCHAR2
4834    ,p_comment                   IN   VARCHAR2 := NULL                   -- only used for co promote-to-implement action
4835   )
4836   IS
4837     l_api_name           CONSTANT VARCHAR2(30)  := 'Update_Lifecycle_States';
4838     l_api_version        CONSTANT NUMBER := 1.0;
4839 
4840     l_return_status      VARCHAR2(1);
4841     l_msg_count          NUMBER;
4842     l_msg_data           VARCHAR2(2000);
4843 
4844     l_fnd_user_id        NUMBER := TO_NUMBER(FND_PROFILE.VALUE('USER_ID'));
4845     l_fnd_login_id       NUMBER := TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID'));
4846 
4847     l_api_caller         VARCHAR2(2) := NULL;
4848 
4849     l_action_id          eng_change_actions.action_id%TYPE;
4850 
4851     l_chg_appr_status    eng_engineering_changes.approval_status_type%TYPE;
4852     l_curr_appr_status   eng_engineering_changes.approval_status_type%TYPE;
4853 
4854     l_cm_type_code       eng_engineering_changes.change_mgmt_type_code%TYPE;
4855     l_base_cm_type_code   eng_change_order_types.BASE_CHANGE_MGMT_TYPE_CODE%TYPE;
4856 
4857     l_curr_status_code   NUMBER;
4858     l_curr_status_type   NUMBER;
4859     l_next_status_code   NUMBER;
4860     l_last_status_code   NUMBER;
4861 
4862     l_status_type        NUMBER; -- status_type for p_status_code as incoming parameter
4863     l_min_appr_sn        eng_lifecycle_statuses.sequence_number%TYPE;
4864 
4865     l_last_wf_route_id   eng_lifecycle_statuses.change_wf_route_id%TYPE;
4866 
4867     l_curr_phase_sn      eng_lifecycle_statuses.sequence_number%TYPE;
4868     l_max_appr_phase_sn  eng_lifecycle_statuses.sequence_number%TYPE;
4869     l_max_phase_sn       eng_lifecycle_statuses.sequence_number%TYPE;
4870 
4871     l_nir_update_flag    VARCHAR2(1) := 'F';
4872 
4873     CURSOR c_lines IS
4874       SELECT s.status_type
4875         FROM eng_change_lines l,
4876              eng_change_statuses s
4877         WHERE l.change_id = p_change_id
4878           AND s.status_code = l.status_code;
4879     l_line_status_type   eng_change_statuses.status_type%TYPE;
4880 
4881     l_chg_notice         eng_engineering_changes.change_notice%TYPE;
4882     l_org_id             eng_engineering_changes.organization_id%TYPE;
4883     l_request_id         NUMBER;
4884 
4885     l_imp_eco_flag       VARCHAR2(1) := 'N';
4886 
4887     l_is_co_last_phase   VARCHAR2(1);
4888     l_auto_demote_status eng_lifecycle_statuses.status_code%TYPE;
4889 
4890     l_flag_imp_failed    VARCHAR2(1) := 'N';
4891 
4892   BEGIN
4893 
4894     -- Standard Start of API savepoint
4895     SAVEPOINT Update_Lifecycle_States;
4896     -- Standard call to check for call compatibility
4897     IF NOT FND_API.Compatible_API_Call ( l_api_version
4898                                         ,p_api_version
4899                                         ,l_api_name
4900                                         ,G_PKG_NAME )
4901     THEN
4902       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4903     END IF;
4904     -- Initialize message list if p_init_msg_list is set to TRUE.
4905     IF FND_API.to_Boolean( p_init_msg_list ) THEN
4906       FND_MSG_PUB.initialize;
4907     END IF;
4908 
4909 
4910     -- For Test/Debug
4911     Check_And_Open_Debug_Session(p_debug, p_output_dir, p_debug_filename) ;
4912     -- R12 Comment out
4913     -- IF FND_API.to_Boolean( p_debug ) THEN
4914     --     Open_Debug_Session(p_output_dir, p_debug_filename ) ;
4915     -- END IF;
4916 
4917     -- Write debug message if debug mode is on
4918     IF g_debug_flag THEN
4919        Write_Debug('ENG_CHANGE_LIFECYCLE_UTIL.Update_Lifecycle_States log');
4920        Write_Debug('-----------------------------------------------------');
4921        Write_Debug('p_change_id         : ' || p_change_id );
4922        Write_Debug('p_status_code       : ' || p_status_code );
4923        Write_Debug('p_api_caller        : ' || p_api_caller );
4924        Write_Debug('p_wf_route_id       : ' || p_wf_route_id );
4925        Write_Debug('p_route_status      : ' || p_route_status );
4926        Write_Debug('-----------------------------------------------------');
4927        Write_Debug('Initializing return status... ' );
4928     END IF;
4929 
4930     -- Initialize API return status to success
4931     x_return_status := FND_API.G_RET_STS_SUCCESS;
4932 
4933     -- FND_PROFILE package is not available for workflow (WF),
4934     -- therefore manually set WHO column values
4935     IF p_api_caller = 'WF' THEN
4936       l_fnd_user_id := G_ENG_WF_USER_ID;
4937       l_fnd_login_id := G_ENG_WF_LOGIN_ID;
4938     ELSIF p_api_caller = 'CP' THEN
4939       l_fnd_user_id := G_ENG_CP_USER_ID;
4940       l_fnd_login_id := G_ENG_CP_LOGIN_ID;
4941     END IF;
4942 
4943     -- Real code starts here -----------------------------------------------
4944 
4945     -- Translate workflow route statuses to change workflow status types
4946     IF (   p_route_status = Eng_Workflow_Util.G_RT_APPROVED
4947         OR p_route_status = Eng_Workflow_Util.G_RT_COMPLETED
4948         OR p_route_status = Eng_Workflow_Util.G_RT_REPLIED)
4949     THEN
4950       l_chg_appr_status := G_ENG_APPR_APPROVED;
4951     ELSIF (p_route_status = Eng_Workflow_Util.G_RT_REJECTED)
4952     THEN
4953       l_chg_appr_status := G_ENG_APPR_REJECTED;
4954     ELSIF (p_route_status = Eng_Workflow_Util.G_RT_TIME_OUT)
4955     THEN
4956       l_chg_appr_status := G_ENG_APPR_TIME_OUT;
4957     ELSIF (   p_route_status = Eng_Workflow_Util.G_RT_ABORTED
4958            OR p_route_status = Eng_Workflow_Util.G_RT_NOT_STARTED)
4959     THEN
4960       l_chg_appr_status := G_ENG_NOT_SUBMITTED;
4961     ELSIF (p_route_status = Eng_Workflow_Util.G_RT_IN_PROGRESS)
4962     THEN
4963       l_chg_appr_status := G_ENG_APPR_REQUESTED;
4964     END IF;
4965     IF g_debug_flag THEN
4966        Write_Debug('After: workflow status >> header approval status translation');
4967        Write_Debug('l_chg_appr_status   : ' || l_chg_appr_status );
4968     END IF;
4969 
4970 
4971     -- Query the required parameters
4972     -- Get the current phase, promote_phase and cm type of the change header
4973     SELECT eec.status_code, eec.promote_status_code, eec.change_mgmt_type_code,
4974            ecot.base_change_mgmt_type_code
4975       INTO l_curr_status_code, l_next_status_code, l_cm_type_code,
4976            l_base_cm_type_code
4977       FROM eng_engineering_changes eec,
4978            eng_change_order_types ecot
4979       WHERE eec.change_id = p_change_id
4980         AND ecot.change_order_type_id = eec.change_order_type_id;
4981 
4982     -- Get the current phase status_type
4983     SELECT status_type
4984       INTO l_curr_status_type
4985       FROM eng_change_statuses
4986       WHERE status_code = l_curr_status_code;
4987 
4988     -- Get the current phase's sequence number
4989     SELECT sequence_number
4990       INTO l_curr_phase_sn
4991       FROM eng_lifecycle_statuses
4992       WHERE entity_name = G_ENG_CHANGE
4993         AND entity_id1 = p_change_id
4994         AND status_code = l_curr_status_code
4995         AND active_flag = 'Y'
4996         AND rownum = 1;
4997 
4998     -- Get the sequence_number of the last phase
4999     -- Note that only phase of IMPLEMENT type can be the last phase
5000     SELECT max(sequence_number)
5001       INTO l_max_phase_sn
5002       FROM eng_lifecycle_statuses
5003       WHERE entity_name = G_ENG_CHANGE
5004         AND entity_id1 = p_change_id
5005         AND active_flag = 'Y';
5006 
5007     -- Get the sequence number of the last phase
5008     SELECT status_code, change_wf_route_id
5009       INTO l_last_status_code, l_last_wf_route_id
5010       FROM eng_lifecycle_statuses
5011       WHERE entity_name = G_ENG_CHANGE
5012         AND entity_id1 = p_change_id
5013         AND active_flag = 'Y'
5014         AND sequence_number = l_max_phase_sn;
5015 
5016 
5017     -- Case: (special 1) last implement phase of ECO: workflow completion
5018     -- Special branch of code to handle post <implement> promotion for ECO
5019     -- (invoked by eco last implement phase workflow call)
5020     IF (    (p_api_caller IS NULL OR p_api_caller <> 'CP')
5021          AND l_base_cm_type_code = G_ENG_ECO
5022          AND l_next_status_code = l_last_status_code
5023          AND p_wf_route_id = l_last_wf_route_id
5024         )
5025     THEN
5026       IF g_debug_flag THEN
5027         Write_Debug('In block: case (special) last implement phase of ECO: workflow completion.');
5028       END IF;
5029 
5030       -- Update the current phase's workflow status
5031       UPDATE eng_lifecycle_statuses
5032         SET workflow_status = p_route_status,
5033             completion_date = sysdate,    -- newly added for 3479509 fix (launch wf after implementation)
5034             last_update_date = sysdate,
5035             last_updated_by = l_fnd_user_id,
5036             last_update_login = l_fnd_login_id
5037         WHERE entity_name = G_ENG_CHANGE
5038           AND entity_id1 = p_change_id
5039           AND status_code = l_next_status_code
5040           AND active_flag = 'Y'
5041           AND rownum = 1;
5042       IF g_debug_flag THEN
5043         Write_Debug('After updating eng_lifecycle_statuses.workflow_status.');
5044         Write_Debug('  Row count = ' || SQL%ROWCOUNT);
5045       END IF;
5046 
5047     -- Case: (special 2) last implement phase of ECO direct promotion w/o wf
5048     -- Special branch of code to handle post <implement> promotion for ECO
5049     -- (invoked by Start_WF_OnlyIf_Necessary w/o wf or with p_skip_wf=Y, which
5050     --  can be recognized by the null p_wf_route_id)
5051     ELSIF (    (p_api_caller IS NULL OR p_api_caller <> 'CP')
5052             AND l_base_cm_type_code = G_ENG_ECO
5053             AND l_next_status_code = l_last_status_code
5054             AND p_wf_route_id IS NULL
5055            )
5056     THEN
5057       IF g_debug_flag THEN
5058         Write_Debug('In block: case (special) last implement phase of ECO');
5059       END IF;
5060 
5061       -- Get the required parameters before calling concurrent program for implementing ECO
5062       SELECT change_notice, organization_id
5063         INTO l_chg_notice, l_org_id
5064         FROM eng_engineering_changes
5065         WHERE change_id = p_change_id;
5066 
5067       -- If so, submit concurrent program to implement the eco and all its revised items
5068       -- as the lifecycle has already reach the last phase for implementation
5069       IF g_debug_flag THEN
5070         Write_Debug('Before: calling ENG_ECO_UTIL.Implement_ECO');
5071         Write_Debug('  l_chg_notice = ' || l_chg_notice);
5072         Write_Debug('  l_org_id     = ' || l_org_id);
5073       END IF;
5074       ENG_ECO_UTIL.Implement_ECO
5075       ( p_api_version          =>    1.0
5076        ,p_init_msg_list        =>    FND_API.G_FALSE
5077        ,p_commit               =>    FND_API.G_FALSE
5078        ,p_validation_level     =>    p_validation_level
5079        ,p_debug                =>    p_debug --FND_API.G_FALSE
5080        ,p_output_dir           =>    p_output_dir
5081        ,p_debug_filename       =>    'engact.impECO.log' --p_debug_filename
5082        ,x_return_status        =>    l_return_status
5083        ,x_msg_count            =>    l_msg_count
5084        ,x_msg_data             =>    l_msg_data
5085        ,p_change_id            =>    p_change_id
5086        ,p_change_notice        =>    l_chg_notice
5087        ,p_org_id               =>    l_org_id
5088        ,x_request_id           =>    l_request_id
5089       );
5090 
5091       IF g_debug_flag THEN
5092          Write_Debug('After: calling ENG_ECO_UTIL.Implement_ECO: ' || l_return_status);
5093       END IF;
5094 
5095 
5096       IF (l_request_id IS NULL) THEN
5097         IF g_debug_flag THEN
5098           Write_Debug('l_request_id = NULL');
5099         END IF;
5100       ELSE
5101         IF g_debug_flag THEN
5102           Write_Debug('l_request_id = ' || l_request_id);
5103         END IF;
5104 
5105         -- ATG Project
5106         -- Putting the request id flag in the implementation_req_id field
5107         UPDATE eng_engineering_changes
5108         SET implementation_req_id = l_request_id
5109         WHERE change_id = p_change_id;
5110       END IF;
5111 
5112       IF (l_return_status <>  FND_API.G_RET_STS_SUCCESS
5113           OR l_request_id IS NULL
5114           OR l_request_id = 0 )
5115       THEN
5116         l_flag_imp_failed := 'Y';
5117 
5118         IF (p_api_caller = 'WF') THEN
5119           -- Log implementation failure message
5120           UPDATE eng_engineering_changes
5121             SET status_type = G_ENG_IMP_FAILED,
5122                 last_update_date = sysdate,
5123                 last_updated_by = l_fnd_user_id,
5124                 last_update_login = l_fnd_login_id
5125             WHERE change_id = p_change_id;
5126 
5127           IF g_debug_flag THEN
5128              Write_Debug('Before: saving action log');
5129           END IF;
5130           l_action_id := 0;
5131           -- create new action log
5132           ENG_CHANGE_ACTIONS_UTIL.Create_Change_Action
5133           ( p_api_version           => 1.0
5134           , p_init_msg_list         => FND_API.G_FALSE        --
5135           , p_commit                => FND_API.G_FALSE        --
5136           , p_validation_level      => FND_API.G_VALID_LEVEL_FULL
5137           , p_debug                 => p_debug --FND_API.G_FALSE
5138           , p_output_dir            => p_output_dir
5139           , p_debug_filename        => NULL
5140           , x_return_status         => l_return_status
5141           , x_msg_count             => l_msg_count
5142           , x_msg_data              => l_msg_data
5143           , p_action_type           => ENG_CHANGE_ACTIONS_UTIL.G_ACT_IMP_FAILED
5144           , p_object_name           => G_ENG_CHANGE
5145           , p_object_id1            => p_change_id
5146           , p_object_id2            => NULL
5147           , p_object_id3            => NULL
5148           , p_object_id4            => NULL
5149           , p_object_id5            => NULL
5150           , p_parent_action_id      => -1
5151           , p_status_code           => NULL
5152           , p_action_date           => SYSDATE
5153           , p_change_description    => NULL
5154           , p_user_id               => l_fnd_user_id
5155           , p_api_caller            => p_api_caller
5156           , x_change_action_id      => l_action_id
5157           );
5158 
5159           IF g_debug_flag THEN
5160              Write_Debug('After: saving action log: ' || l_return_status);
5161              Write_Debug('l_action_id       : ' || l_action_id );
5162           END IF;
5163 
5164 
5165           IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS )
5166           THEN
5167             x_return_status := l_return_status;
5168             x_msg_count := l_msg_count;
5169             x_msg_data := l_msg_data;
5170             --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_PS_API');
5171             --#FND_MESSAGE.Set_Token('OBJECT_NAME', 'ENG_CHANGE_ACTIONS_UTIL.Create_Change_Action');
5172             --#FND_MSG_PUB.Add;
5173             RAISE FND_API.G_EXC_ERROR;
5174           END IF;
5175           IF g_debug_flag THEN
5176              Write_Debug('Successful: saving action log');
5177           END IF;
5178         ELSE -- otherwise generate and return error message to UI
5179           x_return_status := l_return_status;
5180           x_msg_count := l_msg_count;
5181           x_msg_data := l_msg_data;
5182           FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CONCURRENT_PRGM');
5183           FND_MESSAGE.Set_Token('OBJECT_NAME', 'ENG_ECO_UTIL.Implement_ECO');
5184           FND_MSG_PUB.Add;
5185           RAISE FND_API.G_EXC_ERROR;
5186         END IF;
5187       END IF;
5188 
5189       IF (l_flag_imp_failed = 'N') THEN
5190         -- Concurrent program has been set successfully, Set the flag
5191         -- for later code NOT to call PROMOTE_HEADER procedure in order to
5192         -- prevent an infinite loop
5193         l_imp_eco_flag := 'Y';
5194         IF g_debug_flag THEN
5195           Write_Debug('Successful: calling ENG_ECO_UTIL.Implement_ECO');
5196           Write_Debug('l_request_id       : ' || l_request_id );
5197         END IF;
5198 
5199         -- Update change header status_type
5200         UPDATE eng_engineering_changes
5201           SET status_type = G_ENG_IMP_IN_PROGRESS,
5202               last_update_date = sysdate,
5203               last_updated_by = l_fnd_user_id,
5204               last_update_login = l_fnd_login_id
5205           WHERE change_id = p_change_id;
5206 
5207         -- Log action for implementation_in_progress
5208         IF g_debug_flag THEN
5209           Write_Debug('Before: saving action log');
5210         END IF;
5211         l_action_id := 0;
5212         -- create new action log
5213         ENG_CHANGE_ACTIONS_UTIL.Create_Change_Action
5214         ( p_api_version           => 1.0
5215         , p_init_msg_list         => FND_API.G_FALSE        --
5216         , p_commit                => FND_API.G_FALSE        --
5217         , p_validation_level      => FND_API.G_VALID_LEVEL_FULL
5218         , p_debug                 => p_debug --FND_API.G_FALSE
5219         , p_output_dir            => p_output_dir
5220         , p_debug_filename        => NULL
5221         , x_return_status         => l_return_status
5222         , x_msg_count             => l_msg_count
5223         , x_msg_data              => l_msg_data
5224         , p_action_type           => ENG_CHANGE_ACTIONS_UTIL.G_ACT_IMP_IN_PROGRESS
5225         , p_object_name           => G_ENG_CHANGE
5226         , p_object_id1            => p_change_id
5227         , p_object_id2            => NULL
5228         , p_object_id3            => NULL
5229         , p_object_id4            => NULL
5230         , p_object_id5            => NULL
5231         , p_parent_action_id      => -1
5232         , p_status_code           => NULL
5233         , p_action_date           => SYSDATE
5234         , p_change_description    => p_comment
5235         , p_user_id               => l_fnd_user_id
5236         , p_api_caller            => p_api_caller
5237         , x_change_action_id      => l_action_id
5238         );
5239 
5240         IF g_debug_flag THEN
5241            Write_Debug('After: saving action log: ' || l_return_status );
5242            Write_Debug('l_action_id       : ' || l_action_id );
5243         END IF;
5244 
5245         IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS )
5246         THEN
5247           x_return_status := l_return_status;
5248           x_msg_count := l_msg_count;
5249           x_msg_data := l_msg_data;
5250           --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_PS_API');
5251           --#FND_MESSAGE.Set_Token('OBJECT_NAME', 'ENG_CHANGE_ACTIONS_UTIL.Create_Change_Action');
5252           --#FND_MSG_PUB.Add;
5253           RAISE FND_API.G_EXC_ERROR;
5254         END IF;
5255         IF g_debug_flag THEN
5256            Write_Debug('Successful: saving action log');
5257         END IF;
5258 
5259       END IF; -- if (l_flag_imp_failed)
5260 
5261       IF g_debug_flag THEN
5262         Write_Debug('End block: case (special) last implement phase of ECO');
5263       END IF;
5264 
5265     -- Case 1: no workflow associated with the current phase
5266     --         or the workflow is successfully approved
5267     ELSIF (   (   (p_api_caller IS NULL OR p_api_caller <> 'CP')
5268                AND p_route_status IS NULL )
5269            OR l_chg_appr_status = G_ENG_APPR_APPROVED )
5270     THEN
5271       IF g_debug_flag THEN
5272         Write_Debug('Case 1: no workflow or workflow is approved');
5273       END IF;
5274 
5275       -- Update the current phase's workflow status
5276       UPDATE eng_lifecycle_statuses
5277         SET workflow_status = p_route_status,
5278             last_update_date = sysdate,
5279             last_updated_by = l_fnd_user_id,
5280             last_update_login = l_fnd_login_id
5281         WHERE entity_name = G_ENG_CHANGE
5282           AND entity_id1 = p_change_id
5283           AND status_code = l_curr_status_code
5284           AND active_flag = 'Y'
5285           AND rownum = 1;
5286       IF g_debug_flag THEN
5287         Write_Debug('After updating eng_lifecycle_statuses.workflow_status.');
5288         Write_Debug('  Row count = ' || SQL%ROWCOUNT);
5289       END IF;
5290 
5291       -- IF the current phase is the last one, also update its completion date
5292       -- Otherwise leave it for promote_header and demote_header procedures
5293       -- to set the completion_date
5294       IF (l_curr_phase_sn = l_max_phase_sn) THEN
5295         UPDATE eng_lifecycle_statuses
5296           SET completion_date = sysdate,
5297               last_update_date = sysdate,
5298               last_updated_by = l_fnd_user_id,
5299               last_update_login = l_fnd_login_id
5300           WHERE entity_name = G_ENG_CHANGE
5301             AND entity_id1 = p_change_id
5302             AND status_code = l_curr_status_code
5303             AND active_flag = 'Y'
5304             AND rownum = 1;
5305         IF g_debug_flag THEN
5306           Write_Debug('After updating eng_lifecycle_statuses.completion_date.');
5307           Write_Debug('  Row count = ' || SQL%ROWCOUNT);
5308         END IF;
5309       END IF;
5310 
5311       IF g_debug_flag THEN
5312         Write_Debug('Current phase row updated');
5313       END IF;
5314 
5315       -- Force commit to prevent stucking on a phase in case of auto-promotion
5316       -- or auto-demotion failure triggered by workflow background engine
5317       IF (p_api_caller = 'WF' AND l_chg_appr_status = G_ENG_APPR_APPROVED)
5318       THEN
5319         COMMIT WORK;
5320       END IF;
5321 
5322 
5323 
5324       -- Update header and NIR approval status if needed
5325 
5326       -- Check if the change category is NIR, phase is on last implement phase
5327       -- and header approval status has not been set to APPROVED
5328       SELECT approval_status_type
5329         INTO l_curr_appr_status
5330         FROM eng_engineering_changes
5331         where change_id = p_change_id;
5332 
5333       IF ( l_base_cm_type_code = G_ENG_NEW_ITEM_REQ
5334            AND l_curr_phase_sn = l_max_phase_sn
5335            AND l_curr_appr_status <> G_ENG_APPR_APPROVED
5336            )
5337       THEN
5338         l_nir_update_flag := 'Y';
5339         l_chg_appr_status := G_ENG_APPR_APPROVED;
5340         IF g_debug_flag THEN
5341           Write_Debug('Current phase is last phase of NIR, and force setting of header approval status is needed');
5342         END IF;
5343       END IF;
5344 
5345 
5346       -- Check if the current phase is of type REVIEW/APPROVAL,
5347       -- and if it is the last such phase in the lifecycle of the change header
5348       --
5349       IF (    (l_curr_status_type = G_ENG_APPROVED OR l_nir_update_flag = 'Y')
5350            AND l_chg_appr_status IS NOT NULL
5351           )
5352       THEN
5353         IF g_debug_flag THEN
5354           Write_Debug('Current phase is of type APPROVAL, or last NIR phase needs to force header approval status to APPROVED');
5355         END IF;
5356 
5357         -- Get the sequence number of the last phase of type REVIEW/APPROVAL
5358         SELECT max(lcs.sequence_number)
5359           INTO l_max_appr_phase_sn
5360           FROM eng_lifecycle_statuses lcs,
5361                eng_change_statuses chs
5362           WHERE lcs.entity_name = G_ENG_CHANGE
5363             AND lcs.entity_id1 = p_change_id
5364             AND lcs.active_flag = 'Y'
5365             AND chs.status_code = lcs.status_code
5366             AND chs.status_type = G_ENG_APPROVED;
5367 
5368 
5369         IF g_debug_flag THEN
5370           Write_Debug('Check if the current phase is the last phase of type APPROVAL');
5371         END IF;
5372 
5373 
5374         -- Check if the current phase is the last phase of type REVIEW/APPROVAL
5375         -- Update header approval status if so
5376         IF ( l_curr_phase_sn = l_max_appr_phase_sn
5377              OR (p_api_caller IS NULL)
5378              OR l_nir_update_flag = 'Y'
5379              )
5380         THEN
5381           IF g_debug_flag THEN
5382             Write_Debug('Current phase is the last of such type');
5383           END IF;
5384 
5385           IF g_debug_flag THEN
5386             Write_Debug('Before: calling Update_Header_Appr_Status');
5387           END IF;
5388 
5389 
5390           -- Update change header approval status
5391           -- Launch header approval status change workflow
5392           Update_Header_Appr_Status
5393           (
5394             p_api_version               =>  1.0
5395            ,p_init_msg_list             =>  FND_API.G_FALSE
5396            ,p_commit                    =>  FND_API.G_FALSE
5397            ,p_validation_level          =>  FND_API.G_VALID_LEVEL_FULL
5398            ,p_debug                     =>  FND_API.G_FALSE
5399            ,p_output_dir                =>  p_output_dir
5400            ,p_debug_filename            =>  p_debug_filename
5401            ,x_return_status             =>  l_return_status
5402            ,x_msg_count                 =>  l_msg_count
5403            ,x_msg_data                  =>  l_msg_data
5404            ,p_change_id                 =>  p_change_id
5405            ,p_status_code               =>  l_curr_status_code
5406            ,p_appr_status               =>  l_chg_appr_status
5407            ,p_route_status              =>  p_route_status
5408            ,p_api_caller                =>  p_api_caller
5409            ,p_bypass                    =>  l_nir_update_flag
5410           );
5411 
5412 
5413           IF g_debug_flag THEN
5414             Write_Debug('After: Update_Header_Appr_Status: ' || l_return_status );
5415           END IF;
5416 
5417 
5418           IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS )
5419           THEN
5420             x_return_status := l_return_status;
5421             x_msg_count := l_msg_count;
5422             x_msg_data := l_msg_data;
5423             RAISE FND_API.G_EXC_ERROR;
5424           END IF;
5425           IF g_debug_flag THEN
5426             Write_Debug('After: calling Update_Header_Appr_Status');
5427           END IF;
5428 
5429         END IF;
5430       END IF;
5431        IF ( l_base_cm_type_code = G_ENG_NEW_ITEM_REQ
5432            AND l_curr_phase_sn = l_max_phase_sn) then
5433 	ENG_NIR_UTIL_PKG.set_nir_item_approval_status
5434           ( p_change_id          => p_change_id
5435            ,p_approval_status    => G_ENG_APPR_APPROVED
5436            ,x_return_status      => l_return_status
5437            ,x_msg_count          => l_msg_count
5438            ,x_msg_data           => l_msg_data
5439            );
5440 	END IF;
5441 
5442       -- Check if the current phase is the last of type IMPLEMENT (COMPLETE)
5443       -- Note that this code block is only used by non CO header types
5444       IF ( l_curr_status_type = G_ENG_IMPLEMENTED AND l_curr_phase_sn = l_max_phase_sn )
5445       THEN
5446         IF g_debug_flag THEN
5447           Write_Debug('Current phase is the last of type IMPLEMENTED');
5448         END IF;
5449         -- If there are still active lines, raise error message
5450         OPEN c_lines;
5451           LOOP
5452             FETCH c_lines INTO l_line_status_type;
5453             EXIT WHEN c_lines%NOTFOUND;
5454             IF ( l_line_status_type <> G_ENG_COMPLETED
5455                  AND l_line_status_type <> G_ENG_IMPLEMENTED
5456                  AND l_line_status_type <> G_ENG_CANCELLED
5457                     --   R12.C Enhancement : Added following condition because NIRs can have different line statuses
5458                  AND l_base_cm_type_code <> G_ENG_NEW_ITEM_REQ) THEN
5459               -- Stop and return error message
5460               IF g_debug_flag THEN
5461                 Write_Debug('Branch: Exists non completed/implemented/cancelled lines');
5462               END IF;
5463               FND_MESSAGE.Set_Name('ENG','ENG_EXIST_ACTIVE_LINES');
5464               FND_MSG_PUB.Add;
5465               RAISE FND_API.G_EXC_ERROR;
5466             END IF;
5467           END LOOP;
5468         CLOSE c_lines;
5469 
5470         IF g_debug_flag THEN
5471           Write_Debug('Finished checking open lines');
5472         END IF;
5473 
5474         -- If we can reach this line, it means there are no active lines
5475         -- Get the cm type for the change header
5476         SELECT eec.change_mgmt_type_code, eec.change_notice, eec.organization_id,
5477                ecot.base_change_mgmt_type_code
5478           INTO l_cm_type_code, l_chg_notice, l_org_id,
5479                l_base_cm_type_code
5480           FROM eng_engineering_changes eec,
5481                eng_change_order_types ecot
5482           WHERE eec.change_id = p_change_id
5483             AND ecot.change_order_type_id = eec.change_order_type_id;
5484 
5485         -- Complete attachment approval if needed
5486         IF g_debug_flag THEN
5487           Write_Debug('Before: calling DM attachment approval API');
5488         END IF;
5489         IF (   l_base_cm_type_code = G_ENG_ATTACHMENT_APPR
5490             OR l_base_cm_type_code = G_ENG_ATTACHMENT_REVW )
5491         THEN
5492 
5493           IF g_debug_flag THEN
5494             Write_Debug('In: calling DM attachment approval/review API');
5495             Write_Debug('p_workflow_status : ' || p_status_code );
5496             Write_Debug('p_approval_status : ' || l_chg_appr_status );
5497           END IF;
5498 
5499           ENG_ATTACHMENT_IMPLEMENTATION.Update_Attachment_Status
5500           (
5501             p_api_version         => 1.0
5502            ,p_init_msg_list       => FND_API.G_FALSE
5503            ,p_commit              => FND_API.G_FALSE
5504            ,p_validation_level    => p_validation_level
5505            ,p_debug               => p_debug --FND_API.G_FALSE
5506            ,p_output_dir          => p_output_dir
5507            ,p_debug_filename      => NULL --p_debug_filename
5508            ,x_return_status       => l_return_status
5509            ,x_msg_count           => l_msg_count
5510            ,x_msg_data            => l_msg_data
5511            ,p_change_id           => p_change_id
5512            ,p_workflow_status     => p_status_code
5513            ,p_approval_status     => G_ENG_APPR_APPROVED --l_chg_appr_status
5514            ,p_api_caller          => p_api_caller
5515           );
5516 
5517           IF g_debug_flag THEN
5518             Write_Debug('After: ENG_ATTACHMENT_IMPLEMENTATION.Update_Attachment_Status: ' || l_return_status );
5519           END IF;
5520 
5521 
5522 
5523 
5524           IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS ) THEN
5525             x_return_status := l_return_status;
5526             x_msg_count := l_msg_count;
5527             x_msg_data := l_msg_data;
5528             --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_PS_API');
5529             --#FND_MESSAGE.Set_Token('OBJECT_NAME', 'ENG_ATTACHMENT_IMPLEMENTATION.Update_Attachment_Status');
5530             --#FND_MSG_PUB.Add;
5531             RAISE FND_API.G_EXC_ERROR;
5532           END IF;
5533         END IF;
5534         IF g_debug_flag THEN
5535           Write_Debug('After: calling DM attachment approval/review API');
5536         END IF;
5537 
5538         -- moved impECO block to Update_Lifecycle_States procedure
5539       END IF;  -- current phase is the last of type IMPLEMENT?
5540 
5541 
5542       IF g_debug_flag THEN
5543         Write_Debug('Finished checking all conditions prior to promotion, ready to promote if needed');
5544       END IF;
5545       -- Finished checking all the conditions for promotion, ready to promote (if needed)
5546       -- Find the promotion phase in the order of the following priority
5547       -- 1. promote_status_code in the change header (saved by promote action)
5548       SELECT promote_status_code
5549         INTO l_next_status_code
5550         FROM eng_engineering_changes
5551         WHERE change_id = p_change_id;
5552 
5553       -- 2. auto_promote_status in the lifecycle table
5554       IF ( l_next_status_code IS NULL ) THEN
5555         IF g_debug_flag THEN
5556           Write_Debug('promote_status_code is NULL');
5557         END IF;
5558         SELECT auto_promote_status
5559           INTO l_next_status_code
5560           FROM eng_lifecycle_statuses
5561           WHERE entity_name = G_ENG_CHANGE
5562             AND entity_id1 = p_change_id
5563             AND status_code = l_curr_status_code
5564             AND active_flag = 'Y'
5565             AND rownum = 1;
5566       END IF;
5567 
5568       IF ( l_next_status_code IS NOT NULL AND l_imp_eco_flag = 'N' )
5569       THEN
5570         IF g_debug_flag THEN
5571           Write_Debug('l_next_status_code IS NOT NULL AND l_imp_eco_flag = N, call PROMOTE_HEADER');
5572         END IF;
5573 
5574         -- determine API caller
5575         IF (p_api_caller = 'WF' OR p_api_caller = 'CP') THEN
5576           l_api_caller := p_api_caller;
5577         ELSE
5578           l_api_caller := NULL;
5579         END IF;
5580 
5581         Promote_Header
5582         (
5583           p_api_version        => 1.0
5584          ,p_init_msg_list      => FND_API.G_FALSE
5585          ,p_commit             => FND_API.G_FALSE
5586          ,p_validation_level   => p_validation_level
5587          ,p_debug              => FND_API.G_FALSE
5588          ,p_output_dir         => p_output_dir
5589          ,p_debug_filename     => p_debug_filename
5590          ,x_return_status      => l_return_status
5591          ,x_msg_count          => l_msg_count
5592          ,x_msg_data           => l_msg_data
5593          ,p_change_id          => p_change_id
5594          ,p_status_code        => l_next_status_code
5595          ,p_api_caller         => l_api_caller
5596         );
5597 
5598         IF g_debug_flag THEN
5599           Write_Debug('After: Promote_Header: ' || l_return_status );
5600         END IF;
5601 
5602 
5603         IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS ) THEN
5604           x_return_status := l_return_status;
5605           x_msg_count := l_msg_count;
5606           x_msg_data := l_msg_data;
5607           --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_PS_API');
5608           --#FND_MESSAGE.Set_Token('OBJECT_NAME', 'Promote_Header');
5609           --#FND_MSG_PUB.Add;
5610           RAISE FND_API.G_EXC_ERROR;
5611         END IF;
5612 
5613         IF g_debug_flag THEN
5614           Write_Debug('After: calling PROMOTE_HEADER');
5615         END IF;
5616 
5617       ELSE
5618         -- No manual or auto promotion is set, do nothing
5619         NULL;
5620         IF g_debug_flag THEN
5621           Write_Debug('No manual or auto promotion is set, do nothing');
5622         END IF;
5623 
5624       END IF;
5625 
5626 
5627     -- Case 2: workflow is rejected
5628     ELSIF ( p_route_status = Eng_Workflow_Util.G_RT_REJECTED )
5629     THEN
5630       IF g_debug_flag THEN
5631         Write_Debug('Case 2: workflow is rejected');
5632       END IF;
5633 
5634       -- Update lifecycle table
5635       UPDATE eng_lifecycle_statuses
5636         SET workflow_status = p_route_status,
5637             last_update_date = sysdate,
5638             last_updated_by = l_fnd_user_id,
5639             last_update_login = l_fnd_login_id
5640         WHERE entity_name = G_ENG_CHANGE
5641           AND entity_id1 = p_change_id
5642           AND status_code = l_curr_status_code
5643           AND active_flag = 'Y'
5644           AND rownum = 1;
5645       IF g_debug_flag THEN
5646         Write_Debug('After updating eng_lifecycle_statuses.workflow_status.');
5647         Write_Debug('  Row count = ' || SQL%ROWCOUNT);
5648       END IF;
5649 
5650       IF g_debug_flag THEN
5651         Write_Debug('After updating current phase row');
5652       END IF;
5653 
5654       -- Get the auto demotion phase for the current phase of the change header
5655       SELECT auto_demote_status
5656         INTO l_next_status_code
5657         FROM eng_lifecycle_statuses
5658         WHERE entity_name = G_ENG_CHANGE
5659           AND entity_id1 = p_change_id
5660           AND status_code = l_curr_status_code
5661           AND active_flag = 'Y'
5662           AND rownum = 1;
5663 
5664       -- Reset promotion_status_code for the change header
5665       UPDATE eng_engineering_changes
5666         SET promote_status_code = NULL,
5667             last_update_date = sysdate,
5668             last_updated_by = l_fnd_user_id,
5669             last_update_login = l_fnd_login_id
5670         WHERE change_id = p_change_id;
5671       IF g_debug_flag THEN
5672         Write_Debug('After updating eng_engineering_changes.');
5673         Write_Debug('  Row count = ' || SQL%ROWCOUNT);
5674       END IF;
5675 
5676       IF g_debug_flag THEN
5677         Write_Debug('After getting AUTO_PROMOTE_STATUS in phase table and resetting PROMOTE_STATUS_CODE in change header table');
5678       END IF;
5679 
5680       -- update header approval status to "rejected" if the p_status_code passed
5681       -- by workflow is phase of type "approval"
5682 
5683       -- get phase type
5684       SELECT status_type
5685         INTO l_status_type
5686         FROM eng_change_statuses
5687         WHERE status_code = p_status_code;
5688       -- check if the phase type is APPROVAL
5689       IF ( l_status_type = G_ENG_APPROVED ) THEN
5690         IF g_debug_flag THEN
5691            Write_Debug('Phase type is APPROVAL');
5692         END IF;
5693 
5694         -- Update change header approval status
5695         -- Launch header approval status change workflow
5696         IF g_debug_flag THEN
5697            Write_Debug('Before: calling Update_Header_Appr_Status');
5698         END IF;
5699         Update_Header_Appr_Status
5700         (
5701           p_api_version               =>  1.0
5702          ,p_init_msg_list             =>  FND_API.G_FALSE
5703          ,p_commit                    =>  FND_API.G_FALSE
5704          ,p_validation_level          =>  FND_API.G_VALID_LEVEL_FULL
5705          ,p_debug                     =>  FND_API.G_FALSE
5706          ,p_output_dir                =>  p_output_dir
5707          ,p_debug_filename            =>  p_debug_filename
5708          ,x_return_status             =>  l_return_status
5709          ,x_msg_count                 =>  l_msg_count
5710          ,x_msg_data                  =>  l_msg_data
5711          ,p_change_id                 =>  p_change_id
5712          ,p_status_code               =>  p_status_code
5713          ,p_appr_status               =>  l_chg_appr_status
5714          ,p_route_status              =>  p_route_status
5715          ,p_api_caller                =>  p_api_caller
5716         );
5717 
5718         IF g_debug_flag THEN
5719           Write_Debug('After: calling Update_Header_Appr_Status: ' || l_return_status);
5720         END IF;
5721 
5722         IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS )
5723         THEN
5724           x_return_status := l_return_status;
5725           x_msg_count := l_msg_count;
5726           x_msg_data := l_msg_data;
5727           RAISE FND_API.G_EXC_ERROR;
5728         ELSE
5729           IF g_debug_flag THEN
5730             Write_Debug('Successful: calling Update_Header_Appr_Status');
5731           END IF;
5732         END IF;
5733 
5734       END IF; -- IF ( p_status_type = G_ENG_APPROVED )
5735 
5736       IF g_debug_flag THEN
5737         Write_Debug('Before if condition for calling DEMOTE_HEADER procedure');
5738       END IF;
5739       -- Call demotion procedure if necessary
5740       IF ( l_next_status_code IS NOT NULL ) THEN
5741         IF g_debug_flag THEN
5742           Write_Debug('l_next_status_code IS NOT NULL.');
5743           Write_Debug('Before: calling DEMOTE_HEADER procedure');
5744         END IF;
5745 
5746         -- determine API caller - 'UI' should be used only once
5747         IF (p_api_caller = 'WF' OR p_api_caller = 'CP') THEN
5748           l_api_caller := p_api_caller;
5749         ELSE
5750           l_api_caller := NULL;
5751         END IF;
5752 
5753         Demote_Header
5754         (
5755           p_api_version        => 1.0
5756          ,p_init_msg_list      => FND_API.G_FALSE
5757          ,p_commit             => FND_API.G_FALSE
5758          ,p_validation_level   => p_validation_level
5759          ,p_debug              => FND_API.G_FALSE
5760          ,p_output_dir         => p_output_dir
5761          ,p_debug_filename     => p_debug_filename
5762          ,x_return_status      => l_return_status
5763          ,x_msg_count          => l_msg_count
5764          ,x_msg_data           => l_msg_data
5765          ,p_change_id          => p_change_id
5766          ,p_status_code        => l_next_status_code
5767          ,p_api_caller         => l_api_caller
5768         );
5769         IF g_debug_flag THEN
5770           Write_Debug('After: calling DEMOTE_HEADER procedure: ' || l_return_status);
5771         END IF;
5772         IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS ) THEN
5773           x_return_status := l_return_status;
5774           x_msg_count := l_msg_count;
5775           x_msg_data := l_msg_data;
5776           --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_PS_API');
5777           --#FND_MESSAGE.Set_Token('OBJECT_NAME', 'Demote_Header');
5778           --#FND_MSG_PUB.Add;
5779           RAISE FND_API.G_EXC_ERROR;
5780         END IF;
5781         IF g_debug_flag THEN
5782            Write_Debug('Successful: calling Demote_Header procedure');
5783         END IF;
5784 
5785       END IF;  -- if (next status_code is null)
5786 
5787 
5788     -- Case 3: workflow fails due to unexpected problems
5789     ELSIF (    p_route_status = Eng_Workflow_Util.G_RT_ABORTED
5790             OR p_route_status = Eng_Workflow_Util.G_RT_TIME_OUT
5791             OR p_route_status = Eng_Workflow_Util.G_RT_NOT_STARTED )
5792     THEN
5793       IF g_debug_flag THEN
5794         Write_Debug('Case 3: workflow ends with unexpected status');
5795       END IF;
5796 
5797       -- Update lifecycle table
5798       UPDATE eng_lifecycle_statuses
5799         SET workflow_status = p_route_status,
5800             last_update_date = sysdate,
5801             last_updated_by = l_fnd_user_id,
5802             last_update_login = l_fnd_login_id
5803         WHERE entity_name = G_ENG_CHANGE
5804           AND entity_id1 = p_change_id
5805           AND status_code = l_curr_status_code
5806           AND active_flag = 'Y'
5807           AND rownum = 1;
5808       IF g_debug_flag THEN
5809         Write_Debug('After updating eng_lifecycle_statuses.workflow_status.');
5810         Write_Debug('  Row count = ' || SQL%ROWCOUNT);
5811       END IF;
5812 
5813 
5814       IF g_debug_flag THEN
5815         Write_Debug('After updating the current phase');
5816       END IF;
5817 
5818       -- Reset promotion_status_code for the change header
5819       UPDATE eng_engineering_changes
5820         SET promote_status_code = NULL,
5821             last_update_date = sysdate,
5822             last_updated_by = l_fnd_user_id,
5823             last_update_login = l_fnd_login_id
5824         WHERE change_id = p_change_id;
5825       IF g_debug_flag THEN
5826         Write_Debug('After updating eng_lifecycle_statuses.promote_status_code.');
5827         Write_Debug('  Row count = ' || SQL%ROWCOUNT);
5828       END IF;
5829 
5830       IF g_debug_flag THEN
5831         Write_Debug('After resetting promote_status_code in change header table');
5832       END IF;
5833 
5834 
5835       -- Update change header approval status
5836       -- Launch header approval status change workflow
5837       Update_Header_Appr_Status
5838       (
5839         p_api_version               =>  1.0
5840        ,p_init_msg_list             =>  FND_API.G_FALSE
5841        ,p_commit                    =>  FND_API.G_FALSE
5842        ,p_validation_level          =>  FND_API.G_VALID_LEVEL_FULL
5843        ,p_debug                     =>  FND_API.G_FALSE
5844        ,p_output_dir                =>  p_output_dir
5845        ,p_debug_filename            =>  p_debug_filename
5846        ,x_return_status             =>  l_return_status
5847        ,x_msg_count                 =>  l_msg_count
5848        ,x_msg_data                  =>  l_msg_data
5849        ,p_change_id                 =>  p_change_id
5850        ,p_status_code               =>  p_status_code
5851        ,p_appr_status               =>  l_chg_appr_status
5852        ,p_route_status              =>  p_route_status
5853        ,p_api_caller                =>  p_api_caller
5854       );
5855 
5856       IF g_debug_flag THEN
5857          Write_Debug('After: calling Update_Header_Appr_Status: ' || l_return_status);
5858       END IF;
5859 
5860       IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS )
5861       THEN
5862         x_return_status := l_return_status;
5863         x_msg_count := l_msg_count;
5864         x_msg_data := l_msg_data;
5865         RAISE FND_API.G_EXC_ERROR;
5866       END IF;
5867       IF g_debug_flag THEN
5868         Write_Debug('After: calling Update_Header_Appr_Status');
5869       END IF;
5870 
5871     -- Case 4: workflow calls to update phase of p_status_code
5872     -- (may not be current header status_code in case of last ECO implement phase)
5873     -- to IN_PROGRESS
5874     ELSIF (p_route_status = Eng_Workflow_Util.G_RT_IN_PROGRESS) THEN
5875       -- sanity check: workflow must pass p_status_code for the workflow p_route_id
5876       IF (p_status_code IS NULL) THEN
5877         FND_MESSAGE.Set_Name('ENG','ENG_OBJECT_CANT_BE_NULL');
5878         FND_MESSAGE.Set_Token('OBJECT_NAME', 'p_status_code passed by workflow');
5879         FND_MSG_PUB.Add;
5880         RAISE FND_API.G_EXC_ERROR;
5881       END IF;
5882       -- update phase workflow status
5883       UPDATE eng_lifecycle_statuses
5884         SET workflow_status = p_route_status,
5885             last_update_date = sysdate,
5886             last_updated_by = l_fnd_user_id,
5887             last_update_login = l_fnd_login_id
5888         WHERE entity_name = G_ENG_CHANGE
5889           AND entity_id1 = p_change_id
5890           AND status_code = p_status_code
5891           AND active_flag = 'Y';
5892       IF g_debug_flag THEN
5893          Write_Debug('After updating eng_lifecycle_statuses.workflow_status.');
5894          Write_Debug('  Row count = ' || SQL%ROWCOUNT);
5895       END IF;
5896 
5897       -- update header approval status to "submitted" if the p_status_code passed
5898       -- by workflow is phase of type "approval"
5899 
5900       -- get phase type
5901       SELECT status_type
5902         INTO l_status_type
5903         FROM eng_change_statuses
5904         WHERE status_code = p_status_code;
5905       -- check if the phase type is APPROVAL
5906       IF ( l_status_type = G_ENG_APPROVED ) THEN
5907         IF g_debug_flag THEN
5908            Write_Debug('Phase type is APPROVAL');
5909         END IF;
5910 
5911         -- Update change header approval status
5912         -- Launch header approval status change workflow
5913         IF g_debug_flag THEN
5914            Write_Debug('Before: calling Update_Header_Appr_Status');
5915         END IF;
5916         Update_Header_Appr_Status
5917         (
5918           p_api_version               =>  1.0
5919          ,p_init_msg_list             =>  FND_API.G_FALSE
5920          ,p_commit                    =>  FND_API.G_FALSE
5921          ,p_validation_level          =>  FND_API.G_VALID_LEVEL_FULL
5922          ,p_debug                     =>  FND_API.G_FALSE
5923          ,p_output_dir                =>  p_output_dir
5924          ,p_debug_filename            =>  p_debug_filename
5925          ,x_return_status             =>  l_return_status
5926          ,x_msg_count                 =>  l_msg_count
5927          ,x_msg_data                  =>  l_msg_data
5928          ,p_change_id                 =>  p_change_id
5929          ,p_status_code               =>  p_status_code
5930          ,p_appr_status               =>  G_ENG_APPR_REQUESTED
5931          ,p_route_status              =>  p_route_status
5932          ,p_api_caller                =>  p_api_caller
5933         );
5934 
5935         IF g_debug_flag THEN
5936           Write_Debug('After: calling Update_Header_Appr_Status: ' || l_return_status);
5937         END IF;
5938 
5939 
5940         IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS )
5941         THEN
5942           x_return_status := l_return_status;
5943           x_msg_count := l_msg_count;
5944           x_msg_data := l_msg_data;
5945           RAISE FND_API.G_EXC_ERROR;
5946         ELSE
5947           IF g_debug_flag THEN
5948             Write_Debug('Successful: calling Update_Header_Appr_Status');
5949           END IF;
5950         END IF;
5951 
5952       END IF; -- IF ( p_status_type = G_ENG_APPROVED )
5953 
5954     -- Case 6: concurrent program for implementation failure
5955     ELSIF ( p_api_caller = 'CP' AND p_status_code = G_ENG_IMP_FAILED )
5956     THEN
5957       IF g_debug_flag THEN
5958         Write_Debug('Case 6: Concurrent Program Failure');
5959       END IF;
5960 
5961       -- Log implementation failure message
5962       UPDATE eng_engineering_changes
5963         SET status_type = G_ENG_IMP_FAILED,
5964             promote_status_code = NULL,
5965             last_update_date = sysdate,
5966             last_updated_by = l_fnd_user_id,
5967             last_update_login = l_fnd_login_id
5968         WHERE change_id = p_change_id;
5969 
5970       IF g_debug_flag THEN
5971          Write_Debug('Before: saving action log');
5972       END IF;
5973       l_action_id := 0;
5974       -- create new action log
5975       ENG_CHANGE_ACTIONS_UTIL.Create_Change_Action
5976       ( p_api_version           => 1.0
5977       , p_init_msg_list         => FND_API.G_FALSE        --
5978       , p_commit                => FND_API.G_FALSE        --
5979       , p_validation_level      => FND_API.G_VALID_LEVEL_FULL
5980       , p_debug                 => p_debug --FND_API.G_FALSE
5981       , p_output_dir            => p_output_dir
5982       , p_debug_filename        => NULL
5983       , x_return_status         => l_return_status
5984       , x_msg_count             => l_msg_count
5985       , x_msg_data              => l_msg_data
5986       , p_action_type           => ENG_CHANGE_ACTIONS_UTIL.G_ACT_IMP_FAILED
5987       , p_object_name           => G_ENG_CHANGE
5988       , p_object_id1            => p_change_id
5989       , p_object_id2            => NULL
5990       , p_object_id3            => NULL
5991       , p_object_id4            => NULL
5992       , p_object_id5            => NULL
5993       , p_parent_action_id      => -1
5994       , p_status_code           => NULL
5995       , p_action_date           => SYSDATE
5996       , p_change_description    => NULL
5997       , p_user_id               => l_fnd_user_id
5998       , p_api_caller            => p_api_caller
5999       , x_change_action_id      => l_action_id
6000       );
6001 
6002       IF g_debug_flag THEN
6003          Write_Debug('After: saving action log: ' || l_return_status);
6004          Write_Debug('l_action_id       : ' || l_action_id );
6005       END IF;
6006       IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS )
6007       THEN
6008         x_return_status := l_return_status;
6009         x_msg_count := l_msg_count;
6010         x_msg_data := l_msg_data;
6011         --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_PS_API');
6012         --#FND_MESSAGE.Set_Token('OBJECT_NAME', 'ENG_CHANGE_ACTIONS_UTIL.Create_Change_Action');
6013         --#FND_MSG_PUB.Add;
6014         RAISE FND_API.G_EXC_ERROR;
6015       END IF;
6016       IF g_debug_flag THEN
6017          Write_Debug('Successful: saving action log');
6018       END IF;
6019 
6020       -- Commit anyway to make sure implementation failure action message is logged
6021       -- Note that since this branch is not invoked by workflow, no addtional check
6022       -- on p_api_caller = WF is needed, and commit is allowed
6023       COMMIT WORK;
6024 
6025       -- Check whether the header category is CO and it's on last implement phase
6026       Is_CO_On_Last_Imp_Phase
6027       (
6028         p_change_id                 => p_change_id
6029        ,p_api_caller                => p_api_caller
6030        ,x_is_co_last_phase          => l_is_co_last_phase
6031        ,x_auto_demote_status        => l_auto_demote_status
6032       );
6033       IF g_debug_flag THEN
6034         Write_Debug('After: calling procedure Is_CO_On_Last_Imp_Phase.');
6035         Write_Debug('  l_is_co_last_phase =   ' || l_is_co_last_phase);
6036         Write_Debug('  l_auto_demote_status = ' || l_auto_demote_status);
6037         Write_Debug('  l_last_status_code =   ' || l_last_status_code);
6038         Write_Debug('  l_auto_demote_status = ' || l_auto_demote_status);
6039       END IF;
6040 
6041 
6042       -- If header is CO and is on last phase,
6043       -- set header phase to last phase before demotion
6044 
6045       -- If auto demotion phase is defined, set demotion status
6046       -- If auto demotion phase is not defined, demote to the previous phase
6047       IF ( l_auto_demote_status IS NOT NULL) THEN
6048         l_next_status_code := l_auto_demote_status;
6049 
6050         -- must set header phase to last implement phase before demotion
6051         -- just to make the CO implement phase demotion as a normal demotion
6052         UPDATE eng_engineering_changes
6053           SET status_type = G_ENG_IMPLEMENTED,
6054               status_code = l_last_status_code,
6055               last_update_date = sysdate,
6056               last_updated_by = l_fnd_user_id,
6057               last_update_login = l_fnd_login_id
6058           WHERE change_id = p_change_id;
6059         l_curr_status_code := l_last_status_code;
6060 
6061         -- demote
6062         IF g_debug_flag THEN
6063            Write_Debug('Demoting header is needed ');
6064            Write_Debug('Before: calling Demote_Header procedure');
6065         END IF;
6066 
6067         Demote_Header
6068         ( p_api_version        => 1.0
6069          ,p_init_msg_list      => FND_API.G_FALSE
6070          ,p_commit             => FND_API.G_FALSE
6071          ,p_validation_level   => p_validation_level
6072          ,p_debug              => FND_API.G_FALSE
6073          ,p_output_dir         => p_output_dir
6074          ,p_debug_filename     => p_debug_filename
6075          ,x_return_status      => l_return_status
6076          ,x_msg_count          => l_msg_count
6077          ,x_msg_data           => l_msg_data
6078          ,p_change_id          => p_change_id
6079          ,p_status_code        => l_auto_demote_status
6080          ,p_api_caller         => p_api_caller
6081         );
6082         IF g_debug_flag THEN
6083            Write_Debug('After: calling Demote_Header procedure: ' || l_return_status);
6084         END IF;
6085         IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS ) THEN
6086           x_return_status := l_return_status;
6087           x_msg_count := l_msg_count;
6088           x_msg_data := l_msg_data;
6089           --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_PS_API');
6090           --#FND_MESSAGE.Set_Token('OBJECT_NAME', 'Demote_Header');
6091           --#FND_MSG_PUB.Add;
6092           RAISE FND_API.G_EXC_ERROR;
6093         END IF;
6094         IF g_debug_flag THEN
6095           Write_Debug('Successful: calling DEMOTE_HEADER procedure ');
6096         END IF;
6097 
6098       ELSE -- no auto demotion for implement phase, set to previous phase
6099 
6100         NULL;
6101         /*
6102         UPDATE eng_engineering_changes
6103           SET status_type = p_status_code, --G_ENG_IMP_FAILED
6104               promote_status_code = NULL,
6105               last_update_date = sysdate,
6106               last_updated_by = l_fnd_user_id,
6107               last_update_login = l_fnd_login_id
6108           WHERE change_id = p_change_id;
6109         l_curr_status_code := l_last_status_code;
6110 
6111         -- no demotion, but refresh the implement phase workflow route if needed
6112         IF g_debug_flag THEN
6113           Write_Debug('Before: calling Refresh_WF_Route procedure ');
6114         END IF;
6115         Refresh_WF_Route
6116         ( p_api_version         => 1.0
6117          ,p_init_msg_list       => FND_API.G_FALSE
6118          ,p_commit              => FND_API.G_FALSE        --
6119          ,p_validation_level    => p_validation_level
6120          ,p_debug               => FND_API.G_FALSE
6121          ,p_output_dir          => p_output_dir
6122          ,p_debug_filename      => p_debug_filename
6123          ,x_return_status       => l_return_status
6124          ,x_msg_count           => l_msg_count
6125          ,x_msg_data            => l_msg_data
6126          ,p_change_id           => p_change_id
6127          ,p_status_code         => l_last_status_code
6128          ,p_wf_route_id         => NULL
6129          ,p_api_caller          => p_api_caller
6130          );
6131         IF g_debug_flag THEN
6132           Write_Debug('After: calling Refresh_WF_Route procedure: ' || l_return_status);
6133         END IF;
6134         */
6135 
6136       END IF;
6137 
6138     ELSE
6139       NULL; -- IMPOSSIBLE TO REACH THIS BLOCK, THEORETICALLY SPEAKING
6140       IF g_debug_flag THEN
6141         Write_Debug('Case x: this branch should never be reached! ') ;
6142       END IF;
6143     END IF;
6144      IF (      (p_api_caller IS NULL OR p_api_caller <> 'CP')
6145                AND p_route_status IS NOT NULL
6146                AND l_curr_status_type =12)
6147      THEN
6148           ENG_DOCUMENT_UTIL.Update_Approval_Status
6149             ( p_api_version         => 1.0
6150              ,p_init_msg_list       => FND_API.G_FALSE
6151              ,p_commit              => FND_API.G_FALSE
6152              ,p_validation_level    => p_validation_level
6153              ,p_debug               => FND_API.G_FALSE
6154              ,p_output_dir          => p_output_dir
6155              ,p_debug_filename      => p_debug_filename
6156              ,x_return_status       => l_return_status     --
6157              ,x_msg_count           => l_msg_count         --
6158              ,x_msg_data            => l_msg_data          --
6159              ,p_change_id           => p_change_id         -- header's change_id
6160              ,p_approval_status     => l_chg_appr_status       -- header approval status
6161              ,p_wf_route_status     => p_route_status      -- workflow routing status (for document types)
6162              ,p_api_caller          => p_api_caller        -- Optionnal for future use
6163             );
6164 
6165 elsif ( l_curr_status_type = 12 AND  p_route_status IS null)
6166     then
6167 
6168 
6169         ENG_DOCUMENT_UTIL.Update_Approval_Status
6170             ( p_api_version         => 1.0
6171              ,p_init_msg_list       => FND_API.G_FALSE
6172              ,p_commit              => FND_API.G_FALSE
6173              ,p_validation_level    => p_validation_level
6174              ,p_debug               => FND_API.G_FALSE
6175              ,p_output_dir          => p_output_dir
6176              ,p_debug_filename      => p_debug_filename
6177              ,x_return_status       => l_return_status     --
6178              ,x_msg_count           => l_msg_count         --
6179              ,x_msg_data            => l_msg_data          --
6180              ,p_change_id           => p_change_id         -- header's change_id
6181              ,p_approval_status     => l_chg_appr_status       -- header approval status
6182              ,p_wf_route_status     => p_route_status      -- workflow routing status (for document types)
6183              ,p_api_caller          => p_api_caller        -- Optionnal for future use
6184             );
6185   END IF;
6186        -- Standard ending code ------------------------------------------------
6187     IF FND_API.To_Boolean ( p_commit ) THEN
6188       COMMIT WORK;
6189     END IF;
6190 
6191     FND_MSG_PUB.Count_And_Get
6192     ( p_count        =>      x_msg_count,
6193       p_data         =>      x_msg_data );
6194 
6195     IF g_debug_flag THEN
6196       Write_Debug('Finish. End Of procedure: Update_Lifecycle_States');
6197     END IF;
6198 
6199     IF FND_API.to_Boolean( p_debug ) THEN
6200       Close_Debug_Session;
6201     END IF;
6202 
6203   EXCEPTION
6204     WHEN FND_API.G_EXC_ERROR THEN
6205       --ROLLBACK TO Update_Lifecycle_States;
6206       x_return_status := FND_API.G_RET_STS_ERROR;
6207       FND_MSG_PUB.Count_And_Get
6208       ( p_count        =>      x_msg_count
6209        ,p_data         =>      x_msg_data );
6210       IF g_debug_flag THEN
6211         Write_Debug('Rollback and Finish with expected error.') ;
6212       END IF;
6213       IF FND_API.to_Boolean( p_debug ) THEN
6214         Close_Debug_Session;
6215       END IF;
6216     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6217       --ROLLBACK TO Update_Lifecycle_States;
6218       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6219       FND_MSG_PUB.Count_And_Get
6220       ( p_count        =>      x_msg_count
6221        ,p_data         =>      x_msg_data );
6222       IF g_debug_flag THEN
6223         Write_Debug('Rollback and Finish with unexpected error.') ;
6224       END IF;
6225       IF FND_API.to_Boolean( p_debug ) THEN
6226         Close_Debug_Session;
6227       END IF;
6228     WHEN OTHERS THEN
6229       --ROLLBACK TO Update_Lifecycle_States;
6230       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6231       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )
6232       THEN
6233         FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME, l_api_name );
6234       END IF;
6235       FND_MSG_PUB.Count_And_Get
6236       ( p_count        =>      x_msg_count
6237        ,p_data         =>      x_msg_data );
6238       IF g_debug_flag THEN
6239         Write_Debug('Rollback and Finish with other error.') ;
6240       END IF;
6241       IF FND_API.to_Boolean( p_debug ) THEN
6242         Close_Debug_Session;
6243       END IF;
6244   END Update_Lifecycle_States;
6245 
6246 
6247   -- Procedure to refresh the route_id of the currently active phase of a particular
6248   -- change header, called by WF only
6249   PROCEDURE Refresh_WF_Route
6250   (
6251     p_api_version               IN   NUMBER                             --
6252    ,p_init_msg_list             IN   VARCHAR2 := FND_API.G_FALSE        --
6253    ,p_commit                    IN   VARCHAR2 := FND_API.G_FALSE        --
6254    ,p_validation_level          IN   NUMBER   := FND_API.G_VALID_LEVEL_FULL
6255    ,p_debug                     IN   VARCHAR2 := FND_API.G_FALSE        --
6256    ,p_output_dir                IN   VARCHAR2 := NULL
6257    ,p_debug_filename            IN   VARCHAR2 := NULL
6258    ,x_return_status             OUT  NOCOPY  VARCHAR2                   --
6259    ,x_msg_count                 OUT  NOCOPY  NUMBER                     --
6260    ,x_msg_data                  OUT  NOCOPY  VARCHAR2                   --
6261    ,p_change_id                 IN   NUMBER
6262    ,p_status_code               IN   NUMBER
6263    ,p_wf_route_id               IN   NUMBER
6264    ,p_api_caller                IN   VARCHAR2 := NULL                   -- or 'WF'
6265   )
6266   IS
6267     l_api_name           CONSTANT VARCHAR2(30)  := 'Refresh_WF_Route';
6268     l_api_version        CONSTANT NUMBER := 1.0;
6269 
6270     l_return_status      VARCHAR2(1);
6271     l_msg_count          NUMBER;
6272     l_msg_data           VARCHAR2(2000);
6273 
6274     l_fnd_user_id        NUMBER := TO_NUMBER(FND_PROFILE.VALUE('USER_ID'));
6275     l_fnd_login_id       NUMBER := TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID'));
6276 
6277     CURSOR c_currPhase IS
6278       SELECT *
6279         FROM eng_lifecycle_statuses
6280         WHERE entity_name = G_ENG_CHANGE
6281           AND entity_id1 = p_change_id
6282           AND status_code = p_status_code
6283           AND active_flag = 'Y'
6284       FOR UPDATE;
6285     l_row_counter        NUMBER := 0;
6286     l_phase_row          eng_lifecycle_statuses%ROWTYPE;
6287     l_wf_route_id        eng_lifecycle_statuses.change_wf_route_id%TYPE;
6288     l_wf_route_id_new    eng_lifecycle_statuses.change_wf_route_id%TYPE;
6289     l_wf_status          eng_lifecycle_statuses.workflow_status%TYPE;
6290 
6291   BEGIN
6292     -- Standard Start of API savepoint
6293     SAVEPOINT Refresh_WF_Route;
6294     -- Standard call to check for call compatibility
6295     IF NOT FND_API.Compatible_API_Call ( l_api_version
6296                                         ,p_api_version
6297                                         ,l_api_name
6298                                         ,G_PKG_NAME )
6299     THEN
6300       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6301     END IF;
6302     -- Initialize message list if p_init_msg_list is set to TRUE.
6303     IF FND_API.to_Boolean( p_init_msg_list ) THEN
6304        FND_MSG_PUB.initialize;
6305     END IF;
6306 
6307 
6308     -- For Test/Debug
6309     Check_And_Open_Debug_Session(p_debug, p_output_dir, p_debug_filename) ;
6310     -- R12 Comment out
6311     -- IF FND_API.to_Boolean( p_debug ) THEN
6312     --     Open_Debug_Session(p_output_dir, p_debug_filename ) ;
6313     -- END IF;
6314 
6315     -- Write debug message if debug mode is on
6316     IF g_debug_flag THEN
6317       Write_Debug('ENG_CHANGE_LIFECYCLE_UTIL.Refresh_WF_Route log');
6318       Write_Debug('-----------------------------------------------------');
6319       Write_Debug('p_change_id         : ' || p_change_id );
6320       Write_Debug('p_status_code       : ' || p_status_code );
6321       IF (p_wf_route_id IS NULL)
6322       THEN
6323         Write_Debug('p_wf_route_id       : null' );
6324       ELSE
6325         Write_Debug('p_wf_route_id       : ' || p_wf_route_id );
6326       END IF;
6327       Write_Debug('p_api_caller        : ' || p_api_caller );
6328       Write_Debug('-----------------------------------------------------');
6329       Write_Debug('Initializing return status... ' );
6330     END IF;
6331 
6332     -- Initialize API return status to success
6333     x_return_status := FND_API.G_RET_STS_SUCCESS;
6334 
6335     -- FND_PROFILE package is not available for workflow (WF),
6336     -- therefore manually set WHO column values
6337     IF p_api_caller = 'WF' THEN
6338       l_fnd_user_id := G_ENG_WF_USER_ID;
6339       l_fnd_login_id := G_ENG_WF_LOGIN_ID;
6340     ELSIF p_api_caller = 'CP' THEN
6341       l_fnd_user_id := G_ENG_CP_USER_ID;
6342       l_fnd_login_id := G_ENG_CP_LOGIN_ID;
6343     END IF;
6344 
6345     -- Real code starts here -----------------------------------------------
6346     OPEN c_currPhase;
6347       LOOP
6348         FETCH c_currPhase
6349           INTO l_phase_row;
6350         EXIT WHEN c_currPhase%NOTFOUND;
6351         l_row_counter := l_row_counter + 1;
6352 
6353         l_wf_route_id := l_phase_row.change_wf_route_id;
6354         l_wf_status   := l_phase_row.workflow_status;
6355 
6356         /*
6357         -- Make sure a workflow is already associated with the current phase
6358         IF (l_wf_route_id IS NULL) THEN
6359           FND_MESSAGE.Set_Name('ENG','ENG_WF_NOT_DEFINED_ON_PHASE');
6360           FND_MSG_PUB.Add;
6361           RAISE FND_API.G_EXC_ERROR;
6362         END IF;
6363         */
6364 
6365         -- if passed wf route id is null, it means to generate new id
6366         -- from the old id that's stored in the table, so we need to make sure
6367         -- first that the old id is not null
6368         IF (p_wf_route_id IS NULL)
6369         THEN
6370           IF g_debug_flag THEN
6371             Write_Debug('Branch: p_route_id is null') ;
6372           END IF;
6373           -- if the old wf route id doesn't exist then do nothing; otherwise refresh it
6374           IF (l_wf_route_id IS NOT NULL) THEN
6375             -- Get a new workflow route_id
6376             Eng_Change_Route_Util.REFRESH_ROUTE
6377             ( X_NEW_ROUTE_ID   => l_wf_route_id_new,
6378               P_ROUTE_ID       => l_wf_route_id,
6379               P_USER_ID        => l_fnd_user_id,
6380               P_API_CALLER     => p_api_caller
6381             );
6382             -- Replace the old id with the new id
6383             UPDATE eng_lifecycle_statuses
6384               SET change_wf_route_id = l_wf_route_id_new,
6385                   workflow_status = ENG_WORKFLOW_UTIL.G_RT_NOT_STARTED,
6386                   last_update_date = sysdate,
6387                   last_updated_by = l_fnd_user_id,
6388                   last_update_login = l_fnd_login_id
6389               WHERE CURRENT OF c_currPhase;
6390             IF g_debug_flag THEN
6391               Write_Debug('Updated route_id') ;
6392             END IF;
6393           END IF;
6394 
6395         -- else it is the regular case where wf route id is passed as not null
6396         ELSE
6397           IF g_debug_flag THEN
6398             Write_Debug('Branch: p_route_id is not null') ;
6399           END IF;
6400           UPDATE eng_lifecycle_statuses
6401             SET change_wf_route_id = p_wf_route_id,
6402                 workflow_status = ENG_WORKFLOW_UTIL.G_RT_NOT_STARTED,
6403                 last_update_date = sysdate,
6404                 last_updated_by = l_fnd_user_id,
6405                 last_update_login = l_fnd_login_id
6406             WHERE CURRENT OF c_currPhase;
6407           IF g_debug_flag THEN
6408             Write_Debug('Updated route_id') ;
6409           END IF;
6410         END IF;
6411 
6412     END LOOP;
6413     CLOSE c_currPhase;
6414 
6415     -- One and only one record should be found with the cursor
6416     IF (l_row_counter <> 1) THEN
6417       FND_MESSAGE.Set_Name('ENG','ENG_NOT_EXACTLY_ONE_RECORD');
6418       FND_MSG_PUB.Add;
6419       RAISE FND_API.G_EXC_ERROR;
6420     END IF;
6421 
6422     -- Standard ending code ------------------------------------------------
6423     IF FND_API.To_Boolean ( p_commit ) THEN
6424       COMMIT WORK;
6425     END IF;
6426 
6427     FND_MSG_PUB.Count_And_Get
6428     ( p_count        =>      x_msg_count,
6429       p_data         =>      x_msg_data );
6430 
6431     IF g_debug_flag THEN
6432       Write_Debug('Finish. End Of procedure: Refresh_WF_Route') ;
6433     END IF;
6434 
6435     IF FND_API.to_Boolean( p_debug ) THEN
6436       Close_Debug_Session;
6437     END IF;
6438 
6439   EXCEPTION
6440     WHEN FND_API.G_EXC_ERROR THEN
6441       --ROLLBACK TO Refresh_WF_Route;
6442       x_return_status := FND_API.G_RET_STS_ERROR;
6443       FND_MSG_PUB.Count_And_Get
6444       ( p_count        =>      x_msg_count
6445        ,p_data         =>      x_msg_data );
6446       IF g_debug_flag THEN
6447         Write_Debug('Rollback and Finish with expected error.') ;
6448       END IF;
6449       IF FND_API.to_Boolean( p_debug ) THEN
6450         Close_Debug_Session;
6451       END IF;
6452     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6453       --ROLLBACK TO Refresh_WF_Route;
6454       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6455       FND_MSG_PUB.Count_And_Get
6456       ( p_count        =>      x_msg_count
6457        ,p_data         =>      x_msg_data );
6458       IF g_debug_flag THEN
6459         Write_Debug('Rollback and Finish with unexpected error.') ;
6460       END IF;
6461       IF FND_API.to_Boolean( p_debug ) THEN
6462         Close_Debug_Session;
6463       END IF;
6464     WHEN OTHERS THEN
6465       --ROLLBACK TO Refresh_WF_Route;
6466       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6467       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )
6468       THEN
6469         FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME, l_api_name );
6470       END IF;
6471       FND_MSG_PUB.Count_And_Get
6472       ( p_count        =>      x_msg_count
6473        ,p_data         =>      x_msg_data );
6474       IF g_debug_flag THEN
6475         Write_Debug('Rollback and Finish with other error.') ;
6476       END IF;
6477       IF FND_API.to_Boolean( p_debug ) THEN
6478         Close_Debug_Session;
6479       END IF;
6480 
6481   END Refresh_WF_Route;
6482 
6483 
6484 
6485   -- Procedure to automatically initialize lifecycles for a new change header
6486   -- It also takes care of automatically launching the workflow if needed
6487   -- Note that this procedure can ONLY be called directly from UI
6488   -- In R12, Added p_init_status_code to speicify the initialized status code
6489   -- In R12, Added p_init_option  'WF_ONLY'  Start Only WF
6490   PROCEDURE Init_Lifecycle
6491   (
6492     p_api_version               IN   NUMBER                             --
6493    ,p_init_msg_list             IN   VARCHAR2 := FND_API.G_FALSE        --
6494    ,p_commit                    IN   VARCHAR2 := FND_API.G_FALSE        --
6495    ,p_validation_level          IN   NUMBER   := FND_API.G_VALID_LEVEL_FULL
6496    ,p_debug                     IN   VARCHAR2 := FND_API.G_FALSE        --
6497    ,p_output_dir                IN   VARCHAR2 := NULL
6498    ,p_debug_filename            IN   VARCHAR2 := NULL
6499    ,x_return_status             OUT  NOCOPY  VARCHAR2                   --
6500    ,x_msg_count                 OUT  NOCOPY  NUMBER                     --
6501    ,x_msg_data                  OUT  NOCOPY  VARCHAR2                   --
6502    ,p_change_id                 IN   NUMBER                             -- header's change_id
6503    ,p_api_caller                IN   VARCHAR2 := 'UI'
6504    ,p_init_status_code          IN   NUMBER   := NULL                   -- R12
6505    ,p_init_option               IN   VARCHAR2 := NULL                   -- R12
6506   )
6507   IS
6508 
6509     l_api_name           CONSTANT VARCHAR2(30)  := 'Init_Lifecycle';
6510     l_api_version        CONSTANT NUMBER := 1.0;
6511 
6512     l_return_status      VARCHAR2(1);
6513     l_msg_count          NUMBER;
6514     l_msg_data           VARCHAR2(2000);
6515 
6516     l_fnd_user_id        NUMBER := TO_NUMBER(FND_PROFILE.VALUE('USER_ID'));
6517     l_fnd_login_id       NUMBER := TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID'));
6518 
6519     l_cm_type_code       eng_engineering_changes.change_mgmt_type_code%TYPE;
6520     l_base_cm_type_code  eng_change_order_types.BASE_CHANGE_MGMT_TYPE_CODE%TYPE;
6521     l_bug_number         NUMBER := 0;
6522 
6523     l_initial_phase_sn   eng_lifecycle_statuses.sequence_number%TYPE;
6524 
6525     l_status_code        eng_lifecycle_statuses.status_code%TYPE;
6526     l_status_type        eng_engineering_changes.status_type%TYPE;
6527     l_sequence_number    eng_lifecycle_statuses.sequence_number%TYPE;
6528     l_wf_route_id        eng_lifecycle_statuses.change_wf_route_id%TYPE;
6529 
6530     l_pls_block          VARCHAR2(5000);
6531 
6532     -- for auto-propagation if it's defined for first phase
6533     l_auto_prop_flag     eng_type_org_properties.AUTO_PROPAGATE_FLAG%TYPE;
6534     l_change_notice      eng_engineering_changes.change_notice%TYPE;
6535     l_hierarchy_name     per_organization_structures.name%TYPE;
6536     l_org_name           org_organization_definitions.organization_name%TYPE;
6537     l_row_cnt            NUMBER := 0;
6538     CURSOR c_orgProp IS
6539       SELECT op.auto_propagate_flag,
6540              ec.change_notice,
6541              pos.name,
6542              ood.name organization_name
6543         FROM eng_type_org_properties op,
6544              eng_engineering_changes ec,
6545              per_organization_structures pos,
6546              hr_all_organization_units_tl ood
6547         WHERE ec.change_id = p_change_id
6548           --AND ec.PLM_OR_ERP_CHANGE = 'PLM'
6549           AND op.change_type_id = ec.change_order_type_id
6550           AND op.organization_id = ec.organization_id
6551           AND op.propagation_status_code = l_status_code
6552           AND ec.hierarchy_id IS NOT NULL
6553           AND ec.organization_id IS NOT NULL
6554           AND pos.organization_structure_id(+) = ec.hierarchy_id
6555           AND ood.organization_id(+) = ec.organization_id
6556           AND ood.LANGUAGE = USERENV('LANG');
6557 
6558     l_request_id         NUMBER := 0;
6559 
6560     l_action_id          eng_change_actions.action_id%TYPE;
6561     l_param_list         WF_PARAMETER_LIST_T := WF_PARAMETER_LIST_T();
6562     l_wf_only_flag       BOOLEAN ;
6563 
6564   BEGIN
6565     -- Standard Start of API savepoint
6566     SAVEPOINT Init_Lifecycle;
6567     -- Standard call to check for call compatibility
6568     IF NOT FND_API.Compatible_API_Call ( l_api_version
6569                                         ,p_api_version
6570                                         ,l_api_name
6571                                         ,G_PKG_NAME )
6572     THEN
6573       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6574     END IF;
6575     -- Initialize message list if p_init_msg_list is set to TRUE.
6576     IF FND_API.to_Boolean( p_init_msg_list ) THEN
6577        FND_MSG_PUB.initialize;
6578     END IF;
6579 
6580     -- For Test/Debug
6581     Check_And_Open_Debug_Session(p_debug, p_output_dir, p_debug_filename) ;
6582     -- R12 Comment out
6583     -- IF FND_API.to_Boolean( p_debug ) THEN
6584     --     Open_Debug_Session(p_output_dir, p_debug_filename ) ;
6585     -- END IF;
6586 
6587     -- Write debug message if debug mode is on
6588     IF g_debug_flag THEN
6589        Write_Debug('ENG_CHANGE_LIFECYCLE_UTIL.Init_Lifecycle log');
6590        Write_Debug('-----------------------------------------------------');
6591        Write_Debug('p_change_id         : ' || p_change_id );
6592        Write_Debug('p_init_status_code  : ' || p_init_status_code );
6593        Write_Debug('p_init_option       : ' || p_init_option );
6594        Write_Debug('-----------------------------------------------------');
6595        Write_Debug('Initializing return status... ' );
6596     END IF;
6597 
6598     -- Initialize API return status to success
6599     x_return_status := FND_API.G_RET_STS_SUCCESS;
6600 
6601     -- FND_PROFILE package is not available for workflow (WF),
6602     -- therefore manually set WHO column values
6603     IF p_api_caller = 'WF' THEN
6604       l_fnd_user_id := G_ENG_WF_USER_ID;
6605       l_fnd_login_id := G_ENG_WF_LOGIN_ID;
6606     ELSIF p_api_caller = 'CP' THEN
6607       l_fnd_user_id := G_ENG_CP_USER_ID;
6608       l_fnd_login_id := G_ENG_CP_LOGIN_ID;
6609     END IF;
6610 
6611     -- Real code starts here -----------------------------------------------
6612 
6613 
6614     -- Get the first phase from the change header's lifecycle definition
6615     -- In R12, added p_init_status_code condition here
6616     SELECT min(sequence_number)
6617       INTO l_initial_phase_sn
6618       FROM eng_lifecycle_statuses
6619       WHERE entity_name = G_ENG_CHANGE
6620         AND entity_id1 = p_change_id
6621         AND active_flag = 'Y'
6622         AND (status_code = p_init_status_code  OR p_init_status_code IS NULL) ;
6623 
6624 
6625     SELECT lcs.status_code, ecs.status_type
6626       INTO l_status_code, l_status_type
6627       FROM eng_lifecycle_statuses lcs,
6628            eng_change_statuses ecs
6629       WHERE lcs.entity_name = G_ENG_CHANGE
6630         AND lcs.entity_id1 = p_change_id
6631         AND lcs.active_flag = 'Y'
6632         AND lcs.sequence_number = l_initial_phase_sn
6633         AND ecs.status_code = lcs.status_code
6634         AND rownum = 1;
6635 
6636     IF g_debug_flag THEN
6637        Write_Debug('After: getting first phase from lifecycle definitions');
6638     END IF;
6639 
6640     -- and set it in the eng_engineering_changes table
6641     UPDATE eng_engineering_changes
6642       SET status_code = l_status_code,
6643           status_type = l_status_type,
6644           initiation_date = sysdate
6645       WHERE change_id = p_change_id;
6646 
6647     IF g_debug_flag THEN
6648        Write_Debug('After: settting status_type and status_code of header to first phase');
6649     END IF;
6650 
6651     /*
6652     -- Get the current phase from change header
6653     SELECT status_code, status_type
6654       INTO l_status_code, l_status_type
6655       FROM eng_engineering_changes
6656       WHERE change_id = p_change_id;
6657 
6658     IF g_debug_flag THEN
6659        Write_Debug('After: getting current phase from change header');
6660     END IF;
6661     */
6662 
6663     -- Get the sequence number for the current phase
6664     SELECT sequence_number, change_wf_route_id
6665       INTO l_sequence_number, l_wf_route_id
6666       FROM eng_lifecycle_statuses
6667       WHERE entity_name = G_ENG_CHANGE
6668         AND entity_id1 = p_change_id
6669         AND active_flag = 'Y'
6670         AND status_code = l_status_code;
6671 
6672     IF g_debug_flag THEN
6673        Write_Debug('After: getting lifecycle definitions for the current phase');
6674     END IF;
6675 
6676     /*
6677     -- Sanity check
6678     IF (l_sequence_number <> l_initial_phase_sn) THEN
6679       FND_MESSAGE.Set_Name('ENG','ENG_CURR_PHASE_NOT_FIRST');
6680       FND_MSG_PUB.Add;
6681       RAISE FND_API.G_EXC_ERROR;
6682     END IF;
6683     IF g_debug_flag THEN
6684       Write_Debug('After: sanity check of the current phase as the first phase');
6685     END IF;
6686     */
6687 
6688     -- Sanity check on workflow
6689     IF (l_wf_route_id IS NOT NULL) THEN
6690       IF g_debug_flag THEN
6691         Write_Debug('Initial phase has workflow.');
6692       END IF;
6693     END IF;
6694 
6695     -- Initialize workflow_status for all phases that have workflows
6696     UPDATE eng_lifecycle_statuses
6697       SET workflow_status = Eng_Workflow_Util.G_RT_NOT_STARTED,
6698           last_update_date = sysdate,
6699           last_updated_by = l_fnd_user_id,
6700           last_update_login = l_fnd_login_id
6701       WHERE entity_name = G_ENG_CHANGE
6702         AND entity_id1 = p_change_id
6703         AND active_flag = 'Y'
6704         AND change_wf_route_id IS NOT NULL;
6705     IF g_debug_flag THEN
6706       Write_Debug('After updating eng_lifecycle_statuses.workflow_status.');
6707       Write_Debug('  Row count = ' || SQL%ROWCOUNT);
6708     END IF;
6709 
6710     IF g_debug_flag THEN
6711       Write_Debug('After: Initialize workflow_status for all phases that have workflows');
6712     END IF;
6713 
6714     -- Update current phase row
6715     UPDATE eng_lifecycle_statuses
6716       SET start_date = sysdate,
6717           last_update_date = sysdate,
6718           last_updated_by = l_fnd_user_id,
6719           last_update_login = l_fnd_login_id
6720       WHERE entity_name = G_ENG_CHANGE
6721         AND entity_id1 = p_change_id
6722         AND status_code = l_status_code
6723         AND active_flag = 'Y';
6724 
6725     IF g_debug_flag THEN
6726       Write_Debug('After updating eng_lifecycle_statuses.start_date.');
6727       Write_Debug('  Row count = ' || SQL%ROWCOUNT);
6728     END IF;
6729 
6730     IF g_debug_flag THEN
6731       Write_Debug('After: updating the current phase row');
6732     END IF;
6733 
6734 
6735     l_wf_only_flag := FALSE ;
6736     IF (p_init_option = 'WF_ONLY')
6737     THEN
6738 
6739 IF g_debug_flag THEN
6740     Write_Debug('WF Only Flag is TRUE. . .');
6741 END IF;
6742 
6743         l_wf_only_flag := TRUE ;
6744 
6745     END IF ;
6746 
6747     IF (NOT l_wf_only_flag)
6748     THEN
6749 
6750 
6751 IF g_debug_flag THEN
6752     Write_Debug('Before: saving action log');
6753 END IF;
6754         -- Record SUBMIT action log and raise submit business event
6755         l_action_id := 0;
6756         -- create new action log
6757         ENG_CHANGE_ACTIONS_UTIL.Create_Change_Action
6758         ( p_api_version           => 1.0
6759         , p_init_msg_list         => FND_API.G_FALSE        --
6760         , p_commit                => FND_API.G_FALSE        --
6761         , p_validation_level      => FND_API.G_VALID_LEVEL_FULL
6762         , p_debug                 => p_debug --FND_API.G_FALSE
6763         , p_output_dir            => p_output_dir
6764         , p_debug_filename        => 'engact.CreateAction.log'
6765         , x_return_status         => l_return_status
6766         , x_msg_count             => l_msg_count
6767         , x_msg_data              => l_msg_data
6768         , p_action_type           => ENG_CHANGE_ACTIONS_UTIL.G_ACT_SUBMIT
6769         , p_object_name           => G_ENG_CHANGE
6770         , p_object_id1            => p_change_id
6771         , p_object_id2            => NULL
6772         , p_object_id3            => NULL
6773         , p_object_id4            => NULL
6774         , p_object_id5            => NULL
6775         , p_parent_action_id      => -1
6776         , p_status_code           => NULL
6777         , p_action_date           => SYSDATE
6778         , p_change_description    => NULL
6779         , p_user_id               => l_fnd_user_id
6780         , p_api_caller            => p_api_caller
6781         , x_change_action_id      => l_action_id
6782         );
6783         IF g_debug_flag THEN
6784            Write_Debug('After: saving action log: ' || l_return_status);
6785            Write_Debug('l_action_id       : ' || l_action_id );
6786         END IF;
6787         IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS )
6788         THEN
6789           x_return_status := l_return_status;
6790           x_msg_count := l_msg_count;
6791           x_msg_data := l_msg_data;
6792           --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_PS_API');
6793           --#FND_MESSAGE.Set_Token('OBJECT_NAME', 'ENG_CHANGE_ACTIONS_UTIL.Create_Change_Action');
6794           --#FND_MSG_PUB.Add;
6795           RAISE FND_API.G_EXC_ERROR;
6796         END IF;
6797         IF g_debug_flag THEN
6798            Write_Debug('Successful: saving action log');
6799         END IF;
6800 
6801         -- raise business event for SUBMIT action
6802         -- Select cm type and base type code for upcoming API calls
6803         SELECT eec.change_mgmt_type_code, ecot.base_change_mgmt_type_code
6804           INTO l_cm_type_code, l_base_cm_type_code
6805           FROM eng_engineering_changes eec,
6806                eng_change_order_types ecot
6807           WHERE eec.change_id = p_change_id
6808             AND ecot.change_order_type_id = eec.change_order_type_id;
6809 
6810      -- for nir set all included item approval status to submitted for approval
6811 	IF (l_base_cm_type_code = 'NEW_ITEM_REQUEST') THEN
6812 	   ENG_NIR_UTIL_PKG.set_nir_item_approval_status (p_change_id,
6813 							  Eng_Workflow_Util.G_REQUESTED,
6814 							  x_return_status => l_return_status,
6815 							  x_msg_count => l_msg_count,
6816 							  x_msg_data => l_msg_data);
6817 	END IF;
6818 
6819         -- Raise the approval status change business event
6820         -- Adding event parameters to the list
6821         WF_EVENT.AddParameterToList
6822         ( p_name          => ENG_CHANGE_BES_UTIL.G_BES_PARAM_CHANGE_ID
6823          ,p_value         => to_char(p_change_id)
6824          ,p_parameterList => l_param_list
6825          );
6826         WF_EVENT.AddParameterToList
6827         ( p_name          => ENG_CHANGE_BES_UTIL.G_BES_PARAM_BASE_CM_TYPE_CODE
6828          ,p_value         => l_base_cm_type_code
6829          ,p_parameterList => l_param_list
6830          );
6831 
6832         -- Raise event
6833         WF_EVENT.RAISE
6834         ( p_event_name    => ENG_CHANGE_BES_UTIL.G_CMBE_HEADER_SUBMIT
6835          ,p_event_key     => p_change_id
6836          ,p_parameters    => l_param_list
6837          );
6838         l_param_list.DELETE;
6839 
6840 
6841         -- auto propagate if necessary
6842         l_row_cnt := 0;
6843         OPEN c_orgProp;
6844           LOOP
6845             FETCH c_orgProp
6846               INTO l_auto_prop_flag,
6847                    l_change_notice,
6848                    l_hierarchy_name,
6849                    l_org_name;
6850             EXIT WHEN c_orgProp%NOTFOUND;
6851 
6852             l_row_cnt := l_row_cnt + 1;
6853             -- verify the uniqueness of the record
6854             IF (l_row_cnt > 1) THEN
6855               IF g_debug_flag THEN
6856                 Write_Debug('Error: more than one propagation policy is found');
6857               END IF;
6858             END IF;
6859 
6860             IF g_debug_flag THEN
6861               Write_Debug('one record for propagation policy is found');
6862               Write_Debug('l_auto_prop_flag         : ' || l_auto_prop_flag );
6863               Write_Debug('l_change_notice          : ' || l_change_notice );
6864               Write_Debug('l_hierarchy_name         : ' || l_hierarchy_name );
6865               Write_Debug('l_org_name               : ' || l_org_name );
6866             END IF;
6867 
6868             IF ( l_auto_prop_flag = 'Y') THEN
6869               IF g_debug_flag THEN
6870                 Write_Debug('which needs auto propagation');
6871               END IF;
6872 
6873               ENG_ECO_UTIL.Propagate_ECO
6874               (
6875                 p_api_version          =>    1.0
6876                ,p_init_msg_list        =>    FND_API.G_FALSE
6877                ,p_commit               =>    FND_API.G_FALSE
6878                ,p_validation_level     =>    FND_API.G_VALID_LEVEL_FULL
6879                ,p_debug                =>    p_debug --FND_API.G_FALSE
6880                ,p_output_dir           =>    p_output_dir
6881                ,p_debug_filename       =>    NULL --p_debug_filename
6882                ,x_return_status        =>    l_return_status
6883                ,x_msg_count            =>    l_msg_count
6884                ,x_msg_data             =>    l_msg_data
6885                ,p_change_id            =>    p_change_id
6886                ,p_change_notice        =>    l_change_notice
6887                ,p_hierarchy_name       =>    l_hierarchy_name
6888                ,p_org_name             =>    l_org_name
6889                ,x_request_id           =>    l_request_id
6890               );
6891 
6892               IF g_debug_flag THEN
6893                 Write_Debug('After: calling propagate_eco API: ' || l_return_status);
6894               END IF;
6895 
6896               IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS ) THEN
6897                 x_return_status := l_return_status;
6898                 x_msg_count := l_msg_count;
6899                 x_msg_data := l_msg_data;
6900                 --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_PS_API');
6901                 --#FND_MESSAGE.Set_Token('OBJECT_NAME', 'ENG_ECO_UTIL.Propagate_ECO');
6902                 --#FND_MSG_PUB.Add;
6903                 RAISE FND_API.G_EXC_ERROR;
6904               END IF;
6905 
6906               IF g_debug_flag THEN
6907                 Write_Debug('Successful: calling propagate_eco API');
6908                 Write_Debug('l_request_id       : ' || l_request_id );
6909               END IF;
6910 
6911               IF g_debug_flag THEN
6912                  Write_Debug('Before: saving action log');
6913               END IF;
6914               l_action_id := 0;
6915               -- create new action log
6916               -- Bug Fix: 3547844
6917               -- In case of Auto-Propgation Action Log
6918               -- Who column is Workflow
6919               ENG_CHANGE_ACTIONS_UTIL.Create_Change_Action
6920               ( p_api_version           => 1.0
6921               , p_init_msg_list         => FND_API.G_FALSE        --
6922               , p_commit                => FND_API.G_FALSE        --
6923               , p_validation_level      => FND_API.G_VALID_LEVEL_FULL
6924               , p_debug                 => p_debug --FND_API.G_FALSE
6925               , p_output_dir            => p_output_dir
6926               , p_debug_filename        => NULL
6927               , x_return_status         => l_return_status
6928               , x_msg_count             => l_msg_count
6929               , x_msg_data              => l_msg_data
6930               , p_action_type           => ENG_CHANGE_ACTIONS_UTIL.G_ACT_PROPAGATE
6931               , p_object_name           => G_ENG_CHANGE
6932               , p_object_id1            => p_change_id
6933               , p_object_id2            => NULL
6934               , p_object_id3            => NULL
6935               , p_object_id4            => NULL
6936               , p_object_id5            => NULL
6937               , p_parent_action_id      => -1
6938               , p_status_code           => NULL
6939               , p_action_date           => SYSDATE
6940               , p_change_description    => NULL
6941               , p_user_id               => G_ENG_WF_USER_ID
6942               , p_api_caller            => 'WF'
6943               , x_change_action_id      => l_action_id
6944               );
6945 
6946               IF g_debug_flag THEN
6947                  Write_Debug('After: saving action log: ' || l_return_status);
6948                  Write_Debug('l_action_id       : ' || l_action_id );
6949               END IF;
6950 
6951               IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS )
6952               THEN
6953                 x_return_status := l_return_status;
6954                 x_msg_count := l_msg_count;
6955                 x_msg_data := l_msg_data;
6956                 --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_PS_API');
6957                 --#FND_MESSAGE.Set_Token('OBJECT_NAME', 'ENG_CHANGE_ACTIONS_UTIL.Create_Change_Action');
6958                 --#FND_MSG_PUB.Add;
6959                 RAISE FND_API.G_EXC_ERROR;
6960               END IF;
6961               IF g_debug_flag THEN
6962                  Write_Debug('Successful: saving action log');
6963               END IF;
6964 
6965             ELSE
6966               IF g_debug_flag THEN
6967                 Write_Debug('which does not need auto propagation');
6968               END IF;
6969             END IF;
6970 
6971           END LOOP;
6972         CLOSE c_orgProp;
6973         IF g_debug_flag THEN
6974            Write_Debug('After checking propagation policy');
6975         END IF;
6976 
6977     END IF ; -- (NOT l_wf_only_flag)
6978 
6979 
6980     -- Start workflow for new phase if necessary
6981     Start_WF_OnlyIf_Necessary
6982     ( p_api_version       => 1.0
6983      ,p_init_msg_list     => FND_API.G_FALSE
6984      ,p_commit            => FND_API.G_FALSE
6985      ,p_validation_level  => p_validation_level
6986      ,p_debug             => FND_API.G_FALSE
6987      ,p_output_dir        => p_output_dir
6988      ,p_debug_filename    => p_debug_filename
6989      ,x_return_status     => l_return_status
6990      ,x_msg_count         => l_msg_count
6991      ,x_msg_data          => l_msg_data
6992      ,p_change_id         => p_change_id
6993      ,p_status_code       => l_status_code
6994      ,p_status_type       => l_status_type
6995      ,p_sequence_number   => l_sequence_number
6996      --,p_imp_eco_flag      => 'N'
6997      ,p_api_caller        => p_api_caller
6998      ,p_action_type       => NULL
6999     );
7000 
7001     IF g_debug_flag THEN
7002        Write_Debug('After call to procedure Start_WF_OnlyIf_Necessary: ' || l_return_status);
7003     END IF;
7004 
7005     IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS ) THEN
7006       x_return_status := l_return_status;
7007       x_msg_count := l_msg_count;
7008       x_msg_data := l_msg_data;
7009       --#FND_MESSAGE.Set_Name('ENG','ENG_ERROR_CALLING_WF_API');
7010       --#FND_MSG_PUB.Add;
7011       RAISE FND_API.G_EXC_ERROR;
7012     END IF;
7013 
7014     -- Standard ending code ------------------------------------------------
7015     IF FND_API.To_Boolean ( p_commit ) THEN
7016       COMMIT WORK;
7017     END IF;
7018 
7019     FND_MSG_PUB.Count_And_Get
7020     ( p_count        =>      x_msg_count,
7021       p_data         =>      x_msg_data );
7022 
7023     IF g_debug_flag THEN
7024       Write_Debug('Finish. End Of procedure: Init_Lifecycle') ;
7025     END IF;
7026 
7027     IF FND_API.to_Boolean( p_debug ) THEN
7028       Close_Debug_Session;
7029     END IF;
7030 
7031   EXCEPTION
7032     WHEN FND_API.G_EXC_ERROR THEN
7033      --ROLLBACK TO Init_Lifecycle;
7034       x_return_status := FND_API.G_RET_STS_ERROR;
7035       FND_MSG_PUB.Count_And_Get
7036       ( p_count        =>      x_msg_count
7037        ,p_data         =>      x_msg_data );
7038       IF g_debug_flag THEN
7039         Write_Debug('Rollback and Finish with expected error.') ;
7040       END IF;
7041       IF FND_API.to_Boolean( p_debug ) THEN
7042         Close_Debug_Session;
7043       END IF;
7044     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7045       --ROLLBACK TO Init_Lifecycle;
7046       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7047       FND_MSG_PUB.Count_And_Get
7048       ( p_count        =>      x_msg_count
7049        ,p_data         =>      x_msg_data );
7050       IF g_debug_flag THEN
7051         Write_Debug('Rollback and Finish with unexpected error.') ;
7052       END IF;
7053       IF FND_API.to_Boolean( p_debug ) THEN
7054         Close_Debug_Session;
7055       END IF;
7056     WHEN OTHERS THEN
7057       --ROLLBACK TO Init_Lifecycle;
7058       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7059       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )
7060       THEN
7061         FND_MSG_PUB.Add_Exc_Msg (  G_PKG_NAME, l_api_name );
7062       END IF;
7063       FND_MSG_PUB.Count_And_Get
7064       ( p_count        =>      x_msg_count
7065        ,p_data         =>      x_msg_data );
7066       IF g_debug_flag THEN
7067         Write_Debug('Rollback and Finish with other error.') ;
7068       END IF;
7069       IF FND_API.to_Boolean( p_debug ) THEN
7070         Close_Debug_Session;
7071       END IF;
7072 
7073   END Init_Lifecycle;
7074 
7075 
7076 
7077 
7078   -- Procedure to be called by revised item implementation concurrent
7079   -- program to set its status_type
7080   PROCEDURE Update_RevItem_Lifecycle
7081   (
7082     p_api_version               IN   NUMBER                             --
7083    ,p_init_msg_list             IN   VARCHAR2 := FND_API.G_FALSE        --
7084    ,p_commit                    IN   VARCHAR2 := FND_API.G_FALSE        --
7085    ,p_validation_level          IN   NUMBER   := FND_API.G_VALID_LEVEL_FULL
7086    ,p_debug                     IN   VARCHAR2 := FND_API.G_FALSE        --
7087    ,p_output_dir                IN   VARCHAR2 := NULL
7088    ,p_debug_filename            IN   VARCHAR2 := NULL
7089    ,x_return_status             OUT  NOCOPY  VARCHAR2                   --
7090    ,x_msg_count                 OUT  NOCOPY  NUMBER                     --
7091    ,x_msg_data                  OUT  NOCOPY  VARCHAR2                   --
7092    ,p_rev_item_seq_id           IN   NUMBER
7093    ,p_status_type               IN   NUMBER                             -- say 10 for imp_failed
7094    ,p_api_caller                IN   VARCHAR2 := NULL                   -- 'CP'
7095   )
7096   IS
7097     l_api_name           CONSTANT VARCHAR2(30)  := 'Update_RevItem_Lifecycle';
7098     l_api_version        CONSTANT NUMBER := 1.0;
7099 
7100     l_return_status      VARCHAR2(1);
7101     l_msg_count          NUMBER;
7102     l_msg_data           VARCHAR2(2000);
7103 
7104     l_fnd_user_id        NUMBER := TO_NUMBER(FND_PROFILE.VALUE('USER_ID'));
7105     l_fnd_login_id       NUMBER := TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID'));
7106 
7107   BEGIN
7108     -- Standard Start of API savepoint
7109     --SAVEPOINT  Update_RevItem_Lifecycle;
7110     -- Standard call to check for call compatibility
7111     IF NOT FND_API.Compatible_API_Call ( l_api_version
7112                                         ,p_api_version
7113                                         ,l_api_name
7114                                         ,G_PKG_NAME )
7115     THEN
7116       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7117     END IF;
7118     -- Initialize message list if p_init_msg_list is set to TRUE.
7119     IF FND_API.to_Boolean( p_init_msg_list ) THEN
7120        FND_MSG_PUB.initialize;
7121     END IF;
7122 
7123 
7124     -- For Test/Debug
7125     Check_And_Open_Debug_Session(p_debug, p_output_dir, p_debug_filename) ;
7126     -- R12 Comment out
7127     -- IF FND_API.to_Boolean( p_debug ) THEN
7128     --     Open_Debug_Session(p_output_dir, p_debug_filename ) ;
7129     -- END IF;
7130 
7131     -- Write debug message if debug mode is on
7132     IF g_debug_flag THEN
7133        Write_Debug('ENG_CHANGE_LIFECYCLE_UTIL.Update_RevItem_Lifecycle log');
7134        Write_Debug('-----------------------------------------------------');
7135        Write_Debug('p_rev_item_seq_id   : ' || p_rev_item_seq_id );
7136        Write_Debug('p_status_type       : ' || p_status_type );
7137        Write_Debug('-----------------------------------------------------');
7138        Write_Debug('Initializing return status... ' );
7139     END IF;
7140 
7141     -- Initialize API return status to success
7142     x_return_status := FND_API.G_RET_STS_SUCCESS;
7143 
7144     -- FND_PROFILE package is not available for workflow (WF),
7145     -- therefore manually set WHO column values
7146     IF p_api_caller = 'WF' THEN
7147       l_fnd_user_id := G_ENG_WF_USER_ID;
7148       l_fnd_login_id := G_ENG_WF_LOGIN_ID;
7149     ELSIF p_api_caller = 'CP' THEN
7150       l_fnd_user_id := G_ENG_CP_USER_ID;
7151       l_fnd_login_id := G_ENG_CP_LOGIN_ID;
7152     END IF;
7153 
7154 
7155     -- Real code starts here -----------------------------------------------
7156     UPDATE eng_revised_items
7157       SET status_type = p_status_type,
7158           last_update_date = sysdate,
7159           last_updated_by = l_fnd_user_id,
7160           last_update_login = l_fnd_login_id
7161       WHERE revised_item_sequence_id = p_rev_item_seq_id;
7162 
7163 
7164     -- Standard ending code ------------------------------------------------
7165     IF FND_API.To_Boolean ( p_commit ) THEN
7166       COMMIT WORK;
7167     END IF;
7168 
7169     FND_MSG_PUB.Count_And_Get
7170     ( p_count        =>      x_msg_count,
7171       p_data         =>      x_msg_data );
7172 
7173     IF g_debug_flag THEN
7174       Write_Debug('Finish. End Of procedure: Update_RevItem_Lifecycle') ;
7175     END IF;
7176 
7177     IF FND_API.to_Boolean( p_debug ) THEN
7178       Close_Debug_Session;
7179     END IF;
7180 
7181   EXCEPTION
7182     WHEN FND_API.G_EXC_ERROR THEN
7183       --ROLLBACK TO Update_RevItem_Lifecycle;
7184       x_return_status := FND_API.G_RET_STS_ERROR;
7185       FND_MSG_PUB.Count_And_Get
7186       ( p_count        =>      x_msg_count
7187        ,p_data         =>      x_msg_data );
7188       IF g_debug_flag THEN
7189         Write_Debug('Rollback and Finish with expected error.') ;
7190       END IF;
7191       IF FND_API.to_Boolean( p_debug ) THEN
7192         Close_Debug_Session;
7193       END IF;
7194     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7195       --ROLLBACK TO Update_RevItem_Lifecycle;
7196       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7197       FND_MSG_PUB.Count_And_Get
7198       ( p_count        =>      x_msg_count
7199        ,p_data         =>      x_msg_data );
7200       IF g_debug_flag THEN
7201         Write_Debug('Rollback and Finish with unexpected error.') ;
7202       END IF;
7203       IF FND_API.to_Boolean( p_debug ) THEN
7204         Close_Debug_Session;
7205       END IF;
7206     WHEN OTHERS THEN
7207       --ROLLBACK TO Update_RevItem_Lifecycle;
7208       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7209       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )
7210       THEN
7211         FND_MSG_PUB.Add_Exc_Msg (  G_PKG_NAME, l_api_name );
7212       END IF;
7213       FND_MSG_PUB.Count_And_Get
7214       ( p_count        =>      x_msg_count
7215        ,p_data         =>      x_msg_data );
7216       IF g_debug_flag THEN
7217         Write_Debug('Rollback and Finish with other error.') ;
7218       END IF;
7219       IF FND_API.to_Boolean( p_debug ) THEN
7220         Close_Debug_Session;
7221       END IF;
7222 
7223   END Update_RevItem_Lifecycle;
7224 
7225 
7226 
7227   -- R12B Reset Phase
7228   -- Reset Phase
7229   -- R12B
7230   -- Called when Reset Workflow button pressed in Workflow UI
7231   -- to reset Dcoument Status
7232   -- If Change Object is Document LC Object, Call Document API to reset the phase
7233   PROCEDURE Reset_Phase
7234   (
7235     p_api_version               IN   NUMBER                             --
7236    ,p_init_msg_list             IN   VARCHAR2 := FND_API.G_FALSE        --
7237    ,p_commit                    IN   VARCHAR2 := FND_API.G_FALSE        --
7238    ,p_validation_level          IN   NUMBER   := FND_API.G_VALID_LEVEL_FULL
7239    ,p_debug                     IN   VARCHAR2 := FND_API.G_FALSE        --
7240    ,p_output_dir                IN   VARCHAR2 := NULL
7241    ,p_debug_filename            IN   VARCHAR2 := NULL
7242    ,x_return_status             OUT  NOCOPY  VARCHAR2                   --
7243    ,x_msg_count                 OUT  NOCOPY  NUMBER                     --
7244    ,x_msg_data                  OUT  NOCOPY  VARCHAR2                   --
7245    ,p_change_id                 IN   NUMBER                             -- header's change_id
7246    ,p_status_code               IN   NUMBER   := NULL                   -- reset phase/status code
7247    ,p_api_caller                IN   VARCHAR2 := NULL                   -- can also be 'WF'
7248   )
7249   IS
7250 
7251     l_api_name           CONSTANT VARCHAR2(30)  := 'Reset_Phase';
7252     l_api_version        CONSTANT NUMBER := 1.0;
7253 
7254     l_fnd_user_id        NUMBER := TO_NUMBER(FND_PROFILE.VALUE('USER_ID'));
7255     l_fnd_login_id       NUMBER := TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID'));
7256 
7257     l_return_status      VARCHAR2(1);
7258     l_msg_count          NUMBER;
7259     l_msg_data           VARCHAR2(2000);
7260 
7261     l_doc_lc_object_flag  BOOLEAN := FALSE ;
7262 
7263     l_cm_type_code       eng_engineering_changes.CHANGE_MGMT_TYPE_CODE%TYPE;
7264     l_base_cm_type_code  eng_change_order_types.BASE_CHANGE_MGMT_TYPE_CODE%TYPE;
7265 
7266     l_next_status_code   NUMBER;
7267     l_reset_status_code  NUMBER;
7268     l_last_status_code   NUMBER;
7269     l_max_phase_sn       eng_lifecycle_statuses.sequence_number%TYPE;
7270     l_curr_status_type   eng_engineering_changes.status_type%TYPE;
7271 
7272     -- l_last_imp_flag      VARCHAR2(1) := 'N';
7273 
7274 
7275     -- Query the required parameters for current status
7276     -- Get the current phase, promote_phase and cm type of the change header
7277     CURSOR  c_cur_status (c_change_id NUMBER )
7278     IS
7279         SELECT eec.status_code
7280              , eec.promote_status_code
7281              , eec.change_mgmt_type_code
7282              , ecot.base_change_mgmt_type_code
7283           FROM eng_engineering_changes eec,
7284                eng_change_order_types ecot
7285           WHERE eec.change_id = c_change_id
7286             AND ecot.change_order_type_id = eec.change_order_type_id ;
7287 
7288 
7289     recinfo c_cur_status%rowtype;
7290 
7291 
7292   BEGIN
7293 
7294     -- Standard Start of API savepoint
7295     SAVEPOINT Reset_Phase ;
7296     -- Standard call to check for call compatibility
7297     IF NOT FND_API.Compatible_API_Call ( l_api_version
7298                                         ,p_api_version
7299                                         ,l_api_name
7300                                         ,G_PKG_NAME )
7301     THEN
7302       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7303     END IF;
7304 
7305     -- Initialize message list if p_init_msg_list is set to TRUE.
7306     IF FND_API.to_Boolean( p_init_msg_list ) THEN
7307        FND_MSG_PUB.initialize;
7308     END IF;
7309 
7310 
7311     -- For Test/Debug
7312     Check_And_Open_Debug_Session(p_debug, p_output_dir, p_debug_filename) ;
7313 
7314 
7315     -- Write debug message if debug mode is on
7316     IF g_debug_flag THEN
7317        Write_Debug('ENG_CHANGE_LIFECYCLE_UTIL.Reset_Phase log');
7318        Write_Debug('-----------------------------------------------------');
7319        Write_Debug('p_change_id         : ' || to_char(p_change_id) );
7320        Write_Debug('p_status_code       : ' || to_char(p_status_code) );
7321        Write_Debug('p_api_caller        : ' || p_api_caller );
7322        Write_Debug('-----------------------------------------------------');
7323        Write_Debug('Initializing return status... ' );
7324     END IF;
7325 
7326     -- Initialize API return status to success
7327     x_return_status := FND_API.G_RET_STS_SUCCESS;
7328 
7329     -- FND_PROFILE package is not available for workflow (WF),
7330     -- therefore manually set WHO column values
7331     IF p_api_caller = 'WF' THEN
7332       l_fnd_user_id := G_ENG_WF_USER_ID;
7333       l_fnd_login_id := G_ENG_WF_LOGIN_ID;
7334     ELSIF p_api_caller = 'CP' THEN
7335       l_fnd_user_id := G_ENG_CP_USER_ID;
7336       l_fnd_login_id := G_ENG_CP_LOGIN_ID;
7337     END IF;
7338 
7339     -- Real code starts here -----------------------------------------------
7340 
7341 
7342 IF g_debug_flag THEN
7343     Write_Debug('Get the current status code to be reset ');
7344 END IF;
7345 
7346     OPEN c_cur_status(c_change_id => p_change_id) ;
7347     FETCH c_cur_status into recinfo ;
7348     IF (c_cur_status%notfound) THEN
7349        CLOSE c_cur_status;
7350        RAISE no_data_found;
7351     END IF;
7352 
7353     IF (c_cur_status%ISOPEN) THEN
7354        CLOSE c_cur_status ;
7355     END IF ;
7356 
7357     l_reset_status_code := recinfo.status_code ;
7358     l_next_status_code  := recinfo.promote_status_code ;
7359     l_cm_type_code      := recinfo.change_mgmt_type_code ;
7360     l_base_cm_type_code := recinfo.base_change_mgmt_type_code ;
7361 
7362     IF g_debug_flag THEN
7363         Write_Debug('Got current status code : ' || to_char(l_reset_status_code) );
7364     END IF;
7365 
7366     IF l_reset_status_code IS NULL THEN
7367         -- This should not happen
7368         FND_MESSAGE.SET_NAME('ENG', 'ENG_STATUS_CODE_NULL') ;
7369         FND_MSG_PUB.Add ;
7370         RAISE FND_API.G_EXC_ERROR ;
7371     END IF ;
7372 
7373     IF (p_status_code IS NOT NULL AND l_reset_status_code <> p_status_code )
7374     THEN
7375 
7376         -- This should not happen
7377         FND_MESSAGE.SET_NAME('ENG', 'ENG_RESET_STATUS_NOT_CUR') ;
7378         FND_MSG_PUB.Add ;
7379         RAISE FND_API.G_EXC_ERROR ;
7380 
7381     END IF ;
7382 
7383     -- Get the current phase status_type
7384     SELECT status_type
7385       INTO l_curr_status_type
7386       FROM eng_change_statuses
7387       WHERE status_code = l_reset_status_code;
7388 
7389     --
7390     -- Get the current phase's sequence number
7391     -- SELECT sequence_number
7392     --   INTO l_curr_phase_sn
7393     --   FROM eng_lifecycle_statuses
7394     --   WHERE entity_name = G_ENG_CHANGE
7395     --     AND entity_id1 = p_change_id
7396     --     AND status_code = l_reset_status_code
7397     --     AND active_flag = 'Y'
7398     --     AND rownum = 1;
7399     --
7400 
7401     -- Check if the current phase is of type APPROVAL,
7402     -- and if it is the last such phase in the lifecycle of the change header
7403     --
7404     IF (l_curr_status_type in (G_ENG_APPROVED ,G_ENG_REVIEWED))
7405     THEN
7406 
7407 IF g_debug_flag THEN
7408     Write_Debug('Current phase is of type APPROVAL...');
7409 END IF;
7410         -- if l_curr_status_type <> 12
7411          --THEN
7412           -- Update change header approval status
7413           -- Launch header approval status change workflow
7414           Update_Header_Appr_Status
7415           (
7416             p_api_version               =>  1.0
7417            ,p_init_msg_list             =>  FND_API.G_FALSE
7418            ,p_commit                    =>  FND_API.G_FALSE
7419            ,p_validation_level          =>  FND_API.G_VALID_LEVEL_FULL
7420            ,p_debug                     =>  FND_API.G_FALSE
7421            ,p_output_dir                =>  p_output_dir
7422            ,p_debug_filename            =>  p_debug_filename
7423            ,x_return_status             =>  l_return_status
7424            ,x_msg_count                 =>  l_msg_count
7425            ,x_msg_data                  =>  l_msg_data
7426            ,p_change_id                 =>  p_change_id
7427            ,p_status_code               =>  l_reset_status_code
7428            ,p_appr_status               =>  G_ENG_APPR_REQUESTED
7429            ,p_route_status              =>  Eng_Workflow_Util.G_RT_NOT_STARTED
7430            ,p_api_caller                =>  p_api_caller
7431            ,p_bypass                    =>  'N'
7432           );
7433       /* ELSE
7434 
7435 
7436         ENG_DOCUMENT_UTIL.Update_Approval_Status
7437             ( p_api_version         => 1.0
7438              ,p_init_msg_list       => FND_API.G_FALSE
7439              ,p_commit              => FND_API.G_FALSE
7440              ,p_validation_level    => p_validation_level
7441              ,p_debug               => FND_API.G_FALSE
7442              ,p_output_dir          => p_output_dir
7443              ,p_debug_filename      => p_debug_filename
7444              ,x_return_status       => l_return_status     --
7445              ,x_msg_count           => l_msg_count         --
7446              ,x_msg_data            => l_msg_data          --
7447              ,p_change_id           => p_change_id         -- header's change_id
7448              ,p_approval_status     => G_ENG_APPR_REQUESTED-- header approval status
7449              ,p_wf_route_status     => Eng_Workflow_Util.G_RT_NOT_STARTED      -- workflow routing status (for document types)
7450              ,p_api_caller          => p_api_caller        -- Optionnal for future use
7451             );
7452       END IF;*/
7453 
7454 
7455 IF g_debug_flag THEN
7456     Write_Debug('After: Update_Header_Appr_Status: ' || l_return_status );
7457 END IF;
7458 
7459 
7460           IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS )
7461           THEN
7462             x_return_status := l_return_status;
7463             x_msg_count := l_msg_count;
7464             x_msg_data := l_msg_data;
7465             RAISE FND_API.G_EXC_ERROR;
7466           END IF;
7467 
7468           IF g_debug_flag THEN
7469             Write_Debug('After: calling Update_Header_Appr_Status');
7470           END IF;
7471 
7472     END IF; -- current status type is  G_ENG_APPROVED
7473 
7474     -- R12B
7475     -- In case of Document LC Change Object, call ENG_DOCUMENT_UTIL.Change_Doc_LC_Phase
7476     -- to validate and sync Document LC Phase
7477     l_doc_lc_object_flag := ENG_DOCUMENT_UTIL.Is_Dom_Document_Lifecycle
7478                                ( p_change_id => p_change_id
7479                                , p_base_change_mgmt_type_code => l_base_cm_type_code
7480                                )  ;
7481 
7482     IF (l_doc_lc_object_flag)
7483     THEN
7484         IF l_reset_status_code IS NULL
7485         THEN
7486 
7487             IF g_debug_flag THEN
7488                  Write_Debug('param p_status_code is null, get the current status code to be reset ');
7489             END IF;
7490 
7491             OPEN c_cur_status(c_change_id => p_change_id) ;
7492             FETCH c_cur_status into recinfo;
7493             IF (c_cur_status%notfound) THEN
7494                 CLOSE c_cur_status;
7495                 RAISE no_data_found;
7496 
7497             END IF;
7498 
7499             IF (c_cur_status%ISOPEN) THEN
7500                 CLOSE c_cur_status ;
7501             END IF ;
7502 
7503             l_reset_status_code := recinfo.status_code ;
7504 
7505             IF g_debug_flag THEN
7506                  Write_Debug('Got current status code : ' || to_char(l_reset_status_code) );
7507             END IF;
7508 
7509             IF l_reset_status_code IS NULL THEN
7510                 -- This should not happen
7511                 FND_MESSAGE.SET_NAME('ENG', 'ENG_STATUS_CODE_NULL') ;
7512                 FND_MSG_PUB.Add ;
7513                 RAISE FND_API.G_EXC_ERROR ;
7514             END IF ;
7515 
7516         END IF ;
7517 
7518         IF g_debug_flag THEN
7519              Write_Debug('Before: calling ENG_DOCUMENT_UTIL.Change_Doc_LC_Phase');
7520              Write_Debug('  Status Code= ' || to_char(l_reset_status_code));
7521         END IF;
7522 
7523         ENG_DOCUMENT_UTIL.Change_Doc_LC_Phase
7524         ( p_api_version         => 1.0
7525          ,p_init_msg_list       => FND_API.G_FALSE
7526          ,p_commit              => FND_API.G_FALSE
7527          ,p_validation_level    => p_validation_level
7528          ,p_debug               => FND_API.G_FALSE
7529          ,p_output_dir          => p_output_dir
7530          ,p_debug_filename      => p_debug_filename
7531          ,x_return_status       => l_return_status
7532          ,x_msg_count           => l_msg_count
7533          ,x_msg_data            => l_msg_data
7534          ,p_change_id           => p_change_id
7535          ,p_lc_phase_code       => l_reset_status_code
7536          ,p_action_type         => G_ENG_PROMOTE
7537          ,p_api_caller          => p_api_caller
7538         ) ;
7539 
7540         IF g_debug_flag THEN
7541             Write_Debug('After: calling ENG_DOCUMENT_UTIL.Change_Doc_LC_Phase: ' || l_return_status) ;
7542         END IF;
7543 
7544         IF ( l_return_status <>  FND_API.G_RET_STS_SUCCESS )
7545         THEN
7546             x_return_status := l_return_status;
7547             x_msg_count := l_msg_count;
7548             x_msg_data := l_msg_data;
7549             RAISE FND_API.G_EXC_ERROR;
7550         END IF;
7551 
7552         IF g_debug_flag THEN
7553              Write_Debug('Successful: ENG_DOCUMENT_UTIL.Change_Doc_LC_Phase');
7554         END IF;
7555 
7556     END IF ; -- if ( Change Object is Documet LC Object)
7557 
7558 
7559     -- Standard ending code ------------------------------------------------
7560     IF FND_API.To_Boolean ( p_commit ) THEN
7561       COMMIT WORK;
7562     END IF;
7563 
7564     FND_MSG_PUB.Count_And_Get
7565     ( p_count        =>      x_msg_count,
7566       p_data         =>      x_msg_data );
7567 
7568     IF g_debug_flag THEN
7569       Write_Debug('Finish. End Of procedure: Promote_Header');
7570     END IF;
7571 
7572     IF FND_API.to_Boolean( p_debug ) THEN
7573       Close_Debug_Session;
7574     END IF;
7575 
7576   EXCEPTION
7577     WHEN FND_API.G_EXC_ERROR THEN
7578       -- Standard check of p_commit.
7579       IF FND_API.To_Boolean( p_commit ) THEN
7580 IF g_debug_flag THEN
7581    Write_Debug('Rollback . . .') ;
7582 END IF ;
7583           ROLLBACK TO Reset_Phase ;
7584       END IF;
7585 
7586       x_return_status := FND_API.G_RET_STS_ERROR;
7587 
7588       FND_MSG_PUB.Count_And_Get
7589       ( p_count        =>      x_msg_count
7590        ,p_data         =>      x_msg_data );
7591 
7592       IF g_debug_flag THEN
7593         Write_Debug('Finish with expected error.') ;
7594       END IF;
7595 
7596       IF FND_API.to_Boolean( p_debug ) THEN
7597         Close_Debug_Session;
7598       END IF;
7599 
7600     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7601 
7602       -- Standard check of p_commit.
7603       IF FND_API.To_Boolean( p_commit ) THEN
7604 IF g_debug_flag THEN
7605    Write_Debug('Rollback . . .') ;
7606 END IF ;
7607           ROLLBACK TO Reset_Phase ;
7608       END IF;
7609 
7610       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7611 
7612       FND_MSG_PUB.Count_And_Get
7613       ( p_count        =>      x_msg_count
7614        ,p_data         =>      x_msg_data );
7615 
7616       IF g_debug_flag THEN
7617         Write_Debug('Rollback and Finish with unexpected error.') ;
7618       END IF;
7619 
7620 
7621       IF FND_API.to_Boolean( p_debug ) THEN
7622         Close_Debug_Session;
7623       END IF;
7624 
7625 
7626     WHEN OTHERS THEN
7627 
7628       -- Standard check of p_commit.
7629       IF FND_API.To_Boolean( p_commit ) THEN
7630 IF g_debug_flag THEN
7631    Write_Debug('Rollback . . .') ;
7632 END IF ;
7633           ROLLBACK TO Reset_Phase ;
7634       END IF;
7635 
7636       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7637 
7638       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )
7639       THEN
7640         FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME, l_api_name );
7641       END IF;
7642 
7643       FND_MSG_PUB.Count_And_Get
7644       ( p_count        =>      x_msg_count
7645        ,p_data         =>      x_msg_data );
7646 
7647 
7648       IF g_debug_flag THEN
7649         Write_Debug('Finish with other error.') ;
7650       END IF;
7651 
7652       IF FND_API.to_Boolean( p_debug ) THEN
7653         Close_Debug_Session;
7654       END IF;
7655 
7656   END Reset_Phase ;
7657 
7658 
7659   --
7660   -- R12B Sync Workflow Statuses/Lifecycle Phases
7661   -- If a phase is added or removed in an existing lifecycle setup which is already being used by
7662   -- some change objects including document lc change objects  then the lifecycles of the change object
7663   --  would be affected as below:
7664   --
7665   --   1. Adding a Phase: For all Change Objects the lifecycle that is being updated,
7666   --      would get this newly added Phase, if they have not already gone past that phase.
7667   --      For Change Objects that are already past the phase, these will not be affected.
7668   --
7669   --   2. Removing a Phase: Similar to the addition, the removal of the phase would also happen only for those
7670   --      change objects that have not reached the Phase being removed.
7671   --      For Change Objects already past the Phase, will not be affected for history purposes.
7672   --      If a Change Object is in a Phase that is being removed, then it should not be affected.
7673   --
7674   --   3. Updating a Phase Property: Phase Property update will not be affected.
7675   --
7676   -- This API will support the above syncronization and be called when the user update the LC Phase Setup and
7677   -- confirm to go ahead with  reflecting the changes to existing lifecycles of the change object.
7678   --
7679   PROCEDURE Sync_LC_Phase_Setup
7680   (
7681     p_api_version               IN   NUMBER                             --
7682    ,p_init_msg_list             IN   VARCHAR2 := FND_API.G_FALSE        --
7683    ,p_commit                    IN   VARCHAR2 := FND_API.G_FALSE        --
7684    ,p_validation_level          IN   NUMBER   := FND_API.G_VALID_LEVEL_FULL
7685    ,p_debug                     IN   VARCHAR2 := FND_API.G_FALSE        --
7686    ,p_output_dir                IN   VARCHAR2 := NULL
7687    ,p_debug_filename            IN   VARCHAR2 := NULL
7688    ,x_return_status             OUT  NOCOPY  VARCHAR2                   --
7689    ,x_msg_count                 OUT  NOCOPY  NUMBER                     --
7690    ,x_msg_data                  OUT  NOCOPY  VARCHAR2                   --
7691    ,p_change_type_id            IN   NUMBER                             -- header's change_type_id
7692    ,p_api_caller                IN   VARCHAR2 := NULL                   -- Future Use
7693   )
7694   IS
7695 
7696     l_api_name           CONSTANT VARCHAR2(30)  := 'Sync_LC_Phase_Setup';
7697     l_api_version        CONSTANT NUMBER := 1.0;
7698 
7699     l_fnd_user_id        NUMBER := TO_NUMBER(FND_PROFILE.VALUE('USER_ID'));
7700     l_fnd_login_id       NUMBER := TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID'));
7701 
7702     l_return_status      VARCHAR2(1);
7703     l_msg_count          NUMBER;
7704     l_msg_data           VARCHAR2(2000);
7705 
7706     -- Cursor to Fetch all the life cycle Statuses for corresponding Type.
7707     CURSOR  c_lc_phase_setup (c_change_type_id NUMBER )
7708     IS
7709        SELECT CHANGE_LIFECYCLE_STATUS_ID
7710             , SEQUENCE_NUMBER
7711             , STATUS_CODE
7712             , AUTO_PROMOTE_STATUS
7713             , AUTO_DEMOTE_STATUS
7714             , CHANGE_EDITABLE_FLAG
7715             , CHANGE_WF_ROUTE_ID
7716             , ENTITY_ID1 CHANGE_TYPE_ID
7717       FROM eng_lifecycle_statuses
7718       WHERE entity_name = 'ENG_CHANGE_TYPE'
7719       AND entity_id1 = c_change_type_id
7720       ORDER BY SEQUENCE_NUMBER ASC ;
7721 
7722     l_return_status       VARCHAR2(1);
7723     l_err_text            VARCHAR2(2000) ;
7724     l_Mesg_Token_Tbl      Error_Handler.Mesg_Token_Tbl_Type ;
7725 
7726 
7727   BEGIN
7728 
7729     -- Standard Start of API savepoint
7730     SAVEPOINT Sync_LC_Phase_Setup ;
7731     -- Standard call to check for call compatibility
7732     IF NOT FND_API.Compatible_API_Call ( l_api_version
7733                                         ,p_api_version
7734                                         ,l_api_name
7735                                         ,G_PKG_NAME )
7736     THEN
7737       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7738     END IF;
7739 
7740     -- Initialize message list if p_init_msg_list is set to TRUE.
7741     IF FND_API.to_Boolean( p_init_msg_list ) THEN
7742        FND_MSG_PUB.initialize;
7743     END IF;
7744 
7745 
7746     -- For Test/Debug
7747     Check_And_Open_Debug_Session(p_debug, p_output_dir, p_debug_filename) ;
7748 
7749     -- Write debug message if debug mode is on
7750     IF g_debug_flag THEN
7751        Write_Debug('ENG_CHANGE_LIFECYCLE_UTIL.Sync_LC_Phase_Setup log');
7752        Write_Debug('-----------------------------------------------------');
7753        Write_Debug('p_change_type_id    : ' || to_char(p_change_type_id));
7754        Write_Debug('p_api_caller        : ' || p_api_caller );
7755        Write_Debug('-----------------------------------------------------');
7756        Write_Debug('Initializing return status... ' );
7757     END IF;
7758 
7759     -- Initialize API return status to success
7760     x_return_status := FND_API.G_RET_STS_SUCCESS;
7761 
7762     -- FND_PROFILE package is not available for workflow (WF),
7763     -- therefore manually set WHO column values
7764     IF p_api_caller = 'WF' THEN
7765       l_fnd_user_id := G_ENG_WF_USER_ID;
7766       l_fnd_login_id := G_ENG_WF_LOGIN_ID;
7767     ELSIF p_api_caller = 'CP' THEN
7768       l_fnd_user_id := G_ENG_CP_USER_ID;
7769       l_fnd_login_id := G_ENG_CP_LOGIN_ID;
7770     END IF;
7771 
7772     -- Real code starts here -----------------------------------------------
7773 
7774     -------------------------------------------------------------------------------------------
7775     --   1. Adding a Phase: For all Change Objects the lifecycle that is being updated,
7776     --      would get this newly added Phase, if they have not already gone past that phase.
7777     --      For Change Objects that are already past the phase, these will not be affected.
7778     -------------------------------------------------------------------------------------------
7779 
7780 
7781 IF g_debug_flag THEN
7782    Write_Debug(' Add LC Phase for submitted Change Objects . . .') ;
7783 END IF ;
7784 
7785         -- Insert the statuses data if it does not exists in change objects that have not gone past the phase
7786         -- and the phase does not exist in lifecycle of the change objects
7787         INSERT INTO ENG_LIFECYCLE_STATUSES
7788         (   CHANGE_LIFECYCLE_STATUS_ID
7789           , ENTITY_NAME
7790           , ENTITY_ID1
7791           , ENTITY_ID2
7792           , ENTITY_ID3
7793           , ENTITY_ID4
7794           , ENTITY_ID5
7795           , SEQUENCE_NUMBER
7796           , STATUS_CODE
7797           , START_DATE
7798           , COMPLETION_DATE
7799           , CHANGE_WF_ROUTE_ID
7800           , AUTO_PROMOTE_STATUS
7801           , AUTO_DEMOTE_STATUS
7802           , WORKFLOW_STATUS
7803           , CHANGE_EDITABLE_FLAG
7804           , CREATION_DATE
7805           , CREATED_BY
7806           , LAST_UPDATE_DATE
7807           , LAST_UPDATED_BY
7808           , LAST_UPDATE_LOGIN
7809           , ITERATION_NUMBER
7810           , ACTIVE_FLAG
7811           , CHANGE_WF_ROUTE_TEMPLATE_ID
7812         )
7813         SELECT
7814             ENG_LIFECYCLE_STATUSES_S.NEXTVAL
7815           , G_ENG_CHANGE
7816           , ch.CHANGE_ID
7817           , NULL -- ENTITY_ID2
7818           , NULL -- ENTITY_ID3
7819           , NULL -- ENTITY_ID4
7820           , NULL -- ENTITY_ID5
7821           , change_type.SEQUENCE_NUMBER
7822           , change_type.STATUS_CODE
7823           , NULL -- START_DATE
7824           , NULL -- COMPLETION_DATE
7825           , NULL -- CHANGE_WF_ROUTE_ID
7826           , change_type.AUTO_PROMOTE_STATUS
7827           , change_type.AUTO_DEMOTE_STATUS
7828           , NULL -- WORKFLOW_STATUS
7829           , change_type.CHANGE_EDITABLE_FLAG
7830           , SYSDATE
7831           , l_fnd_user_id
7832           , SYSDATE
7833           , l_fnd_user_id
7834           , l_fnd_login_id
7835           , cur_phase.ITERATION_NUMBER
7836           , 'S'
7837           , change_type.CHANGE_WF_ROUTE_ID
7838         FROM ENG_ENGINEERING_CHANGES ch
7839            , ENG_LIFECYCLE_STATUSES cur_phase
7840            , ENG_LIFECYCLE_STATUSES change_type
7841         WHERE change_type.entity_id1 = p_change_type_id
7842          AND change_type.entity_name = 'ENG_CHANGE_TYPE'
7843          AND ch.CHANGE_ORDER_TYPE_ID = change_type.entity_id1
7844          AND ch.STATUS_TYPE NOT IN (0, 5, 6, 11) -- exclude draft, cancel, implemented, completed
7845          AND ch.plm_or_erp_change = 'PLM'
7846          AND cur_phase.entity_name  = G_ENG_CHANGE
7847          AND cur_phase.entity_id1   = ch.CHANGE_ID
7848          AND cur_phase.active_flag  = 'Y'
7849          AND cur_phase.status_code  = ch.STATUS_CODE
7850          AND cur_phase.SEQUENCE_NUMBER < change_type.SEQUENCE_NUMBER
7851          AND NOT EXISTS ( SELECT 'exists'
7852                           FROM  ENG_LIFECYCLE_STATUSES change_lc_phase
7853                           WHERE change_lc_phase.entity_name  = G_ENG_CHANGE
7854                           AND   change_lc_phase.entity_id1   = ch.CHANGE_ID
7855                           AND   change_lc_phase.active_flag  = 'Y'
7856                           AND   change_lc_phase.status_code  = change_type.STATUS_CODE
7857                           AND   change_lc_phase.SEQUENCE_NUMBER = change_type.SEQUENCE_NUMBER
7858                         ) ;
7859 
7860 
7861 
7862 IF g_debug_flag THEN
7863    IF SQL%FOUND THEN
7864       Write_Debug(' Add LC Phase for Submitted Change Object:' || to_char(SQL%ROWCOUNT)) ;
7865    END IF ;
7866 END IF ;
7867 
7868 IF g_debug_flag THEN
7869    Write_Debug(' Add LC Phase for Draft Change Objects. . .') ;
7870 END IF ;
7871 
7872         -- Add LC Phase for Draft Change Objects
7873         INSERT INTO ENG_LIFECYCLE_STATUSES
7874         (   CHANGE_LIFECYCLE_STATUS_ID
7875           , ENTITY_NAME
7876           , ENTITY_ID1
7877           , ENTITY_ID2
7878           , ENTITY_ID3
7879           , ENTITY_ID4
7880           , ENTITY_ID5
7881           , SEQUENCE_NUMBER
7882           , STATUS_CODE
7883           , START_DATE
7884           , COMPLETION_DATE
7885           , CHANGE_WF_ROUTE_ID
7886           , AUTO_PROMOTE_STATUS
7887           , AUTO_DEMOTE_STATUS
7888           , WORKFLOW_STATUS
7889           , CHANGE_EDITABLE_FLAG
7890           , CREATION_DATE
7891           , CREATED_BY
7892           , LAST_UPDATE_DATE
7893           , LAST_UPDATED_BY
7894           , LAST_UPDATE_LOGIN
7895           , ITERATION_NUMBER
7896           , ACTIVE_FLAG
7897           , CHANGE_WF_ROUTE_TEMPLATE_ID
7898         )
7899         SELECT
7900             ENG_LIFECYCLE_STATUSES_S.NEXTVAL
7901           , G_ENG_CHANGE
7902           , ch.CHANGE_ID
7903           , NULL -- ENTITY_ID2
7904           , NULL -- ENTITY_ID3
7905           , NULL -- ENTITY_ID4
7906           , NULL -- ENTITY_ID5
7907           , change_type.SEQUENCE_NUMBER
7908           , change_type.STATUS_CODE
7909           , NULL -- START_DATE
7910           , NULL -- COMPLETION_DATE
7911           , NULL -- CHANGE_WF_ROUTE_ID
7912           , change_type.AUTO_PROMOTE_STATUS
7913           , change_type.AUTO_DEMOTE_STATUS
7914           , NULL -- WORKFLOW_STATUS
7915           , change_type.CHANGE_EDITABLE_FLAG
7916           , SYSDATE
7917           , l_fnd_user_id
7918           , SYSDATE
7919           , l_fnd_user_id
7920           , l_fnd_login_id
7921           , 0   -- ITERATION_NUMBER
7922           , 'S' -- ACTIVE_FLAG
7923           , change_type.CHANGE_WF_ROUTE_ID
7924         FROM ENG_ENGINEERING_CHANGES ch
7925            , ENG_LIFECYCLE_STATUSES change_type
7926         WHERE change_type.entity_id1 = p_change_type_id
7927          AND change_type.entity_name = 'ENG_CHANGE_TYPE'
7928          AND ch.CHANGE_ORDER_TYPE_ID = change_type.entity_id1
7929          AND ch.STATUS_CODE = 0 -- DRAFT
7930          AND NOT EXISTS ( SELECT 'exists'
7931                            FROM  ENG_LIFECYCLE_STATUSES change_lc_phase
7932                            WHERE change_lc_phase.entity_name  = G_ENG_CHANGE
7933                            AND   change_lc_phase.entity_id1   = ch.CHANGE_ID
7934                            AND   change_lc_phase.active_flag  = 'Y'
7935                            AND   change_lc_phase.status_code  = change_type.STATUS_CODE
7936                            AND   change_lc_phase.SEQUENCE_NUMBER = change_type.SEQUENCE_NUMBER
7937                          ) ;
7938 
7939 
7940 IF g_debug_flag THEN
7941    IF SQL%FOUND THEN
7942       Write_Debug(' Added LC Phase for Draft Change Objects:' || to_char(SQL%ROWCOUNT)) ;
7943    END IF ;
7944 END IF ;
7945 
7946 
7947 IF g_debug_flag THEN
7948    Write_Debug(' Add LC Phase Properties for reocrds for active_flag S. . .') ;
7949 END IF ;
7950 
7951         -- Add LC Phase Properties for reocrds for active_flag S
7952         -- Inserting the status properties
7953         INSERT INTO  ENG_STATUS_PROPERTIES
7954         (
7955             CHANGE_LIFECYCLE_STATUS_ID
7956           , STATUS_CODE
7957           , PROMOTION_STATUS_FLAG
7958           , CREATION_DATE
7959           , CREATED_BY
7960           , LAST_UPDATE_DATE
7961           , LAST_UPDATED_BY
7962           , LAST_UPDATE_LOGIN
7963         )
7964         SELECT
7965             lc_phase.CHANGE_LIFECYCLE_STATUS_ID
7966           , phase_prop_setup.STATUS_CODE
7967           , phase_prop_setup.PROMOTION_STATUS_FLAG
7968           , SYSDATE
7969           , l_fnd_user_id
7970           , SYSDATE
7971           , l_fnd_user_id
7972           , l_fnd_login_id
7973         FROM ENG_STATUS_PROPERTIES  phase_prop_setup
7974            , ENG_LIFECYCLE_STATUSES lc_phase_setup
7975            , ENG_LIFECYCLE_STATUSES lc_phase
7976            , ENG_ENGINEERING_CHANGES ch
7977         WHERE lc_phase_setup.entity_id1   = p_change_type_id
7978           AND lc_phase_setup.entity_name = 'ENG_CHANGE_TYPE'
7979           AND phase_prop_setup.CHANGE_LIFECYCLE_STATUS_ID = lc_phase_setup.CHANGE_LIFECYCLE_STATUS_ID
7980           AND ch.CHANGE_ORDER_TYPE_ID = lc_phase_setup.ENTITY_ID1
7981           AND ch.STATUS_TYPE NOT IN (5, 6, 11) -- exclude cancel, implemented, completed
7982           AND lc_phase.entity_name  = G_ENG_CHANGE
7983           AND lc_phase.entity_id1   = ch.CHANGE_ID
7984           AND lc_phase.SEQUENCE_NUMBER = lc_phase_setup.SEQUENCE_NUMBER
7985           AND lc_phase.STATUS_CODE = lc_phase_setup.STATUS_CODE
7986           AND lc_phase.ACTIVE_FLAG = 'S' ;
7987 
7988 
7989 IF g_debug_flag THEN
7990    IF SQL%FOUND THEN
7991       Write_Debug(' Added LC Phase Properties: ' || to_char(SQL%ROWCOUNT)) ;
7992    END IF ;
7993 END IF ;
7994 
7995 
7996 
7997 IF g_debug_flag THEN
7998    Write_Debug(' -------------------------------------------') ;
7999 END IF ;
8000 
8001     --
8002     --   2. Removing a Phase: Similar to the addition, the removal of the phase would also happen only for those
8003     --      change objects that have not reached the Phase being removed.
8004     --      For Change Objects already past the Phase, will not be affected for history purposes.
8005     --      If a Change Object is in a Phase that is being removed, then it should not be affected.
8006     --
8007     --   NOTE: Internally we hide the phase by changing active flag  to 'D'
8008     --
8009 
8010 IF g_debug_flag THEN
8011    Write_Debug(' Removing a Phase. . .') ;
8012 END IF ;
8013 
8014 
8015     UPDATE ENG_LIFECYCLE_STATUSES
8016     SET  ACTIVE_FLAG  = 'D'
8017     WHERE CHANGE_LIFECYCLE_STATUS_ID IN (
8018                                           SELECT change_phase.CHANGE_LIFECYCLE_STATUS_ID
8019                                           FROM ENG_ENGINEERING_CHANGES ch
8020                                              , ENG_LIFECYCLE_STATUSES cur_phase
8021                                              , ENG_LIFECYCLE_STATUSES change_phase
8022                                           WHERE ch.CHANGE_ORDER_TYPE_ID = p_change_type_id
8023                                           AND ch.STATUS_TYPE NOT IN (5, 6, 11) -- exclude cancel, implemented, completed
8024                                           AND cur_phase.entity_name  = G_ENG_CHANGE
8025                                           AND cur_phase.entity_id1   = ch.CHANGE_ID
8026                                           AND cur_phase.active_flag  = 'Y'
8027                                           AND cur_phase.status_code  = ch.STATUS_CODE
8028                                           AND cur_phase.SEQUENCE_NUMBER < change_phase.SEQUENCE_NUMBER
8029                                           AND change_phase.entity_name  = 'ENG_CHANGE'
8030                                           AND change_phase.entity_id1   = ch.CHANGE_ID
8031                                           AND change_phase.active_flag  = 'Y'
8032                                           AND NOT EXISTS ( SELECT 'exists'
8033                                                            FROM  eng_lifecycle_statuses lc_phase_setup
8034                                                            WHERE lc_phase_setup.entity_name  = 'ENG_CHANGE_TYPE'
8035                                                            AND   lc_phase_setup.entity_id1   = ch.CHANGE_ORDER_TYPE_ID
8036                                                            AND   lc_phase_setup.SEQUENCE_NUMBER = change_phase.SEQUENCE_NUMBER
8037                                                            AND   lc_phase_setup.STATUS_CODE = change_phase.STATUS_CODE
8038                                                           )
8039                                           UNION ALL
8040                                           SELECT change_phase.CHANGE_LIFECYCLE_STATUS_ID
8041                                           FROM   ENG_ENGINEERING_CHANGES ch
8042                                                , ENG_LIFECYCLE_STATUSES change_phase
8043                                           WHERE ch.CHANGE_ORDER_TYPE_ID = p_change_type_id
8044                                           AND ch.STATUS_CODE = 0 -- DRAFT
8045                                           AND ch.STATUS_TYPE NOT IN (5, 6, 11) -- exclude cancel, implemented, completed
8046                                           AND change_phase.entity_name  = G_ENG_CHANGE
8047                                           AND change_phase.entity_id1   = ch.CHANGE_ID
8048                                           AND change_phase.active_flag  = 'Y'
8049                                           AND NOT EXISTS ( SELECT 'exists'
8050                                                            FROM  eng_lifecycle_statuses lc_phase_setup
8051                                                            WHERE lc_phase_setup.entity_name  = 'ENG_CHANGE_TYPE'
8052                                                            AND   lc_phase_setup.entity_id1   = ch.CHANGE_ORDER_TYPE_ID
8053                                                            AND   lc_phase_setup.SEQUENCE_NUMBER = change_phase.SEQUENCE_NUMBER
8054                                                            AND   lc_phase_setup.STATUS_CODE = change_phase.STATUS_CODE
8055                                                          )
8056                                          ) ;
8057 
8058 
8059 
8060 IF g_debug_flag THEN
8061    IF SQL%FOUND THEN
8062       Write_Debug(' Removed Phases. . .' || to_char(SQL%ROWCOUNT)) ;
8063    END IF ;
8064 END IF ;
8065 
8066 
8067 IF g_debug_flag THEN
8068    Write_Debug(' Mass Update Change LC Phases marked as S to make them Activie. . .') ;
8069 END IF ;
8070 
8071     -- 3. Mass Update Change LC Phases marked as 'S' in the above process of syncronization of newly added phase.
8072     --  to make them Activie
8073     UPDATE ENG_LIFECYCLE_STATUSES lc_phase
8074     SET  lc_phase.ACTIVE_FLAG  = 'Y'
8075     WHERE lc_phase.CHANGE_LIFECYCLE_STATUS_ID IN (
8076                                           SELECT added_change_phase.CHANGE_LIFECYCLE_STATUS_ID
8077                                           FROM ENG_ENGINEERING_CHANGES ch
8078                                              , ENG_LIFECYCLE_STATUSES added_change_phase
8079                                           WHERE ch.CHANGE_ORDER_TYPE_ID = p_change_type_id
8080                                           AND ch.STATUS_TYPE NOT IN (5, 6, 11) -- exclude cancel, implemented, completed
8081                                           AND added_change_phase.entity_name  = G_ENG_CHANGE
8082                                           AND added_change_phase.entity_id1   = ch.CHANGE_ID
8083                                           AND added_change_phase.active_flag  = 'S'
8084                                          ) ;
8085 
8086 
8087 IF g_debug_flag THEN
8088    IF SQL%FOUND THEN
8089       Write_Debug(' Mass Update Change to make added phase active: ' || to_char(SQL%ROWCOUNT)) ;
8090    END IF ;
8091 END IF ;
8092 
8093 
8094 
8095     -- Standard ending code ------------------------------------------------
8096     IF FND_API.To_Boolean ( p_commit ) THEN
8097       COMMIT WORK;
8098     END IF;
8099 
8100     FND_MSG_PUB.Count_And_Get
8101     ( p_count        =>      x_msg_count,
8102       p_data         =>      x_msg_data );
8103 
8104     IF g_debug_flag THEN
8105       Write_Debug('Finish. End Of procedure: ' || l_api_name);
8106     END IF;
8107 
8108     IF FND_API.to_Boolean( p_debug ) THEN
8109       Close_Debug_Session;
8110     END IF;
8111 
8112   EXCEPTION
8113     WHEN FND_API.G_EXC_ERROR THEN
8114       -- Standard check of p_commit.
8115       IF FND_API.To_Boolean( p_commit ) THEN
8116 IF g_debug_flag THEN
8117    Write_Debug('Rollback . . .') ;
8118 END IF ;
8119           ROLLBACK TO Sync_LC_Phase_Setup ;
8120       END IF;
8121 
8122       x_return_status := FND_API.G_RET_STS_ERROR;
8123 
8124       FND_MSG_PUB.Count_And_Get
8125       ( p_count        =>      x_msg_count
8126        ,p_data         =>      x_msg_data );
8127 
8128       IF g_debug_flag THEN
8129         Write_Debug('Finish with expected error.') ;
8130       END IF;
8131 
8132       IF FND_API.to_Boolean( p_debug ) THEN
8133         Close_Debug_Session;
8134       END IF;
8135 
8136     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8137 
8138       -- Standard check of p_commit.
8139       IF FND_API.To_Boolean( p_commit ) THEN
8140 IF g_debug_flag THEN
8141    Write_Debug('Rollback . . .') ;
8142 END IF ;
8143           ROLLBACK TO Sync_LC_Phase_Setup ;
8144       END IF;
8145 
8146       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8147 
8148       FND_MSG_PUB.Count_And_Get
8149       ( p_count        =>      x_msg_count
8150        ,p_data         =>      x_msg_data );
8151 
8152       IF g_debug_flag THEN
8153         Write_Debug('Rollback and Finish with unexpected error.') ;
8154       END IF;
8155 
8156 
8157       IF FND_API.to_Boolean( p_debug ) THEN
8158         Close_Debug_Session;
8159       END IF;
8160 
8161 
8162     WHEN OTHERS THEN
8163 
8164       -- Standard check of p_commit.
8165       IF FND_API.To_Boolean( p_commit ) THEN
8166 IF g_debug_flag THEN
8167    Write_Debug('Rollback . . .') ;
8168 END IF ;
8169           ROLLBACK TO Sync_LC_Phase_Setup ;
8170       END IF;
8171 
8172       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8173 
8174       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )
8175       THEN
8176         FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME, l_api_name );
8177       END IF;
8178 
8179       FND_MSG_PUB.Count_And_Get
8180       ( p_count        =>      x_msg_count
8181        ,p_data         =>      x_msg_data );
8182 
8183 
8184       IF g_debug_flag THEN
8185         Write_Debug('Finish with other error.') ;
8186       END IF;
8187 
8188       IF FND_API.to_Boolean( p_debug ) THEN
8189         Close_Debug_Session;
8190       END IF;
8191 
8192   END Sync_LC_Phase_Setup ;
8193 
8194 
8195 END ENG_CHANGE_LIFECYCLE_UTIL;
8196