DBA Data[Home] [Help]

PACKAGE BODY: APPS.AMS_APPROVAL_PVT

Source


1 PACKAGE BODY Ams_Approval_Pvt AS
2 /* $Header: amsvappb.pls 120.7.12020000.2 2013/02/21 09:59:40 amitamku ship $ */
3 
4 G_PKG_NAME     CONSTANT VARCHAR2(30) := 'ams_approval_pvt';
5 G_ITEMTYPE     CONSTANT VARCHAR2(30) := 'AMSAPRV';
6 
7 /***************************  PRIVATE ROUTINES  *******************************/
8 /*============================================================================*/
9 -- Start of Comments
10 --
11 -- NAME
12 --   HANDLE_ERR
13 --
14 -- PURPOSE
15 --   This Procedure will Get all the Errors from the Message stack and
16 --
17 -- NOTES
18 --
19 -- End of Comments
20 /*============================================================================*/
21 
22 --------------------------------------------------------------------------
23 procedure set_org_ctx (p_org_id IN NUMBER) is
24 
25 begin
26 
27      if p_org_id is not NULL then
28        MO_GLOBAL.init('OZF');
29        MO_GLOBAL.set_policy_context('S', p_org_id);  -- R12 Enhancements
30      end if;
31 
32 end set_org_ctx;
33 
34 --------------------------------------------------------------------------
35 
36 function find_org_id (p_list_header_id IN NUMBER)
37 return number is
38 
39 l_org_id number;
40 
41 cursor get_offer_org_csr(p_id in number) is
42 select orig_org_id
43 from qp_list_headers_all_b
44 where list_header_id = p_id;
45 
46 begin
47 
48  open get_offer_org_csr(p_list_header_id);
49     fetch get_offer_org_csr into l_org_id;
50  close get_offer_org_csr;
51 
52  return l_org_id;
53 
54 end find_org_id;
55 --------------------------------------------------------------------------
56 
57 PROCEDURE Handle_Err
58   (p_itemtype                IN VARCHAR2    ,
59   p_itemkey                  IN VARCHAR2    ,
60   p_msg_count                IN NUMBER      , -- Number of error Messages
61   p_msg_data                 IN VARCHAR2    ,
62   p_attr_name                IN VARCHAR2,
63   x_error_msg                OUT NOCOPY VARCHAR2
64   )
65 IS
66   l_msg_count            NUMBER ;
67   l_msg_data             VARCHAR2(2000);
68   l_final_data           VARCHAR2(4000);
69   l_msg_index            NUMBER ;
70   l_cnt                  NUMBER := 0 ;
71 
72 BEGIN
73   -- Retriveing Error Message from FND_MSG_PUB
74   -- Called by most of the procedures if it encounter error
75   WHILE l_cnt < p_msg_count
76   LOOP
77     Fnd_Msg_Pub.Get
78       (p_msg_index       => l_cnt + 1,
79       p_encoded         => Fnd_Api.G_FALSE,
80       p_data            => l_msg_data,
81       p_msg_index_out   => l_msg_index );
82     l_final_data := l_final_data ||l_msg_index||': '
83       ||l_msg_data||Fnd_Global.local_chr(10) ;
84     l_cnt := l_cnt + 1 ;
85   END LOOP ;
86   x_error_msg   := l_final_data;
87   Wf_Engine.SetItemAttrText
88     (itemtype  => p_itemtype,
89     itemkey    => p_itemkey ,
90     aname      => p_attr_name,
91     avalue     => l_final_data   );
92 END Handle_Err;
93 -------------------------------------------------------------------------------------
94 FUNCTION Is_Min_Sequence
95    (p_approval_detail_id    IN NUMBER,
96     p_sequence              IN NUMBER)
97  RETURN BOOLEAN IS
98     CURSOR c_min_seq IS
99     SELECT min(approver_seq)
100     FROM ams_approvers
101     WHERE ams_approval_detail_id  = p_approval_detail_id
102     AND active_flag = 'Y'
103     AND TRUNC(sysdate) between TRUNC(nvl(start_date_active,sysdate -1 ))
104     AND TRUNC(nvl(end_date_active,sysdate + 1));
105 
106     l_min_seq NUMBER;
107 BEGIN
108     OPEN c_min_seq;
109     FETCH c_min_seq INTO l_min_seq;
110     IF c_min_seq%NOTFOUND THEN
111        CLOSE c_min_seq;
112        return false;
113     END IF;
114     CLOSE c_min_seq;
115     IF l_min_seq = p_sequence THEN
116        RETURN true;
117     ELSE
118        RETURN false;
119     END IF;
120 END Is_Min_Sequence;
121 /*============================================================================*/
122 -- Procedure to Update status of the activity
123 -- Called by different procedures such as CAMP, EVEH,EVEO
124 /*============================================================================*/
125 PROCEDURE Update_Status(p_activity_type          IN  VARCHAR2,
126             p_activity_id            IN  NUMBER,
127             p_next_stat_id           IN  NUMBER,
128             x_return_status          OUT NOCOPY VARCHAR2)
129 
130 IS
131   l_msg_count  NUMBER;
132   l_msg_data  VARCHAR2(4000);
133   l_return_stat VARCHAR2(1);
134   p_next_stat_code     VARCHAR2(240);
135 
136   CURSOR c_sys_stat(l_user_stat_id NUMBER) IS
137   SELECT system_status_code
138   FROM ams_user_statuses_vl
139   WHERE user_status_id = l_user_stat_id ;
140 
141 BEGIN
142   x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
143   OPEN c_sys_stat(p_next_stat_id) ;
144   FETCH c_sys_stat INTO p_next_stat_code ;
145   CLOSE c_sys_stat ;
146   /*AMS_Utility_PVT.create_log(x_return_status => l_return_stat,
147           p_arc_log_used_by  => 'AMS Markeing Approval',
148           p_log_used_by_id  => 0,
149           p_msg_data => 'Update_Status: p_activity_type' || p_activity_type ||
150                         ' p_activity_id' || p_activity_id || 'p_next_stat_id' || p_next_stat_id);
151   */
152   IF p_activity_type = 'CAMP' THEN
153   /* commented for 11.5.6 using API provided by sonali
154     UPDATE AMS_CAMPAIGNS_ALL_B
155     SET user_status_id = p_next_stat_id,
156     status_code = p_next_stat_code,
157     status_date = sysdate,
158     object_version_number = object_version_number + 1
159     WHERE campaign_id = p_activity_id  ;
160     -- Following code is commented by ptendulk on 09-Oct-2001
161     -- as the wrapper api will take care of it.
162     -- DeActivate all other campaigns by same name if the campaign goes active
163     IF p_next_stat_code = 'ACTIVE' THEN
164       AMS_CampaignRules_PVT.activate_campaign(p_campaign_id  => p_activity_id);
165     END IF ;
166     */
167     Ams_Campaignrules_Pvt.update_status(p_campaign_id => p_activity_id,
168                       p_new_status_id => p_next_stat_id,
169                       p_new_status_code => p_next_stat_code);
170     /* added my murali code given by ravi start*/
171   ELSIF p_activity_type='CSCH' THEN
172     /* commented for 11.5.6 oct 08 using API provided by sonali
173     UPDATE AMS_CAMPAIGN_SCHEDULES_B
174     SET user_status_id = p_next_stat_id,
175     status_code = p_next_stat_code,
176     status_date = sysdate,
177     object_version_number = object_version_number + 1
178     WHERE campaign_id = p_activity_id  ;
179     */
180     Ams_Schedulerules_Pvt.update_status(p_schedule_id => p_activity_id,
181                       p_new_status_id => p_next_stat_id,
182                       p_new_status_code => p_next_stat_code);
183   ELSIF p_activity_type='OFFR' THEN
184   -- Migrate AMS to OZF for Offers
185     Ozf_Offer_Pvt.activate_offer(
186       x_return_status  => x_return_status,
187       x_msg_count => l_msg_count,
188       x_msg_data  => l_msg_data,
189       p_qp_list_header_id  => p_activity_id,
190       p_new_status_id   => p_next_stat_id
191     );
192     /*sathis api commented on aug16-2001 for HP recut 10a
193      commented to call satish api aug07-2001 start */
194     /*commented for 11.5.6 oct 08
195     UPDATE AMS_OFFERS
196     SET user_status_id = p_next_stat_id,
197     status_code = p_next_stat_code,
198     status_date = sysdate,
199     object_version_number = object_version_number + 1
200     WHERE qp_list_header_id = p_activity_id  ;
201     */
202     -- commented to call satish api aug07-2001 end
203     /* added my murali code given by ravi end*/
204   ELSIF p_activity_type = 'DELV' THEN
205   -- commented direct status update call for deliverable
206   -- in favor of an API call VMODUR Aug-01-2002
207      AMS_DeliverableRules_PVT.update_status(
208           p_deliverable_id     => p_activity_id
209          ,p_new_status_id      => p_next_stat_id
210          ,p_new_status_code    => p_next_stat_code
211       );
212   /*
213     UPDATE AMS_DELIVERABLES_ALL_B
214     SET user_status_id = p_next_stat_id,
215     status_code = p_next_stat_code,
216     status_date = SYSDATE,
217     object_version_number = object_version_number + 1
218     WHERE deliverable_id = p_activity_id ;
219   */
220   ELSIF p_activity_type  = 'EVEH' THEN
221     /*UPDATE AMS_EVENT_HEADERS_ALL_B */
222       Ams_Evhrules_Pvt.Update_Event_Header_Status(p_event_header_id => p_activity_id,
223                       p_new_status_id => p_next_stat_id,
224                       p_new_status_code => p_next_stat_code);
225 
226    ELSIF p_activity_type = 'EVEO' OR p_activity_type ='EONE' THEN
227       /*UPDATE AMS_EVENT_OFFERS_ALL_B*/
228      Ams_Evhrules_Pvt.Update_Event_Schedule_Status(p_event_offer_id => p_activity_id,
229                       p_new_status_id => p_next_stat_id,
230                       p_new_status_code => p_next_stat_code);
231   END IF;
232   /*AMS_Utility_PVT.create_log(x_return_status => l_return_stat,
233     p_arc_log_used_by  => 'AMS Markeing Approval',
234     p_log_used_by_id  => 0,
235     p_msg_data => 'Update_Status: Exiting sucessfully'); */
236 
237 
238 EXCEPTION
239   WHEN OTHERS THEN
240     Wf_Core.context ('ams_approval_pvt', 'Update_Status',p_activity_type
241                        ,p_activity_id ,'Unexpected error in Update_Status');
242         RAISE;
243 
244 END Update_Status;
245 
246 /*============================================================================*/
247 -- Attach Notes to Activity
248 -- Update Object Attributes table  if notes are added
249 -- Note Added will be of Type Approval
250 /*============================================================================*/
251 PROCEDURE Update_Note(p_activity_type IN   VARCHAR2,
252           p_activity_id   IN   NUMBER,
253           p_note          IN   VARCHAR2,
254           p_user          IN   NUMBER,
255           x_msg_count     OUT NOCOPY  NUMBER,
256           x_msg_data      OUT NOCOPY  VARCHAR2,
257           x_return_status OUT NOCOPY  VARCHAR2)
258 IS
259   l_id  NUMBER ;
260   l_user  NUMBER;
261   l_resource_name          VARCHAR2(360);
262   l_msg_count              NUMBER;
263   l_msg_data               VARCHAR2(4000);
264   l_error_msg              VARCHAR2(4000);
265 
266   CURSOR c_resource IS
267   SELECT user_id user_id, resource_name
268   FROM ams_jtf_rs_emp_v
269   WHERE resource_id = p_user ;
270 
271 BEGIN
272   x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
273   OPEN c_resource ;
274   FETCH c_resource INTO l_user, l_resource_name ;
275   IF c_resource%NOTFOUND THEN
276     CLOSE c_resource ;
277     Fnd_Message.Set_Name('AMS','AMS_API_DEBUG_MESSAGE');
278     Fnd_Message.Set_Token('ROW', SQLERRM );
279     Fnd_Msg_Pub.ADD;
280     x_return_status := Fnd_Api.G_RET_STS_ERROR;
281     RETURN;
282   END IF;
283   CLOSE c_resource ;
284 
285  -- Handle error when no user is associated with the approver's resource id.
286   IF l_user IS NULL THEN
287     Fnd_Message.Set_Name('AMS','AMS_RESOURCE_HAS_NO_USER');
288     Fnd_Message.Set_Token('RESOURCE', l_resource_name );
289     Fnd_Msg_Pub.ADD;
290     x_return_status := Fnd_Api.G_RET_STS_ERROR;
291     RETURN;
292   END IF;
293 
294   -- Note API to Update Approval Notes
295   /*AMS_ObjectAttribute_PVT.modify_object_attribute(
296     p_api_version        => 1.0,
297     p_init_msg_list      => FND_API.g_false,
298     p_commit             => FND_API.g_false,
299     p_validation_level   => FND_API.g_valid_level_full,
300     x_return_status      => x_return_status,
301     x_msg_count          => x_msg_count,
302     x_msg_data           => x_msg_data,
303     p_object_type        => p_activity_type,
304     p_object_id          => p_activity_id ,
305     p_attr               => 'NOTE',
306     p_attr_defined_flag  => 'Y'
307     );
308   IF x_return_status  <> FND_API.G_RET_STS_SUCCESS THEN
309     FND_MESSAGE.Set_Name('AMS','AMS_API_DEBUG_MESSAGE');
310     FND_MESSAGE.Set_Token('ROW', SQLERRM );
311     FND_MSG_PUB.Add;
312     x_return_status := FND_API.G_RET_STS_ERROR;
313     return;
314   END IF;
315   */
316 
317   Jtf_Notes_Pub.Create_note
318   ( p_api_version      =>  1.0 ,
319   x_return_status      =>  x_return_status,
320   x_msg_count          =>  x_msg_count,
321   x_msg_data           =>  x_msg_data,
322   p_source_object_id   =>  p_activity_id,
323   p_source_object_code =>  'AMS_'||p_activity_type,
324   p_notes              =>  p_note,
325   p_note_status        =>  NULL ,
326   p_entered_by         =>   l_user , -- 1000050 ,  -- FND_GLOBAL.USER_ID,
327   p_entered_date       =>  SYSDATE,
328   p_last_updated_by    =>   l_user , -- 1000050 ,  -- FND_GLOBAL.USER_ID,
329   x_jtf_note_id        =>  l_id ,
330   p_note_type          =>  'AMS_APPROVAL'    ,
331   p_last_update_date   =>  SYSDATE  ,
332   p_creation_date      =>  SYSDATE  ) ;
333   IF x_return_status  <> Fnd_Api.G_RET_STS_SUCCESS THEN
334     Fnd_Message.Set_Name('AMS','AMS_API_DEBUG_MESSAGE');
335     Fnd_Message.Set_Token('ROW', SQLERRM );
336     Fnd_Msg_Pub.ADD;
337     x_return_status := Fnd_Api.G_RET_STS_ERROR;
338     RETURN;
339   END IF;
340   /*AMS_Utility_PVT.create_log(x_return_status => l_return_stat,
341     p_arc_log_used_by  => 'AMS Markeing Approval',
342     p_log_used_by_id  => 0,
343     p_msg_data => 'Update_Note: Exiting sucessfully'); */
344 
345 EXCEPTION
346   WHEN OTHERS THEN
347     -- wf_engine.threshold := l_save_threshold ;
348     Wf_Core.context ('ams_approval_pvt', 'Update_Note',p_activity_type
349             ,p_activity_id , 'Unexpected Error in Update_Note' );
350     RAISE;
351 
352 END Update_Note;
353 
354 /*============================================================================*/
355 -- Attach Notes to Activity
356 -- Update Object Attributes table  if notes are added
357 -- Note Added will be of Type Approval
358 /*============================================================================*/
359 PROCEDURE Update_Justification_Note(p_activity_type IN   VARCHAR2,
360           p_activity_id   IN   NUMBER,
361           p_note          IN   VARCHAR2,
362           p_user          IN   NUMBER,
363           x_msg_count     OUT NOCOPY  NUMBER,
364           x_msg_data      OUT NOCOPY  VARCHAR2,
365           x_return_status OUT NOCOPY  VARCHAR2)
366 IS
367   l_id  NUMBER ;
368   l_user  NUMBER;
369   l_msg_count              NUMBER;
370   l_msg_data               VARCHAR2(4000);
371   l_error_msg              VARCHAR2(4000);
372 
373   CURSOR c_resource IS
374   SELECT user_id user_id
375   FROM ams_jtf_rs_emp_v
376   WHERE resource_id = p_user ;
377 
378 BEGIN
379   x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
380   OPEN c_resource ;
381   FETCH c_resource INTO l_user ;
382   IF c_resource%NOTFOUND THEN
383     CLOSE c_resource ;
384     Fnd_Message.Set_Name('AMS','AMS_API_DEBUG_MESSAGE');
385     Fnd_Message.Set_Token('ROW', SQLERRM );
386     Fnd_Msg_Pub.ADD;
387     x_return_status := Fnd_Api.G_RET_STS_ERROR;
388     RETURN;
389   END IF;
390   CLOSE c_resource ;
391   -- Note API to Update Approval Notes
392   Ams_Objectattribute_Pvt.modify_object_attribute(
393     p_api_version        => 1.0,
394     p_init_msg_list      => Fnd_Api.g_false,
395     p_commit             => Fnd_Api.g_false,
396     p_validation_level   => Fnd_Api.g_valid_level_full,
397     x_return_status      => x_return_status,
398     x_msg_count          => x_msg_count,
399     x_msg_data           => x_msg_data,
400     p_object_type        => p_activity_type,
401     p_object_id          => p_activity_id ,
402     p_attr               => 'NOTE',
403     p_attr_defined_flag  => 'Y'
404     );
405   IF x_return_status  <> Fnd_Api.G_RET_STS_SUCCESS THEN
406     Fnd_Message.Set_Name('AMS','AMS_API_DEBUG_MESSAGE');
407     Fnd_Message.Set_Token('ROW', SQLERRM );
408     Fnd_Msg_Pub.ADD;
409     x_return_status := Fnd_Api.G_RET_STS_ERROR;
410     RETURN;
411   END IF;
412 
413   Jtf_Notes_Pub.Create_note
414   ( p_api_version      =>  1.0 ,
415   x_return_status      =>  x_return_status,
416   x_msg_count          =>  x_msg_count,
417   x_msg_data           =>  x_msg_data,
418   p_source_object_id   =>  p_activity_id,
419   p_source_object_code =>  'AMS_'||p_activity_type,
420   p_notes              =>  p_note,
421   p_note_status        =>  NULL ,
422   p_entered_by         =>   l_user , -- 1000050 ,  -- FND_GLOBAL.USER_ID,
423   p_entered_date       =>  SYSDATE,
424   p_last_updated_by    =>   l_user , -- 1000050 ,  -- FND_GLOBAL.USER_ID,
425   x_jtf_note_id        =>  l_id ,
426   p_note_type          =>  'AMS_FREQ'    ,
427   p_last_update_date   =>  SYSDATE  ,
428   p_creation_date      =>  SYSDATE  ) ;
429   IF x_return_status  <> Fnd_Api.G_RET_STS_SUCCESS THEN
430     Fnd_Message.Set_Name('AMS','AMS_API_DEBUG_MESSAGE');
431     Fnd_Message.Set_Token('ROW', SQLERRM );
432     Fnd_Msg_Pub.ADD;
433     x_return_status := Fnd_Api.G_RET_STS_ERROR;
434     RETURN;
435   END IF;
436   /*AMS_Utility_PVT.create_log(x_return_status => l_return_stat,
437     p_arc_log_used_by  => 'AMS Markeing Approval',
438     p_log_used_by_id  => 0,
439     p_msg_data => 'Update_Note: Exiting sucessfully'); */
440 
441 EXCEPTION
442   WHEN OTHERS THEN
443     -- wf_engine.threshold := l_save_threshold ;
444     Wf_Core.context ('ams_approval_pvt', 'Update_Note',p_activity_type
445             ,p_activity_id , 'Unexpected Error in Update_Note' );
446     RAISE;
447 
448 END Update_Justification_Note;
449 
450 
451 /*============================================================================*/
452 -- Given the resource Name it returns the full name of the user
453 -- Ams_jtf_rs_emp_v is a view created for AMS based on
454 --     fnd_user and jtf_resource_extns
455 /*============================================================================*/
456 PROCEDURE Get_User_Name
457   ( p_user_id            IN     NUMBER,
458   x_full_name          OUT NOCOPY    VARCHAR2,
459   x_return_status      OUT NOCOPY    VARCHAR2)
460 IS
461   l_msg_count              NUMBER;
462   l_msg_data               VARCHAR2(4000);
463   l_error_msg              VARCHAR2(4000);
464 
465   CURSOR c_resource IS
466   SELECT  full_name
467   FROM ams_jtf_rs_emp_v
468   WHERE resource_id = p_user_id ;
469 BEGIN
470   /*AMS_Utility_PVT.create_log(x_return_status => l_return_stat,
471     p_arc_log_used_by  => 'AMS Markeing Approval',
472     p_log_used_by_id  => 0,
473     p_msg_data => 'Get_User_Name: p_user_id' || p_user_id); */
474 
475   x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
476   OPEN c_resource ;
477   FETCH c_resource INTO x_full_name ;
478   IF c_resource%NOTFOUND THEN
479     CLOSE c_resource ;
480     x_return_status := Fnd_Api.G_RET_STS_ERROR;
481     Fnd_Message.Set_Name('AMS','AMS_APPR_INVALID_RESOURCE_ID');
482     Fnd_Msg_Pub.ADD;
483     RETURN;
484   END IF;
485   CLOSE c_resource ;
486 EXCEPTION
487   WHEN OTHERS THEN
488     -- wf_engine.threshold := l_save_threshold ;
489     Wf_Core.context ('Ams_Approval_Pvt', 'Get_User_Name',p_user_id,
490             'Unexpected Error IN Get_User_Name');
491     RAISE;
492 
493 END Get_User_Name;
494 -------------------------------------------------------------------
495 -- Get the forward/reassigned resource details
496 -------------------------------------------------------------------
497 PROCEDURE Get_New_Res_Details(p_responder IN VARCHAR2,
498                               x_resource_id OUT NOCOPY NUMBER,
499                               x_resource_disp_name OUT NOCOPY VARCHAR2,
500                               x_return_status OUT NOCOPY VARCHAR2)
501 
502 IS
503 l_return_status VARCHAR2(1) := Fnd_Api.G_RET_STS_SUCCESS;
504 l_email VARCHAR2(360);
505 l_notif_pref VARCHAR2(30);
506 l_language VARCHAR2(30);
507 l_territory VARCHAR2(30);
508 
509 CURSOR c_res_details(p_user_name IN VARCHAR2) IS
510 SELECT r.resource_id
511 FROM fnd_user f, ams_jtf_rs_emp_v r
512 WHERE r.user_id = f.user_id
513 AND f.user_name = p_user_name;
514 
515 BEGIN
516 
517   wf_directory.getroleinfo(role                    => p_responder,
518                            display_name            => x_resource_disp_name,
519                            email_address           => l_email,
520                            notification_preference => l_notif_pref,
521                            language                => l_language,
522                            territory               => l_territory);
523 
524 
525   OPEN c_res_details(p_responder);
526   FETCH c_res_details INTO x_resource_id;
527   IF c_res_details%NOTFOUND THEN
528      l_return_status := Fnd_Api.G_RET_STS_ERROR;
529   END IF;
530   CLOSE c_res_details;
531 
532 x_return_status := l_return_status;
533 END Get_New_Res_Details;
534 ------------------------------------------------------------------------------
535 PROCEDURE Check_Reassigned (itemtype IN VARCHAR2,
536                             itemkey  IN VARCHAR2,
537                             x_approver_id OUT NOCOPY NUMBER,
538                             x_return_status OUT NOCOPY VARCHAR2)
539 IS
540 l_forward_nid NUMBER;
541 l_responder   VARCHAR2(320);
542 l_approver_id NUMBER;
543 l_appr_display_name VARCHAR2(360);
544 l_return_status VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
545 
546 BEGIN
547 
548      l_forward_nid        := Wf_Engine.GetItemAttrNumber(
549                                  itemtype => itemtype,
550                                  itemkey => itemkey,
551                                  aname   => 'AMS_FORWARD_NID');
552 
553      IF l_forward_nid IS NOT NULL THEN
554 
555        l_responder := wf_notification.responder(l_forward_nid);
556 
557        Get_New_Res_Details(p_responder => l_responder,
558                            x_resource_id => l_approver_id,
559                            x_resource_disp_name => l_appr_display_name,
560                            x_return_status => l_return_status);
561 
562        IF l_return_status = Fnd_Api.G_RET_STS_SUCCESS THEN
563 
564          -- Set the WF Attributes
565          wf_engine.SetItemAttrText(  itemtype => itemtype,
566                               itemkey  => itemkey,
567                               aname    => 'AMS_APPR_USERNAME',
568                               avalue   => l_responder);
569 
570 
571         wf_engine.SetItemAttrNumber(itemtype => itemtype,
572                               itemkey  => itemkey,
573                               aname    => 'AMS_APPROVER_ID',
574                               avalue   => l_approver_id);
575 
576 
577         wf_engine.SetItemAttrText(  itemtype => itemtype,
578                               itemkey  => itemkey,
579                               aname    => 'AMS_APPR_DISPLAY_NAME',
580                               avalue   => l_appr_display_name);
581 
582 
583       END IF;
584         -- Reset the forward_nid wf attribute to null
585         -- This is a must
586 
587         wf_engine.SetItemAttrNumber(itemtype => itemtype,
588                               itemkey  => itemkey,
589                               aname    => 'AMS_FORWARD_NID',
590                               avalue   => null);
591 
592      x_approver_id   := l_approver_id;
593      x_return_status := l_return_status;
594      END IF;
595 END Check_Reassigned;
596 /*============================================================================*/
597 -- Start of Comments
598 -- NAME
599 --   Get_User_Role
600 --
601 -- PURPOSE
602 --   This Procedure will be return the User role for
603 --   the userid sent
604 -- Called By
605 -- NOTES
606 -- End of Comments
607 
608 /*============================================================================*/
609 
610 PROCEDURE Get_User_Role
611   ( p_user_id            IN     NUMBER,
612   x_role_name          OUT NOCOPY    VARCHAR2,
613   x_role_display_name  OUT NOCOPY    VARCHAR2 ,
614   x_return_status      OUT NOCOPY    VARCHAR2)
615 IS
616   l_msg_count              NUMBER;
617   l_msg_data               VARCHAR2(4000);
618   l_error_msg              VARCHAR2(4000);
619 
620 
621   CURSOR c_resource IS
622   SELECT employee_id , user_id, category
623   FROM ams_jtf_rs_emp_v
624   WHERE resource_id = p_user_id ;
625 
626   l_person_id NUMBER;
627   l_user_id NUMBER;
628   l_category  VARCHAR2(30);
629 BEGIN
630   x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
631   OPEN c_resource ;
632   FETCH c_resource INTO l_person_id , l_user_id, l_category;
633   IF c_resource%NOTFOUND THEN
634     CLOSE c_resource ;
635     x_return_status := Fnd_Api.G_RET_STS_ERROR;
636     Fnd_Message.Set_Name('AMS','AMS_APPR_INVALID_RESOURCE_ID');
637     Fnd_Msg_Pub.ADD;
638     RETURN;
639   END IF;
640   CLOSE c_resource ;
641       -- Pass the Employee ID to get the Role
642   IF l_category = 'PARTY' THEN
643     Wf_Directory.getrolename
644     ( p_orig_system     => 'FND_USR',
645     p_orig_system_id    => l_user_id ,
646     p_name              => x_role_name,
647     p_display_name      => x_role_display_name );
648     IF x_role_name IS NULL  THEN
649       x_return_status := Fnd_Api.G_RET_STS_ERROR;
650       Fnd_Message.Set_Name('AMS','AMS_APPR_INVALID_ROLE');
651       Fnd_Msg_Pub.ADD;
652       RETURN;
653     END IF;
654   ELSE
655     Wf_Directory.getrolename
656     ( p_orig_system     => 'PER',
657     p_orig_system_id    => l_person_id ,
658     p_name              => x_role_name,
659     p_display_name      => x_role_display_name );
660     IF x_role_name IS NULL  THEN
661       x_return_status := Fnd_Api.G_RET_STS_ERROR;
662       Fnd_Message.Set_Name('AMS','AMS_APPR_INVALID_ROLE');
663       Fnd_Msg_Pub.ADD;
664       RETURN;
665     END IF;
666   END IF;
667 EXCEPTION
668   WHEN OTHERS THEN
669     -- wf_engine.threshold := l_save_threshold ;
670     Wf_Core.context ('Ams_Approval_Pvt', 'Get_User_Role',p_user_id
671             ,'Unexpected Error IN Get_User_Role' );
672     RAISE;
673 END Get_User_Role;
674 
675 /*============================================================================*/
676 -- Gets Activity details
677 -- If the Activity type is Fund then the priority is taken from the parent
678 /*============================================================================*/
679 PROCEDURE Get_Activity_Details
680   ( p_activity_type       IN     VARCHAR2,
681   p_activity_id           IN   NUMBER,
682   x_object_details     OUT NOCOPY  ObjRecTyp,
683   x_return_status      OUT NOCOPY     VARCHAR2 )
684 IS
685 
686   TYPE obj_csr_type IS REF CURSOR ;
687   l_obj_details obj_csr_type;
688   l_meaning VARCHAR2(80);
689   l_msg_count              NUMBER;
690   l_msg_data               VARCHAR2(4000);
691   l_error_msg              VARCHAR2(4000);
692 
693 BEGIN
694   x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
695   IF p_activity_type = 'CAMP' THEN
696 
697     OPEN l_obj_details  FOR
698     SELECT A.campaign_name,
699     A.business_unit_id,
700     A.city_id,
701     A.custom_setup_id,
702     A.budget_amount_tc,
703     A.org_id,
704     --media_type_code,
705     A.ROLLUP_TYPE,
706     A.priority,
707     A.actual_exec_start_date ,
708     A.actual_exec_end_date ,
709     A.campaign_type ,
710     A.description ,
711     A.owner_user_id ,
712     A.transaction_currency_code ,
713     ''priority_desc,
714     A.source_code,
715     b.source_code,
716     b.campaign_name
717     FROM ams_campaigns_vl A, ams_campaigns_vl b
718     WHERE A.campaign_id = p_activity_id
719     AND NVL(A.parent_campaign_id, 0) = b.campaign_id(+);
720     /* code added by murali and the code from ravi start*/
721   ELSIF p_activity_type = 'CSCH' THEN
722         --insert into ams.test_murali (text) values ('IN CSCH');
723     OPEN l_obj_details  FOR
724     SELECT A.schedule_name,
725     b.business_unit_id,
726     b.city_id, -- changed Bug 2529071 b.country_code
727     A.custom_setup_id,
728     A.budget_amount_tc,
729     A.org_id,
730     A.activity_type_code,
731     A.priority,
732     A.start_date_time,
733     A.end_date_time ,
734     A.objective_code ,
735     A.description ,
736     A.owner_user_id ,
737     A.transaction_currency_code ,
738     ''priority_desc,
739     A.source_code,
740     b.source_code ,
741     b.campaign_name
742     FROM ams_campaign_schedules_vl A, ams_campaigns_vl b
743     WHERE A.schedule_id=p_activity_id
744     AND b.campaign_id=A.campaign_id
745     AND NVL(A.campaign_id, 0) = b.campaign_id;
746   ELSIF p_activity_type = 'OFFR' THEN
747     OPEN l_obj_details  FOR
748     SELECT  SUBSTR(qlh.description,1,240), -- Bug 2986290
749     '' business_unit_id,
750     '' country_code,
751     OFF.custom_setup_id,
752     OFF.budget_amount_tc,
753   --  qlh.orig_org_id, -- org_id Bug 3894489
754    DECODE(OFF.offer_type,'LUMPSUM',OFF.org_id,'SCAN_DATA',OFF.org_id,qlh.orig_org_id) orig_org_id, -- AMITAMKU bug fix 14021238
755     OFF.offer_type,--'' activity_type_code, -- Changed to fix bug#2288550
756     '' priority,
757     qlh.start_date_active,
758     qlh.end_date_active ,
759     OFF.offer_type ,
760     qlh.comments ,
761     OFF.owner_id ,
762     OFF.transaction_currency_code ,
763     ''priority_desc,
764     OFF.offer_code source_code, -- Bug 2873713
765     ''parent_source_code,
766     ''parent_name
767     FROM ozf_offers OFF, qp_list_headers_vl qlh
768     WHERE OFF.qp_list_header_id=qlh.list_header_id
769     AND qlh.list_header_id=p_activity_id;
770       /* code added by murali and the code from ravi end*/
771   ELSIF p_activity_type =  'EVEH' THEN
772     OPEN l_obj_details FOR
773     SELECT A.event_header_name,
774     A.business_unit_id,
775     A.country_code,
776     A.setup_type_id,
777     A.fund_amount_tc,
778     A.org_id,
779     A.event_type_code,
780     A.priority_type_code,
781     A.active_from_date,
782     A.active_to_date,
783     '' ,
784     A.description ,
785     A.owner_user_id ,
786     A.currency_code_tc  ,
787     ''priority_desc,
788     A.source_code,
789     b.source_code ,
790     b.campaign_name
791     FROM ams_event_headers_vl A, ams_campaigns_vl b
792     WHERE A.event_header_id = p_activity_id
793     AND NVL(A.program_id, 0) = b.campaign_id(+);
794   ELSIF p_activity_type = 'EVEO' THEN
795     OPEN l_obj_details FOR
796     SELECT A.event_offer_name,
797     A.business_unit_id,
798     A.country_code,
799     A.setup_type_id,
800     A.fund_amount_tc,
801     A.org_id,
802     A.event_type_code,
803     A.priority_type_code,
804     A.event_start_date ,
805     A.event_end_date ,
806     '' ,
807     A.description ,
808     A.owner_user_id ,
809     A.currency_code_tc  ,
810     ''priority_desc,
811     A.source_code,
812     b.source_code,
813     b.event_header_name
814     FROM ams_event_offers_vl A, ams_event_headers_vl b
815     WHERE A.event_offer_id = p_activity_id
816         AND A.event_object_type = 'EVEO'
817         AND A.event_header_id = b.event_header_id;
818     /* code added by murali start */
819   ELSIF p_activity_type = 'EONE' THEN
820     OPEN l_obj_details FOR
821     SELECT A.event_offer_name,
822     A.business_unit_id,
823     A.country_code,
824     A.setup_type_id,
825     A.fund_amount_tc,
826     A.org_id,
827     A.event_type_code,
828     A.priority_type_code,
829     A.event_start_date ,
830     A.event_end_date ,
831     '' ,
832     A.description ,
833     A.owner_user_id ,
834     A.currency_code_tc  ,
835     '' priority_desc,
836     A.source_code,
837     b.source_code,
838     b.campaign_name
839     FROM ams_event_offers_vl A, ams_campaigns_vl b
840     WHERE A.event_offer_id = p_activity_id
841         AND A.event_object_type = 'EONE'
842         AND NVL(A.parent_id, 0) = b.campaign_id(+);
843     /* code added by murali start */
844   ELSIF p_activity_type = 'FUND' THEN
845     OPEN l_obj_details FOR
846     SELECT short_name,
847     '' business_unit_id,
848     '' country_code,
849     custom_setup_id setup_type_id,
850     NVL(original_budget,0)    +
851     NVL(transfered_in_amt,0)  -
852     NVL(transfered_out_amt,0)
853     total_budget,
854     org_id,
855     TO_CHAR(category_id),
856     '' priority_type_code,
857     start_date_active ,
858     end_date_active ,
859     '' ,
860     description ,
861     owner,
862     currency_code_tc  ,
863     '' priority_desc,
864     '' source_code,
865     '' parent_source_code,
866     '' parent_name
867     FROM ozf_funds_all_vl
868     WHERE fund_id = p_activity_id;
869   ELSIF p_activity_type = 'DELV' THEN
870     OPEN l_obj_details FOR
871     SELECT deliverable_name,
872     '',
873     TO_CHAR(country_id) country_code,
874     custom_setup_id setup_type_id, -- was null vmodur
875     budget_amount_tc,
876     org_id,
877     TO_CHAR(category_type_id),--' setup_type_id, -- Changed to fix bug#2288550
878     '' priority_type_code,
879     actual_avail_from_date ,
880     actual_avail_to_date ,
881     '' ,
882     description ,
883     owner_user_id,
884     transaction_currency_code  ,
885     '' priority_desc,
886     '' source_code,
887     '' parent_source_code,
888     '' parent_name
889     FROM ams_deliverables_vl
890     WHERE deliverable_id = p_activity_id;
891   ELSE
892     Fnd_Message.Set_Name('AMS','AMS_BAD_APPROVAL_OBJECT_TYPE');
893     Fnd_Msg_Pub.ADD;
894     x_return_status := Fnd_Api.G_RET_STS_ERROR;
895     RETURN;
896   END IF ;
897 
898   FETCH l_obj_details INTO x_object_details;
899   IF l_obj_details%NOTFOUND THEN
900     CLOSE l_obj_details;
901     Fnd_Message.Set_Name('AMS','AMS_APPR_BAD_DETAILS');
902     Fnd_Msg_Pub.ADD;
903     x_return_status := Fnd_Api.G_RET_STS_ERROR;
904     RETURN;
905   END IF;
906   CLOSE l_obj_details;
907 
908   -- Bug 2789111 Priority for CSCH and EONE too
909   IF x_object_details.priority IS NOT NULL THEN
910     IF p_activity_type IN ('CAMP','EVEH','EVEO','CSCH','EONE') THEN
911 
912       Ams_Utility_Pvt.get_lookup_meaning( 'AMS_PRIORITY',
913                     x_object_details.priority,
914                     x_return_status,
915                     l_meaning
916                     );
917 
918       IF x_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
919         IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.G_MSG_LVL_ERROR) THEN
920           Fnd_Message.set_name('AMS', 'AMS_BAD_PRIORITY');
921           Fnd_Msg_Pub.ADD;
922           x_return_status := Fnd_Api.G_RET_STS_ERROR;
923           RETURN;
924         END IF;
925       END IF;
926       x_object_details.priority_desc := l_meaning;
927     END IF;
928   END IF;
929 EXCEPTION
930   WHEN OTHERS THEN
931     -- wf_engine.threshold := l_save_threshold ;
932     Wf_Core.context ('ams_approval_pvt', 'Get_Activity_Details',p_activity_type
933             ,p_activity_id ,'Unexpected Error In Get_Activity_Details');
934     RAISE;
935 
936 END Get_Activity_Details;
937 ------------------------------------------------------------------------------
938 PROCEDURE Get_Ntf_Rule_Values
939  (p_approver_name IN VARCHAR2,
940   p_result IN VARCHAR2,
941   x_text_value OUT NOCOPY VARCHAR2,
942   x_number_value OUT NOCOPY NUMBER)
943 IS
944 
945   CURSOR c_get_rule IS
946   SELECT b.text_value, b.number_value
947   FROM wf_routing_rules a, wf_routing_rule_attributes b, wf_routing_rule_attributes c
948   WHERE a.rule_id = b.rule_id
949   AND a.role = p_approver_name
950   AND TRUNC(sysdate) BETWEEN TRUNC(NVL(begin_date, sysdate -1)) AND
951       TRUNC(NVL(end_date,sysdate+1))
952   AND a.message_name = 'AMS_LINE_APPROVE'
953   AND b.name = 'AMS_APPROVED_LINE_AMOUNT'
954   AND b.rule_id = c.rule_id
955   AND c.name = 'RESULT'
956   AND c.text_value = p_result;
957 
958 BEGIN
959   OPEN c_get_rule;
960   FETCH c_get_rule INTO x_text_value, x_number_value;
961   IF c_get_rule%NOTFOUND THEN
962     x_text_value := NULL;
963     x_number_value := 0;
964   END IF;
965   CLOSE c_get_rule;
966 EXCEPTION
967   WHEN OTHERS THEN
968     wf_core.context('ams_approval_pvt','get_ntf_rule_values','p_approver_name',
969                      'Unexpected Error in Get_Ntf_Rule_Values');
970   RAISE;
971 END Get_Ntf_Rule_Values;
972 
973 /*============================================================================*/
974 -- Gets the amount
975 -- Gets the total amount which is approved for activities other than
976 -- Budget
977 -- For Budget it gets the request amount
978 /*============================================================================*/
979 PROCEDURE Get_line_amounts
980          ( p_activity_id         IN  NUMBER,
981            p_activity_type       IN  VARCHAR2,
982            p_act_budget_id       IN  NUMBER, -- was default g_miss_num
983            x_amount              OUT NOCOPY NUMBER)
984 IS
985 
986   CURSOR c_line_amount IS
987   SELECT request_amount
988   FROM ozf_act_budgets
989   WHERE activity_budget_id = p_act_budget_id ;
990 
991 
992   /* New Cursor Definition */
993   CURSOR c_total_amount IS
994   SELECT SUM(NVL(DECODE(a1.transfer_type ,
995           'REQUEST',
996              DECODE(a1.status_code ,'NEW',a1.request_amount,'APPROVED', a1.approved_amount,0),
997           'TRANSFER' ,
998              DECODE(a1.status_code ,'NEW',a1.src_curr_request_amt,'APPROVED', -a1.approved_original_amount))
999           ,0)) approved_amount
1000   FROM ozf_act_budgets a1
1001   WHERE DECODE(a1.transfer_type , 'REQUEST', a1.act_budget_used_by_id, 'TRANSFER' , a1.budget_source_id ) = p_activity_id
1002   AND DECODE(a1.transfer_type , 'REQUEST', a1.arc_act_budget_used_by, 'TRANSFER' , a1.budget_source_type) = p_activity_type
1003   AND a1.transfer_type <> 'UTILIZED'
1004   AND status_code = 'APPROVED';
1005 
1006   l_msg_count              NUMBER;
1007   l_msg_data               VARCHAR2(4000);
1008   l_error_msg              VARCHAR2(4000);
1009 
1010 BEGIN
1011   IF p_act_budget_id IS NOT NULL THEN
1012     OPEN  c_line_amount;
1013     FETCH c_line_amount INTO x_amount;
1014     CLOSE c_line_amount;
1015   ELSE
1016     OPEN  c_total_amount;
1017     FETCH c_total_amount INTO x_amount;
1018     CLOSE c_total_amount;
1019   END IF;
1020 EXCEPTION
1021   WHEN OTHERS THEN
1022     Wf_Core.context ('Ams_Approval_Pvt', 'Get_line_amounts',p_activity_type
1023             ,p_activity_id ,'Unexpected Error IN Get_line_amounts');
1024     RAISE;
1025 
1026 END Get_line_amounts;
1027 
1028 /*============================================================================*/
1029 -- Checks if there are more approvers
1030 /*============================================================================*/
1031 PROCEDURE Check_Approval_Required
1032   ( p_approval_detail_id    IN  NUMBER,
1033   p_current_seq             IN   NUMBER,
1034   x_next_seq                OUT NOCOPY  NUMBER,
1035   x_required_flag           OUT NOCOPY  VARCHAR2)
1036 IS
1037 
1038   CURSOR c_check_app IS
1039   SELECT approver_seq
1040   FROM ams_approvers
1041   WHERE ams_approval_detail_id = p_approval_detail_id
1042   AND approver_seq > p_current_seq
1043   AND TRUNC(SYSDATE) BETWEEN TRUNC(NVL(start_date_active,SYSDATE -1 ))
1044   AND TRUNC(NVL(end_date_active,SYSDATE + 1))
1045   AND active_flag = 'Y'
1046   ORDER BY approver_seq  ;
1047 
1048   l_msg_count              NUMBER;
1049   l_msg_data               VARCHAR2(4000);
1050   l_error_msg              VARCHAR2(4000);
1051 BEGIN
1052   OPEN  c_check_app;
1053   FETCH c_check_app INTO x_next_seq;
1054   IF c_check_app%NOTFOUND THEN
1055     x_required_flag    :=  Fnd_Api.G_FALSE;
1056   ELSE
1057     x_required_flag    :=  Fnd_Api.G_TRUE;
1058   END IF;
1059   CLOSE c_check_app;
1060 EXCEPTION
1061   WHEN OTHERS THEN
1062 
1063     Wf_Core.context ('Ams_Approval_Pvt', 'Check_Approval_Required',p_approval_detail_id
1064             ,p_current_seq ,'Unexpected Error IN Check_Approval_Required');
1065     RAISE;
1066 
1067 END  Check_Approval_Required;
1068 
1069 /*============================================================================*/
1070 -- Gets approver info
1071 -- Approvers Can be user or Role
1072 -- If it is role it should of role_type MKTAPPR AMSAPPR
1073 -- The Seeded role is AMS_DEFAULT_APPROVER
1074 -- SVEERAVE - 10-MAR-2002    Changed x_approver_id, x_object_approver_id to NUMBER from VARCHAR2
1075 /*============================================================================*/
1076 PROCEDURE Get_Approver_Info
1077   ( p_approval_detail_id       IN  NUMBER,
1078   p_current_seq                IN   NUMBER,
1079   x_approver_id                OUT NOCOPY  NUMBER, --  x_approver_id          OUT  VARCHAR2,
1080   x_approver_type              OUT NOCOPY  VARCHAR2,
1081   x_role_name                  OUT NOCOPY  VARCHAR2,
1082   x_object_approver_id         OUT NOCOPY  NUMBER, --  x_object_approver_id   OUT  VARCHAR2,
1083   x_notification_type          OUT NOCOPY  VARCHAR2,
1084   x_notification_timeout       OUT NOCOPY  VARCHAR2,
1085   x_return_status              OUT NOCOPY  VARCHAR2
1086   )
1087 IS
1088   l_msg_count              NUMBER;
1089   l_msg_data               VARCHAR2(4000);
1090   l_error_msg              VARCHAR2(4000);
1091 
1092   l_approver_id            NUMBER;
1093   l_rule_name              VARCHAR2(240);
1094 
1095   CURSOR c_approver_info IS
1096     SELECT approver_id
1097       , approver_type
1098       , object_approver_id
1099       , notification_type
1100       , notification_timeout
1101     FROM ams_approvers
1102     WHERE ams_approval_detail_id = p_approval_detail_id
1103       AND approver_seq = p_current_seq
1104       AND TRUNC(SYSDATE) BETWEEN TRUNC(NVL(start_date_active,SYSDATE-1 ))
1105       AND TRUNC(NVL(end_date_active,SYSDATE + 1))
1106       AND active_flag = 'Y';
1107 
1108   CURSOR c_role_info IS
1109     SELECT rr.role_resource_id, rl.role_name
1110     FROM jtf_rs_role_relations rr, jtf_rs_roles_vl rl
1111     WHERE rr.role_id = rl.role_id
1112     AND rr.role_resource_type = 'RS_INDIVIDUAL'
1113     AND rr.delete_flag = 'N'
1114     AND SYSDATE BETWEEN rr.start_date_active and nvl(rr.end_date_active, SYSDATE)
1115     AND rl.role_type_code in ( 'MKTGAPPR', 'AMSAPPR')
1116     AND rl.role_id = l_approver_id;
1117 -- SQL Repository Fix
1118 /*
1119     SELECT role_resource_id
1120       , role_name
1121     FROM jtf_rs_defresroles_vl
1122     WHERE role_type_code IN ('MKTGAPPR','AMSAPPR')
1123       AND role_id   = l_approver_id
1124       AND role_resource_type = 'RS_INDIVIDUAL'
1125       AND delete_flag = 'N'
1126       AND TRUNC(SYSDATE) BETWEEN TRUNC(res_rl_start_date)
1127       AND TRUNC(NVL(res_rl_end_date,SYSDATE));
1128 */
1129   CURSOR c_role_info_count IS
1130   SELECT COUNT(1)
1131     FROM jtf_rs_role_relations rr, jtf_rs_roles_b rl
1132     WHERE rr.role_id = rl.role_id
1133     AND rr.role_resource_type = 'RS_INDIVIDUAL'
1134     AND rr.delete_flag = 'N'
1135     AND SYSDATE BETWEEN rr.start_date_active and nvl(rr.end_date_active, SYSDATE)
1136     AND rl.role_type_code in ( 'MKTGAPPR', 'AMSAPPR')
1137     AND rl.role_id = l_approver_id;
1138 -- SQL Repository Fix
1139   /*
1140     SELECT COUNT(1)
1141     FROM jtf_rs_defresroles_vl
1142     WHERE role_type_code IN ('MKTGAPPR','AMSAPPR')
1143       AND role_id   = l_approver_id
1144       AND role_resource_type = 'RS_INDIVIDUAL'
1145       AND delete_flag = 'N'
1146       AND TRUNC(SYSDATE) BETWEEN TRUNC(res_rl_start_date)
1147       AND TRUNC(NVL(res_rl_end_date,SYSDATE));
1148   */
1149   CURSOR c_default_role_info IS
1150     SELECT rr.role_id
1151     FROM jtf_rs_role_relations rr,
1152       jtf_rs_roles_b rl
1153     WHERE rr.role_id = rl.role_id
1154       AND  rl.role_type_code IN( 'MKTGAPPR','AMSAPPR')
1155       AND rl.role_code   = 'AMS_DEFAULT_APPROVER'
1156       AND rr.role_resource_type = 'RS_INDIVIDUAL'
1157       AND delete_flag = 'N'
1158       AND TRUNC(SYSDATE) BETWEEN TRUNC(rr.start_date_active)
1159       AND TRUNC(NVL(rr.end_date_active,SYSDATE));
1160 
1161   CURSOR c_rule_name IS
1162     SELECT name
1163       FROM ams_approval_details_vl
1164      WHERE approval_detail_id = p_approval_detail_id;
1165 
1166   l_count NUMBER;
1167 
1168 BEGIN
1169   x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
1170   OPEN  c_approver_info;
1171   FETCH c_approver_info INTO x_approver_id,
1172               x_approver_type,
1173               x_object_approver_id,
1174               x_notification_type,
1175               x_notification_timeout;
1176   IF c_approver_info%NOTFOUND THEN
1177     CLOSE c_approver_info;
1178 
1179     OPEN c_rule_name;
1180     FETCH c_rule_name INTO l_rule_name;
1181     CLOSE c_rule_name;
1182     Fnd_Message.Set_Name('AMS','AMS_NO_APPR_FOR_RULE');
1183     Fnd_Message.Set_Token('RULE_NAME',l_rule_name);
1184     Fnd_Msg_Pub.ADD;
1185     x_return_status := Fnd_Api.G_RET_STS_ERROR;
1186     RETURN;
1187   END IF;
1188 
1189   IF x_approver_type = 'ROLE' THEN
1190     IF x_object_approver_id IS NULL THEN
1191       OPEN  c_default_role_info ;
1192       FETCH c_default_role_info INTO x_object_approver_id;
1193       IF c_default_role_info%NOTFOUND THEN
1194         CLOSE c_default_role_info ;
1195         Fnd_Message.Set_Name('AMS','AMS_NO_DEFAULT_ROLE'); -- VMODUR added
1196         --Fnd_Message.Set_Name('AMS','AMS_APPR_INVALID_ROLE');
1197         Fnd_Msg_Pub.ADD;
1198         x_return_status := Fnd_Api.G_RET_STS_ERROR;
1199         RETURN;
1200       END IF;
1201       CLOSE c_default_role_info ;
1202     END IF;
1203     l_approver_id := x_object_approver_id;
1204     OPEN  c_role_info_count;
1205     FETCH c_role_info_count INTO l_count;
1206     IF l_count > 1 THEN
1207       CLOSE c_role_info_count;
1208       Fnd_Message.Set_Name('AMS','AMS_MANY_DEFAULT_ROLE'); -- VMODUR added
1209       --Fnd_Message.Set_Name('AMS','AMS_APPR_INVALID_ROLE');
1210       Fnd_Msg_Pub.ADD;
1211       x_return_status := Fnd_Api.G_RET_STS_ERROR;
1212       RETURN;
1213     END IF;
1214     CLOSE c_role_info_count;
1215     OPEN  c_role_info;
1216     FETCH c_role_info INTO x_object_approver_id, x_role_name;
1217     IF c_role_info%NOTFOUND THEN
1218       CLOSE c_role_info;
1219       Fnd_Message.Set_Name('AMS','AMS_APPR_INVALID_ROLE');
1220       Fnd_Msg_Pub.ADD;
1221       x_return_status := Fnd_Api.G_RET_STS_ERROR;
1222       RETURN;
1223     END IF;
1224     CLOSE c_role_info;
1225   END IF;
1226   CLOSE c_approver_info;
1227 EXCEPTION
1228   WHEN OTHERS THEN
1229     -- wf_engine.threshold := l_save_threshold ;
1230 
1231     Wf_Core.context ('Ams_Approval_Pvt', 'Get_Approver_Info',p_approval_detail_id
1232             ,p_current_seq ,'Unexpected Error IN Get_Approver_Info');
1233     RAISE;
1234 END Get_Approver_Info;
1235 
1236 /*****************************************************************
1237 -- Start of Comments
1238 -- NAME
1239 --  Get_Approval_Details
1240 -- PURPOSE
1241 --   This Procedure get all the approval details
1242 --
1243 -- Used By Objects
1244 -- p_activity_type           Activity Type or Objects
1245 --                           (CAMP,DELV,EVEO,EVEH .. )
1246 -- p_activity_id             Primary key of the Object
1247 -- p_approval_type           BUDGET,CONCEPT
1248 -- p_act_budget_id           If called from header record this field is null
1249 -- x_object_details          Object details contains the detail of objects
1250 -- x_approval_detail_id      Approval detail Id macthing the criteria
1251 -- x_approver_seq            Approval Sequence
1252 -- x_return_status           Return Status
1253 -- NOTES
1254 -- HISTORY
1255 --  15-SEP-2000          GJOBY       CREATED
1256 -- End of Comments
1257 *****************************************************************/
1258 
1259 
1260 PROCEDURE Get_Approval_Details
1261   ( p_activity_id        IN  NUMBER,
1262   p_activity_type        IN   VARCHAR2,
1263   p_approval_type        IN   VARCHAR2 DEFAULT  'BUDGET',
1264   p_act_budget_id        IN   NUMBER , -- was default g_mss_num
1265   x_object_details       OUT NOCOPY  ObjRecTyp,
1266   x_approval_detail_id   OUT NOCOPY  NUMBER,
1267   x_approver_seq         OUT NOCOPY  NUMBER,
1268   x_return_status        OUT NOCOPY  VARCHAR2)
1269 IS
1270   l_msg_count           NUMBER;
1271   l_msg_data            VARCHAR2(4000);
1272   l_error_msg           VARCHAR2(4000);
1273 
1274   l_amount              NUMBER;
1275   l_business_unit_id    NUMBER           := Fnd_Api.G_MISS_NUM;
1276   l_org_id              NUMBER           := Fnd_Api.G_MISS_NUM;
1277   l_setup_type_id       NUMBER           := Fnd_Api.G_MISS_NUM;
1278   l_object_type         VARCHAR2(30)     := Fnd_Api.G_MISS_CHAR;
1279   l_priority            VARCHAR2(30)     := Fnd_Api.G_MISS_CHAR;
1280   l_country_code        VARCHAR2(30)     := Fnd_Api.G_MISS_CHAR;
1281   l_approver_id         NUMBER;
1282   l_object_details      ObjRecTyp;
1283   l_activity_type       VARCHAR2(30);
1284   l_activity_id         NUMBER;
1285   l_curr_match          VARCHAR2(1) := 'N';
1286   l_appr_amount         NUMBER;
1287   l_rule_curr_code      VARCHAR2(15);
1288   l_curr_code           VARCHAR2(15);
1289   l_obj_curr_code       VARCHAR2(15);
1290   l_lower_limit         NUMBER;
1291   l_upper_limit         NUMBER;
1292   l_budget_amount       NUMBER;
1293   l_return_status       VARCHAR2(1);
1294   d_msg                 VARCHAR2(4000);
1295 
1296  -- Get Approval Detail Id matching the Criteria
1297  -- Approval Object (CAMP, DELV.. ) is mandatory
1298  -- Approval type   (BUDGET    .. ) is mandatory
1299  -- APPROVAL_LIMIT_FROM  is mandatory
1300  -- Using Weightage Percentage
1301  -- business_unit_id            6
1302  -- organization_id             5
1303  -- approval_object_type        4
1304  -- approval_priority           3
1305  -- country_code                2
1306  -- custom_setup_id             1
1307 
1308 -- Start: Added for fixing the bug 6310662 by rsatyava on 2007/08/09
1309 
1310   l_org_cnd_stmt       VARCHAR2(2000):=' AND NVL(organization_id,:orgid) =:orgid ' ;
1311 
1312   l_appr_detl_stmt1 VARCHAR2(4000):=' SELECT approval_detail_id,
1313 					 currency_code,
1314 					 nvl(approval_limit_from,0),
1315 					 nvl(approval_limit_to, :amount)
1316 				    FROM ams_approval_details
1317 				    WHERE NVL(business_unit_id,:businessunit) =:businessunit ' ;
1318 
1319 l_appr_detl_stmt2 VARCHAR2(4000):='  AND NVL(custom_setup_id,:customsetupid) =:customsetupid
1320 				     AND approval_object  =:appobject AND approval_type  =:apptype
1321 				     AND NVL(approval_object_type,:appobjecttype) =:appobjecttype
1322                                      AND NVL(user_country_code,:usercountrycode) =:usercountrycode
1323                                      AND NVL(approval_priority,:apppriority) =:apppriority
1324                                      AND seeded_flag  =''N''
1325                                      AND :amount between DECODE(:amount, 0, 0, nvl(approval_limit_from,0)) and
1326                                      NVL(approval_limit_to,:amount)
1327                                      AND TRUNC(SYSDATE) BETWEEN TRUNC(NVL(start_date_active,SYSDATE -1 ))
1328                                      AND TRUNC(NVL(end_date_active,SYSDATE + 1))
1329 				  ORDER BY (POWER(2,DECODE(business_unit_id,'''',0,6)) +
1330 					       POWER(2,DECODE(organization_id,'''',0,5)) +
1331 					       POWER(2,DECODE(custom_setup_id,'''',0,1)) +
1332 					       POWER(2,DECODE(user_country_code,'''',0,2)) +
1333 					       POWER(2,DECODE(approval_object_type,'''',0,4)) +
1334 					       POWER(2,DECODE(approval_priority,'''',0,3)  )) DESC ';
1335 
1336  l_appr_csr appr_cursor;
1337 
1338 -- End: Added for fixing the bug 6310662 by rsatyava on 2007/08/09
1339 
1340 
1341   CURSOR c_approver_detail_id (amount IN NUMBER) IS
1342   SELECT approval_detail_id,
1343          currency_code,
1344 	 nvl(approval_limit_from,0),
1345 	 nvl(approval_limit_to, amount)
1346   FROM ams_approval_details
1347   WHERE NVL(business_unit_id,l_business_unit_id) = l_business_unit_id
1348   AND NVL(organization_id,l_org_id)              = l_org_id
1349   AND NVL(custom_setup_id,l_setup_type_id)       = l_setup_type_id
1350   AND approval_object                            = p_activity_type
1351   AND approval_type                              = p_approval_type
1352   AND NVL(approval_object_type,l_object_type)    = l_object_type
1353   AND NVL(user_country_code,l_country_code)      = l_country_code
1354   AND NVL(approval_priority,l_priority)          = l_priority
1355   AND seeded_flag                                = 'N'
1356   AND amount between DECODE(amount, 0, 0, nvl(approval_limit_from,0)) and
1357                     nvl(approval_limit_to,amount)
1358   AND TRUNC(SYSDATE) BETWEEN TRUNC(NVL(start_date_active,SYSDATE -1 ))
1359   AND TRUNC(NVL(end_date_active,SYSDATE + 1))
1360   ORDER BY (POWER(2,DECODE(business_unit_id,'',0,6)) +
1361                POWER(2,DECODE(organization_id,'',0,5)) +
1362                POWER(2,DECODE(custom_setup_id,'',0,1)) +
1363 	       POWER(2,DECODE(user_country_code,'',0,2)) +
1364                POWER(2,DECODE(approval_object_type,'',0,4)) +
1365                POWER(2,DECODE(approval_priority,'',0,3)  )) DESC ;
1366 
1367   -- If the there are no matching records it takes the default Rule
1368   CURSOR c_approver_def IS
1369   SELECT approval_detail_id
1370   FROM ams_approval_details
1371   WHERE approval_detail_id = 150;
1372  -- WHERE seeded_flag = 'Y'; -- to avoid FTS
1373 
1374   -- Takes Min Approver Sequence From Ams_approvers Once matching records are
1375   -- Found form ams_approval_deatils
1376   CURSOR c_approver_seq IS
1377   SELECT MIN(approver_seq)
1378   FROM ams_approvers
1379   WHERE ams_approval_detail_id  = x_approval_detail_id
1380   AND TRUNC(SYSDATE) BETWEEN
1381   TRUNC(NVL(start_date_active,SYSDATE -1 )) AND TRUNC(NVL(end_date_active,SYSDATE + 1))
1382   AND active_flag = 'Y';
1383 
1384   -- For Budgets the priority has to to be taken from the Object priority
1385   -- The Following cursor returns the Approval object and Approval Object Id
1386   -- for the parent
1387 
1388   CURSOR c_fund_priority IS
1389   SELECT ARC_ACT_BUDGET_USED_BY,
1390          ACT_BUDGET_USED_BY_ID
1391   FROM ozf_act_budgets
1392   WHERE ACTIVITY_BUDGET_ID =  p_act_budget_id;
1393 BEGIN
1394   x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
1395 
1396 
1397   Get_Activity_Details
1398   ( p_activity_type    => p_activity_type,
1399   p_activity_id        => p_activity_id,
1400   x_object_details     => x_object_details,
1401   x_return_status      => x_return_status);
1402 
1403   IF x_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
1404     RETURN;
1405   END IF;
1406 
1407   l_business_unit_id    := NVL(x_object_details.business_unit_id,l_business_unit_id);
1408   l_org_id              := NVL(x_object_details.org_id,l_org_id);
1409   l_setup_type_id       := NVL(x_object_details.setup_type_id,l_setup_type_id);
1410   l_object_type         := NVL(x_object_details.object_type,l_object_type);
1411   l_priority            := NVL(x_object_details.priority,l_priority);
1412   l_country_code        := NVL(x_object_details.country_code,l_country_code);
1413   l_curr_code           := nvl(x_object_details.currency, 'USD');
1414 
1415 
1416   IF p_act_budget_id = Fnd_Api.G_MISS_NUM OR
1417      p_act_budget_id IS NULL THEN -- Important for JAVA call
1418     -- This amount is in the objects currency
1419     l_amount := NVL(x_object_details.total_header_amount, 0);
1420   ELSE
1421 
1422     Get_line_amounts
1423       ( p_activity_id       => p_activity_id,
1424       p_activity_type       => p_activity_type,
1425       p_act_budget_id       => p_act_budget_id,
1426       x_amount              => l_amount);
1427 
1428     x_object_details.total_header_amount  :=  NVL(l_amount, 0) ;
1429 
1430     OPEN c_fund_priority ;
1431     FETCH c_fund_priority INTO l_activity_type , l_activity_id ;
1432     CLOSE c_fund_priority ;
1433 
1434     Get_Activity_Details
1435       ( p_activity_type      => l_activity_type,
1436         p_activity_id        => l_activity_id,
1437         x_object_details     => l_object_details,
1438         x_return_status      => x_return_status);
1439 
1440     l_priority            := NVL(l_object_details.priority,l_priority);
1441     l_obj_curr_code       := nvl(l_object_details.currency,'USD');
1442 
1443     IF x_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
1444       RETURN;
1445     END IF;
1446   END IF;
1447 
1448     IF (p_approval_type = 'BUDGET' AND p_activity_type = 'FUND') THEN
1449 
1450      -- For Budget Line, if a rule with same currency exists
1451      -- then that rule would be the first choice
1452      -- Convert l_amount from the Object Currency to the currency of the
1453      -- Budget
1454 
1455      AMS_Utility_Pvt.Convert_Currency (
1456           p_from_currency      => l_obj_curr_code,
1457           p_to_currency        => l_curr_code,
1458           p_from_amount        => l_amount,
1459           x_to_amount          => l_budget_amount,
1460           x_return_status      => x_return_status );
1461 
1462      IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1463       return;
1464      END IF;
1465 
1466      OPEN c_approver_detail_id(l_budget_amount);
1467      LOOP
1468      FETCH c_approver_detail_id INTO x_approval_detail_id,
1469            l_rule_curr_code, l_lower_limit, l_upper_limit;
1470      EXIT WHEN c_approver_detail_id%NOTFOUND;
1471 
1472        IF l_rule_curr_code = l_curr_code THEN
1473          l_curr_match := 'Y';
1474          EXIT;
1475        END  IF;
1476      END LOOP;
1477      CLOSE c_approver_detail_id;
1478 
1479      -- If no approval rule has been defined with the same currency as the
1480      -- budget, then convert the amount from the budget currency
1481      -- to the approval rule currency and then compare
1482 
1483      IF l_curr_match <> 'Y' THEN
1484        -- Get all rules regardless of amount
1485        OPEN c_approver_detail_id(0);
1486        LOOP
1487        FETCH c_approver_detail_id INTO x_approval_detail_id, l_rule_curr_code,
1488                                        l_lower_limit, l_upper_limit;
1489        EXIT WHEN c_approver_detail_id%NOTFOUND;
1490 
1491           AMS_Utility_Pvt.Convert_Currency (
1492           p_from_currency      => l_curr_code,
1493           p_to_currency        => l_rule_curr_code,
1494           p_from_amount        => l_budget_amount,
1495           x_to_amount          => l_appr_amount,
1496           x_return_status      => l_return_status );
1497 
1498       IF l_return_status = FND_API.G_RET_STS_SUCCESS THEN
1499 
1500         -- Added following condition to account for upper limit
1501         -- being null in which case it will be initialized as
1502         -- zero. reopned 3655122 fix
1503 
1504         IF l_upper_limit = 0 THEN
1505            l_upper_limit := l_appr_amount;
1506         END IF;
1507 
1508         --
1509         -- Check whether the amount in converted currency is between
1510         -- the approval rules' lower and upper amounts
1511         IF l_appr_amount BETWEEN l_lower_limit and l_upper_limit THEN
1512            EXIT;
1513         END IF;
1514 
1515       END IF;
1516           x_approval_detail_id := NULL;
1517       END LOOP;
1518       CLOSE c_approver_detail_id;
1519 
1520     END IF;
1521 
1522        -- No approval rule in any currency matched the budget line
1523        -- Get the default approval rule
1524        IF x_approval_detail_id IS NULL THEN
1525          OPEN c_approver_def ;
1526          FETCH c_approver_def INTO x_approval_detail_id;
1527          IF c_approver_def%NOTFOUND THEN
1528            CLOSE c_approver_def ;
1529            FND_MESSAGE.Set_Name('AMS','AMS_NO_DEFAULT_ROLE');
1530            FND_MSG_PUB.Add;
1531            x_return_status := FND_API.G_RET_STS_ERROR;
1532            return;
1533          END IF;
1534         CLOSE c_approver_def ;
1535       END IF;
1536 
1537   -- All other object and approval type combinations don't really
1538   -- need currency code for determining an approval rule
1539   ELSE -- Not a Budget Line
1540 
1541   -- Start:Added by rsatyava for fixing the bug#6310662 on 2007/08/09
1542   IF(p_activity_type = 'FUND' OR p_activity_type = 'OFFR') THEN
1543      OPEN l_appr_csr for l_appr_detl_stmt1 ||
1544 			 l_org_cnd_stmt ||
1545 			 l_appr_detl_stmt2
1546      using l_amount,
1547 	   l_business_unit_id,l_business_unit_id,
1548 	   l_org_id,l_org_id,
1549 	   l_setup_type_id,l_setup_type_id,
1550 	   p_activity_type,p_approval_type,
1551 	   l_object_type, l_object_type,
1552 	   l_country_code, l_country_code,
1553 	   l_priority, l_priority,
1554 	   l_amount, l_amount,l_amount;
1555   ELSE
1556     -- If the activity type is CAMP,CSCH,DELV,EVEO,EVEH,EONE then the conditional clause for oragnization id is not appended.
1557     OPEN l_appr_csr for l_appr_detl_stmt1 ||
1558 			l_appr_detl_stmt2
1559 	  using l_amount,
1560 		l_business_unit_id,l_business_unit_id,
1561 		l_setup_type_id,l_setup_type_id,
1562 		p_activity_type,p_approval_type,
1563 		l_object_type, l_object_type,
1564 		l_country_code, l_country_code,
1565 		l_priority, l_priority,
1566 		l_amount, l_amount,l_amount;
1567      END IF;
1568   -- End:Added for fixing the bug 6310662 by rsatyava on 2007/08/09
1569 
1570     -- Start:Modified by rsatyava for fixing the bug#6310662 on 2007/08/09
1571 
1572     LOOP
1573     FETCH l_appr_csr  INTO x_approval_detail_id, l_rule_curr_code,
1574         l_lower_limit, l_upper_limit;
1575     EXIT WHEN l_appr_csr%NOTFOUND;
1576 
1577     -- End:Modified by rsatyava for fixing the bug#6210036 on 2007/08/01
1578 
1579     -- Fix for Bug when zero budget approvals pick up rules starting
1580     -- at more than zero. get all rules and pick up one which the
1581     -- amount is between low and high. if none found use the default
1582 
1583       IF l_amount BETWEEN l_lower_limit and l_upper_limit THEN
1584         EXIT;
1585       END IF;
1586         x_approval_detail_id := null;
1587     END LOOP;
1588    CLOSE l_appr_csr;
1589 
1590    -- End:Modified by rsatyava for fixing the bug#6310662 on 2007/08/09
1591 
1592 
1593 
1594   IF x_approval_detail_id IS NULL THEN
1595       OPEN c_approver_def ;
1596       FETCH c_approver_def INTO x_approval_detail_id;
1597       IF c_approver_def%NOTFOUND THEN
1598         CLOSE c_approver_def ;
1599         FND_MESSAGE.Set_Name('AMS','AMS_NO_DEFAULT_ROLE');
1600         FND_MSG_PUB.Add;
1601         x_return_status := FND_API.G_RET_STS_ERROR;
1602         return;
1603       END IF;
1604       CLOSE c_approver_def ;
1605   END IF;
1606 
1607 END IF; -- not a budget line
1608 
1609   OPEN  c_approver_seq  ;
1610   FETCH c_approver_seq INTO x_approver_seq ;
1611    IF c_approver_seq%NOTFOUND THEN
1612     CLOSE c_approver_seq;
1613     FND_MESSAGE.Set_Name('AMS','AMS_NO_APPROVER_SEQUENCE');
1614     FND_MSG_PUB.Add;
1615     x_return_status := FND_API.G_RET_STS_ERROR;
1616     return;
1617   END IF;
1618   CLOSE c_approver_seq;
1619 END Get_Approval_Details;
1620 
1621 /*****************************************************************
1622 -- Start of Comments
1623 -- NAME
1624 --   StartProcess
1625 -- PURPOSE
1626 --   This Procedure will Start the flow
1627 --
1628 -- Used By Objects
1629 -- p_activity_type                     Activity Type or Objects
1630 --                                     (CAMP,DELV,EVEO,EVEH .. )
1631 -- p_activity_id                       Primary key of the Object
1632 -- p_approval_type                     BUDGET,CONCEPT
1633 -- p_object_version_number             Object Version Number
1634 -- p_orig_stat_id                      The status to which is
1635 --                                     to be reverted if process fails
1636 -- p_new_stat_id                       The status to which it is
1637 --                                     to be updated if the process succeeds
1638 -- p_reject_stat_id                    The status to which is
1639 --                                     to be updated if the process fails
1640 -- p_requester_userid                  The requester who has submitted the
1641 --                                     process
1642 -- p_notes_from_requester              Notes from the requestor
1643 -- p_workflowprocess                   Name of the workflow process
1644 --                                     AMS_CONCEPT_APPROVAL -- For Concept
1645 --                                     AMS_APPROVAL -- For Budget Approvals
1646 -- p_item_type                         AMSAPRV
1647 -- NOTES
1648 -- Item key generated as combination of Activity Type, Activity Id, and Object
1649 -- Version Number.
1650 -- For ex. CAMP100007 where 7 is object version number and 10000 Activity id
1651 -- HISTORY
1652 --  15-SEP-2000          GJOBY       CREATED
1653 -- End of Comments
1654 *****************************************************************/
1655 
1656 PROCEDURE StartProcess
1657       (p_activity_type         IN   VARCHAR2,
1658       p_activity_id            IN   NUMBER,
1659       p_approval_type          IN   VARCHAR2,
1660       p_object_version_number  IN   NUMBER,
1661       p_orig_stat_id           IN   NUMBER,
1662       p_new_stat_id            IN   NUMBER,
1663       p_reject_stat_id         IN   NUMBER,
1664       p_requester_userid       IN   NUMBER,
1665       p_notes_from_requester   IN   VARCHAR2   DEFAULT NULL,
1666       p_workflowprocess        IN   VARCHAR2   ,
1667       p_item_type              IN   VARCHAR2
1668       )
1669 IS
1670   itemtype                 VARCHAR2(30) := NVL(p_item_type,'AMSAPRV');
1671   itemkey                  VARCHAR2(30) := p_activity_type||p_activity_id||
1672                                                 p_object_version_number;
1673   itemuserkey              VARCHAR2(80) := p_activity_type||p_activity_id||
1674                                                 p_object_version_number;
1675 
1676   l_requester_role         VARCHAR2(320) ; -- was 100
1677   l_manager_role           VARCHAR2(100) ;
1678   l_display_name           VARCHAR2(360) ; -- was 240
1679   l_requester_id           NUMBER ;
1680   l_person_id              NUMBER ;
1681   l_appr_for               VARCHAR2(240) ;
1682   l_appr_meaning           VARCHAR2(240);
1683   l_return_status          VARCHAR2(1);
1684   l_msg_count              NUMBER;
1685   l_msg_data               VARCHAR2(4000);
1686   l_error_msg              VARCHAR2(4000);
1687   x_resource_id            NUMBER;
1688 
1689   l_save_threshold         NUMBER := Wf_Engine.threshold;
1690 
1691   l_appr_hist_rec          AMS_Appr_Hist_PVT.appr_hist_rec_type;
1692 
1693   -- [BEGIN OF BUG2741039 FIXING by vmodur 09-Jan-2003]
1694     l_user_id                NUMBER;
1695     l_resp_id                NUMBER;
1696     l_appl_id                NUMBER;
1697     l_security_group_id      NUMBER;
1698   -- [END OF BUG2741039 FIXING]
1699 
1700   CURSOR c_resource IS
1701   SELECT resource_id ,employee_id source_id,full_name resource_name
1702   FROM ams_jtf_rs_emp_v
1703   WHERE user_id = x_resource_id ;
1704 BEGIN
1705   Fnd_Msg_Pub.initialize();
1706 
1707   /* Delete any previous history rows for this object and approval type Bug 2761026*/
1708 
1709    AMS_Appr_Hist_PVT.Delete_Appr_Hist(
1710              p_api_version_number => 1.0,
1711              p_init_msg_list      => FND_API.G_FALSE,
1712              p_commit             => FND_API.G_FALSE,
1713              p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
1714              x_return_status      => l_return_status,
1715              x_msg_count          => l_msg_count,
1716              x_msg_data           => l_msg_data,
1717              p_object_id          => p_activity_id,
1718              p_object_type_code   => p_activity_type,
1719              p_sequence_num       => null,
1720              p_action_code        => null,
1721              p_object_version_num => null,
1722              p_approval_type      => p_approval_type);
1723 
1724            IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
1725              RAISE Fnd_Api.G_EXC_ERROR;
1726            END IF;
1727 
1728   Ams_Utility_Pvt.debug_message('START :Item TYPE : '||itemtype
1729                          ||' Item KEY : '||itemkey);
1730 
1731     -- wf_engine.threshold := -1;
1732   Wf_Engine.CreateProcess (itemtype   =>   itemtype,
1733                             itemkey    =>   itemkey ,
1734                             process    =>   p_workflowprocess);
1735 
1736    Wf_Engine.SetItemUserkey(itemtype   =>   itemtype,
1737                             itemkey    =>   itemkey ,
1738                             userkey    =>   itemuserkey);
1739 
1740   -- 09-Jan-2003  VMODUR  Fix for BUG 2741039 - add pl/sql global context into
1741   --                      workflow item attribute.It could be used later on for
1742   --                      PL/SQL function to initialize the global context when the
1743   --                      session is established by workflow mailer.
1744 
1745    l_user_id := FND_GLOBAL.user_id;
1746    l_resp_id := FND_GLOBAL.resp_id;
1747    l_appl_id := FND_GLOBAL.resp_appl_id;
1748    l_security_group_id := FND_GLOBAL.security_group_id;
1749 
1750    WF_ENGINE.SetItemAttrNumber(itemtype   =>  itemtype ,
1751                                itemkey    =>  itemkey,
1752                                aname      =>  'USER_ID',
1753                                avalue     =>  l_user_id
1754                               );
1755 
1756    WF_ENGINE.SetItemAttrNumber(itemtype   =>  itemtype ,
1757                                itemkey    =>  itemkey,
1758                                aname      =>  'RESPONSIBILITY_ID',
1759                                avalue     =>  l_resp_id
1760                               );
1761 
1762    WF_ENGINE.SetItemAttrNumber(itemtype   =>  itemtype ,
1763                                itemkey    =>  itemkey,
1764                                aname      =>  'APPLICATION_ID',
1765                                avalue     =>  l_appl_id
1766                               );
1767 
1768    WF_ENGINE.SetItemAttrNumber(itemtype   =>  itemtype ,
1769                                itemkey    =>  itemkey,
1770                                aname      =>  'SECURITY_GROUP_ID',
1771                                avalue     =>  l_security_group_id
1772                               );
1773   -- [END OF BUG2741039 FIXING]
1774 
1775    /*****************************************************************
1776       Initialize Workflow Item Attributes
1777    *****************************************************************/
1778 
1779    -- Activity Type  (Some of valid values are 'CAMP','DELV','EVEH','EVEO'..);
1780    Wf_Engine.SetItemAttrText(itemtype   =>  itemtype ,
1781                              itemkey    =>  itemkey,
1782                              aname      =>  'AMS_ACTIVITY_TYPE',
1783                              avalue     =>   p_activity_type  );
1784 
1785    -- Activity ID  (primary Id of Activity Object)
1786    Wf_Engine.SetItemAttrNumber(itemtype  =>  itemtype ,
1787                                itemkey   =>  itemkey,
1788                                aname     =>  'AMS_ACTIVITY_ID',
1789                                avalue    =>  p_activity_id  );
1790 
1791 
1792    -- Original Status Id (If error occurs we have to revert back to this
1793    --                     status )
1794    Wf_Engine.SetItemAttrNumber(itemtype  =>  itemtype,
1795                                itemkey   =>  itemkey,
1796                                aname     =>  'AMS_ORIG_STAT_ID',
1797                                avalue    =>  p_orig_stat_id  );
1798 
1799    -- New Status Id (If activity is approved status of activity is updated
1800    --                by this status )
1801    Wf_Engine.SetItemAttrNumber(itemtype  =>  itemtype,
1802                                itemkey   =>  itemkey,
1803                                aname     =>  'AMS_NEW_STAT_ID',
1804                                avalue    =>  p_new_stat_id  );
1805 
1806    -- Reject Status Id (If activity is approved status of activity is rejected
1807    --                by this status )
1808    Wf_Engine.SetItemAttrNumber(itemtype  =>  itemtype,
1809                                itemkey   =>  itemkey,
1810                                aname     =>  'AMS_REJECT_STAT_ID',
1811                                avalue    =>  p_reject_stat_id  );
1812 
1813 
1814    -- Object Version Number
1815    Wf_Engine.SetItemAttrNumber(itemtype  =>  itemtype,
1816                                itemkey   =>  itemkey,
1817                                aname     =>  'AMS_OBJECT_VERSION_NUMBER',
1818                                avalue    =>  p_object_version_number  );
1819 
1820    -- Notes from the requester
1821    Wf_Engine.SetItemAttrText(itemtype   =>  itemtype,
1822                              itemkey    =>  itemkey,
1823                              aname      =>  'AMS_NOTES_FROM_REQUESTER',
1824                              avalue     =>  NVL(p_notes_from_requester,'') );
1825 
1826    /*-- Fetch resource Id for the requestor
1827    OPEN c_resource ;
1828    FETCH c_resource INTO l_requester_id ,l_person_id ;
1829    CLOSE c_resource ; */
1830 
1831    Wf_Engine.SetItemAttrNumber(itemtype  =>  itemtype,
1832                                itemkey   =>  itemkey,
1833                                aname     =>  'AMS_REQUESTER_ID',
1834                                avalue    =>  p_requester_userid       );
1835 
1836   l_return_status := Fnd_Api.G_RET_STS_SUCCESS;
1837 
1838   --  check for description of activity For ex. CAMP Campaign
1839   Ams_Utility_Pvt.get_lookup_meaning( 'AMS_SYS_ARC_QUALIFIER',
1840                                       p_activity_type,
1841                                       l_return_status,
1842                                       l_appr_meaning
1843                                      );
1844   IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS  THEN
1845      Fnd_Message.Set_Name('AMS','AMS_BAD_APPROVAL_OBJECT_TYPE');
1846      Fnd_Msg_Pub.ADD;
1847      RAISE Fnd_Api.G_EXC_ERROR;
1848   END IF;
1849 
1850   --  set description of activity
1851   Wf_Engine.SetItemAttrText(itemtype =>  itemtype ,
1852                             itemkey  =>  itemkey,
1853                             aname    =>  'AMS_APPROVAL_OBJECT_MEANING',
1854                             avalue   =>  l_appr_meaning  );
1855 
1856   Wf_Engine.SetItemAttrText(itemtype =>  itemtype ,
1857                             itemkey  =>  itemkey,
1858                             aname    =>  'AMS_APPROVAL_TYPE',
1859                             avalue   =>  p_approval_type  );
1860 
1861   -- Setting up the role
1862   Get_User_Role(p_user_id              => p_requester_userid ,
1863                 x_role_name            => l_requester_role,
1864                 x_role_display_name    => l_display_name,
1865                 x_return_status        => l_return_status);
1866 
1867   IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS  THEN
1868      RAISE Fnd_Api.G_EXC_ERROR;
1869   END IF;
1870 
1871 
1872   Wf_Engine.SetItemAttrText(itemtype    =>  itemtype,
1873                             itemkey     =>  itemkey,
1874                             aname       =>  'AMS_REQUESTER',
1875                             avalue      =>  l_requester_role  );
1876 
1877 
1878 
1879    Wf_Engine.SetItemOwner(itemtype    => itemtype,
1880                           itemkey     => itemkey,
1881                           owner       => l_requester_role);
1882 
1883 
1884    -- Start the Process
1885    Wf_Engine.StartProcess (itemtype       => itemtype,
1886                             itemkey       => itemkey);
1887 
1888 
1889    /* 11.5.9 Approval Details Enhancement */
1890 
1891    /* Create the Submitted row in ams_approval_history */
1892 
1893    l_appr_hist_rec.object_id := p_activity_id;
1894    l_appr_hist_rec.object_type_code := p_activity_type;
1895    l_appr_hist_rec.sequence_num := 0;
1896    l_appr_hist_rec.object_version_num := p_object_version_number;
1897    l_appr_hist_rec.action_code := 'SUBMITTED';
1898    l_appr_hist_rec.action_date := sysdate;
1899    l_appr_hist_rec.approver_id := p_requester_userid;
1900    l_appr_hist_rec.note := p_notes_from_requester;
1901    l_appr_hist_rec.approval_type := p_approval_type;
1902    l_appr_hist_rec.approver_type := 'USER'; -- User always submits
1903    --
1904    AMS_Appr_Hist_PVT.Create_Appr_Hist(
1905        p_api_version_number => 1.0,
1906        p_init_msg_list      => FND_API.G_FALSE,
1907        p_commit             => FND_API.G_FALSE,
1908        p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
1909        x_return_status      => l_return_status,
1910        x_msg_count          => l_msg_count,
1911        x_msg_data           => l_msg_data,
1912        p_appr_hist_rec      => l_appr_hist_rec
1913         );
1914 
1915    IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
1916      RAISE Fnd_Api.G_EXC_ERROR;
1917    END IF;
1918 
1919     -- wf_engine.threshold := l_save_threshold ;
1920 
1921  EXCEPTION
1922      WHEN Fnd_Api.G_EXC_ERROR THEN
1923         -- wf_engine.threshold := l_save_threshold ;
1924         Fnd_Msg_Pub.Count_And_Get (
1925           p_encoded => Fnd_Api.G_FALSE,
1926           p_count   => l_msg_count,
1927           p_data    => l_msg_data);
1928         Handle_Err
1929           (p_itemtype          => itemtype   ,
1930            p_itemkey           => itemkey    ,
1931            p_msg_count         => l_msg_count, -- Number of error Messages
1932            p_msg_data          => l_msg_data ,
1933            p_attr_name         => 'AMS_ERROR_MSG',
1934            x_error_msg         => l_error_msg       );
1935         Wf_Core.context ('Ams_Approval_Pvt', 'StartProcess',p_activity_type
1936                        ,p_activity_id ,l_error_msg);
1937         RAISE;
1938      WHEN OTHERS THEN
1939         -- wf_engine.threshold := l_save_threshold ;
1940         Fnd_Msg_Pub.Count_And_Get (
1941                p_encoded => Fnd_Api.G_FALSE,
1942                p_count   => l_msg_count,
1943                p_data    => l_msg_data);
1944         Handle_Err
1945           (p_itemtype          => itemtype   ,
1946            p_itemkey           => itemkey    ,
1947            p_msg_count         => l_msg_count, -- Number of error Messages
1948            p_msg_data          => l_msg_data ,
1949            p_attr_name         => 'AMS_ERROR_MSG',
1950            x_error_msg         => l_error_msg
1951            )               ;
1952         Wf_Core.context ('Ams_Approval_Pvt', 'StartProcess',p_activity_type
1953                        ,p_activity_id ,l_error_msg);
1954         RAISE;
1955 
1956 END StartProcess;
1957 
1958 /*****************************************************************
1959 -- Start of Comments
1960 --
1961 -- NAME
1962 --   set_activity_details
1963 --
1964 -- PURPOSE
1965 -- NOTES
1966 -- HISTORY
1967 -- End of Comments
1968 *****************************************************************/
1969 
1970 PROCEDURE Set_Activity_Details(itemtype     IN  VARCHAR2,
1971                                itemkey      IN  VARCHAR2,
1972                                actid        IN  NUMBER,
1973                                funcmode     IN  VARCHAR2,
1974                                resultout    OUT NOCOPY VARCHAR2) IS
1975 
1976 
1977 l_activity_id           NUMBER;
1978 l_activity_type         VARCHAR2(30);
1979 l_approval_type         VARCHAR2(30);
1980 l_object_details        ObjRecTyp;
1981 l_approval_detail_id    NUMBER;
1982 l_approver_seq          NUMBER;
1983 l_return_status         VARCHAR2(1);
1984 l_msg_count             NUMBER;
1985 l_msg_data              VARCHAR2(4000);
1986 l_error_msg             VARCHAR2(4000);
1987 l_orig_stat_id          NUMBER;
1988 x_resource_id           NUMBER;
1989 l_full_name             VARCHAR2(360); --l_full_name             VARCHAR2(60); --SVEERAVE,04/10/02
1990 l_func_name             VARCHAR2(100);
1991 l_dtail_url             VARCHAR2(200);-- how much
1992 
1993 cursor c_get_function_id (c_func_name varchar2) is
1994  select function_id from fnd_form_functions where function_name = c_func_name ;
1995 
1996 BEGIN
1997   Fnd_Msg_Pub.initialize();
1998   IF (funcmode = 'RUN') THEN
1999      -- get the acitvity id
2000      l_activity_id        := Wf_Engine.GetItemAttrNumber(
2001                                  itemtype => itemtype,
2002                                  itemkey  => itemkey,
2003                                  aname    => 'AMS_ACTIVITY_ID' );
2004 
2005      -- get the activity type
2006      l_activity_type      := Wf_Engine.GetItemAttrText(
2007                                  itemtype => itemtype,
2008                                  itemkey  => itemkey,
2009                                  aname    => 'AMS_ACTIVITY_TYPE' );
2010 
2011      l_approval_type      := Wf_Engine.GetItemAttrText(
2012                                  itemtype => itemtype,
2013                                  itemkey  => itemkey,
2014                                  aname    => 'AMS_APPROVAL_TYPE' );
2015 
2016 
2017      Get_Approval_Details
2018         ( p_activity_id          =>  l_activity_id,
2019           p_activity_type        =>  l_activity_type,
2020           p_approval_type        =>  l_approval_type,
2021           p_act_budget_id        =>  null,
2022           x_object_details       =>  l_object_details,
2023           x_approval_detail_id   =>  l_approval_detail_id,
2024           x_approver_seq         =>  l_approver_seq,
2025           x_return_status        =>  l_return_status
2026         );
2027 
2028      IF l_return_status = Fnd_Api.G_RET_STS_SUCCESS THEN
2029 
2030       IF l_activity_type IN ('CAMP','CSCH') THEN
2031         IF l_activity_type = 'CAMP' THEN
2032           l_func_name := 'AMS_WB_CAMP_DETL';
2033         ELSE
2034           l_func_name := 'AMS_WB_CSCH_UPDATE';
2035         END IF;
2036 /*
2037         open c_get_function_id(l_func_name);
2038         fetch c_get_function_id into l_function_id;
2039         close c_get_function_id;
2040 
2041         l_dtail_url := fnd_run_function.get_run_function_url
2042                    (l_function_id,
2043                         530,
2044                         -1,
2045                          0,
2046                         'objId=' || l_activity_id );
2047 */
2048         l_dtail_url := 'JSP:/OA_HTML/OA.jsp?OAFunc='||l_func_name||'&'||'objId='||l_activity_id;
2049         l_dtail_url := l_dtail_url||'&'||'addBreadCrumb=Y';
2050 
2051         wf_engine.SetItemAttrText( itemtype => itemtype,
2052                                    itemkey  => itemkey,
2053                                    aname    => 'AMS_ACTIVITY_URL',
2054                                    avalue   => l_dtail_url );
2055 
2056       END IF;
2057 
2058         Wf_Engine.SetItemAttrText(itemtype  => itemtype,
2059                                     itemkey   => itemkey,
2060                                     aname     => 'AMS_ACT_NAME',
2061                                     avalue    => l_dtail_url);
2062 
2063         Wf_Engine.SetItemAttrText(itemtype  => itemtype,
2064                                     itemkey   => itemkey,
2065                                     aname     => 'AMS_ACT_NAME',
2066                                     avalue    => l_object_details.name);
2067 
2068         Wf_Engine.SetItemAttrNumber(itemtype  => itemtype,
2069                                     itemkey   => itemkey,
2070                                     aname     => 'AMS_ACTIVITY_DETAIL_ID',
2071                                     avalue    => l_approval_detail_id);
2072         Wf_Engine.SetItemAttrDate(itemtype  => itemtype,
2073                                     itemkey   => itemkey,
2074                                     aname     => 'AMS_START_DATE',
2075                                     avalue    => l_object_details.start_date);
2076         Wf_Engine.SetItemAttrDate(itemtype  => itemtype,
2077                                     itemkey   => itemkey,
2078                                     aname     => 'AMS_END_DATE',
2079                                     avalue    => l_object_details.end_date);
2080         Wf_Engine.SetItemAttrText(itemtype  => itemtype,
2081                                     itemkey   => itemkey,
2082                                     aname     => 'AMS_ACTIVITY_DESCRIPTION',
2083                                     avalue    => l_object_details.description);
2084        -- Changed Priority code to meaning
2085         Wf_Engine.SetItemAttrText(itemtype  => itemtype,
2086                                     itemkey   => itemkey,
2087                                     aname     => 'AMS_PRIORITY',
2088                                     avalue    => l_object_details.priority_desc);
2089         Wf_Engine.SetItemAttrText(itemtype  => itemtype,
2090                                     itemkey   => itemkey,
2091                                     aname     => 'AMS_ACTIVITY_CURRENCY',
2092                                     avalue    => l_object_details.currency);
2093         --
2094         Wf_Engine.SetItemAttrNumber(itemtype  => itemtype,
2095                                     itemkey   => itemkey,
2096                                     aname     => 'AMS_APPROVER_SEQ',
2097                                     avalue    => l_approver_seq);
2098         Wf_Engine.SetItemAttrNumber(itemtype  => itemtype,
2099                                     itemkey   => itemkey,
2100                                     aname     => 'AMS_ACTIVITY_AMOUNT',
2101                                     avalue    =>
2102                                          l_object_details.total_header_amount);
2103             --insert into ams.test_murali (text) values ('source_code ' || l_object_details.source_code);
2104             -- Fix for Bug 2031309. AMS_CURRENCY was being set to source_code
2105           Wf_Engine.SetItemAttrText(itemtype  => itemtype,
2106                                     itemkey   => itemkey,
2107                                     aname     => 'AMS_CURRENCY',
2108                                     avalue    => l_object_details.currency);
2109 
2110           Wf_Engine.SetItemAttrText(itemtype  => itemtype,
2111                                     itemkey   => itemkey,
2112                                     aname     => 'AMS_SOURCE_CODE',
2113                                     avalue    => l_object_details.source_code);
2114 
2115          Wf_Engine.SetItemAttrText(itemtype  => itemtype,
2116                                     itemkey   => itemkey,
2117                                     aname     => 'AMS_PARENT_SOURCE_CODE',
2118                                     avalue    => l_object_details.parent_source_code);
2119 
2120          Wf_Engine.SetItemAttrText(itemtype  => itemtype,
2121                                     itemkey   => itemkey,
2122                                     aname     => 'AMS_PARENT_OBJECT',
2123                                     avalue    => l_object_details.parent_name);
2124 
2125 
2126         Get_User_Name
2127           ( p_user_id            => l_object_details.owner_id,
2128             x_full_name          => l_full_name,
2129             x_return_status      => l_return_status );
2130 
2131         IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
2132 
2133            Fnd_Msg_Pub.Count_And_Get (
2134            p_encoded => Fnd_Api.G_FALSE,
2135            p_count   => l_msg_count,
2136            p_data    => l_msg_data);
2137 
2138            Handle_Err
2139           (p_itemtype          => itemtype   ,
2140            p_itemkey           => itemkey    ,
2141            p_msg_count         => l_msg_count, -- Number of error Messages
2142            p_msg_data          => l_msg_data ,
2143            p_attr_name         => 'AMS_ERROR_MSG',
2144            x_error_msg         => l_error_msg);
2145 
2146            resultout := 'COMPLETE:ERROR';
2147            RETURN;
2148         END IF;
2149 
2150         Wf_Engine.SetItemAttrText(itemtype  => itemtype,
2151                                     itemkey   => itemkey,
2152                                     aname     => 'AMS_ACTIVITY_OWNER',
2153                                     avalue    => l_full_name );
2154 
2155         resultout := 'COMPLETE:SUCCESS';
2156         RETURN;
2157 
2158      ELSE
2159         Fnd_Msg_Pub.Count_And_Get (
2160                p_encoded => Fnd_Api.G_FALSE,
2161                p_count => l_msg_count,
2162                p_data  => l_msg_data
2163           );
2164 
2165         Handle_Err
2166           (p_itemtype          => itemtype   ,
2167            p_itemkey           => itemkey    ,
2168            p_msg_count         => l_msg_count, -- Number of error Messages
2169            p_msg_data          => l_msg_data ,
2170            p_attr_name         => 'AMS_ERROR_MSG',
2171            x_error_msg         => l_error_msg
2172            )               ;
2173 
2174    /*
2175         wf_core.context('Ams_Approval_Pvt','Set_Activity_Details',
2176                         itemtype,itemkey,actid,l_error_msg);  */
2177         -- RAISE FND_API.G_EXC_ERROR;
2178         resultout := 'COMPLETE:ERROR';
2179         RETURN;
2180      END IF;
2181   END IF;
2182 
2183   --
2184   -- CANCEL mode
2185   --
2186   IF (funcmode = 'CANCEL') THEN
2187         resultout := 'COMPLETE:';
2188         RETURN;
2189   END IF;
2190 
2191   --
2192   -- TIMEOUT mode
2193   --
2194   IF (funcmode = 'TIMEOUT') THEN
2195         resultout := 'COMPLETE:';
2196         RETURN;
2197   END IF;
2198   --
2199 
2200 EXCEPTION
2201   WHEN Fnd_Api.G_EXC_ERROR THEN
2202           Fnd_Msg_Pub.Count_And_Get (
2203                p_encoded => Fnd_Api.G_FALSE,
2204                p_count => l_msg_count,
2205                p_data  => l_msg_data
2206           );
2207         Handle_Err
2208           (p_itemtype          => itemtype   ,
2209            p_itemkey           => itemkey    ,
2210            p_msg_count         => l_msg_count, -- Number of error Messages
2211            p_msg_data          => l_msg_data ,
2212            p_attr_name         => 'AMS_ERROR_MSG',
2213            x_error_msg         => l_error_msg
2214            )               ;
2215        resultout := 'COMPLETE:ERROR';
2216       /*wf_core.context('Ams_Approval_Pvt','Set_Activity_Details',
2217                       itemtype,itemkey,actid,funcmode,l_error_msg);
2218       raise; */
2219    WHEN OTHERS THEN
2220         Fnd_Msg_Pub.Count_And_Get (
2221                p_encoded => Fnd_Api.G_FALSE,
2222                p_count => l_msg_count,
2223                p_data  => l_msg_data
2224           );
2225         Handle_Err
2226           (p_itemtype          => itemtype   ,
2227            p_itemkey           => itemkey    ,
2228            p_msg_count         => l_msg_count, -- Number of error Messages
2229            p_msg_data          => l_msg_data ,
2230            p_attr_name         => 'AMS_ERROR_MSG',
2231            x_error_msg         => l_error_msg
2232            )               ;
2233       Wf_Core.context('Ams_Approval_Pvt','Set_Activity_Details',
2234                       itemtype,itemkey,actid,funcmode,l_error_msg);
2235       RAISE;
2236 END Set_Activity_Details ;
2237 
2238 /*============================================================================*/
2239 -- Check_Budget_Lines
2240 /*============================================================================*/
2241 PROCEDURE Check_Budget_Lines( itemtype        IN  VARCHAR2,
2242                               itemkey         IN  VARCHAR2,
2243                               actid           IN  NUMBER,
2244                               funcmode        IN  VARCHAR2,
2245                               resultout       OUT NOCOPY VARCHAR2    )
2246 IS
2247 l_activity_id           NUMBER;
2248 l_activity_type         VARCHAR2(30);
2249 l_dummy_char            VARCHAR2(30);
2250 l_msg_count             NUMBER;
2251 l_msg_data              VARCHAR2(4000);
2252 l_error_msg                VARCHAR2(4000);
2253 
2254   CURSOR c_check_lines IS
2255   SELECT status_code
2256     FROM ozf_act_budgets
2257    WHERE arc_act_budget_used_by = l_activity_type
2258      AND act_budget_used_by_id = l_activity_id
2259      AND status_code NOT IN ('APPROVED','REJECTED','CANCELLED','PENDING')
2260      AND ROWNUM < 2;
2261 BEGIN
2262   Fnd_Msg_Pub.initialize();
2263 
2264   --
2265   -- RUN mode
2266   --
2267   IF (funcmode = 'RUN') THEN
2268      -- get the acitvity id
2269      l_activity_id        := Wf_Engine.GetItemAttrNumber(
2270                                  itemtype => itemtype,
2271                                  itemkey  => itemkey,
2272                                  aname    => 'AMS_ACTIVITY_ID' );
2273 
2274      -- get the activity type
2275      l_activity_type      := Wf_Engine.GetItemAttrText(
2276                                  itemtype => itemtype,
2277                                  itemkey  => itemkey,
2278                                  aname    => 'AMS_ACTIVITY_TYPE' );
2279 
2280       OPEN  c_check_lines;
2281       FETCH c_check_lines INTO l_dummy_char ;
2282       IF l_dummy_char IS NULL  THEN
2283          resultout := 'COMPLETE:LINES_APPROVED' ;
2284       ELSE
2285          resultout := 'COMPLETE:LINES_NOT_APPROVED' ;
2286       END IF;
2287       CLOSE c_check_lines;
2288   END IF;
2289 
2290   --
2291   -- CANCEL mode
2292   --
2293   IF (funcmode = 'CANCEL') THEN
2294         resultout := 'COMPLETE:';
2295         RETURN;
2296   END IF;
2297 
2298   --
2299   -- TIMEOUT mode
2300   --
2301   IF (funcmode = 'TIMEOUT') THEN
2302         resultout := 'COMPLETE:';
2303         RETURN;
2304   END IF;
2305   --
2306 
2307 
2308 EXCEPTION
2309   WHEN OTHERS THEN
2310         Fnd_Msg_Pub.Count_And_Get (
2311                p_encoded => Fnd_Api.G_FALSE,
2312                p_count => l_msg_count,
2313                p_data  => l_msg_data
2314           );
2315         Handle_Err
2316           (p_itemtype          => itemtype   ,
2317            p_itemkey           => itemkey    ,
2318            p_msg_count         => l_msg_count, -- Number of error Messages
2319            p_msg_data          => l_msg_data ,
2320            p_attr_name         => 'AMS_ERROR_MSG',
2321            x_error_msg         => l_error_msg
2322            )               ;
2323     Wf_Core.context('Ams_Approval_Pvt',
2324                     'Check_Budget_Lines',
2325                     itemtype, itemkey,TO_CHAR(actid),l_error_msg);
2326     RAISE;
2327 END Check_Budget_Lines;
2328 
2329 /*============================================================================*/
2330 -- Get_Approval_Rules
2331 /*============================================================================*/
2332 PROCEDURE Get_Approval_Rules( p_activity_type   IN  VARCHAR2,
2333                               p_activity_id     IN  NUMBER,
2334                               p_activity_amount IN  NUMBER,
2335                               x_approved_flag   OUT NOCOPY VARCHAR2)
2336 IS
2337 l_amount         NUMBER;
2338 l_percent        NUMBER;
2339 l_offer_type     VARCHAR2(30);
2340 CURSOR c_offer_type IS
2341   SELECT offer_type
2342     FROM ozf_offers
2343    WHERE qp_list_header_id = p_activity_id;
2344 BEGIN
2345   Fnd_Msg_Pub.initialize();
2346   -- Change for Offers Team for Bug 2861942
2347 
2348   IF p_activity_type = 'OFFR' THEN
2349      OPEN c_offer_type;
2350      FETCH c_offer_type INTO l_offer_type;
2351      IF c_offer_type%NOTFOUND THEN
2352        CLOSE c_offer_type;
2353      END IF;
2354      CLOSE c_offer_type;
2355   END IF;
2356 
2357   IF l_offer_type IN ('SCAN_DATA','LUMPSUM') THEN
2358      l_percent := 1;
2359   ELSE
2360   -- Why is it NVL(profile,0)/100 should it not be NVL(profile,100)/100
2361   l_percent := NVL(Fnd_Profile.Value('AMS_APPROVAL_CUTOFF_PERCENT'),0)/100 ;
2362   END IF;
2363 
2364   Get_line_amounts
2365          ( p_activity_id         => p_activity_id,
2366            p_activity_type       => p_activity_type,
2367            p_act_budget_id       => null,
2368            x_amount              => l_amount);
2369   IF ( NVL(l_amount,0)   >=  NVL(p_activity_amount,0) * l_percent ) THEN
2370        x_approved_flag  := Fnd_Api.G_TRUE;
2371   ELSE
2372      x_approved_flag  := Fnd_Api.G_FALSE ;
2373   END IF;
2374 END Get_Approval_Rules ;
2375 /*============================================================================*/
2376 PROCEDURE local_dummy IS
2377 l_dummy              DATE ;
2378 l_dummy1             CHAR  := 'y';
2379 BEGIN
2380 l_dummy := SYSDATE;
2381 LOOP
2382      SELECT 'x'
2383      INTO l_dummy1
2384      FROM dual
2385      WHERE (( SYSDATE  - l_dummy )*(24*60))  < .01 ;
2386 END LOOP;
2387 EXCEPTION
2388   WHEN OTHERS THEN
2389       NULL;
2390 END local_dummy;
2391 /*============================================================================*/
2392 --Check_Approval_rules
2393 /*============================================================================*/
2394 PROCEDURE Check_Approval_rules( itemtype        IN  VARCHAR2,
2395                                 itemkey         IN  VARCHAR2,
2396                                 actid           IN  NUMBER,
2397                                 funcmode        IN  VARCHAR2,
2398                                 resultout       OUT NOCOPY VARCHAR2    )
2399 
2400 IS
2401 l_activity_id           NUMBER;
2402 l_activity_amount       NUMBER;
2403 l_activity_type         VARCHAR2(30);
2404 l_approved_flag         VARCHAR2(1);
2405 l_msg_count             NUMBER;
2406 l_msg_data              VARCHAR2(4000);
2407 l_error_msg             VARCHAR2(4000);
2408 l_save_threshold        NUMBER := Wf_Engine.threshold;
2409 BEGIN
2410   Fnd_Msg_Pub.initialize();
2411   IF (funcmode = 'RUN') THEN
2412     Wf_Engine.threshold := -1;
2413      -- get the acitvity id
2414      l_activity_id        := Wf_Engine.GetItemAttrNumber(
2415                                  itemtype => itemtype,
2416                                  itemkey  => itemkey,
2417                                  aname    => 'AMS_ACTIVITY_ID' );
2418 
2419      -- get the activity type
2420      l_activity_type      := Wf_Engine.GetItemAttrText(
2421                                  itemtype => itemtype,
2422                                  itemkey  => itemkey,
2423                                  aname    => 'AMS_ACTIVITY_TYPE' );
2424 
2425      l_activity_amount    := Wf_Engine.GetItemAttrNumber(
2426                                itemtype => itemtype,
2427                                itemkey  => itemkey,
2428                                aname    => 'AMS_ACTIVITY_AMOUNT' );
2429 
2430      Get_Approval_Rules( p_activity_type      => l_activity_type ,
2431                          p_activity_id        => l_activity_id,
2432                          p_activity_amount    => l_activity_amount,
2433                          x_approved_flag      => l_approved_flag );
2434 
2435      IF l_approved_flag = Fnd_Api.G_TRUE THEN
2436         resultout := 'COMPLETE:SUCCESS';
2437      ELSE
2438         Fnd_Msg_Pub.Count_And_Get (
2439                p_encoded => Fnd_Api.G_FALSE,
2440                p_count => l_msg_count,
2441                p_data  => l_msg_data
2442           );
2443         Handle_Err
2444           (p_itemtype          => itemtype   ,
2445            p_itemkey           => itemkey    ,
2446            p_msg_count         => l_msg_count, -- Number of error Messages
2447            p_msg_data          => l_msg_data , -- Number of error Messages
2448            p_attr_name         => 'AMS_ERROR_MSG',
2449            x_error_msg         => l_error_msg
2450            )               ;
2451         resultout := 'COMPLETE:FAILURE';
2452      END IF;
2453     Wf_Engine.threshold := l_save_threshold ;
2454 
2455   END IF;
2456 
2457   --
2458   -- CANCEL mode
2459   --
2460   IF (funcmode = 'CANCEL') THEN
2461         resultout := 'COMPLETE:';
2462         RETURN;
2463   END IF;
2464 
2465   --
2466   -- TIMEOUT mode
2467   --
2468   IF (funcmode = 'TIMEOUT') THEN
2469         resultout := 'COMPLETE:';
2470         RETURN;
2471   END IF;
2472 EXCEPTION
2473 WHEN OTHERS THEN
2474         Fnd_Msg_Pub.Count_And_Get (
2475                p_encoded => Fnd_Api.G_FALSE,
2476                p_count => l_msg_count,
2477                p_data  => l_msg_data
2478           );
2479         Handle_Err
2480           (p_itemtype          => itemtype   ,
2481            p_itemkey           => itemkey    ,
2482            p_msg_count         => l_msg_count, -- Number of error Messages
2483            p_msg_data          => l_msg_data ,
2484            p_attr_name         => 'AMS_ERROR_MSG',
2485            x_error_msg         => l_error_msg
2486            )               ;
2487     Wf_Core.context('Ams_Approval_Pvt',
2488                     'check_approval_rules',
2489                     itemtype, itemkey,TO_CHAR(actid),l_error_msg);
2490     RAISE;
2491   --
2492 
2493 END Check_Approval_Rules ;
2494 
2495 /*============================================================================*/
2496 -- Prepare Doc
2497 /*============================================================================*/
2498 
2499 PROCEDURE Prepare_Doc( itemtype        IN  VARCHAR2,
2500                        itemkey         IN  VARCHAR2,
2501                        actid           IN  NUMBER,
2502                        funcmode        IN  VARCHAR2,
2503                        resultout       OUT NOCOPY VARCHAR2 )
2504 IS
2505 BEGIN
2506   Fnd_Msg_Pub.initialize();
2507   IF (funcmode = 'RUN') THEN
2508      resultout := 'COMPLETE:SUCCESS';
2509   END IF;
2510 
2511   --
2512   -- CANCEL mode
2513   --
2514   IF (funcmode = 'CANCEL') THEN
2515         resultout := 'COMPLETE:';
2516         RETURN;
2517   END IF;
2518 
2519   --
2520   -- TIMEOUT mode
2521   --
2522   IF (funcmode = 'TIMEOUT') THEN
2523         resultout := 'COMPLETE:';
2524         RETURN;
2525   END IF;
2526   --
2527 
2528 END;
2529 
2530 /*============================================================================*/
2531 -- Set_Approver_details
2532 /*============================================================================*/
2533 
2534 PROCEDURE Set_Approver_Details( itemtype        IN  VARCHAR2,
2535                                 itemkey         IN  VARCHAR2,
2536                                 actid           IN  NUMBER,
2537                                 funcmode        IN  VARCHAR2,
2538                                 resultout       OUT NOCOPY VARCHAR2 )
2539 IS
2540 l_current_seq             NUMBER;
2541 l_approval_detail_id      NUMBER;
2542 l_approver_id             NUMBER;
2543 l_approver                VARCHAR2(320);--l_approver   VARCHAR2(30); --SVEERAVE,04/10/02
2544 l_prev_approver           VARCHAR2(320);--l_prev_approver  VARCHAR2(30); --SVEERAVE,04/10/02
2545 l_approver_display_name   VARCHAR2(360);
2546 l_notification_type       VARCHAR2(30);
2547 l_notification_timeout    NUMBER;
2548 l_approver_type           VARCHAR2(30);
2549 l_role_name               VARCHAR2(100); --l_role_name  VARCHAR2(30);--SVEERAVE,04/10/02
2550 l_prev_role_name          VARCHAR2(100); --l_prev_role_name  VARCHAR2(30);--SVEERAVE,04/10/02
2551 l_object_approver_id      NUMBER;
2552 l_return_status           VARCHAR2(1);
2553 l_msg_count               NUMBER;
2554 l_msg_data                VARCHAR2(4000);
2555 l_error_msg               VARCHAR2(4000);
2556 l_pkg_name                VARCHAR2(80);
2557 l_proc_name               VARCHAR2(80);
2558 l_appr_id                 NUMBER;
2559 dml_str                   VARCHAR2(2000);
2560 --11.5.9
2561 l_approval_type           VARCHAR2(30);
2562 l_activity_type           VARCHAR2(30);
2563 l_activity_id             NUMBER;
2564 l_object_details          ObjRecTyp;
2565 l_version                 NUMBER;
2566 l_appr_seq                NUMBER;
2567 l_appr_type               VARCHAR2(30);
2568 l_obj_appr_id             NUMBER;
2569 l_appr_hist_rec           AMS_Appr_Hist_PVT.appr_hist_rec_type;
2570 
2571 CURSOR c_API_Name(rule_id_in IN NUMBER) IS
2572      SELECT package_name, procedure_name
2573        FROM ams_object_rules_b
2574       WHERE OBJECT_RULE_ID = rule_id_in;
2575 
2576 CURSOR c_approver(rule_id IN NUMBER) IS
2577      SELECT approver_seq, approver_type, object_approver_id
2578        FROM ams_approvers
2579       WHERE ams_approval_detail_id = rule_id
2580        AND  active_flag = 'Y'
2581        AND  TRUNC(SYSDATE) BETWEEN TRUNC(NVL(start_date_active,SYSDATE -1 ))
2582        AND TRUNC(NVL(end_date_active,SYSDATE + 1));
2583 BEGIN
2584   Fnd_Msg_Pub.initialize();
2585   IF (funcmode = 'RUN') THEN
2586      l_approval_detail_id := Wf_Engine.GetItemAttrNumber(
2587                                  itemtype => itemtype,
2588                                  itemkey  => itemkey,
2589                                  aname    => 'AMS_ACTIVITY_DETAIL_ID' );
2590 
2591      l_current_seq        := Wf_Engine.GetItemAttrText(
2592                                  itemtype => itemtype,
2593                                  itemkey  => itemkey,
2594                                  aname    => 'AMS_APPROVER_SEQ' );
2595      -- 11.5.9
2596 
2597      l_activity_id := Wf_Engine.GetItemAttrNumber(
2598                                  itemtype => itemtype,
2599                                  itemkey  => itemkey,
2600                                  aname    => 'AMS_ACTIVITY_ID' );
2601 
2602      l_activity_type      := Wf_Engine.GetItemAttrText(
2603                                  itemtype => itemtype,
2604                                  itemkey  => itemkey,
2605                                  aname    => 'AMS_ACTIVITY_TYPE' );
2606 
2607      l_version            := Wf_Engine.GetItemAttrNumber(
2608                                  itemtype => itemtype,
2609                                  itemkey => itemkey,
2610                                  aname   => 'AMS_OBJECT_VERSION_NUMBER' );
2611 
2612      l_approval_type      := Wf_Engine.GetItemAttrText(
2613                                  itemtype => itemtype,
2614                                  itemkey => itemkey,
2615                                  aname   => 'AMS_APPROVAL_TYPE' );
2616 
2617      -- 11.5.9
2618       Get_Approver_Info
2619           ( p_approval_detail_id   =>  l_approval_detail_id,
2620             p_current_seq          =>  l_current_seq ,
2621             x_approver_id          =>  l_approver_id,
2622             x_approver_type        =>  l_approver_type,
2623             x_role_name            =>  l_role_name ,
2624             x_object_approver_id   =>  l_object_approver_id,
2625             x_notification_type    =>  l_notification_type,
2626             x_notification_timeout =>  l_notification_type,
2627             x_return_status        =>  l_return_status);
2628 
2629      IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
2630         -- Bug 2745031
2631         RAISE Fnd_Api.G_EXC_ERROR;
2632      END IF;
2633 
2634        -- Bug 2729108 Fix
2635         IF l_current_seq = 1 OR
2636            Is_Min_Sequence(l_approval_detail_id, l_current_seq) THEN
2637 
2638          -- Get all the obj attributes once for inserts
2639          -- Set Record Attributes that won't change for each approver
2640          l_appr_hist_rec.object_id          := l_activity_id;
2641          l_appr_hist_rec.object_type_code   := l_activity_type;
2642          l_appr_hist_rec.object_version_num := l_version;
2643          l_appr_hist_rec.action_code        := 'OPEN';
2644          l_appr_hist_rec.approval_type      := l_approval_type;
2645          l_appr_hist_rec.approval_detail_id := l_approval_detail_id;
2646 
2647          OPEN c_approver(l_approval_detail_id);
2648          LOOP
2649          FETCH c_approver INTO l_appr_seq, l_appr_type, l_obj_appr_id;
2650          EXIT WHEN c_approver%NOTFOUND;
2651 
2652          -- Set Record Attributes that will change for each approver
2653          l_appr_hist_rec.sequence_num  := l_appr_seq;
2654          l_appr_hist_rec.approver_type := l_appr_type;
2655          l_appr_hist_rec.approver_id   := l_obj_appr_id;
2656 
2657          AMS_Appr_Hist_PVT.Create_Appr_Hist(
2658             p_api_version_number => 1.0,
2659             p_init_msg_list      => FND_API.G_FALSE,
2660             p_commit             => FND_API.G_FALSE,
2661             p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
2662             x_return_status      => l_return_status,
2663             x_msg_count          => l_msg_count,
2664             x_msg_data           => l_msg_data,
2665             p_appr_hist_rec      => l_appr_hist_rec
2666             );
2667 
2668         IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
2669            Fnd_Msg_Pub.Count_And_Get (
2670               p_encoded => Fnd_Api.G_FALSE,
2671               p_count => l_msg_count,
2672               p_data  => l_msg_data
2673              );
2674             Handle_Err
2675              (p_itemtype         => itemtype   ,
2676               p_itemkey          => itemkey    ,
2677               p_msg_count        => l_msg_count,
2678               p_msg_data         => l_msg_data ,
2679               p_attr_name        => 'AMS_ERROR_MSG',
2680               x_error_msg         => l_error_msg);
2681 
2682               resultout := 'COMPLETE:ERROR';
2683         END IF;
2684 
2685          END LOOP;
2686          CLOSE c_approver;
2687       END IF;
2688 
2689   IF l_return_status = Fnd_Api.G_RET_STS_SUCCESS THEN
2690     IF (l_approver_type = 'FUNCTION') THEN -- was l_role_name Bug 2346128
2691       OPEN c_API_Name(l_object_approver_id);
2692       FETCH c_API_Name INTO l_pkg_name, l_proc_name;
2693       IF (c_Api_Name%FOUND) THEN
2694         dml_str := 'BEGIN ' || l_pkg_name||'.'||l_proc_name||'(:itemtype,:itemkey,:appr_id, :l_return_stat); END;';
2695         EXECUTE IMMEDIATE dml_str USING IN itemtype,IN itemkey, OUT l_appr_id, OUT l_return_status;
2696 
2697         IF(l_return_status = 'S') THEN
2698           l_object_approver_id := l_appr_id;
2699         ELSE
2700           Fnd_Msg_Pub.Count_And_Get (
2701             p_encoded => Fnd_Api.G_FALSE,
2702             p_count => l_msg_count,
2703             p_data  => l_msg_data
2704           );
2705           Handle_Err
2706           (p_itemtype         => itemtype   ,
2707           p_itemkey           => itemkey    ,
2708           p_msg_count         => l_msg_count, -- Number of error Messages
2709           p_msg_data          => l_msg_data , -- Number of error Messages
2710           p_attr_name         => 'AMS_ERROR_MSG',
2711           x_error_msg         => l_error_msg
2712           )               ;
2713           resultout := 'COMPLETE:ERROR';
2714         END IF;
2715       END IF;
2716       CLOSE c_API_Name;
2717     END IF;
2718 
2719     Get_User_Role(p_user_id      => l_object_approver_id ,
2720           x_role_name            => l_approver,
2721           x_role_display_name    => l_approver_display_name,
2722           x_return_status        => l_return_status);
2723 
2724     IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS  THEN
2725       RAISE Fnd_Api.G_EXC_ERROR;
2726     END IF;
2727 
2728     l_prev_approver  := Wf_Engine.GetItemAttrText(
2729                 itemtype => itemtype,
2730                 itemkey  => itemkey,
2731                 aname    => 'AMS_APPR_USERNAME' );
2732 
2733         /*
2734           wf_engine.SetItemAttrText(  itemtype => itemtype,
2735                                     itemkey  => itemkey,
2736                                     aname    => 'AMS_PREV_APPR_USERNAME',
2737                                     avalue   => l_prev_approver);  */
2738 
2739           Wf_Engine.SetItemAttrText(  itemtype => itemtype,
2740                                     itemkey  => itemkey,
2741                                     aname    => 'AMS_APPR_USERNAME',
2742                                     avalue   => l_approver);
2743 
2744           Wf_Engine.SetItemAttrText(  itemtype => itemtype,
2745                                     itemkey  => itemkey,
2746                                     aname    => 'AMS_APPR_DISPLAY_NAME',
2747                                     avalue   => l_approver_display_name);
2748 
2749           l_prev_role_name  := Wf_Engine.GetItemAttrText(
2750                                  itemtype => itemtype,
2751                                  itemkey  => itemkey,
2752                                  aname    => 'AMS_APPROVAL_ROLE' );
2753 
2754           Wf_Engine.SetItemAttrText(  itemtype => itemtype,
2755                                     itemkey  => itemkey,
2756                                     aname    => 'AMS_APPROVAL_ROLE',
2757                                     avalue   => l_role_name);
2758 
2759         /*
2760           wf_engine.SetItemAttrText(  itemtype => itemtype,
2761                                     itemkey  => itemkey,
2762                                     aname    => 'AMS_PREV_APPROVER_ROLE',
2763                                     avalue   => l_prev_role_name);
2764          */
2765 
2766           Wf_Engine.SetItemAttrNumber(itemtype => itemtype,
2767                                     itemkey  => itemkey,
2768                                     aname    => 'AMS_APPROVER_ID',
2769                                     avalue   => l_object_approver_id);
2770 
2771 
2772         Wf_Engine.SetItemAttrText(  itemtype => itemtype,
2773                                     itemkey  => itemkey,
2774                                     aname    => 'AMS_NOTIFICATION_TYPE',
2775                                     avalue   => l_notification_type);
2776 
2777           Wf_Engine.SetItemAttrNumber(itemtype => itemtype,
2778                                     itemkey  => itemkey,
2779                                     aname    => 'AMS_NOTIFICATION_TIMEOUT',
2780                                     avalue   => l_notification_timeout);
2781 
2782           -- 11.5.9 Update the 'Open' row to 'Pending'
2783 
2784 
2785           l_appr_hist_rec.object_id          := l_activity_id;
2786           l_appr_hist_rec.object_type_code   := l_activity_type;
2787           l_appr_hist_rec.object_version_num := l_version;
2788           l_appr_hist_rec.action_code        := 'PENDING';
2789           l_appr_hist_rec.approval_type      := l_approval_type;
2790           l_appr_hist_rec.approver_type      := l_approver_type;
2791           l_appr_hist_rec.sequence_num       := l_current_seq;
2792           l_appr_hist_rec.approver_id        := l_object_approver_id;
2793 
2794           AMS_Appr_Hist_PVT.Update_Appr_Hist(
2795              p_api_version_number => 1.0,
2796              p_init_msg_list      => FND_API.G_FALSE,
2797              p_commit             => FND_API.G_FALSE,
2798              p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
2799              x_return_status      => l_return_status,
2800              x_msg_count          => l_msg_count,
2801              x_msg_data           => l_msg_data,
2802              p_appr_hist_rec      => l_appr_hist_rec
2803              );
2804 
2805 
2806            IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
2807              RAISE Fnd_Api.G_EXC_ERROR;
2808            END IF;
2809 
2810         resultout := 'COMPLETE:SUCCESS';
2811      ELSE
2812         RAISE Fnd_Api.G_EXC_ERROR;
2813    END IF;
2814   END IF;
2815 
2816   --
2817   -- CANCEL mode
2818   --
2819   IF (funcmode = 'CANCEL') THEN
2820         resultout := 'COMPLETE:';
2821         RETURN;
2822   END IF;
2823 
2824   --
2825   -- TIMEOUT mode
2826   --
2827   IF (funcmode = 'TIMEOUT') THEN
2828         resultout := 'COMPLETE:';
2829         RETURN;
2830   END IF;
2831  EXCEPTION
2832      WHEN Fnd_Api.G_EXC_ERROR THEN
2833         Fnd_Msg_Pub.Count_And_Get (
2834                p_encoded => Fnd_Api.G_FALSE,
2835                p_count => l_msg_count,
2836                p_data  => l_msg_data
2837           );
2838         Handle_Err
2839           (p_itemtype          => itemtype   ,
2840            p_itemkey           => itemkey    ,
2841            p_msg_count         => l_msg_count,
2842            p_msg_data          => l_msg_data ,
2843            p_attr_name         => 'AMS_ERROR_MSG',
2844            x_error_msg         => l_error_msg
2845            )               ;
2846        resultout := 'COMPLETE:ERROR';
2847     /*wf_core.context('Ams_Approval_Pvt',
2848                     'set_approval_rules',
2849                     itemtype, itemkey,to_char(actid),l_error_msg);
2850          RAISE; */
2851  WHEN OTHERS THEN
2852         Fnd_Msg_Pub.Count_And_Get (
2853                p_encoded => Fnd_Api.G_FALSE,
2854                p_count => l_msg_count,
2855                p_data  => l_msg_data
2856           );
2857         Handle_Err
2858           (p_itemtype          => itemtype   ,
2859            p_itemkey           => itemkey    ,
2860            p_msg_count         => l_msg_count, -- Number of error Messages
2861            p_msg_data          => l_msg_data ,
2862            p_attr_name         => 'AMS_ERROR_MSG',
2863            x_error_msg         => l_error_msg
2864            )               ;
2865     Wf_Core.context('Ams_Approval_Pvt',
2866                     'set_approval_rules',
2867                     itemtype, itemkey,TO_CHAR(actid),l_error_msg);
2868     RAISE;
2869   --
2870 
2871 END;
2872 
2873 /*============================================================================*/
2874 --Set_Further_Approvals
2875 /*============================================================================*/
2876 PROCEDURE Set_Further_Approvals( itemtype        IN  VARCHAR2,
2877                                  itemkey         IN  VARCHAR2,
2878                                  actid           IN  NUMBER,
2879                                  funcmode        IN  VARCHAR2,
2880                                  resultout       OUT NOCOPY VARCHAR2 )
2881 IS
2882 l_current_seq             NUMBER;
2883 l_next_seq                NUMBER;
2884 l_approval_detail_id      NUMBER;
2885 l_required_flag           VARCHAR2(1);
2886 l_approver_id             NUMBER;
2887 l_note                    VARCHAR2(4000);
2888 l_all_note                VARCHAR2(4000);
2889 l_activity_type           VARCHAR2(30);
2890 l_activity_id             NUMBER;
2891 l_msg_count               NUMBER;
2892 l_msg_data                VARCHAR2(4000);
2893 l_return_status           VARCHAR2(1);
2894 l_error_msg               VARCHAR2(4000);
2895 -- 11.5.9
2896 l_version                 NUMBER;
2897 l_approval_type           VARCHAR2(30);
2898 l_appr_hist_rec           AMS_Appr_Hist_Pvt.Appr_Hist_Rec_Type;
2899 l_new_approver_id         NUMBER;
2900 BEGIN
2901   Fnd_Msg_Pub.initialize();
2902   IF (funcmode = 'RUN') THEN
2903      l_approval_detail_id := Wf_Engine.GetItemAttrNumber(
2904                                  itemtype => itemtype,
2905                                  itemkey  => itemkey,
2906                                  aname    => 'AMS_ACTIVITY_DETAIL_ID' );
2907 
2908      l_current_seq        := Wf_Engine.GetItemAttrText(
2909                                  itemtype => itemtype,
2910                                  itemkey  => itemkey,
2911                                  aname    => 'AMS_APPROVER_SEQ' );
2912      -- get the activity note
2913      l_note            := Wf_Engine.GetItemAttrText(
2914                                  itemtype => itemtype,
2915                                  itemkey  => itemkey,
2916                                  aname    => 'AMS_NOTE' );
2917 
2918      l_approver_id := Wf_Engine.GetItemAttrNumber(
2919                                  itemtype => itemtype,
2920                                  itemkey  => itemkey,
2921                                  aname    => 'AMS_APPROVER_ID' );
2922 
2923      l_activity_id := Wf_Engine.GetItemAttrNumber(
2924                                  itemtype => itemtype,
2925                                  itemkey  => itemkey,
2926                                  aname    => 'AMS_ACTIVITY_ID' );
2927 
2928      l_activity_type      := Wf_Engine.GetItemAttrText(
2929                                  itemtype => itemtype,
2930                                  itemkey  => itemkey,
2931                                  aname    => 'AMS_ACTIVITY_TYPE' );
2932 
2933      -- put this later
2934      IF l_note IS NOT NULL THEN
2935        Update_Note(p_activity_type => l_activity_type,
2936                    p_activity_id   => l_activity_id,
2937                    p_note          => l_note,
2938                    p_user          => l_approver_id,
2939                    x_msg_count     => l_msg_count,
2940                    x_msg_data     =>  l_msg_data,
2941                    x_return_status => l_return_status);
2942      END IF;
2943 
2944      IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS  THEN
2945              RAISE Fnd_Api.G_EXC_ERROR;
2946      END IF;
2947       -- Added for 11.5.9
2948       l_version := Wf_Engine.GetItemAttrNumber(
2949                               itemtype => itemtype,
2950                               itemkey => itemkey,
2951                               aname   => 'AMS_OBJECT_VERSION_NUMBER' );
2952 
2953       l_approval_type := Wf_Engine.GetItemAttrText(
2954                               itemtype => itemtype,
2955                               itemkey => itemkey,
2956                               aname   => 'AMS_APPROVAL_TYPE' );
2957    -- Commented for Bug 3150550
2958      -- Start of addition for forward/reassign notification
2959 /*
2960      Check_Reassigned (itemtype => itemtype,
2961                        itemkey  => itemkey,
2962                        x_approver_id => l_new_approver_id,
2963                        x_return_status => l_return_status);
2964 
2965 
2966      IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
2967        RAISE Fnd_Api.G_EXC_ERROR;
2968      END IF;
2969 
2970 
2971      IF l_new_approver_id IS NOT NULL THEN
2972        l_approver_id := l_new_approver_id;
2973      END IF;
2974 
2975     -- End of addition for forward/re-assign notification
2976     --End of Comment
2977 */
2978      -- Added in case of failure of get_Approval_details
2979      -- in case of Budget Approval. Will cause missing
2980      -- update target
2981      IF l_approver_id IS NOT NULL THEN
2982 
2983 
2984          -- update the record from 'PENDING' to 'APPROVED'
2985           l_appr_hist_rec.object_id          := l_activity_id;
2986           l_appr_hist_rec.object_type_code   := l_activity_type;
2987           l_appr_hist_rec.object_version_num := l_version;
2988           l_appr_hist_rec.action_code        := 'APPROVED';
2989           l_appr_hist_rec.approval_type      := l_approval_type;
2990           l_appr_hist_rec.sequence_num       := l_current_seq;
2991           l_appr_hist_rec.approver_id        := l_approver_id;
2992           l_appr_hist_rec.note               := l_note;
2993           l_appr_hist_rec.action_date        := sysdate;
2994 
2995           AMS_Appr_Hist_PVT.Update_Appr_Hist(
2996              p_api_version_number => 1.0,
2997              p_init_msg_list      => FND_API.G_FALSE,
2998              p_commit             => FND_API.G_FALSE,
2999              p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
3000              x_return_status      => l_return_status,
3001              x_msg_count          => l_msg_count,
3002              x_msg_data           => l_msg_data,
3003              p_appr_hist_rec      => l_appr_hist_rec
3004              );
3005 
3006           IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
3007              RAISE Fnd_Api.G_EXC_ERROR;
3008           END IF;
3009 
3010       END IF;
3011 
3012      -- get all the activity notes
3013            l_all_note    := Wf_Engine.GetItemAttrText(
3014                                  itemtype => itemtype,
3015                                  itemkey  => itemkey,
3016                                  aname    => 'AMS_ALL_NOTE' );
3017 
3018      -- NOTE another option is to get them from database and display
3019      -- issue : cannot distinguish from notes created by activities or budget lines
3020      -- option : can insert a carriage return when concatenating notes
3021      l_all_note := l_all_note ||l_note;
3022 
3023      Wf_Engine.SetItemAttrText(
3024                                  itemtype => itemtype,
3025                                  itemkey  => itemkey,
3026                                  aname    => 'AMS_ALL_NOTE' ,
3027                                  avalue   => l_all_note ) ;
3028 
3029      -- set the note to null
3030      l_note := NULL;
3031      Wf_Engine.SetItemAttrText(
3032                                  itemtype => itemtype,
3033                                  itemkey  => itemkey,
3034                                  aname    => 'AMS_NOTE' ,
3035                                  avalue   => l_note ) ;
3036 
3037      Check_Approval_Required
3038              ( p_approval_detail_id       => l_approval_detail_id,
3039                p_current_seq              => l_current_seq,
3040                x_next_seq                 => l_next_seq,
3041                x_required_flag            => l_required_flag);
3042 
3043      IF l_next_seq IS NOT NULL THEN
3044           Wf_Engine.SetItemAttrNumber(itemtype => itemtype,
3045                                     itemkey  => itemkey,
3046                                     aname    => 'AMS_APPROVER_SEQ',
3047                                     avalue   => l_next_seq);
3048         resultout := 'COMPLETE:Y';
3049      ELSE
3050         resultout := 'COMPLETE:N';
3051      END IF;
3052   END IF;
3053 
3054   --
3055   -- CANCEL mode
3056   --
3057   IF (funcmode = 'CANCEL') THEN
3058         resultout := 'COMPLETE:';
3059         RETURN;
3060   END IF;
3061 
3062   --
3063   -- TIMEOUT mode
3064   --
3065   IF (funcmode = 'TIMEOUT') THEN
3066         resultout := 'COMPLETE:';
3067         RETURN;
3068   END IF;
3069  EXCEPTION
3070      WHEN Fnd_Api.G_EXC_ERROR THEN
3071         Fnd_Msg_Pub.Count_And_Get (
3072                p_encoded => Fnd_Api.G_FALSE,
3073                p_count => l_msg_count,
3074                p_data  => l_msg_data
3075           );
3076         Handle_Err
3077           (p_itemtype          => itemtype   ,
3078            p_itemkey           => itemkey    ,
3079            p_msg_count         => l_msg_count, -- Number of error Messages
3080            p_msg_data          => l_msg_data ,
3081            p_attr_name         => 'AMS_ERROR_MSG',
3082            x_error_msg         => l_error_msg
3083            )               ;
3084     Wf_Core.context('Ams_Approval_Pvt',
3085                     'set_further_approvals',
3086                     itemtype, itemkey,TO_CHAR(actid),l_error_msg);
3087          RAISE;
3088 WHEN OTHERS THEN
3089         Fnd_Msg_Pub.Count_And_Get (
3090                p_encoded => Fnd_Api.G_FALSE,
3091                p_count => l_msg_count,
3092                p_data  => l_msg_data
3093           );
3094         Handle_Err
3095           (p_itemtype          => itemtype   ,
3096            p_itemkey           => itemkey    ,
3097            p_msg_count         => l_msg_count, -- Number of error Messages
3098            p_msg_data          => l_msg_data ,
3099            p_attr_name         => 'AMS_ERROR_MSG',
3100            x_error_msg         => l_error_msg
3101            )               ;
3102     Wf_Core.context('Ams_Approval_Pvt',
3103                     'set_further_approvals',
3104                     itemtype, itemkey,TO_CHAR(actid),l_error_msg);
3105     RAISE;
3106   --
3107 
3108 END;
3109 
3110 /*============================================================================*/
3111 --Revert_Status
3112 /*============================================================================*/
3113 
3114 PROCEDURE Revert_Status( itemtype        IN  VARCHAR2,
3115                          itemkey         IN  VARCHAR2,
3116                          actid           IN  NUMBER,
3117                          funcmode        IN  VARCHAR2,
3118                          resultout       OUT NOCOPY VARCHAR2    )
3119 IS
3120 l_activity_id            NUMBER;
3121 l_activity_type          VARCHAR2(30);
3122 l_orig_status_id         NUMBER;
3123 l_return_status          VARCHAR2(1);
3124 l_msg_count              NUMBER;
3125 l_msg_data               VARCHAR2(4000);
3126 l_error_msg              VARCHAR2(4000);
3127 -- 11.5.9
3128 l_version                NUMBER;
3129 l_approval_type          VARCHAR2(30);
3130 
3131 BEGIN
3132   Fnd_Msg_Pub.initialize();
3133   --
3134   -- RUN mode
3135   --
3136   IF (funcmode = 'RUN') THEN
3137      -- get the acitvity id
3138      l_activity_id        := Wf_Engine.GetItemAttrNumber(
3139                                  itemtype => itemtype,
3140                                  itemkey  => itemkey,
3141                                  aname    => 'AMS_ACTIVITY_ID' );
3142 
3143      -- get the activity type
3144      l_activity_type      := Wf_Engine.GetItemAttrText(
3145                                  itemtype => itemtype,
3146                                  itemkey  => itemkey,
3147                                  aname    => 'AMS_ACTIVITY_TYPE' );
3148 
3149      l_orig_status_id     := Wf_Engine.GetItemAttrNumber(
3150                                  itemtype => itemtype,
3151                                  itemkey  => itemkey,
3152                                  aname    => 'AMS_ORIG_STAT_ID' );
3153 
3154 
3155      -- 11.5.9 addition
3156      l_version     := Wf_Engine.GetItemAttrNumber(
3157                                  itemtype => itemtype,
3158                                  itemkey  => itemkey,
3159                                  aname    => 'AMS_OBJECT_VERSION_NUMBER' );
3160 
3161      l_approval_type := Wf_Engine.GetItemAttrText(
3162                                  itemtype => itemtype,
3163                                  itemkey  => itemkey,
3164                                  aname    => 'AMS_APPROVAL_TYPE' );
3165 
3166 
3167      -- end 11.5.9 addition
3168 
3169      Update_Status(p_activity_type        => l_activity_type,
3170                    p_activity_id          => l_activity_id,
3171                    p_next_stat_id         => l_orig_status_id,
3172                    x_return_status        => l_return_status);
3173 
3174 
3175     IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS  THEN
3176       RAISE Fnd_Api.G_EXC_ERROR;
3177       -- Delete all history rows
3178     ELSE
3179 
3180              -- Delete all rows
3181            AMS_Appr_Hist_PVT.Delete_Appr_Hist(
3182              p_api_version_number => 1.0,
3183              p_init_msg_list      => FND_API.G_FALSE,
3184              p_commit             => FND_API.G_FALSE,
3185              p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
3186              x_return_status      => l_return_status,
3187              x_msg_count          => l_msg_count,
3188              x_msg_data           => l_msg_data,
3189              p_object_id          => l_activity_id,
3190              p_object_type_code   => l_activity_type,
3191              p_sequence_num       => null,
3192              p_action_code        => null,
3193              p_object_version_num => l_version,
3194              p_approval_type      => l_approval_type);
3195 
3196            IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
3197              RAISE Fnd_Api.G_EXC_ERROR;
3198            END IF;
3199 
3200       resultout := 'COMPLETE:SUCCESS';
3201     END IF;
3202   END IF;
3203 
3204   --
3205   -- CANCEL mode
3206   --
3207   IF (funcmode = 'CANCEL') THEN
3208         resultout := 'COMPLETE:';
3209         RETURN;
3210   END IF;
3211 
3212   --
3213   -- TIMEOUT mode
3214   --
3215   IF (funcmode = 'TIMEOUT') THEN
3216         resultout := 'COMPLETE:';
3217         RETURN;
3218   END IF;
3219   --
3220 
3221 EXCEPTION
3222      WHEN Fnd_Api.G_EXC_ERROR THEN
3223         Fnd_Msg_Pub.Count_And_Get (
3224                p_encoded => Fnd_Api.G_FALSE,
3225                p_count => l_msg_count,
3226                p_data  => l_msg_data
3227           );
3228         Handle_Err
3229           (p_itemtype          => itemtype   ,
3230            p_itemkey           => itemkey    ,
3231            p_msg_count         => l_msg_count, -- Number of error Messages
3232            p_msg_data          => l_msg_data ,
3233            p_attr_name         => 'AMS_ERROR_MSG',
3234            x_error_msg         => l_error_msg
3235            )               ;
3236        resultout := 'COMPLETE:FAILURE';
3237 
3238   WHEN OTHERS THEN
3239         Fnd_Msg_Pub.Count_And_Get (
3240                p_encoded => Fnd_Api.G_FALSE,
3241                p_count => l_msg_count,
3242                p_data  => l_msg_data
3243           );
3244         Handle_Err
3245           (p_itemtype          => itemtype   ,
3246            p_itemkey           => itemkey    ,
3247            p_msg_count         => l_msg_count, -- Number of error Messages
3248            p_msg_data          => l_msg_data ,
3249            p_attr_name         => 'AMS_ERROR_MSG',
3250            x_error_msg         => l_error_msg
3251            )               ;
3252     Wf_Core.context('Ams_Approval_Pvt',
3253                     'Revert_status',
3254                     itemtype, itemkey,TO_CHAR(actid),l_error_msg);
3255     RAISE;
3256 END Revert_Status;
3257 
3258 /*============================================================================*/
3259 -- Approve_Activity_status
3260 /*============================================================================*/
3261 
3262 PROCEDURE Approve_Activity_status( itemtype        IN  VARCHAR2,
3263                          itemkey         IN  VARCHAR2,
3264                          actid           IN  NUMBER,
3265                          funcmode        IN  VARCHAR2,
3266                          resultout       OUT NOCOPY VARCHAR2    )
3267 IS
3268 l_activity_id            NUMBER;
3269 l_activity_type          VARCHAR2(30);
3270 l_orig_status_id         NUMBER;
3271 l_return_status          VARCHAR2(1);
3272 l_msg_count              NUMBER;
3273 l_msg_data               VARCHAR2(4000);
3274 l_error_msg              VARCHAR2(4000);
3275       l_user_id                  NUMBER;
3276       l_resp_id                  NUMBER;
3277       l_appl_id                  NUMBER;
3278        l_org_id                   NUMBER;
3279 BEGIN
3280 
3281   --
3282   -- RUN mode
3283   --
3284   IF (funcmode = 'RUN') THEN
3285      -- get the acitvity id
3286 
3287      l_activity_id        := Wf_Engine.GetItemAttrNumber(
3288                                  itemtype => itemtype,
3289                                  itemkey  => itemkey,
3290                                  aname    => 'AMS_ACTIVITY_ID' );
3291 
3292      -- get the activity type
3293      l_activity_type      := Wf_Engine.GetItemAttrText(
3294                                  itemtype => itemtype,
3295                                  itemkey  => itemkey,
3296                                  aname    => 'AMS_ACTIVITY_TYPE' );
3297 
3298      l_orig_status_id     := Wf_Engine.GetItemAttrNumber(
3299                                  itemtype => itemtype,
3300                                  itemkey  => itemkey,
3301                                  aname    => 'AMS_NEW_STAT_ID' );
3302 
3303 
3304 OZF_UTILITY_PVT.debug_message(FND_LOG.LEVEL_STATEMENT,'OZF','AMS_Approval_PVT Approve_Activity_status l_activity_type : '|| l_activity_type);
3305 
3306 IF (l_activity_type = 'OFFR') THEN
3307          l_user_id := WF_ENGINE.getitemattrnumber( itemtype => itemtype
3308 	  , itemkey  => itemkey
3309 	  , aname    => 'USER_ID'
3310 	  );
3311 
3312 	l_resp_id := WF_ENGINE.getitemattrnumber( itemtype => itemtype
3313 			  , itemkey  => itemkey
3314 			  , aname    => 'RESPONSIBILITY_ID'
3315 			  );
3316 
3317 	l_appl_id := WF_ENGINE.getitemattrnumber( itemtype => itemtype
3318 			  , itemkey  => itemkey
3319 			  , aname    => 'APPLICATION_ID'
3320 			  );
3321 
3322 OZF_UTILITY_PVT.debug_message(FND_LOG.LEVEL_STATEMENT,'OZF','AMS_Approval_PVT Approve_Activity_status l_user_id : '|| l_user_id);
3323 OZF_UTILITY_PVT.debug_message(FND_LOG.LEVEL_STATEMENT,'OZF','AMS_Approval_PVT Approve_Activity_status l_resp_id : '|| l_resp_id);
3324 OZF_UTILITY_PVT.debug_message(FND_LOG.LEVEL_STATEMENT,'OZF','AMS_Approval_PVT Approve_Activity_status l_appl_id : '|| l_appl_id);
3325 
3326 
3327 	IF (l_user_id IS NOT NULL AND l_resp_id IS NOT NULL AND l_appl_id IS NOT NULL) THEN
3328 	  FND_GLOBAL.apps_initialize( user_id           => l_user_id
3329 					  , resp_id           => l_resp_id
3330 					  , resp_appl_id      => l_appl_id
3331 					  --, security_group_id => l_security_group_id
3332 					  );
3333 	END IF;
3334 
3335 
3336 	 OZF_UTILITY_PVT.debug_message(FND_LOG.LEVEL_STATEMENT,'OZF','AMS_Approval_PVT Approve_Activity_status list_header_id : '|| l_activity_id);
3337 
3338 	 l_org_id := find_org_id (l_activity_id);
3339 	     OZF_UTILITY_PVT.debug_message(FND_LOG.LEVEL_STATEMENT,'OZF','AMS_Approval_PVT Approve_Activity_status l_org_id : '||l_org_id);
3340 
3341 	 IF l_org_id IS NOT NULL THEN
3342 	        OZF_DEBUG_PVT.DEBUG_MO('AMS_Approval_PVT Approve_Activity_status before setting context : list_header_id '|| l_activity_id);
3343 		set_org_ctx (l_org_id);
3344 		OZF_DEBUG_PVT.DEBUG_MO('AMS_Approval_PVT Approve_Activity_status after setting context : list_header_id '|| l_activity_id);
3345 	 END IF;
3346 
3347 
3348 END IF;
3349 
3350      Update_Status(p_activity_type        => l_activity_type,
3351                    p_activity_id          => l_activity_id,
3352                    p_next_stat_id         => l_orig_status_id,
3353                    x_return_status           => l_return_status);
3354     IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS  THEN
3355       RAISE Fnd_Api.G_EXC_ERROR;
3356     ELSE
3357       resultout := 'COMPLETE:SUCCESS';
3358     END IF;
3359 
3360 
3361   END IF;
3362 
3363   --
3364   -- CANCEL mode
3365   --
3366   IF (funcmode = 'CANCEL') THEN
3367         resultout := 'COMPLETE:';
3368         RETURN;
3369   END IF;
3370 
3371   --
3372   -- TIMEOUT mode
3373   --
3374   IF (funcmode = 'TIMEOUT') THEN
3375         resultout := 'COMPLETE:';
3376         RETURN;
3377   END IF;
3378   --
3379 
3380 
3381 EXCEPTION
3382      WHEN Fnd_Api.G_EXC_ERROR THEN
3383         Fnd_Msg_Pub.Count_And_Get (
3384                p_encoded => Fnd_Api.G_FALSE,
3385                p_count => l_msg_count,
3386                p_data  => l_msg_data
3387           );
3388         Handle_Err
3389           (p_itemtype          => itemtype   ,
3390            p_itemkey           => itemkey    ,
3391            p_msg_count         => l_msg_count, -- Number of error Messages
3392            p_msg_data          => l_msg_data ,
3393            p_attr_name         => 'AMS_ERROR_MSG',
3394            x_error_msg         => l_error_msg
3395            )               ;
3396        resultout := 'COMPLETE:ERROR';
3397 
3398   WHEN OTHERS THEN
3399         Fnd_Msg_Pub.Count_And_Get (
3400                p_encoded => Fnd_Api.G_FALSE,
3401                p_count => l_msg_count,
3402                p_data  => l_msg_data
3403           );
3404         Handle_Err
3405           (p_itemtype          => itemtype   ,
3406            p_itemkey           => itemkey    ,
3407            p_msg_count         => l_msg_count, -- Number of error Messages
3408            p_msg_data          => l_msg_data ,
3409            p_attr_name         => 'AMS_ERROR_MSG',
3410            x_error_msg         => l_error_msg
3411            )               ;
3412     Wf_Core.context('Ams_Approval_Pvt',
3413                     'Approve_activity_status',
3414                     itemtype, itemkey,TO_CHAR(actid),l_error_msg);
3415     RAISE;
3416 END Approve_Activity_status;
3417 
3418 /*============================================================================*/
3419 -- Reject_Activity_Status
3420 /*============================================================================*/
3421 
3422 PROCEDURE Reject_Activity_status( itemtype        IN  VARCHAR2,
3423                          itemkey         IN  VARCHAR2,
3424                          actid           IN  NUMBER,
3425                          funcmode        IN  VARCHAR2,
3426                          resultout       OUT NOCOPY VARCHAR2    )
3427 IS
3428 l_activity_id            NUMBER;
3429 l_activity_type          VARCHAR2(30);
3430 l_orig_status_id         NUMBER;
3431 l_return_status          VARCHAR2(1);
3432 l_msg_count              NUMBER;
3433 l_msg_data               VARCHAR2(4000);
3434 l_error_msg              VARCHAR2(4000);
3435 -- added for 11.5.9
3436 l_approver_seq           NUMBER;
3437 l_version                NUMBER;
3438 l_approver_id            NUMBER;
3439 l_approval_detail_id     NUMBER;
3440 l_approval_type          VARCHAR2(30);
3441 l_note                   VARCHAR2(4000);
3442 l_appr_hist_rec          AMS_Appr_Hist_Pvt.Appr_Hist_Rec_Type;
3443 l_new_approver_id        NUMBER;
3444 --
3445 BEGIN
3446 
3447   --
3448   -- RUN mode
3449   --
3450   IF (funcmode = 'RUN') THEN
3451      -- get the acitvity id
3452      l_activity_id        := Wf_Engine.GetItemAttrNumber(
3453                                  itemtype => itemtype,
3454                                  itemkey  => itemkey,
3455                                  aname    => 'AMS_ACTIVITY_ID' );
3456 
3457      -- get the activity type
3458      l_activity_type      := Wf_Engine.GetItemAttrText(
3459                                  itemtype => itemtype,
3460                                  itemkey  => itemkey,
3461                                  aname    => 'AMS_ACTIVITY_TYPE' );
3462 
3463      l_orig_status_id     := Wf_Engine.GetItemAttrNumber(
3464                                  itemtype => itemtype,
3465                                  itemkey  => itemkey,
3466                                  aname    => 'AMS_REJECT_STAT_ID' );
3467 
3468      -- Added by VM for 11.5.9
3469      l_approver_seq := Wf_Engine.GetItemAttrNumber(
3470                               itemtype => itemtype,
3471                                 itemkey => itemkey,
3472                                 aname   => 'AMS_APPROVER_SEQ' );
3473      l_version := Wf_Engine.GetItemAttrNumber(
3474                               itemtype => itemtype,
3475                                 itemkey => itemkey,
3476                                 aname   => 'AMS_OBJECT_VERSION_NUMBER' );
3477 
3478      l_approver_id := Wf_Engine.GetItemAttrNumber(
3479                               itemtype => itemtype,
3480                                 itemkey => itemkey,
3481                                 aname   => 'AMS_APPROVER_ID' );
3482 
3483      l_approval_detail_id := Wf_Engine.GetItemAttrNumber(
3484                                  itemtype => itemtype,
3485                                  itemkey  => itemkey,
3486                                  aname    => 'AMS_ACTIVITY_DETAIL_ID' );
3487 
3488      l_approval_type := Wf_Engine.GetItemAttrText(
3489                                  itemtype => itemtype,
3490                                  itemkey  => itemkey,
3491                                  aname    => 'AMS_APPROVAL_TYPE' );
3492      -- End 11.5.9
3493 
3494 
3495      Update_Status(p_activity_type        => l_activity_type,
3496                    p_activity_id          => l_activity_id,
3497                    p_next_stat_id         => l_orig_status_id,
3498                    x_return_status        => l_return_status);
3499 
3500     IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS  THEN
3501       RAISE Fnd_Api.G_EXC_ERROR;
3502     ELSE
3503     -- Commented for bug 3150550
3504       -- Start of addition for forward/reassign notification
3505 /*
3506           Check_Reassigned (itemtype => itemtype,
3507                        itemkey  => itemkey,
3508                        x_approver_id   => l_new_approver_id,
3509                        x_return_status => l_return_status);
3510 
3511      IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
3512        RAISE Fnd_Api.G_EXC_ERROR;
3513      END IF;
3514 
3515      IF l_new_approver_id IS NOT NULL THEN
3516         l_approver_id := l_new_approver_id;
3517      END IF;
3518 
3519      -- End of addition for forward/re-assign notification
3520      -- End of Comment
3521 */
3522          -- update the record from 'PENDING' to 'REJECTED'
3523           l_appr_hist_rec.object_id          := l_activity_id;
3524           l_appr_hist_rec.object_type_code   := l_activity_type;
3525           l_appr_hist_rec.object_version_num := l_version;
3526           l_appr_hist_rec.action_code        := 'REJECTED';
3527           l_appr_hist_rec.approval_type      := l_approval_type;
3528           l_appr_hist_rec.sequence_num       := l_approver_seq;
3529           l_appr_hist_rec.note               := l_note;
3530           l_appr_hist_rec.action_date        := SYSDATE;
3531 
3532           -- should i reset approver_id-- yes
3533           l_appr_hist_rec.approver_id       := l_approver_id;
3534 
3535           AMS_Appr_Hist_PVT.Update_Appr_Hist(
3536              p_api_version_number => 1.0,
3537              p_init_msg_list      => FND_API.G_FALSE,
3538              p_commit             => FND_API.G_FALSE,
3539              p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
3540              x_return_status      => l_return_status,
3541              x_msg_count          => l_msg_count,
3542              x_msg_data           => l_msg_data,
3543              p_appr_hist_rec      => l_appr_hist_rec
3544              );
3545 
3546 	   IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
3547 	     RAISE Fnd_Api.G_EXC_ERROR;
3548 	   END IF;
3549 
3550 	     -- Delete any 'OPEN' rows
3551 	   AMS_Appr_Hist_PVT.Delete_Appr_Hist(
3552              p_api_version_number => 1.0,
3553              p_init_msg_list      => FND_API.G_FALSE,
3554              p_commit             => FND_API.G_FALSE,
3555              p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
3556              x_return_status      => l_return_status,
3557              x_msg_count          => l_msg_count,
3558              x_msg_data           => l_msg_data,
3559 	     p_object_id          => l_activity_id,
3560              p_object_type_code   => l_activity_type,
3561              p_sequence_num       => null, -- all open rows
3562 	     p_action_code        => 'OPEN',
3563              p_object_version_num => l_version,
3564              p_approval_type      => l_approval_type);
3565 
3566 	   IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
3567 	     RAISE Fnd_Api.G_EXC_ERROR;
3568 	   END IF;
3569 
3570       resultout := 'COMPLETE:SUCCESS';
3571     END IF;
3572   END IF;
3573 
3574   --
3575   -- CANCEL mode
3576   --
3577   IF (funcmode = 'CANCEL') THEN
3578         resultout := 'COMPLETE:';
3579         RETURN;
3580   END IF;
3581 
3582   --
3583   -- TIMEOUT mode
3584   --
3585   IF (funcmode = 'TIMEOUT') THEN
3586         resultout := 'COMPLETE:';
3587         RETURN;
3588   END IF;
3589   --
3590 
3591 
3592 EXCEPTION
3593 
3594      WHEN Fnd_Api.G_EXC_ERROR THEN
3595         Fnd_Msg_Pub.Count_And_Get (
3596                p_encoded => Fnd_Api.G_FALSE,
3597                p_count => l_msg_count,
3598                p_data  => l_msg_data
3599           );
3600         Handle_Err
3601           (p_itemtype          => itemtype   ,
3602            p_itemkey           => itemkey    ,
3603            p_msg_count         => l_msg_count, -- Number of error Messages
3604            p_msg_data          => l_msg_data ,
3605            p_attr_name         => 'AMS_ERROR_MSG',
3606            x_error_msg         => l_error_msg
3607            )               ;
3608        resultout := 'COMPLETE:ERROR';
3609      WHEN OTHERS THEN
3610         Fnd_Msg_Pub.Count_And_Get (
3611                p_encoded => Fnd_Api.G_FALSE,
3612                p_count => l_msg_count,
3613                p_data  => l_msg_data
3614           );
3615         Handle_Err
3616           (p_itemtype          => itemtype   ,
3617            p_itemkey           => itemkey    ,
3618            p_msg_count         => l_msg_count, -- Number of error Messages
3619            p_msg_data          => l_msg_data ,
3620            p_attr_name         => 'AMS_ERROR_MSG',
3621            x_error_msg         => l_error_msg
3622            )               ;
3623     Wf_Core.context('Ams_Approval_Pvt',
3624                     'Reject_activity_status',
3625                     itemtype, itemkey,TO_CHAR(actid),l_error_msg);
3626     RAISE;
3627 END Reject_Activity_status;
3628 
3629 
3630 /*****************************************************************
3631 -- Start of Comments
3632 --
3633 -- NAME
3634 --   AbortProcess
3635 -- PURPOSE
3636 --   This Procedure will abort the process of Approvals
3637 -- Used By Activities
3638 --
3639 -- NOTES
3640 -- HISTORY
3641 -- End of Comments
3642 *****************************************************************/
3643 
3644 PROCEDURE AbortProcess
3645              (p_itemkey          IN   VARCHAR2
3646              ,p_workflowprocess  IN   VARCHAR2      DEFAULT NULL
3647              ,p_itemtype         IN   VARCHAR2      DEFAULT NULL
3648              )
3649 IS
3650     itemkey   VARCHAR2(30) := p_itemkey ;
3651     itemtype  VARCHAR2(30) := NVL(p_itemtype,'AMS_APPROVAL') ;
3652 BEGIN
3653    Ams_Utility_Pvt.debug_message('Process ABORT Process');
3654    Wf_Engine.AbortProcess (itemtype   =>   itemtype,
3655                            itemkey       =>  itemkey ,
3656                            process       =>  p_workflowprocess);
3657 EXCEPTION
3658    WHEN OTHERS THEN
3659       Wf_Core.context('Ams_Approval_Pvt',
3660                       'AbortProcess',
3661                       itemtype,
3662                       itemkey
3663                       ,p_workflowprocess);
3664          RAISE;
3665 END AbortProcess;
3666 /*============================================================================*/
3667 -- NAME
3668 --    get_object_currency
3669 -- PURPOSE
3670 --    Return the currency code of the object trying to
3671 --    associate a budget.
3672 -- NOTE
3673 --    To support other objects, the function will need
3674 --    to be modified.
3675 -- HISTORY
3676 -- 15-Aug-2000  choang     Created.
3677 -- 01-Sep-2000  choang     ARC qualifier for deliverables should be DELV
3678 /*============================================================================*/
3679 
3680 FUNCTION get_object_currency (
3681    p_object IN VARCHAR2,
3682    p_object_id IN NUMBER
3683 )
3684 RETURN VARCHAR2
3685 IS
3686    l_currency_code      VARCHAR2(15);
3687 
3688    CURSOR c_campaign IS
3689       SELECT transaction_currency_code
3690       FROM   ams_campaigns_vl
3691       WHERE  campaign_id = p_object_id;
3692       /* code added by murali code done by ravi start*/
3693       CURSOR c_schedule IS
3694       SELECT transaction_currency_code
3695       FROM   ams_campaign_schedules_vl
3696       WHERE  schedule_id = p_object_id;
3697    CURSOR c_offer IS
3698       SELECT transaction_currency_code
3699       FROM   ozf_offers OFF
3700       WHERE  OFF.qp_list_header_id = p_object_id;
3701         /* code added by murali code done by ravi end*/
3702    CURSOR c_eheader IS
3703       SELECT currency_code_tc
3704       FROM   ams_event_headers_vl
3705       WHERE  event_header_id = p_object_id;
3706    CURSOR c_eoffer IS
3707       SELECT currency_code_tc
3708       FROM   ams_event_offers_vl
3709       WHERE  event_offer_id = p_object_id;
3710    CURSOR c_deliverable IS
3711       SELECT transaction_currency_code
3712       FROM   ams_deliverables_vl
3713       WHERE  deliverable_id = p_object_id;
3714    CURSOR c_fund IS
3715       SELECT currency_code_tc
3716       FROM   ozf_funds_all_vl
3717       WHERE  fund_id = p_object_id;
3718 BEGIN
3719    -- Campaign
3720    IF p_object = 'CAMP' THEN
3721       OPEN c_campaign;
3722       FETCH c_campaign INTO l_currency_code;
3723       IF c_campaign%NOTFOUND THEN
3724          CLOSE c_campaign;
3725          Ams_Utility_Pvt.error_message ('AMS_BUDGET_NO_OWNER');
3726          RAISE Fnd_Api.g_exc_error;
3727       END IF;
3728       CLOSE c_campaign;
3729       /* code added by murali code done by ravi start*/
3730     -- Campaign Schedule
3731  ELSIF p_object = 'CSCH' THEN
3732       OPEN c_schedule;
3733       FETCH c_schedule INTO l_currency_code;
3734       IF c_schedule%NOTFOUND THEN
3735          CLOSE c_schedule;
3736          Ams_Utility_Pvt.error_message ('AMS_BUDGET_NO_OWNER');
3737          RAISE Fnd_Api.g_exc_error;
3738       END IF;
3739       CLOSE c_schedule;
3740    -- Offer
3741  ELSIF p_object = 'OFFR' THEN
3742       OPEN c_offer;
3743       FETCH c_offer INTO l_currency_code;
3744       IF c_offer%NOTFOUND THEN
3745          CLOSE c_offer;
3746          Ams_Utility_Pvt.error_message ('AMS_BUDGET_NO_OWNER');
3747          RAISE Fnd_Api.g_exc_error;
3748       END IF;
3749       CLOSE c_offer;
3750       /* code added by murali code done by ravi end*/
3751    -- Event Header/Rollup Event
3752    ELSIF p_object = 'EVEH' THEN
3753       OPEN c_eheader;
3754       FETCH c_eheader INTO l_currency_code;
3755       IF c_eheader%NOTFOUND THEN
3756          CLOSE c_eheader;
3757          Ams_Utility_Pvt.error_message ('AMS_BUDGET_NO_OWNER');
3758          RAISE Fnd_Api.g_exc_error;
3759       END IF;
3760       CLOSE c_eheader;
3761    -- Event Offer/Execution Event
3762    ELSIF (p_object = 'EVEO' OR p_object = 'EONE')THEN
3763       OPEN c_eoffer;
3764       FETCH c_eoffer INTO l_currency_code;
3765       IF c_eoffer%NOTFOUND THEN
3766          CLOSE c_eoffer;
3767          Ams_Utility_Pvt.error_message ('AMS_BUDGET_NO_OWNER');
3768          RAISE Fnd_Api.g_exc_error;
3769       END IF;
3770       CLOSE c_eoffer;
3771    -- Deliverable
3772    ELSIF p_object = 'DELV' THEN
3773       OPEN c_deliverable;
3774       FETCH c_deliverable INTO l_currency_code;
3775       IF c_deliverable%NOTFOUND THEN
3776          CLOSE c_deliverable;
3777          Ams_Utility_Pvt.error_message ('AMS_ACT_BUDG_NO_OWNER');
3778          RAISE Fnd_Api.g_exc_error;
3779       END IF;
3780       CLOSE c_deliverable;
3781    ELSIF p_object = 'FUND' THEN
3782       OPEN c_fund;
3783       FETCH c_fund INTO l_currency_code;
3784       IF c_fund%NOTFOUND THEN
3785          CLOSE c_fund;
3786          Ams_Utility_Pvt.error_message ('AMS_ACT_BUDG_NO_OWNER');
3787          RAISE Fnd_Api.g_exc_error;
3788       END IF;
3789       CLOSE c_fund;
3790    ELSE
3791       RAISE Fnd_Api.g_exc_unexpected_error;
3792    END IF;
3793 
3794    RETURN l_currency_code;
3795 EXCEPTION
3796    WHEN OTHERS THEN
3797       IF c_campaign%ISOPEN THEN
3798          CLOSE c_campaign;
3799       END IF;
3800       IF c_eheader%ISOPEN THEN
3801          CLOSE c_eheader;
3802       END IF;
3803       IF c_eoffer%ISOPEN THEN
3804          CLOSE c_eoffer;
3805       END IF;
3806       IF c_deliverable%ISOPEN THEN
3807          CLOSE c_deliverable;
3808       END IF;
3809       IF c_fund%ISOPEN THEN
3810          CLOSE c_fund;
3811       END IF;
3812       RAISE;
3813 END get_object_currency;
3814 
3815 /*============================================================================*/
3816 -- Start_Process
3817 /*============================================================================*/
3818 
3819 PROCEDURE Start_Process(
3820      p_requestor_id               IN  NUMBER,
3821      p_act_budget_id              IN  NUMBER,
3822      p_orig_stat_id               IN  NUMBER,
3823      p_new_stat_id                IN  NUMBER,
3824      p_rejected_stat_id           IN  NUMBER,
3825      p_parent_process_flag        IN  VARCHAR2,
3826      p_parent_process_key         IN  VARCHAR2,
3827      p_parent_context             IN  VARCHAR2,
3828      p_parent_approval_flag       IN  VARCHAR2,
3829      p_continue_flow              IN  VARCHAR2
3830 )
3831 IS
3832   l_api_name          CONSTANT VARCHAR2(30) := 'Start_Process';
3833   l_itemtype            VARCHAR2(30) := G_ITEMTYPE;
3834   l_itemkey             VARCHAR2(80);
3835   l_itemuserkey         VARCHAR2(80);
3836   l_process             VARCHAR2(80) := 'AMS_LINE_APPROVAL';
3837   l_owner               VARCHAR2(100); --l_owner          VARCHAR2(80);--SVEERAVE,04/10/02
3838   l_requestor_name      VARCHAR2(320);-- was 100  VARCHAR2(30);--SVEERAVE,04/10/02
3839   l_version             NUMBER;
3840   l_activity_detail_id  NUMBER;
3841   l_approver_seq        NUMBER;
3842   l_approver_obj ObjRecTyp;
3843   l_parent_object_details       ObjRecTyp;
3844   l_object_details       ObjRecTyp;
3845 
3846   l_parent_amount       NUMBER;
3847   l_budget_id           NUMBER;
3848   l_budget_type         VARCHAR2(30);
3849   l_activity_id         NUMBER;
3850   l_activity_type       VARCHAR2(30);
3851   l_request_amount      NUMBER;
3852   l_request_currency    VARCHAR2(30);
3853   l_budget_amount       NUMBER;
3854   l_budget_currency     VARCHAR2(30);
3855   l_appr_meaning        VARCHAR2(240);
3856   l_return_status       VARCHAR2(30);
3857   l_requestor_display_name VARCHAR2(360); -- was 80
3858 
3859   l_msg_count           NUMBER;
3860   l_msg_data            VARCHAR2(4000);
3861   l_error_msg           VARCHAR2(4000);
3862   l_full_name           VARCHAR2(360); --l_full_name   VARCHAR2(60); -- SVEERAVE, 04/10/02
3863   l_justification_text  VARCHAR2(2000);
3864 
3865   -- 11.5.9
3866   l_appr_hist_rec       AMS_Appr_Hist_Pvt.Appr_Hist_Rec_Type;
3867 
3868   -- [BEGIN OF BUG2741039 FIXING by vmodur 09-Jan-2003]
3869     l_user_id                NUMBER;
3870     l_resp_id                NUMBER;
3871     l_appl_id                NUMBER;
3872     l_security_group_id      NUMBER;
3873   -- [END OF BUG2741039 FIXING]
3874 
3875   CURSOR      budget_lines_csr IS
3876     SELECT      budget_source_id
3877       , budget_source_type
3878       , act_budget_used_by_id
3879       , arc_act_budget_used_by
3880       , request_amount
3881       , request_currency
3882       , object_version_number
3883       , src_curr_request_amt -- Added Bug 3729490
3884     FROM    ozf_act_budgets
3885     WHERE   activity_budget_id = p_act_budget_id;
3886 
3887   CURSOR justification_text_csr(id_in IN NUMBER) IS
3888     SELECT notes
3889     FROM jtf_notes_vl
3890     WHERE source_object_code = 'AMS_FREQ'
3891     AND note_type = 'AMS_JUSTIFICATION'
3892     AND source_object_id = id_in;
3893 
3894 BEGIN
3895 
3896   Fnd_Msg_Pub.initialize();
3897 
3898      -- Delete any previous approval history rows Bug 2761026
3899 
3900    AMS_Appr_Hist_PVT.Delete_Appr_Hist(
3901              p_api_version_number => 1.0,
3902              p_init_msg_list      => FND_API.G_FALSE,
3903              p_commit             => FND_API.G_FALSE,
3904              p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
3905              x_return_status      => l_return_status,
3906              x_msg_count          => l_msg_count,
3907              x_msg_data           => l_msg_data,
3908 	     p_object_id          => p_act_budget_id,
3909              p_object_type_code   => 'FUND',
3910              p_sequence_num       => null,
3911 	     p_action_code        => null,
3912              p_object_version_num => null,
3913              p_approval_type      => 'BUDGET');
3914 
3915 	   IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
3916 	     RAISE Fnd_Api.G_EXC_ERROR;
3917 	   END IF;
3918 
3919   -- get the budget line ids
3920   OPEN budget_lines_csr;
3921   FETCH budget_lines_csr INTO l_budget_id, l_budget_type,
3922                           l_activity_id, l_activity_type,
3923                           l_request_amount, l_request_currency,
3924                           l_version, l_budget_amount;
3925   IF budget_lines_csr%NOTFOUND THEN
3926     CLOSE budget_lines_csr;
3927     RAISE Fnd_Api.G_EXC_ERROR;
3928   END IF;
3929   CLOSE budget_lines_csr;
3930 
3931      -- set the itemkey and itemuserkey
3932   IF p_parent_process_flag = Fnd_Api.G_TRUE THEN
3933        -- for process started from a parent process
3934     l_itemkey := p_act_budget_id||'_'||p_parent_process_key;
3935     l_itemuserkey := l_activity_id||'_'||l_budget_id||'_'||p_parent_process_key;
3936   ELSE
3937       -- for a standalone process
3938     l_itemkey := p_act_budget_id||'_'||l_version;
3939     l_itemuserkey := l_activity_id||'_'||l_budget_id||'_'||l_version;
3940   END IF;
3941 
3942        -- create a new process
3943        --
3944   Wf_Engine.CreateProcess(ItemType => l_itemtype,
3945                           ItemKey  => l_itemkey,
3946                           process  => l_process );
3947 
3948      -- set the user key for process
3949      --
3950   Wf_Engine.SetItemUserKey(ItemType => l_itemtype,
3951                            ItemKey => l_itemkey,
3952                            UserKey => l_itemuserkey);
3953 
3954   -- 09-Jan-2003  VMODUR  Fix for BUG 2741039 - add pl/sql global context into
3955   --                      workflow item attribute.It could be used later on for
3956   --                      PL/SQL function to initialize the global context when the
3957   --                      session is established by workflow mailer.
3958 
3959    l_user_id := FND_GLOBAL.user_id;
3960    l_resp_id := FND_GLOBAL.resp_id;
3961    l_appl_id := FND_GLOBAL.resp_appl_id;
3962    l_security_group_id := FND_GLOBAL.security_group_id;
3963 
3964    OZF_UTILITY_PVT.debug_message(FND_LOG.LEVEL_STATEMENT,'OZF','AMS_Approval_PVT Start_Process l_user_id : '|| l_user_id);
3965    OZF_UTILITY_PVT.debug_message(FND_LOG.LEVEL_STATEMENT,'OZF','AMS_Approval_PVT Start_Process l_resp_id : '|| l_resp_id);
3966    OZF_UTILITY_PVT.debug_message(FND_LOG.LEVEL_STATEMENT,'OZF','AMS_Approval_PVT Start_Process l_appl_id : '|| l_appl_id);
3967 
3968    WF_ENGINE.SetItemAttrNumber(itemtype   =>  l_itemtype ,
3969                                itemkey    =>  l_itemkey,
3970                                aname      =>  'USER_ID',
3971                                avalue     =>  l_user_id
3972                               );
3973 
3974    WF_ENGINE.SetItemAttrNumber(itemtype   =>  l_itemtype ,
3975                                itemkey    =>  l_itemkey,
3976                                aname      =>  'RESPONSIBILITY_ID',
3977                                avalue     =>  l_resp_id
3978                               );
3979 
3980    WF_ENGINE.SetItemAttrNumber(itemtype   =>  l_itemtype ,
3981                                itemkey    =>  l_itemkey,
3982                                aname      =>  'APPLICATION_ID',
3983                                avalue     =>  l_appl_id
3984                               );
3985 
3986    WF_ENGINE.SetItemAttrNumber(itemtype   =>  l_itemtype ,
3987                                itemkey    =>  l_itemkey,
3988                                aname      =>  'SECURITY_GROUP_ID',
3989                                avalue     =>  l_security_group_id
3990                               );
3991   -- [END OF BUG2741039 FIXING]
3992 
3993      -- set the parent  item
3994      --
3995   IF p_parent_process_flag = Fnd_Api.G_TRUE THEN
3996         -- set parent
3997     Wf_Engine.SetItemParent(itemtype      =>l_itemtype,
3998                             itemkey      => l_itemkey,
3999                             parent_itemtype  => l_itemtype,
4000                             parent_itemkey   => p_parent_process_key,
4001                             parent_context   => p_parent_context );
4002 
4003           -- get parent amount
4004     l_parent_amount := Wf_Engine.GetItemAttrNumber(
4005                                            itemtype => l_itemtype,
4006                                            itemkey  => p_parent_process_key,
4007                                            aname    => 'AMS_ACTIVITY_AMOUNT' );
4008 
4009        --
4010     Wf_Engine.SetItemAttrNumber(itemtype  => l_itemtype,
4011                                 itemkey   => l_itemkey,
4012                                 aname     => 'AMS_ACTIVITY_AMOUNT',
4013                                 avalue    => l_parent_amount);
4014 
4015   END IF;
4016 
4017   OPEN justification_text_csr(p_act_budget_id);
4018   FETCH justification_text_csr INTO l_justification_text;
4019   CLOSE justification_text_csr;
4020 
4021   Wf_Engine.SetItemAttrText(itemtype  => l_itemtype,
4022                                     itemkey   => l_itemkey,
4023                                     aname     => 'AMS_JUSTIFICATION',
4024                                     avalue    => l_justification_text);
4025 
4026   Get_Activity_Details
4027         ( p_activity_type      => l_activity_type,
4028           p_activity_id        => l_activity_id,
4029           x_object_details     => l_parent_object_details,
4030           x_return_status      => l_return_status);
4031   IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
4032     RAISE Fnd_Api.G_EXC_ERROR;
4033   END IF;
4034   Wf_Engine.SetItemAttrText(itemtype  => l_itemtype,
4035                             itemkey   => l_itemkey,
4036                             aname     => 'AMS_ACT_NAME',
4037                             avalue    => l_parent_object_details.name);
4038 
4039   Wf_Engine.SetItemAttrText(itemtype  => l_itemtype,
4040                             itemkey   => l_itemkey,
4041                             aname     => 'AMS_START_DATE',
4042                             avalue    => l_parent_object_details.start_date);
4043 
4044 
4045   Wf_Engine.SetItemAttrText(itemtype  => l_itemtype,
4046                             itemkey   => l_itemkey,
4047                             aname     => 'AMS_END_DATE',
4048                             avalue    => l_parent_object_details.end_date);
4049   Wf_Engine.SetItemAttrText(itemtype  => l_itemtype,
4050                             itemkey   => l_itemkey,
4051                             aname     => 'AMS_ACTIVITY_DESCRIPTION',
4052                             avalue    => l_parent_object_details.description);
4053      -- Changed Priority code to meaning
4054   Wf_Engine.SetItemAttrText(itemtype  => l_itemtype,
4055                             itemkey   =>l_itemkey,
4056                             aname     => 'AMS_PRIORITY',
4057                             avalue    => l_parent_object_details.priority_desc);
4058   Wf_Engine.SetItemAttrText(itemtype  => l_itemtype,
4059                             itemkey   => l_itemkey,
4060                             aname     => 'AMS_ACTIVITY_CURRENCY',
4061                             avalue    => l_parent_object_details.currency);
4062         --
4063         --
4064   Wf_Engine.SetItemAttrNumber(itemtype  => l_itemtype,
4065                               itemkey   => l_itemkey,
4066                               aname     => 'AMS_ACTIVITY_AMOUNT',
4067                               avalue    =>
4068                               l_parent_object_details.total_header_amount);
4069 			      --Version Number Added for 11.5.9
4070 Wf_Engine.SetItemAttrNumber(itemtype  => l_itemtype,
4071                               itemkey   => l_itemkey,
4072                               aname     => 'AMS_OBJECT_VERSION_NUMBER',
4073                               avalue    =>  l_version);
4074 			      -- End add of addition
4075   Wf_Engine.SetItemAttrText(itemtype  => l_itemtype,
4076                             itemkey   => l_itemkey,
4077                             aname     => 'AMS_SOURCE_CODE',
4078                             avalue    => l_parent_object_details.source_code);
4079 
4080   Wf_Engine.SetItemAttrText(itemtype  => l_itemtype,
4081                             itemkey   => l_itemkey,
4082                             aname     => 'AMS_PARENT_SOURCE_CODE',
4083                             avalue    => l_parent_object_details.parent_source_code);
4084 
4085   Wf_Engine.SetItemAttrText(itemtype  => l_itemtype,
4086                             itemkey   => l_itemkey,
4087                             aname     => 'AMS_PARENT_OBJECT',
4088                             avalue    => l_parent_object_details.parent_name);
4089 
4090   Ams_Utility_Pvt.get_lookup_meaning( 'AMS_SYS_ARC_QUALIFIER',
4091                                       l_activity_type,
4092                                       l_return_status,
4093                                       l_appr_meaning);
4094 
4095   --  check for description of activity
4096   Wf_Engine.SetItemAttrText(itemtype =>  l_itemtype ,
4097                             itemkey    =>  l_itemkey,
4098                             aname      =>  'AMS_APPROVAL_OBJECT_MEANING',
4099                             avalue     =>  l_appr_meaning  );
4100   Get_User_Name
4101           ( p_user_id            => l_parent_object_details.owner_id,
4102             x_full_name          => l_full_name,
4103             x_return_status      => l_return_status );
4104 
4105   Wf_Engine.SetItemAttrText(itemtype  => l_itemtype,
4106                                     itemkey   => l_itemkey,
4107                                     aname     => 'AMS_ACTIVITY_OWNER',
4108                                     avalue    => l_full_name );
4109 
4110   IF p_parent_approval_flag = Fnd_Api.G_TRUE THEN
4111          --
4112     Wf_Engine.SetItemAttrText(itemtype  => l_itemtype,
4113                               itemkey   => l_itemkey,
4114                               aname     => 'AMS_CONTINUEFLOW',
4115                               avalue    => 'CONTINUEFLOW-1');
4116   END IF;
4117 
4118   -- set othet attributes required for lines approval
4119   --
4120   Wf_Engine.SetItemAttrNumber(itemtype => l_itemtype,
4121                               itemkey  => l_itemkey,
4122                               aname    => 'AMS_ACT_BUDGET_ID',
4123                               avalue   => p_act_budget_id);
4124       --
4125   Wf_Engine.SetItemAttrNumber(itemtype => l_itemtype,
4126                               itemkey  => l_itemkey,
4127                               aname    => 'AMS_LINE_ORIG_STAT_ID',
4128                               avalue   => p_orig_stat_id);
4129       --
4130   Wf_Engine.SetItemAttrNumber(itemtype => l_itemtype,
4131                               itemkey  => l_itemkey,
4132                               aname    => 'AMS_LINE_NEW_STAT_ID',
4133                               avalue   => p_new_stat_id);
4134       --
4135   Wf_Engine.SetItemAttrNumber(itemtype => l_itemtype,
4136                               itemkey  => l_itemkey,
4137                               aname    => 'AMS_LINE_REJECT_STAT_ID',
4138                               avalue   => p_rejected_stat_id);
4139        --
4140   Wf_Engine.SetItemAttrNumber(itemtype  => l_itemtype,
4141                               itemkey   => l_itemkey,
4142                               aname     => 'AMS_ACTIVITY_ID',
4143                               avalue    => l_activity_id);
4144        --
4145   Wf_Engine.SetItemAttrText(itemtype  => l_itemtype,
4146                             itemkey   => l_itemkey,
4147                             aname     => 'AMS_ACTIVITY_TYPE',
4148                             avalue    => l_activity_type);
4149        --
4150   Wf_Engine.SetItemAttrNumber(itemtype  => l_itemtype,
4151                               itemkey   => l_itemkey,
4152                               aname     => 'AMS_BUDGET_ID',
4153                               avalue    => l_budget_id);
4154        --
4155   Wf_Engine.SetItemAttrText(itemtype  => l_itemtype,
4156                             itemkey   => l_itemkey,
4157                             aname     => 'AMS_BUDGET_TYPE',
4158                             avalue    => l_budget_type);
4159        --
4160   Wf_Engine.SetItemAttrText(itemtype  => l_itemtype,
4161                             itemkey   => l_itemkey,
4162                             aname     => 'AMS_PARENT_WAITING',
4163                             avalue    => p_parent_process_flag);
4164        --
4165   Wf_Engine.SetItemAttrText(itemtype  => l_itemtype,
4166                             itemkey   => l_itemkey,
4167                             aname     => 'AMS_PARENT_ITEMKEY',
4168                             avalue    => p_parent_process_key);
4169        --
4170   Wf_Engine.SetItemAttrText(itemtype  => l_itemtype,
4171                             itemkey   => l_itemkey,
4172                             aname     => 'AMS_PARENT_APPROVED',
4173                             avalue    => p_parent_approval_flag);
4174 
4175        --
4176   Wf_Engine.SetItemAttrText(itemtype  => l_itemtype,
4177                             itemkey   => l_itemkey,
4178                             aname     => 'AMS_CONTINUE_FLOW',
4179                             avalue    => p_continue_flow);
4180 
4181   Get_User_Role(p_user_id      => p_requestor_id,
4182                 x_role_name         => l_requestor_name,
4183                 x_role_display_name => l_requestor_display_name ,
4184                 x_return_status        => l_return_status);
4185 
4186   IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS  THEN
4187     RAISE Fnd_Api.G_EXC_ERROR;
4188   END IF;
4189 
4190        --
4191   Wf_Engine.SetItemAttrNumber(  itemtype  => l_itemtype,
4192                                     itemkey   => l_itemkey,
4193                                     aname     => 'AMS_REQUESTER_ID',
4194                                     avalue    => p_requestor_id);
4195 
4196        --
4197   Wf_Engine.SetItemAttrText(  itemtype  => l_itemtype,
4198                                     itemkey   => l_itemkey,
4199                                     aname     => 'AMS_REQUESTER',
4200                                     avalue    => l_requestor_name);
4201 
4202      -- Get more details for the current activity
4203   BEGIN
4204     Get_Approval_Details (
4205           p_activity_id          => l_budget_id,
4206           p_activity_type        => l_budget_type,
4207           p_act_budget_id        => p_act_budget_id,
4208           x_object_details       => l_object_details,
4209           x_approval_detail_id   => l_activity_detail_id,
4210           x_approver_seq         => l_approver_seq,
4211           x_return_status        => l_return_status );
4212   EXCEPTION
4213     WHEN OTHERS THEN
4214       Fnd_Message.Set_Name('AMS','AMS_API_DEBUG_MESSAGE');
4215       Fnd_Message.Set_Token('ROW', SQLERRM );
4216       Fnd_Msg_Pub.ADD;
4217       RAISE Fnd_Api.G_EXC_ERROR;
4218   END;
4219 
4220   IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
4221     RAISE Fnd_Api.G_EXC_ERROR;
4222   END IF;
4223 
4224   l_budget_currency := get_object_currency(l_budget_type, l_budget_id);
4225 
4226  /*  Bug 3729490 Fix -- Don't recompute always. Get from ozf_act_budgets
4227                          SRC_CURR_REQUEST_AMT. If that is null for some
4228                          reason then convert
4229    */
4230 
4231     IF l_budget_amount IS NULL THEN
4232      -- convert budget line amount to transaction currency of the source
4233     Ams_Utility_Pvt.Convert_Currency (
4234           p_from_currency      => l_request_currency,
4235           p_to_currency        => l_budget_currency,
4236           p_from_amount        => l_request_amount,
4237           x_to_amount          => l_budget_amount,
4238           x_return_status      => l_return_status );
4239 
4240     END IF;
4241 
4242   IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
4243     RAISE Fnd_Api.G_EXC_ERROR;
4244   END IF;
4245 
4246        --
4247      /*
4248        wf_engine.SetItemAttrText(itemtype  => l_itemtype,
4249                                     itemkey   => l_itemkey,
4250                                     aname     => 'AMS_ACT_NAME',
4251                                     avalue    => l_approver_obj.name);
4252        */
4253 
4254   Wf_Engine.SetItemAttrText(itemtype  => l_itemtype,
4255                             itemkey   => l_itemkey,
4256                             aname     => 'AMS_BUDGET_NAME',
4257                             avalue    => l_object_details.name);
4258 
4259   Get_User_Name
4260           ( p_user_id            => l_object_details.owner_id,
4261             x_full_name          => l_full_name,
4262             x_return_status      => l_return_status );
4263 
4264   Wf_Engine.SetItemAttrText(itemtype  => l_itemtype,
4265                             itemkey   => l_itemkey,
4266                             aname     => 'AMS_BUDGET_OWNER',
4267                             avalue    => l_full_name );
4268 
4269   Wf_Engine.SetItemAttrText(itemtype  => l_itemtype,
4270                             itemkey   => l_itemkey,
4271                             aname     => 'AMS_BUDGET_CURRENCY',
4272                             avalue    => l_budget_currency);
4273 
4274   Wf_Engine.SetItemAttrNumber(itemtype  => l_itemtype,
4275                               itemkey   => l_itemkey,
4276                               aname     => 'AMS_BUDGET_AMOUNT',
4277                               avalue    => l_request_amount);
4278        --
4279   Wf_Engine.SetItemAttrNumber(itemtype  => l_itemtype,
4280                               itemkey   => l_itemkey,
4281                               aname     => 'AMS_BUDGET_AMOUNT_CONV',
4282                               avalue    => l_budget_amount);
4283 
4284        --
4285   Wf_Engine.SetItemAttrNumber(itemtype  => l_itemtype,
4286                               itemkey   => l_itemkey,
4287                               aname     => 'AMS_APPROVED_LINE_AMOUNT',
4288                               avalue    => l_budget_amount);
4289 
4290        --
4291   Wf_Engine.SetItemAttrText(itemtype  => l_itemtype,
4292                             itemkey   => l_itemkey,
4293                             aname     => 'AMS_CURRENCY',
4294                             avalue    => l_budget_currency);
4295 
4296        --
4297   Wf_Engine.SetItemAttrNumber(itemtype  => l_itemtype,
4298                               itemkey   => l_itemkey,
4299                               aname     => 'AMS_APPROVER_SEQ',
4300                               avalue    => l_approver_seq);
4301 
4302        --
4303   Wf_Engine.SetItemAttrNumber(itemtype  => l_itemtype,
4304                               itemkey   => l_itemkey,
4305                               aname     => 'AMS_ACTIVITY_DETAIL_ID',
4306                               avalue    => l_activity_detail_id);
4307 
4308 /*
4309         ams_utility_pvt.get_lookup_meaning( 'AMS_SYS_ARC_QUALIFIER',
4310                                       l_budget_type,
4311                                       l_return_status,
4312                                       l_appr_meaning);
4313 
4314          --  check for description of activity
4315          wf_engine.SetItemAttrText(itemtype =>  l_itemtype ,
4316                             itemkey    =>  l_itemkey,
4317                             aname      =>  'AMS_APPROVAL_OBJECT_MEANING',
4318                             avalue     =>  l_appr_meaning  );
4319 */
4320 
4321   Wf_Engine.SetItemAttrText(itemtype =>  l_itemtype ,
4322                             itemkey    =>  l_itemkey,
4323                             aname      =>  'AMS_APPROVAL_TYPE',
4324                             avalue     =>  'BUDGET'  );
4325 
4326      -- set the process owner
4327        --
4328      -- owner of the process
4329   l_owner := l_requestor_name;
4330 
4331   Wf_Engine.SetItemOwner(itemtype =>l_itemtype,
4332                          itemkey => l_itemkey,
4333                          owner   => l_owner );
4334 
4335      -- start the process
4336        --
4337   Wf_Engine.StartProcess(itemtype => l_itemtype,
4338                          itemkey  => l_itemkey );
4339 
4340 
4341    l_appr_hist_rec.object_id          := p_act_budget_id;
4342    l_appr_hist_rec.object_type_code   := 'FUND';
4343    l_appr_hist_rec.sequence_num       := 0;
4344    l_appr_hist_rec.object_version_num := l_version;
4345    l_appr_hist_rec.action_code        := 'SUBMITTED';
4346    l_appr_hist_rec.action_date        := sysdate;
4347    l_appr_hist_rec.approver_id        := p_requestor_id;
4348    l_appr_hist_rec.note               := l_justification_text;
4349    l_appr_hist_rec.approval_detail_id := l_activity_detail_id;
4350    l_appr_hist_rec.approval_type      := 'BUDGET';
4351    l_appr_hist_rec.approver_type      := 'USER';
4352    --
4353    AMS_Appr_Hist_PVT.Create_Appr_Hist(
4354        p_api_version_number => 1.0,
4355        p_init_msg_list      => FND_API.G_FALSE,
4356        p_commit             => FND_API.G_FALSE,
4357        p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
4358        x_return_status      => l_return_status,
4359        x_msg_count          => l_msg_count,
4360        x_msg_data           => l_msg_data,
4361        p_appr_hist_rec      => l_appr_hist_rec
4362         );
4363 
4364    IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
4365      RAISE Fnd_Api.G_EXC_ERROR;
4366    END IF;
4367 
4368 EXCEPTION
4369   WHEN Fnd_Api.G_EXC_ERROR THEN
4370      -- raise error if not able to get the budget line
4371     IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
4372       Fnd_Message.set_name('AMS', 'AMS_BUDGET_LINE_START_ERROR');
4373       Fnd_Msg_Pub.ADD;
4374     END IF;
4375 
4376     Fnd_Msg_Pub.Count_And_Get (
4377                    p_encoded => Fnd_Api.G_FALSE,
4378                    p_count => l_msg_count,
4379                    p_data  => l_msg_data);
4380     Handle_Err
4381           (p_itemtype          => l_itemtype   ,
4382            p_itemkey           => l_itemkey    ,
4383            p_msg_count         => l_msg_count, -- Number of error Messages
4384            p_msg_data          => l_msg_data ,
4385            p_attr_name         => 'AMS_ERROR_MSG',
4386            x_error_msg         => l_error_msg);
4387         Wf_Core.context ('Ams_Approval_Pvt', 'Start_Process',
4388                  l_budget_type ,l_budget_id ,l_error_msg);
4389     RAISE;
4390   WHEN OTHERS THEN
4391     IF Fnd_Msg_Pub.Check_Msg_Level ( Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
4392     THEN
4393       Fnd_Msg_Pub.Add_Exc_Msg( G_PKG_NAME,l_api_name);
4394     END IF;
4395     Fnd_Msg_Pub.Count_And_Get (
4396                    p_encoded => Fnd_Api.G_FALSE,
4397                    p_count => l_msg_count,
4398                    p_data  => l_msg_data);
4399     Handle_Err
4400           (p_itemtype          => l_itemtype   ,
4401            p_itemkey           => l_itemkey    ,
4402            p_msg_count         => l_msg_count, -- Number of error Messages
4403            p_msg_data          => l_msg_data ,
4404            p_attr_name         => 'AMS_ERROR_MSG',
4405            x_error_msg         => l_error_msg);
4406     Wf_Core.context ('Ams_Approval_Pvt', 'Start_Process',
4407                  l_budget_type ,l_budget_id ,l_error_msg);
4408     RAISE;
4409 --
4410 END Start_Process;
4411 
4412 /*============================================================================*/
4413 --
4414 -- Procedure
4415 --      Start_LineApproval
4416 --
4417 -- Description
4418 --      get details of budget line(s)
4419 -- IN
4420 --   p_act_budget_id          - act budget line identifier
4421 --   p_parent_process_flag       - parent process exists flag
4422 --   p_parent_process_key        - parent process itemkey
4423 --   p_parent_context           - parent context
4424 --   p_parent_approval_flag      - parent process approved flag
4425 --
4426 /*============================================================================*/
4427 
4428 PROCEDURE Start_LineApproval(
4429     p_api_version                 IN  NUMBER
4430    ,p_init_msg_list               IN  VARCHAR2 := Fnd_Api.G_FALSE
4431    ,p_commit                      IN  VARCHAR2 := Fnd_Api.G_FALSE
4432    ,p_validation_level            IN  NUMBER   := Fnd_Api.G_VALID_LEVEL_FULL
4433 
4434    ,x_return_status               OUT NOCOPY VARCHAR2
4435    ,x_msg_data                    OUT NOCOPY VARCHAR2
4436    ,x_msg_count                   OUT NOCOPY NUMBER
4437 
4438    ,p_user_id                      IN  NUMBER
4439    ,p_act_budget_id                IN  NUMBER
4440    ,p_orig_status_id               IN  NUMBER
4441    ,p_new_status_id                IN  NUMBER
4442    ,p_rejected_status_id           IN  NUMBER
4443    ,p_parent_process_flag          IN  VARCHAR2 := Fnd_Api.G_FALSE
4444    ,p_parent_process_key           IN  VARCHAR2  -- was g_miss_char
4445    ,p_parent_context               IN  VARCHAR2  -- was g_miss_char
4446    ,p_parent_approval_flag         IN  VARCHAR2 := Fnd_Api.G_FALSE
4447    ,p_continue_flow                IN  VARCHAR2 := Fnd_Api.G_FALSE
4448 )
4449 IS
4450 l_api_name          CONSTANT VARCHAR2(30) := 'Start_LineApproval';
4451 l_api_version       CONSTANT NUMBER := 1.0;
4452 l_full_name         CONSTANT VARCHAR2(60) := G_PKG_NAME ||'.'|| l_api_name;
4453 --
4454 l_resource_id       NUMBER;
4455 l_user_id           NUMBER;
4456 l_login_user_id     NUMBER;
4457 l_login_user_status      VARCHAR2(30);
4458 l_Error_Msg              VARCHAR2(2000);
4459 l_Error_Token            VARCHAR2(80);
4460 l_object_version_number  NUMBER := 1;
4461 --
4462 BEGIN
4463      -- Standard begin of API savepoint
4464      SAVEPOINT  Start_LineApproval_PVT;
4465      -- Standard call to check for call compatibility.
4466      IF NOT Fnd_Api.Compatible_API_Call (
4467           l_api_version,
4468           p_api_version,
4469           l_api_name,
4470           G_PKG_NAME)
4471      THEN
4472           RAISE  Fnd_Api.G_EXC_UNEXPECTED_ERROR;
4473      END IF;
4474      -- Debug Message
4475      IF Fnd_Msg_Pub.Check_Msg_level (Fnd_Msg_Pub.G_MSG_LVL_DEBUG_LOW) THEN
4476           Fnd_Message.Set_Name('AMS','AMS_API_DEBUG_MESSAGE');
4477           Fnd_Message.Set_Token('ROW',l_full_name||': START');
4478           Fnd_Msg_Pub.ADD;
4479      END IF;
4480      --Initialize message list if p_init_msg_list is TRUE.
4481      IF Fnd_Api.To_Boolean (p_init_msg_list) THEN
4482           Fnd_Msg_Pub.initialize;
4483      END IF;
4484      -- Initialize API return status to sucess
4485      x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
4486 
4487       BEGIN
4488           -- kickoff workflow processes for a line
4489           Start_Process (
4490                p_requestor_id            => p_user_id,
4491                p_act_budget_id           => p_act_budget_id,
4492                p_orig_stat_id            => p_orig_status_id,
4493                p_new_stat_id             => p_new_status_id,
4494                p_rejected_stat_id        => p_rejected_status_id,
4495                p_parent_process_flag     => p_parent_process_flag,
4496                p_parent_process_key      => p_parent_process_key,
4497                p_parent_context          => p_parent_context,
4498                p_parent_approval_flag    => p_parent_approval_flag,
4499                p_continue_flow           => p_continue_flow );
4500      EXCEPTION
4501           WHEN OTHERS THEN
4502           Fnd_Message.Set_Name('AMS','AMS_API_DEBUG_MESSAGE');
4503           Fnd_Message.Set_Token('ROW',SQLERRM);
4504           Fnd_Msg_Pub.ADD;
4505           RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
4506      END;
4507 
4508      --Standard check of commit
4509      IF Fnd_Api.To_Boolean ( p_commit ) THEN
4510           COMMIT WORK;
4511      END IF;
4512      -- Debug Message
4513      IF Fnd_Msg_Pub.Check_Msg_level (Fnd_Msg_Pub.G_MSG_LVL_DEBUG_LOW) THEN
4514           Fnd_Message.Set_Name('AMS','AMS_API_DEBUG_MESSAGE');
4515           Fnd_Message.Set_Token('ROW',l_full_name||': END');
4516           Fnd_Msg_Pub.ADD;
4517      END IF;
4518      --Standard call to get message count and if count=1, get the message
4519      Fnd_Msg_Pub.Count_And_Get (
4520           p_encoded => Fnd_Api.G_FALSE,
4521           p_count => x_msg_count,
4522           p_data  => x_msg_data
4523      );
4524 EXCEPTION
4525      WHEN Fnd_Api.G_EXC_ERROR THEN
4526           ROLLBACK TO  Start_LineApproval_PVT;
4527           x_return_status := Fnd_Api.G_RET_STS_ERROR;
4528           -- Standard call to get message count and if count=1, get the message
4529           Fnd_Msg_Pub.Count_And_Get (
4530                p_encoded => Fnd_Api.G_FALSE,
4531                p_count => x_msg_count,
4532                p_data  => x_msg_data
4533           );
4534      WHEN Fnd_Api.G_EXC_UNEXPECTED_ERROR THEN
4535           ROLLBACK TO  Start_LineApproval_PVT;
4536           x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
4537           -- Standard call to get message count and if count=1, get the message
4538           Fnd_Msg_Pub.Count_And_Get (
4539                p_encoded => Fnd_Api.G_FALSE,
4540                p_count => x_msg_count,
4541                p_data  => x_msg_data
4542           );
4543      WHEN OTHERS THEN
4544           ROLLBACK TO  Start_LineApproval_PVT;
4545           x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
4546           IF Fnd_Msg_Pub.Check_Msg_Level ( Fnd_Msg_Pub.G_MSG_LVL_UNEXP_ERROR)
4547           THEN
4548                Fnd_Msg_Pub.Add_Exc_Msg( G_PKG_NAME,l_api_name);
4549           END IF;
4550           -- Standard call to get message count and if count=1, get the message
4551           Fnd_Msg_Pub.Count_And_Get (
4552                p_encoded => Fnd_Api.G_FALSE,
4553                p_count => x_msg_count,
4554                p_data  => x_msg_data
4555           );
4556 --
4557 END Start_LineApproval;
4558 
4559 /*============================================================================*/
4560 --
4561 -- Procedure
4562 --   Start_Line_Approval
4563 --
4564 --   Workflow cover: Get line details for an activity and start process for lines not acted on
4565 -- IN
4566 --   itemtype  - A valid item type from (WF_ITEM_TYPES table).
4567 --   itemkey   - A string generated from the application object's PRIMARY KEY.
4568 --   actid     - The function activity(instance id).
4569 --   funcmode  - Run/Cancel/Timeout
4570 -- OUT
4571 --   Resultout - 'COMPLETE:Y' If there is a parent process that started this process
4572 --             - 'COMPLETE:N' If there is no parent process and this process can end
4573 --
4574 -- USED BY ACTIVITIES
4575 --  <ITEM_TYPE>         <ACTIVITY>
4576 --  AMS_APPROVAL_PVT    AMS_START_LINE_APPROVAL
4577 --
4578 /*============================================================================*/
4579 
4580 PROCEDURE Start_Line_Approval( itemtype  IN  VARCHAR2,
4581                               itemkey    IN  VARCHAR2,
4582                               actid      IN  NUMBER,
4583                               funcmode   IN  VARCHAR2,
4584                               resultout  OUT NOCOPY VARCHAR2    )
4585 IS
4586 --
4587 l_return_status     VARCHAR2(1);
4588 l_msg_data          VARCHAR2(4000);
4589 l_msg_count         NUMBER;
4590 l_error_msg         VARCHAR2(4000);
4591 --
4592 l_approval_amount   NUMBER;
4593 l_activity_type     VARCHAR2(30);
4594 l_activity_id       NUMBER;
4595 l_act_budget_id     NUMBER;
4596 
4597 l_requestor_id      NUMBER;
4598 l_context           VARCHAR2(240);
4599 l_approval_flag     VARCHAR2(30);
4600 l_continue_flow     VARCHAR2(30);
4601 l_object_version_number  NUMBER;
4602 
4603 l_budget_rec          Ozf_Actbudgets_Pvt.Act_Budgets_Rec_Type;
4604 /* New cursor Definition */
4605 CURSOR planned_lines_csr(p_id in NUMBER, p_type in VARCHAR2) IS
4606   select activity_budget_id, object_version_number
4607   from   ozf_act_budgets
4608   where  act_budget_used_by_id = p_id
4609   and    arc_act_budget_used_by = p_type
4610   and    status_code = 'NEW'
4611   AND    transfer_type = 'REQUEST' ;
4612 
4613 BEGIN
4614 
4615   --
4616   -- RUN mode
4617   --
4618   IF (funcmode = 'RUN') THEN
4619      --resultout := 'COMPLETE:ERROR';
4620 
4621      -- get the activity id
4622        l_approval_amount := Wf_Engine.GetItemAttrNumber(
4623                               itemtype => itemtype,
4624                               itemkey => itemkey,
4625                               aname   => 'AMS_ACTIVITY_AMOUNT' );
4626 
4627      -- get the activity id
4628        l_activity_id := Wf_Engine.GetItemAttrNumber(
4629                               itemtype => itemtype,
4630                                 itemkey => itemkey,
4631                                 aname   => 'AMS_ACTIVITY_ID' );
4632 
4633      -- get the activity type
4634        l_activity_type := Wf_Engine.GetItemAttrText(
4635                               itemtype => itemtype,
4636                                 itemkey => itemkey,
4637                                 aname   => 'AMS_ACTIVITY_TYPE' );
4638 
4639      -- get the requestor id
4640        l_requestor_id := Wf_Engine.GetItemAttrNumber(
4641                               itemtype => itemtype,
4642                                 itemkey => itemkey,
4643                                 aname   => 'AMS_REQUESTER_ID' );
4644 
4645          -- check if activity meets the approval condition
4646        Get_Approval_Rules( p_activity_type  => l_activity_type ,
4647                           p_activity_id        => l_activity_id,
4648                           p_activity_amount    => l_approval_amount,
4649                           x_approved_flag      => l_approval_flag );
4650 
4651      -- set the parent context
4652      l_context := itemtype||':'||itemkey||':'||actid;
4653 
4654      -- set continue to true for first line if approval rule already met
4655      IF l_approval_flag = Fnd_Api.G_TRUE THEN
4656           l_continue_flow := Fnd_Api.G_TRUE;
4657      ELSE
4658           l_continue_flow := Fnd_Api.G_FALSE;
4659      END IF;
4660 
4661      -- get all planned budget lines
4662      OPEN planned_lines_csr(l_activity_id, l_activity_type);
4663       LOOP
4664         FETCH planned_lines_csr INTO  l_act_budget_id,l_object_version_number ;
4665         IF planned_lines_csr%NOTFOUND THEN
4666 	   EXIT;
4667 	END IF;
4668           -- process each line
4669           BEGIN
4670                   Ozf_Actbudgets_Pvt.Init_Act_Budgets_Rec(l_budget_rec);
4671 
4672                l_budget_rec.activity_budget_id := l_act_budget_id;
4673                l_budget_rec.status_code := 'APPROVED';
4674 --following block of code added for mumu  start
4675              l_budget_rec.user_status_id := Ams_Utility_Pvt.get_default_user_status (
4676                                                              'OZF_BUDGETSOURCE_STATUS',
4677                                                              l_budget_rec.status_code
4678                                                               );
4679   l_budget_rec.object_version_number := l_object_version_number;
4680 --following block of code added for mumu  END
4681 /* following code commented by mumu
4682                   AMS_ActBudgets_PVT.Complete_Act_Budgets_Rec(l_budget_rec,
4683                                             l_budget_rec );
4684 */
4685                -- start process for each lines activity lines for approval
4686                   Ozf_Actbudgets_Pvt.Update_Act_Budgets (
4687                      p_api_version             => 1.0,
4688                      p_init_msg_list           => Fnd_Api.G_FALSE,
4689                      p_commit                  => Fnd_Api.G_FALSE,
4690                      p_validation_level        => Fnd_Api.G_VALID_LEVEL_FULL,
4691                      x_return_status           => l_return_status,
4692                      x_msg_count               => l_msg_count,
4693                      x_msg_data                => l_msg_data,
4694                      p_act_Budgets_rec         => l_budget_rec,
4695                      p_parent_process_flag     => Fnd_Api.G_TRUE,
4696                      p_parent_process_key      => itemkey,
4697                      p_parent_context          => l_context,
4698                      p_parent_approval_flag    => l_approval_flag,
4699                      p_continue_flow           => l_continue_flow );
4700 
4701                IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
4702            --resultout := 'COMPLETE:ERROR';
4703                     -- raise exception
4704                    RAISE Fnd_Api.G_EXC_ERROR;
4705                END IF;
4706 
4707           EXCEPTION
4708              WHEN OTHERS THEN
4709 	       -- Commented for Bug 2485371
4710                --Fnd_Message.Set_Name('AMS','AMS_API_DEBUG_MESSAGE');
4711                --Fnd_Message.Set_Token('ROW', SQLERRM );
4712                --Fnd_Msg_Pub.ADD;
4713                RAISE Fnd_Api.G_EXC_ERROR;
4714                Fnd_Msg_Pub.Count_And_Get (
4715                              p_encoded => Fnd_Api.G_FALSE,
4716                              p_count => l_msg_count,
4717                              p_data  => l_msg_data);
4718                Handle_Err
4719                     (p_itemtype          => itemtype   ,
4720                      p_itemkey           => itemkey    ,
4721                      p_msg_count         => l_msg_count,
4722                      p_msg_data          => l_msg_data ,
4723                      p_attr_name         => 'AMS_ERROR_MSG',
4724                      x_error_msg         => l_error_msg);
4725           END;
4726 
4727           -- set continue flow for consequtive lines to false
4728           l_continue_flow := Fnd_Api.G_FALSE;
4729        END LOOP;
4730      CLOSE planned_lines_csr;
4731 
4732      resultout := 'COMPLETE:SUCCESS';
4733      RETURN;
4734   END IF;
4735 
4736   --
4737   -- CANCEL mode
4738   --
4739   IF (funcmode = 'CANCEL') THEN
4740         resultout := 'COMPLETE:';
4741         RETURN;
4742   END IF;
4743 
4744   --
4745   -- TIMEOUT mode
4746   --
4747   IF (funcmode = 'TIMEOUT') THEN
4748         resultout := 'COMPLETE:';
4749         RETURN;
4750   END IF;
4751   --
4752 
4753 EXCEPTION
4754   WHEN Fnd_Api.G_EXC_ERROR THEN
4755      -- raise error if not able to get the budget line
4756         -- Commented for Bug 2485371
4757         --IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
4758         --  Fnd_Message.set_name('AMS', 'AMS_BUDGET_LINE_UPDATE_ERROR');
4759         --  Fnd_Msg_Pub.ADD;
4760         --END IF;
4761         Fnd_Msg_Pub.Count_And_Get (
4762                    p_encoded => Fnd_Api.G_FALSE,
4763                    p_count => l_msg_count,
4764                    p_data  => l_msg_data);
4765         Handle_Err
4766           (p_itemtype          => itemtype   ,
4767            p_itemkey           => itemkey    ,
4768            p_msg_count         => l_msg_count, -- Number of error Messages
4769            p_msg_data          => l_msg_data ,
4770            p_attr_name         => 'AMS_ERROR_MSG',
4771            x_error_msg         => l_error_msg);
4772     resultout := 'COMPLETE:ERROR';
4773 /*        wf_core.context('AMS_APPROVAL_PVT',
4774                         'Start_Line_Approval',
4775                         itemtype, itemkey,to_char(actid),l_error_msg);
4776         RAISE;
4777     */
4778   WHEN OTHERS THEN
4779         Fnd_Msg_Pub.Count_And_Get (
4780                    p_encoded => Fnd_Api.G_FALSE,
4781                    p_count => l_msg_count,
4782                    p_data  => l_msg_data);
4783         Handle_Err
4784           (p_itemtype          => itemtype   ,
4785            p_itemkey           => itemkey    ,
4786            p_msg_count         => l_msg_count, -- Number of error Messages
4787            p_msg_data          => l_msg_data ,
4788            p_attr_name         => 'AMS_ERROR_MSG',
4789            x_error_msg         => l_error_msg);
4790         Wf_Core.context('AMS_APPROVAL_PVT',
4791                         'Start_Line_Approval',
4792                         itemtype, itemkey,TO_CHAR(actid),l_error_msg);
4793         RAISE;
4794   --
4795 END Start_Line_Approval;
4796 
4797 /*============================================================================*/
4798 --
4799 -- Procedure
4800 --   Get_Line_Approver_Details
4801 --
4802 --   Workflow cover: Get and set the details for a budget line to start approval process
4803 -- IN
4804 --   itemtype  - A valid item type from (WF_ITEM_TYPES table).
4805 --   itemkey   - A string generated from the application object's primary key.
4806 --   actid     - The function activity(instance id).
4807 --   funcmode  - Run/Cancel/Timeout
4808 -- OUT
4809 --   Resultout - 'COMPLETE:' After getting line approver details
4810 --
4811 -- USED BY ACTIVITIES
4812 --  <ITEM_TYPE>         <ACTIVITY>
4813 --  AMS_APPROVAL_PVT    AMS_GET_LINE_DETAILS
4814 --
4815 /*============================================================================*/
4816 
4817 PROCEDURE Get_Line_Approver_Details( itemtype        IN  VARCHAR2,
4818                            itemkey         IN  VARCHAR2,
4819                            actid           IN  NUMBER,
4820                            funcmode        IN  VARCHAR2,
4821                            resultout       OUT NOCOPY VARCHAR2    )
4822 IS
4823   l_approval_detail_id    NUMBER;
4824   l_budget_type           VARCHAR2(30);
4825   l_budget_id             NUMBER;
4826   l_current_seq           NUMBER;
4827   l_approver_id           NUMBER; --l_approver_id          VARCHAR2(30);--SVEERAVE,04/10/02
4828   l_approver              VARCHAR2(320); -- Was 100  VARCHAR2(30);--SVEERAVE,04/10/02
4829   l_approver_display_name VARCHAR2(360); -- Was 240
4830   l_notification_type     VARCHAR2(30);
4831   l_notification_timeout  NUMBER;
4832   l_approver_type         VARCHAR2(30);
4833   l_object_approver_id    NUMBER; --l_object_approver     VARCHAR2(30); --SVEERAVE,04/10/02
4834   l_role_name             VARCHAR2(100); --l_role_name           VARCHAR2(30);--SVEERAVE,04/10/02
4835   l_prev_role_name        VARCHAR2(100);--l_prev_role_name        VARCHAR2(30); --SVEERAVE,04/10/02
4836   l_return_status         VARCHAR2(1);
4837   l_msg_data              VARCHAR2(4000);
4838   l_msg_count             NUMBER;
4839   l_error_msg             VARCHAR2(4000);
4840   l_note                  VARCHAR2(4000);
4841   l_all_note              VARCHAR2(4000);
4842   l_activity_type         VARCHAR2(30);
4843   l_activity_id           NUMBER;
4844   dml_str                 VARCHAR2(2000);
4845   l_pkg_name              varchar2(80);
4846   l_proc_name             varchar2(80);
4847   l_appr_id               NUMBER;
4848   -- 11.5.9
4849   l_appr_seq              NUMBER;
4850   l_appr_type             VARCHAR2(30);
4851   l_obj_appr_id           NUMBER;
4852   l_act_budget_id         NUMBER;
4853   l_version               NUMBER;
4854   l_appr_hist_rec         AMS_Appr_Hist_Pvt.Appr_Hist_Rec_Type;
4855 
4856 CURSOR c_API_Name (rule_id_in IN NUMBER) is
4857 SELECT package_name, procedure_name
4858 FROM ams_object_rules_b
4859 WHERE OBJECT_RULE_ID = rule_id_in;
4860 
4861 -- 11.5.9
4862 CURSOR c_approver(rule_id IN NUMBER) IS
4863 SELECT approver_seq, approver_type, object_approver_id
4864 FROM ams_approvers
4865 WHERE ams_approval_detail_id = rule_id
4866 AND  active_flag = 'Y'
4867 AND  TRUNC(SYSDATE) BETWEEN TRUNC(NVL(start_date_active,SYSDATE -1 ))
4868 AND TRUNC(NVL(end_date_active,SYSDATE + 1));
4869 
4870 BEGIN
4871 
4872   --
4873   -- RUN mode
4874   --
4875   IF (funcmode = 'RUN') THEN
4876      -- get the approval detail id
4877     l_approval_detail_id := Wf_Engine.GetItemAttrNumber(
4878                                   itemtype => itemtype,
4879                                   itemkey => itemkey,
4880                                   aname   => 'AMS_ACTIVITY_DETAIL_ID'
4881                                   );
4882      -- get the budget type
4883     l_current_seq := Wf_Engine.GetItemAttrNumber(
4884                                   itemtype => itemtype,
4885                                   itemkey => itemkey,
4886                                   aname   => 'AMS_APPROVER_SEQ'
4887                                   );
4888 
4889       -- get the details for current approver
4890       Get_Approver_Info(p_approval_detail_id      => l_approval_detail_id,
4891                  p_current_seq           => l_current_seq,
4892                  x_approver_id           => l_approver_id,
4893                  x_approver_type         => l_approver_type,
4894                  x_role_name             => l_role_name,
4895                  x_object_approver_id    => l_object_approver_id, --l_object_approver,
4896                  x_notification_type     => l_notification_type,
4897                  x_notification_timeout  => l_notification_timeout,
4898                  x_return_status         => l_return_status
4899                  );
4900 
4901       IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
4902           -- raise error
4903         Fnd_Msg_Pub.Count_And_Get(p_encoded => Fnd_Api.G_FALSE,
4904                                   p_count => l_msg_count,
4905                                   p_data  => l_msg_data);
4906 
4907         Handle_Err (p_itemtype => itemtype   ,
4908           p_itemkey           => itemkey    ,
4909           p_msg_count         => l_msg_count, -- Number of error Messages
4910           p_msg_data          => l_msg_data ,
4911           p_attr_name         => 'AMS_ERROR_MSG',
4912           x_error_msg         => l_error_msg
4913         );
4914         resultout := 'COMPLETE:ERROR';
4915         RETURN;
4916       END IF;
4917 
4918        	-- 11.5.9
4919          -- Get the Activity Budget Id
4920 	 l_act_budget_id := Wf_Engine.GetItemAttrNumber(
4921                                   itemtype => itemtype,
4922                                   itemkey => itemkey,
4923                                   aname   => 'AMS_ACT_BUDGET_ID'
4924                                   );
4925 
4926 	 l_version := Wf_Engine.GetItemAttrNumber(
4927                                   itemtype => itemtype,
4928                                   itemkey => itemkey,
4929                                   aname   => 'AMS_OBJECT_VERSION_NUMBER'
4930                                   );
4931 
4932        -- Bug 2835488 Fix similar to 2729108
4933        IF l_current_seq = 1 OR
4934         Is_Min_Sequence(l_approval_detail_id, l_current_seq) THEN
4935 
4936 	 -- Set Record Attributes that won't change for each approver
4937          l_appr_hist_rec.object_id          := l_act_budget_id;
4938 	 l_appr_hist_rec.object_type_code   := 'FUND';
4939          l_appr_hist_rec.object_version_num := l_version;
4940          l_appr_hist_rec.action_code        := 'OPEN';
4941          l_appr_hist_rec.approval_type      := 'BUDGET';
4942 	 l_appr_hist_rec.approval_detail_id := l_approval_detail_id;
4943 
4944          OPEN c_approver(l_approval_detail_id);
4945          LOOP
4946          FETCH c_approver INTO l_appr_seq, l_appr_type, l_obj_appr_id;
4947          EXIT WHEN c_approver%NOTFOUND;
4948 
4949 	 -- Set Record Attributes that will change for each approver
4950          l_appr_hist_rec.sequence_num  := l_appr_seq;
4951          l_appr_hist_rec.approver_type := l_appr_type;
4952 	 l_appr_hist_rec.approver_id   := l_obj_appr_id;
4953 
4954          AMS_Appr_Hist_PVT.Create_Appr_Hist(
4955             p_api_version_number => 1.0,
4956             p_init_msg_list      => FND_API.G_FALSE,
4957             p_commit             => FND_API.G_FALSE,
4958             p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
4959             x_return_status      => l_return_status,
4960             x_msg_count          => l_msg_count,
4961             x_msg_data           => l_msg_data,
4962             p_appr_hist_rec      => l_appr_hist_rec
4963             );
4964 
4965          IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS  THEN
4966 	   CLOSE c_approver;
4967            RAISE Fnd_Api.G_EXC_ERROR;
4968          END IF;
4969 
4970 	 END LOOP;
4971          CLOSE c_approver;
4972        END IF;
4973 
4974    -- in case of functions, object_approver_id is the object_rule_id, and not resource/role id.
4975    -- and hence function needs to be executed, which should return resource_id
4976    -- and this is populated back into object_approver_id
4977    -- This block of code was added by VMODUR for Bug 2390070
4978 
4979          IF (l_approver_type = 'FUNCTION') then
4980                    OPEN c_API_Name(l_object_approver_id);
4981                    FETCH c_API_Name INTO l_pkg_name, l_proc_name;
4982                    IF (c_Api_Name%FOUND) THEN
4983                         dml_str := 'BEGIN ' || l_pkg_name||'.'||l_proc_name||'(:itemtype,:itemkey,:appr_id, :l_return_stat); END;';
4984                         EXECUTE IMMEDIATE dml_str USING IN itemtype,IN itemkey, OUT l_appr_id, OUT l_return_status;
4985 
4986                         IF (l_return_status = 'S') THEN
4987                           l_object_approver_id := l_appr_id;
4988                         ELSE
4989                           FND_MSG_PUB.Count_And_Get (
4990                                                    p_encoded => FND_API.G_FALSE,
4991                                                    p_count => l_msg_count,
4992                                                    p_data  => l_msg_data
4993                                                    );
4994                        Handle_Err
4995                          (p_itemtype          => itemtype   ,
4996                           p_itemkey           => itemkey    ,
4997                           p_msg_count         => l_msg_count, -- Number of error Messages
4998                           p_msg_data          => l_msg_data ,
4999                           p_attr_name         => 'AMS_ERROR_MSG',
5000                           x_error_msg         => l_error_msg);
5001 
5002                           resultout := 'COMPLETE:ERROR';
5003                           return;
5004                         END IF;
5005                    END IF;
5006                    CLOSE c_API_Name;
5007          END IF;
5008   -- End of Addition
5009 
5010       -- Setting up the role
5011       Get_User_Role
5012           (p_user_id             => l_object_approver_id,-- l_object_approver,
5013           x_role_name            => l_approver,
5014           x_role_display_name    => l_approver_display_name,
5015           x_return_status        => l_return_status
5016           );
5017 
5018       IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS  THEN
5019         RAISE Fnd_Api.G_EXC_ERROR;
5020       END IF;
5021 
5022       l_prev_role_name  := Wf_Engine.GetItemAttrText(
5023                                  itemtype => itemtype,
5024                                  itemkey  => itemkey,
5025                                  aname    => 'AMS_APPROVAL_ROLE' );
5026 
5027       Wf_Engine.SetItemAttrText(  itemtype => itemtype,
5028                                     itemkey  => itemkey,
5029                                     aname    => 'AMS_APPROVAL_ROLE',
5030                                     avalue   => l_role_name);
5031 
5032      -- get the activity id
5033     l_activity_id := Wf_Engine.GetItemAttrNumber(
5034                               itemtype => itemtype,
5035                                itemkey => itemkey,
5036                                aname   => 'AMS_ACTIVITY_ID' );
5037 
5038      -- get the activity type
5039     l_activity_type := Wf_Engine.GetItemAttrText(
5040                               itemtype => itemtype,
5041                               itemkey => itemkey,
5042                               aname   => 'AMS_ACTIVITY_TYPE' );
5043 
5044           -- get the activity note
5045     l_note  := Wf_Engine.GetItemAttrText(
5046                                  itemtype => itemtype,
5047                                  itemkey  => itemkey,
5048                                  aname    => 'AMS_NOTE' );
5049 
5050     IF l_note IS NOT NULL THEN
5051       Update_Note(
5052              p_activity_type => l_activity_type,
5053              p_activity_id   => l_activity_id,
5054              p_note          => l_note,
5055              p_user          => l_object_approver_id ,--l_object_approver,
5056              x_msg_count     => l_msg_count,
5057              x_msg_data     =>  l_msg_data,
5058              x_return_status => l_return_status);
5059     END IF;
5060 
5061     IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS  THEN
5062              RAISE Fnd_Api.G_EXC_ERROR;
5063     END IF;
5064 
5065           -- get all the budget line notes
5066     l_all_note    := Wf_Engine.GetItemAttrText(
5067                                  itemtype => itemtype,
5068                                  itemkey  => itemkey,
5069                                  aname    => 'AMS_ALL_NOTE' );
5070 
5071           -- NOTE another option is to get them from database and display
5072           -- issue : cannot distinguish from notes created by activities or budget lines
5073      -- option : can insert a carriage return when concaniting notes
5074     l_all_note := l_all_note || l_note;
5075 
5076     Wf_Engine.SetItemAttrText( itemtype => itemtype,
5077                      itemkey  => itemkey,
5078                      aname    => 'AMS_ALL_NOTE' ,
5079                      avalue   => l_all_note ) ;
5080 
5081     -- set the note to null
5082     l_note := NULL;
5083     Wf_Engine.SetItemAttrText( itemtype => itemtype,
5084                      itemkey  => itemkey,
5085                      aname    => 'AMS_NOTE' ,
5086                      avalue   => l_note ) ;
5087 
5088     Wf_Engine.SetItemAttrNumber(itemtype => itemtype,
5089                                     itemkey  => itemkey,
5090                                     aname    => 'AMS_APPROVER_ID',
5091                                     avalue   => l_object_approver_id);
5092                                     --avalue   => l_object_approver);
5093 
5094     -- Changed by VMODUR from l_approver to l_approver_display_name
5095     -- Unchanged
5096     Wf_Engine.SetItemAttrText(  itemtype => itemtype,
5097                                     itemkey  => itemkey,
5098                                     aname    => 'AMS_APPR_USERNAME',
5099                                     avalue   => l_approver);
5100 
5101     Wf_Engine.SetItemAttrText(  itemtype => itemtype,
5102                                     itemkey  => itemkey,
5103                                     aname    => 'AMS_APPR_DISPLAY_NAME',
5104                                     avalue   => l_approver_display_name);
5105 
5106     Wf_Engine.SetItemAttrText(  itemtype => itemtype,
5107                                     itemkey  => itemkey,
5108                                     aname    => 'AMS_NOTIFICATION_TYPE',
5109                                     avalue   => l_notification_type);
5110 
5111     Wf_Engine.SetItemAttrNumber(itemtype => itemtype,
5112                                     itemkey  => itemkey,
5113                                     aname    => 'AMS_NOTIFICATION_TIMEOUT',
5114                                     avalue   => l_notification_timeout);
5115 
5116           -- 11.5.9 Update the 'Open' row to 'Pending'
5117 
5118           l_appr_hist_rec.object_id := l_act_budget_id;
5119 	  l_appr_hist_rec.object_type_code := 'FUND';
5120           l_appr_hist_rec.object_version_num := l_version;
5121           l_appr_hist_rec.action_code := 'PENDING';
5122           l_appr_hist_rec.approval_type := 'BUDGET';
5123 	  l_appr_hist_rec.approver_type := l_approver_type;
5124 	  l_appr_hist_rec.sequence_num  := l_current_seq;
5125           l_appr_hist_rec.approver_id   := l_object_approver_id;
5126 
5127 
5128           AMS_Appr_Hist_PVT.Update_Appr_Hist(
5129              p_api_version_number => 1.0,
5130              p_init_msg_list      => FND_API.G_FALSE,
5131              p_commit             => FND_API.G_FALSE,
5132              p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
5133              x_return_status      => l_return_status,
5134              x_msg_count          => l_msg_count,
5135              x_msg_data           => l_msg_data,
5136              p_appr_hist_rec      => l_appr_hist_rec
5137              );
5138 
5139 	   IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
5140 	     RAISE Fnd_Api.G_EXC_ERROR;
5141 	   END IF;
5142 
5143     resultout := 'COMPLETE:SUCCESS';
5144     RETURN;
5145   END IF;
5146 
5147   --
5148   -- CANCEL mode
5149   --
5150   IF (funcmode = 'CANCEL') THEN
5151         resultout := 'COMPLETE:';
5152         RETURN;
5153   END IF;
5154 
5155   --
5156   -- TIMEOUT mode
5157   --
5158   IF (funcmode = 'TIMEOUT') THEN
5159         resultout := 'COMPLETE:';
5160         RETURN;
5161   END IF;
5162   --
5163 
5164 EXCEPTION
5165   WHEN Fnd_Api.G_EXC_ERROR THEN
5166     Fnd_Msg_Pub.Count_And_Get (
5167                    p_encoded => Fnd_Api.G_FALSE,
5168                    p_count => l_msg_count,
5169                    p_data  => l_msg_data);
5170     Handle_Err
5171         (p_itemtype          => itemtype   ,
5172         p_itemkey           => itemkey    ,
5173         p_msg_count         => l_msg_count, -- Number of error Messages
5174         p_msg_data          => l_msg_data ,
5175         p_attr_name         => 'AMS_ERROR_MSG',
5176         x_error_msg         => l_error_msg);
5177     resultout := 'COMPLETE:ERROR';
5178 
5179   WHEN OTHERS THEN
5180     Fnd_Msg_Pub.Count_And_Get (
5181                    p_encoded => Fnd_Api.G_FALSE,
5182                    p_count => l_msg_count,
5183                    p_data  => l_msg_data);
5184     Handle_Err
5185           (p_itemtype          => itemtype   ,
5186            p_itemkey           => itemkey    ,
5187            p_msg_count         => l_msg_count, -- Number of error Messages
5188            p_msg_data          => l_msg_data ,
5189            p_attr_name         => 'AMS_ERROR_MSG',
5190            x_error_msg         => l_error_msg);
5191     Wf_Core.context('AMS_APPROVAL_PVT',
5192                         'Get_Line_Approver_Details ' || l_approver || ' : ',
5193                         itemtype, itemkey,TO_CHAR(actid),l_error_msg);
5194     RAISE;
5195   --
5196 END Get_Line_Approver_Details;
5197 
5198 /*============================================================================*/
5199 --
5200 -- Procedure
5201 --   Check_Line_Further_Approval
5202 --
5203 --   Workflow cover: Check if line needs further approvals
5204 -- IN
5205 --   itemtype  - A valid item type from (WF_ITEM_TYPES table).
5206 --   itemkey   - A string generated from the application object's primary key.
5207 --   actid     - The function activity(instance id).
5208 --   funcmode  - Run/Cancel/Timeout
5209 -- OUT
5210 --   Resultout - 'COMPLETE:Y' If line needs further approvals
5211 --             - 'COMPLETE:N' If line does not need further approvals
5212 --
5213 -- USED BY ACTIVITIES
5214 --  <ITEM_TYPE>         <ACTIVITY>
5215 --  AMS_APPROVAL_PVT    AMS_GET_APPROVER_DETAILS
5216 --
5217 /*============================================================================*/
5218 
5219 PROCEDURE Check_Line_Further_Approval( itemtype      IN  VARCHAR2,
5220                                      itemkey         IN  VARCHAR2,
5221                                      actid           IN  NUMBER,
5222                                      funcmode        IN  VARCHAR2,
5223                                      resultout       OUT NOCOPY VARCHAR2    )
5224 IS
5225 l_act_budget_id          NUMBER;
5226 l_approval_detail_id     NUMBER;
5227 l_budget_type            VARCHAR2(30);
5228 l_budget_id              NUMBER;
5229 l_current_seq            NUMBER;
5230 l_next_seq               NUMBER;
5231 l_required_flag          VARCHAR2(30);
5232 l_msg_data               VARCHAR2(4000);
5233 l_msg_count              NUMBER;
5234 l_error_msg              VARCHAR2(4000);
5235 l_note                   VARCHAR2(4000);
5236 -- Added for 11.5.9
5237 l_approver_id            NUMBER;
5238 l_version                NUMBER;
5239 l_return_status          VARCHAR2(1);
5240 l_appr_hist_rec          AMS_Appr_Hist_Pvt.Appr_Hist_Rec_Type;
5241 l_new_approver_id        NUMBER;
5242 
5243 BEGIN
5244 
5245   --
5246   -- RUN mode
5247   --
5248   IF (funcmode = 'RUN') THEN
5249 
5250      -- get the approval detail id
5251        l_approval_detail_id := Wf_Engine.GetItemAttrNumber(
5252                               itemtype  => itemtype,
5253                                 itemkey => itemkey,
5254                                 aname   => 'AMS_ACTIVITY_DETAIL_ID' );
5255 
5256      -- get the budget type
5257        l_current_seq := Wf_Engine.GetItemAttrNumber(
5258                               itemtype => itemtype,
5259                               itemkey  => itemkey,
5260                               aname    => 'AMS_APPROVER_SEQ' );
5261 
5262      -- check if further approval is required
5263      Check_Approval_Required(p_approval_detail_id => l_approval_detail_id,
5264                     p_current_seq       => l_current_seq,
5265                     x_next_seq          => l_next_seq,
5266                     x_required_flag     => l_required_flag);
5267 
5268       -- Added for 11.5.9
5269       l_version := Wf_Engine.GetItemAttrNumber(
5270                               itemtype => itemtype,
5271                               itemkey => itemkey,
5272                               aname   => 'AMS_OBJECT_VERSION_NUMBER' );
5273 
5274       l_act_budget_id := Wf_Engine.GetItemAttrNumber(
5275                               itemtype => itemtype,
5276                               itemkey => itemkey,
5277                               aname   => 'AMS_ACT_BUDGET_ID' );
5278 
5279       l_approver_id   := Wf_Engine.GetItemAttrNumber(
5280                               itemtype => itemtype,
5281                               itemkey => itemkey,
5282                               aname   => 'AMS_APPROVER_ID');
5283 
5284       l_note            := Wf_Engine.GetItemAttrText(
5285                                  itemtype => itemtype,
5286                                  itemkey  => itemkey,
5287                                  aname    => 'AMS_NOTE' );
5288 
5289       -- Bug Fix 2677401 Nov-14-2003
5290       -- Stuck at Check Line Further Approval if first/last approver has a problem
5291 
5292       IF l_next_seq IS NULL AND l_approver_id IS NULL THEN
5293          resultout := 'COMPLETE:N';
5294          RETURN;
5295 
5296       -- Higher up approvers can still approve for Budget Lines
5297       -- If we don't return, the pending to approved update will fail
5298       -- due to lack of target as the current approver_id is null
5299 
5300       ELSIF l_next_seq IS NOT NULL and l_approver_id IS NULL THEN
5301          resultout := 'COMPLETE:Y';
5302          RETURN;
5303       END IF;
5304    -- Commented for bug 3150550
5305      -- Start of addition for forward/reassign notification
5306 /*
5307           Check_Reassigned (itemtype => itemtype,
5308                             itemkey  => itemkey,
5309                             x_approver_id   => l_new_approver_id,
5310                             x_return_status => l_return_status);
5311 
5312      IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
5313         RAISE Fnd_Api.G_EXC_ERROR;
5314      END IF;
5315 
5316      IF l_new_approver_id IS NOT NULL THEN
5317         l_approver_id := l_new_approver_id;
5318      END IF;
5319 
5320      -- End of addition for forward/re-assign notification
5321  -- End of Comment
5322  */
5323 
5324          -- update the record from 'PENDING' to 'APPROVED'
5325           l_appr_hist_rec.object_id := l_act_budget_id;
5326           l_appr_hist_rec.object_type_code := 'FUND';
5327           l_appr_hist_rec.object_version_num := l_version;
5328           l_appr_hist_rec.action_code := 'APPROVED';
5329           l_appr_hist_rec.approval_type := 'BUDGET';
5330           l_appr_hist_rec.sequence_num  := l_current_seq;
5331           l_appr_hist_rec.approver_id  := l_approver_id;
5332           l_appr_hist_rec.note := l_note;
5333           l_appr_hist_rec.action_date := SYSDATE;
5334 
5335 
5336           AMS_Appr_Hist_PVT.Update_Appr_Hist(
5337              p_api_version_number => 1.0,
5338              p_init_msg_list      => FND_API.G_FALSE,
5339              p_commit             => FND_API.G_FALSE,
5340              p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
5341              x_return_status      => l_return_status,
5342              x_msg_count          => l_msg_count,
5343              x_msg_data           => l_msg_data,
5344              p_appr_hist_rec      => l_appr_hist_rec
5345              );
5346 
5347           IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
5348             RAISE Fnd_Api.G_EXC_ERROR;
5349           END IF;
5350 
5351      --IF l_required_flag = FND_API.G_TRUE THEN
5352      IF l_next_seq IS NOT NULL THEN
5353            --
5354             Wf_Engine.SetItemAttrNumber(itemtype => itemtype,
5355                                              itemkey  => itemkey,
5356                                              aname    => 'AMS_APPROVER_SEQ',
5357                                              avalue   => l_next_seq);
5358 
5359              resultout := 'COMPLETE:Y';
5360              RETURN;
5361      ELSE
5362              resultout := 'COMPLETE:N';
5363              RETURN;
5364      END IF;
5365   END IF;
5366 
5367   --
5368   -- CANCEL mode
5369   --
5370   IF (funcmode = 'CANCEL') THEN
5371         resultout := 'COMPLETE:';
5372         RETURN;
5373   END IF;
5374 
5375   --
5376   -- TIMEOUT mode
5377   --
5378   IF (funcmode = 'TIMEOUT') THEN
5379         resultout := 'COMPLETE:';
5380         RETURN;
5381   END IF;
5382   --
5383 
5384 EXCEPTION
5385   WHEN Fnd_Api.G_EXC_ERROR THEN
5386         Fnd_Msg_Pub.Count_And_Get (
5387                    p_encoded => Fnd_Api.G_FALSE,
5388                    p_count => l_msg_count,
5389                    p_data  => l_msg_data);
5390         Handle_Err
5391           (p_itemtype          => itemtype   ,
5392            p_itemkey           => itemkey    ,
5393            p_msg_count         => l_msg_count, -- Number of error Messages
5394            p_msg_data          => l_msg_data ,
5395            p_attr_name         => 'AMS_ERROR_MSG',
5396            x_error_msg         => l_error_msg);
5397         Wf_Core.context('AMS_APPROVAL_PVT',
5398                         'Check_Line_Further_Approval',
5399                         itemtype, itemkey,TO_CHAR(actid),l_error_msg);
5400         RAISE;
5401   WHEN OTHERS THEN
5402         IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
5403           Fnd_Message.Set_Name('AMS','AMS_API_DEBUG_MESSAGE');
5404           Fnd_Message.Set_Token('ROW',SQLERRM);
5405           Fnd_Msg_Pub.ADD;
5406         END IF;
5407         Fnd_Msg_Pub.Count_And_Get (
5408                    p_encoded => Fnd_Api.G_FALSE,
5409                    p_count => l_msg_count,
5410                    p_data  => l_msg_data);
5411         Handle_Err
5412           (p_itemtype          => itemtype   ,
5413            p_itemkey           => itemkey    ,
5414            p_msg_count         => l_msg_count, -- Number of error Messages
5415            p_msg_data          => l_msg_data ,
5416            p_attr_name         => 'AMS_ERROR_MSG',
5417            x_error_msg         => l_error_msg);
5418         Wf_Core.context('AMS_APPROVAL_PVT',
5419                         'Check_Line_Further_Approval',
5420                         itemtype, itemkey,TO_CHAR(actid),l_error_msg);
5421         RAISE;
5422   --
5423 END Check_Line_Further_Approval;
5424 
5425 /*============================================================================*/
5426 --
5427 -- Procedure
5428 --   Approve_Budget_Line
5429 --
5430 --   Workflow cover: Approve a budget line
5431 -- IN
5432 --   itemtype  - A valid item type from (WF_ITEM_TYPES table).
5433 --   itemkey   - A string generated from the application object's primary key.
5434 --   actid     - The function activity(instance id).
5435 --   funcmode  - Run/Cancel/Timeout
5436 -- OUT
5437 --   Resultout - 'COMPLETE:' After approval
5438 --
5439 -- USED BY ACTIVITIES
5440 --  <ITEM_TYPE>         <ACTIVITY>
5441 --  AMS_APPROVAL_PVT    AMS_APPROVE_BUDGET_LINE
5442 --
5443 /*============================================================================*/
5444 
5445 PROCEDURE Approve_Budget_Line(itemtype  IN  VARCHAR2,
5446                              itemkey    IN  VARCHAR2,
5447                              actid      IN  NUMBER,
5448                              funcmode   IN  VARCHAR2,
5449                              resultout  OUT NOCOPY VARCHAR2    )
5450 IS
5451 --
5452 l_return_status     VARCHAR2(1);
5453 l_msg_data          VARCHAR2(4000);
5454 l_msg_count         NUMBER;
5455 l_error_msg         VARCHAR2(4000);
5456 --
5457 l_act_budget_id     NUMBER;
5458 l_approver_id       NUMBER;
5459 l_approved_amount   NUMBER;
5460 l_budget_amount     NUMBER;
5461 l_approver          VARCHAR2(100);
5462 l_text_value        VARCHAR2(2000);
5463 l_number_value      NUMBER;
5464 l_approved_currency VARCHAR2(30);
5465 l_approved_status_id    NUMBER;
5466 l_comment               VARCHAR2(4000); -- Was 2000 Bug 2991398
5467 
5468 l_approver_seq      NUMBER;
5469 l_version           NUMBER;
5470 --
5471 BEGIN
5472 
5473   --
5474   -- RUN mode
5475   --
5476   IF (funcmode = 'RUN') THEN
5477      -- get the budget id
5478        l_act_budget_id := Wf_Engine.GetItemAttrNumber(
5479                               itemtype => itemtype,
5480                                 itemkey => itemkey,
5481                                 aname   => 'AMS_ACT_BUDGET_ID' );
5482 
5483      -- get the approved id
5484        l_approver_id := Wf_Engine.GetItemAttrNumber(
5485                               itemtype => itemtype,
5486                                 itemkey => itemkey,
5487                                 aname   => 'AMS_APPROVER_ID' );
5488 
5489      -- get the approved amount
5490        l_approved_amount := Wf_Engine.GetItemAttrNumber(
5491                               itemtype => itemtype,
5492                                 itemkey => itemkey,
5493                                 aname   => 'AMS_APPROVED_LINE_AMOUNT' );
5494 
5495      -- get the approved currency
5496        l_approved_currency := Wf_Engine.GetItemAttrText(
5497                               itemtype => itemtype,
5498                                 itemkey => itemkey,
5499                                 aname   => 'AMS_CURRENCY' );
5500 
5501      -- get the approved status id
5502        l_approved_status_id := Wf_Engine.GetItemAttrNumber(
5503                                    itemtype => itemtype,
5504                                         itemkey => itemkey,
5505                                         aname   => 'AMS_LINE_NEW_STAT_ID' );
5506 
5507        l_comment := Wf_Engine.GetItemAttrText(
5508                               itemtype => itemtype,
5509                                 itemkey => itemkey,
5510                                 aname   => 'AMS_NOTE' );
5511 
5512        l_approver := wf_engine.GetItemAttrText(
5513                                itemtype => itemtype,
5514                                itemkey => itemkey,
5515                                aname => 'AMS_APPR_USERNAME');
5516 
5517        l_budget_amount := wf_engine.GetItemAttrNumber(
5518                               itemtype => itemtype,
5519                               itemkey => itemkey,
5520                               aname => 'AMS_BUDGET_AMOUNT_CONV');
5521 
5522        Get_Ntf_Rule_Values(l_approver,
5523                               'APPROVE',
5524                               l_text_value,
5525                               l_number_value);
5526 
5527        IF l_number_value > 0 THEN
5528          IF l_number_value > l_budget_amount THEN
5529            l_approved_amount := l_budget_amount;
5530          END IF;
5531        END IF;
5532 
5533      BEGIN
5534           -- update to approved
5535 -- Changed Hard code form USD l_approved_currency
5536           Ozf_Budgetapproval_Pvt.WF_Respond (
5537                p_api_version        => 1.0,
5538                p_init_msg_list      => Fnd_Api.G_FALSE,
5539                p_commit             => Fnd_Api.G_TRUE,
5540                p_validation_level   => Fnd_Api.G_VALID_LEVEL_FULL,
5541                x_return_status      => l_return_status,
5542                x_msg_count          => l_msg_count,
5543                x_msg_data           => l_msg_data,
5544                p_respond_status_id  => l_approved_status_id,
5545                p_activity_budget_id => l_act_budget_id,
5546                p_approver_id        => l_approver_id,
5547                p_approved_amount    => l_approved_amount,
5548                p_approved_currency  => l_approved_currency,
5549                p_comment            => l_comment);
5550 
5551           IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
5552             Fnd_Msg_Pub.Count_And_Get (
5553                    p_encoded => Fnd_Api.G_FALSE,
5554                    p_count => l_msg_count,
5555                    p_data  => l_msg_data);
5556     Handle_Err
5557     (p_itemtype         => itemtype   ,
5558     p_itemkey           => itemkey    ,
5559     p_msg_count         => l_msg_count, -- Number of error Messages
5560     p_msg_data          => l_msg_data ,
5561     p_attr_name         => 'AMS_ERROR_MSG',
5562     x_error_msg         => l_error_msg);
5563          resultout := 'COMPLETE:ERROR';
5564                RETURN;
5565 
5566           END IF;
5567      EXCEPTION
5568         WHEN OTHERS THEN
5569                Fnd_Message.Set_Name('AMS','AMS_API_DEBUG_MESSAGE');
5570           Fnd_Message.Set_Token('ROW', SQLERRM );
5571           Fnd_Msg_Pub.ADD;
5572             RAISE Fnd_Api.G_EXC_ERROR;
5573      END;
5574 
5575      resultout := 'COMPLETE:SUCCESS';
5576      RETURN;
5577 
5578   END IF;
5579 
5580   --
5581   -- CANCEL mode
5582   --
5583   IF (funcmode = 'CANCEL') THEN
5584         resultout := 'COMPLETE:';
5585         RETURN;
5586   END IF;
5587 
5588   --
5589   -- TIMEOUT mode
5590   --
5591   IF (funcmode = 'TIMEOUT') THEN
5592         resultout := 'COMPLETE:';
5593         RETURN;
5594   END IF;
5595   --
5596 
5597 EXCEPTION
5598   WHEN Fnd_Api.G_EXC_ERROR THEN
5599         Fnd_Msg_Pub.Count_And_Get (
5600                    p_encoded => Fnd_Api.G_FALSE,
5601                    p_count => l_msg_count,
5602                    p_data  => l_msg_data);
5603         Handle_Err
5604           (p_itemtype          => itemtype   ,
5605            p_itemkey           => itemkey    ,
5606            p_msg_count         => l_msg_count, -- Number of error Messages
5607            p_msg_data          => l_msg_data ,
5608            p_attr_name         => 'AMS_ERROR_MSG',
5609            x_error_msg         => l_error_msg);
5610         Wf_Core.context('AMS_APPROVAL_PVT',
5611                         'Approve_Budget_Line',
5612                         itemtype, itemkey,TO_CHAR(actid),l_error_msg);
5613         RAISE;
5614   WHEN OTHERS THEN
5615         Fnd_Msg_Pub.Count_And_Get (
5616                    p_encoded => Fnd_Api.G_FALSE,
5617                    p_count => l_msg_count,
5618                    p_data  => l_msg_data);
5619         Handle_Err
5620           (p_itemtype          => itemtype   ,
5621            p_itemkey           => itemkey    ,
5622            p_msg_count         => l_msg_count, -- Number of error Messages
5623            p_msg_data          => l_msg_data ,
5624            p_attr_name         => 'AMS_ERROR_MSG',
5625            x_error_msg         => l_error_msg);
5626         Wf_Core.context('AMS_APPROVAL_PVT',
5627                         'Approve_Budget_Line',
5628                         itemtype, itemkey,TO_CHAR(actid),l_error_msg);
5629         RAISE;
5630 END Approve_Budget_Line;
5631 
5632 /*============================================================================*/
5633 --
5634 -- Procedure
5635 --   Reject_Budget_Line
5636 --
5637 --   Workflow cover: Reject a budget line
5638 -- IN
5639 --   itemtype  - A valid item type from (WF_ITEM_TYPES table).
5640 --   itemkey   - A string generated from the application object's primary key.
5641 --   actid     - The function activity(instance id).
5642 --   funcmode  - Run/Cancel/Timeout
5643 -- OUT
5644 --   Resultout - 'COMPLETE:' After Rejection
5645 --
5646 -- USED BY ACTIVITIES
5647 --  <ITEM_TYPE>         <ACTIVITY>
5648 --  AMS_APPROVAL_PVT    AMS_REJECT_BUDGET_LINE
5649 --
5650 /*============================================================================*/
5651 
5652 PROCEDURE Reject_Budget_Line(itemtype  IN  VARCHAR2,
5653                             itemkey    IN  VARCHAR2,
5654                             actid      IN  NUMBER,
5655                             funcmode   IN  VARCHAR2,
5656                             resultout  OUT NOCOPY VARCHAR2    )
5657 IS
5658 --
5659 l_return_status     VARCHAR2(1);
5660 l_msg_data          VARCHAR2(4000);
5661 l_msg_count         NUMBER;
5662 l_error_msg         VARCHAR2(4000);
5663 --
5664 l_act_budget_id     NUMBER;
5665 l_approver_id       NUMBER;
5666 l_approved_amount   NUMBER;
5667 l_budget_amount     NUMBER;
5668 l_approver          VARCHAR2(100);
5669 l_text_value        VARCHAR2(2000);
5670 l_number_value      NUMBER;
5671 l_approved_currency       VARCHAR2(30);
5672 l_rejected_status_id      NUMBER;
5673 l_comment               VARCHAR2(4000); -- was 2000
5674 -- 11.5.9
5675 l_appr_hist_rec     AMS_Appr_Hist_Pvt.Appr_Hist_Rec_Type;
5676 l_version           NUMBER;
5677 l_approver_seq      NUMBER;
5678 l_note              VARCHAR2(4000);
5679 l_new_approver_id   NUMBER;
5680 
5681 BEGIN
5682 
5683   --
5684   -- RUN mode
5685   --
5686   IF (funcmode = 'RUN') THEN
5687      -- get the budget id
5688        l_act_budget_id := Wf_Engine.GetItemAttrNumber(
5689                               itemtype => itemtype,
5690                                 itemkey => itemkey,
5691                                 aname   => 'AMS_ACT_BUDGET_ID' );
5692 
5693      -- get the approved id
5694        l_approver_id := Wf_Engine.GetItemAttrNumber(
5695                               itemtype => itemtype,
5696                                    itemkey => itemkey,
5697                                    aname   => 'AMS_APPROVER_ID' );
5698 
5699      -- get the approved amount
5700        l_approved_amount := Wf_Engine.GetItemAttrNumber(
5701                               itemtype => itemtype,
5702                                    itemkey => itemkey,
5703                                    aname   => 'AMS_APPROVED_LINE_AMOUNT' );
5704 
5705      -- get the approved currency
5706        l_approved_currency := Wf_Engine.GetItemAttrText(
5707                               itemtype => itemtype,
5708                                    itemkey => itemkey,
5709                                    aname   => 'AMS_CURRENCY' );
5710 
5711      -- get the rejected status id
5712        l_rejected_status_id := Wf_Engine.GetItemAttrNumber(
5713                                    itemtype => itemtype,
5714                                         itemkey => itemkey,
5715                                         aname   => 'AMS_LINE_REJECT_STAT_ID' );
5716 
5717        l_comment := Wf_Engine.GetItemAttrText(
5718                               itemtype => itemtype,
5719                                 itemkey => itemkey,
5720                                 aname   => 'AMS_NOTE' );
5721 
5722        l_approver := wf_engine.GetItemAttrText(
5723                                itemtype => itemtype,
5724                                itemkey => itemkey,
5725                                aname => 'AMS_APPR_USERNAME');
5726 
5727        l_budget_amount := wf_engine.GetItemAttrNumber(
5728                               itemtype => itemtype,
5729                               itemkey => itemkey,
5730                               aname => 'AMS_BUDGET_AMOUNT_CONV');
5731 
5732        -- 11.5.9 version  sequence and note
5733        l_version := Wf_Engine.GetItemAttrNumber(
5734                                 itemtype => itemtype,
5735                                 itemkey => itemkey,
5736                                 aname   => 'AMS_OBJECT_VERSION_NUMBER' );
5737 
5738        l_approver_seq := Wf_Engine.GetItemAttrNumber(
5739                                 itemtype => itemtype,
5740                                 itemkey => itemkey,
5741                                 aname   => 'AMS_APPROVER_SEQ' );
5742 
5743        -- get the note
5744        l_note  := Wf_Engine.GetItemAttrText(
5745                                  itemtype => itemtype,
5746                                  itemkey  => itemkey,
5747                                  aname    => 'AMS_NOTE' );
5748 
5749        Get_Ntf_Rule_Values(l_approver,
5750                               'REJECT',
5751                               l_text_value,
5752                               l_number_value);
5753 
5754        IF l_number_value > 0 THEN
5755          IF l_number_value > l_budget_amount THEN
5756            l_approved_amount := l_budget_amount;
5757          END IF;
5758        END IF;
5759 
5760      BEGIN
5761           -- update to rejected
5762           Ozf_Budgetapproval_Pvt.WF_Respond (
5763                p_api_version        => 1.0,
5764                p_init_msg_list      => Fnd_Api.G_FALSE,
5765                p_commit             => Fnd_Api.G_TRUE,
5766                p_validation_level   => Fnd_Api.G_VALID_LEVEL_FULL,
5767                x_return_status      => l_return_status,
5768                x_msg_count          => l_msg_count,
5769                x_msg_data           => l_msg_data,
5770                p_respond_status_id  => l_rejected_status_id,
5771                p_activity_budget_id => l_act_budget_id,
5772                p_approver_id        => l_approver_id,
5773                p_approved_amount    => l_approved_amount,
5774                p_approved_currency  => l_approved_currency,
5775                p_comment => l_comment);
5776 
5777           IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
5778                Fnd_Msg_Pub.Count_And_Get (
5779                    p_encoded => Fnd_Api.G_FALSE,
5780                    p_count => l_msg_count,
5781                    p_data  => l_msg_data);
5782                Handle_Err
5783                    (p_itemtype          => itemtype   ,
5784                     p_itemkey           => itemkey    ,
5785                     p_msg_count         => l_msg_count,
5786                     p_msg_data          => l_msg_data ,
5787                     p_attr_name         => 'AMS_ERROR_MSG',
5788                     x_error_msg         => l_error_msg);
5789                resultout := 'COMPLETE:ERROR';
5790                RETURN;
5791           END IF;
5792 
5793      EXCEPTION
5794         WHEN OTHERS THEN
5795                Fnd_Message.Set_Name('AMS','AMS_API_DEBUG_MESSAGE');
5796           Fnd_Message.Set_Token('ROW', SQLERRM );
5797           Fnd_Msg_Pub.ADD;
5798             RAISE Fnd_Api.G_EXC_ERROR;
5799      END;
5800    -- Commented for bug 3150550
5801      -- Start of addition for forward/reassign notification
5802 /*
5803           Check_Reassigned (itemtype => itemtype,
5804                             itemkey  => itemkey,
5805                             x_approver_id => l_new_approver_id,
5806                             x_return_status => l_return_status);
5807 
5808      IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
5809       RAISE Fnd_Api.G_EXC_ERROR;
5810      END IF;
5811 
5812      IF l_new_approver_id IS NOT NULL THEN
5813         l_approver_id := l_new_approver_id;
5814      END IF;
5815      -- End of addition for forward/re-assign notification
5816 */
5817          -- update the record from 'PENDING' to 'REJECTED'
5818           l_appr_hist_rec.object_id := l_act_budget_id;
5819           l_appr_hist_rec.object_type_code := 'FUND';
5820           l_appr_hist_rec.object_version_num := l_version;
5821           l_appr_hist_rec.action_code := 'REJECTED';
5822           l_appr_hist_rec.approval_type := 'BUDGET';
5823           l_appr_hist_rec.sequence_num  := l_approver_seq;
5824           l_appr_hist_rec.note := l_note;
5825           -- bug 3161431
5826           l_appr_hist_rec.action_date := SYSDATE;
5827 
5828             -- should i reset approver_id? yes
5829           l_appr_hist_rec.approver_id  := l_approver_id;
5830 
5831           AMS_Appr_Hist_PVT.Update_Appr_Hist(
5832              p_api_version_number => 1.0,
5833              p_init_msg_list      => FND_API.G_FALSE,
5834              p_commit             => FND_API.G_FALSE,
5835              p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
5836              x_return_status      => l_return_status,
5837              x_msg_count          => l_msg_count,
5838              x_msg_data           => l_msg_data,
5839              p_appr_hist_rec      => l_appr_hist_rec
5840              );
5841 
5842            IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
5843              RAISE Fnd_Api.G_EXC_ERROR;
5844            END IF;
5845 
5846             -- Delete any 'OPEN' rows
5847            AMS_Appr_Hist_PVT.Delete_Appr_Hist(
5848              p_api_version_number => 1.0,
5849              p_init_msg_list      => FND_API.G_FALSE,
5850              p_commit             => FND_API.G_FALSE,
5851              p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
5852              x_return_status      => l_return_status,
5853              x_msg_count          => l_msg_count,
5854              x_msg_data           => l_msg_data,
5855              p_object_id          => l_act_budget_id,
5856              p_object_type_code   => 'FUND',
5857              p_sequence_num       => null, -- all open rows
5858              p_action_code        => 'OPEN',
5859              p_object_version_num => l_version,
5860              p_approval_type      => 'BUDGET');
5861 
5862            IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
5863             RAISE Fnd_Api.G_EXC_ERROR;
5864            END IF;
5865 
5866      resultout := 'COMPLETE:SUCCESS';
5867      RETURN;
5868 
5869   END IF;
5870 
5871   --
5872   -- CANCEL mode
5873   --
5874   IF (funcmode = 'CANCEL') THEN
5875         resultout := 'COMPLETE:';
5876         RETURN;
5877   END IF;
5878 
5879   --
5880   -- TIMEOUT mode
5881   --
5882   IF (funcmode = 'TIMEOUT') THEN
5883         resultout := 'COMPLETE:';
5884         RETURN;
5885   END IF;
5886   --
5887 
5888 EXCEPTION
5889   WHEN Fnd_Api.G_EXC_ERROR THEN
5890         Fnd_Msg_Pub.Count_And_Get (
5891                    p_encoded => Fnd_Api.G_FALSE,
5892                    p_count => l_msg_count,
5893                    p_data  => l_msg_data);
5894         Handle_Err
5895           (p_itemtype          => itemtype   ,
5896            p_itemkey           => itemkey    ,
5897            p_msg_count         => l_msg_count, -- Number of error Messages
5898            p_msg_data          => l_msg_data ,
5899            p_attr_name         => 'AMS_ERROR_MSG',
5900            x_error_msg         => l_error_msg);
5901         Wf_Core.context('AMS_APPROVAL_PVT',
5902                         'Reject_Budget_Line',
5903                         itemtype, itemkey,TO_CHAR(actid),l_error_msg);
5904         RAISE;
5905   WHEN OTHERS THEN
5906         Fnd_Msg_Pub.Count_And_Get (
5907                    p_encoded => Fnd_Api.G_FALSE,
5908                    p_count => l_msg_count,
5909                    p_data  => l_msg_data);
5910         Handle_Err
5911           (p_itemtype          => itemtype   ,
5912            p_itemkey           => itemkey    ,
5913            p_msg_count         => l_msg_count, -- Number of error Messages
5914            p_msg_data          => l_msg_data ,
5915            p_attr_name         => 'AMS_ERROR_MSG',
5916            x_error_msg         => l_error_msg);
5917         Wf_Core.context('AMS_APPROVAL_PVT',
5918                         'Reject_Budget_Line',
5919                         itemtype, itemkey,TO_CHAR(actid),l_error_msg);
5920         RAISE;
5921 END Reject_Budget_Line;
5922 
5923 /*============================================================================*/
5924 --
5925 -- Procedure
5926 --   Revert_Budget_Line
5927 --
5928 --   Workflow cover: Reject a budget line
5929 -- IN
5930 --   itemtype  - A valid item type from (WF_ITEM_TYPES table).
5931 --   itemkey   - A string generated from the application object's primary key.
5932 --   actid     - The function activity(instance id).
5933 --   funcmode  - Run/Cancel/Timeout
5934 -- OUT
5935 --   Resultout - 'COMPLETE:' After Rejection
5936 --
5937 -- USED BY ACTIVITIES
5938 --  <ITEM_TYPE>         <ACTIVITY>
5939 --  AMS_APPROVAL_PVT    AMS_REJECT_BUDGET_LINE
5940 --
5941 /*============================================================================*/
5942 
5943 PROCEDURE Revert_Budget_Line(itemtype   IN  VARCHAR2,
5944                             itemkey    IN  VARCHAR2,
5945                             actid      IN  NUMBER,
5946                             funcmode   IN  VARCHAR2,
5947                             resultout  OUT NOCOPY VARCHAR2    )
5948 IS
5949 --
5950 l_return_status     VARCHAR2(1);
5951 l_msg_data          VARCHAR2(4000);
5952 l_msg_count         NUMBER;
5953 l_error_msg         VARCHAR2(4000);
5954 --
5955 l_act_budget_id        NUMBER;
5956 l_approver_id          NUMBER;
5957 l_approved_amount      NUMBER;
5958 l_approved_currency    VARCHAR2(30);
5959 l_rejected_status_id   NUMBER;
5960 l_comment              VARCHAR2(4000); -- Was 2000
5961 l_version              NUMBER;
5962 
5963 BEGIN
5964 
5965   --
5966   -- RUN mode
5967   --
5968   IF (funcmode = 'RUN') THEN
5969      -- get the budget id
5970        l_act_budget_id := Wf_Engine.GetItemAttrNumber(
5971                               itemtype => itemtype,
5972                                 itemkey => itemkey,
5973                                 aname   => 'AMS_ACT_BUDGET_ID' );
5974 
5975      -- get the approved id
5976        l_approver_id := Wf_Engine.GetItemAttrNumber(
5977                               itemtype => itemtype,
5978                                    itemkey => itemkey,
5979                                    aname   => 'AMS_APPROVER_ID' );
5980 
5981      -- get the approved amount
5982        l_approved_amount := Wf_Engine.GetItemAttrNumber(
5983                               itemtype => itemtype,
5984                                itemkey => itemkey,
5985                                aname   => 'AMS_APPROVED_LINE_AMOUNT' );
5986 
5987      -- get the approved currency
5988        l_approved_currency := Wf_Engine.GetItemAttrText(
5989                               itemtype => itemtype,
5990                                    itemkey => itemkey,
5991                                    aname   => 'AMS_CURRENCY' );
5992 
5993      -- get the rejected status id
5994        l_rejected_status_id := Wf_Engine.GetItemAttrNumber(
5995                                    itemtype => itemtype,
5996                                         itemkey => itemkey,
5997                                         aname   => 'AMS_LINE_REJECT_STAT_ID' );
5998 
5999        l_comment := Wf_Engine.GetItemAttrText(
6000                               itemtype => itemtype,
6001                                 itemkey => itemkey,
6002                                 aname   => 'AMS_NOTE' );
6003      BEGIN
6004           -- update to rejected
6005           -- This API is being called with validation_level NONE
6006           -- to prevent errors from being thrown by ams_fund_pvt
6007           -- which compares object_amount and sum of the requested_amounts
6008           -- even during revert
6009 
6010           Ozf_Budgetapproval_Pvt.WF_Respond (
6011                p_api_version        => 1.0,
6012                p_init_msg_list      => Fnd_Api.G_FALSE,
6013                p_commit             => Fnd_Api.G_TRUE,
6014                p_validation_level   => Fnd_Api.G_VALID_LEVEL_NONE,
6015                x_return_status      => l_return_status,
6016                x_msg_count          => l_msg_count,
6017                x_msg_data           => l_msg_data,
6018                p_respond_status_id  => NULL,
6019                p_activity_budget_id => l_act_budget_id,
6020                p_approver_id        => l_approver_id,
6021                p_approved_amount    => l_approved_amount,
6022                p_approved_currency  => l_approved_currency,
6023                p_comment => l_comment);
6024 
6025           IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
6026             Fnd_Message.Set_Name('AMS','AMS_API_DEBUG_MESSAGE');
6027             --Fnd_Message.Set_Token('ROW', SQLERRM );
6028             Fnd_Msg_Pub.ADD;
6029 
6030           Fnd_Msg_Pub.Count_And_Get (
6031                    p_encoded => Fnd_Api.G_FALSE,
6032                    p_count => l_msg_count,
6033                    p_data  => l_msg_data);
6034             Handle_Err
6035             (p_itemtype          => itemtype   ,
6036              p_itemkey           => itemkey    ,
6037              p_msg_count         => l_msg_count, -- Number of error Messages
6038              p_msg_data          => l_msg_data ,
6039              p_attr_name         => 'AMS_ERROR_MSG',
6040              x_error_msg         => l_error_msg);
6041           resultout := 'COMPLETE:ERROR';
6042           RETURN;
6043 
6044           END IF;
6045 
6046      EXCEPTION
6047         WHEN OTHERS THEN
6048           Fnd_Message.Set_Name('AMS','AMS_API_DEBUG_MESSAGE');
6049           Fnd_Message.Set_Token('ROW', SQLERRM );
6050           Fnd_Msg_Pub.ADD;
6051             RAISE Fnd_Api.G_EXC_ERROR;
6052      END;
6053 
6054           -- 11.5.9 addition
6055           l_version     := Wf_Engine.GetItemAttrNumber(
6056                                  itemtype => itemtype,
6057                                  itemkey  => itemkey,
6058                                  aname    => 'AMS_OBJECT_VERSION_NUMBER' );
6059 
6060           -- Delete all rows
6061            AMS_Appr_Hist_PVT.Delete_Appr_Hist(
6062              p_api_version_number => 1.0,
6063              p_init_msg_list      => FND_API.G_FALSE,
6064              p_commit             => FND_API.G_FALSE,
6065              p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
6066              x_return_status      => l_return_status,
6067              x_msg_count          => l_msg_count,
6068              x_msg_data           => l_msg_data,
6069              p_object_id          => l_act_budget_id,
6070              p_object_type_code   => 'FUND',
6071              p_sequence_num       => null,
6072              p_action_code        => null,
6073              p_object_version_num => l_version,
6074              p_approval_type      => 'BUDGET');
6075 
6076           IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
6077              RAISE Fnd_Api.G_EXC_ERROR;
6078           END IF;
6079 
6080      -- end 11.5.9 addition
6081      resultout := 'COMPLETE:SUCCESS';
6082      RETURN;
6083 
6084   END IF;
6085 
6086   --
6087   -- CANCEL mode
6088   --
6089   IF (funcmode = 'CANCEL') THEN
6090         resultout := 'COMPLETE:';
6091         RETURN;
6092   END IF;
6093 
6094   --
6095   -- TIMEOUT mode
6096   --
6097   IF (funcmode = 'TIMEOUT') THEN
6098         resultout := 'COMPLETE:';
6099         RETURN;
6100   END IF;
6101   --
6102 
6103 EXCEPTION
6104   WHEN Fnd_Api.G_EXC_ERROR THEN
6105         Fnd_Msg_Pub.Count_And_Get (
6106                    p_encoded => Fnd_Api.G_FALSE,
6107                    p_count => l_msg_count,
6108                    p_data  => l_msg_data);
6109         Handle_Err
6110           (p_itemtype          => itemtype   ,
6111            p_itemkey           => itemkey    ,
6112            p_msg_count         => l_msg_count, -- Number of error Messages
6113            p_msg_data          => l_msg_data ,
6114            p_attr_name         => 'AMS_ERROR_MSG',
6115            x_error_msg         => l_error_msg);
6116         Wf_Core.context('AMS_APPROVAL_PVT',
6117                         'Revert_Budget_Line',
6118                         itemtype, itemkey,TO_CHAR(actid),l_error_msg);
6119         RAISE;
6120   WHEN OTHERS THEN
6121         Fnd_Msg_Pub.Count_And_Get (
6122                    p_encoded => Fnd_Api.G_FALSE,
6123                    p_count => l_msg_count,
6124                    p_data  => l_msg_data);
6125         Handle_Err
6126           (p_itemtype          => itemtype   ,
6127            p_itemkey           => itemkey    ,
6128            p_msg_count         => l_msg_count, -- Number of error Messages
6129            p_msg_data          => l_msg_data ,
6130            p_attr_name         => 'AMS_ERROR_MSG',
6131            x_error_msg         => l_error_msg);
6132         Wf_Core.context('AMS_APPROVAL_PVT',
6133                         'Reject_Budget_Line',
6134                         itemtype, itemkey,TO_CHAR(actid),l_error_msg);
6135         RAISE;
6136 END Revert_Budget_Line;
6137 
6138 
6139 --------------------------------------------------------------------------------
6140 --
6141 -- Procedure
6142 --   Is_Parent_Waiting
6143 --
6144 --   Workflow cover: Check if there is a parent procoess waiting for further process
6145 -- IN
6146 --   itemtype  - A valid item type from (WF_ITEM_TYPES table).
6147 --   itemkey   - A string generated from the application object's primary key.
6148 --   actid     - The function activity(instance id).
6149 --   funcmode  - Run/Cancel/Timeout
6150 -- OUT
6151 --   Resultout - 'COMPLETE:Y' If there is a parent process that started this process
6152 --             - 'COMPLETE:N' If there is no parent process and this process can end
6153 --
6154 -- USED BY ACTIVITIES
6155 --  <ITEM_TYPE>         <ACTIVITY>
6156 --  AMS_APPROVAL_PVT    AMS_PARENT_EXISTS
6157 --
6158 --------------------------------------------------------------------------------
6159 PROCEDURE Is_Parent_Waiting (itemtype       IN  VARCHAR2,
6160                             itemkey         IN  VARCHAR2,
6161                             actid           IN  NUMBER,
6162                             funcmode        IN  VARCHAR2,
6163                             resultout       OUT NOCOPY VARCHAR2)
6164 IS
6165 l_msg_data           VARCHAR2(4000);
6166 l_msg_count          NUMBER;
6167 l_error_msg          VARCHAR2(4000);
6168 l_parent_waiting_flag VARCHAR2(30);
6169 BEGIN
6170 
6171   --
6172   -- RUN mode
6173   --
6174   IF (funcmode = 'RUN') THEN
6175 
6176        l_parent_waiting_flag := Wf_Engine.GetItemAttrText(
6177                                itemtype => itemtype,
6178                                 itemkey => itemkey,
6179                                 aname   => 'AMS_PARENT_WAITING' );
6180 
6181      IF l_parent_waiting_flag = Fnd_Api.G_TRUE THEN
6182              resultout := 'COMPLETE:Y';
6183              RETURN;
6184      ELSE
6185              resultout := 'COMPLETE:N';
6186              RETURN;
6187      END IF;
6188 
6189   END IF;
6190 
6191   --
6192   -- CANCEL mode
6193   --
6194   IF (funcmode = 'CANCEL') THEN
6195         resultout := 'COMPLETE:';
6196         RETURN;
6197   END IF;
6198 
6199   --
6200   -- TIMEOUT mode
6201   --
6202   IF (funcmode = 'TIMEOUT') THEN
6203         resultout := 'COMPLETE:';
6204         RETURN;
6205   END IF;
6206   --
6207 
6208 EXCEPTION
6209   WHEN OTHERS THEN
6210         IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
6211           Fnd_Message.Set_Name('AMS','AMS_API_DEBUG_MESSAGE');
6212           Fnd_Message.Set_Token('ROW',SQLERRM);
6213           Fnd_Msg_Pub.ADD;
6214         END IF;
6215         Fnd_Msg_Pub.Count_And_Get (
6216                    p_encoded => Fnd_Api.G_FALSE,
6217                    p_count => l_msg_count,
6218                    p_data  => l_msg_data);
6219         Handle_Err
6220           (p_itemtype          => itemtype   ,
6221            p_itemkey           => itemkey    ,
6222            p_msg_count         => l_msg_count, -- Number of error Messages
6223            p_msg_data          => l_msg_data ,
6224            p_attr_name         => 'AMS_ERROR_MSG',
6225            x_error_msg         => l_error_msg);
6226         Wf_Core.context('AMS_APPROVAL_PVT',
6227                         'Is_Parent_Waiting',
6228                         itemtype, itemkey,TO_CHAR(actid),l_error_msg);
6229         RAISE;
6230 END Is_Parent_Waiting;
6231 --------------------------------------------------------------------------------
6232 --
6233 -- Procedure
6234 --   Check_Line_Approval_Rule
6235 --
6236 --   Workflow cover: Check if approval rule is met after action on a particular line
6237 -- IN
6238 --   itemtype  - A valid item type from (WF_ITEM_TYPES table).
6239 --   itemkey   - A string generated from the application object's primary key.
6240 --   actid     - The function activity(instance id).
6241 --   funcmode  - Run/Cancel/Timeout
6242 -- OUT
6243 --   Resultout - 'COMPLETE:Y' If there is a parent process that started this process
6244 --             - 'COMPLETE:N' If there is no parent process and this process can end
6245 --
6246 -- USED BY ACTIVITIES
6247 --  <ITEM_TYPE>         <ACTIVITY>
6248 --  AMS_APPROVAL_PVT    AMS_CHECK_LINE_APPROVAL_RULE
6249 --
6250 --------------------------------------------------------------------------------
6251 PROCEDURE Check_Line_Approval_Rule(itemtype      IN  VARCHAR2,
6252                                  itemkey         IN  VARCHAR2,
6253                                  actid           IN  NUMBER,
6254                                  funcmode        IN  VARCHAR2,
6255                                  resultout       OUT NOCOPY VARCHAR2    )
6256 IS
6257 l_msg_data           VARCHAR2(4000);
6258 l_msg_count          NUMBER;
6259 l_error_msg          VARCHAR2(4000);
6260 l_approval_amount    NUMBER;
6261 l_activity_type      VARCHAR2(30);
6262 l_activity_id        NUMBER;
6263 
6264 l_approval_flag          VARCHAR2(30);
6265 l_continue_flag          VARCHAR2(30);
6266 l_parent_approval_flag   VARCHAR2(30);
6267 
6268 BEGIN
6269 
6270   --
6271   -- RUN mode
6272   --
6273   IF (funcmode = 'RUN') THEN
6274      -- get the approval amount
6275        l_approval_amount := Wf_Engine.GetItemAttrNumber(
6276                               itemtype => itemtype,
6277                                 itemkey => itemkey,
6278                                 aname   => 'AMS_ACTIVITY_AMOUNT' );
6279 
6280      -- get the activity id
6281        l_activity_id := Wf_Engine.GetItemAttrNumber(
6282                               itemtype => itemtype,
6283                                 itemkey => itemkey,
6284                                 aname   => 'AMS_ACTIVITY_ID' );
6285 
6286      -- get the activity type
6287        l_activity_type := Wf_Engine.GetItemAttrText(
6288                                 itemtype => itemtype,
6289                                 itemkey  => itemkey,
6290                                 aname    => 'AMS_ACTIVITY_TYPE' );
6291 
6292      -- get parent process approved flag
6293        l_parent_approval_flag := Wf_Engine.GetItemAttrText(
6294                                itemtype => itemtype,
6295                                 itemkey => itemkey,
6296                                 aname   => 'AMS_PARENT_APPROVED' );
6297 
6298      -- set the continue flow (if continues) to second continue flow
6299        --
6300        Wf_Engine.SetItemAttrText(itemtype  => itemtype,
6301                                  itemkey   => itemkey,
6302                                  aname     => 'AMS_CONTINUEFLOW',
6303                                  avalue    => 'CONTINUEFLOW');
6304 
6305      -- check condition for approval flag coming from parent
6306      IF l_parent_approval_flag = Fnd_Api.G_FALSE THEN
6307             -- check if activity meets the approval condition
6308            Get_Approval_Rules(p_activity_type  => l_activity_type ,
6309                               p_activity_id        => l_activity_id,
6310                               p_activity_amount    => l_approval_amount,
6311                               x_approved_flag      => l_approval_flag );
6312 
6313          IF l_approval_flag = Fnd_Api.G_TRUE THEN
6314           -- set continue to true
6315           l_continue_flag := Fnd_Api.G_TRUE;
6316 
6317             Wf_Engine.SetItemAttrText(itemtype => itemtype,
6318                                               itemkey  => itemkey,
6319                                               aname    => 'AMS_CONTINUE_FLOW',
6320                                               avalue   => l_continue_flag);
6321 
6322              resultout := 'COMPLETE:Y';
6323              RETURN;
6324          ELSE
6325 
6326           --
6327              resultout := 'COMPLETE:N';
6328              RETURN;
6329          END IF;
6330 
6331      ELSE
6332           --
6333              resultout := 'COMPLETE:Y';
6334              RETURN;
6335      END IF;
6336 
6337   END IF;
6338 
6339   --
6340   -- CANCEL mode
6341   --
6342   IF (funcmode = 'CANCEL') THEN
6343         resultout := 'COMPLETE:';
6344         RETURN;
6345   END IF;
6346 
6347   --
6348   -- TIMEOUT mode
6349   --
6350   IF (funcmode = 'TIMEOUT') THEN
6351         resultout := 'COMPLETE:';
6352         RETURN;
6353   END IF;
6354   --
6355 
6356 EXCEPTION
6357   WHEN OTHERS THEN
6358         IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
6359           Fnd_Message.Set_Name('AMS','AMS_API_DEBUG_MESSAGE');
6360           Fnd_Message.Set_Token('ROW',SQLERRM);
6361           Fnd_Msg_Pub.ADD;
6362         END IF;
6363         Fnd_Msg_Pub.Count_And_Get (
6364                    p_encoded => Fnd_Api.G_FALSE,
6365                    p_count => l_msg_count,
6366                    p_data  => l_msg_data);
6367         Handle_Err
6368           (p_itemtype          => itemtype   ,
6369            p_itemkey           => itemkey    ,
6370            p_msg_count         => l_msg_count, -- Number of error Messages
6371            p_msg_data          => l_msg_data ,
6372            p_attr_name         => 'AMS_ERROR_MSG',
6373            x_error_msg         => l_error_msg);
6374         Wf_Core.context('AMS_APPROVAL_PVT',
6375                         'Check_Line_Approval_Rule',
6376                         itemtype, itemkey,TO_CHAR(actid),l_error_msg);
6377         RAISE;
6378 END Check_Line_Approval_Rule;
6379 --------------------------------------------------------------------------------
6380 --
6381 -- Procedure
6382 --   Check_More_Lines_Remaining
6383 --
6384 --   Workflow cover: Check if more lines are remaining for approval
6385 -- IN
6386 --   itemtype  - A valid item type from (WF_ITEM_TYPES table).
6387 --   itemkey   - A string generated from the application object's primary key.
6388 --   actid     - The function activity(instance id).
6389 --   funcmode  - Run/Cancel/Timeout
6390 -- OUT
6391 --   Resultout - 'COMPLETE:Y' If there is a parent process that started this process
6392 --             - 'COMPLETE:N' If there is no parent process and this process can end
6393 --
6394 -- USED BY ACTIVITIES
6395 --  <ITEM_TYPE>         <ACTIVITY>
6396 --  AMS_APPROVAL_PVT    AMS_CHECK_MORE_LINES_REMAINING
6397 --
6398 --------------------------------------------------------------------------------
6399 PROCEDURE Check_More_Lines_Remaining(itemtype        IN  VARCHAR2,
6400                                  itemkey         IN  VARCHAR2,
6401                                  actid        IN  NUMBER,
6402                                  funcmode        IN  VARCHAR2,
6403                                  resultout       OUT NOCOPY VARCHAR2    )
6404 IS
6405 l_msg_data           VARCHAR2(4000);
6406 l_msg_count          NUMBER;
6407 l_error_msg          VARCHAR2(4000);
6408 l_activity_type      VARCHAR2(30);
6409 l_activity_id        NUMBER;
6410 l_line_id            NUMBER;
6411 l_remaining_flag     VARCHAR2(30);
6412 l_continue_flag      VARCHAR2(30);
6413 
6414 CURSOR lines_due_csr(p_id IN NUMBER, p_type IN VARCHAR2) IS
6415 SELECT activity_budget_id
6416 FROM   ozf_act_budgets
6417 WHERE  act_budget_used_by_id = p_id
6418 AND    arc_act_budget_used_by = p_type
6419 AND    status_code = 'PENDING';
6420 
6421 BEGIN
6422 
6423   --
6424   -- RUN mode
6425   --
6426   IF (funcmode = 'RUN') THEN
6427 
6428      -- get the activity id
6429        l_activity_id := Wf_Engine.GetItemAttrNumber(
6430                               itemtype => itemtype,
6431                                 itemkey => itemkey,
6432                                 aname   => 'AMS_ACTIVITY_ID' );
6433 
6434      -- get the activity type
6435        l_activity_type := Wf_Engine.GetItemAttrText(
6436                               itemtype => itemtype,
6437                                 itemkey => itemkey,
6438                                 aname   => 'AMS_ACTIVITY_TYPE' );
6439 
6440      -- check if more lines exist for approval
6441      OPEN lines_due_csr(l_activity_id, l_activity_type);
6442              FETCH lines_due_csr INTO l_line_id;
6443      CLOSE lines_due_csr;
6444 
6445      IF l_line_id IS NOT NULL THEN
6446           l_remaining_flag := Fnd_Api.G_TRUE;
6447      ELSE
6448           l_remaining_flag := Fnd_Api.G_FALSE;
6449      END IF;
6450 
6451      -- continue to parent if no more lines remain
6452      IF l_remaining_flag = Fnd_Api.G_FALSE THEN
6453             -- set continue to true
6454           l_continue_flag := Fnd_Api.G_TRUE;
6455 
6456             Wf_Engine.SetItemAttrText(itemtype => itemtype,
6457                                         itemkey  => itemkey,
6458                                     aname    => 'AMS_CONTINUE_FLOW',
6459                                     avalue   => l_continue_flag);
6460              resultout := 'COMPLETE:N';
6461              RETURN;
6462      ELSE
6463              resultout := 'COMPLETE:Y';
6464              RETURN;
6465      END IF;
6466      --
6467   END IF;
6468 
6469   --
6470   -- CANCEL mode
6471   --
6472   IF (funcmode = 'CANCEL') THEN
6473         resultout := 'COMPLETE:';
6474         RETURN;
6475   END IF;
6476 
6477   --
6478   -- TIMEOUT mode
6479   --
6480   IF (funcmode = 'TIMEOUT') THEN
6481         resultout := 'COMPLETE:';
6482         RETURN;
6483   END IF;
6484   --
6485 
6486 EXCEPTION
6487   WHEN OTHERS THEN
6488         IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
6489           Fnd_Message.Set_Name('AMS','AMS_API_DEBUG_MESSAGE');
6490           Fnd_Message.Set_Token('ROW',SQLERRM);
6491           Fnd_Msg_Pub.ADD;
6492         END IF;
6493         Fnd_Msg_Pub.Count_And_Get (
6494                    p_encoded => Fnd_Api.G_FALSE,
6495                    p_count => l_msg_count,
6496                    p_data  => l_msg_data);
6497         Handle_Err
6498           (p_itemtype          => itemtype   ,
6499            p_itemkey           => itemkey    ,
6500            p_msg_count         => l_msg_count, -- Number of error Messages
6501            p_msg_data          => l_msg_data ,
6502            p_attr_name         => 'AMS_ERROR_MSG',
6503            x_error_msg         => l_error_msg);
6504         Wf_Core.context('AMS_APPROVAL_PVT',
6505                         'Check_More_Lines_Remaining',
6506                         itemtype, itemkey,TO_CHAR(actid),l_error_msg);
6507         RAISE;
6508 END Check_More_Lines_Remaining;
6509 --------------------------------------------------------------------------------
6510 --
6511 -- Procedure
6512 --   Can_Continue_Flow
6513 --
6514 --   Workflow cover: Check if this process can continue the flow of the main process
6515 -- IN
6516 --   itemtype  - A valid item type from (WF_ITEM_TYPES table).
6517 --   itemkey   - A string generated from the application object's primary key.
6518 --   actid     - The function activity(instance id).
6519 --   funcmode  - Run/Cancel/Timeout
6520 -- OUT
6521 --   Resultout - 'COMPLETE:Y' If there is a parent process that started this process
6522 --             - 'COMPLETE:N' If there is no parent process and this process can end
6523 --
6524 -- USED BY ACTIVITIES
6525 --  <ITEM_TYPE>         <ACTIVITY>
6526 --  AMS_APPROVAL_PVT    AMS_PARENT_EXISTS
6527 --
6528 --------------------------------------------------------------------------------
6529 PROCEDURE Can_Continue_Flow (itemtype       IN  VARCHAR2,
6530                             itemkey         IN  VARCHAR2,
6531                             actid           IN  NUMBER,
6532                             funcmode        IN  VARCHAR2,
6533                             resultout       OUT NOCOPY VARCHAR2    )
6534 IS
6535 l_msg_data         VARCHAR2(4000);
6536 l_msg_count        NUMBER;
6537 l_error_msg        VARCHAR2(4000);
6538 l_continue_flag    VARCHAR2(30);
6539 l_parent_approved  VARCHAR2(30);
6540 l_itemkey          VARCHAR2(80);
6541 l_parent_itemkey   VARCHAR2(80);
6542 l_activity_id      NUMBER;
6543 l_activity_type    VARCHAR2(30);
6544 l_line_id          NUMBER;
6545 l_version          NUMBER;
6546 
6547 CURSOR pending_lines_csr(p_id IN NUMBER, p_type IN VARCHAR2) IS
6548 SELECT activity_budget_id
6549 ,      object_version_NUMBER
6550 FROM   ozf_act_budgets
6551 WHERE  act_budget_used_by_id = p_id
6552 AND    arc_act_budget_used_by = p_type
6553 AND    status_code = 'PENDING';
6554 
6555 BEGIN
6556 
6557   --
6558   -- RUN mode
6559   --
6560   IF (funcmode = 'RUN') THEN
6561 
6562        l_continue_flag := Wf_Engine.GetItemAttrText(
6563                                itemtype => itemtype,
6564                                 itemkey => itemkey,
6565                                 aname   => 'AMS_CONTINUE_FLOW' );
6566 
6567      IF l_continue_flag = Fnd_Api.G_TRUE THEN
6568           -- get the activity id
6569             l_activity_id := Wf_Engine.GetItemAttrNumber(
6570                               itemtype => itemtype,
6571                               itemkey => itemkey,
6572                               aname   => 'AMS_ACTIVITY_ID' );
6573 
6574           -- get the activity id
6575             l_activity_type := Wf_Engine.GetItemAttrText(
6576                               itemtype => itemtype,
6577                               itemkey => itemkey,
6578                               aname   => 'AMS_ACTIVITY_TYPE' );
6579 
6580           -- get the activity type
6581             l_parent_itemkey := Wf_Engine.GetItemAttrText(
6582                               itemtype => itemtype,
6583                                 itemkey => itemkey,
6584                                 aname   => 'AMS_PARENT_ITEMKEY' );
6585 
6586           -- set continue flow to FALSE for other lines in the same activity
6587           --
6588           l_continue_flag := Fnd_Api.G_FALSE;
6589           l_parent_approved := Fnd_Api.G_TRUE;
6590 
6591           OPEN pending_lines_csr(l_activity_id, l_activity_type);
6592              LOOP
6593                FETCH pending_lines_csr INTO l_line_id, l_version;
6594                EXIT WHEN pending_lines_csr%NOTFOUND;
6595 
6596                -- derive the itemkeys that were set while creating them
6597                l_itemkey     := l_line_id||'_'||l_parent_itemkey;
6598 
6599                  Wf_Engine.SetItemAttrText ( itemtype => itemtype,
6600                                                  itemkey  => l_itemkey,
6601                                                  aname    => 'AMS_CONTINUE_FLOW',
6602                                                  avalue   => l_continue_flag);
6603 
6604                  Wf_Engine.SetItemAttrText ( itemtype => itemtype,
6605                                                  itemkey  => l_itemkey,
6606                                                  aname    => 'AMS_PARENT_APPROVED',
6607                                                  avalue   => l_parent_approved);
6608              END LOOP;
6609           CLOSE pending_lines_csr;
6610 
6611              resultout := 'COMPLETE:Y';
6612              RETURN;
6613      ELSE
6614              resultout := 'COMPLETE:N';
6615              RETURN;
6616      END IF;
6617 
6618   END IF;  -- end RUN mode
6619 
6620   --
6621   -- CANCEL mode
6622   --
6623   IF (funcmode = 'CANCEL') THEN
6624         resultout := 'COMPLETE:';
6625         RETURN;
6626   END IF;
6627 
6628   --
6629   -- TIMEOUT mode
6630   --
6631   IF (funcmode = 'TIMEOUT') THEN
6632         resultout := 'COMPLETE:';
6633         RETURN;
6634   END IF;
6635   --
6636 
6637 EXCEPTION
6638   WHEN OTHERS THEN
6639         IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
6640           Fnd_Message.Set_Name('AMS','AMS_API_DEBUG_MESSAGE');
6641           Fnd_Message.Set_Token('ROW',SQLERRM);
6642           Fnd_Msg_Pub.ADD;
6643         END IF;
6644         Fnd_Msg_Pub.Count_And_Get (
6645                    p_encoded => Fnd_Api.G_FALSE,
6646                    p_count => l_msg_count,
6647                    p_data  => l_msg_data);
6648         Handle_Err
6649           (p_itemtype          => itemtype   ,
6650            p_itemkey           => itemkey    ,
6651            p_msg_count         => l_msg_count, -- Number of error Messages
6652            p_msg_data          => l_msg_data ,
6653            p_attr_name         => 'AMS_ERROR_MSG',
6654            x_error_msg         => l_error_msg);
6655         Wf_Core.context('AMS_APPROVAL_PVT',
6656                         'Can_Continue_Flow',
6657                         itemtype, itemkey,TO_CHAR(actid),l_error_msg);
6658         RAISE;
6659 END Can_Continue_Flow;
6660 
6661 ------------------------------------------------------------------------------
6662 --
6663 -- Procedure
6664 --   Continue_Parent_Process
6665 --
6666 --   Workflow cover: continues the parent process from the block state
6667 -- IN
6668 --   itemtype  - A valid item type from (WF_ITEM_TYPES table).
6669 --   itemkey   - A string generated from the application object's primary key.
6670 --   actid     - The function activity(instance id).
6671 --   funcmode  - Run/Cancel/Timeout
6672 -- OUT
6673 --   Resultout - 'COMPLETE:' none
6674 --
6675 -- USED BY ACTIVITIES
6676 --  <ITEM_TYPE>         <ACTIVITY>
6677 --  AMS_APPROVAL_PVT    AMS_CONTINUE_PARENT
6678 --
6679 --------------------------------------------------------------------------------
6680 PROCEDURE Continue_Parent_Process (itemtype    IN  VARCHAR2,
6681                                    itemkey     IN  VARCHAR2,
6682                                    actid       IN  NUMBER,
6683                                    funcmode    IN  VARCHAR2,
6684                                    resultout   OUT NOCOPY VARCHAR2    )
6685 IS
6686 l_msg_data           VARCHAR2(4000);
6687 l_msg_count          NUMBER;
6688 l_error_msg          VARCHAR2(4000);
6689 l_parent_itemkey     VARCHAR2(80);
6690 l_parent_process     VARCHAR2(80) := 'AMS_APPROVAL';
6691 l_activity_label     VARCHAR2(30) := 'BLOCK';
6692 BEGIN
6693 
6694   --
6695   -- RUN mode
6696   --
6697   IF (funcmode = 'RUN') THEN
6698      -- get the activity type
6699        l_parent_itemkey := Wf_Engine.GetItemAttrText(
6700                              itemtype => itemtype,
6701                              itemkey =>  itemkey,
6702                              aname   =>  'AMS_PARENT_ITEMKEY' );
6703 
6704      BEGIN
6705          /* Added for zero budget Approval */
6706           Wf_Engine.BeginActivity(itemtype => g_itemtype,
6707                     itemkey  => l_parent_itemkey,
6708                     activity => l_parent_process||':'||l_activity_label
6709                     );
6710           Wf_Engine.CompleteActivity(itemtype => g_itemtype,
6711                     itemkey  => l_parent_itemkey,
6712                     activity => l_parent_process||':'||l_activity_label,
6713                     result   => 'COMPLETE:');
6714      EXCEPTION
6715           WHEN OTHERS THEN
6716              Wf_Core.clear;
6717      END;
6718 
6719          resultout := 'COMPLETE:';
6720      RETURN;
6721   END IF;  -- end RUN mode
6722 
6723   --
6724   -- CANCEL mode
6725   --
6726   IF (funcmode = 'CANCEL') THEN
6727         resultout := 'COMPLETE:';
6728         RETURN;
6729   END IF;
6730 
6731   --
6732   -- TIMEOUT mode
6733   --
6734   IF (funcmode = 'TIMEOUT') THEN
6735         resultout := 'COMPLETE:';
6736         RETURN;
6737   END IF;
6738   --
6739 
6740 EXCEPTION
6741   WHEN OTHERS THEN
6742         IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
6743           Fnd_Message.Set_Name('AMS','AMS_API_DEBUG_MESSAGE');
6744           Fnd_Message.Set_Token('ROW',SQLERRM);
6745           Fnd_Msg_Pub.ADD;
6746         END IF;
6747         Fnd_Msg_Pub.Count_And_Get (
6748                    p_encoded => Fnd_Api.G_FALSE,
6749                    p_count => l_msg_count,
6750                    p_data  => l_msg_data);
6751         Handle_Err
6752           (p_itemtype          => itemtype   ,
6753            p_itemkey           => itemkey    ,
6754            p_msg_count         => l_msg_count, -- Number of error Messages
6755            p_msg_data          => l_msg_data ,
6756            p_attr_name         => 'AMS_ERROR_MSG',
6757            x_error_msg         => l_error_msg);
6758         Wf_Core.context('AMS_APPROVAL_PVT',
6759                         'Continue_Parent_Process',
6760                         itemtype, itemkey,TO_CHAR(actid),l_error_msg);
6761         RAISE;
6762 END Continue_Parent_Process;
6763 --------------------------------------------------------------------------------
6764 /*****************************************************************
6765 -- Start of Comments
6766 -- NAME
6767 --   DelvStartProcess
6768 -- PURPOSE
6769 --   This Procedure will Start the Deliverable Cancellation Process
6770 --
6771 -- Used By Activities
6772 -- NOTES
6773 -- HISTORY
6774 -- End of Comments
6775 *****************************************************************/
6776 PROCEDURE DelvStartProcess
6777            (p_deliverable_id         IN   NUMBER,
6778             p_deliverable_name       IN   VARCHAR2,
6779             p_object_version_number  IN   NUMBER,
6780             p_usedby_object_id       IN   NUMBER,
6781             p_usedby_object_name     IN   VARCHAR2,
6782             p_usedby_object_type_name IN   VARCHAR2,
6783             p_requester_userid       IN   NUMBER,
6784             p_deliverable_userid     IN   NUMBER,
6785             p_workflowprocess        IN   VARCHAR2,
6786             p_item_type              IN   VARCHAR2
6787              )
6788 IS
6789 itemtype   VARCHAR2(30) := NVL(p_item_type,'AMSAPRV');
6790 itemkey    VARCHAR2(30) := 'CHI'||p_deliverable_id||p_object_version_number||p_usedby_object_id;
6791 itemuserkey VARCHAR2(80) :='CHI'||p_deliverable_id||p_object_version_number||p_usedby_object_id;
6792 
6793 l_requester_role         VARCHAR2(320) ;  -- was 100
6794 l_delv_user_role         VARCHAR2(320) ;  -- was 100
6795 l_deliverable_userid     NUMBER ;
6796 l_delv_requester_name    VARCHAR2(360);  -- was 100
6797 l_delv_user_name         VARCHAR2(360);  -- was 100
6798 
6799 l_return_status          VARCHAR2(1);
6800 l_msg_data               VARCHAR2(4000);
6801 l_msg_count              NUMBER;
6802 l_error_msg              VARCHAR2(4000);
6803 
6804 BEGIN
6805    /*****************************************************************
6806      Start Process :
6807       If workflowprocess is passed, it will be run.
6808       If workflowprocess is NOT passed, the selector function
6809       defined in the item type will determine which process to run.
6810    *****************************************************************/
6811 
6812    Ams_Utility_Pvt.debug_message('Start :Item Type : '||itemtype
6813                          ||' Item key : '||itemkey);
6814 
6815    Wf_Engine.CreateProcess (itemtype   =>   itemtype,
6816                             itemkey    =>   itemkey ,
6817                             process    =>   p_workflowprocess);
6818 
6819    Wf_Engine.SetItemUserkey(itemtype   =>   itemtype,
6820                             itemkey    =>   itemkey ,
6821                             userkey    =>   itemuserkey);
6822 
6823 
6824    /*****************************************************************
6825       Initialize Workflow Item Attributes
6826    *****************************************************************/
6827 
6828 
6829    Wf_Engine.SetItemAttrText(itemtype   =>  itemtype ,
6830                              itemkey    =>  itemkey,
6831                              aname      =>  'AMS_DELV_NAME',
6832                              avalue     =>   p_deliverable_name  );
6833 
6834    Wf_Engine.SetItemAttrText(itemtype   =>  itemtype ,
6835                              itemkey    =>  itemkey,
6836                              aname      =>  'AMS_DELV_USEDBY_NAME',
6837                              avalue     =>   p_usedby_object_name  );
6838 
6839    Wf_Engine.SetItemAttrText(itemtype   =>  itemtype ,
6840                              itemkey    =>  itemkey,
6841                              aname      =>  'AMS_DELV_USEDBY_TYPE',
6842                              avalue     =>   p_usedby_object_type_name  );
6843 
6844 
6845 
6846  -- l_return_status := FND_API.G_RET_STS_SUCCESS;
6847 
6848   --  check for description of activity
6849 
6850   -- Setting up the role
6851   Get_User_Role(p_user_id              => p_deliverable_userid ,
6852                 x_role_name            => l_delv_user_role,
6853                 x_role_display_name    => l_delv_user_name,
6854                 x_return_status        => l_return_status);
6855 
6856   IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS  THEN
6857              RAISE Fnd_Api.G_EXC_ERROR;
6858   END IF;
6859 
6860 
6861   Wf_Engine.SetItemAttrText(itemtype    =>  itemtype,
6862                             itemkey     =>  itemkey,
6863                             aname       =>  'AMS_DELV_USER',
6864                             avalue      =>  l_delv_user_role  );
6865 
6866   Get_User_Role(p_user_id              => p_requester_userid ,
6867                 x_role_name            => l_requester_role,
6868                 x_role_display_name    => l_delv_requester_name,
6869                 x_return_status        => l_return_status);
6870 
6871   IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS  THEN
6872              RAISE Fnd_Api.G_EXC_ERROR;
6873   END IF;
6874 
6875 
6876   Wf_Engine.SetItemOwner(itemtype    => itemtype,
6877                           itemkey     => itemkey,
6878                           owner       => l_requester_role);
6879 
6880 
6881    -- Start the Process
6882    Wf_Engine.StartProcess (itemtype       => itemtype,
6883                             itemkey       => itemkey);
6884 
6885 
6886 EXCEPTION
6887   WHEN Fnd_Api.G_EXC_ERROR THEN
6888         Wf_Core.context ('ams_approval_pvt', 'DelvStartProcess'
6889                        ,p_deliverable_id ,p_workflowprocess);
6890         RAISE;
6891   WHEN OTHERS THEN
6892         Wf_Core.context ('ams_approval_pvt', 'DelvStartProcess'
6893                        ,p_deliverable_id ,p_workflowprocess);
6894         Ams_Utility_Pvt.debug_message('Inside Exception');
6895         RAISE;
6896 END DelvStartProcess;
6897 --------------------------------------------------------------------------------
6898 
6899 PROCEDURE RECONCILE_BUDGET_LINE( itemtype        IN  VARCHAR2,
6900                                 itemkey         IN  VARCHAR2,
6901                                 actid           IN  NUMBER,
6902                                 funcmode        IN  VARCHAR2,
6903                                 resultout       OUT NOCOPY VARCHAR2    )
6904 IS
6905 l_activity_id           NUMBER;
6906 l_activity_type         VARCHAR2(30);
6907 l_approved_currency     VARCHAR2(30);
6908 l_return_status         VARCHAR2(1);
6909 l_msg_data              VARCHAR2(4000);
6910 l_msg_count             NUMBER;
6911 l_error_msg             VARCHAR2(4000);
6912 l_child_item_key        VARCHAR2(240);
6913 l_child_item_type       VARCHAR2(30);
6914 
6915 CURSOR c_find_child (item_key_in IN VARCHAR2, item_type_in IN VARCHAR2) IS
6916 SELECT item_type, item_key FROM wf_items_v
6917 WHERE PARENT_ITEM_TYPE = item_type_in
6918 AND parent_item_key = item_key_in
6919 AND END_DATE IS NULL;
6920 
6921 BEGIN
6922       l_activity_id        := Wf_Engine.GetItemAttrNumber(
6923                                  itemtype => itemtype,
6924                                  itemkey  => itemkey,
6925                                  aname    => 'AMS_ACTIVITY_ID' );
6926 
6927      -- get the activity type
6928      l_activity_type      := Wf_Engine.GetItemAttrText(
6929                                  itemtype => itemtype,
6930                                  itemkey  => itemkey,
6931                                  aname    => 'AMS_ACTIVITY_TYPE' );
6932 
6933        l_approved_currency := Wf_Engine.GetItemAttrText(
6934                               itemtype => itemtype,
6935                                 itemkey => itemkey,
6936                                 aname   => 'AMS_CURRENCY' );
6937 
6938 
6939   Ozf_Fund_Reconcile_Pvt.reconcile_budget_line (
6940     p_budget_used_by_id => l_activity_id
6941     ,p_budget_used_by_type => l_activity_type
6942     ,p_object_currency => l_approved_currency
6943     ,p_api_version  => 1.0
6944     ,p_init_msg_list   => Fnd_Api.g_false
6945     ,p_commit   => Fnd_Api.g_false
6946     ,p_validation_level => Fnd_Api.g_valid_level_full
6947     ,x_return_status => l_return_status
6948     ,x_msg_count => l_msg_count
6949     ,x_msg_data => l_msg_data
6950   );
6951         IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
6952     RAISE Fnd_Api.G_EXC_ERROR;
6953   ELSE
6954     OPEN c_find_child(itemkey, itemtype);
6955     FETCH c_find_child INTO l_child_item_type, l_child_item_key;
6956     IF c_find_child%FOUND THEN
6957       LOOP
6958         EXIT WHEN c_find_child%NOTFOUND;
6959         AbortProcess(p_itemkey  => l_child_item_key
6960              ,p_workflowprocess  => NULL
6961              ,p_itemtype  => l_child_item_type
6962         );
6963         FETCH c_find_child INTO l_child_item_type, l_child_item_key;
6964       END LOOP;
6965     END IF;
6966     CLOSE c_find_child;
6967   END IF;
6968  EXCEPTION
6969      WHEN Fnd_Api.G_EXC_ERROR THEN
6970         -- wf_engine.threshold := l_save_threshold ;
6971         Fnd_Msg_Pub.Count_And_Get (
6972           p_encoded => Fnd_Api.G_FALSE,
6973           p_count   => l_msg_count,
6974           p_data    => l_msg_data);
6975         Handle_Err
6976           (p_itemtype          => itemtype   ,
6977            p_itemkey           => itemkey    ,
6978            p_msg_count         => l_msg_count, -- Number of error Messages
6979            p_msg_data          => l_msg_data ,
6980            p_attr_name         => 'AMS_ERROR_MSG',
6981            x_error_msg         => l_error_msg       );
6982         Wf_Core.context ('ams_approval_pvt', 'StartProcess',l_activity_type
6983                        ,l_activity_id ,l_error_msg);
6984         RAISE;
6985 END;
6986 ---------------------------------------------------------------------------
6987 PROCEDURE Validate_Object_Budget_WF(itemtype  IN  VARCHAR2,
6988                                  itemkey   IN  VARCHAR2,
6989                                  actid     IN  NUMBER,
6990                                  funcmode  IN  VARCHAR2,
6991                                  resultout OUT NOCOPY VARCHAR2)
6992 
6993 IS
6994 --
6995 l_return_status   VARCHAR2(1);
6996 l_msg_data        VARCHAR2(4000);
6997 l_msg_count       NUMBER;
6998 l_error_msg       VARCHAR2(4000);
6999 l_activity_id     NUMBER;
7000 l_activity_type   VARCHAR2(30);
7001 l_act_budget_id   NUMBER;
7002 
7003 
7004 BEGIN
7005   Fnd_Msg_Pub.initialize();
7006 
7007   --
7008   -- RUN mode
7009   --
7010     IF (funcmode = 'RUN') THEN
7011        -- get the acitvity id
7012        l_activity_id        := Wf_Engine.GetItemAttrNumber(
7013                                    itemtype => itemtype,
7014                                    itemkey  => itemkey,
7015                                    aname    => 'AMS_ACTIVITY_ID' );
7016 
7017        -- get the activity type
7018        l_activity_type      := Wf_Engine.GetItemAttrText(
7019                                    itemtype => itemtype,
7020                                    itemkey  => itemkey,
7021                                    aname    => 'AMS_ACTIVITY_TYPE' );
7022 
7023       IF l_activity_type = 'OFFR' THEN
7024 
7025         l_act_budget_id     := Wf_Engine.GetItemAttrText(
7026                                    itemtype => itemtype,
7027                                    itemkey  => itemkey,
7028                                    aname    => 'AMS_ACT_BUDGET_ID' );
7029 
7030       -- Call API Validate_Object_Budget
7031       -- with required parameters
7032 
7033       Ozf_Budgetapproval_Pvt.validate_object_budget(
7034           p_object_id     => l_activity_id,
7035           p_object_type   => l_activity_type,
7036           p_actbudget_id  => l_act_budget_id,
7037           x_return_status => l_return_status,
7038           x_msg_count     => l_msg_count,
7039           x_msg_data      => l_msg_data);
7040 
7041         -- After Call to API
7042 
7043         IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS  THEN
7044 
7045     Handle_Err
7046         (p_itemtype  => itemtype,
7047          p_itemkey   => itemkey,
7048          p_msg_count => l_msg_count,
7049          p_msg_data  => l_msg_data,
7050          p_attr_name => 'AMS_ERROR_MSG',
7051          x_error_msg => l_error_msg
7052       );
7053          resultout := 'COMPLETE:FAILURE';
7054   ELSE
7055          resultout := 'COMPLETE:SUCCESS';
7056   END IF;
7057 
7058       -- For all other activity types return complete
7059       --
7060       ELSE
7061 
7062         resultout := 'COMPLETE:SUCCESS';
7063         RETURN;
7064 
7065       END IF;
7066     END IF; --  Run Mode
7067     --
7068     -- CANCEL mode
7069     --
7070 
7071 
7072     IF (funcmode = 'CANCEL') THEN
7073             resultout := 'COMPLETE:';
7074             RETURN;
7075     END IF;
7076 
7077     --
7078     -- TIMEOUT mode
7079     --
7080     IF (funcmode = 'TIMEOUT') THEN
7081           resultout := 'COMPLETE:';
7082           RETURN;
7083     END IF;
7084   --
7085 EXCEPTION
7086   WHEN Fnd_Api.G_EXC_ERROR THEN
7087           Fnd_Msg_Pub.Count_And_Get (
7088                p_encoded => Fnd_Api.G_FALSE,
7089                p_count => l_msg_count,
7090                p_data  => l_msg_data
7091           );
7092         Handle_Err
7093           (p_itemtype          => itemtype   ,
7094            p_itemkey           => itemkey    ,
7095            p_msg_count         => l_msg_count,
7096            p_msg_data          => l_msg_data ,
7097            p_attr_name         => 'AMS_ERROR_MSG',
7098            x_error_msg         => l_error_msg
7099            );
7100   Wf_Core.context('AMS_APPROVAL_PVT',
7101                     'Validate_Object_Budget_WF',
7102                      itemtype, itemkey,TO_CHAR(actid),l_error_msg);
7103     RAISE;
7104           --resultout := 'COMPLETE:ERROR';
7105   WHEN OTHERS THEN
7106     Fnd_Msg_Pub.Count_And_Get (
7107                    p_encoded => Fnd_Api.G_FALSE,
7108                    p_count => l_msg_count,
7109                    p_data  => l_msg_data);
7110     Handle_Err
7111           (p_itemtype          => itemtype   ,
7112            p_itemkey           => itemkey    ,
7113            p_msg_count         => l_msg_count, -- Number of error Messages
7114            p_msg_data          => l_msg_data ,
7115            p_attr_name         => 'AMS_ERROR_MSG',
7116            x_error_msg         => l_error_msg);
7117     Wf_Core.context('AMS_APPROVAL_PVT',
7118                     'Validate_Object_Budget_WF',
7119                      itemtype, itemkey,TO_CHAR(actid),l_error_msg);
7120     RAISE;
7121 END Validate_Object_Budget_WF;
7122 ----------------------------------------------------------------------------
7123 /*****************************************************************
7124 -- Start of Comments
7125 -- NAME
7126 --   Approval_Required
7127 -- PURPOSE
7128 --   This Procedure will determine if the requestor of an activity
7129 --   is the same as the approver for that activity. This is used to
7130 --   bypass approvals if requestor is the same as the approver.
7131 -- Used By Activities
7132 -- NOTES
7133 -- HISTORY
7134 -- End of Comments
7135 ****************************************************************/
7136 PROCEDURE Approval_Required(itemtype  IN  VARCHAR2,
7137                             itemkey   IN  VARCHAR2,
7138                             actid     IN  NUMBER,
7139                             funcmode  IN  VARCHAR2,
7140                             resultout OUT NOCOPY VARCHAR2)
7141 IS
7142 --
7143 l_requestor NUMBER;
7144 l_approver  NUMBER;
7145 
7146 
7147 BEGIN
7148   Fnd_Msg_Pub.initialize();
7149   --
7150   -- RUN mode
7151   --
7152     IF (funcmode = 'RUN') THEN
7153 
7154       -- Get the Requestor
7155       l_requestor := Wf_Engine.GetItemAttrNumber(itemtype  => itemtype,
7156                                     itemkey   => itemkey,
7157                                     aname     => 'AMS_REQUESTER_ID');
7158 
7159       -- Get the Approver
7160       l_approver := Wf_Engine.GetItemAttrNumber(itemtype  => itemtype,
7161                                     itemkey   => itemkey,
7162                                     aname     => 'AMS_APPROVER_ID');
7163 
7164       IF l_requestor = l_approver THEN
7165          resultout := 'COMPLETE:N';
7166       ELSE
7167          resultout := 'COMPLETE:Y';
7168       END IF;
7169 
7170       RETURN;
7171 
7172     END IF;
7173 
7174     IF (funcmode = 'CANCEL') THEN
7175             resultout := 'COMPLETE:';
7176             RETURN;
7177     END IF;
7178 
7179     --
7180     -- TIMEOUT mode
7181     --
7182     IF (funcmode = 'TIMEOUT') THEN
7183           resultout := 'COMPLETE:';
7184           RETURN;
7185     END IF;
7186 END Approval_Required;
7187 ----------------------------------------------------------------------------
7188 /*****************************************************************
7189 -- Start of Comments
7190 -- NAME
7191 --   Auto_Approve
7192 -- PURPOSE
7193 --   This Procedure will determine if the approver of an activity
7194 --   has a profile amount defined that can be used to automatically
7195 --   approve budget lines or budgets(objects). If the profile amount
7196 --   is greater than the line amount or activity amount, the approval
7197 --   is automatic
7198 -- Used By Activities
7199 -- NOTES
7200 -- HISTORY
7201 -- End of Comments
7202 ******************************************************************/
7203 PROCEDURE Auto_Approve (itemtype    IN  VARCHAR2,
7204                         itemkey     IN  VARCHAR2,
7205                         actid       IN  NUMBER,
7206                         funcmode    IN  VARCHAR2,
7207                         resultout   OUT NOCOPY VARCHAR2)
7208 IS
7209 CURSOR res_user_csr (p_resource_id IN NUMBER) IS
7210 SELECT user_id
7211 FROM ams_jtf_rs_emp_v
7212 WHERE resource_id = p_resource_id ;
7213 
7214 l_auto_amount      NUMBER;
7215 l_approver_id      NUMBER;
7216 l_user_id          NUMBER;
7217 l_act_budget_id    NUMBER;
7218 l_appr_line_amount NUMBER;
7219 l_activity_amount  NUMBER;
7220 
7221 BEGIN
7222 
7223   --
7224   -- RUN mode
7225   --
7226 IF (funcmode = 'RUN') THEN
7227 Fnd_Msg_Pub.initialize();
7228 
7229 -- Get the Approver Id
7230 l_approver_id := Wf_Engine.GetItemAttrNumber( itemtype => itemtype,
7231                                         itemkey => itemkey,
7232                                         aname   => 'AMS_APPROVER_ID' );
7233 -- Get the user id
7234 OPEN res_user_csr (l_approver_id);
7235 FETCH res_user_csr INTO l_user_id;
7236 CLOSE res_user_csr;
7237 
7238 -- Get the auto approval amount of the approver
7239 l_auto_amount := NVL(Fnd_Profile.Value_Specific(name => 'AMS_AUTO_APPROVAL_AMOUNT',
7240                                                 user_id => l_user_id),0);
7241 
7242 IF l_auto_amount > 0 THEN
7243 
7244   -- Get the Budget ID
7245 
7246   l_act_budget_id := Wf_Engine.GetItemAttrNumber( itemtype => itemtype,
7247                                         itemkey => itemkey,
7248                                         aname   => 'AMS_ACT_BUDGET_ID' );
7249 
7250   IF l_act_budget_id IS NOT NULL THEN
7251   -- It is a budget line
7252 
7253     -- get the approved line amount that is seen by approver
7254     l_appr_line_amount := Wf_Engine.GetItemAttrNumber( itemtype => itemtype,
7255                                         itemkey => itemkey,
7256                                         aname   => 'AMS_APPROVED_LINE_AMOUNT');
7257 
7258     IF l_appr_line_amount <= l_auto_amount THEN
7259       -- Automatic Approval
7260       resultout := 'COMPLETE:Y';
7261     ELSE
7262       -- No Automatic Approval
7263       resultout := 'COMPLETE:N';
7264     END IF;
7265 
7266     RETURN;
7267 
7268   ELSE
7269   -- It is not a budget line
7270 
7271     -- Get the Activity Amount
7272     l_activity_amount := Wf_Engine.GetItemAttrNumber( itemtype => itemtype,
7273                                         itemkey => itemkey,
7274                                         aname   => 'AMS_ACTIVITY_AMOUNT');
7275     IF l_activity_amount > 0 THEN
7276       IF l_activity_amount <= l_auto_amount THEN
7277         -- Automatic Approval
7278         resultout := 'COMPLETE:Y';
7279       ELSE
7280         -- No Automatic Approval
7281         resultout := 'COMPLETE:N';
7282       END IF;
7283 
7284       RETURN;
7285     END IF;
7286   -- No auto approval for zero budget objects
7287   resultout := 'COMPLETE:N';
7288   return;
7289   END IF; --3
7290 
7291 ELSE -- if l_auto_amount <=0
7292 
7293   resultout := 'COMPLETE:N';
7294   return;
7295 END IF;
7296 END IF;
7297 
7298 --
7299 -- CANCEL mode
7300 --
7301 IF (funcmode = 'CANCEL') THEN
7302       resultout := 'COMPLETE:';
7303       return;
7304 END IF;
7305 
7306 --
7307 -- TIMEOUT mode
7308 --
7309 IF (funcmode = 'TIMEOUT') THEN
7310       resultout := 'COMPLETE:';
7311       return;
7312 END IF;
7313 --
7314 END Auto_Approve;
7315 
7316 --------------------------------------------------------------------------
7317 -- Wrapper for Get_Approval_Details
7318 -- Called by ApproverListVO.java
7319 PROCEDURE Get_Approval_Rule ( p_activity_id        IN  NUMBER,
7320                               p_activity_type      IN  VARCHAR2,
7321                               p_approval_type      IN  VARCHAR2,
7322                               p_act_budget_id      IN  NUMBER,
7323                               x_approval_detail_Id OUT NOCOPY NUMBER,
7324                               x_return_status      OUT NOCOPY  VARCHAR2)
7325 IS
7326 l_obj_details       ObjRecTyp;
7327 l_approver_seq      NUMBER;
7328 BEGIN
7329   -- Get the approval rule
7330   -- Declare Get_Approval_Details as a public procedure
7331 
7332 
7333   IF p_activity_type IN ('PRIC', 'CLAM', 'RFRQ', 'FREQ') THEN -- add others like OFFRADJ
7334 
7335      AMS_Gen_Approval_Pvt.Get_Approval_Rule(p_activity_id   => p_activity_id,
7336                                             p_activity_type => p_activity_type,
7337                                             p_approval_type => p_approval_type,
7338                                             p_act_budget_id => p_act_budget_id,
7339                                             x_approval_detail_id  => x_approval_detail_id,
7340                                             x_return_status       => x_return_status);
7341   ELSE
7342 
7343   AMS_Approval_Pvt.Get_Approval_Details(p_activity_id => p_activity_id,
7344                               p_activity_type       => p_activity_type,
7345                               p_approval_type       => p_approval_type,
7346                               p_act_budget_id       => p_act_budget_id,
7347                               x_object_details      => l_obj_details,
7348                               x_approval_detail_id  => x_approval_detail_id,
7349                               x_approver_seq        => l_approver_seq,
7350                               x_return_status       => x_return_status);
7351 
7352   END IF;
7353 
7354 END Get_Approval_Rule;
7355 
7356 ---------------------------------------------------------------------
7357 -- Called in Approval Notifications
7358 -- Used primarily to capture the new Approver in case of Forward/Re-assign
7359 ---------------------------------------------------------------------
7360 
7361 PROCEDURE PostNotif_Update (itemtype  IN  VARCHAR2,
7362                             itemkey   IN  VARCHAR2,
7363                             actid     IN  NUMBER,
7364                             funcmode  IN  VARCHAR2,
7365                             resultout OUT NOCOPY VARCHAR2)
7366 IS
7367 l_nid NUMBER;
7368 l_result VARCHAR2(30);
7369 l_assignee VARCHAR2(320);
7370 l_new_approver_id NUMBER;
7371 l_appr_display_name VARCHAR2(360);
7372 l_activity_type VARCHAR2(30);
7373 l_version NUMBER;
7374 l_activity_id NUMBER;
7375 l_act_budget_id NUMBER;
7376 l_approval_type VARCHAR2(30);
7377 l_current_seq NUMBER;
7378 l_return_status VARCHAR2(1);
7379 l_msg_count NUMBER;
7380 l_msg_data VARCHAR2(4000);
7381 l_error_msg       VARCHAR2(4000);
7382 
7383 l_appr_hist_rec AMS_Appr_Hist_Pvt.Appr_Hist_Rec_Type;
7384 
7385 BEGIN
7386 l_nid := wf_engine.context_nid;
7387 
7388 IF (funcmode = 'RESPOND') THEN
7389 
7390   l_result := upper(wf_notification.GETATTRTEXT(l_nid, 'RESULT'));
7391 
7392   IF l_result = 'APPROVE' then
7393      resultout := 'COMPLETE:APPROVE';
7394   ELSE
7395      resultout := 'COMPLETE:REJECT';
7396   END IF;
7397 
7398 ELSIF (funcmode = 'TRANSFER' OR funcmode = 'FORWARD') THEN
7399 
7400   -- Set the forwarded/transferred notification id so that
7401   -- we can use it later to see actual approver
7402 
7403 l_assignee := WF_ENGINE.context_text;
7404 
7405 -- ams_forward_nid is not really needed.
7406   wf_engine.SetItemAttrNumber(itemtype => itemtype,
7407                               itemkey  => itemkey,
7408                               aname    => 'AMS_FORWARD_NID',
7409                               avalue   => l_nid);
7410 
7411   Get_New_Res_Details(p_responder => l_assignee,
7412                       x_resource_id => l_new_approver_id,
7413                       x_resource_disp_name => l_appr_display_name,
7414                       x_return_status => l_return_status);
7415 
7416   IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
7417    RAISE Fnd_Api.G_EXC_ERROR;
7418   END IF;
7419 
7420          -- Set the WF Attributes
7421          wf_engine.SetItemAttrText(  itemtype => itemtype,
7422                               itemkey  => itemkey,
7423                               aname    => 'AMS_APPR_USERNAME',
7424                               avalue   => l_assignee);
7425 
7426         wf_engine.SetItemAttrNumber(itemtype => itemtype,
7427                               itemkey  => itemkey,
7428                               aname    => 'AMS_APPROVER_ID',
7429                               avalue   => l_new_approver_id);
7430 
7431         wf_engine.SetItemAttrText(  itemtype => itemtype,
7432                               itemkey  => itemkey,
7433                               aname    => 'AMS_APPR_DISPLAY_NAME',
7434                               avalue   => l_appr_display_name);
7435 
7436   -- Update the approver details here
7437 
7438         l_activity_type      := Wf_Engine.GetItemAttrText(
7439                                  itemtype => itemtype,
7440                                  itemkey  => itemkey,
7441                                  aname    => 'AMS_ACTIVITY_TYPE' );
7442 
7443         l_act_budget_id := Wf_Engine.GetItemAttrNumber(
7444                                   itemtype => itemtype,
7445                                   itemkey => itemkey,
7446                                   aname   => 'AMS_ACT_BUDGET_ID'
7447                                   );
7448 
7449         l_version := Wf_Engine.GetItemAttrNumber(
7450                                   itemtype => itemtype,
7451                                   itemkey => itemkey,
7452                                   aname   => 'AMS_OBJECT_VERSION_NUMBER'
7453                                   );
7454 
7455         l_activity_id        := Wf_Engine.GetItemAttrNumber(
7456                                  itemtype => itemtype,
7457                                  itemkey  => itemkey,
7458                                  aname    => 'AMS_ACTIVITY_ID' );
7459 
7460         l_current_seq        := Wf_Engine.GetItemAttrText(
7461                                  itemtype => itemtype,
7462                                  itemkey  => itemkey,
7463                                  aname    => 'AMS_APPROVER_SEQ' );
7464 
7465         l_approval_type      := Wf_Engine.GetItemAttrText(
7466                                  itemtype => itemtype,
7467                                  itemkey => itemkey,
7468                                  aname   => 'AMS_APPROVAL_TYPE' );
7469 
7470        IF l_act_budget_id IS NOT NULL THEN
7471           l_activity_type := 'FUND';
7472           l_activity_id   := l_act_budget_id;
7473        END IF;
7474 
7475           l_appr_hist_rec.object_id          := l_activity_id;
7476           l_appr_hist_rec.object_type_code   := l_activity_type;
7477           l_appr_hist_rec.object_version_num := l_version;
7478           l_appr_hist_rec.approval_type      := l_approval_type;
7479           l_appr_hist_rec.approver_type      := 'USER'; -- Always
7480           l_appr_hist_rec.sequence_num       := l_current_seq;
7481           l_appr_hist_rec.approver_id        := l_new_approver_id;
7482 
7483           AMS_Appr_Hist_PVT.Update_Appr_Hist(
7484              p_api_version_number => 1.0,
7485              p_init_msg_list      => FND_API.G_FALSE,
7486              p_commit             => FND_API.G_FALSE,
7487              p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
7488              x_return_status      => l_return_status,
7489              x_msg_count          => l_msg_count,
7490              x_msg_data           => l_msg_data,
7491              p_appr_hist_rec      => l_appr_hist_rec
7492              );
7493 
7494            IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS THEN
7495              RAISE Fnd_Api.G_EXC_ERROR;
7496            END IF;
7497 
7498   resultout := 'COMPLETE';
7499 
7500 END IF;
7501 EXCEPTION
7502   WHEN Fnd_Api.G_EXC_ERROR THEN
7503           Fnd_Msg_Pub.Count_And_Get (
7504                p_encoded => Fnd_Api.G_FALSE,
7505                p_count => l_msg_count,
7506                p_data  => l_msg_data
7507           );
7508         Handle_Err
7509           (p_itemtype          => itemtype   ,
7510            p_itemkey           => itemkey    ,
7511            p_msg_count         => l_msg_count,
7512            p_msg_data          => l_msg_data ,
7513            p_attr_name         => 'AMS_ERROR_MSG',
7514            x_error_msg         => l_error_msg
7515            );
7516       wf_core.token('MESSAGE', l_error_msg);
7517       wf_core.raise('WF_PLSQL_ERROR');
7518 
7519  WHEN OTHERS THEN
7520       wf_core.context('ams_approval_pvt','PostNotif_Update',
7521                       itemtype,itemkey,actid,funcmode,'Error in PNF');
7522       raise;
7523 END PostNotif_Update;
7524 ---------------------------------------------------------------
7525 PROCEDURE Validate_Object_Budget_All_WF(itemtype  IN  VARCHAR2,
7526                                  itemkey   IN  VARCHAR2,
7527                                  actid     IN  NUMBER,
7528                                  funcmode  IN  VARCHAR2,
7529                                  resultout OUT NOCOPY VARCHAR2)
7530 
7531 IS
7532 --
7533 l_return_status   VARCHAR2(1);
7534 l_msg_data        VARCHAR2(4000);
7535 l_msg_count       NUMBER;
7536 l_error_msg       VARCHAR2(4000);
7537 l_activity_id     NUMBER;
7538 l_activity_type   VARCHAR2(30);
7539 --l_act_budget_id   NUMBER;
7540 
7541 
7542 BEGIN
7543   Fnd_Msg_Pub.initialize();
7544 
7545   --
7546   -- RUN mode
7547   --
7548     IF (funcmode = 'RUN') THEN
7549        -- get the acitvity id
7550        l_activity_id        := Wf_Engine.GetItemAttrNumber(
7551                                    itemtype => itemtype,
7552                                    itemkey  => itemkey,
7553                                    aname    => 'AMS_ACTIVITY_ID' );
7554 
7555        -- get the activity type
7556        l_activity_type      := Wf_Engine.GetItemAttrText(
7557                                    itemtype => itemtype,
7558                                    itemkey  => itemkey,
7559                                    aname    => 'AMS_ACTIVITY_TYPE' );
7560 
7561       IF l_activity_type = 'OFFR' THEN
7562 
7563   --      l_act_budget_id     := Wf_Engine.GetItemAttrText(
7564   --                                itemtype => itemtype,
7565   --                                 itemkey  => itemkey,
7566   --                                aname    => 'AMS_ACT_BUDGET_ID' );
7567 
7568       -- Call API Validate_Object_Budget
7569       -- with required parameters
7570 
7571       Ozf_Budgetapproval_Pvt.validate_object_budget_all(
7572           p_object_id     => l_activity_id,
7573           p_object_type   => l_activity_type,
7574           x_return_status => l_return_status,
7575           x_msg_count     => l_msg_count,
7576           x_msg_data      => l_msg_data);
7577 
7578         -- After Call to API
7579 
7580         IF l_return_status <> Fnd_Api.G_RET_STS_SUCCESS  THEN
7581 
7582     Handle_Err
7583         (p_itemtype  => itemtype,
7584          p_itemkey   => itemkey,
7585          p_msg_count => l_msg_count,
7586          p_msg_data  => l_msg_data,
7587          p_attr_name => 'AMS_ERROR_MSG',
7588          x_error_msg => l_error_msg
7589       );
7590          resultout := 'COMPLETE:FAILURE';
7591   ELSE
7592          resultout := 'COMPLETE:SUCCESS';
7593   END IF;
7594 
7595       -- For all other activity types return complete
7596       --
7597       ELSE
7598 
7599         resultout := 'COMPLETE:SUCCESS';
7600         RETURN;
7601 
7602       END IF;
7603     END IF; --  Run Mode
7604     --
7605     -- CANCEL mode
7606     --
7607 
7608 
7609     IF (funcmode = 'CANCEL') THEN
7610             resultout := 'COMPLETE:';
7611             RETURN;
7612     END IF;
7613 
7614     --
7615     -- TIMEOUT mode
7616     --
7617     IF (funcmode = 'TIMEOUT') THEN
7618           resultout := 'COMPLETE:';
7619           RETURN;
7620     END IF;
7621   --
7622 EXCEPTION
7623   WHEN Fnd_Api.G_EXC_ERROR THEN
7624           Fnd_Msg_Pub.Count_And_Get (
7625                p_encoded => Fnd_Api.G_FALSE,
7626                p_count => l_msg_count,
7627                p_data  => l_msg_data
7628           );
7629         Handle_Err
7630           (p_itemtype          => itemtype   ,
7631            p_itemkey           => itemkey    ,
7632            p_msg_count         => l_msg_count,
7633            p_msg_data          => l_msg_data ,
7634            p_attr_name         => 'AMS_ERROR_MSG',
7635            x_error_msg         => l_error_msg
7636            );
7637   Wf_Core.context('AMS_APPROVAL_PVT',
7638                     'Validate_Object_Budget_All_WF',
7639                      itemtype, itemkey,TO_CHAR(actid),l_error_msg);
7640     RAISE;
7641           --resultout := 'COMPLETE:ERROR';
7642   WHEN OTHERS THEN
7643     Fnd_Msg_Pub.Count_And_Get (
7644                    p_encoded => Fnd_Api.G_FALSE,
7645                    p_count => l_msg_count,
7646                    p_data  => l_msg_data);
7647     Handle_Err
7648           (p_itemtype          => itemtype   ,
7649            p_itemkey           => itemkey    ,
7650            p_msg_count         => l_msg_count, -- Number of error Messages
7651            p_msg_data          => l_msg_data ,
7652            p_attr_name         => 'AMS_ERROR_MSG',
7653            x_error_msg         => l_error_msg);
7654     Wf_Core.context('AMS_APPROVAL_PVT',
7655                     'Validate_Object_Budget_All_WF',
7656                      itemtype, itemkey,TO_CHAR(actid),l_error_msg);
7657     RAISE;
7658 END Validate_Object_Budget_All_WF;
7659 --------------------------------------------------------------------------
7660 PROCEDURE Bypass_Approval (itemtype    IN  VARCHAR2,
7661                         itemkey     IN  VARCHAR2,
7662                         actid       IN  NUMBER,
7663                         funcmode    IN  VARCHAR2,
7664                         resultout   OUT NOCOPY VARCHAR2)
7665 IS
7666 l_activity_id NUMBER;
7667 l_activity_type VARCHAR2(30);
7668 l_custom_setup_id NUMBER;
7669 
7670 CURSOR c_offr(id IN NUMBER) IS
7671 SELECT custom_setup_id
7672 FROM ozf_offers
7673 WHERE qp_list_header_id = id;
7674 BEGIN
7675   Fnd_Msg_Pub.initialize();
7676   --
7677   -- RUN mode
7678   --
7679     IF (funcmode = 'RUN') THEN
7680 
7681        resultout := 'COMPLETE:N';
7682        -- get the activity type
7683        l_activity_type      := Wf_Engine.GetItemAttrText(
7684                                    itemtype => itemtype,
7685                                    itemkey  => itemkey,
7686                                    aname    => 'AMS_ACTIVITY_TYPE' );
7687     IF l_activity_type = 'OFFR' THEN
7688 
7689       -- Get the Activity Id
7690        l_activity_id        := Wf_Engine.GetItemAttrNumber(
7691                                    itemtype => itemtype,
7692                                    itemkey  => itemkey,
7693                                    aname    => 'AMS_ACTIVITY_ID' );
7694 
7695       -- Get the Custom Setup Id
7696       OPEN c_offr(l_activity_id);
7697       FETCH c_offr INTO l_custom_setup_id;
7698       CLOSE c_offr;
7699 
7700       IF l_custom_setup_id = 110 THEN -- Softfund Lumpsum
7701          resultout := 'COMPLETE:Y';
7702       END IF;
7703 
7704    END IF;
7705 
7706       RETURN;
7707 
7708     END IF;
7709 
7710     IF (funcmode = 'CANCEL') THEN
7711             resultout := 'COMPLETE:';
7712             RETURN;
7713     END IF;
7714 
7715     --
7716     -- TIMEOUT mode
7717     --
7718     IF (funcmode = 'TIMEOUT') THEN
7719           resultout := 'COMPLETE:';
7720           RETURN;
7721     END IF;
7722 
7723 END Bypass_Approval;
7724 
7725 PROCEDURE must_preview (p_activity_id        IN  NUMBER,
7726                         p_activity_type      IN  VARCHAR2,
7727                         p_approval_type      IN  VARCHAR2,
7728                         p_act_budget_id      IN  NUMBER,
7729                         p_requestor_id       IN  NUMBER,
7730                         x_must_preview       OUT NOCOPY VARCHAR2,
7731                         x_return_status      OUT NOCOPY VARCHAR2)
7732 IS
7733 l_approval_detail_id NUMBER;
7734 l_approver_type VARCHAR2(30);
7735 l_obj_approver_id NUMBER;
7736 l_pkg_name VARCHAR2(80);
7737 l_proc_name VARCHAR2(80);
7738 l_appr_id NUMBER;
7739 l_return_status VARCHAR2(1);
7740 
7741 CURSOR c_is_only_approver(id IN NUMBER) IS
7742 SELECT approver_type, object_approver_id
7743 from ams_approvers
7744 where ams_approval_detail_id = id
7745 and sysdate between nvl(start_date_active, sysdate) and nvl(end_date_active, sysdate)
7746 and active_flag = 'Y'
7747 and not exists (select count(approver_id) from ams_approvers
7748 where ams_approval_detail_id = id
7749 and sysdate between nvl(start_date_active, sysdate) and nvl(end_date_active, sysdate)
7750 and active_flag = 'Y'
7751 having count(approver_id) > 1);
7752 
7753 CURSOR c_is_requestor_def_appr IS
7754 SELECT rr.role_resource_id
7755 FROM jtf_rs_role_relations rr, jtf_rs_roles_b rl
7756 WHERE rr.role_id = rl.role_id
7757 AND rr.role_resource_type = 'RS_INDIVIDUAL'
7758 AND rl.role_code = 'AMS_DEFAULT_APPROVER'
7759 AND rr.delete_flag = 'N'
7760 AND SYSDATE BETWEEN rr.start_date_active and nvl(rr.end_date_active, SYSDATE)
7761 AND rl.role_type_code IN ('AMSAPPR','MKTGAPPR')
7762 AND rr.role_resource_id = p_requestor_id;
7763 
7764 CURSOR c_is_requestor_appr_role IS
7765 SELECT rr.role_resource_id
7766 FROM jtf_rs_role_relations rr, jtf_rs_roles_b rl
7767 WHERE rr.role_id = rl.role_id
7768 AND rr.role_resource_type = 'RS_INDIVIDUAL'
7769 AND rr.delete_flag = 'N'
7770 AND SYSDATE BETWEEN rr.start_date_active and nvl(rr.end_date_active, SYSDATE)
7771 AND rl.role_type_code IN ('MKTGAPPR', 'AMSAPPR')
7772 AND rr.role_id = l_obj_approver_id
7773 AND rr.role_resource_id = p_requestor_id;
7774 
7775 BEGIN
7776   x_return_status := Fnd_Api.G_RET_STS_SUCCESS;
7777   x_must_preview := 'Y'; -- Default is must preview
7778   Get_Approval_Rule ( p_activity_id => p_activity_id,
7779                       p_activity_type => p_activity_type,
7780                       p_approval_type => p_approval_type,
7781                       p_act_budget_id => p_act_budget_id,
7782                       x_approval_detail_Id => l_approval_detail_id,
7783                       x_return_status => l_return_status);
7784 
7785   IF l_return_status = Fnd_Api.G_RET_STS_SUCCESS
7786   AND l_approval_detail_id IS NOT NULL THEN
7787 
7788     OPEN c_is_only_approver(l_approval_detail_id);
7789     FETCH c_is_only_approver INTO l_approver_type, l_obj_approver_id;
7790     CLOSE c_is_only_approver;
7791 
7792     IF l_approver_type = 'USER' AND l_obj_approver_id = p_requestor_id THEN
7793       x_must_preview := 'N';
7794     END IF; --'USER'
7795 
7796     IF l_approver_type = 'ROLE' THEN
7797 
7798       IF l_obj_approver_id IS NULL THEN-- Default Approver
7799       -- Check if Default Approver is Requestor
7800         OPEN c_is_requestor_def_appr;
7801         FETCH c_is_requestor_def_appr INTO l_appr_id;
7802         IF c_is_requestor_def_appr%NOTFOUND THEN
7803            CLOSE c_is_requestor_def_appr;
7804            RETURN;
7805         END IF;
7806         x_must_preview := 'N';
7807         CLOSE c_is_requestor_def_appr;
7808         RETURN;
7809       END IF;
7810 
7811       IF l_obj_approver_id IS NOT NULL THEN -- Not Def Approver
7812         OPEN c_is_requestor_appr_role;
7813         FETCH c_is_requestor_appr_role INTO l_appr_id;
7814         IF c_is_requestor_appr_role%NOTFOUND THEN
7815            CLOSE c_is_requestor_appr_role;
7816            RETURN;
7817         END IF;
7818         x_must_preview := 'N';
7819         CLOSE c_is_requestor_appr_role;
7820         RETURN;
7821       END IF;
7822 
7823     END IF; -- 'ROLE'
7824 
7825   END IF;
7826 END must_preview;
7827 ------------------------------------------------------------------------
7828 PROCEDURE Check_Object_Type( itemtype        in  varchar2,
7829                              itemkey         in  varchar2,
7830                              actid           in  number,
7831                              funcmode        in  varchar2,
7832                              resultout   OUT NOCOPY varchar2)
7833 IS
7834 l_process_type varchar2(80);
7835 l_activity_type varchar2(80);
7836 BEGIN
7837 
7838      l_activity_type      := wf_engine.GetItemAttrText(
7839                                  itemtype => itemtype,
7840                                  itemkey  => itemkey,
7841                                  aname    => 'AMS_ACTIVITY_TYPE' );
7842 
7843     If (l_activity_type = 'CAMP'
7844             OR l_activity_type = 'CSCH') THEN
7845               resultout := 'CAMP';
7846     else
7847               resultout := 'OTHER';
7848     end if;
7849 
7850 END Check_Object_Type;
7851 --------------------------------------------------------------------------
7852 END Ams_Approval_Pvt;