DBA Data[Home] [Help]

PACKAGE BODY: APPS.PO_REQCHANGEREQUESTWF_PVT

Source


1 PACKAGE BODY PO_ReqChangeRequestWF_PVT AS
2 /* $Header: POXVRCWB.pls 120.65.12010000.11 2009/01/29 08:40:59 pilamuru ship $ */
3 
4 
5 -- Read the profile option that enables/disables the debug log
6 g_po_wf_debug VARCHAR2(1) := NVL(FND_PROFILE.VALUE('PO_SET_DEBUG_WORKFLOW_ON'),'N');
7 g_fnd_debug CONSTANT VARCHAR2(1) := NVL(FND_PROFILE.VALUE('AFLOG_ENABLED'),'N');
8 -- Debugging
9 g_debug_stmt CONSTANT BOOLEAN := po_debug.is_debug_stmt_on;
10 g_debug_unexp CONSTANT BOOLEAN := po_debug.is_debug_unexp_on;
11 --Bug#3497033
12 --g_currency_format_mask declared to pass in as the second parameter
13 --in FND_CURRENCY.GET_FORMAT_MASK
14 g_currency_format_mask NUMBER := 60;
15 
16 g_tolerances_tbl po_co_tolerances_grp.tolerances_tbl_type;
17 
18 -- Logging Static Variables
19   G_CURRENT_RUNTIME_LEVEL      NUMBER;
20   G_LEVEL_UNEXPECTED           CONSTANT NUMBER             := FND_LOG.LEVEL_UNEXPECTED;
21   G_LEVEL_ERROR                CONSTANT NUMBER             := FND_LOG.LEVEL_ERROR;
22   G_LEVEL_EXCEPTION            CONSTANT NUMBER             := FND_LOG.LEVEL_EXCEPTION;
23   G_LEVEL_EVENT                CONSTANT NUMBER             := FND_LOG.LEVEL_EVENT;
24   G_LEVEL_PROCEDURE            CONSTANT NUMBER             := FND_LOG.LEVEL_PROCEDURE;
25   G_LEVEL_STATEMENT            CONSTANT NUMBER             := FND_LOG.LEVEL_STATEMENT;
26   G_MODULE_NAME                CONSTANT VARCHAR2(100) := 'po.plsql.PO_REQCHANGEREQUESTWF_PVT';
27 
28 /*************************************************************************
29  * Private Procedure: StartPOChangeWF
30  *
31  * Effects: Start process of workflow PORPOCHA, which process the PO
32  *          change requests. The p_process can be 'INFORM_BUYER_PO_CHANGE'
33  *          or 'PROCESS_BUYER_RESPONSE'. The first process is to inform
34  *          buyer of the new PO change, and the second is to process buyer's
35  *          response to the PO change request.
36  *
37  *          the process will commit when it exits.
38  *
39  * Returns:
40  ************************************************************************/
41 procedure StartPOChangeWF(p_change_request_group_id in number,
42         p_item_key in varchar2,
43         p_process in varchar2,
44         p_forward_from_username in varchar2,
45         p_user_id in number,
46         p_responsibility_id in number,
47         p_application_id in number);
48 
49 /*************************************************************************
50  * Private Procedure: StartConvertProcess
51  *
52  * Effects: Start CONVERT_INTO_PO_REQUEST process of workflow POREQCHA.
53  *
54  *          This process will be called when PO cancel API is called, which
55  *          in turn close the affected req change requests. If a req change
56  *          request is closed by the PO cancel API, we need start the wf
57  *          process CONVERT_INTO_PO_REQUEST to do some cleaning work.
58  *          This procedure will start the workflow process.
59  *
60  *          This procedure will be called by procedure
61  *          Process_Cancelled_Req_Lines ( which will be called by PO cancel
62  *          API)
63  *
64  * Returns:
65  ************************************************************************/
66 procedure StartConvertProcess(p_change_request_group_id in number,
67         p_old_item_key in varchar2);
68 
69 /*************************************************************************
70  * Private Procedure: SetReqChangeFlag
71  * Effects: set the change_pending_flag in requisition headers table
72  *          if the flag is set to 'Y', which means there is pending
73  *          change coming, also store the itemtype and itemkey to
74  *          po_change_requests table.
75  *
76  *          the process will commit when it exits.
77  *
78  * Returns:
79  ************************************************************************/
80 procedure SetReqChangeFlag(p_change_request_group_id in number,
81         p_document_id in number,
82         p_itemtype in varchar2,
83         p_itemkey in varchar2,
84         p_change_flag in varchar2);
85 
86 /*************************************************************************
87  * Private Procedure: SetReqRequestStatus
88  * Effects: set the status of the requester change records
89  *
90  *          It is only called in POREQCHA workflow, which means the
91  *          status can be set to 'MGR_PRE_APP', 'MGR_APP' or 'REJECTED'.
92  *
93  *          the process will commit when it exits.
94  *
95  * Returns:
96  ************************************************************************/
97 procedure SetReqRequestStatus(p_change_request_group_id in number,
98         p_cancel_flag in varchar2,
99         p_request_status in varchar2,
100         p_responder_id in number,
101         p_response_reason in varchar);
102 
103 /*************************************************************************
104  * Private Procedure: SetPoRequestStatus
105  * Effects: set the status of the PO change records
106  *
107  *          It is only called in PORPOCHA workflow
108  *
109  *          the process will commit when it exits.
110  *
111  * Returns:
112  ************************************************************************/
113 procedure SetPoRequestStatus(p_change_request_group_id in number,
114         p_request_status in varchar2,
115         p_response_reason in varchar2 default null);
116 
117 
118 /*************************************************************************
119  * Private Procedure: ProcessBuyerAction
120  * Effects: This procedure is called to process the buyer's action
121  *
122  *          the parameter p_action can be 'CANCELLATION', 'REJECTION'
123  *          or 'ACCEPTANCE'.
124  *
125  *          'REJECTION' means to process buyer's
126  *          rejection to PO change request. the main task is to
127  *          reject the corresponding req change.
128  *
129  *          'CANCELLATION' is to process buyer's acceptance of cancel
130  *          request. It will call PO cancel API to cancel the
131  *          corresponding PO part, and also update the status to 'ACCEPTED'
132  *          of the req change
133  *
134  *          'ACCEPTANCE is to process the buyer's acceptance of
135  *          change request. It will call movechangetopo to move the accepted
136  *          change request to PO, and then update the req with the
137  *          new value. also update the corresponding req change status.
138  *
139  *          the process will commit when it exits.
140  *
141  * Returns:
142  ************************************************************************/
143 procedure ProcessBuyerAction(p_change_request_group_id in number,
144         p_action in varchar2, p_launch_approvals_flag IN VARCHAR2 default 'N', p_supplier_change IN varchar2 default 'N');
145 
146 /*************************************************************************
147  * Private Procedure: InsertActionHist
148  * Effects: insert into action history table.
149  *
150  *          It is called when the change request is submitted (by requester
151  *          or buyer) and when buyer responds to the change request.
152  *
153  *          the action can be 'SUBMIT CHANGE', 'ACCEPTED', 'REJECTED'
154  *          or 'RESPOND'
155  *
156  *          the process will commit when it exits.
157  *
158  * Returns:
159  ************************************************************************/
160 PROCEDURE InsertActionHist(itemtype varchar2,
161         itemkey varchar2,
162         p_doc_id number,
163         p_doc_type varchar2,
164         p_doc_subtype varchar2,
165         p_employee_id number,
166         p_action varchar2,
167         p_note varchar2,
168         p_path_id number);
169 procedure ConvertIntoPOChange(p_change_request_group_id in number);
170 
171 
172 
173 /*************************************************************************
174  * Private Procedure: CheckPOAutoApproval
175  * Effects: check if the change already match the PO. If that is the case
176  *          the change request should be automatically approved. It will
177  *          call the procedure AutoApprove to update the status of the
178  *          change request and the corresponding requisition.
179  *
180  *          the process will commit when it exits.
181  *
182  * Returns:
183  ************************************************************************/
184 PROCEDURE CheckPOAutoApproval(p_change_request_group_id in number);
185 
186 /*************************************************************************
187  * Private Procedure: AutoApprove
188  * Effects: set the status of the po change records to accepted, also
189  *          update the corresponding req change record and the requisition.
190  *
191  *          called by CheckPOAutoApproval, and will be committed in that
192  *          procedure.
193  *
194  * Returns:
195  ************************************************************************/
196 procedure AutoApprove(p_change_request_id in number);
197 
198 /*************************************************************************
199  * Private Procedure: setNewTotal
200  * Effects: set the attribute of change amount, changetotal and change tax
201  *
202  * Returns:
203  ************************************************************************/
204 procedure setNewTotal(itemtype in varchar2, itemkey in varchar2);
205 
206 /*************************************************************************
207  * Private Procedure: UpdateReqDistribution
208  * Effects: update the quantity of a requisition distribution.
209  *
210  * Returns:
211  ************************************************************************/
212 procedure UpdateReqDistribution(p_req_line_id in number,
213         p_req_distribution_id in number,
214         p_new_quantity in number,
215         p_old_quantity in number,
216         p_new_dist_amount number,
217         p_old_dist_amount number,
218         p_new_currency_dist_amount number,
219         p_old_currency_dist_amount number);
220 
221 
222 /*************************************************************************
223  * Private Procedure: ValidateAndSaveRequest
224  *
225  * Effects:
226  *          update the corresponding req change record and the requisition.
227  *          call validate api to check if the request is valid or not.
228  *          if yes, save the request to database
229  *
230  * Returns:
231  ************************************************************************/
232 procedure ValidateAndSaveRequest(
233         p_po_header_id         in number,
234         p_po_release_id        in number,
235         p_revision_num         in number,
236         p_po_change_requests   in out nocopy pos_chg_rec_tbl);
237 
238 /*************************************************************************
239  * Private Procedure: UpdateReqLine
240  * Effects: update the need by date and/or price of a requisition line
241  *
242  * Returns:
243  ************************************************************************/
244 procedure UpdateReqLine(p_req_line_id in number,
245         p_new_need_by_date in DATE,
246         p_new_unit_price in number,
247         p_new_currency_unit_price in number,
248         p_new_start_date date,
249         p_new_end_date date);
250 
251 /*************************************************************************
252  * Private Procedure: CalculateRcoTotal
253  * Effects: calculate the new total and old total in RCO buyer notification
254  *          used in Set_Buyer_Approval_Notfn_Attr and Set_Buyer_FYI_Notif_Attributes
255  * Returns:
256  ************************************************************************/
257 procedure CalculateRcoTotal (  p_change_request_group_id in number,
258                      p_org_id in number,
259                      p_po_currency in varchar2,
260                      x_old_total out nocopy number,
261                      x_new_total out nocopy number );
262 
263 /*************************************************************************
264  * Private Procedure: get_po_line_amount
265  * Effects: calculate the old and new line amount for a SPO line. Used in
266  *          buyer tolerance checking.
267  * Returns:
268  ************************************************************************/
269  procedure get_po_line_amount( p_chg_request_grp_id IN NUMBER,
270                               p_po_line_id IN NUMBER,
271                               x_old_amount OUT NOCOPY VARCHAR2,
272                               x_new_amount OUT NOCOPY VARCHAR2 );
273 
274 /*********************************************************************************************
275  * Private Procedure: UpdatePODocHeaderTables
276  * Effects: This procedure gets invoked from PO_ReqChangeRequestWF_PVT.New_PO_Change_Exists
277  *
278  *           When there is a change request,updating of the table po_header_all/po_release_all
279  *           based on the status of the  change requests can also be done by an autonomous block
280  *           which there by can create a deadlock.Hence included the updating of tables in
281  *           this separate autonomous transaction procedure to avoid any deadlock error.
282  ********************************************************************************************/
283  procedure UpdatePODocHeaderTables(p_document_type varchar2, p_document_id number);
284 
285 
286 /*************************************************************************
287  *
288  * Public Procedure: get_sales_order_org
289  * Effects: This function returns the sales order org id
290  *
291  ************************************************************************/
292 /*FUNCTION get_sales_order_org( p_req_hdr_id IN VARCHAR2 DEFAULT null,
293                               p_req_line_id IN VARCHAR2  DEFAULT null
294                            ) RETURN NUMBER;*/
295 
296 FUNCTION get_sales_order_org( p_req_hdr_id IN NUMBER DEFAULT null,
297                               p_req_line_id IN NUMBER  DEFAULT null
298                            ) RETURN NUMBER
299 IS
300    l_org_id number;
301 begin
302 
303     SELECT ooh.ORG_ID
304     INTO l_org_id
305     from po_requisition_lines_all prl,
306          po_requisition_headers_all prh,
307          oe_order_headers_all ooh,
308          po_system_parameters_all psp
309     WHERE prl.requisition_header_id = nvl(p_req_hdr_id,prh.requisition_header_id)
310     AND prl.requisition_line_id = nvl(p_req_line_id,prl.requisition_line_id)
311     AND prh.requisition_header_id = nvl(p_req_hdr_id,prh.requisition_header_id)
312     AND prh.requisition_header_id = ooh.source_document_id
313     AND prh.segment1 = ooh.orig_sys_document_ref
314     AND psp.org_id = prh.org_id
315     AND psp.order_source_id = ooh.order_source_id
316     AND nvl(p_req_hdr_id,p_req_line_id) IS NOT NULL
317     and rownum =1;
318   return l_org_id;
319 
320   EXCEPTION
321     WHEN NO_DATA_FOUND THEN
322       RETURN NULL;
323 END get_sales_order_org;
324 
325 FUNCTION get_requisition_org( p_req_hdr_id IN NUMBER DEFAULT null,
326                               p_req_line_id IN NUMBER  DEFAULT null
327                            ) RETURN NUMBER
328 IS
329    l_org_id number;
330 begin
331 
332     SELECT prh.org_id
333     INTO l_org_id
334     from po_requisition_lines_all prl,
335          po_requisition_headers_all prh
336     WHERE prl.requisition_header_id = nvl(p_req_hdr_id,prh.requisition_header_id)
337     AND prh.requisition_header_id = nvl(p_req_hdr_id,prh.requisition_header_id)
338     AND prl.requisition_line_id = nvl(p_req_line_id,prl.requisition_line_id)
339     AND nvl(p_req_hdr_id,p_req_line_id) IS NOT NULL
340      and rownum =1;
341    return l_org_id;
342 
343   EXCEPTION
344     WHEN NO_DATA_FOUND THEN
345       RETURN NULL;
346 END get_requisition_org;
347 
348 -----------------------------------------------------------------------------------------
349 
350 -- FPJ approver currency change
351 --Start of Comments
352 --Name: getReqAmountInfo
353 --Pre-reqs:
354 --  None.
355 --Modifies:
356 --  None.
357 --Locks:
358 --  None.
359 --Function:
360 --  convert req total, req amount, req tax into approver preferred currency for display
361 --Parameters:
362 --IN:
363 --itemtype
364 --  workflow item type
365 --itemtype
366 --  workflow item key
367 --p_function_currency
368 --  functional currency
369 --p_total_amount_disp
370 --  req total including tax, in displayable format
371 --p_total_amount
372 --  req total including tax, number
373 --p_req_amount_disp
374 --  req total without including tax, in displayable format
375 --p_req_amount
376 --  req total without including tax, number
377 --p_tax_amount_disp
378 --  req tax, in displayable format
379 --p_tax_amount
380 --  req tax number
381 --OUT:
382 --p_amount_for_subject
383 --p_amount_for_header
384 --p_amount_for_tax
385 --End of Comments
386 -------------------------------------------------------------------------------
387 procedure getReqAmountInfo(itemtype        in varchar2,
388                           itemkey         in varchar2,
389                           p_function_currency in varchar2,
390                           p_total_amount_disp in varchar2,
391                           p_total_amount in number,
392                           p_req_amount_disp in varchar2,
393                           p_req_amount in number,
394                           p_tax_amount_disp in varchar2,
395                           p_tax_amount in number,
396                           x_amount_for_subject out nocopy varchar2,
397                           x_amount_for_header out nocopy varchar2,
398                           x_amount_for_tax out nocopy varchar2) is
399 
400   l_rate_type po_system_parameters.default_rate_type%TYPE;
401   l_rate number;
402   l_denominator_rate number;
403   l_numerator_rate number;
404   l_approval_currency varchar2(30);
405   l_amount_disp varchar2(60);
406   l_amount_approval_currency number;
407   l_approver_user_name fnd_user.user_name%TYPE;
408   l_user_id fnd_user.user_id%TYPE;
409   l_progress varchar2(200);
410   l_no_rate_msg varchar2(200);
411 
412 begin
413   SELECT  default_rate_type
414   INTO l_rate_type
415   FROM po_system_parameters;
416 
417   l_progress := 'getReqAmountInfo:' || l_rate_type;
418   IF (g_po_wf_debug = 'Y') THEN
419      /* DEBUG */  PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,l_progress);
420   END IF;
421 
422   l_approver_user_name := PO_WF_UTIL_PKG.GetItemAttrText(itemtype=>itemtype,
423                                                  itemkey=>itemkey,
424                                                  aname=>'APPROVER_USER_NAME');
425   if (l_approver_user_name is not null) then
426     SELECT user_id
427     INTO l_user_id
428     FROM fnd_user
429     WHERE user_name = l_approver_user_name;
430 
431     l_progress := 'getReqAmountInfo:' || l_user_id;
432     IF (g_po_wf_debug = 'Y') THEN
433        /* DEBUG */  PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,l_progress);
434     END IF;
435 
436     l_approval_currency := FND_PROFILE.VALUE_SPECIFIC('ICX_PREFERRED_CURRENCY', l_user_id);
437   end if;
438 
439   if (l_approval_currency = p_function_currency or l_approver_user_name is null
440       or l_rate_type is null or l_approval_currency is null) then
441     x_amount_for_subject := p_total_amount_disp || ' ' || p_function_currency;
442     x_amount_for_header := p_req_amount_disp || ' ' || p_function_currency;
443     x_amount_for_tax := p_tax_amount_disp || ' ' || p_function_currency;
444     return;
445   end if;
446 
447   l_progress := 'getReqAmountInfo:' || l_approval_currency;
448   IF (g_po_wf_debug = 'Y') THEN
449      /* DEBUG */  PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,l_progress);
450   END IF;
451 
452   gl_currency_api.get_closest_triangulation_rate(
453                   x_from_currency => p_function_currency,
454                   x_to_currency => l_approval_currency,
455                   x_conversion_date => sysdate,
456                   x_conversion_type => l_rate_type,
457                   x_max_roll_days  => 5,
458                   x_denominator => l_denominator_rate,
459                   x_numerator => l_numerator_rate,
460                   x_rate => l_rate);
461 
462 
463   l_progress := 'getReqAmountInfo:' || substrb(to_char(l_rate), 1, 30);
464 
465   IF (g_po_wf_debug = 'Y') THEN
466      /* DEBUG */  PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,l_progress);
467   END IF;
468 
469   /* setting amount for notification subject */
470   l_amount_approval_currency := (p_total_amount/l_denominator_rate) * l_numerator_rate;
471 
472   l_amount_disp := TO_CHAR(l_amount_approval_currency,
473                             FND_CURRENCY.GET_FORMAT_MASK(l_approval_currency,g_currency_format_mask));
474   x_amount_for_subject := l_amount_disp || ' ' || l_approval_currency;
475 
476   /* setting amount for header attribute */
477   l_amount_approval_currency := (p_req_amount/l_denominator_rate) * l_numerator_rate;
478 
479   l_amount_disp := TO_CHAR(l_amount_approval_currency,
480                             FND_CURRENCY.GET_FORMAT_MASK(l_approval_currency,g_currency_format_mask));
481 
482   l_progress := 'getReqAmountInfo:' || l_amount_disp;
483   IF (g_po_wf_debug = 'Y') THEN
484      /* DEBUG */  PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,l_progress);
485   END IF;
486 
487   x_amount_for_header := p_req_amount_disp || ' ' || p_function_currency;
488   x_amount_for_header :=  x_amount_for_header || ' (' || l_amount_disp || ' ' || l_approval_currency || ')';
489 
490   l_amount_approval_currency := (p_tax_amount/l_denominator_rate) * l_numerator_rate;
491 
492   l_amount_disp := TO_CHAR(l_amount_approval_currency,
493                             FND_CURRENCY.GET_FORMAT_MASK(l_approval_currency,g_currency_format_mask));
494 
495   x_amount_for_tax := p_tax_amount_disp || ' ' || p_function_currency;
496   x_amount_for_tax :=  x_amount_for_tax || ' (' || l_amount_disp || ' ' || l_approval_currency || ')';
497 
498 exception
499 when gl_currency_api.no_rate then
500   l_progress := 'getReqAmountInfo: no rate';
501 
502   IF (g_po_wf_debug = 'Y') THEN
503      /* DEBUG */  PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,l_progress);
504   END IF;
505   x_amount_for_subject := p_total_amount_disp || ' ' || p_function_currency;
506 
507   l_no_rate_msg := fnd_message.get_string('PO', 'PO_WF_NOTIF_NO_RATE');
508   l_no_rate_msg := replace (l_no_rate_msg, '&CURRENCY', l_approval_currency);
509 
510   x_amount_for_header :=  p_req_amount_disp || ' ' || p_function_currency;
511   x_amount_for_header :=  x_amount_for_header || ' (' || l_no_rate_msg || ')';
512 
513   x_amount_for_tax := p_tax_amount_disp || ' ' || p_function_currency;
514   x_amount_for_tax :=  x_amount_for_tax || ' (' || l_no_rate_msg || ')';
515 
516 when others then
517 
518   l_progress := 'getReqAmountInfo:' || substrb(SQLERRM, 1,200);
519 
520   IF (g_po_wf_debug = 'Y') THEN
521      /* DEBUG */  PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,l_progress);
522   END IF;
523   x_amount_for_subject := p_total_amount_disp || ' ' || p_function_currency;
524   x_amount_for_header :=  p_req_amount_disp || ' ' || p_function_currency;
525   x_amount_for_tax := p_tax_amount_disp || ' ' || p_function_currency;
526 
527 end;
528 
529 
530 /*************************************************************************
531  * Private Procedure: ValidateAndSaveRequest
532  *
533  * Effects:
534  *          update the corresponding req change record and the requisition.
535  *          call validate api to check if the request is valid or not.
536  *          if yes, save the request to database
537  *
538  * Returns:
539  ************************************************************************/
540 procedure ValidateAndSaveRequest(
541     p_po_header_id         in number,
542     p_po_release_id        in number,
543     p_revision_num         in number,
544     p_po_change_requests   in out nocopy pos_chg_rec_tbl) is
545 
546 l_doc_check_rec_type Doc_Check_Return_Type;
547 l_pos_errors pos_err_type;
548 l_online_report_id number;
549 l_return_status varchar2(100);
550 l_error_code varchar2(100);
551 l_msg_data varchar2(1000);
552 
553 l_record_count number;
554 l_change_request_group_id number;
555 l_doc_status po_headers_all.authorization_status%type;
556 i number;
557 l_save_failure exception;
558 l_error_message varchar2(240);
559 l_error_message1 varchar2(2000);
560 x_progress varchar2(100):='PO_ReqChangeRequestWF_PVT.ValidateAndSaveRequest:000';
561 begin
562 
563 
564     -- get the status of the doc, validation api will be called
565     -- only if the doc is in 'APPROVED' status
566     if(p_po_release_id is null) then
567         select nvl(authorization_status, 'IN PROCESS')
568             into l_doc_status
569             from po_headers_all
570             where po_header_id=p_po_header_id;
571     else
572         select nvl(authorization_status, 'IN PROCESS')
573             into l_doc_status
574             from po_releases_all
575             where po_release_id=p_po_release_id;
576     end if;
577 
578     x_progress :='PO_ReqChangeRequestWF_PVT.ValidateAndSaveRequest:001';
579 
580     -- if the doc is in approved status, call validation
581     -- api. if the validation check fails, we won't save
582     -- the po change requests, and also reject the parent
583     -- req change.
584     if(l_doc_status = 'APPROVED') then
585         PO_CHG_REQUEST_PVT.validate_change_request (
586             p_api_version         => 1.0,
587             p_init_msg_list       => FND_API.G_FALSE,
588             x_return_status       => l_return_status,
589             x_msg_data            => l_msg_data,
590             p_po_header_id        => p_po_header_id,
591             p_po_release_id       => p_po_release_id,
592             p_revision_num        => p_revision_num,
593             p_po_change_requests  => p_po_change_requests,
594             x_online_report_id    => l_online_report_id,
595             x_pos_errors          => l_pos_errors,
596             x_doc_check_error_msg => l_doc_check_rec_type);
597         x_progress :='PO_ReqChangeRequestWF_PVT.ValidateAndSaveRequest:002'
598                           ||l_return_status;
599 
600         if(l_return_status <> 'S' ) then
601         -- error, so reject the change request
602         -- or can we do this later, check for all the change requests
603         -- which does not have child change id, performance will be better.
604             l_error_message1:=' ';
605             if(l_pos_errors is not null) then
606                 l_error_message:=' ';
607                 l_error_message1:=l_pos_errors.text_line(1);
608                 for y in 1..l_pos_errors.text_line.count
609                 loop
610                     l_error_message:=substr(l_error_message||l_pos_errors.MESSAGE_NAME(y)||':', 1, 240);
611                 end loop;
612             end if;
613             l_error_message:=substr(l_error_message||l_msg_data||':', 1, 240);
614 
615             x_progress :='PO_ReqChangeRequestWF_PVT.ValidateAndSaveRequest:003';
616             l_record_count:=p_po_change_requests.count();
617             FOR i in 1..l_record_count LOOP
618                         update po_change_requests
619                             set request_status='REJECTED',
620                                 change_active_flag='N',
621                                 response_reason=substr(fnd_message.get_string('PO',
622                                                    'PO_RCO_VALIDATION_ERROR')||':'||
623                                                    l_error_message1, 1, 2000),
624                                 response_date=sysdate,
625                                 validation_error=l_error_message
626                             where change_request_id=
627                                   p_po_change_requests(i).parent_change_request_id;
628                     end loop;
629                     x_progress :='PO_ReqChangeRequestWF_PVT.ValidateAndSaveRequest:004';
630                 else
631                 -- save the request, check for auto approval
632                     x_progress :='PO_ReqChangeRequestWF_PVT.ValidateAndSaveRequest:005';
633 
634                     PO_CHG_REQUEST_PVT.save_request(
635                         p_api_version          =>1.0,
636                         p_Init_Msg_List        =>FND_API.G_FALSE,
637                         x_return_status        =>l_return_status,
638                         p_po_header_id         =>p_po_header_id,
639                         p_po_release_id        =>p_po_release_id,
640                         p_revision_num         =>p_revision_num,
641                         p_po_change_requests   =>p_po_change_requests,
642                         x_request_group_id     =>l_change_request_group_id);
643 
644                     if(l_return_status <>'S') then
645                         raise l_save_failure;
646                     end if;
647 
648                     CheckPOAutoApproval(l_change_request_group_id);
649                     x_progress :='PO_ReqChangeRequestWF_PVT.ValidateAndSaveRequest:006';
650                 end if;
651     elsif(l_doc_status='REJECTED') then
652             -- reject the change immediately
653                 x_progress :='PO_ReqChangeRequestWF_PVT.ValidateAndSaveRequest:007';
654                 l_record_count:=p_po_change_requests.count();
655                 FOR i in 1..l_record_count LOOP
656                     update po_change_requests
657                 set request_status='REJECTED',
658                     change_active_flag='N',
659                     response_date=sysdate,
660                     response_reason=fnd_message.get_string('PO', 'PO_RCO_PO_REJECTED')
661                 where change_request_id=
662                       p_po_change_requests(i).parent_change_request_id;
663         end loop;
664         x_progress :='PO_ReqChangeRequestWF_PVT.ValidateAndSaveRequest:008';
665     else
666     -- save the request. no auto approval at this time
667     -- it will be done when the PO is back to approved status
668         x_progress :='PO_ReqChangeRequestWF_PVT.ValidateAndSaveRequest:009';
669         PO_CHG_REQUEST_PVT.save_request(
670             p_api_version          =>1.0,
671             p_Init_Msg_List        =>FND_API.G_FALSE,
672             x_return_status        =>l_return_status,
673             p_po_header_id         =>p_po_header_id,
674             p_po_release_id        =>p_po_release_id,
675             p_revision_num         =>p_revision_num,
676             p_po_change_requests   =>p_po_change_requests,
677             x_request_group_id     =>l_change_request_group_id);
678         if(l_return_status <>'S') then
679             raise l_save_failure;
680         end if;
681         x_progress :='PO_ReqChangeRequestWF_PVT.ValidateAndSaveRequest:010';
682     end if;
683 exception
684     when l_save_failure then
685         l_record_count:=p_po_change_requests.count();
686         FOR i in 1..l_record_count LOOP
687             update po_change_requests
688                 set request_status='REJECTED',
689                     change_active_flag='N',
690                     response_date=sysdate,
691                     response_reason=fnd_message.get_string('PO', 'PO_RCO_EXCEPTION_WHEN_SAVING')
692                 where change_request_id=
693                       p_po_change_requests(i).parent_change_request_id;
694         end loop;
695     when others then
696         wf_core.context('PO_ReqChangeRequestWF_PVT',
697                         'ValidateAndSaveRequest',x_progress||sqlerrm);
698         raise;
699 end ValidateAndSaveRequest;
700 
701 
702 
703 /*************************************************************************
704  * Private Procedure: UpdateReqDistribution
705  * Effects: update the quantity of a requisition distribution.
706  *
707  * Returns:
708  ************************************************************************/
709 procedure UpdateReqDistribution(
710         p_req_line_id in number,
711         p_req_distribution_id in number,
712         p_new_quantity in number,
713         p_old_quantity in number,
714         p_new_dist_amount number,
715         p_old_dist_amount number,
716         p_new_currency_dist_amount number,
717         p_old_currency_dist_amount number) is
718 l_quantity number;
719 l_recoverable_tax number;
720 l_nonrecoverable_tax number;
721 l_price number;
722 l_return_status varchar2(1);
723 x_progress varchar2(3):='000';
724 begin
725 
726     PO_RCO_VALIDATION_PVT.Calculate_DistTax(
727         p_api_version =>1.0,
728         x_return_status =>l_return_status,
729         p_dist_id =>p_req_distribution_id,
730         p_price =>null,
731         p_dist_amount => p_new_dist_amount,
732         p_quantity =>p_new_quantity,
733         p_rec_tax =>l_recoverable_tax,
734         p_nonrec_tax =>l_nonrecoverable_tax);
735 
736     x_progress :='0'||l_return_status;
737     if(l_return_status<>FND_API.G_RET_STS_SUCCESS) then
738         raise g_update_data_exp;
739     end if;
740     update po_req_distributions_all
741     set req_line_quantity=p_new_quantity,
742         req_line_amount = p_new_dist_amount,
743         req_line_currency_amount = p_new_currency_dist_amount,
744         recoverable_tax=l_recoverable_tax,
745         nonrecoverable_tax=l_nonrecoverable_tax
746     where distribution_id=p_req_distribution_id;
747 
748     x_progress :='001';
749     update po_requisition_lines_all
750     set
751       quantity = quantity + decode(p_new_quantity,null,0,(p_new_quantity-p_old_quantity)),
752       amount = amount + decode(p_new_dist_amount,null,0,(p_new_dist_amount - p_old_dist_amount)),
753       currency_amount = currency_amount + decode(p_new_currency_dist_amount,null,0,
754                                          (p_new_currency_dist_amount-p_old_currency_dist_amount))
755 
756     where requisition_line_id=p_req_line_id;
757     x_progress :='002';
758 exception
759     when others then
760         wf_core.context('PO_ReqChangeRequestWF_PVT',
761                         'UpdateReqDistribution',x_progress||sqlerrm);
762         raise;
763 end UpdateReqDistribution;
764 
765 
766 /*************************************************************************
767  * Private Procedure: UpdateReqLine
768  * Effects: update the need by date and/or price of a requisition line
769  *
770  * Returns:
771  ************************************************************************/
772 procedure UpdateReqLine(
773         p_req_line_id in number,
774         p_new_need_by_date in DATE,
775         p_new_unit_price in number,
776         p_new_currency_unit_price in number,
777         p_new_start_date date,
778         p_new_end_date date) is
779 l_quantity number;
780 l_recoverable_tax number;
781 l_nonrecoverable_tax number;
782 l_distributions_id number;
783 x_progress varchar2(100):='000';
784 l_return_status varchar2(1);
785 
786 cursor l_distributions_csr is
787     select distribution_id
788     from po_req_distributions_all
789     where requisition_line_id=p_req_line_id;
790 
791 begin
792 
793     if(p_new_unit_price is not null) then
794         open l_distributions_csr;
795         loop
796             fetch l_distributions_csr into l_distributions_id;
797             exit when l_distributions_csr%NOTFOUND;
798             x_progress :='001-'||to_char(l_distributions_id);
799             PO_RCO_VALIDATION_PVT.Calculate_DistTax(
800                 p_api_version =>1.0,
801                 x_return_status =>l_return_status,
802                 p_dist_id =>l_distributions_id,
803                 p_price =>p_new_unit_price,
804                 p_quantity =>null,
805                 p_dist_amount => null,
806                 p_rec_tax =>l_recoverable_tax,
807                 p_nonrec_tax =>l_nonrecoverable_tax);
808             if(l_return_status<>FND_API.G_RET_STS_SUCCESS) then
809                 raise g_update_data_exp;
810             end if;
811 
812             x_progress :='002-'||to_char(l_distributions_id);
813 
814             update po_req_distributions_all
815                 set recoverable_tax=l_recoverable_tax,
816                     nonrecoverable_tax=l_nonrecoverable_tax
817                 where distribution_id=l_distributions_id;
818             x_progress :='003-'||to_char(l_distributions_id);
819         end loop;
820         close l_distributions_csr;
821     end if;
822     x_progress :='0042-';
823 
824     update po_requisition_lines_all
825     set need_by_date=nvl(p_new_need_by_date, need_by_date),
826         unit_price=nvl(p_new_unit_price, unit_price),
827         currency_unit_price=nvl(p_new_currency_unit_price, currency_unit_price),        assignment_start_date = nvl(p_new_start_date, assignment_start_date),
828         assignment_end_date = nvl(p_new_end_date, assignment_end_date)
829     where requisition_line_id=p_req_line_id;
830     x_progress :='005-';
831 EXCEPTION
832   WHEN OTHERS THEN
833     wf_core.context('PO_ReqChangeRequestWF_PVT','UpdateReqLine',x_progress||sqlerrm);
834         raise;
835 
836 end UpdateReqLine;
837 
838 
839 /*************************************************************************
840  * Private Procedure: AutoApprove
841  * Effects: Auto approve cancel request or price change request only
842  *          (for need by date or quantity, another procedure
843  *           AutoApproveShipment will handle it.)
844  *
845  * Returns:
846  ************************************************************************/
847 procedure AutoApprove(p_change_request_id in number) is
848 x_progress varchar2(3);
849 l_req_change_request_id number;
850 
851 l_header_id number;
852 l_line_id number;
853 l_distribution_id number;
854 l_action_type varchar2(20);
855 l_request_level varchar2(20);
856 l_new_quantity number;
857 l_old_quantity number;
858 l_new_need_by_date DATE;
859 l_new_unit_price number;
860 l_new_currency_unit_price number;
861 l_new_start_date date;
862 l_new_expiration_date date;
863 
864 begin
865 
866     select parent_change_request_id
867     into l_req_change_request_id
868     from po_change_requests
869     where change_request_id=p_change_request_id;
870 
871     update po_change_requests
872     set request_status='ACCEPTED',
873         change_active_flag='N',
874         response_date=sysdate,
875         response_reason=fnd_message.get_string('PO','PO_RCO_AUTO_ACCEPTED')
876     where change_request_id in (l_req_change_request_id, p_change_request_id);
877 
878     select document_header_id, document_line_id, document_distribution_id, action_type, request_level
879     into l_header_id, l_line_id, l_distribution_id, l_action_type, l_request_level
880     from po_change_requests
881     where change_request_id=l_req_change_request_id;
882 
883     if(l_action_type='CANCELLATION') then
884         if(l_request_level='HEADER') then
885             update po_requisition_headers_all
886             set cancel_flag='Y'
887             where requisition_header_id=l_header_id;
888         else
889             -- it is line level
890             update po_requisition_lines_all
891             set cancel_flag='Y'
892             where requisition_line_id=l_line_id;
893         end if;
894     else
895         if(l_request_level='LINE') then
896             -- change can be need by date, price
897             select new_need_by_date, new_price, new_currency_unit_price,
898                new_start_date, new_expiration_date
899             into l_new_need_by_date, l_new_unit_price,
900               l_new_currency_unit_price, l_new_start_date,
901               l_new_expiration_date
902             from po_change_requests
903             where change_request_id=l_req_change_request_id;
904 
905             UpdateReqLine(l_line_id, l_new_need_by_date,
906                         l_new_unit_price, l_new_currency_unit_price,
907                         l_new_start_date, l_new_expiration_date);
908 /*        else
909             select new_quantity, old_quantity
910             into l_new_quantity, l_old_quantity
911             from po_change_requests
912             where change_request_id=l_req_change_request_id;
913 
914             UpdateReqDistribution(l_line_id, l_distribution_id,
915                 l_new_quantity, l_old_quantity);
916 */
917         end if;
918     end if;
919     x_progress:='000';
920 
921 end AutoApprove;
922 
923 /*************************************************************************
924  * Private Procedure: AutoApproveShipment
925  * Effects: Auto Approve need-by-date change and quantity change
926  *
927  * Returns:
928  ************************************************************************/
929 procedure AutoApproveShipment(p_change_request_group_id in number,
930                               p_line_location_id in number) is
931 x_progress varchar2(3);
932 l_request_id number;
933 l_parent_request_id number;
934 
935 l_header_id number;
936 l_line_id number;
937 l_distribution_id number;
938 l_action_type varchar2(20);
939 l_request_level varchar2(20);
940 l_new_quantity number;
941 l_old_quantity number;
942 l_new_amount number;
943 l_old_amount number;
944 l_new_currency_amount number;
945 l_old_currency_amount number;
946 l_new_need_by_date DATE;
947 l_new_currency_unit_price number;
948 l_line_id1 number;
949 l_req_change_group_id number;
950 l_req_change_group_id1 number;
951 l_new_price number;
952 
953 cursor l_approve_request_csr is
954     select pcr1.change_request_id,
955            pcr1.parent_change_request_id,
956            pcr2.request_level,
957            pcr2.new_need_by_date,
958            pcr2.new_quantity,
959            pcr2.old_quantity,
960            pcr2.new_amount,
961            pcr2.old_amount,
962            pcr2.new_currency_amount,
963            pcr2.old_currency_amount,
964            pcr2.document_line_id,
965            pcr2.document_distribution_id,
966            pcr2.change_request_group_id
967       from po_change_requests pcr1, po_change_requests pcr2
968      where pcr1.change_request_group_id=p_change_request_group_id
969            and pcr1.document_line_location_id=p_line_location_id
970            and pcr1.parent_change_request_id=pcr2.change_request_id;
971 
972 begin
973 
974     open l_approve_request_csr;
975     loop
976         fetch l_approve_request_csr
977          into l_request_id,
978               l_parent_request_id,
979               l_request_level,
980               l_new_need_by_date,
981               l_new_quantity,
982               l_old_quantity,
983               l_new_amount,
984               l_old_amount,
985               l_new_currency_amount,
986               l_old_currency_amount,
987               l_line_id,
988               l_distribution_id,
989               l_req_change_group_id;
990         exit when l_approve_request_csr%NOTFOUND;
991 
992         l_line_id1:=l_line_id;
993         l_req_change_group_id1:=l_req_change_group_id;
994 
995         update po_change_requests
996         set request_status='ACCEPTED',
997             change_active_flag='N',
998             response_date=sysdate,
999             response_reason=fnd_message.get_string('PO','PO_RCO_AUTO_ACCEPTED')
1000         where change_request_id in (l_request_id, l_parent_request_id);
1001 
1002         if(l_request_level='LINE') then
1003             -- change can be need by date
1004             UpdateReqLine(l_line_id, l_new_need_by_date,
1005                         null, null, null, null);
1006         else
1007             UpdateReqDistribution(l_line_id, l_distribution_id,
1008                 l_new_quantity, l_old_quantity, l_new_amount, l_old_amount,
1009                 l_new_currency_amount, l_old_currency_amount);
1010         end if;
1011     end loop;
1012 
1013     begin
1014         update po_change_requests
1015            set request_status='ACCEPTED',
1016                change_active_flag='N',
1017                response_date=sysdate,
1018                response_reason=fnd_message.get_string('PO','PO_RCO_AUTO_ACCEPTED')
1019          where change_request_group_id=p_change_request_group_id
1020                and document_line_location_id=p_line_location_id
1021                and parent_change_request_id is null;
1022 
1023     exception
1024         when others then
1025             null;
1026     end;
1027 
1028     begin
1029         select new_price, new_currency_unit_price
1030         into l_new_price, l_new_currency_unit_price
1031         from po_change_requests
1032         where change_request_group_id=l_req_change_group_id1
1033               and document_line_id=l_line_id1
1034               and action_type='DERIVED'
1035               and new_price is not null;
1036 
1037         UpdateReqLine(l_line_id1, null, l_new_price, l_new_currency_unit_price, null, null);
1038     exception
1039         when no_data_found then
1040             null;
1041     end;
1042 
1043 
1044 end AutoApproveShipment;
1045 
1046 
1047 /*************************************************************************
1048  * Private Procedure: CheckPOAutoApproval
1049  * Effects:
1050  *          This function is called when a po is back to approved
1051  *          or rejected status. We check if there is some po change
1052  *          can be auto approved
1053  *
1054  * Returns:
1055  ************************************************************************/
1056 PROCEDURE   CheckPOAutoApproval(p_change_request_group_id in number) is
1057 
1058 e_exception exception;
1059 x_progress varchar2(3):= '000';
1060 l_request_level varchar(20);
1061 l_document_type varchar2(20);
1062 l_document_id number;
1063 l_document_line_id number;
1064 l_document_shipment_id number;
1065 l_document_distribution_id number;
1066 l_release_id number;
1067 l_change_request_id number;
1068 l_request_status varchar2(20);
1069 
1070 
1071 l_action_type po_change_requests.action_type%type;
1072 l_last_action_type po_change_requests.action_type%type;
1073 l_new_need_by_date DATE;
1074 l_po_need_by_date DATE;
1075 l_new_price number;
1076 l_po_price number;
1077 l_new_quantity number;
1078 l_po_quantity number;
1079 l_new_start_date date;
1080 l_new_end_date date;
1081 l_new_amount number;
1082 l_po_start_date date;
1083 l_po_end_date date;
1084 l_po_amount number;
1085 
1086 l_current_line_location_id number:=null;
1087 l_auto_approve_flag boolean:=true;
1088 
1089 l_header_cancel_flag varchar2(1);
1090 l_line_cancel_flag varchar2(1);
1091 l_shipment_cancel_flag varchar2(1);
1092 
1093 l_po_currency_code  PO_HEADERS_ALL.currency_code%type;
1094 l_functional_currency_code gl_sets_of_books.currency_code%TYPE;
1095 l_org_id number;
1096 
1097 cursor l_pending_change_csr(p_change_request_group_id in number) is
1098     select  pcr.document_type,
1099             pcr.document_header_id,
1100             pcr.document_line_id,
1101             pcr.document_line_location_id,
1102             pcr.document_distribution_id,
1103             pcr.po_release_id,
1104             pcr.change_request_id,
1105             pcr.request_level,
1106             pcr.action_type,
1107             pcr.new_need_by_date,
1108             pcr.new_price,
1109             pcr.new_quantity,
1110             pcr.new_start_date,
1111             pcr.new_expiration_date,
1112             pcr.new_amount,
1113             pol.unit_price line_price,
1114             pll.need_by_date ship_need_by_date,
1115             pod.quantity_ordered dist_quantity,
1116             pol.start_date line_start_date,
1117             pol.expiration_date line_end_date,
1118             pod.amount_ordered dist_amount,
1119             nvl(por.cancel_flag, poh.cancel_flag) header_cancel_flag,
1120             pol.cancel_flag line_cancel_flag,
1121             pll.cancel_flag shipment_cancel_flag,
1122             poh.currency_code,
1123             pol.org_id
1124     from po_change_requests pcr,
1125          po_headers_all poh,
1126          po_lines_all pol,
1127          po_line_locations_all pll,
1128          po_distributions_all pod,
1129          po_releases_all por
1130     where pcr.change_request_group_id=p_change_request_group_id
1131         and pcr.request_status='PENDING'
1132         and pcr.parent_change_request_id is not null
1133         and pcr.document_header_id=poh.po_header_id
1134         and pcr.document_line_id=pol.po_line_id(+)
1135         and pcr.document_line_location_id=pll.line_location_id(+)
1136         and pcr.document_distribution_id=pod.po_distribution_id(+)
1137         and pcr.po_release_id=por.po_release_id(+)
1138     order by document_line_id, nvl(document_line_location_id, 0), nvl(document_distribution_id,0);
1139 
1140 begin
1141     open l_pending_change_csr(p_change_request_group_id);
1142     loop
1143         fetch l_pending_change_csr into
1144             l_document_type,
1145             l_document_id,
1146             l_document_line_id,
1147             l_document_shipment_id,
1148             l_document_distribution_id,
1149             l_release_id,
1150             l_change_request_id,
1151             l_request_level,
1152             l_action_type,
1153             l_new_need_by_date,
1154             l_new_price,
1155             l_new_quantity,
1156             l_new_start_date,
1157             l_new_end_date,
1158             l_new_amount,
1159             l_po_price,
1160             l_po_need_by_date,
1161             l_po_quantity,
1162             l_po_start_date,
1163             l_po_end_date,
1164             l_po_amount,
1165             l_header_cancel_flag,
1166             l_line_cancel_flag,
1167             l_shipment_cancel_flag,
1168             l_po_currency_code,
1169             l_org_id;
1170         exit when l_pending_change_csr%NOTFOUND;
1171 
1172         l_last_action_type:=l_action_type;
1173 
1174         if(l_action_type='CANCELLATION') then
1175 
1176             -- there is an assumption here, when po got canceled,
1177             -- the cancel flag in po line and po shipment will also
1178             -- be set. if po line is canceled, the cancel flag on po
1179             -- shipment will be set.
1180             -- confirmed with Jang Kim
1181             -- the request level can be 'HEADER', 'LINE'
1182             -- or 'SHIPMENT'. It should be at 'DISTRIBUTION level
1183 
1184             if(l_header_cancel_flag='Y'
1185                            or l_line_cancel_flag='Y'
1186                            or l_shipment_cancel_flag='Y') then
1187                 AutoApprove(l_change_request_id);
1188             end if;
1189         else
1190 
1191           if(l_current_line_location_id is null) then
1192             l_current_line_location_id:=l_document_shipment_id;
1193             l_auto_approve_flag:=true;
1194           elsif(l_document_shipment_id is null
1195                      or l_current_line_location_id<>l_document_shipment_id) then
1196             if(l_auto_approve_flag) then
1197                 AutoApproveShipment(p_change_request_group_id, l_current_line_location_id);
1198                 l_current_line_location_id:=l_document_shipment_id;
1199             else
1200                 l_current_line_location_id:=l_document_shipment_id;
1201                 l_auto_approve_flag:=true;
1202             end if;
1203           end if;
1204 
1205           if(l_auto_approve_flag) then
1206 
1207             -- then it is modification
1208             if(l_request_level='DISTRIBUTION') then
1209             -- it must be quantity change
1210 
1211                 -- bug 5363103
1212                 -- l_new_quantity and l_new_amount are from po_change_requests table
1213                 -- which are always in functional currency;
1214                 -- l_po_quantity and l_po_amount are from PO.
1215                 -- If PO is created in txn currency, RCO shouldn't be autoapproved
1216                 -- even if the quantities (amounts) are the same ( since they are
1217                 -- in different currency ).
1218 
1219                 SELECT sob.currency_code
1220                 INTO  l_functional_currency_code
1221                 FROM  gl_sets_of_books sob, financials_system_params_all fsp
1222                 WHERE fsp.org_id = l_org_id
1223                 AND  fsp.set_of_books_id = sob.set_of_books_id;
1224 
1225                 if(l_po_quantity<>l_new_quantity OR
1226                    l_po_amount<>l_new_amount  OR
1227                     l_functional_currency_code <> l_po_currency_code ) then
1228                     l_auto_approve_flag:=false;
1229                 end if;
1230             elsif(l_request_level='SHIPMENT') then
1231             -- can it be need-by-date (can't be price?
1232                 if(l_po_need_by_date<>l_new_need_by_date) then
1233                 l_auto_approve_flag:=false;
1234                 end if;
1235             elsif(l_request_level='LINE') then
1236                 -- it can be a price change,
1237                 if(l_po_price=l_new_price OR
1238                    l_po_start_date=l_new_start_date OR
1239                    l_po_end_date=l_new_end_date) then
1240                     AutoApprove(l_change_request_id);
1241                 else
1242                   l_auto_approve_flag := false;
1243                 end if;
1244             end if;
1245           end if;
1246         end if;
1247         x_progress:='000';
1248     end loop;
1249     close l_pending_change_csr;
1250 
1251     if(l_current_line_location_id is not null and l_auto_approve_flag
1252            and l_last_action_type<>'CANCELLATION') then
1253          AutoApproveShipment(p_change_request_group_id,
1254                              l_current_line_location_id);
1255     end if;
1256 
1257 end CheckPOAutoApproval;
1258 
1259 
1260 /*************************************************************************
1261  * Private Procedure: ValidateChgAgainstNewPO
1262  * Effects:
1263  *          This function is called when a po is back to approved
1264  *          or rejected status. We check if there is some po change
1265  *          can be auto approved
1266  *
1267  * Returns:
1268  ************************************************************************/
1269 PROCEDURE   ValidateChgAgainstNewPO(p_change_request_group_id in number) is
1270 --pragma AUTONOMOUS_TRANSACTION;
1271 
1272 l_doc_status po_headers_all.authorization_status%type;
1273 
1274 x_progress varchar2(3):= '000';
1275 
1276 l_doc_check_rec_type Doc_Check_Return_Type;
1277 l_pos_errors pos_err_type;
1278 l_online_report_id number;
1279 l_return_status varchar2(100);
1280 l_error_code varchar2(100);
1281 l_change_request_group_id number;
1282 temp number;
1283 my_chg_rec_tbl pos_chg_rec_tbl := pos_chg_rec_tbl();
1284 
1285 
1286 ll_document_header_id number;
1287 ll_document_type varchar2(30);
1288 ll_po_release_id number;
1289 ll_document_revision_num number;
1290 
1291 cursor l_get_id_csr(p_change_request_group_id in number) is
1292     select document_header_id,
1293            po_release_id,
1294            document_type
1295       from po_change_requests
1296      where change_request_group_id=p_change_request_group_id
1297            and request_status='PENDING';
1298 
1299 l_document_header_id number;
1300 l_po_release_id number;
1301 l_document_num number;
1302 l_action_type varchar2(30);
1303 l_document_type varchar2(30);
1304 l_request_level varchar2(30);
1305 l_document_revision_num number;
1306 l_created_by number;
1307 l_document_line_id number;
1308 l_document_line_number number;
1309 l_document_line_location_id number;
1310 l_document_shipment_number number;
1311 l_document_distribution_id number;
1312 l_document_distribution_num number;
1313 l_old_quantity number;
1314 l_new_quantity number;
1315 l_old_need_by_date date;
1316 l_new_need_by_date date;
1317 l_old_price number;
1318 l_new_price number;
1319 l_old_amount number;
1320 l_new_amount number;
1321 l_old_start_date date;
1322 l_new_start_date date;
1323 l_old_expiration_date date;
1324 l_new_expiration_date date;
1325 l_request_reason varchar2(2000);
1326 l_parent_change_request_id number;
1327 
1328 l_msg_data varchar2(1000);
1329 l_error_message1 varchar2(2000);
1330 
1331 
1332 cursor l_pending_change_csr(p_change_request_group_id in number) is
1333     select
1334         document_header_id,
1335         po_release_id,
1336         document_num,
1337         action_type,
1338         document_type,
1339         request_level,
1340         document_revision_num,
1341         created_by,
1342         document_line_id,
1343         document_line_number,
1344         document_line_location_id,
1345         document_shipment_number,
1346         document_distribution_id,
1347         document_distribution_number,
1348         request_reason,
1349         old_need_by_date,
1350         new_need_by_date,
1351         old_price,
1352         new_price,
1353         old_quantity,
1354         new_quantity,
1355         old_start_date,
1356         new_start_date,
1357         old_expiration_date,
1358         new_expiration_date,
1359         old_amount,
1360         new_amount,
1361         parent_change_request_id
1362     from po_change_requests
1363     where change_request_group_id=p_change_request_group_id
1364         and request_status='PENDING';
1365 
1366 begin
1367 
1368     open l_get_id_csr(p_change_request_group_id);
1369     fetch l_get_id_csr into ll_document_header_id, ll_po_release_id, ll_document_type;
1370     close l_get_id_csr;
1371 
1372     x_progress :='001';
1373     if(ll_document_type ='PO' ) then
1374         select nvl(authorization_status, 'IN PROCESS'), revision_num
1375             into l_doc_status,ll_document_revision_num
1376             from po_headers_all
1377             where po_header_id=ll_document_header_id;
1378         x_progress :='002';
1379     else
1380         select nvl(authorization_status, 'IN PROCESS'), revision_num
1381             into l_doc_status, ll_document_revision_num
1382             from po_releases_all
1383             where po_release_id=ll_po_release_id;
1384         x_progress :='003';
1385     end if;
1386 
1387     x_progress :='004';
1388     if(l_doc_status='REJECTED') then
1389     -- reject the change immediately
1390         x_progress :='005';
1391         update po_change_requests
1392            set request_status='REJECTED',
1393                change_active_flag='N',
1394                response_date=sysdate,
1395                response_reason=fnd_message.get_string('PO', 'PO_RCO_PO_REJECTED')
1396          where change_request_id in (
1397                 select parent_change_request_id
1398                   from po_change_requests
1399                  where change_request_group_id=p_change_request_group_id
1400                        and request_status='PENDING');
1401 
1402         x_progress :='006';
1403         update po_change_requests
1404            set request_status='REJECTED',
1405                change_active_flag='N',
1406                response_date=sysdate,
1407                response_reason=fnd_message.get_string('PO', 'PO_RCO_PO_REJECTED')
1408          where change_request_group_id=p_change_request_group_id
1409                and request_status='PENDING';
1410         x_progress :='007';
1411     else
1412 
1413         x_progress :='008';
1414         open l_pending_change_csr(p_change_request_group_id);
1415         temp:=1;
1416         my_chg_rec_tbl := pos_chg_rec_tbl();
1417 
1418         loop
1419             fetch l_pending_change_csr into
1420                 l_document_header_id,
1421                 l_po_release_id,
1422                 l_document_num,
1423                 l_action_type,
1424                 l_document_type,
1425                 l_request_level,
1426                 l_document_revision_num,
1427                 l_created_by,
1428                 l_document_line_id,
1429                 l_document_line_number,
1430                 l_document_line_location_id,
1431                 l_document_shipment_number,
1432                 l_document_distribution_id,
1433                 l_document_distribution_num,
1434                 l_request_reason,
1435                 l_old_need_by_date,
1436                 l_new_need_by_date,
1437                 l_old_price,
1438                 l_new_price,
1439                 l_old_quantity,
1440                 l_new_quantity,
1441                 l_old_start_date,
1442                 l_new_start_date,
1443                 l_old_expiration_date,
1444                 l_new_expiration_date,
1445                 l_old_amount,
1446                 l_new_amount,
1447                 l_parent_change_request_id;
1448             exit when l_pending_change_csr%NOTFOUND;
1449 
1450             my_chg_rec_tbl.extend;
1451             my_chg_rec_tbl(temp) := pos_chg_rec(
1452                 Action_Type                     =>l_action_type,
1453                 Initiator                       =>'REQUESTER',
1454                 Request_Reason                  =>l_request_reason,
1455                 Document_Type                   =>l_document_type,
1456                 Request_Level                   =>l_request_level,
1457                 Request_Status                  =>'PENDING',
1458                 Document_Header_Id              =>l_document_header_id,
1459                 PO_Release_Id                   =>l_po_release_id,
1460                 Document_Num                    =>l_document_num,
1461                 Document_Revision_Num           =>l_document_revision_num,
1462                 Document_Line_Id                =>l_document_line_id,
1463                 Document_Line_Number            =>l_document_line_number,
1464                 Document_Line_Location_Id       =>l_document_line_location_id,
1465                 Document_Shipment_Number        =>l_document_shipment_number,
1466                 Document_Distribution_id        =>l_document_distribution_id,
1467                 Document_Distribution_Number    =>l_document_distribution_num,
1468                 Parent_Line_Location_Id         =>null, --NUMBER,
1469                 Old_Quantity                    =>l_old_quantity, --NUMBER,
1470                 New_Quantity                    =>l_new_quantity, --NUMBER,
1471                 Old_Promised_Date               =>null, --DATE,
1472                 New_Promised_Date               =>null, --DATE,
1473                 Old_Supplier_Part_Number        =>null, --VARCHAR2(25),
1474                 New_Supplier_Part_Number        =>null, --VARCHAR2(25),
1475                 Old_Price                       =>l_old_price,
1476                 New_Price                       =>l_new_price,
1477                 Old_Supplier_Reference_Number   =>null, --VARCHAR2(30),
1478                 New_Supplier_reference_number   =>null,
1479                 From_Header_id                  =>null, --NUMBER
1480                 Recoverable_Tax                 =>null, --NUMBER
1481                 Non_recoverable_tax             =>null, --NUMBER
1482                 Ship_To_Location_Id             =>null, --NUMBER
1483                 Ship_To_Organization_Id         =>null, --NUMBER
1484                 Old_Need_By_Date                =>l_old_need_by_date,
1485                 New_Need_By_Date                =>l_new_need_by_date,
1486                 Approval_Required_Flag          =>null,
1487                 Parent_Change_request_Id        =>l_parent_change_request_id,
1488                 Requester_id                    =>null,
1489                 Old_Supplier_Order_Number       =>null,
1490                 New_Supplier_Order_Number       =>null,
1491                 Old_Supplier_Order_Line_Number  =>null,
1492                 New_Supplier_Order_Line_Number  =>null,
1493                 ADDITIONAL_CHANGES             => null,
1494                 OLD_START_DATE            => l_old_start_date,
1495                 NEW_START_DATE            => l_new_start_date,
1496                 OLD_EXPIRATION_DATE       => l_old_expiration_date,
1497                 NEW_EXPIRATION_DATE       => l_new_expiration_date,
1498                 OLD_AMOUNT     => l_old_amount,
1499                 NEW_AMOUNT     => l_new_amount,
1500                 SUPPLIER_DOC_REF => null,
1501                 SUPPLIER_LINE_REF => null,
1502                 SUPPLIER_SHIPMENT_REF => null,
1503                 NEW_PROGRESS_TYPE => null,
1504                 NEW_PAY_DESCRIPTION => null
1505             );
1506 
1507             temp:=temp+1;
1508         end loop;
1509         close l_pending_change_csr;
1510 
1511         x_progress :='010';
1512         PO_CHG_REQUEST_PVT.validate_change_request (
1513             p_api_version         => 1.0,
1514             p_init_msg_list       => FND_API.G_FALSE,
1515             x_return_status       => l_return_status,
1516             x_msg_data            => l_msg_data,
1517             p_po_header_id        => ll_document_header_id,
1518             p_po_release_id       => ll_po_release_id,
1519             p_revision_num        => ll_document_revision_num,
1520             p_po_change_requests  => my_chg_rec_tbl,
1521             x_online_report_id    => l_online_report_id,
1522             x_pos_errors          => l_pos_errors,
1523             x_doc_check_error_msg => l_doc_check_rec_type);
1524 
1525         x_progress :='011';
1526         if(l_return_status<>'S') then
1527             x_progress :='012';
1528             l_error_message1:=null;
1529             if(l_pos_errors is not null) then
1530                 l_error_message1:=l_pos_errors.text_line(1);
1531             end if;
1532             update po_change_requests
1533                set request_status='REJECTED',
1534                    change_active_flag='N',
1535                    response_date=sysdate,
1536                    response_reason=substr(fnd_message.get_string('PO',
1537                                            'PO_RCO_VALIDATION_ERROR')||':'||
1538                                            l_error_message1, 1, 2000)
1539              where change_request_id in (
1540                     select parent_change_request_id
1541                       from po_change_requests
1542                      where change_request_group_id=p_change_request_group_id
1543                            and request_status='PENDING');
1544 
1545             x_progress :='013';
1546             update po_change_requests
1547                set request_status='REJECTED',
1548                    change_active_flag='N',
1549                    response_date=sysdate,
1550                    response_reason=substr(fnd_message.get_string('PO',
1551                                            'PO_RCO_VALIDATION_ERROR')||':'||
1552                                            l_error_message1, 1, 2000)
1553              where change_request_group_id=p_change_request_group_id
1554                    and request_status='PENDING';
1555             x_progress :='014';
1556         else
1557             x_progress :='015';
1558             CheckPOAutoApproval(p_change_request_group_id);
1559             x_progress :='016';
1560         end if;
1561         x_progress :='017';
1562     end if;
1563 
1564 --    commit;
1565 
1566 exception
1567     when others then
1568         wf_core.context('PO_ReqChangeRequestWF_PVT','ValidateChgAgainstNewPO',x_progress||sqlerrm);
1569         raise;
1570 end ValidateChgAgainstNewPO;
1571 
1572 
1573 /*************************************************************************
1574  * Private Procedure: ProcessBuyerAction
1575  * Effects: This procedure is called to process the buyer's action
1576  *
1577  *          the parameter p_action can be 'CANCELLATION', 'REJECTION'
1578  *          or 'ACCEPTANCE'.
1579  *
1580  *          'REJECTION' means to process buyer's
1581  *          rejection to PO change request. the main task is to
1582  *          reject the corresponding req change.
1583  *
1584  *          'CANCELLATION' is to process buyer's acceptance of cancel
1585  *          request. It will call PO cancel API to cancel the
1586  *          corresponding PO part, and also update the status to 'ACCEPTED'
1587  *          of the req change
1588  *
1589  *          'ACCEPTANCE is to process the buyer's acceptance of
1590  *          change request. It will call movechangetopo to move the accepted
1591  *          change request to PO, and then update the req with the
1592  *          new value. also update the corresponding req change status.
1593  *
1594  *          the process will COMMIT when it exits.
1595  *
1596  * Returns:
1597  ************************************************************************/
1598 procedure ProcessBuyerAction(p_change_request_group_id in number, p_action in varchar2, p_launch_approvals_flag IN VARCHAR2 default 'N', p_supplier_change IN varchar2 default 'N') is
1599 pragma AUTONOMOUS_TRANSACTION;
1600 
1601 x_progress varchar2(100):= '000';
1602 l_request_level PO_CHANGE_REQUESTS.request_level%type;
1603 l_document_type PO_CHANGE_REQUESTS.document_type%type;
1604 l_document_id number;
1605 l_document_line_id number;
1606 l_document_line_id1 number:=null;
1607 l_document_shipment_id number;
1608 l_document_distribution_id number;
1609 l_release_id number;
1610 l_change_request_id number;
1611 l_request_status PO_CHANGE_REQUESTS.request_status%type;
1612 l_requester_id number;
1613 l_return_code number;
1614 l_err_msg varchar2(200);
1615 l_return_msg varchar2(2000);
1616 l_document_subtype varchar2(100);
1617 l_change_reason PO_CHANGE_REQUESTS.request_reason%TYPE;
1618 l_return_status varchar2(1);
1619 
1620 l_old_need_by_date DATE;
1621 l_new_need_by_date DATE;
1622 l_old_price number;
1623 l_new_price number;
1624 l_new_price1 number;
1625 l_old_quantity number;
1626 l_new_quantity number;
1627 l_old_currency_unit_price number;
1628 l_new_currency_unit_price number;
1629 l_new_currency_unit_price1 number;
1630 l_old_start_date date;
1631 l_new_start_date date;
1632 l_old_expiration_date date;
1633 l_new_expiration_date date;
1634 l_old_amount number;
1635 l_new_amount number;
1636 l_old_currency_amount number;
1637 l_new_currency_amount number;
1638 
1639 l_po_cancel_api exception;
1640 i number;
1641 l_group_id number;
1642 
1643 l_num_of_shipments number;
1644 
1645 l_req_doc_id number;
1646 l_req_change_grp_id number;
1647 
1648 /* this is for the date change request id
1649    which isnot tied by parent request id
1650 */
1651 l_date_change_id PO_CHANGE_REQUESTS.change_request_id%type;
1652 l_date_change_id1 PO_CHANGE_REQUESTS.change_request_id%type;
1653 
1654 -- Added variable l_validation_error
1655 l_validation_error PO_CHANGE_REQUESTS.VALIDATION_ERROR%type;
1656 
1657 cursor cancel_request is
1658     select decode (document_type, 'RELEASE', null, document_line_id), document_line_location_id,
1659         change_request_id, request_reason
1660     from po_change_requests
1661     where change_request_group_id=p_change_request_group_id
1662         and request_status='BUYER_APP'
1663         and action_type='CANCELLATION';
1664 
1665 cursor change_request is
1666     select request_level, document_type, document_header_id,
1667             document_line_id, document_distribution_id, po_release_id,
1668             change_request_id, old_need_by_date, new_need_by_date,
1669             old_price, new_price, old_quantity, new_quantity,
1670             old_currency_unit_price, new_currency_unit_price,
1671             old_start_date, new_start_date,
1672             old_expiration_date, new_expiration_date,
1673             old_amount, new_amount,
1674             old_currency_amount, new_currency_amount,
1675             change_request_group_id
1676     from po_change_requests
1677     where change_request_id in
1678             (select parent_change_request_id
1679             from po_change_requests
1680             where   change_request_group_id=p_change_request_group_id
1681                 and request_status='BUYER_APP'
1682                 and action_type='MODIFICATION')
1683     order by document_line_id, document_distribution_id;
1684 
1685 cursor sco_change_request is
1686     select request_level, document_type, document_header_id,
1687             document_line_id, document_distribution_id, po_release_id,
1688             change_request_id, old_need_by_date, new_need_by_date,
1689             old_price, new_price, old_quantity, new_quantity,
1690             old_currency_unit_price, new_currency_unit_price,
1691             old_start_date, new_start_date,
1692             old_expiration_date, new_expiration_date,
1693             old_amount, new_amount,
1694             old_currency_amount, new_currency_amount,
1695             change_request_group_id
1696     from po_change_requests
1697     where change_request_group_id in
1698             (select parent_change_request_id
1699             from po_change_requests
1700             where   change_request_group_id=p_change_request_group_id
1701                 and request_status='BUYER_APP'
1702                 and action_type='MODIFICATION')
1703     order by document_line_id, document_distribution_id;
1704 
1705 cursor l_document_id_csr is
1706     select document_type, document_header_id, po_release_id, nvl(requester_id, created_by)
1707         from po_change_requests
1708         where change_request_group_id =p_change_request_group_id;
1709 
1710 cursor l_exist_change_request_csr is
1711     select change_request_id
1712         from po_change_requests
1713         where change_request_group_id=p_change_request_group_id
1714             and request_status in ('PENDING', 'BUYER_APP')
1715             and action_type='MODIFICATION';
1716 l_doc_check_rec POS_ERR_TYPE;
1717 
1718 --This cursor doesn't include 'amount' change records
1719 -- since for 'amount'change records, both new_start_date and new_expiration_date are null
1720 cursor l_date_change_csr is
1721   select document_line_id,change_request_group_id
1722   from po_change_requests
1723   where document_type = 'REQ'
1724   and change_request_id in
1725     (select parent_change_request_id
1726             from po_change_requests pcr2
1727             where pcr2.change_request_group_id=p_change_request_group_id
1728                   and pcr2.action_type='MODIFICATION'
1729                   and ( pcr2.new_start_date is not null
1730                        or pcr2.new_expiration_date is not null ) );
1731 
1732 
1733 BEGIN
1734 
1735     if(p_action='REJECTION') then
1736     -- this is to handle the buyer rejection
1737         x_progress:='REJECTION:001';
1738         -- fix bug 2733303.
1739         -- when buyer response to the po change, the response_date
1740         -- response_reason and resonded_by is not carried back to
1741         -- requisition change request. thus the requisition history
1742         -- page shows null on those field
1743         update PO_CHANGE_REQUESTS pcr1
1744         set (pcr1.request_status,
1745              pcr1.change_active_flag,
1746              pcr1.response_date,
1747              pcr1.response_reason,
1748              pcr1.responded_by,
1749              pcr1.last_updated_by,
1750              pcr1.last_update_login,
1751              pcr1.last_update_date) =
1752             (select 'REJECTED',
1753                     'N',
1754                     pcr2.response_date,
1755                     pcr2.response_reason,
1756                     pcr2.responded_by,
1757                     fnd_global.user_id,
1758                     fnd_global.login_id,
1759                     sysdate
1760              from po_change_requests pcr2
1761              where pcr2.parent_change_request_id=pcr1.change_request_id
1762                    and pcr2.change_request_group_id=p_change_request_group_id
1763                    and pcr2.request_status='REJECTED')
1764         where pcr1.change_request_id in
1765             (select parent_change_request_id
1766                 from po_change_requests
1767                 where change_request_group_id=p_change_request_group_id
1768                     and request_status='REJECTED');
1769 
1770         x_progress:='REJECTION:002';
1771 
1772         /*
1773            because req's change requests has separate records
1774            for start date and end date
1775            while only 1 merged record for po counter part,
1776            the problem is that the parent request id will
1777            only identify one of the record.
1778            to work around this imperfection in data model,
1779            the following sql finds the second record who shares the
1780            1) same request group id
1781            2) within the same req line
1782            because this situation only happens to temp labor line,
1783            let it only applies to temp labor line.
1784         */
1785 
1786         update PO_CHANGE_REQUESTS pcr1
1787         set (pcr1.request_status,
1788              pcr1.change_active_flag,
1789              pcr1.response_date,
1790              pcr1.response_reason,
1791              pcr1.responded_by,
1792              pcr1.last_updated_by,
1793              pcr1.last_update_login,
1794              pcr1.last_update_date) =
1795              (select 'REJECTED',
1796                     'N',
1797                     pcr2.response_date,
1798                     pcr2.response_reason,
1799                     pcr2.responded_by,
1800                     fnd_global.user_id,
1801                     fnd_global.login_id,
1802                     sysdate
1803              from po_change_requests pcr2
1804              where pcr2.parent_change_request_id in
1805                    ( select pcr3.change_request_id
1806                      from  po_change_requests pcr3
1807                      where
1808                      pcr3.change_request_group_id=pcr1.change_request_group_id
1809                      and pcr3.document_line_id = pcr1.document_line_id
1810                    )
1811                    and pcr2.change_request_group_id=p_change_request_group_id
1812                    and pcr2.request_status='REJECTED' and rownum=1
1813              )
1814         where  pcr1.change_request_id in (
1815              select pcr5.change_request_id
1816              from
1817              po_change_requests pcr,
1818              po_change_requests pcr4,
1819              po_change_requests pcr5,
1820              po_requisition_lines_all por
1821              where
1822              pcr.change_request_group_id=p_change_request_group_id
1823              and pcr.parent_change_request_id=pcr4.change_request_id
1824              and pcr4.change_request_group_id=pcr5.change_request_group_id
1825              and pcr4.document_line_id = pcr5.document_line_id
1826              and pcr.request_status='REJECTED'
1827              and por.requisition_line_id = pcr4.document_line_id
1828              and por.purchase_basis='TEMP LABOR' );
1829 
1830         x_progress:='REJECTION:003';
1831 
1832         -- when change request is rejected,
1833         -- set po shipments approved_flag column value to 'Y'
1834         -- since when change request is submitted, it is set to 'R'
1835         UPDATE po_line_locations_all
1836         SET
1837           approved_flag = 'Y',
1838           last_update_date = sysdate,
1839           last_updated_by = fnd_global.user_id,
1840           last_update_login = fnd_global.login_id
1841         WHERE line_location_id IN
1842           (SELECT document_line_location_id
1843            FROM po_change_requests
1844            WHERE
1845              request_level = 'SHIPMENT' AND
1846              change_request_group_id = p_change_request_group_id AND
1847              action_type IN ('MODIFICATION', 'CANCELLATION') AND
1848              initiator = 'REQUESTER') AND
1849            approved_flag = 'R';
1850 
1851         commit;
1852 
1853         x_progress:='REJECTION:004';
1854 
1855     elsif (p_action='CANCELLATION') then
1856     -- this is to handle the buyer acceptance
1857     -- of cancel request
1858         x_progress:='CANCELLATION:001';
1859         begin
1860 
1861         open l_document_id_csr;
1862         fetch l_document_id_csr
1863          into l_document_type,
1864               l_document_id,
1865               l_release_id,
1866               l_requester_id;
1867         close l_document_id_csr;
1868 
1869         -- before call PO CANCEL API, the document
1870         -- has to be in 'APPROVED' status, otherwise
1871         -- the api will fail. so change the document
1872         -- to 'APPROVED' status first.
1873         if(l_document_type= 'PO') then
1874             l_document_subtype := 'STANDARD';
1875 
1876             update po_headers_all
1877             set AUTHORIZATION_STATUS = 'APPROVED',
1878                 approved_flag='Y',
1879                 CHANGE_REQUESTED_BY=null,
1880                 last_updated_by         = fnd_global.user_id,
1881                 last_update_login       = fnd_global.login_id,
1882                 last_update_date        = sysdate
1883             where po_header_id = l_document_id;
1884         else
1885             l_document_subtype := 'BLANKET';
1886             l_document_type:= 'RELEASE';
1887 
1888             update po_releases_all
1889             set AUTHORIZATION_STATUS = 'APPROVED',
1890                 approved_flag='Y',
1891                 CHANGE_REQUESTED_BY=null,
1892                 last_updated_by         = fnd_global.user_id,
1893                 last_update_login       = fnd_global.login_id,
1894                 last_update_date        = sysdate
1895             where po_release_id = l_release_id;
1896         end if;
1897 
1898         x_progress:='CANCELLATION:002';
1899         commit;
1900 
1901         open cancel_request;
1902         loop
1903             fetch cancel_request into l_document_line_id, l_document_shipment_id,
1904                     l_change_request_id, l_change_reason;
1905             exit when cancel_request%NOTFOUND;
1906             x_progress:='CANCELLATION:002'||to_char(l_change_request_id);
1907 
1908             if (l_document_type <> 'RELEASE') then
1909               select count(1)
1910               into l_num_of_shipments
1911               from po_line_locations_all
1912               where po_line_id = l_document_line_id
1913                     and nvl(cancel_flag, 'N') = 'N';
1914 
1915               if (l_num_of_shipments = 1) then
1916                 l_document_shipment_id := null;
1917               end if;
1918             end if;
1919 
1920             PO_Document_Control_GRP.control_document
1921                     (p_api_version  => 1.0,
1922                     p_init_msg_list => FND_API.G_FALSE,
1923                     p_commit     => FND_API.G_TRUE,
1924                     x_return_status  => l_return_status,
1925                     p_doc_type    => l_document_type,
1926                     p_doc_subtype  => l_document_subtype ,
1927                     p_doc_id    => l_document_id,
1928                     p_doc_num    => null,
1929                     p_release_id  => l_release_id,
1930                     p_release_num  => null,
1931                     p_doc_line_id  => l_document_line_id,
1932                     p_doc_line_num  => null,
1933                     p_doc_line_loc_id  => l_document_shipment_id,
1934                     p_doc_shipment_num => null,
1935                     p_source     => null,
1936                     p_action      => 'CANCEL',
1937                     p_action_date   => sysdate,
1938                     p_cancel_reason  => l_change_reason,
1939                     p_cancel_reqs_flag  => 'Y',
1940                     p_print_flag     => 'N',
1941                     p_note_to_vendor  =>null);
1942             x_progress:='CANCELLATION:003-'||to_char(l_change_request_id)
1943                          ||'-'||l_return_status;
1944             if(l_return_status is null or l_return_status <> 'S') then
1945                 --Instead of raising exception, just exit the loop to set
1946                 --the authorization_status back to 'IN PROCESS'
1947                 --raise l_po_cancel_api;
1948                 exit;
1949             end if;
1950         end loop;
1951         close cancel_request;
1952         x_progress:='CANCELLATION:004';
1953 
1954         --Update change request status for success case
1955         IF(l_return_status = FND_API.g_ret_sts_success) THEN
1956         -- accept the po cancel request and req cancel request
1957         -- fix bug 2733303.
1958         -- when buyer response to the po change, the response_date
1959         -- response_reason and resonded_by is not carried back to
1960         -- requisition change request. thus the requisition history
1961         -- page shows null on those field
1962 
1963         update PO_CHANGE_REQUESTS pcr1
1964         set (pcr1.request_status,
1965              pcr1.change_active_flag,
1966              pcr1.response_date,
1967              pcr1.response_reason,
1968              pcr1.responded_by,
1969              pcr1.last_updated_by,
1970              pcr1.last_update_login,
1971              pcr1.last_update_date) =
1972             (select 'ACCEPTED',
1973                     'N',
1974                     pcr2.response_date,
1975                     pcr2.response_reason,
1976                     pcr2.responded_by,
1977                     fnd_global.user_id,
1978                     fnd_global.login_id,
1979                     sysdate
1980              from po_change_requests pcr2
1981              where pcr2.parent_change_request_id=pcr1.change_request_id
1982                    and pcr2.change_request_group_id=p_change_request_group_id
1983                    and pcr2.request_status in ('BUYER_APP', 'ACCEPTED')
1984                    and pcr2.action_type='CANCELLATION')
1985         where pcr1.change_request_id in
1986             (select parent_change_request_id
1987              from po_change_requests
1988              where change_request_group_id=p_change_request_group_id
1989                  and request_status in ('BUYER_APP', 'ACCEPTED')
1990                  and action_type='CANCELLATION');
1991         x_progress:='CANCELLATION:005';
1992 
1993         update PO_CHANGE_REQUESTS
1994         set request_status='ACCEPTED',
1995             change_active_flag='N'
1996         where change_request_group_id=p_change_request_group_id
1997             and request_status='BUYER_APP'
1998             and action_type='CANCELLATION';
1999         END IF;
2000         x_progress:='CANCELLATION:006';
2001 
2002         -- In case of success/error case update back the authorization_status
2003         -- if there are pending change requests
2004         IF( l_return_status in ('S', 'E')) THEN
2005         -- if there is still pending change request, update the
2006         -- status of the document back to 'IN PROCESS'
2007         open l_exist_change_request_csr;
2008         fetch l_exist_change_request_csr into l_change_request_id;
2009         if l_exist_change_request_csr%FOUND then
2010             x_progress:='CANCELLATION:007';
2011             if(l_document_type= 'PO') then
2012 
2013                 -- fix bug 2733373. when change is submitted, and wait
2014                 -- for buyer's response, we just set the status to
2015                 -- 'IN PROCESS', but not the approved_flag
2016                 update po_headers_all   set
2017                 AUTHORIZATION_STATUS = 'IN PROCESS',
2018 --                approved_flag='N',
2019                 CHANGE_REQUESTED_BY='REQUESTER',
2020                 last_updated_by         = fnd_global.user_id,
2021                 last_update_login       = fnd_global.login_id,
2022                 last_update_date        = sysdate
2023                 where po_header_id = l_document_id;
2024             else
2025                 -- fix bug 2733373. when change is submitted, and wait
2026                 -- for buyer's response, we just set the status to
2027                 -- 'IN PROCESS', but not the approved_flag
2028                 update po_releases_all   set
2029                 AUTHORIZATION_STATUS = 'IN PROCESS',
2030 --                approved_flag='N',
2031                 CHANGE_REQUESTED_BY='REQUESTER',
2032                 last_updated_by         = fnd_global.user_id,
2033                 last_update_login       = fnd_global.login_id,
2034                 last_update_date        = sysdate
2035                 where po_release_id = l_release_id;
2036             end if;
2037             x_progress:='CANCELLATION:008';
2038         end if;
2039         close l_exist_change_request_csr;
2040         END IF;
2041 
2042         -- Raise the exceptions here, to handle the error case to
2043         -- continue workflow. In case of unexpected exception, raise back the
2044         -- error and make the error process to handle them.
2045         IF (l_return_status = FND_API.g_ret_sts_error) THEN
2046           RAISE FND_API.g_exc_error;
2047         ELSIF (l_return_status = FND_API.g_ret_sts_unexp_error) THEN
2048           RAISE FND_API.g_exc_unexpected_error;
2049         END IF;
2050 
2051         exception
2052             -- In case of error, set the change request status to Rejected and
2053             -- continue the workflow.
2054             when FND_API.g_exc_error then
2055                 -- automacally reject the cancel request.
2056                 -- additionally updating the validation_error field with the message stack populated
2057                 -- by cancel api(if no. of message is 1).
2058 
2059                 IF(FND_MSG_PUB.COUNT_MSG = 1) THEN
2060                   l_validation_error := fnd_msg_pub.get(p_encoded => 'F');
2061                 END IF;
2062 
2063                 update PO_CHANGE_REQUESTS
2064                 set request_status='REJECTED',
2065                     change_active_flag='N',
2066                     response_date=sysdate,
2067                     response_reason=fnd_message.get_string('PO', 'PO_RCO_EXCEPTION_WHEN_PROCESS') ||
2068                        decode(l_validation_error, NULL, '', ' : ' || l_validation_error ),
2069                     validation_error =l_validation_error
2070                 where change_request_id in (select parent_change_request_id
2071                                     from po_change_requests
2072                                     where change_request_group_id=p_change_request_group_id
2073                                         and request_status='BUYER_APP'
2074                                         and action_type='CANCELLATION');
2075 
2076                 update PO_CHANGE_REQUESTS
2077                 set request_status='REJECTED',
2078                     change_active_flag='N',
2079                     response_date=sysdate,
2080                     response_reason=fnd_message.get_string('PO', 'PO_RCO_EXCEPTION_WHEN_PROCESS') ||
2081                        decode(l_validation_error, NULL, '', ' : ' || l_validation_error ),
2082                     validation_error =l_validation_error
2083                 where change_request_group_id=p_change_request_group_id
2084                     and request_status='BUYER_APP'
2085                     and action_type='CANCELLATION';
2086                 --Don't raise for error case. continue workflow
2087                 --raise;
2088             when others then
2089               rollback;
2090               raise;
2091         end;
2092 
2093     elsif (p_action='ACCEPTANCE') then
2094         x_progress:='ACCEPTANCE:001';
2095         --Set savepoint for rolling back in case the movechangetopo ends with
2096         --error
2097 
2098         SAVEPOINT PO_REQCHANGEREQUESTWF_PVT_SP;
2099 
2100         begin
2101             if (p_supplier_change = 'Y') then
2102               open sco_change_request;
2103             else
2104                open change_request;
2105             end if;
2106 
2107             loop
2108               if (p_supplier_change = 'Y') then
2109 
2110                 fetch sco_change_request
2111                 into l_request_level,
2112                      l_document_type,
2113                      l_document_id,
2114                      l_document_line_id,
2115                      l_document_distribution_id,
2116                      l_release_id,
2117                      l_change_request_id,
2118                      l_old_need_by_date,
2119                      l_new_need_by_date,
2120                      l_old_price,
2121                      l_new_price,
2122                      l_old_quantity,
2123                      l_new_quantity,
2124                      l_old_currency_unit_price,
2125                      l_new_currency_unit_price,
2126                      l_old_start_date,
2127                      l_new_start_date,
2128                      l_old_expiration_date,
2129                      l_new_expiration_date,
2130                      l_old_amount,
2131                      l_new_amount,
2132                      l_old_currency_amount,
2133                      l_new_currency_amount,
2134                      l_group_id;
2135                  exit when sco_change_request%NOTFOUND;
2136               else
2137 
2138                 fetch change_request
2139                 into l_request_level,
2140                      l_document_type,
2141                      l_document_id,
2142                      l_document_line_id,
2143                      l_document_distribution_id,
2144                      l_release_id,
2145                      l_change_request_id,
2146                      l_old_need_by_date,
2147                      l_new_need_by_date,
2148                      l_old_price,
2149                      l_new_price,
2150                      l_old_quantity,
2151                      l_new_quantity,
2152                      l_old_currency_unit_price,
2153                      l_new_currency_unit_price,
2154                      l_old_start_date,
2155                      l_new_start_date,
2156                      l_old_expiration_date,
2157                      l_new_expiration_date,
2158                      l_old_amount,
2159                      l_new_amount,
2160                      l_old_currency_amount,
2161                      l_new_currency_amount,
2162                      l_group_id;
2163                 exit when change_request%NOTFOUND;
2164               end if;
2165 
2166                 x_progress:='ACCEPTANCE:005-'||to_char(l_change_request_id);
2167 
2168                 if(l_document_line_id1 is null
2169                         or l_document_line_id1<>l_document_line_id) then
2170                     if(l_new_need_by_date is not null
2171                             or l_new_quantity is not null) then
2172 
2173                         l_document_line_id1:=l_document_line_id;
2174 
2175                         begin
2176                             select new_price, new_currency_unit_price
2177                             into l_new_price1, l_new_currency_unit_price1
2178                             from po_change_requests
2179                             where change_request_group_id=l_group_id
2180                                   and document_line_id=l_document_line_id
2181                                   and action_type='DERIVED'
2182                                   and new_price is not null;
2183 
2184                             UpdateReqLine(l_document_line_id,
2185                                null,
2186                                l_new_price1,
2187                                l_new_currency_unit_price1, null, null);
2188                         exception
2189                             when others then null;
2190                         end;
2191                     end if;
2192                 end if;
2193 
2194                     -- move req change to req
2195                 if(l_request_level='LINE') then
2196 
2197                     -- update start_date and end_date
2198                     -- select new_start_date and new_expiration_date from po_change_requests
2199                     --  req's change requests has separate records
2200                     --  for start date and end date
2201                     --  while only 1 merged record for po counter part,
2202                     --  the change_request cursor doesn't capture both start and end date change.
2203                     BEGIN
2204                       select new_start_date
2205                       into l_new_start_date
2206                       from po_change_requests
2207                       where change_request_group_id = l_group_id
2208                       and document_type = 'REQ'
2209                       and document_line_id = l_document_line_id
2210                       and request_level= 'LINE'
2211                       and action_type = 'MODIFICATION'
2212                       and new_expiration_date is null
2213                       and new_start_date is not null;
2214 
2215                     EXCEPTION
2216                       when no_data_found then
2217                         l_new_start_date:= null;
2218                     END;
2219 
2220                     BEGIN
2221                       select new_expiration_date
2222                       into l_new_expiration_date
2223                       from po_change_requests
2224                       where change_request_group_id = l_group_id
2225                       and document_type = 'REQ'
2226                       and document_line_id = l_document_line_id
2227                       and request_level= 'LINE'
2228                       and action_type = 'MODIFICATION'
2229                       and new_start_date is null
2230                       and new_expiration_date is not null;
2231 
2232                     EXCEPTION
2233                       when no_data_found then
2234                         l_new_expiration_date:= null;
2235                     END;
2236 
2237                     UpdateReqLine(l_document_line_id,
2238                                l_new_need_by_date,
2239                                l_new_price,
2240                                l_new_currency_unit_price,
2241                                l_new_start_date,
2242                                l_new_expiration_date);
2243 
2244                 elsif(l_request_level='DISTRIBUTION') then
2245                     UpdateReqDistribution(l_document_line_id,
2246                                l_document_distribution_id,
2247                                l_new_quantity,
2248                                l_old_quantity,
2249                                l_new_amount,
2250                                l_old_amount,
2251                                l_new_currency_amount,
2252                                l_old_currency_amount);
2253                 end if;
2254                 x_progress:='ACCEPTANCE:006-'||to_char(l_change_request_id);
2255 
2256             end loop;
2257             x_progress:='ACCEPTANCE:007';
2258 
2259             if (p_supplier_change = 'Y') then
2260               close sco_change_request;
2261             else
2262               close change_request;
2263             end if;
2264 
2265             -- call move to PO to update the PO
2266             open l_document_id_csr;
2267             fetch l_document_id_csr
2268             into l_document_type,
2269                  l_document_id,
2270                  l_release_id,
2271                  l_requester_id;
2272             close l_document_id_csr;
2273             x_progress:='ACCEPTANCE:008';
2274 
2275 
2276             PO_CHANGE_RESPONSE_PVT.MoveChangeToPO(
2277                     p_api_version => 1.0,
2278                     x_return_status =>l_return_status,
2279                     p_po_header_id =>l_document_id,
2280                     p_po_release_id =>l_release_id,
2281                     p_change_request_group_id =>p_change_request_group_id,
2282                     p_user_id =>l_requester_id,
2283                     x_return_code =>l_return_code ,
2284                     x_err_msg => l_err_msg,
2285                     x_doc_check_rec_type =>l_doc_check_rec,
2286                     p_launch_approvals_flag => p_launch_approvals_flag);
2287             x_progress:='ACCEPTANCE:009-'||l_return_status;
2288 
2289             IF(  l_doc_check_rec IS NOT NULL AND
2290                  l_doc_check_rec.message_name.Count > 0) THEN
2291               -- Populate the first validation error message
2292               -- for populating the response reason
2293               l_validation_error := l_doc_check_rec.text_line(1);
2294             END IF;
2295 
2296 
2297             -- Api returns 0,1 or 2. In case of 1, rollback the changes
2298             -- and reject the change request.
2299             /*
2300             if(l_return_code<>0) then
2301                 raise l_po_cancel_api;
2302             end if;
2303             */
2304             IF (l_return_code = 1) THEN
2305               RAISE FND_API.g_exc_error;
2306             ELSIF (l_return_code= 2) THEN
2307               RAISE FND_API.g_exc_unexpected_error;
2308             END IF;
2309 
2310         -- fix bug 2733303.
2311         -- when buyer response to the po change, the response_date
2312         -- response_reason and resonded_by is not carried back to
2313         -- requisition change request. thus the requisition history
2314         -- page shows null on those field
2315             update PO_CHANGE_REQUESTS pcr1
2316             set (pcr1.request_status,
2317                  pcr1.change_active_flag,
2318                  pcr1.response_date,
2319                  pcr1.response_reason,
2320                  pcr1.responded_by,
2321                  pcr1.last_updated_by,
2322                  pcr1.last_update_login,
2323                  pcr1.last_update_date) =
2324                 (select 'ACCEPTED',
2325                         'N',
2326                         pcr2.response_date,
2327                         pcr2.response_reason,
2328                         pcr2.responded_by,
2329                         fnd_global.user_id,
2330                         fnd_global.login_id,
2331                         sysdate
2332                  from po_change_requests pcr2
2333                  where pcr2.parent_change_request_id=pcr1.change_request_id
2334                        and pcr2.change_request_group_id=p_change_request_group_id
2335                        and pcr2.request_status='BUYER_APP'
2336                        and pcr2.action_type='MODIFICATION')
2337             where pcr1.change_request_id in
2338                 (select parent_change_request_id
2339                  from po_change_requests
2340                  where change_request_group_id=p_change_request_group_id
2341                      and request_status='BUYER_APP'
2342                      and action_type='MODIFICATION');
2343 
2344             x_progress:='ACCEPTANCE:010';
2345 
2346             -- When there are start_date and end_date changes, above 'update' doesn't update all of the REQ change requests
2347             -- This is because for REQ change requests, start_date and end_date changes for the same req line are stored in two different rows;
2348             -- while for PO change requests, start_date and end_date changes are stored in one row.
2349             -- Below we update the status for those remaining records.
2350 
2351             open l_date_change_csr;
2352             loop
2353               fetch l_date_change_csr
2354               into  l_req_doc_id,
2355                     l_req_change_grp_id;
2356               exit when l_date_change_csr%NOTFOUND;
2357 
2358             if (l_req_doc_id is not null) then
2359               update PO_CHANGE_REQUESTS pcr1
2360               set (pcr1.request_status,
2361                  pcr1.change_active_flag,
2362                  pcr1.response_date,
2363                  pcr1.response_reason,
2364                  pcr1.responded_by,
2365                  pcr1.last_updated_by,
2366                  pcr1.last_update_login,
2367                  pcr1.last_update_date) =
2368                 (select 'ACCEPTED',
2369                         'N',
2370                         pcr2.response_date,
2371                         pcr2.response_reason,
2372                         pcr2.responded_by,
2373                         fnd_global.user_id,
2374                         fnd_global.login_id,
2375                         sysdate
2376                  from po_change_requests pcr2
2377                  where pcr2.document_type= 'REQ'
2378                        and pcr2.change_request_group_id=l_req_change_grp_id
2379                        and pcr2.document_line_id = l_req_doc_id
2380                        and pcr2.request_status= 'ACCEPTED'
2381                        and pcr2.action_type ='MODIFICATION'
2382                        and pcr2.request_level ='LINE')
2383 
2384                 where pcr1.change_request_group_id =l_req_change_grp_id
2385                 and pcr1.document_line_id = l_req_doc_id
2386                 and pcr1.request_status <>'ACCEPTED'
2387                 and pcr1.action_type='MODIFICATION'
2388                 and pcr1.request_level = 'LINE';
2389 
2390             end if;
2391             end loop;
2392 
2393             close l_date_change_csr;
2394 
2395             x_progress:='ACCEPTANCE:011';
2396 
2397         exception
2398             --In case of error, continue workflow
2399             when FND_API.g_exc_error then
2400             rollback to PO_REQCHANGEREQUESTWF_PVT_SP;
2401 
2402                 update PO_CHANGE_REQUESTS
2403                 set request_status='REJECTED',
2404                     change_active_flag='N',
2405                     response_date=sysdate,
2406                     response_reason=fnd_message.get_string('PO', 'PO_RCO_EXCEPTION_WHEN_PROCESS') ||
2407                        decode(l_validation_error, NULL, '', ' : ' || l_validation_error ),
2408                     validation_error=l_err_msg
2409                 where change_request_id in (select parent_change_request_id
2410                                     from po_change_requests
2411                                     where change_request_group_id=p_change_request_group_id
2412                                         and request_status='BUYER_APP'
2413                                         and action_type='MODIFICATION');
2414 
2415                 update PO_CHANGE_REQUESTS
2416                 set request_status='REJECTED',
2417                     change_active_flag='N',
2418                     response_date=sysdate,
2419                     response_reason=fnd_message.get_string('PO', 'PO_RCO_EXCEPTION_WHEN_PROCESS') ||
2420                         decode(l_validation_error, NULL, '', ' : ' || l_validation_error ),
2421                     validation_error=l_err_msg
2422                 where change_request_group_id=p_change_request_group_id
2423                     and request_status='BUYER_APP'
2424                     and action_type='MODIFICATION';
2425                 --comment raise and continue workflow
2426                 --raise;
2427                  when others then
2428                  rollback to PO_REQCHANGEREQUESTWF_PVT_SP ;
2429                 raise;
2430         end;
2431 
2432     end if;
2433 
2434     commit;
2435 
2436 EXCEPTION
2437   WHEN OTHERS THEN
2438     wf_core.context('PO_ReqChangeRequestWF_PVT','ProcessBuyerAction',x_progress||sqlerrm);
2439     raise;
2440 
2441 END ProcessBuyerAction;
2442 
2443 /*************************************************************************
2444  * Private Procedure: ProcessSCOAcceptance
2445  * Effects:
2446  *
2447  * Returns:
2448  ************************************************************************/
2449 procedure ProcessSCOAcceptance(p_change_request_group_id in number,p_launch_approvals_flag IN VARCHAR2 default 'N') is
2450 pragma AUTONOMOUS_TRANSACTION;
2451 
2452 x_progress varchar2(100):= '000';
2453 l_request_level PO_CHANGE_REQUESTS.request_level%type;
2454 l_document_type PO_CHANGE_REQUESTS.document_type%type;
2455 l_document_id number;
2456 l_document_line_id number;
2457 l_document_line_id1 number:=null;
2458 l_document_shipment_id number;
2459 l_document_distribution_id number;
2460 l_release_id number;
2461 l_change_request_id number;
2462 l_request_status PO_CHANGE_REQUESTS.request_status%type;
2463 l_requester_id number;
2464 l_return_code number;
2465 l_err_msg varchar2(200);
2466 l_return_msg varchar2(2000);
2467 l_document_subtype varchar2(100);
2468 l_change_reason PO_CHANGE_REQUESTS.request_reason%TYPE;
2469 l_return_status varchar2(1);
2470 
2471 l_old_need_by_date DATE;
2472 l_new_need_by_date DATE;
2473 l_old_price number;
2474 l_new_price number;
2475 l_new_price1 number;
2476 l_old_quantity number;
2477 l_new_quantity number;
2478 l_old_currency_unit_price number;
2479 l_new_currency_unit_price number;
2480 l_new_currency_unit_price1 number;
2481 l_old_start_date date;
2482 l_new_start_date date;
2483 l_old_expiration_date date;
2484 l_new_expiration_date date;
2485 l_old_amount number;
2486 l_new_amount number;
2487 l_old_currency_amount number;
2488 l_new_currency_amount number;
2489 
2490 l_po_cancel_api exception;
2491 i number;
2492 l_group_id number;
2493 
2494 l_num_of_shipments number;
2495 
2496 /* this is for the date change request id
2497    which isnot tied by parent request id
2498 */
2499 l_date_change_id PO_CHANGE_REQUESTS.change_request_id%type;
2500 l_date_change_id1 PO_CHANGE_REQUESTS.change_request_id%type;
2501 
2502 -- Added variable l_validation_error
2503 l_validation_error PO_CHANGE_REQUESTS.VALIDATION_ERROR%type;
2504 l_doc_check_rec POS_ERR_TYPE;
2505 
2506 l_temp_date date;
2507 l_temp_reason varchar2(1000);
2508 l_temp_responder number;
2509 
2510 
2511 cursor sco_change_request is
2512     select request_level, document_type, document_header_id,
2513             document_line_id, document_distribution_id, po_release_id,
2514             change_request_id, old_need_by_date, new_need_by_date,
2515             old_price, new_price, old_quantity, new_quantity,
2516             old_currency_unit_price, new_currency_unit_price,
2517             old_start_date, new_start_date,
2518             old_expiration_date, new_expiration_date,
2519             old_amount, new_amount,
2520             old_currency_amount, new_currency_amount,
2521             change_request_group_id
2522     from po_change_requests
2523     where change_request_group_id in
2524             (select parent_change_request_id
2525             from po_change_requests
2526             where   change_request_group_id=p_change_request_group_id
2527                 and request_status='BUYER_APP'
2528                 and action_type='MODIFICATION')
2529     order by document_line_id, document_distribution_id;
2530 
2531 cursor l_document_id_csr is
2532     select document_type, document_header_id, po_release_id, nvl(requester_id, created_by)    from po_change_requests
2533     where change_request_group_id =p_change_request_group_id;
2534 
2535 BEGIN
2536 
2537         x_progress:='ACCEPTANCE:001';
2538 
2539          --Set savepoint for rolling back in case the movechangetopo ends with
2540          --error
2541 
2542         SAVEPOINT PO_REQCHANGEREQUESTWF_PVT_SP;
2543 
2544         begin
2545              open sco_change_request;
2546              loop
2547                  fetch sco_change_request
2548                  into l_request_level,
2549                      l_document_type,
2550                      l_document_id,
2551                      l_document_line_id,
2552                      l_document_distribution_id,
2553                      l_release_id,
2554                      l_change_request_id,
2555                      l_old_need_by_date,
2556                      l_new_need_by_date,
2557                      l_old_price,
2558                      l_new_price,
2559                      l_old_quantity,
2560                      l_new_quantity,
2561                      l_old_currency_unit_price,
2562                      l_new_currency_unit_price,
2563                      l_old_start_date,
2564                      l_new_start_date,
2565                      l_old_expiration_date,
2566                      l_new_expiration_date,
2567                      l_old_amount,
2568                      l_new_amount,
2569                      l_old_currency_amount,
2570                      l_new_currency_amount,
2571                      l_group_id;
2572                 exit when sco_change_request%NOTFOUND;
2573 
2574                 x_progress:='ACCEPTANCE:002-'||to_char(l_change_request_id);
2575 
2576 
2577                 if(l_document_line_id1 is null
2578                         or l_document_line_id1<>l_document_line_id) then
2579                      if(l_new_need_by_date is not null
2580                             or l_new_quantity is not null) then
2581 
2582                         l_document_line_id1:=l_document_line_id;
2583 
2584                         begin
2585                             select new_price, new_currency_unit_price
2586                             into l_new_price1, l_new_currency_unit_price1
2587                             from po_change_requests
2588                             where change_request_group_id=l_group_id
2589                                   and document_line_id=l_document_line_id
2590                                   and action_type='DERIVED'
2591                                   and new_price is not null;
2592 
2593                             UpdateReqLine(l_document_line_id,
2594                                null,
2595                                l_new_price1,
2596                                l_new_currency_unit_price1, null, null);
2597                         exception
2598                             when others then null;
2599                         end;
2600                      end if;
2601                 end if;  -- document_id1
2602 
2603                  -- move req change to req
2604                 if(l_request_level='LINE') then
2605                     /*
2606                     because req's change requests has separate records
2607                     for start date and end date
2608                     while only 1 merged record for po counter part,
2609                     the cursor does not capture both start and end dates.
2610                     */
2611                    begin
2612                       select nvl(pcr1.new_start_date, pcr2.new_start_date),
2613                              nvl(pcr1.new_expiration_date, pcr2.new_expiration_date),
2614                              pcr1.change_request_id,
2615                              pcr2.change_request_id
2616                       into   l_new_start_date,
2617                              l_new_expiration_date,
2618                              l_date_change_id1,
2619                              l_date_change_id
2620                       from po_change_requests pcr1, po_change_requests pcr2
2621                       where pcr1.change_request_group_id = pcr2.change_request_group_id
2622                           --  and pcr1.change_request_id in
2623                               and pcr1.change_request_group_id in
2624                                (select parent_change_request_id
2625                                 from po_change_requests
2626                                 where   change_request_group_id=p_change_request_group_id
2627                                          and request_status='BUYER_APP'
2628                                         and action_type='MODIFICATION')
2629                            and (pcr2.new_start_date is not null
2630                                 or pcr2.new_expiration_date is not null)
2631                            and pcr1.change_request_id <> pcr2.change_request_id
2632                            and (pcr1.new_start_date is not null
2633                                 or pcr1.new_expiration_date is not null)
2634                            and pcr2.request_level='LINE'
2635                            and pcr2.action_type='MODIFICATION';
2636 
2637                     exception
2638                       when others then
2639                       null; -- not (both begin and end dates are changed)
2640                     end;
2641 
2642                     UpdateReqLine(l_document_line_id,
2643                                l_new_need_by_date,
2644                                l_new_price,
2645                                l_new_currency_unit_price,
2646                                l_new_start_date,
2647                                l_new_expiration_date);
2648                 elsif(l_request_level='DISTRIBUTION') then
2649                     UpdateReqDistribution(l_document_line_id,
2650                                l_document_distribution_id,
2651                                l_new_quantity,
2652                                l_old_quantity,
2653                                l_new_amount,
2654                                l_old_amount,
2655                                l_new_currency_amount,
2656                                l_old_currency_amount);
2657                 end if;
2658                 x_progress:='ACCEPTANCE:003-'||to_char(l_change_request_id);
2659 
2660 
2661              end loop;
2662             x_progress:='ACCEPTANCE:004';
2663 
2664             close sco_change_request;
2665 
2666              -- call move to PO to update the PO
2667             open l_document_id_csr;
2668             fetch l_document_id_csr
2669             into l_document_type,
2670                  l_document_id,
2671                  l_release_id,
2672                  l_requester_id;
2673             close l_document_id_csr;
2674             x_progress:='ACCEPTANCE:005';
2675 
2676 
2677             PO_CHANGE_RESPONSE_PVT.MoveChangeToPO(
2678                     p_api_version => 1.0,
2679                     x_return_status =>l_return_status,
2680                     p_po_header_id =>l_document_id,
2681                     p_po_release_id =>l_release_id,
2682                     p_change_request_group_id =>p_change_request_group_id,
2683                     p_user_id =>l_requester_id,
2684                     x_return_code =>l_return_code ,
2685                     x_err_msg => l_err_msg,
2686                     x_doc_check_rec_type =>l_doc_check_rec,
2687                     p_launch_approvals_flag => p_launch_approvals_flag);
2688             x_progress:='ACCEPTANCE:006-'||l_return_status;
2689 
2690             -- Api returns 0,1 or 2. In case of 1, rollback the changes
2691             -- and reject the change request.
2692 
2693             IF (l_return_code = 1) THEN
2694               RAISE FND_API.g_exc_error;
2695             ELSIF (l_return_code= 2) THEN
2696 
2697               RAISE FND_API.g_exc_unexpected_error;
2698             END IF;
2699 
2700         -- fix bug 2733303.
2701         -- when buyer response to the po change, the response_date
2702         -- response_reason and resonded_by is not carried back to
2703         -- requisition change request. thus the requisition history
2704         -- page shows null on those field
2705 
2706         -- PO can't approve line by line.
2707         -- We get the date,reason and responder of one PO line and update for all req lines.
2708             select   pcr.response_date,
2709                      pcr.response_reason,
2710                      pcr.responded_by
2711             into    l_temp_date,
2712                     l_temp_reason,
2713                     l_temp_responder
2714             from po_change_requests pcr
2715             where pcr.change_request_group_id=p_change_request_group_id
2716             and pcr.action_type='MODIFICATION'
2717             and rownum=1;
2718 
2719             update PO_CHANGE_REQUESTS pcr1
2720               set (pcr1.request_status,
2721                    pcr1.change_active_flag,
2722                    pcr1.response_date,
2723                    pcr1.response_reason,
2724                    pcr1.responded_by,
2725                    pcr1.last_updated_by,
2726                    pcr1.last_update_login,
2727                    pcr1.last_update_date) =
2728                    (select 'ACCEPTED',
2729                         'N',
2730                         l_temp_date,
2731                         l_temp_reason,
2732                         l_temp_responder,
2733                         fnd_global.user_id,
2734                         fnd_global.login_id,
2735                         sysdate
2736                   from dual)
2737               where pcr1.change_request_group_id in
2738                 (select parent_change_request_id
2739                  from po_change_requests
2740                  where change_request_group_id=p_change_request_group_id
2741                      and action_type='MODIFICATION');
2742 
2743          if (l_date_change_id is not null) then
2744               update PO_CHANGE_REQUESTS pcr1
2745               set (pcr1.request_status,
2746                  pcr1.change_active_flag,
2747                  pcr1.response_date,
2748                  pcr1.response_reason,
2749                  pcr1.responded_by,
2750                  pcr1.last_updated_by,
2751                  pcr1.last_update_login,
2752                  pcr1.last_update_date) =
2753                 (select 'ACCEPTED',
2754                         'N',
2755                         pcr2.response_date,
2756                         pcr2.response_reason,
2757                         pcr2.responded_by,
2758                         fnd_global.user_id,
2759                         fnd_global.login_id,
2760                         sysdate
2761                  from po_change_requests pcr2
2762                  where pcr2.parent_change_request_id=l_date_change_id1
2763                        and pcr2.change_request_group_id=p_change_request_group_id
2764                        and pcr2.action_type='MODIFICATION'
2765                        and rownum=1)
2766               where pcr1.change_request_id = l_date_change_id;
2767             end if;
2768 
2769             x_progress:='ACCEPTANCE:007';
2770 
2771         exception
2772          --In case of error, rollback
2773 
2774             when FND_API.g_exc_error then
2775                rollback to PO_REQCHANGEREQUESTWF_PVT_SP;
2776 
2777                update PO_CHANGE_REQUESTS
2778                 set request_status='REJECTED',
2779                     change_active_flag='N',
2780                     response_date=sysdate,
2781                     response_reason=fnd_message.get_string('PO', 'PO_RCO_EXCEPTION_WHEN_PROCESS'),
2782                     validation_error=l_err_msg
2783                 where change_request_group_id in (select parent_change_request_id
2784                                     from po_change_requests
2785                                     where change_request_group_id=p_change_request_group_id
2786                                         and action_type='MODIFICATION');
2787 
2788                 update PO_CHANGE_REQUESTS
2789                 set request_status='REJECTED',
2790                     change_active_flag='N',
2791                     response_date=sysdate,
2792                     response_reason=fnd_message.get_string('PO', 'PO_RCO_EXCEPTION_WHEN_PROCESS'),
2793                     validation_error=l_err_msg
2794                 where change_request_group_id=p_change_request_group_id
2795                     and action_type='MODIFICATION';
2796                raise;
2797 
2798             when others then
2799                rollback to PO_REQCHANGEREQUESTWF_PVT_SP ;
2800                raise;
2801 
2802         end;
2803 
2804      commit;
2805 
2806 EXCEPTION
2807   WHEN OTHERS THEN
2808    wf_core.context('PO_ReqChangeRequestWF_PVT','ProcessSCOAcceptance',x_progress||sqlerrm);
2809    raise;
2810 
2811 END ProcessSCOAcceptance;
2812 /*************************************************************************
2813  * Private Procedure: SetPoRequestStatus
2814  * Effects: set the status of the PO change records
2815  *
2816  *          It is only called in PORPOCHA workflow
2817  *
2818  *          the process will commit when it exits.
2819  *
2820  * Returns:
2821  ************************************************************************/
2822 procedure SetPoRequestStatus(p_change_request_group_id in number,p_request_status in varchar2, p_response_reason in varchar2 default null) is
2823 pragma AUTONOMOUS_TRANSACTION;
2824 
2825 x_progress varchar2(3):= '000';
2826 
2827 BEGIN
2828 
2829     update PO_CHANGE_REQUESTS
2830     set last_updated_by         = fnd_global.user_id,
2831         last_update_login       = fnd_global.login_id,
2832         last_update_date        = sysdate,
2833         request_status=p_request_status,
2834         response_date=sysdate,
2835         responded_by=fnd_global.user_id,
2836         response_reason = nvl(p_response_reason, response_reason),
2837         change_active_flag=decode(p_request_status, 'ACCEPTED', 'N',
2838                                  'REJECTED', 'N', 'Y')
2839     where change_request_group_id=p_change_request_group_id
2840         and request_status not in ('ACCEPTED', 'REJECTED');
2841 
2842     x_progress := '001';
2843 
2844 
2845     commit;
2846 
2847 EXCEPTION
2848   WHEN OTHERS THEN
2849     wf_core.context('PO_ReqChangeRequestWF_PVT','SetPoRequestStatus',sqlerrm);
2850         raise;
2851 
2852 END SetPoRequestStatus;
2853 
2854 /*************************************************************************
2855  * Private Procedure: ConvertIntoPOChange
2856  * Effects: convert the requester change to PO change
2857  *
2858  *          It is only called in PORPOCHA workflow
2859  *
2860  *          the process will commit when it exits.
2861  *
2862  * Returns:
2863  ************************************************************************/
2864 procedure ConvertIntoPOChange(p_change_request_group_id in number) is
2865 pragma AUTONOMOUS_TRANSACTION;
2866 
2867 l_document_header_id number;
2868 l_po_release_id number;
2869 l_document_num varchar2(50);
2870 l_action_type varchar2(30);
2871 l_document_type varchar2(30);
2872 l_request_level varchar2(30);
2873 l_document_revision_num number;
2874 l_created_by number;
2875 l_document_line_id number;
2876 l_document_line_number number;
2877 l_document_line_location_id number;
2878 l_document_shipment_number number;
2879 l_document_distribution_id number;
2880 l_document_distribution_num number;
2881 l_old_quantity number;
2882 l_new_quantity number;
2883 l_old_need_by_date date;
2884 l_new_need_by_date date;
2885 l_old_price number;
2886 l_new_price number;
2887 l_request_reason varchar2(2000);
2888 l_parent_change_request_id number;
2889 l_ship_quantity number;
2890 l_dist_quantity number;
2891 l_new_ship_quantity number;
2892 l_old_ship_quantity number;
2893 l_item_id number;
2894 l_req_uom po_requisition_lines_all.UNIT_MEAS_LOOKUP_CODE%TYPE;
2895 l_po_uom po_line_locations_all.UNIT_MEAS_LOOKUP_CODE%TYPE;
2896 l_old_amount number;
2897 l_new_amount number;
2898 l_old_start_date date;
2899 l_new_start_date date;
2900 l_old_expiration_date date;
2901 l_new_expiration_date date;
2902 l_ship_amount number;
2903 l_dist_amount number;
2904 l_matching_basis po_requisition_lines.matching_basis%type;
2905 l_new_ship_amount number;
2906 l_old_ship_amount number;
2907 l_converted_quantity number;
2908 
2909 -- Bug # 3862383
2910 -- htank, 06-Oct-2004
2911 -- Requester_ID was not inserted into the po_change_requests table where document_type = 'PO'
2912 -- Due to this Approval History for PO was showing wrong name in the Change Request event
2913 l_requester_id number;   -- Bug # 3862383
2914 
2915 -- SQL What: get all the elements required to generate
2916 --           the PO change request from the req change request
2917 -- SQL Why: need to generate PO change request from approved
2918 --          req change request
2919 -- SQL Join: po_header_id,
2920 --           po_release_id,
2921 --           change_request_group_id,
2922 --           document_distribution_id ,
2923 --           requisition_line_id,
2924 --           line_location_id,
2925 --           po_line_id,
2926 cursor po_change_csr(l_change_request_group_id in number) is
2927 select * from (
2928 select max(pll.po_header_id) document_header_id,
2929 max(pll.po_release_id) po_release_id,
2930 max(decode(pll.po_release_id, null, poh.segment1, poh.segment1||'-'||to_char(por.release_num))) document_num,
2931 max(pcr.action_type) action_type,
2932 max(decode(pll.po_release_id, null, 'PO', 'RELEASE')) document_type,
2933 'LINE' request_level,
2934 max(pcr.document_revision_num) document_revision_num,
2935 max(pcr.created_by) created_by,
2936 max(pol.po_line_id) document_line_id,
2937 max(pol.line_num) document_line_number,
2938 to_number(null) document_line_location_id,
2939 to_number(null) document_shipment_number,
2940 to_number(null) document_distribution_id,
2941 to_number(null) document_distribution_num,
2942 max(pcr.request_reason) request_reason,
2943 max(nvl(pcr.old_need_by_date, pll.need_by_date)) old_need_by_date,
2944 max(pcr.new_need_by_date) new_need_by_date,
2945 max((pcr.old_price)/nvl(poh.rate,1)) old_price, -- If Order is in trn currency divide price by rate, else leave it as it is
2946 max((pcr.new_price)/nvl(poh.rate,1)) new_price,
2947 to_number(null) old_quantity,
2948 to_number(null) new_quantity,
2949 max(pcr.change_request_id) parent_change_request_id,
2950 max(pll.quantity) ship_quantity,
2951 to_number(null) dist_quantity,
2952 max(pll.SHIP_TO_ORGANIZATION_ID),
2953 max(pll.SHIP_TO_LOCATION_ID),
2954 max(prl.item_id),
2955 max(prl.unit_meas_lookup_code) req_uom,
2956 max(nvl(pll.unit_meas_lookup_code,pol.unit_meas_lookup_code)) po_uom,
2957 max(pol.start_date) old_start_date,
2958 max(pcr.new_start_date) new_start_date,
2959 max(pol.expiration_date) old_expiration_date,
2960 max(pcr.new_expiration_date) new_expiration_date,
2961 to_number(null) old_amount,
2962 to_number(null) new_amount,
2963 max(pll.amount) ship_amount,
2964 to_number(null) dist_amount,
2965 max(prl.matching_basis),
2966 max(pcr.requester_id) requester_id   -- Bug # 3862383
2967 from po_change_requests pcr,
2968 po_line_locations_all pll,
2969 po_requisition_lines_all prl,
2970 po_headers_all poh,
2971 po_releases_all por,
2972 po_lines_all pol
2973 where pcr.change_request_group_id=l_change_request_group_id
2974 and pcr.request_status='MGR_APP'
2975 and pcr.document_distribution_id is null
2976 and pcr.document_line_id=prl.requisition_line_id
2977 and prl.purchase_basis = 'TEMP LABOR'
2978 and prl.line_location_id=pll.line_location_id
2979 and poh.po_header_id=pll.po_header_id
2980 and por.po_release_id(+)=pll.po_release_id
2981 and pol.po_line_id=pll.po_line_id
2982 group by pol.po_line_id
2983 union select pll.po_header_id document_header_id,
2984 pll.po_release_id po_release_id,
2985 decode(pll.po_release_id, null, poh.segment1, poh.segment1||'-'||to_char(por.release_num)) document_num,
2986 pcr.action_type action_type,
2987 decode(pll.po_release_id, null, 'PO', 'RELEASE') document_type,
2988 decode(pcr.new_price, null, 'SHIPMENT', 'LINE') request_level,
2989 pcr.document_revision_num document_revision_num,
2990 pcr.created_by created_by,
2991 pol.po_line_id document_line_id,
2992 pol.line_num document_line_number,
2993 decode(pcr.new_price, null, pll.line_location_id, null) document_line_location_id,
2994 decode(pcr.new_price, null, pll.shipment_num, null) document_shipment_number,
2995 to_number(null) document_distribution_id,
2996 to_number(null) document_distribution_num,
2997 pcr.request_reason request_reason,
2998 nvl(pcr.old_need_by_date, pll.need_by_date) old_need_by_date,
2999 pcr.new_need_by_date new_need_by_date,
3000 (pcr.old_price)/nvl(poh.rate,1)  old_price, -- If Order is in trn currency divide price by rate, else leave it as it is
3001 (pcr.new_price)/nvl(poh.rate,1) new_price,
3002 to_number(null) old_quantity,
3003 to_number(null) new_quantity,
3004 pcr.change_request_id parent_change_request_id,
3005 pll.quantity ship_quantity,
3006 to_number(null) dist_quantity,
3007 pll.SHIP_TO_ORGANIZATION_ID,
3008 pll.SHIP_TO_LOCATION_ID,
3009 prl.item_id,
3010 prl.unit_meas_lookup_code req_uom,
3011 nvl(pll.unit_meas_lookup_code,pol.unit_meas_lookup_code) po_uom,
3012 pol.start_date old_start_date,
3013 pcr.new_start_date new_start_date,
3014 pol.expiration_date old_expiration_date,
3015 pcr.new_expiration_date new_expiration_date,
3016 to_number(null) old_amount,
3017 to_number(null) new_amount,
3018 pll.amount ship_amount,
3019 to_number(null) dist_amount,
3020 prl.matching_basis,
3021 pcr.requester_id requester_id   -- Bug # 3862383
3022 from po_change_requests pcr,
3023 po_line_locations_all pll,
3024 po_requisition_lines_all prl,
3025 po_headers_all poh,
3026 po_releases_all por,
3027 po_lines_all pol
3028 where pcr.change_request_group_id=l_change_request_group_id
3029 and pcr.request_status='MGR_APP'
3030 and pcr.document_distribution_id is null
3031 and pcr.document_line_id=prl.requisition_line_id
3032 and prl.purchase_basis <> 'TEMP LABOR'
3033 and prl.line_location_id=pll.line_location_id
3034 and poh.po_header_id=pll.po_header_id
3035 and por.po_release_id(+)=pll.po_release_id
3036 and pol.po_line_id=pll.po_line_id
3037 union
3038 select pll.po_header_id document_header_id,
3039 pll.po_release_id po_release_id,
3040 decode(pll.po_release_id, null, poh.segment1, poh.segment1||'-'||to_char(por.release_num)) document_num,
3041 pcr.action_type action_type,
3042 decode(pll.po_release_id, null, 'PO', 'RELEASE') document_type,
3043 'DISTRIBUTION' request_level,
3044 pcr.document_revision_num document_revision_num,
3045 pcr.created_by created_by,
3046 pol.po_line_id document_line_id,
3047 pol.line_num document_line_number,
3048 pll.line_location_id document_line_location_id,
3049 pll.shipment_num document_shipment_number,
3050 pod.po_distribution_id document_distribution_id,
3051 pod.distribution_num document_distribution_num,
3052 pcr.request_reason request_reason,
3053 nvl(pcr.old_need_by_date, pll.need_by_date) old_need_by_date,
3054 pcr.new_need_by_date new_need_by_date,
3055 decode(prl.ORDER_TYPE_LOOKUP_CODE,'AMOUNT',pcr.old_price,(pcr.old_price)/nvl(pod.rate,1)) old_price, /* If Order is in trn currency divide price by rate if order_type is not AMOUNT. Beacuse we are dividing qty by rate in that case. */
3056 decode(prl.ORDER_TYPE_LOOKUP_CODE,'AMOUNT',pcr.new_price,(pcr.new_price)/nvl(pod.rate,1)) new_price,
3057 decode(prl.ORDER_TYPE_LOOKUP_CODE,'AMOUNT', (pcr.old_quantity/nvl(pod.RATE,1)), pcr.old_quantity) old_quantity,--Divide qty by rate for amt based reqs only
3058 decode(prl.ORDER_TYPE_LOOKUP_CODE,'AMOUNT', (pcr.new_quantity/nvl(pod.RATE,1)), pcr.new_quantity) new_quantity,
3059 pcr.change_request_id parent_change_request_id,
3060 pll.quantity ship_quantity,
3061 pod.quantity_ordered dist_quantity,
3062 pll.SHIP_TO_ORGANIZATION_ID,
3063 pll.SHIP_TO_LOCATION_ID,
3064 prl.item_id,
3065 prl.unit_meas_lookup_code req_uom,
3066 nvl(pll.unit_meas_lookup_code,pol.unit_meas_lookup_code) po_uom,
3067 to_date(null) old_start_date,
3068 to_date(null) new_start_date,
3069 to_date(null) old_expiration_date,
3070 to_date(null) new_expiration_date,
3071 (pcr.old_amount)/nvl(pod.rate,1) old_amount,
3072 (pcr.new_amount)/nvl(pod.rate,1) new_amount,
3073 pll.amount ship_amount,
3074 pod.amount_ordered dist_amount,
3075 prl.matching_basis,
3076 pcr.requester_id requester_id   -- Bug # 3862383
3077 from po_change_requests pcr,
3078 po_line_locations_all pll,
3079 po_requisition_lines_all prl,
3080 po_headers_all poh,
3081 po_releases_all por,
3082 po_distributions_all pod,
3083 po_req_distributions_all prd,
3084 po_lines_all pol
3085 where pcr.change_request_group_id=l_change_request_group_id
3086 and pcr.request_status='MGR_APP'
3087 and pcr.document_distribution_id is not null
3088 and pcr.document_line_id=prl.requisition_line_id
3089 and prl.line_location_id=pll.line_location_id
3090 and poh.po_header_id=pll.po_header_id
3091 and por.po_release_id(+)=pll.po_release_id
3092 and pcr.document_distribution_id=prd.distribution_id
3093 and pcr.document_distribution_id=pod.req_distribution_id
3094 and pll.line_location_id=pod.line_location_id
3095 and pol.po_line_id=pll.po_line_id
3096 )
3097 order by document_header_id, po_release_id, document_line_id,
3098     document_line_location_id, document_distribution_id;
3099 
3100 my_chg_rec_tbl pos_chg_rec_tbl := pos_chg_rec_tbl();
3101 
3102 l_SHIP_TO_ORGANIZATION_ID number;
3103 l_SHIP_TO_LOCATION_ID number;
3104 temp number;
3105 l_error_code varchar2(200);
3106 l_return_status varchar2(1);
3107 l_change_request_group_id number;
3108 x_progress varchar2(100):='001';
3109 
3110 l_current_header_id number := null;
3111 l_current_release_id number := null;
3112 l_current_ship_id number :=null;
3113 l_quantity_change boolean := false;
3114 l_current_matching_basis po_requisition_lines.matching_basis%type;
3115 
3116 begin
3117 
3118     open po_change_csr(p_change_request_group_id);
3119     temp:=1;
3120     loop
3121         fetch po_change_csr into
3122             l_document_header_id,
3123             l_po_release_id,
3124             l_document_num,
3125             l_action_type,
3126             l_document_type,
3127             l_request_level,
3128             l_document_revision_num,
3129             l_created_by,
3130             l_document_line_id,
3131             l_document_line_number,
3132             l_document_line_location_id,
3133             l_document_shipment_number,
3134             l_document_distribution_id,
3135             l_document_distribution_num,
3136             l_request_reason,
3137             l_old_need_by_date,
3138             l_new_need_by_date,
3139             l_old_price,
3140             l_new_price,
3141             l_old_quantity,
3142             l_new_quantity,
3143             l_parent_change_request_id,
3144             l_ship_quantity,
3145             l_dist_quantity,
3146             l_SHIP_TO_ORGANIZATION_ID,
3147             l_SHIP_TO_LOCATION_ID,
3148             l_item_id,
3149             l_req_uom,
3150             l_po_uom,
3151             l_old_start_date,
3152             l_new_start_date,
3153             l_old_expiration_date,
3154             l_new_expiration_date,
3155             l_old_amount,
3156             l_new_amount,
3157             l_ship_amount,
3158             l_dist_amount,
3159             l_matching_basis,
3160             l_requester_id;   -- Bug # 3862383
3161         exit when po_change_csr%NOTFOUND;
3162 
3163         if(l_new_quantity is not null) then
3164             if(l_req_uom <> l_po_uom) then
3165                 po_uom_s.uom_convert(
3166                          from_quantity => l_new_quantity,
3167                          from_uom => l_req_uom,
3168                          item_id => l_item_id,
3169                          to_uom => l_po_uom,
3170                          to_quantity => l_converted_quantity);
3171                 l_new_quantity:=l_converted_quantity;
3172 
3173             end if;
3174         end if;
3175 
3176         x_progress:='002:'||to_char(l_current_header_id)
3177                       ||'-'||to_char(l_document_header_id);
3178 
3179         if(l_current_header_id is null) then
3180             l_current_header_id:=l_document_header_id;
3181             l_current_release_id:=l_po_release_id;
3182         end if;
3183         if(l_quantity_change
3184                 and (l_document_distribution_id is null
3185                      or l_current_ship_id<>
3186                         nvl(l_document_line_location_id, -999))) then
3187             l_quantity_change := false;
3188 
3189             if(l_current_ship_id=l_document_line_location_id) then
3190                 -- it is a shipment change
3191                 -- figure out whether we need set the old price
3192                 -- use the l_new_ship_quantity and l_new_need_by_date
3193                 if (l_matching_basis = 'AMOUNT') then
3194                   l_old_amount := l_ship_amount;
3195                   l_new_amount := l_new_ship_amount;
3196                 else
3197                   l_old_quantity:=l_ship_quantity;
3198                   l_new_quantity:=l_new_ship_quantity;
3199                 end if;
3200             else
3201 
3202                 if (l_current_matching_basis='AMOUNT') then
3203                   l_old_ship_quantity:= null;
3204                   l_new_ship_quantity:= null;
3205                 else
3206                   l_old_ship_amount:= null;
3207                   l_new_ship_amount:= null;
3208                 end if;
3209 
3210                 -- add a new shipment quantity change
3211                 -- figure out whether we need set the old price
3212                 -- use the l_new_ship_quantity and need_by_date from po_ship
3213                 my_chg_rec_tbl.extend;
3214                 my_chg_rec_tbl(temp) := pos_chg_rec(
3215                     Action_Type         =>'MODIFICATION',
3216                     Initiator           =>'REQUESTER',
3217                     Request_Reason      =>my_chg_rec_tbl(temp-1).Request_reason,
3218                     Document_Type       =>my_chg_rec_tbl(temp-1).document_type,
3219                     Request_Level       =>'SHIPMENT',
3220                     Request_Status      =>'PENDING',
3221                     Document_Header_Id  =>my_chg_rec_tbl(temp-1).
3222                                               document_header_id,
3223                     PO_Release_Id       =>my_chg_rec_tbl(temp-1).
3224                                               PO_Release_id,
3225                     Document_Num        =>my_chg_rec_tbl(temp-1).
3226                                               Document_Num,
3227                     Document_Revision_Num           =>my_chg_rec_tbl(temp-1).
3228                                                          Document_Revision_Num,
3229                     Document_Line_Id    =>my_chg_rec_tbl(temp-1).
3230                                               Document_Line_Id,
3231                     Document_Line_Number=>my_chg_rec_tbl(temp-1).
3232                                               Document_Line_Number,
3233                     Document_Line_Location_Id   =>l_current_ship_id,
3234                     Document_Shipment_Number    =>my_chg_rec_tbl(temp-1).
3235                                                     Document_Shipment_Number,
3236                     Document_Distribution_id    =>null,
3237                     Document_Distribution_Number=>null,
3238                     Parent_Line_Location_Id     =>null, --NUMBER,
3239                     Old_Quantity        =>l_old_ship_quantity, --NUMBER,
3240                     New_Quantity        =>l_new_ship_quantity, --NUMBER,
3241                     Old_Promised_Date   =>null, --DATE,
3242                     New_Promised_Date   =>null, --DATE,
3243                     Old_Supplier_Part_Number        =>null, --VARCHAR2(25),
3244                     New_Supplier_Part_Number        =>null, --VARCHAR2(25),
3245                     Old_Price           =>null,
3246                     New_Price           =>null,
3247                     Old_Supplier_Reference_Number   =>null, --VARCHAR2(30),
3248                     New_Supplier_reference_number   =>null,
3249                     From_Header_id      =>null, --NUMBER
3250                     Recoverable_Tax     =>null, --NUMBER
3251                     Non_recoverable_tax =>null, --NUMBER
3252                     Ship_To_Location_Id =>my_chg_rec_tbl(temp-1).Ship_To_Location_Id, --NUMBER
3253                     Ship_To_Organization_Id =>my_chg_rec_tbl(temp-1).Ship_To_Organization_Id, --NUMBER
3254                     Old_Need_By_Date    =>my_chg_rec_tbl(temp-1).old_need_by_date,
3255                     New_Need_By_Date    =>null,
3256                     Approval_Required_Flag          =>null,
3257                     Parent_Change_request_Id        =>null,
3258                     Requester_id        =>l_requester_id,  -- Bug # 3862383
3259                     Old_Supplier_Order_Number       =>null,
3260                     New_Supplier_Order_Number       =>null,
3261                     Old_Supplier_Order_Line_Number  =>null,
3262                     New_Supplier_Order_Line_Number  =>null,
3263                     ADDITIONAL_CHANGES             => null,
3264                     OLD_START_DATE            => null,
3265                     NEW_START_DATE            => null,
3266                     OLD_EXPIRATION_DATE       => null,
3267                     NEW_EXPIRATION_DATE       => null,
3268                     OLD_AMOUNT     => l_old_ship_amount,
3269                     NEW_AMOUNT     => l_new_ship_amount,
3270                     SUPPLIER_DOC_REF => null,
3271                     SUPPLIER_LINE_REF => null,
3272                     SUPPLIER_SHIPMENT_REF => null,
3273                     NEW_PROGRESS_TYPE => NULL,
3274                     NEW_PAY_DESCRIPTION => NULL
3275                 );
3276                 temp:=temp+1;
3277             end if;
3278         end if;
3279         if(l_document_distribution_id is not null) then
3280             if(l_quantity_change = false) then
3281                 l_quantity_change:=true;
3282 
3283                 if (l_matching_basis='AMOUNT') then
3284                   l_new_ship_amount := l_ship_amount+
3285                         l_new_amount-nvl(l_dist_amount, l_old_amount);
3286                   l_old_ship_amount := l_ship_amount;
3287                 else
3288                   l_new_ship_quantity:=l_ship_quantity+
3289                           l_new_quantity-l_dist_quantity;
3290                   l_old_ship_quantity:=l_ship_quantity;
3291                 end if;
3292             else
3293                 if (l_matching_basis = 'AMOUNT') then
3294                    l_new_ship_amount := l_new_ship_amount+
3295                         l_new_amount-nvl(l_dist_amount, l_old_amount);
3296 
3297                 else
3298                    l_new_ship_quantity:=l_new_ship_quantity+
3299                           l_new_quantity-l_dist_quantity;
3300                 end if;
3301             end if;
3302         end if;
3303 
3304         /* bug 3916594: added condition to check for release
3305            note, this fix will not impact standard PO,
3306            as null<>null yields null
3307          */
3308         if(nvl(l_current_header_id, l_document_header_id)
3309                     <>l_document_header_id
3310            or nvl(l_current_release_id, l_po_release_id)
3311                     <>l_po_release_id) then
3312 
3313            ValidateAndSaveRequest(
3314                      p_po_header_id       =>l_document_header_id,
3315                      p_po_release_id      =>l_po_release_id,
3316                      p_revision_num       =>l_document_revision_num,
3317                      p_po_change_requests =>my_chg_rec_tbl);
3318 
3319             temp:=1;
3320             my_chg_rec_tbl := pos_chg_rec_tbl();
3321             l_current_header_id:=l_document_header_id;
3322             l_current_release_id:=l_po_release_id;
3323         end if;
3324 
3325         l_current_ship_id:=l_document_line_location_id;
3326         l_current_matching_basis :=l_matching_basis;
3327         my_chg_rec_tbl.extend;
3328 
3329         my_chg_rec_tbl(temp) := pos_chg_rec(
3330             Action_Type                     =>l_action_type,
3331             Initiator                       =>'REQUESTER',
3332             Request_Reason                  =>l_request_reason,
3333             Document_Type                   =>l_document_type,
3334             Request_Level                   =>l_request_level,
3335             Request_Status                  =>'PENDING',
3336             Document_Header_Id              =>l_document_header_id,
3337             PO_Release_Id                   =>l_po_release_id,
3338             Document_Num                    =>l_document_num,
3339             Document_Revision_Num           =>l_document_revision_num,
3340             Document_Line_Id                =>l_document_line_id,
3341             Document_Line_Number            =>l_document_line_number,
3342             Document_Line_Location_Id       =>l_document_line_location_id,
3343             Document_Shipment_Number        =>l_document_shipment_number,
3344             Document_Distribution_id        =>l_document_distribution_id,
3345             Document_Distribution_Number    =>l_document_distribution_num,
3346             Parent_Line_Location_Id         =>null, --NUMBER,
3347             Old_Quantity                    =>l_old_quantity, --NUMBER,
3348             New_Quantity                    =>l_new_quantity, --NUMBER,
3349             Old_Promised_Date               =>null, --DATE,
3350             New_Promised_Date               =>null, --DATE,
3351             Old_Supplier_Part_Number        =>null, --VARCHAR2(25),
3352             New_Supplier_Part_Number        =>null, --VARCHAR2(25),
3353             Old_Price                       =>l_old_price,
3354             New_Price                       =>l_new_price,
3355             Old_Supplier_Reference_Number   =>null, --VARCHAR2(30),
3356             New_Supplier_reference_number   =>null,
3357             From_Header_id                  =>null, --NUMBER
3358             Recoverable_Tax                 =>null, --NUMBER
3359             Non_recoverable_tax             =>null, --NUMBER
3360             Ship_To_Location_Id             =>l_SHIP_TO_LOCATION_ID, --NUMBER
3361             Ship_To_Organization_Id         =>l_SHIP_TO_ORGANIZATION_ID, --NUMBER
3362             Old_Need_By_Date                =>l_old_need_by_date,
3363             New_Need_By_Date                =>l_new_need_by_date,
3364             Approval_Required_Flag          =>null,
3365             Parent_Change_request_Id        =>l_parent_change_request_id,
3366             Requester_id                    =>l_requester_id,  -- Bug # 3862383
3367             Old_Supplier_Order_Number       =>null,
3368             New_Supplier_Order_Number       =>null,
3369             Old_Supplier_Order_Line_Number  =>null,
3370             New_Supplier_Order_Line_Number  =>null,
3371             ADDITIONAL_CHANGES             => null,
3372             OLD_START_DATE            => l_old_start_date,
3373             NEW_START_DATE            => l_new_start_date,
3374             OLD_EXPIRATION_DATE       => l_old_expiration_date,
3375             NEW_EXPIRATION_DATE       => l_new_expiration_date,
3376             OLD_AMOUNT     => l_old_amount,
3377             NEW_AMOUNT     => l_new_amount,
3378             SUPPLIER_DOC_REF => null,
3379             SUPPLIER_LINE_REF => null,
3380             SUPPLIER_SHIPMENT_REF => null,
3381             NEW_PROGRESS_TYPE => NULL,
3382             NEW_PAY_DESCRIPTION => NULL
3383         );
3384 
3385         temp:=temp+1;
3386 
3387 
3388     end loop;
3389     close po_change_csr;
3390 
3391     -- save the last po
3392     if(l_quantity_change) then
3393         -- add a new shipment change
3394         -- figure out whether we need set the old price
3395         -- use the l_new_ship_quantity and po_ship.need-by
3396 
3397         if (l_current_matching_basis='AMOUNT') then
3398           l_old_ship_quantity:= null;
3399           l_new_ship_quantity:= null;
3400         else
3401           l_old_ship_amount:= null;
3402           l_new_ship_amount:= null;
3403         end if;
3404 
3405         my_chg_rec_tbl.extend;
3406         my_chg_rec_tbl(temp) := pos_chg_rec(
3407             Action_Type         =>'MODIFICATION',
3408             Initiator           =>'REQUESTER',
3409             Request_Reason      =>my_chg_rec_tbl(temp-1).Request_reason,
3410             Document_Type       =>my_chg_rec_tbl(temp-1).document_type,
3411             Request_Level       =>'SHIPMENT',
3412             Request_Status      =>'PENDING',
3413             Document_Header_Id  =>my_chg_rec_tbl(temp-1).
3414                                       document_header_id,
3415             PO_Release_Id       =>my_chg_rec_tbl(temp-1).
3416                                       PO_Release_id,
3417             Document_Num        =>my_chg_rec_tbl(temp-1).
3418                                       Document_Num,
3419             Document_Revision_Num           =>my_chg_rec_tbl(temp-1).
3420                                                  Document_Revision_Num,
3421             Document_Line_Id    =>my_chg_rec_tbl(temp-1).
3422                               Document_Line_Id,
3423             Document_Line_Number=>my_chg_rec_tbl(temp-1).
3424                                       Document_Line_Number,
3425             Document_Line_Location_Id   =>l_current_ship_id,
3426             Document_Shipment_Number    =>my_chg_rec_tbl(temp-1).
3427                                             Document_Shipment_Number,
3428             Document_Distribution_id    =>null,
3429             Document_Distribution_Number=>null,
3430             Parent_Line_Location_Id     =>null, --NUMBER,
3431             Old_Quantity        =>l_old_ship_quantity, --NUMBER,
3432             New_Quantity        =>l_new_ship_quantity, --NUMBER,
3433             Old_Promised_Date   =>null, --DATE,
3434             New_Promised_Date   =>null, --DATE,
3435             Old_Supplier_Part_Number        =>null, --VARCHAR2(25),
3436             New_Supplier_Part_Number        =>null, --VARCHAR2(25),
3437             Old_Price           =>null,
3438             New_Price           =>null,
3439             Old_Supplier_Reference_Number   =>null, --VARCHAR2(30),
3440             New_Supplier_reference_number   =>null,
3441             From_Header_id      =>null, --NUMBER
3442             Recoverable_Tax     =>null, --NUMBER
3443             Non_recoverable_tax =>null, --NUMBER
3444             Ship_To_Location_Id =>my_chg_rec_tbl(temp-1).Ship_To_Location_Id, --NUMBER
3445             Ship_To_Organization_Id         =>my_chg_rec_tbl(temp-1).Ship_To_Organization_Id, --NUMBER
3446             Old_Need_By_Date    =>my_chg_rec_tbl(temp-1).Old_Need_By_Date,
3447             New_Need_By_Date    =>null,
3448             Approval_Required_Flag          =>null,
3449             Parent_Change_request_Id        =>null,
3450             Requester_id        =>l_requester_id,  -- Bug # 3862383
3451             Old_Supplier_Order_Number       =>null,
3452             New_Supplier_Order_Number       =>null,
3453             Old_Supplier_Order_Line_Number  =>null,
3454             New_Supplier_Order_Line_Number  =>null,
3455             ADDITIONAL_CHANGES             => null,
3456             OLD_START_DATE            => null,
3457             NEW_START_DATE            => null,
3458             OLD_EXPIRATION_DATE       => null,
3459             NEW_EXPIRATION_DATE       => null,
3460             OLD_AMOUNT     => l_old_ship_amount,
3461             NEW_AMOUNT     => l_new_ship_amount,
3462             SUPPLIER_DOC_REF => null,
3463             SUPPLIER_LINE_REF => null,
3464             SUPPLIER_SHIPMENT_REF => null,
3465             NEW_PROGRESS_TYPE => null,
3466             NEW_PAY_DESCRIPTION => null
3467         );
3468         temp:=temp+1;
3469     end if;
3470     ValidateAndSaveRequest(
3471                          p_po_header_id      =>l_document_header_id,
3472                          p_po_release_id      =>l_po_release_id,
3473                          p_revision_num       =>l_document_revision_num,
3474                          p_po_change_requests =>my_chg_rec_tbl);
3475     commit;
3476 
3477 EXCEPTION
3478   WHEN OTHERS THEN
3479     wf_core.context('PO_ReqChangeRequestWF_PVT','ConvertIntoPOChange',x_progress);
3480         raise;
3481 
3482 END ConvertIntoPOChange;
3483 
3484 
3485 
3486 /*************************************************************************
3487  * Private Procedure: SetReqRequestStatus
3488  * Effects: set the status of the requester change records
3489  *
3490  *          It is only called in POREQCHA workflow, which means the
3491  *          status can be set to 'MGR_PRE_APP', 'MGR_APP' or 'REJECTED'.
3492  *
3493  *          the process will commit when it exits.
3494  *
3495  * Returns:
3496  ************************************************************************/
3497 procedure SetReqRequestStatus(
3498         p_change_request_group_id in number,
3499         p_cancel_flag in varchar2,
3500         p_request_status in varchar2,
3501         p_responder_id in number,
3502         p_response_reason in varchar) is
3503 pragma AUTONOMOUS_TRANSACTION;
3504 
3505 x_progress varchar2(3):= '000';
3506 
3507 BEGIN
3508 
3509     if(p_cancel_flag='Y') then
3510 
3511         if(p_request_status='MGR_APP') then
3512 
3513             x_progress := '001';
3514 
3515             update PO_CHANGE_REQUESTS
3516             set last_updated_by         = fnd_global.user_id,
3517                 last_update_login       = fnd_global.login_id,
3518                 last_update_date        = sysdate,
3519                 request_status='MGR_APP'
3520             where change_request_group_id=p_change_request_group_id
3521                 and action_type='CANCELLATION'
3522                 and request_status='NEW';
3523 
3524             x_progress := '002';
3525 
3526         end if;
3527     else
3528         if(p_request_status='MGR_APP') then
3529 
3530             x_progress := '003';
3531 
3532             update PO_CHANGE_REQUESTS
3533             set last_updated_by         = fnd_global.user_id,
3534                 last_update_login       = fnd_global.login_id,
3535                 last_update_date        = sysdate,
3536                 request_status='MGR_APP'
3537             where change_request_group_id=p_change_request_group_id
3538                 and action_type='MODIFICATION'
3539                 and request_status in ('NEW', 'MGR_PRE_APP');
3540 
3541             x_progress := '004';
3542 
3543         elsif(p_request_status='MGR_PRE_APP') then
3544 
3545             x_progress := '005';
3546 
3547             update PO_CHANGE_REQUESTS
3548             set last_updated_by         = fnd_global.user_id,
3549                 last_update_login       = fnd_global.login_id,
3550                 last_update_date        = sysdate,
3551                 request_status='MGR_PRE_APP'
3552             where change_request_group_id=p_change_request_group_id
3553                 and action_type='MODIFICATION'
3554                 and request_status ='NEW';
3555 
3556             x_progress := '006';
3557         else -- reject
3558             x_progress := '005';
3559 
3560             update PO_CHANGE_REQUESTS
3561             set last_updated_by         = fnd_global.user_id,
3562                 last_update_login       = fnd_global.login_id,
3563                 last_update_date        = sysdate,
3564                 request_status='REJECTED',
3565                 change_active_flag='N',
3566                 responded_by=p_responder_id,
3567                 response_reason=p_response_reason,
3568                 response_date = sysdate
3569             where change_request_group_id=p_change_request_group_id
3570                 and action_type='MODIFICATION'
3571                 and request_status in ('NEW', 'MGR_PRE_APP');
3572 
3573             x_progress := '006';
3574 
3575         end if;
3576     end if;
3577 
3578     commit;
3579 
3580 EXCEPTION
3581   WHEN OTHERS THEN
3582     wf_core.context('PO_ReqChangeRequestWF_PVT','SetReqRequestStatus',x_progress);
3583         raise;
3584 
3585 END SetReqRequestStatus;
3586 
3587 
3588 
3589 /*************************************************************************
3590  * Private Procedure: SetReqChangeFlag
3591  * Effects: set the change_pending_flag in requisition headers table
3592  *          if the flag is set to 'Y', which means there is pending
3593  *          change coming, also store the itemtype and itemkey to
3594  *          po_change_requests table.
3595  *
3596  *          the process will commit when it exits.
3597  *
3598  * Returns:
3599  ************************************************************************/
3600 procedure SetReqChangeFlag(
3601         p_change_request_group_id in number,
3602         p_document_id in number,
3603         p_itemtype in varchar2,
3604         p_itemkey in varchar2,
3605         p_change_flag in varchar2) is
3606 pragma AUTONOMOUS_TRANSACTION;
3607 
3608 x_progress varchar2(3):= '000';
3609 
3610 BEGIN
3611 
3612     update po_requisition_headers_all
3613     set last_updated_by         = nvl(fnd_global.user_id, last_updated_by),
3614         last_update_login       = nvl(fnd_global.login_id, last_update_login),
3615         last_update_date        = sysdate,
3616         change_pending_flag=p_change_flag
3617     where requisition_header_id = p_document_id;
3618 
3619     x_progress:='001';
3620 
3621     if(p_change_flag='Y') then
3622         update po_change_requests
3623         set wf_item_type=p_itemtype,
3624             wf_item_key=p_itemkey
3625         where change_request_group_id=p_change_request_group_id;
3626     end if;
3627 
3628   commit;
3629 
3630 EXCEPTION
3631   WHEN OTHERS THEN
3632     wf_core.context('PO_ReqChangeRequestWF_PVT','SetReqChangeFlag',x_progress);
3633         raise;
3634 
3635 END SetReqChangeFlag;
3636 
3637 
3638 /*************************************************************************
3639  * Private Procedure: InsertActionHist
3640  * Effects: insert into action history table.
3641  *
3642  *          It is called when the change request is submitted (by requester
3643  *          or buyer) and when buyer responds to the change request.
3644  *
3645  *          the action can be 'SUBMIT CHANGE', 'ACCEPTED', 'REJECTED'
3646  *          or 'RESPOND'
3647  *
3648  *          the process will commit when it exits.
3649  *
3650  * Returns:
3651  ************************************************************************/
3652 PROCEDURE InsertActionHist(
3653         itemtype varchar2,
3654         itemkey varchar2,
3655         p_doc_id number,
3656         p_doc_type varchar2,
3657         p_doc_subtype varchar2,
3658         p_employee_id number,
3659         p_action varchar2,
3660         p_note varchar2,
3661         p_path_id number) is
3662 
3663 pragma AUTONOMOUS_TRANSACTION;
3664 
3665 l_action_code po_action_history.action_code%type;
3666 l_revision_num number := NULL;
3667 l_hist_count   number := NULL;
3668 l_sequence_num   number := NULL;
3669 l_approval_path_id number;
3670 
3671 CURSOR action_hist_cursor(doc_id number , doc_type varchar2) is
3672    select max(sequence_num)
3673    from po_action_history
3674    where object_id= doc_id and
3675    object_type_code = doc_type;
3676 
3677 CURSOR action_hist_code_cursor (doc_id number , doc_type varchar2, seq_num number) is
3678    select action_code
3679    from po_action_history
3680    where object_id = doc_id and
3681    object_type_code = doc_type and
3682    sequence_num = seq_num;
3683 
3684 
3685 x_progress varchar2(3):='000';
3686 
3687 BEGIN
3688 
3689   /* Get the document authorization status.
3690   ** has been submitted before, i.e.
3691   ** First insert a row with  a SUBMIT action.
3692   ** Then insert a row with a NULL ACTION_CODE to simulate the forward-to
3693   */
3694 
3695   x_progress := '001';
3696 
3697   l_approval_path_id := p_path_id;
3698 
3699   IF p_doc_type IN ('PO','PA') THEN
3700 
3701     x_progress := '003';
3702 
3703       select revision_num
3704              into l_revision_num
3705       from PO_HEADERS_ALL
3706       where po_header_id = p_doc_id;
3707 
3708   ELSIF p_doc_type = 'RELEASE' THEN
3709 
3710       x_progress := '004';
3711 
3712       select revision_num
3713              into l_revision_num
3714       from PO_RELEASES_ALL
3715       where po_release_id = p_doc_id;
3716 
3717   END IF;
3718 
3719    x_progress := '005';
3720 
3721    /* Check if this document had been submitted to workflow at some point
3722    ** and somehow kicked out. If that's the case, the sequence number
3723    ** needs to be incremented by one. Otherwise start at zero.
3724    */
3725    OPEN action_hist_cursor(p_doc_id , p_doc_type );
3726    FETCH action_hist_cursor into l_sequence_num;
3727    CLOSE action_hist_cursor;
3728    IF l_sequence_num is NULL THEN
3729       l_sequence_num := 0;
3730    ELSE
3731       OPEN action_hist_code_cursor(p_doc_id , p_doc_type, l_sequence_num);
3732       FETCH action_hist_code_cursor into l_action_code;
3733       l_sequence_num := l_sequence_num +1;
3734    END IF;
3735 
3736 
3737    x_progress := '006';
3738 
3739    IF ((l_sequence_num = 0)
3740         OR
3741        (l_sequence_num > 0 and l_action_code is NOT NULL)) THEN
3742       x_progress := '007';
3743       INSERT into PO_ACTION_HISTORY
3744              (object_id,
3745               object_type_code,
3746               object_sub_type_code,
3747               sequence_num,
3748               last_update_date,
3749               last_updated_by,
3750               creation_date,
3751               created_by,
3752               action_code,
3753               action_date,
3754               employee_id,
3755               note,
3756               object_revision_num,
3757               last_update_login,
3758               request_id,
3759               program_application_id,
3760               program_id,
3761               program_update_date,
3762               approval_path_id,
3763               offline_code)
3764              VALUES
3765              (p_doc_id,
3766               p_doc_type,
3767               p_doc_subtype,
3768               l_sequence_num,
3769               sysdate,
3770               nvl(fnd_global.user_id, 1),
3771               sysdate,
3772               nvl(fnd_global.user_id, 1),
3773               p_action,
3774               decode(p_action, '',to_date('', 'YYYY-MM-DD'), sysdate),
3775               p_employee_id,
3776               p_note,
3777               l_revision_num,
3778               nvl(fnd_global.login_id, 1),
3779               0,
3780               0,
3781               0,
3782               '',
3783               l_approval_path_id,
3784               '' );
3785       x_progress := '008';
3786 
3787     ELSE
3788         l_sequence_num := l_sequence_num - 1;
3789         x_progress := '009';
3790 
3791         UPDATE PO_ACTION_HISTORY
3792           set object_id = p_doc_id,
3793               object_type_code = p_doc_type,
3794               object_sub_type_code = p_doc_subtype,
3795               sequence_num = l_sequence_num,
3796               last_update_date = sysdate,
3797               last_updated_by = nvl(fnd_global.user_id, 1),
3798               creation_date = sysdate,
3799               created_by = nvl(fnd_global.user_id, 1),
3800               action_code = p_action,
3801               action_date = decode(p_action, '',to_date('', 'YYYY-MM-DD'), sysdate),
3802               employee_id = p_employee_id,
3803               note = p_note,
3804               object_revision_num = l_revision_num,
3805               last_update_login = nvl(fnd_global.login_id, 1),
3806               request_id = 0,
3807               program_application_id = 0,
3808               program_id = 0,
3809               program_update_date = '',
3810               approval_path_id = l_approval_path_id,
3811               offline_code = ''
3812         WHERE
3813               object_id= p_doc_id and
3814               object_type_code = p_doc_type and
3815               sequence_num = l_sequence_num;
3816 
3817       x_progress := '010';
3818 
3819     END IF;
3820     x_progress := '017';
3821 
3822     /*
3823     -- AME Integration phase II enhancement.
3824     -- No need to insert null action code.
3825 
3826     if (p_action not in ('ACCEPT', 'REJECT')) then
3827       INSERT into PO_ACTION_HISTORY
3828              (object_id,
3829               object_type_code,
3830               object_sub_type_code,
3831               sequence_num,
3832               last_update_date,
3833               last_updated_by,
3834               creation_date,
3835               created_by,
3836               action_code,
3837               action_date,
3838               employee_id,
3839               note,
3840               object_revision_num,
3841               last_update_login,
3842               request_id,
3843               program_application_id,
3844               program_id,
3845               program_update_date,
3846               approval_path_id,
3847               offline_code)
3848              VALUES
3849              (p_doc_id,
3850               p_doc_type,
3851               p_doc_subtype,
3852               l_sequence_num + 1,
3853               sysdate,
3854               nvl(fnd_global.user_id, 1),
3855               sysdate,
3856               nvl(fnd_global.user_id, 1),
3857               NULL,              -- ACTION_CODE
3858               decode(p_action, '',to_date('', 'YYYY-MM-DD'), sysdate),
3859               p_employee_id,
3860               NULL,
3861               l_revision_num,
3862               nvl(fnd_global.login_id,1),
3863               0,
3864               0,
3865               0,
3866               '',
3867               0,
3868               '' );
3869       x_progress := '018';
3870 
3871     end if;
3872     */
3873 commit;
3874 EXCEPTION
3875    WHEN OTHERS THEN
3876         wf_core.context('PO_ReqChangeRequestWF_PVT',
3877                                'InsertActionHist'||sqlerrm,x_progress);
3878         raise;
3879 
3880 END InsertActionHist;
3881 
3882 
3883 /*************************************************************************
3884  *
3885  * Private Procedure: SetPOAuthStat
3886  * Effects: set the PO status to 'IN PROCESS',
3887  *          also set the change_requested_by to 'REQUESTER'
3888  *
3889  *          it will commit when it exit.
3890  *
3891  ************************************************************************/
3892 procedure SetPOAuthStat(p_document_id in number) is
3893 pragma AUTONOMOUS_TRANSACTION;
3894 
3895 x_progress varchar2(3):= '000';
3896 
3897 BEGIN
3898 
3899     x_progress := '001';
3900 
3901     -- fix bug 2733373. when change is submitted, and wait
3902     -- for buyer's response, we just set the status to
3903     -- 'IN PROCESS', but not the approved_flag
3904     update po_headers_all set
3905     AUTHORIZATION_STATUS = 'IN PROCESS',
3906 --    approved_flag='N',
3907     CHANGE_REQUESTED_BY='REQUESTER',
3908     last_updated_by         = fnd_global.user_id,
3909     last_update_login       = fnd_global.login_id,
3910     last_update_date        = sysdate
3911     where po_header_id = p_document_id;
3912 
3913     x_progress := '002';
3914 
3915     UPDATE po_line_locations_all
3916     SET
3917       approved_flag='R',
3918       last_update_date = sysdate,
3919       last_updated_by = fnd_global.user_id,
3920       last_update_login = fnd_global.login_id
3921     WHERE
3922      line_location_id in
3923        (select document_line_location_id
3924         from po_change_requests
3925         where
3926           request_level = 'SHIPMENT' and
3927           document_header_id = p_document_id and
3928           action_type        IN ('MODIFICATION', 'CANCELLATION') and
3929           initiator          = 'REQUESTER' and
3930           request_status     ='PENDING') and
3931     approved_flag='Y';
3932 
3933     commit;
3934 
3935 EXCEPTION
3936     WHEN OTHERS THEN
3937         wf_core.context('PO_REQ_CHANGE_WF','SetPOAuthStat',x_progress);
3938         raise;
3939 
3940 END SetPOAuthStat;
3941 
3942 /*************************************************************************
3943  *
3944  * Private Procedure: SetRelAuthStat
3945  * Effects: set the RELEASE status to 'IN PROCESS',
3946  *          also set the change_requested_by to 'REQUESTER'
3947  *
3948  *          it will commit when it exit.
3949  *
3950  ************************************************************************/
3951 procedure SetRelAuthStat(p_document_id in number) is
3952 pragma AUTONOMOUS_TRANSACTION;
3953 
3954 x_progress varchar2(3):= '000';
3955 
3956 BEGIN
3957 
3958     x_progress := '001';
3959 
3960     -- fix bug 2733373. when change is submitted, and wait
3961     -- for buyer's response, we just set the status to
3962     -- 'IN PROCESS', but not the approved_flag
3963     update po_releases_all   set
3964     AUTHORIZATION_STATUS = 'IN PROCESS',
3965 --    approved_flag='N',
3966     CHANGE_REQUESTED_BY='REQUESTER',
3967     last_updated_by         = fnd_global.user_id,
3968     last_update_login       = fnd_global.login_id,
3969     last_update_date        = sysdate
3970     where po_release_id = p_document_id;
3971 
3972     x_progress := '002';
3973 
3974     UPDATE po_line_locations_all
3975     SET
3976       approved_flag = 'R',
3977       last_update_date = sysdate,
3978       last_updated_by = fnd_global.user_id,
3979       last_update_login = fnd_global.login_id
3980     WHERE  line_location_id in (select document_line_location_id
3981                                 from   po_change_requests
3982                                 where  request_level  = 'SHIPMENT' and
3983                                 po_release_id  = p_document_id and
3984                                 action_type    IN ('MODIFICATION', 'CANCELLATION') and
3985                                 initiator      = 'REQUESTER' and
3986                                 request_status = 'PENDING') and
3987               approved_flag='Y';
3988 
3989 
3990     commit;
3991 
3992 EXCEPTION
3993     WHEN OTHERS THEN
3994         wf_core.context('PO_REQ_CHANGE_WF','SetRelAuthStat',x_progress);
3995         raise;
3996 
3997 END SetRelAuthStat;
3998 
3999 /*************************************************************************
4000  *
4001  * Private Procedure: StartInformBuyerWF
4002  * Effects: This procedure start the workflow process
4003  *          INFORM_BUYER_PO_CHANGE(PORPOCHA). It will be called
4004  *          by workflow POREQCHA when the change request is approved by
4005  *          requisition approval hierarchy.
4006  *
4007  *          It will call another private procedure StartPOChangeWF which
4008  *          will COMMIT the change.
4009  *
4010  ************************************************************************/
4011 procedure StartInformBuyerWF(p_change_request_group_id in number) is
4012 
4013 item_key varchar2(240);
4014 l_count number;
4015 x_progress              varchar2(100);
4016 
4017 l_doc_string varchar2(200);
4018 l_preparer_user_name varchar2(100);
4019 l_parent_item_type wf_items.item_type%type;
4020 l_parent_item_key wf_items.item_key%type;
4021 l_forward_from_username varchar2(200);
4022 l_user_id number;
4023 l_application_id number;
4024 l_responsibility_id number;
4025 
4026 cursor get_parent_info_csr(l_change_request_group_id number) is
4027     select pcr.wf_item_type, pcr.wf_item_key
4028     from po_change_requests pcr, po_change_requests pcr2
4029     where pcr2.change_request_group_id=l_change_request_group_id
4030           and pcr.change_request_id=pcr2.parent_change_request_id
4031           and pcr.wf_item_type is not null;
4032 
4033 begin
4034 
4035     x_progress :='StartInformBuyerWF:001';
4036     select PO_REQUESTER_CHANGE_WF_S.nextval into l_count from dual;
4037     item_key:='INFORM_'||to_char(p_change_request_group_id)||'_'||to_char(l_count);
4038     x_progress :='StartInformBuyerWF:key:'||item_key;
4039 
4040     open get_parent_info_csr(p_change_request_group_id);
4041     fetch get_parent_info_csr into l_parent_item_type, l_parent_item_key;
4042     close get_parent_info_csr;
4043 
4044     l_forward_from_username:= PO_WF_UTIL_PKG.GetItemAttrText(
4045                               itemtype=>l_parent_item_type,
4046                               itemkey=>l_parent_item_key,
4047                               aname =>'RESPONDER_USER_NAME');
4048 
4049     l_user_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => l_parent_item_type,
4050                                          itemkey  => l_parent_item_key,
4051                                          aname    => 'USER_ID');
4052 
4053     l_responsibility_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => l_parent_item_type,
4054                                          itemkey  => l_parent_item_key,
4055                                          aname    => 'RESPONSIBILITY_ID');
4056 
4057     l_application_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => l_parent_item_type,
4058                                          itemkey  => l_parent_item_key,
4059                                          aname    => 'APPLICATION_ID');
4060 
4061     StartPOChangeWF(p_change_request_group_id, item_key, 'INFORM_BUYER_PO_CHANGE', l_forward_from_username, l_user_id, l_responsibility_id, l_application_id);
4062     x_progress :='StartInformBuyerWF:002';
4063 EXCEPTION
4064   WHEN OTHERS THEN
4065     wf_core.context('PO_ReqChangeRequestWF_PVT','StartInformBuyerWF',x_progress);
4066 
4067     raise;
4068 
4069 end StartInformBuyerWF;
4070 
4071 
4072 /*************************************************************************
4073  * Private Procedure: StartConvertProcess
4074  *
4075  * Effects: Start CONVERT_INTO_PO_REQUEST process of workflow POREQCHA.
4076  *
4077  *          This process will be called when PO cancel API is called, which
4078  *          in turn close the affected req change requests. If a req change
4079  *          request is closed by the PO cancel API, we need start the wf
4080  *          process CONVERT_INTO_PO_REQUEST to do some cleaning work.
4081  *          This procedure will start the workflow process.
4082  *
4083  *          This procedure will be called by procedure
4084  *          Process_Cancelled_Req_Lines ( which will be called by PO cancel
4085  *          API)
4086  *
4087  * Returns:
4088  ************************************************************************/
4089 procedure StartConvertProcess(p_change_request_group_id in number,
4090                               p_old_item_key in varchar2) is
4091 l_count number;
4092 item_key wf_items.item_key%type;
4093 item_type wf_items.item_type%type:='POREQCHA';
4094 l_document_id number;
4095 l_document_number po_change_requests.document_num%type;
4096 l_org_id number;
4097 l_requester_id number;
4098 l_requester_name wf_roles.name%type;
4099 l_requester_display_name wf_roles.display_name%type;
4100 x_progress varchar2(100);
4101 l_functional_currency varchar2(200);
4102 l_total_amount_dsp varchar2(100);
4103 cursor change_request(l_change_request_group_id number) is
4104     select document_header_id, nvl(pcr.requester_id, por.preparer_id),
4105       document_num
4106     from po_change_requests pcr, po_requisition_headers_all por
4107     where pcr.change_request_group_id=l_change_request_group_id
4108           and pcr.document_header_id=por.requisition_header_id;
4109 
4110 l_old_req_amount varchar2(40);
4111 l_old_tax_amount varchar2(40);
4112 l_note po_action_history.note%TYPE;
4113 l_old_amount_currency varchar2(40);
4114 l_new_amount_currency varchar2(40);
4115 l_old_tax_currency varchar2(40);
4116 l_new_tax_currency varchar2(40);
4117 l_new_total_amount_dsp varchar2(40);
4118 
4119 n_varname   Wf_Engine.NameTabTyp;
4120 n_varval    Wf_Engine.NumTabTyp;
4121 
4122 t_varname   Wf_Engine.NameTabTyp;
4123 t_varval    Wf_Engine.TextTabTyp;
4124 
4125 begin
4126     x_progress :='StartConvertProcess:001';
4127 
4128     item_key:=p_old_item_key||'C';
4129 
4130     open change_request(p_change_request_group_id);
4131     fetch change_request into l_document_id, l_requester_id, l_document_number;
4132     close change_request;
4133     x_progress :='StartConvertProcess:002';
4134 
4135     select org_id
4136         into l_org_id
4137         from po_requisition_headers_all
4138         where requisition_header_id=l_document_id;
4139 
4140     x_progress :='StartConvertProcess:003';
4141 
4142     wf_engine.CreateProcess(itemtype => item_type,
4143                                          itemkey  => item_key,
4144                                          process => 'CONVERT_INTO_PO_REQUEST');
4145     x_progress :='StartConvertProcess:004';
4146 
4147     PO_REQAPPROVAL_INIT1.get_user_name(l_requester_id, l_requester_name, l_requester_display_name);
4148     x_progress :='StartConvertProcess:005';
4149 
4150     n_varname(1) := 'CHANGE_REQUEST_GROUP_ID';
4151     n_varval(1)  := p_change_request_group_id;
4152     n_varname(2) := 'DOCUMENT_ID';
4153     n_varval(2)  := l_document_id;
4154     n_varname(3) := 'ORG_ID';
4155     n_varval(3)  := l_org_id;
4156     Wf_Engine.SetItemAttrNumberArray(item_type, item_key,n_varname,n_varval);
4157     x_progress :='StartConvertProcess:006';
4158     t_varname(1) := 'DOCUMENT_TYPE';
4159     t_varval(1)  := 'REQ';
4160     t_varname(2) := 'DOCUMENT_SUBTYPE';
4161     t_varval(2)  := 'PURCHASING';
4162     t_varname(3) := 'PREPARER_USER_NAME';
4163     t_varval(3)  := l_requester_name;
4164     t_varname(4) := 'INTERFACE_SOURCE_CODE';
4165     t_varval(4)  := 'PO_CANCEL';
4166     t_varname(5) := 'PREPARER_DISPLAY_NAME';
4167     t_varval(5)  := l_requester_display_name;
4168     t_varname(6) := 'REQ_CHANGE_RESPONSE_NOTIF_BODY';
4169     t_varval(6)  := 'plsqlclob:PO_ReqChangeRequestNotif_PVT.get_req_chg_response_notif/'||to_char(p_change_request_group_id);
4170     l_total_amount_dsp:= PO_WF_UTIL_PKG.GetItemAttrText(itemtype=>item_type,
4171                               itemkey=>p_old_item_key,
4172                               aname =>'TOTAL_AMOUNT_DSP');
4173     l_functional_currency:= PO_WF_UTIL_PKG.GetItemAttrText(itemtype=>item_type,
4174                               itemkey=>p_old_item_key,
4175                               aname =>'FUNCTIONAL_CURRENCY');
4176     t_varname(7) := 'TOTAL_AMOUNT_DSP';
4177     t_varval(7)  := l_total_amount_dsp;
4178     t_varname(8) := 'FUNCTIONAL_CURRENCY';
4179     t_varval(8)  := l_functional_currency;
4180     l_note := PO_WF_UTIL_PKG.GetItemAttrText
4181                                 (itemtype   => item_type,
4182                                 itemkey    => p_old_item_key,
4183                                 aname      => 'JUSTIFICATION');
4184     l_old_amount_currency := PO_WF_UTIL_PKG.GetItemAttrText
4185                                 (itemtype   => item_type,
4186                                 itemkey    => p_old_item_key,
4187                                 aname      => 'REQ_AMOUNT_CURRENCY_DSP');
4188     l_new_amount_currency:= PO_WF_UTIL_PKG.GetItemAttrText
4189                                 (itemtype   => item_type,
4190                                 itemkey    => p_old_item_key,
4191                                 aname      => 'NEW_REQ_AMOUNT_CURRENCY_DSP');
4192     l_old_tax_currency:= PO_WF_UTIL_PKG.GetItemAttrText
4193                                 (itemtype   => item_type,
4194                                 itemkey    => p_old_item_key,
4195                                 aname      => 'TAX_AMOUNT_CURRENCY_DSP');
4196     l_new_tax_currency:= PO_WF_UTIL_PKG.GetItemAttrText
4197                                 (itemtype   => item_type,
4198                                 itemkey    => p_old_item_key,
4199                                 aname      => 'NEW_TAX_AMOUNT_CURRENCY_DSP');
4200     l_new_total_amount_dsp:= PO_WF_UTIL_PKG.GetItemAttrText
4201                                 (itemtype   => item_type,
4202                                 itemkey    => p_old_item_key,
4203                                 aname      => 'NEW_TOTAL_AMOUNT_DSP');
4204 
4205     t_varname(9) := 'JUSTIFICATION';
4206     t_varval(9)  := l_note;
4207     t_varname(10) := 'REQ_AMOUNT_CURRENCY_DSP';
4208     t_varval(10)  := l_old_amount_currency;
4209     t_varname(11) := 'NEW_REQ_AMOUNT_CURRENCY_DSP';
4210     t_varval(11)  := l_new_amount_currency;
4211     t_varname(12) := 'TAX_AMOUNT_CURRENCY_DSP';
4212     t_varval(12)  := l_old_tax_currency;
4213     t_varname(13) := 'NEW_TAX_AMOUNT_CURRENCY_DSP';
4214     t_varval(13)  := l_new_tax_currency;
4215     t_varname(14) := 'NEW_TOTAL_AMOUNT_DSP';
4216     t_varval(14)  := l_new_total_amount_dsp;
4217     t_varname(15) := 'DOCUMENT_NUMBER';
4218     t_varval(15)  := l_document_number;
4219 
4220     x_progress :='StartConvertProcess:007';
4221 
4222     Wf_Engine.SetItemAttrTextArray(item_type, item_key,t_varname,t_varval);
4223 
4224     wf_engine.StartProcess(itemtype => item_type,
4225                                          itemkey  => item_key);
4226 
4227     x_progress :='StartConvertProcess:008';
4228     commit;
4229 EXCEPTION
4230   WHEN OTHERS THEN
4231     wf_core.context('PO_ReqChangeRequestWF_PVT','StartConvertProcess',x_progress);
4232 
4233     raise;
4234 end StartConvertProcess;
4235 
4236 
4237 /*************************************************************************
4238  * Private Procedure: StartPOChangeWF
4239  *
4240  * Effects: It will be called to start the processes in PORPOCHA workflow
4241  *          The process name and item key will be passed by the caller.
4242  *
4243  *          it will commit when it exit.
4244  *
4245  ************************************************************************/
4246 procedure StartPOChangeWF(p_change_request_group_id in number,
4247                     p_item_key in varchar2,
4248                     p_process in varchar2,
4249                     p_forward_from_username in varchar2,
4250                     p_user_id in number,
4251                     p_responsibility_id in number,
4252                     p_application_id in number) is
4253 --pragma AUTONOMOUS_TRANSACTION;
4254 
4255 l_debug number:=1;
4256 
4257 l_count number;
4258 item_key varchar2(240);
4259 item_type varchar2(8):='PORPOCHA';
4260 l_document_id number;
4261 l_document_type varchar2(100);
4262 l_document_subtype varchar2(100);
4263 l_document_revision_num number;
4264 l_release_id number;
4265 l_org_id number;
4266 l_agent_id number;
4267 l_buyer_name varchar2(100);
4268 l_buyer_display_name varchar2(300);
4269 x_progress varchar2(100);
4270 l_preparer_id number;
4271 l_preparer_username varchar2(100);
4272 l_preparer_display_name varchar2(300);
4273 l_parent_group_id number;
4274 l_parent_item_type wf_items.item_type%type;
4275 l_parent_item_key wf_items.item_key%type;
4276 l_forward_from_username varchar2(100);
4277 
4278 l_document_num varchar2(20);
4279 l_requisition_num varchar2(20);
4280 
4281 l_req_amount number;
4282 l_tax_amount number;
4283 l_req_amount_dsp varchar2(100);
4284 l_tax_amount_dsp varchar2(100);
4285 l_total_amount number;
4286 l_total_amount_dsp varchar2(100);
4287 l_req_header_id number;
4288 l_currency_code varchar2(30);
4289 l_amount_for_subject varchar2(400);
4290 l_amount_for_header varchar2(400);
4291 l_amount_for_tax varchar2(400);
4292 
4293 --Bug#5114191
4294 l_inform_item_type PO_CHANGE_REQUESTS.wf_item_type%type;
4295 l_inform_item_key  PO_CHANGE_REQUESTS.wf_item_key%type;
4296 l_responder_user_id number;
4297 l_responder_resp_id number;
4298 l_responder_appl_id number;
4299 
4300 l_procedure_name CONSTANT VARCHAR2(100) := 'startPoChangeWf';
4301 
4302 
4303 cursor change_request(l_change_request_group_id number) is
4304     select document_type,
4305            document_header_id,
4306            document_revision_num,
4307            po_release_id,
4308            wf_item_type,
4309            wf_item_key
4310     from po_change_requests
4311     where change_request_group_id=l_change_request_group_id;
4312 
4313 --SQL What: get the preparer id of the req
4314 --SQL Why: wf attribute needs it
4315 --SQL Join: parent_change_request_id, requisition_header_id
4316 cursor req_preparer_id(l_change_request_group_id number) is
4317     select por.preparer_id
4318     from po_requisition_headers_all por,
4319          po_change_requests pcr1,
4320          po_change_requests pcr2
4321     where pcr2.change_request_group_id=l_change_request_group_id
4322         and pcr2.parent_change_request_id=pcr1.change_request_id
4323         and pcr1.document_header_id=por.requisition_header_id;
4324 
4325 cursor get_parent_group_id_csr(l_change_request_group_id number) is
4326     select change_request_group_id
4327     from po_change_requests
4328     where change_request_id in
4329         (select parent_change_request_id
4330             from po_change_requests
4331             where change_request_group_id=l_change_request_group_id);
4332 
4333 cursor get_parent_info_csr(l_change_request_group_id number) is
4334     select prh.segment1, pcr.wf_item_type, pcr.wf_item_key, prh.requisition_header_id
4335     from po_requisition_headers_all prh, po_change_requests pcr
4336     where prh.requisition_header_id=pcr.document_header_id
4337         and pcr.change_request_group_id=l_change_request_group_id;
4338 
4339 n_varname   Wf_Engine.NameTabTyp;
4340 n_varval    Wf_Engine.NumTabTyp;
4341 
4342 t_varname   Wf_Engine.NameTabTyp;
4343 t_varval    Wf_Engine.TextTabTyp;
4344 
4345 l_document_type_disp varchar2(200);
4346 
4347 begin
4348 
4349     G_CURRENT_RUNTIME_LEVEL := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
4350 
4351     IF (g_fnd_debug = 'Y') THEN
4352       IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
4353          FND_LOG.string(log_level => G_LEVEL_STATEMENT,
4354                         module    => G_MODULE_NAME||'.'||l_procedure_name,
4355                         message   => l_procedure_name||'.begin' || '-p_change_request_group_id: ' || to_char(p_change_request_group_id));
4356 
4357          FND_LOG.string(log_level => G_LEVEL_STATEMENT,
4358                         module    => G_MODULE_NAME||'.'||l_procedure_name,
4359                         message   => 'Parameters: '||'-p_change_request_group_id: ' || to_char(p_change_request_group_id) || '-p_item_key: ' || p_item_key || '-p_process: ' || p_process );
4360 
4361          FND_LOG.string(log_level => G_LEVEL_STATEMENT,
4362                         module    => G_MODULE_NAME||'.'||l_procedure_name,
4363                         message   => '-p_forward_from_username:'||p_forward_from_username || '-p_user_id:' ||to_char(p_user_id) || '-p_responsibility_id:' || to_char(p_responsibility_id) || '-p_application_id:' || to_char(p_application_id) );
4364 
4365       END IF;
4366     END IF;
4367 
4368     x_progress :='StartPOChangeWF:001';
4369 
4370     item_key:=p_item_key;
4371 
4372     open change_request(p_change_request_group_id);
4373     fetch change_request
4374     into  l_document_type,
4375           l_document_id,
4376           l_document_revision_num,
4377           l_release_id,
4378           l_inform_item_type,
4379           l_inform_item_key;
4380     close change_request;
4381     x_progress :='StartPOChangeWF:002';
4382 
4383     open req_preparer_id(p_change_request_group_id);
4384     fetch req_preparer_id into l_preparer_id;
4385     close req_preparer_id;
4386 
4387     if(l_document_type = 'RELEASE') then
4388         l_document_id:=l_release_id;
4389         l_document_subtype:='BLANKET';
4390 
4391         fnd_message.set_name ('PO','PO_MRC_VIEWCURR_PO_RELEASE');
4392         l_document_type_disp := fnd_message.get;
4393 
4394         select por.org_id, por.agent_id, poh.segment1||'-'||to_char(por.release_num)
4395         into l_org_id, l_agent_id, l_document_num
4396         from po_releases_all por, po_headers_all poh
4397         where por.po_release_id=l_document_id
4398             and por.po_header_id=poh.po_header_id;
4399     else
4400         l_document_subtype:='STANDARD';
4401 
4402         fnd_message.set_name ('PO','PO_WF_NOTIF_ORDER');
4403         l_document_type_disp := fnd_message.get;
4404 
4405         select org_id, agent_id, segment1
4406         into l_org_id, l_agent_id, l_document_num
4407         from po_headers_all
4408         where po_header_id=l_document_id;
4409     end if;
4410 
4411     x_progress :='StartPOChangeWF:003';
4412 
4413     IF (g_fnd_debug = 'Y') THEN
4414       IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
4415                      FND_LOG.string(log_level => G_LEVEL_STATEMENT,
4416                                     module    => G_MODULE_NAME||'.'||l_procedure_name,
4417                                     message   => 'change_request_group_id:' ||to_char(p_change_request_group_id) || '-before creating workflow');
4418       END IF;
4419     END IF;
4420 
4421     wf_engine.CreateProcess(itemtype => item_type,
4422                                          itemkey  => item_key,
4423                                          process => p_process);
4424     x_progress :='StartPOChangeWF:004';
4425 
4426     IF (g_fnd_debug = 'Y') THEN
4427       IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
4428                      FND_LOG.string(log_level => G_LEVEL_STATEMENT,
4429                                     module    => G_MODULE_NAME||'.'||l_procedure_name,
4430                                     message   => 'change_request_group_id:' ||to_char(p_change_request_group_id) || '-after creating workflow');
4431       END IF;
4432     END IF;
4433 
4434     PO_REQAPPROVAL_INIT1.get_user_name(l_agent_id, l_buyer_name, l_buyer_display_name);
4435     PO_REQAPPROVAL_INIT1.get_user_name(l_preparer_id, l_preparer_username, l_preparer_display_name);
4436     x_progress :='StartPOChangeWF:005';
4437 
4438     open get_parent_group_id_csr(p_change_request_group_id);
4439     fetch get_parent_group_id_csr into l_parent_group_id;
4440     close get_parent_group_id_csr;
4441 
4442     open get_parent_info_csr(l_parent_group_id);
4443     fetch get_parent_info_csr
4444      into l_requisition_num, l_parent_item_type, l_parent_item_key, l_req_header_id;
4445     close get_parent_info_csr;
4446 
4447     IF (g_fnd_debug = 'Y') THEN
4448          IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
4449                      FND_LOG.string(log_level => G_LEVEL_STATEMENT,
4450                                     module    => G_MODULE_NAME ||l_procedure_name,
4451                                     message   => 'change_request_group_id:' ||to_char(p_change_request_group_id)|| '-l_req_header_id:'||to_char(l_req_header_id) );
4452          END IF;
4453     END IF;
4454 
4455     x_progress :='StartPOChangeWF:006';
4456 
4457     n_varname(1) := 'CHANGE_REQUEST_GROUP_ID';
4458     n_varval(1)  := p_change_request_group_id;
4459     n_varname(2) := 'DOCUMENT_ID';
4460     n_varval(2)  := l_document_id;
4461     n_varname(3) := 'DOCUMENT_REVISION_NUM';
4462     n_varval(3)  := l_document_revision_num;
4463     n_varname(4) := 'ORG_ID';
4464     n_varval(4)  := l_org_id;
4465     n_varname(5) := 'PREPARER_ID';
4466     n_varval(5)  := l_preparer_id;
4467     n_varname(6) := 'USER_ID';
4468     n_varval(6)  := p_user_id;
4469     n_varname(7) := 'RESPONSIBILITY_ID';
4470     n_varval(7)  := p_responsibility_id;
4471     n_varname(8) := 'APPLICATION_ID';
4472     n_varval(8)  := p_application_id;
4473     Wf_Engine.SetItemAttrNumberArray(item_type, item_key,n_varname,n_varval);
4474 
4475 --    fnd_global.APPS_INITIALIZE (p_user_id, p_responsibility_id, p_application_id);
4476 
4477     t_varname(1) := 'DOCUMENT_TYPE';
4478     t_varval(1)  := l_document_type;
4479     t_varname(2) := 'DOCUMENT_SUBTYPE';
4480     t_varval(2)  := l_document_subtype;
4481     t_varname(3) := 'BUYER_USER_NAME';
4482     t_varval(3)  := l_buyer_name;
4483     t_varname(4) := 'PREPARER_USER_NAME';
4484     t_varval(4)  := l_preparer_username;
4485     t_varname(5) := 'PREPARER_DISPLAY_NAME';
4486     t_varval(5)  := l_preparer_display_name;
4487     t_varname(6) := 'PO_CHANGE_APPROVAL_NOTIF_BODY';
4488     t_varval(6)  := 'plsqlclob:PO_ReqChangeRequestNotif_PVT.get_po_chg_approval_notif/'
4489                         ||to_char(p_change_request_group_id);
4490     t_varname(7) := 'DOCUMENT_NUMBER';
4491     t_varval(7)  := l_document_num;
4492     t_varname(8) := 'REQ_CHANGE_RESPONSE_NOTIF_BODY';
4493     t_varval(8)  := 'plsqlclob:PO_ReqChangeRequestNotif_PVT.get_req_chg_response_notif/'
4494                         ||to_char(l_parent_group_id);
4495     t_varname(9) := 'REQUISITION_NUMBER';
4496     t_varval(9)  := l_requisition_num;
4497     t_varname(10) := 'FORWARD_FROM_USERNAME';
4498     t_varval(10)  := p_forward_from_username;
4499     t_varname(11) := 'CHANGE_REQUESTS_URL';
4500     t_varval(11)  := FND_WEB_CONFIG.JSP_AGENT||'OA.jsp?OAFunc=POS_PENDING_CHANGES';
4501     t_varname(12) := 'DOCUMENT_TYPE_DISP';
4502     t_varval(12)  := l_document_type_disp;
4503 
4504     x_progress :='StartPOChangeWF:007';
4505     Wf_Engine.SetItemAttrTextArray(item_type, item_key,t_varname,t_varval);
4506 
4507     -- bug 5379796, need to get old req_amount,tax_amount before kicking off the workflow,    -- since 'buyer response' wf will update reqs.
4508     if (p_process='PROCESS_BUYER_RESPONSE') then
4509 
4510        SELECT gsb.currency_code
4511        INTO   l_currency_code
4512        FROM   financials_system_params_all fsp,
4513             gl_sets_of_books gsb
4514        WHERE  fsp.set_of_books_id = gsb.set_of_books_id
4515        AND  fsp.org_id          = l_org_id;
4516 
4517        -- get old req amount and amount dsp
4518        SELECT nvl(SUM(nvl(decode(matching_basis, 'AMOUNT', amount, quantity * unit_price), 0)), 0)
4519        into l_req_amount
4520        FROM   po_requisition_lines_all
4521        WHERE  requisition_header_id = l_req_header_id
4522        AND  NVL(cancel_flag,'N') = 'N'
4523        AND  NVL(modified_by_agent_flag, 'N') = 'N';
4524 
4525        l_req_amount_dsp := TO_CHAR(l_req_amount,FND_CURRENCY.GET_FORMAT_MASK(
4526                                        l_currency_code, g_currency_format_mask));
4527 
4528        -- get old tax and tax dsp
4529        SELECT nvl(sum(nonrecoverable_tax), 0)
4530        into l_tax_amount
4531        FROM   po_requisition_lines_all rl,
4532           po_req_distributions_all rd
4533        WHERE  rl.requisition_header_id = l_req_header_id
4534        AND  rd.requisition_line_id = rl.requisition_line_id
4535        AND  NVL(rl.cancel_flag,'N') = 'N'
4536        AND  NVL(rl.modified_by_agent_flag, 'N') = 'N';
4537 
4538        l_tax_amount_dsp := TO_CHAR(l_tax_amount,FND_CURRENCY.GET_FORMAT_MASK(
4539                                        l_currency_code, g_currency_format_mask));
4540        l_total_amount := l_req_amount + l_tax_amount;
4541 
4542        l_total_amount_dsp := TO_CHAR(l_total_amount,FND_CURRENCY.GET_FORMAT_MASK(
4543                                        l_currency_code, g_currency_format_mask));
4544 
4545        x_progress :='StartPOChangeWF:008';
4546 
4547        getReqAmountInfo(itemtype => item_type,
4548                           itemkey => item_key,
4549                           p_function_currency => l_currency_code,
4550                           p_total_amount_disp => l_total_amount_dsp,
4551                           p_total_amount => l_total_amount,
4552                           p_req_amount_disp => l_req_amount_dsp,
4553                           p_req_amount => l_req_amount,
4554                           p_tax_amount_disp => l_tax_amount_dsp,
4555                           p_tax_amount => l_tax_amount,
4556                           x_amount_for_subject => l_amount_for_subject,
4557                           x_amount_for_header => l_amount_for_header,
4558                           x_amount_for_tax => l_amount_for_tax);
4559 
4560        x_progress :='StartPOChangeWF:009';
4561 
4562        PO_WF_UTIL_PKG.SetItemAttrText (     itemtype    => item_type,
4563                                    itemkey     => item_key,
4564                                    aname       => 'TOTAL_AMOUNT_DSP',
4565                                    avalue      =>  l_amount_for_subject);
4566 
4567        PO_WF_UTIL_PKG.SetItemAttrText (     itemtype    => item_type,
4568                                    itemkey     => item_key,
4569                                    aname       => 'REQ_AMOUNT_CURRENCY_DSP',
4570                                    avalue      =>  l_amount_for_header);
4571 
4572        PO_WF_UTIL_PKG.SetItemAttrText (     itemtype    => item_type,
4573                                    itemkey     => item_key,
4574                                    aname       => 'TAX_AMOUNT_CURRENCY_DSP',
4575                                    avalue      =>  l_amount_for_tax);
4576 
4577        --Bug#5114191 : Set responsibility user id, resp id, appl id for
4578        -- processbuyer response wf from inform buyer wf.
4579 
4580        l_responder_user_id := nvl(PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => l_inform_item_type,
4581                                          itemkey  => l_inform_item_key,
4582                                          aname    => 'RESPONDER_USER_ID'),
4583                                   fnd_global.user_id);
4584        l_responder_resp_id := nvl( PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => l_inform_item_type,
4585                                          itemkey  => l_inform_item_key,
4586                                          aname    => 'RESPONDER_RESP_ID'),
4587                                   fnd_global.resp_id);
4588        l_responder_appl_id := nvl( PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => l_inform_item_type,
4589                                          itemkey  => l_inform_item_key,
4590                                          aname    => 'RESPONDER_APPL_ID'),
4591                                   fnd_global.resp_appl_id);
4592 
4593 
4594        IF (g_fnd_debug = 'Y') THEN
4595          IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
4596                      FND_LOG.string(log_level => G_LEVEL_STATEMENT,
4597                                     module    => G_MODULE_NAME||'.'||l_procedure_name,
4598                                     message   => 'l_responder_user_id => ' ||l_responder_user_id || 'l_responder_resp_id => ' || l_responder_resp_id || ' l_responder_appl_id => ' ||l_responder_appl_id );
4599          END IF;
4600        END IF;
4601 
4602 
4603       PO_WF_UTIL_PKG.SetItemAttrNumber (   itemtype   => item_type,
4604                                         itemkey    => item_key,
4605                                         aname      => 'RESPONDER_USER_ID',
4606                                         avalue     => l_responder_user_id);
4607       PO_WF_UTIL_PKG.SetItemAttrNumber (   itemtype   => item_type,
4608                                         itemkey    => item_key,
4609                                         aname      => 'RESPONDER_RESP_ID',
4610                                         avalue     => l_responder_resp_id);
4611       PO_WF_UTIL_PKG.SetItemAttrNumber (   itemtype   => item_type,
4612                                         itemkey    => item_key,
4613                                         aname      => 'RESPONDER_APPL_ID',
4614                                         avalue     => l_responder_appl_id);
4615 
4616     end if;
4617         x_progress :='StartPOChangeWF:0095';
4618 
4619 /* Bug# 3431902 - Need to set the Parent Child relationship between processes */
4620 
4621     IF (g_fnd_debug = 'Y') THEN
4622       IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
4623                      FND_LOG.string(log_level => G_LEVEL_STATEMENT,
4624                                     module    => G_MODULE_NAME||'.'||l_procedure_name,
4625                                     message   => 'change_request_group_id:' ||to_char(p_change_request_group_id)||'-before setting the parent-child relationship.');
4626       END IF;
4627     END IF;
4628 
4629     wf_engine.setItemParent( itemtype => item_type,
4630                              itemkey  => item_key,
4631                              parent_itemtype => l_parent_item_type,
4632                              parent_itemkey  => l_parent_item_key,
4633                              parent_context  => NULL);
4634 
4635      IF (g_fnd_debug = 'Y') THEN
4636       IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
4637                      FND_LOG.string(log_level => G_LEVEL_STATEMENT,
4638                                     module    => G_MODULE_NAME||'.'||l_procedure_name,
4639                                     message   => 'change_request_group_id:' ||to_char(p_change_request_group_id) ||'-after setting the parent-child relationship.');
4640 
4641                      FND_LOG.string(log_level => G_LEVEL_STATEMENT,
4642                                     module    => G_MODULE_NAME||'.'||l_procedure_name,
4643                                     message   =>  'change_request_group_id:' ||to_char(p_change_request_group_id)||'-before kicking off workflow');
4644       END IF;
4645     END IF;
4646 
4647     x_progress :='StartPOChangeWF:010';
4648 
4649     wf_engine.StartProcess(itemtype => item_type,
4650                                          itemkey  => item_key);
4651 
4652     IF (g_fnd_debug = 'Y') THEN
4653       IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
4654                      FND_LOG.string(log_level => G_LEVEL_STATEMENT,
4655                                     module    => G_MODULE_NAME||'.'||l_procedure_name,
4656                                     message   => 'change_request_group_id:' ||to_char(p_change_request_group_id) ||'-after kicking off workflow');
4657       END IF;
4658     END IF;
4659 
4660     x_progress :='StartPOChangeWF:011';
4661     if(p_process='INFORM_BUYER_PO_CHANGE') then
4662         update po_change_requests
4663            set wf_item_type=item_type,
4664                wf_item_key=item_key
4665          where change_request_group_id=p_change_request_group_id;
4666     end if;
4667 
4668 --    commit;
4669 
4670     IF (g_fnd_debug = 'Y') THEN
4671       IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
4672                      FND_LOG.string(log_level => G_LEVEL_STATEMENT,
4673                                     module    => G_MODULE_NAME||'.'||l_procedure_name ,
4674                                     message   => l_procedure_name||'.end'||'-change_request_group_id:' ||to_char(p_change_request_group_id));
4675       END IF;
4676     END IF;
4677 
4678 EXCEPTION
4679   WHEN OTHERS THEN
4680     IF (g_fnd_debug = 'Y') THEN
4681       IF( FND_LOG.LEVEL_EXCEPTION >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
4682                      FND_LOG.string(log_level => G_LEVEL_EXCEPTION,
4683                                     module    => G_MODULE_NAME||'.'||l_procedure_name,
4684                                     message   => l_procedure_name||'-'|| x_progress|| ' ' || sqlerrm);
4685       END IF;
4686     END IF;
4687 
4688     wf_core.context('PO_ReqChangeRequestWF_PVT','StartPOChangeWF',x_progress||sqlerrm);
4689     raise;
4690 end StartPOChangeWF;
4691 
4692 
4693 procedure UpdatePODocHeaderTables(p_document_type varchar2, p_document_id number)
4694 is
4695 pragma AUTONOMOUS_TRANSACTION;
4696 x_progress   varchar2(100);
4697 
4698 BEGIN
4699     x_progress := 'PO_ReqChangeRequestWF_PVT.UpdatePODocHeaderTables';
4700 
4701     if(p_document_type = 'PO') then
4702         update po_headers_all
4703            set change_requested_by = null,
4704            last_updated_by         = fnd_global.user_id,
4705            last_update_login       = fnd_global.login_id,
4706            last_update_date        = sysdate
4707          where po_header_id = p_document_id;
4708     else
4709         update po_releases_all
4710            set change_requested_by = null,
4711            last_updated_by         = fnd_global.user_id,
4712            last_update_login       = fnd_global.login_id,
4713            last_update_date        = sysdate
4714          where po_release_id = p_document_id;
4715     end if;
4716     commit;
4717 
4718 EXCEPTION
4719    WHEN OTHERS THEN
4720       wf_core.context('PO_ReqChangeRequestWF_PVT','UpdatePODocHeaderTables',x_progress|| sqlerrm);
4721       raise;
4722 END UpdatePODocHeaderTables;
4723 
4724 
4725 /*************************************************************************
4726  *
4727  * Public Procedure: Get_Req_Chg_Attributes
4728  * Effects: workflow procedure, used in POREQCHA
4729  *
4730  *          Initialize the workflow attribute
4731  *
4732 ************************************************************************/
4733 procedure Get_Req_Chg_Attributes(itemtype        in varchar2,
4734                                 itemkey         in varchar2,
4735                                 actid           in number,
4736                                 funcmode        in varchar2,
4737                                 resultout       out NOCOPY varchar2    )is
4738 l_orgid       number;
4739 l_change_request_group_id number;
4740 l_doc_id number;
4741 
4742 x_progress              varchar2(100);
4743 
4744 l_doc_string varchar2(200);
4745 l_preparer_user_name varchar2(100);
4746 
4747 cursor change_request_group_id is
4748     select max(change_request_group_id)
4749     from po_change_requests
4750     where document_header_id=l_doc_id
4751             and initiator='REQUESTER'
4752             and request_status='NEW';
4753 BEGIN
4754 
4755   x_progress := 'PO_ReqChangeRequestWF_PVT.Get_Req_Chg_Attributes';
4756 
4757 
4758   -- Do nothing in cancel or timeout mode
4759   if (funcmode <> wf_engine.eng_run) then
4760 
4761       resultout := wf_engine.eng_null;
4762       return;
4763 
4764   end if;
4765 
4766   l_doc_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
4767                                          itemkey  => itemkey,
4768                                          aname    => 'DOCUMENT_ID');
4769 
4770   -- Set the multi-org context
4771   l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
4772                                          itemkey  => itemkey,
4773                                          aname    => 'ORG_ID');
4774 
4775   IF l_orgid is NOT NULL THEN
4776 
4777     PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
4778 
4779   END IF;
4780 
4781   open change_request_group_id;
4782   fetch change_request_group_id into l_change_request_group_id;
4783   close change_request_group_id;
4784 
4785   PO_WF_UTIL_PKG.SetItemAttrNumber (   itemtype   => itemtype,
4786                                         itemkey    => itemkey,
4787                                         aname      => 'CHANGE_REQUEST_GROUP_ID',
4788                                         avalue     => l_change_request_group_id);
4789 
4790   PO_WF_UTIL_PKG.SetItemAttrText(itemtype => itemtype,
4791                             itemkey  => itemkey,
4792                             aname    => 'REQ_CHANGE_NOTIF_BODY',
4793                             avalue   =>
4794                          'plsqlclob:PO_ReqChangeRequestNotif_PVT.get_req_chg_approval_notif/'||
4795                          itemtype||':'||
4796                          itemkey);
4797   PO_WF_UTIL_PKG.SetItemAttrText(itemtype => itemtype,
4798                             itemkey  => itemkey,
4799                             aname    => 'REQ_CHANGE_RESPONSE_NOTIF_BODY',
4800                             avalue   =>
4801                          'plsqlclob:PO_ReqChangeRequestNotif_PVT.get_req_chg_response_notif/'||
4802                          to_char(l_change_request_group_id));
4803 
4804 /*
4805   PO_WF_UTIL_PKG.SetItemAttrText(itemtype => itemtype,
4806                             itemkey  => itemkey,
4807                             aname    => 'NEW_AMOUNT_WITH_CURRENCY_D',
4808                             avalue   =>
4809                          'plsql:PO_ReqChangeRequestNotif_PVT.get_new_req_amount/'||itemtype||':'||itemkey);
4810 
4811 */
4812   x_progress := 'PO_ReqChangeRequestWF_PVT.Get_Req_Chg_Attributes: 02';
4813   resultout := wf_engine.eng_completed || ':' ||  'ACTIVITY_PERFORMED';
4814 
4815 EXCEPTION
4816   WHEN OTHERS THEN
4817     l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
4818     l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(itemType, itemkey);
4819     wf_core.context('PO_ReqChangeRequestWF_PVT','Get_Req_Chg_Attributes',x_progress);
4820     PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey, l_preparer_user_name, l_doc_string, sqlerrm, 'PO_ReqChangeRequestWF_PVT.Get_Req_Chg_Attributes');
4821     raise;
4822 
4823 
4824 end Get_Req_Chg_Attributes;
4825 
4826 
4827 
4828 /*************************************************************************
4829  *
4830  * Public Procedure: Update_Req_Change_Flag
4831  * Effects: workflow procedure, called at the beginning of POREQCHA
4832  *
4833  *          set the change_pending_flag in the po_requisition_headers_all
4834  *          table to 'Y'
4835  *
4836  ************************************************************************/
4837 procedure Update_Req_Change_Flag(itemtype        in varchar2,
4838                                 itemkey         in varchar2,
4839                                 actid           in number,
4840                                 funcmode        in varchar2,
4841                                 resultout       out NOCOPY varchar2    ) is
4842 
4843 l_document_id           NUMBER;
4844 l_change_request_group_id number;
4845 l_orgid                 NUMBER;
4846 x_progress              varchar2(100);
4847 
4848 l_doc_string varchar2(200);
4849 l_preparer_user_name wf_roles.name%type;
4850 
4851 BEGIN
4852 
4853     x_progress := 'PO_ReqChangeRequestWF_PVT.Update_Req_Change_Flag: 01';
4854 
4855     -- Do nothing in cancel or timeout mode
4856     --
4857     if (funcmode <> wf_engine.eng_run) then
4858 
4859         resultout := wf_engine.eng_null;
4860         return;
4861 
4862     end if;
4863 
4864     -- Set the multi-org context
4865     l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
4866                                          itemkey  => itemkey,
4867                                          aname    => 'ORG_ID');
4868 
4869     IF l_orgid is NOT NULL THEN
4870 
4871         PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
4872 
4873     END IF;
4874 
4875     l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (
4876                itemtype => itemtype,
4877                itemkey  => itemkey,
4878                aname    => 'CHANGE_REQUEST_GROUP_ID');
4879 
4880     l_document_id := PO_WF_UTIL_PKG.GetItemAttrNumber (
4881                itemtype => itemtype,
4882                itemkey  => itemkey,
4883                aname    => 'DOCUMENT_ID');
4884 
4885     SetReqChangeFlag(l_change_request_group_id,
4886                l_document_id, itemtype, itemkey, 'Y');
4887 
4888     --
4889     resultout := wf_engine.eng_completed || ':' ||  'ACTIVITY_PERFORMED';
4890     --
4891 
4892     x_progress := 'PO_REQAPPROVAL_INIT1.Update_Req_Change_Flag: 02';
4893 
4894 
4895 EXCEPTION
4896     WHEN OTHERS THEN
4897         l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
4898         l_preparer_user_name := PO_REQAPPROVAL_INIT1.
4899                get_preparer_user_name(itemType, itemkey);
4900         wf_core.context('PO_ReqChangeRequestWF_PVT',
4901                'Update_Req_Change_Flag',x_progress);
4902         PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey,
4903                l_preparer_user_name, l_doc_string,
4904                sqlerrm, 'PO_ReqChangeRequestWF_PVT.Update_Req_Change_Flag');
4905         raise;
4906 
4907 END Update_Req_Change_Flag;
4908 
4909 /*************************************************************************
4910  *
4911  * Public Procedure: Insert_into_History_CHGsubmit
4912  * Effects: workflow procedure, called in workflow POREQCHA and
4913  *          PORPOCHA (INFORM_BUYER_PO_CHANGE)
4914  *
4915  *          inserting into action history table 'submit change'
4916  *
4917  ************************************************************************/
4918 procedure Insert_into_History_CHGsubmit(itemtype        in varchar2,
4919                                 itemkey         in varchar2,
4920                                 actid           in number,
4921                                 funcmode        in varchar2,
4922                                 resultout       out NOCOPY varchar2    ) is
4923 
4924 l_doc_id number;
4925 l_doc_type varchar2(25);
4926 l_doc_subtype varchar2(25);
4927 l_note        PO_ACTION_HISTORY.note%TYPE;
4928 l_employee_id number;
4929 l_orgid       number;
4930 
4931 x_progress              varchar2(100);
4932 
4933 l_doc_string varchar2(200);
4934 l_preparer_user_name varchar2(100);
4935 
4936 l_path_id number;
4937 l_change_request_group_id number;
4938 cursor change_request is
4939     select requester_id
4940     from po_change_requests
4941     where change_request_group_id=l_change_request_group_id;
4942 
4943 
4944 BEGIN
4945 
4946     x_progress := 'PO_ReqChangeRequestWF_PVT.Insert_into_History_CHGsubmit: 01';
4947 
4948     -- Do nothing in cancel or timeout mode
4949     if (funcmode <> wf_engine.eng_run) then
4950         resultout := wf_engine.eng_null;
4951         return;
4952     end if;
4953 
4954     l_change_request_group_id:= PO_WF_UTIL_PKG.GetItemAttrNumber (
4955                    itemtype => itemtype,
4956                    itemkey  => itemkey,
4957                    aname    => 'CHANGE_REQUEST_GROUP_ID');
4958 
4959     x_progress := 'PO_ReqChangeRequestWF_PVT.Insert_into_History_CHGsubmit: 02';
4960     open change_request;
4961     fetch change_request into l_employee_id;
4962     close change_request;
4963 
4964     x_progress := 'PO_ReqChangeRequestWF_PVT.Insert_into_History_CHGsubmit: 03';
4965     l_doc_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
4966                                          itemkey  => itemkey,
4967                                          aname    => 'DOCUMENT_ID');
4968 
4969     l_doc_type := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
4970                                          itemkey  => itemkey,
4971                                          aname    => 'DOCUMENT_TYPE');
4972 
4973     l_doc_subtype := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
4974                                          itemkey  => itemkey,
4975                                          aname    => 'DOCUMENT_SUBTYPE');
4976 
4977     x_progress := 'PO_ReqChangeRequestWF_PVT.Insert_into_History_CHGsubmit: 04';
4978     -- Set the multi-org context
4979     l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
4980                                          itemkey  => itemkey,
4981                                          aname    => 'ORG_ID');
4982 
4983     IF l_orgid is NOT NULL THEN
4984         PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
4985     END IF;
4986 
4987     l_note := PO_WF_UTIL_PKG.GetItemAttrText(itemtype => itemtype,
4988                                          itemkey  => itemkey,
4989                                          aname    => 'NOTE');
4990 
4991 
4992     x_progress := 'PO_ReqChangeRequestWF_PVT.Insert_into_History_CHGsubmit: 05';
4993     if(l_employee_id is null) then
4994         if(l_doc_type='PO') then
4995             select agent_id
4996             into l_employee_id
4997             from po_headers_all
4998             where po_header_id=l_doc_id;
4999         elsif(l_doc_type='REQUISITION') then
5000             select preparer_id
5001             into l_employee_id
5002             from po_requisition_headers_all
5003             where requisition_header_id=l_doc_id;
5004         else
5005             select agent_id
5006             into l_employee_id
5007             from po_releases_all
5008             where po_release_id=l_doc_id;
5009         end if;
5010     end if;
5011 
5012     x_progress := 'PO_ReqChangeRequestWF_PVT.Insert_into_History_CHGsubmit: 06';
5013     InsertActionHist(itemtype,itemkey,l_doc_id, l_doc_type,
5014                      l_doc_subtype, l_employee_id,
5015                      'SUBMIT CHANGE', l_note, null);
5016 
5017     resultout := wf_engine.eng_completed || ':' || 'ACTIVITY_PERFORMED' ;
5018 
5019     x_progress := 'PO_ReqChangeRequestWF_PVT.Insert_into_History_CHGsubmit: 07';
5020 
5021 EXCEPTION
5022     WHEN OTHERS THEN
5023         l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
5024         l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(
5025                     itemType, itemkey);
5026         wf_core.context('PO_ReqChangeRequestWF_PVT',
5027                     'Insert_into_History_CHGsubmit',x_progress||sqlerrm);
5028         PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey,
5029                     l_preparer_user_name, l_doc_string, sqlerrm,
5030                     'PO_ReqChangeRequestWF_PVT.Insert_into_History_CHGsubmit');
5031         raise;
5032 
5033 END Insert_into_History_CHGsubmit;
5034 
5035 
5036 /*************************************************************************
5037  *
5038  * Public Procedure: Req_Change_Needs_Approval
5039  * Effects: workflow procedure, called in workflow POREQCHA
5040  *
5041  *          check if there is still pending change request requires
5042  *          requisition approval hierarchy's approval
5043  *
5044  ************************************************************************/
5045 procedure Req_Change_Needs_Approval(itemtype        in varchar2,
5046                                 itemkey         in varchar2,
5047                                 actid           in number,
5048                                 funcmode        in varchar2,
5049                                 resultout       out NOCOPY varchar2    )is
5050 
5051 l_document_id           NUMBER;
5052 l_change_request_group_id number;
5053 l_orgid                 NUMBER;
5054 x_progress              varchar2(100);
5055 
5056 l_doc_string varchar2(200);
5057 l_preparer_user_name varchar2(100);
5058 
5059 l_approval_flag po_change_requests.approval_required_flag%type;
5060 
5061 cursor approval_flag is
5062     select approval_required_flag
5063     from po_change_requests
5064     where change_request_group_id=l_change_request_group_id
5065         and action_type='MODIFICATION'
5066         and request_status = 'NEW';
5067 
5068 BEGIN
5069 
5070     x_progress := 'PO_ReqChangeRequestWF_PVT.Req_Change_Needs_Approval: 01';
5071 
5072     -- Do nothing in cancel or timeout mode
5073     if (funcmode <> wf_engine.eng_run) then
5074         resultout := wf_engine.eng_null;
5075         return;
5076     end if;
5077 
5078     l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (
5079                           itemtype => itemtype,
5080                           itemkey  => itemkey,
5081                           aname    => 'CHANGE_REQUEST_GROUP_ID');
5082 
5083     SetReqRequestStatus(l_change_request_group_id, 'Y', 'MGR_APP', null, null);
5084 
5085     open approval_flag;
5086     fetch approval_flag into l_approval_flag;
5087     if(approval_flag%NOTFOUND) then
5088         resultout := wf_engine.eng_completed || ':' ||  'N';
5089     else
5090         resultout := wf_engine.eng_completed || ':'
5091                          || nvl(l_approval_flag, 'Y');
5092     end if;
5093     close approval_flag;
5094 
5095     x_progress := 'PO_ReqChangeRequestWF_PVT.Req_Change_Needs_Approval: 02';
5096 
5097 
5098 EXCEPTION
5099     WHEN OTHERS THEN
5100         l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
5101         l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(
5102                             itemType, itemkey);
5103         wf_core.context('PO_REQ_CHANGE_WF','APPROVAL_NEEDED',x_progress);
5104         PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey,
5105                             l_preparer_user_name, l_doc_string,
5106                             sqlerrm,
5107                             'PO_REQ_CHANGE_WF.Req_Change_Needs_Approval');
5108         raise;
5109 
5110 END Req_Change_Needs_Approval;
5111 
5112 /*************************************************************************
5113  *
5114  * Public Procedure: Set_Change_Mgr_Pre_App
5115  * Effects: workflow procedure, called in workflow POREQCHA
5116  *
5117  *          set the request_status of change request to 'MGR_PRE_APP'
5118  *
5119  ************************************************************************/
5120 procedure Set_Change_Mgr_Pre_App(itemtype        in varchar2,
5121                                 itemkey         in varchar2,
5122                                 actid           in number,
5123                                 funcmode        in varchar2,
5124                                 resultout       out NOCOPY varchar2    ) is
5125 
5126 l_document_id           NUMBER;
5127 l_change_request_group_id number;
5128 l_orgid                 NUMBER;
5129 x_progress              varchar2(100);
5130 
5131 l_doc_string varchar2(200);
5132 l_preparer_user_name varchar2(100);
5133 
5134 BEGIN
5135 
5136     x_progress := 'PO_ReqChangeRequestWF_PVT.Set_Change_Mgr_Pre_App: 01';
5137 
5138 
5139     -- Do nothing in cancel or timeout mode
5140     if (funcmode <> wf_engine.eng_run) then
5141         resultout := wf_engine.eng_null;
5142         return;
5143     end if;
5144 
5145     l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
5146                                          itemkey  => itemkey,
5147                                          aname    => 'CHANGE_REQUEST_GROUP_ID');
5148 
5149 
5150     SetReqRequestStatus(l_change_request_group_id, 'N', 'MGR_PRE_APP',
5151                         null, null);
5152 
5153   --
5154     resultout := wf_engine.eng_completed || ':' ||  'ACTIVITY_PERFORMED';
5155   --
5156 
5157     x_progress := 'PO_ReqChangeRequestWF_PVT.Set_Change_Mgr_Pre_App: 02';
5158 
5159 
5160 EXCEPTION
5161     WHEN OTHERS THEN
5162         l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
5163         l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(
5164                           itemType, itemkey);
5165         wf_core.context('PO_REQ_CHANGE_WF','Set_Change_Mgr_Pre_App',x_progress);
5166         PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey,
5167                           l_preparer_user_name, l_doc_string,
5168                           sqlerrm, 'PO_REQ_CHANGE_WF.Set_Change_Mgr_Pre_App');
5169         raise;
5170 
5171 END Set_Change_Mgr_Pre_App;
5172 
5173 
5174 
5175 /*************************************************************************
5176  *
5177  * Public Procedure: Set_Change_Mgr_App
5178  * Effects: workflow procedure, called in workflow POREQCHA
5179  *
5180  *          set the request_status of change request to 'MGR_APP'
5181  *
5182  ************************************************************************/
5183 procedure Set_Change_Mgr_App(itemtype        in varchar2,
5184                                 itemkey         in varchar2,
5185                                 actid           in number,
5186                                 funcmode        in varchar2,
5187                                 resultout       out NOCOPY varchar2    ) is
5188 
5189 l_document_id           NUMBER;
5190 l_change_request_group_id number;
5191 l_orgid                 NUMBER;
5192 x_progress              varchar2(100);
5193 
5194 l_doc_string varchar2(200);
5195 l_preparer_user_name varchar2(100);
5196 
5197 BEGIN
5198 
5199     x_progress := 'PO_ReqChangeRequestWF_PVT.Set_Change_Mgr_App: 01';
5200 
5201 
5202     -- Do nothing in cancel or timeout mode
5203     if (funcmode <> wf_engine.eng_run) then
5204         resultout := wf_engine.eng_null;
5205         return;
5206     end if;
5207 
5208     l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (
5209                      itemtype => itemtype,
5210                      itemkey  => itemkey,
5211                      aname    => 'CHANGE_REQUEST_GROUP_ID');
5212 
5213     SetReqRequestStatus(l_change_request_group_id, 'N', 'MGR_APP', null, null);
5214 
5215     resultout := wf_engine.eng_completed || ':' ||  'ACTIVITY_PERFORMED';
5216 
5217     x_progress := 'PO_ReqChangeRequestWF_PVT.Set_Change_Mgr_App: 02';
5218 
5219 EXCEPTION
5220     WHEN OTHERS THEN
5221         l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
5222         l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(
5223                         itemType, itemkey);
5224         wf_core.context('PO_REQ_CHANGE_WF','Set_Change_Mgr_App',x_progress);
5225         PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey,
5226                         l_preparer_user_name, l_doc_string, sqlerrm,
5227                         'PO_REQ_CHANGE_WF.Set_Change_Mgr_App');
5228         raise;
5229 
5230 END Set_Change_Mgr_App;
5231 
5232 
5233 /*************************************************************************
5234  *
5235  * Public Procedure: Set_Change_Rejected
5236  * Effects: workflow procedure, used in POREQCHA
5237  *
5238  *          set the status of req change request to 'REJECTED'
5239  *
5240 ************************************************************************/
5241 procedure Set_Change_Rejected(itemtype        in varchar2,
5242                                 itemkey         in varchar2,
5243                                 actid           in number,
5244                                 funcmode        in varchar2,
5245                                 resultout       out NOCOPY varchar2    ) is
5246 
5247 l_document_id           NUMBER;
5248 l_change_request_group_id number;
5249 l_orgid                 NUMBER;
5250 x_progress              varchar2(100);
5251 
5252 l_doc_string varchar2(200);
5253 l_preparer_user_name varchar2(100);
5254 
5255 BEGIN
5256 
5257   x_progress := 'PO_ReqChangeRequestWF_PVT.Set_Change_Rejected: 01';
5258 
5259 
5260   -- Do nothing in cancel or timeout mode
5261   --
5262   if (funcmode <> wf_engine.eng_run) then
5263 
5264       resultout := wf_engine.eng_null;
5265       return;
5266 
5267   end if;
5268 
5269   l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
5270                                          itemkey  => itemkey,
5271                                          aname    => 'CHANGE_REQUEST_GROUP_ID');
5272 
5273 
5274   SetReqRequestStatus(l_change_request_group_id, 'N', 'REJECTED', null, null);
5275 
5276   --
5277   resultout := wf_engine.eng_completed || ':' ||  'ACTIVITY_PERFORMED';
5278   --
5279 
5280   x_progress := 'PO_ReqChangeRequestWF_PVT.Set_Change_Rejected: 02';
5281 
5282 
5283 EXCEPTION
5284   WHEN OTHERS THEN
5285     l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
5286     l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(itemType, itemkey);
5287     wf_core.context('PO_REQ_CHANGE_WF','Set_Change_Rejected',x_progress);
5288     PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey, l_preparer_user_name, l_doc_string, sqlerrm, 'PO_REQ_CHANGE_WF.Set_Change_Rejected');
5289     raise;
5290 
5291 END Set_Change_Rejected;
5292 
5293 
5294 /*************************************************************************
5295  *
5296  * Public Procedure: Is_Chg_Mgr_Pre_App
5297  * Effects: workflow procedure, used in POREQCHA
5298  *
5299  *          check if the change request is in 'MGR_PRE_APP' status
5300  *
5301 ************************************************************************/
5302 procedure Is_Chg_Mgr_Pre_App(itemtype        in varchar2,
5303                                 itemkey         in varchar2,
5304                                 actid           in number,
5305                                 funcmode        in varchar2,
5306                                 resultout       out NOCOPY varchar2    )
5307 is
5308 l_orgid       number;
5309 l_change_request_group_id number;
5310 l_change_request_id number;
5311 
5312 x_progress              varchar2(100);
5313 
5314 l_doc_string varchar2(200);
5315 l_preparer_user_name varchar2(100);
5316 
5317 cursor accepted_change is
5318 select change_request_id
5319 from po_change_requests
5320 where change_request_group_id=l_change_request_group_id
5321     and request_status='MGR_PRE_APP';
5322 
5323 BEGIN
5324 
5325   x_progress := 'PO_ReqChangeRequestWF_PVT.Is_Chg_Mgr_Pre_App';
5326 
5327 
5328   -- Do nothing in cancel or timeout mode
5329   if (funcmode <> wf_engine.eng_run) then
5330 
5331       resultout := wf_engine.eng_null;
5332       return;
5333 
5334   end if;
5335 
5336 
5337   l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
5338                                          itemkey  => itemkey,
5339                                          aname    => 'CHANGE_REQUEST_GROUP_ID');
5340 
5341   -- Set the multi-org context
5342   l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
5343                                          itemkey  => itemkey,
5344                                          aname    => 'ORG_ID');
5345 
5346   IF l_orgid is NOT NULL THEN
5347 
5348     PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
5349 
5350   END IF;
5351 
5352   open accepted_change;
5353   fetch accepted_change into l_change_request_id;
5354   close accepted_change;
5355 
5356   if(l_change_request_id is null) then
5357     -- not exist
5358     resultout := wf_engine.eng_completed || ':' || 'N' ;
5359   else
5360     resultout := wf_engine.eng_completed || ':' || 'Y' ;
5361   end if;
5362   x_progress := 'PO_ReqChangeRequestWF_PVT.Is_Chg_Mgr_Pre_App: 02';
5363 
5364 EXCEPTION
5365   WHEN OTHERS THEN
5366     l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
5367     l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(itemType, itemkey);
5368     wf_core.context('PO_ReqChangeRequestWF_PVT','Is_Chg_Mgr_Pre_App',x_progress);
5369     PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey, l_preparer_user_name, l_doc_string, sqlerrm, 'PO_ReqChangeRequestWF_PVT.Is_Chg_Mgr_Pre_App');
5370     raise;
5371 
5372 
5373 end Is_Chg_Mgr_Pre_App;
5374 
5375 
5376 
5377 /*************************************************************************
5378  *
5379  * Public Procedure: Reset_Reminder_Counter
5380  * Effects: workflow procedure, used in POREQCHA
5381  *
5382  *          reset the counter
5383  *
5384 ************************************************************************/
5385 procedure Reset_Reminder_Counter(itemtype        in varchar2,
5386                                 itemkey         in varchar2,
5387                                 actid           in number,
5388                                 funcmode        in varchar2,
5389                                 resultout       out NOCOPY varchar2    )
5390 IS
5391 x_progress varchar2(3) := '000';
5392 BEGIN
5393 
5394     PO_WF_UTIL_PKG.SetItemAttrNumber (   itemtype   => itemtype,
5395                                         itemkey    => itemkey,
5396                                         aname      => 'REMINDER_COUNTER',
5397                                         avalue     => 0);
5398     resultout := wf_engine.eng_completed || ':' ||  'ACTIVITY_PERFORMED';
5399 
5400 exception when others then
5401     wf_core.context('PO_ReqChangeRequestWF_PVT','Reset_Reminder_Counter',x_progress);
5402     raise;
5403 END Reset_Reminder_Counter;
5404 
5405 
5406 /*************************************************************************
5407  *
5408  * Public Procedure: Reminder_Need_To_Be_Sent
5409  * Effects: workflow procedure, used in POREQCHA
5410  *
5411  *          check if the reminder message need to be sent
5412  *
5413 ************************************************************************/
5414 procedure Reminder_Need_To_Be_Sent(itemtype        in varchar2,
5415                                 itemkey         in varchar2,
5416                                 actid           in number,
5417                                 funcmode        in varchar2,
5418                                 resultout       out NOCOPY varchar2    )
5419 IS
5420 l_reminder_counter number;
5421 x_progress varchar2(3) := '000';
5422 l_max_reminder number;
5423 BEGIN
5424 
5425     l_reminder_counter:= PO_WF_UTIL_PKG.GetItemAttrNumber(itemtype => itemtype,
5426                                             itemkey => itemkey,
5427                                             aname => 'REMINDER_COUNTER');
5428     l_max_reminder := PO_WF_UTIL_PKG.GetItemAttrNumber(itemtype => itemtype,
5429                                             itemkey => itemkey,
5430                                             aname => 'MAX_REMINDER_MSG_COUNT');
5431 
5432     if(l_reminder_counter=l_max_reminder) then
5433         resultout := wf_engine.eng_completed || ':' ||  'N';
5434     else
5435         resultout := wf_engine.eng_completed || ':' ||  'Y';
5436         wf_engine.SetItemAttrNumber (   itemtype   => itemtype,
5437                                         itemkey    => itemkey,
5438                                         aname      => 'REMINDER_COUNTER',
5439                                         avalue     => l_reminder_counter+1);
5440         if(l_reminder_counter=l_max_reminder-1) then
5441             PO_WF_UTIL_PKG.SetItemAttrText (   itemtype   => itemtype,
5442                                         itemkey    => itemkey,
5443                                         aname      => 'REMINDER_MSG',
5444                                         avalue     => fnd_message.get_string('PO', 'PO_WF_NOTIF_LAST_REMINDER')||':');
5445         elsif(l_reminder_counter=1) then
5446             PO_WF_UTIL_PKG.SetItemAttrText (   itemtype   => itemtype,
5447                                         itemkey    => itemkey,
5448                                         aname      => 'REMINDER_MSG',
5449                                         avalue     => fnd_message.get_string('PO', 'PO_WF_NOTIF_FIRST_REMINDER')||':');
5450         elsif(l_reminder_counter=2) then
5451             PO_WF_UTIL_PKG.SetItemAttrText (   itemtype   => itemtype,
5452                                         itemkey    => itemkey,
5453                                         aname      => 'REMINDER_MSG',
5454                                         avalue     => fnd_message.get_string('PO', 'PO_WF_NOTIF_SECOND_REMINDER')||':');
5455         else
5456             PO_WF_UTIL_PKG.SetItemAttrText (   itemtype   => itemtype,
5457                                         itemkey    => itemkey,
5458                                         aname      => 'REMINDER_MSG',
5459                                         avalue     => fnd_message.get_string('PO', 'PO_WF_NOTIF_REMINDER')||' '||to_char(l_reminder_counter)||':');
5460         end if;
5461     end if;
5462 
5463 exception when others then
5464     wf_core.context('PO_ReqChangeRequestWF_PVT','Reminder_Need_To_Be_Sent',x_progress);
5465     raise;
5466 END Reminder_Need_To_Be_Sent;
5467 
5468 
5469 
5470 
5471 
5472 
5473 
5474 
5475 /*************************************************************************
5476  *
5477  * Public Procedure: Start_From_PO_Cancel
5478  * Effects: workflow procedure, used in POREQCHA
5479  *
5480  *          check if the workflow is start because of PO cancel API is
5481  *          called
5482  *
5483 ************************************************************************/
5484 procedure Start_From_PO_Cancel(itemtype        in varchar2,
5485                                 itemkey         in varchar2,
5486                                 actid           in number,
5487                                 funcmode        in varchar2,
5488                                 resultout       out NOCOPY varchar2    )is
5489 
5490 l_ActionOriginatedFrom varchar2(30);
5491 x_progress              varchar2(100);
5492 
5493 l_doc_string varchar2(200);
5494 l_preparer_user_name varchar2(100);
5495 
5496 BEGIN
5497 
5498   x_progress := 'PO_ReqChangeRequestWF_PVT.Start_From_PO_Cancel';
5499 
5500   -- Do nothing in cancel or timeout mode
5501   if (funcmode <> wf_engine.eng_run) then
5502 
5503       resultout := wf_engine.eng_null;
5504       return;
5505 
5506   end if;
5507   l_ActionOriginatedFrom := PO_WF_UTIL_PKG.GetItemAttrText (     itemtype   => itemtype,
5508                                         itemkey    => itemkey,
5509                                         aname      => 'INTERFACE_SOURCE_CODE');
5510 
5511 
5512   if(nvl(l_ActionOriginatedFrom, 'N')='PO_CANCEL') then
5513     resultout := wf_engine.eng_completed || ':' ||  'Y';
5514   else
5515     resultout := wf_engine.eng_completed || ':' ||  'N';
5516   end if;
5517   x_progress := 'PO_ReqChangeRequestWF_PVT.Start_From_PO_Cancel: 02';
5518 
5519 EXCEPTION
5520   WHEN OTHERS THEN
5521     l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
5522     l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(itemType, itemkey);
5523     wf_core.context('PO_ReqChangeRequestWF_PVT','Start_From_PO_Cancel',x_progress);
5524     PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey, l_preparer_user_name, l_doc_string, sqlerrm, 'PO_ReqChangeRequestWF_PVT.Start_From_PO_Cancel');
5525     raise;
5526 
5527 
5528 end Start_From_PO_Cancel;
5529 
5530 
5531 /*************************************************************************
5532  *
5533  * Public Procedure: Change_Request_Mgr_Approved
5534  * Effects: workflow procedure, used in POREQCHA
5535  *
5536  *          check if there is req change request in 'MGR_APP' status
5537  *
5538 ************************************************************************/
5539 procedure Change_Request_Mgr_Approved(itemtype        in varchar2,
5540                                 itemkey         in varchar2,
5541                                 actid           in number,
5542                                 funcmode        in varchar2,
5543                                 resultout       out NOCOPY varchar2    )is
5544 
5545 l_orgid       number;
5546 l_change_request_group_id number;
5547 l_change_request_status varchar2(20);
5548 l_doc_id number;
5549 
5550 x_progress              varchar2(100);
5551 
5552 l_doc_string varchar2(200);
5553 l_preparer_user_name varchar2(100);
5554 
5555 cursor change_request_status is
5556     select request_status
5557     from po_change_requests
5558     where change_request_group_id=l_change_request_group_id
5559             and request_status='MGR_APP';
5560 BEGIN
5561 
5562   x_progress := 'PO_ReqChangeRequestWF_PVT.Change_Request_Mgr_Approved';
5563 
5564 
5565   -- Do nothing in cancel or timeout mode
5566   if (funcmode <> wf_engine.eng_run) then
5567 
5568       resultout := wf_engine.eng_null;
5569       return;
5570 
5571   end if;
5572 
5573   l_change_request_group_id:= PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
5574                                          itemkey  => itemkey,
5575                                          aname    => 'CHANGE_REQUEST_GROUP_ID');
5576 
5577   -- Set the multi-org context
5578   l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
5579                                          itemkey  => itemkey,
5580                                          aname    => 'ORG_ID');
5581 
5582   IF l_orgid is NOT NULL THEN
5583 
5584     PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
5585 
5586   END IF;
5587 
5588   open change_request_status ;
5589   fetch change_request_status into l_change_request_status ;
5590   close change_request_status ;
5591 
5592   if(l_change_request_status is not null) then
5593     resultout := wf_engine.eng_completed || ':' ||  'Y';
5594   else
5595     resultout := wf_engine.eng_completed || ':' ||  'N';
5596   end if;
5597   x_progress := 'PO_ReqChangeRequestWF_PVT.Change_Request_Mgr_Approved: 02';
5598 
5599 EXCEPTION
5600   WHEN OTHERS THEN
5601     l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
5602     l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(itemType, itemkey);
5603     wf_core.context('PO_ReqChangeRequestWF_PVT','Change_Request_Mgr_Approved',x_progress);
5604     PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey, l_preparer_user_name, l_doc_string, sqlerrm, 'PO_ReqChangeRequestWF_PVT.Change_Request_Mgr_Approved');
5605     raise;
5606 
5607 
5608 end Change_Request_Mgr_Approved;
5609 
5610 procedure Update_Action_History(p_object_id in number,
5611                    p_employee_id in number,
5612                    p_action_code in varchar2) is
5613 pragma AUTONOMOUS_TRANSACTION;
5614 begin
5615     po_forward_sv1.update_action_history (
5616         p_object_id,
5617         'REQUISITION',
5618         p_employee_id,
5619         p_action_code,
5620         null,
5621         fnd_global.user_id,
5622         fnd_global.login_id
5623     );
5624     commit;
5625 end Update_Action_History;
5626 
5627 
5628 procedure Insert_Action_History(l_document_id      in NUMBER,
5629                                 l_document_type    in VARCHAR2,
5630                                 l_document_subtype in VARCHAR2,
5631                                 l_sequence_num     in NUMBER,
5632                                 l_employee_id      in NUMBER,
5633                                 l_new_action_code  in VARCHAR2,
5634                                 l_object_rev_num   in NUMBER,
5635                                 l_approval_path_id in NUMBER) is
5636 
5637 pragma AUTONOMOUS_TRANSACTION;
5638 
5639 begin
5640           INSERT into PO_ACTION_HISTORY
5641              (object_id,
5642               object_type_code,
5643               object_sub_type_code,
5644               sequence_num,
5645               last_update_date,
5646               last_updated_by,
5647               creation_date,
5648               created_by,
5649               action_code,
5650               action_date,
5651               employee_id,
5652               note,
5653               object_revision_num,
5654               last_update_login,
5655               request_id,
5656               program_application_id,
5657               program_id,
5658               program_update_date,
5659               approval_path_id,
5660               offline_code)
5661              VALUES
5662              (l_document_id,
5663               l_document_type,
5664               l_document_subtype,
5665               l_sequence_num + 1,
5666               sysdate,
5667               l_employee_id,
5668               sysdate,
5669               l_employee_id,
5670               l_new_action_code,
5671               sysdate,
5672               l_employee_id,
5673               NULL,
5674               l_object_rev_num,
5675               l_employee_id,
5676               0,
5677               0,
5678               0,
5679               '',
5680               l_approval_path_id,
5681               '' );
5682 
5683     COMMIT;
5684 
5685 end Insert_Action_History;
5686 
5687 
5688 /*************************************************************************
5689  *
5690  * Public Procedure: Update_Action_History_App_Rej
5691  * Effects: workflow procedure, used in POREQCHA
5692  *
5693  *          if a change request is responded because of PO Cancel
5694  *          This procedure will insert into the action history table
5695  *          a record with action 'RETURN'
5696  *
5697 ************************************************************************/
5698 procedure Update_Action_History_App_Rej(itemtype        in varchar2,
5699                                 itemkey         in varchar2,
5700                                 actid           in number,
5701                                 funcmode        in varchar2,
5702                                 resultout       out NOCOPY varchar2) IS
5703 
5704   l_progress                  VARCHAR2(100) := '000';
5705   l_action                    VARCHAR2(30)  := null;
5706   l_forward_to_id             NUMBER:='';
5707   l_document_id               NUMBER:='';
5708   l_document_type             VARCHAR2(25):='';
5709   l_document_subtype          VARCHAR2(25):='';
5710   l_return_code               NUMBER;
5711   l_result                    BOOLEAN:=FALSE;
5712   l_note                      VARCHAR2(2000);
5713   l_object_rev_num            NUMBER;
5714   l_approval_path_id          NUMBER;
5715 
5716   l_doc_string varchar2(200);
5717   l_preparer_user_name varchar2(100);
5718 
5719   l_org_id     number;
5720 
5721   l_change_request_group_id number;
5722   l_change_request_id number;
5723 
5724   l_action_code po_action_history.action_code%type;
5725   l_new_action_code po_action_history.action_code%type;
5726   l_employee_id number;
5727   l_sequence_num number;
5728 
5729   cursor l_approved_change_request_csr(grp_id number) is
5730     select change_request_id
5731       from po_change_requests
5732      where change_request_group_id=grp_id
5733            and request_status='MGR_APP'
5734            and action_type='MODIFICATION';
5735   cursor l_rejected_change_request_csr(grp_id number) is
5736     select change_request_id
5737       from po_change_requests
5738      where change_request_group_id=grp_id
5739            and request_status='REJECTED'
5740            and action_type='MODIFICATION';
5741   cursor l_approved_cancel_request_csr(grp_id number) is
5742     select change_request_id
5743       from po_change_requests
5744      where change_request_group_id=grp_id
5745            and request_status='MGR_APP'
5746            and action_type='CANCELLATION';
5747 
5748 BEGIN
5749     l_progress := 'Update_Action_History_App_Rej: 001';
5750 
5751     IF (g_po_wf_debug = 'Y') THEN
5752       PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey, 'Entering Update_Action_History_App_Rej...' );
5753     END IF;
5754 
5755     IF (funcmode='RUN') THEN
5756 
5757      l_document_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
5758                                          itemkey  => itemkey,
5759                                          aname    => 'DOCUMENT_ID');
5760 
5761      l_document_type := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
5762                                          itemkey  => itemkey,
5763                                          aname    => 'DOCUMENT_TYPE');
5764 
5765      l_document_subtype := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
5766                                          itemkey  => itemkey,
5767                                          aname    => 'DOCUMENT_SUBTYPE');
5768 
5769      l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
5770                                          itemkey  => itemkey,
5771                                          aname    => 'CHANGE_REQUEST_GROUP_ID');
5772 
5773      -- Set the multi-org context
5774      l_org_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
5775                                              itemkey  => itemkey,
5776                                              aname    => 'ORG_ID');
5777 
5778      IF l_org_id is NOT NULL THEN
5779        PO_MOAC_UTILS_PVT.set_org_context(l_org_id) ;       -- <R12 MOAC>
5780      END IF;
5781 
5782      SELECT max(sequence_num)
5783        INTO l_sequence_num
5784        FROM PO_ACTION_HISTORY
5785       WHERE object_type_code = 'REQUISITION'
5786         AND object_id = l_document_id;
5787 
5788     select action_code, employee_id, object_revision_num, approval_path_id
5789       into l_action_code, l_employee_id, l_object_rev_num, l_approval_path_id
5790       from po_action_history
5791      where object_id=l_document_id
5792            and object_type_code='REQUISITION'
5793            and sequence_num=l_sequence_num;
5794 
5795     IF (g_po_wf_debug = 'Y') THEN
5796       PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey, '  l_action_code = ' || l_action_code );
5797       PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey, '  l_sequence_num = ' || l_sequence_num );
5798     END IF;
5799 
5800     if (l_action_code is null OR l_action_code = 'SUBMIT CHANGE') then
5801         open l_approved_change_request_csr(l_change_request_group_id);
5802         fetch l_approved_change_request_csr into l_change_request_id;
5803 
5804         if(l_approved_change_request_csr%FOUND) then
5805             l_new_action_code:='APPROVE';
5806         else
5807             open l_rejected_change_request_csr(l_change_request_group_id);
5808             fetch l_rejected_change_request_csr into l_change_request_id;
5809 
5810             if(l_rejected_change_request_csr%FOUND) then
5811                 l_new_action_code:='REJECT';
5812             else
5813                 open l_approved_cancel_request_csr(l_change_request_group_id);
5814                 fetch l_approved_cancel_request_csr into l_change_request_id;
5815                 if(l_approved_cancel_request_csr%FOUND) then
5816                     l_new_action_code:='APPROVE';
5817                 else
5818                     l_new_action_code:='REJECT';
5819                 end if;
5820                 close l_approved_cancel_request_csr;
5821             end if;
5822 
5823             close l_rejected_change_request_csr;
5824         end if;
5825 
5826         close l_approved_change_request_csr;
5827 
5828         IF (g_po_wf_debug = 'Y') THEN
5829           PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey, '  l_new_action_code = ' || l_new_action_code );
5830         END IF;
5831 
5832         if (l_action_code = 'SUBMIT CHANGE') then
5833           Insert_Action_History(l_document_id,
5834                                 l_document_type,
5835                                 l_document_subtype,
5836                                 l_sequence_num,
5837                                 l_employee_id,
5838                                 l_new_action_code,
5839                                 l_object_rev_num,
5840                                 l_approval_path_id);
5841 
5842         else
5843           Update_Action_History(l_document_id,
5844                                 l_employee_id,
5845                                 l_new_action_code);
5846 
5847         end if;
5848 
5849     end if;  -- if (l_action_code is null OR l_action_code = 'SUBMIT CHANGE')
5850 
5851     l_progress := 'Update_Action_History_App_Rej: 006';
5852 
5853     IF (g_po_wf_debug = 'Y') THEN
5854        PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey, 'Leaving Update_Action_History_App_Rej...');
5855     END IF;
5856 
5857     resultout:='COMPLETE' || ':' ||  'ACTIVITY_PERFORMED';
5858     return;
5859 
5860     END IF; -- run mode
5861     l_progress := 'Update_Action_History_App_Rej: 999';
5862 
5863 EXCEPTION
5864  WHEN OTHERS THEN
5865     l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
5866     l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(itemType, itemkey);
5867     wf_core.context('PO_ReqChangeRequestWF_PVT','Update_Action_History_App_Rej',l_progress,sqlerrm);
5868     PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey, l_preparer_user_name, l_doc_string, sqlerrm, 'PO_ReqChangeRequestWF_PVT.Update_Action_History_App_Rej');
5869     RAISE;
5870 
5871 END Update_Action_History_App_Rej;
5872 
5873 
5874 /*************************************************************************
5875  *
5876  * Public Procedure: Update_Action_History_Return
5877  * Effects: workflow procedure, used in POREQCHA
5878  *
5879  *          if a change request is responded because of PO Cancel
5880  *          This procedure will insert into the action history table
5881  *          a record with action 'RETURN'
5882  *
5883 ************************************************************************/
5884 procedure Update_Action_History_Return(itemtype        in varchar2,
5885                                 itemkey         in varchar2,
5886                                 actid           in number,
5887                                 funcmode        in varchar2,
5888                                 resultout       out NOCOPY varchar2) IS
5889 
5890   l_progress                  VARCHAR2(100) := '000';
5891   l_action                    VARCHAR2(30)  := 'RETURN';
5892   l_forward_to_id             NUMBER:='';
5893   l_document_id               NUMBER:='';
5894   l_document_type             VARCHAR2(25):='';
5895   l_document_subtype          VARCHAR2(25):='';
5896   l_return_code               NUMBER;
5897   l_result                    BOOLEAN:=FALSE;
5898   l_note                      VARCHAR2(2000);
5899 
5900   l_doc_string varchar2(200);
5901   l_preparer_user_name varchar2(100);
5902 
5903   l_org_id     number;
5904 
5905 BEGIN
5906     l_progress := 'Update_Action_History_Return: 001';
5907 
5908     IF (funcmode='RUN') THEN
5909 
5910      l_document_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
5911                                          itemkey  => itemkey,
5912                                          aname    => 'DOCUMENT_ID');
5913 
5914      l_document_type := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
5915                                          itemkey  => itemkey,
5916                                          aname    => 'DOCUMENT_TYPE');
5917 
5918      l_document_subtype := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
5919                                          itemkey  => itemkey,
5920                                          aname    => 'DOCUMENT_SUBTYPE');
5921 
5922      l_note := PO_WF_UTIL_PKG.GetItemAttrText(itemtype => itemtype,
5923                                          itemkey  => itemkey,
5924                                          aname    => 'NOTE');
5925 
5926      -- Set the multi-org context
5927 
5928      l_org_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
5929                                              itemkey  => itemkey,
5930                                              aname    => 'ORG_ID');
5931 
5932      IF l_org_id is NOT NULL THEN
5933 
5934        PO_MOAC_UTILS_PVT.set_org_context(l_org_id) ;       -- <R12 MOAC>
5935 
5936      END IF;
5937 
5938      l_progress := 'Update_Action_History_Return: 002-'||
5939                            to_char(l_document_id)||'-'||
5940                            l_document_type||'-'||l_document_subtype;
5941 
5942      /* update po action history */
5943      PO_APPROVAL_LIST_HISTORY_SV.Update_Action_History(itemtype=>itemtype,
5944                                          itemkey=>itemkey,
5945                                          x_action=>l_action,
5946                                          x_req_header_id=>l_document_id,
5947                                          x_last_approver=>l_result,
5948                                          x_note=>l_note);
5949 
5950 
5951      l_progress := 'Update_Action_History_Return: 006';
5952 
5953      resultout:='COMPLETE' || ':' ||  'ACTIVITY_PERFORMED';
5954      return;
5955 
5956     END IF; -- run mode
5957     l_progress := 'Update_Action_History_Return: 999';
5958 
5959 EXCEPTION
5960  WHEN OTHERS THEN
5961     l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
5962     l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(itemType, itemkey);
5963     wf_core.context('PO_ReqChangeRequestWF_PVT','Update_Action_History_Return',l_progress,sqlerrm);
5964     PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey, l_preparer_user_name, l_doc_string, sqlerrm, 'PO_ReqChangeRequestWF_PVT.Update_Action_History_Return');
5965     RAISE;
5966 
5967 END Update_Action_History_Return;
5968 
5969 
5970 /*************************************************************************
5971  *
5972  * Public Procedure: Convert_Into_PO_Change
5973  * Effects: workflow procedure, used in POREQCHA
5974  *
5975  *          convert the manager approved requester change request into
5976  *          PO change request.
5977  *
5978 ************************************************************************/
5979 procedure Convert_Into_PO_Change(itemtype        in varchar2,
5980                                 itemkey         in varchar2,
5981                                 actid           in number,
5982                                 funcmode        in varchar2,
5983                                 resultout       out NOCOPY varchar2    ) is
5984 
5985 l_change_request_group_id number;
5986 temp number;
5987 l_error_code varchar2(200);
5988 
5989 begin
5990     l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
5991                                          itemkey  => itemkey,
5992                                          aname    => 'CHANGE_REQUEST_GROUP_ID');
5993 
5994     ConvertIntoPOChange(l_change_request_group_id);
5995 
5996 
5997   resultout := wf_engine.eng_completed || ':' ||  'ACTIVITY_PERFORMED';
5998 end Convert_Into_PO_Change;
5999 
6000 
6001 
6002 
6003 /*************************************************************************
6004  *
6005  * Public Procedure: Kickoff_POChange_WF
6006  * Effects: workflow procedure, used in POREQCHA
6007  *
6008  *          For each PO that has new change request, kick off a PORPOCHA
6009  *          workflow to process the PO change request
6010  *
6011  * NOTE :
6012  *    This procedure will not be used anymore for release 12.0
6013  *    but cannot be obsoleted because of upgrade issues. If there are
6014  *    any existing open RCO notifications, when they are responded,
6015  *    they will still be calling the previous version of wf process.
6016  *    We created a new procedure (Start_POChange_WF) to handle the
6017  *    new functionality.
6018  ************************************************************************/
6019 procedure Kickoff_POChange_WF(itemtype        in varchar2,
6020                                 itemkey         in varchar2,
6021                                 actid           in number,
6022                                 funcmode        in varchar2,
6023                                 resultout       out NOCOPY varchar2    )
6024 is
6025 
6026 l_document_id           NUMBER;
6027 l_change_request_group_id number;
6028 l_child_request_group_id number;
6029 l_orgid                 NUMBER;
6030 x_progress              varchar2(100);
6031 
6032 l_doc_string varchar2(200);
6033 l_preparer_user_name varchar2(100);
6034 
6035 cursor child_request_group_id is
6036     select distinct pcr1.change_request_group_id
6037     from po_change_requests pcr1, po_change_requests pcr2
6038     where pcr1.parent_change_request_id=pcr2.change_request_id
6039         and pcr2.change_request_group_id=l_change_request_group_id
6040         and pcr1.request_status='PENDING';
6041 
6042 
6043 BEGIN
6044 
6045   x_progress := 'PO_ReqChangeRequestWF_PVT.Kickoff_POChange_WF: 01';
6046 
6047 
6048   -- Do nothing in cancel or timeout mode
6049   --
6050   if (funcmode <> wf_engine.eng_run) then
6051 
6052       resultout := wf_engine.eng_null;
6053       return;
6054 
6055   end if;
6056 
6057   -- Set the multi-org context
6058   l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
6059                                          itemkey  => itemkey,
6060                                          aname    => 'ORG_ID');
6061 
6062   IF l_orgid is NOT NULL THEN
6063 
6064       PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
6065 
6066   END IF;
6067 
6068   l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
6069                                          itemkey  => itemkey,
6070                                          aname    => 'CHANGE_REQUEST_GROUP_ID');
6071 
6072   open child_request_group_id;
6073   loop
6074     fetch child_request_group_id into l_child_request_group_id;
6075     exit when child_request_group_id%NOTFOUND;
6076     x_progress:='Kickoff_POChange_WF'||l_child_request_group_id;
6077     StartInformBuyerWF(l_child_request_group_id);
6078     x_progress:='Kickoff_POChange_WF'||l_child_request_group_id||'--'||'1';
6079   end loop;
6080   close child_request_group_id;
6081 
6082   --
6083   resultout := wf_engine.eng_completed || ':' ||  'ACTIVITY_PERFORMED';
6084   --
6085 
6086   x_progress := 'PO_ReqChangeRequestWF_PVT.Kickoff_POChange_WF: 02';
6087 
6088 
6089 EXCEPTION
6090   WHEN OTHERS THEN
6091     l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
6092     l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(itemType, itemkey);
6093     wf_core.context('PO_ReqChangeRequestWF_PVT','Kickoff_POChange_WF',x_progress);
6094     PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey, l_preparer_user_name, l_doc_string, sqlerrm, 'PO_ReqChangeRequestWF_PVT.Kickoff_POChange_WF');
6095     raise;
6096 
6097 END Kickoff_POChange_WF;
6098 
6099 /*************************************************************************
6100  *
6101  * Public Procedure: Reset_Change_Flag
6102  * Effects: workflow procedure, used in POREQCHA
6103  *
6104  *          reset the change_pending_flag in po_requisition_headers_all
6105  *          table to 'N'
6106  *
6107 ************************************************************************/
6108 procedure Reset_Change_Flag(itemtype        in varchar2,
6109                                 itemkey         in varchar2,
6110                                 actid           in number,
6111                                 funcmode        in varchar2,
6112                                 resultout       out NOCOPY varchar2    ) is
6113 
6114 l_document_id           NUMBER;
6115 l_change_request_group_id number;
6116 l_orgid                 NUMBER;
6117 x_progress              varchar2(100);
6118 
6119 l_doc_string varchar2(200);
6120 l_preparer_user_name varchar2(100);
6121 
6122 
6123 BEGIN
6124 
6125   x_progress := 'PO_ReqChangeRequestWF_PVT.Reset_Change_Flag: 01';
6126 
6127 
6128   -- Do nothing in cancel or timeout mode
6129   --
6130   if (funcmode <> wf_engine.eng_run) then
6131 
6132       resultout := wf_engine.eng_null;
6133       return;
6134 
6135   end if;
6136 
6137   -- Set the multi-org context
6138   l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
6139                                          itemkey  => itemkey,
6140                                          aname    => 'ORG_ID');
6141 
6142   IF l_orgid is NOT NULL THEN
6143 
6144       PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
6145 
6146   END IF;
6147 
6148   l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
6149                                          itemkey  => itemkey,
6150                                          aname    => 'CHANGE_REQUEST_GROUP_ID');
6151 
6152   l_document_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
6153                                          itemkey  => itemkey,
6154                                          aname    => 'DOCUMENT_ID');
6155 
6156   SetReqChangeFlag(l_change_request_group_id, l_document_id, itemtype, itemkey, 'N');
6157 
6158   --
6159   resultout := wf_engine.eng_completed || ':' ||  'ACTIVITY_PERFORMED';
6160   --
6161 
6162   x_progress := 'PO_REQAPPROVAL_INIT1.Reset_Change_Flag: 02';
6163 
6164 
6165 EXCEPTION
6166   WHEN OTHERS THEN
6167     l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
6168     l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(itemType, itemkey);
6169     wf_core.context('PO_ReqChangeRequestWF_PVT','Reset_Change_Flag',x_progress);
6170     PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey, l_preparer_user_name, l_doc_string, sqlerrm, 'PO_ReqChangeRequestWF_PVT.Reset_Change_Flag');
6171     raise;
6172 
6173 END Reset_Change_Flag;
6174 
6175 
6176 
6177 /*************************************************************************
6178  *
6179  * Public Procedure: Get_Change_Total_Attr
6180  * Effects: workflow procedure, used in POREQCHA workflow
6181  *
6182  *          Set 2 attributes: NEW_REQ_AMOUNT_CURRENCY_DSP and
6183  *          NEW_TAX_AMOUNT_CURRENCY_DSP
6184  *
6185 ************************************************************************/
6186 procedure Get_Change_Total_Attr(     itemtype        in varchar2,
6187                                     itemkey         in varchar2,
6188                                     actid           in number,
6189                                     funcmode        in varchar2,
6190                                     resultout       out NOCOPY varchar2)
6191 IS
6192 l_orgid number;
6193 l_document_id           NUMBER;
6194 x_progress varchar2(3) := '000';
6195 l_change_request_group_id number;
6196 l_new_tax_amount number;
6197 l_new_req_amount number;
6198 l_currency_code    fnd_currencies.CURRENCY_CODE%TYPE;
6199 
6200 BEGIN
6201   if (funcmode <> wf_engine.eng_run) then
6202 
6203       resultout := wf_engine.eng_null;
6204       return;
6205 
6206   end if;
6207 
6208   -- Set the multi-org context
6209   l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
6210                                          itemkey  => itemkey,
6211                                          aname    => 'ORG_ID');
6212 
6213   IF l_orgid is NOT NULL THEN
6214 
6215       PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
6216 
6217   END IF;
6218 
6219   setNewTotal(itemtype, itemkey);
6220 
6221   resultout := wf_engine.eng_completed || ':' || 'ACTIVITY_PERFORMED' ;
6222 
6223 exception when others then
6224     wf_core.context('PO_ReqChangeRequestWF_PVT','Get_Change_Total_Attr',x_progress);
6225     raise;
6226 END Get_Change_Total_Attr;
6227 
6228 procedure setNewTotal(itemtype in varchar2, itemkey in varchar2) is
6229 
6230 l_change_request_group_id number;
6231 l_document_id number;
6232 l_currency_code varchar2(40);
6233 l_new_tax_amount number;
6234 l_new_req_amount number;
6235 x_progress varchar2(100):='000';
6236 l_amount_for_subject varchar2(400);
6237 l_amount_for_header varchar2(400);
6238 l_amount_for_tax varchar2(400);
6239 l_req_amount_disp   varchar2(60);
6240 l_tax_amount_disp   varchar2(60);
6241 l_total_amount_disp varchar2(60);
6242 
6243 begin
6244   l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber
6245                                   (itemtype   => itemtype,
6246                                   itemkey    => itemkey,
6247                                   aname      => 'CHANGE_REQUEST_GROUP_ID');
6248   l_document_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
6249                                          itemkey  => itemkey,
6250                                          aname    => 'DOCUMENT_ID');
6251 
6252   l_currency_code := PO_WF_UTIL_PKG.GetItemAttrText
6253                                   (itemtype   => itemtype,
6254                                   itemkey    => itemkey,
6255                                   aname      => 'FUNCTIONAL_CURRENCY');
6256 
6257   x_progress :='001';
6258   select  nvl(sum(nvl(decode(pcr4.action_type, 'CANCELLATION', 0,
6259             decode(prl.unit_price, 0, 0, decode(prl.matching_basis, 'AMOUNT',nvl(pcr3.new_amount, prl.amount) * por_view_reqs_pkg.get_line_nonrec_tax_total(
6260                         prl.requisition_line_id)/prl.amount,
6261             nvl(pcr1.new_price, prl.unit_price)*
6262             nvl(pcr2.new_quantity, prl.quantity)*
6263             por_view_reqs_pkg.get_line_nonrec_tax_total(
6264                         prl.requisition_line_id)/
6265             (prl.unit_price*prl.quantity)))),0)),0),
6266           nvl(sum(decode(pcr4.action_type, 'CANCELLATION', 0, decode(prl.matching_basis, 'AMOUNT', nvl(pcr3.new_amount, prl.amount),
6267             nvl(pcr1.new_price, prl.unit_price)*
6268             nvl(pcr2.new_quantity, prl.quantity)))), 0)
6269   into l_new_tax_amount, l_new_req_amount
6270   from po_requisition_lines_all prl,
6271         po_change_requests pcr1,
6272         po_change_requests pcr2,
6273         po_change_requests pcr3,
6274         po_change_requests pcr4
6275   where prl.requisition_line_id=pcr1.document_line_id(+)
6276         and pcr1.change_request_group_id(+)=l_change_request_group_id
6277         and pcr1.request_level(+)='LINE'
6278         and pcr1.new_price(+) is not null
6279         and prl.requisition_line_id=pcr2.document_line_id(+)
6280         and pcr2.change_request_group_id(+)=l_change_request_group_id
6281         and pcr2.request_level(+)='LINE'
6282         and pcr2.action_type(+)='DERIVED'
6283         and pcr2.new_quantity(+) is not null
6284         and prl.requisition_line_id=pcr3.document_line_id(+)
6285         and pcr3.change_request_group_id(+)=l_change_request_group_id
6286         and pcr3.request_level(+)='LINE'
6287         and pcr3.action_type(+)='DERIVED'
6288         and pcr3.new_amount(+) is not null
6289         and prl.requisition_line_id=pcr4.document_line_id(+)
6290         and pcr4.change_request_group_id(+)=l_change_request_group_id
6291         and pcr4.request_level(+)='LINE'
6292         and pcr4.action_type(+)='CANCELLATION'
6293         and prl.requisition_header_id=l_document_id
6294     AND NVL(prl.modified_by_agent_flag, 'N') = 'N'
6295     and NVL(prl.cancel_flag, 'N')='N';
6296   x_progress :='002';
6297 /*
6298   select sum(decode(pcr1.action_type, 'CANCELLATION', 0,
6299                      decode(prl.unit_price, 0, 0,
6300                      nvl(pcr1.new_price, prl.unit_price)*
6301                      nvl(pcr2.new_quantity,prd.req_line_quantity)*
6302                      prd.nonrecoverable_tax /
6303                      (prl.unit_price*prd.req_line_quantity)))),
6304          nvl(sum(decode(pcr1.action_type, 'CANCELLATION', 0,
6305                      nvl(pcr1.new_price, prl.unit_price)*
6306                      nvl(pcr2.new_quantity,prd.req_line_quantity))), 0)
6307   into l_new_tax_amount, l_new_req_amount
6308   from po_requisition_lines_all prl,
6309         po_req_distributions_all prd,
6310         po_change_requests pcr1,
6311         po_change_requests pcr2
6312   where prl.requisition_line_id=pcr1.document_line_id(+)
6313         and pcr1.change_request_group_id(+)=l_change_request_group_id
6314         and pcr1.request_level(+)='LINE'
6315         and pcr1.change_active_flag(+)='Y'
6316         and prl.requisition_line_id=prd.requisition_line_id
6317         and prd.distribution_id=pcr2.document_distribution_id(+)
6318         and pcr2.change_request_group_id(+)=l_change_request_group_id
6319         and pcr2.change_active_flag(+)='Y'
6320         and prl.requisition_header_id=l_document_id
6321     AND NVL(prl.modified_by_agent_flag, 'N') = 'N'
6322     and NVL(prl.cancel_flag, 'N')='N';
6323 */
6324 
6325   x_progress :='003';
6326 
6327 
6328   /* FPJ
6329      support approval currency in notification header and subject
6330      because TOTAL_AMOUNT_DSP is only used in notification,
6331      this bug fix changes the meaning of this attribute from total to
6332      total with currency;
6333      the workflow definition is modified such that
6334      currency atribute is removed from the subject.
6335    */
6336   l_total_amount_disp := to_char(l_new_tax_amount+l_new_req_amount,
6337                     FND_CURRENCY.GET_FORMAT_MASK(l_currency_code,g_currency_format_mask));
6338   l_req_amount_disp := to_char(l_new_req_amount,
6339                    FND_CURRENCY.GET_FORMAT_MASK(l_currency_code,g_currency_format_mask));
6340   l_tax_amount_disp := to_char(l_new_tax_amount,
6341                     FND_CURRENCY.GET_FORMAT_MASK(l_currency_code,g_currency_format_mask));
6342 
6343   getReqAmountInfo(itemtype => itemtype,
6344                           itemkey => itemkey,
6345                           p_function_currency => l_currency_code,
6346                           p_total_amount_disp => l_total_amount_disp,
6347                           p_total_amount => l_new_tax_amount+l_new_req_amount,
6348                           p_req_amount_disp => l_req_amount_disp,
6349                           p_req_amount => l_new_req_amount,
6350                           p_tax_amount_disp => l_tax_amount_disp,
6351                           p_tax_amount => l_new_tax_amount,
6352                           x_amount_for_subject => l_amount_for_subject,
6353                           x_amount_for_header => l_amount_for_header,
6354                           x_amount_for_tax => l_amount_for_tax);
6355 
6356 
6357   PO_WF_UTIL_PKG.SetItemAttrText (     itemtype    => itemtype,
6358          itemkey=> itemkey,
6359          aname  => 'NEW_REQ_AMOUNT_CURRENCY_DSP',
6360          avalue => l_amount_for_header);
6361 
6362   PO_WF_UTIL_PKG.SetItemAttrText (     itemtype    => itemtype,
6363          itemkey=> itemkey,
6364          aname  => 'NEW_TAX_AMOUNT_CURRENCY_DSP',
6365          avalue => l_amount_for_tax);
6366   PO_WF_UTIL_PKG.SetItemAttrText (     itemtype    => itemtype,
6367          itemkey=> itemkey,
6368          aname  => 'NEW_TOTAL_AMOUNT_DSP',
6369          avalue => l_amount_for_subject);
6370   x_progress :='004';
6371 exception
6372   when others then
6373     wf_core.context('PO_ReqChangeRequestWF_PVT','setNewTotal',x_progress||sqlerrm);
6374     raise;
6375 
6376 end setNewTotal;
6377 
6378 
6379 
6380 
6381 /*************************************************************************
6382  *
6383  * Public Procedure: Is_Doc_Approved
6384  * Effects: workflow procedure, called in workflow PORPOCHA
6385  *
6386  *          check if the document is in 'APPROVED' status or not.
6387  *
6388  ************************************************************************/
6389 procedure Is_Doc_Approved(itemtype        in varchar2,
6390                                 itemkey         in varchar2,
6391                                 actid           in number,
6392                                 funcmode        in varchar2,
6393                                 resultout       out NOCOPY varchar2    ) is
6394 
6395 l_doc_id number;
6396 l_doc_type varchar2(25);
6397 l_orgid       number;
6398 l_authorization_status PO_HEADERS_ALL.authorization_status%TYPE;
6399 
6400 x_progress              varchar2(100);
6401 
6402 l_doc_string varchar2(200);
6403 l_preparer_user_name varchar2(100);
6404 
6405 BEGIN
6406 
6407     x_progress := 'PO_ReqChangeRequestWF_PVT.Is_Doc_Approved: 01';
6408 
6409     -- Do nothing in cancel or timeout mode
6410     if (funcmode <> wf_engine.eng_run) then
6411         resultout := wf_engine.eng_null;
6412         return;
6413     end if;
6414 
6415     l_doc_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
6416                                          itemkey  => itemkey,
6417                                          aname    => 'DOCUMENT_ID');
6418 
6419     l_doc_type := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
6420                                          itemkey  => itemkey,
6421                                          aname    => 'DOCUMENT_TYPE');
6422 
6423     -- Set the multi-org context
6424     l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
6425                                          itemkey  => itemkey,
6426                                          aname    => 'ORG_ID');
6427 
6428     IF l_orgid is NOT NULL THEN
6429 
6430         PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
6431 
6432     END IF;
6433 
6434     IF l_doc_type IN ('PO','PA') THEN
6435 
6436         select AUTHORIZATION_STATUS
6437         into l_authorization_status
6438         from po_headers_all
6439         where PO_HEADER_ID = l_doc_id;
6440 
6441     ELSIF l_doc_type = 'RELEASE' THEN
6442 
6443         select AUTHORIZATION_STATUS
6444         into l_authorization_status
6445         from po_releases_all
6446         where  PO_RELEASE_ID = l_doc_id;
6447     end if;
6448 
6449 
6450     --bug 5440657
6451     -- In 'signature required' case, after buyer accepts the change request,
6452     -- the authorization_status of corresponding PO/REALEASE will be 'pre_approved'(pending signature)
6453     -- This way,is_doc_approved should return 'Y' so that PO change requests will be updated to 'ACCEPTED' status later.
6454 
6455     if(nvl(l_authorization_status, 'IN PROCESS') in ('APPROVED','PRE-APPROVED') ) then
6456         resultout := wf_engine.eng_completed || ':' || 'Y' ;
6457 
6458     else
6459         resultout := wf_engine.eng_completed || ':' || 'N' ;
6460     end if;
6461 
6462     x_progress := 'PO_ReqChangeRequestWF_PVT.Is_Doc_Approved: 02';
6463 
6464 EXCEPTION
6465     WHEN OTHERS THEN
6466         l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
6467         l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(
6468                        itemType, itemkey);
6469         wf_core.context('PO_ReqChangeRequestWF_PVT','Is_Doc_Approved',
6470                        x_progress);
6471         PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey,
6472                        l_preparer_user_name, l_doc_string, sqlerrm,
6473                        'PO_ReqChangeRequestWF_PVT.Is_Doc_Approved');
6474         raise;
6475 
6476 
6477 end Is_Doc_Approved;
6478 
6479 
6480 /*************************************************************************
6481  *
6482  * Public Procedure: Set_Doc_In_Process
6483  * Effects: workflow procedure, used in PORPOCHA(INFORM_BUYER_PO_CHANGE)
6484  *
6485  *          set the doc status to 'IN PROCESS'
6486  *
6487  ************************************************************************/
6488 procedure Set_Doc_In_Process(itemtype        in varchar2,
6489                                 itemkey         in varchar2,
6490                                 actid           in number,
6491                                 funcmode        in varchar2,
6492                                 resultout       out NOCOPY varchar2    ) is
6493 
6494 l_doc_id number;
6495 l_doc_type varchar2(25);
6496 l_orgid       number;
6497 l_authorization_status PO_HEADERS_ALL.authorization_status%type;
6498 
6499 x_progress              varchar2(100);
6500 
6501 l_doc_string varchar2(200);
6502 l_preparer_user_name varchar2(100);
6503 
6504 BEGIN
6505 
6506     x_progress := 'PO_ReqChangeRequestWF_PVT.Set_Doc_In_Process01';
6507 
6508     if (funcmode <> wf_engine.eng_run) then
6509         resultout := wf_engine.eng_null;
6510         return;
6511     end if;
6512 
6513     l_doc_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
6514                                          itemkey  => itemkey,
6515                                          aname    => 'DOCUMENT_ID');
6516 
6517     l_doc_type := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
6518                                          itemkey  => itemkey,
6519                                          aname    => 'DOCUMENT_TYPE');
6520 
6521     -- Set the multi-org context
6522     l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
6523                                          itemkey  => itemkey,
6524                                          aname    => 'ORG_ID');
6525 
6526     IF l_orgid is NOT NULL THEN
6527 
6528         PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
6529 
6530     END IF;
6531 
6532     IF l_doc_type IN ('PO','PA') THEN
6533         SetPOAuthStat(l_doc_id);
6534     ELSIF l_doc_type = 'RELEASE' THEN
6535         SetRelAuthStat(l_doc_id);
6536     end if;
6537   --
6538 
6539     x_progress := 'PO_ReqChangeRequestWF_PVT.Set_Doc_In_Process: 02';
6540 
6541 EXCEPTION
6542     WHEN OTHERS THEN
6543         l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
6544         l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(
6545                   itemType, itemkey);
6546         wf_core.context('PO_ReqChangeRequestWF_PVT',
6547                   'Set_Doc_In_Process',x_progress);
6548         PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey,
6549                   l_preparer_user_name, l_doc_string, sqlerrm,
6550                   'PO_ReqChangeRequestWF_PVT.Set_Doc_In_Process');
6551         raise;
6552 
6553 end Set_Doc_In_Process;
6554 
6555 
6556 
6557 
6558 /*************************************************************************
6559  *
6560  * Public Procedure: Compare_Revision
6561  * Effects: workflow procedure, used in PORPOCHA(INFORM_BUYER_PO_CHANGE)
6562  *
6563  *          determine if the PO has changed or not. (whether send fyi notif)
6564  *
6565  ************************************************************************/
6566 procedure Compare_Revision(itemtype        in varchar2,
6567                                 itemkey         in varchar2,
6568                                 actid           in number,
6569                                 funcmode        in varchar2,
6570                                 resultout       out NOCOPY varchar2    ) is
6571 
6572 l_doc_id number;
6573 l_doc_type varchar2(25);
6574 l_doc_revision NUMBER;
6575 l_old_doc_revision NUMBER;
6576 l_orgid       number;
6577 l_authorization_status PO_HEADERS_ALL.authorization_status%type;
6578 
6579 x_progress              varchar2(100);
6580 
6581 l_doc_string varchar2(200);
6582 l_preparer_user_name varchar2(100);
6583 
6584 BEGIN
6585 
6586     x_progress := 'PO_ReqChangeRequestWF_PVT.Compare_Revision';
6587 
6588     -- Do nothing in cancel or timeout mode
6589     if (funcmode <> wf_engine.eng_run) then
6590 
6591           resultout := wf_engine.eng_null;
6592           return;
6593 
6594     end if;
6595 
6596     l_doc_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
6597                                          itemkey  => itemkey,
6598                                          aname    => 'DOCUMENT_ID');
6599 
6600     l_doc_type := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
6601                                          itemkey  => itemkey,
6602                                          aname    => 'DOCUMENT_TYPE');
6603     l_old_doc_revision := PO_WF_UTIL_PKG.GetItemAttrNumber(itemtype => itemtype,
6604                                          itemkey  => itemkey,
6605                                          aname    => 'DOCUMENT_REVISION_NUM');
6606     -- Set the multi-org context
6607     l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
6608                                          itemkey  => itemkey,
6609                                          aname    => 'ORG_ID');
6610 
6611     IF l_orgid is NOT NULL THEN
6612 
6613         PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
6614 
6615     END IF;
6616 
6617     IF l_doc_type IN ('PO','PA') THEN
6618         select revision_num
6619         into l_doc_revision
6620         from po_headers_all
6621         where po_header_id=l_doc_id;
6622     ELSIF l_doc_type = 'RELEASE' THEN
6623         select revision_num
6624         into l_doc_revision
6625         from po_releases_all
6626         where po_release_id=l_doc_id;
6627     end if;
6628 
6629     if(nvl(l_old_doc_revision, -1)=nvl(l_doc_revision, -1)) then
6630         resultout := wf_engine.eng_completed || ':' || 'N' ;
6631     else
6632         resultout := wf_engine.eng_completed || ':' || 'Y' ;
6633     end if;
6634     x_progress := 'PO_ReqChangeRequestWF_PVT.Compare_Revision: 02';
6635 
6636 EXCEPTION
6637     WHEN OTHERS THEN
6638         l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
6639         l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(
6640                       itemType, itemkey);
6641         wf_core.context('PO_ReqChangeRequestWF_PVT',
6642                       'Compare_Revision',x_progress||sqlerrm);
6643         PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey,
6644                       l_preparer_user_name, l_doc_string, sqlerrm,
6645                       'PO_ReqChangeRequestWF_PVT.Compare_Revision');
6646         raise;
6647 end Compare_Revision;
6648 
6649 
6650 
6651 /*************************************************************************
6652  *
6653  * Public Procedure: Record_Buyer_Rejection
6654  * Effects: workflow procedure, used in PORPOCHA(INFORM_BUYER_PO_CHANGE)
6655  *
6656  *          if buyer respond to the change request through notification
6657  *          this function will update the status to 'REJECTED'
6658  *
6659  ************************************************************************/
6660 procedure Record_Buyer_Rejection(itemtype        in varchar2,
6661                                 itemkey         in varchar2,
6662                                 actid           in number,
6663                                 funcmode        in varchar2,
6664                                 resultout       out NOCOPY varchar2    ) is
6665 
6666 l_doc_id number;
6667 l_doc_type varchar2(25);
6668 l_doc_revision NUMBER;
6669 l_old_doc_revision NUMBER;
6670 l_orgid       number;
6671 l_change_request_group_id number;
6672 l_authorization_status PO_HEADERS_ALL.authorization_status%type;
6673 
6674 x_progress              varchar2(100);
6675 
6676 l_doc_string varchar2(200);
6677 l_preparer_user_name varchar2(100);
6678 l_response_reason varchar2(2000);
6679 
6680 
6681 BEGIN
6682 
6683     x_progress := 'PO_ReqChangeRequestWF_PVT.Record_Buyer_Rejection';
6684 
6685     -- Do nothing in cancel or timeout mode
6686     if (funcmode <> wf_engine.eng_run) then
6687         resultout := wf_engine.eng_null;
6688         return;
6689     end if;
6690 
6691     l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (
6692                     itemtype => itemtype,
6693                     itemkey  => itemkey,
6694                     aname    => 'CHANGE_REQUEST_GROUP_ID');
6695 
6696     l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
6697                                          itemkey  => itemkey,
6698                                          aname    => 'ORG_ID');
6699     l_response_reason := PO_WF_UTIL_PKG.GetItemAttrText(itemtype => itemtype,
6700                                          itemkey  => itemkey,
6701                                          aname    => 'RESPONSE_REASON');
6702 
6703     IF l_orgid is NOT NULL THEN
6704         PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
6705     END IF;
6706     SetPoRequestStatus(l_change_request_group_id, 'REJECTED', l_response_reason);
6707 
6708     x_progress := 'PO_ReqChangeRequestWF_PVT.Record_Buyer_Rejection: 02';
6709 
6710 EXCEPTION
6711     WHEN OTHERS THEN
6712         l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
6713         l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(
6714                       itemType, itemkey);
6715         wf_core.context('PO_ReqChangeRequestWF_PVT',
6716                       'Record_Buyer_Rejection',x_progress);
6717         PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey,
6718                       l_preparer_user_name, l_doc_string, sqlerrm,
6719                       'PO_ReqChangeRequestWF_PVT.Record_Buyer_Rejection');
6720         raise;
6721 
6722 end Record_Buyer_Rejection;
6723 
6724 
6725 /*************************************************************************
6726  *
6727  * Public Procedure: Record_Buyer_Acceptance
6728  * Effects: workflow procedure, used in PORPOCHA(INFORM_BUYER_PO_CHANGE)
6729  *
6730  *          if buyer respond to the change request through notification
6731  *          this function will update the status to 'BUYER_APP'
6732  *
6733  ************************************************************************/
6734 procedure Record_Buyer_Acceptance(itemtype        in varchar2,
6735                                 itemkey         in varchar2,
6736                                 actid           in number,
6737                                 funcmode        in varchar2,
6738                                 resultout       out NOCOPY varchar2    ) is
6739 
6740 l_doc_id number;
6741 l_doc_type varchar2(25);
6742 l_doc_revision NUMBER;
6743 l_old_doc_revision NUMBER;
6744 l_orgid       number;
6745 l_change_request_group_id number;
6746 l_authorization_status PO_HEADERS_ALL.authorization_status%type;
6747 
6748 x_progress              varchar2(100);
6749 
6750 l_doc_string varchar2(200);
6751 l_preparer_user_name varchar2(100);
6752 l_response_reason varchar2(2000);
6753 
6754 BEGIN
6755 
6756     x_progress := 'PO_ReqChangeRequestWF_PVT.Record_Buyer_Acceptance';
6757 
6758     -- Do nothing in cancel or timeout mode
6759     if (funcmode <> wf_engine.eng_run) then
6760         resultout := wf_engine.eng_null;
6761         return;
6762     end if;
6763 
6764     l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (
6765                     itemtype => itemtype,
6766                     itemkey  => itemkey,
6767                     aname    => 'CHANGE_REQUEST_GROUP_ID');
6768 
6769     l_response_reason := PO_WF_UTIL_PKG.GetItemAttrText(itemtype => itemtype,
6770                                          itemkey  => itemkey,
6771                                          aname    => 'RESPONSE_REASON');
6772 
6773     l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
6774                                          itemkey  => itemkey,
6775                                          aname    => 'ORG_ID');
6776 
6777     IF l_orgid is NOT NULL THEN
6778         PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
6779     END IF;
6780     SetPoRequestStatus(l_change_request_group_id, 'BUYER_APP', l_response_reason);
6781 
6782     x_progress := 'PO_ReqChangeRequestWF_PVT.Record_Buyer_Acceptance: 02';
6783 
6784 EXCEPTION
6785     WHEN OTHERS THEN
6786         l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
6787         l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(
6788                       itemType, itemkey);
6789         wf_core.context('PO_ReqChangeRequestWF_PVT',
6790                       'Record_Buyer_Acceptance',x_progress);
6791         PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey,
6792                       l_preparer_user_name, l_doc_string, sqlerrm,
6793                       'PO_ReqChangeRequestWF_PVT.Record_Buyer_Acceptance');
6794         raise;
6795 
6796 end Record_Buyer_Acceptance;
6797 
6798 
6799 /*************************************************************************
6800  *
6801  * Public Procedure: Start_Process_Buy_Response_WF
6802  * Effects: workflow procedure, used in PORPOCHA(INFORM_BUYER_PO_CHANGE)
6803  *
6804  *          if buyer respond to the change request through notification
6805  *          this function will start the process buyer's response process
6806  *
6807  ************************************************************************/
6808 procedure Start_Process_Buy_Response_WF(itemtype        in varchar2,
6809                                 itemkey         in varchar2,
6810                                 actid           in number,
6811                                 funcmode        in varchar2,
6812                                 resultout       out NOCOPY varchar2    ) is
6813 
6814 l_doc_id number;
6815 l_doc_type varchar2(25);
6816 l_doc_revision NUMBER;
6817 l_old_doc_revision NUMBER;
6818 l_orgid       number;
6819 l_change_request_group_id number;
6820 l_authorization_status varchar2(25);
6821 
6822 x_progress              varchar2(100);
6823 
6824 l_doc_string varchar2(200);
6825 l_preparer_user_name varchar2(100);
6826 
6827 BEGIN
6828 
6829     x_progress := 'PO_ReqChangeRequestWF_PVT.Start_Process_Buy_Response_WF';
6830 
6831     if (funcmode <> wf_engine.eng_run) then
6832         resultout := wf_engine.eng_null;
6833         return;
6834     end if;
6835 
6836     l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (
6837                      itemtype => itemtype,
6838                      itemkey  => itemkey,
6839                      aname    => 'CHANGE_REQUEST_GROUP_ID');
6840 
6841     -- Set the multi-org context
6842     l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
6843                                            itemkey  => itemkey,
6844                                            aname    => 'ORG_ID');
6845 
6846     IF l_orgid is NOT NULL THEN
6847         PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
6848     END IF;
6849     Start_ProcessBuyerResponseWF(l_change_request_group_id);
6850 
6851     x_progress := 'PO_ReqChangeRequestWF_PVT.Start_Process_Buy_Response_WF: 02';
6852 
6853 EXCEPTION
6854     WHEN OTHERS THEN
6855         l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
6856         l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(
6857                     itemType, itemkey);
6858         wf_core.context('PO_ReqChangeRequestWF_PVT',
6859                     'Start_Process_Buy_Response_WF',x_progress);
6860         PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey,
6861                     l_preparer_user_name, l_doc_string, sqlerrm,
6862                     'PO_ReqChangeRequestWF_PVT.Inssert_into_History_CHGsubmit');
6863         raise;
6864 
6865 end Start_Process_Buy_Response_WF;
6866 
6867 
6868 
6869 
6870 /*************************************************************************
6871  *
6872  * Public Procedure: Insert_Buyer_Action_History
6873  * Effects: workflow procedure, called in workflow
6874  *          PORPOCHA (PROCESS_BUYER_RESPONSE)
6875  *
6876  *          inserting into action history table buyer's response
6877  *
6878  ************************************************************************/
6879 procedure Insert_Buyer_Action_History(itemtype        in varchar2,
6880                                 itemkey         in varchar2,
6881                                 actid           in number,
6882                                 funcmode        in varchar2,
6883                                 resultout       out NOCOPY varchar2    ) is
6884 
6885 l_doc_id number;
6886 l_doc_type varchar2(25);
6887 l_doc_subtype varchar2(25);
6888 l_note        PO_ACTION_HISTORY.note%TYPE;
6889 l_employee_id number;
6890 l_orgid       number;
6891 l_count number;
6892 l_action varchar2(10);
6893 
6894 x_progress              varchar2(100);
6895 
6896 l_doc_string varchar2(200);
6897 l_preparer_user_name varchar2(100);
6898 
6899 l_path_id number;
6900 l_change_request_group_id number;
6901 cursor change_request is
6902     select request_status
6903     from po_change_requests
6904     where change_request_group_id=l_change_request_group_id;
6905 
6906 
6907 BEGIN
6908 
6909     x_progress := 'PO_ReqChangeRequestWF_PVT.Insert_Buyer_Action_History: 01';
6910 
6911     -- Do nothing in cancel or timeout mode
6912     --
6913     if (funcmode <> wf_engine.eng_run) then
6914         resultout := wf_engine.eng_null;
6915         return;
6916     end if;
6917 
6918     l_change_request_group_id:= PO_WF_UTIL_PKG.GetItemAttrNumber (
6919                    itemtype => itemtype,
6920                    itemkey  => itemkey,
6921                    aname    => 'CHANGE_REQUEST_GROUP_ID');
6922 
6923     select count(distinct(request_status))
6924     into l_count
6925     from po_change_requests
6926     where change_request_group_id=l_change_request_group_id;
6927 
6928     if(l_count=1) then
6929         open change_request;
6930         fetch change_request into l_action;
6931         close change_request;
6932 
6933         if(l_action='BUYER_APP') then
6934             l_action:='ACCEPT';
6935         else
6936             l_action:='REJECT';
6937         end if;
6938     else
6939         l_action:='RESPOND';
6940     end if;
6941 
6942     l_doc_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
6943                                          itemkey  => itemkey,
6944                                          aname    => 'DOCUMENT_ID');
6945 
6946     l_doc_type := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
6947                                          itemkey  => itemkey,
6948                                          aname    => 'DOCUMENT_TYPE');
6949 
6950     l_doc_subtype := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
6951                                          itemkey  => itemkey,
6952                                          aname    => 'DOCUMENT_SUBTYPE');
6953 
6954     -- Set the multi-org context
6955     l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
6956                                          itemkey  => itemkey,
6957                                          aname    => 'ORG_ID');
6958 
6959     IF l_orgid is NOT NULL THEN
6960 
6961         PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
6962 
6963     END IF;
6964 
6965     IF l_doc_type IN ('PO','PA') THEN
6966 
6967         select agent_id
6968         into l_employee_id
6969         from po_headers_all
6970         where PO_HEADER_ID = l_doc_id;
6971 
6972     ELSIF l_doc_type = 'RELEASE' THEN
6973 
6974         select agent_id
6975         into l_employee_id
6976         from po_releases_all
6977         where  PO_RELEASE_ID = l_doc_id;
6978     end if;
6979 
6980 
6981     InsertActionHist(itemtype,itemkey,l_doc_id, l_doc_type,
6982                    l_doc_subtype, l_employee_id, l_action, l_note, null);
6983 
6984     resultout := wf_engine.eng_completed || ':' || 'ACTIVITY_PERFORMED' ;
6985 
6986     x_progress := 'PO_ReqChangeRequestWF_PVT.Insert_Buyer_Action_History: 02';
6987 
6988 EXCEPTION
6989     WHEN OTHERS THEN
6990         l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
6991         l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(
6992                        itemType, itemkey);
6993         wf_core.context('PO_ReqChangeRequestWF_PVT',
6994                        'Insert_Buyer_Action_History',x_progress);
6995         PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey,
6996                        l_preparer_user_name, l_doc_string, sqlerrm,
6997                        'PO_ReqChangeRequestWF_PVT.Insert_Buyer_Action_History');
6998         raise;
6999 
7000 END Insert_Buyer_Action_History;
7001 
7002 
7003 
7004 
7005 /*************************************************************************
7006  *
7007  * Public Procedure: Process_Buyer_Rejection
7008  * Effects: workflow procedure, used in PORPOCHA(PROCESS_BUYER_RESPONSE)
7009  *
7010  *          after buyer responds to the change request, the status of the
7011  *          PO change request can be in 'REJECTED', 'BUYER_APP'.
7012  *          This procedure will process those PO change request
7013  *          which is rejected by the buyer by update the related req change
7014  *          requests to 'REJECTED'.
7015  *
7016  *          it commits when it exits.
7017  *
7018  ************************************************************************/
7019 procedure Process_Buyer_Rejection(itemtype        in varchar2,
7020                                 itemkey         in varchar2,
7021                                 actid           in number,
7022                                 funcmode        in varchar2,
7023                                 resultout       out NOCOPY varchar2    )
7024 is
7025 l_orgid       number;
7026 l_change_request_group_id number;
7027 
7028 x_progress              varchar2(100);
7029 
7030 l_doc_string varchar2(200);
7031 l_preparer_user_name varchar2(100);
7032 
7033 BEGIN
7034 
7035     x_progress := 'PO_ReqChangeRequestWF_PVT.Process_Buyer_Rejection';
7036 
7037     -- Do nothing in cancel or timeout mode
7038     if (funcmode <> wf_engine.eng_run) then
7039         resultout := wf_engine.eng_null;
7040         return;
7041     end if;
7042 
7043 
7044     l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (
7045               itemtype => itemtype,
7046               itemkey  => itemkey,
7047               aname    => 'CHANGE_REQUEST_GROUP_ID');
7048 
7049     -- Set the multi-org context
7050     l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
7051                                          itemkey  => itemkey,
7052                                          aname    => 'ORG_ID');
7053 
7054     IF l_orgid is NOT NULL THEN
7055 
7056         PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
7057 
7058     END IF;
7059     ProcessBuyerAction(l_change_request_group_id, 'REJECTION');
7060 
7061     x_progress := 'PO_ReqChangeRequestWF_PVT.Process_Buyer_Rejection: 02';
7062 
7063 EXCEPTION
7064     WHEN OTHERS THEN
7065         l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
7066         l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(
7067                    itemType, itemkey);
7068         wf_core.context('PO_ReqChangeRequestWF_PVT',
7069                    'Process_Buyer_Rejection',x_progress);
7070         PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey,
7071                    l_preparer_user_name, l_doc_string, sqlerrm,
7072                    'PO_ReqChangeRequestWF_PVT.Process_Buyer_Rejection');
7073     raise;
7074 
7075 
7076 end Process_Buyer_Rejection;
7077 
7078 
7079 /*************************************************************************
7080  *
7081  * Public Procedure: Process_Cancel_Acceptance
7082  * Effects: workflow procedure, used in PORPOCHA(PROCESS_BUYER_RESPONSE)
7083  *
7084  *          after buyer responds to the change request, the status of the
7085  *          PO change request can be in 'REJECTED', 'BUYER_APP'.
7086  *          This procedure will process those PO cancel request
7087  *          which is accepted by the buyer by update the related req change
7088  *          requests to 'accepted', and cancel the req line.
7089  *
7090  *          it commits when it exits.
7091  *
7092  ************************************************************************/
7093 procedure Process_Cancel_Acceptance(itemtype        in varchar2,
7094                                 itemkey         in varchar2,
7095                                 actid           in number,
7096                                 funcmode        in varchar2,
7097                                 resultout       out NOCOPY varchar2    )
7098 is
7099 l_orgid       number;
7100 l_change_request_group_id number;
7101 l_responsibility_id number := fnd_global.resp_id;
7102 l_application_id  number := fnd_global.RESP_APPL_ID;
7103 l_user_id number := fnd_global.user_id;
7104 
7105 x_progress              varchar2(100);
7106 
7107 l_doc_string varchar2(200);
7108 l_preparer_user_name varchar2(100);
7109 
7110 BEGIN
7111 
7112   x_progress := 'PO_ReqChangeRequestWF_PVT.Process_Cancel_Acceptance';
7113 
7114   -- Do nothing in cancel or timeout mode
7115   if (funcmode <> wf_engine.eng_run) then
7116       resultout := wf_engine.eng_null;
7117       return;
7118   end if;
7119 
7120   l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
7121                                          itemkey  => itemkey,
7122                                          aname    => 'CHANGE_REQUEST_GROUP_ID');
7123   /* Bug#3769157 : Set the responsibility and application id of the preparer
7124        if null(happens when notification responded by buyer from worklist) */
7125 
7126          if( (nvl(l_responsibility_id, -1) <0) or (nvl(l_application_id, -1) < 0) ) then
7127                 l_responsibility_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
7128                                                                     itemkey  => itemkey,
7129                                                                     aname    => 'RESPONSIBILITY_ID');
7130 
7131                 l_application_id  := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
7132                                                                   itemkey  => itemkey,
7133                                                                   aname    => 'APPLICATION_ID');
7134 
7135                 fnd_global.APPS_INITIALIZE (l_user_id, l_responsibility_id, l_application_id);
7136 
7137          end if;
7138 
7139 
7140   -- Set the multi-org context
7141   l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
7142                                          itemkey  => itemkey,
7143                                          aname    => 'ORG_ID');
7144 
7145   IF l_orgid is NOT NULL THEN
7146 
7147     PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
7148 
7149   END IF;
7150 
7151   ProcessBuyerAction(l_change_request_group_id, 'CANCELLATION');
7152 
7153   x_progress := 'PO_ReqChangeRequestWF_PVT.Process_Cancel_Acceptance: 02';
7154 
7155 EXCEPTION
7156   WHEN OTHERS THEN
7157     l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
7158     l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(itemType, itemkey);
7159     wf_core.context('PO_ReqChangeRequestWF_PVT','Process_Cancel_Acceptance',x_progress);
7160     PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey, l_preparer_user_name, l_doc_string, sqlerrm, 'PO_ReqChangeRequestWF_PVT.Process_Cancel_Acceptance');
7161     raise;
7162 
7163 
7164 end Process_Cancel_Acceptance;
7165 
7166 
7167 
7168 /*************************************************************************
7169  *
7170  * Public Procedure: Change_Acceptance_Exists
7171  * Effects: workflow procedure, used in PORPOCHA(PROCESS_BUYER_RESPONSE)
7172  *
7173  *          check if there is PO change requests with status 'BUYER_APP'
7174  *          exists.
7175  *
7176  ************************************************************************/
7177 procedure Change_Acceptance_Exists(itemtype        in varchar2,
7178                                 itemkey         in varchar2,
7179                                 actid           in number,
7180                                 funcmode        in varchar2,
7181                                 resultout       out NOCOPY varchar2    )
7182 is
7183 l_orgid       number;
7184 l_change_request_group_id number;
7185 l_change_request_id number;
7186 
7187 x_progress              varchar2(100);
7188 
7189 l_doc_string varchar2(200);
7190 l_preparer_user_name varchar2(100);
7191 
7192 cursor accepted_change is
7193 select change_request_id
7194 from po_change_requests
7195 where change_request_group_id=l_change_request_group_id
7196     and request_status='BUYER_APP';
7197 
7198 BEGIN
7199 
7200   x_progress := 'PO_ReqChangeRequestWF_PVT.Change_Acceptance_Exists';
7201 
7202   -- Do nothing in cancel or timeout mode
7203   if (funcmode <> wf_engine.eng_run) then
7204       resultout := wf_engine.eng_null;
7205       return;
7206   end if;
7207 
7208   l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
7209                                          itemkey  => itemkey,
7210                                          aname    => 'CHANGE_REQUEST_GROUP_ID');
7211 
7212   -- Set the multi-org context
7213   l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
7214                                          itemkey  => itemkey,
7215                                          aname    => 'ORG_ID');
7216 
7217   IF l_orgid is NOT NULL THEN
7218     PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
7219   END IF;
7220 
7221   open accepted_change;
7222   fetch accepted_change into l_change_request_id;
7223   close accepted_change;
7224 
7225   if(l_change_request_id is null) then
7226     -- not exist
7227     resultout := wf_engine.eng_completed || ':' || 'N' ;
7228   else
7229     resultout := wf_engine.eng_completed || ':' || 'Y' ;
7230   end if;
7231   x_progress := 'PO_ReqChangeRequestWF_PVT.Change_Acceptance_Exists: 02';
7232 
7233 EXCEPTION
7234   WHEN OTHERS THEN
7235     l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
7236     l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(itemType, itemkey);
7237     wf_core.context('PO_ReqChangeRequestWF_PVT','Change_Acceptance_Exists',x_progress);
7238     PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey, l_preparer_user_name, l_doc_string, sqlerrm, 'PO_ReqChangeRequestWF_PVT.Change_Acceptance_Exists');
7239     raise;
7240 
7241 
7242 end Change_Acceptance_Exists;
7243 
7244 
7245 /*************************************************************************
7246  *
7247  * Public Procedure: Process_Change_Acceptance
7248  * Effects: workflow procedure, used in PORPOCHA(PROCESS_BUYER_RESPONSE)
7249  *
7250  *          after buyer responds to the change request, the status of the
7251  *          PO change request can be in 'REJECTED', 'BUYER_APP'.
7252  *          This procedure will process those PO change request
7253  *          which is accepted by the buyer by update the related req change
7254  *          requests to 'accepted', and update the req line/distribution.
7255  *          it will also call MoveChangeToPO to move the changes to PO
7256  *
7257  *          it commits when it exits.
7258  *
7259 ************************************************************************/
7260 procedure Process_Change_Acceptance(itemtype        in varchar2,
7261                                 itemkey         in varchar2,
7262                                 actid           in number,
7263                                 funcmode        in varchar2,
7264                                 resultout       out NOCOPY varchar2    )
7265 is
7266 l_orgid       number;
7267 l_change_request_group_id number;
7268 
7269 x_progress              varchar2(100);
7270 
7271 l_doc_string varchar2(200);
7272 l_preparer_user_name varchar2(100);
7273 
7274 BEGIN
7275 
7276   x_progress := 'PO_ReqChangeRequestWF_PVT.Process_Change_Acceptance';
7277 
7278 
7279   -- Do nothing in cancel or timeout mode
7280   if (funcmode <> wf_engine.eng_run) then
7281 
7282       resultout := wf_engine.eng_null;
7283       return;
7284 
7285   end if;
7286 
7287 
7288   l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
7289                                          itemkey  => itemkey,
7290                                          aname    => 'CHANGE_REQUEST_GROUP_ID');
7291 
7292   -- Set the multi-org context
7293   l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
7294                                          itemkey  => itemkey,
7295                                          aname    => 'ORG_ID');
7296 
7297   IF l_orgid is NOT NULL THEN
7298 
7299     PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
7300 
7301   END IF;
7302   ProcessBuyerAction(l_change_request_group_id, 'ACCEPTANCE');
7303 
7304   x_progress := 'PO_ReqChangeRequestWF_PVT.Process_Change_Acceptance: 02';
7305 
7306 EXCEPTION
7307   WHEN OTHERS THEN
7308     l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
7309     l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(itemType, itemkey);
7310     wf_core.context('PO_ReqChangeRequestWF_PVT','Process_Change_Acceptance',x_progress);
7311     PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey, l_preparer_user_name, l_doc_string, sqlerrm, 'PO_ReqChangeRequestWF_PVT.Process_Change_Acceptance');
7312     raise;
7313 
7314 
7315 end Process_Change_Acceptance;
7316 
7317 
7318 procedure KickOffPOApproval(p_po_header_id in number,
7319                     p_po_release_id in number,
7320                     p_return_status out NOCOPY varchar2,
7321                     p_return_msg out NOCOPY varchar2) is
7322 pragma AUTONOMOUS_TRANSACTION;
7323 /*
7324 l_responsibility_id     number;
7325 l_user_id               number;
7326 l_application_id        number;
7327 */
7328 
7329 begin
7330 /*
7331     FND_PROFILE.GET('USER_ID', l_user_id);
7332     FND_PROFILE.GET('RESP_ID', l_responsibility_id);
7333     FND_PROFILE.GET('RESP_APPL_ID', l_application_id);
7334     fnd_global.APPS_INITIALIZE (l_user_id, l_responsibility_id, l_application_id);
7335 */
7336     po_sup_chg_request_wf_grp.KickOffPOApproval(1, p_return_status,
7337                      p_po_header_id, p_po_release_id, p_return_msg);
7338     if(p_return_status=FND_API.G_RET_STS_SUCCESS) then
7339         commit;
7340     end if;
7341 exception
7342     when others then
7343     wf_core.context('PO_ReqChangeRequestWF_PVT','KickOffPOApproval',sqlerrm);
7344     raise;
7345 end;
7346 
7347 
7348 /*************************************************************************
7349  *
7350  * Public Procedure: Start_Poapprv_WF
7351  * Effects: workflow procedure, used in PORPOCHA(PROCESS_BUYER_RESPONSE)
7352  *
7353  *          start POAPPRV workflow for buyer accepted requester change
7354  *
7355 ************************************************************************/
7356 procedure Start_Poapprv_WF(itemtype        in varchar2,
7357                                 itemkey         in varchar2,
7358                                 actid           in number,
7359                                 funcmode        in varchar2,
7360                                 resultout       out NOCOPY varchar2    )
7361 is
7362 l_orgid       number;
7363 l_change_request_group_id number;
7364 l_change_request_id number;
7365 
7366 l_header_id number;
7367 l_release_id number;
7368 
7369 x_progress              varchar2(100);
7370 
7371 
7372 l_doc_string varchar2(200);
7373 l_preparer_user_name varchar2(100);
7374 
7375 l_return_status varchar2(1);
7376 l_return_msg varchar2(2000);
7377 
7378 l_exception exception;
7379 
7380 cursor document_id is
7381 select document_header_id, po_release_id
7382 from po_change_requests
7383 where change_request_group_id=l_change_request_group_id
7384     and request_status='BUYER_APP';
7385 
7386 --Set the authorization status of po to
7387 --approved for those change request automatically rejected
7388 
7389 cursor document_id_rejected is
7390 select document_header_id, po_release_id
7391 from po_change_requests
7392 where change_request_group_id=l_change_request_group_id
7393     and request_status='REJECTED';
7394 
7395 BEGIN
7396   x_progress := 'PO_ReqChangeRequestWF_PVT.Start_Poapprv_WF';
7397 
7398 
7399   -- Do nothing in cancel or timeout mode
7400   if (funcmode <> wf_engine.eng_run) then
7401 
7402       resultout := wf_engine.eng_null;
7403       return;
7404 
7405   end if;
7406 
7407 
7408   l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
7409                                          itemkey  => itemkey,
7410                                          aname    => 'CHANGE_REQUEST_GROUP_ID');
7411 
7412   -- Set the multi-org context
7413   l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
7414                                          itemkey  => itemkey,
7415                                          aname    => 'ORG_ID');
7416 
7417   IF l_orgid is NOT NULL THEN
7418 
7419     PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
7420 
7421   END IF;
7422 
7423   x_progress := 'PO_ReqChangeRequestWF_PVT.Start_Poapprv_WF:01';
7424   open document_id;
7425   fetch document_id into l_header_id, l_release_id;
7426   close document_id;
7427   x_progress := 'PO_ReqChangeRequestWF_PVT.Start_Poapprv_WF:02';
7428 
7429   --In case the change request is automatically
7430   --rejected, there will be no change reqeust in BUY_APP. So,
7431   --the document_id cursor will not fetch records. So, call
7432   --KickOffPOApproval only if either l_header_id or l_release_id
7433   --is not null
7434   l_return_status := FND_API.G_RET_STS_SUCCESS;
7435 
7436   IF ( (l_header_id is not null) OR (l_release_id is not null)) THEN
7437   KickOffPOApproval(l_header_id,l_release_id, l_return_status, l_return_msg);
7438   END IF;
7439   --po_sup_chg_request_wf_grp.KickOffPOApproval(1, l_return_status, l_header_id, l_release_id, l_return_msg);
7440   --PO_ChangeOrderWF_GRP.KickOffPOApproval(1, l_return_status, l_header_id, l_release_id, l_return_msg);
7441 
7442   if(l_return_status<>FND_API.G_RET_STS_SUCCESS) then
7443     x_progress := 'PO_ReqChangeRequestWF_PVT.Start_Poapprv_WF:-'||l_return_status||'-'||to_char(l_header_id)||'-'||to_char(l_release_id)||'-'||l_return_msg;
7444     raise l_exception;
7445   else
7446     x_progress := 'PO_ReqChangeRequestWF_PVT.Start_Poapprv_WF: 03';
7447   end if;
7448 
7449   l_header_id := null;
7450   l_release_id := null;
7451 
7452   --Now set the authorization status of PO corresponding
7453   --to automatically rejected change request as 'APPROVED'
7454   x_progress := 'PO_ReqChangeRequestWF_PVT.Start_Poapprv_WF:04';
7455   open document_id_rejected;
7456   fetch document_id_rejected into l_header_id, l_release_id;
7457   close document_id_rejected;
7458   x_progress := 'PO_ReqChangeRequestWF_PVT.Start_Poapprv_WF:05';
7459 
7460   IF(l_release_id is not null) THEN
7461     UPDATE po_releases_all
7462     SET    authorization_status='APPROVED'
7463     WHERE  po_release_id = l_release_id;
7464   ELSE
7465     UPDATE po_headers_all
7466     SET    authorization_status='APPROVED'
7467     WHERE  po_header_id = l_header_id;
7468   END IF;
7469 
7470   x_progress := 'PO_ReqChangeRequestWF_PVT.Start_Poapprv_WF:06';
7471 
7472 EXCEPTION
7473   WHEN OTHERS THEN
7474     l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
7475     l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(itemType, itemkey);
7476     wf_core.context('PO_ReqChangeRequestWF_PVT','Start_Poapprv_WF',x_progress||sqlerrm);
7477     PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey, l_preparer_user_name, l_doc_string, sqlerrm, 'PO_ReqChangeRequestWF_PVT.Start_Poapprv_WF');
7478     raise;
7479 
7480 
7481 end Start_Poapprv_WF;
7482 
7483 
7484 
7485 
7486 /*************************************************************************
7487  *
7488  * Public Procedure: Req_Change_Responded
7489  * Effects: workflow procedure, used in PORPOCHA(NOTIFY_REQUESTER_PROCESS)
7490  *
7491  *          Check if the whole change request is responded.
7492  *
7493 ************************************************************************/
7494 procedure Req_Change_Responded(itemtype        in varchar2,
7495                                 itemkey         in varchar2,
7496                                 actid           in number,
7497                                 funcmode        in varchar2,
7498                                 resultout       out NOCOPY varchar2    ) is
7499 l_orgid       number;
7500 l_change_request_group_id number;
7501 l_parent_request_group_id number;
7502 
7503 x_progress              varchar2(100);
7504 l_change_request_id  number;
7505 
7506 l_doc_string varchar2(200);
7507 l_preparer_user_name varchar2(100);
7508 
7509 --SQL What: get the parent change_request_id which is still in pending status
7510 --SQL Why: need it to check whether the parent change is fully responded
7511 --SQL Join: parent_change_request_id
7512 cursor pending_parent_change_csr(p_change_request_group_id number) is
7513     select pcr3.change_request_id
7514     from po_change_requests pcr1,
7515          po_change_requests pcr2,
7516          po_change_requests pcr3
7517     where pcr2.change_request_group_id=p_change_request_group_id
7518         and pcr2.parent_change_request_id=pcr1.change_request_id
7519         and pcr1.change_request_group_id=pcr3.change_request_group_id
7520         and pcr3.action_type in ('MODIFICATION', 'CANCELLATION')
7521         and pcr3.request_status not in ('ACCEPTED', 'REJECTED');
7522 
7523 BEGIN
7524 
7525   x_progress := 'PO_ReqChangeRequestWF_PVT.Req_Change_Responded';
7526 
7527   -- Do nothing in cancel or timeout mode
7528   if (funcmode <> wf_engine.eng_run) then
7529       resultout := wf_engine.eng_null;
7530       return;
7531   end if;
7532 
7533   l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
7534                                          itemkey  => itemkey,
7535                                          aname    => 'CHANGE_REQUEST_GROUP_ID');
7536 
7537   -- Set the multi-org context
7538   l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
7539                                          itemkey  => itemkey,
7540                                          aname    => 'ORG_ID');
7541 
7542   IF l_orgid is NOT NULL THEN
7543 
7544     PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
7545 
7546   END IF;
7547 
7548   open pending_parent_change_csr(l_change_request_group_id);
7549   fetch pending_parent_change_csr into l_change_request_id;
7550   close pending_parent_change_csr;
7551 
7552   if(l_change_request_id is null) then
7553     resultout := wf_engine.eng_completed || ':' || 'Y' ;
7554   else
7555     resultout := wf_engine.eng_completed || ':' || 'N' ;
7556   end if;
7557 
7558   x_progress := 'PO_ReqChangeRequestWF_PVT.Req_Change_Responded: 02';
7559 
7560 EXCEPTION
7561   WHEN OTHERS THEN
7562     l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
7563     l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(itemType, itemkey);
7564     wf_core.context('PO_ReqChangeRequestWF_PVT','Req_Change_Responded',x_progress);
7565     PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey, l_preparer_user_name, l_doc_string, sqlerrm, 'PO_ReqChangeRequestWF_PVT.Req_Change_Responded');
7566     raise;
7567 
7568 
7569 end Req_Change_Responded;
7570 
7571 
7572 /*************************************************************************
7573  *
7574  * Public Procedure: Reset_Req_Change_Flag
7575  * Effects: workflow procedure, used in PORPOCHA(NOTIFY_REQUESTER_PROCESS)
7576  *
7577  *          when the req change request is fully responded, reset the
7578  *          change_pending_flag in the po_requisiton_headers_all table to 'N'
7579  *
7580 ************************************************************************/
7581 PROCEDURE Reset_Req_Change_Flag(itemtype        in varchar2,
7582                                 itemkey         in varchar2,
7583                                 actid           in number,
7584                                 funcmode        in varchar2,
7585                                 resultout       out NOCOPY varchar2 )
7586 IS
7587 
7588   l_document_id                 NUMBER;
7589   l_req_change_request_group_id NUMBER;
7590   l_orgid                       NUMBER;
7591   x_progress                    VARCHAR2(100);
7592   l_doc_string varchar2(200);
7593   l_preparer_user_name varchar2(100);
7594 
7595 BEGIN
7596 
7597   x_progress := 'PO_ReqChangeRequestWF_PVT.Reset_Req_Change_Flag: 01';
7598 
7599 
7600   -- Do nothing in cancel or timeout mode
7601   --
7602   if (funcmode <> wf_engine.eng_run) then
7603 
7604       resultout := wf_engine.eng_null;
7605       return;
7606 
7607   end if;
7608 
7609   -- Set the multi-org context
7610   l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber( itemtype => itemtype,
7611                                           itemkey  => itemkey,
7612                                           aname    => 'ORG_ID' );
7613 
7614   IF l_orgid is NOT NULL THEN
7615 
7616       PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
7617 
7618   END IF;
7619 
7620   l_req_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (
7621                                          itemtype => itemtype,
7622                                          itemkey  => itemkey,
7623                                          aname    => 'REQ_CHANGE_GROUP_ID');
7624 
7625   l_document_id := PO_WF_UTIL_PKG.GetItemAttrNumber (
7626                                  itemtype => itemtype,
7627                                  itemkey  => itemkey,
7628                                  aname    => 'REQ_HEADER_ID');
7629 
7630   x_progress := 'PO_REQAPPROVAL_INIT1.Reset_Req_Change_Flag: 02';
7631 
7632   SetReqChangeFlag(l_req_change_request_group_id, l_document_id, itemtype, itemkey, 'N');
7633 
7634   --
7635   resultout := wf_engine.eng_completed || ':' ||  'ACTIVITY_PERFORMED';
7636   --
7637 
7638   x_progress := 'PO_REQAPPROVAL_INIT1.Reset_Req_Change_Flag: 03';
7639 
7640 
7641 EXCEPTION
7642   WHEN OTHERS THEN
7643     l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
7644     l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(itemType, itemkey);
7645     wf_core.context('PO_ReqChangeRequestWF_PVT','Reset_Req_Change_Flag',x_progress);
7646     PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey, l_preparer_user_name, l_doc_string, sqlerrm, 'PO_ReqChangeRequestWF_PVT.Reset_Req_Change_Flag');
7647     raise;
7648 
7649 END Reset_Req_Change_Flag;
7650 
7651 /*************************************************************************
7652  *
7653  * Public Procedure: Get_Total_Amount_Currency
7654  * Effects: workflow procedure, used in PORPOCHA(NOTIFY_REQUESTER_PROCESS)
7655  *
7656  *          before sending the notification, get the req total and
7657  *          functional currency which will be used in notification
7658  *
7659 ************************************************************************/
7660 PROCEDURE Get_Total_Amount_Currency(itemtype        in varchar2,
7661                                     itemkey         in varchar2,
7662                                     actid           in number,
7663                                     funcmode        in varchar2,
7664                                     resultout       out NOCOPY varchar2)
7665 IS
7666 
7667   l_document_id           NUMBER;
7668   l_orgid                 NUMBER;
7669   l_req_item_key          wf_items.item_key%type;
7670   l_req_item_type         wf_items.item_type%type;
7671   x_progress              varchar2(100);
7672   l_functional_currency varchar2(200);
7673   l_total_amount_dsp varchar2(100);
7674   t_varname   Wf_Engine.NameTabTyp;
7675   t_varval    Wf_Engine.TextTabTyp;
7676   l_doc_string varchar2(200);
7677   l_preparer_user_name varchar2(100);
7678   l_note po_action_history.note%TYPE;
7679   l_old_amount_currency varchar2(40);
7680   l_old_tax_currency varchar2(40);
7681   l_new_amount_currency number;
7682   L_NEW_TAX_CURRENCY number;
7683   l_req_amount_disp   varchar2(60);
7684   l_tax_amount_disp   varchar2(60);
7685   l_total_amount_disp varchar2(60);
7686   l_amount_for_subject varchar2(400);
7687   l_amount_for_header varchar2(400);
7688   l_amount_for_tax varchar2(400);
7689   l_contractor_req_flag varchar2(30);
7690   l_is_ame_approval      varchar2(30);
7691   l_req_change_group_id number;
7692   l_preparer_display_name varchar2(80);
7693 
7694   l_rco_wf_available varchar2(5):='N';
7695   l_req_amount number;
7696   l_tax_amount number;
7697   l_total_amount number;
7698 
7699 BEGIN
7700 
7701   x_progress := 'PO_ReqChangeRequestWF_PVT.Get_Total_Amount_Currency: 01';
7702 
7703   -- Do nothing in cancel or timeout mode
7704   if (funcmode <> wf_engine.eng_run) then
7705       resultout := wf_engine.eng_null;
7706       return;
7707   end if;
7708 
7709   -- Set the multi-org context
7710   l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
7711                                          itemkey  => itemkey,
7712                                          aname    => 'ORG_ID');
7713 
7714   IF l_orgid is NOT NULL THEN
7715       PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
7716   END IF;
7717 
7718   x_progress := 'PO_ReqChangeRequestWF_PVT.Get_Total_Amount_Currency: 02';
7719 
7720   l_req_change_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber
7721                                         (itemtype   => itemtype,
7722                                          itemkey    => itemkey,
7723                                          aname      => 'REQ_CHANGE_GROUP_ID');
7724 
7725   x_progress := 'PO_ReqChangeRequestWF_PVT.Get_Total_Amount_Currency: 03';
7726 
7727   SELECT wf_item_type, wf_item_key
7728   INTO l_req_item_type, l_req_item_key
7729   FROM po_change_requests
7730   WHERE
7731     change_request_group_id = l_req_change_group_id and rownum=1;
7732 
7733   --bug 5379796,if POREQCHA wf has been purged before this moment,
7734   -- we can't get notification related attributes from  POREQCHA wf,
7735   -- here we check the availability of POREQCHA wf.
7736   BEGIN
7737    select 'Y'
7738    into l_rco_wf_available
7739    from wf_items
7740    where item_type = l_req_item_type
7741    and item_key = l_req_item_key;
7742 
7743   EXCEPTION
7744   when others then
7745     l_rco_wf_available:= 'N';
7746   END;
7747 
7748    x_progress := 'PO_ReqChangeRequestWF_PVT.Get_Total_Amount_Currency: 02';
7749 
7750   -- If POREQCHA wf is available, we can get attributes from that wf.
7751   if ( l_rco_wf_available = 'Y') then
7752 
7753   x_progress := 'PO_ReqChangeRequestWF_PVT.Get_Total_Amount_Currency: 04:' || l_req_item_type || '-' || l_req_item_key;
7754 
7755   l_total_amount_dsp:= PO_WF_UTIL_PKG.GetItemAttrText(itemtype=>l_req_item_type,
7756                               itemkey=>l_req_item_key,
7757                               aname =>'TOTAL_AMOUNT_DSP');
7758 
7759   x_progress := 'PO_ReqChangeRequestWF_PVT.Get_Total_Amount_Currency: 05:';
7760 
7761   l_functional_currency:= PO_WF_UTIL_PKG.GetItemAttrText(itemtype=>l_req_item_type,
7762                               itemkey=>l_req_item_key,
7763                               aname =>'FUNCTIONAL_CURRENCY');
7764 
7765   x_progress := 'PO_ReqChangeRequestWF_PVT.Get_Total_Amount_Currency: 06:';
7766 
7767   l_document_id := PO_WF_UTIL_PKG.GetItemAttrNumber
7768                                         (itemtype   => l_req_item_type,
7769                                          itemkey    => l_req_item_key,
7770                                          aname      => 'DOCUMENT_ID');
7771 
7772   l_is_ame_approval:= PO_WF_UTIL_PKG.GetItemAttrText(itemtype=>l_req_item_type,
7773                               itemkey=>l_req_item_key,
7774                               aname =>'IS_AME_APPROVAL');
7775 
7776   l_contractor_req_flag:= PO_WF_UTIL_PKG.GetItemAttrText(itemtype=>l_req_item_type,
7777                               itemkey=>l_req_item_key,
7778                               aname =>'CONTRACTOR_REQUISITION_FLAG');
7779 
7780   l_req_change_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber
7781                                         (itemtype   => l_req_item_type,
7782                                          itemkey    => l_req_item_key,
7783                                          aname      => 'CHANGE_REQUEST_GROUP_ID');
7784 
7785   PO_WF_UTIL_PKG.SetItemAttrText ( itemtype   => itemtype,
7786                                    itemkey    => itemkey,
7787                                    aname      => 'IS_AME_APPROVAL',
7788                                    avalue     => l_is_ame_approval);
7789 
7790   PO_WF_UTIL_PKG.SetItemAttrText ( itemtype   => itemtype,
7791                                    itemkey    => itemkey,
7792                                    aname      => 'CONTRACTOR_REQUISITION_FLAG',
7793                                    avalue     => l_contractor_req_flag);
7794 
7795   t_varname(1) := 'TOTAL_AMOUNT_DSP';
7796   t_varval(1)  := l_total_amount_dsp;
7797   t_varname(2) := 'FUNCTIONAL_CURRENCY';
7798   t_varval(2)  := l_functional_currency;
7799 
7800   l_note := PO_WF_UTIL_PKG.GetItemAttrText
7801                                 (itemtype   => l_req_item_type,
7802                                 itemkey    => l_req_item_key,
7803                                 aname      => 'JUSTIFICATION');
7804 
7805   l_old_amount_currency := PO_WF_UTIL_PKG.GetItemAttrText
7806                                 ( itemtype   => l_req_item_type,
7807                                   itemkey    => l_req_item_key,
7808                                   aname      => 'REQ_AMOUNT_CURRENCY_DSP');
7809 
7810   l_old_tax_currency:= PO_WF_UTIL_PKG.GetItemAttrText
7811                                 ( itemtype   => l_req_item_type,
7812                                   itemkey    => l_req_item_key,
7813                                   aname      => 'TAX_AMOUNT_CURRENCY_DSP');
7814 
7815   SELECT nvl(sum(nvl(nonrecoverable_tax, 0)), 0)
7816     INTO l_new_tax_currency
7817     FROM po_requisition_lines_all rl,
7818          po_req_distributions_all rd
7819    WHERE rl.requisition_header_id = l_document_id
7820      AND rd.requisition_line_id = rl.requisition_line_id
7821      AND  NVL(rl.modified_by_agent_flag, 'N') = 'N'
7822      and NVL(rl.cancel_flag, 'N')='N';
7823 
7824    SELECT nvl(SUM(nvl(decode(matching_basis, 'AMOUNT', amount, quantity * unit_price), 0)), 0)
7825    into l_new_amount_currency
7826    FROM   po_requisition_lines_all
7827    WHERE  requisition_header_id = l_document_id
7828      AND  NVL(cancel_flag,'N') = 'N'
7829      AND  NVL(modified_by_agent_flag, 'N') = 'N';
7830 
7831 
7832   /* FPJ
7833      support approval currency in notification header and subject
7834      because TOTAL_AMOUNT_DSP is only used in notification,
7835      this bug fix changes the meaning of this attribute from total to
7836      total with currency;
7837      the workflow definition is modified such that
7838      currency atribute is removed from the subject.
7839    */
7840   l_total_amount_disp := to_char(l_new_tax_currency+l_new_amount_currency, FND_CURRENCY.GET_FORMAT_MASK(l_functional_currency,g_currency_format_mask));
7841   l_req_amount_disp := to_char(l_new_amount_currency, FND_CURRENCY.GET_FORMAT_MASK(l_functional_currency,g_currency_format_mask));
7842   l_tax_amount_disp := to_char(l_new_tax_currency, FND_CURRENCY.GET_FORMAT_MASK(l_functional_currency,g_currency_format_mask));
7843 
7844 
7845   getReqAmountInfo(itemtype => itemtype,
7846                           itemkey => itemkey,
7847                           p_function_currency => l_functional_currency,
7848                           p_total_amount_disp => l_total_amount_disp,
7849                           p_total_amount => l_new_tax_currency+l_new_amount_currency,
7850                           p_req_amount_disp => l_req_amount_disp,
7851                           p_req_amount => l_new_amount_currency,
7852                           p_tax_amount_disp => l_tax_amount_disp,
7853                           p_tax_amount => l_new_tax_currency,
7854                           x_amount_for_subject => l_amount_for_subject,
7855                           x_amount_for_header => l_amount_for_header,
7856                           x_amount_for_tax => l_amount_for_tax);
7857 
7858   l_preparer_display_name := PO_WF_UTIL_PKG.GetItemAttrText
7859                                ( itemtype   => l_req_item_type,
7860                                  itemkey    => l_req_item_key,
7861                                  aname      => 'PREPARER_DISPLAY_NAME' );
7862 
7863   l_preparer_user_name := PO_WF_UTIL_PKG.GetItemAttrText
7864                                ( itemtype   => l_req_item_type,
7865                                  itemkey    => l_req_item_key,
7866                                  aname      => 'PREPARER_USER_NAME' );
7867 
7868   t_varname(3) := 'JUSTIFICATION';
7869   t_varval(3)  := l_note;
7870   t_varname(4) := 'REQ_AMOUNT_CURRENCY_DSP';
7871   t_varval(4)  := l_old_amount_currency;
7872   t_varname(5) := 'NEW_REQ_AMOUNT_CURRENCY_DSP';
7873   t_varval(5)  := l_amount_for_header;
7874 
7875   t_varname(6) := 'TAX_AMOUNT_CURRENCY_DSP';
7876   t_varval(6)  := l_old_tax_currency;
7877   t_varname(7) := 'NEW_TAX_AMOUNT_CURRENCY_DSP';
7878   t_varval(7)  := l_amount_for_tax;
7879 
7880   t_varname(8) := 'NEW_TOTAL_AMOUNT_DSP';
7881   t_varval(8)  := l_amount_for_subject;
7882 
7883   t_varname(9) := 'PREPARER_DISPLAY_NAME';
7884   t_varval(9)  := l_preparer_display_name;
7885 
7886   t_varname(10) := 'PREPARER_USER_NAME';
7887   t_varval(10)  := l_preparer_user_name;
7888 
7889   Wf_Engine.SetItemAttrTextArray(itemtype, itemkey,t_varname,t_varval);
7890 
7891   x_progress := 'PO_REQAPPROVAL_INIT1.Get_Total_Amount_Currency: 04';
7892 
7893   --bug 5379796, if POREQCHA is not available, we need to get notification related
7894   --attributes from other places ( pcr table, po_headers,po_lines etc.)
7895   --Note: three attributes (REQ_AMOUNT_CURRENCY_DSP,TAX_AMOUNT_CURRENCY_DSP, TOTAL_AMOUNT_DSP) are set in StartPOChangeWF before kicking off the 'buyer response' wf,
7896   --since these three attibutes are old amount information and should be fetched before req gets updated.
7897 
7898   else
7899      SELECT document_header_id
7900      INTO l_document_id
7901      FROM po_change_requests pcr
7902      WHERE pcr.change_request_group_id = l_req_change_group_id and rownum=1;
7903 
7904      PO_WF_UTIL_PKG.SetItemAttrNumber ( itemtype   => itemtype,
7905                                         itemkey    => itemkey,
7906                                         aname      => 'REQ_HEADER_ID',
7907                                         avalue     => l_document_id);
7908 
7909      x_progress := 'PO_ReqChangeRequestWF_PVT.Get_Total_Amount_Currency: 05';
7910 
7911      select NVL(CONTRACTOR_REQUISITION_FLAG, 'N'), NOTE_TO_AUTHORIZER
7912      into   l_contractor_req_flag,l_note
7913      from po_requisition_headers_all
7914      where REQUISITION_HEADER_ID = l_document_id;
7915 
7916      t_varname(1) := 'CONTRACTOR_REQUISITION_FLAG';
7917      t_varval(1)  := l_contractor_req_flag ;
7918 
7919      t_varname(2) := 'JUSTIFICATION';
7920      t_varval(2)  := l_note ;
7921 
7922      x_progress := 'PO_ReqChangeRequestWF_PVT.Get_Total_Amount_Currency: 06';
7923 
7924      SELECT gsb.currency_code
7925      INTO   l_functional_currency
7926      FROM   financials_system_params_all fsp,
7927             gl_sets_of_books gsb
7928      WHERE  fsp.set_of_books_id = gsb.set_of_books_id
7929        AND  fsp.org_id = l_orgid;
7930 
7931      t_varname(3) := 'FUNCTIONAL_CURRENCY' ;
7932      t_varval(3)  := l_functional_currency ;
7933 
7934       x_progress := 'PO_ReqChangeRequestWF_PVT.Get_Total_Amount_Currency: 07';
7935 
7936      -- get new req amount and req amount disp (at this moment, req has already been updated)
7937      SELECT nvl(SUM(nvl(decode(matching_basis, 'AMOUNT', amount, quantity * unit_price), 0)), 0)
7938      into l_req_amount
7939      FROM   po_requisition_lines_all
7940      WHERE  requisition_header_id = l_document_id
7941      AND  NVL(cancel_flag,'N') = 'N'
7942      AND  NVL(modified_by_agent_flag, 'N') = 'N';
7943 
7944      l_req_amount_disp := TO_CHAR(l_req_amount,FND_CURRENCY.GET_FORMAT_MASK(
7945                                        l_functional_currency, g_currency_format_mask));
7946 
7947      -- get new tax and tax disp
7948       SELECT nvl(sum(nonrecoverable_tax), 0)
7949       into l_tax_amount
7950       FROM   po_requisition_lines_all rl,
7951           po_req_distributions_all rd
7952       WHERE  rl.requisition_header_id = l_document_id
7953       AND  rd.requisition_line_id = rl.requisition_line_id
7954       AND  NVL(rl.cancel_flag,'N') = 'N'
7955       AND  NVL(rl.modified_by_agent_flag, 'N') = 'N';
7956 
7957       l_tax_amount_disp := TO_CHAR(l_tax_amount,FND_CURRENCY.GET_FORMAT_MASK(
7958                                        l_functional_currency, g_currency_format_mask));
7959 
7960       l_total_amount_disp := to_char(l_req_amount+l_tax_amount, FND_CURRENCY.GET_FORMAT_MASK(l_functional_currency,g_currency_format_mask));
7961 
7962        getReqAmountInfo(itemtype => itemtype,
7963                           itemkey => itemkey,
7964                           p_function_currency => l_functional_currency,
7965                           p_total_amount_disp => l_total_amount_disp,
7966                           p_total_amount => l_req_amount+l_tax_amount,
7967                           p_req_amount_disp => l_req_amount_disp,
7968                           p_req_amount => l_req_amount,
7969                           p_tax_amount_disp => l_tax_amount_disp,
7970                           p_tax_amount => l_tax_amount,
7971                           x_amount_for_subject => l_amount_for_subject,
7972                           x_amount_for_header => l_amount_for_header,
7973                           x_amount_for_tax => l_amount_for_tax);
7974 
7975         t_varname(4) := 'NEW_REQ_AMOUNT_CURRENCY_DSP' ;
7976         t_varval(4)  := l_amount_for_header ;
7977 
7978         t_varname(5) := 'NEW_TAX_AMOUNT_CURRENCY_DSP' ;
7979         t_varval(5)  := l_amount_for_tax ;
7980 
7981         t_varname(6) := 'NEW_TOTAL_AMOUNT_DSP';
7982         t_varval(6)  := l_functional_currency ;
7983 
7984         Wf_Engine.SetItemAttrTextArray(itemtype, itemkey,t_varname,t_varval);
7985 
7986         x_progress := 'PO_ReqChangeRequestWF_PVT.Get_Total_Amount_Currency: 08';
7987 
7988   end if;
7989   --
7990   resultout := wf_engine.eng_completed || ':' ||  'ACTIVITY_PERFORMED';
7991   --
7992 
7993   x_progress := 'PO_REQAPPROVAL_INIT1.Get_Total_Amount_Currency: 02';
7994 
7995 
7996 EXCEPTION
7997   WHEN OTHERS THEN
7998     l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
7999     l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(itemType, itemkey);
8000     wf_core.context('PO_ReqChangeRequestWF_PVT','Get_Total_Amount_Currency',x_progress);
8001     PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey, l_preparer_user_name, l_doc_string, sqlerrm, 'PO_ReqChangeRequestWF_PVT.Get_Total_Amount_Currency');
8002     raise;
8003 
8004 END Get_Total_Amount_Currency;
8005 
8006 /*
8007 procedure REQ_PO_CHANGE_RESPONDED(itemtype        in varchar2,
8008                                 itemkey         in varchar2,
8009                                 actid           in number,
8010                                 funcmode        in varchar2,
8011                                 resultout       out NOCOPY varchar2    ) is
8012 l_orgid       number;
8013 l_change_request_group_id number;
8014 l_parent_request_group_id number;
8015 
8016 x_progress              varchar2(100);
8017 l_change_request_id  number;
8018 
8019 l_doc_string varchar2(200);
8020 l_preparer_user_name varchar2(100);
8021 
8022 cursor parent_change(p_change_request_group_id number) is
8023     select pcr1.change_request_group_id
8024     from po_change_requests pcr1, po_change_requests pcr2
8025     where pcr2.change_request_group_id=p_change_request_group_id
8026         and pcr2.parent_change_request_id=pcr1.change_request_id;
8027 
8028 -- this is call from child po, check for parent req
8029 cursor pending_change(p_change_request_group_id number) is
8030     select pcr1.change_request_id
8031     from po_change_requests pcr1
8032     where pcr1.change_request_group_id=p_change_request_group_id
8033         and pcr1.request_status not in ('ACCEPTED', 'REJECTED');
8034 
8035 cursor pending_child_change(p_change_request_group_id number) is
8036     select pcr1.change_request_id
8037     from po_change_requests pcr1, po_change_requests pcr2
8038     where pcr2.change_request_group_id=p_change_request_group_id
8039         and pcr1.parent_change_request_id=pcr2.change_request_id
8040         and pcr1.request_status not in ('ACCEPTED', 'REJECTED');
8041 
8042 BEGIN
8043 
8044   x_progress := 'PO_ReqChangeRequestWF_PVT.Req_Change_Responded';
8045 
8046 
8047   -- Do nothing in cancel or timeout mode
8048   if (funcmode <> wf_engine.eng_run) then
8049 
8050       resultout := wf_engine.eng_null;
8051       return;
8052 
8053   end if;
8054 
8055 
8056   l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
8057                                          itemkey  => itemkey,
8058                                          aname    => 'CHANGE_REQUEST_GROUP_ID');
8059 
8060   -- Set the multi-org context
8061   l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
8062                                          itemkey  => itemkey,
8063                                          aname    => 'ORG_ID');
8064 
8065   IF l_orgid is NOT NULL THEN
8066 
8067     PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
8068 
8069   END IF;
8070 
8071   open parent_change(l_change_request_group_id);
8072   fetch parent_change into l_parent_request_group_id;
8073   close parent_change;
8074 
8075   open pending_change(l_parent_request_group_id);
8076   fetch pending_change into l_change_request_id;
8077   close pending_change;
8078 
8079   if(l_change_request_id is null) then
8080 
8081     open pending_child_change(l_parent_request_group_id);
8082     fetch pending_child_change into l_change_request_id;
8083     close pending_child_change;
8084 
8085     if(l_change_request_id is null) then
8086 
8087       resultout := wf_engine.eng_completed || ':' || 'Y' ;
8088     else
8089       resultout := wf_engine.eng_completed || ':' || 'N' ;
8090     end if;
8091   else
8092     resultout := wf_engine.eng_completed || ':' || 'N' ;
8093   end if;
8094 
8095   x_progress := 'PO_ReqChangeRequestWF_PVT.REQ_PO_CHANGE_RESPONDED: 02';
8096 
8097 EXCEPTION
8098   WHEN OTHERS THEN
8099     l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
8100     l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(itemType, itemkey);
8101     wf_core.context('PO_ReqChangeRequestWF_PVT','REQ_PO_CHANGE_RESPONDED',x_progress);
8102     PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey, l_preparer_user_name, l_doc_string, sqlerrm, 'PO_ReqChangeRequestWF_PVT.REQ_PO_CHANGE_RESPONDED');
8103     raise;
8104 
8105 
8106 end REQ_PO_CHANGE_RESPONDED;
8107 */
8108 
8109 
8110 
8111 
8112 
8113 /*************************************************************************
8114  *
8115  * Public Procedure: Get_Change_Attribute
8116  * Effects: workflow procedure, used in PORPOCHA(RECEIVE_REQ_CHANGE_EVENT)
8117  *
8118  *          when we get the event, new workflow process
8119  *          RECEIVE_REQ_CHANGE_EVENT is started. We set the attribute in
8120  *          this node
8121  *
8122 ************************************************************************/
8123 procedure Get_Change_Attribute(itemtype        in varchar2,
8124                                 itemkey         in varchar2,
8125                                 actid           in number,
8126                                 funcmode        in varchar2,
8127                                 resultout       out NOCOPY varchar2    )
8128 IS
8129 l_document_id number;
8130 l_document_type varchar2(240);
8131 l_document_subtype varchar2(240);
8132 l_change_request_group_id number;
8133 l_parent_group_id number;
8134 l_seq number;
8135 l_item_key varchar2(240);
8136 x_progress varchar2(3) := '000';
8137 l_requester_id number;
8138 l_requester_name wf_roles.name%type;
8139 l_requester_display_name wf_roles.display_name%type;
8140 l_po_chg_itemtype wf_items.item_type%type;
8141 l_po_chg_itemkey wf_items.item_key%type;
8142 
8143 cursor l_get_group_id_po_csr is
8144     select change_request_group_id, wf_item_type, wf_item_key
8145         from po_change_requests
8146         where document_header_id=l_document_id
8147             and document_type=l_document_type
8148             and initiator='REQUESTER'
8149             and request_status in ('PENDING', 'BUYER_APP');
8150 cursor l_get_group_id_rel_csr is
8151     select change_request_group_id, wf_item_type, wf_item_key
8152         from po_change_requests
8153         where po_release_id=l_document_id
8154             and document_type=l_document_type
8155             and initiator='REQUESTER'
8156             and request_status in ('PENDING', 'BUYER_APP');
8157 cursor l_get_parent_group_id_csr is
8158     select change_request_group_id
8159         from po_change_requests
8160         where change_request_id in
8161             (select parent_change_request_id
8162                 from po_change_requests
8163                 where change_request_group_id=l_change_request_group_id);
8164 cursor change_request(l_change_request_group_id number) is
8165     select nvl(pcr.requester_id, por.preparer_id)
8166     from po_change_requests pcr, po_requisition_headers_all por
8167     where pcr.change_request_group_id=l_change_request_group_id
8168           and pcr.document_header_id=por.requisition_header_id;
8169 
8170 BEGIN
8171     l_document_id := PO_WF_UTIL_PKG.GetItemAttrNumber(itemtype => itemtype,
8172                                             itemkey => itemkey,
8173                                             aname => 'DOCUMENT_ID');
8174     l_document_type := PO_WF_UTIL_PKG.GetItemAttrText(itemtype => itemtype,
8175                                             itemkey => itemkey,
8176                                             aname => 'DOCUMENT_TYPE');
8177 
8178     if (l_document_type = 'RELEASE' ) then
8179         open l_get_group_id_rel_csr;
8180         fetch l_get_group_id_rel_csr
8181         into l_change_request_group_id, l_po_chg_itemtype, l_po_chg_itemkey;
8182         close l_get_group_id_rel_csr;
8183         l_document_subtype := 'BLANKET';
8184     else
8185         open l_get_group_id_po_csr;
8186         fetch l_get_group_id_po_csr
8187         into l_change_request_group_id, l_po_chg_itemtype, l_po_chg_itemkey;
8188         close l_get_group_id_po_csr;
8189         l_document_subtype := 'STANDARD';
8190     end if;
8191 
8192     PO_WF_UTIL_PKG.SetItemAttrText(itemtype => itemtype,
8193                             itemkey  => itemkey,
8194                             aname    => 'DOCUMENT_SUBTYPE',
8195                             avalue   =>l_document_subtype);
8196 
8197     PO_WF_UTIL_PKG.SetItemAttrNumber( itemtype   => itemtype,
8198                                  itemkey    => itemkey,
8199                                  aname      => 'CHANGE_REQUEST_GROUP_ID',
8200                                  avalue     => l_change_request_group_id);
8201 
8202     open l_get_parent_group_id_csr;
8203     fetch l_get_parent_group_id_csr into l_parent_group_id;
8204     close l_get_parent_group_id_csr;
8205 
8206     open change_request(l_parent_group_id);
8207     fetch change_request into l_requester_id;
8208     close change_request;
8209 
8210     PO_REQAPPROVAL_INIT1.get_user_name(l_requester_id, l_requester_name, l_requester_display_name);
8211 
8212     PO_WF_UTIL_PKG.SetItemAttrText(itemtype => itemtype,
8213                             itemkey  => itemkey,
8214                             aname    => 'PREPARER_USER_NAME',
8215                             avalue   =>l_requester_name);
8216 
8217     PO_WF_UTIL_PKG.SetItemAttrText(itemtype => itemtype,
8218                             itemkey  => itemkey,
8219                             aname    => 'REQ_CHANGE_RESPONSE_NOTIF_BODY',
8220                             avalue   =>
8221                          'plsqlclob:PO_ReqChangeRequestNotif_PVT.get_req_chg_response_notif/'||
8222                          to_char(l_parent_group_id));
8223 
8224 exception when others then
8225     wf_core.context('PO_ReqChangeRequestWF_PVT','Get_Change_Attribute',x_progress);
8226     raise;
8227 END Get_Change_Attribute;
8228 
8229 
8230 
8231 /*************************************************************************
8232  *
8233  * Public Procedure: New_PO_Change_Exists
8234  * Effects: workflow procedure, used in PORPOCHA(RECEIVE_REQ_CHANGE_EVENT)
8235  *
8236  *          when the po approval workflow raise the event because of pending
8237  *          requester change request exists for the document, check if the
8238  *          change request is in new status or buyer_app status.
8239  *
8240 ************************************************************************/
8241 procedure New_PO_Change_Exists(itemtype        in varchar2,
8242                                 itemkey         in varchar2,
8243                                 actid           in number,
8244                                 funcmode        in varchar2,
8245                                 resultout       out NOCOPY varchar2    )
8246 is
8247 l_orgid       number;
8248 l_change_request_group_id number;
8249 l_document_id number;
8250 l_document_type varchar2(100);
8251 
8252 x_progress              varchar2(100);
8253 l_change_request_id  number;
8254 
8255 l_doc_string varchar2(200);
8256 l_preparer_user_name varchar2(100);
8257 
8258 cursor new_change(p_change_request_group_id number) is
8259     select change_request_id
8260     from po_change_requests
8261     where change_request_group_id=p_change_request_group_id
8262         and request_status='PENDING';
8263 
8264 BEGIN
8265 
8266   x_progress := 'PO_ReqChangeRequestWF_PVT.New_PO_Change_Exists';
8267 
8268   -- Do nothing in cancel or timeout mode
8269   if (funcmode <> wf_engine.eng_run) then
8270       resultout := wf_engine.eng_null;
8271       return;
8272   end if;
8273 
8274   l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
8275                                          itemkey  => itemkey,
8276                                          aname    => 'CHANGE_REQUEST_GROUP_ID');
8277 
8278   -- Set the multi-org context
8279   l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
8280                                          itemkey  => itemkey,
8281                                          aname    => 'ORG_ID');
8282 
8283   IF l_orgid is NOT NULL THEN
8284 
8285     PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
8286 
8287   END IF;
8288 
8289   open new_change(l_change_request_group_id);
8290   fetch new_change into l_change_request_id;
8291   close new_change;
8292 
8293   if(l_change_request_id is null) then
8294     l_document_id := PO_WF_UTIL_PKG.GetItemAttrNumber(itemtype => itemtype,
8295                                             itemkey => itemkey,
8296                                             aname => 'DOCUMENT_ID');
8297     l_document_type := PO_WF_UTIL_PKG.GetItemAttrText(itemtype => itemtype,
8298                                             itemkey => itemkey,
8299                                             aname => 'DOCUMENT_TYPE');
8300 
8301     --Moved the update to po_headers/po_releases to an autonomous transaction procedure UpdatePODocheaderTables
8302     --to avoid deadlock in POAPPRV workflow while updating the Order status for a change request response.
8303     UpdatePODocHeaderTables(l_document_type, l_document_id);
8304 
8305     resultout := wf_engine.eng_completed || ':' || 'N' ;
8306   else
8307     resultout := wf_engine.eng_completed || ':' || 'Y' ;
8308   end if;
8309 
8310   x_progress := 'PO_ReqChangeRequestWF_PVT.New_PO_Change_Exists: 02';
8311 
8312 EXCEPTION
8313   WHEN OTHERS THEN
8314     l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
8315     l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(itemType, itemkey);
8316     wf_core.context('PO_ReqChangeRequestWF_PVT','New_PO_Change_Exists',x_progress);
8317     PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey, l_preparer_user_name, l_doc_string, sqlerrm, 'PO_ReqChangeRequestWF_PVT.New_PO_Change_Exists');
8318     raise;
8319 
8320 
8321 end New_PO_Change_Exists;
8322 
8323 
8324 /*************************************************************************
8325  *
8326  * Public Procedure: Record_PO_Approval
8327  * Effects: workflow procedure, used in PORPOCHA(RECEIVE_REQ_CHANGE_EVENT)
8328  *
8329  *          when the po is approved, update the status of the corresponding
8330  *          PO change requests to ACCEPTED
8331  *
8332 ************************************************************************/
8333 procedure Record_PO_Approval(itemtype        in varchar2,
8334                                 itemkey         in varchar2,
8335                                 actid           in number,
8336                                 funcmode        in varchar2,
8337                                 resultout       out NOCOPY varchar2    ) is
8338 l_orgid       number;
8339 l_change_request_group_id number;
8340 
8341 x_progress              varchar2(100);
8342 
8343 l_doc_string varchar2(200);
8344 l_preparer_user_name varchar2(100);
8345 
8346 BEGIN
8347 
8348   x_progress := 'PO_ReqChangeRequestWF_PVT.Record_PO_Approval';
8349 
8350 
8351   -- Do nothing in cancel or timeout mode
8352   if (funcmode <> wf_engine.eng_run) then
8353 
8354       resultout := wf_engine.eng_null;
8355       return;
8356 
8357   end if;
8358 
8359 
8360   l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
8361                                          itemkey  => itemkey,
8362                                          aname    => 'CHANGE_REQUEST_GROUP_ID');
8363 
8364   -- Set the multi-org context
8365   l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
8366                                          itemkey  => itemkey,
8367                                          aname    => 'ORG_ID');
8368 
8369   IF l_orgid is NOT NULL THEN
8370 
8371     PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
8372 
8373   END IF;
8374   SetPoRequestStatus(l_change_request_group_id, 'ACCEPTED');
8375 
8376   x_progress := 'PO_ReqChangeRequestWF_PVT.Record_PO_Approval: 02';
8377 
8378 EXCEPTION
8379   WHEN OTHERS THEN
8380     l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
8381     l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(itemType, itemkey);
8382     wf_core.context('PO_ReqChangeRequestWF_PVT','Record_PO_Approval',x_progress);
8383     PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey, l_preparer_user_name, l_doc_string, sqlerrm, 'PO_ReqChangeRequestWF_PVT.Record_PO_Approval');
8384     raise;
8385 
8386 
8387 end Record_PO_Approval;
8388 
8389 
8390 /*************************************************************************
8391  *
8392  * Public Procedure: Record_PO_Rejection
8393  * Effects: workflow procedure, used in PORPOCHA(RECEIVE_REQ_CHANGE_EVENT)
8394  *
8395  *          when the po is rejected, update the status of the corresponding
8396  *          PO change requests to REJECTED
8397  *
8398 ************************************************************************/
8399 procedure Record_PO_Rejection(itemtype        in varchar2,
8400                                 itemkey         in varchar2,
8401                                 actid           in number,
8402                                 funcmode        in varchar2,
8403                                 resultout       out NOCOPY varchar2    )is
8404 l_orgid       number;
8405 l_change_request_group_id number;
8406 
8407 x_progress              varchar2(100);
8408 
8409 l_doc_string varchar2(200);
8410 l_preparer_user_name varchar2(100);
8411 
8412 BEGIN
8413 
8414   x_progress := 'PO_ReqChangeRequestWF_PVT.Record_PO_Rejection';
8415 
8416 
8417   -- Do nothing in cancel or timeout mode
8418   if (funcmode <> wf_engine.eng_run) then
8419 
8420       resultout := wf_engine.eng_null;
8421       return;
8422 
8423   end if;
8424 
8425 
8426   l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
8427                                          itemkey  => itemkey,
8428                                          aname    => 'CHANGE_REQUEST_GROUP_ID');
8429 
8430   -- Set the multi-org context
8431   l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
8432                                          itemkey  => itemkey,
8433                                          aname    => 'ORG_ID');
8434 
8435   IF l_orgid is NOT NULL THEN
8436 
8437     PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
8438 
8439   END IF;
8440   SetPoRequestStatus(l_change_request_group_id, 'REJECTED');
8441 
8442   x_progress := 'PO_ReqChangeRequestWF_PVT.Record_PO_Rejection: 02';
8443 
8444 EXCEPTION
8445   WHEN OTHERS THEN
8446     l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
8447     l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(itemType, itemkey);
8448     wf_core.context('PO_ReqChangeRequestWF_PVT','Record_PO_Rejection',x_progress);
8449     PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey, l_preparer_user_name, l_doc_string, sqlerrm, 'PO_ReqChangeRequestWF_PVT.Record_PO_Rejection');
8450     raise;
8451 
8452 
8453 end Record_PO_Rejection;
8454 
8455 
8456 
8457 
8458 /*************************************************************************
8459  *
8460  * Public Procedure: Validate_Chg_Against_New_PO
8461  * Effects: workflow procedure, used in PORPOCHA(RECEIVE_REQ_CHANGE_EVENT)
8462  *
8463  *          when the po is approved, and if there is new requester PO change
8464  *          request, check if the new PO change request is already in the
8465  *          PO
8466  *
8467 ************************************************************************/
8468 procedure Validate_Chg_Against_New_PO(itemtype        in varchar2,
8469                                 itemkey         in varchar2,
8470                                 actid           in number,
8471                                 funcmode        in varchar2,
8472                                 resultout       out NOCOPY varchar2    )is
8473 l_orgid       number;
8474 l_change_request_group_id number;
8475 
8476 x_progress              varchar2(100);
8477 
8478 l_doc_string varchar2(200);
8479 l_preparer_user_name varchar2(100);
8480 
8481 BEGIN
8482 
8483   x_progress := 'PO_ReqChangeRequestWF_PVT.Validate_Chg_Against_New_PO';
8484 
8485 
8486   -- Do nothing in cancel or timeout mode
8487   if (funcmode <> wf_engine.eng_run) then
8488 
8489       resultout := wf_engine.eng_null;
8490       return;
8491 
8492   end if;
8493 
8494 
8495   l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
8496                                          itemkey  => itemkey,
8497                                          aname    => 'CHANGE_REQUEST_GROUP_ID');
8498 
8499   -- Set the multi-org context
8500   l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
8501                                          itemkey  => itemkey,
8502                                          aname    => 'ORG_ID');
8503 
8504   IF l_orgid is NOT NULL THEN
8505 
8506     PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
8507 
8508   END IF;
8509   ValidateChgAgainstNewPO(l_change_request_group_id);
8510 
8511   x_progress := 'PO_ReqChangeRequestWF_PVT.Validate_Chg_Against_New_PO: 02';
8512 
8513 EXCEPTION
8514   WHEN OTHERS THEN
8515     l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
8516     l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(itemType, itemkey);
8517     wf_core.context('PO_ReqChangeRequestWF_PVT','Validate_Chg_Against_New_PO',x_progress);
8518     PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey, l_preparer_user_name, l_doc_string, sqlerrm, 'PO_ReqChangeRequestWF_PVT.Validate_Chg_Against_New_PO');    raise;
8519 
8520 
8521 end Validate_Chg_Against_New_PO;
8522 
8523 
8524 
8525 /*************************************************************************
8526  *
8527  * Public Procedure: Any_Requester_Change
8528  * Effects: workflow procedure, used in POAPPRV workflow
8529  *
8530  *          when a new revision PO is approved or rejected, check if there
8531  *          is pending requester change request for that PO. If so, we need
8532  *          raise event.
8533  *
8534 ************************************************************************/
8535 procedure Any_Requester_Change(itemtype        in varchar2,
8536                                 itemkey         in varchar2,
8537                                 actid           in number,
8538                                 funcmode        in varchar2,
8539                                 resultout       out NOCOPY varchar2    )
8540 IS
8541 l_document_id number;
8542 l_document_type varchar2(240);
8543 l_change_request_id number:=null;
8544 x_progress varchar2(3) := '000';
8545 cursor l_pending_req_po_chg_csr is
8546     select change_request_id
8547         from po_change_requests
8548         where document_header_id=l_document_id
8549             and request_status in ('BUYER_APP', 'PENDING')
8550             and initiator='REQUESTER'
8551             and document_type='PO';
8552 cursor l_pending_req_rel_chg_csr is
8553     select change_request_id
8554         from po_change_requests
8555         where po_release_id=l_document_id
8556             and request_status in ('BUYER_APP', 'PENDING')
8557             and initiator='REQUESTER'
8558             and document_type='RELEASE';
8559 BEGIN
8560 
8561     l_document_id := PO_WF_UTIL_PKG.GetItemAttrNumber(itemtype => itemtype,
8562                                             itemkey => itemkey,
8563                                             aname => 'DOCUMENT_ID');
8564     l_document_type := PO_WF_UTIL_PKG.GetItemAttrText(itemtype => itemtype,
8565                                             itemkey => itemkey,
8566                                             aname => 'DOCUMENT_TYPE');
8567 
8568     if(l_document_type = 'RELEASE') then
8569         open l_pending_req_rel_chg_csr;
8570         fetch l_pending_req_rel_chg_csr into l_change_request_id;
8571         close l_pending_req_rel_chg_csr;
8572     else
8573         open l_pending_req_po_chg_csr;
8574         fetch l_pending_req_po_chg_csr into l_change_request_id;
8575         close l_pending_req_po_chg_csr;
8576     end if;
8577     if(l_change_request_id is not null) then
8578         resultout := 'Y';
8579     else
8580         resultout := 'N';
8581     end if;
8582 
8583 exception when no_data_found then
8584     wf_core.context('PO_ReqChangeRequestWF_PVT','Any_Requester_Change','010');
8585     raise;
8586 when others then
8587     wf_core.context('PO_ReqChangeRequestWF_PVT','Any_Requester_Change',x_progress);
8588     raise;
8589 END Any_Requester_Change;
8590 
8591 /*************************************************************************
8592  *
8593  * Public Procedure: Set_Data_Req_Chn_Evt
8594  * Effects: workflow procedure, used in POAPPRV workflow
8595  *
8596  *          when we need raise the event in POAPPRV workflow, this node
8597  *          set the parameter for that event
8598  *
8599 ************************************************************************/
8600 procedure Set_Data_Req_Chn_Evt(     itemtype        in varchar2,
8601                                     itemkey         in varchar2,
8602                                     actid           in number,
8603                                     funcmode        in varchar2,
8604                                     resultout       out NOCOPY varchar2)
8605 IS
8606 l_document_id number;
8607 l_document_type varchar2(240);
8608 l_seq number;
8609 l_item_key varchar2(240);
8610 x_progress varchar2(3) := '000';
8611 BEGIN
8612     l_document_id := PO_WF_UTIL_PKG.GetItemAttrNumber(itemtype => itemtype,
8613                                             itemkey => itemkey,
8614                                             aname => 'DOCUMENT_ID');
8615     l_document_type := PO_WF_UTIL_PKG.GetItemAttrText(itemtype => itemtype,
8616                                             itemkey => itemkey,
8617                                             aname => 'DOCUMENT_TYPE');
8618 
8619     select PO_REQUESTER_CHANGE_WF_S.nextval into l_seq from dual;
8620 
8621     if(l_document_type = 'RELEASE') then
8622         l_item_key := 'RC_REL'||'-'||l_document_id||'-'||l_seq;
8623     else
8624         l_item_key := 'RC_PO'||'-'||l_document_id||'-'||l_seq;
8625     end if;
8626     PO_WF_UTIL_PKG.SetItemAttrText (   itemtype   => itemtype,
8627                                   itemkey    => itemkey,
8628                                   aname      => 'REQ_CHANGE_EVENT_KEY',
8629                                   avalue     => l_item_key );
8630 
8631 
8632 exception when others then
8633     wf_core.context('PO_ReqChangeRequestWF_PVT','set_data_sup_chn_evt',x_progress);
8634     raise;
8635 END Set_Data_Req_Chn_Evt;
8636 
8637 
8638 
8639 /*************************************************************************
8640  * Public Procedure: Process_Cancelled_Req_Lines
8641  *
8642  * Effects: This procedure is called by the PO Cancel API.
8643  *
8644  *          When there is a pending requester change request going on,
8645  *          if the PO get canceled, which cause the underlying req calceled
8646  *          also, there is no need for the requester change request to be
8647  *          approved by manager any more. We should immediately close the
8648  *          requester change request. If the request change request is
8649  *          a cancel request, we should immediately accept it, and if it is
8650  *          a change request, we should reject it.
8651  *
8652  * Returns:
8653  ************************************************************************/
8654 Procedure Process_Cancelled_Req_Lines(
8655          p_api_version in number,
8656          p_init_msg_list in varchar2:=FND_API.G_FALSE,
8657          p_commit in varchar2 :=FND_API.G_FALSE,
8658          x_return_status out NOCOPY varchar2,
8659          x_msg_count out NOCOPY number,
8660          x_msg_data out NOCOPY varchar2,
8661          p_CanceledReqLineIDs_tbl in ReqLineID_tbl_type) is
8662 
8663 l_index number;
8664 l_last number;
8665 l_change_request_id number;
8666 l_action_type PO_CHANGE_REQUESTS.ACTION_TYPE%TYPE;
8667 l_change_request_group_id number;
8668 
8669 l_wf_item_type wf_items.item_type%TYPE;
8670 l_wf_item_key wf_items.item_key%type;
8671 
8672 l_result                    BOOLEAN:=FALSE;
8673 l_action                    VARCHAR2(30)  := 'RETURN';
8674 l_document_id               NUMBER;
8675 l_note                      VARCHAR2(2000);
8676 
8677 temp number;
8678 
8679 l_api_name CONSTANT VARCHAR2(30) := 'Process_Cancelled_Req_Lines';
8680 l_api_version CONSTANT NUMBER := 1.0;
8681 
8682 cursor l_pending_change_csr(group_id in number) is
8683     select change_request_id
8684         from po_change_requests
8685         where change_request_group_id=group_id
8686             and request_status in ('NEW', 'MGR_PRE_APP');
8687 cursor l_pending_change_line_csr(requisition_line_id in number) is
8688     select pcr1.change_request_id, pcr1.document_header_id,
8689            pcr1.action_type, pcr1.change_request_group_id,
8690            pcr1.wf_item_type, pcr1.wf_item_key
8691         from po_change_requests pcr1
8692         where pcr1.document_type='REQ'
8693             and pcr1.document_line_id=requisition_line_id
8694             and pcr1.action_type <>'DERIVED'
8695             and pcr1.request_status in ('NEW', 'MGR_PRE_APP', 'MGR_APP')
8696             and not exists
8697                 (select pcr2.change_request_id
8698                  from po_change_requests pcr2
8699                  where pcr2.parent_change_request_id=pcr1.change_request_id);
8700 begin
8701     IF NOT FND_API.compatible_api_call(l_api_version, p_api_version,
8702                                        l_api_name, g_pkg_name) THEN
8703         RAISE FND_API.g_exc_unexpected_error;
8704     END IF;
8705 
8706     IF FND_API.to_boolean(p_init_msg_list) THEN
8707         FND_MSG_PUB.initialize;
8708     END IF;
8709 
8710     l_index:=p_CanceledReqLineIDs_tbl.FIRST;
8711     l_last:=p_CanceledReqLineIDs_tbl.LAST;
8712 
8713     loop
8714         l_change_request_id :=null;
8715         open l_pending_change_line_csr(p_CanceledReqLineIDs_tbl(l_index));
8716         fetch l_pending_change_line_csr
8717          into l_change_request_id,
8718               l_document_id,
8719               l_action_type,
8720               l_change_request_group_id,
8721               l_wf_item_type,
8722               l_wf_item_key;
8723         close l_pending_change_line_csr;
8724 
8725         if(l_change_request_id is not null) then
8726             if(l_action_type='CANCELLATION') then
8727             -- accept the cancellation request
8728                 update po_requisition_lines_all
8729                     set cancel_flag='Y'
8730                     where requisition_line_id=p_CanceledReqLineIDs_tbl(l_index);
8731                 update po_change_requests
8732                     set request_status='ACCEPTED',
8733                         change_active_flag='N',
8734                         response_date=sysdate,
8735                         response_reason=fnd_message.get_string('PO', 'PO_RCO_PO_CANCELLED')
8736                     where change_request_id=l_change_request_id;
8737 
8738 
8739   --bug 7664476 -- roll up the authorization status if all lines of requisiton is cancelled
8740             UPDATE po_requisition_headers_all h
8741             SET    h.AUTHORIZATION_STATUS  = 'CANCELLED'
8742             WHERE  h.REQUISITION_HEADER_ID = l_document_id
8743                 AND NOT EXISTS
8744                     (SELECT 'UNCANCELLED LINE EXISTS'
8745                     FROM    po_requisition_lines_all prl
8746                     WHERE   prl.requisition_header_id = l_document_id
8747                         AND NVL(prl.cancel_flag,'N')  = 'N'
8748                     );
8749 
8750 
8751 
8752             else
8753             -- reject the change request
8754             -- it can have multiple records, so we can't use change request id
8755                 update po_change_requests
8756                     set request_status='REJECTED',
8757                         change_active_flag='N',
8758                         response_date=sysdate,
8759                         response_reason=fnd_message.get_string('PO', 'PO_RCO_PO_CANCELLED')
8760                     where change_request_group_id=l_change_request_group_id
8761                           and document_line_id=p_CanceledReqLineIDs_tbl(l_index);
8762             end if;
8763 
8764             open l_pending_change_csr(l_change_request_group_id);
8765             fetch l_pending_change_csr into temp;
8766 
8767             if(l_pending_change_csr%NOTFOUND) then
8768             -- no pending change request in status 'new' or 'mgr_pre_app'
8769             -- so we need start the convert into po change
8770             -- workflow to convert the change request.
8771                 begin
8772                     wf_engine.abortprocess(l_wf_item_type, l_wf_item_key);
8773 
8774                     l_note := PO_WF_UTIL_PKG.GetItemAttrText(itemtype => l_wf_item_type,
8775                                          itemkey  => l_wf_item_key,
8776                                          aname    => 'NOTE');
8777 
8778                     PO_APPROVAL_LIST_HISTORY_SV.Update_Action_History(
8779                                          itemtype=>l_wf_item_type,
8780                                          itemkey=>l_wf_item_key,
8781                                          x_action=>l_action,
8782                                          x_req_header_id=>l_document_id,
8783                                          x_last_approver=>l_result,
8784                                          x_note=>l_note);
8785 
8786                     StartConvertProcess(l_change_request_group_id,
8787                                         l_wf_item_key);
8788                 exception
8789                     when others then
8790                         null;
8791                 end;
8792             else
8793             -- need reset the workflow attribute of change total, change tax
8794             -- change amount etc.
8795                 setNewTotal(l_wf_item_type, l_wf_item_key);
8796             end if;
8797 
8798             close l_pending_change_csr;
8799 
8800         end if;
8801 
8802         exit when l_index=l_last;
8803         l_index:=p_CanceledReqLineIDs_tbl.next(l_index);
8804     end loop;
8805 
8806     -- Standard API check of p_commit
8807     IF FND_API.to_boolean(p_commit) THEN
8808         COMMIT WORK;
8809     END IF;
8810 
8811     x_return_status:=FND_API.g_ret_sts_success;
8812 EXCEPTION
8813     WHEN FND_API.g_exc_unexpected_error THEN
8814         x_return_status := FND_API.g_ret_sts_unexp_error;
8815     when others then
8816         x_return_status := FND_API.g_ret_sts_unexp_error;
8817         null;
8818 end Process_Cancelled_Req_Lines;
8819 
8820 
8821 /*************************************************************************
8822  * Public Procedure: Submit_Req_Change
8823  *
8824  * Effects: This procedure is called by the requester change UI. When user
8825  *          want to submit a change request, the validation API will be
8826  *          called. If the change request is valid, the validation API
8827  *          will call this procedure to submit the request and start
8828  *          workflow to process the request.
8829  *
8830  *          it will call PO_REQAPPROVAL_INIT1.Start_WF_Process to start
8831  *          the workflow
8832  *
8833  * Returns:
8834  ************************************************************************/
8835 Procedure Submit_Req_Change( p_api_version IN NUMBER,
8836                              p_commit IN VARCHAR2,
8837                              p_req_header_id IN NUMBER,
8838                              p_note_to_approver IN VARCHAR2,
8839                              p_initiator IN VARCHAR2,
8840                              x_return_status OUT NOCOPY VARCHAR2 )
8841 IS
8842 
8843   p_document_type VARCHAR2(20) := 'REQUISITION';
8844   p_document_subtype VARCHAR2(20) := 'PURCHASE';
8845   p_interface_source_code VARCHAR2(20):= 'POR';
8846   p_item_key  wf_items.item_key%type;
8847   p_item_type wf_items.item_type%type:='POREQCHA';
8848   p_submitter_action VARCHAR2(20) := 'APPROVE';
8849   p_workflow_process VARCHAR2(30):='MAIN_CHANGE_APPROVAL';
8850   l_change_request_group_id number;
8851   l_preparer_id number;
8852   l_req_num po_requisition_headers_all.segment1%type;
8853   cursor change_request_group_id is
8854     select max(change_request_group_id)
8855         from po_change_requests
8856         where document_header_id = p_req_header_id
8857             and initiator='REQUESTER'
8858             and request_status='NEW';
8859 
8860   l_api_name CONSTANT VARCHAR2(30) := 'Submit_Req_Change';
8861   l_api_version CONSTANT NUMBER := 1.0;
8862 
8863 BEGIN
8864     IF NOT FND_API.compatible_api_call(l_api_version, p_api_version,
8865                                        l_api_name, g_pkg_name) THEN
8866         RAISE FND_API.g_exc_unexpected_error;
8867     END IF;
8868     x_return_status := FND_API.g_ret_sts_success;
8869     -- End standard API initialization
8870 
8871     open change_request_group_id;
8872     fetch change_request_group_id into l_change_request_group_id;
8873     close change_request_group_id;
8874 
8875     SELECT to_char(p_req_header_id) || '-'
8876                 ||to_char(l_change_request_group_id)||'-'
8877                 || to_char(PO_REQUESTER_CHANGE_WF_S.nextval)
8878     INTO p_item_key
8879     FROM sys.dual;
8880 
8881     select preparer_id, segment1
8882     into l_preparer_id, l_req_num
8883     from po_requisition_headers_all
8884     where requisition_header_id= p_req_header_id;
8885 
8886     PO_REQAPPROVAL_INIT1.Start_WF_Process(
8887          ItemType => p_item_type,
8888          ItemKey   => p_item_key,
8889          WorkflowProcess => p_workflow_process,
8890          ActionOriginatedFrom => p_interface_source_code,
8891          DocumentID  => p_req_header_id,
8892          DocumentNumber =>  l_req_num,
8893          PreparerID => l_preparer_id,
8894          DocumentTypeCode => p_document_type,
8895          DocumentSubtype  => p_document_subtype,
8896          SubmitterAction => p_submitter_action,
8897          forwardToID  =>  null,
8898          forwardFromID  => l_preparer_id,
8899          DefaultApprovalPathID => NULL,
8900          note => p_note_to_approver,
8901          p_Initiator => p_initiator);
8902 
8903     IF FND_API.to_boolean(p_commit) THEN
8904         COMMIT WORK;
8905     END IF;
8906 EXCEPTION
8907     WHEN FND_API.g_exc_unexpected_error THEN
8908         x_return_status := FND_API.g_ret_sts_unexp_error;
8909     WHEN OTHERS THEN
8910         x_return_status := FND_API.g_ret_sts_unexp_error;
8911         RAISE;
8912 END;
8913 
8914 /*************************************************************************
8915  *
8916  * Public Procedure: Start_ProcessBuyerResponseWF
8917  * Effects: This procedure start the workflow process
8918  *          PROCESS_BUYER_RESPONSE(PORPOCHA). It will be called
8919  *          by both workflow and UI.
8920  *
8921  *          if buyer respond to the change request through notification
8922  *          this procedure will be called in the workflow. If buyer choose
8923  *          to respond through UI, then this procedure will be called
8924  *          by the UI.
8925  *
8926  *          It will call another private procedure StartPOChangeWF which
8927  *          will COMMIT the change.
8928  *
8929  ************************************************************************/
8930 procedure Start_ProcessBuyerResponseWF(p_change_request_group_id in number) is
8931 
8932 x_progress varchar2(3):= '000';
8933 l_count number;
8934 item_key varchar2(240);
8935 item_type varchar2(8):='PORPOCHA';
8936 l_parent_item_type wf_items.item_type%type;
8937 l_parent_item_key wf_items.item_key%type;
8938 l_forward_from_username varchar2(200);
8939 l_user_id number;
8940 l_application_id number;
8941 l_responsibility_id number;
8942 
8943 cursor get_parent_info_csr(l_change_request_group_id number) is
8944     select pcr.wf_item_type, pcr.wf_item_key
8945     from po_change_requests pcr, po_change_requests pcr2
8946     where pcr2.change_request_group_id=l_change_request_group_id
8947           and pcr.change_request_id=pcr2.parent_change_request_id
8948           and pcr.wf_item_type is not null;
8949 
8950 begin
8951 
8952     select PO_REQUESTER_CHANGE_WF_S.nextval into l_count from dual;
8953     item_key:='RESPONSE_'||to_char(p_change_request_group_id)||'_'||to_char(l_count);
8954 
8955     open get_parent_info_csr(p_change_request_group_id);
8956     fetch get_parent_info_csr into l_parent_item_type, l_parent_item_key;
8957     close get_parent_info_csr;
8958 
8959     l_forward_from_username:= PO_WF_UTIL_PKG.GetItemAttrText(
8960                               itemtype=>l_parent_item_type,
8961                               itemkey=>l_parent_item_key,
8962                               aname =>'RESPONDER_USER_NAME');
8963     l_user_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => l_parent_item_type,
8964                                          itemkey  => l_parent_item_key,
8965                                          aname    => 'USER_ID');
8966 
8967     l_responsibility_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => l_parent_item_type,
8968                                          itemkey  => l_parent_item_key,
8969                                          aname    => 'RESPONSIBILITY_ID');
8970     l_application_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => l_parent_item_type,
8971                                          itemkey  => l_parent_item_key,
8972                                          aname    => 'APPLICATION_ID');
8973 
8974 
8975     StartPOChangeWF(p_change_request_group_id, item_key, 'PROCESS_BUYER_RESPONSE', l_forward_from_username,  l_user_id, l_responsibility_id, l_application_id);
8976 
8977 end Start_ProcessBuyerResponseWF;
8978 
8979 Procedure Record_Buyer_Response(
8980             p_api_version in number,
8981             p_commit in varchar2,
8982             x_return_status out NOCOPY varchar2,
8983             p_change_request_id IN NUMBER,
8984             p_acceptance_flag in varchar2,
8985             p_responded_by in number,
8986             p_response_reason in varchar2) is
8987 
8988 l_item_type wf_items.item_type%type;
8989 l_item_key wf_items.item_key%type;
8990 l_api_name CONSTANT VARCHAR2(30) := 'Record_Buyer_Response';
8991 l_api_version CONSTANT NUMBER := 1.0;
8992 
8993 l_notif_status wf_notifications.status%type;
8994 l_notification_id number;
8995 l_next_flag boolean:=true;
8996 
8997 cursor l_notif_status_csr(activity in varchar2) is
8998     select wfn.status, wfn.notification_id
8999      from wf_item_activity_statuses_v was, wf_notifications wfn
9000      where was.item_type=l_item_type
9001            and was.item_key=l_item_key
9002            and was.activity_name=activity
9003            and was.notification_id=wfn.notification_id;
9004 begin
9005     IF NOT FND_API.compatible_api_call(l_api_version, p_api_version,
9006                                        l_api_name, g_pkg_name) THEN
9007         RAISE FND_API.g_exc_unexpected_error;
9008     END IF;
9009 
9010     x_return_status := FND_API.g_ret_sts_success;
9011 
9012     update po_change_requests
9013        set request_status=p_acceptance_flag,
9014            change_active_flag=decode(p_acceptance_flag, 'REJECTED', 'N', 'Y'),
9015            response_date=sysdate,
9016            response_reason=p_response_reason,
9017            responded_by=p_responded_by
9018      where change_request_id=p_change_request_id;
9019 
9020     select wf_item_type, wf_item_key
9021       into l_item_type, l_item_key
9022       from po_change_requests
9023      where change_request_id=p_change_request_id;
9024 
9025     begin
9026         open l_notif_status_csr('NEW_PO_CHANGE');
9027         fetch l_notif_status_csr into l_notif_status, l_notification_id;
9028         if(l_notif_status_csr%FOUND) then
9029             l_next_flag:=false;
9030             if(l_notif_status='OPEN') then
9031                 update wf_notifications
9032                    set status='CLOSED'
9033                  where notification_id=l_notification_id;
9034             end if;
9035         end if;
9036         close l_notif_status_csr;
9037     exception
9038         when others then null;
9039     end;
9040 
9041     begin
9042         if(l_next_flag) then
9043             open l_notif_status_csr('NEW_PO_CHANGE');
9044             fetch l_notif_status_csr into l_notif_status, l_notification_id;
9045             if(l_notif_status_csr%FOUND) then
9046                 if(l_notif_status='OPEN') then
9047                     wf_engine.AbortProcess(l_item_type, l_item_key);
9048                 end if;
9049             end if;
9050             close l_notif_status_csr;
9051         end if;
9052     exception
9053         when others then null;
9054     end;
9055     IF FND_API.to_boolean(p_commit) THEN
9056         COMMIT WORK;
9057     END IF;
9058 
9059 EXCEPTION
9060     when others then
9061         x_return_status := FND_API.g_ret_sts_unexp_error;
9062 end Record_Buyer_Response;
9063 
9064 Procedure Process_Buyer_Response(
9065             p_api_version in number,
9066             x_return_status out NOCOPY varchar2,
9067             p_change_request_group_id IN NUMBER) is
9068 
9069 l_api_name CONSTANT VARCHAR2(30) := 'Process_Buyer_Response';
9070 l_api_version CONSTANT NUMBER := 1.0;
9071 
9072 x_progress varchar2(3):= '000';
9073 l_count number;
9074 item_key varchar2(240);
9075 item_type varchar2(8):='PORPOCHA';
9076 l_parent_item_type wf_items.item_type%type;
9077 l_parent_item_key wf_items.item_key%type;
9078 l_forward_from_username varchar2(200);
9079 l_user_id number;
9080 l_application_id number;
9081 l_responsibility_id number;
9082 
9083 l_parent_wf_available varchar2(1):='N';
9084 l_procedure_name CONSTANT VARCHAR2(100) := 'process_buyer_response';
9085 
9086 
9087 cursor get_parent_info_csr(l_change_request_group_id number) is
9088     select pcr.wf_item_type, pcr.wf_item_key
9089     from po_change_requests pcr, po_change_requests pcr2
9090     where pcr2.change_request_group_id=l_change_request_group_id
9091           and pcr.change_request_id=pcr2.parent_change_request_id
9092           and pcr.wf_item_type is not null;
9093 begin
9094     G_CURRENT_RUNTIME_LEVEL := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
9095 
9096     IF (g_fnd_debug = 'Y') THEN
9097       IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
9098                      FND_LOG.string(log_level => G_LEVEL_STATEMENT,
9099                                     module    => G_MODULE_NAME||'.'||l_procedure_name,
9100                                     message   => l_procedure_name||'.begin'||'-change_request_group_id:' ||to_char(p_change_request_group_id) );
9101                      FND_LOG.string(log_level => G_LEVEL_STATEMENT,
9102                                     module    => G_MODULE_NAME||'.'||l_procedure_name,
9103                                     message   => 'Parameters:'||'-p_change_request_group_id:'||to_char(p_change_request_group_id) || '-p_api_version:' || to_char(p_api_version) );
9104       END IF;
9105 
9106     END IF;
9107 
9108     IF NOT FND_API.compatible_api_call(l_api_version, p_api_version,
9109                                        l_api_name, g_pkg_name) THEN
9110         IF (g_fnd_debug = 'Y') THEN
9111           IF( FND_LOG.LEVEL_EXCEPTION >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
9112                      FND_LOG.string(log_level => G_LEVEL_EXCEPTION,
9113                                     module    => G_MODULE_NAME||'.'||l_procedure_name,
9114                                     message   => 'api is not compatible');
9115           END IF;
9116         END IF;
9117 
9118         RAISE FND_API.g_exc_unexpected_error;
9119     END IF;
9120 
9121     x_progress:='001';
9122     x_return_status := FND_API.g_ret_sts_success;
9123     select PO_REQUESTER_CHANGE_WF_S.nextval into l_count from dual;
9124     item_key:='RESPONSE_'||to_char(p_change_request_group_id)||'_'||to_char(l_count);
9125 
9126     x_progress:='002';
9127     open get_parent_info_csr(p_change_request_group_id);
9128     fetch get_parent_info_csr into l_parent_item_type, l_parent_item_key;
9129     close get_parent_info_csr;
9130 
9131     x_progress:='003';
9132     -- bug 5357773
9133     -- if the parent wf has been purged, can't get following attributes from wf.
9134     -- we get the attributes through fnd_global instead
9135     BEGIN
9136       select 'Y'
9137       into l_parent_wf_available
9138       from wf_items
9139       where item_type = l_parent_item_type
9140       and item_key = l_parent_item_key;
9141 
9142     EXCEPTION
9143       when others then
9144       l_parent_wf_available:= 'N';
9145     END;
9146 
9147     x_progress:='004';
9148     IF (g_fnd_debug = 'Y') THEN
9149       IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
9150                      FND_LOG.string(log_level => G_LEVEL_STATEMENT,
9151                                     module    => G_MODULE_NAME||'.'||l_procedure_name,
9152                                     message   => 'change_request_group_id:' ||to_char(p_change_request_group_id)||'-is_parent_wf_available:' || l_parent_wf_available );
9153 
9154       END IF;
9155     END IF;
9156 
9157     if ( l_parent_wf_available = 'Y') then
9158 
9159     l_forward_from_username:= PO_WF_UTIL_PKG.GetItemAttrText(
9160                               itemtype=>l_parent_item_type,
9161                               itemkey=>l_parent_item_key,
9162                               aname =>'RESPONDER_USER_NAME');
9163 
9164     l_user_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => l_parent_item_type,
9165                                          itemkey  => l_parent_item_key,
9166                                          aname    => 'USER_ID');
9167 
9168     l_responsibility_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => l_parent_item_type,
9169                                          itemkey  => l_parent_item_key,
9170                                          aname    => 'RESPONSIBILITY_ID');
9171 
9172     l_application_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => l_parent_item_type,
9173                                          itemkey  => l_parent_item_key,
9174                                          aname    => 'APPLICATION_ID');
9175 
9176     x_progress:='005';
9177     -- if the parent wf has been purged, we get these attributes from session
9178     else
9179       l_forward_from_username:= fnd_global.user_name;
9180 
9181       l_user_id := fnd_global.user_id;
9182 
9183       l_responsibility_id := fnd_global.resp_id;
9184 
9185       l_application_id := fnd_global.resp_appl_id;
9186 
9187       x_progress:='006';
9188 
9189       IF (g_fnd_debug = 'Y') THEN
9190         IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
9191                      FND_LOG.string(log_level => G_LEVEL_STATEMENT,
9192                                     module    => G_MODULE_NAME||'.'||l_procedure_name ,
9193                                     message   => 'change_request_group_id:' ||to_char(p_change_request_group_id)||' getting ids from session since parent wf is not available' );
9194 
9195         END IF;
9196       END IF;
9197 
9198     end if;
9199 
9200 
9201     StartPOChangeWF(p_change_request_group_id, item_key, 'PROCESS_BUYER_RESPONSE', l_forward_from_username, l_user_id, l_responsibility_id, l_application_id);
9202 
9203     x_progress:='007';
9204 
9205     IF (g_fnd_debug = 'Y') THEN
9206       IF( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
9207                      FND_LOG.string(log_level => G_LEVEL_STATEMENT,
9208                                     module    => G_MODULE_NAME||'.'||l_procedure_name,
9209                                     message   => l_procedure_name||'.end'|| '-change_request_group_id:' ||to_char(p_change_request_group_id));
9210       END IF;
9211     END IF;
9212 
9213 EXCEPTION
9214     when others then
9215         x_return_status := FND_API.g_ret_sts_unexp_error;
9216         IF( FND_LOG.LEVEL_EXCEPTION >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) THEN
9217                      FND_LOG.string(log_level => G_LEVEL_EXCEPTION,
9218                                     module    => G_MODULE_NAME||'.'||l_procedure_name,
9219                                     message   => 'x_progress:'||x_progress||'-sqlerrm:'||sqlerrm);
9220         END IF;
9221 end Process_Buyer_Response;
9222 
9223 /*************************************************************************
9224  * Public Procedure: Any_Cancellation_Change
9225  *
9226  * Effects: This procedure will check whether there are any cancellation
9227  *          requests for the current PO/Release.
9228  *
9229  * Returns: Yes if there is any cancellation request.
9230  ************************************************************************/
9231 procedure Any_Cancellation_Change(itemtype        in varchar2,
9232                                 itemkey         in varchar2,
9233                                 actid           in number,
9234                                 funcmode        in varchar2,
9235                                 resultout       out NOCOPY varchar2    )
9236 IS
9237   l_document_id number;
9238   l_document_type varchar2(240);
9239   l_change_request_id number:=null;
9240   x_progress varchar2(3) := '000';
9241 BEGIN
9242 
9243   l_document_id := PO_WF_UTIL_PKG.GetItemAttrNumber(itemtype => itemtype,
9244                                             itemkey => itemkey,
9245                                             aname => 'DOCUMENT_ID');
9246 
9247   l_document_type := PO_WF_UTIL_PKG.GetItemAttrText(itemtype => itemtype,
9248                                             itemkey => itemkey,
9249                                             aname => 'DOCUMENT_TYPE');
9250 
9251   IF (l_document_type = 'RELEASE') THEN
9252     x_progress := '001';
9253 
9254     select max(change_request_id)
9255     into l_change_request_id
9256     from po_change_requests
9257     where po_release_id=l_document_id
9258       and request_status in ('BUYER_APP', 'PENDING')
9259       and initiator='REQUESTER'
9260       and document_type='RELEASE'
9261       and action_type='CANCELLATION';
9262 
9263   ELSE
9264     x_progress := '002';
9265 
9266     select max(change_request_id)
9267     into l_change_request_id
9268     from po_change_requests
9269     where document_header_id=l_document_id
9270       and request_status in ('BUYER_APP', 'PENDING')
9271       and initiator='REQUESTER'
9272       and document_type='PO'
9273       and action_type='CANCELLATION';
9274 
9275   END IF;
9276 
9277   x_progress := '003';
9278 
9279   IF (l_change_request_id is not null) THEN
9280     resultout := 'Y';
9281   ELSE
9282     resultout := 'N';
9283   END IF;
9284 
9285 EXCEPTION
9286 
9287   WHEN no_data_found THEN
9288     resultout := 'N';
9289 
9290   WHEN others THEN
9291     wf_core.context('PO_ReqChangeRequestWF_PVT','Any_Cancellation_Change',x_progress);
9292     raise;
9293 END Any_Cancellation_Change;
9294 
9295 /*************************************************************************
9296  * Private Procedure: CalculateRcoTotal
9297  * Effects:
9298  *   calculate the new total and old total in RCO buyer notification
9299  *   used in Set_Buyer_Approval_Notfn_Attr and Set_Buyer_FYI_Notif_Attributes
9300 ************************************************************************/
9301 procedure CalculateRcoTotal (p_change_request_group_id  in number,
9302                              p_org_id in number,
9303                              p_po_currency in varchar2, --added currency to format the totals
9304                              x_old_total out nocopy number,
9305                              x_new_total out nocopy number) is
9306 
9307 x_progress              varchar2(100);
9308 
9309 l_old_total number :=0;
9310 l_new_total number :=0;
9311 
9312 l_header_id number;
9313 l_line_id number;
9314 l_line_loc_id number;
9315 l_pcr_old_amount number;
9316 l_pol_amount number;
9317 l_pcr_old_price number;
9318 l_pol_quantity number;
9319 l_pll_amount number;
9320 l_pll_quantity number;
9321 l_pll_quantity_cancelled number;
9322 l_pll_price_override number;
9323 l_only_line_change_amt varchar(5);
9324 l_only_line_change_qty varchar(5);
9325 l_shipment_chg_exists_amt number;
9326 l_shipment_chg_exists_qty number;
9327 l_pol_matching varchar2(100);
9328 l_pcr_new_amount number;
9329 l_pcr_new_price number;
9330 l_pcr_action_type varchar(100);
9331 l_item_id number;
9332 l_uom varchar2(500);
9333 l_pol_unit_price number;
9334 
9335 l_functional_currency_code varchar2(30);
9336 l_rate number;
9337 l_po_in_txn_curr varchar2(1):='N';
9338 
9339  cursor l_po_info(p_change_request_group_id  number) is
9340     select pcr.document_header_id,
9341            pcr.document_line_id,
9342            pcr.document_line_location_id,
9343            pcr.old_amount,
9344            pol.amount,
9345            pol.quantity,
9346            pcr.old_price,
9347            pll.amount,
9348            pll.quantity,
9349            pll.quantity_cancelled,
9350            pll.price_override,
9351            pol.matching_basis,
9352            pcr.new_amount,
9353            pcr.new_price,
9354            pcr.action_type,
9355            pol.item_id,
9356            pll.unit_meas_lookup_code,
9357            pol.unit_price
9358     FROM   po_change_requests pcr, po_lines_all pol,
9359            po_line_locations_all pll
9360     WHERE  pcr.change_request_group_id= p_change_request_group_id
9361       AND pcr.request_status IN ('PENDING', 'ACCEPTED')
9362       AND pcr.document_header_id=pol.po_header_id
9363       AND pcr.document_line_id=pol.po_line_id
9364       AND nvl(pcr.document_line_location_id,-1)=pll.line_location_id(+)
9365       AND pcr.request_level<>'DISTRIBUTION';
9366 
9367 
9368 BEGIN
9369 
9370    SELECT sob.currency_code
9371    INTO  l_functional_currency_code
9372    FROM  gl_sets_of_books sob, financials_system_params_all fsp
9373    WHERE fsp.org_id = p_org_id
9374    AND  fsp.set_of_books_id = sob.set_of_books_id;
9375 
9376    select poh.rate
9377    into l_rate
9378    from po_headers_all poh
9379    where poh.po_header_id in (
9380      select document_header_id from po_change_requests
9381      where change_request_group_id = p_change_request_group_id ) ;
9382 
9383 
9384    if (l_functional_currency_code <> p_po_currency)  then
9385      l_po_in_txn_curr := 'Y';
9386 
9387    end if;
9388 
9389    open l_po_info (p_change_request_group_id );
9390       loop
9391         fetch l_po_info
9392          into l_header_id,
9393               l_line_id,
9394               l_line_loc_id,
9395               l_pcr_old_amount,
9396               l_pol_amount,
9397               l_pol_quantity,
9398               l_pcr_old_price,
9399               l_pll_amount,
9400               l_pll_quantity,
9401               l_pll_quantity_cancelled,
9402               l_pll_price_override,
9403               l_pol_matching,
9404               l_pcr_new_amount,
9405               l_pcr_new_price,
9406               l_pcr_action_type,
9407               l_item_id,
9408               l_uom,
9409               l_pol_unit_price;
9410         exit when l_po_info%NOTFOUND;
9411 
9412      x_progress := '001';
9413 
9414      -- for amount-based line
9415      -- start_date,end_date changes are stored at 'line' level
9416      -- amount change is stored at 'shipment' level ( for PO change request)
9417      if (l_pol_matching = 'AMOUNT' ) then
9418 
9419         -- if l_line_loc_id is null, this is a line-level row
9420         -- If there are shipment level changes for the same line,we ignore this line's amount here since amount will be caught later at shipment level.
9421         -- If there is no shipment level change for this line, we should include its amount.
9422         if ( l_line_loc_id is null ) then
9423            Begin
9424               SELECT 1
9425               into l_shipment_chg_exists_amt
9426               FROM   po_change_requests pcr
9427               WHERE  pcr.change_request_group_id= p_change_request_group_id
9428               AND pcr.request_status IN ('PENDING', 'ACCEPTED')
9429               AND pcr.document_header_id= l_header_id
9430               AND pcr.document_line_id=  l_line_id
9431               AND pcr.document_line_location_id IS NOT NULL
9432               AND pcr.request_level='SHIPMENT';
9433 
9434            exception
9435               WHEN NO_DATA_FOUND THEN
9436                 l_only_line_change_amt := 'Y';
9437            End;
9438 
9439         end if;
9440 
9441         x_progress := '002';
9442         if ( (l_line_loc_id is null and  l_only_line_change_amt='Y')
9443               or l_line_loc_id is not  null ) then
9444 
9445         -- add this line's old amount into the old amount total
9446           if ( l_pcr_old_amount is not null) then
9447             l_old_total:= l_old_total + get_formatted_total(l_pcr_old_amount, p_po_currency) ;
9448 
9449           elsif ( l_line_loc_id is null ) then
9450             l_old_total:= l_old_total + get_formatted_total(l_pol_amount, p_po_currency);
9451 
9452           else
9453             l_old_total:= l_old_total + get_formatted_total(l_pll_amount, p_po_currency);
9454 
9455           end if;
9456 
9457           -- add this line's new amount into the new amount total
9458           if ( l_pcr_action_type <> 'CANCELLATION') then
9459             if ( l_pcr_new_amount is not null) then
9460 
9461               /* Removed code to divide new_amt by rate */
9462               l_new_total:= l_new_total + get_formatted_total(l_pcr_new_amount, p_po_currency) ;
9463 
9464             elsif ( l_line_loc_id is null ) then
9465               l_new_total:= l_new_total + get_formatted_total(l_pol_amount, p_po_currency);
9466 
9467             else
9468               l_new_total:= l_new_total + get_formatted_total(l_pll_amount, p_po_currency);
9469 
9470             end if;
9471           end if;
9472 
9473        end if;
9474 
9475        l_only_line_change_amt := 'N';
9476 
9477        x_progress := '003';
9478 
9479      -- below is for qty based line
9480      -- for qty based line, price change is at line level;
9481      -- quantity, need_by_date changes are at shipment level
9482      else
9483        if ( l_line_loc_id is null ) then
9484       -- if l_line_loc_id is null, this is a line-level row(stores price change)
9485       -- If there are shipment level changes for the same line,we ignore this line's amount here since amount will be caught later at shipment level.
9486       -- If there is only price change for this line, we should include its amount.
9487 
9488             Begin
9489               SELECT 1
9490               into l_shipment_chg_exists_qty
9491               FROM   po_change_requests pcr
9492               WHERE  pcr.change_request_group_id= p_change_request_group_id
9493               AND pcr.request_status IN ('PENDING', 'ACCEPTED')
9494               AND pcr.document_header_id= l_header_id
9495               AND pcr.document_line_id=  l_line_id
9496               AND pcr.document_line_location_id IS NOT NULL
9497               AND pcr.request_level='SHIPMENT';
9498 
9499             exception
9500               WHEN NO_DATA_FOUND THEN
9501                 l_only_line_change_qty := 'Y';
9502             End;
9503 
9504             if (l_only_line_change_qty = 'Y') then
9505 
9506                l_old_total := l_old_total + get_formatted_total(nvl(l_pol_amount,l_pol_quantity*l_pcr_old_price), p_po_currency);
9507                /* Removed code to divide new_price by rate */
9508                l_new_total := l_new_total + get_formatted_total(l_pol_quantity*l_pcr_new_price, p_po_currency);
9509 
9510                l_only_line_change_qty :='N';
9511 
9512             end if;
9513 
9514        x_progress := '004';
9515        else
9516 
9517          l_old_total := l_old_total + get_formatted_total(nvl(l_pll_amount,
9518                      (nvl(l_pll_quantity,0)- nvl(l_pll_quantity_cancelled,0))*( nvl(l_pll_price_override,l_pol_unit_price))), p_po_currency) ;
9519 
9520          if ( l_pcr_action_type <> 'CANCELLATION') then
9521 
9522           l_new_total :=  l_new_total +
9523             get_formatted_total(PO_ReqChangeRequestNotif_PVT.get_goods_shipment_new_amount(
9524               p_org_id,p_change_request_group_id,l_line_id,l_item_id,l_uom,
9525                 nvl(l_pcr_old_price, nvl(l_pll_price_override,l_pol_unit_price)),
9526                   l_line_loc_id), p_po_currency);
9527 
9528          end if;
9529 
9530       end if; -- for line_loc_id
9531      end if; -- for matching basis
9532 
9533     end loop;
9534     close l_po_info;
9535 
9536     x_progress := '005';
9537 
9538     x_old_total := l_old_total;
9539     x_new_total := l_new_total;
9540 
9541 EXCEPTION
9542   WHEN OTHERS THEN
9543     wf_core.context('PO_ReqChangeRequestWF_PVT','CalculateRcoTotal',x_progress||sqlerrm);
9544     raise;
9545 
9546 
9547 END CalculateRcoTotal;
9548 
9549 /*************************************************************************
9550  * Public Procedure: Set_Buyer_Approval_Notfn_Attr
9551  *
9552  * Effects: This procedure is to set the attributes required by buyer
9553  *          approval notification (INFORM_BUYER_PO_CHANGE).
9554  *
9555  ************************************************************************/
9556 Procedure Set_Buyer_Approval_Notfn_Attr(itemtype   in varchar2,
9557                                         itemkey    in varchar2,
9558                                         actid      in number,
9559                                         funcmode   in varchar2,
9560                                         resultout  out NOCOPY varchar2)
9561 IS
9562 
9563 l_doc_id number;
9564 l_doc_type varchar2(25);
9565 l_orgid       number;
9566 
9567 x_progress              varchar2(100);
9568 
9569 l_change_request_group_id number;
9570 l_order_date DATE;
9571 l_old_total number;
9572 l_new_total number;
9573 l_po_currency varchar2(10);
9574 
9575 l_doc_string varchar2(200);
9576 l_preparer_user_name varchar2(100);
9577 
9578 BEGIN
9579     x_progress := 'PO_ReqChangeRequestWF_PVT.Set_Buyer_Approval_Notfn_Attr: 01';
9580 
9581     -- Do nothing in cancel or timeout mode
9582     if (funcmode <> wf_engine.eng_run) then
9583         resultout := wf_engine.eng_null;
9584         return;
9585     end if;
9586 
9587     l_doc_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
9588                                          itemkey  => itemkey,
9589                                          aname    => 'DOCUMENT_ID');
9590 
9591     l_doc_type := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
9592                                          itemkey  => itemkey,
9593                                          aname    => 'DOCUMENT_TYPE');
9594 
9595     l_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (
9596                     itemtype => itemtype,
9597                     itemkey  => itemkey,
9598                     aname    => 'CHANGE_REQUEST_GROUP_ID');
9599 
9600     -- Set the multi-org context
9601     l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
9602                                          itemkey  => itemkey,
9603                                          aname    => 'ORG_ID');
9604 
9605     IF l_orgid is NOT NULL THEN
9606 
9607         PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
9608 
9609     END IF;
9610 
9611     x_progress := 'PO_ReqChangeRequestWF_PVT.Set_Buyer_Approval_Notfn_Attr: 02';
9612 
9613     IF l_doc_type IN ('PO','PA') THEN
9614 
9615         select creation_date, currency_code
9616         into l_order_date, l_po_currency
9617         from po_headers_all
9618         where PO_HEADER_ID = l_doc_id;
9619 
9620     ELSIF l_doc_type = 'RELEASE' THEN
9621 
9622         select pr.creation_date, ph.currency_code
9623         into l_order_date, l_po_currency
9624         from po_releases_all pr, po_headers_all ph
9625         where pr.po_release_id = l_doc_id
9626           and pr.po_header_id = ph.po_header_id;
9627 
9628     END IF;
9629 
9630     x_progress := 'PO_ReqChangeRequestWF_PVT.Set_Buyer_Approval_Notfn_Attr: 03';
9631 
9632     -- get the order total (old/new)
9633     -- used by the approval notification
9634     CalculateRcoTotal( p_change_request_group_id => l_change_request_group_id,
9635                        p_org_id =>l_orgid,
9636                        p_po_currency => l_po_currency,
9637                        x_old_total =>l_old_total ,
9638                        x_new_total =>l_new_total);
9639 
9640      x_progress := 'PO_ReqChangeRequestWF_PVT.Set_Buyer_Approval_Notfn_Attr: 04';
9641 
9642     -- set the order totals and the order date.
9643     -- These are used by approval notification
9644     PO_WF_UTIL_PKG.SetItemAttrDate ( itemtype   => itemtype,
9645                                      itemkey    => itemkey,
9646                                      aname      => 'ORDER_DATE',
9647                                      avalue     => l_order_date);
9648 
9649     PO_WF_UTIL_PKG.SetItemAttrText ( itemtype   => itemtype,
9650                                        itemkey    => itemkey,
9651                                        aname      => 'OLD_ORD_TOTAL_CURRENCY_DSP',
9652                                        avalue     => to_char(l_old_total,
9653                                                       FND_CURRENCY.GET_FORMAT_MASK(l_po_currency,
9654                                                            g_currency_format_mask))
9655                                                       || ' ' ||l_po_currency);
9656     PO_WF_UTIL_PKG.SetItemAttrText ( itemtype   => itemtype,
9657                                        itemkey    => itemkey,
9658                                        aname      => 'NEW_ORD_TOTAL_CURRENCY_DSP',
9659                                        avalue     => to_char(l_new_total,
9660                                                       FND_CURRENCY.GET_FORMAT_MASK(l_po_currency,
9661                                                             g_currency_format_mask))
9662                                                       || ' ' ||l_po_currency );
9663 
9664     x_progress := 'PO_ReqChangeRequestWF_PVT.Set_Buyer_Approval_Notfn_Attr: 05';
9665 
9666 EXCEPTION
9667     WHEN OTHERS THEN
9668         l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
9669         l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(
9670                        itemType, itemkey);
9671         wf_core.context('PO_ReqChangeRequestWF_PVT','Set_Buyer_Approval_Notfn_Attr',
9672                        x_progress);
9673         PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey,
9674                        l_preparer_user_name, l_doc_string, sqlerrm,
9675                        'PO_ReqChangeRequestWF_PVT.Set_Buyer_Approval_Notfn_Attr');
9676         raise;
9677 
9678 END Set_Buyer_Approval_Notfn_Attr;
9679 
9680 /*************************************************************************
9681  * Public Procedure: Reject_Supplier_Change
9682  *
9683  * Effects: This procedure handles rejection case for Supplier Initiated
9684  *          RCO change request.
9685  *
9686  * Returns:
9687  ************************************************************************/
9688 PROCEDURE Reject_Supplier_Change( itemtype        in varchar2,
9689                                   itemkey         in varchar2,
9690                                   actid           in number,
9691                                   funcmode        in varchar2,
9692                                   resultout       out NOCOPY varchar2 )
9693 IS
9694   l_change_request_group_id NUMBER;
9695   l_po_request_group_id NUMBER;
9696   l_document_header_id NUMBER;
9697   l_document_revision_num NUMBER;
9698   l_document_type po_change_requests.document_type%type;
9699   l_header_id NUMBER;
9700   l_release_id NUMBER;
9701   l_req_document_id NUMBER;
9702   x_progress VARCHAR2(3) := '000';
9703 BEGIN
9704 
9705   l_change_request_group_id :=
9706     PO_WF_UTIL_PKG.GetItemAttrNumber( itemtype => itemtype,
9707                                  itemkey  => itemkey,
9708                                  aname    => 'CHANGE_REQUEST_GROUP_ID');
9709 
9710   x_progress := '001';
9711 
9712   select change_request_group_id
9713   INTO l_po_request_group_id
9714   FROM po_change_requests
9715   WHERE parent_change_request_id = l_change_request_group_id
9716   and rownum=1;
9717 
9718   x_progress := '002';
9719 
9720   -- update PO rows in po_change_request for rejection
9721   SetPoRequestStatus(l_po_request_group_id, 'REJECTED');
9722 
9723   x_progress := '003';
9724 
9725   --For Release the document_header_id will be null.Hence query the po_release_id
9726   --to set the release id to pass it to PO_CHANGEORDERWF_PVT.NotifySupAllChgRpdWF
9727   SELECT document_header_id, document_revision_num, document_type, po_release_id
9728   INTO l_document_header_id, l_document_revision_num, l_document_type, l_release_id
9729   FROM po_change_requests
9730   WHERE change_request_group_id = l_po_request_group_id
9731     AND rownum=1;
9732 
9733   x_progress := '004';
9734 
9735   -- Send rejection notification to suppliers
9736   PO_CHANGEORDERWF_PVT.NotifySupAllChgRpdWF
9737     ( p_header_id => l_document_header_id,
9738       p_release_id => l_release_id,
9739       p_revision_num => l_document_revision_num,
9740       p_chg_req_grp_id => l_po_request_group_id );
9741 
9742   -- set change_pending flag to 'N' for the req
9743   l_req_document_id :=
9744     PO_WF_UTIL_PKG.GetItemAttrNumber( itemtype => itemtype,
9745                                  itemkey  => itemkey,
9746                                  aname    => 'DOCUMENT_ID');
9747 
9748    SetReqChangeFlag(l_change_request_group_id, l_req_document_id, itemtype, itemkey, 'N');
9749 
9750 EXCEPTION when others THEN
9751     wf_core.context('PO_ReqChangeRequestWF_PVT','Reject_Supplier_Change',x_progress);
9752     raise;
9753 END Reject_Supplier_Change;
9754 
9755 /*************************************************************************
9756  * Public Procedure: Accept_Supplier_Change
9757  *
9758  * Effects: This procedure handles acceptance case for Supplier Initiated
9759  *          RCO change request.
9760  *
9761  * Returns:
9762  ************************************************************************/
9763 PROCEDURE Accept_Supplier_Change( itemtype        in varchar2,
9764                                   itemkey         in varchar2,
9765                                   actid           in number,
9766                                   funcmode        in varchar2,
9767                                   resultout       out NOCOPY varchar2 )
9768 IS
9769   l_change_request_group_id NUMBER;
9770   l_po_chg_request_group_id NUMBER;
9771   l_document_id NUMBER;
9772   x_progress VARCHAR2(3) := '000';
9773 
9774   l_temp_status varchar2(1000);
9775 BEGIN
9776 
9777   l_change_request_group_id :=
9778     PO_WF_UTIL_PKG.GetItemAttrNumber( itemtype => itemtype,
9779                                  itemkey  => itemkey,
9780                                  aname    => 'CHANGE_REQUEST_GROUP_ID');
9781   x_progress := '001';
9782 
9783   l_document_id :=
9784     PO_WF_UTIL_PKG.GetItemAttrNumber( itemtype => itemtype,
9785                                  itemkey  => itemkey,
9786                                  aname    => 'DOCUMENT_ID');
9787   x_progress := '002';
9788 
9789   SELECT change_request_group_id
9790   INTO l_po_chg_request_group_id
9791   FROM po_change_requests
9792   WHERE parent_change_request_id = l_change_request_group_id
9793     AND rownum=1;
9794 
9795   x_progress := '003';
9796 
9797     -- for SCO, buyer's approval is assumed.Here we flip the PO Row status to 'BUYER_APP'
9798     SetPoRequestStatus(l_po_chg_request_group_id, 'BUYER_APP');
9799 
9800     -- call ProcessSCOAcceptance to update req,PO
9801     ProcessSCOAcceptance(l_po_chg_request_group_id,'Y');
9802 
9803   x_progress := '004';
9804 
9805 
9806     SetReqChangeFlag(l_change_request_group_id, l_document_id, itemtype, itemkey, 'N');
9807 
9808 EXCEPTION when others THEN
9809     wf_core.context('PO_ReqChangeRequestWF_PVT','Accept_Supplier_Change',x_progress);
9810     raise;
9811 END Accept_Supplier_Change;
9812 
9813 /*************************************************************************
9814  *
9815  * Public Procedure: Start_POChange_WF
9816  * Effects: Kicks off PORPCHA workflow process for a specific PO.
9817  *
9818  * NOTE :
9819  *    Kickoff_POChange_WF cannot be updated because of upgrade issues.
9820  *    That is why we created this new procedure to handle the new
9821  *    functionality.
9822  ************************************************************************/
9823 PROCEDURE Start_POChange_WF( itemtype        IN VARCHAR2,
9824                              itemkey         IN VARCHAR2,
9825                              actid           IN NUMBER,
9826                              funcmode        IN VARCHAR2,
9827                              resultout       OUT NOCOPY VARCHAR2 )
9828 IS
9829 
9830   l_po_chg_request_group_id number;
9831   l_orgid    NUMBER;
9832   x_progress VARCHAR2(100);
9833   l_doc_string VARCHAR2(200);
9834   l_preparer_user_name varchar2(100);
9835 BEGIN
9836 
9837   x_progress := 'PO_ReqChangeRequestWF_PVT.Start_POChange_WF: 01';
9838 
9839 
9840   -- Do nothing in cancel or timeout mode
9841   --
9842   if (funcmode <> wf_engine.eng_run) then
9843 
9844       resultout := wf_engine.eng_null;
9845       return;
9846 
9847   end if;
9848 
9849   x_progress := 'PO_ReqChangeRequestWF_PVT.Start_POChange_WF: 02';
9850 
9851   -- Set the multi-org context
9852   l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
9853                                           itemkey  => itemkey,
9854                                           aname    => 'ORG_ID');
9855 
9856   x_progress := 'PO_ReqChangeRequestWF_PVT.Start_POChange_WF: 03';
9857 
9858   IF l_orgid is NOT NULL THEN
9859 
9860       PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
9861 
9862   END IF;
9863 
9864   x_progress := 'PO_ReqChangeRequestWF_PVT.Start_POChange_WF: 04';
9865 
9866   l_po_chg_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (
9867                                  itemtype => itemtype,
9868                                  itemkey  => itemkey,
9869                                  aname    => 'PO_CHANGE_REQUEST_GROUP_ID');
9870 
9871   x_progress := 'PO_ReqChangeRequestWF_PVT.Start_POChange_WF: 05';
9872 
9873   StartInformBuyerWF(l_po_chg_request_group_id);
9874 
9875   x_progress := 'PO_ReqChangeRequestWF_PVT.Start_POChange_WF: 06';
9876 
9877   resultout := wf_engine.eng_completed || ':' ||  'ACTIVITY_PERFORMED';
9878 
9879 EXCEPTION
9880   WHEN OTHERS THEN
9881     l_doc_string := PO_REQAPPROVAL_INIT1.get_error_doc(itemType, itemkey);
9882     l_preparer_user_name := PO_REQAPPROVAL_INIT1.get_preparer_user_name(itemType, itemkey);
9883     wf_core.context('PO_ReqChangeRequestWF_PVT','Start_POChange_WF',x_progress);
9884     PO_REQAPPROVAL_INIT1.send_error_notif(itemType, itemkey, l_preparer_user_name, l_doc_string, sqlerrm, 'PO_ReqChangeRequestWF_PVT.Start_POChange_WF');
9885     raise;
9886 
9887 END Start_POChange_WF;
9888 
9889 /*************************************************************************
9890  *
9891  * Public Procedure: Populate_Tolerances
9892  * Effects: This procedure will populate buyer approval tolerances
9893  *          by calling PO's get_tolerances API.
9894  *
9895  ************************************************************************/
9896 FUNCTION Populate_Tolerances( p_itemtype IN VARCHAR2,
9897                               p_itemkey IN VARCHAR2,
9898                               p_organization_id IN NUMBER ) RETURN po_co_tolerances_grp.tolerances_tbl_type
9899 IS
9900   l_tolerances_tbl po_co_tolerances_grp.tolerances_tbl_type;
9901   l_return_status VARCHAR2(1);
9902   l_msg_count NUMBER;
9903   l_msg_data VARCHAR2(100);
9904 BEGIN
9905 
9906   PO_CO_Tolerances_Grp.get_tolerances(
9907     p_api_version => 1.0,
9908     p_init_msg_list => FND_API.G_TRUE,
9909     p_organization_id => p_organization_id,
9910     p_change_order_type => po_co_tolerances_grp.g_rco_buy_app,
9911     x_tolerances_tbl => l_tolerances_tbl,
9912     x_return_status => l_return_status,
9913     x_msg_count => l_msg_count,
9914     x_msg_data => l_msg_data);
9915 
9916   IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
9917     IF (g_po_wf_debug = 'Y') THEN
9918       PO_WF_DEBUG_PKG.insert_debug(p_itemtype,p_itemkey,'PO_CO_Tolerances_Grp.get_tolerances API failed:' || l_return_status || ' ' || l_msg_data);
9919     END IF;
9920   END IF;
9921 
9922   RETURN l_tolerances_tbl;
9923 
9924 END Populate_Tolerances;
9925 
9926 /*************************************************************************
9927  *
9928  * Public Procedure: Is_Tolerance_Check_Needed
9929  * Effects: This procedure will decide whether tolerance check is
9930  *          needed for the particular PO or not.
9931  *
9932  ************************************************************************/
9933 PROCEDURE Is_Tolerance_Check_Needed( itemtype IN VARCHAR2,
9934                                      itemkey IN VARCHAR2,
9935                                      actid IN NUMBER,
9936                                      funcmode IN VARCHAR2,
9937                                      resultout OUT NOCOPY VARCHAR2 )
9938 IS
9939   l_result varchar2(1) := 'Y';
9940   l_value  varchar2(1) := '';
9941   l_po_chg_group_id NUMBER;
9942   l_po_org_id NUMBER;
9943   l_progress VARCHAR2(100) := '000';
9944   l_creation_method po_headers_all.document_creation_method%TYPE;
9945   l_document_type po_change_requests.document_type%TYPE;
9946 BEGIN
9947 
9948   l_po_chg_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (
9949                          itemtype   => itemtype,
9950                          itemkey    => itemkey,
9951                          aname      => 'PO_CHANGE_REQUEST_GROUP_ID');
9952 
9953   l_document_type := PO_WF_UTIL_PKG.GetItemAttrText (
9954                        itemtype   => itemtype,
9955                        itemkey    => itemkey,
9956                        aname      => 'PO_DOCUMENT_TYPE');
9957 
9958   l_progress := '001';
9959 
9960   IF (l_document_type = 'PO') THEN
9961 
9962       l_progress := '002';
9963 
9964       SELECT poh.document_creation_method, poh.org_id
9965       INTO l_creation_method, l_po_org_id
9966       FROM
9967         po_headers_all poh,
9968         po_change_requests pcr
9969       WHERE poh.po_header_id =pcr.document_header_id
9970         AND pcr.change_request_group_id = l_po_chg_group_id
9971         AND rownum=1;
9972 
9973   ELSE
9974 
9975       l_progress := '003';
9976 
9977       SELECT por.document_creation_method, por.org_id
9978       INTO l_creation_method, l_po_org_id
9979       FROM
9980         po_releases_all por,
9981         po_change_requests pcr
9982       WHERE por.po_release_id = pcr.po_release_id
9983         AND pcr.change_request_group_id = l_po_chg_group_id
9984         AND rownum = 1;
9985 
9986   END IF;
9987 
9988   l_progress := '004';
9989 
9990   g_tolerances_tbl := Populate_Tolerances(itemtype, itemkey, l_po_org_id);
9991 
9992   l_progress := '005';
9993 
9994   l_value := g_tolerances_tbl(TOL_RCO_ROUTING_IND).enabled_flag;
9995 
9996   l_progress := '006';
9997 
9998   IF (l_value = 'Y') THEN
9999 
10000     --Check for both autocreated POs and releases
10001     IF (l_creation_method in ('CREATEDOC','CREATE_RELEASES')) THEN
10002       l_result := 'N';
10003     END IF;
10004 
10005   END IF;
10006 
10007   resultout := wf_engine.eng_completed || ':' || l_result;
10008 
10009 EXCEPTION when others THEN
10010   wf_core.context('PO_ReqChangeRequestWF_PVT','Is_Tolerance_Check_Needed',l_progress);
10011   raise;
10012 END Is_Tolerance_Check_Needed;
10013 
10014 /*************************************************************************
10015  *
10016  * Public Procedure: More_Po_To_Process
10017  * Effects: This procedure will find the next PO/Release
10018  *          to process in HandleBuyerApproval.
10019  *
10020  ************************************************************************/
10021 PROCEDURE More_Po_To_Process( itemtype IN VARCHAR2,
10022                               itemkey IN VARCHAR2,
10023                               actid IN NUMBER,
10024                               funcmode IN VARCHAR2,
10025                               resultout OUT NOCOPY VARCHAR2 )
10026 IS
10027   l_po_chg_request_group_id number;
10028   l_next_po_grp_id number;
10029   l_result varchar2(1) := 'N';
10030   l_change_request_grp_id number;
10031   l_po_document_type po_change_requests.document_type%type;
10032   l_po_document_rev po_change_requests.document_revision_num%type;
10033   l_po_document_id po_change_requests.document_header_id%type;
10034   l_progress VARCHAR2(100);
10035 BEGIN
10036   -- Do nothing in cancel or timeout mode
10037   if (funcmode <> wf_engine.eng_run) then
10038 
10039     resultout := wf_engine.eng_null;
10040     return;
10041 
10042   end if;
10043 
10044   l_progress := 'PO_ReqChangeRequestWF_PVT.More_Po_To_Process: 01';
10045 
10046   l_po_chg_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (
10047                                   itemtype   => itemtype,
10048                                   itemkey    => itemkey,
10049                                   aname      => 'PO_CHANGE_REQUEST_GROUP_ID');
10050 
10051   l_progress := 'PO_ReqChangeRequestWF_PVT.More_Po_To_Process: 02';
10052 
10053   l_change_request_grp_id := PO_WF_UTIL_PKG.GetItemAttrNumber (
10054                                itemtype   => itemtype,
10055                                itemkey    => itemkey,
10056                                aname      => 'CHANGE_REQUEST_GROUP_ID');
10057 
10058   l_progress := 'PO_ReqChangeRequestWF_PVT.More_Po_To_Process: 03';
10059 
10060   SELECT MIN(po_change.change_request_group_id)
10061   INTO l_next_po_grp_id
10062   FROM
10063     po_change_requests po_change,
10064     po_change_requests req_change
10065   WHERE
10066     po_change.parent_change_request_id = req_change.change_request_id AND
10067     req_change.change_request_group_id = l_change_request_grp_id AND
10068     po_change.change_request_group_id > l_po_chg_request_group_id
10069   ORDER BY po_change.change_request_group_id;
10070 
10071   l_progress := 'PO_ReqChangeRequestWF_PVT.More_Po_To_Process: 04';
10072 
10073   -- when next po change request group id is found
10074   IF (l_next_po_grp_id IS NOT NULL) THEN
10075 
10076     PO_WF_UTIL_PKG.SetItemAttrNumber( itemtype   => itemtype,
10077                                     itemkey    => itemkey,
10078                                  aname      => 'PO_CHANGE_REQUEST_GROUP_ID',
10079                                  avalue     => l_next_po_grp_id );
10080 
10081     SELECT document_type, document_revision_num,
10082            decode(document_type, 'RELEASE', po_release_id,document_header_id)
10083     into l_po_document_type, l_po_document_rev, l_po_document_id
10084     FROM po_change_requests
10085     WHERE change_request_group_id = l_next_po_grp_id AND rownum=1;
10086 
10087     PO_WF_UTIL_PKG.SetItemAttrText( itemtype   => itemtype,
10088                                   itemkey    => itemkey,
10089                                aname      => 'PO_DOCUMENT_TYPE',
10090                                avalue     => l_po_document_type );
10091 
10092     PO_WF_UTIL_PKG.SetItemAttrText( itemtype   => itemtype,
10093                                   itemkey    => itemkey,
10094                                aname      => 'PO_DOCUMENT_REVISION',
10095                                avalue     => l_po_document_rev );
10096 
10097     PO_WF_UTIL_PKG.SetItemAttrNumber( itemtype   => itemtype,
10098                                   itemkey    => itemkey,
10099                                aname      => 'CURRENT_PO_HEADER_ID',
10100                                avalue     => l_po_document_id );
10101     l_result := 'Y';
10102   END IF;
10103 
10104   resultout := wf_engine.eng_completed || ':' || l_result;
10105 
10106 EXCEPTION when others THEN
10107   wf_core.context('PO_ReqChangeRequestWF_PVT','More_Po_To_Process',l_progress);
10108   raise;
10109 END More_Po_To_Process;
10110 
10111 /*************************************************************************
10112  *
10113  * Public Procedure: Accept_Po_Changes
10114  * Effects: This procedure will decide whether tolerance check is
10115  *          needed for the particular PO or not.
10116  *
10117  ************************************************************************/
10118 PROCEDURE Accept_Po_Changes( itemtype    IN VARCHAR2,
10119                              itemkey     IN VARCHAR2,
10120                              actid       IN NUMBER,
10121                              funcmode    IN VARCHAR2,
10122                              resultout   OUT NOCOPY VARCHAR2 )
10123 IS
10124   l_po_change_request_group_id number;
10125   l_document_id number;
10126   l_document_type varchar2(25);
10127   l_document_subtype varchar2(25);
10128   l_buyer_id number;
10129   l_buyer_name varchar2(100);
10130   l_buyer_display_name varchar2(300);
10131   l_order_date date;
10132   l_document_number po_change_requests.document_num%type;
10133   l_progress VARCHAR2(100);
10134   l_po_header_id number;
10135   l_po_release_id number;
10136   l_return_status varchar2(1);
10137   l_return_msg varchar2(2000);
10138   l_change_exists BOOLEAN := TRUE;
10139   l_exception exception;
10140 
10141 BEGIN
10142 
10143   -- Do nothing in cancel or timeout mode
10144   if (funcmode <> wf_engine.eng_run) then
10145 
10146     resultout := wf_engine.eng_null;
10147     return;
10148 
10149   end if;
10150 
10151   l_progress := 'PO_ReqChangeRequestWF_PVT.Accept_Po_Changes: 01';
10152 
10153   l_po_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber(
10154                                     itemtype => itemtype,
10155                                     itemkey  => itemkey,
10156                                     aname    => 'PO_CHANGE_REQUEST_GROUP_ID');
10157 
10158   l_progress := 'PO_ReqChangeRequestWF_PVT.Accept_Po_Changes: 02';
10159 
10160   SELECT document_type, decode(document_type, 'RELEASE', po_release_id, document_header_id), document_num
10161   INTO l_document_type, l_document_id, l_document_number
10162   FROM po_change_requests
10163   WHERE change_request_group_id = l_po_change_request_group_id
10164     AND rownum=1;
10165 
10166   IF (l_document_type = 'RELEASE') THEN
10167     l_document_subtype:='BLANKET';
10168   ELSE
10169     l_document_subtype:='STANDARD';
10170   END IF;
10171 
10172   l_progress := 'PO_ReqChangeRequestWF_PVT.Accept_Po_Changes: 03';
10173 
10174   IF l_document_type = 'PO' THEN
10175 
10176     SELECT agent_id, creation_date
10177     INTO l_buyer_id, l_order_date
10178     FROM po_headers_all
10179     WHERE PO_HEADER_ID = l_document_id;
10180 
10181   ELSIF l_document_type = 'RELEASE' THEN
10182 
10183     SELECT agent_id, creation_date
10184     INTO l_buyer_id, l_order_date
10185     FROM po_releases_all
10186     WHERE  PO_RELEASE_ID = l_document_id;
10187 
10188   END IF;
10189 
10190   l_progress := 'PO_ReqChangeRequestWF_PVT.Accept_Po_Changes: 04';
10191 
10192   -- get Buyer's name
10193   PO_REQAPPROVAL_INIT1.get_user_name(l_buyer_id, l_buyer_name, l_buyer_display_name);
10194 
10195   -- set value of BUYER_USER_NAME WF attribute
10196   PO_WF_UTIL_PKG.SetItemAttrText( itemtype   => itemtype,
10197                                 itemkey    => itemkey,
10198                              aname      => 'BUYER_USER_NAME',
10199                              avalue     => l_buyer_name );
10200 
10201   -- set value of PO_ORDER_DATE WF attribute
10202   PO_WF_UTIL_PKG.SetItemAttrDate( itemtype   => itemtype,
10203                                 itemkey    => itemkey,
10204                              aname      => 'PO_ORDER_DATE',
10205                              avalue     => l_order_date );
10206 
10207   -- set value of PO_DOCUMENT_NUMBER WF attribute
10208   PO_WF_UTIL_PKG.SetItemAttrText( itemtype   => itemtype,
10209                                 itemkey    => itemkey,
10210                              aname      => 'PO_DOCUMENT_NUMBER',
10211                              avalue     => l_document_number );
10212 
10213   l_progress := 'PO_ReqChangeRequestWF_PVT.Accept_Po_Changes: 05';
10214 
10215   -- update PO action history as accepted by buyer
10216   InsertActionHist(itemtype, itemkey, l_document_id, l_document_type,
10217                    l_document_subtype, l_buyer_id, 'ACCEPT', '', null);
10218 
10219   l_progress := 'PO_ReqChangeRequestWF_PVT.Accept_Po_Changes: 06';
10220 
10221   -- set PO records status to 'BUYER_APP' with no reason
10222   SetPoRequestStatus(l_po_change_request_group_id, 'BUYER_APP', '');
10223 
10224   l_progress := 'PO_ReqChangeRequestWF_PVT.Accept_Po_Changes: 07';
10225 
10226   -- handle cancellations
10227   ProcessBuyerAction(l_po_change_request_group_id, 'CANCELLATION');
10228 
10229   l_progress := 'PO_ReqChangeRequestWF_PVT.Accept_Po_Changes: 08';
10230 
10231   -- check if there is any modifications. Request_Status column will
10232   -- be ACCEPTED here for cancellations. Since ProcessBuyerAction
10233   -- call in the previous line set cancellation rows to 'ACCEPTED'
10234   -- status. The remaining lines that are in BUYER_APP are for modifications.
10235   BEGIN
10236 
10237     SELECT document_header_id, po_release_id
10238     INTO l_po_header_id, l_po_release_id
10239     FROM po_change_requests
10240     WHERE change_request_group_id=l_po_change_request_group_id
10241       and request_status='BUYER_APP' and rownum=1;
10242   EXCEPTION
10243     WHEN NO_DATA_FOUND THEN
10244       l_change_exists := FALSE;
10245   END;
10246 
10247   l_progress := 'PO_ReqChangeRequestWF_PVT.Accept_Po_Changes: 09';
10248 
10249   IF (l_change_exists) THEN
10250 
10251     -- update requisition, PO and kick PO approval wf
10252     ProcessBuyerAction(l_po_change_request_group_id, 'ACCEPTANCE');
10253 
10254     l_progress := 'PO_ReqChangeRequestWF_PVT.Accept_Po_Changes: 10';
10255 
10256     -- kick off PO approval
10257     KickOffPOApproval(l_po_header_id, l_po_release_id, l_return_status, l_return_msg);
10258 
10259     IF (l_return_status<>FND_API.G_RET_STS_SUCCESS) THEN
10260       l_progress := 'PO_ReqChangeRequestWF_PVT.Accept_Po_Changes: 11-'||l_return_status||'-'||to_char(l_po_header_id)||'-'||to_char(l_po_release_id)||'-'||l_return_msg;
10261       raise l_exception;
10262     else
10263       l_progress := 'PO_ReqChangeRequestWF_PVT.Accept_Po_Changes: 11';
10264     end if;
10265   END IF;
10266 
10267 EXCEPTION when others THEN
10268   wf_core.context('PO_ReqChangeRequestWF_PVT','Accept_Po_Changes',l_progress);
10269   raise;
10270 END Accept_Po_Changes;
10271 
10272 /*************************************************************************
10273  *
10274  * Public Procedure: Set_Buyer_FYI_Notif_Attributes
10275  * Effects: This procedure sets WF attributes that are needed for
10276  *          Buyer's FYI notification.
10277  *
10278  ************************************************************************/
10279 PROCEDURE Set_Buyer_FYI_Notif_Attributes( itemtype    IN VARCHAR2,
10280                                           itemkey     IN VARCHAR2,
10281                                           actid       IN NUMBER,
10282                                           funcmode    IN VARCHAR2,
10283                                           resultout   OUT NOCOPY VARCHAR2 )
10284 IS
10285   l_po_chg_request_group_id NUMBER;
10286   l_progress  VARCHAR2(100);
10287   l_old_total NUMBER;
10288   l_new_total NUMBER;
10289   l_po_doc_id NUMBER;
10290   l_po_doc_type VARCHAR2(25);
10291   l_po_currency VARCHAR2(10);
10292   l_orgid number;
10293 
10294 BEGIN
10295 
10296   l_progress := 'PO_ReqChangeRequestWF_PVT.Set_Buyer_FYI_Notif_Attributes: 01';
10297 
10298   -- Do nothing in cancel or timeout mode
10299   if (funcmode <> wf_engine.eng_run) then
10300         resultout := wf_engine.eng_null;
10301         return;
10302   end if;
10303 
10304   l_progress := 'PO_ReqChangeRequestWF_PVT.Set_Buyer_FYI_Notif_Attributes: 02';
10305 
10306   l_po_doc_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
10307                                               itemkey  => itemkey,
10308                                               aname    => 'CURRENT_PO_HEADER_ID');
10309 
10310   l_po_doc_type := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
10311                                               itemkey  => itemkey,
10312                                               aname    => 'PO_DOCUMENT_TYPE');
10313 
10314   l_progress := 'PO_ReqChangeRequestWF_PVT.Set_Buyer_FYI_Notif_Attributes: 03';
10315 
10316   -- get po/release currency
10317   IF l_po_doc_type IN ('PO','PA') THEN
10318 
10319      SELECT currency_code
10320      INTO l_po_currency
10321      FROM po_headers_all
10322      WHERE PO_HEADER_ID = l_po_doc_id;
10323 
10324   ELSIF l_po_doc_type = 'RELEASE' THEN
10325 
10326      SELECT ph.currency_code
10327      INTO l_po_currency
10328      FROM po_releases_all pr, po_headers_all ph
10329      WHERE pr.po_release_id = l_po_doc_id
10330           and pr.po_header_id = ph.po_header_id;
10331 
10332   END IF;
10333 
10334   l_progress := 'PO_ReqChangeRequestWF_PVT.Set_Buyer_FYI_Notif_Attributes: 04';
10335 
10336   l_po_chg_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (
10337                                   itemtype => itemtype,
10338                                   itemkey  => itemkey,
10339                                   aname    => 'PO_CHANGE_REQUEST_GROUP_ID');
10340 
10341   l_progress := 'PO_ReqChangeRequestWF_PVT.Set_Buyer_FYI_Notif_Attributes: 05';
10342 
10343   l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
10344                                          itemkey  => itemkey,
10345                                          aname    => 'ORG_ID');
10346 
10347   -- get the order total (old/new)
10348   -- used by the approval notification
10349   CalculateRcoTotal( p_change_request_group_id =>l_po_chg_request_group_id,
10350                        p_org_id =>l_orgid,
10351                        p_po_currency => l_po_currency,
10352                        x_old_total =>l_old_total ,
10353                        x_new_total =>l_new_total);
10354 
10355   l_progress := 'PO_ReqChangeRequestWF_PVT.Set_Buyer_FYI_Notif_Attributes: 06';
10356 
10357   PO_WF_UTIL_PKG.SetItemAttrText ( itemtype   => itemtype,
10358                               itemkey    => itemkey,
10359                               aname      => 'OLD_PO_TOTAL',
10360                               avalue     =>
10361               to_char(l_old_total, FND_CURRENCY.GET_FORMAT_MASK(l_po_currency, 30)) || ' ' || l_po_currency);
10362 
10363   PO_WF_UTIL_PKG.SetItemAttrText ( itemtype   => itemtype,
10364                                itemkey    => itemkey,
10365                                aname      => 'NEW_PO_TOTAL',
10366                                avalue     =>
10367                to_char(l_new_total, FND_CURRENCY.GET_FORMAT_MASK(l_po_currency, 30)) || ' ' || l_po_currency);
10368 
10369 EXCEPTION when others THEN
10370   wf_core.context('PO_ReqChangeRequestWF_PVT','Set_Buyer_FYI_Notif_Attributes',l_progress);
10371   raise;
10372 END Set_Buyer_FYI_Notif_Attributes;
10373 
10374 /*************************************************************************
10375  *
10376  * Procedure: Shipmnt_Level_Changes_Wthn_Tol
10377  * Effects: This procedure will check for PO Shipment related
10378  *          Buyer Auto-Acceptance tolerance values.
10379  *
10380  ************************************************************************/
10381 FUNCTION Shipmnt_Level_Changes_Wthn_Tol(
10382   p_item_type IN varchar2,
10383   p_item_key IN varchar2,
10384   p_tolerances_tab IN po_co_tolerances_grp.tolerances_tbl_type,
10385   p_pochggrp_id IN NUMBER) RETURN VARCHAR2
10386 IS
10387   l_return_val VARCHAR2(1) := 'Y';
10388   l_api_name varchar2(30) := 'Shipmnt_Level_Changes_Wthn_Tol';
10389 BEGIN
10390 
10391   -- check for shipment quantity, shipment amount(% and Functional currency)  tolerances
10392 
10393   SELECT 'N'
10394   INTO l_return_val
10395   FROM dual
10396   WHERE exists (
10397     SELECT 'N'
10398     FROM
10399       po_change_requests pcr,   -- for quantity/amount change
10400       po_change_requests pcr1,  -- for unit price change
10401       po_lines_all pl,
10402       po_distributions_all pod
10403     WHERE pl.po_line_id = pod.po_line_id
10404       AND pcr.change_request_group_id = p_pochggrp_id
10405       AND pcr.action_type(+) = 'MODIFICATION'
10406       AND pcr.request_status(+) NOT IN ('ACCEPTED', 'REJECTED')
10407       AND pcr.request_level(+) = 'DISTRIBUTION'
10408       AND pcr.initiator(+) = 'REQUESTER'
10409       AND pcr.document_distribution_id(+) = pod.po_distribution_id
10410       AND pcr1.change_request_group_id(+) = p_pochggrp_id
10411       AND pcr1.document_line_id(+) = pl.po_line_id
10412       AND pcr1.action_type(+) = 'MODIFICATION'
10413       AND pcr1.request_status(+) NOT IN ('ACCEPTED', 'REJECTED')
10414       AND pcr1.request_level (+) = 'LINE'
10415       AND pcr1.initiator(+) = 'REQUESTER'
10416       AND pcr1.new_price(+) IS NOT NULL
10417     GROUP BY pcr.document_line_location_id
10418     HAVING
10419       ((PO_RCOTOLERANCE_PVT.changes_within_tol(
10420          sum(decode(pl.matching_basis, 'AMOUNT', pod.amount_ordered, pl.unit_price * (pod.quantity_ordered-nvl(pod.quantity_cancelled,0)))),
10421          sum(decode(pl.matching_basis, 'AMOUNT',  nvl(pcr.new_amount, pod.amount_ordered), nvl(pcr.new_quantity,pod.quantity_ordered)*nvl(pcr1.new_price, pl.unit_price))),
10422          p_tolerances_tab(TOL_SHIPAMT_IND).max_increment,
10423          p_tolerances_tab(TOL_SHIPAMT_IND).max_decrement,
10424          p_tolerances_tab(TOL_SHIPAMT_AMT_IND).max_increment,
10425          p_tolerances_tab(TOL_SHIPAMT_AMT_IND).max_decrement) = 'N')
10426            OR
10427        (PO_RCOTOLERANCE_PVT.change_within_tol_percent(
10428          sum(pod.quantity_ordered-nvl(pod.quantity_cancelled,0)),
10429          sum(nvl(pcr.new_quantity, pod.quantity_ordered-nvl(pod.quantity_cancelled, 0))),
10430          p_tolerances_tab(TOL_SHIPQTY_IND).max_increment,
10431          p_tolerances_tab(TOL_SHIPQTY_IND).max_decrement) = 'N')));
10432 
10433   IF (g_po_wf_debug = 'Y') THEN
10434     PO_WF_DEBUG_PKG.insert_debug(p_item_type, p_item_key, l_api_name || ' Return Value:' || l_return_val);
10435   END IF;
10436 
10437   RETURN l_return_val;
10438 
10439   EXCEPTION
10440     WHEN NO_DATA_FOUND THEN
10441 
10442       IF (g_po_wf_debug = 'Y') THEN
10443         PO_WF_DEBUG_PKG.insert_debug(p_item_type, p_item_key, l_api_name || ' Return Value(No data Found):Y');
10444       END IF;
10445 
10446       RETURN 'Y';
10447 
10448 END Shipmnt_Level_Changes_Wthn_Tol;
10449 
10450 
10451 /*************************************************************************
10452  *
10453  * Procedure: Line_Level_Changes_Wthn_Tol
10454  * Effects: This procedure will check for PO Line related
10455  *          Buyer Auto-Acceptance tolerance values.
10456  *
10457  ************************************************************************/
10458 FUNCTION Line_Level_Changes_Wthn_Tol (
10459   p_item_type varchar2,
10460   p_item_key varchar2,
10461   p_tolerances_tab IN po_co_tolerances_grp.tolerances_tbl_type,
10462   p_pochggrp_id IN NUMBER ) RETURN varchar2
10463 IS
10464   l_return_val varchar2(1) := 'Y';
10465   l_api_name varchar2(30) := 'Line_Level_Changes_Wthn_Tol';
10466 
10467   l_old_amount number:=0;
10468   l_new_amount number:=0;
10469 
10470   l_doc_line_id number;
10471   l_document_type varchar2(30);
10472 
10473   l_progress varchar2(6);
10474 
10475   cursor l_line_info_csr is
10476     select distinct pcr.document_line_id, document_type
10477     from po_change_requests pcr,
10478          po_lines_all pol
10479     where change_request_group_id = p_pochggrp_id
10480           and pol.po_line_id = pcr.document_line_id
10481           and action_type <> 'DERIVED'
10482           and request_status not in ('ACCEPTED', 'REJECTED')
10483           and pcr.initiator(+) = 'REQUESTER'
10484           and pcr.request_level <> 'DISTRIBUTION';
10485 
10486 BEGIN
10487 
10488   -- check for need by date changes
10489   l_progress:= '001';
10490 
10491   BEGIN
10492 
10493   SELECT 'N'
10494   INTO l_return_val
10495   FROM dual
10496   WHERE exists (
10497     SELECT 'N'
10498     FROM po_change_requests pcr
10499     WHERE change_request_group_id = p_pochggrp_id
10500       AND action_type='MODIFICATION'
10501       AND request_status not in ('ACCEPTED', 'REJECTED')
10502       AND request_level='SHIPMENT'
10503       AND
10504         PO_RCOTOLERANCE_PVT.change_within_tol_date(
10505           old_need_by_date,
10506           new_need_by_date,
10507           p_tolerances_tab(TOL_NEEDBY_IND).max_increment,
10508           p_tolerances_tab(TOL_NEEDBY_IND).max_decrement) = 'N');
10509 
10510   EXCEPTION
10511     WHEN NO_DATA_FOUND THEN
10512       l_return_val := 'Y';
10513   END;
10514 
10515   IF (g_po_wf_debug = 'Y') THEN
10516     PO_WF_DEBUG_PKG.insert_debug(p_item_type, p_item_key, l_api_name || ' Return Value(need by date check):' || l_return_val);
10517   END IF;
10518 
10519   l_progress:= '002';
10520 
10521   IF (l_return_val <> 'N') THEN
10522 
10523     BEGIN
10524       SELECT 'N'
10525       INTO l_return_val
10526       FROM dual
10527       WHERE exists (
10528         SELECT 'N'
10529         FROM po_change_requests pcr
10530         WHERE change_request_group_id = p_pochggrp_id
10531           AND action_type='MODIFICATION'
10532           AND request_status not in ('ACCEPTED', 'REJECTED')
10533           AND request_level='LINE'
10534           AND (
10535         (PO_RCOTOLERANCE_PVT.change_within_tol_date(
10536            old_start_date,
10537            new_start_date,
10538            p_tolerances_tab(TOL_STARTDATE_IND).max_increment,
10539            p_tolerances_tab(TOL_STARTDATE_IND).max_decrement) = 'N')
10540         OR
10541         (PO_RCOTOLERANCE_PVT.change_within_tol_date(
10542            old_expiration_date,
10543            new_expiration_date,
10544            p_tolerances_tab(TOL_ENDDATE_IND).max_increment,
10545            p_tolerances_tab(TOL_ENDDATE_IND).max_decrement) = 'N')
10546         OR
10547         (PO_RCOTOLERANCE_PVT.change_within_tol_percent(
10548            old_price,
10549            new_price,
10550            p_tolerances_tab(TOL_UNITPRICE_IND).max_increment,
10551            p_tolerances_tab(TOL_UNITPRICE_IND).max_decrement) = 'N'))
10552         );
10553 
10554     EXCEPTION
10555       WHEN NO_DATA_FOUND THEN
10556         l_return_val := 'Y';
10557     END;
10558   END IF;
10559 
10560   IF (g_po_wf_debug = 'Y') THEN
10561     PO_WF_DEBUG_PKG.insert_debug(p_item_type, p_item_key, l_api_name || ' Return Value(start/end date check):' || l_return_val);
10562   END IF;
10563 
10564   l_progress:= '003';
10565 
10566   IF (l_return_val <> 'N') THEN
10567 
10568     -- check for line amount tolerances (funccur and percent)
10569 
10570     l_return_val := 'Y';
10571     open l_line_info_csr;
10572         loop
10573              fetch l_line_info_csr
10574              into  l_doc_line_id, l_document_type;
10575              exit when l_line_info_csr%NOTFOUND;
10576 
10577              l_progress:= '004';
10578 
10579              if (l_document_type = 'PO') then
10580                get_po_line_amount( p_chg_request_grp_id=>p_pochggrp_id,
10581                                    p_po_line_id => l_doc_line_id,
10582                                    x_old_amount => l_old_amount,
10583                                    x_new_amount => l_new_amount ) ;
10584 
10585 
10586              l_progress:= '005';
10587 
10588              -- RELEASE has no lines. All tolerance check against release should be done
10589              -- in either shipment level or document level.
10590              elsif (l_document_type = 'RELEASE') then
10591 
10592                 l_return_val := 'Y';
10593 
10594              end if;
10595              l_progress:= '006';
10596 
10597 
10598              l_return_val := PO_RCOTOLERANCE_PVT.changes_within_tol(
10599                l_old_amount,
10600                l_new_amount,
10601                p_tolerances_tab(TOL_LINEAMT_IND).max_increment,
10602                p_tolerances_tab(TOL_LINEAMT_IND).max_decrement,
10603                p_tolerances_tab(TOL_LINEAMT_AMT_IND).max_increment,
10604                p_tolerances_tab(TOL_LINEAMT_AMT_IND).max_decrement );
10605 
10606              l_progress:= '007';
10607 
10608             EXIT WHEN (l_return_val = 'N');
10609         end loop;
10610     close l_line_info_csr;
10611 
10612     l_progress:= '008';
10613 
10614   END IF;
10615 
10616   IF (g_po_wf_debug = 'Y') THEN
10617     PO_WF_DEBUG_PKG.insert_debug(p_item_type, p_item_key, l_api_name || ' Return Value(line amount check):' || l_return_val);
10618   END IF;
10619 
10620   RETURN l_return_val;
10621 
10622 EXCEPTION
10623   WHEN OTHERS THEN
10624     l_return_val := 'N';
10625 
10626     IF (g_po_wf_debug = 'Y') THEN
10627       PO_WF_DEBUG_PKG.insert_debug(p_item_type, p_item_key, l_api_name || ' Return Value(line level check):' || l_return_val);
10628 
10629     raise;
10630     END IF;
10631 
10632 END Line_Level_Changes_Wthn_Tol;
10633 
10634 /*************************************************************************
10635  *
10636  * Procedure: Doc_Level_Changes_Wthn_Tol
10637  * Effects: This procedure will check for PO Line related
10638  *          Buyer Auto-Acceptance tolerance values.
10639  *
10640  ************************************************************************/
10641 FUNCTION Doc_Level_Changes_Wthn_Tol(
10642   p_item_type varchar2,
10643   p_item_key varchar2,
10644   p_tolerances_tab IN po_co_tolerances_grp.tolerances_tbl_type,
10645   p_pochggrp_id IN NUMBER,
10646   p_poheader_id IN NUMBER) RETURN VARCHAR2
10647 IS
10648   l_return_val varchar2(1) := 'Y';
10649   l_api_name varchar2(30) := 'Doc_Level_Changes_Wthn_Tol';
10650 
10651   l_document_type varchar(100);
10652   l_old_amount_release number;
10653   l_new_amount_release number;
10654 
10655 BEGIN
10656 
10657   select distinct document_type
10658   into l_document_type
10659   from po_change_requests
10660   where change_request_group_id =  p_pochggrp_id;
10661 
10662   if ( l_document_type = 'PO' ) then
10663 
10664   SELECT PO_RCOTOLERANCE_PVT.changes_within_tol(
10665     sum(decode(pl.matching_basis,
10666                'AMOUNT',
10667                pod.amount_ordered,
10668                pl.unit_price * (pod.quantity_ordered-nvl(pod.quantity_cancelled,0)))),
10669     sum(decode(pcr2.action_type, 'CANCELLATION',
10670                0,
10671                decode(pl.matching_basis,
10672                       'AMOUNT',
10673                       decode(pcr2.action_type,
10674                              'CANCELLATION',
10675                              0,
10676                              nvl(pcr.new_amount, pod.amount_ordered)),
10677                       nvl(pcr.new_quantity, pod.quantity_ordered) * nvl(pcr1.new_price, pl.unit_price)))),
10678     p_tolerances_tab(TOL_POTOTAL_IND).max_increment,
10679     p_tolerances_tab(TOL_POTOTAL_IND).max_decrement,
10680     p_tolerances_tab(TOL_POTOTAL_AMT_IND).max_increment,
10681     p_tolerances_tab(TOL_POTOTAL_AMT_IND).max_decrement)  INTO l_return_val
10682   FROM
10683     po_change_requests pcr,
10684     po_change_requests pcr1,
10685     po_change_requests pcr2,
10686     po_lines_all pl,
10687     po_distributions_all pod
10688   WHERE  pl.po_line_id = pod.po_line_id
10689     AND pcr.change_request_group_id(+) = p_pochggrp_id
10690     AND pcr.action_type(+) = 'MODIFICATION'
10691     AND pcr.request_status(+) NOT IN ('ACCEPTED', 'REJECTED')
10692     AND pcr.request_level(+) = 'DISTRIBUTION'
10693     AND pcr.initiator(+) = 'REQUESTER'
10694     AND pcr.document_distribution_id(+) = pod.po_distribution_id
10695 --    AND pcr.document_line_id = pcr1.document_line_id
10696     AND pcr1.change_request_group_id(+) = p_pochggrp_id
10697     AND pl.po_header_id = p_poheader_id
10698     AND pcr1.document_line_id(+) = pl.po_line_id
10699     AND pcr1.action_type(+) = 'MODIFICATION'
10700     AND pcr1.request_status(+) NOT IN ('ACCEPTED', 'REJECTED')
10701     AND pcr1.request_level (+) = 'LINE'
10702     AND pcr1.initiator(+) = 'REQUESTER'
10703     AND pcr1.new_price(+) IS NOT NULL
10704     AND pcr2.change_request_group_id(+) = p_pochggrp_id
10705     AND pcr2.document_line_id(+) = pl.po_line_id
10706     AND pcr2.action_type(+) = 'CANCELLATION';
10707 
10708    elsif ( l_document_type = 'RELEASE') then
10709 
10710     SELECT
10711     sum ( nvl(pcr.old_quantity, nvl(pll.quantity,0))
10712                            *pll.price_override ),
10713     sum ( decode ( pcr.document_line_location_id, null, 0,
10714             decode (pcr.action_type , 'CANCELLATION', 0,
10715     PO_ReqChangeRequestNotif_PVT.get_goods_shipment_new_amount
10716     (  pol.org_id,p_pochggrp_id,pol.po_line_id,
10717        pol.item_id,pll.unit_meas_lookup_code,
10718        nvl(pcr.old_price, nvl(pll.price_override, pol.unit_price)),
10719        pll.line_location_id)) ) )
10720 
10721     into l_old_amount_release, l_new_amount_release
10722 
10723     FROM  po_change_requests pcr,
10724           po_lines_all pol,
10725           po_line_locations_all pll
10726     WHERE pcr.change_request_group_id= p_pochggrp_id
10727     AND   pcr.po_release_id = p_poheader_id
10728     AND   pcr.document_line_id = pol.po_line_id
10729     AND   pcr.request_status NOT IN ('ACCEPTED', 'REJECTED')
10730     AND   pcr.document_line_location_id =pll.line_location_id (+)
10731     AND   pcr.request_level<>'DISTRIBUTION'
10732     AND   pcr.initiator(+) = 'REQUESTER';
10733 
10734    l_return_val:= PO_RCOTOLERANCE_PVT.changes_within_tol(
10735     l_old_amount_release,
10736     l_new_amount_release,
10737     p_tolerances_tab(TOL_POTOTAL_IND).max_increment,
10738     p_tolerances_tab(TOL_POTOTAL_IND).max_decrement,
10739     p_tolerances_tab(TOL_POTOTAL_AMT_IND).max_increment,
10740     p_tolerances_tab(TOL_POTOTAL_AMT_IND).max_decrement);
10741 
10742   end if;
10743 
10744   IF (g_po_wf_debug = 'Y') THEN
10745     PO_WF_DEBUG_PKG.insert_debug(p_item_type, p_item_key, l_api_name || ' Return Value:' || l_return_val);
10746   END IF;
10747 
10748   RETURN l_return_val;
10749 
10750   EXCEPTION
10751     WHEN NO_DATA_FOUND THEN
10752 
10753     IF (g_po_wf_debug = 'Y') THEN
10754       PO_WF_DEBUG_PKG.insert_debug(p_item_type, p_item_key, l_api_name || ' Return Value(No Data Found):Y');
10755     END IF;
10756       RETURN 'Y';
10757 END Doc_Level_Changes_Wthn_Tol;
10758 
10759 /*************************************************************************
10760  *
10761  * Public Procedure: Changes_Wthn_Buyer_Tol_Values
10762  * Effects: This procedure will check for the Buyer Auto-Acceptance
10763  *          tolerance values and will decide whetehr buyer needs to be
10764  *          skipped or not.
10765  *
10766  ************************************************************************/
10767 PROCEDURE Changes_Wthn_Buyer_Tol_Values( itemtype IN VARCHAR2,
10768                                          itemkey IN VARCHAR2,
10769                                          actid IN NUMBER,
10770                                          funcmode IN VARCHAR2,
10771                                          resultout OUT NOCOPY VARCHAR2 ) IS
10772   l_return_val VARCHAR2(1) := 'Y';
10773   l_pochggrp_id NUMBER;
10774   l_poheader_id NUMBER;
10775   l_tolerances_tab po_co_tolerances_grp.tolerances_tbl_type;
10776   l_api_name varchar2(30) := 'Changes_Wthn_Buyer_Tol_Values';
10777   l_progress VARCHAR2(100) := '000';
10778 
10779   l_po_currency_code  PO_HEADERS_ALL.currency_code%type;
10780   l_functional_currency_code gl_sets_of_books.currency_code%TYPE;
10781   l_org_id number;
10782   l_po_doc_type VARCHAR2(25);
10783 
10784 BEGIN
10785 
10786   -- Do nothing in cancel or timeout mode
10787   IF (funcmode <> wf_engine.eng_run) THEN
10788     resultout := wf_engine.eng_null;
10789     RETURN;
10790   END IF;
10791 
10792   l_pochggrp_id := PO_WF_UTIL_PKG.GetItemAttrNumber( itemtype => itemtype,
10793                                                 itemkey  => itemkey,
10794                                                 aname    => 'PO_CHANGE_REQUEST_GROUP_ID');
10795   l_poheader_id := PO_WF_UTIL_PKG.GetItemAttrNumber( itemtype => itemtype,
10796                                                 itemkey  => itemkey,
10797                                                 aname    => 'CURRENT_PO_HEADER_ID');
10798 
10799   IF (g_po_wf_debug = 'Y') THEN
10800     PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey, l_api_name || '.Begin');
10801     PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey, 'l_pochggrp_id:' || l_pochggrp_id || ' l_poheader_id:' || l_poheader_id);
10802   END IF;
10803 
10804   l_progress := '001';
10805 
10806   IF (  l_poheader_id is not null ) THEN
10807     -- bug 5363103
10808     -- To simply the logic, if PO is created in txn currency, RCO always needs buyer approval.
10809 
10810     l_po_doc_type := PO_WF_UTIL_PKG.GetItemAttrText( itemtype => itemtype,
10811                                                 itemkey  => itemkey,
10812                                                 aname    => 'PO_DOCUMENT_TYPE');
10813 
10814     -- For releases, the current doc header id is the release id
10815     if (l_po_doc_type = 'RELEASE') then
10816       select poh.currency_code, poh.org_id
10817       into l_po_currency_code, l_org_id
10818       from po_headers_all poh, po_releases_all pr
10819       where pr.po_release_id = l_poheader_id
10820       and poh.po_header_id = pr.po_header_id;
10821     else
10822       select poh.currency_code, poh.org_id
10823       into l_po_currency_code, l_org_id
10824       from po_headers_all poh
10825       where poh.po_header_id = l_poheader_id;
10826     end if;
10827 
10828      l_progress := '002';
10829 
10830     SELECT sob.currency_code
10831     INTO  l_functional_currency_code
10832     FROM  gl_sets_of_books sob, financials_system_params_all fsp
10833     WHERE fsp.org_id = l_org_id
10834     AND  fsp.set_of_books_id = sob.set_of_books_id;
10835 
10836      l_progress := '003';
10837     if (l_functional_currency_code <> l_po_currency_code) then
10838         l_return_val := 'N';
10839     end if;
10840 
10841     PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey, 'doc type: '||l_po_doc_type|| ' func currency code: '||l_functional_currency_code||' po curr code: '||l_po_currency_code );
10842   END IF;
10843 
10844 
10845   IF (l_pochggrp_id IS NOT NULL) THEN
10846 
10847     l_progress := '004';
10848 
10849     -- check for shipment level changes
10850     IF (l_return_val <> 'N') THEN
10851        l_return_val := Shipmnt_Level_Changes_Wthn_Tol(itemtype, itemkey, g_tolerances_tbl, l_pochggrp_id);
10852 
10853        IF (g_po_wf_debug = 'Y') THEN
10854          PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey, 'Return Value(shipmnt_level_changes_wthn_tol):' || l_return_val);
10855        END IF;
10856     END IF;
10857 
10858 
10859     IF (l_return_val <> 'N') THEN
10860        -- check for line level changes
10861        l_return_val := Line_Level_Changes_Wthn_Tol(itemtype, itemkey, g_tolerances_tbl, l_pochggrp_id);
10862       IF (g_po_wf_debug = 'Y') THEN
10863         PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey, 'Return Value(line_level_changes_wthn_tol):' || l_return_val);
10864       END IF;
10865     END IF;
10866 
10867     IF (l_return_val <> 'N') THEN
10868       -- check for document level changes
10869       l_return_val := Doc_Level_Changes_Wthn_Tol(itemtype, itemkey, g_tolerances_tbl, l_pochggrp_id, l_poheader_id);
10870       IF (g_po_wf_debug = 'Y') THEN
10871         PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey, 'Return Value(doc_level_changes_wthn_tol):' || l_return_val);
10872       END IF;
10873     END IF;
10874 
10875     IF (g_po_wf_debug = 'Y') THEN
10876       PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey, l_api_name || ' Return Value:' || l_return_val);
10877     END IF;
10878 
10879     -- set result value
10880     resultout := wf_engine.eng_completed || ':' || l_return_val;
10881 
10882   END IF; -- IF ( p_funcmode = 'RUN' )
10883 
10884 EXCEPTION WHEN OTHERS THEN
10885   -- if something is wrong, just assume it needs approval
10886   IF (g_po_wf_debug = 'Y') THEN
10887     PO_WF_DEBUG_PKG.insert_debug(itemtype, itemkey, l_progress || 'SQL ERROR:' || sqlerrm);
10888   END IF;
10889   resultout := wf_engine.eng_completed || ':N';
10890 END Changes_Wthn_Buyer_Tol_values;
10891 
10892 /*************************************************************************
10893  * Public Procedure: More_Req_To_Process
10894  *
10895  * Effects: This procedure will check whether all requisitions
10896  *          are processed for a approved PO change.
10897  *
10898  * Returns: Yes if there is more Req to be processed
10899  ************************************************************************/
10900 PROCEDURE More_Req_To_Process( itemtype        in varchar2,
10901                                itemkey         in varchar2,
10902                                actid           in number,
10903                                funcmode        in varchar2,
10904                                resultout       out NOCOPY varchar2 )
10905 IS
10906 
10907   l_req_request_group_id NUMBER;
10908   l_po_request_group_id  NUMBER;
10909   l_next_req_grp_id      NUMBER;
10910   l_document_id          NUMBER;
10911   l_document_num         po_change_requests.document_num%type;
10912   l_result               VARCHAR2(1) := 'N';
10913   l_progress             VARCHAR2(100);
10914 
10915 BEGIN
10916   -- Do nothing in cancel or timeout mode
10917   IF (funcmode <> wf_engine.eng_run) THEN
10918 
10919     resultout := wf_engine.eng_null;
10920     return;
10921 
10922   END IF;
10923 
10924   l_progress := 'PO_ReqChangeRequestWF_PVT.More_Req_To_Process: 01';
10925 
10926   l_req_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (
10927                               itemtype   => itemtype,
10928                               itemkey    => itemkey,
10929                               aname      => 'REQ_CHANGE_GROUP_ID');
10930 
10931   l_progress := 'PO_ReqChangeRequestWF_PVT.More_Req_To_Process: 02';
10932 
10933   l_po_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (
10934                               itemtype   => itemtype,
10935                               itemkey    => itemkey,
10936                               aname      => 'CHANGE_REQUEST_GROUP_ID');
10937 
10938   l_progress := 'PO_ReqChangeRequestWF_PVT.More_Req_To_Process: 03';
10939 
10940   SELECT MIN(req_change.change_request_group_id)
10941   INTO l_next_req_grp_id
10942   FROM
10943     po_change_requests po_change,
10944     po_change_requests req_change
10945   WHERE
10946     po_change.change_request_group_id = l_po_request_group_id AND
10947     po_change.parent_change_request_id = req_change.change_request_id AND
10948     req_change.change_request_group_id > l_req_request_group_id;
10949 
10950   l_progress := 'PO_ReqChangeRequestWF_PVT.More_Req_To_Process: 04';
10951 
10952   -- when next po change request group id is found
10953   IF (l_next_req_grp_id IS NOT NULL) THEN
10954 
10955     PO_WF_UTIL_PKG.SetItemAttrNumber( itemtype   => itemtype,
10956                                     itemkey    => itemkey,
10957                                  aname      => 'REQ_CHANGE_GROUP_ID',
10958                                  avalue     => l_next_req_grp_id );
10959 
10960     SELECT document_header_id, document_num
10961     INTO l_document_id, l_document_num
10962     FROM po_change_requests
10963     WHERE change_request_group_id = l_next_req_grp_id AND rownum=1;
10964 
10965     PO_WF_UTIL_PKG.SetItemAttrNumber( itemtype   => itemtype,
10966                                     itemkey    => itemkey,
10967                                  aname      => 'REQ_HEADER_ID',
10968                                  avalue     => l_document_id );
10969 
10970     PO_WF_UTIL_PKG.SetItemAttrText( itemtype   => itemtype,
10971                                   itemkey    => itemkey,
10972                                aname      => 'REQUISITION_NUMBER',
10973                                avalue     => l_document_num );
10974     l_result := 'Y';
10975   END IF;
10976 
10977   resultout := wf_engine.eng_completed || ':' || l_result;
10978 
10979 EXCEPTION when others THEN
10980   wf_core.context('PO_ReqChangeRequestWF_PVT','More_Req_To_Process',l_progress);
10981   raise;
10982 END More_Req_To_Process;
10983 
10984 /*************************************************************************
10985  *
10986  * Public Procedure: Start_Notify_Requester_Process
10987  * Effects: This procedure will call Start_NotifyRequesterProcess
10988  *          to launch NOTIFY_REQUESTER_PROCESS
10989  *          process in porpocha.
10990  *
10991  ************************************************************************/
10992 PROCEDURE Start_Notify_Requester_Process( itemtype   IN VARCHAR2,
10993                                           itemkey    IN VARCHAR2,
10994                                           actid      IN NUMBER,
10995                                           funcmode   IN VARCHAR2,
10996                                           resultout  OUT NOCOPY VARCHAR2 )
10997 IS
10998   l_orgid                      NUMBER;
10999   l_po_change_request_group_id NUMBER;
11000   l_progress                   VARCHAR2(100);
11001 BEGIN
11002 
11003   l_progress := 'PO_ReqChangeRequestWF_PVT.Start_Notify_Requester_Process : 01';
11004 
11005   IF (funcmode <> wf_engine.eng_run) THEN
11006         resultout := wf_engine.eng_null;
11007         return;
11008   END IF;
11009 
11010   -- Set the multi-org context
11011   l_orgid := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
11012                                           itemkey  => itemkey,
11013                                           aname    => 'ORG_ID');
11014 
11015   l_progress := 'PO_ReqChangeRequestWF_PVT.Start_Notify_Requester_Process: 02';
11016 
11017   IF l_orgid is NOT NULL THEN
11018     PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
11019   END IF;
11020 
11021   l_progress := 'PO_ReqChangeRequestWF_PVT.Start_Notify_Requester_Process: 03';
11022 
11023   l_po_change_request_group_id := PO_WF_UTIL_PKG.GetItemAttrNumber (
11024                      itemtype => itemtype,
11025                      itemkey  => itemkey,
11026                      aname    => 'PO_CHANGE_REQUEST_GROUP_ID');
11027 
11028   Start_NotifyRequesterProcess(l_po_change_request_group_id, itemtype, itemkey);
11029 
11030   l_progress := 'PO_ReqChangeRequestWF_PVT.Start_Notify_Requester_Process: 04';
11031 
11032 EXCEPTION when others THEN
11033   wf_core.context('PO_ReqChangeRequestWF_PVT','Start_Notify_Requester_Process',l_progress || 'SQL ERROR:' || sqlerrm);
11034   RAISE;
11035 END Start_Notify_Requester_Process;
11036 
11037 /*************************************************************************
11038  *
11039  * Public Procedure: Start_NotifyRequesterProcess
11040  * Effects: This procedure will call StartPOChangeWF
11041  *          to launch NOTIFY_REQUESTER_PROCESS
11042  *          process in porpocha
11043  *
11044  ************************************************************************/
11045 PROCEDURE Start_NotifyRequesterProcess( p_po_change_request_group_id IN NUMBER,
11046                                         p_req_item_type IN VARCHAR2,
11047                                         p_req_item_key  IN VARCHAR2 )
11048 IS
11049 
11050   l_progress VARCHAR2(100):= '000';
11051   l_count NUMBER;
11052   l_item_key VARCHAR2(240);
11053   l_item_type VARCHAR2(8):='PORPOCHA';
11054   l_forward_from_username VARCHAR2(200);
11055   l_user_id NUMBER;
11056   l_application_id NUMBER;
11057   l_responsibility_id NUMBER;
11058 
11059 BEGIN
11060 
11061   l_progress := 'PO_ReqChangeRequestWF_PVT.Start_NotifyRequesterProcess: 01' || ' P_PO_CHANGE_REQUEST_GROUP_ID:' || p_po_change_request_group_id;
11062 
11063   SELECT po_requester_change_wf_s.nextval INTO l_count FROM dual;
11064 
11065   l_item_key:='NOTIFREQ_'||to_char(p_po_change_request_group_id)||'_'||to_char(l_count);
11066 
11067   l_progress := 'PO_ReqChangeRequestWF_PVT.Start_NotifyRequesterProcess: 02' || ' ITEM KEY:' || l_item_key;
11068 
11069   l_forward_from_username:= PO_WF_UTIL_PKG.GetItemAttrText(
11070                               itemtype=>p_req_item_type,
11071                               itemkey=>p_req_item_key,
11072                               aname =>'RESPONDER_USER_NAME');
11073 
11074   l_user_id := PO_WF_UTIL_PKG.GetItemAttrNumber( itemtype => p_req_item_type,
11075                                             itemkey  => p_req_item_key,
11076                                             aname    => 'USER_ID');
11077 
11078   l_responsibility_id := PO_WF_UTIL_PKG.GetItemAttrNumber(
11079                                          itemtype => p_req_item_type,
11080                                          itemkey  => p_req_item_key,
11081                                          aname    => 'RESPONSIBILITY_ID');
11082 
11083   l_application_id := PO_WF_UTIL_PKG.GetItemAttrNumber(
11084                                          itemtype => p_req_item_type,
11085                                          itemkey  => p_req_item_key,
11086                                          aname    => 'APPLICATION_ID');
11087 
11088   StartPOChangeWF(p_po_change_request_group_id, l_item_key, 'NOTIFY_REQUESTER_PROCESS', l_forward_from_username, l_user_id, l_responsibility_id, l_application_id);
11089 
11090 EXCEPTION when others THEN
11091     wf_core.context('PO_ReqChangeRequestWF_PVT','Start_NotifyRequesterProcess',l_progress || 'SQL ERROR:' || sqlerrm);
11092     RAISE;
11093 END Start_NotifyRequesterProcess;
11094 
11095 
11096 /**************************************************************************
11097  * This function is used to format a number according to the formatting   *
11098  * currency. The formatting is used while adding up the old and the new   *
11099  * totals for the buyer approval notifications. The formatting is applied *
11100  * only on the fractional part of the number. The integer portion is      *
11101  * returned unformatted. This is done as the formatted total needs to be  *
11102  * added and hence, has to be returned as a number.                       *
11103  **************************************************************************/
11104 FUNCTION get_formatted_total( l_total NUMBER, l_po_currency VARCHAR2)
11105 RETURN NUMBER IS
11106 
11107  l_total_trunc NUMBER;
11108 
11109 BEGIN
11110 
11111 --truncate the number to get the integer portion of the number
11112 l_total_trunc := trunc(l_total);
11113 
11114 -- add the unformatted integer portion to the formatted fractional portion of the number and return the same
11115 return (l_total_trunc + to_number(to_char(l_total - l_total_trunc,
11116                                           fnd_currency.get_format_mask(l_po_currency,
11117                                                                        g_currency_format_mask))));
11118 
11119 END get_formatted_total;
11120 
11121 /***************************************************************************
11122  * This procedure is used for getting old/new line amount for a po line.   *
11123  * It is used in buyer auto approval tolerance checking related to         *
11124  * LINE_AMOUNT.                                                            *
11125  **************************************************************************/
11126 PROCEDURE get_po_line_amount( p_chg_request_grp_id IN NUMBER,
11127                               p_po_line_id IN NUMBER,
11128                               x_old_amount OUT NOCOPY VARCHAR2,
11129                               x_new_amount OUT NOCOPY VARCHAR2 )
11130 IS
11131 
11132 l_po_matching_basis varchar2(20);
11133 l_old_amount number:=0;
11134 l_new_amount number:=0;
11135 l_new_price number;
11136 l_pcr_new_price number;
11137 l_new_quantity number;
11138 l_pcr_new_quantity number;
11139 l_progress varchar2(6);
11140 
11141 l_pcr_line_loc_id number;
11142 l_has_shipment_chg varchar(1):='N';
11143 l_shipmt_amt_increase number:=0;
11144 l_line_amt_increase number:=0;
11145 l_po_org_id number;
11146 l_item_id number;
11147 l_unit_price number;
11148 l_unit_lookup_code varchar2(25);
11149 l_price_override number;
11150 l_action_type varchar2(30);
11151 l_old_shipment_amt number;
11152 
11153 cursor l_shipment_info_csr ( p_chg_request_grp_id in number,
11154                               p_po_line_id in number)  is
11155 
11156 select document_line_location_id,action_type,
11157        decode(pol.matching_basis, 'AMOUNT',
11158                   ( nvl(pll.amount,0)-nvl(pll.amount_cancelled,0) ),
11159                   ( nvl(pll.quantity,0)- nvl(pll.quantity_cancelled,0)) * pol.unit_price
11160               )
11161    from po_change_requests,
11162         po_lines_all pol,
11163         po_line_locations_all pll
11164    where change_request_group_id = p_chg_request_grp_id
11165           and document_line_id = p_po_line_id
11166           and document_line_location_id = pll.line_location_id
11167           and pol.po_line_id = document_line_id
11168           and request_status not in ('ACCEPTED', 'REJECTED')
11169           and request_level = 'SHIPMENT'
11170           and action_type <> 'DERIVED' ;
11171 
11172 BEGIN
11173 
11174     l_progress := '001';
11175 
11176     select matching_basis,org_id,item_id,unit_price
11177     into l_po_matching_basis,l_po_org_id,l_item_id,l_unit_price
11178     from po_lines_all
11179     where po_line_id = p_po_line_id;
11180 
11181     l_progress := '002';
11182 
11183     -- get old po line amount
11184     select sum( decode(l_po_matching_basis, 'AMOUNT',
11185                   ( nvl(pll.amount,0)-nvl(pll.amount_cancelled,0) ),
11186                   ( nvl(pll.quantity,0)- nvl(pll.quantity_cancelled,0)) * pol.unit_price
11187                   )
11188                )
11189     into l_old_amount
11190     FROM   po_lines_all pol,
11191            po_line_locations_all pll
11192     WHERE pol.po_line_id =  p_po_line_id
11193     AND pol.po_line_id = pll.po_line_id;
11194 
11195     l_progress := '003';
11196 
11197     -- get new po line amount
11198 
11199     -- First, get price for quantity based line before entering the shipment cursor.
11200     -- Note: Price change is at line level. For performance consideration, we get price
11201     -- outside the shipment cursor.
11202 
11203     if ( l_po_matching_basis = 'QUANTITY' ) then
11204           begin
11205             SELECT pcr.new_price,pcr.new_price
11206             into l_pcr_new_price,l_new_price
11207             FROM po_change_requests pcr
11208 
11209             WHERE pcr.change_request_group_id = p_chg_request_grp_id
11210             AND pcr.document_line_id =  p_po_line_id
11211             AND pcr.action_type(+) = 'MODIFICATION'
11212             AND pcr.request_status(+) NOT IN ('ACCEPTED', 'REJECTED')
11213             AND pcr.initiator(+) = 'REQUESTER'
11214             AND pcr.request_level = 'LINE'
11215             AND pcr.new_price is not null;
11216 
11217           -- No need to go to check pll.PRICE_OVERRIDE since for SPO shipments, pll.PRICE_OVERRIDE always equals the purchase order line price.
11218           exception
11219           when no_data_found then
11220             select pol.unit_price
11221             into l_new_price
11222             from po_lines_all pol
11223             where pol.po_line_id = p_po_line_id;
11224 
11225             l_pcr_new_price := null;
11226           end;
11227     end if;
11228 
11229     l_progress:='004';
11230 
11231     open l_shipment_info_csr ( p_chg_request_grp_id,p_po_line_id ) ;
11232       loop
11233           fetch l_shipment_info_csr
11234           into l_pcr_line_loc_id,
11235                l_action_type,
11236                l_old_shipment_amt;
11237           exit when l_shipment_info_csr%NOTFOUND;
11238 
11239       l_has_shipment_chg := 'Y';
11240 
11241       if (l_action_type = 'CANCELLATION') then
11242 
11243          l_shipmt_amt_increase:= l_old_shipment_amt * (-1);
11244 
11245 
11246       elsif (l_action_type = 'MODIFICATION') then
11247 
11248         if ( l_po_matching_basis = 'AMOUNT' ) then
11249 
11250            begin
11251              select (pcr.new_amount - pll.amount)
11252              into l_shipmt_amt_increase
11253              from po_change_requests pcr,
11254                   po_line_locations_all pll
11255              where pcr.change_request_group_id =  p_chg_request_grp_id
11256              and pcr.document_line_location_id =  l_pcr_line_loc_id
11257              and pll.line_location_id =  l_pcr_line_loc_id
11258              and pcr.request_level = 'SHIPMENT'
11259              and pcr.request_status(+) NOT IN ('ACCEPTED', 'REJECTED')
11260              and pcr.initiator(+) = 'REQUESTER'
11261              and pcr.new_amount is not null;
11262 
11263            exception
11264            when no_data_found then
11265              l_shipmt_amt_increase := 0;
11266 
11267            end;
11268 
11269            l_progress := '005';
11270 
11271         elsif ( l_po_matching_basis = 'QUANTITY' ) then
11272 
11273           -- get new quantity
11274           begin
11275             SELECT pcr.new_quantity,pcr.new_quantity,pll.unit_meas_lookup_code,pll.price_override
11276             into l_pcr_new_quantity,l_new_quantity,l_unit_lookup_code,l_price_override
11277             FROM po_change_requests pcr,
11278                  po_line_locations_all pll
11279             WHERE pcr.change_request_group_id = p_chg_request_grp_id
11280             AND pcr.document_line_location_id =  l_pcr_line_loc_id
11281             AND pll.line_location_id =  l_pcr_line_loc_id
11282             AND pcr.action_type(+) = 'MODIFICATION'
11283             AND pcr.request_status(+) NOT IN ('ACCEPTED', 'REJECTED')
11284             AND pcr.initiator(+) = 'REQUESTER'
11285             AND pcr.request_level = 'SHIPMENT'
11286             AND pcr.new_quantity is not null;
11287 
11288          exception
11289            -- if no qty change, we get the qty from po_line_locations_all.
11290            when no_data_found then
11291              select (pll.quantity - pll.quantity_cancelled)
11292              into l_new_quantity
11293              from po_line_locations_all pll
11294              where pll.line_location_id = l_pcr_line_loc_id;
11295 
11296            l_pcr_new_quantity := null;
11297 
11298          end;
11299          l_progress := '006';
11300 
11301          -- quantity change could cause price change, call price break function to update the price
11302          if (l_pcr_new_price is null and l_pcr_new_quantity is not null ) then
11303            l_new_price:= PO_ReqChangeRequestNotif_PVT.Get_PO_Price_Break_Grp(
11304                                l_po_org_id,
11305                                p_chg_request_grp_id,
11306                                p_po_line_id,
11307                                l_item_id,
11308                                l_unit_lookup_code,
11309                                nvl(l_price_override, l_unit_price),
11310                                l_pcr_line_loc_id);
11311 
11312          l_progress := '007';
11313          end if;
11314 
11315          -- get shipment amount increase
11316 
11317          select ( (l_new_price * l_new_quantity) -
11318                    nvl(pll.price_override,pol.unit_price) * pll.quantity )
11319          into l_shipmt_amt_increase
11320          from po_lines_all pol,
11321               po_line_locations_all pll
11322          where pll.line_location_id = l_pcr_line_loc_id
11323          and pol.po_line_id = pll.po_line_id ;
11324 
11325         end if;
11326 
11327       end if;
11328 
11329       l_line_amt_increase :=  l_line_amt_increase +  l_shipmt_amt_increase;
11330 
11331       l_progress := '008';
11332 
11333       end loop;
11334     close l_shipment_info_csr;
11335 
11336     -- possibly one line only has line level change, i.e. price change , start/end date change.
11337     -- This pcr row is not captured by shipment_info_csr.Calculate the amount increase here.
11338     l_progress := '009';
11339 
11340     if ( l_has_shipment_chg = 'N' ) then
11341 
11342        select decode(pol.matching_basis, 'AMOUNT',pol.amount,
11343                       (nvl(pcr.new_price,pol.unit_price) * pol.quantity ))
11344        into l_new_amount
11345        from po_change_requests pcr,
11346             po_lines_all pol
11347        where pcr.change_request_group_id =  p_chg_request_grp_id
11348        and pol.po_line_id =  p_po_line_id
11349        and pcr.document_line_id =  p_po_line_id
11350        and pcr.request_level = 'LINE'
11351        and pcr.request_status(+) NOT IN ('ACCEPTED', 'REJECTED')
11352        and pcr.initiator(+) = 'REQUESTER' ;
11353 
11354        l_progress := '010';
11355 
11356     else
11357 
11358       l_new_amount := l_old_amount +  l_line_amt_increase;
11359 
11360     end if;
11361 
11362     x_old_amount := l_old_amount;
11363     x_new_amount := l_new_amount;
11364 
11365     l_progress := '011';
11366 
11367 EXCEPTION
11368   WHEN OTHERS THEN
11369 
11370     wf_core.context('PO_ReqChangeRequestWF_PVT',
11371                         'get_po_line_amount',l_progress||'-'||sqlerrm);
11372 
11373     raise;
11374 
11375 END  get_po_line_amount;
11376 
11377 
11378 
11379 
11380 /*************************************************************************
11381  * Public Procedure: Submit_Internal_Req_Change
11382  *
11383  * Effects: This procedure is called by the requester change UI. When user
11384  *          want to submit a change request, the validation API will be
11385  *          called. If the change request is valid, the validation API
11386  *          will call this procedure to submit the request and start
11387  *          workflow to process the request.
11388  *
11389  *          it will call PO_REQAPPROVAL_INIT1.Start_WF_Process to start
11390  *          the workflow
11391  *
11392  * Returns:
11393  ************************************************************************/
11394   PROCEDURE submit_internal_req_change(p_api_version IN NUMBER,
11395                                        p_commit IN VARCHAR2,
11396                                        p_req_header_id IN NUMBER,
11397                                        p_note_to_approver IN VARCHAR2,
11398                                        p_initiator IN VARCHAR2,
11399                                        x_return_status OUT NOCOPY VARCHAR2 )
11400   IS
11401 
11402   p_document_type VARCHAR2(20) := 'REQUISITION';
11403   p_document_subtype VARCHAR2(20) := '';
11404   p_interface_source_code VARCHAR2(20) := 'POR';
11405   p_item_key  wf_items.item_key%TYPE;
11406   p_item_type wf_items.item_type%TYPE := 'POREQCHA';
11407   p_submitter_action VARCHAR2(20) := 'APPROVE';
11408   p_workflow_process VARCHAR2(30) := 'MAIN_CHANGE_APPROVAL';
11409   l_change_request_group_id NUMBER;
11410   l_preparer_id NUMBER;
11411   p_source_type_code VARCHAR2(20) := 'INVENTORY';
11412   l_req_num po_requisition_headers_all.segment1%TYPE;
11413   CURSOR change_request_group_id IS
11414   SELECT MAX(change_request_group_id)
11415       FROM po_change_requests
11416       WHERE document_header_id = p_req_header_id
11417           AND initiator = 'REQUESTER'
11418           AND request_status = 'NEW';
11419 
11420   l_api_name CONSTANT VARCHAR2(30) := 'Submit_Internal_Req_Change';
11421   l_api_version CONSTANT NUMBER := 1.0;
11422   l_log_head              CONSTANT VARCHAR2(100) := G_MODULE_NAME|| '.' || l_api_name;
11423   l_progress VARCHAR2(3) := '000' ;
11424   l_line_id number;
11425   l_msg_count number;
11426   l_msg_data varchar2(3000);
11427   l_return_status varchar2(1);
11428   l_orgid number;
11429   CURSOR req_line_id_chn_csr(grp_id NUMBER) IS
11430 	  SELECT DISTINCT document_line_id
11431 	  FROM po_change_requests
11432 	  WHERE change_request_group_id = grp_id;
11433 
11434 
11435   BEGIN
11436     IF NOT fnd_api.compatible_api_call(l_api_version, p_api_version,
11437                                        l_api_name, g_pkg_name) THEN
11438 
11439       RAISE fnd_api.g_exc_unexpected_error;
11440     END IF;
11441     x_return_status := fnd_api.g_ret_sts_success;
11442     -- End standard API initialization
11443     l_progress := '001';
11444 
11445     IF g_debug_stmt THEN
11446       po_debug.debug_var(l_log_head,l_progress,'p_req_header_id',p_req_header_id );
11447       po_debug.debug_var(l_log_head,l_progress,'p_initiator',p_initiator);
11448       po_debug.debug_stmt(l_log_head, l_progress,'Placing the Sales order on hold');
11449     END IF;
11450 
11451 
11452     l_progress := '002';
11453     OPEN change_request_group_id;
11454     FETCH change_request_group_id INTO l_change_request_group_id;
11455     CLOSE change_request_group_id;
11456 
11457     l_progress := '003';
11458     SELECT to_char(p_req_header_id) || '-'
11459                 || to_char(l_change_request_group_id) || '-'
11460                 || to_char(po_requester_change_wf_s.nextval)
11461     INTO p_item_key
11462     FROM sys.dual;
11463 
11464 
11465     SELECT preparer_id, segment1,TYPE_LOOKUP_CODE
11466     INTO l_preparer_id, l_req_num,p_document_subtype
11467     FROM po_requisition_headers_all
11468     WHERE requisition_header_id = p_req_header_id;
11469 
11470 
11471     -- Provide an hold on SO before calling reapproval of change
11472     -- call OM_API.Place_SO_ON_HOLD()
11473     -- need to appli req line level hold
11474     l_orgid := get_sales_order_org(p_req_hdr_id  => p_req_header_id);
11475 
11476     IF l_orgid is NOT NULL THEN
11477         PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
11478     END IF;
11479 
11480      OPEN req_line_id_chn_csr(l_change_request_group_id);
11481 	    LOOP
11482 	      FETCH req_line_id_chn_csr INTO l_line_id;
11483 	      EXIT WHEN req_line_id_chn_csr%notfound;
11484 
11485  OE_Internal_Requisition_Pvt.Apply_Hold_for_IReq  -- Specification definition
11486 (  P_API_Version           => 1.0
11487 ,  P_internal_req_line_id  => l_line_id
11488 ,  P_internal_req_header_id =>p_req_header_id
11489 ,  X_msg_count           =>l_msg_count
11490 ,  X_msg_data         =>   l_msg_data
11491 ,  X_return_status      =>    l_return_status
11492 );
11493        END LOOP;
11494 	      CLOSE req_line_id_chn_csr;
11495 
11496     l_orgid := get_requisition_org( p_req_hdr_id  => p_req_header_id);
11497 
11498     IF l_orgid is NOT NULL THEN
11499         PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
11500     END IF;
11501 
11502      IF g_debug_stmt THEN
11503       po_debug.debug_var(l_log_head,l_progress,'p_item_key',p_item_key );
11504       po_debug.debug_var(l_log_head,l_progress,'l_change_request_group_id',l_change_request_group_id);
11505       po_debug.debug_var(l_log_head,l_progress,'l_preparer_id',l_preparer_id );
11506       po_debug.debug_var(l_log_head,l_progress,'p_document_subtype',p_document_subtype );
11507       po_debug.debug_var(l_log_head,l_progress,'l_change_request_group_id',l_change_request_group_id);
11508       po_debug.debug_var(l_log_head,l_progress,'l_req_num',l_req_num );
11509       po_debug.debug_stmt(l_log_head, l_progress,'Starting POREQCHA workflow');
11510     END IF;
11511 
11512     po_reqapproval_init1.start_wf_process(
11513                                           itemtype => p_item_type,
11514                                           itemkey   => p_item_key,
11515                                           workflowprocess => p_workflow_process,
11516                                           actionoriginatedfrom => p_interface_source_code,
11517                                           documentid  => p_req_header_id,
11518                                           documentnumber =>  l_req_num,
11519                                           preparerid => l_preparer_id,
11520                                           documenttypecode => p_document_type,
11521                                           documentsubtype  => p_document_subtype,
11522                                           submitteraction => p_submitter_action,
11523                                           forwardtoid  =>  NULL,
11524                                           forwardfromid  => l_preparer_id,
11525                                           defaultapprovalpathid => NULL,
11526                                           note => p_note_to_approver,
11527                                           p_initiator => p_initiator,
11528                                           p_source_type_code => p_source_type_code);
11529 
11530 
11531     IF fnd_api.to_boolean(p_commit) THEN
11532       COMMIT WORK;
11533     END IF;
11534 
11535     l_progress := '004';
11536     IF g_debug_stmt THEN
11537      po_debug.debug_stmt(l_log_head, l_progress,'ending submit_internal_req_change');
11538     END IF;
11539   EXCEPTION
11540     WHEN fnd_api.g_exc_unexpected_error THEN
11541     x_return_status := fnd_api.g_ret_sts_unexp_error;
11542     WHEN OTHERS THEN
11543     x_return_status := fnd_api.g_ret_sts_unexp_error;
11544 
11545     RAISE;
11546   END submit_internal_req_change;
11547 
11548 /*************************************************************************
11549  *
11550  * Public Procedure: Convert_Into_sO_Change
11551  * Effects: workflow procedure, used in POREQCHA
11552  *
11553  *          convert the manager approved requester change request into
11554  *          sO change request.
11555  *
11556 ************************************************************************/
11557   PROCEDURE convert_into_so_change(itemtype        IN VARCHAR2,
11558                                    itemkey         IN VARCHAR2,
11559                                    actid           IN NUMBER,
11560                                    funcmode        IN VARCHAR2,
11561                                    resultout       OUT NOCOPY VARCHAR2    ) IS
11562 
11563   l_change_request_group_id NUMBER;
11564   l_return_status VARCHAR2(200);
11565   l_orgid number;
11566   l_msg_count number;
11567 
11568   CURSOR iso_change_csr(l_change_request_group_id IN NUMBER) IS
11569   SELECT     prl.requisition_header_id,
11570                  prl.requisition_line_id,
11571                  prl.line_num,
11572              pcr.old_quantity,
11573              pcr.new_quantity,
11574              pcr.old_need_by_date,
11575              pcr.new_need_by_date,
11576              pcr.action_type
11577   FROM po_change_requests pcr,
11578        po_requisition_lines_all prl
11579   WHERE pcr.change_request_group_id = l_change_request_group_id
11580     AND pcr.request_status = 'MGR_APP'
11581     AND pcr.document_line_id = prl.requisition_line_id
11582   ORDER BY  prl.line_num;
11583 
11584   --Bug 8205507 - Added code to release the hold for IReq when the approver rejects the
11585   -- change request.
11586 
11587   CURSOR iso_change_csr_rejected(l_change_request_group_id IN NUMBER) IS
11588   SELECT     prl.requisition_header_id,
11589                  prl.requisition_line_id
11590   FROM po_change_requests pcr,
11591        po_requisition_lines_all prl
11592   WHERE pcr.change_request_group_id = l_change_request_group_id
11593     AND pcr.request_status = 'REJECTED'
11594     AND pcr.document_line_id = prl.requisition_line_id
11595   ORDER BY  prl.line_num;
11596 
11597 
11598   l_document_header_id NUMBER;
11599   l_document_line_id NUMBER;
11600   l_document_num NUMBER;
11601   l_old_quantity NUMBER;
11602   l_new_quantity NUMBER;
11603   l_old_need_by_date DATE;
11604   l_new_need_by_date DATE;
11605   l_action_type  VARCHAR2(20);
11606   l_doc_string VARCHAR2(200);
11607   l_preparer_user_name VARCHAR2(100);
11608   x_progress VARCHAR2(100) := '001';
11609   l_CHANGE_REQUEST_ID number;
11610   l_return_msg  VARCHAR2(200);
11611   BEGIN
11612     l_change_request_group_id := po_wf_util_pkg.getitemattrnumber (itemtype => itemtype,
11613                                                                    itemkey  => itemkey,
11614                                                                    aname    => 'CHANGE_REQUEST_GROUP_ID');
11615     x_progress := '002';
11616     --query the pcr table to get these values to pass to the public api
11617     --mode will be the item type to signify that public api is getting called from
11618     --workflow
11619    /*  at this point the po_chnage_request record is MGR_APP if changes are approved by hierarchy
11620       or in REJECTED state if changes are rejected
11621     (req hdr id, req line id, old qty, new qty, old date, new date,
11622      action, mode)
11623    */
11624     OPEN iso_change_csr(l_change_request_group_id);
11625     LOOP
11626       FETCH iso_change_csr INTO
11627       l_document_header_id,
11628       l_document_line_id,
11629       l_document_num,
11630       l_old_quantity,
11631       l_new_quantity,
11632       l_old_need_by_date,
11633       l_new_need_by_date,
11634       l_action_type;
11635       EXIT WHEN iso_change_csr%notfound;
11636       x_progress := '003';
11637       ConvertIntoSOChange(p_chn_request_group_id => l_change_request_group_id,
11638                            p_document_header_id => l_document_header_id,
11639                            p_document_line_id => l_document_line_id,
11640                            p_document_num => l_document_num,
11641                            p_old_quantity => l_old_quantity,
11642                            p_new_quantity => l_new_quantity,
11643                            p_old_need_by_date => l_old_need_by_date,
11644                            p_new_need_by_date => l_new_need_by_date,
11645                            p_action_type => l_action_type,
11646                            p_mode => itemtype,
11647                            x_return_status => l_return_status,
11648                            x_return_msg    => l_return_msg );
11649       x_progress := '004';
11650     END LOOP;
11651     CLOSE iso_change_csr;
11652     x_progress := '005';
11653 
11654 
11655 
11656     OPEN iso_change_csr_rejected(l_change_request_group_id);
11657     LOOP
11658       FETCH iso_change_csr_rejected INTO
11659         l_document_header_id,
11660         l_document_line_id;
11661       EXIT WHEN iso_change_csr_rejected%notfound;
11662 
11663     x_progress := '015';
11664 
11665     l_orgid := get_sales_order_org(p_req_hdr_id  => l_document_header_id);
11666 
11667 
11668     IF l_orgid is NOT NULL AND itemtype <> 'REQ_RESCHEDULE' THEN
11669       PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
11670 
11671     --release the OM LINE FROM HOLD call OM_API
11672      x_progress := '020';
11673       OE_Internal_Requisition_Pvt.Release_Hold_for_IReq
11674             (  P_API_Version           => 1.0
11675             ,  P_internal_req_line_id  => l_document_line_id
11676             ,  P_internal_req_header_id =>l_document_header_id
11677             ,  X_msg_count           =>l_msg_count
11678             ,  X_msg_data         =>   l_return_msg
11679             ,  X_return_status      =>    l_return_status
11680             );
11681 
11682     END IF;
11683    END LOOP;
11684    CLOSE iso_change_csr_rejected;
11685 
11686     --set the item attribute INTERNAL_CHANGE_REQUEST_ID with minimum
11687     --CHANGE_REQUEST_ID for the sendng of notification
11688 
11689       SELECT  min(CHANGE_REQUEST_ID)
11690       into L_CHANGE_REQUEST_ID
11691       FROM po_change_requests
11692       WHERE change_request_group_id = l_change_request_group_id
11693         AND request_status = 'ACCEPTED';
11694 
11695       po_wf_util_pkg.setitemattrnumber (itemtype   => itemtype,
11696                                         itemkey    => itemkey,
11697                                         aname      => 'INTERNAL_CHANGE_REQUEST_ID',
11698                                         avalue     => L_CHANGE_REQUEST_ID);
11699 
11700 
11701     resultout := wf_engine.eng_completed || ':' ||  'ACTIVITY_PERFORMED';
11702 
11703   EXCEPTION
11704     WHEN OTHERS THEN
11705     l_doc_string := po_reqapproval_init1.get_error_doc(itemtype, itemkey);
11706     l_preparer_user_name := po_reqapproval_init1.get_preparer_user_name(itemtype, itemkey);
11707     wf_core.context('PO_ReqChangeRequestWF_PVT', 'Convert_Into_sO_Change', x_progress);
11708     po_reqapproval_init1.send_error_notif(itemtype, itemkey, l_preparer_user_name, l_doc_string, SQLERRM, 'PO_ReqChangeRequestWF_PVT.Convert_Into_sO_Change'||l_return_status||l_return_msg);
11709     RAISE;
11710 
11711   END convert_into_so_change;
11712 
11713    PROCEDURE ConvertIntoSOChange(p_chn_request_group_id IN NUMBER,
11714                                  p_document_header_id IN NUMBER,
11715                                  p_document_line_id IN NUMBER,
11716                                  p_document_num IN NUMBER,
11717                                  p_old_quantity IN NUMBER,
11718                                  p_new_quantity IN NUMBER,
11719                                  p_old_need_by_date IN DATE,
11720                                  p_new_need_by_date IN DATE,
11721                                  p_action_type IN VARCHAR2,
11722                                  p_mode IN VARCHAR2,
11723                                  x_return_status OUT NOCOPY VARCHAR2,
11724                                  x_return_msg out NOCOPY varchar2)
11725                                   IS
11726 
11727   PRAGMA autonomous_transaction;
11728   l_api_name VARCHAR2(50) := 'ConvertIntoSOChange';
11729   l_log_head              CONSTANT VARCHAR2(100) := G_MODULE_NAME|| '.' || l_api_name;
11730 
11731   l_return_status VARCHAR2(200);
11732   l_progress VARCHAR2(4) := '000';
11733   l_bool_ret_sts BOOLEAN;
11734   l_delta_quantity number;
11735   l_document_header_id number;
11736   l_orgid number;
11737   l_msg_count number;
11738   l_msg_data varchar2(200);
11739   BEGIN
11740     l_return_status := 'Y';
11741     x_return_status :=fnd_api.g_ret_sts_success;
11742     l_progress := '000';
11743   /*
11744     Algorithm: this procedure gets called for every req int line
11745     place on SO .The changes are approved when this is called
11746       * processing for each line
11747      - establish a save point
11748      - call the OM api to do the change/cancel
11749      - release order on hold only for when not called from req rescedule
11750      - for failed lines update change request as rejected and return
11751         = rollback to save point
11752      - for successful lines update the change request as accepted
11753         = adjust encumbrance
11754         = update po req tables
11755         = update mtl_supply
11756 
11757      */
11758     SAVEPOINT convertintosochange_sp;
11759     l_progress := '001';
11760     IF g_debug_stmt THEN
11761       po_debug.debug_begin(l_log_head);
11762       po_debug.debug_var(l_log_head, l_progress, 'p_chn_request_group_id', p_chn_request_group_id);
11763       po_debug.debug_var(l_log_head, l_progress, 'p_document_header_id', p_document_header_id);
11764       po_debug.debug_var(l_log_head, l_progress, 'p_document_line_id', p_document_line_id);
11765       po_debug.debug_var(l_log_head, l_progress, 'p_document_num', p_document_num);
11766       po_debug.debug_var(l_log_head, l_progress, 'p_old_quantity', p_old_quantity);
11767       po_debug.debug_var(l_log_head, l_progress, 'p_new_quantity', p_new_quantity);
11768       po_debug.debug_var(l_log_head, l_progress, 'p_old_need_by_date', p_old_need_by_date);
11769       po_debug.debug_var(l_log_head, l_progress, 'p_new_need_by_date', p_new_need_by_date);
11770       po_debug.debug_var(l_log_head, l_progress, 'p_action_type', p_action_type);
11771       po_debug.debug_var(l_log_head, l_progress, 'p_mode', p_mode);
11772     END IF;
11773 
11774     --calling the OM API for hold if called from reqschedule program
11775 
11776     IF (p_mode = 'REQ_RESCHEDULE' ) THEN
11777    /* --  This proc gets called as
11778      PO_ReqChangeRequestWF_PVT.ConvertIntoSOChange(
11779 	                           p_chn_request_group_id => null,
11780 	                           p_document_header_id => null,
11781 	                           p_document_line_id => l_document_line_id,
11782 	                           p_document_num => null,
11783 	                           p_old_quantity => l_old_quantity,
11784 	                           p_new_quantity => l_new_quantity,
11785 	                           p_old_need_by_date => l_old_need_by_date,
11786 	                           p_new_need_by_date => l_new_need_by_date,
11787 	                           p_action_type => l_action_type,
11788 	                           p_mode => 'REQ_RESCHEDULE',
11789 	                           x_return_status => :l_return_status
11790 	                           x_return_msg => :l_return_msg);
11791    Algorithm :
11792     Step 1: Since called from Plannning, the changes are approved
11793             and hence directly propogate.
11794     Step 2: No need to put SO on hold and release as this involves no wait
11795 
11796     */
11797     l_progress := '002';
11798 
11799     select requisition_header_id
11800     into l_document_header_id
11801     from po_requisition_lines_all
11802     where requisition_line_id =  p_document_line_id;
11803 
11804     --call OM API
11805     END IF;
11806 
11807     l_progress := '003';
11808     l_document_header_id := nvl(l_document_header_id,p_document_header_id);
11809     l_orgid := get_sales_order_org(p_req_line_id   => p_document_line_id);
11810 
11811     l_progress := '004';
11812 
11813     IF l_orgid is NOT NULL THEN
11814       PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
11815 
11816   --  Algorithm : call om api according to action type
11817 
11818       IF (p_action_type = 'MODIFICATION' ) THEN
11819 
11820         IF (p_new_quantity IS NOT NULL) THEN
11821           l_delta_quantity := p_new_quantity - p_old_quantity;
11822         ELSE
11823           l_delta_quantity :=0;
11824         END IF;
11825 
11826         l_progress := '005';
11827 
11828         OE_Internal_Requisition_Pvt.Call_Process_Order_for_IReq  -- Specification definition
11829             (  P_API_Version            =>1.0
11830           ,  P_internal_req_line_id    =>p_document_line_id
11831           ,  P_internal_req_header_id  =>l_document_header_id
11832           ,  P_Mode                    => 'P'
11833           ,  P_New_Request_Date       =>p_new_need_by_date
11834           ,  P_Delta_Ordered_Qty      => l_delta_quantity
11835           ,  X_msg_count           =>l_msg_count
11836           ,  X_msg_data         =>   l_msg_data
11837           ,  X_return_status      =>    l_return_status
11838           ,  P_Cancel_ISO          =>FALSE
11839           ,  P_Cancel_ISO_Lines    => FALSE
11840           );
11841 
11842         IF g_debug_stmt THEN
11843               po_debug.debug_var(l_log_head, l_progress,'returning from OM API Call_Process_Order_for_IReq for modification',l_return_status);
11844         END IF;
11845 
11846       ELSIF (p_action_type = 'CANCELLATION') THEN
11847 
11848         l_progress := '006';
11849 
11850         OE_Internal_Requisition_Pvt.Call_Process_Order_for_IReq  -- Specification definition
11851             (  P_API_Version            =>1.0
11852             ,  P_internal_req_line_id    =>p_document_line_id
11853             ,  P_internal_req_header_id  =>l_document_header_id
11854             ,  P_Mode                    => 'P'
11855             ,  P_Cancel_ISO          => null
11856             ,  P_Cancel_ISO_Lines    => TRUE
11857             ,  X_msg_count           =>l_msg_count
11858             ,  X_msg_data         =>   l_msg_data
11859             ,  X_return_status      =>  l_return_status
11860             );
11861 
11862         IF g_debug_stmt THEN
11863               po_debug.debug_var(l_log_head, l_progress,'returning from OM API Call_Process_Order_for_IReq for cancellation',l_return_status);
11864         END IF;
11865 
11866       END IF;
11867 
11868     END IF;-- IF l_orgid is NOT NULL THEN
11869 
11870 
11871 
11872     l_orgid := get_requisition_org( p_req_hdr_id  => l_document_header_id);
11873 
11874     IF l_orgid is NOT NULL THEN
11875         PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
11876     END IF;
11877 
11878     IF g_debug_stmt THEN
11879         po_debug.debug_var(l_log_head, l_progress,'returning from OM API ',l_return_status);
11880     END IF;
11881 
11882 
11883     l_progress := '007';
11884 
11885     IF (l_return_status = fnd_api.g_ret_sts_success) THEN --return status from OM API
11886       -- for successful lines update the change request as accepted
11887       l_progress := '008';
11888       IF g_debug_stmt THEN
11889         po_debug.debug_stmt(l_log_head, l_progress,'for successful lines updating the change request as accepted');
11890       END IF;
11891 
11892       IF (p_action_type = 'MODIFICATION') THEN
11893 
11894         l_progress := '009';
11895 
11896         UPDATE po_change_requests
11897         SET request_status = 'ACCEPTED'
11898         WHERE change_request_group_id = p_chn_request_group_id
11899         AND document_header_id = l_document_header_id
11900         AND document_line_id = p_document_line_id ;
11901 
11902 
11903         --now update po req tables with changed data
11904         IF (p_new_need_by_date IS NOT NULL) THEN
11905            update_req_line_date_changes(     p_req_line_id=>p_document_line_id,
11906                                              p_need_by_date=> p_new_need_by_date,
11907                                              x_return_status =>l_return_status);
11908         END IF;
11909 
11910         l_progress := '010';
11911         --now update po req tables with changed data
11912         IF (p_new_quantity IS NOT NULL) THEN
11913             l_delta_quantity := p_new_quantity - p_old_quantity;
11914             update_reqline_quan_changes(p_req_line_id=>p_document_line_id,
11915                                              p_delta_quantity=> l_delta_quantity,
11916                                              x_return_status =>l_return_status);
11917         END IF;
11918         l_progress := '011';
11919 
11920       ELSE IF (p_action_type = 'CANCELLATION') THEN
11921        /*    - call the OM api to do the change/cancel    already done
11922      - release order on hold
11923      - for successful lines update the change request as accepted and failed lines update change request as rejected
11924      - update po req tables    */
11925 
11926         l_progress := '012';
11927 
11928         UPDATE po_change_requests
11929         SET request_status = 'ACCEPTED'
11930         WHERE change_request_group_id = p_chn_request_group_id
11931         AND document_header_id = l_document_header_id
11932         AND document_line_id = p_document_line_id ;
11933 
11934         IF g_debug_stmt THEN
11935          po_debug.debug_stmt(l_log_head, l_progress,'UPDATED po_change_requests');
11936         END IF;
11937 
11938         req_line_CANCEL(p_req_line_id => p_document_line_id,
11939                     x_return_status =>l_return_status);
11940           END IF;
11941       END IF;
11942     ELSE
11943     -- for unsuccessful lines update the change request as rejected
11944       l_progress := '013';
11945 
11946       IF g_debug_stmt THEN
11947             po_debug.debug_stmt(l_log_head, l_progress,'for unsuccessful lines rollback');
11948             po_debug.debug_stmt(l_log_head, l_progress,'update the change request as rejected');
11949       END IF;
11950 
11951       ROLLBACK TO convertintosochange_SP; -- revert the so changes
11952 
11953       UPDATE po_change_requests
11954       SET request_status = 'REJECTED',
11955           change_active_flag = 'N',
11956           request_reason=l_msg_data
11957       WHERE change_request_group_id = p_chn_request_group_id
11958       AND document_header_id = l_document_header_id
11959       AND document_line_id = p_document_line_id ;
11960 
11961       l_progress := '014';
11962       -- add return msg to pass back to req reschedule
11963       x_return_msg := l_msg_data;
11964       x_return_status := l_return_status;
11965     END IF;
11966 
11967 
11968 
11969     --release the OM LINE FROM HOLD call OM_API
11970     l_orgid := get_sales_order_org(p_req_hdr_id  => l_document_header_id);
11971 
11972     IF l_orgid is NOT NULL AND p_mode <> 'REQ_RESCHEDULE' THEN
11973       PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
11974 
11975     --release the OM LINE FROM HOLD call OM_API
11976 
11977       OE_Internal_Requisition_Pvt.Release_Hold_for_IReq
11978             (  P_API_Version           => 1.0
11979             ,  P_internal_req_line_id  => p_document_line_id
11980             ,  P_internal_req_header_id =>l_document_header_id
11981             ,  X_msg_count           =>l_msg_count
11982             ,  X_msg_data         =>   x_return_msg
11983             ,  X_return_status      =>    x_return_status
11984             );
11985 
11986     END IF;
11987 
11988     l_orgid := get_requisition_org( p_req_hdr_id  => l_document_header_id);
11989 
11990     IF l_orgid is NOT NULL THEN
11991         PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
11992     END IF;
11993 
11994     COMMIT;
11995     l_progress := '015';
11996 
11997   EXCEPTION
11998     WHEN OTHERS THEN
11999       l_orgid := get_requisition_org( p_req_hdr_id  => l_document_header_id);
12000       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12001 
12002       IF l_orgid is NOT NULL THEN
12003           PO_MOAC_UTILS_PVT.set_org_context(l_orgid) ;       -- <R12 MOAC>
12004       END IF;
12005 
12006       wf_core.context('PO_ReqChangeRequestWF_PVT', 'ConvertIntoSOChange '||l_return_status, l_progress);
12007       ROLLBACK TO convertintosochange_SP;
12008       RAISE;
12009   END ConvertIntoSOChange;
12010 
12011 
12012   /* This procedure will be called from
12013      1. Req initiated IR ISO change from poreqcha WF
12014      2. Req Rescedule initiated change from CP
12015      3. Fulfillment intiated change.
12016 
12017      The procedure updates the requisition line with changes
12018      of quntity.
12019      It retrives the existing quantity and adds the delta quntity
12020      to compute the new quantity
12021   */
12022   PROCEDURE update_reqline_quan_changes(p_req_line_id IN NUMBER,
12023                                              p_delta_quantity IN NUMBER,
12024                                              p_uom IN VARCHAR2 default null,
12025                                              x_return_status      OUT NOCOPY  VARCHAR2)
12026   IS
12027   l_mtl_quantity number;
12028   l_bool_ret_sts boolean;
12029   l_preparer_id number;
12030   l_return_status  VARCHAR2(1);
12031   po_return_code VARCHAR2(10);
12032   x_detailed_results po_fcout_type;
12033   l_req_line_id po_tbl_number;
12034   l_distribution_id_tbl po_tbl_number;
12035   l_old_quantity number;
12036   l_new_quantity number;
12037   l_new_price number;
12038   l_new_amount number;
12039   l_api_name     CONSTANT VARCHAR(30) := 'update_reqline_quan_changes';
12040   l_log_head     CONSTANT VARCHAR2(100) := G_MODULE_NAME|| '.' || l_api_name;
12041   l_progress     VARCHAR2(3) := '000';
12042   l_price NUMBER;
12043   l_rec_tax NUMBER;
12044   l_nonrec_tax NUMBER;
12045   l_cal_disttax_status VARCHAR2(1);
12046   l_dist_rec_tax NUMBER;
12047   l_dist_nonrec_tax NUMBER;
12048   l_new_tax NUMBER;
12049   l_fc_result_status VARCHAR2(1);
12050   l_po_return_code VARCHAR2(100) := '';
12051   l_fc_out_tbl po_fcout_type;
12052 
12053 
12054   l_req_dist_id number;
12055   CURSOR l_changed_req_dists_csr(req_line_id NUMBER) IS
12056   select DISTRIBUTION_ID
12057   from PO_REQ_DISTRIBUTIONS_ALL
12058   where REQUISITION_LINE_ID= req_line_id;
12059   -- this is inventory line and hence shall select one dist
12060 
12061   CURSOR l_dist_tax_csr(req_line_id NUMBER) IS
12062   SELECT -- any quantity change
12063     prda.distribution_id,
12064     prla.unit_price,
12065     prla.quantity
12066   FROM
12067     po_req_distributions_all prda,
12068     po_requisition_lines_all prla
12069   WHERE
12070    prla.requisition_line_id = req_line_id AND
12071    prla.requisition_line_id = prda.requisition_line_id;
12072 
12073 
12074 
12075   BEGIN
12076    /*
12077     Algorithm : Step 1: ADJUST the encumberance only if req encumbrance is ON
12078                 Step 2: Update the req line and dist with the quantity changes
12079                 Step 3: Update the mtl_supply by the PO API
12080     */
12081 
12082   -- Step 1: ADJUST the encumberance
12083   l_progress := '001';
12084 
12085    IF g_debug_stmt THEN
12086       po_debug.debug_var(l_log_head,l_progress,'p_req_line_id', p_req_line_id );
12087       po_debug.debug_var(l_log_head,l_progress,'p_delta_quantity', p_delta_quantity );
12088       po_debug.debug_var(l_log_head,l_progress,'p_uom', p_uom );
12089 
12090     END IF;
12091   IF( p_req_line_id is not null) THEN
12092 
12093       l_progress := '002';
12094     --check whether req encumbrance is on
12095    IF( PO_CORE_S.is_encumbrance_on(
12096             p_doc_type => PO_DOCUMENT_CHECKS_PVT.g_document_type_REQUISITION
12097          ,  p_org_id => NULL
12098          )) THEN
12099 
12100 
12101       select prh.preparer_id  into l_preparer_id
12102       from po_requisition_headers_all prh,
12103            po_requisition_lines_all prl
12104       where prl.requisition_line_id = p_req_line_id
12105       and   prl.requisition_header_id =  prh.requisition_header_id;
12106 
12107      IF g_debug_stmt THEN
12108       po_debug.debug_var(l_log_head,l_progress,'l_preparer_id',l_preparer_id );
12109       po_debug.debug_stmt(l_log_head, l_progress,'Populating encumbrance gt');
12110      END IF;
12111       l_distribution_id_tbl   := po_tbl_number();
12112       l_progress := '003';
12113 
12114       OPEN l_changed_req_dists_csr(p_req_line_id);
12115 
12116       FETCH l_changed_req_dists_csr BULK COLLECT
12117       INTO l_distribution_id_tbl;
12118 
12119       CLOSE l_changed_req_dists_csr;
12120       l_progress := '004';
12121 
12122       po_document_funds_grp.populate_encumbrance_gt(
12123                                                       p_api_version => 1.0,
12124                                                       x_return_status => l_return_status,
12125                                                       p_doc_type => po_document_funds_grp.g_doc_type_requisition,
12126                                                       p_doc_level => po_document_funds_grp.g_doc_level_distribution,
12127                                                       p_doc_level_id_tbl => l_distribution_id_tbl,
12128                                                       p_make_old_copies_flag => po_document_funds_grp.g_parameter_yes,
12129                                                       p_make_new_copies_flag => po_document_funds_grp.g_parameter_yes,
12130                                                       p_check_only_flag => po_document_funds_grp.g_parameter_NO);
12131 
12132       l_progress := '005';
12133 
12134        -- error handling after calling populate_encumbrance_gt
12135       IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
12136         IF g_debug_stmt THEN
12137           po_debug.debug_stmt(l_log_head, l_progress,'error exists with funds check');
12138         END IF;
12139         RAISE fnd_api.g_exc_unexpected_error;
12140       END IF;
12141 
12142        -- re-initialize distributions list table
12143         l_distribution_id_tbl.delete;
12144 
12145         -- Update NEW record in PO_ENCUMBRANCE_GT with the new
12146         -- values
12147         l_progress := '006';
12148 
12149     IF g_debug_stmt THEN
12150      po_debug.debug_stmt(l_log_head, l_progress,'after populating encumbrance gt');
12151     END IF;
12152 
12153       OPEN l_dist_tax_csr(p_req_line_id);
12154 
12155         LOOP
12156           FETCH l_dist_tax_csr INTO
12157           l_req_dist_id,
12158           l_new_price,
12159           l_old_quantity;
12160           EXIT WHEN l_dist_tax_csr%notfound;
12161 
12162           l_progress := '007';
12163 
12164           l_new_quantity := l_old_quantity + p_delta_quantity;
12165 
12166           IF g_debug_stmt THEN
12167             po_debug.debug_var(l_log_head, l_progress,'l_req_dist_id', l_req_dist_id );
12168             po_debug.debug_var(l_log_head, l_progress,'l_new_price', l_new_price );
12169             po_debug.debug_var(l_log_head, l_progress, 'l_quantity', l_new_quantity);
12170           END IF;
12171 
12172           po_rco_validation_pvt.calculate_disttax(1.0, l_cal_disttax_status, l_req_dist_id, l_new_price, l_new_quantity, NULL,
12173                             l_rec_tax, l_nonrec_tax);
12174 
12175           l_progress := '008';
12176           l_new_tax := l_nonrec_tax;
12177           l_new_amount := l_new_price*l_new_quantity;
12178 
12179           IF g_debug_stmt THEN
12180             po_debug.debug_var(l_log_head, l_progress, 'l_rec_tax', l_rec_tax);
12181             po_debug.debug_var(l_log_head, l_progress, 'l_nonrec_tax', l_nonrec_tax);
12182           END IF;
12183 
12184           -- update new values in PO_ENCUMBRANCE_GT
12185           UPDATE po_encumbrance_gt
12186           SET
12187             amount_ordered = l_new_amount,
12188             quantity_ordered = l_new_quantity,
12189             price = l_new_price,
12190             nonrecoverable_tax = l_new_tax
12191           WHERE
12192             distribution_id = l_req_dist_id AND
12193             adjustment_status = po_document_funds_grp.g_adjustment_status_new;
12194 
12195           l_progress := '009';
12196         IF g_debug_stmt THEN
12197           po_debug.debug_stmt(l_log_head, l_progress,'Updating po_encumbrance_gt NEW record');
12198         END IF;
12199 
12200 
12201 
12202         END LOOP;
12203         CLOSE l_dist_tax_csr;
12204 
12205 
12206         l_progress := '010';
12207         --Execute PO Funds Check API
12208 
12209         po_document_funds_grp.do_adjust(
12210           p_api_version => 1.0,
12211           x_return_status => l_fc_result_status,
12212           p_doc_type => po_document_funds_grp.g_doc_type_REQUISITION,
12213           p_doc_subtype => NULL,
12214           p_employee_id  => l_preparer_id,
12215           p_override_funds => po_document_funds_grp.g_parameter_USE_PROFILE,
12216           p_use_gl_date => po_document_funds_grp.g_parameter_YES,
12217           p_override_date => sysdate,
12218           p_report_successes => po_document_funds_grp.g_parameter_NO,
12219           x_po_return_code => l_po_return_code,
12220           x_detailed_results => l_fc_out_tbl);
12221 
12222         l_progress := '011';
12223 
12224         IF g_debug_stmt THEN
12225           po_debug.debug_stmt(l_log_head, l_progress, 'FUNDS ADJUST:' || l_fc_result_status ||' PO RETURN CODE:' || l_po_return_code);
12226         END IF;
12227 
12228         IF (l_fc_result_status = fnd_api.g_ret_sts_unexp_error) THEN
12229             IF g_debug_stmt THEN
12230               po_debug.debug_stmt(l_log_head, l_progress,'error exists with funds check');
12231             END IF;
12232             RAISE fnd_api.g_exc_unexpected_error;
12233             RETURN;
12234 
12235         ELSE
12236 
12237           IF g_debug_stmt THEN
12238             po_debug.debug_STmt(l_log_head, l_progress, 'after DO adjust of funds');
12239           END IF;
12240 
12241           IF (l_po_return_code = po_document_funds_grp.g_return_success) THEN
12242             x_return_status := fnd_api.g_ret_sts_success;
12243           ELSE
12244             x_return_status := fnd_api.g_ret_sts_error;
12245             RAISE fnd_api.g_exc_unexpected_error;
12246             RETURN;
12247           END IF;
12248 
12249       END IF;
12250     END IF; --check req encumbrance
12251       --Step 2: Update the req line and dist with the quantity changes
12252 
12253     IF (p_delta_quantity IS NOT NULL) THEN
12254 
12255       l_progress := '012';
12256       IF g_debug_stmt THEN
12257         po_debug.debug_STmt(l_log_head, l_progress, 'Updating the req line and dist with the quantity changes');
12258       END IF;
12259 
12260         UPDATE po_requisition_lines_all
12261         SET quantity = quantity + p_delta_quantity
12262         WHERE requisition_line_id = p_req_line_id ;
12263 
12264         l_progress := '013';
12265 
12266         -- only one distribution to one internal req line
12267         UPDATE po_req_distributions_all
12268         SET req_line_quantity = req_line_quantity + p_delta_quantity
12269         WHERE requisition_line_id = p_req_line_id ;
12270 
12271         l_progress := '014';
12272 
12273       -- Step 3: Update the mtl_supply by the PO API
12274         select quantity into l_mtl_quantity
12275         from mtl_supply
12276         where supply_type_code = 'REQ'
12277         and req_line_id = p_req_line_id;
12278 
12279         l_mtl_quantity := l_mtl_quantity +p_delta_quantity;
12280 
12281         l_bool_ret_sts := po_supply.po_req_supply(
12282                                                     p_docid => NULL
12283                                                   , p_lineid => p_req_line_id
12284                                                   , p_shipid => NULL
12285                                                   , p_action => 'Update_Req_Line_Qty'
12286                                                   , p_recreate_flag => FALSE
12287                                                   , p_qty => l_mtl_quantity
12288                                                   , p_receipt_date => NULL
12289                                                   );
12290       -- the above api takes care of primary uom conversion
12291       l_progress := '015';
12292 
12293         IF NOT l_bool_ret_sts THEN
12294           RAISE fnd_api.g_exc_unexpected_error;
12295         END IF;
12296       IF g_debug_stmt THEN
12297         po_debug.debug_STmt(l_log_head, l_progress, 'Updated the req line and dist and mtl_supply with the quantity changes');
12298         po_debug.debug_STmt(l_log_head, l_progress, 'Returning from update_reqline_quan_changes');
12299       END IF;
12300    END IF;
12301   END IF; --  IF( p_req_line_id is not null)
12302 
12303   END update_reqline_quan_changes;
12304 
12305 
12306     /* This procedure will be called from
12307      1. Req initiated IR ISO change from poreqcha WF
12308      2. Req Rescedule initiated change from CP
12309      3. Fulfillment intiated change.
12310 
12311      The procedure updates the requisition line with changes
12312      of need by date
12313      It retrives the existing quantity and adds the delta quntity
12314      to compute the new quantity
12315   */
12316   PROCEDURE update_req_line_date_changes(p_req_line_id IN NUMBER,
12317                                              p_need_by_date IN DATE,
12318                                              x_return_status      OUT NOCOPY  VARCHAR2)
12319   IS
12320   x_progress varchar2(3);
12321   l_bool_ret_sts boolean;
12322   l_api_name VARCHAR2(50) := 'update_req_line_date_changes';
12323   l_log_head              CONSTANT VARCHAR2(100) := G_MODULE_NAME|| '.' || l_api_name;
12324 
12325 
12326   BEGIN
12327     /*
12328     Algorithm : Step 1: Update the req line and dist with the need by date changes
12329                 Step 2: Update the mtl_supply by the PO API
12330 
12331     */
12332 
12333        IF (p_need_by_date IS NOT NULL) THEN
12334           x_progress := '001';
12335           IF g_debug_stmt THEN
12336                 po_debug.debug_var(l_log_head,x_progress,'p_need_by_date', p_need_by_date );
12337                 po_debug.debug_var(l_log_head,x_progress,'p_req_line_id', p_req_line_id );
12338           END IF;
12339 
12340             UPDATE po_requisition_lines_all
12341             SET need_by_date = p_need_by_date
12342             WHERE requisition_line_id = p_req_line_id ;
12343 
12344 
12345             l_bool_ret_sts := po_supply.po_req_supply(
12346                                                       p_docid => NULL
12347                                                       , p_lineid => p_req_line_id
12348                                                       , p_shipid => NULL
12349                                                       , p_action => 'Update_Req_Line_Date'
12350                                                       , p_recreate_flag => FALSE
12351                                                       , p_qty => NULL
12352                                                       , p_receipt_date => p_need_by_date
12353                                                       );
12354 
12355             IF NOT l_bool_ret_sts THEN
12356               RAISE fnd_api.g_exc_unexpected_error;
12357             END IF;
12358 
12359 
12360           x_progress := '003';
12361 
12362         END IF;
12363 
12364     IF g_debug_stmt THEN
12365      po_debug.debug_stmt(l_log_head, x_progress,'Need by updation successful');
12366     END IF;
12367 
12368    END update_req_line_date_changes;
12369 
12370   PROCEDURE req_line_CANCEL(p_req_line_id IN NUMBER,
12371                             x_return_status      OUT NOCOPY  VARCHAR2)
12372   IS
12373   x_progress varchar2(3);
12374   l_bool_ret_sts boolean;
12375   l_detailed_results PO_FCOUT_TYPE;
12376   l_po_return_code VARCHAR2(50);
12377   l_return_status VARCHAR2(10);
12378   l_api_name VARCHAR2(50) := 'req_line_CANCEL';
12379   l_log_head              CONSTANT VARCHAR2(100) := G_MODULE_NAME|| '.' || l_api_name;
12380   l_document_id number;
12381   BEGIN
12382     x_progress := '001';
12383     IF g_debug_stmt THEN
12384       po_debug.debug_var(l_log_head,x_progress,'p_req_line_id', p_req_line_id );
12385       po_debug.debug_stmt(l_log_head, x_progress,'Updating po_requisition_lines_all');
12386     END IF;
12387 
12388      --update po req lines as cancel flag y
12389             UPDATE po_requisition_lines_all
12390             SET cancel_flag = 'Y',
12391   --          quantity_cancelled = p_old_quantity,
12392             cancel_date = SYSDATE
12393             WHERE requisition_line_id = p_req_line_id ;
12394 
12395            x_progress := '012';
12396 
12397             --bug 7664476 -- roll up the authorization status if all lines of requisiton is cancelled
12398             Select REQUISITION_HEADER_ID into l_document_id
12399             from po_requisition_lines_all
12400             where requisition_line_id = p_req_line_id ;
12401 
12402            x_progress := '013';
12403 
12404             UPDATE po_requisition_headers_all h
12405             SET    h.AUTHORIZATION_STATUS  = 'CANCELLED'
12406             WHERE  h.REQUISITION_HEADER_ID = l_document_id
12407                 AND NOT EXISTS
12408                     (SELECT 'UNCANCELLED LINE EXISTS'
12409                     FROM    po_requisition_lines_all prl
12410                     WHERE   prl.requisition_header_id = l_document_id
12411                         AND NVL(prl.cancel_flag,'N')  = 'N'
12412                     );
12413 
12414      -- delete the record in mtl_supply
12415 
12416             x_progress := '002';
12417 
12418              l_bool_ret_sts := po_supply.po_req_supply(
12419                                                     p_docid => NULL
12420                                                     , p_lineid => p_req_line_id
12421                                                     , p_shipid => NULL
12422                                                     , p_action => 'Remove_Req_Line_Supply'
12423                                                     , p_recreate_flag => FALSE
12424                                                     , p_qty => NULL
12425                                                     , p_receipt_date => NULL
12426                                                     );
12427 
12428             IF NOT l_bool_ret_sts THEN
12429               RAISE fnd_api.g_exc_unexpected_error;
12430             END IF;
12431       IF g_debug_stmt THEN
12432         po_debug.debug_stmt(l_log_head, x_progress,'Mtl_supply deleted and cancelling funds');
12433       END IF;
12434 
12435     -- cancel the encumbered funds
12436 
12437   PO_DOCUMENT_FUNDS_GRP.do_cancel(
12438       p_api_version => 1.0
12439    ,  p_commit    => fnd_api.g_true
12440    ,  p_init_msg_list   => fnd_api.g_false
12441    ,  p_validation_level=> FND_API.G_VALID_LEVEL_FULL
12442    ,  x_return_status =>l_return_status
12443    ,  p_doc_type        =>'REQ'
12444    ,  p_doc_subtype   =>null
12445    ,  p_doc_level     =>'LINE'
12446    ,  p_doc_level_id  => p_req_line_id
12447    ,  p_override_funds =>PO_DOCUMENT_FUNDS_GRP.g_parameter_USE_PROFILE
12448    ,  p_use_gl_date => PO_DOCUMENT_FUNDS_GRP.g_parameter_YES
12449    ,  p_override_date  => SYSDATE
12450    ,  p_report_successes => PO_DOCUMENT_FUNDS_GRP.g_parameter_NO
12451    ,  x_po_return_code => l_po_return_code
12452    ,  x_detailed_results=> L_detailed_results
12453    );
12454 
12455 x_return_status := l_return_status;
12456  IF g_debug_stmt THEN
12457       po_debug.debug_var(l_log_head,x_progress,'l_return_status', l_return_status );
12458       po_debug.debug_stmt(l_log_head, x_progress,'Mtl_supply deleted and cancelling funds');
12459  END IF;
12460 
12461   END req_line_CANCEL;
12462 
12463 
12464 /*************************************************************************
12465  *
12466  * Public Procedure: SO_CANCELLATION_EXISTS
12467  * Effects: workflow procedure, called in workflow POREQCHA
12468  *
12469  *
12470  ************************************************************************/
12471   PROCEDURE SEND_INTERNAL_NOTIF(itemtype        IN VARCHAR2,
12472                                       itemkey         IN VARCHAR2,
12473                                       actid           IN NUMBER,
12474                                       funcmode        IN VARCHAR2,
12475                                       resultout       OUT NOCOPY VARCHAR2    )IS
12476   l_change_request_group_id NUMBER;
12477   l_orgid                 NUMBER;
12478   x_progress              VARCHAR2(100);
12479   l_action_type           VARCHAR2(100);
12480   L_CHANGE_REQUEST_ID     NUMBER;
12481   l_planner_id              NUMBER;
12482   x_planner_name            VARCHAR2(100);
12483   x_planner_display_name    VARCHAR2(100);
12484   l_doc_string VARCHAR2(200);
12485   l_preparer_user_name VARCHAR2(100);
12486   l_PLANNERS_NOTIFIED varchar2(5000);
12487   l_sql  varchar2(5000);
12488   l_cur     SYS_REFCURSOR;
12489 
12490 
12491   BEGIN
12492 --INTERNAL_CHANGE_REQUEST_ID
12493      x_progress := 'PO_ReqChangeRequestWF_PVT.SEND_INTERNAL_NOTIF: 01';
12494 
12495     -- Do nothing in cancel or timeout mode
12496     IF (funcmode <> wf_engine.eng_run) THEN
12497       resultout := wf_engine.eng_null;
12498       RETURN;
12499     END IF;
12500 
12501 
12502   l_change_request_group_id := po_wf_util_pkg.getitemattrnumber (
12503                                                                    itemtype => itemtype,
12504                                                                    itemkey  => itemkey,
12505                                                                    aname    => 'CHANGE_REQUEST_GROUP_ID');
12506 
12507 
12508   l_change_request_id := po_wf_util_pkg.getitemattrnumber (
12509                                                                    itemtype => itemtype,
12510                                                                    itemkey  => itemkey,
12511                                                                    aname    => 'INTERNAL_CHANGE_REQUEST_ID');
12512 
12513    /* Algorithm :  get the INTERNAL_CHANGE_REQUEST_ID and populate attributes for this notification
12514                    increment the INTERNAL_CHANGE_REQUEST_ID
12515 
12516                    varchar query should be
12517 
12518   SELECT mp.EMPLOYEE_ID
12519   into l_planner_id
12520   FROM po_change_requests pcr,
12521        po_requisition_lines_all prl,
12522        mtl_system_items_b mi,
12523        financials_system_params_all fsp,
12524        mtl_planners mp
12525   WHERE pcr.change_request_group_id =l_change_request_group_id
12526       AND pcr.change_request_id =L_CHANGE_REQUEST_ID
12527       AND pcr.request_status = 'ACCEPTED'
12528       AND pcr.DOCUMENT_LINE_ID = prl.requisition_line_id
12529       and prl.org_id = fsp.org_id
12530       AND prl.ITEM_ID = mi.INVENTORY_ITEM_ID
12531       AND mi.organization_id = fsp.inventory_organization_id
12532       and mi.PLANNER_CODE  = mp.planner_code
12533       AND mi.organization_id = mp.organization_id
12534       and prl.source_type_code = 'INVENTORY'
12535       and  mp.EMPLOYEE_ID NOT IN (planners already notified);
12536 
12537    */
12538 
12539    l_planner_id := 0;
12540    l_PLANNERS_NOTIFIED := po_wf_util_pkg.getitemattrtext (      itemtype => itemtype,
12541                                                                    itemkey  => itemkey,
12542                                                                    aname    => 'PLANNERS_NOTIFIED');
12543 
12544    l_sql := '
12545   SELECT mp.EMPLOYEE_ID
12546   FROM po_change_requests pcr,
12547        po_requisition_lines_all prl,
12548        mtl_system_items_b mi,
12549        financials_system_params_all fsp,
12550        mtl_planners mp
12551   WHERE pcr.change_request_group_id =:1
12552       AND pcr.change_request_id =:2
12553       AND pcr.request_status = ''ACCEPTED''
12554       AND pcr.DOCUMENT_LINE_ID = prl.requisition_line_id
12555       and prl.org_id = fsp.org_id
12556       AND prl.ITEM_ID = mi.INVENTORY_ITEM_ID
12557       AND mi.organization_id = fsp.inventory_organization_id
12558       and mi.PLANNER_CODE  = mp.planner_code
12559       AND mi.organization_id = mp.organization_id
12560       and prl.source_type_code = ''INVENTORY''
12561       ';
12562   IF l_PLANNERS_NOTIFIED IS NOT NULL THEN
12563       l_sql := l_sql || ' AND   mp.EMPLOYEE_ID NOT IN ( ' ||  l_PLANNERS_NOTIFIED || ' ) ' ;
12564   END IF ;
12565 --   l_sql := l_sql || ';';
12566 
12567    BEGIN
12568 
12569  /*  OPEN l_cur FOR l_sql;
12570      FETCH l_cur INTO l_planner_id;
12571    CLOSE l_cur;
12572    */
12573    EXECUTE IMMEDIATE l_sql INTO l_planner_id USING l_change_request_group_id, L_CHANGE_REQUEST_ID;
12574 
12575    EXCEPTION
12576        WHEN OTHERS THEN
12577            l_planner_id :=null;
12578 
12579     END;
12580 
12581    IF( l_planner_id is not null) THEN
12582       WF_DIRECTORY.GetUserName(  'PER',
12583   	        	                   l_planner_id,
12584          		         	           x_planner_name,
12585                   	        	   x_planner_display_name);
12586 
12587       wf_engine.setitemattrtext(itemtype, itemkey, 'PLANNER_USER_NAME',x_planner_name);
12588 
12589       IF  (l_PLANNERS_NOTIFIED is not null) THEN -- already planners
12590          l_PLANNERS_NOTIFIED := l_PLANNERS_NOTIFIED || ',' || l_planner_id;
12591       else
12592          l_PLANNERS_NOTIFIED := l_planner_id; -- first planner
12593       END IF;
12594 
12595       wf_engine.setitemattrtext(itemtype, itemkey, 'PLANNERS_NOTIFIED',l_PLANNERS_NOTIFIED);
12596 
12597    END IF;
12598 
12599    IF( l_planner_id is null) THEN
12600     resultout := wf_engine.eng_completed || ':' ||  'NO_PLANNER';
12601    ELSE
12602     resultout := wf_engine.eng_completed || ':' ||  'NOTIFY_PLANNER';
12603  /*  ELSIF l_action_type = 'MODIFICATION' AND l_planner_id is not null THEN
12604       resultout := wf_engine.eng_completed || ':' ||  'NOTIFY_PLANNER_OF_CHANGE';
12605    ELSIF l_action_type = 'CANCELLATION' AND l_planner_id is not null THEN
12606       resultout := wf_engine.eng_completed || ':' ||  'NOTIFY_PLANNER_OF_CANCEL';*/
12607    END IF;
12608 
12609    EXCEPTION
12610     WHEN OTHERS THEN
12611     l_doc_string := po_reqapproval_init1.get_error_doc(itemtype, itemkey);
12612     l_preparer_user_name := po_reqapproval_init1.get_preparer_user_name(
12613                                                                         itemtype, itemkey);
12614     wf_core.context('PO_REQ_CHANGE_WF', 'SEND_INTERNAL_NOTIF', x_progress);
12615     po_reqapproval_init1.send_error_notif(itemtype, itemkey,
12616                                           l_preparer_user_name, l_doc_string,
12617                                           SQLERRM,
12618                                           'PO_REQ_CHANGE_WF.SEND_INTERNAL_NOTIF');
12619     RAISE;
12620 
12621   END SEND_INTERNAL_NOTIF;
12622 
12623 PROCEDURE NEXT_INTERNAL_NOTIF(itemtype        IN VARCHAR2,
12624                                       itemkey         IN VARCHAR2,
12625                                       actid           IN NUMBER,
12626                                       funcmode        IN VARCHAR2,
12627                                       resultout       OUT NOCOPY VARCHAR2    )IS
12628   l_change_request_group_id NUMBER;
12629   x_progress              VARCHAR2(100);
12630   L_OLD_CHANGE_REQUEST_ID     NUMBER;
12631   L_NEW_CHANGE_REQUEST_ID     NUMBER:= -99;
12632   l_doc_string VARCHAR2(200);
12633   l_preparer_user_name VARCHAR2(100);
12634   BEGIN
12635 --INTERNAL_CHANGE_REQUEST_ID
12636      x_progress := 'PO_ReqChangeRequestWF_PVT.NEXT_INTERNAL_NOTIF: 01';
12637 
12638     -- Do nothing in cancel or timeout mode
12639     IF (funcmode <> wf_engine.eng_run) THEN
12640       resultout := wf_engine.eng_null;
12641       RETURN;
12642     END IF;
12643 
12644 
12645   l_change_request_group_id := po_wf_util_pkg.getitemattrnumber (
12646                                                                    itemtype => itemtype,
12647                                                                    itemkey  => itemkey,
12648                                                                    aname    => 'CHANGE_REQUEST_GROUP_ID');
12649 
12650 
12651   l_old_change_request_id := po_wf_util_pkg.getitemattrnumber (
12652                                                                    itemtype => itemtype,
12653                                                                    itemkey  => itemkey,
12654                                                                    aname    => 'INTERNAL_CHANGE_REQUEST_ID');
12655 
12656    /* Algorithm :  get the INTERNAL_CHANGE_REQUEST_ID and
12657                    increment the INTERNAL_CHANGE_REQUEST_ID
12658    */
12659    BEGIN
12660       SELECT  min(CHANGE_REQUEST_ID)
12661       into L_NEW_CHANGE_REQUEST_ID
12662       FROM po_change_requests
12663       WHERE change_request_group_id = l_change_request_group_id
12664         AND request_status = 'ACCEPTED'
12665         and CHANGE_REQUEST_ID > l_old_change_request_id;
12666   EXCEPTION
12667      WHEN NO_DATA_FOUND THEN
12668      L_NEW_CHANGE_REQUEST_ID := -99;
12669   END;
12670     IF (L_NEW_CHANGE_REQUEST_ID <> -99) THEN
12671       po_wf_util_pkg.setitemattrnumber (itemtype   => itemtype,
12672                                         itemkey    => itemkey,
12673                                         aname      => 'INTERNAL_CHANGE_REQUEST_ID',
12674                                         avalue     => L_NEW_CHANGE_REQUEST_ID);
12675 
12676       resultout := wf_engine.eng_completed || ':' ||  'ACTIVITY_PERFORMED';
12677      END IF;
12678 
12679    EXCEPTION
12680     WHEN OTHERS THEN
12681     l_doc_string := po_reqapproval_init1.get_error_doc(itemtype, itemkey);
12682     l_preparer_user_name := po_reqapproval_init1.get_preparer_user_name(
12683                                                                         itemtype, itemkey);
12684     wf_core.context('PO_REQ_CHANGE_WF', 'NEXT_INTERNAL_NOTIF', x_progress);
12685     po_reqapproval_init1.send_error_notif(itemtype, itemkey,
12686                                           l_preparer_user_name, l_doc_string,
12687                                           SQLERRM,
12688                                           'PO_REQ_CHANGE_WF.NEXT_INTERNAL_NOTIF');
12689     RAISE;
12690 
12691   END NEXT_INTERNAL_NOTIF;
12692 
12693 end PO_ReqChangeRequestWF_PVT;