DBA Data[Home] [Help]

PACKAGE BODY: APPS.PO_REQCHANGEREQUESTWF_PVT

Source


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