DBA Data[Home] [Help]

PACKAGE BODY: APPS.AMS_APPROVAL_PVT

Source


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