DBA Data[Home] [Help]

PACKAGE BODY: APPS.PO_CHANGEORDERWF_PVT

Source


1 PACKAGE BODY PO_ChangeOrderWF_PVT AS
2 /* $Header: POXVSCWB.pls 120.36.12010000.5 2008/11/15 03:37:51 vchiran ship $ */
3 
4 
5 g_pkg_name CONSTANT VARCHAR2(50) := 'PO_ChangeOrderWF_PVT';
6 g_module_prefix CONSTANT VARCHAR2(50) := 'po.plsql.' || g_pkg_name || '.';
7 g_fnd_debug VARCHAR2(1) := NVL(FND_PROFILE.VALUE('AFLOG_ENABLED'),'N');
8 g_po_wf_debug VARCHAR2(1) := NVL(FND_PROFILE.VALUE('PO_SET_DEBUG_WORKFLOW_ON'),'N');
9 
10 -- Read the profile option that determines whether the promise date will be defaulted with need-by date or not
11 g_default_promise_date VARCHAR2(1) :=  NVL(FND_PROFILE.VALUE('POS_DEFAULT_PROMISE_DATE_ACK'),'N');
12 NL                VARCHAR2(1) := fnd_global.newline;
13 
14 /*Initializing Private Procedures/Functions*/
15 PROCEDURE Notify_Requester_Sup_Change(	p_header_id in number,
16 										p_release_id in number,
17 										p_revision_num in number,
18 										p_chg_req_grp_id in number,
19 										p_requestor_id in number);
20 
21 PROCEDURE Notify_Planner_Sup_Change(	p_header_id in number,
22 										p_release_id in number,
23 										p_revision_num in number,
24 										p_chg_req_grp_id in number,
25 										p_planner_id in number);
26 
27 /*************************************************************************
28  * Private Procedure: InsertActionHist
29  * Effects: insert into action history table.
30  *
31  *          It is called when the change request is submitted (by requester
32  *          or buyer) and when buyer responds to the change request.
33  *
34  *          the action can be 'SUBMIT CHANGE', 'ACCEPTED', 'REJECTED'
35  *          or 'RESPOND'
36  *
37  *          the process will commit when it exits.
38  *
39  * Returns:
40  ************************************************************************/
41 PROCEDURE InsertActionHist(
42         p_doc_id number,
43         p_doc_type varchar2,
44         p_doc_subtype varchar2,
45         p_employee_id number,
46         p_action varchar2,
47         p_note varchar2,
48         p_path_id number);
49 
50 PROCEDURE		Commit_CancelPO(
51 			x_return_status out NOCOPY varchar2,
52 			p_can_doc_type in varchar2,
53 			p_can_doc_subtype in varchar2,
54 			p_can_hdr_id in number,
55 			p_can_rel_id in number,
56 			p_can_line_id in number,
57 			p_can_line_loc_id in number,
58 			p_can_reason in varchar2,
59                   p_can_req_flag in varchar2);
60 
61 PROCEDURE initialize(p_employee_id in NUMBER, p_org_id IN NUMBER);
62 
63 -- Making this api public RDP requirements
64 /* procedure NotifySupAllChgRpdWF( 	p_header_id in number,
65 									p_release_id in number,
66 									p_revision_num in number,
67 									p_chg_req_grp_id in number);  */
68 
69 procedure CancelPO( x_return_status out NOCOPY varchar2,
70 					p_header_id in number,
71 					p_release_id in number,
72 					p_chg_req_grp_id in number);
73 
74 Procedure Insert_Acc_Rejection_Row(	p_itemtype        in  varchar2,
75                               	   	p_itemkey         in  varchar2,
76 	                           		p_actid           in  number,
77 				   					p_flag		   in  varchar2);
78 
79 Procedure Update_Chg_Req_If_Po_Apprvd(  p_header_id  in number,
80                                         p_release_id in number);
81 
82 PROCEDURE Notify_Requester_Sup_Change(	p_header_id in number,
83 										p_release_id in number,
84 										p_revision_num in number,
85 										p_chg_req_grp_id in number,
86 										p_requestor_id in number)
87 IS
88 l_api_name varchar2(50):= 'Notify_Requester_Sup_Change';
89 l_progress varchar2(5) := '000';
90 l_seq number;
91 l_item_key 					varchar2(2000);
92 
93 l_supplier_username fnd_user.user_name%type;
94 l_requestor_username fnd_user.user_name%type;
95 l_requestor_disp_name varchar2(2000);
96 
97 n_varname   Wf_Engine.NameTabTyp;
98 n_varval    Wf_Engine.NumTabTyp;
99 t_varname   Wf_Engine.NameTabTyp;
100 t_varval    Wf_Engine.TextTabTyp;
101 BEGIN
102 
103 	if(p_requestor_id is null or p_chg_req_grp_id is null) then
104 		return;
105 	end if;
106 
107 	select PO_SUPPLIER_CHANGE_WF_S.nextval into l_seq from dual;
108 	if(p_release_id is null) then
109 		l_item_key := 'NRSC-'||to_char(p_requestor_id)||'-'||to_char(p_header_id)||'-'||to_char(p_revision_num)||'-'||to_char(l_seq);
110 	else
111 		l_item_key := 'NRSC-'||to_char(p_requestor_id)||'-'||to_char(p_release_id)||'-'||to_char(p_revision_num)||'-'||to_char(l_seq);
112 	end if;
113 
114 	l_progress := '001';
115 
116 	wf_engine.createProcess (	ItemType => 'POSCHORD',
117 								ItemKey => l_item_key,
118 								Process => 'NOTIFY_REQUESTER_SUP_CHN');
119 
120 	l_progress := '002';
121 	wf_directory.GetUserName    ( p_orig_system    => 'PER',
122                                   p_orig_system_id => p_requestor_id,
123                                   p_name           => l_requestor_username,
124                                   p_display_name   => l_requestor_disp_name);
125 
126   IF(l_requestor_disp_name is not null) THEN
127     l_progress := '003';
128 	-- Get Supplier User Name
129 	select user_name
130 	into l_supplier_username
131 	from fnd_user
132 	where user_id = fnd_global.user_id;
133 
134 	n_varname(1) := 'PO_HEADER_ID';
135 	n_varval(1)  := p_header_id;
136 	n_varname(2) := 'PO_RELEASE_ID';
137 	n_varval(2)  := p_release_id;
138 	n_varname(3) := 'PO_REVISION_NUM';
139 	n_varval(3)  := p_revision_num;
140         n_varname(4) := 'CHANGE_REQUEST_GROUP_ID';
141         n_varval(4)  := p_chg_req_grp_id;
142 
143 	t_varname(1) := 'FROM_SUPPLIER';
144 	t_varval(1)  := l_supplier_username;
145 	t_varname(2) := 'NTF_FOR_REQ_SUP_CHN';
146 	t_varval(2)  := 'PLSQLCLOB:PO_ChangeOrderWF_PVT.GEN_NTF_FOR_REQ_SUP_CHN/'||
147 							'@'	||p_header_id||'#'
148 								||p_release_id||'$'
149 								||p_chg_req_grp_id||'%'
150 								||p_requestor_id||'^';
151 
152 	t_varname(3) := 'NTF_FOR_REQ_SUBJECT';
153 	t_varval(3)  := 'PLSQL:PO_ChangeOrderWF_PVT.GEN_NTF_FOR_REQ_SUBJECT/'||l_item_key;
154 	t_varname(4) := 'PREPARER_USERNAME';
155 	t_varval(4)  := l_requestor_username;
156 
157 
158 	l_progress := '004';
159 
160 	Wf_Engine.SetItemAttrNumberArray('POSCHORD', l_item_key,n_varname,n_varval);
161 	Wf_Engine.SetItemAttrTextArray('POSCHORD', l_item_key,t_varname,t_varval);
162 
163 	wf_engine.StartProcess(	ItemType => 'POSCHORD',
164 							ItemKey => l_item_key);
165 END IF;
166 exception when others then
167 	IF g_fnd_debug = 'Y' AND FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL  THEN
168 		FND_LOG.string(FND_LOG.level_unexpected, g_module_prefix ||
169 				l_api_name || '.others_exception', l_progress||':'||sqlerrm);
170 	END IF;
171 
172 END Notify_Requester_Sup_Change;
173 
174 PROCEDURE Notify_Planner_Sup_Change(	p_header_id in number,
175 										p_release_id in number,
176 										p_revision_num in number,
177 										p_chg_req_grp_id in number,
178 										p_planner_id in number)
179 IS
180 l_api_name varchar2(50):= 'Notify_Planner_Sup_Change';
181 l_progress varchar2(5) := '000';
182 l_seq number;
183 l_item_key 					varchar2(2000);
184 
185 l_supplier_username fnd_user.user_name%type;
186 l_planner_username fnd_user.user_name%type;
187 l_planner_disp_name varchar2(2000);
188 l_type_lookup_code po_headers_all.type_lookup_code%type;
189 
190 n_varname   Wf_Engine.NameTabTyp;
191 n_varval    Wf_Engine.NumTabTyp;
192 t_varname   Wf_Engine.NameTabTyp;
193 t_varval    Wf_Engine.TextTabTyp;
194 
195 BEGIN
196 
197 	if(p_planner_id is null or p_chg_req_grp_id is null) then
198 		return;
199 	end if;
200 
201 	select PO_SUPPLIER_CHANGE_WF_S.nextval into l_seq from dual;
202 
203         if(p_release_id is null) then
204                 l_item_key := 'NPSC-'||to_char(p_planner_id)||'-'||to_char(p_header_id)||'-'||to_char(p_revision_num)||'-'||to_char(l_seq);
205         else
206                 l_item_key := 'NPSC-'||to_char(p_planner_id)||'-'||to_char(p_release_id)||'-'||to_char(p_revision_num)||'-'||to_char(l_seq);
207 	end if;
208 
209 	l_progress := '001';
210 	wf_engine.createProcess (	ItemType => 'POSCHORD',
211 								ItemKey => l_item_key,
212 								Process => 'NOTIFY_PLANNER_SUP_CHN');
213 
214 	l_progress := '002';
215 	wf_directory.GetUserName    ( p_orig_system    => 'PER',
216                                   p_orig_system_id => p_planner_id,
217                                   p_name           => l_planner_username,
218                                   p_display_name   => l_planner_disp_name);
219 
220   IF(l_planner_disp_name is not null) THEN
221     l_progress := '003';
222 	-- Get Supplier User Name
223 	select user_name
224 	into l_supplier_username
225 	from fnd_user
226 	where user_id = fnd_global.user_id;
227 
228 
229 	n_varname(1) := 'PO_HEADER_ID';
230 	n_varval(1)  := p_header_id;
231 	n_varname(2) := 'PO_RELEASE_ID';
232 	n_varval(2)  := p_release_id;
233 	n_varname(3) := 'PO_REVISION_NUM';
234 	n_varval(3)  := p_revision_num;
235         n_varname(4) := 'CHANGE_REQUEST_GROUP_ID';
236 	n_varval(4)  := p_chg_req_grp_id;
237 
238 
239 	t_varname(1) := 'FROM_SUPPLIER';
240 	t_varval(1)  := l_supplier_username;
241 	t_varname(2) := 'NTF_FOR_PLAN_SUP_CHN';
242 	t_varval(2)  := 'PLSQLCLOB:PO_ChangeOrderWF_PVT.GEN_NTF_FOR_PLAN_SUP_CHN/'||
243 							'@'	||p_header_id||'#'
244 								||p_release_id||'$'
245 								||p_chg_req_grp_id||'%'
246 								||p_planner_id||'^';
247 
248 	t_varname(3) := 'NTF_FOR_PLAN_SUBJECT';
249 	t_varval(3)  := 'PLSQL:PO_ChangeOrderWF_PVT.GEN_NTF_FOR_PLAN_SUBJECT/'||l_item_key;
250 	t_varname(4) := 'PLANNER_USERNAME';
251 	t_varval(4)  := l_planner_username;
252 
253 
254 	l_progress := '004';
255 
256 	Wf_Engine.SetItemAttrNumberArray('POSCHORD', l_item_key,n_varname,n_varval);
257 	Wf_Engine.SetItemAttrTextArray('POSCHORD', l_item_key,t_varname,t_varval);
258 
259 	wf_engine.StartProcess(	ItemType => 'POSCHORD',
260 							ItemKey => l_item_key);
261 END IF;
262 exception when others then
263 	IF g_fnd_debug = 'Y' AND FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN
264 		FND_LOG.string(FND_LOG.level_unexpected, g_module_prefix ||
265 				l_api_name || '.others_exception', l_progress||':'||sqlerrm);
266 	END IF;
267 
268 END Notify_Planner_Sup_Change;
269 
270 
271 /*************************************************************************
272  * Private Procedure: InsertActionHist
273  * Effects: insert into action history table.
274  *
275  *          It is called when the change request is submitted (by requester
276  *          or buyer) and when buyer responds to the change request.
277  *
278  *          the action can be 'SUBMIT CHANGE', 'ACCEPTED', 'REJECTED'
279  *          or 'RESPOND'
280  *
281  *          the process will commit when it exits.
282  *
283  * Returns:
284  ************************************************************************/
285 PROCEDURE InsertActionHist(
286         p_doc_id number,
287         p_doc_type varchar2,
288         p_doc_subtype varchar2,
289         p_employee_id number,
290         p_action varchar2,
291         p_note varchar2,
292         p_path_id number) is
293 
294 pragma AUTONOMOUS_TRANSACTION;
295 
296 l_action_code po_action_history.action_code%type;
297 l_revision_num number := NULL;
298 l_hist_count   number := NULL;
299 l_sequence_num   number := NULL;
300 l_approval_path_id number;
301 
302 CURSOR action_hist_cursor(doc_id number , doc_type varchar2) is
303    select max(sequence_num)
304    from po_action_history
305    where object_id= doc_id and
306    object_type_code = doc_type;
307 
308 CURSOR action_hist_code_cursor (doc_id number , doc_type varchar2, seq_num number) is
309    select action_code
310    from po_action_history
311    where object_id = doc_id and
312    object_type_code = doc_type and
313    sequence_num = seq_num;
314 
315 
316 x_progress varchar2(3):='000';
317 
318 BEGIN
319 
320   /* Get the document authorization status.
321   ** has been submitted before, i.e.
322   ** First insert a row with  a SUBMIT action.
323   ** Then insert a row with a NULL ACTION_CODE to simulate the forward-to
324   */
325 
326   x_progress := '001';
327 
328   l_approval_path_id := p_path_id;
329 
330   IF p_doc_type IN ('PO','PA') THEN
331 
332     x_progress := '003';
333 
334       select revision_num
335              into l_revision_num
336       from PO_HEADERS_ALL
337       where po_header_id = p_doc_id;
338 
339   ELSIF p_doc_type = 'RELEASE' THEN
340 
341       x_progress := '004';
342 
343       select revision_num
344              into l_revision_num
345       from PO_RELEASES_ALL
346       where po_release_id = p_doc_id;
347 
348   END IF;
349 
350    x_progress := '005';
351 
352    /* Check if this document had been submitted to workflow at some point
353    ** and somehow kicked out. If that's the case, the sequence number
354    ** needs to be incremented by one. Otherwise start at zero.
355    */
356    OPEN action_hist_cursor(p_doc_id , p_doc_type );
357    FETCH action_hist_cursor into l_sequence_num;
358    CLOSE action_hist_cursor;
359    IF l_sequence_num is NULL THEN
360       l_sequence_num := 0;
361    ELSE
362       OPEN action_hist_code_cursor(p_doc_id , p_doc_type, l_sequence_num);
363       FETCH action_hist_code_cursor into l_action_code;
364       l_sequence_num := l_sequence_num +1;
365    END IF;
366 
367 
368    x_progress := '006';
369    IF ((l_sequence_num = 0)
370         OR
371        (l_sequence_num > 0 and l_action_code is NOT NULL)) THEN
372       x_progress := '007';
373       INSERT into PO_ACTION_HISTORY
374              (object_id,
375               object_type_code,
376               object_sub_type_code,
377               sequence_num,
378               last_update_date,
379               last_updated_by,
380               creation_date,
381               created_by,
382               action_code,
383               action_date,
384               employee_id,
385               note,
386               object_revision_num,
387               last_update_login,
388               request_id,
389               program_application_id,
390               program_id,
391               program_update_date,
392               approval_path_id,
393               offline_code)
394              VALUES
395              (p_doc_id,
396               p_doc_type,
397               p_doc_subtype,
398               l_sequence_num,
399               sysdate,
400               nvl(fnd_global.user_id, 1),
401               sysdate,
402               nvl(fnd_global.user_id, 1),
403               p_action,
404               decode(p_action, '',to_date(null,'DD/MM/YYYY'), sysdate),
405               p_employee_id,
406               p_note,
407               l_revision_num,
408               nvl(fnd_global.login_id, 1),
409               0,
410               0,
411               0,
412               '',
413               l_approval_path_id,
414               '' );
415       x_progress := '008';
416 
417     ELSE
418         l_sequence_num := l_sequence_num - 1;
419         x_progress := '009';
420         UPDATE PO_ACTION_HISTORY
421           set object_id = p_doc_id,
422               object_type_code = p_doc_type,
423               object_sub_type_code = p_doc_subtype,
424               sequence_num = l_sequence_num,
425               last_update_date = sysdate,
426               last_updated_by = nvl(fnd_global.user_id, 1),
427               creation_date = sysdate,
428               created_by = nvl(fnd_global.user_id, 1),
429               action_code = p_action,
430               action_date = decode(p_action, '',to_date(null,'DD/MM/YYYY'), sysdate),
431               employee_id = p_employee_id,
432               note = p_note,
433               object_revision_num = l_revision_num,
434               last_update_login = nvl(fnd_global.login_id, 1),
435               request_id = 0,
436               program_application_id = 0,
437               program_id = 0,
438               program_update_date = '',
439               approval_path_id = l_approval_path_id,
440               offline_code = ''
441         WHERE
442               object_id= p_doc_id and
443               object_type_code = p_doc_type and
444               sequence_num = l_sequence_num;
445 
446       x_progress := '010';
447     END IF;
448     x_progress := '017';
449 
450 commit;
451 EXCEPTION
452    WHEN OTHERS THEN
453         wf_core.context('PO_ChangeOrderWF_PVT',
454                                'InsertActionHist'||sqlerrm,x_progress);
455         raise;
456 
457 END InsertActionHist;
458 
459 
460 PROCEDURE Update_Chg_Req_If_Po_Apprvd(p_header_id number ,
461                                          p_release_id number)
462 IS
463 PRAGMA AUTONOMOUS_TRANSACTION;
464 l_api_name varchar2(50) :='Update_Chg_Req_If_Po_Apprvd';
465 Begin
466 
467 IF (p_release_id is not null) THEN
468                 update po_releases_all
469                 set change_requested_by = null
470                 where po_release_id = p_release_id;
471 ELSE
472         update po_headers_all
473                 set change_requested_by = null
474                 where po_header_id = p_header_id;
475 END IF;
476 
477 commit;
478 exception when others then
479 	IF g_fnd_debug = 'Y' AND FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL  THEN
480 		FND_LOG.string(FND_LOG.level_unexpected, g_module_prefix ||
481 				l_api_name || '.others_exception',sqlerrm);
482 	END IF;
483 End Update_Chg_Req_If_Po_Apprvd;
484 
485 
486 
487 PROCEDURE		Commit_CancelPO(
488 			x_return_status out NOCOPY varchar2,
489 			p_can_doc_type in varchar2,
490 			p_can_doc_subtype in varchar2,
491 			p_can_hdr_id in number,
492 			p_can_rel_id in number,
493 			p_can_line_id in number,
494 			p_can_line_loc_id in number,
495 			p_can_reason in varchar2,
496                   p_can_req_flag in varchar2)
497 IS
498 pragma AUTONOMOUS_TRANSACTION;
499 l_api_name varchar2(50):= 'Commit_CancelPO';
500 l_cancel_flag varchar2(1);
501 BEGIN
502 
503 -- start of fix for bug 3864512
504 -- update status of buyer approved cancel shipment requests to a special status WAIT_CANCEL_APP
505 
506 update po_change_requests
507 set request_status = 'WAIT_CANCEL_APP'
508 where request_status = 'BUYER_APP'
509 and action_type = 'CANCELLATION'
510 and request_level = 'SHIPMENT'
511 and document_line_location_id = p_can_line_loc_id;
512 
513 -- end of fix for bug 3864512
514 
515 
516 --Commiting Status to "APPOVED".
517 	if(p_can_rel_id is null) then
518 		update po_headers_all
519 		set authorization_status = 'APPROVED'
520 		where po_header_id = p_can_hdr_id;
521 	else
522 		update po_releases_all
523 		set authorization_status = 'APPROVED'
524 		where po_release_id = p_can_rel_id;
525 	end if;
526 	commit;
527 	PO_Document_Control_GRP.control_document
528 		   (p_api_version  => 1.0,
529 		    p_init_msg_list => FND_API.G_TRUE,
530 		    p_commit     => FND_API.G_TRUE,
531 		    x_return_status  => x_return_status,
532 		    p_doc_type    => p_can_doc_type,
533 		    p_doc_subtype  => p_can_doc_subtype,
534 		    p_doc_id    => p_can_hdr_id,
535 		    p_doc_num    => null,
536 		    p_release_id  => p_can_rel_id,
537 		    p_release_num  => null,
538 		    p_doc_line_id  => p_can_line_id,
539 		    p_doc_line_num  => null,
540 		    p_doc_line_loc_id  => p_can_line_loc_id ,
541 		    p_doc_shipment_num => null,
542 		    p_source     => null,
543 		    p_action      => 'CANCEL',
544 		    p_action_date   => sysdate,
545 		    p_cancel_reason  => p_can_reason,
546 		    p_cancel_reqs_flag  => p_can_req_flag,
547 		    p_print_flag     => 'N',
548 		    p_note_to_vendor  =>null);
549 	--Restoring PO's Authorization status back to "IN PROCESS".
550 	if(p_can_rel_id is null) then
551 		if(p_can_line_loc_id is null) then
552 			select cancel_flag into l_cancel_flag
553 			from po_headers_all
554 			where po_header_id = p_can_hdr_id;
555 		else
556 			select cancel_flag into l_cancel_flag
557 			from po_line_locations_all
558 			where line_location_id = p_can_line_loc_id;
559 		end if;
560 		update po_headers_all
561 		set authorization_status = 'IN PROCESS'
562 		where po_header_id = p_can_hdr_id;
563 	else
564 		if(p_can_line_loc_id is null) then
565 			select cancel_flag into l_cancel_flag
566 			from po_releases_all
567 			where po_release_id = p_can_rel_id;
568 		else
569 			select cancel_flag into l_cancel_flag
570 			from po_line_locations_all
571 			where line_location_id = p_can_line_loc_id;
572 		end if;
573 
574 		update po_releases_all
575 		set authorization_status = 'IN PROCESS'
576 		where po_release_id = p_can_rel_id;
577 	end if;
578 	commit;
579 
580 	if(l_cancel_flag = 'Y') then
581 		x_return_status := FND_API.G_RET_STS_SUCCESS;
582 	end if;
583 
584 exception when others then
585 	IF g_fnd_debug = 'Y' AND FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN
586 		FND_LOG.string(FND_LOG.level_unexpected, g_module_prefix ||
587 				l_api_name || '.others_exception', sqlerrm);
588 	END IF;
589 	x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
590 	--Restoring PO's Authorization status back to "IN PROCESS".
591 	if(p_can_rel_id is null) then
592 		update po_headers_all
593 		set authorization_status = 'IN PROCESS'
594 		where po_header_id = p_can_hdr_id;
595 	else
596 		update po_releases_all
597 		set authorization_status = 'IN PROCESS'
598 		where po_release_id = p_can_rel_id;
599 	end if;
600 	commit;
601 
602 end 	Commit_CancelPO;
603 
604 
605 /*
606 *This API is called by ProcessResponse, to process Cancellation Requests approved by the Buyer.
607 *This API needs to be an autonomous transaction because of the following reason:
608 *The moment when this API is called, the PO authorization status should be "IN PROCESS". However, in order to
609 *call PO Cancel API, the autho status needs to be commited to "APPROVED". Thus this API
610 *commits the status to "APPROVED", afterwhich commits it back to "IN PROCESS".
611 */
612 procedure CancelPO( x_return_status out NOCOPY varchar2,
613 					p_header_id in number,
614 					p_release_id in number,
615 					p_chg_req_grp_id in number)
616 is
617 l_api_name varchar2(50) := 'CancelPO';
618 l_can_req_id number;
619 l_can_hdr_id number;
620 l_can_rel_id number;
621 l_can_line_id number;
622 l_can_line_loc_id number;
623 l_can_reason po_change_requests.request_reason%TYPE;
624 l_cancel_backing_req  po_change_requests.cancel_backing_req%TYPE;
625 l_can_doc_type po_document_types_all.document_type_code%TYPE;
626 l_can_doc_subtype po_document_types_all.document_subtype%TYPE;
627 l_type_lookup_code po_headers_all.type_lookup_code%TYPE;
628 l_return_status varchar2(1);
629 x_progress varchar2(3);
630 l_ship_can_err_msg varchar2(2000) := fnd_message.get_string('PO','PO_CHN_CAN_SHIP_ERR');
631 l_hdr_can_err_msg varchar2(2000) := fnd_message.get_string('PO','PO_CHN_CAN_HDR_ERR');
632 cursor l_cancel_ship_csr(grp_id number) is
633 select
634 	change_request_id,
635 	document_header_id,
636 	po_release_id,
637 	document_line_id,
638 	document_line_location_id,
639 	request_reason,
640       document_type,
641       cancel_backing_req
642 from po_change_requests
643 where change_request_group_id = grp_id
644 and action_type = 'CANCELLATION'
645 and request_level = 'SHIPMENT'
646 and request_status = 'BUYER_APP';
647 
648 cursor l_cancel_hdr_csr(grp_id number) is
649 select
650 	change_request_id,
651 	document_header_id,
652 	po_release_id,
653 	request_reason
654 	,document_type
655 from po_change_requests
656 where change_request_group_id = grp_id
657 and action_type = 'CANCELLATION'
658 and request_level = 'HEADER'
659 and request_status = 'BUYER_APP';
660 
661 begin
662 	x_return_status := FND_API.G_RET_STS_SUCCESS;
663 
664 --Calling PO Cancel API to process Shipment level Cancellation approved by the buyer.
665 	open l_cancel_ship_csr(p_chg_req_grp_id);
666 	loop
667 	fetch l_cancel_ship_csr into
668 		l_can_req_id,
669 		l_can_hdr_id,
670 		l_can_rel_id,
671 		l_can_line_id,
672 		l_can_line_loc_id,
673 		l_can_reason,
674 		l_can_doc_type,
675             l_cancel_backing_req;
676 	exit when l_cancel_ship_csr%NOTFOUND;
677 
678             if (l_cancel_backing_req <> 'Y') then
679                l_cancel_backing_req := 'N';
680             end if;
681 
682 		select type_lookup_code
683 		into l_type_lookup_code
684 		from po_headers_all
685 		where po_header_id = l_can_hdr_id;
686 		if(l_can_rel_id is null) then
687 			l_can_doc_type := 'PO';
688 			if(l_type_lookup_code = 'STANDARD') then
689 				l_can_doc_subtype := 'STANDARD';
690 			elsif(l_type_lookup_code = 'PLANNED') then
691 				l_can_doc_subtype := 'PLANNED';
692 			elsif(l_type_lookup_code = 'BLANKET') then
693 				l_can_doc_type := 'PA';
694 				l_can_doc_subtype := 'BLANKET';
695 			else
696 				l_can_doc_subtype := 'ERROR';
697 			end if;
698 
699 		else
700 			l_can_doc_type := 'RELEASE';
701 			l_can_line_id := null;
702 			if(l_type_lookup_code = 'BLANKET') then
703 				l_can_doc_subtype := 'BLANKET';
704 			elsif(l_type_lookup_code = 'PLANNED') then
705 				l_can_doc_subtype := 'SCHEDULED';
706 			else
707 				l_can_doc_subtype := 'ERROR';
708 			end if;
709 
710 		end if;
711 
712 		Commit_CancelPO(
713 			l_return_status,
714 			l_can_doc_type,
715 			l_can_doc_subtype,
716 			l_can_hdr_id,
717 			l_can_rel_id,
718 			l_can_line_id,
719 			l_can_line_loc_id,
720 			l_can_reason,
721                   l_cancel_backing_req);
722 
723 		--If PO Cancel API fails, we'll assume buyer has rejected the cancellation request
724 		if(l_return_status <> 'S') then
725 			x_return_status := FND_API.G_RET_STS_ERROR;
726 			update po_change_requests
727 			set request_status = 'REJECTED',
728 			response_reason = l_ship_can_err_msg,
729 			validation_error = l_ship_can_err_msg
730 			-- change_active_flag = 'N'       /* commented out due to bug 3574114 */
731 			where change_request_id = l_can_req_id;
732 		end if;
733 	end loop;
734 	close l_cancel_ship_csr;
735 x_progress:='003';
736 
737 --Calling PO Cancel API to process Header level Cancellation approved by the buyer.
738 	open l_cancel_hdr_csr(p_chg_req_grp_id);
739 	loop
740 	fetch l_cancel_hdr_csr into
741 		l_can_req_id,
742 		l_can_hdr_id,
743 		l_can_rel_id,
744 		l_can_reason,
745 		l_can_doc_type;
746 	exit when l_cancel_hdr_csr%NOTFOUND;
747 		select type_lookup_code
748 		into l_type_lookup_code
749 		from po_headers_all
750 		where po_header_id = l_can_hdr_id;
751 		if(l_can_rel_id is null) then
752 			l_can_doc_type := 'PO';
753 			if(l_type_lookup_code = 'STANDARD') then
754 				l_can_doc_subtype := 'STANDARD';
755 			elsif(l_type_lookup_code = 'PLANNED') then
756 				l_can_doc_subtype := 'PLANNED';
757 			elsif(l_type_lookup_code = 'BLANKET') then
758 				l_can_doc_type := 'PA';
759 				l_can_doc_subtype := 'BLANKET';
760 			elsif(l_type_lookup_code = 'CONTRACT') then
761 				l_can_doc_type := 'PA';
762 				l_can_doc_subtype := 'CONTRACT';
763 			else
764 				l_can_doc_subtype := 'ERROR';
765 			end if;
766 
767 		else
768 			l_can_doc_type := 'RELEASE';
769 			if(l_type_lookup_code = 'BLANKET') then
770 				l_can_doc_subtype := 'BLANKET';
771 			elsif(l_type_lookup_code = 'PLANNED') then
772 				l_can_doc_subtype := 'SCHEDULED';
773 			else
774 				l_can_doc_subtype := 'ERROR';
775 			end if;
776 
777 		end if;
778 
779 		Commit_CancelPO(
780 			l_return_status,
781 			l_can_doc_type,
782 			l_can_doc_subtype,
783 			l_can_hdr_id,
784 			l_can_rel_id,
785 			null,
786 			null,
787 			l_can_reason,
788                   'N');
789 
790 		--If PO Cancel API fails, we'll assume buyer has rejected the cancellation request
791 		if(l_return_status <> 'S') then
792 			x_return_status := FND_API.G_RET_STS_ERROR;
793 			update po_change_requests
794 			set request_status = 'REJECTED' ,
795 			response_reason = l_hdr_can_err_msg,
796 			validation_error = l_hdr_can_err_msg,
797 			change_active_flag = 'N'
798 			where change_request_id = l_can_req_id;
799 		end if;
800 	end loop;
801 	close l_cancel_hdr_csr;
802 
803 exception when others then
804 	x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
805 	IF g_fnd_debug = 'Y' AND FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN
806 		FND_LOG.string(FND_LOG.level_unexpected, g_module_prefix ||
807 				l_api_name || '.others_exception', sqlerrm);
808 	END IF;
809 end CancelPO;
810 
811 /*
812 *taken from PO_REQAPPROVAL_INIT1, because it had the procedure as a PRIVATE procedure
813 *This API is used by Register_rejection
814 */
815 Procedure Insert_Acc_Rejection_Row(p_itemtype        in  varchar2,
816                               	   p_itemkey         in  varchar2,
817 	                           p_actid           in  number,
818 				   p_flag		   in  varchar2)
819 is
820 	l_api_name varchar2(50) := 'Insert_Acc_Rejection_Row';
821    l_Acceptance_id      number;
822    --  Bug 2850566
823    l_rowid              ROWID;
824    l_Last_Update_Login  PO_ACCEPTANCES.last_update_login%TYPE;
825    l_acc_po_header_id   PO_HEADERS_ALL.po_header_id%TYPE;
826    -- End of Bug 2850566
827    l_Last_Update_Date   date;
828    l_Last_Updated_By    number;
829    l_Creation_Date      date           	:=  TRUNC(SYSDATE);
830    l_Created_By         number         	:=  fnd_global.user_id;
831    l_Po_Header_Id       number;
832    l_Po_Release_Id      number;
833    l_Action             varchar2(240)	:= 'NEW';
834    l_Action_Date        date    	:=  TRUNC(SYSDATE);
835    l_Employee_Id        number;
836    l_Revision_Num       number;
837    l_Accepted_Flag      varchar2(1)	:= p_flag;
838 --   l_Acceptance_Lookup_Code varchar2(25);
839    l_document_id	number;
840    l_document_type_code po_document_types_all.DOCUMENT_TYPE_CODE%TYPE;
841    l_acceptance_note	PO_ACCEPTANCES.note%TYPE;	--bug 2178922
842    l_rspndr_usr_name    fnd_user.user_name%TYPE := '';
843    l_accepting_party    varchar2(1);
844 begin
845 
846 	SELECT po_acceptances_s.nextval into l_Acceptance_id FROM sys.dual;
847 
848 -- commented out the usage of accptance_type (FPI)
849 /*
850 	l_Acceptance_Lookup_Code := wf_engine.GetItemAttrText( itemtype => p_itemtype,
851                                    		       	       itemkey  => p_itemkey,
852                             	 	               	       aname    => 'ACCEPTANCE_LOOKUP_CODE');
853 */
854 
855 	l_acceptance_note := PO_WF_UTIL_PKG.GetItemAttrText( itemtype => p_itemtype,
856                                    		       	itemkey  => p_itemkey,
857                             	 	               	aname    => 'ACCEPTANCE_COMMENTS');
858 
859 -- commented out the usage of accptance_type (FPI)
860 /*
861 	if (l_Acceptance_Lookup_Code is NULL) then
862 	   if p_flag = 'Y' then
863 		l_Acceptance_Lookup_Code := 'Accepted Terms';
864 	   else
865 		l_Acceptance_Lookup_Code := 'Unacceptable Changes';
866 	   end if;
867         end if;
868 */
869 
870 	l_document_id := wf_engine.GetItemAttrNumber ( itemtype => p_itemtype,
871                                    		       itemkey  => p_itemkey,
872                             	 	               aname    => 'DOCUMENT_ID');
873 
874 	l_document_type_code := wf_engine.GetItemAttrText   ( itemtype => p_itemtype,
875                                    		       	      itemkey  => p_itemkey,
876                             	 	               	      aname    => 'DOCUMENT_TYPE');
877 
878 	-- abort any outstanding acceptance notifications for any previous revision of the document.
879 
880 	if l_document_type_code <> 'RELEASE' then
881 		l_Po_Header_Id := l_document_id;
882 
883 		select
884 			revision_num,
885 			agent_id
886 		into
887 			l_revision_num,
888 			l_employee_id
889 		from po_headers
890 		where po_header_id = l_document_id;
891 	else
892 		l_Po_Release_Id := l_document_id;
893 
894 		select
895 			po_header_id,
896 			revision_num,
897 			agent_id
898 		into
899 			l_Po_Header_Id,
900 			l_revision_num,
901 			l_employee_id
902 		from po_releases
903 		where po_release_id = l_document_id;
904 	end if;
905 
906    --  Bug 2850566 RBAIRRAJ
907    --  Calling the Acceptances row handler to insert into the PO_ACCEPTANCES table
908    --  instead of writing an Insert statement.
909 
910    IF l_po_release_id IS NULL THEN
911      l_acc_po_header_id := l_po_header_id;
912    ELSE
913      l_acc_po_header_id := NULL;
914    END IF;
915 
916    l_rspndr_usr_name := wf_engine.GetItemAttrText   ( itemtype => p_itemtype,
917                                       		       	      itemkey  => p_itemkey,
918                                	 	               	      aname    => 'SUPPLIER_USER_NAME');
919 
920        begin
921          select user_id into   l_Last_Updated_By
922          from fnd_user
923          where user_name = upper(l_rspndr_usr_name);
924          l_accepting_party := 'S';
925        exception when others then
926          --in case of non-isp users there wont be any suppliers
927          l_Last_Updated_By := l_Created_By;
928          l_accepting_party := 'S';  --ack is always by the supplier.
929        end;
930     l_Last_Update_Login := l_Last_Updated_By;
931 
932 
933     PO_ACCEPTANCES_INS_PVT.insert_row(
934             x_rowid                 =>  l_rowid,
935 			x_acceptance_id			=>  l_Acceptance_id,
936             x_Last_Update_Date      =>  l_Last_Update_Date,
937             x_Last_Updated_By       =>  l_Last_Updated_By,
938             x_Last_Update_Login     =>  l_Last_Update_Login,
939 			p_creation_date			=>  l_Creation_Date,
940 			p_created_by			=>  l_Last_Updated_By,
941 			p_po_header_id			=>  l_acc_po_header_id,
942 			p_po_release_id			=>  l_Po_Release_Id,
943 			p_action			    =>  l_Action,
944 			p_action_date			=>  l_Action_Date,
945 			p_employee_id			=>  NULL,
946 			p_revision_num			=>  l_Revision_Num,
947 			p_accepted_flag			=>  l_Accepted_Flag,
948 			p_note                  =>  l_acceptance_note,
949 			p_accepting_party        =>  l_accepting_party
950 			);
951 
952    --  End of Bug 2850566 RBAIRRAJ
953 
954    -- Reset the Acceptance required Flag
955    --Bug 6772960 - Start
956    --Update the last update date when po_headers_all/po_releases_all tables are updated.
957    if l_po_release_id is not null then
958       update po_releases
959       set acceptance_required_flag = 'N',
960       LAST_UPDATE_DATE = SYSDATE,
961       acceptance_due_date = ''
962       where po_release_id = l_po_release_id;
963    else
964       update po_headers
965       set acceptance_required_flag = 'N',
966       LAST_UPDATE_DATE = SYSDATE,
967       acceptance_due_date = ''
968       where po_header_id = l_po_header_id;
969    end if;
970    --Bug 6772960 - End
971 
972 exception
973 	when others then
974 	IF g_fnd_debug = 'Y' AND FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL  THEN
975 		FND_LOG.string(FND_LOG.level_unexpected, g_module_prefix ||
976 				l_api_name || '.others_exception', sqlerrm);
977 	END IF;
978 	raise;
979 end;
980 
981 /*
982 *taken from PO_REQAPPROVAL_INIT1
983 *PO_REQAPPROVAL_INIT1.Register_rejection will be updated to support older version of poxwfpoa.wft
984 *In other words, initial version of poxwfpoa.wft only calls PO_REQ_APPROVAL_INIT1.Register_rejection.
985 *New version will call PO_ChangeOrderWF_PVT.IS_PO_HDR_REJECTED followed by PO_ChangeOrderWF_PVT.Register_rejection.
986 *In order for older version of workflow to have the new functionality, PO_REQ_APPROVAL_INIT1.Register_rejection will
987 *need to include the logic of PO_ChangeOrderWF_PVT.IS_PO_HDR_REJECTED within.
988 */
989 procedure  Register_rejection   (  itemtype        in  varchar2,
990                               	   itemkey         in  varchar2,
991 		                           actid           in number,
992                                    funcmode        in  varchar2,
993                                    result          out NOCOPY varchar2    )
994 is
995 		l_api_name varchar2(50):= 'Register_rejection';
996 		l_progress              varchar2(3) := '000';
997 		l_acceptance_result	varchar2(30);
998 		l_org_id		number;
999         l_document_id           number;
1000         l_document_type_code    po_document_types_all.DOCUMENT_TYPE_CODE%TYPE;
1001         l_vendor po_vendors.vendor_name%TYPE;
1002 		--l_accp_type		varchar2(100);
1003         x_supp_user_name        varchar2(100);
1004         x_supplier_displayname  varchar2(100);
1005 	l_nid                   number;
1006         l_ntf_role_name         varchar2(320);
1007 begin
1008 
1009   -- set the org context
1010   l_org_id := wf_engine.GetItemAttrNumber ( itemtype => itemtype,
1011                                    	    itemkey  => itemkey,
1012                             	 	    aname    => 'ORG_ID');
1013 
1014   PO_MOAC_UTILS_PVT.set_org_context(l_org_id) ;         -- <R12 MOAC>
1015 
1016   fnd_message.set_name ('PO','PO_WF_REJECTED_VALUE');
1017   l_acceptance_result := fnd_message.get;
1018 
1019   l_document_id := wf_engine.GetItemAttrNumber ( itemtype => itemtype,
1020                                                  itemkey  => itemkey,
1021                                                  aname    => 'DOCUMENT_ID');
1022 
1023   l_document_type_code := wf_engine.GetItemAttrText   ( itemtype => itemtype,
1024                                                         itemkey  => itemkey,
1025                                                         aname    => 'DOCUMENT_TYPE');
1026 
1027 	-- commented out the usage of accptance_type (FPI)
1028 	/*
1029   l_accp_type := PO_WF_UTIL_PKG.GetItemAttrText(itemtype => itemtype,
1030                                                	itemkey  => itemkey,
1031                                                	aname    => 'ACCEPTANCE_TYPE');
1032 */
1033 
1034   l_acceptance_result := PO_WF_UTIL_PKG.GetItemAttrText(itemtype => itemtype,
1035                                                		itemkey  => itemkey,
1036                                                		aname    => 'ACCEPTANCE_RESULT');
1037 
1038 if l_document_type_code <> 'RELEASE' then
1039         select pov.vendor_name
1040         into l_vendor
1041         from
1042         	po_vendors pov,
1043         	po_headers poh
1044         where pov.vendor_id = poh.vendor_id
1045         and poh.po_header_id=l_document_id;
1046 else
1047         select pov.vendor_name
1048         into l_vendor
1049         from
1050         	po_releases por,
1051         	po_headers_all poh,   -- <R12 MOAC>
1052         	po_vendors pov
1053         where por.po_release_id = l_document_id
1054         and por.po_header_id    = poh.po_header_id
1055         and poh.vendor_id       = pov.vendor_id;
1056 end if;
1057 
1058 
1059 
1060   PO_WF_UTIL_PKG.SetItemAttrText ( itemtype => itemtype,
1061                                    itemkey  => itemkey,
1062                                    aname    => 'SUPPLIER',
1063                                    avalue   => l_vendor);
1064 
1065 -- commented out the usage of accptance_type (FPI)
1066 /*
1067    IF (l_accp_type is NULL) THEN
1068       PO_WF_UTIL_PKG.SetItemAttrText  (	itemtype => itemtype,
1069                                    	itemkey  => itemkey,
1070                                    	aname    => 'ACCEPTANCE_TYPE',
1071                                    	avalue   => 'Rejected' );
1072    END IF;
1073 */
1074 
1075    IF (l_acceptance_result is NULL) THEN
1076       PO_WF_UTIL_PKG.SetItemAttrText  (	itemtype => itemtype,
1077                               		itemkey  => itemkey,
1078                               		aname    => 'ACCEPTANCE_RESULT',
1079                               		avalue   => fnd_message.get_string('PO','PO_WF_NOTIF_REJ') );
1080    END IF;
1081 
1082    if (l_document_type_code <> 'RELEASE') then
1083 
1084            --dbms_output.put_line('For std pos');
1085           begin
1086            select a.notification_id, a.recipient_role
1087            INTO   l_nid, l_ntf_role_name
1088            from   wf_notifications a,
1089                   wf_item_activity_statuses wa
1090            where  itemkey=wa.item_key
1091            and    itemtype=wa.item_type
1092            and    a.message_name in ('PO_EMAIL_PO_WITH_RESPONSE', 'PO_EMAIL_PO_PDF_WITH_RESPONSE')
1093            and    a.notification_id=wa.notification_id and a.status = 'CLOSED';
1094           exception
1095            when others then l_nid := null;
1096           end;
1097 
1098           else
1099           begin
1100            --dbms_output.put_line('For Releases');
1101            select a.notification_id, a.recipient_role
1102            INTO  l_nid, l_ntf_role_name
1103            from  wf_notifications a,
1104                  wf_item_activity_statuses wa
1105            where itemkey=wa.item_key
1106            and   itemtype=wa.item_type
1107            and   a.message_name in ('PO_EMAIL_PO_WITH_RESPONSE', 'PO_EMAIL_PO_PDF_WITH_RESPONSE')
1108            and   a.notification_id=wa.notification_id and a.status = 'CLOSED';
1109           exception
1110            when others then l_nid := null;
1111          end;
1112        end if;
1113 
1114        if (l_nid is null) then
1115          --we do not want to continue if the notification is not closed.
1116          return;
1117        else
1118         x_supp_user_name := wf_notification.responder(l_nid);
1119        end if;
1120 
1121      if (substr(x_supp_user_name, 1, 6) = 'email:') then
1122         --Get the username and store that in the supplier_user_name.
1123         x_supp_user_name := PO_ChangeOrderWF_PVT.getEmailResponderUserName
1124                                       (x_supp_user_name, l_ntf_role_name);
1125 
1126 
1127      end if;
1128       PO_WF_UTIL_PKG.SetItemAttrText (itemtype => itemtype,
1129                                          itemkey  => itemkey,
1130                                          aname    => 'SUPPLIER_USER_NAME',
1131                                    avalue   => x_supp_user_name);
1132 
1133 
1134 
1135   Insert_Acc_Rejection_Row(itemtype, itemkey, actid, 'N');
1136 
1137 EXCEPTION
1138   WHEN OTHERS THEN
1139 	IF g_fnd_debug = 'Y' AND FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN
1140 		FND_LOG.string(FND_LOG.level_unexpected, g_module_prefix ||
1141 				l_api_name || '.others_exception', sqlerrm);
1142 	END IF;
1143     	wf_core.context('PO_ChangeOrderWF_PVT','Register_rejection',l_progress);
1144     	raise;
1145 end;
1146 
1147 /*
1148 *Kicks of POAPPRV workflow for supplier change or for requester change.
1149 */
1150 procedure KickOffPOApproval( 		p_api_version in number,
1151 									x_return_status out NOCOPY varchar2,
1152 									p_header_id in number,
1153 									p_release_id in number,
1154 									x_return_msg out NOCOPY varchar2)
1155 IS
1156 l_api_name varchar2(50) := 'KickOffPOApproval';
1157 l_org_id number;
1158 l_agent_id number;
1159 l_document_id number;
1160 l_document_num po_headers_all.segment1%TYPE;
1161 l_document_type_code po_document_types_all.DOCUMENT_TYPE_CODE%TYPE;
1162 l_document_subtype po_document_types_all.DOCUMENT_SUBTYPE%TYPE;
1163 l_seq number;
1164 l_item_key po_headers_all.wf_item_key%TYPE;
1165 l_item_type po_headers_all.wf_item_type%TYPE;
1166 l_workflow_process PO_DOCUMENT_TYPES_V.WF_APPROVAL_PROCESS%TYPE;
1167 
1168 /* Bug 2810156 BEGIN*/
1169 l_orig_itemtype		po_headers.wf_item_type%TYPE;
1170 l_orig_itemkey	       	po_headers.wf_item_key%TYPE;
1171 l_user_id		number;
1172 l_resp_id		number;
1173 l_appl_id		number;
1174 /* Bug 2810156 END */
1175 
1176 /* BUG 5228026: Added the l_CommunicatePriceChange variable */
1177  l_CommunicatePriceChange varchar2(1) := 'N';
1178 
1179 BEGIN
1180 
1181 	if p_release_id is not null then
1182 		-- shipment for a release
1183 		l_document_id := p_release_id;
1184 		select 	por.release_type,
1185 			por.org_id ,
1186 			poh.segment1,
1187 			por.agent_id
1188 		into   	l_Document_SubType,
1189 			l_org_id,
1190 			l_document_num,
1191 			l_agent_id
1192 		from po_releases_all por,
1193                      po_headers_all  poh
1194 		where po_release_id = p_release_id
1195                 and  por.po_header_id=poh.po_header_id;
1196 
1197 		l_Document_Type_Code := 'RELEASE';
1198 
1199 	else
1200 		l_document_id := p_header_id;
1201 		select
1202 			poh.segment1,
1203 			poh.agent_id,
1204 			poh.type_lookup_code,
1205 			poh.org_id
1206 		into
1207 			l_document_num,
1208 			l_agent_id,
1209 			l_document_subtype,
1210 			l_org_id
1211 		from po_headers_all poh
1212 		where
1213 		     poh.po_header_id = p_header_id;
1214 
1215 		if l_Document_SubType in ('BLANKET', 'CONTRACT') then
1216 			l_Document_Type_Code := 'PA';
1217 		else
1218 			l_Document_Type_Code := 'PO';
1219 		end if;
1220 	end if;
1221 
1222 	x_return_msg:='001';
1223 
1224        	/* Bug 2810156 BEGIN
1225            Call apps_initialze using IDs stored in the original PO approval workflow.
1226        	*/
1227 
1228         BEGIN
1229 	  if (NVL(l_Document_Type_Code, 'PO') <> 'RELEASE') then
1230              select wf_item_type, wf_item_key
1231              into   l_orig_itemtype, l_orig_itemkey
1232              from   po_headers_all
1233              where  po_header_id = l_document_id;
1234           else
1235              select wf_item_type, wf_item_key
1236              into   l_orig_itemtype, l_orig_itemkey
1237              from   po_releases_all
1238              where  po_release_id = l_document_id;
1239 	  end if;
1240 
1241         EXCEPTION
1242           WHEN NO_DATA_FOUND THEN
1243 	    l_orig_itemtype := null;
1244 	    l_orig_itemkey  := null;
1245         END;
1246 
1247         if (l_orig_itemtype is not null and l_orig_itemkey is not null) then
1248 
1249   	  l_user_id := PO_WF_UTIL_PKG.GetItemAttrNumber(itemtype => l_orig_itemtype,
1250                                          	   itemkey  => l_orig_itemkey,
1251                                          	   aname    => 'USER_ID');
1252 
1253   	  l_resp_id := PO_WF_UTIL_PKG.GetItemAttrNumber(itemtype => l_orig_itemtype,
1254                                          	   itemkey  => l_orig_itemkey,
1255                                          	   aname    => 'RESPONSIBILITY_ID');
1256 
1257   	  l_appl_id := PO_WF_UTIL_PKG.GetItemAttrNumber(itemtype => l_orig_itemtype,
1258                                          	   itemkey  => l_orig_itemkey,
1259                                          	   aname    => 'APPLICATION_ID');
1260 
1261   	   if (l_user_id is not null and
1262       	      l_resp_id is not null and
1263       	      l_appl_id is not null )then
1264 
1265       	    fnd_global.APPS_INITIALIZE(l_user_id, l_resp_id, l_appl_id);
1266 
1267          else
1268 
1269            	initialize (l_agent_id, l_org_id);
1270 
1271   	   end if;
1272 
1273         end if;
1274 
1275 	x_return_msg:='002';
1276 
1277 	if (l_org_id is NOT NULL) then
1278           PO_MOAC_UTILS_PVT.set_org_context(l_org_id) ;         -- <R12 MOAC>
1279 	end if;
1280 
1281        	/* Bug 2810156 END */
1282 
1283 
1284 	x_return_msg:='003';
1285 
1286 	select
1287 		wf_approval_itemtype,
1288 		wf_approval_process
1289 	into
1290 		l_Item_Type,
1291 		l_Workflow_Process
1292 	from PO_DOCUMENT_TYPES_V
1293 	where DOCUMENT_TYPE_CODE = l_Document_Type_Code
1294 	and DOCUMENT_SUBTYPE =  l_Document_Subtype;
1295 
1296     select to_char(PO_WF_ITEMKEY_S.NEXTVAL) into l_seq from sys.dual;
1297     l_Item_Key := to_char(l_Document_ID) || '-' || l_seq;
1298 
1299 
1300 
1301 x_return_msg:='004';
1302 IF g_fnd_debug = 'Y' AND FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN
1303 	FND_LOG.string(FND_LOG.level_procedure, g_module_prefix ||
1304 			l_api_name || '.start_wf_process', l_Item_Type||'*'||l_Item_Key);
1305 END IF;
1306 
1307 IF( (l_Document_Type_Code = 'RELEASE' AND l_Document_Subtype = 'BLANKET') OR (l_Document_Type_Code = 'PO' AND l_Document_Subtype = 'STANDARD') ) THEN
1308         l_CommunicatePriceChange := 'Y';
1309 END IF;
1310 
1311 	PO_REQAPPROVAL_INIT1.Start_WF_Process(
1312 							ItemType => l_Item_Type,
1313 							ItemKey => l_Item_Key,
1314 							WorkflowProcess => l_workflow_process,
1315 							ActionOriginatedFrom => 'POS_SUP_CHN',
1316 							DocumentID => l_document_id,
1317 							DocumentNumber => l_document_num,
1318 							preparerid => l_agent_id,
1319 							DocumentTypeCode => l_Document_Type_Code,
1320 			  				DocumentSubtype => l_Document_Subtype,
1321 			  				SubmitterAction => 'APPROVE',
1322 			  				forwardToID => null,
1323 			  				forwardFromID => null,
1324 			  				DefaultApprovalPathID => null,
1325 							Note => '',
1326 							printFlag => 'N',
1327                                                         CommunicatePriceChange => l_CommunicatePriceChange);
1328 x_return_msg:='005';
1329 	x_return_status := FND_API.G_RET_STS_SUCCESS;
1330 exception when others then
1331 	x_return_msg:='KO_U'||x_return_msg||sqlerrm;
1332 	x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1333 	IF g_fnd_debug = 'Y' AND FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL  THEN
1334 		FND_LOG.string(FND_LOG.level_unexpected, g_module_prefix ||
1335 				l_api_name || '.others_exception', x_return_msg);
1336 	END IF;
1337 
1338 END KickOffPOApproval;
1339 
1340 
1341 /*
1342 *Called from POAPPRV workflow, to execute IsPOHeaderRejected
1343 */
1344 procedure IS_PO_HDR_REJECTED(		  	itemtype        in varchar2,
1345 			                           	itemkey         in varchar2,
1346     	    		                   	actid           in number,
1347 	    	        		         	funcmode        in varchar2,
1348             	            		    resultout       out NOCOPY varchar2)
1349 IS
1350 l_document_id number;
1351 l_document_type_code    po_document_types_all.DOCUMENT_TYPE_CODE%TYPE;
1352 l_revision_num number;
1353 x_progress varchar2(3) := '000';
1354 l_return_status varchar2(1);
1355 BEGIN
1356   	l_document_id := wf_engine.GetItemAttrNumber ( itemtype => itemtype,
1357                                                  itemkey  => itemkey,
1358                                                  aname    => 'DOCUMENT_ID');
1359 
1360   	l_document_type_code := wf_engine.GetItemAttrText   ( itemtype => itemtype,
1361                                                         itemkey  => itemkey,
1362                                                         aname    => 'DOCUMENT_TYPE');
1363 	x_progress := '001';
1364     if (l_document_type_code <> 'RELEASE') then
1365 		select revision_num
1366 		into l_revision_num
1367 		from po_headers_all
1368 		where po_header_id = l_document_id;
1369 		IsPOHeaderRejected(
1370 							1.0,
1371 							l_return_status,
1372 							l_document_id,
1373 							null,
1374 							l_revision_num,
1375 							resultout);
1376 	else
1377 		select revision_num
1378 		into l_revision_num
1379 		from po_releases_all
1380 		where po_release_id = l_document_id;
1381 		IsPOHeaderRejected(
1382 							1.0,
1383 							l_return_status,
1384 							null,
1385 							l_document_id,
1386 							l_revision_num,
1387 							resultout);
1388 	end if;
1389 exception when others then
1390 	wf_core.context('PO_ChangeOrderWF_PVT','IS_PO_HDR_REJECTED',x_progress);
1391     raise;
1392 END IS_PO_HDR_REJECTED;
1393 
1394 /*
1395 *Checks if Supplier has rejected the PO (which requires acknowledgement) at the Header Level.
1396 *Explaination: When a PO (which requires ACK) is created, it holds at the node which awaits supplier to respond.
1397 *If supplier rejects the header level via notification or UI, this node will be awaken, and this API shall return "Y".
1398 *On the other hand, supplier could respond to the PO at the shipment level, and this node will be awaken with
1399 *action = "REJECT", even though there is no rejection. Thus this API will return "N".
1400 */
1401 procedure IsPOHeaderRejected(			p_api_version in number,
1402 										x_return_status out NOCOPY varchar2,
1403 										p_header_id in number,
1404 										p_release_id in number,
1405 										p_revision_num in number,
1406 										x_result_code out NOCOPY varchar)
1407 IS
1408 l_id number;
1409 
1410 cursor l_header_csr(hdr_id number, rev_num number) is
1411 select change_request_id
1412 from po_change_requests
1413 where document_header_id = hdr_id
1414 and document_revision_num = rev_num
1415 and request_status = 'PENDING'
1416 and initiator = 'SUPPLIER'
1417 union all
1418 select acceptance_id
1419 from po_acceptances
1420 where po_header_id = hdr_id
1421 and revision_num = rev_num
1422 and po_line_location_id is not null;
1423 
1424 cursor l_release_csr(rel_id number,rev_num number) is
1425 select change_request_id
1426 from po_change_requests
1427 where po_release_id = rel_id
1428 and document_revision_num = rev_num
1429 and request_status = 'PENDING'
1430 and initiator = 'SUPPLIER'
1431 union all
1432 select acceptance_id
1433 from po_acceptances
1434 where po_release_id = rel_id
1435 and revision_num = rev_num
1436 and po_line_location_id is not null;
1437 
1438 BEGIN
1439 	x_result_code := 'Y';
1440 	-- If any Pending records exist in PO_CHANGE_REQUESTS table, that means the PO is not really header rejected
1441 	if(p_header_id is not null) then
1442 		open l_header_csr(p_header_id, p_revision_num);
1443 		fetch l_header_csr into l_id;
1444 		close l_header_csr;
1445 		if(l_id is not null) then
1446 			x_result_code := 'N';
1447 		end if;
1448 	else
1449 		open l_release_csr(p_release_id, p_revision_num);
1450 		fetch l_release_csr into l_id;
1451 		close l_release_csr;
1452 		if(l_id is not null) then
1453 			x_result_code := 'N';
1454 		end if;
1455 	end if;
1456 exception when others then
1457 	wf_core.context('PO_ChangeOrderWF_PVT','IsPOHeaderRejected','000');
1458     raise;
1459 END IsPOHeaderRejected;
1460 
1461 PROCEDURE GEN_NTF_FOR_REQ_SUP_CHN( 	p_code IN varchar2,
1462 								    display_type   in      Varchar2,
1463 								    document in out NOCOPY clob,
1464 								    document_type  in out NOCOPY  varchar2)
1465 IS
1466 l_header_id number;
1467 l_release_id number;
1468 l_grp_id number;
1469 l_requester_id number;
1470 l_api_name varchar2(50) := 'GEN_NTF_FOR_REQ_SUP_CHN';
1471 
1472 l_progress varchar2(5) := '000';
1473 
1474 l_rel_total number;
1475 l_rel_currency po_headers.currency_code%TYPE;
1476 --l_blanket_num number;
1477 l_blanket_num po_headers_all.segment1%TYPE;
1478 --l_release_num number;
1479 l_release_num po_releases_all.release_num%TYPE;
1480 l_po_doc_id number;
1481 l_rel_doc_id number;
1482 l_acc_req_flag varchar2(1);
1483 l_document varchar2(32000);
1484 l_type_lookup_code po_headers_all.type_lookup_code%TYPE;
1485 l_document_type varchar2(2000);
1486 l_po_num po_headers_all.segment1%TYPE;
1487 l_revision_num number;
1488 l_po_total number;
1489 l_po_currency po_headers.currency_code%TYPE;
1490 l_vendor_id number;
1491 l_vendor_site_id number;
1492 l_supplier_name po_vendors.vendor_name%TYPE;
1493 l_sup_address_line1 po_vendor_sites_all.address_line1%TYPE;
1494 l_sup_address_line2 po_vendor_sites_all.address_line2%TYPE;
1495 l_sup_address_line3 po_vendor_sites_all.address_line3%TYPE;
1496 l_sup_city	po_vendor_sites_all.city%TYPE;
1497 l_sup_state po_vendor_sites_all.state%TYPE;
1498 l_sup_zip po_vendor_sites_all.zip%TYPE;
1499 l_order_date varchar2(2000);
1500 l_fob po_headers_all.fob_lookup_code%TYPE;
1501 l_carrier po_headers_all.ship_via_lookup_code%TYPE;
1502 l_ship_to_id number;
1503 l_ship_addr_l1 hr_locations_all.address_line_1%TYPE;
1504 l_ship_addr_l2 hr_locations_all.address_line_2%TYPE;
1505 l_ship_addr_l3 hr_locations_all.address_line_3%TYPE;
1506 l_ship_city hr_locations_all.town_or_city%TYPE;
1507 l_ship_state hr_locations_all.region_1%TYPE;
1508 l_ship_zip hr_locations_all.postal_code%TYPE;
1509 l_base_url varchar2(2000);
1510 l_base_url_tag varchar2(2000);
1511 
1512 l_document1 VARCHAR2(32000) := '';
1513 
1514 /*varchar Variables for creating the notification*/
1515 lc_line_num 		varchar2(2000);
1516 lc_ship_num 		varchar2(2000);
1517 lc_buyer_pt_num 	mtl_system_items_kfv.concatenated_segments%TYPE;
1518 lc_old_sup_pt_num 	po_lines_all.vendor_product_num%TYPE;
1519 lc_new_sup_pt_num 	po_change_requests.new_supplier_part_number%TYPE;
1520 lc_old_prom_date	varchar2(2000);
1521 lc_new_prom_date	varchar2(2000);
1522 lc_old_qty 			varchar2(2000);
1523 lc_new_qty 			varchar2(2000);
1524 lc_old_price 		varchar2(2000);
1525 lc_new_price 		varchar2(2000);
1526 lc_action_type 		varchar2(2000);
1527 lc_item_desc 		po_lines_all.item_description%TYPE;
1528 lc_uom 				po_lines_all.unit_meas_lookup_code%TYPE;
1529 lc_ship_to_location hr_locations_all.location_code%TYPE;
1530 lc_action_code 		po_change_requests.ACTION_TYPE%TYPE;
1531 lc_reason			po_change_requests.request_reason%TYPE;
1532 lc_split			varchar2(2000);
1533 l_global_agreement_flag  po_headers_all.GLOBAL_AGREEMENT_FLAG%TYPE := 'F';
1534 cursor l_po_chg_req_csr(grp_id number, l_requestor_id number) is
1535 select
1536 	to_char(LINE_NUM),
1537 	to_char(SHIPMENT_NUM),
1538 	BUYER_PT_NUM,
1539 	OLD_SUP_PT_NUM,
1540 	NEW_SUP_PT_NUM,
1541 	to_char(OLD_PROM_DATE,fnd_profile.value_wnps('ICX_DATE_FORMAT_MASK')||' HH24:MI:SS'),
1542 	to_char(NEW_PROM_DATE,fnd_profile.value_wnps('ICX_DATE_FORMAT_MASK')||' HH24:MI:SS'),
1543 	to_char(OLD_QTY),
1544 	to_char(NEW_QTY),
1545 	to_char(OLD_PRICE),
1546 	to_char(NEW_PRICE),
1547 	ACTION_TYPE,
1548 	ITEM_DESCRIPTION,
1549 	UOM,
1550 	SHIP_TO_LOCATION,
1551 	ACTION_CODE,
1552 	REASON,
1553 	SPLIT
1554 from(
1555 
1556 -- LINE CHANGES for Standard PO
1557 select
1558 	pcr.document_line_number LINE_NUM,
1559 	pcr.document_shipment_number SHIPMENT_NUM,
1560 	msi.concatenated_segments BUYER_PT_NUM,
1561 	pla.vendor_product_num OLD_SUP_PT_NUM,
1562 	pcr.new_supplier_part_number NEW_SUP_PT_NUM,
1563 	to_date(null,'DD/MM/YYYY') OLD_PROM_DATE,
1564 	to_date(null,'DD/MM/YYYY') NEW_PROM_DATE,
1565 	pla.quantity OLD_QTY,
1566 	to_number(null) NEW_QTY,
1567 	pla.unit_price		OLD_PRICE,
1568 	pcr.new_price		NEW_PRICE,
1569 	decode(pcr.ACTION_TYPE,'MODIFICATION',fnd_message.get_string('PO','PO_WF_NOTIF_CHANGE'),
1570 						'CANCELLATION',fnd_message.get_string('PO','PO_WF_NOTIF_CANCEL'),'Error') ACTION_TYPE,
1571 	pla.item_description ITEM_DESCRIPTION,
1572 	pla.unit_meas_lookup_code	UOM,
1573 	null SHIP_TO_LOCATION,
1574 	pcr.ACTION_TYPE ACTION_CODE,
1575 	pcr.request_reason REASON,
1576 	null SPLIT
1577 from
1578 	po_change_requests pcr,
1579 	po_lines_archive_all pla,
1580 	mtl_system_items_kfv msi,
1581     FINANCIALS_SYSTEM_PARAMS_ALL FSP
1582 where pla.po_line_id = pcr.document_line_id
1583 and pcr.change_request_group_id =grp_id
1584 and pcr.request_level = 'LINE'
1585 and pcr.request_status in ('PENDING','REQ_APP')
1586 and pla.latest_external_flag = 'Y'
1587 and msi.inventory_item_id(+) = pla.item_id
1588 --and msi.organization_id(+) = pla.org_id
1589 and NVL(MSI.ORGANIZATION_ID, FSP.INVENTORY_ORGANIZATION_ID) = FSP.INVENTORY_ORGANIZATION_ID
1590 AND FSP.ORG_ID = PLA.ORG_ID
1591 and exists (select 1 from po_distributions_all
1592 			where po_line_id = pcr.document_line_id
1593 			and deliver_to_person_id = l_requestor_id)
1594 UNION ALL
1595 
1596 -- SHIPMENT CHANGES for Standard PO and Releases
1597 select
1598 	pla.line_num LINE_NUM,
1599 	pcr.document_shipment_number SHIPMENT_NUM,
1600 	msi.concatenated_segments BUYER_PT_NUM,
1601 	pla.vendor_product_num OLD_SUP_PT_NUM,
1602 	null NEW_SUP_PT_NUM,
1603 	nvl(pcr.old_promised_date, nvl(plla.promised_date,plla.need_by_date))	OLD_PROM_DATE,
1604 	pcr.new_promised_date 	NEW_PROM_DATE,
1605 	plla.quantity	OLD_QTY,
1606 	pcr.new_quantity	NEW_QTY,
1607 	decode(pla.matching_basis,'AMOUNT',DECODE(plla.payment_type,'RATE',nvl(pcr.old_price,plla.price_override),nvl(pcr.old_amount,plla.amount)),nvl(pcr.old_price,plla.price_override))      OLD_PRICE,
1608         decode(pla.matching_basis,'AMOUNT',DECODE(plla.payment_type,'RATE',pcr.new_price,pcr.new_amount),pcr.new_price) NEW_PRICE,
1609 	decode(pcr.ACTION_TYPE,'MODIFICATION',fnd_message.get_string('PO','PO_WF_NOTIF_CHANGE'),'CANCELLATION',
1610 			fnd_message.get_string('PO','PO_WF_NOTIF_CANCEL'),'Error') ACTION_TYPE,
1611 	pla.item_description ITEM_DESCRIPTION,
1612 	nvl(plla.unit_meas_lookup_code,pla.unit_meas_lookup_code) UOM,
1613 	nvl(hla.location_code, substr(rtrim(hz.address1)||'-'||rtrim(hz.city),1,60)) SHIP_TO_LOCATION,
1614 	pcr.ACTION_TYPE ACTION_CODE,
1615 	pcr.REQUEST_REASON REASON,
1616 	null SPLIT
1617 from
1618 	po_change_requests pcr,
1619 	po_lines_archive_all pla,
1620 	po_line_locations_archive_all plla,
1621 	hr_locations_all hla, hz_locations hz,
1622 	mtl_system_items_kfv msi,
1623     FINANCIALS_SYSTEM_PARAMS_ALL FSP
1624 where pcr.change_request_group_id =grp_id
1625 and pla.po_line_id = pcr.document_line_id
1626 and pla.latest_external_flag = 'Y'
1627 and plla.line_location_id = pcr.document_line_location_id
1628 and plla.latest_external_flag = 'Y'
1629 and request_level = 'SHIPMENT'
1630 and pcr.request_status in ('PENDING','REQ_APP')
1631 and hla.location_id(+) = plla.ship_to_location_id
1632 and plla.SHIP_TO_LOCATION_ID = hz.LOCATION_ID(+)
1633 and msi.inventory_item_id(+) = pla.item_id
1634 --and msi.organization_id(+) = pla.org_id
1635 and NVL(MSI.ORGANIZATION_ID, FSP.INVENTORY_ORGANIZATION_ID) = FSP.INVENTORY_ORGANIZATION_ID
1636 AND FSP.ORG_ID = PLA.ORG_ID
1637 and exists (select 1 from po_distributions_all
1638 			where line_location_id = pcr.document_line_location_id
1639 			and deliver_to_person_id = l_requestor_id)
1640 UNION ALL
1641 
1642 --SPLIT SHIPMENTS
1643 select
1644 	pla.line_num LINE_NUM,
1645 	pcr.document_shipment_number SHIPMENT_NUM,
1646 	msi.concatenated_segments BUYER_PT_NUM,
1647 	pla.vendor_product_num OLD_SUP_PT_NUM,
1648 	null NEW_SUP_PT_NUM,
1649 	nvl(pcr.old_promised_date, nvl(plla.promised_date,plla.need_by_date))	OLD_PROM_DATE,
1650 	pcr.new_promised_date 	NEW_PROM_DATE,
1651 	plla.quantity	OLD_QTY,
1652 	pcr.new_quantity	NEW_QTY,
1653 	decode(pla.matching_basis,'AMOUNT',DECODE(nvl(pcr.new_progress_type,plla.payment_type),'RATE',nvl(pcr.new_price,plla.price_override),nvl(pcr.old_amount,plla.amount)),nvl(pcr.old_price,plla.price_override))      OLD_PRICE,
1654         decode(pla.matching_basis,'AMOUNT',DECODE(nvl(pcr.new_progress_type,plla.payment_type),'RATE',pcr.new_price,pcr.new_amount),pcr.new_price) NEW_PRICE,
1655 	decode(pcr.ACTION_TYPE,'MODIFICATION',fnd_message.get_string('PO','PO_WF_NOTIF_CHANGE'),'CANCELLATION',
1656 			fnd_message.get_string('PO','PO_WF_NOTIF_CANCEL'),'Error') ACTION_TYPE,
1657 	pla.item_description ITEM_DESCRIPTION,
1658 	nvl(plla.unit_meas_lookup_code,pla.unit_meas_lookup_code) UOM,
1659 	nvl(hla.location_code, substr(rtrim(hz.address1)||'-'||rtrim(hz.city),1,60)) SHIP_TO_LOCATION,
1660 	pcr.ACTION_TYPE ACTION_CODE,
1661 	pcr.REQUEST_REASON REASON,
1662 	fnd_message.get_string('PO','PO_WF_NOTIF_YES') SPLIT
1663 from
1664 	po_change_requests pcr,
1665 	po_lines_archive_all pla,
1666 	po_line_locations_archive_all plla,
1667 	hr_locations_all hla, hz_locations hz,
1668 	mtl_system_items_kfv msi,
1669     FINANCIALS_SYSTEM_PARAMS_ALL FSP
1670 where pcr.change_request_group_id =grp_id
1671 and pla.po_line_id = pcr.document_line_id
1672 and pla.latest_external_flag = 'Y'
1673 and plla.line_location_id = pcr.parent_line_location_id
1674 and plla.latest_external_flag = 'Y'
1675 and request_level = 'SHIPMENT'
1676 and pcr.request_status in ('PENDING','REQ_APP')
1677 and hla.location_id(+) = plla.ship_to_location_id
1678 and plla.SHIP_TO_LOCATION_ID = hz.LOCATION_ID(+)
1679 and msi.inventory_item_id(+) = pla.item_id
1680 --and msi.organization_id(+) = pla.org_id
1681 and NVL(MSI.ORGANIZATION_ID, FSP.INVENTORY_ORGANIZATION_ID) = FSP.INVENTORY_ORGANIZATION_ID
1682 AND FSP.ORG_ID = PLA.ORG_ID
1683 and exists (select 1 from po_distributions_all
1684 			where line_location_id = pcr.parent_line_location_id
1685 			and deliver_to_person_id = l_requestor_id)
1686 )
1687 order by LINE_NUM,nvl(SHIPMENT_NUM,0);
1688 
1689 l_id number;
1690 l_header_response po_change_requests.request_status%type;
1691 l_header_cancel varchar2(1);
1692 cursor l_header_cancel_csr(grp_id number) is
1693 select change_request_id,
1694 	request_status
1695 from po_change_requests
1696 where change_request_group_id = grp_id
1697 and action_type = 'CANCELLATION'
1698 and request_level = 'HEADER';
1699 
1700 l_additional_changes po_change_requests.additional_changes%type;
1701 
1702 cursor l_header_changes_csr(grp_id number)
1703 is
1704 select additional_changes
1705 from po_change_requests
1706 where change_request_group_id = grp_id
1707 and request_level = 'HEADER'
1708 and additional_changes is not null;
1709 
1710 BEGIN
1711 	l_header_id := 	to_number(substr(p_code,2,						instr(p_code,'#')-instr(p_code,'@')-1));
1712 	l_release_id := to_number(substr(p_code,instr(p_code,'#')+1,	instr(p_code,'$')-instr(p_code,'#')-1));
1713 	l_grp_id := 	to_number(substr(p_code,instr(p_code,'$')+1,	instr(p_code,'%')-instr(p_code,'$')-1));
1714 	l_requester_id := 		  substr(p_code,instr(p_code,'%')+1,	instr(p_code,'^')-instr(p_code,'%')-1);
1715 
1716 	l_progress := '001';
1717 	l_base_url := fnd_profile.value('APPS_WEB_AGENT');
1718 	l_base_url := substr(
1719 							l_base_url,
1720 							0,
1721 							instr(rtrim(l_base_url,'/'),'/',-1,2)-1
1722 						);
1723 	l_base_url_tag := '<base href= "'||l_base_url||'">';
1724 	l_progress := '001a';
1725 
1726 	l_header_cancel := 'N';
1727 	if(l_grp_id is not null) then
1728 		open l_header_cancel_csr(l_grp_id);
1729 		fetch l_header_cancel_csr
1730 		into l_id,
1731 			l_header_response;
1732 		close l_header_cancel_csr;
1733 		if(l_id is not null) then
1734 			l_header_cancel := 'Y';
1735 		end if;
1736 	end if;
1737 
1738 	if(l_release_id is null) then
1739 
1740 		select 	segment1,
1741 				revision_num,
1742 				pos_totals_po_sv.get_po_total(po_header_id),
1743 				currency_code,
1744 				vendor_id,
1745 				vendor_site_id,
1746 				to_char(creation_date, fnd_profile.value_wnps('ICX_DATE_FORMAT_MASK')||' HH24:MI:SS'),
1747 				fob_lookup_code,
1748 				ship_via_lookup_code,
1749 				ship_to_location_id,
1750 				type_lookup_code,
1751 				GLOBAL_AGREEMENT_FLAG
1752 		into
1753 				l_po_num,
1754 				l_revision_num,
1755 				l_po_total,
1756 				l_po_currency,
1757 				l_vendor_id,
1758 				l_vendor_site_id,
1759 				l_order_date,
1760 				l_fob,
1761 				l_carrier,
1762 				l_ship_to_id,
1763 				l_type_lookup_code,
1764 				l_global_agreement_flag
1765 		from po_headers_all
1766 		where po_header_id = l_header_id;
1767 
1768 		if(l_type_lookup_code = 'STANDARD') then
1769 			l_document_type := fnd_message.get_string('PO','PO_WF_NOTIF_STD_PO');
1770 		elsif(l_type_lookup_code = 'PLANNED') then
1771 			l_document_type := fnd_message.get_string('PO','PO_WF_NOTIF_PLAN_PO');
1772 		elsif (l_global_agreement_flag = 'Y' and l_type_lookup_code = 'BLANKET') then
1773  	                l_document_type := fnd_message.get_string('PO','PO_GA_TYPE');
1774 		elsif(l_type_lookup_code = 'BLANKET') then
1775 			l_document_type := fnd_message.get_string('PO','PO_WF_NOTIF_BLANKET');
1776 		elsif(l_type_lookup_code = 'CONTRACT') then
1777 			l_document_type := fnd_message.get_string('PO','PO_POTYPE_CNTR');
1778 		else
1779 			l_document_type := 'Error';
1780 		end if;
1781 
1782 	else
1783 
1784 		select
1785 				ph.segment1,
1786 				pr.release_num,
1787 				pr.revision_num,
1788 				pos_totals_po_sv.get_release_total(pr.po_release_id),
1789 				ph.currency_code,
1790 				ph.vendor_id,
1791 				ph.vendor_site_id,
1792 				to_char(pr.creation_date, fnd_profile.value_wnps('ICX_DATE_FORMAT_MASK')||' HH24:MI:SS'),
1793 				ph.fob_lookup_code,
1794 				ph.ship_via_lookup_code,
1795 				ph.ship_to_location_id,
1796 				ph.type_lookup_code
1797 		into
1798 				l_blanket_num,
1799 				l_release_num,
1800 				l_revision_num,
1801 				l_rel_total,
1802 				l_rel_currency,
1803 				l_vendor_id,
1804 				l_vendor_site_id,
1805 				l_order_date,
1806 				l_fob,
1807 				l_carrier,
1808 				l_ship_to_id,
1809 				l_type_lookup_code
1810 		from po_releases_all pr,
1811 				po_headers_all ph
1812 		where pr.po_release_id = l_release_id
1813 		and pr.po_header_id = ph.po_header_id;
1814 
1815 		if(l_type_lookup_code = 'BLANKET') then
1816 			l_document_type := fnd_message.get_string('PO','PO_WF_NOTIF_BKT_REL');
1817 		elsif(l_type_lookup_code = 'PLANNED') then
1818 			l_document_type := fnd_message.get_string('PO','PO_WF_NOTIF_SCH_REL');
1819 		else
1820 			l_document_type := 'Error';
1821 		end if;
1822 
1823 	end if;
1824 	l_progress := '002';
1825 	select
1826 		vendor_name
1827 	into
1828 		l_supplier_name
1829 	from po_vendors
1830 	where vendor_id = l_vendor_id;
1831 
1832 	select
1833 		address_line1,
1834 		address_line2,
1835 		address_line3,
1836 		city,
1837 		state,
1838 		zip
1839 	into
1840 		l_sup_address_line1,
1841 		l_sup_address_line2,
1842 		l_sup_address_line3,
1843 		l_sup_city,
1844 		l_sup_state,
1845 		l_sup_zip
1846 	from po_vendor_sites_all
1847 	where vendor_site_id = l_vendor_site_id;
1848 
1849       /*  Shipto  */
1850 	select
1851 		address_line_1,
1852 		address_line_2,
1853 		address_line_3,
1854 		town_or_city,
1855 		region_1,
1856 		postal_code
1857 	into
1858 		l_ship_addr_l1,
1859 		l_ship_addr_l2,
1860 		l_ship_addr_l3,
1861 		l_ship_city,
1862 		l_ship_state,
1863 		l_ship_zip
1864 	from hr_locations_all
1865 	where location_id = l_ship_to_id;
1866 	l_progress := '003';
1867 
1868 if(l_release_id is null) then
1869 	l_document :=	l_base_url_tag||
1870 		'
1871 		<font size=3 color=#336699 face=arial><b>'||l_document_type||' '||l_po_num||' '||fnd_message.get_string('PO','PO_WF_NOTIF_REV')
1872 		||' '||l_revision_num||' ('||fnd_message.get_string('PO','PO_WF_NOTIF_TOTAL')||'('||
1873 		l_po_currency||') '||
1874 		TO_CHAR(l_po_total, FND_CURRENCY.GET_FORMAT_MASK(l_po_currency, 30))
1875 		||')</B></font><HR size=1 color=#cccc99>';
1876 else
1877 	l_document :=	l_base_url_tag||
1878 		'
1879 		<font size=3 color=#336699 face=arial><b>'||l_document_type||' '||l_blanket_num||'-'||l_release_num||' '||fnd_message.get_string('PO','PO_WF_NOTIF_REV')||' '
1880 		||l_revision_num||' ('||fnd_message.get_string('PO','PO_WF_NOTIF_TOTAL')||'('||
1881 		l_po_currency||') '||
1882 		TO_CHAR(l_po_total, FND_CURRENCY.GET_FORMAT_MASK(l_po_currency, 30))
1883 		||')</B></font><HR size=1 color=#cccc99>';
1884 end if;
1885 
1886 l_document := l_document||'
1887 <TABLE  cellpadding=2 cellspacing=1>
1888 <TR>
1889 <TD>
1890 <TABLE cellpadding=2 cellspacing=1>
1891 <TR>
1892 <TD nowrap><font color=black><B>
1893                       '||fnd_message.get_string('PO','PO_WF_NOTIF_VENDOR')||'</B></font></TD>
1894 <TD nowrap><font color=black>'||l_supplier_name||'</font></TD>
1895 </TR>
1896 <TR>
1897 <TD nowrap valign=TOP><font color=black><B>
1898                       '||fnd_message.get_string('PO','PO_WF_NOTIF_ADDRESS')||'</B></font></TD>
1899 <TD nowrap><font color=black>';
1900 
1901 if(l_sup_address_line1 is not null) then
1902 	l_document := l_document || l_sup_address_line1||'<BR>';
1903 end if;
1904 if(l_sup_address_line2 is not null) then
1905 	l_document := l_document || l_sup_address_line2||'<BR>';
1906 end if;
1907 if(l_sup_address_line3 is not null) then
1908 	l_document := l_document || l_sup_address_line3||'<BR>';
1909 end if;
1910 
1911 l_document := l_document||l_sup_city||', '||l_sup_state||' '||l_sup_zip||
1912                       '</font></TD>
1913 </TR>
1914 <TR>
1915 	<TD nowrap><font color=black><B>
1916 	'||fnd_message.get_string('PO','PO_WF_NOTIF_FOB')||'</B></font></TD>
1917 	<TD nowrap><font color=black>'||l_fob||
1918     '</font></TD>
1919 </TR>
1920 
1921 
1922 <TR>
1923 <TD nowrap><font color=black><B>
1924                       '||fnd_message.get_string('PO','PO_WF_NOTIF_CARRIER')||'</B></font></TD>
1925 <TD nowrap><font color=black>'||l_carrier||
1926                       '</font></TD>
1927 </TR>
1928 </TABLE>
1929 </TD>
1930 <TD valign=TOP>
1931 <TABLE cellpadding=2 cellspacing=1>
1932 <TR>
1933 	<TD nowrap><font color=black><B>
1934 	'||fnd_message.get_string('PO','PO_WF_NOTIF_ORDER_DATE')||'</B></font></TD>
1935 	<TD nowrap><font color=black>'||l_order_date||
1936     '</font></TD>
1937 </TR>
1938 
1939 <TR>
1940 <TD nowrap valign=TOP><font color=black><B>
1941                       '||fnd_message.get_string('PO','PO_WF_NOTIF_SHPTO_ADD')||'</B></font></TD>
1942 <TD nowrap><font color=black>
1943 						'||
1944 						l_ship_addr_l1||' '||l_ship_addr_l2||' '||l_ship_addr_l3||'<BR>'||
1945 						l_ship_city||', '||l_ship_state||' '||l_ship_zip
1946 						||'
1947 						</font></TD>
1948 </TR>
1949 
1950 </TABLE>
1951 </TD>
1952 </TABLE></P>';
1953 WF_NOTIFICATION.WriteToClob(document,l_document);
1954 
1955 
1956 lc_line_num := null;
1957 if(l_header_cancel = 'N') then
1958 	open l_po_chg_req_csr(l_grp_id,l_requester_id);
1959 	fetch l_po_chg_req_csr
1960 	into
1961 		lc_line_num,
1962 		lc_ship_num,
1963 		lc_buyer_pt_num,
1964 		lc_old_sup_pt_num,
1965 		lc_new_sup_pt_num,
1966 		lc_old_prom_date,
1967 		lc_new_prom_date,
1968 		lc_old_qty,
1969 		lc_new_qty,
1970 		lc_old_price,
1971 		lc_new_price,
1972 		lc_action_type,
1973 		lc_item_desc,
1974 		lc_uom,
1975 		lc_ship_to_location,
1976 		lc_action_code,
1977 		lc_reason,
1978 		lc_split;
1979 	close l_po_chg_req_csr;
1980 	if(lc_line_num is not null) then
1981 		l_document := '<font size=3 color=#336699 face=arial><b>'||
1982 					fnd_message.get_string('PO', 'PO_WF_NOTIF_CH_CA_REQ')||
1983 					'</B></font><HR size=1 color=#cccc99>';
1984 		l_document := l_document||'<TABLE><TR>'|| NL;
1985 	    l_document:= l_document||'<TD class=instructiontext>'||
1986 	      			'<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">'
1987 	                 || fnd_message.get_string('PO', 'PO_WF_NOTIF_NEW_VALUE');
1988 	    l_document := l_document || '</TD></TR>' || NL;
1989 	    l_document := l_document || '<TR>'|| NL;
1990 	    l_document:= l_document||'<TD class=instructiontext>'||
1991 	      			'<img src=/OA_MEDIA/cancelind_status.gif ALT="">'
1992 	                 || fnd_message.get_string('PO', 'PO_WF_NOTIF_CANCEL_PENDING');
1993 	    l_document := l_document || '</TD></TR>' || NL;
1994 	    l_document := l_document || '</TABLE>' || NL;
1995 		l_document := l_document ||'
1996 		<TABLE WIDTH=100% cellpadding=2 cellspacing=1>
1997 		<TR bgcolor=#cccc99>
1998 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_LINE_NUMBER')||'</font></TH>
1999 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_SHIPMENT')||'</font></TH>
2000 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_ITEM')||'</font></TH>
2001 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_SUP_ITEM')||'</font></TH>
2002 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_DOC_DESCRIPTION')||'</font></TH>
2003 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_UNIT')||'</font></TH>';
2004 		if (l_type_lookup_code = 'BLANKET') then
2005 		    l_document := l_document || ' <TH align=left><font color=#336699 >'||fnd_message.get_string('PO','POS_PRICE_BREAK')||'</font></TH>';
2006 		 else
2007 		   l_document := l_document || ' <TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_PRICE')||'</font></TH> ';
2008 		end if;
2009 		l_document := l_document ||
2010 		'<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_QTY_ORDERED')||'</font></TH>
2011 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_PROM_DATE')||'</font></TH>
2012 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_SHIPTO')||'</font></TH>
2013 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_ACTION')||'</font></TH>
2014 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_REASON')||'</font></TH>
2015 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_SPLIT')||'</font></TH>
2016 		</TR>
2017 		</B>';
2018 
2019 		WF_NOTIFICATION.WriteToClob(document,l_document);
2020 
2021 		open l_po_chg_req_csr(l_grp_id,l_requester_id);
2022 		loop
2023 		fetch l_po_chg_req_csr
2024 		into
2025 			lc_line_num,
2026 			lc_ship_num,
2027 			lc_buyer_pt_num,
2028 			lc_old_sup_pt_num,
2029 			lc_new_sup_pt_num,
2030 			lc_old_prom_date,
2031 			lc_new_prom_date,
2032 			lc_old_qty,
2033 			lc_new_qty,
2034 			lc_old_price,
2035 			lc_new_price,
2036 			lc_action_type,
2037 			lc_item_desc,
2038 			lc_uom,
2039 			lc_ship_to_location,
2040 			lc_action_code,
2041 			lc_reason,
2042 			lc_split;
2043 		EXIT when l_po_chg_req_csr%NOTFOUND;
2044 
2045 		if(lc_split is not null) then
2046 			lc_old_sup_pt_num := nvl(lc_old_sup_pt_num,lc_new_sup_pt_num);
2047 			lc_new_sup_pt_num := null;
2048 
2049 			lc_old_price:=nvl(lc_old_price,lc_new_price);
2050 			lc_new_price:= null;
2051 
2052 			lc_new_prom_date:= nvl(lc_new_prom_date,lc_old_prom_date);
2053 			lc_old_prom_date:= null;
2054 
2055 			lc_new_qty:=nvl(lc_new_qty,lc_old_qty);
2056 			lc_old_qty:= null;
2057 		end if;
2058 
2059 		lc_line_num := nvl(lc_line_num,'` ');
2060 		lc_ship_num:= nvl(lc_ship_num,'` ');
2061 		lc_buyer_pt_num:= nvl(lc_buyer_pt_num,'` ');
2062 
2063 		lc_old_sup_pt_num:= nvl(lc_old_sup_pt_num,'` ');
2064 		lc_new_sup_pt_num:= nvl(lc_new_sup_pt_num,'` ');
2065 
2066 		lc_old_prom_date:= nvl(lc_old_prom_date,'` ');
2067 		lc_new_prom_date:= nvl(lc_new_prom_date,'` ');
2068 
2069 		lc_old_qty:= nvl(lc_old_qty,'` ');
2070 		lc_new_qty:= nvl(lc_new_qty,'` ');
2071 
2072 		lc_old_price:= nvl(lc_old_price,'` ');
2073 		lc_new_price:= nvl(lc_new_price,'` ');
2074 
2075 		lc_action_type:= nvl(lc_action_type,'` ');
2076 		lc_item_desc:= nvl(lc_item_desc,'` ');
2077 		lc_uom:= nvl(lc_uom,'` ');
2078 		lc_ship_to_location:= nvl(lc_ship_to_location,'` ');
2079 		lc_reason := nvl(lc_reason,'` ');
2080 		lc_split := nvl(lc_split,'` ');
2081 
2082 
2083 		l_document := '
2084 		        <TR bgcolor=#f7f7e7>
2085 
2086 		        <TD align=left><font color=black>'||lc_line_num||
2087 		                      '</font></TD> ';
2088 
2089 		if(lc_action_code = 'CANCELLATION') then
2090 			l_document := l_document ||'
2091 			        <TD align=left><font color=black>
2092 			        	<TABLE>
2093 			        		<TR>
2094 			        			<TD>
2095 				        			<img src=/OA_MEDIA/cancelind_status.gif ALT="">
2096 			        			</TD>
2097 			        			<TD alight=right>'
2098 				        			||lc_ship_num||
2099 			        			'</TD>
2100 			        		</TR>
2101 			        	</TABLE>
2102 			        </font></TD> ';
2103 		else
2104 			l_document := l_document ||'
2105 			        <TD align=left><font color=black>'||lc_ship_num||
2106 		    	                  '</font></TD> ';
2107 		end if;
2108 		l_document := l_document ||'
2109 			<TD align=left><font color=black>'||lc_buyer_pt_num||
2110 	                                  '</font></TD> ';
2111 
2112 		    if(lc_new_sup_pt_num = '` ') then
2113 				l_document := l_document || '
2114 		        <TD align=left><font color=black>'||lc_old_sup_pt_num||'
2115 		                      </font></TD>';
2116 			else
2117 				l_document := l_document || '
2118 		        <TD align=left><font color=black>
2119 		        	<table>
2120 		        		<tr>
2121 		        			<td>'||lc_old_sup_pt_num||'
2122 		        			</td>
2123 		        		</tr>
2124 		        		<tr>
2125 		        			<td>'||lc_new_sup_pt_num||'
2126 		        			</td>
2127 		        			<td>
2128 		        			<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">
2129 		        			</td>
2130 		        		</tr>
2131 		        	</table>
2132 		                      </font></TD>';
2133 			end if;
2134 
2135 			l_document := l_document || '
2136 		        <TD align=left><font color=black>'||lc_item_desc||
2137 		                      '</font></TD>
2138 
2139 		        <TD align=left><font color=black>'||lc_uom||
2140 		                      '</font></TD> ';
2141 
2142 		    if(lc_new_price = '` ') then
2143 				l_document := l_document || '
2144 		        <TD align=left><font color=black>'||lc_old_price||'
2145 		                      </font></TD>';
2146 			else
2147 				l_document := l_document || '
2148 		        <TD align=left><font color=black>
2149 		        	<table>
2150 		        		<tr>
2151 		        			<td>'||lc_old_price||'
2152 		        			</td>
2153 		        		</tr>
2154 		        		<tr>
2155 		        			<td>'||lc_new_price||'
2156 		        			</td>
2157 		        			<td>
2158 		        			<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">
2159 		        			</td>
2160 		        		</tr>
2161 		        	</table>
2162 		                      </font></TD>';
2163 			end if;
2164 
2165 		    if(lc_new_qty = '` ') then
2166 				l_document := l_document || '
2167 		        <TD align=left><font color=black>'||lc_old_qty||'
2168 		                      </font></TD>';
2169 		    elsif(lc_split <> '` ') then
2170 				l_document := l_document || '
2171 		        <TD align=left><font color=black>
2172 		        	<table>
2173 		        		<tr>
2174 		        			<td>'||lc_new_qty||'
2175 		        			</td>
2176 		        			<td>
2177 		        			<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">
2178 		        			</td>
2179 		        		</tr>
2180 		        	</table>
2181 		                      </font></TD>';
2182 			else
2183 				l_document := l_document || '
2184 		        <TD align=left><font color=black>
2185 		        	<table>
2186 		        		<tr>
2187 		        			<td>'||lc_old_qty||'
2188 		        			</td>
2189 		        		</tr>
2190 		        		<tr>
2191 		        			<td>'||lc_new_qty||'
2192 		        			</td>
2193 		        			<td>
2194 		        			<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">
2195 		        			</td>
2196 		        		</tr>
2197 		        	</table>
2198 		                      </font></TD>';
2199 			end if;
2200 
2201 		     if(lc_new_prom_date = '` ') then
2202 				l_document := l_document || '
2203 		        <TD align=left><font color=black>'||lc_old_prom_date||'
2204 		                      </font></TD>';
2205 		    elsif(lc_split <> '` ') then
2206 				l_document := l_document || '
2207 		        <TD align=left><font color=black>
2208 		        	<table>
2209 		        		<tr>
2210 		        			<td>'||lc_new_prom_date||'
2211 		        			</td>
2212 		        			<td>
2213 		        			<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">
2214 		        			</td>
2215 		        		</tr>
2216 		        	</table>
2217 		                      </font></TD>';
2218 			else
2219 				l_document := l_document || '
2220 		        <TD align=left><font color=black>
2221 		        	<table>
2222 		        		<tr>
2223 		        			<td>'||lc_old_prom_date||'
2224 		        			</td>
2225 		        		</tr>
2226 		        		<tr>
2227 		        			<td>'||lc_new_prom_date||'
2228 		        			</td>
2229 		        			<td>
2230 		        			<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">
2231 		        			</td>
2232 		        		</tr>
2233 		        	</table>
2234 		                      </font></TD>';
2235 			end if;
2236 			l_document := l_document || '
2237 
2238 		        <TD align=left><font color=black>'||lc_ship_to_location||
2239 		                      '</font></TD>
2240 
2241 		        <TD align=left><font color=black>'||lc_action_type||
2242 		                      '</font></TD>
2243 
2244 		        <TD align=left><font color=black>'||lc_reason||
2245 		                      '</font></TD>
2246 
2247 		        <TD align=left><font color=black>'||lc_split||
2248 		                      '</font></TD>
2249 
2250 		        </TR>
2251 			';
2252 			WF_NOTIFICATION.WriteToClob(document,l_document);
2253 
2254 		END LOOP;
2255 		CLOSE l_po_chg_req_csr;
2256 		WF_NOTIFICATION.WriteToClob(document, '
2257 		</TABLE></P>
2258 		');
2259 
2260 
2261 	end if;
2262 	open l_header_changes_csr(l_grp_id);
2263 	fetch l_header_changes_csr into l_additional_changes;
2264 	close l_header_changes_csr;
2265 
2266 	if(l_additional_changes is not null) then
2267 		l_document := '<BR><B>'||fnd_message.get_string('PO','PO_WF_NOTIF_ADD_CHN_REQ')||'</B>'||
2268                 ' `  '||l_additional_changes||'<BR>';
2269 		WF_NOTIFICATION.WriteToClob(document,l_document);
2270 	end if;
2271 
2272 else
2273 	fnd_message.set_name('PO','PO_WF_NOTIF_CAN_ENTIER');
2274 	fnd_message.set_token('DOC',l_document_type);
2275 	l_document := fnd_message.get;
2276 	WF_NOTIFICATION.WriteToClob(document,l_document);
2277 end if;
2278 exception when others then
2279 	WF_NOTIFICATION.WriteToClob(document,'Exception occured:'||sqlerrm);
2280 END GEN_NTF_FOR_REQ_SUP_CHN;
2281 
2282 
2283 PROCEDURE GEN_NTF_FOR_PLAN_SUP_CHN( 	p_code IN varchar2,
2284 								    display_type   in      Varchar2,
2285 								    document in out NOCOPY clob,
2286 								    document_type  in out NOCOPY  varchar2)
2287 IS
2288 l_header_id number;
2289 l_release_id number;
2290 l_grp_id number;
2291 l_planner_id number;
2292 l_api_name varchar2(50) := 'GEN_NTF_FOR_PLAN_SUP_CHN';
2293 
2294 l_progress varchar2(5) := '000';
2295 
2296 l_rel_total number;
2297 l_rel_currency po_headers.currency_code%TYPE;
2298 --l_blanket_num number;
2299 l_blanket_num po_headers_all.segment1%TYPE;
2300 --l_release_num number;
2301 l_release_num po_releases_all.release_num%TYPE;
2302 l_po_doc_id number;
2303 l_rel_doc_id number;
2304 l_acc_req_flag varchar2(1);
2305 l_document varchar2(32000);
2306 l_type_lookup_code po_headers_all.type_lookup_code%TYPE;
2307 l_document_type varchar2(2000);
2308 l_po_num po_headers_all.segment1%TYPE;
2309 l_revision_num number;
2310 l_po_total number;
2311 l_po_currency po_headers.currency_code%TYPE;
2312 l_vendor_id number;
2313 l_vendor_site_id number;
2314 l_supplier_name po_vendors.vendor_name%TYPE;
2315 l_sup_address_line1 po_vendor_sites_all.address_line1%TYPE;
2316 l_sup_address_line2 po_vendor_sites_all.address_line2%TYPE;
2317 l_sup_address_line3 po_vendor_sites_all.address_line3%TYPE;
2318 l_sup_city	po_vendor_sites_all.city%TYPE;
2319 l_sup_state po_vendor_sites_all.state%TYPE;
2320 l_sup_zip po_vendor_sites_all.zip%TYPE;
2321 l_order_date varchar2(2000);
2322 l_fob po_headers_all.fob_lookup_code%TYPE;
2323 l_carrier po_headers_all.ship_via_lookup_code%TYPE;
2324 l_ship_to_id number;
2325 l_ship_addr_l1 hr_locations_all.address_line_1%TYPE;
2326 l_ship_addr_l2 hr_locations_all.address_line_2%TYPE;
2327 l_ship_addr_l3 hr_locations_all.address_line_3%TYPE;
2328 l_ship_city hr_locations_all.town_or_city%TYPE;
2329 l_ship_state hr_locations_all.region_1%TYPE;
2330 l_ship_zip hr_locations_all.postal_code%TYPE;
2331 
2332 l_document1 VARCHAR2(32000) := '';
2333 l_base_url varchar2(2000);
2334 l_base_url_tag varchar2(2000);
2335 
2336 /*varchar Variables for creating the notification*/
2337 lc_change_id 		number;
2338 lc_line_num 		varchar2(2000);
2339 lc_ship_num 		varchar2(2000);
2340 lc_buyer_pt_num 	mtl_system_items_kfv.concatenated_segments%TYPE;
2341 lc_old_sup_pt_num 	po_lines_all.vendor_product_num%TYPE;
2342 lc_new_sup_pt_num 	po_change_requests.new_supplier_part_number%TYPE;
2343 lc_old_prom_date	varchar2(2000);
2344 lc_new_prom_date	varchar2(2000);
2345 lc_old_qty 			varchar2(2000);
2346 lc_new_qty 			varchar2(2000);
2347 lc_old_price 		varchar2(2000);
2348 lc_new_price 		varchar2(2000);
2349 lc_action_type 		varchar2(2000);
2350 lc_item_desc 		po_lines_all.item_description%TYPE;
2351 lc_uom 				po_lines_all.unit_meas_lookup_code%TYPE;
2352 lc_ship_to_location hr_locations_all.location_code%TYPE;
2353 lc_action_code 		po_change_requests.ACTION_TYPE%TYPE;
2354 lc_reason			po_change_requests.request_reason%TYPE;
2355 lc_split			varchar2(2000);
2356 l_global_agreement_flag  po_headers_all.GLOBAL_AGREEMENT_FLAG%TYPE := 'F';
2357 
2358 cursor l_po_chg_req_csr(grp_id number, l_planner_id number) is
2359 select
2360 	distinct
2361 	CHANGE_ID,
2362 	to_char(LINE_NUM),
2363 	to_char(SHIPMENT_NUM),
2364 	BUYER_PT_NUM,
2365 	OLD_SUP_PT_NUM,
2366 	NEW_SUP_PT_NUM,
2367 	to_char(OLD_PROM_DATE,fnd_profile.value_wnps('ICX_DATE_FORMAT_MASK')||' HH24:MI:SS'),
2368 	to_char(NEW_PROM_DATE,fnd_profile.value_wnps('ICX_DATE_FORMAT_MASK')||' HH24:MI:SS'),
2369 	to_char(OLD_QTY),
2370 	to_char(NEW_QTY),
2371 	to_char(OLD_PRICE),
2372 	to_char(NEW_PRICE),
2373 	ACTION_TYPE,
2374 	ITEM_DESCRIPTION,
2375 	UOM,
2376 	SHIP_TO_LOCATION,
2377 	ACTION_CODE,
2378 	REASON,
2379 	SPLIT
2380 from(
2381 
2382 -- LINE CHANGES for Standard PO
2383 select
2384 	pcr.change_request_id CHANGE_ID,
2385 	pcr.document_line_number LINE_NUM,
2386 	pcr.document_shipment_number SHIPMENT_NUM,
2387 	msi.concatenated_segments BUYER_PT_NUM,
2388 	pla.vendor_product_num OLD_SUP_PT_NUM,
2389 	pcr.new_supplier_part_number NEW_SUP_PT_NUM,
2390 	to_date(null,'DD/MM/YYYY') OLD_PROM_DATE,
2391 	to_date(null,'DD/MM/YYYY') NEW_PROM_DATE,
2392 	pla.quantity OLD_QTY,
2393 	to_number(null) NEW_QTY,
2394 	pla.unit_price		OLD_PRICE,
2395 	pcr.new_price		NEW_PRICE,
2396 	decode(pcr.ACTION_TYPE,'MODIFICATION',fnd_message.get_string('PO','PO_WF_NOTIF_CHANGE'),
2397 						'CANCELLATION',fnd_message.get_string('PO','PO_WF_NOTIF_CANCEL'),'Error') ACTION_TYPE,
2398 	pla.item_description ITEM_DESCRIPTION,
2399 	pla.unit_meas_lookup_code	UOM,
2400 	null SHIP_TO_LOCATION,
2401 	pcr.ACTION_TYPE ACTION_CODE,
2402 	pcr.request_reason REASON,
2403 	null SPLIT
2404 from
2405 	po_change_requests pcr,
2406 	po_lines_archive_all pla,
2407 	mtl_system_items_kfv msi,
2408 	mtl_planners mtp,
2409 	financials_system_params_all fsp
2410 where pla.po_line_id = pcr.document_line_id
2411 and pcr.change_request_group_id =grp_id
2412 and pcr.request_level = 'LINE'
2413 and pcr.request_status in ('PENDING','REQ_APP')
2414 and pla.latest_external_flag = 'Y'
2415 and msi.inventory_item_id(+) = pla.item_id
2416 --and msi.organization_id(+) = pla.org_id
2417 and fsp.org_id = pla.org_id
2418 and msi.organization_id = fsp.inventory_organization_id
2419 and msi.planner_code = mtp.planner_code
2420 and msi.organization_id = mtp.organization_id
2421 and mtp.employee_id = l_planner_id
2422 UNION ALL
2423 
2424 -- SHIPMENT CHANGES for Standard PO and Releases
2425 select
2426 	pcr.change_request_id CHANGE_ID,
2427 	pla.line_num LINE_NUM,
2428 	pcr.document_shipment_number SHIPMENT_NUM,
2429 	msi.concatenated_segments BUYER_PT_NUM,
2430 	pla.vendor_product_num OLD_SUP_PT_NUM,
2431 	null NEW_SUP_PT_NUM,
2432 	nvl(pcr.old_promised_date, nvl(plla.promised_date,plla.need_by_date))	OLD_PROM_DATE,
2433 	pcr.new_promised_date 	NEW_PROM_DATE,
2434 	plla.quantity	OLD_QTY,
2435 	pcr.new_quantity	NEW_QTY,
2436 	decode(pla.matching_basis,'AMOUNT',DECODE(plla.payment_type,'RATE',nvl(pcr.old_price,plla.price_override),nvl(pcr.old_amount,plla.amount)),nvl(pcr.old_price,plla.price_override))      OLD_PRICE,
2437         decode(pla.matching_basis,'AMOUNT',DECODE(plla.payment_type,'RATE',pcr.new_price,pcr.new_amount),pcr.new_price) NEW_PRICE,
2438 	decode(pcr.ACTION_TYPE,'MODIFICATION',fnd_message.get_string('PO','PO_WF_NOTIF_CHANGE'),'CANCELLATION',
2439 			fnd_message.get_string('PO','PO_WF_NOTIF_CANCEL'),'Error') ACTION_TYPE,
2440 	pla.item_description ITEM_DESCRIPTION,
2441 	nvl(plla.unit_meas_lookup_code,pla.unit_meas_lookup_code) UOM,
2442 	nvl(hla.location_code, substr(rtrim(hz.address1)||'-'||rtrim(hz.city),1,60)) SHIP_TO_LOCATION,
2443 	pcr.ACTION_TYPE ACTION_CODE,
2444 	pcr.REQUEST_REASON REASON,
2445 	null SPLIT
2446 from
2447 	po_change_requests pcr,
2448 	po_lines_archive_all pla,
2449 	po_line_locations_archive_all plla,
2450 	hr_locations_all hla, hz_locations hz,
2451 	mtl_system_items_kfv msi,
2452 	mtl_planners mtp,
2453 	financials_system_params_all fsp
2454 where pcr.change_request_group_id =grp_id
2455 and pla.po_line_id = pcr.document_line_id
2456 and pla.latest_external_flag = 'Y'
2457 and plla.line_location_id = pcr.document_line_location_id
2458 and plla.latest_external_flag = 'Y'
2459 and request_level = 'SHIPMENT'
2460 and pcr.request_status in ('PENDING','REQ_APP')
2461 and hla.location_id(+) = plla.ship_to_location_id
2462 and plla.SHIP_TO_LOCATION_ID = hz.LOCATION_ID(+)
2463 and msi.inventory_item_id(+) = pla.item_id
2464 --and msi.organization_id(+) = pla.org_id
2465 and fsp.org_id = pla.org_id
2466 and msi.organization_id = fsp.inventory_organization_id
2467 and msi.planner_code = mtp.planner_code
2468 and msi.organization_id = mtp.organization_id
2469 and mtp.employee_id = l_planner_id
2470 
2471 UNION ALL
2472 
2473 --SPLIT SHIPMENTS
2474 select
2475 	pcr.change_request_id CHANGE_ID,
2476 	pla.line_num LINE_NUM,
2477 	pcr.document_shipment_number SHIPMENT_NUM,
2478 	msi.concatenated_segments BUYER_PT_NUM,
2479 	pla.vendor_product_num OLD_SUP_PT_NUM,
2480 	null NEW_SUP_PT_NUM,
2481 	nvl(pcr.old_promised_date, nvl(plla.promised_date,plla.need_by_date))	OLD_PROM_DATE,
2482 	pcr.new_promised_date 	NEW_PROM_DATE,
2483 	plla.quantity	OLD_QTY,
2484 	pcr.new_quantity	NEW_QTY,
2485 	decode(pla.matching_basis,'AMOUNT',DECODE(nvl(pcr.new_progress_type,plla.payment_type),'RATE',nvl(pcr.new_price,plla.price_override),nvl(pcr.old_amount,plla.amount)),nvl(pcr.old_price,plla.price_override))      OLD_PRICE,
2486         decode(pla.matching_basis,'AMOUNT',DECODE(nvl(pcr.new_progress_type,plla.payment_type),'RATE',pcr.new_price,pcr.new_amount),pcr.new_price) NEW_PRICE,
2487 	decode(pcr.ACTION_TYPE,'MODIFICATION',fnd_message.get_string('PO','PO_WF_NOTIF_CHANGE'),'CANCELLATION',
2488 			fnd_message.get_string('PO','PO_WF_NOTIF_CANCEL'),'Error') ACTION_TYPE,
2489 	pla.item_description ITEM_DESCRIPTION,
2490 	nvl(plla.unit_meas_lookup_code,pla.unit_meas_lookup_code) UOM,
2491 	nvl(hla.location_code, substr(rtrim(hz.address1)||'-'||rtrim(hz.city),1,60)) SHIP_TO_LOCATION,
2492 	pcr.ACTION_TYPE ACTION_CODE,
2493 	pcr.REQUEST_REASON REASON,
2494 	fnd_message.get_string('PO','PO_WF_NOTIF_YES') SPLIT
2495 from
2496 	po_change_requests pcr,
2497 	po_lines_archive_all pla,
2498 	po_line_locations_archive_all plla,
2499 	hr_locations_all hla, hz_locations hz,
2500 	mtl_system_items_kfv msi,
2501 	mtl_planners mtp,
2502 	financials_system_params_all fsp
2503 where pcr.change_request_group_id =grp_id
2504 and pla.po_line_id = pcr.document_line_id
2505 and pla.latest_external_flag = 'Y'
2506 and plla.line_location_id = pcr.parent_line_location_id
2507 and plla.latest_external_flag = 'Y'
2508 and request_level = 'SHIPMENT'
2509 and pcr.request_status in ('PENDING','REQ_APP')
2510 and hla.location_id(+) = plla.ship_to_location_id
2511 and plla.SHIP_TO_LOCATION_ID = hz.LOCATION_ID(+)
2512 and msi.inventory_item_id(+) = pla.item_id
2513 --and msi.organization_id(+) = pla.org_id
2514 and fsp.org_id = pla.org_id
2515 and msi.organization_id = fsp.inventory_organization_id
2516 and msi.planner_code = mtp.planner_code
2517 and msi.organization_id = mtp.organization_id
2518 and mtp.employee_id = l_planner_id
2519 )
2520 order by LINE_NUM,nvl(SHIPMENT_NUM,0);
2521 
2522 l_id number;
2523 l_header_response po_change_requests.request_status%type;
2524 l_header_cancel varchar2(1);
2525 cursor l_header_cancel_csr(grp_id number) is
2526 select change_request_id,
2527 	request_status
2528 from po_change_requests
2529 where change_request_group_id = grp_id
2530 and action_type = 'CANCELLATION'
2531 and request_level = 'HEADER';
2532 
2533 l_additional_changes po_change_requests.additional_changes%type;
2534 
2535 cursor l_header_changes_csr(grp_id number)
2536 is
2537 select additional_changes
2538 from po_change_requests
2539 where change_request_group_id = grp_id
2540 and request_level = 'HEADER'
2541 and additional_changes is not null;
2542 
2543 BEGIN
2544 	l_header_id := 	to_number(substr(p_code,2,						instr(p_code,'#')-instr(p_code,'@')-1));
2545 	l_release_id := to_number(substr(p_code,instr(p_code,'#')+1,	instr(p_code,'$')-instr(p_code,'#')-1));
2546 	l_grp_id := 	to_number(substr(p_code,instr(p_code,'$')+1,	instr(p_code,'%')-instr(p_code,'$')-1));
2547 	l_planner_id := 		  substr(p_code,instr(p_code,'%')+1,	instr(p_code,'^')-instr(p_code,'%')-1);
2548 
2549 	l_progress := '001';
2550 	l_base_url := fnd_profile.value('APPS_WEB_AGENT');
2551 	l_base_url := substr(
2552 							l_base_url,
2553 							0,
2554 							instr(rtrim(l_base_url,'/'),'/',-1,2)-1
2555 						);
2556 	l_base_url_tag := '<base href= "'||l_base_url||'">';
2557 	l_progress := '001a';
2558 
2559 	l_header_cancel := 'N';
2560 	if(l_grp_id is not null) then
2561 		open l_header_cancel_csr(l_grp_id);
2562 		fetch l_header_cancel_csr
2563 		into l_id,
2564 			l_header_response;
2565 		close l_header_cancel_csr;
2566 		if(l_id is not null) then
2567 			l_header_cancel := 'Y';
2568 		end if;
2569 	end if;
2570 
2571 	if(l_release_id is null) then
2572 
2573 		select 	segment1,
2574 				revision_num,
2575 				pos_totals_po_sv.get_po_total(po_header_id),
2576 				currency_code,
2577 				vendor_id,
2578 				vendor_site_id,
2579 				to_char(creation_date, fnd_profile.value_wnps('ICX_DATE_FORMAT_MASK')||' HH24:MI:SS'),
2580 				fob_lookup_code,
2581 				ship_via_lookup_code,
2582 				ship_to_location_id,
2583 				type_lookup_code,
2584 				GLOBAL_AGREEMENT_FLAG
2585 		into
2586 				l_po_num,
2587 				l_revision_num,
2588 				l_po_total,
2589 				l_po_currency,
2590 				l_vendor_id,
2591 				l_vendor_site_id,
2592 				l_order_date,
2593 				l_fob,
2594 				l_carrier,
2595 				l_ship_to_id,
2596 				l_type_lookup_code,
2597 				l_global_agreement_flag
2598 		from po_headers_all
2599 		where po_header_id = l_header_id;
2600 
2601 		if(l_type_lookup_code = 'STANDARD') then
2602 			l_document_type := fnd_message.get_string('PO','PO_WF_NOTIF_STD_PO');
2603 		elsif(l_type_lookup_code = 'PLANNED') then
2604 			l_document_type := fnd_message.get_string('PO','PO_WF_NOTIF_PLAN_PO');
2605 		elsif (l_global_agreement_flag = 'Y' and l_type_lookup_code = 'BLANKET') then
2606 	               l_document_type := fnd_message.get_string('PO','PO_GA_TYPE');
2607 		elsif(l_type_lookup_code = 'BLANKET') then
2608 			l_document_type := fnd_message.get_string('PO','PO_WF_NOTIF_BLANKET');
2609 		elsif(l_type_lookup_code = 'CONTRACT') then
2610 			l_document_type := fnd_message.get_string('PO','PO_POTYPE_CNTR');
2611 		else
2612 			l_document_type := 'Error';
2613 		end if;
2614 
2615 	else
2616 
2617 		select
2618 				ph.segment1,
2619 				pr.release_num,
2620 				pr.revision_num,
2621 				pos_totals_po_sv.get_release_total(pr.po_release_id),
2622 				ph.currency_code,
2623 				ph.vendor_id,
2624 				ph.vendor_site_id,
2625 				to_char(pr.creation_date, fnd_profile.value_wnps('ICX_DATE_FORMAT_MASK')||' HH24:MI:SS'),
2626 				ph.fob_lookup_code,
2627 				ph.ship_via_lookup_code,
2628 				ph.ship_to_location_id,
2629 				ph.type_lookup_code
2630 		into
2631 				l_blanket_num,
2632 				l_release_num,
2633 				l_revision_num,
2634 				l_rel_total,
2635 				l_rel_currency,
2636 				l_vendor_id,
2637 				l_vendor_site_id,
2638 				l_order_date,
2639 				l_fob,
2640 				l_carrier,
2641 				l_ship_to_id,
2642 				l_type_lookup_code
2643 		from po_releases_all pr,
2644 				po_headers_all ph
2645 		where pr.po_release_id = l_release_id
2646 		and pr.po_header_id = ph.po_header_id;
2647 
2648 		if(l_type_lookup_code = 'BLANKET') then
2649 			l_document_type := fnd_message.get_string('PO','PO_WF_NOTIF_BKT_REL');
2650 		elsif(l_type_lookup_code = 'PLANNED') then
2651 			l_document_type := fnd_message.get_string('PO','PO_WF_NOTIF_SCH_REL');
2652 		else
2653 			l_document_type := 'Error';
2654 		end if;
2655 
2656 	end if;
2657 	l_progress := '002';
2658 	select
2659 		vendor_name
2660 	into
2661 		l_supplier_name
2662 	from po_vendors
2663 	where vendor_id = l_vendor_id;
2664 
2665 	select
2666 		address_line1,
2667 		address_line2,
2668 		address_line3,
2669 		city,
2670 		state,
2671 		zip
2672 	into
2673 		l_sup_address_line1,
2674 		l_sup_address_line2,
2675 		l_sup_address_line3,
2676 		l_sup_city,
2677 		l_sup_state,
2678 		l_sup_zip
2679 	from po_vendor_sites_all
2680 	where vendor_site_id = l_vendor_site_id;
2681 
2682 	select
2683 		address_line_1,
2684 		address_line_2,
2685 		address_line_3,
2686 		town_or_city,
2687 		region_1,
2688 		postal_code
2689 	into
2690 		l_ship_addr_l1,
2691 		l_ship_addr_l2,
2692 		l_ship_addr_l3,
2693 		l_ship_city,
2694 		l_ship_state,
2695 		l_ship_zip
2696 	from hr_locations_all
2697 	where location_id = l_ship_to_id;
2698 	l_progress := '003';
2699 
2700 if(l_release_id is null) then
2701 	l_document :=l_base_url_tag||
2702 		'
2703 		<font size=3 color=#336699 face=arial><b>'||l_document_type||' '||l_po_num||' '||fnd_message.get_string('PO','PO_WF_NOTIF_REV')
2704 		||' '||l_revision_num||' ('||fnd_message.get_string('PO','PO_WF_NOTIF_TOTAL')||'('||
2705 		l_po_currency||') '||
2706 		TO_CHAR(l_po_total, FND_CURRENCY.GET_FORMAT_MASK(l_po_currency, 30))
2707 		||')</B></font><HR size=1 color=#cccc99>';
2708 else
2709 	l_document :=l_base_url_tag||
2710 		'
2711 		<font size=3 color=#336699 face=arial><b>'||l_document_type||' '||l_blanket_num||'-'||l_release_num||' '||fnd_message.get_string('PO','PO_WF_NOTIF_REV')||' '
2712 		||l_revision_num||' ('||fnd_message.get_string('PO','PO_WF_NOTIF_TOTAL')||'('||
2713 		l_po_currency||') '||
2714 		TO_CHAR(l_po_total, FND_CURRENCY.GET_FORMAT_MASK(l_po_currency, 30))
2715 		||')</B></font><HR size=1 color=#cccc99>';
2716 end if;
2717 
2718 l_document := l_document||'
2719 <TABLE  cellpadding=2 cellspacing=1>
2720 <TR>
2721 <TD>
2722 <TABLE cellpadding=2 cellspacing=1>
2723 <TR>
2724 <TD nowrap><font color=black><B>
2725                       '||fnd_message.get_string('PO','PO_WF_NOTIF_VENDOR')||'</B></font></TD>
2726 <TD nowrap><font color=black>'||l_supplier_name||'</font></TD>
2727 </TR>
2728 <TR>
2729 <TD nowrap valign=TOP><font color=black><B>
2730                       '||fnd_message.get_string('PO','PO_WF_NOTIF_ADDRESS')||'</B></font></TD>
2731 <TD nowrap><font color=black>';
2732 
2733 if(l_sup_address_line1 is not null) then
2734 	l_document := l_document || l_sup_address_line1||'<BR>';
2735 end if;
2736 if(l_sup_address_line2 is not null) then
2737 	l_document := l_document || l_sup_address_line2||'<BR>';
2738 end if;
2739 if(l_sup_address_line3 is not null) then
2740 	l_document := l_document || l_sup_address_line3||'<BR>';
2741 end if;
2742 
2743 l_document := l_document||l_sup_city||', '||l_sup_state||' '||l_sup_zip||
2744                       '</font></TD>
2745 </TR>
2746 <TR>
2747 	<TD nowrap><font color=black><B>
2748 	'||fnd_message.get_string('PO','PO_WF_NOTIF_FOB')||'</B></font></TD>
2749 	<TD nowrap><font color=black>'||l_fob||
2750     '</font></TD>
2751 </TR>
2752 
2753 
2754 <TR>
2755 <TD nowrap><font color=black><B>
2756                       '||fnd_message.get_string('PO','PO_WF_NOTIF_CARRIER')||'</B></font></TD>
2757 <TD nowrap><font color=black>'||l_carrier||
2758                       '</font></TD>
2759 </TR>
2760 </TABLE>
2761 </TD>
2762 <TD valign=TOP>
2763 <TABLE cellpadding=2 cellspacing=1>
2764 <TR>
2765 	<TD nowrap><font color=black><B>
2766 	'||fnd_message.get_string('PO','PO_WF_NOTIF_ORDER_DATE')||'</B></font></TD>
2767 	<TD nowrap><font color=black>'||l_order_date||
2768     '</font></TD>
2769 </TR>
2770 
2771 <TR>
2772 <TD nowrap valign=TOP><font color=black><B>
2773                       '||fnd_message.get_string('PO','PO_WF_NOTIF_SHPTO_ADD')||'</B></font></TD>
2774 <TD nowrap><font color=black>
2775 						'||
2776 						l_ship_addr_l1||' '||l_ship_addr_l2||' '||l_ship_addr_l3||'<BR>'||
2777 						l_ship_city||', '||l_ship_state||' '||l_ship_zip
2778 						||'
2779 						</font></TD>
2780 </TR>
2781 
2782 </TABLE>
2783 </TD>
2784 </TABLE></P>';
2785 WF_NOTIFICATION.WriteToClob(document,l_document);
2786 
2787 lc_line_num := null;
2788 if(l_header_cancel = 'N') then
2789 	open l_po_chg_req_csr(l_grp_id,l_planner_id);
2790 	fetch l_po_chg_req_csr
2791 	into
2792 		lc_change_id,
2793 		lc_line_num,
2794 		lc_ship_num,
2795 		lc_buyer_pt_num,
2796 		lc_old_sup_pt_num,
2797 		lc_new_sup_pt_num,
2798 		lc_old_prom_date,
2799 		lc_new_prom_date,
2800 		lc_old_qty,
2801 		lc_new_qty,
2802 		lc_old_price,
2803 		lc_new_price,
2804 		lc_action_type,
2805 		lc_item_desc,
2806 		lc_uom,
2807 		lc_ship_to_location,
2808 		lc_action_code,
2809 		lc_reason,
2810 		lc_split;
2811 	close l_po_chg_req_csr;
2812 	if(lc_line_num is not null) then
2813 		l_document := '<font size=3 color=#336699 face=arial><b>'||
2814 					fnd_message.get_string('PO', 'PO_WF_NOTIF_CH_CA_REQ')||
2815 					'</B></font><HR size=1 color=#cccc99>';
2816 		l_document := l_document||'<TABLE><TR>'|| NL;
2817 	    l_document:= l_document||'<TD class=instructiontext>'||
2818 	      			'<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">'
2819 	                 || fnd_message.get_string('PO', 'PO_WF_NOTIF_NEW_VALUE');
2820 	    l_document := l_document || '</TD></TR>' || NL;
2821 	    l_document := l_document || '<TR>'|| NL;
2822 	    l_document:= l_document||'<TD class=instructiontext>'||
2823 	      			'<img src=/OA_MEDIA/cancelind_status.gif ALT="">'
2824 	                 || fnd_message.get_string('PO', 'PO_WF_NOTIF_CANCEL_PENDING');
2825 	    l_document := l_document || '</TD></TR>' || NL;
2826 	    l_document := l_document || '</TABLE>' || NL;
2827 		l_document := l_document ||'
2828 		<TABLE WIDTH=100% cellpadding=2 cellspacing=1>
2829 		<TR bgcolor=#cccc99>
2830 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_LINE_NUMBER')||'</font></TH>
2831 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_SHIPMENT')||'</font></TH>
2832 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_ITEM')||'</font></TH>
2833 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_SUP_ITEM')||'</font></TH>
2834 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_DOC_DESCRIPTION')||'</font></TH>
2835 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_UNIT')||'</font></TH>
2836 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_PRICE')||'</font></TH>
2837 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_QTY_ORDERED')||'</font></TH>
2838 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_PROM_DATE')||'</font></TH>
2839 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_SHIPTO')||'</font></TH>
2840 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_ACTION')||'</font></TH>
2841 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_REASON')||'</font></TH>
2842 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_SPLIT')||'</font></TH>
2843 		</TR>
2844 		</B>';
2845 
2846 		WF_NOTIFICATION.WriteToClob(document,l_document);
2847 
2848 		open l_po_chg_req_csr(l_grp_id,l_planner_id);
2849 		loop
2850 		fetch l_po_chg_req_csr
2851 		into
2852 			lc_change_id,
2853 			lc_line_num,
2854 			lc_ship_num,
2855 			lc_buyer_pt_num,
2856 			lc_old_sup_pt_num,
2857 			lc_new_sup_pt_num,
2858 			lc_old_prom_date,
2859 			lc_new_prom_date,
2860 			lc_old_qty,
2861 			lc_new_qty,
2862 			lc_old_price,
2863 			lc_new_price,
2864 			lc_action_type,
2865 			lc_item_desc,
2866 			lc_uom,
2867 			lc_ship_to_location,
2868 			lc_action_code,
2869 			lc_reason,
2870 			lc_split;
2871 		EXIT when l_po_chg_req_csr%NOTFOUND;
2872 
2873 		if(lc_split is not null) then
2874 			lc_old_sup_pt_num := nvl(lc_old_sup_pt_num,lc_new_sup_pt_num);
2875 			lc_new_sup_pt_num := null;
2876 
2877 			lc_old_price:=nvl(lc_old_price,lc_new_price);
2878 			lc_new_price:= null;
2879 
2880 			lc_new_prom_date:= nvl(lc_new_prom_date,lc_old_prom_date);
2881 			lc_old_prom_date:= null;
2882 
2883 			lc_new_qty:=nvl(lc_new_qty,lc_old_qty);
2884 			lc_old_qty:= null;
2885 		end if;
2886 		lc_line_num := nvl(lc_line_num,'` ');
2887 		lc_ship_num:= nvl(lc_ship_num,'` ');
2888 		lc_buyer_pt_num:= nvl(lc_buyer_pt_num,'` ');
2889 		lc_old_sup_pt_num:= nvl(lc_old_sup_pt_num,'` ');
2890 		lc_new_sup_pt_num:= nvl(lc_new_sup_pt_num,'` ');
2891 		lc_old_prom_date:= nvl(lc_old_prom_date,'` ');
2892 		lc_new_prom_date:= nvl(lc_new_prom_date,'` ');
2893 		lc_old_qty:= nvl(lc_old_qty,'` ');
2894 		lc_new_qty:= nvl(lc_new_qty,'` ');
2895 		lc_old_price:= nvl(lc_old_price,'` ');
2896 		lc_new_price:= nvl(lc_new_price,'` ');
2897 		lc_action_type:= nvl(lc_action_type,'` ');
2898 		lc_item_desc:= nvl(lc_item_desc,'` ');
2899 		lc_uom:= nvl(lc_uom,'` ');
2900 		lc_ship_to_location:= nvl(lc_ship_to_location,'` ');
2901 		lc_reason := nvl(lc_reason,'` ');
2902 		lc_split := nvl(lc_split,'` ');
2903 
2904 
2905 		l_document := '
2906 		        <TR bgcolor=#f7f7e7>
2907 
2908 		        <TD align=left><font color=black>'||lc_line_num||
2909 		                      '</font></TD> ';
2910 
2911 		if(lc_action_code = 'CANCELLATION') then
2912 			l_document := l_document ||'
2913 			        <TD align=left><font color=black>
2914 			        	<TABLE>
2915 			        		<TR>
2916 			        			<TD>
2917 				        			<img src=/OA_MEDIA/cancelind_status.gif ALT="">
2918 			        			</TD>
2919 			        			<TD alight=right>'
2920 				        			||lc_ship_num||
2921 			        			'</TD>
2922 			        		</TR>
2923 			        	</TABLE>
2924 			        </font></TD> ';
2925 		else
2926 			l_document := l_document ||'
2927 			        <TD align=left><font color=black>'||lc_ship_num||
2928 		    	                  '</font></TD> ';
2929 		end if;
2930 		l_document := l_document ||'
2931 			<TD align=left><font color=black>'||lc_buyer_pt_num||
2932 	                                  '</font></TD> ';
2933 
2934 		    if(lc_new_sup_pt_num = '` ') then
2935 				l_document := l_document || '
2936 		        <TD align=left><font color=black>'||lc_old_sup_pt_num||'
2937 		                      </font></TD>';
2938 			else
2939 				l_document := l_document || '
2940 		        <TD align=left><font color=black>
2941 		        	<table>
2942 		        		<tr>
2943 		        			<td>'||lc_old_sup_pt_num||'
2944 		        			</td>
2945 		        		</tr>
2946 		        		<tr>
2947 		        			<td>'||lc_new_sup_pt_num||'
2948 		        			</td>
2949 		        			<td>
2950 		        			<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">
2951 		        			</td>
2952 		        		</tr>
2953 		        	</table>
2954 		                      </font></TD>';
2955 			end if;
2956 
2957 			l_document := l_document || '
2958 		        <TD align=left><font color=black>'||lc_item_desc||
2959 		                      '</font></TD>
2960 
2961 		        <TD align=left><font color=black>'||lc_uom||
2962 		                      '</font></TD> ';
2963 
2964 		    if(lc_new_price = '` ') then
2965 				l_document := l_document || '
2966 		        <TD align=left><font color=black>'||lc_old_price||'
2967 		                      </font></TD>';
2968 			else
2969 				l_document := l_document || '
2970 		        <TD align=left><font color=black>
2971 		        	<table>
2972 		        		<tr>
2973 		        			<td>'||lc_old_price||'
2974 		        			</td>
2975 		        		</tr>
2976 		        		<tr>
2977 		        			<td>'||lc_new_price||'
2978 		        			</td>
2979 		        			<td>
2980 		        			<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">
2981 		        			</td>
2982 		        		</tr>
2983 		        	</table>
2984 		                      </font></TD>';
2985 			end if;
2986 
2987 		    if(lc_new_qty = '` ') then
2988 				l_document := l_document || '
2989 		        <TD align=left><font color=black>'||lc_old_qty||'
2990 		                      </font></TD>';
2991 		    elsif(lc_split <> '` ') then
2992 				l_document := l_document || '
2993 		        <TD align=left><font color=black>
2994 		        	<table>
2995 		        		<tr>
2996 		        			<td>'||lc_new_qty||'
2997 		        			</td>
2998 		        			<td>
2999 		        			<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">
3000 		        			</td>
3001 		        		</tr>
3002 		        	</table>
3003 		                      </font></TD>';
3004 			else
3005 				l_document := l_document || '
3006 		        <TD align=left><font color=black>
3007 		        	<table>
3008 		        		<tr>
3009 		        			<td>'||lc_old_qty||'
3010 		        			</td>
3011 		        		</tr>
3012 		        		<tr>
3013 		        			<td>'||lc_new_qty||'
3014 		        			</td>
3015 		        			<td>
3016 		        			<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">
3017 		        			</td>
3018 		        		</tr>
3019 		        	</table>
3020 		                      </font></TD>';
3021 			end if;
3022 
3023 			if(lc_new_prom_date = '` ') then
3024 				l_document := l_document || '
3025 		        <TD align=left><font color=black>'||lc_old_prom_date||'
3026 		                      </font></TD>';
3027 		    elsif(lc_split <> '` ') then
3028 				l_document := l_document || '
3029 		        <TD align=left><font color=black>
3030 		        	<table>
3031 		        		<tr>
3032 		        			<td>'||lc_new_prom_date||'
3033 		        			</td>
3034 		        			<td>
3035 		        			<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">
3036 		        			</td>
3037 		        		</tr>
3038 		        	</table>
3039 		                      </font></TD>';
3040 			else
3041 				l_document := l_document || '
3042 		        <TD align=left><font color=black>
3043 		        	<table>
3044 		        		<tr>
3045 		        			<td>'||lc_old_prom_date||'
3046 		        			</td>
3047 		        		</tr>
3048 		        		<tr>
3049 		        			<td>'||lc_new_prom_date||'
3050 		        			</td>
3051 		        			<td>
3052 		        			<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">
3053 		        			</td>
3054 		        		</tr>
3055 		        	</table>
3056 		                      </font></TD>';
3057 			end if;
3058 			l_document := l_document || '
3059 
3060 		        <TD align=left><font color=black>'||lc_ship_to_location||
3061 		                      '</font></TD>
3062 
3063 		        <TD align=left><font color=black>'||lc_action_type||
3064 		                      '</font></TD>
3065 
3066 		        <TD align=left><font color=black>'||lc_reason||
3067 		                      '</font></TD>
3068 
3069 		        <TD align=left><font color=black>'||lc_split||
3070 		                      '</font></TD>
3071 
3072 		        </TR>
3073 			';
3074 			WF_NOTIFICATION.WriteToClob(document,l_document);
3075 
3076 		END LOOP;
3077 		CLOSE l_po_chg_req_csr;
3078 		WF_NOTIFICATION.WriteToClob(document, '
3079 		</TABLE></P>
3080 		');
3081 	end if;
3082 	open l_header_changes_csr(l_grp_id);
3083 	fetch l_header_changes_csr into l_additional_changes;
3084 	close l_header_changes_csr;
3085 
3086 	if(l_additional_changes is not null) then
3087 		l_document := '<BR><B>'||fnd_message.get_string('PO','PO_WF_NOTIF_ADD_CHN_REQ')||'</B>'||
3088                               ' `  '||l_additional_changes||'<BR>';
3089 		WF_NOTIFICATION.WriteToClob(document,l_document);
3090 	end if;
3091 
3092 else
3093 	fnd_message.set_name('PO','PO_WF_NOTIF_CAN_ENTIER');
3094 	fnd_message.set_token('DOC',l_document_type);
3095 	l_document := fnd_message.get;
3096 	WF_NOTIFICATION.WriteToClob(document,l_document);
3097 end if;
3098 exception when others then
3099 	WF_NOTIFICATION.WriteToClob(document,'Exception occured:'||sqlerrm);
3100 
3101 END GEN_NTF_FOR_PLAN_SUP_CHN;
3102 
3103 /*
3104 * Generate Notification Body for Buyer.
3105 * This Notification will be sent when
3106 * 1. Supplier changes a PO which was in 'APPROVED' status
3107 * 2. Supplier acknowledges a PO (which requires acknowledgement) with acceptance, rejection, or modifications
3108 */
3109 PROCEDURE GEN_NTF_FOR_BUYER_SUP_CHG(p_code IN varchar2,
3110 								    display_type   in      Varchar2,
3111 								    document in out NOCOPY clob,
3112 								    document_type  in out NOCOPY  varchar2)
3113 IS
3114 /* l_api_name varchar2(50) := 'GEN_NTF_FOR_BUYER_SUP_CHG';  --   RDP Not needed , Replaced by JRAD Notification
3115 l_progress varchar2(5) := '000';
3116 l_header_id number;
3117 l_release_id number;
3118 l_grp_id number;
3119 l_rel_total number;
3120 l_rel_currency po_headers.currency_code%TYPE;
3121 --l_blanket_num number;
3122 l_blanket_num po_headers_all.segment1%TYPE;
3123 --l_release_num number;
3124 l_release_num po_releases_all.release_num%TYPE;
3125 l_po_doc_id number;
3126 l_rel_doc_id number;
3127 l_acc_req_flag varchar2(1);
3128 l_document varchar2(32000);
3129 l_type_lookup_code po_headers_all.type_lookup_code%TYPE;
3130 l_document_type varchar2(2000);
3131 l_po_num po_headers_all.segment1%TYPE;
3132 l_revision_num number;
3133 l_po_total number;
3134 l_po_currency po_headers.currency_code%TYPE;
3135 l_vendor_id number;
3136 l_vendor_site_id number;
3137 l_supplier_name po_vendors.vendor_name%TYPE;
3138 l_sup_address_line1 po_vendor_sites_all.address_line1%TYPE;
3139 l_sup_address_line2 po_vendor_sites_all.address_line2%TYPE;
3140 l_sup_address_line3 po_vendor_sites_all.address_line3%TYPE;
3141 l_sup_city	po_vendor_sites_all.city%TYPE;
3142 l_sup_state po_vendor_sites_all.state%TYPE;
3143 l_sup_zip po_vendor_sites_all.zip%TYPE;
3144 l_order_date varchar2(2000);
3145 l_fob po_headers_all.fob_lookup_code%TYPE;
3146 l_carrier po_headers_all.ship_via_lookup_code%TYPE;
3147 l_ship_to_id number;
3148 l_ship_addr_l1 hr_locations_all.address_line_1%TYPE;
3149 l_ship_addr_l2 hr_locations_all.address_line_2%TYPE;
3150 l_ship_addr_l3 hr_locations_all.address_line_3%TYPE;
3151 l_ship_city hr_locations_all.town_or_city%TYPE;
3152 l_ship_state hr_locations_all.region_1%TYPE;
3153 l_ship_zip hr_locations_all.postal_code%TYPE;
3154 
3155 l_document1 VARCHAR2(32000) := '';
3156 l_base_url varchar2(2000);
3157 l_base_url_tag varchar2(2000);
3158 l_new_prom_date	varchar2(2000);
3159 l_old_prom_date	varchar2(2000); */
3160 
3161 /*varchar Variables for creating the notification*/
3162  /* lc_line_num 		varchar2(2000);
3163 lc_ship_num 		varchar2(2000);
3164 lc_buyer_pt_num 	mtl_system_items_kfv.concatenated_segments%TYPE;
3165 lc_old_sup_pt_num 	po_lines_all.vendor_product_num%TYPE;
3166 lc_new_sup_pt_num 	po_change_requests.new_supplier_part_number%TYPE;
3167 lc_old_prom_date        date;
3168 lc_new_prom_date        date;
3169 lc_old_qty 			varchar2(2000);
3170 lc_new_qty 			varchar2(2000);
3171 lc_old_price 		varchar2(2000);
3172 lc_new_price 		varchar2(2000);
3173 lc_action_type 		varchar2(2000);
3174 lc_item_desc 		po_lines_all.item_description%TYPE;
3175 lc_uom 				po_lines_all.unit_meas_lookup_code%TYPE;
3176 lc_ship_to_location hr_locations_all.location_code%TYPE;
3177 lc_action_code 		po_change_requests.ACTION_TYPE%TYPE;
3178 lc_reason			po_change_requests.request_reason%TYPE;
3179 lc_split			varchar2(2000);
3180 l_global_agreement_flag  po_headers_all.GLOBAL_AGREEMENT_FLAG%TYPE;
3181 l_po_rev    number;
3182 l_rel_rev   number;
3183 
3184 cursor l_po_chg_req_csr(grp_id number) is
3185 select
3186 	to_char(LINE_NUM),
3187 	to_char(SHIPMENT_NUM),
3188 	BUYER_PT_NUM,
3189 	OLD_SUP_PT_NUM,
3190 	NEW_SUP_PT_NUM,
3191 	OLD_PROM_DATE,
3192         NEW_PROM_DATE,
3193 	to_char(OLD_QTY),
3194 	to_char(NEW_QTY),
3195 	to_char(OLD_PRICE),
3196 	to_char(NEW_PRICE),
3197 	ACTION_TYPE,
3198 	ITEM_DESCRIPTION,
3199 	UOM,
3200 	SHIP_TO_LOCATION,
3201 	ACTION_CODE,
3202 	REASON,
3203 	SPLIT
3204 from(
3205 
3206 -- LINE CHANGES for Standard PO
3207 select
3208 	pcr.document_line_number LINE_NUM,
3209 	pcr.document_shipment_number SHIPMENT_NUM,
3210 	msi.concatenated_segments BUYER_PT_NUM,
3211 	pla.vendor_product_num OLD_SUP_PT_NUM,
3212 	pcr.new_supplier_part_number NEW_SUP_PT_NUM,
3213 	to_date(null,'DD/MM/YYYY') OLD_PROM_DATE,
3214 	to_date(null,'DD/MM/YYYY') NEW_PROM_DATE,
3215 	pla.quantity OLD_QTY,
3216 	to_number(null) NEW_QTY,
3217 	pla.unit_price		OLD_PRICE,
3218 	pcr.new_price		NEW_PRICE,
3219 	decode(pcr.ACTION_TYPE,'MODIFICATION',fnd_message.get_string('PO','PO_WF_NOTIF_CHANGE'),
3220 						'CANCELLATION',fnd_message.get_string('PO','PO_WF_NOTIF_CANCEL'),'Error') ACTION_TYPE,
3221 	pla.item_description ITEM_DESCRIPTION,
3222 	pla.unit_meas_lookup_code	UOM,
3223 	null SHIP_TO_LOCATION,
3224 	pcr.ACTION_TYPE ACTION_CODE,
3225 	pcr.request_reason REASON,
3226 	null SPLIT
3227 from
3228 	po_change_requests pcr,
3229 	po_lines_archive_all pla,
3230 	mtl_system_items_kfv msi,
3231     FINANCIALS_SYSTEM_PARAMS_ALL FSP
3232 where pla.po_line_id = pcr.document_line_id
3233 and pcr.change_request_group_id =grp_id
3234 and pcr.request_level = 'LINE'
3235 and pcr.request_status = 'PENDING'
3236 and pla.latest_external_flag = 'Y'
3237 and msi.inventory_item_id(+) = pla.item_id
3238 --and msi.organization_id(+) = pla.org_id
3239 and NVL(MSI.ORGANIZATION_ID, FSP.INVENTORY_ORGANIZATION_ID) = FSP.INVENTORY_ORGANIZATION_ID
3240 AND FSP.ORG_ID = PLA.ORG_ID
3241 UNION ALL
3242 
3243 -- SHIPMENT CHANGES for Standard PO and Releases
3244 select
3245 	pla.line_num LINE_NUM,
3246 	pcr.document_shipment_number SHIPMENT_NUM,
3247 	msi.concatenated_segments BUYER_PT_NUM,
3248 	pla.vendor_product_num OLD_SUP_PT_NUM,
3249 	null NEW_SUP_PT_NUM,
3250 	decode(pcr.new_promised_date, null, plla.promised_date, pcr.old_promised_date)	OLD_PROM_DATE,
3251 	pcr.new_promised_date 	NEW_PROM_DATE,
3252 	plla.quantity	OLD_QTY,
3253 	pcr.new_quantity	NEW_QTY,
3254 	plla.price_override	OLD_PRICE,
3255 	pcr.new_price NEW_PRICE,
3256 	decode(pcr.ACTION_TYPE,'MODIFICATION',fnd_message.get_string('PO','PO_WF_NOTIF_CHANGE'),'CANCELLATION',
3257 			fnd_message.get_string('PO','PO_WF_NOTIF_CANCEL'),'Error') ACTION_TYPE,
3258 	pla.item_description ITEM_DESCRIPTION,
3259 	nvl(plla.unit_meas_lookup_code,pla.unit_meas_lookup_code) UOM,
3260 		nvl(hla.location_code, substr(rtrim(hz.address1)||'-'||rtrim(hz.city),1,60)) SHIP_TO_LOCATION,
3261 	pcr.ACTION_TYPE ACTION_CODE,
3262 	pcr.REQUEST_REASON REASON,
3263 	null SPLIT
3264 from
3265 	po_change_requests pcr,
3266 	po_lines_archive_all pla,
3267 	po_line_locations_archive_all plla,
3268 	hr_locations_all hla, hz_locations hz,
3269 	mtl_system_items_kfv msi,
3270     FINANCIALS_SYSTEM_PARAMS_ALL FSP
3271 where pcr.change_request_group_id =grp_id
3272 and pla.po_line_id = pcr.document_line_id
3273 and pla.latest_external_flag = 'Y'
3274 and plla.line_location_id = pcr.document_line_location_id
3275 and plla.latest_external_flag = 'Y'
3276 and request_level = 'SHIPMENT'
3277 and pcr.request_status = 'PENDING'
3278 and hla.location_id(+) = plla.ship_to_location_id
3279 and plla.SHIP_TO_LOCATION_ID = hz.LOCATION_ID(+)
3280 and msi.inventory_item_id(+) = pla.item_id
3281 --and msi.organization_id(+) = pla.org_id
3282 and NVL(MSI.ORGANIZATION_ID, FSP.INVENTORY_ORGANIZATION_ID) = FSP.INVENTORY_ORGANIZATION_ID
3283 AND FSP.ORG_ID = PLA.ORG_ID
3284 UNION ALL
3285 
3286 --SPLIT SHIPMENTS
3287 select
3288 	pla.line_num LINE_NUM,
3289 	pcr.document_shipment_number SHIPMENT_NUM,
3290 	msi.concatenated_segments BUYER_PT_NUM,
3291 	pla.vendor_product_num OLD_SUP_PT_NUM,
3292 	null NEW_SUP_PT_NUM,
3293 	decode(pcr.new_promised_date, null, plla.promised_date, pcr.old_promised_date)	OLD_PROM_DATE,
3294 	pcr.new_promised_date 	NEW_PROM_DATE,
3295 	plla.quantity	OLD_QTY,
3296 	pcr.new_quantity	NEW_QTY,
3297 	plla.price_override	OLD_PRICE,
3298 	pcr.new_price NEW_PRICE,
3299 	decode(pcr.ACTION_TYPE,'MODIFICATION',fnd_message.get_string('PO','PO_WF_NOTIF_CHANGE'),'CANCELLATION',
3300 			fnd_message.get_string('PO','PO_WF_NOTIF_CANCEL'),'Error') ACTION_TYPE,
3301 	pla.item_description ITEM_DESCRIPTION,
3302 	nvl(plla.unit_meas_lookup_code,pla.unit_meas_lookup_code) UOM,
3303 	nvl(hla.location_code, substr(rtrim(hz.address1)||'-'||rtrim(hz.city),1,60)) SHIP_TO_LOCATION,
3304 	pcr.ACTION_TYPE ACTION_CODE,
3305 	pcr.REQUEST_REASON REASON,
3306 	fnd_message.get_string('PO','PO_WF_NOTIF_YES') SPLIT
3307 from
3308 	po_change_requests pcr,
3309 	po_lines_archive_all pla,
3310 	po_line_locations_archive_all plla,
3311 	hr_locations_all hla, hz_locations hz,
3312 	mtl_system_items_kfv msi,
3313     FINANCIALS_SYSTEM_PARAMS_ALL FSP
3314 where pcr.change_request_group_id =grp_id
3315 and pla.po_line_id = pcr.document_line_id
3316 and pla.latest_external_flag = 'Y'
3317 and plla.line_location_id = pcr.parent_line_location_id
3318 and plla.latest_external_flag = 'Y'
3319 and request_level = 'SHIPMENT'
3320 and pcr.request_status = 'PENDING'
3321 and hla.location_id(+) = plla.ship_to_location_id
3322 and plla.SHIP_TO_LOCATION_ID = hz.LOCATION_ID(+)
3323 and msi.inventory_item_id(+) = pla.item_id
3324 --and msi.organization_id(+) = pla.org_id
3325 and NVL(MSI.ORGANIZATION_ID, FSP.INVENTORY_ORGANIZATION_ID) = FSP.INVENTORY_ORGANIZATION_ID
3326 AND FSP.ORG_ID = PLA.ORG_ID
3327 )
3328 order by LINE_NUM,nvl(SHIPMENT_NUM,0);
3329 
3330 cursor l_po_ack_csr(l_po_document_id number, l_rel_document_id number, l_rev number)
3331 IS
3332 select
3333 	to_char(LINE_NUM),
3334 	to_char(SHIPMENT_NUM),
3335 	BUYER_PT_NUM,
3336 	OLD_SUP_PT_NUM,
3337 	NEW_SUP_PT_NUM,
3338         OLD_PROM_DATE,
3339         NEW_PROM_DATE,
3340 	to_char(OLD_QTY),
3341 	to_char(NEW_QTY),
3342 	to_char(OLD_PRICE),
3343 	to_char(NEW_PRICE),
3344 	ACTION_TYPE,
3345 	ITEM_DESCRIPTION,
3346 	UOM,
3347 	SHIP_TO_LOCATION,
3348 	NOTE
3349 from(
3350 -- SHIPMENT ACCEPTANCE/REJECTION for Standard PO
3351 select
3352 	pla.line_num LINE_NUM,
3353 	plla.shipment_num SHIPMENT_NUM,
3354 	msi.concatenated_segments BUYER_PT_NUM,
3355 	pla.VENDOR_PRODUCT_NUM OLD_SUP_PT_NUM,
3356 	null NEW_SUP_PT_NUM,
3357 	nvl(plla.promised_date,plla.need_by_date)	OLD_PROM_DATE,
3358 	to_date(null,'DD/MM/YYYY') NEW_PROM_DATE,
3359 	plla.quantity OLD_QTY,
3360 	to_number(null) NEW_QTY,
3361 	plla.price_override OLD_PRICE,
3362 	to_number(null) NEW_PRICE,
3363 	decode(pa.accepted_flag,'Y',fnd_message.get_string('PO','PO_WF_NOTIF_ACCEPT'),
3364 					'N',fnd_message.get_string('PO','PO_WF_NOTIF_REJECT'),'NA') ACTION_TYPE,
3365 	pla.item_description ITEM_DESCRIPTION,
3366 	nvl(plla.unit_meas_lookup_code,pla.unit_meas_lookup_code) UOM,
3367 	nvl(hla.location_code, substr(rtrim(hz.address1)||'-'||rtrim(hz.city),1,60)) SHIP_TO_LOCATION,
3368 	pa.note NOTE
3369 from
3370 	po_acceptances pa,
3371 	po_lines_archive_all pla,
3372 	po_line_locations_archive_all plla,
3373 	hr_locations_all hla, hz_locations hz,
3374 	mtl_system_items_kfv msi,
3375     FINANCIALS_SYSTEM_PARAMS_ALL FSP
3376 where plla.po_header_id = l_po_document_id
3377 and plla.po_line_id = pla.po_line_id
3378 and pa.po_line_location_id = plla.line_location_id
3379 and hla.location_id(+) = plla.ship_to_location_id
3380 and plla.SHIP_TO_LOCATION_ID = hz.LOCATION_ID(+)
3381 --and plla.latest_external_flag = 'Y'
3382 and pla.latest_external_flag = 'Y'
3383 and pa.revision_num = l_rev
3384 and plla.revision_num = (select  max(revision_num)
3385                           from po_line_locations_archive_all plla2
3386                           where plla2.line_location_id =  plla.line_location_id and
3387                           plla.revision_num <= l_rev)
3388 and msi.inventory_item_id(+) = pla.item_id
3389 --and msi.organization_id(+) = pla.org_id
3390 and NVL(MSI.ORGANIZATION_ID, FSP.INVENTORY_ORGANIZATION_ID) = FSP.INVENTORY_ORGANIZATION_ID
3391 AND FSP.ORG_ID = PLA.ORG_ID
3392 UNION ALL
3393 
3394 -- SHIPMENT ACCEPTANCE/REJECTION for Releases
3395 select
3396 	pla.line_num LINE_NUM,
3397 	plla.shipment_num SHIPMENT_NUM,
3398 	msi.concatenated_segments BUYER_PT_NUM,
3399 	pla.VENDOR_PRODUCT_NUM OLD_SUP_PT_NUM,
3400 	null NEW_SUP_PT_NUM,
3401 	nvl(plla.promised_date,plla.need_by_date)	OLD_PROM_DATE,
3402 	to_date(null,'DD/MM/YYYY') NEW_PROM_DATE,
3403 	plla.quantity OLD_QTY,
3404 	to_number(null) NEW_QTY,
3405 	plla.price_override OLD_PRICE,
3406 	to_number(null) NEW_PRICE,
3407 	decode(pa.accepted_flag,'Y',fnd_message.get_string('PO','PO_WF_NOTIF_ACCEPT'),'N',fnd_message.get_string('PO','PO_WF_NOTIF_REJECT'),'NA') ACTION_TYPE,
3408 	pla.item_description ITEM_DESCRIPTION,
3409 	nvl(plla.unit_meas_lookup_code,pla.unit_meas_lookup_code) UOM,
3410 	nvl(hla.location_code, substr(rtrim(hz.address1)||'-'||rtrim(hz.city),1,60)) SHIP_TO_LOCATION,
3411 	pa.NOTE NOTE
3412 from
3413 	po_acceptances pa,
3414 	po_lines_archive_all pla,
3415 	po_line_locations_archive_all plla,
3416 	hr_locations_all hla, hz_locations hz,
3417 	mtl_system_items_kfv msi,
3418     FINANCIALS_SYSTEM_PARAMS_ALL FSP
3419 where plla.po_release_id = l_rel_document_id
3420 and plla.po_line_id = pla.po_line_id
3421 and pa.po_line_location_id = plla.line_location_id
3422 and hla.location_id(+) = plla.ship_to_location_id
3423 and plla.SHIP_TO_LOCATION_ID = hz.LOCATION_ID(+)
3424 --and plla.latest_external_flag = 'Y'
3425 and pla.latest_external_flag = 'Y'
3426 and pa.revision_num = l_rev
3427 and plla.revision_num = (select  max(revision_num)
3428                           from po_line_locations_archive_all plla2
3429                           where plla2.line_location_id =  plla.line_location_id and
3430                           plla.revision_num <= l_rev)
3431 and msi.inventory_item_id(+) = pla.item_id
3432 --and msi.organization_id(+) = pla.org_id
3433 and NVL(MSI.ORGANIZATION_ID, FSP.INVENTORY_ORGANIZATION_ID) = FSP.INVENTORY_ORGANIZATION_ID
3434 AND FSP.ORG_ID = PLA.ORG_ID
3435 )
3436 order by LINE_NUM,SHIPMENT_NUM;
3437 
3438 l_id number;
3439 l_header_response po_change_requests.request_status%type;
3440 l_header_cancel varchar2(1);
3441 
3442 cursor l_header_cancel_csr(grp_id number) is
3443 select change_request_id,
3444 	request_status
3445 from po_change_requests
3446 where change_request_group_id = grp_id
3447 and action_type = 'CANCELLATION'
3448 and request_level = 'HEADER';
3449 
3450 l_additional_changes po_change_requests.additional_changes%type;
3451 
3452 cursor l_header_changes_csr(grp_id number)
3453 is
3454 select additional_changes
3455 from po_change_requests
3456 where change_request_group_id = grp_id
3457 and request_level = 'HEADER'
3458 and additional_changes is not null;
3459  */
3460 BEGIN
3461 /*	l_header_id := 	to_number(substr(p_code,2,						instr(p_code,'#')-instr(p_code,'@')-1));
3462 	l_release_id := to_number(substr(p_code,instr(p_code,'#')+1,	instr(p_code,'$')-instr(p_code,'#')-1));
3463 	l_grp_id := 	to_number(substr(p_code,instr(p_code,'$')+1,	instr(p_code,'%')-instr(p_code,'$')-1));
3464 	l_acc_req_flag := 		  substr(p_code,instr(p_code,'%')+1,	instr(p_code,'^')-instr(p_code,'%')-1);
3465 
3466 
3467 	l_progress := '001';
3468 	l_base_url := fnd_profile.value('APPS_WEB_AGENT');
3469 	l_base_url := substr(
3470 							l_base_url,
3471 							0,
3472 							instr(rtrim(l_base_url,'/'),'/',-1,2)-1
3473 						);
3474 	l_base_url_tag := '<base href= "'||l_base_url||'">';
3475 	l_progress := '001a';
3476 	l_header_cancel := 'N';
3477 	if(l_grp_id is not null) then
3478 		open l_header_cancel_csr(l_grp_id);
3479 		fetch l_header_cancel_csr
3480 		into l_id,
3481 			l_header_response;
3482 		close l_header_cancel_csr;
3483 		if(l_id is not null) then
3484 			l_header_cancel := 'Y';
3485 		end if;
3486 	end if;
3487 
3488 	if(l_release_id is null) then
3489 
3490 		select 	segment1,
3491 				revision_num,
3492 				pos_totals_po_sv.get_po_total(po_header_id),
3493 				currency_code,
3494 				vendor_id,
3495 				vendor_site_id,
3496 				to_char(creation_date, fnd_profile.value_wnps('ICX_DATE_FORMAT_MASK')||' HH24:MI:SS'),
3497 				fob_lookup_code,
3498 				ship_via_lookup_code,
3499 				ship_to_location_id,
3500 				type_lookup_code,
3501 				GLOBAL_AGREEMENT_FLAG
3502 		into
3503 				l_po_num,
3504 				l_revision_num,
3505 				l_po_total,
3506 				l_po_currency,
3507 				l_vendor_id,
3508 				l_vendor_site_id,
3509 				l_order_date,
3510 				l_fob,
3511 				l_carrier,
3512 				l_ship_to_id,
3513 				l_type_lookup_code,
3514 				l_global_agreement_flag
3515 		from po_headers_all
3516 		where po_header_id = l_header_id;
3517 
3518 		if(l_type_lookup_code = 'STANDARD') then
3519 			l_document_type := fnd_message.get_string('PO','PO_WF_NOTIF_STD_PO');
3520 		elsif(l_type_lookup_code = 'PLANNED') then
3521 			l_document_type := fnd_message.get_string('PO','PO_WF_NOTIF_PLAN_PO');
3522 	        elsif (l_global_agreement_flag = 'Y' and l_type_lookup_code = 'BLANKET') then
3523 	          l_document_type := fnd_message.get_string('PO','PO_GA_TYPE');
3524 		elsif(l_type_lookup_code = 'BLANKET') then
3525 			l_document_type := fnd_message.get_string('PO','PO_WF_NOTIF_BLANKET');
3526 		elsif(l_type_lookup_code = 'CONTRACT') then
3527 			l_document_type := fnd_message.get_string('PO','PO_POTYPE_CNTR');
3528 		else
3529 			l_document_type := 'Error';
3530 		end if;
3531 
3532 		if(l_acc_req_flag = 'Y') then
3533 			l_po_doc_id := l_header_id;
3534 		else
3535 			l_po_doc_id := -1;
3536 		end if;
3537 
3538 	else
3539 
3540 		select
3541 				ph.segment1,
3542 				pr.release_num,
3543 				pr.revision_num,
3544 				pos_totals_po_sv.get_release_total(pr.po_release_id),
3545 				ph.currency_code,
3546 				ph.vendor_id,
3547 				ph.vendor_site_id,
3548 				to_char(pr.creation_date, fnd_profile.value_wnps('ICX_DATE_FORMAT_MASK')||' HH24:MI:SS'),
3549 				ph.fob_lookup_code,
3550 				ph.ship_via_lookup_code,
3551 				ph.ship_to_location_id,
3552 				ph.type_lookup_code
3553 		into
3554 				l_blanket_num,
3555 				l_release_num,
3556 				l_revision_num,
3557 				l_rel_total,
3558 				l_rel_currency,
3559 				l_vendor_id,
3560 				l_vendor_site_id,
3561 				l_order_date,
3562 				l_fob,
3563 				l_carrier,
3564 				l_ship_to_id,
3565 				l_type_lookup_code
3566 		from po_releases_all pr,
3567 				po_headers_all ph
3568 		where pr.po_release_id = l_release_id
3569 		and pr.po_header_id = ph.po_header_id;
3570 
3571 		if(l_type_lookup_code = 'BLANKET') then
3572 			l_document_type := fnd_message.get_string('PO','PO_WF_NOTIF_BKT_REL');
3573 		elsif(l_type_lookup_code = 'PLANNED') then
3574 			l_document_type := fnd_message.get_string('PO','PO_WF_NOTIF_SCH_REL');
3575 		else
3576 			l_document_type := 'Error';
3577 		end if;
3578 
3579 		if(l_acc_req_flag = 'Y') then
3580 			l_rel_doc_id := l_release_id;
3581 		else
3582 			l_rel_doc_id := -1;
3583 		end if;
3584 
3585 	end if;
3586 	l_progress := '002';
3587 	select
3588 		vendor_name
3589 	into
3590 		l_supplier_name
3591 	from po_vendors
3592 	where vendor_id = l_vendor_id;
3593 
3594 	select
3595 		address_line1,
3596 		address_line2,
3597 		address_line3,
3598 		city,
3599 		state,
3600 		zip
3601 	into
3602 		l_sup_address_line1,
3603 		l_sup_address_line2,
3604 		l_sup_address_line3,
3605 		l_sup_city,
3606 		l_sup_state,
3607 		l_sup_zip
3608 	from po_vendor_sites_all
3609 	where vendor_site_id = l_vendor_site_id;
3610 
3611 	select
3612 		address_line_1,
3613 		address_line_2,
3614 		address_line_3,
3615 		town_or_city,
3616 		region_1,
3617 		postal_code
3618 	into
3619 		l_ship_addr_l1,
3620 		l_ship_addr_l2,
3621 		l_ship_addr_l3,
3622 		l_ship_city,
3623 		l_ship_state,
3624 		l_ship_zip
3625 	from hr_locations_all
3626 	where location_id = l_ship_to_id;
3627 	l_progress := '003';
3628 
3629 if(l_release_id is null) then
3630 	l_document :=l_base_url_tag||
3631 		'
3632 		<font size=3 color=#336699 face=arial><b>'||l_document_type||' '||l_po_num||' '||fnd_message.get_string('PO','PO_WF_NOTIF_REV')
3633 		||' '||l_revision_num||' ('||fnd_message.get_string('PO','PO_WF_NOTIF_TOTAL')||'('||
3634 		l_po_currency||') '||
3635 		TO_CHAR(l_po_total, FND_CURRENCY.GET_FORMAT_MASK(l_po_currency, 30))
3636 		||')</B></font><HR size=1 color=#cccc99>';  */
3637 		/*
3638 	if(l_acc_req_flag <> 'Y') then
3639 		l_document := l_document ||
3640 			'- '||fnd_message.get_string('PO','PO_WF_NOTIF_NUM_OF_CHN')||' - '||
3641 			l_num_of_changes||' '||fnd_message.get_string('PO','PO_WF_NOTIF_CANCELLED')||' - '||l_num_of_cancels||'</B></font><HR size=1 color=#cccc99>';
3642 	end if;	*/
3643 /*
3644 else
3645 	l_document :=l_base_url_tag||
3646 		'
3647 		<font size=3 color=#336699 face=arial><b>'||l_document_type||' '||l_blanket_num||'-'||l_release_num||' '||fnd_message.get_string('PO','PO_WF_NOTIF_REV')||' '
3648 		||l_revision_num||' ('||fnd_message.get_string('PO','PO_WF_NOTIF_TOTAL')||'('||
3649 		l_rel_currency||') '||
3650 		TO_CHAR(l_rel_total, FND_CURRENCY.GET_FORMAT_MASK(l_rel_currency, 30))
3651 		||')</B></font><HR size=1 color=#cccc99>'; */
3652 		/*
3653 	if(l_acc_req_flag <> 'Y') then
3654 		l_document := l_document ||
3655 			'- '||fnd_message.get_string('PO','PO_WF_NOTIF_NUM_OF_CHN')||' - '||
3656 			l_num_of_changes||' '||fnd_message.get_string('PO','PO_WF_NOTIF_CANCELLED')||' - '||l_num_of_cancels||'</B></font><HR size=1 color=#cccc99>';
3657 	end if;*/
3658 /*
3659 end if;
3660 
3661 l_document := l_document||'
3662 <TABLE  cellpadding=2 cellspacing=1>
3663 <TR>
3664 <TD>
3665 <TABLE cellpadding=2 cellspacing=1>
3666 <TR>
3667 <TD nowrap><font color=black><B>
3668                       '||fnd_message.get_string('PO','PO_WF_NOTIF_VENDOR')||'</B></font></TD>
3669 <TD nowrap><font color=black>'||l_supplier_name||'</font></TD>
3670 </TR>
3671 <TR>
3672 <TD nowrap valign=TOP><font color=black><B>
3673                       '||fnd_message.get_string('PO','PO_WF_NOTIF_ADDRESS')||'</B></font></TD>
3674 <TD nowrap><font color=black>';
3675 
3676 if(l_sup_address_line1 is not null) then
3677 	l_document := l_document || l_sup_address_line1||'<BR>';
3678 end if;
3679 if(l_sup_address_line2 is not null) then
3680 	l_document := l_document || l_sup_address_line2||'<BR>';
3681 end if;
3682 if(l_sup_address_line3 is not null) then
3683 	l_document := l_document || l_sup_address_line3||'<BR>';
3684 end if;
3685 
3686 l_document := l_document||l_sup_city||', '||l_sup_state||' '||l_sup_zip||
3687                       '</font></TD>
3688 </TR>
3689 <TR>
3690 	<TD nowrap><font color=black><B>
3691 	'||fnd_message.get_string('PO','PO_WF_NOTIF_FOB')||'</B></font></TD>
3692 	<TD nowrap><font color=black>'||l_fob||
3693     '</font></TD>
3694 </TR>
3695 
3696 
3697 <TR>
3698 <TD nowrap><font color=black><B>
3699                       '||fnd_message.get_string('PO','PO_WF_NOTIF_CARRIER')||'</B></font></TD>
3700 <TD nowrap><font color=black>'||l_carrier||
3701                       '</font></TD>
3702 </TR>
3703 </TABLE>
3704 </TD>
3705 <TD valign=TOP>
3706 <TABLE cellpadding=2 cellspacing=1>
3707 <TR>
3708 	<TD nowrap><font color=black><B>
3709 	'||fnd_message.get_string('PO','PO_WF_NOTIF_ORDER_DATE')||'</B></font></TD>
3710 	<TD nowrap><font color=black>'||l_order_date||
3711     '</font></TD>
3712 </TR>
3713 
3714 <TR>
3715 <TD nowrap valign=TOP><font color=black><B>
3716                       '||fnd_message.get_string('PO','PO_WF_NOTIF_SHPTO_ADD')||'</B></font></TD>
3717 <TD nowrap><font color=black>
3718 						'||
3719 						l_ship_addr_l1||' '||l_ship_addr_l2||' '||l_ship_addr_l3||'<BR>'||
3720 						l_ship_city||', '||l_ship_state||' '||l_ship_zip
3721 						||'
3722 						</font></TD>
3723 </TR>
3724 
3725 </TABLE>
3726 </TD>
3727 </TABLE></P>';
3728 WF_NOTIFICATION.WriteToClob(document,l_document);
3729 
3730 if(l_header_cancel = 'N') then
3731 	open l_po_ack_csr(l_po_doc_id, l_rel_doc_id, l_revision_num);
3732 	fetch l_po_ack_csr
3733 	into
3734 		lc_line_num,
3735 		lc_ship_num,
3736 		lc_buyer_pt_num,
3737 		lc_old_sup_pt_num,
3738 		lc_new_sup_pt_num,
3739 		lc_old_prom_date,
3740 		lc_new_prom_date,
3741 		lc_old_qty,
3742 		lc_new_qty,
3743 		lc_old_price,
3744 		lc_new_price,
3745 		lc_action_type,
3746 		lc_item_desc,
3747 		lc_uom,
3748 		lc_ship_to_location,
3749 		lc_reason;
3750 	close l_po_ack_csr;
3751 	if(lc_line_num is not null) then
3752 		l_document := '<font size=3 color=#336699 face=arial><b>'||fnd_message.get_string('PO','PO_WF_NOTIF_ACK_RESPONSE')||'</B></font><HR size=1 color=#cccc99>
3753 		<TABLE WIDTH=100% cellpadding=2 cellspacing=1>
3754 		<TR bgcolor=#cccc99>
3755 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_LINE_NUMBER')||'</font></TH>
3756 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_SHIPMENT')||'</font></TH>
3757 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_ITEM')||'</font></TH>
3758 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_SUP_ITEM')||'</font></TH>
3759 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_DOC_DESCRIPTION')||'</font></TH>
3760 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_UNIT')||'</font></TH>
3761 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_PRICE')||'</font></TH>
3762 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_QTY_ORDERED')||'</font></TH>
3763 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_PROM_DATE')||'</font></TH>
3764 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_SHIPTO')||'</font></TH>
3765 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_ACTION')||'</font></TH>
3766 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_REASON')||'</font></TH>
3767 		</TR>
3768 		</B>';
3769 
3770 		WF_NOTIFICATION.WriteToClob(document,l_document);
3771 		open l_po_ack_csr(l_po_doc_id, l_rel_doc_id, l_revision_num);
3772 		loop
3773 		fetch l_po_ack_csr
3774 		into
3775 			lc_line_num,
3776 			lc_ship_num,
3777 			lc_buyer_pt_num,
3778 			lc_old_sup_pt_num,
3779 			lc_new_sup_pt_num,
3780 			lc_old_prom_date,
3781 			lc_new_prom_date,
3782 			lc_old_qty,
3783 			lc_new_qty,
3784 			lc_old_price,
3785 			lc_new_price,
3786 			lc_action_type,
3787 			lc_item_desc,
3788 			lc_uom,
3789 			lc_ship_to_location,
3790 			lc_reason;
3791 		EXIT when l_po_ack_csr%NOTFOUND;
3792 
3793 	        l_old_prom_date := to_char(lc_old_prom_date,fnd_profile.value_wnps('ICX_DATE_FORMAT_MASK')||' HH24:MI:SS');
3794                 l_new_prom_date := to_char(lc_new_prom_date,fnd_profile.value_wnps('ICX_DATE_FORMAT_MASK')||' HH24:MI:SS');
3795 		lc_line_num := nvl(lc_line_num,'` ');
3796 		lc_ship_num:= nvl(lc_ship_num,'` ');
3797 		lc_buyer_pt_num := nvl(lc_buyer_pt_num,'` ');
3798 		lc_old_sup_pt_num:= nvl(lc_old_sup_pt_num,'` ');
3799 		lc_new_sup_pt_num:= nvl(lc_new_sup_pt_num,'` ');
3800 		l_old_prom_date:= nvl(l_old_prom_date,'` ');
3801 		l_new_prom_date:= nvl(l_new_prom_date,'` ');
3802 		lc_old_qty:= nvl(lc_old_qty,'` ');
3803 		lc_new_qty:= nvl(lc_new_qty,'` ');
3804 		lc_old_price:= nvl(lc_old_price,'` ');
3805 		lc_new_price:= nvl(lc_new_price,'` ');
3806 		lc_action_type:= nvl(lc_action_type,'` ');
3807 		lc_item_desc:= nvl(lc_item_desc,'` ');
3808 		lc_uom:= nvl(lc_uom,'` ');
3809 		lc_ship_to_location:= nvl(lc_ship_to_location,'` ');
3810 		lc_reason := nvl(lc_reason,'` ');
3811 
3812 
3813 		l_document := '
3814 		        <TR bgcolor=#f7f7e7>
3815 
3816 		        <TD align=left><font color=black>'||lc_line_num||
3817 		                      '</font></TD>
3818 		        <TD align=left><font color=black>'||lc_ship_num||
3819 	    	                      '</font></TD>
3820 			<TD align=left><font color=black>'||lc_buyer_pt_num||
3821 	                              '</font></TD>
3822 		        <TD align=left><font color=black>'||lc_old_sup_pt_num||
3823 		                      '</font></TD>
3824 		        <TD align=left><font color=black>'||lc_item_desc||
3825 		                      '</font></TD>
3826 		        <TD align=left><font color=black>'||lc_uom||
3827 		                      '</font></TD>
3828 		        <TD align=left><font color=black>'||lc_old_price||'
3829 		                      </font></TD>
3830 		        <TD align=left><font color=black>'||lc_old_qty||'
3831 		                      </font></TD>
3832 		        <TD align=left><font color=black>'||l_old_prom_date||'
3833 		                      </font></TD>
3834 		        <TD align=left><font color=black>'||lc_ship_to_location||
3835 		                      '</font></TD>
3836 		        <TD align=left><font color=black>'||lc_action_type||
3837 		                      '</font></TD>
3838 		        <TD align=left><font color=black>'||lc_reason||
3839 		                      '</font></TD>
3840 
3841 		        </TR>
3842 			';
3843 			WF_NOTIFICATION.WriteToClob(document,l_document);
3844 
3845 		END LOOP;
3846 		CLOSE l_po_ack_csr;
3847 		WF_NOTIFICATION.WriteToClob(document, '
3848 		</TABLE></P>
3849 		');
3850 	end if;
3851 
3852 
3853 	lc_line_num := null;
3854 	open l_po_chg_req_csr(l_grp_id);
3855 	fetch l_po_chg_req_csr
3856 	into
3857 		lc_line_num,
3858 		lc_ship_num,
3859 		lc_buyer_pt_num,
3860 		lc_old_sup_pt_num,
3861 		lc_new_sup_pt_num,
3862 		lc_old_prom_date,
3863 		lc_new_prom_date,
3864 		lc_old_qty,
3865 		lc_new_qty,
3866 		lc_old_price,
3867 		lc_new_price,
3868 		lc_action_type,
3869 		lc_item_desc,
3870 		lc_uom,
3871 		lc_ship_to_location,
3872 		lc_action_code,
3873 		lc_reason,
3874 		lc_split;
3875 	close l_po_chg_req_csr;
3876 	if(lc_line_num is not null) then
3877 		l_document := '<font size=3 color=#336699 face=arial><b>'||
3878 					fnd_message.get_string('PO', 'PO_WF_NOTIF_CH_CA_REQ')||
3879 					'</B></font><HR size=1 color=#cccc99>';
3880 		l_document := l_document||'<TABLE><TR>'|| NL;
3881 	    l_document:= l_document||'<TD class=instructiontext>'||
3882 	      			'<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">'
3883 	                 || fnd_message.get_string('PO', 'PO_WF_NOTIF_NEW_VALUE');
3884 	    l_document := l_document || '</TD></TR>' || NL;
3885 	    l_document := l_document || '<TR>'|| NL;
3886 	    l_document:= l_document||'<TD class=instructiontext>'||
3887 	      			'<img src=/OA_MEDIA/cancelind_status.gif ALT="">'
3888 	                 || fnd_message.get_string('PO', 'PO_WF_NOTIF_CANCEL_PENDING');
3889 	    l_document := l_document || '</TD></TR>' || NL;
3890 	    l_document := l_document || '</TABLE>' || NL;
3891 		l_document := l_document ||'
3892 		<TABLE WIDTH=100% cellpadding=2 cellspacing=1>
3893 		<TR bgcolor=#cccc99>
3894 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_LINE_NUMBER')||'</font></TH>
3895 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_SHIPMENT')||'</font></TH>
3896 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_ITEM')||'</font></TH>
3897 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_SUP_ITEM')||'</font></TH>
3898 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_DOC_DESCRIPTION')||'</font></TH>
3899 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_UNIT')||'</font></TH>
3900 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_PRICE')||'</font></TH>
3901 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_QTY_ORDERED')||'</font></TH>
3902 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_PROM_DATE')||'</font></TH>
3903 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_SHIPTO')||'</font></TH>
3904 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_ACTION')||'</font></TH>
3905 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_REASON')||'</font></TH>
3906 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_SPLIT')||'</font></TH>
3907 		</TR>
3908 		</B>';
3909 
3910 		WF_NOTIFICATION.WriteToClob(document,l_document);
3911 		--, l_po_doc_id, l_rel_doc_id
3912 		open l_po_chg_req_csr(l_grp_id);
3913 		loop
3914 		fetch l_po_chg_req_csr
3915 		into
3916 			lc_line_num,
3917 			lc_ship_num,
3918 			lc_buyer_pt_num,
3919 			lc_old_sup_pt_num,
3920 			lc_new_sup_pt_num,
3921 			lc_old_prom_date,
3922 			lc_new_prom_date,
3923 			lc_old_qty,
3924 			lc_new_qty,
3925 			lc_old_price,
3926 			lc_new_price,
3927 			lc_action_type,
3928 			lc_item_desc,
3929 			lc_uom,
3930 			lc_ship_to_location,
3931 			lc_action_code,
3932 			lc_reason,
3933 			lc_split;
3934 		EXIT when l_po_chg_req_csr%NOTFOUND;
3935 
3936 		if(lc_split is not null) then
3937 			lc_old_sup_pt_num := nvl(lc_old_sup_pt_num,lc_new_sup_pt_num);
3938 			lc_new_sup_pt_num := null;
3939 
3940 			lc_old_price:=nvl(lc_old_price,lc_new_price);
3941 			lc_new_price:= null;
3942 
3943 			lc_new_prom_date:= nvl(lc_new_prom_date,lc_old_prom_date);
3944 			lc_old_prom_date:= null;
3945 
3946 			lc_new_qty:=nvl(lc_new_qty,lc_old_qty);
3947 			lc_old_qty:= null;
3948 		end if;
3949 
3950 		if(fnd_timezones.timezones_enabled()='Y') then
3951                             fnd_date_tz.init_timezones_for_fnd_date(true);
3952                             l_old_prom_date := fnd_date.date_to_displayDT(lc_old_prom_date);
3953                             l_new_prom_date := fnd_date.date_to_displayDT(lc_new_prom_date);
3954                         else
3955 			    l_old_prom_date := to_char(lc_old_prom_date,fnd_profile.value_wnps('ICX_DATE_FORMAT_MASK')||' HH24:MI:SS');
3956             		    l_new_prom_date := to_char(lc_new_prom_date,fnd_profile.value_wnps('ICX_DATE_FORMAT_MASK')||' HH24:MI:SS');
3957                         end if;
3958 
3959 
3960 		lc_line_num := nvl(lc_line_num,'` ');
3961 		lc_ship_num:= nvl(lc_ship_num,'` ');
3962 		lc_buyer_pt_num:= nvl(lc_buyer_pt_num,'` ');
3963 		lc_old_sup_pt_num:= nvl(lc_old_sup_pt_num,'` ');
3964 		lc_new_sup_pt_num:= nvl(lc_new_sup_pt_num,'` ');
3965 		l_old_prom_date:= nvl(l_old_prom_date,'` ');
3966 		l_new_prom_date:= nvl(l_new_prom_date,'` ');
3967 		lc_old_qty:= nvl(lc_old_qty,'` ');
3968 		lc_new_qty:= nvl(lc_new_qty,'` ');
3969 		lc_old_price:= nvl(lc_old_price,'` ');
3970 		lc_new_price:= nvl(lc_new_price,'` ');
3971 		lc_action_type:= nvl(lc_action_type,'` ');
3972 		lc_item_desc:= nvl(lc_item_desc,'` ');
3973 		lc_uom:= nvl(lc_uom,'` ');
3974 		lc_ship_to_location:= nvl(lc_ship_to_location,'` ');
3975 		lc_reason := nvl(lc_reason,'` ');
3976 		lc_split := nvl(lc_split,'` ');
3977 
3978 
3979 		l_document := '
3980 		        <TR bgcolor=#f7f7e7>
3981 
3982 		        <TD align=left><font color=black>'||lc_line_num||
3983 		                      '</font></TD> ';
3984 
3985 		if(lc_action_code = 'CANCELLATION') then
3986 			l_document := l_document ||'
3987 			        <TD align=left><font color=black>
3988 			        	<TABLE>
3989 			        		<TR>
3990 			        			<TD>
3991 				        			<img src=/OA_MEDIA/cancelind_status.gif ALT="">
3992 			        			</TD>
3993 			        			<TD alight=right>'
3994 				        			||lc_ship_num||
3995 			        			'</TD>
3996 			        		</TR>
3997 			        	</TABLE>
3998 			        </font></TD> ';
3999 		else
4000 			l_document := l_document ||'
4001 			        <TD align=left><font color=black>'||lc_ship_num||
4002 		    	                  '</font></TD> ';
4003 		end if;
4004 		l_document := l_document ||'
4005 			<TD align=left><font color=black>'||lc_buyer_pt_num||
4006 	                                  '</font></TD> ';
4007 
4008 		    if(lc_new_sup_pt_num = '` ') then
4009 				l_document := l_document || '
4010 		        <TD align=left><font color=black>'||lc_old_sup_pt_num||'
4011 		                      </font></TD>';
4012 			else
4013 				l_document := l_document || '
4014 		        <TD align=left><font color=black>
4015 		        	<table>
4016 		        		<tr>
4017 		        			<td>'||lc_old_sup_pt_num||'
4018 		        			</td>
4019 		        		</tr>
4020 		        		<tr>
4021 		        			<td>'||lc_new_sup_pt_num||'
4022 		        			</td>
4023 		        			<td>
4024 		        			<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">
4025 		        			</td>
4026 		        		</tr>
4027 		        	</table>
4028 		                      </font></TD>';
4029 			end if;
4030 
4031 			l_document := l_document || '
4032 		        <TD align=left><font color=black>'||lc_item_desc||
4033 		                      '</font></TD>
4034 
4035 		        <TD align=left><font color=black>'||lc_uom||
4036 		                      '</font></TD> ';
4037 
4038 		    if(lc_new_price = '` ') then
4039 				l_document := l_document || '
4040 		        <TD align=left><font color=black>'||lc_old_price||'
4041 		                      </font></TD>';
4042 			else
4043 				l_document := l_document || '
4044 		        <TD align=left><font color=black>
4045 		        	<table>
4046 		        		<tr>
4047 		        			<td>'||lc_old_price||'
4048 		        			</td>
4049 		        		</tr>
4050 		        		<tr>
4051 		        			<td>'||lc_new_price||'
4052 		        			</td>
4053 		        			<td>
4054 		        			<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">
4055 		        			</td>
4056 		        		</tr>
4057 		        	</table>
4058 		                      </font></TD>';
4059 			end if;
4060 
4061 		    if(lc_new_qty = '` ') then
4062 				l_document := l_document || '
4063 		        <TD align=left><font color=black>'||lc_old_qty||'
4064 		                      </font></TD>';
4065 		    elsif(lc_split <> '` ') then
4066 				l_document := l_document || '
4067 		        <TD align=left><font color=black>
4068 		        	<table>
4069 		        		<tr>
4070 		        			<td>'||lc_new_qty||'
4071 		        			</td>
4072 		        			<td>
4073 		        			<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">
4074 		        			</td>
4075 		        		</tr>
4076 		        	</table>
4077 		                      </font></TD>';
4078 			else
4079 				l_document := l_document || '
4080 		        <TD align=left><font color=black>
4081 		        	<table>
4082 		        		<tr>
4083 		        			<td>'||lc_old_qty||'
4084 		        			</td>
4085 		        		</tr>
4086 		        		<tr>
4087 		        			<td>'||lc_new_qty||'
4088 		        			</td>
4089 		        			<td>
4090 		        			<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">
4091 		        			</td>
4092 		        		</tr>
4093 		        	</table>
4094 		                      </font></TD>';
4095 			end if;
4096 
4097 		    if(l_new_prom_date = '` ') then
4098 				l_document := l_document || '
4099 		        <TD align=left><font color=black>'||l_old_prom_date||'
4100 		                      </font></TD>';
4101 		    elsif(lc_split <> '` ' OR l_old_prom_date = '` ') then
4102 				l_document := l_document || '
4103 		        <TD align=left><font color=black>
4104 		        	<table>
4105 		        		<tr>
4106 		        			<td>'||l_new_prom_date||'
4107 		        			</td>
4108 		        			<td>
4109 		        			<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">
4110 		        			</td>
4111 		        		</tr>
4112 		        	</table>
4113 		                      </font></TD>';
4114 			else
4115 				l_document := l_document || '
4116 		        <TD align=left><font color=black>
4117 		        	<table>
4118 		        		<tr>
4119 		        			<td>'||l_old_prom_date||'
4120 		        			</td>
4121 		        		</tr>
4122 		        		<tr>
4123 		        			<td>'||l_new_prom_date||'
4124 		        			</td>
4125 		        			<td>
4126 		        			<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">
4127 		        			</td>
4128 		        		</tr>
4129 		        	</table>
4130 		                      </font></TD>';
4131 			end if;
4132 			l_document := l_document || '
4133 
4134 		        <TD align=left><font color=black>'||lc_ship_to_location||
4135 		                      '</font></TD>
4136 
4137 		        <TD align=left><font color=black>'||lc_action_type||
4138 		                      '</font></TD>
4139 
4140 		        <TD align=left><font color=black>'||lc_reason||
4141 		                      '</font></TD>
4142 
4143 		        <TD align=left><font color=black>'||lc_split||
4144 		                      '</font></TD>
4145 
4146 		        </TR>
4147 			';
4148 			WF_NOTIFICATION.WriteToClob(document,l_document);
4149 
4150 		END LOOP;
4151 		CLOSE l_po_chg_req_csr;
4152 		WF_NOTIFICATION.WriteToClob(document, '
4153 		</TABLE></P>
4154 		');
4155 	end if;
4156 	open l_header_changes_csr(l_grp_id);
4157 	fetch l_header_changes_csr into l_additional_changes;
4158 	close l_header_changes_csr;
4159 
4160 	if(l_additional_changes is not null) then
4161 		l_document := '<BR><B>'||fnd_message.get_string('PO','PO_WF_NOTIF_ADD_CHN_REQ')||'</B>'||
4162 			      ' `  '||l_additional_changes||'<BR>';
4163 		WF_NOTIFICATION.WriteToClob(document,l_document);
4164 	end if;
4165 else
4166 	fnd_message.set_name('PO','PO_WF_NOTIF_CAN_ENTIER');
4167 	fnd_message.set_token('DOC',l_document_type);
4168 	l_document := fnd_message.get;
4169 	WF_NOTIFICATION.WriteToClob(document,l_document);
4170 end if;
4171 exception when others then
4172 	IF g_fnd_debug = 'Y' THEN
4173 		FND_LOG.string(FND_LOG.level_unexpected, g_module_prefix ||
4174 				l_api_name || '.others_exception', l_progress||':'||sqlerrm);
4175 	END IF;
4176 	WF_NOTIFICATION.WriteToClob(document,'Exception occured:'||sqlerrm);  */
4177 null;
4178 
4179 END GEN_NTF_FOR_BUYER_SUP_CHG;
4180 
4181 
4182 /*
4183 *Generate Notification for Supplier, informing him/her of Buyer's response to supplier's change request.
4184 */
4185 PROCEDURE GEN_NTF_FOR_SUP_BUY_RP(	p_chg_req_grp_id IN number,
4186 								    display_type   in      Varchar2,
4187 								    document in out NOCOPY clob,
4188 								    document_type  in out NOCOPY  varchar2)
4189 IS
4190 /*                                                                        -- RDP changes replaced by Jrad Notification
4191 l_api_name varchar2(50):='GEN_NTF_FOR_SUP_BUY_RP';
4192 l_progress varchar2(5) := '000';
4193 l_po_release_id number;
4194 l_rel_total number;
4195 l_rel_currency po_headers.currency_code%TYPE;
4196 --l_blanket_num number;
4197 l_blanket_num po_headers_all.segment1%TYPE;
4198 --l_release_num number;
4199 l_release_num po_releases_all.release_num%TYPE;
4200 l_document varchar2(32000);
4201 l_document_header_id number;
4202 l_document_type varchar2(2000);
4203 l_po_num po_headers_all.segment1%TYPE;
4204 l_revision_num number;
4205 l_po_total number;
4206 l_po_currency po_headers.currency_code%TYPE;
4207 l_vendor_id number;
4208 l_vendor_site_id number;
4209 l_supplier_name po_vendors.vendor_name%TYPE;
4210 l_sup_address_line1 po_vendor_sites_all.address_line1%TYPE;
4211 l_sup_address_line2 po_vendor_sites_all.address_line2%TYPE;
4212 l_sup_address_line3 po_vendor_sites_all.address_line3%TYPE;
4213 l_sup_city	po_vendor_sites_all.city%TYPE;
4214 l_sup_state po_vendor_sites_all.state%TYPE;
4215 l_sup_zip po_vendor_sites_all.zip%TYPE;
4216 l_order_date varchar2(2000);
4217 l_fob po_headers_all.fob_lookup_code%TYPE;
4218 l_carrier po_headers_all.ship_via_lookup_code%TYPE;
4219 l_ship_to_id number;
4220 l_ship_addr_l1 hr_locations_all.address_line_1%TYPE;
4221 l_ship_addr_l2 hr_locations_all.address_line_2%TYPE;
4222 l_ship_addr_l3 hr_locations_all.address_line_3%TYPE;
4223 l_ship_city hr_locations_all.town_or_city%TYPE;
4224 l_ship_state hr_locations_all.region_1%TYPE;
4225 l_ship_zip hr_locations_all.postal_code%TYPE;
4226 
4227 l_base_url varchar2(2000);
4228 l_base_url_tag varchar2(2000);
4229 
4230 l_document1 VARCHAR2(32000) := '';
4231 l_type_lookup_code po_headers_all.type_lookup_code%TYPE;
4232 l_global_agreement_flag  po_headers_all.GLOBAL_AGREEMENT_FLAG%TYPE := 'F';
4233 
4234 l_old_prom_date	varchar2(2000);
4235 l_new_prom_date	varchar2(2000);
4236 
4237 l_doc_hdr_info varchar2(2000);
4238 lc_action_code po_change_requests.action_type%type;
4239 lc_line_num 		varchar2(2000);
4240 lc_ship_num 		varchar2(2000);
4241 lc_old_sup_pt_num 	po_lines_all.vendor_product_num%TYPE;
4242 lc_new_sup_pt_num 	po_change_requests.new_supplier_part_number%TYPE;
4243 lc_old_prom_date        date;
4244 lc_new_prom_date        date;
4245 lc_old_qty 			varchar2(2000);
4246 lc_new_qty 			varchar2(2000);
4247 lc_old_price 		varchar2(2000);
4248 lc_new_price 		varchar2(2000);
4249 lc_response 		varchar2(2000);
4250 lc_item_desc 		po_lines_all.item_description%TYPE;
4251 lc_uom 				po_lines_all.unit_meas_lookup_code%TYPE;
4252 lc_ship_to_location hr_locations_all.location_code%TYPE;
4253 lc_reason			po_change_requests.response_reason%TYPE;
4254 lc_split varchar2(2000);
4255 
4256 
4257 cursor l_po_chg_req_csr(grp_id number) is
4258 select
4259 	to_char(LINE_NUM),
4260 	to_char(SHIPMENT_NUM),
4261 	OLD_SUP_PT_NUM,
4262 	NEW_SUP_PT_NUM,
4263         OLD_PROM_DATE,
4264         NEW_PROM_DATE,
4265 	to_char(OLD_QTY),
4266 	to_char(NEW_QTY),
4267 	to_char(OLD_PRICE),
4268 	to_char(NEW_PRICE),
4269 	RESPONSE,
4270 	ITEM_DESCRIPTION,
4271 	UOM,
4272 	SHIP_TO_LOCATION,
4273 	REASON,
4274 	SPLIT,
4275 	ACTION_CODE
4276 from(
4277 -- Respond to Changes on Line for Standard PO
4278 select
4279 	pcr.document_line_number LINE_NUM,
4280 	pcr.document_shipment_number SHIPMENT_NUM,
4281 	pcr.old_supplier_part_number OLD_SUP_PT_NUM,
4282 	pcr.new_supplier_part_number NEW_SUP_PT_NUM,
4283 	to_date(null,'DD/MM/YYYY') OLD_PROM_DATE,
4284 	to_date(null,'DD/MM/YYYY') NEW_PROM_DATE,
4285 	nvl(pcr.old_quantity, pla.quantity) OLD_QTY,
4286 	to_number(null) NEW_QTY,
4287 	nvl(pcr.old_price,pla.unit_price) OLD_PRICE,
4288 	pcr.new_price		NEW_PRICE,
4289 	decode(pcr.REQUEST_STATUS,'ACCEPTED',fnd_message.get_string('PO','PO_WF_NOTIF_ACCEPTED'),
4290 		'REJECTED',fnd_message.get_string('PO','PO_WF_NOTIF_REJ'),'Error') RESPONSE,
4291 	pla.item_description ITEM_DESCRIPTION,
4292 	pla.unit_meas_lookup_code	UOM,
4293 	null SHIP_TO_LOCATION,
4294 	pcr.response_reason REASON,
4295 	null SPLIT,
4296 	pcr.action_type ACTION_CODE
4297 from po_change_requests pcr, po_lines_archive_all pla
4298 where pla.po_line_id = pcr.document_line_id
4299 and pcr.change_request_group_id =grp_id
4300 --and pcr.document_revision_num = pla.revision_num
4301 and pcr.request_level = 'LINE'
4302 and pla.latest_external_flag = 'Y'
4303 UNION ALL
4304 --Respond to changes on Shipment for Releases AND Standard PO
4305 select
4306 	pla.line_num LINE_NUM,
4307 	pcr.document_shipment_number SHIPMENT_NUM,
4308 	pla.vendor_product_num OLD_SUP_PT_NUM,
4309 	null NEW_SUP_PT_NUM,
4310 	pcr.old_promised_date	OLD_PROM_DATE,
4311 	pcr.new_promised_date 	NEW_PROM_DATE,
4312 	nvl(pcr.old_quantity,plla.quantity)	OLD_QTY,
4313 	pcr.new_quantity	NEW_QTY,
4314 	nvl(pcr.old_price,plla.price_override) OLD_PRICE,
4315 	pcr.new_price NEW_PRICE,
4316 	decode(pcr.REQUEST_STATUS,'ACCEPTED',fnd_message.get_string('PO','PO_WF_NOTIF_ACCEPTED'),
4317 		'REJECTED',fnd_message.get_string('PO','PO_WF_NOTIF_REJ'),'Error') RESPONSE,
4318 	pla.item_description ITEM_DESCRIPTION,
4319 	nvl(plla.unit_meas_lookup_code,pla.unit_meas_lookup_code) UOM,
4320         nvl(hla.location_code, substr(rtrim(hz.address1)||'-'||rtrim(hz.city),1,60)) SHIP_TO_LOCATION,
4321 	pcr.response_reason REASON,
4322 	decode(pcr.parent_line_location_id, null, null, fnd_message.get_string('PO','PO_WF_NOTIF_YES')) SPLIT,
4323 	pcr.action_type ACTION_CODE
4324 
4325 from po_change_requests pcr, po_lines_archive_all pla, po_line_locations_archive_all plla,
4326 hr_locations_all hla, hz_locations hz
4327 where change_request_group_id =grp_id
4328 and pla.po_line_id = pcr.document_line_id
4329 and pla.latest_external_flag = 'Y'
4330 and plla.line_location_id = pcr.document_line_location_id
4331 --and pcr.document_revision_num = pla.revision_num
4332 --and pcr.document_revision_num = plla.revision_num
4333 and plla.latest_external_flag = 'Y'
4334 and request_level = 'SHIPMENT'
4335 and pcr.new_supplier_order_line_number is null
4336 and hla.location_id(+) = plla.ship_to_location_id
4337 and plla.SHIP_TO_LOCATION_ID = hz.LOCATION_ID(+)
4338 and pcr.parent_line_location_id is null
4339 UNION ALL
4340 --Respond to changes on Split Shipment
4341 select
4342 	pla.line_num LINE_NUM,
4343 	pcr.document_shipment_number SHIPMENT_NUM,
4344 	pla.vendor_product_num OLD_SUP_PT_NUM,
4345 	null NEW_SUP_PT_NUM,
4346 	pcr.old_promised_date	OLD_PROM_DATE,
4347 	pcr.new_promised_date 	NEW_PROM_DATE,
4348 	pcr.old_quantity	OLD_QTY,
4349 	pcr.new_quantity	NEW_QTY,
4350         nvl(pcr.old_price,plla.price_override) OLD_PRICE,
4351 	pcr.new_price NEW_PRICE,
4352 	decode(pcr.REQUEST_STATUS,'ACCEPTED',fnd_message.get_string('PO','PO_WF_NOTIF_ACCEPTED'),
4353 		'REJECTED',fnd_message.get_string('PO','PO_WF_NOTIF_REJ'),'Error') RESPONSE,
4354 	pla.item_description ITEM_DESCRIPTION,
4355 	nvl(plla.unit_meas_lookup_code,pla.unit_meas_lookup_code) UOM,
4356 	nvl(hla.location_code, substr(rtrim(hz.address1)||'-'||rtrim(hz.city),1,60)) SHIP_TO_LOCATION,
4357 	pcr.response_reason REASON,
4358 	decode(pcr.parent_line_location_id, null, null, fnd_message.get_string('PO','PO_WF_NOTIF_YES')) SPLIT,
4359 	pcr.action_type ACTION_CODE
4360 from po_change_requests pcr, po_lines_archive_all pla, po_line_locations_archive_all plla,
4361 hr_locations_all hla, hz_locations hz
4362 where change_request_group_id =grp_id
4363 and pla.po_line_id = pcr.document_line_id
4364 and pla.latest_external_flag = 'Y'
4365 and plla.line_location_id = pcr.parent_line_location_id
4366 --and pcr.document_revision_num = pla.revision_num
4367 --and pcr.document_revision_num = plla.revision_num
4368 and plla.latest_external_flag = 'Y'
4369 and request_level = 'SHIPMENT'
4370 and hla.location_id(+) = plla.ship_to_location_id
4371 and plla.SHIP_TO_LOCATION_ID = hz.LOCATION_ID(+)
4372 
4373 )
4374 order by LINE_NUM,nvl(SHIPMENT_NUM,0);
4375 
4376 l_id number;
4377 l_header_response po_change_requests.request_status%type;
4378 l_header_cancel varchar2(1);
4379 
4380 cursor l_header_cancel_csr(grp_id number) is
4381 select change_request_id,
4382 	request_status
4383 from po_change_requests
4384 where change_request_group_id = grp_id
4385 and action_type = 'CANCELLATION'
4386 and request_level = 'HEADER';
4387 
4388 l_additional_changes po_change_requests.additional_changes%type;
4389 
4390 cursor l_header_changes_csr(grp_id number)
4391 is
4392 select additional_changes, request_status
4393 from po_change_requests
4394 where change_request_group_id = grp_id
4395 and request_level = 'HEADER'
4396 and additional_changes is not null;  */
4397 
4398 BEGIN
4399   /*	select distinct
4400 		document_header_id,
4401 		document_type,
4402 		po_release_id
4403 	into
4404 		l_document_header_id,
4405 		l_document_type,
4406 		l_po_release_id
4407 	from po_change_requests
4408 	where change_request_group_id = p_chg_req_grp_id;
4409 
4410 	l_progress := '001';
4411 	l_base_url := POS_URL_PKG.get_external_url;
4412 
4413 	l_base_url_tag := '<base href= "'||l_base_url||'">';
4414 	l_progress := '001a';
4415 	l_header_cancel := 'N';
4416 	if(p_chg_req_grp_id is not null) then
4417 		open l_header_cancel_csr(p_chg_req_grp_id);
4418 		fetch l_header_cancel_csr
4419 		into l_id,
4420 			l_header_response;
4421 		close l_header_cancel_csr;
4422 		if(l_id is not null) then
4423 			l_header_cancel := 'Y';
4424 		end if;
4425 	end if;
4426 
4427 	if(l_po_release_id is null) then
4428 		select 	segment1,
4429 				revision_num,
4430 				pos_totals_po_sv.get_po_total(po_header_id),
4431 				currency_code,
4432 				vendor_id,
4433 				vendor_site_id,
4434 				to_char(creation_date, fnd_profile.value_wnps('ICX_DATE_FORMAT_MASK')||' HH24:MI:SS'),
4435 				fob_lookup_code,
4436 				ship_via_lookup_code,
4437 				ship_to_location_id,
4438 				type_lookup_code,
4439 				GLOBAL_AGREEMENT_FLAG
4440 		into
4441 				l_po_num,
4442 				l_revision_num,
4443 				l_po_total,
4444 				l_po_currency,
4445 				l_vendor_id,
4446 				l_vendor_site_id,
4447 				l_order_date,
4448 				l_fob,
4449 				l_carrier,
4450 				l_ship_to_id,
4451 				l_type_lookup_code,
4452 				l_global_agreement_flag
4453 		from po_headers_all
4454 		where po_header_id = l_document_header_id;
4455 	else
4456 		select 	ph.segment1,
4457 				pr.release_num,
4458 				pr.revision_num,
4459 				pos_totals_po_sv.get_release_total(pr.po_release_id),
4460 				ph.currency_code,
4461 				ph.vendor_id,
4462 				ph.vendor_site_id,
4463 				to_char(pr.creation_date, fnd_profile.value_wnps('ICX_DATE_FORMAT_MASK')||' HH24:MI:SS'),
4464 				ph.fob_lookup_code,
4465 				ph.ship_via_lookup_code,
4466 				ph.ship_to_location_id,
4467 				ph.type_lookup_code,
4468 				global_agreement_flag
4469 		into
4470 				l_blanket_num,
4471 				l_release_num,
4472 				l_revision_num,
4473 				l_rel_total,
4474 				l_rel_currency,
4475 				l_vendor_id,
4476 				l_vendor_site_id,
4477 				l_order_date,
4478 				l_fob,
4479 				l_carrier,
4480 				l_ship_to_id,
4481 				l_type_lookup_code,
4482 				l_global_agreement_flag
4483 		from po_releases_all pr,
4484 			po_headers_all ph
4485 		where pr.po_release_id = l_po_release_id
4486 		and pr.po_header_id = ph.po_header_id;
4487 	end if;
4488 	l_progress := '002';
4489 	select vendor_name
4490 	into l_supplier_name
4491 	from po_vendors
4492 	where vendor_id = l_vendor_id;
4493 
4494 	select
4495 		address_line1,
4496 		address_line2,
4497 		address_line3,
4498 		city,
4499 		state,
4500 		zip
4501 	into
4502 		l_sup_address_line1,
4503 		l_sup_address_line2,
4504 		l_sup_address_line3,
4505 		l_sup_city,
4506 		l_sup_state,
4507 		l_sup_zip
4508 	from po_vendor_sites_all
4509 	where vendor_site_id = l_vendor_site_id;
4510 
4511 	select
4512 		address_line_1,
4513 		address_line_2,
4514 		address_line_3,
4515 		town_or_city,
4516 		region_1,
4517 		postal_code
4518 	into
4519 		l_ship_addr_l1,
4520 		l_ship_addr_l2,
4521 		l_ship_addr_l3,
4522 		l_ship_city,
4523 		l_ship_state,
4524 		l_ship_zip
4525 	from hr_locations_all
4526 	where location_id = l_ship_to_id;
4527 	l_progress := '003';
4528 if (display_type = 'text/html') then
4529 
4530 	if(l_po_release_id is null) then
4531 	if(l_type_lookup_code = 'STANDARD') then
4532 	  l_doc_hdr_info := fnd_message.get_string('PO','PO_WF_NOTIF_STD_PO')||' '||l_po_num;
4533 	elsif (l_global_agreement_flag = 'Y' and l_type_lookup_code = 'BLANKET') then
4534 	  l_doc_hdr_info := fnd_message.get_string('PO','PO_GA_TYPE') ||' '||l_po_num;
4535 	elsif(l_type_lookup_code = 'BLANKET') then
4536 	  l_doc_hdr_info := fnd_message.get_string('PO','PO_WF_NOTIF_BLANKET')||' '||l_po_num;
4537 	  l_po_total := pos_totals_po_sv.get_po_archive_total(l_document_header_id,l_revision_num,
4538 				                              l_type_lookup_code );
4539 	elsif(l_type_lookup_code = 'CONTRACT') then
4540 	  l_doc_hdr_info := fnd_message.get_string('PO','PO_POTYPE_CNTR')||' '||l_po_num;
4541 	elsif(l_type_lookup_code = 'PLANNED') then
4542 	  l_doc_hdr_info := fnd_message.get_string('PO','PO_WF_NOTIF_PLAN_PO')||' '||l_po_num;
4543 	end if;
4544 			l_document :=l_base_url_tag|| '
4545 			<font size=3 color=#336699 face=arial><b>'||l_doc_hdr_info||' '||
4546 			fnd_message.get_string('PO','PO_WF_NOTIF_REV')||' '
4547 			||l_revision_num||' ('||fnd_message.get_string('PO','PO_WF_NOTIF_TOTAL')||'('||l_po_currency||')'||
4548 			TO_CHAR(l_po_total, FND_CURRENCY.GET_FORMAT_MASK(l_po_currency, 30))
4549 			||')</B></font><HR size=1 color=#cccc99>';
4550 
4551 	else
4552 			if(l_type_lookup_code = 'BLANKET') then
4553 				l_doc_hdr_info := fnd_message.get_string('PO','PO_WF_NOTIF_BKT_REL')||' '||l_blanket_num||'-'||l_release_num;
4554 			elsif(l_type_lookup_code = 'PLANNED') then
4555 				l_doc_hdr_info := fnd_message.get_string('PO','PO_WF_NOTIF_SCH_REL')||' '||l_blanket_num||'-'||l_release_num;
4556 			end if;
4557 			l_document :=l_base_url_tag|| '
4558 			<font size=3 color=#336699 face=arial><b>'||l_doc_hdr_info||
4559 			' '||fnd_message.get_string('PO','PO_WF_NOTIF_REV')||' '
4560 			||l_revision_num||' ('||fnd_message.get_string('PO','PO_WF_NOTIF_TOTAL')||'('||
4561 			l_rel_currency||')'||
4562 			TO_CHAR(l_rel_total, FND_CURRENCY.GET_FORMAT_MASK(l_rel_currency, 30))
4563 			||')</B></font><HR size=1 color=#cccc99>';
4564 
4565 	end if;
4566 	l_progress := '004';
4567 	l_document := l_document || '
4568 	<TABLE  cellpadding=2 cellspacing=1>
4569 	<TR>
4570 	<TD>
4571 	<TABLE cellpadding=2 cellspacing=1>
4572 	<TR>
4573 	<TD nowrap><font color=black><B>
4574 	                      '||fnd_message.get_string('PO','PO_WF_NOTIF_VENDOR')||'</B></font></TD>
4575 	<TD nowrap><font color=black>'||l_supplier_name||'</font></TD>
4576 	</TR>
4577 	<TR>
4578 	<TD nowrap valign=TOP><font color=black><B>
4579 	                      '||fnd_message.get_string('PO','PO_WF_NOTIF_ADDRESS')||'</B></font></TD>
4580 	<TD nowrap><font color=black>'||l_sup_address_line1||' '||l_sup_address_line2||' '||
4581 									l_sup_address_line3||'<BR>'||l_sup_city||', '||l_sup_state||' '||l_sup_zip||
4582 	                      '</font></TD>
4583 	</TR>
4584 	<TR>
4585 		<TD nowrap><font color=black><B>
4586 		'||fnd_message.get_string('PO','PO_WF_NOTIF_FOB')||'</B></font></TD>
4587 		<TD nowrap><font color=black>'||l_fob||
4588 	    '</font></TD>
4589 	</TR>
4590 
4591 
4592 	<TR>
4593 	<TD nowrap><font color=black><B>
4594 	                      '||fnd_message.get_string('PO','PO_WF_NOTIF_CARRIER')||'</B></font></TD>
4595 	<TD nowrap><font color=black>'||l_carrier||
4596 	                      '</font></TD>
4597 	</TR>
4598 	</TABLE>
4599 	</TD>
4600 	<TD valign=TOP>
4601 	<TABLE cellpadding=2 cellspacing=1>
4602 	<TR>
4603 		<TD nowrap><font color=black><B>
4604 		'||fnd_message.get_string('PO','PO_WF_NOTIF_ORDER_DATE')||'</B></font></TD>
4605 		<TD nowrap><font color=black>'||l_order_date||
4606 	    '</font></TD>
4607 	</TR>
4608 
4609 	<TR>
4610 	<TD nowrap valign=TOP><font color=black><B>
4611 	                      '||fnd_message.get_string('PO','PO_WF_NOTIF_SHPTO_ADD')||'</B></font></TD>
4612 	<TD nowrap><font color=black>';
4613 	if(l_ship_addr_l1 is not null) then
4614 		l_document := l_document || l_ship_addr_l1||'<BR>';
4615 	end if;
4616 	if(l_ship_addr_l2 is not null) then
4617 		l_document := l_document || l_ship_addr_l2||'<BR>';
4618 	end if;
4619 	if(l_ship_addr_l3 is not null) then
4620 		l_document := l_document || l_ship_addr_l3||'<BR>';
4621 	end if;
4622 
4623 
4624 	l_document := l_document || l_ship_city||', '||l_ship_state||' '||l_ship_zip
4625 							||'
4626 							</font></TD>
4627 	</TR>
4628 
4629 	</TABLE>
4630 	</TD>
4631 	</TABLE></P>';
4632 
4633 	WF_Notification.WriteToClob(document,l_document);
4634 	if(l_header_cancel = 'N') then
4635 		l_document := '<font size=3 color=#336699 face=arial><b>'||fnd_message.get_string('PO', 'PO_WF_NOTIF_PO_DETAILS')||'</B></font><HR size=1 color=#cccc99>';
4636 	--LEGENDS
4637 		l_document := l_document||'<TABLE><TR>'|| NL;
4638 	    l_document:= l_document||'<TD class=instructiontext>'||
4639 	      			'<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">'
4640 	                 || fnd_message.get_string('PO', 'PO_WF_NOTIF_NEW_VALUE');
4641 	    l_document := l_document || '</TD></TR>' || NL;
4642 	    l_document := l_document || '<TR>'|| NL;
4643 	    l_document:= l_document||'<TD class=instructiontext>'||
4644 	      			'<img src=/OA_MEDIA/cancelind_status.gif ALT="">'
4645 	                 || fnd_message.get_string('PO', 'PO_WF_NOTIF_CANCEL_PENDING');
4646 	    l_document := l_document || '</TD></TR>' || NL;
4647 	    l_document := l_document || '</TABLE>' || NL;
4648 
4649 		l_document := l_document ||
4650 		'<TABLE WIDTH=100% cellpadding=2 cellspacing=1>
4651 		<TR bgcolor=#cccc99>
4652 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_LINE_NUMBER')||'</font></TH>
4653 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_SHIPMENT')||'</font></TH>
4654 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_SUP_ITEM')||'</font></TH>
4655 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_DOC_DESCRIPTION')||'</font></TH>
4656 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_UNIT')||'</font></TH>
4657 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_PRICE')||'</font></TH>
4658 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_QTY_ORDERED')||'</font></TH>
4659 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_PROM_DATE')||'</font></TH>
4660 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_SHIPTO')||'</font></TH>
4661 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_RESPONSE')||'</font></TH>
4662 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_REASON')||'</font></TH>
4663 		<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_SPLIT')||'</font></TH>
4664 		</TR>
4665 		</B>';
4666 		WF_NOTIFICATION.WriteToClob(document,l_document);
4667 		open l_po_chg_req_csr(p_chg_req_grp_id);
4668 		loop
4669 		fetch l_po_chg_req_csr
4670 		into
4671 			lc_line_num,
4672 			lc_ship_num,
4673 			lc_old_sup_pt_num,
4674 			lc_new_sup_pt_num,
4675 			lc_old_prom_date,
4676 			lc_new_prom_date,
4677 			lc_old_qty,
4678 			lc_new_qty,
4679 			lc_old_price,
4680 			lc_new_price,
4681 			lc_response,
4682 			lc_item_desc,
4683 			lc_uom,
4684 			lc_ship_to_location,
4685 			lc_reason,
4686 			lc_split,
4687 			lc_action_code;
4688 		EXIT when l_po_chg_req_csr%NOTFOUND;
4689 			if(lc_split is not null) then
4690 				lc_old_sup_pt_num := nvl(lc_old_sup_pt_num,lc_new_sup_pt_num);
4691 				lc_new_sup_pt_num := null;
4692 
4693 				lc_old_price:=nvl(lc_old_price,lc_new_price);
4694 				lc_new_price:= null;
4695 
4696 
4697 				lc_new_prom_date := nvl(lc_new_prom_date,lc_old_prom_date);
4698 				lc_old_prom_date := null;
4699 
4700 				lc_new_qty:= nvl(lc_new_qty,lc_old_qty);
4701 				lc_old_qty:= null;
4702 			end if;
4703 
4704 			if(fnd_timezones.timezones_enabled()='Y') then
4705                             fnd_date_tz.init_timezones_for_fnd_date(true);
4706                             l_old_prom_date := fnd_date.date_to_displayDT(lc_old_prom_date);
4707                             l_new_prom_date := fnd_date.date_to_displayDT(lc_new_prom_date);
4708                         else
4709 			    l_old_prom_date := to_char(lc_old_prom_date,fnd_profile.value_wnps('ICX_DATE_FORMAT_MASK')||' HH24:MI:SS');
4710 			    l_new_prom_date := to_char(lc_new_prom_date,fnd_profile.value_wnps('ICX_DATE_FORMAT_MASK')||' HH24:MI:SS');
4711                         end if;
4712 
4713 
4714 			lc_line_num := nvl(lc_line_num,'` ');
4715 			lc_ship_num:= nvl(lc_ship_num,'` ');
4716 			lc_old_sup_pt_num:= nvl(lc_old_sup_pt_num,'` ');
4717 			lc_new_sup_pt_num:= nvl(lc_new_sup_pt_num,'` ');
4718 			l_old_prom_date:= nvl(l_old_prom_date,'` ');
4719 			l_new_prom_date:= nvl(l_new_prom_date,'` ');
4720 			lc_old_qty:= nvl(lc_old_qty,'` ');
4721 			lc_new_qty:= nvl(lc_new_qty,'` ');
4722 			lc_old_price:= nvl(lc_old_price,'` ');
4723 			lc_new_price:= nvl(lc_new_price,'` ');
4724 			lc_response := nvl(lc_response ,'` ');
4725 			lc_item_desc:= nvl(lc_item_desc,'` ');
4726 			lc_uom:= nvl(lc_uom,'` ');
4727 			lc_ship_to_location:= nvl(lc_ship_to_location,'` ');
4728 			lc_reason:= nvl(lc_reason,'` ');
4729 			lc_split:= nvl(lc_split,'` ');
4730 
4731 		l_document := '
4732 		        <TR bgcolor=#f7f7e7>';
4733 
4734 		if(lc_action_code = 'CANCELLATION') then
4735 			l_document := l_document ||'
4736 		        <TD align=left><font color=black>
4737 			        	<TABLE>
4738 			        		<TR>
4739 			        			<TD>
4740 				        			<img src=/OA_MEDIA/cancelind_status.gif ALT="">
4741 			        			</TD>
4742 			        			<TD alight=right>'
4743 				        			||lc_line_num||
4744 			        			'</TD>
4745 			        		</TR>
4746 			        	</TABLE>
4747 			        </font></TD> ';
4748 		else
4749 			l_document := l_document ||'
4750 		        <TD align=left><font color=black>'||lc_line_num||
4751 		    	                  '</font></TD> ';
4752 		end if;
4753 			l_document := l_document ||
4754 				'<TD align=left><font color=black>'||lc_ship_num||
4755 		                      '</font></TD> ';
4756 
4757 		    if(lc_new_sup_pt_num = '` ') then
4758 				l_document := l_document || '
4759 		        <TD align=left><font color=black>'||lc_old_sup_pt_num||'
4760 		                      </font></TD>';
4761 			else
4762 				l_document := l_document || '
4763 		        <TD align=left><font color=black>
4764 		        	<table>
4765 		        		<tr>
4766 		        			<td>'||lc_old_sup_pt_num||'
4767 		        			</td>
4768 		        		</tr>
4769 		        		<tr>
4770 		        			<td>'||lc_new_sup_pt_num||'
4771 		        			</td>
4772 		        			<td>
4773 		        			<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">
4774 		        			</td>
4775 		        		</tr>
4776 		        	</table>
4777 		                      </font></TD>';
4778 			end if;
4779 
4780 			l_document := l_document || '
4781 		        <TD align=left><font color=black>'||lc_item_desc||
4782 		                      '</font></TD>
4783 
4784 		        <TD align=left><font color=black>'||lc_uom||
4785 		                      '</font></TD> ';
4786 
4787 		    if(lc_new_price = '` ') then
4788 				l_document := l_document || '
4789 		        <TD align=left><font color=black>'||lc_old_price||'
4790 		                      </font></TD>';
4791 			else
4792 				l_document := l_document || '
4793 		        <TD align=left><font color=black>
4794 		        	<table>
4795 		        		<tr>
4796 		        			<td>'||lc_old_price||'
4797 		        			</td>
4798 		        		</tr>
4799 		        		<tr>
4800 		        			<td>'||lc_new_price||'
4801 		        			</td>
4802 		        			<td>
4803 		        			<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">
4804 		        			</td>
4805 		        		</tr>
4806 		        	</table>
4807 		                      </font></TD>';
4808 			end if;
4809 
4810 		    if(lc_new_qty = '` ') then
4811 				l_document := l_document || '
4812 		        <TD align=left><font color=black>'||lc_old_qty||'
4813 		                      </font></TD>';
4814 		    elsif(lc_split <> '` ') then
4815 				l_document := l_document || '
4816 		        <TD align=left><font color=black>
4817 		        	<table>
4818 		        		<tr>
4819 		        			<td>'||lc_new_qty||'
4820 		        			</td>
4821 		        			<td>
4822 		        			<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">
4823 		        			</td>
4824 		        		</tr>
4825 		        	</table>
4826 		                      </font></TD>';
4827 			else
4828 				l_document := l_document || '
4829 		        <TD align=left><font color=black>
4830 		        	<table>
4831 		        		<tr>
4832 		        			<td>'||lc_old_qty||'
4833 		        			</td>
4834 		        		</tr>
4835 		        		<tr>
4836 		        			<td>'||lc_new_qty||'
4837 		        			</td>
4838 		        			<td>
4839 		        			<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">
4840 		        			</td>
4841 		        		</tr>
4842 		        	</table>
4843 		                      </font></TD>';
4844 			end if;
4845 
4846 		    if(l_new_prom_date = '` ') then
4847 				l_document := l_document || '
4848 		        <TD align=left><font color=black>'||l_old_prom_date||'
4849 		                      </font></TD>';
4850 		    elsif(lc_split <> '` ' OR l_old_prom_date = '` ') then
4851 				l_document := l_document || '
4852 		        <TD align=left><font color=black>
4853 		        	<table>
4854 		        		<tr>
4855 		        			<td>'||l_new_prom_date||'
4856 		        			</td>
4857 		        			<td>
4858 		        			<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">
4859 		        			</td>
4860 		        		</tr>
4861 		        	</table>
4862 		                      </font></TD>';
4863 			else
4864 				l_document := l_document || '
4865 		        <TD align=left><font color=black>
4866 		        	<table>
4867 		        		<tr>
4868 		        			<td>'||l_old_prom_date||'
4869 		        			</td>
4870 		        		</tr>
4871 		        		<tr>
4872 		        			<td>'||l_new_prom_date||'
4873 		        			</td>
4874 		        			<td>
4875 		        			<img src=/OA_MEDIA/newupdateditem_status.gif ALT="">
4876 		        			</td>
4877 		        		</tr>
4878 		        	</table>
4879 		                      </font></TD>';
4880 			end if;
4881 			l_document := l_document || '
4882 
4883 		        <TD align=left><font color=black>'||lc_ship_to_location||
4884 		                      '</font></TD>
4885 
4886 		        <TD align=left><font color=black>'||lc_response||
4887 		                      '</font></TD>
4888 		        <TD align=left><font color=black>'||lc_reason||
4889 		                      '</font></TD>
4890 		        <TD align=left><font color=black>'||lc_split||
4891 		                      '</font></TD>
4892 
4893 		        </TR>
4894 			';
4895 			WF_NOTIFICATION.WriteToClob(document,l_document);
4896 		END LOOP;
4897 		CLOSE l_po_chg_req_csr;
4898 		WF_NOTIFICATION.WriteToClob(document, '
4899 		</TABLE></P>
4900 		');
4901 		open l_header_changes_csr(p_chg_req_grp_id);
4902 				fetch l_header_changes_csr into l_additional_changes, l_header_response ;
4903 				close l_header_changes_csr;
4904 
4905 				if(l_additional_changes is not null) then
4906 				  if(l_header_response = 'ACCEPTED') then
4907 					l_header_response := fnd_message.get_string('PO','PO_WF_NOTIF_ACCEPTED');
4908 				  else
4909 					l_header_response := fnd_message.get_string('PO','PO_WF_NOTIF_REJ');
4910 				  end if;
4911 				  l_document := '<BR><TABLE WIDTH=100% cellpadding=2 cellspacing=1>' ||
4912 				                   '<TR bgcolor=#cccc99>' ||
4913 				                   '<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_ADD_CHN_REQ_NCOL')||
4914 				                                  '</font></TH>' ||
4915 				                   '<TH align=left><font color=#336699 >'||fnd_message.get_string('PO','PO_WF_NOTIF_RESPONSE')||
4916 				                   '</font></TH>' ||
4917 				                   '</TR>' ||
4918 				                   '<TR>' || '<TD  align=left><font color=black>' || l_additional_changes ||
4919 				                   '</font></TD><TD  align=left><font color=black>' || l_header_response ||
4920 
4921 				                   '</font></TD></TR>' ||
4922 				                   '</TABLE>';
4923 
4924 					WF_NOTIFICATION.WriteToClob(document,l_document);
4925 				end if;
4926 
4927 	else  --if header_cnacel = N
4928 		if(l_header_response = 'ACCEPTED') then
4929 			l_header_response := fnd_message.get_string('PO','PO_WF_NOTIF_SACCEPTED');
4930 		else
4931 			l_header_response := fnd_message.get_string('PO','PO_WF_NOTIF_SREJECTED');
4932 		end if;
4933 		fnd_message.set_name('PO','PO_WF_NOTIF_BUY_RESP_CAN');
4934 	    fnd_message.set_token('DOC',l_doc_hdr_info);
4935 	    fnd_message.set_token('RESPONSE',l_header_response);
4936 	    l_document := fnd_message.get;
4937 	    WF_NOTIFICATION.WriteToClob(document,l_document);
4938 	end if;
4939 else
4940 	WF_NOTIFICATION.WriteToClob(document,'GEN_NTF_FOR_SUP_BUY_RP for no html customer');
4941 end if;
4942 exception when others then
4943 	IF g_fnd_debug = 'Y' THEN
4944 		FND_LOG.string(FND_LOG.level_unexpected, g_module_prefix ||
4945 				l_api_name || '.others_exception', l_progress||':'||sqlerrm);
4946 	END IF;
4947 	WF_NOTIFICATION.WriteToClob(document,'Exception occured:'||sqlerrm);  */
4948  null;
4949 
4950 END GEN_NTF_FOR_SUP_BUY_RP;
4951 
4952 ---------------------------------------------------------
4953 --  public
4954 --
4955 --  workflow document procedure to generate notification
4956 --  subject for buyer
4957 ---------------------------------------------------------
4958 PROCEDURE GEN_NTF_FOR_BUYER_SUBJECT
4959   (document_id   IN VARCHAR2,
4960    display_type  IN VARCHAR2,
4961    document      IN OUT nocopy VARCHAR2,
4962    document_type IN OUT nocopy VARCHAR2)
4963 IS
4964 
4965 l_po_num po_headers_all.segment1%TYPE;
4966 l_supplier_name po_vendors.vendor_name%TYPE;
4967 l_blanket_num po_headers_all.segment1%TYPE;
4968 --l_release_num number;
4969 l_release_num po_releases_all.release_num%TYPE;
4970 l_document_header_info varchar2(2000);
4971 l_type_lookup_code po_headers_all.type_lookup_code%TYPE;
4972 l_global_agreement_flag varchar2(3);
4973 l_header_cancel varchar2(1);
4974 l_id NUMBER;
4975 l_revision_num NUMBER;  -- RDP changes
4976 l_po_total NUMBER;
4977 l_currency_code VARCHAR2(5);
4978 l_rel_revision_num NUMBER;
4979 l_rel_total NUMBER;
4980 l_rel_currency_code VARCHAR2(5);
4981 
4982 CURSOR l_header_cancel_csr(grp_id number) IS
4983     select change_request_id
4984     from po_change_requests
4985     where change_request_group_id = grp_id
4986     and action_type = 'CANCELLATION'
4987     and request_level = 'HEADER';
4988 
4989 l_itemtype VARCHAR2(30);
4990 l_itemkey po_change_requests.wf_item_key%TYPE;
4991 l_header_id NUMBER;
4992 l_release_id NUMBER;
4993 l_chg_req_grp_id NUMBER;
4994 l_ack_req_flag VARCHAR2(30);
4995 l_notif_usage VARCHAR2(30);                -- RDP changes
4996 l_message_name fnd_new_messages.message_name%TYPE;
4997 l_display_name varchar2(1000);
4998 l_msg_header varchar2(1000);
4999 
5000 BEGIN
5001 
5002   l_itemtype := 'POSCHORD';
5003   l_itemkey :=  document_id;
5004 
5005   l_header_id := wf_engine.GetItemAttrNumber(
5006 				    itemtype => l_itemtype,
5007 				    itemkey => l_itemkey,
5008 				    aname => 'PO_HEADER_ID');
5009 
5010   l_release_id := wf_engine.GetItemAttrNumber(
5011 				    itemtype => l_itemtype,
5012 				    itemkey => l_itemkey,
5013 				    aname => 'PO_RELEASE_ID');
5014 
5015   l_chg_req_grp_id := wf_engine.GetItemAttrNumber(
5016 				    itemtype => l_itemtype,
5017 				    itemkey => l_itemkey,
5018 				    aname => 'CHANGE_REQUEST_GROUP_ID');
5019 
5020   l_ack_req_flag := wf_engine.GetItemAttrText(
5021 				    itemtype => l_itemtype,
5022 				    itemkey => l_itemkey,
5023 				    aname => 'ACKNOWLEDGE_REQ_FLAG');
5024 
5025   l_notif_usage := wf_engine.GetItemAttrText(                                -- RDP changes
5026                                     itemtype => l_itemtype,
5027                                     itemkey => l_itemKey,
5028                                     aname => 'NOTIF_USAGE');
5029 
5030 
5031 
5032 
5033   l_header_cancel := 'N';
5034 
5035   IF(l_chg_req_grp_id is not null) THEN
5036     OPEN l_header_cancel_csr(l_chg_req_grp_id);
5037     FETCH l_header_cancel_csr
5038     INTO l_id;
5039     CLOSE l_header_cancel_csr;
5040     IF(l_id is not null) THEN
5041       l_header_cancel := 'Y';
5042     END IF;
5043   END IF;
5044 
5045   IF(l_release_id is null) THEN
5046     select
5047       pv.vendor_name,
5048       pha.segment1,
5049       pha.type_lookup_code,
5050       pha.GLOBAL_AGREEMENT_FLAG,
5051       pha.revision_num,
5052       pos_totals_po_sv.get_po_total(pha.po_header_id),
5053       pha.currency_code,
5054       PDSL.DISPLAY_NAME
5055      into
5056       l_supplier_name,
5057       l_po_num,
5058       l_type_lookup_code,
5059       l_global_agreement_flag,
5060       l_revision_num,
5061       l_po_total,
5062       l_currency_code,
5063       l_display_name
5064      from
5065       po_headers_all pha,
5066       po_vendors pv,
5067       PO_ALL_DOC_STYLE_LINES PDSL
5068     where pha.po_header_id = l_header_id
5069 	    and pha.vendor_id = pv.vendor_id
5070 	    AND NVL(PHA.STYLE_ID, 0) = PDSL.STYLE_ID (+)
5071             AND PHA.TYPE_LOOKUP_CODE = PDSL.DOCUMENT_SUBTYPE (+)
5072             AND PDSL.ENABLED_FLAG (+) = 'Y' AND PDSL.STATUS (+) = 'ACTIVE'
5073             AND PDSL.LANGUAGE (+) = USERENV('LANG');
5074 
5075   IF( l_display_name IS NULL) THEN
5076     if(l_type_lookup_code = 'STANDARD') then
5077       l_message_name := 'PO_WF_NOTIF_STD_PO';
5078     elsif (l_global_agreement_flag = 'Y' and l_type_lookup_code = 'BLANKET') then
5079       l_message_name := 'PO_GA_TYPE';
5080     elsif(l_type_lookup_code = 'BLANKET') then
5081       l_message_name := 'PO_WF_NOTIF_BLANKET';
5082     elsif(l_type_lookup_code = 'CONTRACT') then
5083       l_message_name := 'PO_POTYPE_CNTR';
5084     elsif(l_type_lookup_code = 'PLANNED') then
5085       l_message_name := 'PO_WF_NOTIF_PLAN_PO';
5086     else
5087       return;
5088     end if;
5089 
5090     fnd_message.set_name('PO', l_message_name);
5091     l_msg_header := fnd_message.get;
5092   ELSIF( l_display_name IS NOT NULL) THEN
5093     l_msg_header := l_display_name;
5094   END IF;
5095 
5096     --fnd_message.set_token('PO_NUMBER', l_po_num);
5097   IF (l_notif_usage = 'BUYER_AUTO_FYI') THEN                                         -- RDP if Auto Approved add Auto Approve message
5098     l_document_header_info := l_msg_header || ' ' || l_po_num || ', ' ||
5099                               l_revision_num|| ' ('||fnd_message.get_string('PO','PO_WF_NOTIF_TOTAL')||' '
5100                               ||TO_CHAR(l_po_total, FND_CURRENCY.GET_FORMAT_MASK(l_currency_code, 30)) ||
5101                               ' ' || l_currency_code || ') ' || fnd_message.get_string('POS','POS_AUTO_APP_MSG');
5102     ELSE
5103         IF(l_type_lookup_code = 'BLANKET') THEN
5104           l_document_header_info := l_msg_header || ' ' || l_po_num || ', ' || l_revision_num;     -- RDP if Blanket don't display Total
5105         ELSE
5106     l_document_header_info := l_msg_header || ' ' || l_po_num || ', ' || l_revision_num|| ' ('||fnd_message.get_string('PO','PO_WF_NOTIF_TOTAL')
5107                               ||' '||TO_CHAR(l_po_total, FND_CURRENCY.GET_FORMAT_MASK(l_currency_code, 30))
5108                               || ' ' || l_currency_code || ') ';
5109         END IF;
5110     END IF;
5111 
5112   ELSE  -- l_release_id is not null
5113     select
5114 	pv.vendor_name,
5115 	pha.segment1,
5116 	pra.release_num,
5117 	pha.type_lookup_code,
5118 	pra.revision_num,
5119 	pos_totals_po_sv.get_release_total(pra.po_release_id),
5120         pha.currency_code
5121     into
5122 	l_supplier_name,
5123 	l_blanket_num,
5124 	l_release_num,
5125 	l_type_lookup_code,
5126 	l_rel_revision_num,
5127 	l_rel_total,
5128         l_rel_currency_code
5129     from
5130         po_releases_all pra,
5131 	po_headers_all pha,
5132 	po_vendors pv
5133     where pra.po_release_id = l_release_id
5134       and pra.po_header_id = pha.po_header_id
5135       and pha.vendor_id = pv.vendor_id;
5136 
5137     if(l_type_lookup_code = 'BLANKET')then
5138       l_message_name := 'PO_WF_NOTIF_BKT_REL2';
5139     elsif(l_type_lookup_code = 'PLANNED') then
5140       l_message_name := 'PO_WF_NOTIF_SCH_REL2';
5141     else
5142       return;
5143     end if;
5144 
5145     fnd_message.set_name('PO', l_message_name);
5146     fnd_message.set_token('BLANKET_NUMBER', l_blanket_num);
5147     fnd_message.set_token('RELEASE_NUMBER', l_release_num);
5148    IF (l_notif_usage = 'BUYER_AUTO_FYI') THEN                      -- RDP  generating subject for releases if auto approved
5149         l_document_header_info := fnd_message.get  || ', ' || l_rel_revision_num ||'('
5150           ||fnd_message.get_string('PO','PO_WF_NOTIF_TOTAL')|| ' '|| TO_CHAR(l_rel_total, FND_CURRENCY.GET_FORMAT_MASK(l_rel_currency_code, 30))||' '||l_rel_currency_code||') '|| fnd_message.get_string('POS','POS_AUTO_APP_MSG');
5151    ELSE
5152         l_document_header_info :=  fnd_message.get  || ', ' || l_rel_revision_num ||'('
5153           ||fnd_message.get_string('PO','PO_WF_NOTIF_TOTAL')|| ' '|| TO_CHAR(l_rel_total, FND_CURRENCY.GET_FORMAT_MASK(l_rel_currency_code, 30))||' '||l_rel_currency_code||')';
5154    END IF;
5155 
5156   END IF;
5157 
5158 
5159 
5160   IF(l_header_cancel = 'N') THEN
5161     if(l_ack_req_flag = 'Y') then
5162       l_message_name := 'PO_WF_NOTIF_ACK_REP_FROM2';
5163     else
5164       l_message_name := 'PO_WF_NOTIF_CHN_REQ_FROM2';
5165     end if;
5166 
5167   ELSE
5168     l_message_name := 'PO_WF_NOTIF_CANCEL_REQ_FROM';
5169   END IF;
5170 
5171   fnd_message.set_name('PO', l_message_name);
5172   fnd_message.set_token('SUPPLIER_NAME', l_supplier_name);
5173   fnd_message.set_token('HEADER_INFO', l_document_header_info);
5174   document := fnd_message.get;
5175 
5176 END GEN_NTF_FOR_BUYER_SUBJECT;
5177 
5178 ---------------------------------------------------------
5179 --  public
5180 --
5181 --  workflow document procedure to generate notification
5182 --  subject for supplier
5183 ---------------------------------------------------------
5184 PROCEDURE GEN_NTF_FOR_SUP_SUBJECT
5185   (document_id   IN VARCHAR2,
5186    display_type  IN VARCHAR2,
5187    document      IN OUT nocopy VARCHAR2,
5188    document_type IN OUT nocopy VARCHAR2)
5189 IS
5190 
5191 l_po_num                                po_headers_all.segment1%TYPE;
5192 l_blanket_num                           po_headers_all.segment1%TYPE;
5193 --l_release_num                           number;
5194 l_release_num po_releases_all.release_num%TYPE;
5195 l_buyer_name                            hr_all_organization_units_tl.name%TYPE;
5196 l_type_lookup_code                      po_headers_all.type_lookup_code%TYPE;
5197 l_global_agreement_flag  po_headers_all.GLOBAL_AGREEMENT_FLAG%TYPE := 'F';
5198 l_doc_hdr_info varchar2(2000);
5199 
5200 l_id number;
5201 l_header_cancel varchar2(1);
5202 CURSOR l_header_cancel_csr(grp_id number) IS
5203     select change_request_id
5204     from po_change_requests
5205     where change_request_group_id = grp_id
5206       and action_type = 'CANCELLATION'
5207       and request_level = 'HEADER';
5208 
5209 l_itemtype VARCHAR2(30);
5210 l_itemkey po_change_requests.wf_item_key%TYPE;
5211 l_header_id NUMBER;
5212 l_release_id NUMBER;
5213 l_chg_req_grp_id NUMBER;
5214 l_revision_num NUMBER;
5215 l_display_name varchar2(1000);
5216 l_msg_header varchar2(1000);
5217 l_message_name fnd_new_messages.message_name%TYPE;
5218 l_temp_message fnd_new_messages.message_name%TYPE;         -- RDP changes
5219 
5220 BEGIN
5221 
5222   l_itemtype := 'POSCHORD';
5223   l_itemkey :=  document_id;
5224 
5225   l_header_id := wf_engine.GetItemAttrNumber(
5226                                     itemtype => l_itemtype,
5227                                     itemkey => l_itemkey,
5228                                     aname => 'PO_HEADER_ID');
5229 
5230   l_release_id := wf_engine.GetItemAttrNumber(
5231                                     itemtype => l_itemtype,
5232                                     itemkey => l_itemkey,
5233                                     aname => 'PO_RELEASE_ID');
5234 
5235   l_chg_req_grp_id := wf_engine.GetItemAttrNumber(
5236                                     itemtype => l_itemtype,
5237                                     itemkey => l_itemkey,
5238                                     aname => 'CHANGE_REQUEST_GROUP_ID');
5239 
5240   l_revision_num := wf_engine.GetItemAttrNumber(
5241                                     itemtype => l_itemtype,
5242                                     itemkey => l_itemkey,
5243                                     aname => 'PO_REVISION_NUM');
5244 
5245   l_header_cancel := 'N';
5246   if(l_chg_req_grp_id is not null) then
5247     open l_header_cancel_csr(l_chg_req_grp_id);
5248     fetch l_header_cancel_csr
5249     into l_id;
5250     close l_header_cancel_csr;
5251     if(l_id is not null) then
5252       l_header_cancel := 'Y';
5253     end if;
5254   end if;
5255 
5256   IF(l_release_id is null) THEN
5257     select pha.segment1,
5258 	   hou.name,
5259 	   pha.type_lookup_code,
5260 	   pha.global_agreement_flag,
5261 	   PDSL.DISPLAY_NAME
5262     into   l_po_num,
5263 	   l_buyer_name,
5264 	   l_type_lookup_code,
5265 	   l_global_agreement_flag,
5266 	   l_display_name
5267     from   po_headers_all pha,
5268 	   hr_all_organization_units_tl hou,
5269 	   PO_ALL_DOC_STYLE_LINES PDSL
5270     where  pha.po_header_id = l_header_id
5271       and  pha.org_id = hou.organization_id(+)
5272       and  hou.language(+) = userenv('LANG')
5273       AND NVL(PHA.STYLE_ID, 0) = PDSL.STYLE_ID (+)
5274       AND PHA.TYPE_LOOKUP_CODE = PDSL.DOCUMENT_SUBTYPE (+)
5275       AND PDSL.ENABLED_FLAG (+) = 'Y' AND PDSL.STATUS (+) = 'ACTIVE'
5276       AND PDSL.LANGUAGE (+) = USERENV('LANG');
5277 
5278  IF(l_display_name IS NULL) THEN
5279     if(l_type_lookup_code = 'STANDARD') then
5280       l_message_name := 'PO_WF_NOTIF_STD_PO';
5281     elsif(l_type_lookup_code = 'PLANNED') then
5282       l_message_name := 'PO_WF_NOTIF_PLAN_PO';
5283     elsif (l_global_agreement_flag = 'Y' and l_type_lookup_code = 'BLANKET') then
5284       l_message_name := 'PO_GA_TYPE';
5285     elsif(l_type_lookup_code = 'BLANKET') then
5286       l_message_name := 'PO_WF_NOTIF_BLANKET';
5287     elsif(l_type_lookup_code = 'CONTRACT') then
5288       l_message_name := 'PO_POTYPE_CNTR';
5289     else
5290       RETURN;
5291     end if;
5292 
5293     fnd_message.set_name('PO', l_message_name);
5294     l_msg_header := fnd_message.get;
5295   ELSIF(l_display_name IS NOT NULL) THEN
5296     l_msg_header := l_display_name;
5297   END IF;
5298     --fnd_message.set_token('PO_NUMBER', l_po_num);
5299      l_doc_hdr_info := l_msg_header || ' ' || l_po_num || ',' || l_revision_num;     -- RDP changes (generating subject for Supplier Notification)
5300 
5301   ELSE
5302     select pha.segment1,
5303 	   pra.release_num,
5304 	   hou.name,
5305 	   pha.type_lookup_code
5306     into   l_blanket_num,
5307 	   l_release_num,
5308 	   l_buyer_name,
5309 	   l_type_lookup_code
5310     from   po_headers_all pha,
5311 	   po_releases_all pra,
5312 	   hr_all_organization_units_tl hou
5313     where  pra.po_release_id = l_release_id
5314     and    pha.po_header_id = pra.po_header_id
5315     and    pha.org_id = hou.organization_id(+)
5316     and    hou.language(+) = userenv('LANG');
5317 
5318     if(l_type_lookup_code = 'BLANKET') then
5319       l_message_name := 'PO_WF_NOTIF_BKT_REL2';
5320     elsif(l_type_lookup_code = 'PLANNED') then
5321       l_message_name := 'PO_WF_NOTIF_SCH_REL2';
5322     else
5323       RETURN;
5324     end if;
5325 
5326     fnd_message.set_name('PO', l_message_name);
5327     fnd_message.set_token('BLANKET_NUMBER', l_blanket_num);
5328     fnd_message.set_token('RELEASE_NUMBER', l_release_num);
5329     l_doc_hdr_info := fnd_message.get || ',' || l_revision_num;                     --   RDP changes
5330 
5331   END IF;
5332 
5333   if(l_header_cancel = 'N') then
5334 
5335 
5336      /*   fnd_message.set_name('POS','PO_WF_NOTIF_RESP_SUB_CHG');
5337           fnd_message.set_token('DOC',l_doc_hdr_info);
5338           fnd_message.set_token('BUYER',l_buyer_name);
5339           document := fnd_message.get;   */
5340 
5341         l_temp_message := l_buyer_name || ' - ';
5342         fnd_message.set_name('POS','POS_SUP_RESP_MSG');
5343         fnd_message.set_token('DOC',l_doc_hdr_info);
5344         document := l_temp_message ||  fnd_message.get;                             --  RDP changes
5345 
5346   else
5347 
5348      /*   fnd_message.set_name('POS','PO_WF_NOTIF_RESP_SUB_CAN');
5349           fnd_message.set_token('DOC',l_doc_hdr_info);
5350           fnd_message.set_token('BUYER',l_buyer_name);
5351           document := fnd_message.get;     */
5352 
5353         l_temp_message := l_buyer_name || ' - ';
5354         fnd_message.set_name('POS','POS_SUP_RESP_MSG_CAN');
5355         fnd_message.set_token('DOC',l_doc_hdr_info);
5356         document := l_temp_message ||  fnd_message.get;                             --  RDP changes
5357 
5358   end if;
5359 
5360 END GEN_NTF_FOR_SUP_SUBJECT;
5361 
5362 ---------------------------------------------------------
5363 --  public
5364 --
5365 --  workflow document procedure to generate notification
5366 --  subject for planner
5367 ---------------------------------------------------------
5368 PROCEDURE GEN_NTF_FOR_PLAN_SUBJECT
5369   (document_id   IN VARCHAR2,
5370    display_type  IN VARCHAR2,
5371    document      IN OUT nocopy VARCHAR2,
5372    document_type IN OUT nocopy VARCHAR2)
5373 IS
5374 
5375 l_po_num                                po_headers_all.segment1%TYPE;
5376 l_blanket_num                           po_headers_all.segment1%TYPE;
5377 --l_release_num                           number;
5378 l_release_num                           po_releases_all.release_num%TYPE;
5379 l_supplier_name po_vendors.vendor_name%TYPE;
5380 l_type_lookup_code                      po_headers_all.type_lookup_code%TYPE;
5381 l_document_header_info varchar2(2000);
5382 
5383 l_id number;
5384 l_header_cancel varchar2(1);
5385 CURSOR l_header_cancel_csr(grp_id number) IS
5386     select change_request_id
5387     from po_change_requests
5388     where change_request_group_id = grp_id
5389       and action_type = 'CANCELLATION'
5390       and request_level = 'HEADER';
5391 
5392 l_itemtype VARCHAR2(30);
5393 l_itemkey po_change_requests.wf_item_key%TYPE;
5394 l_header_id NUMBER;
5395 l_release_id NUMBER;
5396 l_chg_req_grp_id NUMBER;
5397 
5398 l_message_name fnd_new_messages.message_name%TYPE;
5399 l_global_agreement_flag  po_headers_all.GLOBAL_AGREEMENT_FLAG%TYPE := 'F';
5400 l_display_name varchar2(1000);
5401 l_msg_header varchar2(1000);
5402 
5403 
5404 BEGIN
5405 
5406   l_itemtype := 'POSCHORD';
5407   l_itemkey :=  document_id;
5408 
5409   l_header_id := wf_engine.GetItemAttrNumber(
5410                                     itemtype => l_itemtype,
5411                                     itemkey => l_itemkey,
5412                                     aname => 'PO_HEADER_ID');
5413 
5414   l_release_id := wf_engine.GetItemAttrNumber(
5415                                     itemtype => l_itemtype,
5416                                     itemkey => l_itemkey,
5417                                     aname => 'PO_RELEASE_ID');
5418 
5419   l_chg_req_grp_id := wf_engine.GetItemAttrNumber(
5420                                     itemtype => l_itemtype,
5421                                     itemkey => l_itemkey,
5422                                     aname => 'CHANGE_REQUEST_GROUP_ID');
5423 
5424   IF(l_release_id is null) THEN
5425     select pha.type_lookup_code,
5426            pha.segment1,
5427 	   pv.vendor_name,
5428 	   pha.global_agreement_flag,
5429 	   PDSL.display_name
5430     into   l_type_lookup_code,
5431 	   l_po_num,
5432 	   l_supplier_name,
5433 	   l_global_agreement_flag,
5434 	   l_display_name
5435     from   po_headers_all pha,
5436 	   po_vendors pv,
5437 	   PO_ALL_DOC_STYLE_LINES PDSL
5438     where  pha.po_header_id = l_header_id
5439     and    pv.vendor_id = pha.vendor_id
5440     AND NVL(PHA.STYLE_ID, 0) = PDSL.STYLE_ID (+)
5441     AND PHA.TYPE_LOOKUP_CODE = PDSL.DOCUMENT_SUBTYPE (+)
5442     AND PDSL.ENABLED_FLAG (+) = 'Y' AND PDSL.STATUS (+) = 'ACTIVE'
5443     AND PDSL.LANGUAGE (+) = USERENV('LANG');
5444 
5445  IF(l_display_name IS NULL) THEN
5446     if(l_type_lookup_code = 'STANDARD') then
5447       l_message_name := 'PO_WF_NOTIF_STD_PO';
5448     elsif(l_type_lookup_code = 'PLANNED') then
5449       l_message_name := 'PO_WF_NOTIF_PLAN_PO';
5450     elsif (l_global_agreement_flag = 'Y' and l_type_lookup_code = 'BLANKET') then
5451       l_message_name := 'PO_GA_TYPE';
5452     elsif(l_type_lookup_code = 'BLANKET') then
5453       l_message_name := 'PO_WF_NOTIF_BLANKET';
5454     elsif(l_type_lookup_code = 'CONTRACT') then
5455       l_message_name := 'PO_POTYPE_CNTR';
5456     end if;
5457 
5458     fnd_message.set_name('PO', l_message_name);
5459     l_msg_header := fnd_message.get;
5460   ELSIF(l_display_name IS NOT NULL) THEN
5461     l_msg_header := l_display_name;
5462   END IF;
5463     --fnd_message.set_token('PO_NUMBER', l_po_num);
5464     l_document_header_info := l_msg_header || ' ' || l_po_num;
5465 
5466   ELSE
5467     select pha.type_lookup_code,
5468 	   pha.segment1,
5469 	   pra.release_num,
5470 	   pv.vendor_name
5471     into   l_type_lookup_code,
5472 	   l_blanket_num,
5473 	   l_release_num,
5474 	   l_supplier_name
5475     from   po_headers_all pha,
5476 	   po_releases_all pra,
5477 	   po_vendors pv
5478     where  pra.po_release_id = l_release_id
5479     and    pra.po_header_id = pha.po_header_id
5480     and    pv.vendor_id = pha.vendor_id;
5481 
5482     if(l_type_lookup_code = 'BLANKET')then
5483       l_message_name := 'PO_WF_NOTIF_BKT_REL2';
5484     elsif(l_type_lookup_code = 'PLANNED') then
5485       l_message_name := 'PO_WF_NOTIF_SCH_REL2';
5486     end if;
5487 
5488     fnd_message.set_name('PO', l_message_name);
5489     fnd_message.set_token('BLANKET_NUMBER', l_blanket_num);
5490     fnd_message.set_token('RELEASE_NUMBER', l_release_num);
5491     l_document_header_info := fnd_message.get;
5492 
5493   END IF;
5494 
5495   l_header_cancel := 'N';
5496   if(l_chg_req_grp_id is not null) then
5497     open l_header_cancel_csr(l_chg_req_grp_id);
5498     fetch l_header_cancel_csr
5499     into l_id;
5500     close l_header_cancel_csr;
5501     if(l_id is not null) then
5502       l_header_cancel := 'Y';
5503     end if;
5504   end if;
5505 
5506   if(l_header_cancel = 'N') then
5507     l_message_name := 'PO_WF_NOTIF_CHN_REQ_FROM2';
5508   else
5509     l_message_name := 'PO_WF_NOTIF_CANCEL_REQ_FROM';
5510   end if;
5511 
5512   fnd_message.set_name('PO', l_message_name);
5513   fnd_message.set_token('SUPPLIER_NAME', l_supplier_name);
5514   fnd_message.set_token('HEADER_INFO', l_document_header_info);
5515   document := fnd_message.get;
5516 
5517 END GEN_NTF_FOR_PLAN_SUBJECT;
5518 
5519 ---------------------------------------------------------
5520 --  public
5521 --
5522 --  workflow document procedure to generate notification
5523 --  subject for requester
5524 ---------------------------------------------------------
5525 PROCEDURE GEN_NTF_FOR_REQ_SUBJECT
5526   (document_id   IN VARCHAR2,
5527    display_type  IN VARCHAR2,
5528    document      IN OUT nocopy VARCHAR2,
5529    document_type IN OUT nocopy VARCHAR2)
5530 IS
5531 
5532 l_po_num                                po_headers_all.segment1%TYPE;
5533 l_blanket_num                           po_headers_all.segment1%TYPE;
5534 --l_release_num                           number;
5535 l_release_num po_releases_all.release_num%TYPE;
5536 l_supplier_name po_vendors.vendor_name%TYPE;
5537 l_type_lookup_code                      po_headers_all.type_lookup_code%TYPE;
5538 l_document_header_info varchar2(2000);
5539 
5540 l_id number;
5541 l_header_cancel varchar2(1);
5542 CURSOR l_header_cancel_csr(grp_id number) IS
5543     select change_request_id
5544     from po_change_requests
5545     where change_request_group_id = grp_id
5546       and action_type = 'CANCELLATION'
5547       and request_level = 'HEADER';
5548 
5549 l_itemtype VARCHAR2(30);
5550 l_itemkey po_change_requests.wf_item_key%TYPE;
5551 l_header_id NUMBER;
5552 l_release_id NUMBER;
5553 l_chg_req_grp_id NUMBER;
5554 
5555 l_message_name fnd_new_messages.message_name%TYPE;
5556 l_global_agreement_flag  po_headers_all.GLOBAL_AGREEMENT_FLAG%TYPE := 'F';
5557 l_display_name varchar2(1000);
5558 l_msg_header varchar2(1000);
5559 
5560 BEGIN
5561 
5562   l_itemtype := 'POSCHORD';
5563   l_itemkey :=  document_id;
5564 
5565   l_header_id := wf_engine.GetItemAttrNumber(
5566                                     itemtype => l_itemtype,
5567                                     itemkey => l_itemkey,
5568                                     aname => 'PO_HEADER_ID');
5569 
5570   l_release_id := wf_engine.GetItemAttrNumber(
5571                                     itemtype => l_itemtype,
5572                                     itemkey => l_itemkey,
5573                                     aname => 'PO_RELEASE_ID');
5574 
5575   l_chg_req_grp_id := wf_engine.GetItemAttrNumber(
5576                                     itemtype => l_itemtype,
5577                                     itemkey => l_itemkey,
5578                                     aname => 'CHANGE_REQUEST_GROUP_ID');
5579 
5580   IF(l_release_id is null) THEN
5581     select pha.type_lookup_code,
5582 	   pha.segment1,
5583 	   pv.vendor_name,
5584 	   pha.global_agreement_flag,
5585 	   PDSL.display_name
5586     into   l_type_lookup_code,
5587 	   l_po_num,
5588 	   l_supplier_name,
5589 	   l_global_agreement_flag,
5590 	   l_display_name
5591     from   po_headers_all pha,
5592 	   po_vendors pv,
5593 	   PO_ALL_DOC_STYLE_LINES PDSL
5594     where  pha.po_header_id = l_header_id
5595     and    pv.vendor_id = pha.vendor_id
5596            AND NVL(PHA.STYLE_ID, 0) = PDSL.STYLE_ID (+)
5597            AND PHA.TYPE_LOOKUP_CODE = PDSL.DOCUMENT_SUBTYPE (+)
5598            AND PDSL.ENABLED_FLAG (+) = 'Y' AND PDSL.STATUS (+) = 'ACTIVE'
5599            AND PDSL.LANGUAGE (+) = USERENV('LANG');
5600 
5601  IF(l_display_name IS NULL) THEN
5602     if(l_type_lookup_code = 'STANDARD') then
5603       l_message_name := 'PO_WF_NOTIF_STD_PO';
5604     elsif(l_type_lookup_code = 'PLANNED') then
5605       l_message_name := 'PO_WF_NOTIF_PLAN_PO';
5606     elsif (l_global_agreement_flag = 'Y' and l_type_lookup_code = 'BLANKET') then
5607       l_message_name := 'PO_GA_TYPE';
5608     elsif(l_type_lookup_code = 'BLANKET') then
5609       l_message_name := 'PO_WF_NOTIF_BLANKET';
5610     elsif(l_type_lookup_code = 'CONTRACT') then
5611       l_message_name := 'PO_POTYPE_CNTR';
5612     end if;
5613 
5614     fnd_message.set_name('PO', l_message_name);
5615     l_msg_header := fnd_message.get;
5616  ELSIF(l_display_name IS NOT NULL) THEN
5617     l_msg_header := l_display_name;
5618  END IF;
5619     --fnd_message.set_token('PO_NUMBER', l_po_num);
5620     l_document_header_info := l_msg_header || ' ' || l_po_num;
5621 
5622   ELSE
5623     select pha.type_lookup_code,
5624 	   pha.segment1,
5625 	   pra.release_num,
5626 	   pv.vendor_name
5627     into   l_type_lookup_code,
5628 	   l_blanket_num,
5629 	   l_release_num,
5630 	   l_supplier_name
5631     from   po_headers_all pha,
5632 	   po_releases_all pra,
5633 	   po_vendors pv
5634     where  pra.po_release_id = l_release_id
5635     and    pra.po_header_id = pha.po_header_id
5636     and    pv.vendor_id = pha.vendor_id;
5637 
5638     if(l_type_lookup_code = 'BLANKET')then
5639       l_message_name := 'PO_WF_NOTIF_BKT_REL2';
5640     elsif(l_type_lookup_code = 'PLANNED') then
5641       l_message_name := 'PO_WF_NOTIF_SCH_REL2';
5642     end if;
5643 
5644     fnd_message.set_name('PO', l_message_name);
5645     fnd_message.set_token('BLANKET_NUMBER', l_blanket_num);
5646     fnd_message.set_token('RELEASE_NUMBER', l_release_num);
5647     l_document_header_info := fnd_message.get;
5648 
5649   end if;
5650 
5651   l_header_cancel := 'N';
5652   if(l_chg_req_grp_id is not null) then
5653     open l_header_cancel_csr(l_chg_req_grp_id);
5654     fetch l_header_cancel_csr
5655     into l_id;
5656     close l_header_cancel_csr;
5657     if(l_id is not null) then
5658       l_header_cancel := 'Y';
5659     end if;
5660   end if;
5661 
5662   if(l_header_cancel = 'N') then
5663     l_message_name := 'PO_WF_NOTIF_CHN_REQ_FROM2';
5664   else
5665     l_message_name := 'PO_WF_NOTIF_CANCEL_REQ_FROM';
5666   end if;
5667 
5668   fnd_message.set_name('PO', l_message_name);
5669   fnd_message.set_token('SUPPLIER_NAME', l_supplier_name);
5670   fnd_message.set_token('HEADER_INFO', l_document_header_info);
5671   document := fnd_message.get;
5672 
5673 END GEN_NTF_FOR_REQ_SUBJECT;
5674 
5675 /*
5676 *Prorate is needed if supplier has changed the Quantity of a PO SHipment,Price of a Shipment of FPS Type  with multiple distributions.
5677 */
5678 procedure IS_PRORATE_NEEDED(		  	itemtype        in varchar2,
5679 			                           	itemkey         in varchar2,
5680     	    		                   	actid           in number,
5681 	    	        		         	funcmode        in varchar2,
5682             	            		    resultout       out NOCOPY varchar2)
5683 IS
5684 l_chg_req_grp_id number;
5685 l_po_header_id number;
5686 l_temp number;
5687 x_progress varchar2(3) := '000';
5688 cursor l_x_csr(id number) is
5689 select pcr.document_line_locatiON_id
5690 from    po_change_requests pcr,
5691 	po_distributions_all pda
5692 where pcr.document_line_location_id = pda.line_location_id
5693 and pcr.change_request_group_id = id                                      -- added checks FPS Shipment Amount, Shipment price Prorate
5694 and (pcr.new_quantity is not null or pcr.new_amount is not null or  pcr.new_price is not null)
5695 group by pcr.document_line_location_id
5696 having count(1) > 1;
5697 l_header_change number;
5698 l_retro_count number;
5699 
5700 BEGIN
5701     l_chg_req_grp_id := wf_engine.GetItemAttrNumber(itemtype => itemtype,
5702                                             itemkey => itemkey,
5703                                             aname => 'CHANGE_REQUEST_GROUP_ID');
5704 
5705     l_po_header_id := wf_engine.GetItemAttrNumber(itemtype => itemtype,
5706                                             itemkey => itemkey,
5707                                             aname => 'PO_HEADER_ID');
5708 
5709     select count(*) into l_retro_count
5710     from po_change_requests pcr, po_headers_all poh
5711     where poh.po_header_id = pcr.document_header_id
5712     and poh.po_header_id = l_po_header_id
5713     and poh.type_lookup_code = 'BLANKET'
5714     and pcr.change_request_group_id = l_chg_req_grp_id
5715     and pcr.po_release_id is null
5716     and pcr.request_level in ('LINE', 'SHIPMENT')
5717     and pcr.action_type='MODIFICATION'
5718     and ((pcr.new_price is not null) or (pcr.new_quantity is not null))
5719     and pcr.request_status in ('PENDING');
5720 
5721     open l_x_csr(l_chg_req_grp_id);
5722     fetch l_x_csr into l_temp;
5723     close l_x_csr;
5724 
5725 	select count(*)
5726 	into l_header_change
5727 	from po_change_requests
5728 	where change_request_group_id = l_chg_req_grp_id
5729 	and request_level = 'HEADER'
5730 	and additional_changes is not null;
5731 
5732     if(l_temp is not null or l_header_change > 0 or l_retro_count > 0) then
5733 		resultout := 'Y';
5734 		wf_engine.SetItemAttrText(itemtype => itemtype,     -- RDP if PRORATE,ADIITIONAL CHANGES , set the NOTIF_USAGE as BUYER_FYI
5735 		                          itemkey => itemkey,
5736 		                          aname => 'NOTIF_USAGE',
5737                                           avalue => 'BUYER_FYI');
5738 
5739 
5740 		if(l_header_change = 0) then                        -- RDP means no ADDITIONAL changes but PRORATE is required, set EXPLAIN_FYI
5741 		    wf_engine.SetItemAttrText (   itemtype   => itemtype,
5742 		                                  itemkey    => itemkey,
5743 		                                  aname      => 'EXPLAIN_FYI',
5744 		                                  avalue     => fnd_message.get_string('PO','PO_WF_NOTIF_EXPLAIN_FYI'));
5745 		else
5746 		    wf_engine.SetItemAttrText (   itemtype   => itemtype,
5747 		                                  itemkey    => itemkey,
5748 		                                  aname      => 'EXPLAIN_FYI',
5749 		                                  avalue     => '');
5750 		end if;
5751 
5752 	else
5753 		resultout := 'N';
5754 	    wf_engine.SetItemAttrText (   itemtype   => itemtype,
5755 	                                  itemkey    => itemkey,
5756 	                                  aname      => 'EXPLAIN_FYI',
5757 	                                  avalue     => '');
5758 
5759 	end if;
5760 exception when others then
5761 	wf_core.context('PO_ChangeOrderWF_PVT','IS_PRORATE_NEEDED',x_progress);
5762     raise;
5763 END IS_PRORATE_NEEDED;
5764 
5765 /*
5766 *update authorization_status of PO to "APPROVED".
5767 */
5768 procedure CHG_STATUS_TO_APPROVED(	  	itemtype        in varchar2,
5769 			                           	itemkey         in varchar2,
5770     	    		                   	actid           in number,
5771 	    	        		         	funcmode        in varchar2,
5772             	            		    resultout       out NOCOPY varchar2)
5773 IS
5774 l_header_id number;
5775 l_release_id number;
5776 l_revision_num number;
5777 l_chg_req_grp_id number;
5778 l_add_changes_accepted number;
5779 l_responded_by number;
5780 l_authorization_status po_headers_all.authorization_status%TYPE;
5781 l_check_result varchar2(1);
5782 
5783 x_progress varchar2(3) := '000';
5784 BEGIN
5785 
5786     l_header_id := wf_engine.GetItemAttrNumber(itemtype => itemtype,
5787                                             itemkey => itemkey,
5788                                             aname => 'PO_HEADER_ID');
5789     l_release_id := wf_engine.GetItemAttrNumber(itemtype => itemtype,
5790                                             itemkey => itemkey,
5791                                             aname => 'PO_RELEASE_ID');
5792     l_revision_num := wf_engine.GetItemAttrNumber(itemtype => itemtype,
5793                                             itemkey => itemkey,
5794                                             aname => 'PO_REVISION_NUM');
5795      l_chg_req_grp_id := wf_engine.GetItemAttrNumber(itemtype => itemtype,
5796                                             itemkey => itemkey,
5797                                             aname => 'CHANGE_REQUEST_GROUP_ID');
5798 
5799 
5800       l_authorization_status := 'APPROVED';
5801 
5802                 select count(*)
5803                 into l_add_changes_accepted
5804                 from po_change_requests
5805                 where change_request_group_id = l_chg_req_grp_id
5806                 -- and request_status = 'ACCEPTED'
5807                 and request_status = 'BUYER_APP'     /* fix for bug 3691061 */
5808                 and request_level = 'HEADER'
5809                 and additional_changes is not null;
5810 
5811                 if (l_add_changes_accepted > 0) then
5812                    l_authorization_status := 'REQUIRES REAPPROVAL';
5813                 end if;
5814 
5815       if (l_authorization_status = 'REQUIRES REAPPROVAL') then
5816 
5817         select max(responded_by) into l_responded_by
5818         from po_change_requests
5819         where request_level = 'HEADER'
5820         -- and request_status = 'ACCEPTED'
5821         and request_status = 'BUYER_APP'         /* fix for bug 3691061 */
5822         and additional_changes is not null
5823         and change_request_group_id = l_chg_req_grp_id;
5824 
5825         if (l_responded_by = 0) then
5826           l_responded_by :=  null;
5827         end if;
5828 
5829         if(l_release_id is not null) then
5830 
5831           update po_releases_all set
5832           authorization_status = 'REQUIRES REAPPROVAL',
5833           revision_num = revision_num + 1,
5834           revised_date = sysdate,
5835           last_update_date = sysdate,
5836           last_updated_by = nvl(l_responded_by, last_updated_by),
5837           change_requested_by = null,
5838           approved_flag = 'R'
5839           where po_release_id = l_release_id;
5840 
5841         else
5842 
5843           update po_headers_all set
5844           authorization_status = 'REQUIRES REAPPROVAL',
5845           revision_num = revision_num + 1,
5846           revised_date = sysdate,
5847           last_update_date = sysdate,
5848           last_updated_by = nvl(l_responded_by, last_updated_by),
5849           change_requested_by = null,
5850           approved_flag = 'R'
5851           where po_header_id = l_header_id;
5852 
5853         end if;
5854 
5855       else  -- authorization status is approved
5856 
5857       -- added due to bug 3574114
5858       update po_change_requests
5859       set change_active_flag = 'N'
5860       where change_request_group_id = l_chg_req_grp_id;
5861 
5862 	if(l_release_id is not null) then
5863 		update po_releases_all
5864 		set authorization_status = l_authorization_status,
5865 		change_requested_by = null
5866 		where po_release_id = l_release_id;
5867 	else
5868 		update po_headers_all
5869 		set authorization_status = l_authorization_status,
5870 		change_requested_by = null
5871 		where po_header_id = l_header_id;
5872 	end if;
5873 
5874 
5875 	x_progress := '002';
5876 
5877         /* Bug 3534807, mji
5878            Check if all shipments has been acknowledged, if yes post header
5879            acknowledgement record.
5880         */
5881         PO_ACKNOWLEDGE_PO_GRP.Set_Header_Acknowledgement (
5882     		1.0,
5883     		FND_API.G_FALSE,
5884 		l_check_result,
5885 		l_header_id,
5886 		l_release_id );
5887 
5888 	x_progress := '003';
5889 
5890 
5891       end if;
5892 
5893 exception when others then
5894 	wf_core.context('PO_ChangeOrderWF_PVT','CHG_STATUS_TO_APPROVED',x_progress);
5895     raise;
5896 END CHG_STATUS_TO_APPROVED;
5897 
5898 
5899 /*
5900 *Supplier could have changed and accepted shipments at the same time. Once the change requests are responded, we will need
5901 *to carry over the previously accepted shipments to the new revision, by Calling
5902 *PO_ACKNOWLEDGE_PO_GRP.carry_over_acknowledgement
5903 */
5904 procedure CARRY_SUP_ACK_TO_NEW_REV(	  	itemtype        in varchar2,
5905 			                           	itemkey         in varchar2,
5906     	    		                   	actid           in number,
5907 	    	        		         	funcmode        in varchar2,
5908             	            		    resultout       out NOCOPY varchar2)
5909 IS
5910 l_document_id number;
5911 l_document_type po_document_types_all.document_type_code%TYPE;
5912 l_header_id number;
5913 l_release_id number;
5914 l_revision_num number;
5915 x_progress varchar2(3) := '000';
5916 l_carryover_status varchar2(1);
5917 l_carryover_exception exception;
5918 BEGIN
5919 
5920     l_document_id := wf_engine.GetItemAttrNumber(itemtype => itemtype,
5921                                             itemkey => itemkey,
5922                                             aname => 'DOCUMENT_ID');
5923     l_document_type := wf_engine.GetItemAttrText(itemtype => itemtype,
5924                                             itemkey => itemkey,
5925                                             aname => 'DOCUMENT_TYPE');
5926 	x_progress := '001';
5927     if(l_document_type = 'RELEASE') then
5928     	l_header_id := null;
5929     	l_release_id := l_document_id;
5930     	select revision_num
5931     	into l_revision_num
5932     	from po_releases_all
5933     	where po_release_id = l_document_id;
5934     else
5935     	l_header_id := l_document_id;
5936     	l_release_id := null;
5937     	select revision_num
5938     	into l_revision_num
5939     	from po_headers_all
5940     	where po_header_id = l_document_id;
5941     end if;
5942     x_progress := '002';
5943 	PO_ACKNOWLEDGE_PO_GRP.carry_over_acknowledgement(1.0,
5944 													FND_API.G_FALSE,
5945 													l_carryover_status,
5946 													l_header_id ,
5947 													l_release_id ,
5948 													l_revision_num);
5949 	if(l_carryover_status <> FND_API.G_RET_STS_SUCCESS) then
5950 		x_progress := '003';
5951 		raise l_carryover_exception;
5952 	end if;
5953 exception when others then
5954 	wf_core.context('PO_ChangeOrderWF_PVT','CARRY_SUP_ACK_TO_NEW_REV',x_progress);
5955     raise;
5956 END CARRY_SUP_ACK_TO_NEW_REV;
5957 
5958 /*
5959 *Checks if acceptance_required_flag = 'Y'
5960 */
5961 procedure DOES_PO_REQ_SUP_ACK(	  	itemtype        in varchar2,
5962 		                           	itemkey         in varchar2,
5963         		                   	actid           in number,
5964 	            		         	funcmode        in varchar2,
5965                         		    resultout       out NOCOPY varchar2)
5966 IS
5967 l_document_id number;
5968 l_document_type po_document_types_all.document_type_code%TYPE;
5969 l_acc_flag varchar2(1);
5970 x_progress varchar2(3) := '000';
5971 BEGIN
5972     l_document_id := wf_engine.GetItemAttrNumber(itemtype => itemtype,
5973                                             itemkey => itemkey,
5974                                             aname => 'DOCUMENT_ID');
5975     l_document_type := wf_engine.GetItemAttrText(itemtype => itemtype,
5976                                             itemkey => itemkey,
5977                                             aname => 'DOCUMENT_TYPE');
5978 
5979 	if(l_document_type = 'RELEASE') then
5980 		select acceptance_required_flag
5981 		into l_acc_flag
5982 		from po_releases_all
5983 		where po_release_id = l_document_id;
5984 	else
5985 		select acceptance_required_flag
5986 		into l_acc_flag
5987 		from po_headers_all
5988 		where po_header_id = l_document_id;
5989 	end if;
5990 	if(l_acc_flag= 'Y') then
5991 		resultout := 'Y';
5992 	else
5993 		resultout := 'N';
5994 	END if;
5995 
5996 exception when others then
5997 	wf_core.context('PO_ChangeOrderWF_PVT','DOES_PO_REQ_SUP_ACK',x_progress);
5998     raise;
5999 END DOES_PO_REQ_SUP_ACK;
6000 
6001 /*
6002 *Checks if PO Change request is approved/rejected by the PO Approval Hierachy.
6003 *Meanwhile, prepare the notification which is to be sent to the supplier, informing him/her of buyer's response to
6004 *Supplier's Change request
6005 *Note:  Also sets the request_group_id value into the wf attribute
6006 *       which is useful for the later activities.
6007 */
6008 procedure is_po_approved_by_hie( itemtype  in varchar2,
6009                                  itemkey   in varchar2,
6010                                  actid     in number,
6011                                  funcmode  in varchar2,
6012                                  resultout out NOCOPY varchar2)
6013 IS
6014   l_document_id                number;
6015   p_header_id                  number;
6016   l_document_type              po_document_types_all.document_type_code%TYPE;
6017   l_authorization_status       po_headers_all.authorization_status%TYPE;
6018   l_acceptance_required_flag   po_headers_all.acceptance_required_flag%TYPE;   -- fix for bug 4946410
6019   l_supplier_user_id           number;
6020   l_change_request_group_id    number;
6021   l_supplier_username          fnd_user.user_name%TYPE;
6022   l_blanket_num                po_headers_all.segment1%TYPE;
6023   --l_release_num              number;
6024   l_release_num                po_releases_all.release_num%TYPE;
6025   l_buyer_name                 hr_all_organization_units_tl.name%TYPE;
6026   l_po_num                     po_headers_all.segment1%TYPE;
6027   l_revision_num               number;
6028   l_revision_num2              number;
6029   l_notification_subject       varchar2(2000);
6030   l_type_lookup_code           po_headers_all.type_lookup_code%TYPE;
6031   l_doc_hdr_info               varchar2(1000);
6032   l_buyer_agent_id             number;
6033   l_responded_by_id            number;
6034   l_buyer_username             fnd_user.user_name%TYPE;
6035   l_buyer_disp_name            varchar2(2000);
6036   l_po_style                   varchar2(10);
6037   x_return_status              varchar2(1);
6038   x_complex_flag               varchar2(1);
6039 
6040   -- Bug 6722239 - Start
6041   /*
6042   Added the following variables
6043   l_role_name          -> Adhoc Role Name Created
6044   l_vendor_id          -> Supplier Id
6045   l_role_display_name  -> Supplier_Name which need to be displayed in the Notification
6046                           sent to supplier for buyer's response
6047   l_expiration_date    -> Notification Expiry Date
6048   l_user_name          -> Supplier User Name who modified the purchase order
6049   l_users              -> Users list in the form of userTable to create an Adhoc role
6050   x_resultout          -> Variable to set the role in the Workflow file
6051   t_document_type      -> Temporary Variable used to store the document type
6052   */
6053 
6054   l_role_name                  WF_USER_ROLES.ROLE_NAME%TYPE;
6055   l_role_display_name          varchar2(100):=null;
6056   l_expiration_date            DATE;
6057   l_vendor_id                  NUMBER;
6058   x_resultout                  varchar2(100):=null;
6059   l_users                      WF_DIRECTORY.UserTable;
6060   l_user_name                  varchar2(100);
6061   t_document_type              po_document_types_all.document_type_code%TYPE := null;
6062   u1                           pls_integer := 0;
6063   --Bug 6722239 - End
6064 
6065   x_progress                   varchar2(100) := '000';
6066   t_varname                    Wf_Engine.NameTabTyp;
6067   t_varval                     Wf_Engine.TextTabTyp;
6068   l_global_agreement_flag      po_headers_all.GLOBAL_AGREEMENT_FLAG%TYPE := 'F';
6069   l_po_header_id               po_headers_all.po_header_id%TYPE;
6070   l_id                         number;
6071   l_header_cancel              varchar2(1);
6072 
6073   cursor l_header_cancel_csr(grp_id number) is
6074     select change_request_id
6075     from po_change_requests
6076     where change_request_group_id = grp_id
6077     and action_type = 'CANCELLATION'
6078     and request_level = 'HEADER';
6079 
6080   /*
6081   Collecting the accepted line location ids for which changes were made during
6082   Acknowledgement process.
6083   */
6084   l_line_loc_id                 number;
6085   l_user_id                     number;
6086 
6087   -- Bug 6722239 - Start
6088   /*
6089   Added the Following Cursor; This will get the list of supplier users
6090   who have done the changes to the purchase order during the process of
6091   PO Acknowledgement.
6092   */
6093   cursor l_get_user_list_csr(x_document_id NUMBER,x_document_type varchar2) is
6094     select distinct created_by ,
6095     change_request_group_id
6096     from po_change_requests
6097     where request_status in ('BUYER_APP', 'WAIT_CANCEL_APP')
6098     and ((document_header_id = x_document_id) OR (po_release_id = x_document_id))
6099     and initiator = 'SUPPLIER'
6100     and document_type = x_document_type;
6101   --Bug 6722239 - End
6102 
6103   CURSOR getLineLocID(l_chg_req_grp_id_csr IN NUMBER) IS
6104     SELECT document_line_location_id
6105     FROM PO_CHANGE_REQUESTS
6106     WHERE change_request_group_id = l_chg_req_grp_id_csr
6107     AND request_level = 'SHIPMENT'
6108     AND request_status = 'BUYER_APP'
6109     AND (NEW_AMOUNT is not null OR NEW_QUANTITY is not null);
6110 
6111   /* Curosr to get the change_responded by*/
6112   CURSOR getChangeRespBy(l_chg_req_grp_id_csr IN NUMBER) IS
6113     SELECT distinct fndu.employee_id
6114     FROM   po_change_requests pcr,
6115     fnd_user fndu
6116     WHERE  pcr.change_request_group_id = l_chg_req_grp_id_csr
6117     AND fndu.user_id = pcr.responded_by;
6118 
6119 BEGIN
6120   l_document_id := wf_engine.GetItemAttrNumber(itemtype => itemtype,
6121                                                itemkey => itemkey,
6122                                                aname => 'DOCUMENT_ID');
6123 
6124   l_document_type := wf_engine.GetItemAttrText(itemtype => itemtype,
6125                                                itemkey => itemkey,
6126                                                aname => 'DOCUMENT_TYPE');
6127 
6128   if(l_document_type = 'RELEASE') then
6129     -- Bug 6722239 - Start
6130     -- Added a cursor above to fetch multiple records
6131     /*
6132     select distinct created_by,
6133                     change_request_group_id
6134     into l_supplier_user_id ,
6135          l_change_request_group_id
6136     from po_change_requests
6137     where request_status in ('BUYER_APP', 'WAIT_CANCEL_APP')
6138     and po_release_id = l_document_id
6139     and initiator = 'SUPPLIER'
6140     and document_type = 'RELEASE';
6141     */
6142     -- Bug 6722239 - End
6143 
6144     select pha.segment1,
6145            pha.po_header_id,                                 --    RDP changes
6146            pra.release_num,
6147            pra.revision_num,                                 --    RDP changes
6148            hou.name,
6149            pra.authorization_status,
6150            pha.type_lookup_code,
6151            pra.agent_id,
6152            pha.vendor_id,
6153            pra.acceptance_required_flag                      -- bug 4868859
6154     into   l_blanket_num,
6155            l_po_header_id,
6156            l_release_num,
6157            l_revision_num,
6158            l_buyer_name ,
6159            l_authorization_status,
6160            l_type_lookup_code,
6161            l_buyer_agent_id,
6162            l_vendor_id,
6163            l_acceptance_required_flag
6164     from   po_headers_all pha,
6165            po_releases_all pra,
6166            hr_all_organization_units_tl hou
6167     where  pra.po_release_id = l_document_id
6168     and pha.po_header_id = pra.po_header_id
6169     and pha.org_id = hou.organization_id(+)
6170     and hou.language(+) = userenv('LANG');
6171 
6172     select revision_num
6173     into l_revision_num2
6174     from po_releases_archive_all
6175     where po_release_id = l_document_id
6176     and latest_external_flag='Y';
6177 
6178     l_header_cancel := 'N';
6179     if(l_change_request_group_id is not null) then
6180       open l_header_cancel_csr(l_change_request_group_id);
6181       fetch l_header_cancel_csr
6182       into l_id;
6183       close l_header_cancel_csr;
6184       if(l_id is not null) then
6185         l_header_cancel := 'Y';
6186       end if;
6187     end if;
6188 
6189     if(l_type_lookup_code = 'PLANNED') then
6190       l_doc_hdr_info := fnd_message.get_string('PO','PO_WF_NOTIF_SCH_REL')||' '||l_blanket_num||'-'||l_release_num;
6191     elsif(l_type_lookup_code = 'BLANKET') then
6192       l_doc_hdr_info := fnd_message.get_string('PO','PO_WF_NOTIF_BKT_REL')||' '||l_blanket_num||'-'||l_release_num;
6193     end if;
6194 
6195     if(l_header_cancel = 'N') then
6196       fnd_message.set_name('PO','PO_WF_NOTIF_RESP_SUB_CHG');
6197       fnd_message.set_token('DOC',l_doc_hdr_info);
6198       fnd_message.set_token('BUYER',l_buyer_name);
6199       l_notification_subject := fnd_message.get;
6200     else
6201       fnd_message.set_name('PO','PO_WF_NOTIF_RESP_SUB_CAN');
6202       fnd_message.set_token('DOC',l_doc_hdr_info);
6203       fnd_message.set_token('BUYER',l_buyer_name);
6204       l_notification_subject := fnd_message.get;
6205     end if;
6206 
6207     /*
6208     update po_releases_all
6209     set change_requested_by = null
6210     where po_release_id = l_document_id;
6211     */
6212 
6213     Update_Chg_Req_If_Po_Apprvd(null,l_document_id);
6214 
6215     PO_WF_UTIL_PKG.SetItemAttrNumber (itemtype => itemtype,
6216                                       itemkey  => itemkey,
6217                                       aname    => 'PO_RELEASE_ID',
6218                                       avalue     => l_document_id);
6219 
6220     PO_WF_UTIL_PKG.SetItemAttrNumber (itemtype => itemtype,
6221                                       itemkey  => itemkey,
6222                                       aname    => 'PO_REVISION_NUM',
6223                                       avalue   => l_revision_num2);
6224 
6225      PO_WF_UTIL_PKG.SetItemAttrNumber (itemtype => itemtype,
6226                                        itemkey  => itemkey,
6227                                        aname    => 'PO_HEADER_ID',
6228                                        avalue     => l_po_header_id);
6229 
6230    else
6231      -- Bug 6722239 - Start
6232      -- Added a cursor above to fetch multiple records
6233      /*
6234      select distinct created_by ,
6235                      change_request_group_id
6236      into l_supplier_user_id  ,
6237           l_change_request_group_id
6238      from po_change_requests
6239      where request_status in ('BUYER_APP', 'WAIT_CANCEL_APP')
6240      and document_header_id = l_document_id
6241      and initiator = 'SUPPLIER'
6242      and document_type = 'PO';
6243      */
6244      -- Bug 6722239 - End
6245 
6246      select pha.segment1,
6247             hou.name,
6248             pha.authorization_status,
6249             pha.revision_num,
6250             pha.type_lookup_code,
6251             pha.agent_id,
6252             pha.GLOBAL_AGREEMENT_FLAG,
6253             pha.vendor_id,
6254             pha.acceptance_required_flag
6255       into  l_po_num,
6256             l_buyer_name,
6257             l_authorization_status,
6258             l_revision_num,
6259             l_type_lookup_code,
6260             l_buyer_agent_id,
6261             l_global_agreement_flag,
6262             l_vendor_id,
6263             l_acceptance_required_flag
6264       from  po_headers_all pha,
6265             hr_all_organization_units_tl hou
6266       where pha.po_header_id = l_document_id
6267       and pha.org_id = hou.organization_id(+)
6268       and hou.language(+) = userenv('LANG');
6269 
6270       select revision_num
6271       into l_revision_num2
6272       from po_headers_archive_all
6273       where po_header_id=l_document_id
6274       and latest_external_flag='Y';
6275 
6276       l_header_cancel := 'N';
6277       if(l_change_request_group_id is not null) then
6278         open l_header_cancel_csr(l_change_request_group_id);
6279         fetch l_header_cancel_csr
6280         into l_id;
6281         close l_header_cancel_csr;
6282         if(l_id is not null) then
6283           l_header_cancel := 'Y';
6284         end if;
6285       end if;
6286 
6287       if(l_type_lookup_code = 'STANDARD') then
6288         l_doc_hdr_info := fnd_message.get_string('PO','PO_WF_NOTIF_STD_PO')||' '||l_po_num;
6289       elsif (l_global_agreement_flag = 'Y' and l_type_lookup_code = 'BLANKET') then
6290         l_document_type := fnd_message.get_string('PO','PO_GA_TYPE') ||' '||l_po_num;
6291       elsif(l_type_lookup_code = 'BLANKET') then
6292         l_doc_hdr_info := fnd_message.get_string('PO','PO_WF_NOTIF_BLANKET')||' '||l_po_num;
6293       elsif(l_type_lookup_code = 'CONTRACT') then
6294         l_doc_hdr_info := fnd_message.get_string('PO','PO_POTYPE_CNTR')||' '||l_po_num;
6295       elsif(l_type_lookup_code = 'PLANNED') then
6296         l_doc_hdr_info := fnd_message.get_string('PO','PO_WF_NOTIF_PLAN_PO')||' '||l_po_num;
6297       end if;
6298 
6299       if(l_header_cancel = 'N') then
6300         fnd_message.set_name('PO','PO_WF_NOTIF_RESP_SUB_CHG');
6301         fnd_message.set_token('DOC',l_doc_hdr_info);
6302         fnd_message.set_token('BUYER',l_buyer_name);
6303         l_notification_subject := fnd_message.get;
6304       else
6305         fnd_message.set_name('PO','PO_WF_NOTIF_RESP_SUB_CAN');
6306         fnd_message.set_token('DOC',l_doc_hdr_info);
6307         fnd_message.set_token('BUYER',l_buyer_name);
6308         l_notification_subject := fnd_message.get;
6309       end if;
6310 
6311       /*
6312       update po_headers_all
6313       set change_requested_by = null
6314       where po_header_id = l_document_id;
6315       */
6316 
6317       Update_Chg_Req_If_Po_Apprvd(l_document_id,null);
6318 
6319       PO_WF_UTIL_PKG.SetItemAttrNumber (itemtype => itemtype,
6320                                         itemkey  => itemkey,
6321                                         aname    => 'PO_HEADER_ID',
6322                                         avalue => l_document_id);
6323 
6324        PO_WF_UTIL_PKG.SetItemAttrNumber (itemtype => itemtype,
6325                                          itemkey  => itemkey,
6326                                          aname    => 'PO_REVISION_NUM',
6327                                          avalue   => l_revision_num2);
6328 
6329      end if;  --end of if on l_doc_type
6330 
6331      -- Bug 6722239 - Start
6332      /* Added a cursor to fetch multiple records. Previous code was not capable of holding more than one record if
6333      fetched FROM database. Because of the cursor multiple records can be stored from which supplier user names
6334      will be fetched. So, the notifications will be send to all the supplier users who have done the modification
6335      to the PO during PO Acknowledgement process.*/
6336 
6337      if(l_document_type = 'RELEASE') THEN
6338        t_document_type := 'RELEASE';
6339      ELSE
6340        t_document_type := 'PO';
6341      END IF;
6342 
6343      open l_get_user_list_csr(l_document_id,t_document_type);
6344        loop
6345          fetch l_get_user_list_csr INTO
6346            l_supplier_user_id  ,
6347            l_change_request_group_id;
6348          exit when l_get_user_list_csr%NOTFOUND;
6349          select user_name
6350          INTO l_user_name
6351          from fnd_user
6352          where user_id = l_supplier_user_id;
6353          l_users(u1) := l_user_name;
6354          u1 := u1 + 1;
6355        end loop;
6356      close l_get_user_list_csr;
6357      -- Bug 6722239 - End
6358 
6359      x_progress := '001';
6360 
6361      PO_WF_UTIL_PKG.SetItemAttrNumber (itemtype => itemtype,
6362                                        itemkey  => itemkey,
6363                                        aname    => 'CHANGE_REQUEST_GROUP_ID',
6364                                        avalue     => l_change_request_group_id);
6365 
6366      -- fix for bug 4946410 added signature flag check( these po's are blocked and changed to 'APPROVED' after background process is run
6367      if((l_authorization_status = 'APPROVED') OR (l_authorization_status = 'PRE-APPROVED' AND l_acceptance_required_flag = 'S')) then
6368        resultout := 'Y';
6369        l_user_id := FND_GLOBAL.USER_ID;
6370        -- For those records which has been approved default the promised date
6371        if(l_authorization_status = 'APPROVED' AND l_acceptance_required_flag = 'Y' AND g_default_promise_date  = 'Y') THEN
6372          OPEN getLineLocID(l_change_request_group_id);
6373            LOOP
6374              FETCH getLineLocID INTO l_line_loc_id;
6375              EXIT WHEN getLineLocID%NOTFOUND;
6376              IF(l_document_type = 'RELEASE') THEN
6377                POS_ACK_PO.Acknowledge_promise_date(l_line_loc_id,l_po_header_id,l_document_id,l_revision_num,l_user_id);
6378              ELSE
6379                POS_ACK_PO.Acknowledge_promise_date(l_line_loc_id,l_document_id,null,l_revision_num,l_user_id);
6380              END IF;
6381            END LOOP;
6382          CLOSE getLineLocID;
6383        end if;
6384 
6385        update po_change_requests
6386        set request_status = 'ACCEPTED'
6387        where change_request_group_id = l_change_request_group_id
6388        and request_status in ('BUYER_APP', 'WAIT_CANCEL_APP');
6389 
6390        /* added code for bug 3574114 - update change active flag due to final buyer response */
6391 
6392        update po_change_requests
6393        set change_active_flag = 'N'
6394        where change_request_group_id = l_change_request_group_id;
6395 
6396      else
6397        resultout := 'N';
6398 
6399        update po_change_requests
6400        set request_status = 'REJECTED',
6401        response_reason = null
6402        where change_request_group_id = l_change_request_group_id
6403        and request_status in ('BUYER_APP', 'WAIT_CANCEL_APP');
6404 
6405        /* added code for bug 3574114 - update change active flag due to final buyer response */
6406 
6407        update po_change_requests
6408        set change_active_flag = 'N'
6409        where change_request_group_id = l_change_request_group_id;
6410 
6411      END if;
6412 
6413 
6414      x_progress := '002';
6415      -- Bug 6722239 - Start
6416      -- Commented the below code because the new changes will send
6417      -- notification to the role instead of a single supplier.
6418 
6419      /*
6420      select user_name
6421      into l_supplier_username
6422      from fnd_user
6423      where user_id = l_supplier_user_id;
6424      */
6425 
6426      /*Following code creates and Adhoc role for all the supplier users who have done
6427      some modification to the Purchase order during PO Acknowledgement process.
6428      l_role_name         -> Adhoc role name created for the above mentioned users
6429      l_role_display_name -> Name which need to be mentioned in the Notification
6430      l_expiration_date   -> Expiry date of the notification
6431      CreateAdHocRole2    -> Takes the above parameters as input and creates an Adhoc role.
6432      x_resultout         -> Variable used to set the Performer in the Workflow.
6433      */
6434 
6435      l_role_name := substr('ADHOC_SUP_CHG_RESP' || to_char(sysdate, 'JSSSSS')|| l_document_id || l_document_type, 1, 50);
6436 
6437      select vendor_name
6438      into l_role_display_name
6439      from po_vendors
6440      where vendor_id=l_vendor_id;
6441 
6442      if (l_document_type in ('PO', 'PA')) then
6443        select max(need_by_date)+180
6444        into l_expiration_date
6445        from po_line_locations
6446        where po_header_id = to_number(l_document_id)
6447        and cancel_flag = 'N';
6448 
6449        if l_expiration_date <= sysdate then
6450          l_expiration_date := sysdate + 180;
6451        end if;
6452      elsif (l_document_type = 'RELEASE') then
6453        select max(need_by_date)+180
6454        into l_expiration_date
6455        from po_line_locations
6456        where po_release_id = to_number(l_document_id)
6457        and cancel_flag = 'N';
6458 
6459        if l_expiration_date <= sysdate then
6460          l_expiration_date := sysdate + 180;
6461        end if;
6462      else
6463        l_expiration_date:=null;
6464      end if;
6465 
6466      WF_DIRECTORY.CreateAdHocRole2(l_role_name,
6467                                    l_role_display_name,
6468                                    null,
6469                                    null,
6470                                    null,
6471                                    'MAILHTML',
6472                                    l_users,
6473                                    null,
6474                                    null,
6475                                    'ACTIVE',
6476                                    l_expiration_date);
6477      x_resultout:=l_role_name;
6478      -- Bug 6722239 - End
6479 
6480      /* Bug 4949617 get the reponded_by id from po_change_requests table */
6481      OPEN getChangeRespBy(l_change_request_group_id);
6482        LOOP
6483          FETCH getChangeRespBy INTO l_responded_by_id;
6484          EXIT WHEN getChangeRespBy%NOTFOUND;
6485        END LOOP;
6486      CLOSE getChangeRespBy;
6487 
6488      IF(l_responded_by_id = l_buyer_agent_id) THEN
6489        wf_directory.GetUserName ( p_orig_system    => 'PER',
6490                                   p_orig_system_id => l_buyer_agent_id,
6491                                   p_name           => l_buyer_username,
6492                                   p_display_name   => l_buyer_disp_name);
6493      ELSE
6494        wf_directory.GetUserName ( p_orig_system    => 'PER',
6495                                   p_orig_system_id => l_responded_by_id,
6496                                   p_name           => l_buyer_username,
6497                                   p_display_name   => l_buyer_disp_name);
6498      END IF;
6499 
6500      if(l_document_type = 'RELEASE' OR l_document_type = 'PA') THEN
6501        l_po_style := 'NORMAL';
6502      elsif(l_document_type = 'PO') THEN
6503        p_header_id := l_document_id;
6504 
6505        /*
6506        PO- API  set item attribute PO_STYLE_TYPE depending upon the type of the PO
6507        PO_STYLE_TYPE - COMPLEX ,complex work PO's
6508        PO_STYLE_TYPE - NORMAL
6509        */
6510 
6511        PO_COMPLEX_WORK_GRP.is_complex_work_po(1.0,
6512                                               p_header_id,
6513                                               x_return_status,
6514                                               x_complex_flag);
6515 
6516        IF x_return_status IS NOT NULL AND  x_return_status = FND_API.g_ret_sts_success THEN
6517          IF( g_fnd_debug = 'Y' AND FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6518            FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
6519                           g_module_prefix,
6520                           x_progress || 'x_return_status=' || x_return_status || 'x_complex_flag ' || x_complex_flag);
6521          END IF;
6522 
6523        ELSE
6524          IF( g_fnd_debug = 'Y' AND FND_LOG.LEVEL_ERROR >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6525            FND_LOG.STRING(FND_LOG.LEVEL_ERROR,
6526                           g_module_prefix,
6527                           x_progress ||'x_return_status = ' || x_return_status);
6528          END IF;
6529        END IF;
6530 
6531        IF (x_complex_flag = 'N') THEN
6532          l_po_style :='NORMAL';
6533        ELSE
6534          l_po_style := 'COMPLEX';
6535        END IF;
6536      end if;
6537 
6538      t_varname(1) := 'NTF_FOR_SUP_SUBJECT';
6539      t_varval(1)  := 'PLSQL:PO_ChangeOrderWF_PVT.GEN_NTF_FOR_SUP_SUBJECT/'||itemkey;
6540      t_varname(2) := 'NTF_FOR_SUP_BUY_RP';
6541      -- RDP set the item Attribute to point to the notifications page
6542      t_varval(2)  := 'JSP:/OA_HTML/OA.jsp?page=/oracle/apps/pos/notifications/webui/PosNotificationsRN'||
6543      '&L_CHG_REQ_GRP_ID=-&CHANGE_REQUEST_GROUP_ID-&L_PO_HEADER_ID=-&PO_HEADER_ID-&L_PO_RELEASE_ID=-&PO_RELEASE_ID-'||
6544      '&L_PO_ACC_REQ_FLAG=-&ACKNOWLEDGE_REQ_FLAG-&L_NOTIF_USAGE=-&NOTIF_USAGE-&L_EXPLAIN_FYI=-&EXPLAIN_FYI-&L_STYLE_PARAM=-&PO_STYLE_TYPE-';
6545      t_varname(3) := 'SUPPLIER_USERNAME';
6546      --t_varval(3)  := l_supplier_username;
6547      t_varval(3)  := x_resultout;
6548      t_varname(4) := 'FROM_BUYER';
6549      t_varval(4)  := l_buyer_username;
6550      t_varname(5)  := 'NOTIF_USAGE';
6551      t_varval(5)  := 'BUYER_RESP';
6552      t_varname(6)  := 'PO_STYLE_TYPE';
6553      t_varval(6)   := l_po_style;
6554 
6555      Wf_Engine.SetItemAttrTextArray(itemtype, itemkey,t_varname,t_varval);
6556 
6557 exception when others then
6558   wf_core.context('PO_ChangeOrderWF_PVT','is_po_approved_by_hie',x_progress);
6559   raise;
6560 END is_po_approved_by_hie;
6561 
6562 
6563 procedure set_data_sup_chn_evt(	  	itemtype        in varchar2,
6564 		                           	itemkey         in varchar2,
6565         		                   	actid           in number,
6566 	            		         	funcmode        in varchar2,
6567                         		    resultout       out NOCOPY varchar2)
6568 IS
6569 l_document_id number;
6570 l_document_type po_document_types_all.document_type_code%TYPE;
6571 l_seq number;
6572 l_item_key varchar2(2000);
6573 x_progress varchar2(3) := '000';
6574 BEGIN
6575     l_document_id := wf_engine.GetItemAttrNumber(itemtype => itemtype,
6576                                             itemkey => itemkey,
6577                                             aname => 'DOCUMENT_ID');
6578     l_document_type := wf_engine.GetItemAttrText(itemtype => itemtype,
6579                                             itemkey => itemkey,
6580                                             aname => 'DOCUMENT_TYPE');
6581 
6582 
6583 
6584 	select PO_SUPPLIER_CHANGE_WF_S.nextval into l_seq from dual;
6585 
6586 	if(l_document_type = 'RELEASE') then
6587 		l_item_key := 'SC_REL'||'-'||l_document_id||'-'||l_seq;
6588 	else
6589 		l_item_key := 'SC_PO'||'-'||l_document_id||'-'||l_seq;
6590 	end if;
6591 
6592 
6593     wf_engine.SetItemAttrText (   itemtype   => itemtype,
6594                                   itemkey    => itemkey,
6595                                   aname      => 'SUP_CHANGE_EVENT_KEY',
6596                                   avalue     => l_item_key );
6597 
6598 
6599 exception when others then
6600 	wf_core.context('PO_ChangeOrderWF_PVT','set_data_sup_chn_evt',x_progress);
6601     raise;
6602 END set_data_sup_chn_evt;
6603 
6604 
6605 procedure ANY_NEW_SUP_CHN(	  		itemtype        in varchar2,
6606 		                           	itemkey         in varchar2,
6607         		                   	actid           in number,
6608 	            		         	funcmode        in varchar2,
6609                         		    resultout       out NOCOPY varchar2)
6610 IS
6611 l_chg_req_grp_id number;
6612 l_header_id number;
6613 l_release_id number;
6614 l_doc_id number;
6615 l_doc_type po_document_types_all.document_type_code%TYPE;
6616 l_doc_subtype po_document_types_all.document_subtype%TYPE;
6617 l_employee_id number;
6618 l_acc_req_flag VARCHAR2(10);
6619 
6620 x_progress varchar2(3) := '000';
6621 cursor l_change_csr(grp_id number) is
6622 select change_request_id from
6623 po_change_requests
6624 where action_type = 'MODIFICATION'
6625 and change_request_group_id = grp_id;
6626 
6627 BEGIN
6628 	l_header_id := wf_engine.GetItemAttrNumber(itemtype => itemtype,
6629                                             itemkey => itemkey,
6630                                             aname => 'PO_HEADER_ID');
6631 	l_release_id := wf_engine.GetItemAttrNumber(itemtype => itemtype,
6632                                             itemkey => itemkey,
6633                                             aname => 'PO_RELEASE_ID');
6634 
6635         l_chg_req_grp_id := wf_engine.GetItemAttrNumber(itemtype => itemtype,
6636                                             itemkey => itemkey,
6637                                             aname => 'CHANGE_REQUEST_GROUP_ID');
6638         l_acc_req_flag  :=  wf_engine.GetItemAttrText( itemtype => itemtype,
6639                                                       itemkey => itemkey,
6640                                                       aname => 'ACKNOWLEDGE_REQ_FLAG');
6641 
6642 
6643 	if(l_release_id is null) then
6644 		l_doc_id := l_header_id;
6645 		select
6646 			type_lookup_code,
6647 			agent_id
6648 		into
6649 			l_doc_subtype,
6650 			l_employee_id
6651 		from po_headers_all
6652 		where po_header_id = l_header_id;
6653 
6654 		if(l_doc_subtype = 'BLANKET' or l_doc_subtype = 'CONTRACT') then
6655 			l_doc_type := 'PA';
6656 		elsif(l_doc_subtype in ('PLANNED','STANDARD')) then
6657 			l_doc_type := 'PO';
6658 		end if;
6659 	else
6660 		l_doc_id := l_release_id;
6661 		l_doc_type := 'RELEASE';
6662 		select
6663 			pha.type_lookup_code,
6664 			pra.agent_id
6665 		into
6666 			l_doc_subtype,
6667 			l_employee_id
6668 		from po_headers_all pha, po_releases_all pra
6669 		where pra.po_release_id = l_release_id
6670 		and pra.po_header_id = pha.po_header_id;
6671 	end if;
6672 	x_progress := '001';
6673     if(l_chg_req_grp_id is null) then
6674     	resultout := 'N';
6675 	    wf_engine.SetItemAttrText (   itemtype   => itemtype,
6676 	                                  itemkey    => itemkey,
6677 	                                  aname      => 'EXPLAIN_FYI',
6678 	                                  avalue     => '');
6679 
6680 	if(l_acc_req_flag = 'Y') then                                     -- RDP no changes just Acknowledged ( Set the NOTIF_USAGE as BUYER_ACK)
6681              wf_engine.SetItemAttrText (  itemtype   => itemtype,
6682                                           itemkey    => itemkey,
6683                                           aname      => 'NOTIF_USAGE',
6684                                           avalue     => 'BUYER_ACK');
6685         end if;
6686     else
6687     	resultout := 'Y';
6688 		InsertActionHist(
6689 				p_doc_id => l_doc_id,
6690 				p_doc_type => l_doc_type,
6691 				p_doc_subtype => l_doc_subtype,
6692 				p_employee_id => null,
6693 				p_action => 'SUBMIT CHANGE',
6694 				p_note => null,
6695 				p_path_id => null);
6696     end if;
6697 exception when others then
6698 	wf_core.context('PO_ChangeOrderWF_PVT','ANY_NEW_SUP_CHN',x_progress);
6699     raise;
6700 END ANY_NEW_SUP_CHN;
6701 
6702 procedure any_supplier_change(	  	itemtype        in varchar2,
6703 		                           	itemkey         in varchar2,
6704         		                   	actid           in number,
6705 	            		         	funcmode        in varchar2,
6706                         		    resultout       out NOCOPY varchar2)
6707 IS
6708 l_document_id number;
6709 l_document_type po_document_types_all.document_type_code%TYPE;
6710 l_count number;
6711 l_cancel_app_count number;   /* fix for bug 3864512 */
6712 x_progress varchar2(100) := '000';
6713 l_change_request_group_id  number := null;
6714 BEGIN
6715 
6716     l_document_id := wf_engine.GetItemAttrNumber(itemtype => itemtype,
6717                                             itemkey => itemkey,
6718                                             aname => 'DOCUMENT_ID');
6719     l_document_type := wf_engine.GetItemAttrText(itemtype => itemtype,
6720                                             itemkey => itemkey,
6721                                             aname => 'DOCUMENT_TYPE');
6722 
6723 
6724     if(l_document_type = 'RELEASE') then
6725 		select count(1)
6726 		into l_count
6727 		from po_change_requests
6728 		where initiator = 'SUPPLIER'
6729 		and document_type = 'RELEASE'
6730 		and po_release_id = l_document_id
6731 		and request_status = 'BUYER_APP';
6732 
6733                 select count(1)
6734                 into l_cancel_app_count
6735                 from po_change_requests
6736                 where initiator = 'SUPPLIER'
6737                 and document_type = 'RELEASE'
6738                 and po_release_id = l_document_id
6739                 and request_status = 'WAIT_CANCEL_APP';
6740 
6741 		if ((l_count > 0) OR (l_cancel_app_count > 0)) then
6742 		  -- no chance for no_data_found exception.
6743 		  select distinct(change_request_group_id)
6744 		                  into l_change_request_group_id
6745 		                  from po_change_requests
6746 		  where initiator = 'SUPPLIER'
6747 		  and document_type = 'RELEASE'
6748 		  and po_release_id = l_document_id
6749 		  and request_status in ('BUYER_APP', 'WAIT_CANCEL_APP');
6750 		end if;
6751 
6752                 -- need to update the processed cancel shipment record to ACCEPTED
6753                 -- so as to prevent it from blocking processing of other records
6754 
6755                 if (l_count > 0 AND l_cancel_app_count > 0) then
6756 
6757                   update po_change_requests set request_status = 'ACCEPTED'
6758                   where initiator = 'SUPPLIER'
6759                   and document_type = 'RELEASE'
6760                   and po_release_id = l_document_id
6761                   and request_status = 'WAIT_CANCEL_APP';
6762 
6763                 end if;
6764 
6765 	else
6766 		select count(1)
6767 		into l_count
6768 		from po_change_requests
6769 		where initiator = 'SUPPLIER'
6770 		and document_type = 'PO'
6771 		and document_header_id = l_document_id
6772 		and request_status = 'BUYER_APP';
6773 
6774                 select count(1)
6775                 into l_cancel_app_count
6776                 from po_change_requests
6777                 where initiator = 'SUPPLIER'
6778                 and document_type = 'PO'
6779                 and document_header_id = l_document_id
6780                 and request_status = 'WAIT_CANCEL_APP';
6781 
6782                 if ((l_count > 0) OR (l_cancel_app_count > 0)) then
6783 		  -- no chance for no_data_found exception.
6784 		  select distinct(change_request_group_id)
6785 		                  into l_change_request_group_id
6786 		                  from po_change_requests
6787 		  where initiator = 'SUPPLIER'
6788 		  and document_type = 'PO'
6789 		  and document_header_id = l_document_id
6790 		  and request_status in ('BUYER_APP', 'WAIT_CANCEL_APP');
6791 	       end if;
6792 
6793                -- need to update the processed cancel shipment record to ACCEPTED
6794                -- so as to prevent it from blocking processing of other records
6795 
6796                if (l_count > 0 AND l_cancel_app_count > 0) then
6797 
6798                   update po_change_requests set request_status = 'ACCEPTED'
6799                   where initiator = 'SUPPLIER'
6800                   and document_type = 'PO'
6801                   and document_header_id = l_document_id
6802                   and request_status = 'WAIT_CANCEL_APP';
6803 
6804                end if;
6805 
6806 	end if;
6807 
6808 	begin
6809           PO_WF_UTIL_PKG.SetItemAttrNumber (itemtype => itemtype,
6810 	                             itemkey  => itemkey,
6811 	                             aname    => 'CHANGE_REQUEST_GROUP_ID',
6812 	                             avalue     => l_change_request_group_id);
6813 	exception when others then
6814 	  null;
6815 	end;
6816 
6817 	if ((l_count > 0 AND l_cancel_app_count = 0) OR (l_count = 0 AND l_cancel_app_count > 0)) then
6818           resultout := 'Y';
6819         else
6820 	  resultout := 'N';
6821 	end if;
6822 
6823 exception when no_data_found then
6824 	wf_core.context('PO_ChangeOrderWF_PVT','any_supplier_change','010');
6825 	raise;
6826 when others then
6827 	wf_core.context('PO_ChangeOrderWF_PVT','any_supplier_change',x_progress);
6828     raise;
6829 END any_supplier_change;
6830 
6831 
6832 
6833 procedure NotifySupAllChgRpdWF( 	p_header_id in number,
6834 									p_release_id in number,
6835 									p_revision_num in number,
6836 									p_chg_req_grp_id in number)
6837 IS
6838 l_api_name varchar2(50) := 'NotifySupAllChgRpdWF';
6839 l_progress varchar2(100) := '000';
6840 l_item_key 					varchar2(2000);
6841 l_seq 						number;
6842 l_supplier_username 		fnd_user.user_name%TYPE;
6843 l_notification_subject 		varchar2(2000);
6844 
6845 --l_po_num 					po_headers_all.segment1%TYPE;
6846 --l_blanket_num 				po_headers_all.segment1%TYPE;
6847 --l_release_num 				number;
6848 --l_buyer_name 				hr_all_organization_units_tl.name%TYPE;
6849 --l_type_lookup_code 			po_headers_all.type_lookup_code%TYPE;
6850 --l_document_type 			varchar2(2000);
6851 l_buyer_agent_id number;
6852 l_responded_by_id number;
6853 l_buyer_username fnd_user.user_name%TYPE;
6854 l_buyer_disp_name varchar2(2000);
6855 --l_doc_type po_document_types_all.document_type_code%TYPE;
6856 l_document_id number;
6857 l_po_style varchar2(10);
6858 x_return_status  varchar2(1);
6859 x_complex_flag   varchar2(1);
6860 
6861 n_varname   Wf_Engine.NameTabTyp;
6862 n_varval    Wf_Engine.NumTabTyp;
6863 t_varname   Wf_Engine.NameTabTyp;
6864 t_varval    Wf_Engine.TextTabTyp;
6865 
6866 
6867 --l_id number;
6868 --l_header_cancel varchar2(1);
6869 --cursor l_header_cancel_csr(grp_id number) is
6870 --select change_request_id
6871 --from po_change_requests
6872 --where change_request_group_id = grp_id
6873 --and action_type = 'CANCELLATION'
6874 --and request_level = 'HEADER';
6875 --l_doc_hdr_info varchar2(2000);
6876 l_supplier_user_id number;
6877 
6878 /* Curosr to get the change_responded by*/
6879 CURSOR getChangeRespBy(l_chg_req_grp_id_csr IN NUMBER) IS
6880 SELECT distinct fndu.employee_id
6881 FROM   po_change_requests pcr,
6882        fnd_user fndu
6883 WHERE  pcr.change_request_group_id = l_chg_req_grp_id_csr
6884        AND fndu.user_id = pcr.responded_by;
6885 
6886 
6887 BEGIN
6888 	select PO_SUPPLIER_CHANGE_WF_S.nextval into l_seq from dual;
6889 	if(p_release_id is not null) then
6890 		l_document_id := p_release_id;
6891 		l_item_key := 'NSCR'||'-'||to_char(p_release_id)||'-'||to_char(p_revision_num)||'-'||to_char(l_seq);
6892 	else
6893 		l_document_id := p_header_id;
6894 		l_item_key := 'NSCR'||'-'||to_char(p_header_id)||'-'||to_char(p_revision_num)||'-'||to_char(l_seq);
6895 	end if;
6896 
6897 	wf_engine.createProcess (	ItemType => 'POSCHORD',
6898 								ItemKey => l_item_key,
6899 								Process => 'CHG_RESPONDED_FYI_TO_SUPPLIER');
6900 
6901 	l_progress := '001';
6902 	if(p_release_id is null) then
6903 		select pha.agent_id
6904 		into l_buyer_agent_id
6905 		from po_headers_all pha
6906 		where pha.po_header_id = p_header_id;
6907 	else
6908 		select pra.agent_id
6909 		into l_buyer_agent_id
6910 		from po_releases_all pra
6911 		where pra.po_release_id = p_release_id;
6912 	end if;
6913 
6914 	/* Commenting out for bug 3484201. Since change has already been requested by a
6915 	specific user, the notification needs to be sent to the user on the change request */
6916 
6917 	--PO_REQAPPROVAL_INIT1.locate_notifier(l_document_id, l_doc_type, l_supplier_username);
6918 
6919 	if(l_supplier_username is null) then
6920 		select max(created_by)
6921 		into l_supplier_user_id
6922 		from po_change_requests
6923 		where change_request_group_id = p_chg_req_grp_id;
6924 
6925 		select user_name
6926 		into l_supplier_username
6927 		from fnd_user
6928 		where user_id = l_supplier_user_id;
6929 	end if;
6930 
6931      /* Bug 4949617 get the reponded_by id from po_change_requests table */
6932        OPEN getChangeRespBy(p_chg_req_grp_id);
6933        LOOP
6934        FETCH getChangeRespBy INTO l_responded_by_id;
6935        EXIT WHEN getChangeRespBy%NOTFOUND;
6936        END LOOP;
6937        CLOSE getChangeRespBy;
6938 
6939        IF(l_responded_by_id = l_buyer_agent_id) THEN
6940 
6941 	wf_directory.GetUserName ( p_orig_system    => 'PER',
6942                                   p_orig_system_id => l_buyer_agent_id,
6943                                   p_name           => l_buyer_username,
6944                                   p_display_name   => l_buyer_disp_name);
6945         ELSE
6946 	  wf_directory.GetUserName ( p_orig_system    => 'PER',
6947                                   p_orig_system_id => l_responded_by_id,
6948                                   p_name           => l_buyer_username,
6949                                   p_display_name   => l_buyer_disp_name);
6950         END IF;
6951 
6952 
6953      l_progress := '002: call is_complex_po';          -- RDP Changes
6954 
6955          /*   PO- API  set item attribute PO_STYLE_TYPE depending upon the type of the PO
6956         PO_STYLE_TYPE - COMPLEX ,complex work PO's
6957         PO_STYLE_TYPE - NORMAL            */
6958 
6959  IF( p_release_id is not null) THEN
6960    l_po_style := 'NORMAL';
6961 
6962  ELSIF( p_release_id is null) THEN
6963   PO_COMPLEX_WORK_GRP.is_complex_work_po(
6964                           1.0,
6965                           p_header_id,
6966                           x_return_status,
6967                          x_complex_flag);
6968 
6969    IF x_return_status IS NOT NULL AND  x_return_status = FND_API.g_ret_sts_success THEN
6970      IF( g_fnd_debug = 'Y' AND FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6971         FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
6972                        g_module_prefix,
6973                        l_progress
6974                        || 'x_return_status=' || x_return_status
6975                        || 'x_complex_flag ' || x_complex_flag);
6976      END IF;
6977 
6978    ELSE
6979      IF( g_fnd_debug = 'Y' AND FND_LOG.LEVEL_ERROR >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6980        FND_LOG.STRING(FND_LOG.LEVEL_ERROR,
6981                        g_module_prefix,
6982                        l_progress
6983                        ||'x_return_status = ' || x_return_status);
6984      END IF;
6985    END IF;
6986 
6987   IF(x_complex_flag = 'N') THEN
6988   l_po_style :='NORMAL';
6989   ELSE
6990   l_po_style := 'COMPLEX';
6991   END IF;
6992 
6993   END IF;
6994 
6995 
6996     n_varname(1) := 'PO_HEADER_ID';
6997 	n_varval(1)  := p_header_id;
6998 	n_varname(2) := 'PO_RELEASE_ID';
6999 	n_varval(2)  := p_release_id;
7000 	n_varname(3) := 'PO_REVISION_NUM';
7001 	n_varval(3)  := p_revision_num;
7002 	n_varname(4) := 'CHANGE_REQUEST_GROUP_ID';
7003 	n_varval (4) := p_chg_req_grp_id;
7004 
7005 	t_varname(1) := 'SUPPLIER_USERNAME';
7006 	t_varval(1)  := l_supplier_username;
7007      /*	t_varname(2) := 'NTF_FOR_SUP_BUY_RP';
7008 	t_varval(2)  := 'PLSQLCLOB:PO_ChangeOrderWF_PVT.GEN_NTF_FOR_SUP_BUY_RP/'|| p_chg_req_grp_id;  */
7009 
7010         t_varname(2) := 'NTF_FOR_SUP_BUY_RP';                     -- RDP changing to point to the notifications Page
7011         t_varval(2)  :=  'JSP:/OA_HTML/OA.jsp?page=/oracle/apps/pos/notifications/webui/PosNotificationsRN'||
7012                          '&L_CHG_REQ_GRP_ID=-&CHANGE_REQUEST_GROUP_ID-&L_PO_HEADER_ID=-&PO_HEADER_ID-&L_PO_RELEASE_ID=-&PO_RELEASE_ID-'||
7013                          '&L_PO_ACC_REQ_FLAG=-&ACKNOWLEDGE_REQ_FLAG-&L_NOTIF_USAGE=-&NOTIF_USAGE-&L_EXPLAIN_FYI=-&EXPLAIN_FYI-&L_STYLE_PARAM=-&PO_STYLE_TYPE-';
7014 	t_varname(3) := 'NTF_FOR_SUP_SUBJECT';
7015 	t_varval(3)  := 'PLSQL:PO_ChangeOrderWF_PVT.GEN_NTF_FOR_SUP_SUBJECT/'||l_item_key;
7016 	t_varname(4) := 'FROM_BUYER';
7017 	t_varval(4)  := l_buyer_username;
7018         t_varname(5) := 'NOTIF_USAGE';
7019         t_varval(5)  := 'BUYER_RESP';
7020         t_varname(6) := 'PO_STYLE_TYPE';
7021         t_varval(6)  := l_po_style;
7022 	t_varname(7) := '#WFM_HTMLAGENT';
7023 	t_varval(7)  := fnd_profile.value('POS_EXTERNAL_URL');
7024 
7025 
7026 	Wf_Engine.SetItemAttrNumberArray('POSCHORD', l_item_key,n_varname,n_varval);
7027 	Wf_Engine.SetItemAttrTextArray('POSCHORD', l_item_key,t_varname,t_varval);
7028 	l_progress := '003';
7029 	wf_engine.StartProcess(	ItemType => 'POSCHORD',
7030 							ItemKey => l_item_key);
7031 
7032 exception when others then
7033 	IF g_fnd_debug = 'Y' AND FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL  THEN
7034 		FND_LOG.string(FND_LOG.level_unexpected, g_module_prefix ||
7035 				l_api_name || '.others_exception', l_progress||':'||sqlerrm);
7036 	END IF;
7037 END NotifySupAllChgRpdWF;
7038 
7039 procedure ProcessHdrCancelResponse( p_api_version in number,
7040 									x_return_status out NOCOPY varchar2,
7041 									p_header_id in number,
7042 									p_release_id in number,
7043 									p_revision_num in number,
7044 									p_user_id in number,
7045 									p_acc_rej in varchar2,
7046 									p_reason in varchar2)
7047 IS
7048 l_api_name varchar2(50) := 'ProcessHdrCancelResponse';
7049 l_progress varchar2(5) := '000';
7050 l_chn_req_grp_id number;
7051 l_retcode number;
7052 l_retmsg varchar2(2000);
7053 l_doc_check_rec_type POS_ERR_TYPE;
7054 
7055 BEGIN
7056 	x_return_status := 'S';
7057 	if(p_release_id is null) then
7058 		select change_request_group_id
7059 		into l_chn_req_grp_id
7060 		from po_change_requests
7061 		where document_header_id = p_header_id
7062 		and request_status = 'PENDING'
7063 		and request_level = 'HEADER'
7064 		and initiator = 'SUPPLIER'
7065 		and action_type ='CANCELLATION';
7066 
7067 	else
7068 		select change_request_group_id
7069 		into l_chn_req_grp_id
7070 		from po_change_requests
7071 		where po_release_id = p_release_id
7072 		and request_status = 'PENDING'
7073 		and request_level = 'HEADER'
7074 		and initiator = 'SUPPLIER'
7075 		and action_type ='CANCELLATION';
7076 	end if;
7077 	l_progress := '001';
7078 	if(p_acc_rej = 'A') then
7079 		update po_change_requests
7080 		set request_status = 'BUYER_APP',
7081                 responded_by = p_user_id,
7082                 response_date = sysdate,
7083                 response_reason = p_reason
7084         	where change_request_group_id = l_chn_req_grp_id;
7085 	else
7086 		update po_change_requests
7087 		set request_status = 'REJECTED',
7088 		    change_active_flag = 'N',
7089                 responded_by = p_user_id,
7090                 response_date = sysdate,
7091                 response_reason = p_reason
7092 		where change_request_group_id = l_chn_req_grp_id;
7093 	end if;
7094 	l_progress := '002';
7095 	ProcessResponse(1.0, x_return_status, p_header_id, p_release_id,
7096                         p_revision_num, l_chn_req_grp_id, p_user_id, l_retmsg,
7097                         l_retcode, l_doc_check_rec_type, null, 'Y');
7098 
7099 exception when others then
7100 	IF g_fnd_debug = 'Y' AND FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL  THEN
7101 		FND_LOG.string(FND_LOG.level_unexpected, g_module_prefix ||
7102 				l_api_name || '.others_exception', l_progress||':'||sqlerrm);
7103 	END IF;
7104 	x_return_status := 'U';
7105 End ProcessHdrCancelResponse;
7106 
7107 
7108 /*
7109 *This API could originate from 2 sources
7110 *1. Buyer Accept or Reject Supplier Change through Notification => Buyer Accept ALL OR Rejects ALL
7111 *2. Buyer Accept or Reject Supplier Change through UI. If response DOES NOT cover all changes
7112 *	return;
7113 *   Else (response cover all changes)
7114 *   	continue...
7115 *   		1. Send Notification to Supplier if all changes are responded
7116 *   			=> 	NO BUYER_APP/PENDING
7117 *   		2. Send Notification to Buyer if PO requires Acknowledgement, and everything is responded
7118 *   			=> 	ACC_REQUIRED_FLAG = 'Y'
7119 *   				NO BUYER_APP/PENDING
7120 *   				ALL SHIPMENTS ACCEPTED/REJECTED
7121 *
7122 *
7123 */
7124 procedure ProcessResponse(p_api_version in number,
7125 			x_return_status out NOCOPY varchar2,
7126 			p_header_id in number,
7127 			p_release_id in number,
7128 			p_revision_num in number,
7129 			p_chg_req_grp_id in number,
7130 			p_user_id in number,
7131 			x_err_msg out NOCOPY varchar2,
7132 			x_return_code out NOCOPY number,
7133 			x_doc_check_rec_type out NOCOPY POS_ERR_TYPE,
7134 			p_flag in varchar2,
7135                         p_launch_approvals_flag in varchar2,
7136                         p_mass_update_releases   IN VARCHAR2 DEFAULT NULL -- Bug 3373453
7137                        )
7138 IS
7139 l_api_name varchar2(50):= 'ProcessResponse';
7140 l_count number;
7141 l_number_of_buyer_app number;
7142 l_acc_req_flag varchar2(1);
7143 l_item_key  		po_change_requests.wf_item_key%TYPE;
7144 l_return_status 	varchar2(1);
7145 l_po_cancel_api exception;
7146 l_org_id number;
7147 l_agent_id number;
7148 x_progress 			varchar2(3):='000';
7149 l_cancel_status 	varchar2(1);
7150 l_mc_return_status 	varchar2(1);
7151 l_mc_return_code number;
7152 l_kickoff_status 	varchar2(1);
7153 l_kickoff_msg 		varchar2(2000);
7154 l_doc_id number;
7155 l_employee_id number;
7156 
7157 l_authorization_status po_headers_all.authorization_status%TYPE;
7158 l_add_changes_accepted NUMBER;
7159 
7160 l_doc_type po_document_types_all.document_type_code%TYPE;
7161 l_doc_subtype po_document_types_all.document_subtype%TYPE;
7162 l_id number;
7163 l_action po_lookup_codes.lookup_code%TYPE;
7164 l_acc_id number;
7165 l_rej_id number;
7166 l_closed_code po_headers_all.closed_code%type;
7167 
7168 cursor l_accept_csr(grp_id number) is
7169 select change_request_id
7170 from po_change_requests
7171 where change_request_group_id = grp_id
7172 and request_status in ('ACCEPTED','BUYER_APP');
7173 
7174 cursor l_reject_csr(grp_id number) is
7175 select change_request_id
7176 from po_change_requests
7177 where change_request_group_id = grp_id
7178 and request_status = 'REJECTED';
7179 
7180 cursor l_pending_csr(grp_id number) is
7181 select change_request_id
7182 from po_change_requests
7183 where change_request_group_id = grp_id
7184 and request_status = 'PENDING';
7185 
7186 
7187 BEGIN
7188 
7189 	x_return_status := FND_API.G_RET_STS_SUCCESS;
7190 
7191 
7192 	if p_release_id is not null then
7193 		l_doc_id := p_release_id;
7194 		l_doc_type := 'RELEASE';
7195 		select
7196 			por.org_id ,
7197 			por.agent_id,
7198 			pha.type_lookup_code,
7199 			nvl(por.closed_code,'OPEN')
7200 		into
7201 			l_org_id,
7202 			l_agent_id,
7203 			l_doc_subtype,
7204 			l_closed_code
7205 		from po_releases_all por,
7206 			po_headers_all pha
7207 		where po_release_id = p_release_id
7208 		and por.po_header_id= pha.po_header_id;
7209 
7210 
7211 	else
7212 		l_doc_id := p_header_id;
7213 		select
7214 			poh.agent_id,
7215 			poh.org_id,
7216 			poh.type_lookup_code,
7217 			nvl(poh.closed_code,'OPEN')
7218 		into
7219 			l_agent_id,
7220 			l_org_id,
7221 			l_doc_subtype,
7222 			l_closed_code
7223 		from po_headers_all poh
7224 		where poh.po_header_id = p_header_id ;
7225 
7226 		if(l_doc_subtype = 'BLANKET' or l_doc_subtype = 'CONTRACT') then
7227 			l_doc_type := 'PA';
7228 		elsif(l_doc_subtype = 'PLANNED' or l_doc_subtype = 'STANDARD') then
7229 			l_doc_type := 'PO';
7230 		end if;
7231 	end if;
7232 	initialize(l_agent_id, l_org_id);
7233         PO_MOAC_UTILS_PVT.set_org_context(l_org_id) ;         -- <R12 MOAC>
7234 
7235 --Check if any Change is still Pending, if yes, return
7236 --This is just for double check.. This API should only be called when all changes are responded
7237 	open l_pending_csr(p_chg_req_grp_id);
7238 	fetch l_pending_csr into l_id;
7239 	if (l_id is not null) then
7240 		x_return_status := FND_API.G_RET_STS_ERROR;
7241 		x_err_msg := 'PENDING RECORDS EXIST';
7242 		close l_pending_csr;
7243 		return;
7244 	end if;
7245 	close l_pending_csr;
7246 	l_id := null;
7247 
7248 --Action History
7249 	open l_accept_csr(p_chg_req_grp_id);
7250 	fetch l_accept_csr into l_acc_id;
7251 	close l_accept_csr;
7252 	open l_reject_csr(p_chg_req_grp_id);
7253 	fetch l_reject_csr into l_rej_id;
7254 	close l_reject_csr;
7255 
7256 	if(l_acc_id is not null and l_rej_id is not null) then
7257 		l_action := 'RESPOND';
7258 	elsif(l_acc_id is not null) then
7259 		l_action := 'ACCEPT';
7260 	elsif(l_rej_id is not null) then
7261 		l_action := 'REJECT';
7262 	end if;
7263 
7264 	if(l_closed_code = 'OPEN') then
7265 
7266 		l_employee_id := l_agent_id;
7267 		if (p_user_id is not null) then
7268 		    begin
7269 		        select employee_id into l_employee_id from fnd_user where user_id = p_user_id;
7270 		    exception when others then
7271 		        null;
7272 		    end;
7273 		end if;
7274 
7275 		if (l_employee_id is null) then
7276 
7277 		    l_employee_id := l_agent_id;
7278 		end if ;
7279 
7280 
7281 		InsertActionHist(
7282 					p_doc_id => l_doc_id,
7283 					p_doc_type => l_doc_type,
7284 					p_doc_subtype => l_doc_subtype,
7285 				        p_employee_id => l_employee_id,
7286 				     -- p_employee_id => l_agent_id,
7287                                      -- p_employee_id => p_user_id,            -- RDP ( instead of agent_id , user_id should be used)
7288 					p_action => l_action,
7289 					p_note => null,
7290 					p_path_id => null);
7291 	end if;
7292 
7293 /* 0. Are changes responded at header level?
7294 	If NO => There is a notification waiting for Buyer to Respond at the Header Level.
7295 				Kill IT!!!
7296 */
7297 	x_progress:='001';
7298 	if(p_flag is null) then
7299 		begin
7300 			select distinct wf_item_key
7301 			into l_item_key
7302 			from po_change_requests
7303 			where change_request_group_id = p_chg_req_grp_id;
7304 			wf_engine.completeActivity('POSCHORD',l_item_key,'NOTIFY_BUYER_OF_CHN', 'Abort');
7305 		exception when others then
7306 			null;
7307 		end;
7308 	end if;
7309 
7310 	x_progress:='002';
7311 	CancelPO(l_cancel_status, p_header_id,p_release_id,p_chg_req_grp_id);
7312 
7313 
7314 	x_progress:='004';
7315 
7316 -- 2. Update po_change_requests table.. for those supplier change request to cancel, if accepted
7317 -- 		change them from BUYER_APP to ACCEPTED
7318 	update po_change_requests
7319 	set request_status = 'ACCEPTED'
7320 	-- change_active_flag = 'N'   /* commented out due to bug 3574114 */
7321 	where change_request_group_id = p_chg_req_grp_id
7322 	and request_status in ('BUYER_APP', 'WAIT_CANCEL_APP')
7323 	and action_type = 'CANCELLATION';
7324 
7325 /*
7326 WHETHER TO SEND NOTIFICATION TO SUPPLIER OR NOT:
7327 At this Stage, there is NO MORE Changes in Pending Stage.
7328 If There is no more change that requires Buyer Hierachy  Approval (BUYER_APP),
7329 	we know the supplier changes have been fully responded. => Send Notification to Supplier
7330 Else
7331 	There are records which requries Buyer Hierachy Approval
7332 		=> MoveChangeToPO, kick off Buyer Approval Workflow
7333 */
7334 
7335 -- Bug 3771964
7336 
7337 if (l_rej_id is not null) then
7338  if (p_release_id is null) then
7339         update po_line_locations_all
7340         set approved_flag = 'Y'
7341         where po_header_id=p_header_id
7342         and po_release_id is null;
7343     else
7344         update po_line_locations_all
7345         set approved_flag = 'Y'
7346         where po_header_id=p_header_id
7347         and po_release_id = p_release_id;
7348    end if;
7349 end if;
7350 
7351 	x_progress:='005';
7352 
7353 	select count(1)
7354 	into l_number_of_buyer_app
7355 	from po_change_requests
7356 	where change_request_group_id = p_chg_req_grp_id
7357 	and request_status = 'BUYER_APP';
7358 
7359 	if(l_number_of_buyer_app = 0) then
7360 		x_progress:='006';
7361 
7362         	NotifySupAllChgRpdWF(
7363 				p_header_id,
7364 				p_release_id,
7365 				p_revision_num,
7366 				p_chg_req_grp_id);
7367 
7368 	else
7369 		-- Call MoveChangeToPO
7370 		x_progress:='007';
7371 		PO_CHANGE_RESPONSE_PVT.MoveChangeToPO(
7372 	         					1.0,
7373 							l_mc_return_status,
7374 							p_header_id,
7375 							p_release_id,
7376 							p_chg_req_grp_id ,
7377 							p_user_id,
7378 							l_mc_return_code,
7379 							x_err_msg,
7380 							x_doc_check_rec_type,
7381                                                         p_launch_approvals_flag,
7382                                                         p_mass_update_releases
7383                                                        );
7384 
7385 		x_return_status := l_mc_return_status;
7386 		x_return_code := l_mc_return_code;
7387 
7388 
7389                 /* COMMENTED OUT - WAIT_MGR_APP status is no longer used and is replaced by BUYER_APP
7390 		update po_change_requests
7391 		set request_status = 'WAIT_MGR_APP'
7392 		where change_request_group_id = p_chg_req_grp_id
7393 		and request_status = 'BUYER_APP';
7394                 */
7395 
7396 		x_progress:='008';
7397                 /* COMMENTED OUT - Now handled in Change PO API
7398         	KickOffPOApproval(
7399 							1.0,
7400 							l_kickoff_status,
7401 							p_header_id,
7402 							p_release_id,
7403 							l_kickoff_msg);
7404 
7405 
7406 		if(l_kickoff_status <> FND_API.G_RET_STS_SUCCESS) then
7407 			x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7408 			x_err_msg := 'STARTPOWF_ERROR:'||l_kickoff_msg;
7409 		end if;
7410                 */
7411 	end if;
7412 
7413 exception when others then
7414 	IF g_fnd_debug = 'Y' AND FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN
7415 		FND_LOG.string(FND_LOG.level_unexpected, g_module_prefix ||
7416 				l_api_name || '.others_exception', x_progress||':'||sqlerrm);
7417 	END IF;
7418 	x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7419 	x_err_msg := x_progress||'*'||sqlerrm;
7420 
7421 END ProcessResponse;
7422 
7423 procedure NOTIFY_REQ_PLAN (	  		itemtype        in varchar2,
7424 		                           	itemkey         in varchar2,
7425         		                   	actid           in number,
7426 	            		         	funcmode        in varchar2,
7427                         		    resultout       out NOCOPY varchar2)
7428 IS
7429 l_progress varchar2(5) := '000';
7430 l_chg_req_grp_id number;
7431 l_requestor_id number;
7432 l_planner_id number;
7433 l_header_id number;
7434 l_release_id number;
7435 l_revision_num number;
7436 l_agent_id number;
7437 
7438 --Bug6885296 - Added condition to restrict requesters from being
7439 --notified when supplier requests changes for Blanket Purchase Agreement.
7440 
7441 cursor l_requestors_csr(grp_id number)
7442 is
7443 select pda.deliver_to_person_id
7444 from
7445 	po_change_requests pcr,
7446 	po_distributions_all pda,
7447         po_line_locations_all pll
7448 where pcr.change_request_group_id = grp_id
7449 and pcr.request_level = 'LINE'
7450 and pcr.document_line_id = pda.po_line_id
7451 and pda.line_location_id = pll.line_location_id
7452 and pll.shipment_type = 'STANDARD'
7453 union
7454 select pda.deliver_to_person_id
7455 from
7456 	po_change_requests pcr,
7457 	po_distributions_all pda
7458 where pcr.change_request_group_id = grp_id
7459 and pcr.request_level = 'SHIPMENT'
7460 and pcr.document_line_location_id = pda.line_location_id;
7461 
7462 cursor l_planners_csr(grp_id number)
7463 is
7464 select
7465 	distinct mtp.employee_id
7466 from
7467 	mtl_system_items msi,
7468 	mtl_planners mtp,
7469 	po_change_requests pcr,
7470 	po_lines_all pla,
7471 	financials_system_params_all fsp
7472 where pcr.change_request_group_id = grp_id
7473 and pcr.document_line_id = pla.po_line_id
7474 and pla.item_id = msi.inventory_item_id
7475 and pla.org_id = fsp.org_id
7476 and fsp.inventory_organization_id= msi.organization_id
7477 and msi.planner_code = mtp.planner_code
7478 and msi.organization_id = mtp.organization_id;
7479 
7480 BEGIN
7481     l_header_id := wf_engine.GetItemAttrNumber(itemtype => itemtype,
7482                                             itemkey => itemkey,
7483                                             aname => 'PO_HEADER_ID');
7484     l_release_id := wf_engine.GetItemAttrNumber(itemtype => itemtype,
7485                                             itemkey => itemkey,
7486                                             aname => 'PO_RELEASE_ID');
7487     l_revision_num := wf_engine.GetItemAttrNumber(itemtype => itemtype,
7488                                             itemkey => itemkey,
7489                                             aname => 'PO_REVISION_NUM');
7490 
7491     l_chg_req_grp_id := wf_engine.GetItemAttrNumber(itemtype => itemtype,
7492                                             itemkey => itemkey,
7493                                             aname => 'CHANGE_REQUEST_GROUP_ID');
7494 	if(l_release_id is null) then
7495 		select agent_id
7496 		into l_agent_id
7497 		from po_headers_all
7498 		where po_header_id = l_header_id;
7499 	else
7500 		select agent_id
7501 		into l_agent_id
7502 		from po_releases_all
7503 		where po_release_id = l_release_id;
7504 	end if;
7505 
7506 	l_progress := '001';
7507 	open l_requestors_csr( l_chg_req_grp_id );
7508 	loop
7509 	fetch l_requestors_csr into l_requestor_id;
7510 	exit when l_requestors_csr%NOTFOUND;
7511 		if(l_requestor_id is not null AND
7512 			l_requestor_id <> l_agent_id) then
7513 			Notify_Requester_Sup_Change(
7514 										l_header_id,
7515 										l_release_id,
7516 										l_revision_num,
7517 										l_chg_req_grp_id,
7518 										l_requestor_id);
7519 		end if;
7520 	end loop;
7521 	close l_requestors_csr;
7522 
7523 	l_progress := '002';
7524 
7525 	open l_planners_csr(l_chg_req_grp_id );
7526 	loop
7527 	fetch l_planners_csr into l_planner_id;
7528 	exit when l_planners_csr%NOTFOUND;
7529 		if(l_planner_id is not null AND
7530 			l_planner_id <> l_agent_id) then
7531 			Notify_Planner_Sup_Change(
7532 										l_header_id,
7533 										l_release_id,
7534 										l_revision_num,
7535 										l_chg_req_grp_id,
7536 										l_planner_id);
7537 		end if;
7538 	end loop;
7539 	close l_planners_csr;
7540 
7541 exception when others then
7542 	wf_core.context('PO_ChangeOrderWF_PVT','NOTIFY_REQ_PLAN',l_progress);
7543     raise;
7544 END   NOTIFY_REQ_PLAN ;
7545 
7546 procedure PROCESS_RESPONSE	(	  	itemtype        in varchar2,
7547 		                           	itemkey         in varchar2,
7548         		                   	actid           in number,
7549 	            		         	funcmode        in varchar2,
7550                         		    resultout       out NOCOPY varchar2)
7551 IS
7552 l_header_id number;
7553 l_release_id number;
7554 l_revision_num number;
7555 l_chg_req_grp_id number;
7556 l_return_code number;
7557 l_err_msg 		varchar2(2000);
7558 x_progress 		varchar2(3) := '000';
7559 l_return_status varchar2(1);
7560 l_doc_check_rec_type POS_ERR_TYPE;
7561 
7562 BEGIN
7563     l_header_id := wf_engine.GetItemAttrNumber(itemtype => itemtype,
7564                                             itemkey => itemkey,
7565                                             aname => 'PO_HEADER_ID');
7566     l_release_id := wf_engine.GetItemAttrNumber(itemtype => itemtype,
7567                                             itemkey => itemkey,
7568                                             aname => 'PO_RELEASE_ID');
7569     l_revision_num := wf_engine.GetItemAttrNumber(itemtype => itemtype,
7570                                             itemkey => itemkey,
7571                                             aname => 'PO_REVISION_NUM');
7572     l_chg_req_grp_id := wf_engine.GetItemAttrNumber(itemtype => itemtype,
7573                                             itemkey => itemkey,
7574                                             aname => 'CHANGE_REQUEST_GROUP_ID');
7575 
7576 	x_progress := '001';
7577 	ProcessResponse(1.0, l_return_status, l_header_id, l_release_id,
7578                         l_revision_num, l_chg_req_grp_id, fnd_global.user_id, l_err_msg,
7579                         l_return_code,l_doc_check_rec_type, 'Y', 'Y');
7580 
7581 exception when others then
7582 	wf_core.context('PO_ChangeOrderWF_PVT','PROCESS_RESPONSE',x_progress);
7583     raise;
7584 END PROCESS_RESPONSE;
7585 
7586 procedure BUYER_ACCEPT_CHANGE  (  	itemtype        in varchar2,
7587 		                           	itemkey         in varchar2,
7588         		                   	actid           in number,
7589 	            		         	funcmode        in varchar2,
7590                         		    resultout       out NOCOPY varchar2)
7591 IS
7592 
7593 l_chg_req_grp_id number;
7594 l_responded_by number;
7595 l_notif_usage VARCHAR2(30);
7596 
7597 BEGIN
7598 
7599     l_chg_req_grp_id := wf_engine.GetItemAttrNumber(itemtype => itemtype,
7600                                             itemkey => itemkey,
7601                                             aname => 'CHANGE_REQUEST_GROUP_ID');
7602 
7603     l_notif_usage := wf_engine.GetItemAttrText(itemtype =>itemtype,
7604                                     itemkey => itemKey,
7605                                     aname => 'NOTIF_USAGE');
7606 
7607    IF( l_notif_usage = 'BUYER_AUTO_FYI') THEN
7608      l_responded_by :=null;
7609   ELSE
7610    l_responded_by :=fnd_global.user_id;
7611   END IF;
7612 
7613       update po_change_requests
7614 	set request_status ='BUYER_APP',
7615 		responded_by = l_responded_by,
7616 		response_date = sysdate
7617 	where change_request_group_id = l_chg_req_grp_id
7618 	and request_status in ('PENDING','REQ_APP');           -- RDP ( Update records lying in REQ_APP status too)
7619 
7620 exception when others then
7621 	wf_core.context('PO_ChangeOrderWF_PVT','BUYER_ACCEPT_CHANGE','000');
7622     raise;
7623 end BUYER_ACCEPT_CHANGE;
7624 
7625 procedure BUYER_REJECT_CHANGE  (  	itemtype        in varchar2,
7626 		                           	itemkey         in varchar2,
7627         		                   	actid           in number,
7628 	            		         	funcmode        in varchar2,
7629                         		    resultout       out NOCOPY varchar2)
7630 IS
7631 
7632 -- curosr to get the ReqHeaderId
7633 cursor c_getReqHdrId(p_po_header_id_csr IN NUMBER) is
7634         select distinct porh.requisition_header_id
7635         from   po_requisition_headers_all porh,
7636                po_requisition_lines_all porl,
7637                po_headers_all poh,
7638                po_line_locations_all poll
7639         where  porh.requisition_header_id = porl.requisition_header_id AND
7640                porl.line_location_id = poll.line_location_id  AND
7641                poh.po_header_id = poll.po_header_id AND
7642                poh.po_header_id = p_po_header_id_csr;
7643 
7644 l_chg_req_grp_id number;
7645 l_notif_usage VARCHAR2(30);
7646 l_req_hdr_id number;
7647 l_po_header_id number;
7648 BEGIN
7649 
7650     l_chg_req_grp_id := wf_engine.GetItemAttrNumber(itemtype => itemtype,
7651                                             itemkey => itemkey,
7652                                             aname => 'CHANGE_REQUEST_GROUP_ID');
7653 
7654 
7655     l_notif_usage      :=  wf_engine.GetItemAttrText(itemtype => itemtype,
7656                                                      itemkey => itemkey,
7657                                                      aname => 'NOTIF_USAGE');
7658 
7659     l_po_header_id     :=  wf_engine.GetItemAttrNumber (itemtype => itemtype,
7660 					                itemkey  => itemkey,
7661 	                                                aname    => 'PO_HEADER_ID');
7662 
7663     -- Withdraw the lock from requisition , if the changes have been rejected by the requester
7664    IF( l_notif_usage = 'REQ') THEN
7665        OPEN c_getReqHdrId(l_po_header_id);
7666         LOOP
7667           FETCH c_getReqHdrId INTO l_req_hdr_id;
7668           EXIT WHEN c_getReqHdrId%NOTFOUND;
7669             BEGIN
7670 	     update po_requisition_headers_all
7671              set change_pending_flag = 'N'
7672              where requisition_header_id = l_req_hdr_id
7673 	     AND change_pending_flag = 'Y';
7674             EXCEPTION
7675               WHEN OTHERS THEN NULL;
7676             END;
7677 	 END LOOP;
7678        CLOSE c_getReqHdrId;
7679     END IF;
7680 
7681 
7682 
7683 	update po_change_requests
7684 	set request_status ='REJECTED',
7685 		change_active_flag = 'N',
7686 		responded_by = fnd_global.user_id,
7687 		response_date = sysdate
7688 	where change_request_group_id = l_chg_req_grp_id
7689 	and request_status in ('PENDING','REQ_APP');           -- RDP ( Update records lying in REQ_APP status too)
7690 exception when others then
7691 	wf_core.context('PO_ChangeOrderWF_PVT','BUYER_REJECT_CHANGE','000');
7692     raise;
7693 end BUYER_REJECT_CHANGE;
7694 
7695 /*
7696 This API is called from ISP Change Details Page. If the supplier submits a change, OR, if the supplier completely finishes
7697 acknowledging the PO at the shipment level (which may contain accept/reject/change), this API will be executed.
7698 */
7699 procedure StartSupplierChangeWF( 	p_api_version in number,
7700 									x_return_status out NOCOPY varchar2,
7701 									p_header_id in number,
7702 									p_release_id in number,
7703 									p_revision_num in number,
7704 									p_chg_req_grp_id in number,
7705 									p_acc_req_flag in varchar2 default 'N')
7706 IS
7707 l_api_name varchar2(50) := 'StartSupplierChangeWF';
7708 l_progress varchar2(100) := '000';
7709 l_count number;
7710 l_seq number;
7711 l_po_style varchar2(10);
7712 l_item_key po_change_requests.wf_item_key%TYPE;
7713 l_doc_type_code varchar2(1);
7714 l_buyer_notif_code varchar2(240);
7715 l_buyer_agent_id number;
7716 l_buyer_username fnd_user.user_name%TYPE;
7717 l_supplier_username fnd_user.user_name%TYPE;
7718 l_buyer_disp_name varchar2(2000);
7719 n_varname   Wf_Engine.NameTabTyp;
7720 n_varval    Wf_Engine.NumTabTyp;
7721 t_varname   Wf_Engine.NameTabTyp;
7722 t_varval    Wf_Engine.TextTabTyp;
7723 x_ret_status  varchar2(1);
7724 x_complex_flag   varchar2(1);
7725 
7726 
7727 BEGIN
7728 IF g_fnd_debug = 'Y' AND FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL  THEN
7729 	FND_LOG.string(FND_LOG.level_procedure, g_module_prefix ||
7730 			l_api_name || 'start',p_header_id||'*'||p_release_id||'*'||p_revision_num||'*'||p_chg_req_grp_id);
7731 END IF;
7732 
7733 	-- Start Workflow
7734 	select PO_SUPPLIER_CHANGE_WF_S.nextval into l_seq from dual;
7735 	if(p_release_id is not null) then
7736 		l_doc_type_code := 'R';
7737 		l_item_key := p_release_id||'-'||p_revision_num||'-'||l_doc_type_code||'-'||to_char(l_seq);
7738 	else
7739 		l_doc_type_code := 'P';
7740 		l_item_key := p_header_id||'-'||p_revision_num||'-'||l_doc_type_code||'-'||to_char(l_seq);
7741 	end if;
7742 
7743 
7744 
7745 
7746 	wf_engine.createProcess (	ItemType => 'POSCHORD',
7747 								ItemKey => l_item_key,
7748 								Process => 'MAIN_PROCESS');
7749 
7750 	l_progress := '001';
7751 
7752 	l_buyer_notif_code := '@'||p_header_id||'#'||p_release_id||'$'||p_chg_req_grp_id||'%'||p_acc_req_flag||'^';
7753 
7754 	-- Get Supplier User Name
7755 	select user_name
7756 	into l_supplier_username
7757 	from fnd_user
7758 	where user_id = fnd_global.user_id;
7759 
7760 	wf_engine.SetItemAttrText(itemtype => 'POSCHORD',
7761 			          itemkey  => l_item_key,
7762 			          aname    => 'RESP_ID',
7763                                   avalue   => fnd_global.RESP_ID);
7764 
7765         wf_engine.SetItemAttrText(itemtype => 'POSCHORD',
7766 			          itemkey  => l_item_key,
7767 			          aname    => 'APPL_RESP_ID',
7768                                   avalue   => fnd_global.RESP_APPL_ID);
7769 
7770 	if(p_release_id is null) then
7771 	    select agent_id
7772 	    into   l_buyer_agent_id
7773 	    from   po_headers_all
7774 	    where  po_headers_all.po_header_id = p_header_id;
7775 	else
7776 	    select pra.agent_id
7777 	    into l_buyer_agent_id
7778 	    from po_releases_all pra,
7779 	    	 po_headers_all pha
7780 	    where pra.po_release_id = p_release_id
7781 	    and pra.po_header_id = pha.po_header_id;
7782         end if;
7783 
7784 	l_progress := '003';
7785 
7786 	-- Get Buyer UserName:
7787 	wf_directory.GetUserName    ( p_orig_system    => 'PER',
7788                                   p_orig_system_id => l_buyer_agent_id,
7789                                   p_name           => l_buyer_username,
7790                                   p_display_name   => l_buyer_disp_name);
7791 
7792  IF(p_release_id is not null) THEN
7793 
7794     l_po_style := 'NORMAL';
7795 
7796  ELSIF(p_release_id is null) THEN
7797 
7798          l_progress := '004: call is_complex_po';
7799 
7800          /*   PO- API  set item attribute PO_STYLE_TYPE depending upon the type of the PO
7801         PO_STYLE_TYPE - COMPLEX ,complex work PO's
7802         PO_STYLE_TYPE - NORMAL */
7803 
7804 
7805    PO_COMPLEX_WORK_GRP.is_complex_work_po(
7806                           1.0,
7807                           p_header_id,
7808                           x_ret_status,
7809                          x_complex_flag);
7810 
7811    IF x_return_status IS NOT NULL AND  x_return_status = FND_API.g_ret_sts_success THEN
7812      IF( g_fnd_debug = 'Y' AND FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7813         FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
7814                        g_module_prefix,
7815                        l_progress
7816                        || 'x_return_status=' || x_return_status);
7817      END IF;
7818 
7819    ELSE
7820      IF( g_fnd_debug = 'Y' AND FND_LOG.LEVEL_ERROR >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7821        FND_LOG.STRING(FND_LOG.LEVEL_ERROR,
7822                        g_module_prefix,
7823                        l_progress
7824                        ||'x_return_status = ' || x_return_status);
7825      END IF;
7826    END IF;
7827 
7828     IF(x_complex_flag = 'N') THEN
7829     l_po_style :='NORMAL';
7830     ELSE
7831     l_po_style := 'COMPLEX';
7832     END IF;
7833  END IF;
7834 
7835 	n_varname(1) := 'PO_HEADER_ID';
7836 	n_varval(1)  := p_header_id;
7837 	n_varname(2) := 'PO_RELEASE_ID';
7838 	n_varval(2)  := p_release_id;
7839 	n_varname(3) := 'PO_REVISION_NUM';
7840 	n_varval(3)  := p_revision_num;
7841 	n_varname(4) := 'CHANGE_REQUEST_GROUP_ID';
7842 	n_varval(4)  := p_chg_req_grp_id;
7843 
7844 	t_varname(1) := 'NTF_FOR_BUYER_SUBJECT';
7845 	t_varval(1)  := 'PLSQL:PO_ChangeOrderWF_PVT.GEN_NTF_FOR_BUYER_SUBJECT/'||l_item_key;
7846 	t_varname(2) := 'NOTIF_FOR_BUYER_SUP_CHG';                                          -- RDP set the item Attibute value to the Notifications Page
7847 	t_varval(2)  := 'JSP:/OA_HTML/OA.jsp?page=/oracle/apps/pos/notifications/webui/PosNotificationsRN'||
7848                          '&L_CHG_REQ_GRP_ID=-&CHANGE_REQUEST_GROUP_ID-&L_PO_HEADER_ID=-&PO_HEADER_ID-&L_PO_RELEASE_ID=-&PO_RELEASE_ID-'||
7849                          '&L_PO_ACC_REQ_FLAG=-&ACKNOWLEDGE_REQ_FLAG-&L_NOTIF_USAGE=-&NOTIF_USAGE-&L_EXPLAIN_FYI=-&EXPLAIN_FYI-&L_STYLE_PARAM=-&PO_STYLE_TYPE-';
7850 	t_varname(3) := 'BUYER_USERNAME';
7851 	t_varval(3)  := l_buyer_username;
7852 	t_varname(4) := 'FROM_SUPPLIER';
7853 	t_varval(4)  := l_supplier_username;
7854 	t_varname(5) := 'ACKNOWLEDGE_REQ_FLAG';
7855 	t_varval(5)  := p_acc_req_flag;
7856         t_varname(6) := 'PO_STYLE_TYPE';
7857         t_varval(6)  := l_po_style;
7858 
7859 	Wf_Engine.SetItemAttrNumberArray('POSCHORD', l_item_key,n_varname,n_varval);
7860 	Wf_Engine.SetItemAttrTextArray('POSCHORD', l_item_key,t_varname,t_varval);
7861 
7862 
7863 
7864 /*update data to include wf item key and item type. p_chg_req_grp_id could be null in the case where user accepts/rejects
7865 all shipments and no changes was made. Thus, in this case, we will NOT store the wf_item_key or wf_item_type anywhere.
7866 */
7867 	if(p_chg_req_grp_id is not null) then
7868 		update po_change_requests
7869 		set wf_item_type = 'POSCHORD',wf_item_key = l_item_key
7870 		where change_request_group_id = p_chg_req_grp_id;
7871 	end if;
7872 	l_progress := '004';
7873 
7874 	wf_engine.StartProcess(	ItemType => 'POSCHORD',
7875 							ItemKey => l_item_key);
7876 
7877 	x_return_status := FND_API.G_RET_STS_SUCCESS;
7878 exception when others then
7879 	IF g_fnd_debug = 'Y' AND FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL  THEN
7880 		FND_LOG.string(FND_LOG.level_unexpected, g_module_prefix ||
7881 				l_api_name || '.others_exception', l_progress||':'||sqlerrm);
7882 	END IF;
7883 	x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7884 end StartSupplierChangeWF;
7885 
7886 
7887 PROCEDURE initialize(p_employee_id in NUMBER,p_org_id IN NUMBER) IS
7888 
7889   l_resp_id NUMBER := -1;
7890   l_user_id NUMBER := -1;
7891   l_resp_appl_id NUMBER := 201;
7892   l_api_name varchar2(50) := 'initialize';
7893   l_progress varchar2(5) := '000';
7894 
7895   cursor l_get_user_info_csr
7896   is
7897 	select fr.responsibility_id,fu.user_id
7898 	from wf_local_user_roles wur,
7899 	     fnd_responsibility fr,
7900 	     financials_system_params_all fsp,
7901 	     fnd_user fu
7902 	  where wur.user_name = fu.user_name
7903 	    and wur.role_orig_system = 'FND_RESP'
7904 	    and wur.role_orig_system_id = fr.responsibility_id
7905 	    and wur.partition_id = 2
7906 	   and (((wur.start_date is NULL) or (trunc(sysdate) >= trunc(wur.start_date)))
7907 	  and	 ((wur.expiration_date is NULL) or
7908 	  (trunc(sysdate) < trunc(wur.expiration_date)))
7909 	  and	 ((wur.user_start_date is NULL) or
7910 	  (trunc(sysdate) >= trunc(wur.user_start_date)))
7911 	  and	 ((wur.user_end_date is NULL) or
7912 	  (trunc(sysdate) < trunc(wur.user_end_date)))
7913 	  and	 ((wur.role_start_date is NULL) or
7914 	  (trunc(sysdate) >= trunc(wur.role_start_date)))
7915 	  and	 ((wur.role_end_date is NULL) or
7916 	  (trunc(sysdate) < trunc(wur.role_end_date))))
7917 	    and fr.application_id = 201
7918 	    and fr.start_date < sysdate
7919 	    and nvl(fr.end_date, sysdate +1) >= sysdate
7920            and nvl(fsp.org_id,-1) = nvl(p_org_id,-1)
7921            and nvl(fsp.business_group_id,-1) = nvl(fnd_profile.value_specific('PER_BUSINESS_GROUP_ID', NULL, fr.responsibility_id, fr.application_id),-1);
7922 
7923 
7924 BEGIN
7925    begin
7926 	SELECT FND.user_id
7927 	INTO   l_user_id
7928 	FROM   FND_USER FND, HR_EMPLOYEES_CURRENT_V HR
7929         WHERE  HR.EMPLOYEE_ID = p_employee_id
7930         AND    FND.EMPLOYEE_ID = HR.EMPLOYEE_ID
7931         AND    ROWNUM = 1;
7932 
7933    EXCEPTION
7934       WHEN OTHERS THEN
7935 	 l_user_id := -1;
7936    END;
7937 
7938    l_progress := '001';
7939 
7940   if (l_user_id <> -1) then
7941    BEGIN
7942 
7943      FND_PROFILE.GET('RESP_ID', l_resp_id);
7944 
7945 	select MIN(fr.responsibility_id)
7946 	into l_resp_id
7947    	from wf_local_user_roles wur,
7948 	     fnd_responsibility fr,
7949 	     financials_system_params_all fsp,
7950 	     fnd_user fu
7951 	  where wur.user_name = fu.user_name
7952 	    and fu.user_id = l_user_id
7953 	    and wur.role_orig_system = 'FND_RESP'
7954 	    and wur.role_orig_system_id = fr.responsibility_id
7955 	    and wur.partition_id = 2
7956 	   and (((wur.start_date is NULL) or (trunc(sysdate) >= trunc(wur.start_date)))
7957 	  and	 ((wur.expiration_date is NULL) or
7958 	  (trunc(sysdate) < trunc(wur.expiration_date)))
7959 	  and	 ((wur.user_start_date is NULL) or
7960 	  (trunc(sysdate) >= trunc(wur.user_start_date)))
7961 	  and	 ((wur.user_end_date is NULL) or
7962 	  (trunc(sysdate) < trunc(wur.user_end_date)))
7963 	  and	 ((wur.role_start_date is NULL) or
7964 	  (trunc(sysdate) >= trunc(wur.role_start_date)))
7965 	  and	 ((wur.role_end_date is NULL) or
7966 	  (trunc(sysdate) < trunc(wur.role_end_date))))
7967 	    and fr.application_id = l_resp_appl_id
7968 	    and fr.start_date < sysdate
7969 	    and nvl(fr.end_date, sysdate +1) >= sysdate
7970            and nvl(fsp.org_id,-1) = nvl(p_org_id,-1)
7971            and nvl(fsp.business_group_id,-1) = nvl(fnd_profile.value_specific('PER_BUSINESS_GROUP_ID', NULL, fr.responsibility_id, fr.application_id),-1);
7972 
7973    EXCEPTION
7974      when others then
7975 	l_resp_id := -1;
7976    END;
7977   end if;
7978 
7979    l_progress := '002';
7980 
7981    if(l_resp_id = -1 OR l_resp_id is null) then
7982 		open l_get_user_info_csr;
7983 		fetch l_get_user_info_csr into
7984 			l_resp_id, l_user_id;
7985 		close l_get_user_info_csr;
7986    end if;
7987 
7988    l_progress := '003';
7989 
7990    IF g_fnd_debug = 'Y' AND FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN
7991 		FND_LOG.string(FND_LOG.level_procedure, g_module_prefix ||
7992 				l_api_name || '.intialize parameters',l_user_id||'*'||l_resp_id||'*'||l_resp_appl_id );
7993    END IF;
7994    FND_GLOBAL.APPS_INITIALIZE(l_user_id,l_resp_id,l_resp_appl_id);
7995 exception when others then
7996 	IF g_fnd_debug = 'Y' AND FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL  THEN
7997 		FND_LOG.string(FND_LOG.level_unexpected, g_module_prefix ||
7998 				l_api_name || '.others_exception', sqlerrm);
7999 	END IF;
8000 END initialize;
8001 
8002 function getEmailResponderUserName(p_supp_user_name varchar2,
8003                                    p_ntf_role_name  varchar2) return varchar2
8004          IS
8005 l_user_name   varchar2(320);
8006 l_email    varchar2(2000);
8007 l_start    pls_integer;
8008 l_end      pls_integer;
8009 begin
8010    /*  This part of the code is copied from the procedure
8011        wf_directory.GetInfoFromMail.  So, watch for fixes by wf in this part
8012        of the code.
8013     */
8014 
8015    -- strip off the unwanted info from email. Emails from the mailer
8016    -- could be of the form "Vijay Shanmugam"[email protected]>
8017    l_start := instr(p_supp_user_name, '<', 1, 1);
8018    if (l_start > 0) then
8019        l_end := instr(p_supp_user_name, '>', l_start);
8020        l_email := substr(p_supp_user_name, l_start+1, l_end-l_start-1);
8021    else
8022        l_email := p_supp_user_name;
8023    end if;
8024 
8025   select wur.user_name
8026     into l_user_name
8027   from wf_user_roles wur,wf_users wr
8028   where wur.role_name=p_ntf_role_name and
8029         wr.name =wur.user_name and
8030         upper(wr.email_address)=upper(l_email);
8031 
8032   return l_user_name;
8033 
8034   exception when others then
8035   --note: if the ntf has been responded by somebody not in the role,
8036   -- it can fail.
8037     return null;
8038 end;
8039                                                             -- RDP called from NotificationsAMImpl.java ( gets the additional changes if any)
8040 PROCEDURE getAdditionalChanges( p_chg_req_grp_id in number,
8041                                 x_addl_chg out nocopy  varchar2,
8042                                 x_count out nocopy number)
8043 is
8044 CURSOR additional_changes(chg_req_grp_id_csr IN NUMBER) IS
8045 select  additional_changes
8046 from po_change_requests
8047 where change_request_group_id = chg_req_grp_id_csr
8048 and request_level = 'HEADER'
8049 and additional_changes is not null;
8050 
8051 -- returns 1 when only additional changes at header level are requested otherwise 0
8052 CURSOR getRequestLevel(chg_req_grp_id_csr IN NUMBER)  IS
8053 SELECT COUNT(1) FROM po_change_requests WHERE NOT EXISTS ( SELECT 1
8054                                                            FROM po_change_requests
8055                                                            WHERE change_request_group_id = chg_req_grp_id_csr
8056                                                            AND request_level IN ('LINE','SHIPMENT'))
8057 AND change_request_group_id = chg_req_grp_id_csr
8058 AND action_type = 'MODIFICATION';
8059 
8060 -- Bug 7487461
8061 -- Modified the data type for additional changes to capture the entire text.
8062 l_additional_changes po_change_requests.additional_changes%type;
8063 l_count NUMBER(10);
8064 begin
8065 OPEN getRequestLevel(p_chg_req_grp_id);
8066 LOOP
8067 FETCH getRequestLevel
8068 INTO l_count;
8069 EXIT WHEN getRequestLevel%NOTFOUND;
8070 END LOOP;
8071 CLOSE getRequestLevel;
8072 
8073 IF(l_count = 1) THEN
8074 x_count := l_count;
8075 ELSE
8076 x_count := 0;
8077 END IF;
8078 
8079 OPEN additional_changes(p_chg_req_grp_id);
8080 LOOP
8081 FETCH additional_changes
8082 INTO l_additional_changes;
8083 EXIT WHEN additional_changes%NOTFOUND;
8084 END LOOP;
8085 CLOSE additional_changes;
8086 
8087 x_addl_chg := l_additional_changes;
8088 
8089 end getAdditionalChanges;
8090 
8091 
8092 PROCEDURE  getReqNumber(p_po_header_id in number , p_po_release_id in number,  x_req_num out nocopy varchar2, x_req_hdr_id out nocopy varchar2)  -- RDP(backing)
8093 IS
8094 CURSOR getReqHdrId(c_po_header_id in NUMBER) IS
8095 select   distinct(porla.requisition_header_id)
8096 from     po_requisition_lines_all  porla,
8097          po_line_locations_all   polla
8098 where    polla.po_header_id = c_po_header_id
8099          and porla.line_location_id = polla.line_location_id;
8100 
8101 CURSOR getReqNum(c_req_header_id in NUMBER) IS
8102 select   porha.segment1
8103 from po_requisition_headers_all porha
8104 where requisition_header_id = c_req_header_id;
8105 
8106 -- for releases
8107 CURSOR getReqHdrId_rel(c_po_header_id in NUMBER, c_po_release_id in NUMBER) IS
8108 select   distinct(porla.requisition_header_id)
8109 from     po_requisition_lines_all  porla,
8110          po_line_locations_all   polla
8111 where    polla.po_header_id = c_po_header_id
8112          and polla.po_release_id = c_po_release_id
8113          and porla.line_location_id = polla.line_location_id;
8114 
8115 
8116 
8117 l_req_header_id  NUMBER(10);
8118 l_req_num        NUMBER(10);
8119 
8120 
8121 BEGIN
8122 IF (p_po_release_id is null) THEN
8123     OPEN getReqHdrId(p_po_header_id);
8124     LOOP
8125     FETCH getReqHdrId INTO l_req_header_id;
8126     EXIT WHEN getReqHdrId%NOTFOUND;
8127     END LOOP;
8128     CLOSE getReqHdrId;
8129 ELSE
8130    OPEN getReqHdrId_rel(p_po_header_id,p_po_release_id);
8131    LOOP
8132    FETCH getReqHdrId_rel INTO l_req_header_id;
8133    EXIT WHEN getReqHdrId_rel%NOTFOUND;
8134    END LOOP;
8135    CLOSE getReqHdrId_rel;
8136 END IF;
8137 
8138 OPEN getReqNum(l_req_header_id);
8139 LOOP
8140 FETCH getReqNum INTO l_req_num;
8141 EXIT  WHEN getReqNum%NOTFOUND;
8142 END LOOP;
8143 
8144 CLOSE getReqNum;
8145 
8146 IF g_fnd_debug = 'Y' AND FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN
8147         FND_LOG.string(FND_LOG.level_procedure, g_module_prefix || '.getReqNumber', l_req_num ||' * '|| l_req_header_id);
8148 END IF;
8149 
8150 x_req_num := TO_CHAR(l_req_num);
8151 x_req_hdr_id := TO_CHAR(l_req_header_id);
8152 
8153 EXCEPTION
8154    WHEN OTHERS THEN
8155      IF( g_fnd_debug = 'Y' AND FND_LOG.LEVEL_ERROR >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
8156           FND_LOG.STRING(FND_LOG.LEVEL_ERROR,
8157                          g_module_prefix,
8158                          ':unexpected error' || Sqlerrm);
8159      END IF;
8160 
8161 end  getReqNumber;
8162 
8163 
8164 PROCEDURE getChgReqMode(p_chg_req_grp_id in number, x_req_mode out nocopy varchar2)  -- RDP(identifies whether this is a CANCELLATION or MODIFICATION request)
8165 IS                                                                                   -- To be used to set the Header Msg of Chg Request Table
8166 l_temp  VARCHAR2(15);
8167 l_req_mode  VARCHAR2(15);
8168 BEGIN
8169 select decode(count(distinct(action_type)),2,'BOTH',1,'CHG') into
8170 l_req_mode
8171 from
8172 po_change_requests
8173 where
8174 change_request_group_id = p_chg_req_grp_id
8175 and request_level in('LINE','SHIPMENT')
8176 and action_type in('MODIFICATION','CANCELLATION');
8177 
8178 if l_req_mode = 'BOTH' then
8179 x_req_mode := 'BOTH';
8180 elsif l_req_mode = 'CHG' then
8181 select distinct(action_type) into
8182 l_temp
8183 from
8184 po_change_requests
8185 where
8186 change_request_group_id = p_chg_req_grp_id
8187 and request_level in('LINE','SHIPMENT')
8188 and action_type in('MODIFICATION','CANCELLATION');
8189 
8190  if l_temp = 'CANCELLATION' then
8191  x_req_mode := 'CANCELLATION';
8192  elsif l_temp = 'MODIFICATION' then
8193  x_req_mode := 'MODIFICATION';
8194  end if;
8195 end if;
8196 
8197 EXCEPTION
8198    WHEN OTHERS THEN
8199       IF( g_fnd_debug = 'Y' AND FND_LOG.LEVEL_ERROR >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
8200           FND_LOG.STRING(FND_LOG.LEVEL_ERROR,
8201                          g_module_prefix,
8202                          ':unexpected error' || Sqlerrm);
8203      END IF;
8204 
8205 end getChgReqMode;
8206 
8207 PROCEDURE getOpenShipCount(po_line_location_id in number, x_ship_invalid_for_ctrl_actn out nocopy varchar2)
8208 IS
8209 l_ship_invalid_for_ctrl_actn varchar2(1);
8210 BEGIN
8211  SELECT 'N'
8212  INTO l_ship_invalid_for_ctrl_actn
8213  FROM DUAL
8214  WHERE EXISTS(
8215               SELECT 1
8216               FROM po_line_locations_all poll1,
8217                    po_line_locations_all poll2
8218               WHERE poll1.line_location_id = po_line_location_id
8219               AND poll1.po_line_id  = poll2.po_line_id
8220                         AND NVL(poll2.cancel_flag,'N') <> 'Y'
8221                         AND NVL(poll2.payment_type, 'NULL') NOT IN ('ADVANCE', 'DELIVERY') --<Complex Work R12>
8222                         AND NVL(poll2.closed_code, 'OPEN')
8223                             <> 'FINALLY CLOSED'
8224                         AND poll2.line_location_id <> po_line_location_id);
8225 
8226  x_ship_invalid_for_ctrl_actn := l_ship_invalid_for_ctrl_actn;
8227 EXCEPTION
8228 WHEN NO_DATA_FOUND THEN
8229 --Current shipment is the only shipment on the line that is not cancelled or finally closed
8230 --OR there are no open, uncancelled shipments.
8231 x_ship_invalid_for_ctrl_actn := 'Y';
8232 END getOpenShipCount;
8233 
8234 
8235 
8236 PROCEDURE getHdrCancel(p_chg_req_grp_id in number,
8237                        po_header_id   in  number,
8238                        po_release_id  in  number,
8239                        x_action_type out nocopy varchar2,
8240                        x_request_status out nocopy varchar2,
8241                        x_doc_type out nocopy varchar2,
8242                        x_doc_num out nocopy varchar2,
8243                        x_revision_num out nocopy varchar2)
8244 IS
8245 l_count              number;
8246 l_action_type        varchar2(15) := null;
8247 l_request_status     varchar2(15) := null;
8248 l_doc_type           varchar2(15) := null;
8249 l_doc_num            varchar2(15) := null;
8250 l_revision_num       varchar2(15) := null;
8251 
8252 CURSOR HdrCancelCsrCount(p_chg_req_grp_id_csr IN NUMBER) IS
8253 SELECT count(*)
8254 FROM   po_change_requests pcr
8255 WHERE  pcr.CHANGE_REQUEST_GROUP_ID = p_chg_req_grp_id_csr
8256 AND   pcr.REQUEST_LEVEL = 'HEADER'
8257 AND   pcr.ACTION_TYPE = 'CANCELLATION';
8258 
8259 CURSOR HdrCancelCsrVal(p_chg_req_grp_id_csr IN NUMBER) IS
8260 SELECT pcr.action_type,
8261        pcr.request_status,
8262        poh.type_lookup_code,
8263        pcr.document_num
8264 FROM   PO_CHANGE_REQUESTS pcr,
8265        PO_HEADERS_ALL     poh
8266 WHERE  pcr.CHANGE_REQUEST_GROUP_ID =  p_chg_req_grp_id_csr
8267        AND   pcr.REQUEST_LEVEL = 'HEADER'
8268        AND   pcr.ACTION_TYPE = 'CANCELLATION'
8269        AND pcr.document_header_id = poh.po_header_id;
8270 
8271 CURSOR getRevisionNum(p_chg_req_grp_id_csr IN NUMBER) IS
8272 SELECT distinct(pcr.document_revision_num)
8273 FROM   po_change_requests pcr
8274 WHERE  pcr.CHANGE_REQUEST_GROUP_ID = p_chg_req_grp_id_csr;
8275 
8276 CURSOR getRevisionNumAck(po_header_id_csr IN NUMBER) IS
8277 SELECT revision_num
8278 FROM   po_headers_all
8279 WHERE  po_header_id = po_header_id_csr;
8280 
8281 CURSOR getRevisionNumAckRel(po_header_id_csr IN NUMBER,po_release_id_csr IN NUMBER) IS
8282 SELECT pora.revision_num
8283 FROM po_releases_all pora
8284 WHERE pora.po_header_id = po_header_id_csr
8285 AND   pora.po_release_id = po_release_id_csr;
8286 
8287 BEGIN
8288 
8289 IF(p_chg_req_grp_id is null) THEN
8290 
8291  IF(po_release_id is null) THEN
8292   OPEN getRevisionNumAck(po_header_id);
8293   FETCH getRevisionNumAck
8294   INTO
8295   l_revision_num;
8296   CLOSE getRevisionNumAck;
8297  ELSE
8298   OPEN getRevisionNumAckRel(po_header_id,po_release_id);
8299   FETCH getRevisionNumAckRel
8300   INTO
8301   l_revision_num;
8302   CLOSE getRevisionNumAckRel;
8303  END IF;
8304 
8305 ELSE
8306 
8307 OPEN HdrCancelCsrCount(p_chg_req_grp_id);
8308 LOOP
8309 FETCH HdrCancelCsrCount
8310 INTO
8311 l_count;
8312 EXIT WHEN HdrCancelCsrCount%NOTFOUND;
8313 END LOOP;
8314 CLOSE HdrCancelCsrCount;
8315 
8316 IF(l_count = 0) THEN
8317 l_action_type := 'NOCANCEL';
8318 OPEN getRevisionNum(p_chg_req_grp_id);
8319 LOOP
8320 FETCH getRevisionNum
8321 INTO l_revision_num;
8322 EXIT WHEN getRevisionNum%NOTFOUND;
8323 END LOOP;
8324 CLOSE getRevisionNum;
8325 
8326 ELSIF(l_count = 1) THEN
8327 OPEN HdrCancelCsrVal(p_chg_req_grp_id);
8328 LOOP
8329 FETCH HdrCancelCsrVal
8330 INTO l_action_type,
8331      l_request_status,
8332      l_doc_type,
8333      l_doc_num;
8334 EXIT WHEN HdrCancelCsrVal%NOTFOUND;
8335 END LOOP;
8336 CLOSE HdrCancelCsrVal;
8337 
8338  END IF;
8339 END IF;
8340 
8341 x_action_type := l_action_type;
8342 x_request_status := l_request_status;
8343 x_doc_type       := l_doc_type;
8344 x_doc_num        := l_doc_num;
8345 x_revision_num   := l_revision_num;
8346 
8347 EXCEPTION
8348   WHEN OTHERS THEN
8349      IF( g_fnd_debug = 'Y' AND FND_LOG.LEVEL_ERROR >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
8350           FND_LOG.STRING(FND_LOG.LEVEL_ERROR,
8351                          g_module_prefix,
8352                          ':unexpected error' || Sqlerrm);
8353      END IF;
8354 
8355 END  getHdrCancel;
8356 
8357 /*
8358 This procedure sets the supplier user context after the auto acceptance of
8359 the PO.
8360 */
8361 
8362 PROCEDURE SET_SUPPLIER_CONTEXT(p_supplier_user_id in NUMBER,p_resp_id in NUMBER,p_appl_resp_id in NUMBER)
8363 IS
8364 BEGIN
8365 
8366 IF g_fnd_debug = 'Y' AND FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN
8367         FND_LOG.string(FND_LOG.level_procedure, g_module_prefix || '.SET_SUPPLIER_CONTEXT', 'user_id:'||p_supplier_user_id||'resp:'||p_resp_id||'appl id:'||p_appl_resp_id );
8368 END IF;
8369 
8370 fnd_global.APPS_INITIALIZE(p_supplier_user_id, p_resp_id, p_appl_resp_id);
8371 
8372 EXCEPTION
8373   WHEN OTHERS THEN
8374      IF( g_fnd_debug = 'Y' AND FND_LOG.LEVEL_ERROR >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
8375           FND_LOG.STRING(FND_LOG.LEVEL_ERROR,
8376                          g_module_prefix,
8377                          ':unexpected error' || Sqlerrm);
8378      END IF;
8379 
8380 END SET_SUPPLIER_CONTEXT;
8381 
8382 procedure CHECK_POS_EXTERNAL_URL(  		itemtype        in varchar2,
8383 		                           	itemkey         in varchar2,
8384         		                   	actid           in number,
8385 	            		         	funcmode        in varchar2,
8386                         		        resultout       out NOCOPY varchar2)
8387 is
8388   l_api_name varchar2(50) := 'CHECK_POS_EXTERNAL_URL';
8389   l_external_url varchar2(500);
8390 begin
8391 
8392   l_external_url := fnd_profile.value('POS_EXTERNAL_URL');
8393   IF (g_fnd_debug = 'Y' AND FND_LOG.level_procedure >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
8394 		FND_LOG.string(FND_LOG.level_procedure, g_module_prefix ||
8395 				l_api_name || '.start', 'POS_EXTERNAL_URL=' || l_external_url);
8396   END IF;
8397   resultout := 'N:' || l_external_url;
8398 
8399   if (l_external_url is not null) then
8400 
8401     PO_WF_UTIL_PKG.SetItemAttrText ( itemtype => itemtype,
8402                                    itemkey  => itemkey,
8403                                    aname    => '#WFM_HTMLAGENT',
8404                                    avalue   => l_external_url);
8405 
8406     resultout := 'Y:' || l_external_url;
8407   end if;
8408 
8409 exception when others then
8410     IF (g_fnd_debug = 'Y' AND FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
8411       FND_LOG.string(FND_LOG.level_unexpected, g_module_prefix ||
8412 				l_api_name || '.others_exception', sqlerrm);
8413     END IF;
8414     resultout := 'F:' || l_external_url;
8415     return;
8416 end CHECK_POS_EXTERNAL_URL;
8417 
8418 END PO_ChangeOrderWF_PVT;