DBA Data[Home] [Help]

PACKAGE BODY: APPS.PO_AUTOCREATE_DOC

Source


4 -- Read the profile option that enables/disables the debug log
1 PACKAGE BODY PO_AUTOCREATE_DOC AS
2 /* $Header: POXWATCB.pls 120.37.12020000.13 2013/04/09 07:05:54 ssindhe ship $ */
3 
5 g_po_wf_debug VARCHAR2(1) := NVL(FND_PROFILE.VALUE('PO_SET_DEBUG_WORKFLOW_ON'),'N');
6 
7 /* Private Procedure/Function prototypes */
8 
9 FUNCTION valid_contact(p_vendor_site_id number, p_vendor_contact_id number) RETURN BOOLEAN;
10 FUNCTION get_contact_id(p_contact_name varchar2, p_vendor_site_id number) RETURN NUMBER;
11 
12 -- bug2821542
13 PROCEDURE validate_buyer (p_agent_id IN NUMBER,
14                           x_result   OUT NOCOPY VARCHAR2);
15 
16 --<Shared Proc FPJ START>
17 PROCEDURE set_purchasing_org_id(
18   itemtype        IN VARCHAR2,
19   itemkey         IN VARCHAR2,
20   p_org_id      IN NUMBER,
21   p_suggested_vendor_site_id    IN NUMBER
22 );
23 --<Shared Proc FPJ END>
24 
25 -- <SERVICES FPJ START>
26 PROCEDURE purge_expense_lines(itemtype IN VARCHAR2,
27                               itemkey  IN VARCHAR2);
28 -- <SERVICES FPJ END>
29 
30 --<Bug 14314684 Create Doc WF project>
34 /* Start of procedure/function bodies */
31 FUNCTION is_doc_clm_enabled (l_po_header_id IN NUMBER) RETURN BOOLEAN ;
32 
33 
35 
36 /***************************************************************************
37  *
38  *  Procedure:  start_wf_process
39  *
40  *  Description:  Generates the itemkey, sets up the Item Attributes,
41  *      then starts the Main workflow process.
42  *
43  **************************************************************************/
44 procedure start_wf_process ( ItemType             VARCHAR2,
45                              ItemKey              VARCHAR2,
46                              workflow_process     VARCHAR2,
47                              req_header_id        NUMBER,
48                              po_number            VARCHAR2,
49 			     interface_source_code  VARCHAR2,
50 			     org_id     NUMBER,
51 			     is_federal_flow VARCHAR2 DEFAULT NULL) is -- Bug14364343
52 
53 
54 x_org_id      number;
55 x_progress    varchar2(300);
56 
57 --< Bug 3636669 Start >
58 l_user_id           NUMBER;
59 l_application_id    NUMBER;
60 l_responsibility_id NUMBER;
61 --< Bug 3636669 End >
62 
63 BEGIN
64 
65   x_progress := '10: start_wf_process:  Called with following parameters:' ||
66     'ItemType = ' || ItemType || '/ ' ||
67     'ItemKey = '  || ItemKey  || '/ ' ||
68     'workflow_process = ' || workflow_process || '/ ' ||
69     'req_header_id = ' || to_char(req_header_id) || '/ ' ||
70     'po_number = ' || po_number || '/ ' ||
71     'interface_source_code = ' || interface_source_code;
72 
73   IF (g_po_wf_debug = 'Y') THEN
74      po_wf_debug_pkg.insert_debug(Itemtype,Itemkey,x_progress);
75   END IF;
76 
77 
78   /* If a process is passed then it will be run
79    * If a process is not passed then the selector function defined in
80    * item type will be determine which process to run
81    */
82 
83   IF  ( ItemType is NOT NULL )   AND
84       ( ItemKey is NOT NULL)     AND
85       ( req_header_id is NOT NULL ) THEN
86 
87         --Bug 5490243. Removed the commit introduced in Bug 3293852
88 
89         wf_engine.CreateProcess(itemtype => itemtype,
90                                 itemkey  => itemkey,
91                                 process  => workflow_process );
92 
93         x_progress:= '20: start_wf_process: Just after CreateProcess';
94         IF (g_po_wf_debug = 'Y') THEN
95            po_wf_debug_pkg.insert_debug(Itemtype,Itemkey,x_progress);
96         END IF;
97 
98 
99         /* Initialize workflow item attributes */
100 
101         po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
102                                      itemkey    => itemkey,
103                                      aname      => 'REQ_HEADER_ID',
104                                      avalue     => req_header_id);
105 
106          po_wf_util_pkg.SetItemAttrText (itemtype   => itemtype,
107                                      itemkey    => itemkey,
108                                      aname      => 'PO_NUM_TO_CREATE',
109                                      avalue     => po_number);
110 
111          /* Interface source code can be:
112     *   - FORM = 10sc Enter Req form
113           * - ICX  = Web Reqs
114           *     - SRS  = Conc. program.
115           */
116 
117          po_wf_util_pkg.SetItemAttrText (itemtype   => itemtype,
118                                      itemkey    => itemkey,
119                                      aname      => 'INTERFACE_SOURCE_CODE',
120                                      avalue     => interface_source_code);
121 
122 
123         /* Both web reqs and sc enter reqs should pass in both a req_header_id
124          * and an org_id tied to that header. Even so, I'm going to get the
125          * org_id again from the req_header_id just to be sure.
126          *
127          * Eventually if this workflow gets called for >1 req eg. thru srs then
128          * there the req_header_id maybe null so then we'll just take the
129          * org_id as passed in.
130          */
131 
132   x_org_id := org_id;
133 
134   /* The calling proc should pass in the right org_id associated with the
135    * the req, but get it again just in case.
136    */
137 
138         if (req_header_id is NOT NULL) then
139 
140            select org_id
141        into x_org_id
142              from po_requisition_headers
143       where requisition_header_id = req_header_id;
144 
145         end if;
146 
147          po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
148                                         itemkey    => itemkey,
149                                         aname      => 'ORG_ID',
150                                         avalue     => x_org_id);
151 
152         --< Bug 3636669 Start >
153         -- Retrieve the current application context values. This assumes that
154         -- the application context has been set prior to calling this procedure.
155         FND_PROFILE.get('USER_ID',      l_user_id);
156         FND_PROFILE.get('RESP_ID',      l_responsibility_id);
157         FND_PROFILE.get('RESP_APPL_ID', l_application_id);
158 
159         -- Populate the application context workflow attributes
160         PO_WF_UTIL_PKG.SetItemAttrNumber( itemtype => itemtype
161                                         , itemkey  => itemkey
162                                         , aname    => 'USER_ID'
163                                         , avalue   => l_user_id
164                                         );
165         PO_WF_UTIL_PKG.SetItemAttrNumber( itemtype => itemtype
169                                         );
166                                         , itemkey  => itemkey
167                                         , aname    => 'APPLICATION_ID'
168                                         , avalue   => l_application_id
170         PO_WF_UTIL_PKG.SetItemAttrNumber( itemtype => itemtype
171                                         , itemkey  => itemkey
172                                         , aname    => 'RESPONSIBILITY_ID'
173                                         , avalue   => l_responsibility_id
174                                         );
175         --< Bug 3636669 End >
176 	--< Bug14364343 - create Doc Workflow Project>
177 	-- Setting the IS_CLM_FLOW to determine if it is clm flow
178 	PO_WF_UTIL_PKG.SetItemAttrText( itemtype => itemtype
179                                         , itemkey  => itemkey
180                                         , aname    => 'IS_CLM_FLOW'
181                                         , avalue   => is_federal_flow
182                                         );
183 
184 
185 
186 
187         /* Kick off the process */
188 
189   x_progress :=  '30: start_wf_process: Kicking off StartProcess ';
190   IF (g_po_wf_debug = 'Y') THEN
191     po_wf_debug_pkg.insert_debug(Itemtype,Itemkey,x_progress);
192   END IF;
193 
194         wf_engine.StartProcess(itemtype => itemtype,
195                                itemkey  => itemkey );
196 
197     END IF;
198 
199 exception
200   when others then
201    wf_core.context('po_autocreate_doc','start_wf_process',x_progress);
202    raise;
203 end start_wf_process;
204 
205 
206 /***************************************************************************
207  *
208  *  Procedure:  should_req_be_autocreated
209  *
210  *  Description:  Decides whether automatic autocreation should
211  *      take place or not.
212  *
213  **************************************************************************/
214 procedure should_req_be_autocreated(itemtype   IN   VARCHAR2,
215                                     itemkey    IN   VARCHAR2,
216                                     actid      IN   NUMBER,
217                                     funcmode   IN   VARCHAR2,
218                                     resultout  OUT NOCOPY  VARCHAR2 ) is
219 
220 x_autocreate_doc   varchar2(1);
221 x_progress         varchar2(300);
222 
223 begin
224 
225    /* This decision is made by simply looking at an item atrribute,
226     * which has a default value. All the user needs to do is change
227     * that attribute according to their needs.
228     */
229 
230    x_autocreate_doc := po_wf_util_pkg.GetItemAttrText (itemtype => itemtype,
231                                                   itemkey  => itemkey,
232                                                   aname    => 'AUTOCREATE_DOC');
233 
234    if (x_autocreate_doc = 'Y') then
235      resultout := wf_engine.eng_completed || ':' ||  'Y';
236 
237      x_progress:= '10: should_req_be_autocreated: result = Y';
238      IF (g_po_wf_debug = 'Y') THEN
239         po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
240      END IF;
241 
242    else
243      resultout := wf_engine.eng_completed || ':' ||  'N';
244 
245      x_progress:= '20: should_req_be_autocreated: result = N';
246      IF (g_po_wf_debug = 'Y') THEN
247         po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
248      END IF;
249    end if;
250 
251 exception
252   when others then
253     wf_core.context('po_autocreate_doc','should_req_be_autocreated',x_progress);
254     raise;
255 end should_req_be_autocreated;
256 
257 
258 /***************************************************************************
259  *
260  *  Procedure:  launch_req_line_processing
261  *
262  *  Description:  This process gets the req lines available for
263  *      autocreate either belonging to the req or all
264  *      the ones in the req pool. This depends on whether
265  *      whether a req_header_id was passed into the
266  *      workflow.
267  *
268  **************************************************************************/
269 procedure launch_req_line_processing(itemtype  IN   VARCHAR2,
270                                      itemkey   IN   VARCHAR2,
271                                      actid     IN   NUMBER,
272                                      funcmode  IN   VARCHAR2,
273                                      resultout OUT NOCOPY  VARCHAR2 ) is
274 
275 
276 
277 x_ItemType              varchar2(20) := itemtype; /* Calling proc has same
278                * item type as called proc
279                  */
280 x_ItemKey               varchar2(60) := null;
281 x_workflow_process      varchar2(40) := 'REQ_LINE_PROCESSING';
282 x_seq_for_item_key  varchar2(25)  := null;  --Bug14305923
283 x_req_header_id     number;
284 x_req_line_id     number;
285 x_group_id    number;
286 x_org_id    number;
287 x_progress          varchar2(300);
288 l_is_clm_flow VARCHAR2(2);
289 
290 
291 /* Declare cursor to get all the req lines for the req
292  * passed into the workflow.
293  */
294 
295 /* Bug# 1121317: kagarwal
296 ** Desc: The cursor c1 in launch_req_line_processing() of POXWATCB.pls
297 ** is fetching the requisition_line_id using the po_requisition_lines_inq_v
298 ** view which is a join of more than 20 tables. The cursor has been modified
299 ** to improve performance.
300 */
301 
302 /* Bug1366981
303    The following query is still performance intensive and so commenting
304    the same and change the cursor c1 to increase the performance
305 
309       where line_location_id                is null           AND
306 cursor c1 is           x_req_header_id is a parameter
307      select pol.requisition_line_id
308        from po_requisition_headers poh, po_requisition_lines pol
310       nvl(pol.cancel_flag,'N')            ='N'    AND
311       nvl(pol.closed_code,'OPEN')     <> 'FINALLY CLOSED' AND
312             nvl(modified_by_agent_flag,'N') ='N'          AND
313             source_type_code        = 'VENDOR'          AND
314       authorization_status      = 'APPROVED'  AND
315       (poh.requisition_header_id      = x_req_header_id
316        OR
317        x_req_header_id is null)       AND
318       poh.requisition_header_id = pol.requisition_header_id
319    order by poh.requisition_header_id, line_num;
320 */
321 /*Bug 1366981
322   To handle  the x_req_header_id null case and to ensure the
323   index on requisition_header_id is used,modified the cursor c1
324   to use  a union all  and thereby increase performance.
325  */
326 /*The first part of this query cause performance hit for bug 10243160. splite the cursor
327 cursor c1 is                            -- x_req_header_id is a parameter
328 select pol.requisition_line_id
329   from po_requisition_headers_all poh,    -- <R12 MOAC>
330        po_requisition_lines pol
331  where x_req_header_id is null                            AND
332       line_location_id                is null            AND
333       nvl(pol.cancel_flag,'N')            ='N'            AND
334       nvl(pol.closed_code,'OPEN')    <> 'FINALLY CLOSED' AND
335       nvl(modified_by_agent_flag,'N') ='N'                AND
336       source_type_code                = 'VENDOR'          AND
337       authorization_status            = 'APPROVED'        AND
338       poh.requisition_header_id = pol.requisition_header_id
339 union all
340 select pol.requisition_line_id
341   from po_requisition_headers_all poh,     -- <R12 MOAC>
342        po_requisition_lines pol
343  where x_req_header_id is not null                        AND
344       poh.requisition_header_id      = x_req_header_id    AND
345       line_location_id                is null            AND
346       nvl(pol.cancel_flag,'N')            ='N'            AND
347       nvl(pol.closed_code,'OPEN')    <> 'FINALLY CLOSED' AND
348       nvl(modified_by_agent_flag,'N') ='N'                AND
349       source_type_code                = 'VENDOR'          AND
350       authorization_status            = 'APPROVED'        AND
351       poh.requisition_header_id = pol.requisition_header_id;
352 */
353 --Bug 10243160 start. Separate the cursor into c1 and c2. c2 is for concurrent program. It may cause performance hit.
354 --Use c2 only if it needs. so that reduce the overhead for regular autocreate flow.
355 cursor c1 is  --c1 is for autocreate flow. x_req_header_id is a parameter
356 select pol.requisition_line_id
357   from po_requisition_headers_all poh,     -- <R12 MOAC>
358        po_requisition_lines pol
359  where poh.requisition_header_id      = x_req_header_id    AND
360       line_location_id                is null            AND
361       nvl(pol.cancel_flag,'N')            ='N'            AND
362       nvl(pol.closed_code,'OPEN')    <> 'FINALLY CLOSED' AND
363       nvl(modified_by_agent_flag,'N') ='N'                AND
364       source_type_code                = 'VENDOR'          AND
365       authorization_status            = 'APPROVED'        AND
366       poh.requisition_header_id = pol.requisition_header_id
367       order by pol.requisition_line_id asc;
368 
369 --c2 is for concurrent program flow.
370 cursor c2 is
371 select pol.requisition_line_id
372   from po_requisition_headers_all poh,    -- <R12 MOAC>
373        po_requisition_lines pol
374  where line_location_id                is null            AND
375       nvl(pol.cancel_flag,'N')            ='N'            AND
376       nvl(pol.closed_code,'OPEN')    <> 'FINALLY CLOSED' AND
377       nvl(modified_by_agent_flag,'N') ='N'                AND
378       source_type_code                = 'VENDOR'          AND
379       authorization_status            = 'APPROVED'        AND
380       poh.requisition_header_id = pol.requisition_header_id;
381 --end 10243160
382 --<CONSUME REQ DEMAND FPI START>
383 l_consume_req_demand_doc_id po_headers.po_header_id%type;
384 --sql what: get all the req lines which have the same bid/negotiation info as
385 --          those on the lines of the current blanket po approval workflow
386 --          just approved.
387 --sql why : This cursor fetches all these eligible requisition lines and lauches
388 --      start_wf_line_process one by one. This would happen only if
389 --      l_consume_req_demand_doc_id is not null. l_consume_req_demand_doc_id
390 --      is the blanket document id passed into created document workflow
391 --      from PO approval workflow.
392 --sql join: find all the blanket lines of l_consume_req_demand_doc_id,
393 --      equate the bid_number,bid_line_number and auction_header_id
394 --      of these lines to requisition lines from po_requisition_lines.
395 --      Also ensures these lines are not placed on another PO, they are
396 --      still in approved status, not modified by the buyer, source type is
397 --      vendor and not finally closed
398 
399 CURSOR C_ConsumeReqLines is
400 SELECT prl.requisition_line_id
401   FROM po_lines pol,
402        po_requisition_lines prl,
403        po_requisition_headers_all prh    -- <R12 MOAC>
404  WHERE pol.po_header_id=l_consume_req_demand_doc_id
405    AND prl.auction_header_id = pol.auction_header_id
406    AND prl.bid_line_number = pol.bid_line_number
407    AND prl.bid_number = pol.bid_number
408    AND prl.line_location_id is null
409    AND nvl(prl.cancel_flag,'N') ='N'
410    AND nvl(prl.closed_code,'OPEN') <> 'FINALLY CLOSED'
411    AND nvl(prl.modified_by_agent_flag,'N') ='N'
412    AND prl.source_type_code   = 'VENDOR'
416 
413    AND prh.authorization_status = 'APPROVED'
414    AND prh.requisition_header_id = prl.requisition_header_id;
415 --<CONSUME REQ DEMAND FPI END>
417 --<Bug 14314684 - Create Doc Workflow project START>
418 cursor C_CLMReqLines is
419 select pol.requisition_line_id
420   from po_requisition_headers_all poh,     -- <R12 MOAC>
421        po_requisition_lines pol
422  where poh.requisition_header_id      = x_req_header_id    AND
423       line_location_id                is null            AND
424       nvl(pol.cancel_flag,'N')            ='N'            AND
425       nvl(pol.closed_code,'OPEN')    <> 'FINALLY CLOSED' AND
426       nvl(modified_by_agent_flag,'N') ='N'                AND
427       source_type_code                = 'VENDOR'          AND
428       authorization_status            = 'APPROVED'        AND
429       poh.requisition_header_id = pol.requisition_header_id AND
430       pol.GROUP_LINE_ID IS NULL AND
431       pol.CLM_BASE_LINE_NUM is NULL AND
432       Nvl(pol.CLM_INFO_FLAG,'N') = 'N' AND
433       NOT EXISTS
434      (SELECT 'Y' FROM  po_requisition_lines_all  prl1
435       WHERE prl1.requisition_header_id = poh.requisition_header_id
436       AND prl1.GROUP_LINE_ID = pol.REQUISITION_LINE_ID
437       OR prl1.CLM_BASE_LINE_NUM = pol.REQUISITION_LINE_ID)
438       order by pol.requisition_line_id asc;
439 --<Bug 14314684 - Create Doc Workflow project END>
440 
441 BEGIN
442    /* Set org context */
443    x_org_id := po_wf_util_pkg.GetItemAttrNumber (itemtype => itemtype,
444                                            itemkey  => itemkey,
445                                            aname    => 'ORG_ID');
446 
447    po_moac_utils_pvt.set_org_context(x_org_id); --<R12 MOAC>
448 
449    --<CONSUME REQ DEMAND FPI START>
450    l_consume_req_demand_doc_id := PO_WF_UTIL_PKG.GetItemAttrNumber
451           (itemtype => itemtype,
452                                    itemkey  => itemkey,
453                                    aname    => 'CONSUME_REQ_DEMAND_DOC_ID');
454    IF l_consume_req_demand_doc_id is null then
455    --<CONSUME REQ DEMAND FPI END>
456 
457       /* If this create doc workflow was called from either the
458        * 10sc form or web req form, they pass in a req_header_id
459        * If the workflow was called by the conc. prg then there
460        * may or may not be a req_header_id passed in.
461        */
462 
463 
464       x_req_header_id := po_wf_util_pkg.GetItemAttrText (itemtype => itemtype,
465                                              itemkey  => itemkey,
466                                              aname    => 'REQ_HEADER_ID');
467 
468       --<Bug 14314684 - Create Doc Workkflow Project>
469       l_is_clm_flow :=  po_wf_util_pkg.GetItemAttrText (itemtype => itemtype,
470                                              itemkey  => itemkey,
471                                              aname    => 'IS_CLM_FLOW');
472 
473       /* Get the group_id for this set of requision lines. The
474        * group_id determines which lines should be processed together,
475        * which in this case are all the lines belonging to the one req.
476        */
477 
478        select to_char(PO_WF_GROUP_S.NEXTVAL)
479            into x_group_id
480            from sys.dual;
481 
482 
483        /* Store the group_id so grouping (later) only considers
484         * records with this group_id.
485         */
486 
487        po_wf_util_pkg.SetItemAttrNumber (itemtype     => itemtype,
488                                         itemkey    => itemkey,
489                                         aname      => 'GROUP_ID',
490                                         avalue     => x_group_id);
491 
492 
493 
494       /* Open cursor and loop thru all the req lines, launching the
495        * the req line processing workflow for each line.
496        */
497 
498       x_progress:= '10: launch_req_line_processing: Just before opening cursor c1 ' ||
499         'for req_header_id = ' || to_char(x_req_header_id);
500 
501       IF (g_po_wf_debug = 'Y') THEN
502          po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
503       END IF;
504 
505       --Bug 10243160 Added IF x_req_header_id is not null  condition
506       IF x_req_header_id is not null then
507 	--<Bug 14314684 - Create Doc Workflow Project START>
508 	IF NVL(l_is_clm_flow,'N') = 'Y'  THEN
509         open C_CLMReqLines;
510 
511 	loop
512          fetch C_CLMReqLines into x_req_line_id;
513          exit when C_CLMReqLines%NOTFOUND;
514 
515          x_progress:= '20: launch_req_line_processing in CLM flow : In loop,fetched C_CLMReqLines req_line_id = '||
516            to_char(x_req_line_id);
517          IF (g_po_wf_debug = 'Y') THEN
518             po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
519          END IF;
520 
521          /* Get the unique sequence to make sure item key will be unique */
522 
523          select to_char(PO_WF_ITEMKEY_S.NEXTVAL)
524            into x_seq_for_item_key
525            from sys.dual;
526 
527          /* The item key is the req_line_id concatenated with the
528           * unique id from a seq.
529           */
530 
531          x_ItemKey := to_char(x_req_line_id) || '-' || x_seq_for_item_key;
532 
533          /* Launch the req line processing process
534           *
535           * Need to pass in the parent's itemtype and itemkey so as to
536           * all the parent child relationship to be setup in the called
537           * process.
538           */
539 
540          x_progress:= '30: launch_req_line_processing: Just about to launch '||
541          ' start_wf_line_process with: called_item_type = ' || x_ItemType
545          IF (g_po_wf_debug = 'Y') THEN
542           || '/ ' || 'called_item_key = ' || x_ItemKey || '/ ' ||
543                        'group_id = ' || to_char(x_group_id);
544 
546             po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
547          END IF;
548 
549          po_autocreate_doc.start_wf_line_process (x_ItemType,
550                                       x_ItemKey,
551                                       x_workflow_process,
552                       x_group_id,
553                       x_req_header_id,
554                       x_req_line_id,
555                       itemtype,
556                       itemkey);
557 
558       end loop;
559       close C_CLMReqLines;
560       --<Bug 14314684 - Create Doc Workflow Project END>
561 
562       ELSE
563 
564       open c1;   /* Based on x_req_header_id */
565 
566       loop
567          fetch c1 into x_req_line_id;
568          exit when c1%NOTFOUND;
569 
570          x_progress:= '20: launch_req_line_processing: In loop,fetched c1 req_line_id = '||
571            to_char(x_req_line_id);
572          IF (g_po_wf_debug = 'Y') THEN
573             po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
574          END IF;
575 
576          /* Get the unique sequence to make sure item key will be unique */
577 
578          select to_char(PO_WF_ITEMKEY_S.NEXTVAL)
579            into x_seq_for_item_key
580            from sys.dual;
581 
582          /* The item key is the req_line_id concatenated with the
583           * unique id from a seq.
584           */
585 
586          x_ItemKey := to_char(x_req_line_id) || '-' || x_seq_for_item_key;
587 
588          /* Launch the req line processing process
589           *
590           * Need to pass in the parent's itemtype and itemkey so as to
591           * all the parent child relationship to be setup in the called
592           * process.
593           */
594 
595          x_progress:= '30: launch_req_line_processing: Just about to launch '||
596          ' start_wf_line_process with: called_item_type = ' || x_ItemType
597           || '/ ' || 'called_item_key = ' || x_ItemKey || '/ ' ||
598                        'group_id = ' || to_char(x_group_id);
599 
600          IF (g_po_wf_debug = 'Y') THEN
601             po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
602          END IF;
603 
604          po_autocreate_doc.start_wf_line_process (x_ItemType,
605                                       x_ItemKey,
606                                       x_workflow_process,
607                       x_group_id,
608                       x_req_header_id,
609                       x_req_line_id,
610                       itemtype,
611                       itemkey);
612 
613       end loop;
614       close c1;
615       END IF;
616 
617       -- Bug 10243160. make use of c2 for concurrent program
618       ELSE --x_requesition_header_id is null
619                 open c2;
620                 loop
621                  fetch c2 into x_req_line_id;
622                  exit when c2%NOTFOUND;
623 
624                  x_progress:= '21: launch_req_line_processing: In loop,fetched c2 req_line_id = '||
625                  to_char(x_req_line_id);
626                  IF (g_po_wf_debug = 'Y') THEN
627                     po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
628                  END IF;
629 
630                /* Get the unique sequence to make sure item key will be unique */
631 
632                  select to_char(PO_WF_ITEMKEY_S.NEXTVAL)
633                  into x_seq_for_item_key
634                  from sys.dual;
635 
636                /* The item key is the req_line_id concatenated with the
637                 * unique id from a seq.
638                 */
639 
640                x_ItemKey := to_char(x_req_line_id) || '-' || x_seq_for_item_key;
641 
642                /* Launch the req line processing process
643                 *
644                 * Need to pass in the parent's itemtype and itemkey so as to
645                 * all the parent child relationship to be setup in the called
646                 * process.
647                 */
648 
649                x_progress:= '31: launch_req_line_processing: Just about to launch '||
650                ' start_wf_line_process with: called_item_type = ' || x_ItemType
651                 || '/ ' || 'called_item_key = ' || x_ItemKey || '/ ' ||
652                              'group_id = ' || to_char(x_group_id);
653 
654                IF (g_po_wf_debug = 'Y') THEN
655                   po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
656                END IF;
657 
658                po_autocreate_doc.start_wf_line_process (x_ItemType,
659                                             x_ItemKey,
660                                             x_workflow_process,
661                             x_group_id,
662                             x_req_header_id,
663                             x_req_line_id,
664                             itemtype,
665                             itemkey);
666 
667              end loop;
668              close c2;
669       END IF;
670       resultout := wf_engine.eng_completed || ':' ||  'ACTIVITY_PERFORMED';
671 
672       x_progress:= '40:launch_req_line_processing: result = ACTIVITY_PERFORMED';
673       IF (g_po_wf_debug = 'Y') THEN
674          po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
675       END IF;
676 
677    --<CONSUME REQ DEMAND FPI START>
678    ELSE  --if l_consume_req_demand_doc_id is not null
679 
683 
680     --Get the group_id for this set of requision lines. The
681       --group_id determines which lines should be processed together,
682       --which in this case are all the lines belonging to the one req.
684       SELECT to_char(PO_WF_GROUP_S.NEXTVAL)
685          INTO x_group_id
686          FROM sys.dual;
687 
688       -- Store the group_id so grouping (later) only considers
689       -- records with this group_id.
690         PO_WF_UTIL_PKG.SetItemAttrNumber (itemtype     => itemtype,
691                                      itemkey    => itemkey,
692                                      aname      => 'GROUP_ID',
693                                      avalue     => x_group_id);
694 
695     -- Open cursor and loop thru all the req lines, launching the
696       --the req line processing workflow for each line.
697 
698     x_progress:= '10: launch_req_line_processing: Just before opening '
699          ||'cursor C_ConsumeReqLines '
700          || 'for l_consume_req_demand_doc_id='
701          || to_char(l_consume_req_demand_doc_id);
702 
703     IF (g_po_wf_debug = 'Y') THEN
704         po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
705     END IF;
706 
707     OPEN C_ConsumeReqLines;
708 
709     LOOP
710           FETCH C_ConsumeReqLines into x_req_line_id;
711           EXIT WHEN C_ConsumeReqLines%NOTFOUND;
712 
713           x_progress:= '20: launch_req_line_processing: In loop,fetched'
714            ||' C_ConsumeReqLines req_line_id = '
715            || to_char(x_req_line_id);
716           IF (g_po_wf_debug = 'Y') THEN
717              po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
718           END IF;
719 
720           --Get the unique sequence to make sure item key will be unique
721 
722     SELECT to_char(PO_WF_ITEMKEY_S.NEXTVAL)
723       INTO x_seq_for_item_key
724       FROM sys.dual;
725 
726           --The item key is the req_line_id concatenated with the
727           --unique id from a seq.
728 
729           x_ItemKey := to_char(x_req_line_id) || '-' || x_seq_for_item_key;
730 
731           --Launch the req line processing process
732           --Need to pass in the parent's itemtype and itemkey so as to
733           --all the parent child relationship to be setup in the called
734           --process.
735 
736            x_progress:= '30: launch_req_line_processing: Just about to launch '
737       || ' start_wf_line_process with: called_item_type = '
738       || x_ItemType || '/ ' || 'called_item_key = '
739       || x_ItemKey || '/ ' || 'group_id = '
740       || to_char(x_group_id);
741 
742            IF (g_po_wf_debug = 'Y') THEN
743               po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
744            END IF;
745 
746            po_autocreate_doc.start_wf_line_process (x_ItemType,
747                                        x_ItemKey,
748                                        x_workflow_process,
749                  x_group_id,
750                  x_req_header_id,
751                  x_req_line_id,
752                  itemtype,
753                  itemkey);
754 
755         END LOOP;
756         CLOSE C_ConsumeReqLines;
757 
758         resultout := wf_engine.eng_completed || ':' ||  'ACTIVITY_PERFORMED';
759 
760         x_progress:='40:launch_req_line_processing: result =ACTIVITY_PERFORMED';
761         IF (g_po_wf_debug = 'Y') THEN
762            po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
763         END IF;
764    END IF; --l_consume_req_demand_doc_id is null--
765    --<CONSUME REQ DEMAND FPI END>
766 
767 exception
768   when others then
769     if l_consume_req_demand_doc_id is not null then
770       close c1;
771     else
772       CLOSE C_ConsumeReqLines;
773     end if;
774     wf_core.context('po_autocreate_doc','launch_req_line_processing',x_progress);
775     raise;
776 end launch_req_line_processing;
777 
778 /***************************************************************************
779  *
780  *  Procedure:  start_wf_line_process
781  *
782  *  Description:  Generates the itemkey, sets up the Item Attributes,
783  *      then starts the workflow process.
784  *
785  **************************************************************************/
786 procedure start_wf_line_process ( ItemType            VARCHAR2,
787                                   ItemKey             VARCHAR2,
788                                   workflow_process    VARCHAR2,
789                 group_id    NUMBER,
790               req_header_id   NUMBER,
791           req_line_id   NUMBER,
792           parent_itemtype VARCHAR2,
793           parent_itemkey  VARCHAR2) is
794 
795 
796 x_progress    varchar2(300);
797 
798 begin
799 
800   x_progress := '10: start_wf_line_process: Called with item_type = ' || ItemType ||
801      '/ '|| 'item_key = ' || ItemKey;
802   IF (g_po_wf_debug = 'Y') THEN
803      po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
804   END IF;
805 
806   /* If a process is passed then it will be run
807    * If a process is not passed then the selector function defined in
808    * item type will be determine which process to run
809    */
810 
811   IF  (ItemType    is NOT NULL ) AND
812       (ItemKey     is NOT NULL)  AND
813       (req_line_id is NOT NULL ) then
814         wf_engine.CreateProcess(itemtype => itemtype,
815                                 itemkey  => itemkey,
816                                 process  => workflow_process );
817 
818         x_progress := '20: start_wf_line_process: Just after CreateProcess';
819   IF (g_po_wf_debug = 'Y') THEN
823         /* Initialize workflow item attributes */
820     po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
821   END IF;
822 
824 
825         po_wf_util_pkg.SetItemAttrNumber (itemtype     => itemtype,
826                                      itemkey    => itemkey,
827                                      aname      => 'GROUP_ID',
828                                      avalue     => group_id);
829 
830         po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
831                                      itemkey    => itemkey,
832                                      aname      => 'REQ_LINE_ID',
833                                      avalue     => req_line_id);
834 
835   po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
836                                      itemkey    => itemkey,
837                                      aname      => 'REQ_HEADER_ID',
838                                      avalue     => req_header_id);
839 
840 
841   /* Need to set the parent child relationship between processes */
842 
843   wf_engine.SetItemParent (itemtype        => itemtype,
844          itemkey         => itemkey,
845          parent_itemtype => parent_itemtype,
846          parent_itemkey  => parent_itemkey,
847          parent_context  => NULL);
848 
849 
850         /* Kick off the process */
851 
852         x_progress:= '30: start_wf_line_process: Kicking off StartProcess';
853         IF (g_po_wf_debug = 'Y') THEN
854            po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
855         END IF;
856 
857         wf_engine.StartProcess(itemtype => itemtype,
858                                itemkey  => itemkey );
859 
860     end if;
861 
862 exception
863   when others then
864    x_progress:= '40: start_wf_line_process: IN EXCEPTION';
865    IF (g_po_wf_debug = 'Y') THEN
866       po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
867    END IF;
868    raise;
869 end start_wf_line_process;
870 
871 
872 /***************************************************************************
873  *
874  *  Procedure:  get_req_info
875  *
876  *  Description:  Gets all the necessary info from the req line
877  *
878  *
879  **************************************************************************/
880 procedure get_req_info (itemtype   IN   VARCHAR2,
881                         itemkey    IN   VARCHAR2,
882                         actid      IN   NUMBER,
883                         funcmode   IN   VARCHAR2,
884                         resultout  OUT NOCOPY  VARCHAR2 ) is
885 
886 x_req_line_id       number;
887 x_suggested_buyer_id    number;
888 x_suggested_vendor_name   po_requisition_lines_all.suggested_vendor_name%type;
889 x_suggested_vendor_location     varchar2(240);
890 
891 /* Bug 2577940 The vendor id and vendor site id should also be populated from Req line  */
892 x_suggested_vendor_id           po_requisition_lines_all.vendor_id%type;
893 x_suggested_vendor_site_id      po_requisition_lines_all.vendor_site_id%type;
894 /* Bug 2577940 */
895 
896 x_source_doc_type_code    varchar2(25);
897 x_source_doc_po_header_id number;
898 x_source_doc_line_num   number;
899 x_rfq_required_flag   varchar2(1);
900 x_on_rfq_flag     varchar2(1);
901 x_item_id     number;
902 x_category_id     number;
903 x_currency_code     varchar2(15);
904 x_rate_type     varchar2(30);
905 x_rate_date     date;
906 x_rate        number;
907 x_org_id      number;
908 x_pcard_id      number;
909 x_pcard_flag      varchar2(1);
910 x_progress          varchar2(300);
911 x_organization_id               number;
912 x_catalog_type      varchar2(40);
913 /* Supplier Pcard FPH */
914 x_vendor_id     number;
915 x_vendor_site_id    number;
916 
917 x_ga_flag                       varchar2(1) := 'N'; -- FPI GA
918 l_job_id                        number := null;  -- <SERVICES FPJ>
919 l_labor_req_line_id             po_requisition_lines_all.labor_req_line_id%TYPE;  -- <SERVICES FPJ>
920 x_federal_flag VARCHAR2(1);
921 
922 begin
923 
924   x_req_line_id := po_wf_util_pkg.GetItemAttrNumber (itemtype => itemtype,
925                                                  itemkey  => itemkey,
926                                                  aname    => 'REQ_LINE_ID');
927 
928   /* Get the necessary info from the req line */
929 
930   select pls.org_id,
931    pls.suggested_buyer_id,
932    pls.suggested_vendor_name,
933          pls.suggested_vendor_location,
934      pls.document_type_code,
935          pls.blanket_po_header_id,
936    pls.blanket_po_line_num,
937          pls.rfq_required_flag,
938    pls.on_rfq_flag,
939          pls.item_id,
940    pls.category_id,
941          pls.currency_code,
942    pls.rate_type,
943    pls.rate_date,
944    pls.rate,
945    pls.pcard_flag,
946    /* Supplier PCard FPH */
947    --decode(pls.pcard_flag, 'Y', phs.pcard_id,'S',-99999,'N', null),
948    --16027770
949    decode(pls.pcard_flag, 'Y', phs.pcard_id,'S',nvl((po_pcard_pkg.get_valid_pcard_id(-99999,pls.vendor_id,pls.vendor_site_id)),-99999),'N', null),
950          pls.destination_organization_id,
951    pls.catalog_type,
952          pls.vendor_id, /* Bug 2577940 */
953          pls.vendor_site_id,
954          pls.job_id,  -- <SERVICES FPJ>
955          pls.labor_req_line_id , -- <SERVICES FPJ>
956 	 phs.federal_flag -- Bug 14314684
957     into x_org_id,
958    x_suggested_buyer_id,
959          x_suggested_vendor_name,
960          x_suggested_vendor_location,
961          x_source_doc_type_code,
962    x_source_doc_po_header_id,
963    x_source_doc_line_num,
964    x_rfq_required_flag,
965    x_on_rfq_flag,
966          x_item_id,
967    x_category_id,
968    x_currency_code,
969    x_rate_type,
970    x_rate_date,
971    x_rate,
972    x_pcard_flag,
973    x_pcard_id,
974          x_organization_id,
975    x_catalog_type,
976          x_suggested_vendor_id,
977          x_suggested_vendor_site_id,
978          l_job_id,  -- <SERVICES FPJ>
979          l_labor_req_line_id,  -- <SERVICES FPJ>
980  	x_federal_flag -- Bug 14314684
981     from po_requisition_headers_all phs,   -- <R12 MOAC>
982          po_requisition_lines pls
983    where pls.requisition_line_id = x_req_line_id
984      and phs.requisition_header_id = pls.requisition_header_id;
985 
986 
987   x_progress:= '10: get_req_info: Just after executing sql stmt with req_line_id ' ||
988     to_char(x_req_line_id);
989   IF (g_po_wf_debug = 'Y') THEN
990      po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
991   END IF;
992 
993 
994   /* Set the item attributes */
995 
996   po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
997                                itemkey    => itemkey,
998                                aname      => 'ORG_ID',
999                                avalue     => x_org_id);
1000 
1001   po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
1002                                itemkey    => itemkey,
1003                                aname      => 'SUGGESTED_BUYER_ID',
1004                                avalue     => x_suggested_buyer_id);
1005 
1006   po_wf_util_pkg.SetItemAttrText (itemtype   => itemtype,
1007                              itemkey    => itemkey,
1008                              aname      => 'SUGGESTED_VENDOR_NAME',
1009                              avalue     => x_suggested_vendor_name);
1010 
1011   po_wf_util_pkg.SetItemAttrText (itemtype   => itemtype,
1012                              itemkey    => itemkey,
1013                              aname      => 'SUGGESTED_VENDOR_LOCATION',
1014                              avalue     => x_suggested_vendor_location);
1015   /* Bug 2577940 */
1016   po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
1017                               itemkey    => itemkey,
1018                               aname      => 'SUGGESTED_VENDOR_ID',
1019                               avalue     => x_suggested_vendor_id);
1020 
1021   po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
1022                                itemkey    => itemkey,
1023                                aname      => 'SUGGESTED_VENDOR_SITE_ID',
1024                                avalue     => x_suggested_vendor_site_id);
1025   /* Bug 2577940 */
1026   po_wf_util_pkg.SetItemAttrText (itemtype   => itemtype,
1027                              itemkey    => itemkey,
1028                              aname      => 'SOURCE_DOCUMENT_TYPE_CODE',
1029                              avalue     => x_source_doc_type_code);
1030 
1031   po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
1032                                itemkey    => itemkey,
1033                                aname      => 'SOURCE_DOCUMENT_ID',
1034                                avalue     => x_source_doc_po_header_id);
1035 
1036   /* FPI GA Start */
1037   /* Get the global agreement flag */
1038 
1039    if x_source_doc_po_header_id is not null then
1040      select global_agreement_flag
1041      into x_ga_flag
1042      from po_headers_all
1043      where po_header_id = x_source_doc_po_header_id;
1044    end if;
1045 
1046      po_wf_util_pkg.SetItemAttrText (itemtype   => itemtype,
1047                                        itemkey    => itemkey,
1048                                        aname      => 'SOURCE_DOC_GA_FLAG',
1049                                        avalue     => x_ga_flag);
1050 
1051 
1052   /* FPI GA End */
1053 
1054   po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
1055                                itemkey    => itemkey,
1056                                aname      => 'SOURCE_DOCUMENT_LINE_NUM',
1057                                avalue     => x_source_doc_line_num);
1058 
1059   po_wf_util_pkg.SetItemAttrText (itemtype   => itemtype,
1060                              itemkey    => itemkey,
1061                              aname      => 'RFQ_REQUIRED_FLAG',
1062                              avalue     => x_rfq_required_flag);
1063 
1064   po_wf_util_pkg.SetItemAttrText (itemtype   => itemtype,
1065                              itemkey    => itemkey,
1066                              aname      => 'ON_RFQ_FLAG',
1067                              avalue     => x_on_rfq_flag);
1068 
1069   po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
1070                                itemkey    => itemkey,
1071                                aname      => 'ITEM_ID',
1072                                avalue     => x_item_id);
1073 
1074   po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
1075                                itemkey    => itemkey,
1076                                aname      => 'CATEGORY_ID',
1077                                avalue     => x_category_id);
1078 
1079   po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
1080                                itemkey    => itemkey,
1081                                aname      => 'ORGANIZATION_ID',
1082                                avalue     => x_organization_id);
1083 
1084   -- Bug 587589, lpo, 12/11/97
1085   -- Added the follow 4 lines to populate the currency_code, rate_type, rate_date and rate.
1086   po_wf_util_pkg.SetItemAttrText (itemtype   => itemtype,
1087                              itemkey    => itemkey,
1088                              aname      => 'CURRENCY_CODE',
1089                              avalue     => x_currency_code);
1093                              aname      => 'RATE_TYPE',
1090 
1091   po_wf_util_pkg.SetItemAttrText (itemtype   => itemtype,
1092                              itemkey    => itemkey,
1094                              avalue     => x_rate_type);
1095 
1096   po_wf_util_pkg.SetItemAttrDate (itemtype   => itemtype,
1097                              itemkey    => itemkey,
1098                              aname      => 'RATE_DATE',
1099                              avalue     => x_rate_date);
1100 
1101   po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
1102                                itemkey    => itemkey,
1103                                aname      => 'RATE',
1104                                avalue     => x_rate);
1105 
1106   po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
1107                                itemkey    => itemkey,
1108                                aname      => 'PCARD_ID',
1109                                avalue     => x_pcard_id);
1110 
1111   po_wf_util_pkg.SetItemAttrText  (itemtype   => itemtype,
1112                                itemkey    => itemkey,
1113                                aname      => 'CATALOG_TYPE',
1114                                avalue     => x_catalog_type);
1115   -- Bug 587589, lpo, 12/11/97
1116 
1117   --<Shared Proc FPJ START>
1118   --Set the PURCHASING_ORG_ID workflow item attribute
1119   set_purchasing_org_id(itemtype,
1120       itemkey,
1121       x_org_id,
1122       x_suggested_vendor_site_id);
1123   --<Shared Proc FPJ END>
1124 
1125   -- <SERVICES FPJ START>
1126   po_wf_util_pkg.SetItemAttrNumber(itemtype => itemtype,
1127                                    itemkey  => itemkey,
1128                                    aname    => 'JOB_ID',
1129                                    avalue   => l_job_id);
1130 
1131   po_wf_util_pkg.SetItemAttrNumber(itemtype => itemtype,
1132                                    itemkey  => itemkey,
1133                                    aname    => 'LABOR_REQ_LINE_ID',
1134                                    avalue   => l_labor_req_line_id);
1135   -- <SERVICES FPJ END>
1136 
1137   --<Bug 14314684 Create Doc WF Project>
1138   po_wf_util_pkg.SetItemAttrText(itemtype => itemtype,
1139                                    itemkey  => itemkey,
1140                                    aname    => 'IS_CLM_FLOW',
1141                                    avalue   => x_federal_flag);
1142 
1143 
1144   resultout := wf_engine.eng_completed || ':' ||  'ACTIVITY_PERFORMED';
1145 
1146   x_progress:= '20: get_req_info: result = ACTIVITY_PERFORMED';
1147   IF (g_po_wf_debug = 'Y') THEN
1148      po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
1149   END IF;
1150 
1151 exception
1152   when others then
1153     wf_core.context('po_autocreate_doc','get_req_info',x_progress);
1154     raise;
1155 end get_req_info;
1156 
1157 
1158 /***************************************************************************
1159  *
1160  *  Procedure:  rfq_required_check
1161  *
1162  *  Description:  Checks if an this req line should  be on an RFQ before
1163  *      it can be autocreated
1164  *
1165  **************************************************************************/
1166 procedure rfq_required_check (itemtype   IN   VARCHAR2,
1167                               itemkey    IN   VARCHAR2,
1168                               actid      IN   NUMBER,
1169                               funcmode   IN   VARCHAR2,
1170                               resultout  OUT NOCOPY  VARCHAR2 ) is
1171 
1172 x_org_id    number;
1173 x_rfq_required_flag varchar2(1);
1174 x_on_rfq_flag   varchar2(1);
1175 x_warn_rfq_required varchar2(1);
1176 x_progress        varchar2(300);
1177 
1178 
1179 begin
1180 
1181   /* Set org context */
1182   x_org_id := po_wf_util_pkg.GetItemAttrNumber (itemtype => itemtype,
1183                                            itemkey  => itemkey,
1184                                            aname    => 'ORG_ID');
1185   po_moac_utils_pvt.set_org_context(x_org_id); --<R12 MOAC>
1186 
1187   /* Get rfq check flags */
1188 
1189   x_rfq_required_flag := po_wf_util_pkg.GetItemAttrText
1190           (itemtype => itemtype,
1191                                          itemkey  => itemkey,
1192                                          aname    => 'RFQ_REQUIRED_FLAG');
1193 
1194   x_on_rfq_flag := po_wf_util_pkg.GetItemAttrText
1195           (itemtype => itemtype,
1196                                          itemkey  => itemkey,
1197                                          aname    => 'ON_RFQ_FLAG');
1198 
1199   /* In 10sc this profile option will just warn the user not stop him
1200    * from going ahead. We could potentially send a notification to the
1201    * requestor (preparer) here and wait for a response that its ok
1202    * to go ahead, but for now we'll just fail the req line.
1203    */
1204 
1205   fnd_profile.get('PO_AUTOCREATE_WARN_RFQ_REQUIRED', x_warn_rfq_required);
1206 
1207   if ((x_warn_rfq_required = 'Y') AND
1208       (x_rfq_required_flag = 'Y') AND
1209       ((x_on_rfq_flag is NULL) OR (x_on_rfq_flag = 'N'))) then
1210     resultout := wf_engine.eng_completed || ':' ||  'Y';
1211 
1212     x_progress:= '10: rfq_required_check: result = Y';
1213     IF (g_po_wf_debug = 'Y') THEN
1214        po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
1215     END IF;
1216 
1217   else
1218     resultout := wf_engine.eng_completed || ':' ||  'N';
1219 
1220     x_progress:= '20: rfq_required_check: result = N';
1221     IF (g_po_wf_debug = 'Y') THEN
1222        po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
1223     END IF;
1224 
1225   end if;
1226 
1227 
1228 exception
1229   when others then
1230     wf_core.context('po_autocreate_doc','rfq_required_check',x_progress);
1231     raise;
1232 end rfq_required_check;
1233 
1234 
1235 /***************************************************************************
1236  *
1237  *  Procedure:  get_supp_info_for_acrt
1238  *
1239  *  Description:  Gets the suggested supplier/site info from the req line
1240  *      and makes sure they are valid.
1241  *
1242  **************************************************************************/
1243 procedure get_supp_info_for_acrt (itemtype   IN   VARCHAR2,
1244                                   itemkey    IN   VARCHAR2,
1245                                   actid      IN   NUMBER,
1246                                   funcmode   IN   VARCHAR2,
1247                                   resultout  OUT NOCOPY  VARCHAR2 ) is
1248 
1249 x_suggested_vendor_name  po_requisition_lines_all.suggested_vendor_name%type;
1250 x_suggested_vendor_site  varchar2(240);
1251 
1252 /* Bug 2577940 */
1253 x_suggested_vendor_id      number;
1254 x_suggested_vendor_site_id number;
1255 x_vendor                   po_requisition_lines_all.suggested_vendor_name%type;
1256 x_vendor_site              varchar2(240);
1257 /* Bug 2577940 */
1258 
1259 x_valid_vendor     varchar2(1);
1260 x_valid_vendor_site  varchar2(1);
1261 x_vendor_id    number;
1262 x_vendor_site_id   number;
1263 x_progress         varchar2(300);
1264 x_org_id                 number;
1265 
1266 begin
1267 
1268   /* Set the org context. */
1269 
1270   x_org_id := po_wf_util_pkg.GetItemAttrNumber (itemtype => itemtype,
1271                                            itemkey  => itemkey,
1272                                            aname    => 'ORG_ID');
1273 
1274   po_moac_utils_pvt.set_org_context(x_org_id); --<R12 MOAC>
1275 
1276   x_suggested_vendor_name := po_wf_util_pkg.GetItemAttrText
1277           (itemtype => itemtype,
1278                                          itemkey  => itemkey,
1279                                          aname    => 'SUGGESTED_VENDOR_NAME');
1280 
1281   x_suggested_vendor_site := po_wf_util_pkg.GetItemAttrText
1282           (itemtype => itemtype,
1283                                          itemkey  => itemkey,
1284                                          aname    => 'SUGGESTED_VENDOR_LOCATION');
1285   /* Bug 2577940 */
1286   x_suggested_vendor_id := po_wf_util_pkg.GetItemAttrNumber
1287                                      (itemtype => itemtype,
1288                                       itemkey  => itemkey,
1289                                       aname    => 'SUGGESTED_VENDOR_ID');
1290 
1291   x_suggested_vendor_site_id := po_wf_util_pkg.GetItemAttrNumber
1292                                            (itemtype => itemtype,
1293                                             itemkey  => itemkey,
1294                                             aname    => 'SUGGESTED_VENDOR_SITE_ID');
1295   /* Bug 2577940 */
1296 
1297   /* Here we should do some validation since the req form doesn't validate
1298    * the vendor or vendor site against an lov.
1299    * Actually the lov for the source doc on the req line does have the suggested
1300    * vendor as part of the where clause. So the source doc check would have failed
1301    * since it would have been null for an invalid supplier. But no harm double
1302    * checking here. We need to get the suggested vendor/vendor_site id's anyway
1303    * since the req line doesn't store them.
1304    */
1305 
1306   /* Check to see if the vendor on the req line is a valid one */
1307 
1308   x_vendor               :=  x_suggested_vendor_name;
1309   x_vendor_site          := x_suggested_vendor_site;
1310   x_vendor_id            := x_suggested_vendor_id;
1311   x_vendor_site_id       := x_suggested_vendor_site_id;
1312 
1313 
1314 if x_suggested_vendor_id is not null then
1315 
1316    /* Bug 2577940 if the id is provided, then it should take the precedence */
1317 
1318    begin
1319      select 'Y',
1320             vendor_name
1321      into x_valid_vendor,
1322           x_vendor
1323      from po_suppliers_val_v
1324      where vendor_id = x_suggested_vendor_id;
1325    exception
1326      when NO_DATA_FOUND then
1327        x_valid_vendor := 'N';
1328    end;
1329 
1330   /* If the vendor is not valid then we exit right here. */
1331 
1332    if (x_valid_vendor ='N') then
1333      resultout := wf_engine.eng_completed || ':' ||  'N';
1334 
1335      x_progress:= '10: get_supp_info_for_acrt: result = N ' ||
1336                    'because the supplier id is invalid';
1337      IF (g_po_wf_debug = 'Y') THEN
1338         po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
1339      END IF;
1340 
1341      purge_expense_lines(itemtype, itemkey);  -- <SERVICES FPJ>
1342 
1343      return;
1344     end if;
1345 
1346 else  /* Bug 2577940 */
1347 
1348    /* If the id is null the name will be used */
1349 
1350    begin
1351      select 'Y',
1352       vendor_id
1353        into x_valid_vendor,
1354       x_vendor_id
1355        from po_suppliers_val_v
1356       where vendor_name = x_suggested_vendor_name;
1357    exception
1358      when NO_DATA_FOUND then
1359        x_valid_vendor := 'N';
1360    end;
1361 
1362   /* If the vendor is not valid then we exit right here. */
1363 
1364    if (x_valid_vendor ='N') then
1365      resultout := wf_engine.eng_completed || ':' ||  'N';
1366 
1367      x_progress:= '20: get_supp_info_for_acrt: result = N ' ||
1368        'because the supplier is null or invalid';
1369      IF (g_po_wf_debug = 'Y') THEN
1370         po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
1371      END IF;
1372 
1373      purge_expense_lines(itemtype, itemkey);  -- <SERVICES FPJ>
1374 
1375      return;
1376     end if;
1377 
1378 end if; /* Bug 2577940 */
1379 
1380    /* If we get to this point then the vendor is valid, now
1381     * lets check for vendor site.
1382     */
1383 
1384 if x_suggested_vendor_site_id is not null then  /* Bug 2577940 */
1385 
1386   /* If the id is provided already then it should take the precedence */
1387 
1388  begin
1389 
1390      --<Shared Proc FPJ>
1391      --Changed the query to go against po_vendor_sites_all
1392      --instead of po_supplier_sites_val_v.
1393      select 'Y',
1394             vendor_site_code
1395        into x_valid_vendor_site,
1396             x_vendor_site
1397        from po_vendor_sites_all
1398       where vendor_id = x_vendor_id
1399         and vendor_site_id = x_suggested_vendor_site_id
1400         --<Shared Proc FPJ START>
1401         and purchasing_site_flag = 'Y'
1402         and NVL(rfq_only_site_flag, 'N') = 'N'
1403         and sysdate < NVL(inactive_date, sysdate + 1);
1404         --<Shared Proc FPJ END>
1405     exception
1406       when NO_DATA_FOUND then
1407        x_valid_vendor_site := 'N';
1408     end;
1409 
1410   /* If the vendor site id is not valid then we exit right here. */
1411 
1412    if (x_valid_vendor_site ='N') then
1413      resultout := wf_engine.eng_completed || ':' ||  'N';
1414 
1415      x_progress:= '30: get_supp_info_for_acrt: result = N ' ||
1416                    'becuase the supplier site id is invalid';
1417      IF (g_po_wf_debug = 'Y') THEN
1418         po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
1419      END IF;
1420 
1421      purge_expense_lines(itemtype, itemkey);  -- <SERVICES FPJ>
1422 
1423      return;
1424    end if;
1425 
1426 else  /* Bug 2577940 */
1427 
1428   begin
1429      select 'Y',
1430       vendor_site_id
1431        into x_valid_vendor_site,
1432       x_vendor_site_id
1433        from po_supplier_sites_val_v
1434       where vendor_id = x_vendor_id
1435         and vendor_site_code = x_suggested_vendor_site;
1436     exception
1437       when NO_DATA_FOUND then
1438        x_valid_vendor_site := 'N';
1439     end;
1440 
1441   /* If the vendor site is not valid then we exit right here. */
1442 
1443    if (x_valid_vendor_site ='N') then
1444      resultout := wf_engine.eng_completed || ':' ||  'N';
1445 
1446      x_progress:= '40: get_supp_info_for_acrt: result = N ' ||
1447        'becuase the supplier site  is null or invalid';
1448      IF (g_po_wf_debug = 'Y') THEN
1449         po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
1450      END IF;
1451 
1452      purge_expense_lines(itemtype, itemkey);  -- <SERVICES FPJ>
1453 
1454      return;
1455    end if;
1456 
1457 end if; /* Bug 2577940 */
1458 
1459    /* If we get here then both the vendor and vendor site are valid. */
1460 
1461    po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
1462                                 itemkey    => itemkey,
1463                                 aname      => 'SUGGESTED_VENDOR_ID',
1464                                 avalue     => x_vendor_id);
1465 
1466    po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
1467                                 itemkey    => itemkey,
1468                                 aname      => 'SUGGESTED_VENDOR_SITE_ID',
1469                                 avalue     => x_vendor_site_id);
1470 
1471    /* Bug 2577940  The correct names also should be set */
1472    po_wf_util_pkg.SetItemAttrText   (itemtype   => itemtype,
1473                                 itemkey    => itemkey,
1474                                 aname      => 'SUGGESTED_VENDOR_NAME',
1475                                 avalue      => x_vendor);
1476 
1477    po_wf_util_pkg.SetItemAttrText   (itemtype   => itemtype,
1478                                 itemkey    => itemkey,
1479                                 aname      => 'SUGGESTED_VENDOR_LOCATION',
1480                                 avalue      => x_vendor_site);
1481    /* Bug 2577940 */
1482 
1483    resultout := wf_engine.eng_completed || ':' ||  'Y';
1484 
1485    x_progress:= '50: get_supp_info_for_acrt: result = Y';
1486    IF (g_po_wf_debug = 'Y') THEN
1487       po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
1488    END IF;
1489 
1490 exception
1491   when others then
1492     wf_core.context('po_autocreate_doc','get_supp_info_for_acrt',x_progress);
1493     raise;
1494 end get_supp_info_for_acrt;
1495 
1496 
1497 /***************************************************************************
1498  *
1499  *  Procedure:  is_source_doc_info_ok
1500  *
1501  *  Description:  This checks to make sure we have source document
1502  *      reference information
1503  *
1504  **************************************************************************/
1505 procedure is_source_doc_info_ok (itemtype   IN   VARCHAR2,
1506                            itemkey    IN   VARCHAR2,
1507                                  actid      IN   NUMBER,
1508                                  funcmode   IN   VARCHAR2,
1509                                  resultout  OUT NOCOPY  VARCHAR2 ) is
1510 
1511 
1512 x_source_doc_type_code    varchar2(25);
1513 x_source_doc_po_header_id number;
1514 x_source_doc_line_num   number;
1515 x_progress                varchar2(300);
1516 
1517 --Bug 2745549
1518 l_source_doc_ok                 varchar2(1) := 'N';
1519 x_is_clm_flow VARCHAR2(1);
1520 
1521 begin
1522 
1523       /* When the source doc and source line was put onto the req line
1524        * it was all validated to make sure it was ok.
1525        * Ie. docs were within effectivity dates, not canelled or closed etc.
1526        * So not doing the check here again.
1527        * We just need to make sure the source_doc_type,  source_doc  and
1528        * source_line have been populated.
1529        */
1530 
1531        x_source_doc_type_code := po_wf_util_pkg.GetItemAttrText
1532             				(itemtype   => itemtype,
1533                                    	itemkey    => itemkey,
1534                                      	aname      => 'SOURCE_DOCUMENT_TYPE_CODE');
1535 
1536        x_source_doc_po_header_id := po_wf_util_pkg.GetItemAttrNumber
1537           				(itemtype   => itemtype,
1538                                         itemkey    => itemkey,
1539                                         aname      => 'SOURCE_DOCUMENT_ID');
1540 
1541        x_source_doc_line_num := po_wf_util_pkg.GetItemAttrNumber
1542           				(itemtype   => itemtype,
1543                                         itemkey    => itemkey,
1544                                         aname      => 'SOURCE_DOCUMENT_LINE_NUM');
1545 
1546        x_is_clm_flow :=  po_wf_util_pkg.GetItemAttrText (itemtype => itemtype,
1547                                              		 itemkey  => itemkey,
1548                                              		 aname    => 'IS_CLM_FLOW');
1549 
1550 
1551      if ((x_source_doc_type_code is NULL)     or
1552           (x_source_doc_po_header_id is NULL) or
1553     (x_source_doc_line_num is NULL))    then
1554         resultout := wf_engine.eng_completed || ':' ||  'N';
1555 
1556         x_progress:= '10: is_source_doc_info_ok: result = N';
1557         IF (g_po_wf_debug = 'Y') THEN
1558            po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
1559         END IF;
1560 
1561      --<Bug 2745549 mbhargav START>
1562      --Do not create PO if the referenced GA/Blanket is not valid
1563      --i.e. is Cancelled or Finally Closed
1564      elsif x_source_doc_type_code = 'BLANKET' THEN
1565 	  --<Bug 14314684 Create Doc WF project START>
1566           -- Check if clm requisition is referring a commerical agreement
1567           IF(nvl(x_is_clm_flow, 'N')= 'Y') THEN
1568               IF NOT (is_doc_clm_enabled(x_source_doc_po_header_id))     THEN
1569                  resultout := wf_engine.eng_completed || ':' ||  'N';
1570                  x_progress:= '20: is_source_doc_info_ok: result = N';
1571                  return;
1572               END IF;
1573           END IF;
1574 	--<Bug 14314684 Create Doc WF project END>
1575 
1576 	 is_ga_still_valid(x_source_doc_po_header_id, l_source_doc_ok);
1577 
1578          IF l_source_doc_ok = 'N' THEN
1579             resultout := wf_engine.eng_completed || ':' ||  'N';
1580 
1581             x_progress:= '30: is_source_doc_info_ok: result = N';
1582          ELSE
1583             resultout := wf_engine.eng_completed || ':' ||  'Y';
1584 
1585             x_progress:= '30: is_source_doc_info_ok: result = Y';
1586          END IF;
1587 
1588          IF (g_po_wf_debug = 'Y') THEN
1589              po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
1590          END IF;
1591 
1592      --<Bug 2745549 mbhargav END>
1593 
1594      else
1595         resultout := wf_engine.eng_completed || ':' ||  'Y';
1596 
1597         x_progress:= '40: is_source_doc_info_ok: result = Y';
1598         IF (g_po_wf_debug = 'Y') THEN
1599            po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
1600         END IF;
1601 
1602      end if;
1603 
1604 exception
1605   when others then
1606     wf_core.context('po_autocreate_doc','is_source_doc_info_ok',x_progress);
1607     raise;
1608 end is_source_doc_info_ok;
1609 
1610 /***************************************************************************
1611  *
1612  *  Procedure:  does_contract_exist
1613  *
1614  *  Description:  Check if use_contract_flag is true and if contract
1615  *      for the vendor and vendor site exists.  This procedure
1616  *      is added for self service purchasing.
1617  *                      this is also validating the expiration of the contract.
1618  *
1619  **************************************************************************/
1620 procedure does_contract_exist(itemtype   IN   VARCHAR2,
1621                           itemkey    IN   VARCHAR2,
1622                                 actid      IN   NUMBER,
1623                                 funcmode   IN   VARCHAR2,
1624                                 resultout  OUT NOCOPY  VARCHAR2 ) is
1625 
1626 x_contract_id       number;
1627 x_supplier_id       number;
1628 x_supplier_site_id  number;
1629 
1630 x_use_contract_flag varchar2(1);
1631 x_item_currency      PO_REQUISITION_LINES_ALL.currency_code%TYPE;
1632 
1633 x_progress                varchar2(300) := '000';
1634 
1635 -- <GC FPJ START>
1636 
1637 l_gc_flag            PO_HEADERS_ALL.global_agreement_flag%TYPE;
1638 l_currency           PO_HEADERS_ALL.currency_code%TYPE;
1639 l_base_currency      PO_HEADERS_ALL.currency_code%TYPE;
1640 l_rate               PO_HEADERS_ALL.rate%TYPE;
1641 l_rate_date          PO_HEADERS_ALL.rate_date%TYPE;
1642 l_rate_type          PO_HEADERS_ALL.rate_type%TYPE;
1643 
1644 -- bug4198095
1645 -- No need to match local contract anymore after R12
1646 l_current_org_id     PO_HEADERS_ALL.org_id%TYPE;
1647 
1648 -- SQL What: Find available contract that is valid. First it
1649 --           finds local contracts; if none exists then find global contracts.
1650 --           Also, latest contract takes priority
1651 -- SQL Why:  Need to see if any contract out there that can be attached to
1652 --           the req line, if the req line does not already have a source
1653 --           document
1654 
1655 CURSOR c_contract_currency IS
1656   SELECT POH.po_header_id,
1657          POH.global_agreement_flag,
1658          POH.currency_code
1659   FROM   po_headers_all POH
1660   WHERE  POH.vendor_id = x_supplier_id
1661   AND    POH.currency_code = nvl(x_item_currency, l_base_currency)
1662   AND    POH.type_lookup_code = 'CONTRACT'
1663   /* R12 GCPA
1664   + If Profile ALLOW_REFERENCING_CPA_UNDER_AMENDMENT is Y, then we can refer any Contract Which is approved Once
1665   + Else Contract should be in APPROVED state  */
1666   AND    ( (NVL(FND_PROFILE.VALUE('ALLOW_REFERENCING_CPA_UNDER_AMENDMENT'),'N') = 'Y'
1667            AND POH.Approved_Date Is Not Null
1668             )
1669          or (  POH.authorization_status = 'APPROVED' )
1670          )
1671   AND    NVL(POH.cancel_flag, 'N') = 'N'
1672   AND    NVL(POH.frozen_flag, 'N') = 'N'
1673   AND    NVL(POH.closed_code, 'OPEN') = 'OPEN'
1674   AND    TRUNC(SYSDATE) BETWEEN NVL(TRUNC(POH.start_date), SYSDATE - 1)
1675                         AND     NVL(TRUNC(POH.end_date),   SYSDATE + 1)
1676   AND    POH.global_agreement_flag = 'Y'
1677   /* R12 GCPA
1678   + Vendor Site validation needs to be skipped if Enable All Sites on Contracts is Set to Y.   */
1679   AND    EXISTS (SELECT 1
1680               FROM   po_ga_org_assignments PGOA
1681               WHERE  PGOA.po_header_id = POH.po_header_id
1682               AND    PGOA.vendor_site_id = decode(Nvl(poh.Enable_All_Sites,'N'),'Y',PGOA.vendor_site_id, x_supplier_site_id)
1683               AND    PGOA.organization_id = l_current_org_id
1684               AND    PGOA.enabled_flag = 'Y')
1685   ORDER BY POH.creation_date desc;
1686 
1687 CURSOR c_contract IS
1688   SELECT POH.po_header_id,
1689          POH.global_agreement_flag,
1690          POH.currency_code
1691   FROM   po_headers_all POH
1692   WHERE  POH.vendor_id = x_supplier_id
1693   AND    POH.type_lookup_code = 'CONTRACT'
1694   /* R12 GCPA
1695   + If Profile ALLOW_REFERENCING_CPA_UNDER_AMENDMENT is Y, then we can refer any Contract Which is approved Once
1696   + Else Contract should be in APPROVED state
1697   */
1698   AND    ( (NVL(FND_PROFILE.VALUE('ALLOW_REFERENCING_CPA_UNDER_AMENDMENT'),'N') = 'Y'
1699            AND POH.Approved_Date Is Not Null
1700             )
1701          or (  POH.authorization_status = 'APPROVED' )
1702          )
1703   AND    NVL(POH.cancel_flag, 'N') = 'N'
1704   AND    NVL(POH.frozen_flag, 'N') = 'N'
1705   AND    NVL(POH.closed_code, 'OPEN') = 'OPEN'
1706   AND    TRUNC(SYSDATE) BETWEEN NVL(TRUNC(POH.start_date), SYSDATE - 1)
1707                         AND     NVL(TRUNC(POH.end_date),   SYSDATE + 1)
1708   AND    POH.global_agreement_flag = 'Y'
1709   /* R12 GCPA
1710   + Vendor Site validation needs to be skipped if Enable All Sites on Contracts is Set to Y.    */
1711   AND    EXISTS (SELECT 1
1712               FROM   po_ga_org_assignments PGOA
1713               WHERE  PGOA.po_header_id = POH.po_header_id
1714               AND    PGOA.vendor_site_id = decode(Nvl(poh.Enable_All_Sites,'N'),'Y',PGOA.vendor_site_id, x_supplier_site_id)
1715               AND    PGOA.organization_id = l_current_org_id
1716               AND    PGOA.enabled_flag = 'Y')
1717   ORDER BY POH.creation_date desc;
1718 
1719 -- <GC FPJ END>
1720 
1721 begin
1722 
1723    x_supplier_id := po_wf_util_pkg.GetItemAttrNumber
1724           (itemtype   => itemtype,
1725                                        itemkey    => itemkey,
1726                                        aname      => 'SUGGESTED_VENDOR_ID');
1727 
1728    x_supplier_site_id := po_wf_util_pkg.GetItemAttrNumber
1729           (itemtype   => itemtype,
1730                                        itemkey    => itemkey,
1731                                        aname      => 'SUGGESTED_VENDOR_SITE_ID');
1732 
1733    x_use_contract_flag := po_wf_util_pkg.GetItemAttrText
1734           (itemtype   => itemtype,
1735                                        itemkey    => itemkey,
1736                                        aname      => 'USE_CONTRACT_FLAG');
1737 
1738    x_item_currency     := po_wf_util_pkg.GetItemAttrText
1739           (itemtype   => itemtype,
1740                                        itemkey    => itemkey,
1741                                        aname      => 'CURRENCY_CODE');
1742 
1743    if nvl(x_use_contract_flag, 'N') <> 'Y' then
1744 
1745         resultout := wf_engine.eng_completed || ':' ||  'N';
1746 
1747         return;
1748 
1749    end if;
1750 
1751    -- <GC FPJ START>
1752 
1753    x_progress := '001';
1754 
1755    -- The original query has been moved to the declare section as a cursor.
1756    l_base_currency := PO_CORE_S2.get_base_currency;
1757    l_current_org_id := PO_GA_PVT.get_current_org;
1758 
1759    OPEN c_contract_currency;
1760 
1761    -- Only take the first contract
1762 
1763 
1764    FETCH c_contract_currency INTO x_contract_id,
1765                          l_gc_flag,
1766                          l_currency;
1767 
1768    IF (c_contract_currency%NOTFOUND) THEN
1769        x_contract_id := NULL;
1770    END IF;
1771 
1772    CLOSE c_contract_currency;
1773 
1774    if (x_contract_id IS NULL) then
1775 
1776         OPEN c_contract;
1777 
1778   FETCH c_contract INTO x_contract_id,
1779                          l_gc_flag,
1780                          l_currency;
1781 
1782         IF (c_contract%NOTFOUND) THEN
1783             x_contract_id := NULL;
1784         END IF;
1785 
1786         CLOSE c_contract;
1787    end if;
1788 
1789    x_progress := '002';
1790 
1791    --<Bug 3079146>
1792    IF (x_contract_id IS NOT NULL AND l_gc_flag = 'Y') THEN
1793 
1794    /*    l_base_currency := PO_CORE_S2.get_base_currency;
1795 
1796        IF (l_base_currency <> l_currency) THEN 9728328 */
1797 
1798            -- Since a PO line referencing a gloabal contract must be in a PO
1799            -- having the same currency, we need to derive the rate information
1800            -- if the global contract is using a foreign currency. Contract
1801            -- reference is not allowed if currency rate is not defined.
1802 
1803            PO_GA_PVT.get_currency_info
1804            (  p_po_header_id  => x_contract_id,
1805                  x_currency_code => l_currency,
1806                  x_rate_type     => l_rate_type,
1807                  x_rate_date     => l_rate_date,
1808                  x_rate          => l_rate
1809            );
1810 
1811            IF (l_rate IS NULL) THEN
1812                x_contract_id := NULL;
1813            ELSE    -- rate is defined
1814 
1815                po_wf_util_pkg.SetItemAttrText (itemtype   => itemtype,
1816                                       itemkey    => itemkey,
1817                                       aname      => 'CURRENCY_CODE',
1818                                       avalue     => l_currency);
1819 
1820                po_wf_util_pkg.SetItemAttrText (itemtype   => itemtype,
1821                                       itemkey    => itemkey,
1822                                       aname      => 'RATE_TYPE',
1823                                       avalue     => l_rate_type);
1824 
1825                po_wf_util_pkg.SetItemAttrDate (itemtype   => itemtype,
1826                                       itemkey    => itemkey,
1827                                       aname      => 'RATE_DATE',
1828                                       avalue     => l_rate_date);
1829 
1830                po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
1831                                       itemkey    => itemkey,
1832                                       aname      => 'RATE',
1833                                       avalue     => l_rate);
1834            END IF;  -- l_rate is null
1835      /*  END IF;  -- l_currency <> l_base_currency */
1836 
1837        IF (x_contract_id IS NOT NULL) THEN
1838            po_wf_util_pkg.SetItemAttrText (itemtype   => itemtype,
1839                                   itemkey    => itemkey,
1840                                   aname      => 'SOURCE_DOC_GA_FLAG',
1841                                   avalue     => l_gc_flag);
1842        END IF;  -- x_contract_id is not null
1843    END IF; -- l_gc_flag = 'Y'
1844 
1845    -- <GC FPJ END>
1846 
1847    if x_contract_id is not null then
1848 
1849         po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
1850                                itemkey    => itemkey,
1851                                aname      => 'CONTRACT_ID',
1852                                avalue     => x_contract_id);
1853         po_wf_util_pkg.SetItemAttrText( itemtype   => itemtype,
1854                                itemkey    => itemkey,
1855                                aname      => 'SOURCE_DOCUMENT_TYPE_CODE',
1856                                avalue     => 'CONTRACT');
1857 
1858         -- <GC FPJ START>
1859         -- Since the ref is a contract and is stored in attr CONTRACT_ID,
1860         -- null out the reference in SOURCE_DOCUMENT_ID
1861 
1862         po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
1863                                itemkey    => itemkey,
1864                                aname      => 'SOURCE_DOCUMENT_ID',
1865                                avalue     => NULL);
1866 
1867         -- <GC FPJ END>
1868 
1869         resultout := wf_engine.eng_completed || ':' ||  'Y';
1870 
1871    else
1872 
1873         resultout := wf_engine.eng_completed || ':' ||  'N';
1874 
1875    end if;
1876 
1877 exception
1878 
1879   when others then
1880 
1881     -- <GC FPJ START>
1882     IF (c_contract%ISOPEN) THEN
1883         CLOSE c_contract;
1884     END IF;
1885     -- <GC FPJ END>
1886 
1887     wf_core.context('po_autocreate_doc','does_contract_exist',x_progress);
1888     raise;
1889 
1890 end does_contract_exist;
1891 
1892 /***************************************************************************
1893  *
1894  *  Procedure:  is_req_pcard_line
1895  *
1896  *  Description:  For Pcard req line, it doen't need source doc.
1897  *
1898  **************************************************************************/
1899 procedure is_req_pcard_line (itemtype   IN   VARCHAR2,
1900                              itemkey    IN   VARCHAR2,
1901                              actid      IN   NUMBER,
1902                              funcmode   IN   VARCHAR2,
1903                              resultout  OUT NOCOPY  VARCHAR2 ) is
1904 
1905 
1906 x_pcard_id  number;
1907 
1908 x_progress                varchar2(300);
1909 
1910 begin
1911 
1912      x_pcard_id := po_wf_util_pkg.GetItemAttrNumber
1913           (itemtype   => itemtype,
1914                                itemkey    => itemkey,
1915                                  aname      => 'PCARD_ID');
1916 
1917 
1918      if (x_pcard_id is NULL)    then
1919         resultout := wf_engine.eng_completed || ':' ||  'N';
1920 
1921         x_progress:= '10: is_req_pcard_line: result = N';
1922         IF (g_po_wf_debug = 'Y') THEN
1923            po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
1924         END IF;
1925 
1926         purge_expense_lines(itemtype, itemkey);  -- <SERVICES FPJ>
1927 
1928      else
1929         resultout := wf_engine.eng_completed || ':' ||  'Y';
1930 
1931         x_progress:= '20: is_req_pcard_line: result = Y';
1932         IF (g_po_wf_debug = 'Y') THEN
1933            po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
1934         END IF;
1935 
1936      end if;
1937 
1938 exception
1939   when others then
1940     wf_core.context('po_autocreate_doc','is_req_pcard_line',x_progress);
1941     raise;
1942 end is_req_pcard_line;
1943 
1944 /***************************************************************************
1945  *
1946  *  Procedure:  get_buyer_from_req_line
1947  *
1948  *  Description:  Gets the suggested buyer on the req line
1949  *
1950  *
1951  **************************************************************************/
1952 procedure get_buyer_from_req_line (itemtype   IN   VARCHAR2,
1953                                    itemkey    IN   VARCHAR2,
1954                                    actid      IN   NUMBER,
1955                                    funcmode   IN   VARCHAR2,
1956                                    resultout  OUT NOCOPY  VARCHAR2 ) is
1957 
1958 x_suggested_buyer_id  number;
1959 x_progress              varchar2(300);
1960 
1961 -- bug2821542
1962 l_validate_result       VARCHAR2(1) := FND_API.G_TRUE;
1963 
1964 begin
1965 
1966   x_suggested_buyer_id := po_wf_util_pkg.GetItemAttrNumber
1967           (itemtype => itemtype,
1968                                          itemkey  => itemkey,
1969                                          aname    => 'SUGGESTED_BUYER_ID');
1970 
1971 -- bug2821542
1972   IF (x_suggested_buyer_id IS NOT NULL) THEN
1973     validate_buyer(p_agent_id => x_suggested_buyer_id,
1974                    x_result   => l_validate_result);
1975   END IF;
1976 
1977   if (x_suggested_buyer_id  is NULL OR
1978       l_validate_result = FND_API.G_FALSE) then  -- bug2821542
1979 
1980       resultout := wf_engine.eng_completed || ':' ||  'ACTION_FAILED';
1981 
1982       x_progress:= '10: get_buyer_from_req_line: result = ACTION_FAILED';
1983       IF (g_po_wf_debug = 'Y') THEN
1984          po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
1985       END IF;
1986 
1987   else
1988       resultout := wf_engine.eng_completed || ':' ||  'ACTION_SUCCEEDED';
1989 
1990       x_progress:= '20: get_buyer_from_req_line: result = ACTION_SUCCEEDED';
1991       IF (g_po_wf_debug = 'Y') THEN
1992          po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
1993       END IF;
1994 
1995  end if;
1996 
1997 exception
1998   when others then
1999     wf_core.context('po_autocreate_doc','get_buyer_from_req_line',x_progress);
2000     raise;
2001 end get_buyer_from_req_line;
2002 
2003 
2004 /***************************************************************************
2005  *
2006  *  Procedure:  get_buyer_from_item
2007  *
2008  *  Description:  Gets the buyer from the item master based on the
2009  *      item on the requisition line.
2010  *
2011  **************************************************************************/
2012 procedure get_buyer_from_item (itemtype   IN   VARCHAR2,
2013                                itemkey    IN   VARCHAR2,
2014                                actid      IN   NUMBER,
2015                                funcmode   IN   VARCHAR2,
2016                                resultout  OUT NOCOPY  VARCHAR2 ) is
2017 
2018 x_item_id number;
2019 x_buyer_id  number;
2020 x_inv_org_id  number;
2021 x_org_id  number;
2022 x_progress      varchar2(300);
2023 
2024 -- bug2821542
2025 l_validate_result       VARCHAR2(1) := FND_API.G_TRUE;
2026 l_purchasing_org_id     PO_HEADERS_ALL.org_id%TYPE; --<Shared Proc FPJ>
2027 
2028 begin
2029 
2030   x_org_id := po_wf_util_pkg.GetItemAttrNumber (itemtype => itemtype,
2031                                            itemkey  => itemkey,
2032                                            aname    => 'ORG_ID');
2033 
2034 --<Shared Proc FPJ START>
2035   l_purchasing_org_id := po_wf_util_pkg.GetItemAttrNumber
2036           (itemtype => itemtype,
2037                                          itemkey  => itemkey,
2038                                          aname    => 'PURCHASING_ORG_ID');
2039 --<Shared Proc FPJ END>
2040   x_item_id := po_wf_util_pkg.GetItemAttrNumber
2041           (itemtype => itemtype,
2042                                          itemkey  => itemkey,
2043                                          aname    => 'ITEM_ID');
2044 
2045   --<Shared Proc FPJ>
2046   --Get default inventory org of purchasing org from the fsp_all table.
2047 
2048   select inventory_organization_id
2049     into x_inv_org_id
2050     from financials_system_params_all
2051      where org_id = l_purchasing_org_id;
2052 
2053   /* Now get the buyer from the item master. There may/may not be one
2054    * assigned to the item. MTL_SYSTEM_ITEMS is a table that isn't striped
2055    * org.
2056    */
2057 
2058    begin
2059       select buyer_id
2060         into x_buyer_id
2061         from mtl_system_items
2062         where inventory_item_id = x_item_id
2063               and organization_id = x_inv_org_id;
2064    exception
2065      /* For one time items this will not return anything */
2066      when NO_DATA_FOUND then
2067         x_buyer_id := null;
2068    end;
2069 
2070 -- bug2821542
2071   IF (x_buyer_id IS NOT NULL) THEN
2072     validate_buyer(p_agent_id => x_buyer_id,
2073                    x_result   => l_validate_result);
2074   END IF;
2075 
2076   if (x_buyer_id  is NULL OR
2077       l_validate_result = FND_API.G_FALSE) then -- bug2821542
2078 
2079       resultout := wf_engine.eng_completed || ':' ||  'ACTION_FAILED';
2080 
2081       x_progress:='10: get_buyer_from_item: result = ACTION_FAILED';
2082       IF (g_po_wf_debug = 'Y') THEN
2083          po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
2084       END IF;
2085 
2086   else
2087       po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
2088                                itemkey    => itemkey,
2089                                aname      => 'SUGGESTED_BUYER_ID',
2090                                avalue     => x_buyer_id);
2091 
2092       resultout := wf_engine.eng_completed || ':' ||  'ACTION_SUCCEEDED';
2093 
2094       x_progress:='20: get_buyer_from_item: result = ACTION_SUCCEEDED';
2095       IF (g_po_wf_debug = 'Y') THEN
2096          po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
2097       END IF;
2098 
2099  end if;
2100 
2101 exception
2102   when others then
2103     wf_core.context('po_autocreate_doc','get_buyer_from_item',x_progress);
2104     raise;
2105 end get_buyer_from_item;
2106 
2107 
2108 /***************************************************************************
2109  *
2110  *  Procedure:  get_buyer_from_category
2111  *
2112  *  Description:  Gets buyer from the category on the req line
2113  *
2114  *
2115  **************************************************************************/
2116 procedure get_buyer_from_category (itemtype   IN   VARCHAR2,
2117                                    itemkey    IN   VARCHAR2,
2118                                    actid      IN   NUMBER,
2119                                    funcmode   IN   VARCHAR2,
2120                                    resultout  OUT NOCOPY  VARCHAR2 ) is
2121 
2122 x_category_id number;
2123 x_agent_id  number := null;
2124 x_progress      varchar2(300);
2125 
2126 -- bug2821542
2127 l_validate_result       VARCHAR2(1) := FND_API.G_TRUE;
2128 
2129 begin
2130 
2131   x_category_id := po_wf_util_pkg.GetItemAttrNumber
2132           (itemtype => itemtype,
2133                                          itemkey  => itemkey,
2134                                          aname    => 'CATEGORY_ID');
2135 
2136   /* Bug - 1895237
2137 
2138   /* Get the buyer from the buyers table.
2139      If the HR profile Cross Business Groups is set to 'Y' , get it from
2140      po_agents as po_agents is not striped. But if that profile is set to No,
2141      then get only buyers for that business group.   */
2142 
2143 
2144  If (nvl(hr_general.get_xbg_profile, 'N') = 'Y') then
2145 
2146     begin
2147       select agent_id
2148       into x_agent_id
2149       from po_agents
2150       where category_id = x_category_id
2151        and trunc(sysdate) between start_date_active
2152                           and nvl(end_date_active, sysdate+1);
2153 
2154     exception
2155 
2156       /* It's possible that the same category is assigned to multiple buyers*/
2157 
2158       when TOO_MANY_ROWS then
2159 
2160         x_agent_id := NULL;
2161 
2162         x_progress := '10: get_buyer_from_category: More than 1 buyer for this category ';
2163         IF (g_po_wf_debug = 'Y') THEN
2164            po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
2165         END IF;
2166 
2167       when NO_DATA_FOUND then
2168 
2169         x_agent_id := NULL;
2170 
2171         x_progress := '20:get_buyer_from_category: No buyer assinged to category';
2172         IF (g_po_wf_debug = 'Y') THEN
2173            po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
2174         END IF;
2175 
2176      end;
2177 
2178   else
2179 
2180      begin
2181 
2182    /* Bug - 1915033 - Added the effectivity dates condition for
2183       per_people_f also and also introduced TRUNC function   */
2184 
2185       select agent_id
2186       into x_agent_id
2187       from po_agents poa,
2188 	  per_all_people_f ppf, --Bug 16249921. Changed per_people_f to per_all_people_f
2189 	  financials_system_parameters fsp
2190       where poa.agent_id = ppf.person_id
2191         and ppf.business_group_id = fsp.business_group_id
2192         and trunc(sysdate) between ppf.effective_start_date
2193                            and nvl(ppf.effective_end_date, sysdate+1)
2194         and poa.category_id = x_category_id
2195         and trunc(sysdate) between poa.start_date_active
2196                            and nvl(poa.end_date_active, sysdate+1);
2197 
2198      exception
2199 
2200       /* It's possible that the same category is assigned to multiple buyers in the
2201        same business group  */
2202 
2203        when TOO_MANY_ROWS then
2204 
2205          x_agent_id := NULL;
2206 
2207          x_progress := '10: get_buyer_from_category: More than 1 buyer for this category ';
2208          IF (g_po_wf_debug = 'Y') THEN
2209             po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
2210          END IF;
2211 
2212        when NO_DATA_FOUND then
2213 
2214          x_agent_id := NULL;
2215 
2216          x_progress := '20:get_buyer_from_category: No buyer assinged to category';
2217          IF (g_po_wf_debug = 'Y') THEN
2218             po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
2219          END IF;
2220 
2221       end;
2222 
2223   end if;
2224 
2225 -- bug2821542
2226   IF (x_agent_id IS NOT NULL) THEN
2227     validate_buyer(p_agent_id => x_agent_id,
2228                    x_result   => l_validate_result);
2229   END IF;
2230 
2231   if (x_agent_id is NULL OR
2232       l_validate_result = FND_API.G_FALSE) then  -- bug2821542
2233 
2234       resultout := wf_engine.eng_completed || ':' ||  'ACTION_FAILED';
2235 
2236       x_progress := '30:get_buyer_from_category: result = ACTION_FAILED';
2237       IF (g_po_wf_debug = 'Y') THEN
2238          po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
2239       END IF;
2240 
2241 
2242   else
2243       po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
2244                                    itemkey    => itemkey,
2245                                    aname      => 'SUGGESTED_BUYER_ID',
2246                                    avalue     => x_agent_id);
2247 
2248       resultout := wf_engine.eng_completed || ':' ||  'ACTION_SUCCEEDED';
2249 
2250       x_progress := '40:get_buyer_from_category: result = ACTION_SUCCEEDED';
2251       IF (g_po_wf_debug = 'Y') THEN
2252          po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
2253       END IF;
2254 
2255   end if;
2256 
2257 exception
2258 
2259   when others then
2260     wf_core.context('po_autocreate_doc','get_buyer_from_category',x_progress);
2261     raise;
2262 
2263 end get_buyer_from_category;
2264 
2265 /***************************************************************************
2266  *
2267  *  Procedure:  get_buyer_from_source_doc
2268  *
2269  *  Description:  Gets buyer from the source doc
2270  *
2271  *
2272  **************************************************************************/
2273 procedure get_buyer_from_source_doc (itemtype   IN   VARCHAR2,
2274                                    itemkey    IN   VARCHAR2,
2275                                    actid      IN   NUMBER,
2276                                    funcmode   IN   VARCHAR2,
2277                                    resultout  OUT NOCOPY  VARCHAR2 ) is
2278 
2279 x_source_doc_po_header_id number;
2280 x_source_doc_type_code    varchar2(25);
2281 x_agent_id  number := null;
2282 x_progress      varchar2(300);
2283 
2284 -- bug2821542
2285 l_validate_result       VARCHAR2(1) := FND_API.G_TRUE;
2286 
2287 begin
2288 
2289   x_source_doc_type_code := po_wf_util_pkg.GetItemAttrText
2290             (itemtype   => itemtype,
2291                                    itemkey    => itemkey,
2292                                      aname      => 'SOURCE_DOCUMENT_TYPE_CODE');
2293 
2294   x_source_doc_po_header_id := po_wf_util_pkg.GetItemAttrNumber
2295           (itemtype => itemtype,
2296                                          itemkey  => itemkey,
2297                                          aname    => 'SOURCE_DOCUMENT_ID');
2298   if( x_source_doc_type_code = 'BLANKET' or
2299       x_source_doc_type_code = 'QUOTATION') then
2300     /* Get the buyer from the PO headers table.
2301      */
2302     begin
2303 /*Bug 928568-removed the _all reference and used the striped table
2304   table instead */
2305 
2306       --<Shared Proc FPJ>
2307       -- Modified the query to select from po_headers_all instead of po_headers.
2308       select agent_id
2309       into   x_agent_id
2310       from   po_headers_all
2311       where  po_header_id  = x_source_doc_po_header_id;
2312     exception
2313       when NO_DATA_FOUND then
2314         x_agent_id := NULL;
2315         x_progress := '10:get_buyer_from_source_doc: Source Doc id is wrong';
2316         IF (g_po_wf_debug = 'Y') THEN
2317            po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
2318         END IF;
2319     end;
2320   else
2321     /* Right now, it's for pcard one-time item po, need to figure out
2322      * how to get buyer*/
2323     null;
2324   end if;
2325 
2326 -- bug2821542
2327   IF (x_agent_id IS NOT NULL) THEN
2328     validate_buyer(p_agent_id => x_agent_id,
2329                    x_result   => l_validate_result);
2330   END IF;
2331 
2332   if (x_agent_id is NULL OR
2333       l_validate_result = FND_API.G_FALSE) then -- bug2821542
2334 
2335       resultout := wf_engine.eng_completed || ':' ||  'ACTION_FAILED';
2336 
2337       x_progress := '30:get_buyer_from_source_doc: result = ACTION_FAILED';
2338       IF (g_po_wf_debug = 'Y') THEN
2339          po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
2340       END IF;
2341 
2342 
2343   else
2344       po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
2345                                    itemkey    => itemkey,
2346                                    aname      => 'SUGGESTED_BUYER_ID',
2347                                    avalue     => x_agent_id);
2348 
2349       resultout := wf_engine.eng_completed || ':' ||  'ACTION_SUCCEEDED';
2350 
2351       x_progress := '40:get_buyer_from_source_doc: result = ACTION_SUCCEEDED';
2352       IF (g_po_wf_debug = 'Y') THEN
2353          po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
2354       END IF;
2355 
2356   end if;
2357 
2358 exception
2359   when others then
2360     wf_core.context('po_autocreate_doc','get_buyer_from_source_doc',x_progress);
2361     raise;
2362 end get_buyer_from_source_doc;
2363 
2364 /***************************************************************************
2365  *
2366  *  Procedure:  get_buyer_from_contract
2367  *
2368  *  Description:  Gets buyer from the contract
2369  *
2370  *
2371  **************************************************************************/
2372 procedure get_buyer_from_contract (itemtype   IN   VARCHAR2,
2373                                    itemkey    IN   VARCHAR2,
2374                                    actid      IN   NUMBER,
2375                                    funcmode   IN   VARCHAR2,
2376                                    resultout  OUT NOCOPY  VARCHAR2 ) is
2377 
2378 x_contract_id number;
2379 x_agent_id    number;
2380 x_progress    VARCHAR2(300);
2381 
2382 -- bug2821542
2383 l_validate_result       VARCHAR2(1) := FND_API.G_TRUE;
2384 
2385 begin
2386 
2387   x_contract_id := po_wf_util_pkg.GetItemAttrNumber
2388           (itemtype => itemtype,
2389                                          itemkey  => itemkey,
2390                                          aname    => 'CONTRACT_ID');
2391 
2392   x_progress := '001';
2393 
2394   begin
2395 
2396     --<Shared Proc FPJ>
2397     --Modified the query to select from po_headers_all instead of po_headers
2398     select agent_id
2399       into x_agent_id
2400       from po_headers_all
2401      where po_header_id  = x_contract_id;
2402 
2403   exception
2404 
2405     when others then
2406       x_agent_id := null;
2407 
2408   end;
2409 
2410   x_progress := '002';
2411 
2412 -- bug2821542
2413   IF (x_agent_id IS NOT NULL) THEN
2414     validate_buyer(p_agent_id => x_agent_id,
2415                    x_result   => l_validate_result);
2416   END IF;
2417 
2418   if (x_agent_id is NULL OR
2419       l_validate_result = FND_API.G_FALSE) then -- bug2821542
2420 
2421       resultout := wf_engine.eng_completed || ':' ||  'ACTION_FAILED';
2422 
2423       x_progress := '30:get_buyer_from_source_doc: result = ACTION_FAILED';
2424       IF (g_po_wf_debug = 'Y') THEN
2425          po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
2426       END IF;
2427 
2431       po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
2428       purge_expense_lines(itemtype, itemkey);  -- <SERVICES FPJ>
2429 
2430   else
2432                                    itemkey    => itemkey,
2433                                    aname      => 'SUGGESTED_BUYER_ID',
2434                                    avalue     => x_agent_id);
2435 
2436       resultout := wf_engine.eng_completed || ':' ||  'ACTION_SUCCEEDED';
2437 
2438       x_progress := '40:get_buyer_from_contract: result = ACTION_SUCCEEDED';
2439       IF (g_po_wf_debug = 'Y') THEN
2440          po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
2441       END IF;
2442 
2443   end if;
2444 
2445 exception
2446 
2447   when others then
2448     wf_core.context('po_autocreate_doc','get_buyer_from_contract',x_progress);
2449     raise;
2450 
2451 end get_buyer_from_contract;
2452 
2453 /***************************************************************************
2454  *
2455  *  Procedure:  get_source_doc_type
2456  *
2457  *  Description:  Gets the source document type  from the req line.
2458  *
2459  *
2460  **************************************************************************/
2461 procedure get_source_doc_type (itemtype   IN   VARCHAR2,
2462                                itemkey    IN   VARCHAR2,
2463                                actid      IN   NUMBER,
2464                                funcmode   IN   VARCHAR2,
2465                                resultout  OUT NOCOPY  VARCHAR2 ) is
2466 
2467 x_source_doc_type_code  varchar2(25);
2468 x_progress        varchar2(300);
2469 x_ga_flag               varchar2(1);
2470 begin
2471 
2472   x_source_doc_type_code := po_wf_util_pkg.GetItemAttrText
2473           (itemtype => itemtype,
2474                                          itemkey  => itemkey,
2475                                          aname    => 'SOURCE_DOCUMENT_TYPE_CODE');
2476 
2477   /* The source doc must be a blanket or quote so return the
2478    * appropriate value.
2479    */
2480 
2481   /* FPI GA Start */
2482   /* Get the GA Flag */
2483    x_ga_flag := po_wf_util_pkg.GetItemAttrText
2484            (itemtype => itemtype,
2485                                          itemkey  => itemkey,
2486                                          aname    => 'SOURCE_DOC_GA_FLAG');
2487 
2488   /* FPI GA End */
2489 /* draising 2692119 */
2490    if (x_source_doc_type_code is null) then
2491      resultout := wf_engine.eng_completed || ':' || 'NONE';
2492 
2493     x_progress := '10: get_source_doc_type: result = NONE';
2494      IF (g_po_wf_debug = 'Y') THEN
2495         po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
2496      END IF;
2497 
2498   elsif (x_source_doc_type_code = 'BLANKET') then
2499     if nvl(x_ga_flag,'N') = 'Y' then                               -- FPI GA
2500       x_progress := '10: get_source_doc_type: result = GLOBAL_PA';
2501       resultout := wf_engine.eng_completed || ':' || 'GLOBAL_PA';  -- FPI GA
2502     else
2503       x_progress := '10: get_source_doc_type: result = BLANKET_PO';
2504       resultout := wf_engine.eng_completed || ':' || 'BLANKET_PO';
2505     end if;
2506 
2507      x_progress := '10: get_source_doc_type: result = BLANKET_PO';
2508      IF (g_po_wf_debug = 'Y') THEN
2509         po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
2510      END IF;
2511 
2512   elsif (x_source_doc_type_code = 'CONTRACT') then
2513     resultout := wf_engine.eng_completed || ':' || 'CONTRACT_PO';
2514 
2515     x_progress := '10: get_source_doc_type: result = CONTRACT_PO';
2516      IF (g_po_wf_debug = 'Y') THEN
2517         po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
2518      END IF;
2519 
2520   else
2521     /* Must be QUOTATION */
2522     resultout := wf_engine.eng_completed || ':' || 'QUOTATION';
2523 
2524     x_progress := '10: get_source_doc_type: result = QUOTATION';
2525     IF (g_po_wf_debug = 'Y') THEN
2526        po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
2527     END IF;
2528 
2529   end if;
2530 
2531 exception
2532   when others then
2533     wf_core.context('po_autocreate_doc','get_source_doc_type',x_progress);
2534     raise;
2535 end get_source_doc_type;
2536 
2537 /***************************************************************************
2538  *
2539  *  Procedure:  one_time_item_check
2540  *
2541  *  Description:  Checks if this is a one-time req line (ie. no
2542  *      item num)
2543  *
2544  **************************************************************************/
2545 procedure one_time_item_check (itemtype   IN   VARCHAR2,
2546                                itemkey    IN   VARCHAR2,
2547                                actid      IN   NUMBER,
2548                                funcmode   IN   VARCHAR2,
2549                                resultout  OUT NOCOPY  VARCHAR2 ) is
2550 
2551 x_item_id   number;
2552 x_progress        varchar2(300);
2553 
2554 begin
2555 
2556   x_item_id := po_wf_util_pkg.GetItemAttrNumber (itemtype => itemtype,
2557                                             itemkey  => itemkey,
2558                                             aname    => 'ITEM_ID');
2559 
2560   /* If item_id is null then this is a one-time item. */
2561 
2562   if (x_item_id is NULL) then
2563     resultout := wf_engine.eng_completed || ':' || 'Y';
2564 
2565     x_progress:= '10: one_time_item_check: result = Y';
2566     IF (g_po_wf_debug = 'Y') THEN
2567        po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
2568     END IF;
2569 
2570   else
2571     resultout := wf_engine.eng_completed || ':' || 'N';
2572 
2576     END IF;
2573     x_progress:= '10: one_time_item_check: result = N';
2574     IF (g_po_wf_debug = 'Y') THEN
2575        po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
2577 
2578   end if;
2579 
2580 exception
2581   when others then
2582     wf_core.context('po_autocreate_doc','one_time_item_check',x_progress);
2583     raise;
2584 end one_time_item_check;
2585 
2586 
2587 
2588 
2589 
2590 /***************************************************************************
2591  *
2592  *  Procedure:  get_rel_gen_method
2593  *
2594  *  Description:  Gets the release generation method from the asl
2595  *      associated with the supplier/site/item combination
2596  *            on the req line
2597  *
2598  **************************************************************************/
2599 procedure get_rel_gen_method (itemtype   IN   VARCHAR2,
2600                                    itemkey    IN   VARCHAR2,
2601                                    actid      IN   NUMBER,
2602                                    funcmode   IN   VARCHAR2,
2603                                    resultout  OUT NOCOPY  VARCHAR2 ) is
2604 
2605 x_org_id        number;
2606 x_inv_org_id                    number;
2607 x_item_id       number;
2608 x_suggested_vendor_id     number;
2609 x_suggested_vendor_site_id  number;
2610 x_organization_id               number;
2611 x_category_id NUMBER;  -- bug No 5943024
2612 x_rel_gen_method  varchar2(25);
2613 x_progress        varchar2(300);
2614 
2615 begin
2616 
2617    /* Set the org context. Backend create_po process assumes it is in
2618     * an org.
2619     */
2620 
2621     x_org_id := po_wf_util_pkg.GetItemAttrNumber
2622                                         (itemtype => itemtype,
2623                                          itemkey  => itemkey,
2624                                          aname    => 'ORG_ID');
2625 
2626      po_moac_utils_pvt.set_org_context(x_org_id); --<R12 MOAC>
2627 
2628     /* Retrieve required info from item attributes */
2629 
2630     x_item_id := po_wf_util_pkg.GetItemAttrNumber
2631           (itemtype => itemtype,
2632                                          itemkey  => itemkey,
2633                                          aname    => 'ITEM_ID');
2634 
2635     x_suggested_vendor_id := po_wf_util_pkg.GetItemAttrNumber
2636           (itemtype => itemtype,
2637                                          itemkey  => itemkey,
2638                                          aname    => 'SUGGESTED_VENDOR_ID');
2639 
2640     x_suggested_vendor_site_id := po_wf_util_pkg.GetItemAttrNumber
2641           (itemtype => itemtype,
2642                                          itemkey  => itemkey,
2643                                          aname    => 'SUGGESTED_VENDOR_SITE_ID');
2644 
2645     x_organization_id := po_wf_util_pkg.GetItemAttrNumber
2646                                 (itemtype => itemtype,
2647                                  itemkey  => itemkey,
2648                                  aname    => 'ORGANIZATION_ID');
2649    /* bug no:5943024*/
2650     x_category_id := po_wf_util_pkg.GetItemAttrNumber
2651                                         (itemtype => itemtype,
2652                                          itemkey  => itemkey,
2653                                          aname    => 'CATEGORY_ID');
2654 /* end of Bug No 5943024*/
2655     /* Get the release geneartion method for the item/vendor/vendor_site
2656      * combination from the asl attributes table.
2657      * We have a subquery in the following query. The purpose of the subquery
2658      * is to get the local asl if both a local and global asl exist for the
2659      * item/vendor/vendor_site combination. Ie. it is possible to have both
2660      * a local and global asl for the same item/vendor/vendor_site combination.
2661      * For local asl's the using_organization_id is set to the inv_org_id.
2662      * For global asl's it is set to -1. Thus the max on the subquery will
2663      * return the local over the global if both exist.
2664      */
2665 
2666     /* The release gen method can be:
2667      * Automatic Release/Review (CREATE)            (not allowed if encumbrance on)
2668      * Automatic Release  (CREATE_AND_APPROVE)
2669      * Release Using Autocrate  (MANUAL)
2670      *
2671      * For the first two cases we let the Auotmatic Release Generation conc. program
2672      * pick the req lines up. We will pick up the latter case.
2673      */
2674 
2675     BEGIN
2676     /*  Bug No :5943024
2677          This query has been modified in such a way that it could select a release generation method even for the asl at commodity level  Earlier the query selects release generation method only for item.
2678     */
2679 
2680 
2681    select release_generation_method
2682           into x_rel_gen_method
2683           from (
2684   select paa.release_generation_method
2685           from po_asl_attributes_val_v paa
2686           WHERE Paa.Item_Id = x_item_id
2687          AND Paa.Vendor_Id = x_suggested_vendor_id
2688          AND (Paa.Vendor_Site_Id IS NULL
2689                OR ( x_suggested_vendor_site_id  = Paa.Vendor_Site_Id
2690                    AND NOT EXISTS (SELECT 'select supplier line with null supplier site'
2691                                    FROM   po_Asl_Attributes_val_v Paa3
2692                                    WHERE  Paa.Item_Id = Paa3.Item_Id
2693                                           AND Nvl(Paa.Category_Id,- 1) = Nvl(Paa3.Category_Id,- 1)
2694                                           AND Paa.Vendor_Id = Paa3.Vendor_Id
2695                                           AND Paa3.Vendor_Site_Id IS NULL
2696                                           AND Paa3.UsIng_Organization_Id IN (- 1,
2700                                           WHERE  Paa.Item_Id = Paa2.Item_Id
2697                                                                              x_organization_id))))
2698          AND Paa.UsIng_Organization_Id = (SELECT MAX(Paa2.UsIng_Organization_Id)
2699                                           FROM   po_Asl_Attributes_val_v Paa2
2701                                                  AND Nvl(Paa.Category_Id,- 1) = Nvl(Paa2.Category_Id,- 1)
2702                                                  AND Paa.Vendor_Id = Paa2.Vendor_Id
2703                                                  AND Nvl(Paa.Vendor_Site_Id,- 1) = Nvl(Paa2.Vendor_Site_Id,- 1)
2704                                                  AND Paa2.UsIng_Organization_Id IN (- 1,x_organization_id))
2705   union all
2706   select paa.release_generation_method
2707           from po_asl_attributes_val_v paa
2708           WHERE (Paa.Item_Id IS NULL
2709                    AND x_category_id = Paa.Category_Id
2710                    AND NOT EXISTS (SELECT 'commodity level ASL should be used only if there is no item level ASL'
2711                                    FROM   po_Asl_Attributes_val_v Paa4
2712                                    WHERE  Paa4.Item_Id = x_item_id
2713                                           AND Paa4.Vendor_Id = Paa.Vendor_Id
2714                                           AND Nvl(Paa4.Vendor_Site_Id,- 1) = Nvl(Paa.Vendor_Site_Id,- 1)
2715                                           AND Paa4.UsIng_Organization_Id IN (- 1,x_organization_id)))
2716          AND Paa.Vendor_Id = x_suggested_vendor_id
2717          AND (Paa.Vendor_Site_Id IS NULL
2718                OR ( x_suggested_vendor_site_id  = Paa.Vendor_Site_Id
2719                    AND NOT EXISTS (SELECT 'select supplier line with null supplier site'
2720                                    FROM   po_Asl_Attributes_val_v Paa3
2721                                    WHERE  Paa3.Item_Id IS NULL
2722                                           AND Paa.Category_Id = Paa3.Category_Id
2723                                           AND Paa.Vendor_Id = Paa3.Vendor_Id
2724                                           AND Paa3.Vendor_Site_Id IS NULL
2725                                           AND Paa3.UsIng_Organization_Id IN (- 1,
2726                                                                              x_organization_id))))
2727          AND Paa.UsIng_Organization_Id = (SELECT MAX(Paa2.UsIng_Organization_Id)
2728                                           FROM   po_Asl_Attributes_val_v Paa2
2729                                           WHERE  Paa2.Item_Id IS NULL
2730                                                  AND Paa.Category_Id = Paa2.Category_Id
2731                                                  AND Paa.Vendor_Id = Paa2.Vendor_Id
2732                                                  AND Nvl(Paa.Vendor_Site_Id,- 1) = Nvl(Paa2.Vendor_Site_Id,- 1)
2733                                                  AND Paa2.UsIng_Organization_Id IN (- 1,x_organization_id))
2734   )  WHERE ROWNUM =1;
2735 /* end of Bug No 5943024 */
2736 
2737     exception
2738       when NO_DATA_FOUND then
2739          x_rel_gen_method :=null;
2740     end;
2741 
2742     /* Set item attribute so it can be used later */
2743 
2744     po_wf_util_pkg.SetItemAttrText (itemtype => itemtype,
2745                                itemkey  => itemkey,
2746                                aname    => 'REL_GEN_METHOD',
2747              avalue   => x_rel_gen_method);
2748 
2749     if (x_rel_gen_method is NULL) then
2750       resultout := wf_engine.eng_completed || ':' || 'NO_METHOD_FOUND';
2751 
2752       x_progress:= '10: get_rel_gen_method: result = NO_METHOD_FOUND';
2753       IF (g_po_wf_debug = 'Y') THEN
2754          po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
2755       END IF;
2756 
2757     else
2758       resultout := wf_engine.eng_completed || ':' || x_rel_gen_method;
2759 
2760       x_progress:= '20: get_rel_gen_method: ' || 'result = ' || x_rel_gen_method;
2761       IF (g_po_wf_debug = 'Y') THEN
2762          po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
2763       END IF;
2764     end if;
2765 
2766     -- <SERVICES FPJ START>
2767     IF (x_rel_gen_method <> 'MANUAL') THEN
2768         -- Remove any associated expense line
2769         purge_expense_lines(itemtype, itemkey);
2770     END IF;
2771     -- <SERVICES FPJ END>
2772 
2773 exception
2774   when others then
2775     wf_core.context('po_autocreate_doc','get_rel_gen_method',x_progress);
2776     raise;
2777 end get_rel_gen_method;
2778 
2779 
2780 /***************************************************************************
2781  *
2782  *  Procedure:  cont_wf_autocreate_rel_gen
2783  *
2784  *  Description:  Decides whether automatic autocreation should
2785  *      take place (ie. continue with workflow) if
2786  *      the release generation method is AutoCreate.
2787  *
2788  **************************************************************************/
2789 procedure cont_wf_autocreate_rel_gen (itemtype   IN   VARCHAR2,
2790                                       itemkey    IN   VARCHAR2,
2791                                       actid      IN   NUMBER,
2792                                       funcmode   IN   VARCHAR2,
2793                                       resultout  OUT NOCOPY  VARCHAR2 ) is
2794 
2795 x_cont_wf_for_ac_rel_gen   varchar2(1);
2796 x_progress                 varchar2(300);
2797 
2798 begin
2799 
2800    x_cont_wf_for_ac_rel_gen:= po_wf_util_pkg.GetItemAttrText (itemtype => itemtype,
2801                                                   itemkey  => itemkey,
2802                                                   aname    => 'CONT_WF_FOR_AC_REL_GEN');
2803 
2804    if (x_cont_wf_for_ac_rel_gen = 'Y') then
2805      resultout := wf_engine.eng_completed || ':' ||  'Y';
2806 
2807      x_progress:= '10: cont_wf_autocreate_rel_gen: Y';
2808      IF (g_po_wf_debug = 'Y') THEN
2809         po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
2810      END IF;
2811 
2812    else
2813      resultout := wf_engine.eng_completed || ':' ||  'N';
2814 
2815      x_progress:= '20: cont_wf_autocreate_rel_gen: N';
2816      IF (g_po_wf_debug = 'Y') THEN
2817         po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
2818      END IF;
2819 
2820      -- Remove any associated expense line
2821      purge_expense_lines(itemtype, itemkey);  -- <SERVICES FPJ>
2822    end if;
2823 
2824 exception
2825   when others then
2826     wf_core.context('po_autocreate_doc','cont_wf_autocreate_rel_gen',x_progress);
2827     raise;
2828 end cont_wf_autocreate_rel_gen;
2829 
2830 
2831 /***************************************************************************
2832  *
2833  *  Procedure:  insert_cand_req_lines_into_tbl
2834  *
2835  *  Description:  Inserts a req line into the temp table.
2836  *      This means its possible to try and autocreate this
2837  *      line.
2838  *
2839  **************************************************************************/
2840 procedure insert_cand_req_lines_into_tbl (itemtype   IN   VARCHAR2,
2841                                           itemkey    IN   VARCHAR2,
2842                                           actid      IN   NUMBER,
2843                                           funcmode   IN   VARCHAR2,
2844                                           resultout  OUT NOCOPY  VARCHAR2 ) is
2845 
2846 
2847 x_group_id      number;
2848 x_req_header_id     number;
2849 x_req_line_id     number;
2850 x_suggested_buyer_id    number;
2851 x_source_doc_type_code    varchar2(25);
2852 x_source_doc_id     number;
2853 x_source_doc_line   number;
2854 x_suggested_vendor_id     number;
2855 x_suggested_vendor_site_id  number;
2856 x_currency_code     varchar2(15);
2857 x_rate_type     varchar2(30);
2858 x_rate_date     date;
2859 x_rate        number;
2860 x_pcard_id      number;
2861 x_rel_gen_method    varchar2(25);
2862 x_item_id     number;
2863 x_progress          varchar2(300);
2864 
2865 x_contract_id                   number;
2866 l_job_id                        number := null;  -- <SERVICES FPJ>
2867 
2868 begin
2869 
2870   /* Get all the item attributes that we need to put into the
2871    * temp table.
2872    */
2873 
2874   /* Not all the fields are needed in the temp table for our
2875    * processing (grouping) to work. But no harm having them there.
2876    * If in the future they are needed then they'll already be there.
2877    */
2878 
2879    x_group_id:= po_wf_util_pkg.GetItemAttrNumber
2880           (itemtype => itemtype,
2881                                      itemkey  => itemkey,
2882                                      aname    => 'GROUP_ID');
2883 
2884    x_req_header_id:= po_wf_util_pkg.GetItemAttrNumber
2885           (itemtype => itemtype,
2886                                      itemkey  => itemkey,
2887                                          aname    => 'REQ_HEADER_ID');
2888 
2889    x_req_line_id:= po_wf_util_pkg.GetItemAttrNumber
2890           (itemtype => itemtype,
2891                                      itemkey  => itemkey,
2892                                      aname    => 'REQ_LINE_ID');
2893 
2894    x_suggested_buyer_id := po_wf_util_pkg.GetItemAttrNumber
2895           (itemtype => itemtype,
2896                                          itemkey  => itemkey,
2897                                          aname    => 'SUGGESTED_BUYER_ID');
2898 
2899    x_source_doc_type_code := po_wf_util_pkg.GetItemAttrText
2900           (itemtype => itemtype,
2901                                          itemkey  => itemkey,
2902                                          aname    => 'SOURCE_DOCUMENT_TYPE_CODE');
2903 
2904    x_source_doc_id := po_wf_util_pkg.GetItemAttrNumber
2905           (itemtype => itemtype,
2906                                          itemkey  => itemkey,
2907                                          aname    => 'SOURCE_DOCUMENT_ID');
2908 
2909    x_source_doc_line := po_wf_util_pkg.GetItemAttrNumber
2910           (itemtype => itemtype,
2911                                          itemkey  => itemkey,
2912                                          aname    => 'SOURCE_DOCUMENT_LINE_NUM');
2913 
2914 
2915    x_suggested_vendor_id := po_wf_util_pkg.GetItemAttrNumber
2916           (itemtype => itemtype,
2917                                          itemkey  => itemkey,
2918                                          aname    => 'SUGGESTED_VENDOR_ID');
2919 
2920    x_suggested_vendor_site_id := po_wf_util_pkg.GetItemAttrNumber
2921           (itemtype => itemtype,
2922                                          itemkey  => itemkey,
2923                                          aname    => 'SUGGESTED_VENDOR_SITE_ID');
2924 
2925 
2926   x_contract_id := po_wf_util_pkg.GetItemAttrNumber
2927           (itemtype => itemtype,
2928                                          itemkey  => itemkey,
2929                                          aname    => 'CONTRACT_ID');
2930 
2931 
2932    x_currency_code := po_wf_util_pkg.GetItemAttrText
2933           (itemtype => itemtype,
2934                                          itemkey  => itemkey,
2935                                          aname    => 'CURRENCY_CODE');
2936 
2937    x_rate_type := po_wf_util_pkg.GetItemAttrText
2938           (itemtype => itemtype,
2939                                          itemkey  => itemkey,
2940                                          aname    => 'RATE_TYPE');
2941 
2942    x_rate_date := po_wf_util_pkg.GetItemAttrDate
2943           (itemtype => itemtype,
2944                                          itemkey  => itemkey,
2945                                          aname    => 'RATE_DATE');
2946 
2947    x_rate := po_wf_util_pkg.GetItemAttrNumber
2948           (itemtype => itemtype,
2949                                          itemkey  => itemkey,
2950                                          aname    => 'RATE');
2951 
2952    x_pcard_id := po_wf_util_pkg.GetItemAttrNumber
2953           (itemtype => itemtype,
2954                                          itemkey  => itemkey,
2955                                          aname    => 'PCARD_ID');
2956 
2957    x_rel_gen_method := po_wf_util_pkg.GetItemAttrText
2958           (itemtype => itemtype,
2959                                          itemkey  => itemkey,
2960                                          aname    => 'REL_GEN_METHOD');
2961 
2962    x_item_id := po_wf_util_pkg.GetItemAttrText
2963            (itemtype => itemtype,
2964                                           itemkey  => itemkey,
2965                                           aname    => 'ITEM_ID');
2966 
2967    -- <SERVICES FPJ>
2968    l_job_id := po_wf_util_pkg.GetItemAttrNumber
2969                                          (itemtype => itemtype,
2970                                           itemkey  => itemkey,
2971                                           aname    => 'JOB_ID');
2972 
2973   /* Insert the req line into the the temp table.
2974    * The req lines in this table will then be picked up
2975    * later to be autocreated.
2976    */
2977 
2978    x_progress := '10:insert_cand_req_lines_into_tbl: inserting into temp table for ' ||
2979      'req line = ' || to_char(x_req_line_id);
2980 
2981    IF (g_po_wf_debug = 'Y') THEN
2982       po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
2983    END IF;
2984 
2985    begin
2986       INSERT INTO po_wf_candidate_req_lines_temp
2987         (group_id,
2988      requisition_header_id,
2989      requisition_line_id,
2990      suggested_buyer_id,
2991      source_doc_type_code,
2992        source_doc_id,
2993      source_doc_line,
2994      suggested_vendor_id,
2995      suggested_vendor_site_id,
2996                  contract_id,
2997      currency_code,
2998      rate_type,
2999      rate_date,
3000      rate,
3001      pcard_id,
3002      process_code,
3003      release_generation_method,
3004      item_id,
3005                  job_id)  -- <SERVICES FPJ>
3006    VALUES (x_group_id,
3007      x_req_header_id,
3008      x_req_line_id,
3009      x_suggested_buyer_id,
3010      x_source_doc_type_code,
3011        x_source_doc_id,
3012      x_source_doc_line,
3013      x_suggested_vendor_id,
3014      x_suggested_vendor_site_id,
3015                  x_contract_id,
3016      x_currency_code,
3017      x_rate_type,
3018      x_rate_date,
3019      x_rate,
3020      x_pcard_id,
3021      'PENDING',
3022      x_rel_gen_method,
3023      x_item_id,
3024                  l_job_id);  -- <SERVICES FPJ>
3025    exception
3026      when others then
3027        x_progress := '15: insert_cand_req_lines_into_tbl: IN EXCEPTION when inserting' ||
3028          'into po_wf_candidate_req_lines_temp';
3029        IF (g_po_wf_debug = 'Y') THEN
3030           po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
3031        END IF;
3032        raise;
3033    end;
3034 
3035    /* Calling process should do the commit, so comment out here.
3036     * COMMIT;
3037     */
3038 
3039    resultout := wf_engine.eng_completed || ':' ||  'ACTIVITY_PERFORMED';
3040 
3041    x_progress:= '20: insert_cand_req_lines_into_tbl: ACTIVITY_PERFORMED';
3042    IF (g_po_wf_debug = 'Y') THEN
3043       po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
3044    END IF;
3045 
3046 exception
3047   when others then
3048     wf_core.context('po_autocreate_doc','insert_cand_req_lines_into_tbl',x_progress);
3049     raise;
3050 end insert_cand_req_lines_into_tbl;
3051 
3052 
3053 /***************************************************************************
3054  *
3055  *  Procedure:  group_req_lines
3056  *
3057  *  Description:  Groups the requistion lines in the temp table into
3058  *      header and line records which it then inserts into
3059  *      the interface tables.
3060  *
3061  **************************************************************************/
3062 procedure group_req_lines (itemtype   IN   VARCHAR2,
3063                            itemkey    IN   VARCHAR2,
3067 
3064                            actid      IN   NUMBER,
3065                            funcmode   IN   VARCHAR2,
3066                            resultout  OUT NOCOPY  VARCHAR2 ) is
3068 
3069 c1_group_id     number;
3070 c1_req_header_id    number;
3071 c1_req_line_id      number;
3072 c1_suggested_buyer_id   number;
3073 c1_source_doc_type_code   varchar2(25);
3074 c1_source_doc_id    number;
3075 c1_source_doc_line    number;
3076 c1_suggested_vendor_id    number;
3077 c1_suggested_vendor_site_id number;
3078 c1_currency_code    varchar2(15);
3079 c1_rate_type      varchar2(30);
3080 c1_rate_date      date;
3081 c1_rate       number;
3082 c1_process_code     varchar2(30);
3083 c1_rel_gen_method   varchar2(25);
3084 c1_item_id      number;
3085 c1_pcard_id     number;
3086 c1_contract_id      number;
3087 c1_deliver_to_location_code     hr_locations_all.location_code%type;
3088 c1_dest_org_id                  number;                                          -- Consigned FPI
3089 c1_dest_type_code               po_requisition_lines.destination_type_code%TYPE; -- Consigned FPI
3090 c1_cons_from_supp_flag          varchar2(1);                                     -- Consigned FPI
3091 c1_labor_req_line_id            number;  -- <SERVICES FPJ>
3092 
3093 c2_rowid      rowid;
3094 c2_group_id     number;
3095 c2_req_header_id    number;
3096 c2_req_line_id      number;
3097 c2_suggested_buyer_id   number;
3098 c2_source_doc_type_code   varchar2(25);
3099 c2_source_doc_id    number;
3100 c2_source_doc_line    number;
3101 c2_suggested_vendor_id    number;
3102 c2_suggested_vendor_site_id number;
3103 c2_currency_code    varchar2(15);
3104 c2_rate_type      varchar2(30);
3105 c2_rate_date      date;
3106 c2_rate       number;
3107 c2_process_code     varchar2(30);
3108 c2_rel_gen_method   varchar2(25);
3109 c2_item_id      number;
3110 c2_pcard_id     number;
3111 c2_contract_id      number;
3112 c2_labor_req_line_id            number;  -- <SERVICES FPJ>
3113 --<R12 STYLES PHASE II START>
3114 c2_line_type_id             number;
3115 c2_purchase_basis           varchar2(30);
3116 c1_line_type_id             number;
3117 c1_purchase_basis           varchar2(30);
3118 l_pcard_id                  number;
3119 --<R12 STYLES PHASE II END>
3120 
3121 
3122 l_enable_vmi_flag       po_asl_attributes.enable_vmi_flag%TYPE;               -- Consigned FPI
3123 l_last_billing_date     po_asl_attributes.last_billing_date%TYPE;             -- Consigned FPI
3124 l_cons_billing_cycle    po_asl_attributes.consigned_billing_cycle%TYPE;       -- Consigned FPI
3125 
3126 c2_dest_org_id              number;                                           -- Consigned FPI
3127 c2_dest_type_code           po_requisition_lines.destination_type_code%TYPE;  -- Consigned FPI
3128 c2_cons_from_supp_flag      varchar2(1);                                      -- Consigned FPI
3129 
3130 x_group_id      number;
3131 x_first_time_for_this_comb  varchar2(5);
3132 x_interface_header_id   number;
3133 x_suggested_vendor_contact_id   number;
3134 x_suggested_vendor_contact      varchar2(240);
3135 c2_deliver_to_location_code     hr_locations_all.location_code%type;
3136 x_prev_sug_vendor_contact_id    number;
3137 x_carry_contact_to_po_flag      varchar2(10);
3138 
3139 /*  x_grouping_allowed              varchar2(1); Bug 2974129 */
3140 x_group_one_time_address        varchar2(1);
3141 
3142 x_progress          varchar2(300);
3143 x_valid       number;
3144 
3145 c1_ga_flag                      varchar2(1);     -- FPI GA
3146 c2_ga_flag                      varchar2(1);     -- FPI GA
3147 
3148 --Bug 2745549
3149 l_ref_ga_is_valid               varchar2(1) := 'N';
3150 
3151 l_return_status         varchar2(1)    := NULL;
3152 l_msg_count             number         := NULL;
3153 l_msg_data              varchar2(2000) := NULL;
3154 
3155 x_source_contact_id   NUMBER :=NULL; -- Bug 3586181
3156 c2_found      VARCHAR2(1); -- Bug 3586181
3157 l_is_clm_flow       VARCHAR2(1); -- Bug14314684
3158 
3159 
3160 
3161 /* Define the cursor which picks up records from the temp table.
3162  * We need the 'for update' since we are going to update the
3163  * process_code.
3164  */
3165 /* Bug # 1721991.
3166    The 'for update' clause was added to update the row which was processed
3167    in the Cursor c2 but this led to another problem in Oracle 8.1.6.3 or above
3168    where you can't have a commit inside a 'for update' Cursor loop.
3169    This let to the Runtime Error 'fetch out of sequence'
3170    The commit was actually issued in the procedure insert_into_header_interface.
3171    To solve this we removed the for update in the cursor and instead used rowid
3172    to update the row processed by the Cursor.
3173 */
3174 -- <SERVICES FPJ>
3175 -- Added labor_req_line_id to the select statement
3176 cursor c1  is       /* x_group_id is a parameter */
3177   select prlt.group_id,
3178          prlt.requisition_header_id,
3179          prlt.requisition_line_id,
3180    prlt.suggested_buyer_id,
3181          prlt.source_doc_type_code,
3182    prlt.source_doc_id,
3183    prlt.source_doc_line,
3184    prlt.suggested_vendor_id,
3185          prlt.suggested_vendor_site_id,
3186    prlt.currency_code,
3187          prlt.rate_type,
3188    prlt.rate_date,
3189    prlt.rate,
3190    prlt.process_code,
3191    prlt.release_generation_method,
3192    prlt.item_id,
3193    prlt.pcard_id,
3194          prlt.contract_id,
3195          hrl.location_code,
3196          prl.destination_organization_id,
3197          prl.destination_type_code,
3198          prl.labor_req_line_id
3199          --<R12 STYLES PHASE II START>
3200         ,prl.line_type_id,
3201          prl.purchase_basis
3202          --<R12 STYLES PHASE II END>
3203     from po_wf_candidate_req_lines_temp prlt,
3204          po_requisition_lines prl,
3205          hr_locations_all hrl
3206    where prlt.process_code = 'PENDING'
3207      and prlt.group_id     = x_group_id
3208      and prlt.requisition_line_id = prl.requisition_line_id
3209      and prl.deliver_to_location_id = hrl.location_id(+)    -- bug 2709046
3210      and prl.line_location_id IS NULL-- <REQINPOOL> --bug10064616
3211    for update; -- <BUG 5256593>
3212    --bug10064616 changed the condition reqs_in_pool_flag = 'Y' to line_location_id is NULL
3213 
3214 
3215 
3216 -- <SERVICES FPJ>
3217 -- Added labor_req_line_id to the select statement
3218 cursor c2  is       /* x_group_id is a parameter */
3219   select prlt.rowid,   -- Bug# 1721991 , Added rowid to update row processed
3220          prlt.group_id,
3221          prlt.requisition_header_id,
3222          prlt.requisition_line_id,
3223    prlt.suggested_buyer_id,
3224          prlt.source_doc_type_code,
3225    prlt.source_doc_id,
3226    prlt.source_doc_line,
3227    prlt.suggested_vendor_id,
3228          prlt.suggested_vendor_site_id,
3229    prlt.currency_code,
3230          prlt.rate_type,
3231    prlt.rate_date,
3232    prlt.rate,
3233    prlt.process_code,
3234    prlt.release_generation_method,
3235    prlt.item_id,
3236    prlt.pcard_id,
3237          prlt.contract_id,
3238    prl.suggested_vendor_contact,
3239    prl.vendor_contact_id,
3240          hrl.location_code,
3241          prl.destination_organization_id,
3242          prl.destination_type_code,
3243          prl.labor_req_line_id
3244          --<R12 STYLES PHASE II START>
3245         ,prl.line_type_id,
3246          prl.purchase_basis
3247          --<R12 STYLES PHASE II END>
3248 
3249     from po_wf_candidate_req_lines_temp prlt,
3250    po_requisition_lines prl,
3251          hr_locations_all hrl
3252    where prlt.process_code = 'PENDING'
3253      and prlt.group_id     = x_group_id
3254      and prlt.requisition_line_id = prl.requisition_line_id
3255      and prl.deliver_to_location_id = hrl.location_id(+)  -- bug 2709046
3256     and prl.line_location_id IS NULL-- <REQINPOOL> --bug10064616
3257      --Bug# 1721991, for update;
3258    for update; -- <BUG 5256593>
3259    --bug10064616 changed the condition reqs_in_pool_flag = 'Y' to line_location_id is NULL
3260 
3261     --<R12 STYLES PHASE II START>
3262     c1_style_id  PO_DOC_STYLE_HEADERS.style_id%type;
3263     c2_style_id  PO_DOC_STYLE_HEADERS.style_id%type;
3264     c1_valid_style   BOOLEAN;
3265     c2_valid_style   BOOLEAN;
3266     x_return_status   VARCHAR2(1);
3267     x_msg_count       NUMBER;
3268     x_msg_data        VARCHAR2(2000);
3269     --<R12 STYLES PHASE II END>
3270 
3271     x_different varchar2(1) ;
3272     x_src_doc_id number;
3273 
3274 begin
3275 
3276 
3277    /* Get the group_id since we only want to process lines belonging
3278     * to the same group. We need to get the group_id before opening
3279     * the cursor since it is a parameter to the cursor.
3280     */
3281 
3282    x_group_id := po_wf_util_pkg.GetItemAttrNumber (itemtype => itemtype,
3283                                               itemkey  => itemkey,
3284                                               aname    => 'GROUP_ID');
3285 
3286  /* Bug 2974129. This Grouping allowed flag should not decide the #of documents
3287     Instead this should be applied to group the lines.
3288 
3289    x_grouping_allowed := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
3290                                               itemkey  => itemkey,
3291                                               aname    => 'GROUPING_ALLOWED_FLAG');   */
3292 
3293    x_group_one_time_address := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
3294                                               itemkey  => itemkey,
3295                                               aname    => 'GROUP_ONE_ADDR_LINE_FLAG');
3296 
3297 /*   if x_grouping_allowed is NULL then
3298 
3299      x_grouping_allowed := 'Y';
3300 
3301    end if; Bug 2974129 */
3302 
3303    if x_group_one_time_address is NULL then
3304 
3305      x_group_one_time_address := 'Y';
3306 
3307    end if;
3308 
3309    /* Open the cursor with that group_id */
3310    open c1;   /* Based on x_group_id */
3311    -- Outer Loop: Loop through all the Req Lines with process_code = PENDING
3312    loop
3313       fetch c1 into c1_group_id,
3314         c1_req_header_id,
3315               c1_req_line_id,
3316               c1_suggested_buyer_id,
3317               c1_source_doc_type_code,
3318               c1_source_doc_id,
3319               c1_source_doc_line,
3320               c1_suggested_vendor_id,
3321               c1_suggested_vendor_site_id,
3322               c1_currency_code,
3323               c1_rate_type,
3324               c1_rate_date,
3325               c1_rate,
3326               c1_process_code,
3327               c1_rel_gen_method,
3328               c1_item_id,
3329         c1_pcard_id,
3330                     c1_contract_id,
3331                     c1_deliver_to_location_code,
3332                     c1_dest_org_id,
3333                     c1_dest_type_code,
3334                     c1_labor_req_line_id
3335                      --<R12 STYLES PHASE II START>
3336                     ,c1_line_type_id,
3337                      c1_purchase_basis
3338                      --<R12 STYLES PHASE II END>
3339                     ;
3340         exit when c1%NOTFOUND;
3341 
3342         --<R12 STYLES PHASE II START>
3343         c1_valid_style := TRUE;
3344 
3345         x_progress := '01: group_req_lines : c1_req_line_id = '|| to_char(c1_req_line_id);
3346         IF (g_po_wf_debug = 'Y') THEN
3347              po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
3348         END IF;
3349 
3350         -- bug 4923134
3351 
3352         x_progress := '01: group_req_lines : c1_source_doc_id = '|| to_char(c1_source_doc_id);
3353         IF (g_po_wf_debug = 'Y') THEN
3354              po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
3355         END IF;
3356 
3357         x_progress := '01: group_req_lines : c1_contract_id = '|| to_char(c1_contract_id);
3358         IF (g_po_wf_debug = 'Y') THEN
3359              po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
3360         END IF;
3361 
3362 
3363         if (nvl(c1_source_doc_id,c1_contract_id) is not null) then
3364 
3365             c1_style_id := PO_DOC_STYLE_PVT.get_doc_style_id(nvl(c1_source_doc_id,c1_contract_id));  -- bug 4923134
3366 
3367             x_progress := '01: group_req_lines : c1_style_id = '|| to_char(c1_style_id);
3368             IF (g_po_wf_debug = 'Y') THEN
3369                 po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
3370             END IF;
3371 
3372             PO_DOC_STYLE_PVT.style_validate_req_attrs(p_api_version      => 1.0,
3373                                            p_init_msg_list    => fnd_api.g_true,
3374                                            x_return_status    => x_return_status,
3375                                            x_msg_count        => x_msg_count,
3376                                            x_msg_data         => x_msg_data,
3377                                            p_doc_style_id     => c1_style_id,
3378                                            p_document_id      => NULL,
3379                                            p_line_type_id     => c1_line_type_id,
3380                                            p_purchase_basis   => c1_purchase_basis,
3381                                            p_destination_type => c1_dest_type_code,
3382                                            p_source           => 'AUTOCREATE');
3383 
3384              if x_return_status <> FND_API.g_ret_sts_success THEN
3385                 c1_valid_style := FALSE;
3386              end if;
3387          --<Bug#5695323 vmaduri START>
3388          --Set Style ID as 1 if P card is used and source doc or cotract are not there
3389           else
3390           c1_style_id:=1;
3391           end if;
3392          --<Bug#5695323 vmaduri END>
3393          --<R12 STYLES PHASE II END>
3394 
3395      if  c1_valid_style then  --< R12 STYLES PHASE II>
3396 
3397         x_progress := '01: group_req_lines : c1_req_line_id = '|| to_char(c1_req_line_id)||'valid style';
3398         IF (g_po_wf_debug = 'Y') THEN
3399              po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
3400         END IF;
3401 
3402      /* FPI GA start */
3403         if (c1_source_doc_id is not null) then
3404             select global_agreement_flag
3405             into c1_ga_flag
3406             from po_headers_all
3407             where po_header_id = c1_source_doc_id;
3408 		-- <Bug 14172394>
3409         -- Reset c1_ga_flag to NULL if there is no sourcing BPA.
3410         else
3411 		  c1_ga_flag := NULL;
3412         end if;
3413 
3414      /* FPI GA End */
3415 
3416      /* Consigned FPI start */
3417         PO_THIRD_PARTY_STOCK_GRP.Get_Asl_Attributes
3418        ( p_api_version                  => 1.0
3419        , p_init_msg_list                => NULL
3420        , x_return_status                => l_return_status
3421        , x_msg_count                    => l_msg_count
3422        , x_msg_data                     => l_msg_data
3423        , p_inventory_item_id            => c1_item_id
3424        , p_vendor_id                    => c1_suggested_vendor_id
3425        , p_vendor_site_id               => c1_suggested_vendor_site_id
3426        , p_using_organization_id        => c1_dest_org_id
3427        , x_consigned_from_supplier_flag => c1_cons_from_supp_flag
3428        , x_enable_vmi_flag              => l_enable_vmi_flag
3429        , x_last_billing_date            => l_last_billing_date
3430        , x_consigned_billing_cycle      => l_cons_billing_cycle
3431       );
3432     /*Bug11802312 We ll retain the document reference for an consigned PO*/
3433 
3434      --  if c1_cons_from_supp_flag = 'Y' and
3435      --     nvl(c1_dest_type_code,'INVENTORY') = 'EXPENSE' then
3436      --     c1_cons_from_supp_flag := 'N';
3437      --  end if;
3438 
3439     /* Bug#14305183: We will retain the document reference for consigned PO.
3440      * With Profile 'PO: Create Consigned PO As Standard PO' control the type
3441      * of consigned document to be created, if source document as local Blanket.
3442      * If Profile is set to 'Yes', we should still create a Standard PO without
3443      * having the source document reference. Hence x_doc_type_to_create should
3444      * be STANDARD in this case.
3445      * In Case Profile is set to 'No', system will created Consigned Releases.
3446      */
3447     -- Bug#14305183::Start
3448 
3449        If c1_cons_from_supp_flag = 'Y' and nvl(c1_dest_type_code,'INVENTORY') <> 'EXPENSE'
3450        Then
3451          If (NVL(fnd_profile.value('PO_CREATE_CONSIGNED_PO_AS_SPO'),'N') = 'Y')
3452 	     AND (NVL(c1_ga_flag,'N') = 'N')  -- Retain the Source Doc Ref for GBPA
3453 	 Then
3454 	   c1_source_doc_id := NULL;
3455            c1_contract_id   := NULL;
3456          End If;
3457        Else
3458          c1_cons_from_supp_flag := 'N';
3459        End If;
3460 
3461     --Bug Fix#14305183::End
3462      /* Consigned FPI end */
3463 
3464      --<Bug 2745549 mbhargav START>
3465      --Null out GA information if GA is not valid
3466      if c1_source_doc_id is not null then
3467 
3468          is_ga_still_valid(c1_source_doc_id, l_ref_ga_is_valid);
3469 
3470          if l_ref_ga_is_valid = 'N' then
3471              c1_source_doc_id := null;
3472          end if;
3473      end if;
3474      --<Bug 2745549 mbhargav END>
3475 
3476 
3477      /* Supplier PCard FPH. Check whether c1_pcard_id is valid. The function
3478       * will return pcard_id if valid else will have value null if not.
3479      */
3480 
3481      If (c1_pcard_id is not null) then
3482     c1_pcard_id := po_pcard_pkg.get_valid_pcard_id(c1_pcard_id,c1_suggested_vendor_id,c1_suggested_vendor_site_id);
3483      end if;
3484       /* Supplier PCard FPH */
3485       x_first_time_for_this_comb := 'TRUE';
3486       x_suggested_vendor_contact_id := NULL;
3487       x_carry_contact_to_po_flag := 'TRUE';
3488       x_prev_sug_vendor_contact_id := NULL;
3489       c2_found :='Y';
3490 
3491       open c2;
3492       -- Inner Loop: Loop through all the Req Lines with process_code = PENDING,
3493       -- to compare it with current Req Line in cursor c1, and to check if it
3494       -- can be grouped with the Req Line in cursor c1.
3495       loop
3496          fetch c2 into  c2_rowid,  -- Bug# 1721991, Added rowid
3497                         c2_group_id,
3498       c2_req_header_id,
3499             c2_req_line_id,
3500                 c2_suggested_buyer_id,
3501                 c2_source_doc_type_code,
3502                 c2_source_doc_id,
3503       c2_source_doc_line,
3504                 c2_suggested_vendor_id,
3505                 c2_suggested_vendor_site_id,
3506                 c2_currency_code,
3507                 c2_rate_type,
3508                 c2_rate_date,
3509                 c2_rate,
3510                 c2_process_code,
3511                 c2_rel_gen_method,
3512                 c2_item_id,
3513             c2_pcard_id,
3514                         c2_contract_id,
3515       x_suggested_vendor_contact,
3516       x_suggested_vendor_contact_id,
3517                         c2_deliver_to_location_code,
3518                         c2_dest_org_id,
3519                         c2_dest_type_code,
3520                         c2_labor_req_line_id
3521                         --<R12 STYLES PHASE II START>
3522                        ,c2_line_type_id,
3523                         c2_purchase_basis
3524                         --<R12 STYLES PHASE II END>
3525                         ;
3526 
3527 
3528      if (c2%rowcount)= 0 then  -- Bug 3586181
3529               c2_found:='N';
3530            end if;
3531           exit when c2%NOTFOUND;
3532 
3533 
3534 
3535 
3536         --<R12 STYLES PHASE II START>
3537         c2_valid_style := TRUE;
3538         x_progress := '02: group_req_lines : c2_req_line_id = '|| to_char(c2_req_line_id);
3539         IF (g_po_wf_debug = 'Y') THEN
3540              po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
3541         END IF;
3542 
3543         -- bug 4923134
3544         x_progress := '02: group_req_lines : c2_source_doc_id = '|| to_char(c2_source_doc_id);
3545         IF (g_po_wf_debug = 'Y') THEN
3546              po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
3547         END IF;
3548 
3549         x_progress := '02: group_req_lines : c2_contract_id = '|| to_char(c2_contract_id);
3550         IF (g_po_wf_debug = 'Y') THEN
3551              po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
3552         END IF;
3553 
3554         if (nvl(c2_source_doc_id,c2_contract_id) is not null) then
3555 
3556             c2_style_id := PO_DOC_STYLE_PVT.get_doc_style_id(nvl(c2_source_doc_id,c2_contract_id));  -- bug 4923134
3557 
3558             x_progress := '02: group_req_lines : c2_style_id = '|| to_char(c2_style_id);
3559             IF (g_po_wf_debug = 'Y') THEN
3560                  po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
3561             END IF;
3562 
3563             PO_DOC_STYLE_PVT.style_validate_req_attrs(p_api_version      => 1.0,
3564                                            p_init_msg_list    => fnd_api.g_true,
3565                                            x_return_status    => x_return_status,
3566                                            x_msg_count        => x_msg_count,
3567                                            x_msg_data         => x_msg_data,
3568                                            p_doc_style_id     => c2_style_id,
3569                                            p_document_id      => NULL,
3570                                            p_line_type_id     => c2_line_type_id,
3571                                            p_purchase_basis   => c2_purchase_basis,
3572                                            p_destination_type => c2_dest_type_code,
3573                                            p_source           => 'AUTOCREATE');
3574 
3575              if x_return_status <> FND_API.g_ret_sts_success THEN
3576                 c2_valid_style := FALSE;
3577              end if;
3578          --<Bug#5695323 vmaduri START>
3579          --Set Style ID as 1 if P card is used and source doc or cotract are not there
3580           else
3581           c2_style_id:=1;
3582           end if;
3583          --<Bug#5695323 vmaduri END>
3584         --<R12 STYLES PHASE II END>
3585 
3586        if c2_valid_style then          --<R12 STYLES PHASE II>
3587 
3588         x_progress := '02: group_req_lines : c2_req_line_id = '|| to_char(c2_req_line_id)||'valid style';
3589         IF (g_po_wf_debug = 'Y') THEN
3590              po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
3591         END IF;
3592 
3593       /* FPI GA start */
3594         if (c2_source_doc_id is not null) then
3595             select global_agreement_flag
3596             into c2_ga_flag
3597             from po_headers_all
3598             where po_header_id = c2_source_doc_id;
3599 		-- <Bug 14172394>
3600         -- Reset c2_ga_flag to NULL if there is no sourcing BPA.
3601         else
3602 		  c2_ga_flag := NULL;
3603         end if;
3604 
3605      /* FPI GA End */
3606      /* Consigned FPI start */
3607         PO_THIRD_PARTY_STOCK_GRP.Get_Asl_Attributes
3608        ( p_api_version                  => 1.0
3609        , p_init_msg_list                => NULL
3610        , x_return_status                => l_return_status
3611        , x_msg_count                    => l_msg_count
3612        , x_msg_data                     => l_msg_data
3613        , p_inventory_item_id            => c2_item_id
3614        , p_vendor_id                    => c2_suggested_vendor_id
3615        , p_vendor_site_id               => c2_suggested_vendor_site_id
3616        , p_using_organization_id        => c2_dest_org_id
3617        , x_consigned_from_supplier_flag => c2_cons_from_supp_flag
3618        , x_enable_vmi_flag              => l_enable_vmi_flag
3619        , x_last_billing_date            => l_last_billing_date
3620        , x_consigned_billing_cycle      => l_cons_billing_cycle
3621       );
3622 
3623      /*Bug11802312 We ll retain the document reference for an consigned PO*/
3624       -- if c2_cons_from_supp_flag = 'Y' and
3625       --    nvl(c2_dest_type_code,'INVENTORY') = 'EXPENSE' then
3626       --    c2_cons_from_supp_flag := 'N';
3627       -- end if;
3628 
3629     /* Bug#14305183: We will retain the document reference for consigned PO.
3630      * With Profile 'PO: Create Consigned PO As Standard PO' control the type
3631      * of consigned document to be created, if source document as local Blanket.
3632      * If Profile is set to 'Yes', we should still create a Standard PO without
3633      * having the source document reference. Hence x_doc_type_to_create should
3634      * be STANDARD in this case.
3635      * In Case Profile is set to 'No', system will created Consigned Releases.
3636      */
3637      -- Bug#14305183::Start
3638 
3639       If c2_cons_from_supp_flag = 'Y' and nvl(c2_dest_type_code,'INVENTORY') <> 'EXPENSE'
3640       Then
3641         If (NVL(fnd_profile.value('PO_CREATE_CONSIGNED_PO_AS_SPO'),'N') = 'Y')
3642 	    AND (NVL(c2_ga_flag,'N') = 'N')  -- Retain the Source Doc Ref for GBPA
3643 	Then
3644            c2_source_doc_id := NULL;
3645            c2_contract_id := NULL;
3646         End If;
3647       Else
3648         c2_cons_from_supp_flag := 'N';
3649       End If;
3650 
3651       --Bug Fix#14305183::End
3652      /* Consigned FPI end */
3653 
3654      --<Bug 2745549 mbhargav START>
3655      --Null out GA information if GA is not valid
3656      if c2_source_doc_id is not null then
3657 
3658          is_ga_still_valid(c2_source_doc_id, l_ref_ga_is_valid);
3659 
3660          if l_ref_ga_is_valid = 'N' then
3661              c2_source_doc_id := null;
3662          end if;
3663      end if;
3664      --<Bug 2745549 mbhargav END>
3665 
3666      /* Supplier PCard FPH. Check whether c2_pcard_id is valid. The function
3667       * will return pcard_id if valid else will have value null if not.
3668      */
3669   If (c2_pcard_id is not null) then
3670     c2_pcard_id := po_pcard_pkg.get_valid_pcard_id(c2_pcard_id,c2_suggested_vendor_id,c2_suggested_vendor_site_id);
3671   end if;
3672         /* Supplier PCard FPH */
3673     /* Associate similiar lines with the same header. This is the core
3674            * grouping logic.
3675            */
3676 
3677     x_progress := '10: group_req_lines : c1_req_line_id = '
3678         || to_char(c1_req_line_id) || '   c2_req_line_id = '
3679         || to_char(c2_req_line_id);
3680 
3681     if (x_suggested_vendor_contact_id is null) then
3682     x_suggested_vendor_contact_id := get_contact_id(x_suggested_vendor_contact, c2_suggested_vendor_site_id);
3683     end if;
3684 
3685           IF (g_po_wf_debug = 'Y') THEN
3686              po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
3687           END IF;
3688 
3689    /* Bug 1362315
3690    When you initiate the create doc workflow from requisition import
3691    for a batch of 5000 requisitions or more, the process
3692    failed to create the po for one or two requisitions bcos
3693    we were not truncating the sysdate before a comparison in the following
3694    if logic and thereby not creating records in the po_headers_interface
3695    table for the autocreate logic to process the req to a PO.
3696    */
3697 
3698 
3699           /* Add one time location grouping logic */
3700 
3701     if (c1_req_line_id = c2_req_line_id) /* Always insert if c1 and c2 is the same line */
3702              OR
3703              ( /* (x_grouping_allowed = 'Y') AND Bug 2974129 */
3704              (x_group_one_time_address = 'Y' OR
3705               (x_group_one_time_address = 'N' AND
3706 
3707                /* Bug 16518070
3708 			     Added NVL for  deliver to location code as theyre failing for drop ship so flow
3709 			     Because deliver to location is not present in hr locations for drop ship SO instead in hz locations.
3710 				 So above cursors will retrieve NULL values for these variables*/
3711 
3712                nvl(c1_deliver_to_location_code,-99) <> nvl(fnd_profile.value('POR_ONE_TIME_LOCATION'),-99) AND	--bug 4449781 : added nvl
3713                nvl(c2_deliver_to_location_code,-99) <> nvl(fnd_profile.value('POR_ONE_TIME_LOCATION'),-99))) AND
3714              (c1_suggested_buyer_id     = c2_suggested_buyer_id)       AND
3715              (c1_style_id = c2_style_id)  AND                         --<R12 STYLES PHASE II>
3716        (c1_suggested_vendor_id    = c2_suggested_vendor_id)    AND
3717        (c1_suggested_vendor_site_id = c2_suggested_vendor_site_id) AND
3718        (nvl(c1_source_doc_type_code ,'QUOTATION')    =
3719               nvl(c2_source_doc_type_code,'QUOTATION'))                  AND
3720              (nvl(c1_ga_flag,'N')         = nvl(c2_ga_flag,'N'))         AND      -- FPI GA
3721              (nvl(c1_contract_id,-1)    = nvl(c2_contract_id,-1))  AND
3722              (nvl(c1_currency_code,'ok')  = nvl(c2_currency_code, 'ok')) AND
3723        (nvl(c1_rate_type, 'ok')   = nvl(c2_rate_type, 'ok'))   AND
3724              ((c1_rate is NULL AND c2_rate is NULL)     --<Bug 3343855>
3725               OR
3726         (nvl(trunc(c1_rate_date), trunc(sysdate))  = nvl(trunc(c2_rate_date), trunc(sysdate))))  AND --bug9104813
3727        (nvl(c1_rate,-1)     = nvl(c2_rate, -1))    AND
3728        (nvl(c1_pcard_id,-1)   = nvl(c2_pcard_id,-1))   AND
3729        ((nvl(c1_source_doc_id,-1)   = nvl(c2_source_doc_id,-1))
3730         OR
3731               (nvl(c1_source_doc_type_code ,'QUOTATION')   = 'QUOTATION')
3732               OR
3733               ((nvl(c1_source_doc_type_code,'QUOTATION') = 'BLANKET') AND (nvl(c1_ga_flag,'N') = 'Y'))) -- FPI GA   AND
3734              )
3735              -- <SERVICES FPJ START>
3736              OR
3737              (nvl(c1_req_line_id, -1) = nvl(c2_labor_req_line_id, -1))
3738              OR
3739              (nvl(c1_labor_req_line_id, -1) = nvl(c2_req_line_id, -1))
3740              -- <SERVICES FPJ END>
3741           THEN
3742 
3746         END IF;
3743         x_progress := '20: group_req_lines: c1 and c2 match ';
3744         IF (g_po_wf_debug = 'Y') THEN
3745           po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
3747 
3748 
3749        /* Update the process code of the current line in the temp table so
3750           * it doesn't get picked up again by the cursor for processing.
3751         */
3752 
3753        update po_wf_candidate_req_lines_temp
3754        set process_code = 'PROCESSED'
3755              where rowid=c2_rowid;
3756        -- Bug# 1721991, where current of c2;
3757 
3758        x_progress:= '30:group_req_lines: Updated process_code ';
3759        IF (g_po_wf_debug = 'Y') THEN
3760          po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
3761        END IF;
3762 
3763        -- bug 14314684, When the Req has multiple lines with same suggested supplier
3764        -- but with different sourcing document, create doc wf should create different Awards
3765         l_is_clm_flow :=  po_wf_util_pkg.GetItemAttrText (itemtype => itemtype,
3766                                              itemkey  => itemkey,
3767                                              aname    => 'IS_CLM_FLOW');
3768 
3769         if (x_first_time_for_this_comb  = 'TRUE' or (Nvl(l_is_clm_flow,'N')= 'Y' AND nvl(c1_source_doc_id,-1)   <> nvl(c2_source_doc_id,-1))) then
3770 
3771          --<R12 STYLES PHASE II START>
3772          -- bug5731406
3773          -- The check for source doc id (added through bug4923134) has been
3774          -- removed as c2_style_id is always populated after bug5695323
3775 
3776          x_progress:= '30:group_req_lines: is progress payments  ';
3777          IF (g_po_wf_debug = 'Y') THEN
3778            po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
3779          END IF;
3780 
3781          if PO_DOC_STYLE_PVT.is_progress_payments_enabled(c2_style_id) then
3782            l_pcard_id := NULL;
3783          else
3784            l_pcard_id := c2_pcard_id;
3785          end if;
3786 
3787          --<R12 STYLES PHASE II END>
3788 
3789                 if(po_autocreate_doc.insert_into_headers_interface
3790                      (itemtype,
3791                       itemkey,
3792                       c2_group_id,
3793                       c2_suggested_vendor_id,
3794                       c2_suggested_vendor_site_id,
3795                       c2_suggested_buyer_id,
3796                       c2_source_doc_type_code,
3797                       c2_source_doc_id,
3798                       c2_currency_code,
3799                       c2_rate_type,
3800                       c2_rate_date,
3801                       c2_rate,
3802                       l_pcard_id,  --<R12 STYLES PHASE II>
3803                       c2_style_id,  --<R12 STYLES PHASE II>
3804                       x_interface_header_id) = FALSE) then
3805                   exit; --bug 3401653: po creation failed, skip out of inner loop
3806                 end if;
3807 
3808 
3809 
3810           po_autocreate_doc.insert_into_lines_interface (itemtype,
3811                   itemkey,
3812                   x_interface_header_id,
3813                   c2_req_line_id,
3814                   c2_source_doc_line,
3815                   c2_source_doc_type_code,
3816                                                             c2_contract_id,
3817                                                             c2_source_doc_id,         -- GA FPI
3818                                                             c2_cons_from_supp_flag);  -- Consigned FPI
3819 
3820     /* Bug  3586181 When the document is Contract or Global Aggrement
3821                                get the vendor contact from them*/
3822 
3823     /*Bug9060101 Removed the code added in Bug8632992. Since the contact can be
3824       valid across orgs. We validate the contact against the site on the requisition.
3825       If valid we set the source contact id*/
3826 
3827       BEGIN
3828         IF ((NVL(c1_source_doc_type_code,'BLANKET')='CONTRACT')) THEN
3829              SELECT vendor_contact_id
3830              INTO   x_source_contact_id
3831              FROM   po_headers_all
3832              WHERE  po_header_id=c2_contract_id;
3833 
3834         elsif (NVL(c2_ga_flag,'N')='Y') THEN -- For Global Aggrement.
3835              SELECT vendor_contact_id
3836              INTO   x_source_contact_id
3837              FROM   po_headers_all -- To take care of GAs in Diff Operating unit
3838              WHERE  po_header_id=c2_source_doc_id;
3839 
3840         ELSE
3841              x_source_contact_id := NULL;
3842         END IF;
3843 
3844         IF x_source_contact_id IS NOT NULL THEN
3845            IF (NOT valid_contact(c2_suggested_vendor_site_id, x_source_contact_id))
3846            THEN
3847              x_source_contact_id := NULL;
3848            END IF;
3849         END IF;
3850 
3851       EXCEPTION
3852       WHEN no_data_found THEN
3853          x_source_contact_id := NULL;
3854       END;
3855 
3856    /* End  3586181*/
3857 
3858           x_progress := '40: group_req_lines: inserted header'||
3859           ' and line for req line = ' || to_char(c2_req_line_id);
3860     IF (g_po_wf_debug = 'Y') THEN
3861       po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
3862     END IF;
3863 /* bug 2656323
3864    Added code to update vendor_contact_id when  po_headers is inserted for first time. */
3865      if (x_carry_contact_to_po_flag = 'TRUE' and
3866               valid_contact(c2_suggested_vendor_site_id, x_suggested_vendor_contact_id)) then
3867      begin
3868                       update po_headers_interface
3869                 set vendor_contact_id = x_suggested_vendor_contact_id
3870           where interface_header_id = x_interface_header_id;
3871            exception
3872              when others then
3873           IF (g_po_wf_debug = 'Y') THEN
3874                          po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
3875                 END IF;
3876            end;
3877      end if;
3878 
3879 
3880 
3881           x_first_time_for_this_comb := 'FALSE';
3882     --bug#3586181
3883     if (x_suggested_vendor_contact_id is not NULL) then
3884       x_prev_sug_vendor_contact_id := x_suggested_vendor_contact_id;
3885     end if;
3886     --bug#3586181
3887 
3888 
3889        else  /*  ie. x_first_time_for_this_comb  = FALSE */
3890 
3891               /* The line we are checking now can put put onto the same header
3892            * as a previous one, so only insert a new line into the
3893                  * po_lines_interface table.
3894            */
3895 
3896                 po_autocreate_doc.insert_into_lines_interface (itemtype,
3897                   itemkey,
3898                   x_interface_header_id,
3899                   c2_req_line_id,
3900                   c2_source_doc_line,
3901                   c2_source_doc_type_code,
3902                                                             c2_contract_id,
3903                                                             c2_source_doc_id,          -- GA FPI
3904                                                             c2_cons_from_supp_flag);   -- Consigned FPI
3905 
3906            x_progress := '50: group_req_lines: inserted just line for '||
3907              'req line = ' || to_char(c2_req_line_id);
3908      IF (g_po_wf_debug = 'Y') THEN
3909        po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
3910      END IF;
3911 
3912     --bug#3586181
3913      if (x_carry_contact_to_po_flag = 'TRUE')  then -- SS
3914          if ( x_suggested_vendor_contact_id is not null and x_prev_sug_vendor_contact_id is not null) and
3915             (x_suggested_vendor_contact_id <> x_prev_sug_vendor_contact_id) then -- SS
3916               x_carry_contact_to_po_flag := 'FALSE';
3917          end if;
3918      end if;
3919 
3920      -- Start Bug 5250863
3921      if(x_suggested_vendor_contact_id is not null and
3922          x_prev_sug_vendor_contact_id  is null )then
3923           x_prev_sug_vendor_contact_id := x_suggested_vendor_contact_id;
3924      end if;
3925      -- End Bug 5250863
3926     --bug#3586181
3927 
3928        end if;
3929 
3930           end if;
3931     /* Commented by Bug 5250863
3932     --bug#3586181
3933     if(x_suggested_vendor_contact_id is not null)then
3934       x_prev_sug_vendor_contact_id := x_suggested_vendor_contact_id;
3935     end if;
3936     --bug#3586181
3937     */
3938    else
3939         x_progress := '02: group_req_lines : c2_req_line_id = '|| to_char(c2_req_line_id)||'invalid style';
3940         IF (g_po_wf_debug = 'Y') THEN
3941              po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
3942         END IF;
3943 
3944    end if ; --if c2_valid_style     --<R12 STYLES PHASE II>
3945 
3946       end loop;
3947 /* Commented this code as we are updating vendor_contact_id when header is inserted first time.
3948       if (x_carry_contact_to_po_flag = 'TRUE' and
3949           valid_contact(c2_suggested_vendor_site_id, x_suggested_vendor_contact_id)) then
3950             begin
3951                     x_progress := '55: group_req_lines: updating header with vendor contact :'||x_interface_header_id;
3952                     update po_headers_interface
3953                     set vendor_contact_id = x_suggested_vendor_contact_id
3954                     where interface_header_id = x_interface_header_id;
3955             exception
3956                     when others then
3957                     IF (g_po_wf_debug = 'Y') THEN
3958                        po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
3959                     END IF;
3960       end;
3961       end if;
3962 */
3963       close c2;
3964 
3965 
3966 
3967  /* Bug 3586181 Update the contact id if the either Contract or GA has
3968                 got a valid contact */
3969        if (c2_found='Y') then
3970        Begin
3971 
3972          if ( x_source_contact_id is not null) then
3973         update po_headers_interface
3974               set    vendor_contact_id = x_source_contact_id
3975               where  interface_header_id = x_interface_header_id;
3976 
3977          elsif (x_carry_contact_to_po_flag = 'FALSE') then -- Implies contacts in Req lines are different
3978               update po_headers_interface
3982               update po_headers_interface
3979               set    vendor_contact_id = NULL
3980               where  interface_header_id = x_interface_header_id;
3981          elsif (x_carry_contact_to_po_flag = 'TRUE') and (x_prev_sug_vendor_contact_id is not null) then
3983         set    vendor_contact_id = x_prev_sug_vendor_contact_id
3984               where  interface_header_id = x_interface_header_id;
3985 
3986           end if;
3987         end;
3988       end if;
3989  /* End 3586181 */
3990   else
3991         x_progress := '01: group_req_lines : c1_req_line_id = '|| to_char(c1_req_line_id)||'invalid style';
3992         IF (g_po_wf_debug = 'Y') THEN
3993              po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
3994         END IF;
3995   end if;  --if c1_valid_style --<R12 STYLES PHASE II>
3996 
3997 
3998    end loop;
3999 
4000    close c1;
4001 
4002    /* Calling process should do the commit, so comment out here.
4003     * COMMIT;
4004     */
4005 
4006   resultout := wf_engine.eng_completed || ':' ||  'ACTIVITY_PERFORMED';
4007 
4008   x_progress := '60: group_req_lines: result = ACTIVITY_PERFORMED ';
4009   IF (g_po_wf_debug = 'Y') THEN
4010      po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
4011   END IF;
4012 
4013 exception
4014   when others then
4015     close c1;
4016     close c2;
4017     wf_core.context('po_autocreate_doc','group_req_lines',x_progress);
4018     raise;
4019 end group_req_lines;
4020 
4021 
4022 /***************************************************************************
4023  *
4024  *  Procedure:  insert_into_headers_interface
4025  *
4026  *  Description:  Inserts a row into the po_headers_interface
4027  *  Returns false if creating PO header fails, and true otherwise (bug 3401653)
4028  *
4029  **************************************************************************/
4030 function insert_into_headers_interface (itemtype         IN  VARCHAR2,
4031            itemkey         IN  VARCHAR2,
4032            x_group_id        IN  NUMBER,
4033            x_suggested_vendor_id       IN  NUMBER,
4034            x_suggested_vendor_site_id  IN  NUMBER,
4035            x_suggested_buyer_id      IN  NUMBER,
4036            x_source_doc_type_code      IN  VARCHAR2,
4037            x_source_doc_id       IN  NUMBER,
4038            x_currency_code       IN  VARCHAR2,
4039            x_rate_type         IN  VARCHAR2,
4040            x_rate_date         IN  DATE,
4041            x_rate          IN  NUMBER,
4042            x_pcard_id        IN  NUMBER,
4043                      p_style_id                  IN  NUMBER,  --<R12 STYLES PHASE II>
4044            x_interface_header_id   IN OUT NOCOPY  NUMBER)
4045 RETURN boolean is --bug 3401653
4046 
4047 
4048 x_batch_id      number;
4049 x_creation_date     date  := sysdate;
4050 x_last_update_date    date  := sysdate;
4051 x_created_by      number;
4052 x_last_updated_by   number;
4053 x_org_id      number;
4054 x_doc_type_to_create    varchar2(25);
4055 x_release_date      date;
4056 x_document_num      varchar2(25);
4057 x_release_num     number;
4058 x_release_num1      number;
4059 x_currency_code_doc   varchar2(15);
4060 x_found       varchar2(30);
4061 
4062 x_no_releases     number;
4063 x_ga_flag                       varchar2(1);   -- FPI GA
4064 x_progress          varchar2(300);
4065 
4066 x_grouping_allowed              varchar2(1); /* Bug 2974129 */
4067 x_group_code                    po_headers_interface.group_code%TYPE; /* Bug 2974129 */
4068 l_purchasing_org_id             po_headers_all.org_id%TYPE;  --<Shared Proc FPJ>
4069 
4070 --begin bug 3401653
4071 l_source_doc_currency_code      po_headers_all.currency_code%TYPE := NULL;
4072 l_pou_currency_code      po_headers_all.currency_code%TYPE;
4073 l_rou_currency_code      po_headers_all.currency_code%TYPE;
4074 l_pou_sob_id             gl_sets_of_books.set_of_books_id%TYPE;
4075 l_pou_default_rate_type  po_headers_all.rate_type%TYPE;
4076 l_interface_rate         po_headers_all.rate%TYPE := NULL;
4077 l_interface_rate_type    po_headers_all.rate_type%TYPE := NULL;
4078 l_interface_rate_date    po_headers_all.rate_date%TYPE := NULL;
4079 l_display_rate           po_headers_all.rate%TYPE := NULL;
4080 --end bug 3401653
4081 
4082 begin
4083 
4084    /* Set the org context. Backend create_po process assumes it is in
4085     * an org.
4086     */
4087 
4088     x_org_id := po_wf_util_pkg.GetItemAttrNumber
4089           (itemtype => itemtype,
4090                                          itemkey  => itemkey,
4091                                          aname    => 'ORG_ID');
4092 
4093     --<Shared Proc FPJ START>
4094 
4095     x_progress := '10:insert_into_headers_interface:' ||
4096       'just before set_purchasing_org_id';
4097 
4098     set_purchasing_org_id(itemtype,
4099       itemkey,
4100       x_org_id,
4101       x_suggested_vendor_site_id);
4102 
4103     l_purchasing_org_id := po_wf_util_pkg.GetItemAttrNumber
4104                                         (itemtype => itemtype,
4105                                          itemkey  => itemkey,
4106                                          aname    => 'PURCHASING_ORG_ID');
4107 
4108     x_progress:= '20: insert_into_headers_interface: org_id = ' ||
4109     to_char(x_org_id) || ' purchasing_org_id = ' ||
4110     to_char(l_purchasing_org_id);
4111 
4112     --<Shared Proc FPJ END>
4113 
4114 
4115   /* Bug 2974129.
4116      This attribute should decide the grouping logic in Auto Create. If this is set Y,
4117      then the 'DEFAULT' will be populated as grope code else 'REQUISITION' will be
4118      populated as group code */
4119 
4120     x_grouping_allowed := PO_WF_UTIL_PKG.GetItemAttrText (itemtype => itemtype,
4121                                                           itemkey  => itemkey,
4122                                                           aname    => 'GROUPING_ALLOWED_FLAG');
4123 
4124    if x_grouping_allowed = 'N' then
4125           x_group_code := 'REQUISITION';
4126    else
4127           x_group_code := 'DEFAULT';
4128    end if;
4129 
4130 
4131    po_moac_utils_pvt.set_org_context(x_org_id); --<R12 MOAC>
4132 
4133    /* Get user values */
4134 
4135    x_created_by       := to_number(FND_PROFILE.VALUE('user_id'));
4136    x_last_updated_by  := to_number(FND_PROFILE.VALUE('user_id'));
4137 
4138 
4139    /* Get the interface_header_id from the sequence */
4140 
4141    select po_headers_interface_s.nextval
4142      into x_interface_header_id
4143      from sys.dual;
4144 
4145    /* Set the batch id which can be the same as
4146     * the interface_header_id since we create only one
4147     * po at a time from workflow
4148     */
4149 
4150    x_batch_id := x_interface_header_id;
4151 
4152    /* If the source doc is a blanket then we are going to create a blanket release.
4153     * If the source doc is a quotation then we are going to create a standard po.
4154     */
4155 
4156   /* FPI GA - If ga flag is Y then we create a standard PO */
4157 
4158   -- Bug 2695074 getting the ga flag from the db as the attribute does not have any value
4159   -- in this process
4160 
4161    if x_source_doc_id is not null then
4162      select global_agreement_flag, currency_code
4163      into x_ga_flag, l_source_doc_currency_code
4164      from po_headers_all
4165      where po_header_id = x_source_doc_id;
4166    end if;
4167 
4168    /* Bug 2735730.
4172     * we should still create a Standard PO. Hence x_doc_type_to_create
4169     * If x_source_doc_id is null, then it would be only in the case
4170     * when the supplier is set up as a consigned enabled and the
4171     * destination type is INVENTORY for the requisition. In this case,
4173     * should be STANDARD in this case.
4174    */
4175    if (x_source_doc_id is null) then
4176   x_doc_type_to_create := 'STANDARD';
4177    else
4178      if (x_source_doc_type_code = 'BLANKET')
4179       and nvl(x_ga_flag,'N') = 'N' then  -- FPI GA
4180         x_doc_type_to_create    := 'RELEASE';
4181      else
4182         x_doc_type_to_create    := 'STANDARD';
4183      end if;
4184    end if;
4185 
4186 
4187    if (x_doc_type_to_create = 'STANDARD') then
4188 
4189      /* Whether automatic numbering is on our not, we are going to use
4190       * the automatic number from the unique identifier table. This is
4191       * as per req import. If however we have an  po num (eg. emergency po)
4192       * passed into the workflow then we need to use that.
4193       *
4194       * The autocreate backend will take whatever doc num we give it and
4195       * will try and create that. If we weren't to pass in a doc num and
4196       * automatic numbering was on, it would get the next number.
4197       *
4198       * If we are not using automatic numbering but we get the po num
4199       * from the unique identifier table then we could get a number that
4200       * has been used (entered manually by the user). We need to make sure
4201       * that the doc number is unique here since the backend expects that
4202       * when using manual numbering.
4203       */
4204 
4205      x_document_num := po_wf_util_pkg.GetItemAttrText
4206           (itemtype => itemtype,
4207                                          itemkey  => itemkey,
4208                                          aname    => 'PO_NUM_TO_CREATE');
4209 
4210      if (x_document_num is NULL) then
4211 
4212         x_progress := '30: insert_into_headers_interface: Just about to get doc' ||
4213            'num from po_unique_identifier_control';
4214 
4215   IF (g_po_wf_debug = 'Y') THEN
4216     po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
4217   END IF;
4218 
4219 /*
4220    Bug# 1869409
4221    Created a function get_document_num  to get the next doucument
4222    Number from the PO_UNIQUE_IDENTIFIER_CONTROL table. This was
4223    done as the Commit after the UPDATE of the PO_UNIQUE_IDENTIFIER_CONTROL
4224    table was also affecting the Workflow transactions.
4225    The function get_document_num is an autonomous transaction.
4226 */
4227         --<Shared Proc FPJ>
4228         --Get document num in purchasing org
4229         x_document_num := get_document_num(l_purchasing_org_id);
4230 
4231         x_progress := '40: insert_into_headers_interface: Got doc' ||
4232            'num from po_unique_identifier_control';
4233   IF (g_po_wf_debug = 'Y') THEN
4234     po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
4235   END IF;
4236 
4237      end if;
4238 
4239      /* Check to make sure the doc num is not a duplicate */
4240 
4241      begin
4242         --<Shared Proc FPJ>
4243         --Modified the query to select from po_headers_all instead of po_headers.
4244         select 'PO EXISTS'
4245           into x_found
4246           from po_headers_all
4247          where segment1 = x_document_num
4248            and org_id = l_purchasing_org_id    -- <R12 MOAC>
4249            and type_lookup_code IN ('STANDARD', 'PLANNED', 'BLANKET', 'CONTRACT');
4250 
4251      exception
4252         when NO_DATA_FOUND then
4253              null;
4254   when others then
4255        /* We have found a duplicate so raise the exception */
4256 
4257              x_progress := '45: insert_into_headers_interface: document_num is a ' ||
4258          'duplicate - not going to insert into po_headers_interface';
4259        IF (g_po_wf_debug = 'Y') THEN
4260          po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
4261        END IF;
4262 
4263        raise;
4264      end;
4265 
4266      x_release_num      := NULL;
4267      x_release_date     := NULL;
4268 
4269 
4270     --begin bug 3401653
4271     select sob.currency_code, fsp.set_of_books_id
4272       into l_pou_currency_code, l_pou_sob_id
4273       from financials_system_params_all fsp,
4274            gl_sets_of_books sob
4275      where fsp.set_of_books_id = sob.set_of_books_id
4276            and fsp.org_id = l_purchasing_org_id;  -- <R12 MOAC>
4277 
4278     select default_rate_type
4279       into l_pou_default_rate_type
4280       from po_system_parameters_all psp  --<Shared Proc FPJ>
4281      where psp.org_id = l_purchasing_org_id;    -- <R12 MOAC> removed nvl                  --<Shared Proc FPJ>
4282 
4283     select sob.currency_code
4284       into l_rou_currency_code
4285       from financials_system_params_all fsp,
4286            gl_sets_of_books sob
4287      where fsp.set_of_books_id = sob.set_of_books_id
4288            and fsp.org_id = x_org_id;
4289     --end bug 3401653
4290 
4291      /* Bug:565623. gtummala. 10/17/97
4292       * The backend also needs the currency_code to be populated in the
4293       * the po_headers_interface table. Should use functional currency if
4294       * its null.
4295       */
4296      if (x_currency_code is NULL) then
4297        x_currency_code_doc := l_pou_currency_code;
4298      else
4299        x_currency_code_doc := x_currency_code;
4300      end if;
4301 
4302 
4303     --begin bug 3401653
4304 
4305     IF(l_source_doc_currency_code is not null) THEN
4306         x_currency_code_doc := l_source_doc_currency_code;
4307     END IF;
4308 
4309     l_interface_rate_date := x_rate_date;
4310     IF(l_purchasing_org_id = x_org_id) THEN --x_org_id is req_org_id
4311        IF(x_currency_code_doc <> l_rou_currency_code) THEN
4312           --rate from req can go to po because pou=rou
4313           l_interface_rate_type := x_rate_type;
4314           l_interface_rate := x_rate;
4315        END IF;
4316     ELSE
4317         IF(l_pou_currency_code <> x_currency_code_doc) THEN
4318             IF l_pou_default_rate_type IS NULL THEN
4319                 IF (g_po_wf_debug = 'Y') THEN
4320                     x_progress := '47: insert_into_headers_interface: Purchasing Operating unit' ||
4321               ' has no default rate type, cannot create PO';
4322                     po_wf_debug_pkg.insert_debug (itemtype, itemkey, x_progress);
4323                 END IF;
4324                 return FALSE;
4325             END IF;
4326 
4327             -- copy rate info for PO currency to pou_currency
4328             l_interface_rate_type := l_pou_default_rate_type;
4329             l_interface_rate_date := trunc(sysdate);
4330             PO_CURRENCY_SV.get_rate(x_set_of_books_id => l_pou_sob_id,
4331                                     x_currency_code => x_currency_code_doc,
4332                                     x_rate_type => l_pou_default_rate_type,
4333                                     x_rate_date => l_interface_rate_date,
4334                                     x_inverse_rate_display_flag => 'N',
4335                                     x_rate => l_interface_rate,
4336                                     x_display_rate => l_display_rate);
4337 
4338        END IF;
4339        IF(l_rou_currency_code <> x_currency_code_doc) THEN
4340             IF l_pou_default_rate_type IS NULL THEN
4341                 IF (g_po_wf_debug = 'Y') THEN
4342                     x_progress := '47: insert_into_headers_interface: Purchasing Operating unit' ||
4343               ' has no default rate type, cannot create PO';
4344                     po_wf_debug_pkg.insert_debug (itemtype, itemkey, x_progress);
4345                 END IF;
4346 
4347                return FALSE;
4348             END IF;
4349 
4350             -- Fail creation of the PO if there is no rate to convert from
4351             -- ROU currency to PO currency
4352             IF(PO_CURRENCY_SV.rate_exists (
4353                                       p_from_currency => l_rou_currency_code,
4354                                       p_to_currency => x_currency_code_doc,
4355                                       p_conversion_date => trunc(sysdate),
4356                                       p_conversion_type => l_pou_default_rate_type) <> 'Y')
4357                 THEN
4358                 IF (g_po_wf_debug = 'Y') THEN
4359                     x_progress := '48: insert_into_headers_interface: No rate defined to' ||
4360               ' convert from Requesting OU currency to PO currency, cannot create PO';
4361                     po_wf_debug_pkg.insert_debug (itemtype, itemkey, x_progress);
4362                 END IF;
4363                 return FALSE;
4364             END IF;
4365        END IF;
4366     END IF;
4367     --end bug 3401653
4368 
4369 
4370 
4371    else
4372 
4373      -- Doc is RELEASE
4374      -- Bug 4471683
4375      -- If Currency code is null get the functional currency
4376      IF x_currency_code is not NULL THEN
4377        x_currency_code_doc := x_currency_code;
4378      ELSE
4379        x_currency_code_doc := PO_CORE_S2.get_base_currency;
4380      END IF;
4381 
4382      l_interface_rate_type := x_rate_type; --bug 3401653
4383      l_interface_rate_date := x_rate_date; --bug 3401653
4384      l_interface_rate := x_rate; --bug 3401653
4385 
4386      select segment1
4387        into x_document_num
4388        from po_headers
4389       where po_header_id = x_source_doc_id;
4390 
4391      /* Get the release number as the next release in sequence */
4392 
4393      select nvl(max(release_num),0)+1
4394        into x_release_num
4395        from po_releases_all por,    -- <R12 MOAC>
4396             po_headers poh
4397       where poh.po_header_id = x_source_doc_id
4398         and poh.po_header_id = por.po_header_id;
4399 
4400      /* Bug565530. gtummala. 10/23/97.
4401       * Even if the po_releases table gives us the next one in sequence,
4402       * this could conflict with a release_num that we have inserted into
4403       * the po_headers_interface table previously that has yet to converted
4404       * into a release eg. when we have two req lines that will be created
4405       * onto two diff. releases.
4406       */
4407 
4408      -- Bug 722352, lpo, 08/26/98
4409      -- Commented out the release_num filters for the next 2 queries.
4410 
4411      select count (*)
4412        into x_no_releases
4413        from po_headers_interface phi
4414       where phi.document_num = x_document_num;
4415       -- and phi.release_num  = x_release_num;
4416 
4417      if (x_no_releases <> 0) then
4418        select max(release_num)+1
4419    into x_release_num1
4420          from po_headers_interface phi
4421         where phi.document_num = x_document_num;
4422   --  and phi.release_num  = x_release_num;
4423      end if;
4424 
4425      -- End of fix. Bug 722352, lpo, 08/26/98
4426 
4427 
4428 
4429      -- <Action Date TZ FPJ>
4430       /* Bug 638599, lpo, 03/26/98
4431        * Strip out time portion to be consistent with Enter Release form.
4432        * 10/22/2003: Action Date TZ FPJ Change
4433        * Since release_date on the Enter Release form is now
4434        * a datetime, the trunc is now removed.
4435        */
4436       /* Set release date to sysdate */
4437       x_release_date := SYSDATE;
4438 
4439       -- <End Action Date TZ FPJ>
4440 
4441 
4442     end if;
4443 
4444     /* dreddy : bug 1394312 */
4445     if (x_release_num1 >= x_release_num) then
4446      x_release_num := x_release_num1;
4447     end if;
4448 
4449    /* Insert into po_headers_inteface */
4450 
4451    x_progress := '50: insert_into_headers_interface: Just about to insert into ' ||
4452       'po_headers_interface';
4453    IF (g_po_wf_debug = 'Y') THEN
4454       po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
4455    END IF;
4456 
4457    x_progress :=  '11: the doc type to be created ' || x_doc_type_to_create ;
4458 
4459     IF (g_po_wf_debug = 'Y') THEN
4460        po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
4461     END IF;
4462 
4463     begin
4464       insert into po_headers_interface
4465               (wf_group_id,
4466          interface_header_id,
4467                interface_source_code,
4468                batch_id,
4469                process_code,
4470                action,
4471                  document_type_code,
4472                document_subtype,
4473                document_num,
4474                group_code,
4475                vendor_id,
4476                vendor_site_id,
4477          release_num,
4478                release_date,
4479                agent_id,
4480          currency_code,
4481          rate_type_code,
4482          rate_date,
4483          rate,
4484          creation_date,
4485          created_by,
4486              last_update_date,
4487          last_updated_by,
4488          pcard_id,
4489                  style_id     --<R12 STYLES PHASE II>
4490                  )
4491             values
4492               (x_group_id,
4493          x_interface_header_id,
4494                'PO',
4495                x_batch_id,
4496          'NEW',
4497                'NEW',
4498                'PO',                -- PO for both po's and releases
4499                x_doc_type_to_create,
4500                x_document_num,
4501                x_group_code, /* Bug 2974129 */
4502                x_suggested_vendor_id,
4503                x_suggested_vendor_site_id,
4504          x_release_num,
4505          x_release_date,
4506                x_suggested_buyer_id,
4507          x_currency_code_doc,
4508          l_interface_rate_type, --bug 3401653
4509          l_interface_rate_date, --bug 3401653
4510          l_interface_rate, --bug 3401653
4511          x_creation_date,
4512          x_created_by,
4513          x_last_update_date,
4514          x_last_updated_by,
4515          x_pcard_id,
4516                  p_style_id     --<R12 STYLES PHASE II>
4517                  );
4518 
4519                  return TRUE; --bug 3401653
4520 
4521     exception
4522         when others then
4523     x_progress := '55: insert_into_headers_interface: IN EXCEPTION when '||
4524       'inserting into po_headers_interface';
4525           IF (g_po_wf_debug = 'Y') THEN
4526              po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
4527           END IF;
4528 
4529           raise;
4530     end;
4531 
4532     x_progress := '60: insert_into_headers_interface: Inserted into ' ||
4533       'po_headers_interface';
4534     IF (g_po_wf_debug = 'Y') THEN
4535        po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
4536     END IF;
4537 
4538     /* The interface_header_id is returned as an out parameter so that
4539      * subsequent lines can be tied to this same header if needed.
4540      */
4541 
4542 
4543 exception
4544   when others then
4545     wf_core.context('po_autoinsert_into_headers_interface','create_doc',x_progress);
4546     raise;
4547 end insert_into_headers_interface;
4548 
4549 
4550 /***************************************************************************
4551  *
4552  *  Procedure:  insert_into_lines_interface
4553  *
4554  *  Description:  Inserts a row in the po_lines_interface table
4555  *
4556  *
4557  **************************************************************************/
4558 procedure insert_into_lines_interface (itemtype         IN VARCHAR2,
4559                itemkey          IN VARCHAR2,
4560                x_interface_header_id  IN NUMBER,
4561                x_req_line_id        IN NUMBER,
4562                x_source_doc_line      IN NUMBER,
4563                x_source_doc_type_code IN VARCHAR2,
4564                                        x_contract_id          IN NUMBER,
4565                                        x_source_doc_id        IN NUMBER,            -- GA FPI
4566                                        x_cons_from_supp_flag  IN VARCHAR2) is       -- Consigned FPI
4567 
4568 -- <GC FPJ> : removed variable x_contract_num
4569 
4570 x_interface_line_id       number;
4571 x_creation_date     date  := sysdate;
4572 x_last_update_date    date  := sysdate;
4573 x_created_by      number;
4574 x_last_updated_by   number;
4575 x_org_id      number;
4576 x_doc_type_to_create    varchar2(25);
4577 x_action_type_code_line   varchar2(3);
4578 x_line_num      number;
4579 x_progress          varchar2(300);
4580 x_source_line_id                number;
4581 x_ga_flag                       varchar2(1);
4582 
4583 begin
4584 
4585    /* Set the org context. Backend create_po process assumes it is in
4586     * an org.
4587     */
4588 
4589     x_org_id := po_wf_util_pkg.GetItemAttrNumber
4590           (itemtype => itemtype,
4591                                          itemkey  => itemkey,
4592                                          aname    => 'ORG_ID');
4593 
4594     po_moac_utils_pvt.set_org_context(x_org_id); --<R12 MOAC>
4595 
4596    /* Get user values */
4597 
4598    x_created_by       := to_number(FND_PROFILE.VALUE('user_id'));
4599    x_last_updated_by  := to_number(FND_PROFILE.VALUE('user_id'));
4600 
4601    /* FPI GA - If ga flag is Y then we create a standard PO */
4602 
4603    if x_source_doc_id is not null then
4604      select global_agreement_flag
4605      into x_ga_flag
4606      from po_headers_all
4607      where po_header_id = x_source_doc_id;
4608    end if;
4609 
4610    /* Bug 2735730.
4611     * If x_source_doc_id is null, then it would be only in the case
4612     * when the supplier is set up as a consigned enabled and the
4613     * destination type is INVENTORY for the requisition. In this case,
4614     * we should still create a Standard PO. Hence x_doc_type_to_create
4615     * should be STANDARD in this case.
4616    */
4617    if (x_source_doc_id is null) then
4618   x_doc_type_to_create := 'STANDARD';
4619    else
4620      if (x_source_doc_type_code = 'BLANKET')
4621       and nvl(x_ga_flag,'N') = 'N' then  -- FPI GA
4622         x_doc_type_to_create    := 'RELEASE';
4623      else
4624         x_doc_type_to_create    := 'STANDARD';
4625      end if;
4626    end if;
4627 
4628    if (x_doc_type_to_create = 'STANDARD') then
4629      x_action_type_code_line  := NULL;
4630      x_line_num           := NULL;
4631 
4632      -- <GC FPJ START>
4633      -- We can now insert contract_id into po_lines_interface directly
4634      -- and therefore no need to derive contract_num
4635 
4636      --if (x_contract_id is not null) then
4637      --
4638      --  select max(segment1)
4639      --    into x_contract_num
4640      --    from po_headers
4641      --   where po_header_id = x_contract_id;
4642      --
4643      --end if;
4644 
4645      -- <GC FPJ END>
4646 
4647    else
4648       /* RELEASE */
4649       x_action_type_code_line := 'ADD';
4650       x_line_num        :=  x_source_doc_line;
4651     end if;
4652 
4653 
4654     select po_lines_interface_s.nextval
4655       into x_interface_line_id
4656       from sys.dual;
4657 
4658     /*  GA FPI start */
4659 
4660      if  x_source_doc_id is not null and
4661          x_source_doc_line is not null then
4662 
4663     -- SQL what  Get the line id from the source doc line
4664     -- SQL why    Requisition line does not store the line id
4665     -- Bug fix 2703592 - need to select from all table instead of po_lines
4666          Select  po_line_id
4667          into x_source_line_id
4668          From po_lines_all
4669          Where  po_header_id = x_source_doc_id
4670          And line_num = x_source_doc_line;
4671 
4672      end if;
4673 
4674     /*  GA FPI end */
4675 
4676     /* Insert into po_lines */
4677 
4678     x_progress :=  '10: insert_into_lines_interface: Just about to insert into ' ||
4679        'po_lines_interface';
4680 
4681     IF (g_po_wf_debug = 'Y') THEN
4682        po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
4683     END IF;
4684 
4685     x_progress :=  '11: the doc type to be created ' || x_doc_type_to_create ;
4686 
4687     IF (g_po_wf_debug = 'Y') THEN
4688        po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
4689     END IF;
4690 /*Bug11802312 We ll retain the document reference for an consigned PO*/
4691     begin
4692       insert into po_lines_interface
4693                   (interface_header_id,
4694                 interface_line_id,
4695           action,
4696                 line_num,
4697                 shipment_num,
4698                 requisition_line_id,
4699                                 contract_id,     -- <GC FPJ>
4700                                 from_header_id, -- GA FPI
4701                                 from_line_id,   -- GA FPI
4702                                 consigned_flag,  -- Bug 2798503
4703         creation_date,
4704         created_by,
4705             last_update_date,
4706         last_updated_by)
4707             values
4708               (x_interface_header_id,
4709                  x_interface_line_id,
4710            x_action_type_code_line,
4711                  x_line_num,
4712                  null,
4713                  x_req_line_id,
4714                                  x_contract_id,   -- <GC FPJ>
4715                                  x_source_doc_id ,  -- Consigned FPI
4716                                  x_source_line_id,  -- Consigned FPI
4717                                  x_cons_from_supp_flag,    -- Bug 2798503
4718          x_creation_date,
4719          x_created_by,
4720          x_last_update_date,
4721          x_last_updated_by);
4722 
4723      exception
4724         when others then
4725            x_progress:= '15: insert_into_lines_interface: IN EXCEPTION when' ||
4726       'inserting into po_lines_interface';
4727      IF (g_po_wf_debug = 'Y') THEN
4728        po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
4729      END IF;
4730      raise;
4731      end;
4732 
4733      x_progress := '20: insert_into_lines_interface: Inserted into ' ||
4734        'po_lines_interface';
4735      IF (g_po_wf_debug = 'Y') THEN
4736         po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
4737      END IF;
4738 
4739 
4740 exception
4741   when others then
4742     wf_core.context('po_autoinsert_into_lines_interface','insert_into_lines_interface',
4743                       x_progress);
4744     raise;
4745 end insert_into_lines_interface;
4746 
4747 
4748 /***************************************************************************
4749  *
4750  *  Procedure:  launch_doc_creation_approval
4751  *
4752  *  Description:  Launches the child doc creation and approval process
4753  *      per document.
4754  *
4755  **************************************************************************/
4756 procedure launch_doc_creation_approval (itemtype   IN   VARCHAR2,
4757                           itemkey    IN   VARCHAR2,
4758                           actid      IN   NUMBER,
4759                           funcmode   IN   VARCHAR2,
4760                           resultout  OUT NOCOPY  VARCHAR2 )  is
4761 
4762 x_ItemType              varchar2(20) := itemtype; /* Calling proc has same
4763                * item type as called proc
4764                  */
4765 x_ItemKey               varchar2(60) := null;
4766 x_workflow_process      varchar2(40) := 'CREATE_AND_APPROVE_DOC';
4767 x_group_id    number;
4768 x_interface_header_id   number;
4769 x_doc_type_to_create  varchar2(25);
4770 x_seq_for_item_key  varchar2(25)  := null;  --Bug14305923
4771 x_agent_id    number;
4772 x_org_id    number;
4773 l_purchasing_org_id   number;  --<Shared Proc FPJ>
4774 x_progress        varchar2(300);
4775 l_vendor_site_id        PO_HEADERS_ALL.vendor_site_id%TYPE := NULL; --<BUG 3538308>
4776 
4777 cursor c1 is        /* x_group_id is a parameter */
4778      select interface_header_id,
4779       document_subtype,
4780       agent_id,
4781             vendor_site_id --<BUG 3538308>
4782        from po_headers_interface
4783       where wf_group_id = x_group_id
4784    order by interface_header_id;
4785 
4786 
4787 begin
4788 
4789    /* Set the org context. Backend create_po process assumes it is in
4790     * an org.
4791     */
4792 
4793     x_org_id := po_wf_util_pkg.GetItemAttrNumber
4794           (itemtype => itemtype,
4795                                          itemkey  => itemkey,
4796                                          aname    => 'ORG_ID');
4797 
4798     po_moac_utils_pvt.set_org_context(x_org_id); --<R12 MOAC>
4799 
4800 
4801     /* Get the group id so we can launch doc creation and approval
4802      * for this group.
4803      */
4804 
4805     x_group_id := po_wf_util_pkg.GetItemAttrNumber (itemtype => itemtype,
4806                                                itemkey  => itemkey,
4807                                                aname    => 'GROUP_ID');
4808 
4809    open c1;  /* x_group_id is a parameter */
4810 
4811    loop
4812       fetch c1 into x_interface_header_id,
4813         x_doc_type_to_create,
4814         x_agent_id,
4815                     l_vendor_site_id;  --<BUG 3538308>
4816       exit when c1%NOTFOUND;
4817 
4818       --<BUG 3538308 START>
4819       --For every iteration of the for-loop, need to set purchasing_org_id for
4820       --the PO document to be created. The purchasing_org_id retrieved here
4821       --is passed into po_autocreate_doc.start_wf_create_apprv_process.
4822 
4823       x_progress:= '5: launch_doc_creation_approval: before set_purchasing_org_id: ' ||
4824                    ' x_org_id (from workflow)= '|| to_char(x_org_id) ||
4825                    ' l_vendor_site_id (from interface table)='||to_char(l_vendor_site_id);
4826 
4827       set_purchasing_org_id(itemtype                   => itemtype,
4828                             itemkey                    => itemkey,
4829                             p_org_id                   => x_org_id,
4830                             p_suggested_vendor_site_id => l_vendor_site_id);
4831 
4832       l_purchasing_org_id := PO_WF_UTIL_PKG.GetItemAttrNumber (itemtype => itemtype,
4833                                                                itemkey  => itemkey,
4834                                                                aname    => 'PURCHASING_ORG_ID');
4835 
4836       x_progress:= '6: launch_doc_creation_approval: after set_purchasing_org_id: ' ||
4837                    ' l_purchasing_org_id (from workflow)= '|| to_char(l_purchasing_org_id) ||
4838                    ' x_org_id (from workflow)= '|| to_char(x_org_id) ||
4839                    ' l_vendor_site_id (from interface table)='||to_char(l_vendor_site_id);
4840 
4841       IF (g_po_wf_debug = 'Y') THEN
4842         PO_WF_DEBUG_PKG.insert_debug(itemtype   => itemtype,
4843                                      itemkey    => itemkey,
4844                                      x_progress => x_progress);
4845       END IF;
4846 
4847       --<BUG 3538308 END>
4848 
4849       /* Get the unique sequence to make sure item key will be unique */
4850 
4851       select to_char(PO_WF_ITEMKEY_S.NEXTVAL)
4852         into x_seq_for_item_key
4853         from sys.dual;
4854 
4855       /* The item key is the interface_header_id concatenated with the
4856        * unique id from a seq.
4857        */
4858 
4859       x_ItemKey := to_char(x_interface_header_id) || '-' || x_seq_for_item_key;
4860 
4861       /* Launch the req line processing process
4862        *
4863        * Need to pass in the parent's itemtype and itemkey so as to
4864        * all the parent child relationship to be setup in the called
4865        * process.
4866        */
4867 
4868       x_progress:= '10: launch_doc_creation_approval: '||
4869       ' Called start_wf_create_apprv_process with itemkey = '||
4870         x_Itemkey;
4871       IF (g_po_wf_debug = 'Y') THEN
4872          po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
4873       END IF;
4874 
4875       po_autocreate_doc.start_wf_create_apprv_process (x_ItemType,
4876                                                x_ItemKey,
4877                                                x_workflow_process,
4878                          x_interface_header_id,
4879                    x_doc_type_to_create,
4880                    x_agent_id,
4881                    x_org_id,
4882                    l_purchasing_org_id, --<Shared Proc FPJ>
4883                    itemtype,
4884                    itemkey);
4885 
4886 
4887 
4888    end loop;
4889    close c1;
4890 
4891    resultout := wf_engine.eng_completed || ':' ||  'ACTIVITY_PERFORMED';
4892 
4893    x_progress := '20: launch_doc_creation_approval: result = ACTIVITY_PERFORMED';
4894    IF (g_po_wf_debug = 'Y') THEN
4895       po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
4896    END IF;
4897 
4898 exception
4899   when others then
4900     wf_core.context('po_autocreate_doc.launch_doc_creation_approval','launch_doc_creation_approval',
4901                       x_progress);
4902     raise;
4903 end launch_doc_creation_approval;
4904 
4905 
4906 /***************************************************************************
4907  *
4908  *  Procedure:  start_wf_create_apprv_process
4909  *
4910  *  Description:  Creates and starts the 'CREATE_AND_APPROVE_DOC'
4911  *      workflow proccess.
4912  *
4913  **************************************************************************/
4914 procedure start_wf_create_apprv_process (ItemType             VARCHAR2,
4915                                      ItemKey              VARCHAR2,
4916                                      workflow_process     VARCHAR2,
4917                    interface_header_id    NUMBER,
4918            doc_type_to_create VARCHAR2,
4919            agent_id   NUMBER,
4920            org_id     NUMBER,
4921            purchasing_org_id  NUMBER, --<Shared Proc FPJ>
4922            parent_itemtype  VARCHAR2,
4923            parent_itemkey   VARCHAR2) is
4924 
4925 x_progress    varchar2(300);
4926 
4927 begin
4928 
4929   x_progress := '10: start_wf_create_apprv_process: This was called with' ||
4930      'ItemType = ' || ItemType || '/ ' || 'ItemKey = ' || ItemKey;
4931 
4932   IF (g_po_wf_debug = 'Y') THEN
4933      po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
4934   END IF;
4935 
4936 
4937   /* If a process is passed then it will be run
4938    * If a process is not passed then the selector function defined in
4939    * item type will be determine which process to run
4940    */
4941 
4942   IF  (ItemType    is NOT NULL ) AND
4943       (ItemKey     is NOT NULL)  AND
4944       (interface_header_id is NOT NULL ) then
4945         wf_engine.CreateProcess(itemtype => itemtype,
4946                                 itemkey  => itemkey,
4947                                 process  => workflow_process );
4948 
4949         x_progress:= '20. start_wf_create_apprv_process: Just after create process';
4950         IF (g_po_wf_debug = 'Y') THEN
4951            po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
4952         END IF;
4953 
4954   /* Set the item attributes */
4955 
4956   po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
4957                                      itemkey    => itemkey,
4958                                      aname      => 'ORG_ID',
4959                                      avalue     => org_id);
4960 
4961   --<Shared Proc FPJ START>
4962   po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
4963                                      itemkey    => itemkey,
4964                                      aname      => 'PURCHASING_ORG_ID',
4965                                      avalue     => purchasing_org_id);
4966   --<Shared Proc FPJ END>
4967 
4968   po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
4969                                      itemkey    => itemkey,
4970                                      aname      => 'INTERFACE_HEADER_ID',
4971                                      avalue     => interface_header_id);
4972 
4973 
4974   po_wf_util_pkg.SetItemAttrText   (itemtype    => itemtype,
4975                                      itemkey    => itemkey,
4976                                      aname      => 'DOC_TYPE_TO_CREATE',
4977                                      avalue     => doc_type_to_create);
4978 
4979 
4980         po_wf_util_pkg.SetItemAttrNumber (itemtype    => itemtype,
4981                                      itemkey    => itemkey,
4982                                      aname      => 'AGENT_ID',
4983                                      avalue     => agent_id);
4984 
4985 
4986   /* Need to set the parent child relationship between processes */
4987 
4988   wf_engine.SetItemParent (itemtype        => itemtype,
4992          parent_context  => NULL);
4989          itemkey         => itemkey,
4990          parent_itemtype => parent_itemtype,
4991          parent_itemkey  => parent_itemkey,
4993 
4994 
4995         /* Kick off the process */
4996 
4997         x_progress:= '30. start_wf_create_apprv_process: Kicking off StartProcess ' ||
4998                'with item_type = ' || itemtype || '/ ' || 'item_key = ' ||
4999           itemkey;
5000         IF (g_po_wf_debug = 'Y') THEN
5001            po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
5002         END IF;
5003 
5004         wf_engine.StartProcess(itemtype => itemtype,
5005                                itemkey  => itemkey );
5006 
5007     END IF;
5008 
5009 exception
5010   when others then
5011    x_progress:= '40. start_wf_create_apprv_process: IN EXCEPTION';
5012    IF (g_po_wf_debug = 'Y') THEN
5013       po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
5014    END IF;
5015    raise;
5016 end start_wf_create_apprv_process;
5017 
5018 
5019 /***************************************************************************
5020  *
5021  *  Procedure:  create_doc
5022  *
5023  *  Description:  Calls backend autocreate package to create the
5024  *      standard po or blanket release
5025  *
5026  *
5027  **************************************************************************/
5028 procedure create_doc (itemtype   IN   VARCHAR2,
5029                       itemkey     IN   VARCHAR2,
5030                       actid       IN   NUMBER,
5031                       funcmode    IN   VARCHAR2,
5032                       resultout   OUT NOCOPY  VARCHAR2 ) is
5033 
5034 x_interface_header_id     number;
5035 x_num_lines_processed     number;
5036 x_autocreated_doc_id    number;
5037 x_org_id      number;
5038 x_progress          varchar2(300);
5039 
5040 --<Shared Proc FPJ START>
5041 l_purchasing_org_id             PO_HEADERS_ALL.org_id%TYPE;
5042 l_return_status                 VARCHAR2(1);
5043 l_msg_count                     NUMBER;
5044 l_msg_data                      FND_NEW_MESSAGES.message_text%TYPE;
5045 l_doc_number                    PO_HEADERS_ALL.segment1%TYPE;
5046 --<Shared Proc FPJ END>
5047 x_group_shipments varchar2(1);             --<Bug 14608120 Autocreate GE ER>
5048 x_operting_unit_id    Number;              --<Bug 14608120 Autocreate GE ER>
5049 
5050 begin
5051 
5052    /* Set the org context. Backend create_po process assumes it is in
5053     * an org.
5054     */
5055 
5056     x_org_id := po_wf_util_pkg.GetItemAttrNumber
5057           (itemtype => itemtype,
5058                                          itemkey  => itemkey,
5059                                          aname    => 'ORG_ID');
5060     --<Shared Proc FPJ START>
5061     l_purchasing_org_id := po_wf_util_pkg.GetItemAttrNumber
5062           (itemtype => itemtype,
5063                                          itemkey  => itemkey,
5064                                          aname    => 'PURCHASING_ORG_ID');
5065     --<Shared Proc FPJ END>
5066 
5067     PO_MOAC_UTILS_PVT.set_org_context(x_org_id); --<R12 MOAC>
5068     x_interface_header_id := po_wf_util_pkg.GetItemAttrNumber
5069               (itemtype  => itemtype,
5070                                       itemkey    => itemkey,
5071                                       aname      => 'INTERFACE_HEADER_ID');
5072 
5073 
5074 
5075 
5076     /* Call the main sever side routine to actually create
5077      * the documents, ie:
5078      *      - default in values not populated
5079      *      - group accordingly
5080      *      - insert into the main tables from the
5081      *        the interface tables.
5082      *
5083      * x_document_id is populated with po_header_id for pos
5084      * and po_release_id for releases
5085      */
5086 
5087 
5088      x_progress:= '10: create_doc: Kicking off backend with' ||
5089             'interface_header_id = '|| to_char(x_interface_header_id);
5090      IF (g_po_wf_debug = 'Y') THEN
5091         po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
5092      END IF;
5093 
5094 	 --<Bug 14608120 Autocreate GE ER Start>
5095     /*Logic to get the group shipments value in the purchasing options
5096     and pass it to create_document proc*/
5097 
5098     select nvl(group_shipments_flag, 'Y')
5099     into x_group_shipments
5100     from po_system_parameters;
5101 
5102     --<Bug 14608120 Autocreate GE ER End>
5103 
5104      --<Shared Proc FPJ>
5105      --Call Autocreate Backend to create the document
5106      --in the purchasing org specified.
5107      po_interface_s.create_documents(p_api_version              => 1.0,
5108                                      x_return_status            => l_return_status,
5109                                      x_msg_count                => l_msg_count,
5110                                      x_msg_data                 => l_msg_data,
5111                                      p_batch_id                 => x_interface_header_id,
5112                                      p_req_operating_unit_id  => x_org_id,
5113                                      p_purch_operating_unit_id  => l_purchasing_org_id,
5114                                      x_document_id              => x_autocreated_doc_id,
5115                                      x_number_lines             => x_num_lines_processed,
5116                                      x_document_number        => l_doc_number,
5117                  -- Bug 3648268. Using lookup code instead of hardcoded value
5118                                      p_document_creation_method => 'CREATEDOC',
5119              p_orig_org_id              => x_org_id ,   -- <R12 MOAC>
5120 			 p_group_shipments          => x_group_shipments  --<Bug 14608120 Autocreate GE ER>
5121                                     );
5122 
5123 
5124      x_progress := '20: create_doc: Came back from the backend with '  ||
5125        'doc_id = ' || to_char(x_autocreated_doc_id) || '/ ' ||
5126        'num_lines_processed = ' || to_char(x_num_lines_processed);
5127 
5128      IF (g_po_wf_debug = 'Y') THEN
5129         po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
5130      END IF;
5131 
5132 
5133      /* If at least one req line got processed then we have succeeded in
5134       * creating the po or release
5135       */
5136 
5137      if (x_num_lines_processed > 0) then
5138        po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
5139                                     itemkey    => itemkey,
5140                                     aname      => 'AUTOCREATED_DOC_ID',
5141                                     avalue     => x_autocreated_doc_id);
5142 
5143        /* Call procedure to setup notification data which will be used
5144         * in sending a notification to the buyer that the doc has been
5145         * created successfully.
5146         */
5147 
5148        po_autocreate_doc.setup_notification_data (itemtype, itemkey);
5149 
5150        resultout := wf_engine.eng_completed || ':' ||  'CREATE_OK';
5151 
5152        x_progress:= '30: create_doc: result = CREATE_OK';
5153        IF (g_po_wf_debug = 'Y') THEN
5154           po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
5155        END IF;
5156 
5157      else
5158        resultout := wf_engine.eng_completed || ':' ||  'CREATE_FAILED';
5159 
5160        x_progress:= '40: create_doc: result = CREATE_FAILED';
5161        IF (g_po_wf_debug = 'Y') THEN
5162           po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
5163        END IF;
5164 
5165      end if;
5166 
5167 exception
5168   when others then
5169     wf_core.context('po_autocreate_doc','create_doc',x_progress);
5170     raise;
5171 end create_doc;
5172 
5173 
5174 /***************************************************************************
5175  *
5176  *  Procedure:  setup_notification_data
5177  *
5178  *  Description:  Setup all the data (item attributes etc) needed
5179  *      for the notification.
5180  *
5181  **************************************************************************/
5182 procedure setup_notification_data (itemtype   IN   VARCHAR2,
5183                              itemkey    IN   VARCHAR2) is
5184 
5185 
5186 
5187 x_doc_type_to_create          varchar2(25);
5188 x_doc_type_created_disp   varchar2(80);
5189 x_segment1      varchar2(20);
5190 x_release_num     number;
5191 x_agent_id      number;
5192 x_username          varchar2(100);
5193 x_user_display_name     varchar2(240);
5194 x_autocreated_doc_id    number;
5195 l_open_form                     varchar2(200); --Bug#2982867
5196 l_view_po_url varchar2(1000);   -- HTML Orders R12
5197 l_edit_po_url varchar2(1000);   -- HTML Orders R12
5198 l_style_id    po_headers_all.style_id%TYPE;  -- HTML Orders R12
5199 
5200 x_progress          varchar2(300);
5201 
5202 begin
5203 
5204    x_doc_type_to_create := po_wf_util_pkg.GetItemAttrText
5205             (itemtype   => itemtype,
5206                                          itemkey    => itemkey,
5207                                          aname      => 'DOC_TYPE_TO_CREATE');
5208 
5209    x_autocreated_doc_id := po_wf_util_pkg.GetItemAttrNumber
5210             (itemtype   => itemtype,
5211                                          itemkey    => itemkey,
5212                                          aname      => 'AUTOCREATED_DOC_ID');
5213 
5214 
5215    x_agent_id :=  po_wf_util_pkg.GetItemAttrNumber
5216               (itemtype  => itemtype,
5217                                       itemkey    => itemkey,
5218                                       aname      => 'AGENT_ID');
5219 
5220 
5221    /* Get the displayed value from po_lookup_codes so it will be translated
5222     * This will return either 'Standard PO' or 'Release'
5223     */
5224    --<R12 STYLES PHASE II START>
5225    if (x_doc_type_to_create = 'STANDARD') then
5226        x_doc_type_created_disp:= PO_DOC_STYLE_PVT.get_style_display_name(x_autocreated_doc_id);
5227    else    --releases
5228 
5229      select displayed_field
5230        into x_doc_type_created_disp
5231        from po_lookup_codes
5232       where lookup_type = 'NOTIFICATION TYPE'
5233         and lookup_code = x_doc_type_to_create;
5234    end if;
5235      --<R12 STYLES PHASE II END>
5236 
5237 
5238    po_wf_util_pkg.SetItemAttrText   (itemtype   => itemtype,
5239                                 itemkey    => itemkey,
5240                                 aname      => 'DOC_TYPE_CREATED_DISP',
5241                                 avalue     => x_doc_type_created_disp);
5242 
5243    /* Get the document number created */
5244 
5245    if (x_doc_type_to_create = 'STANDARD') then
5246 
5247      --<Shared Proc FPJ>
5248      --Modified the query to select from po_headers_all instead of
5249      --po_headers.
5250      --< HTML Orders R12>
5251      -- selected style id
5252      select segment1,
5253             style_id
5254        into x_segment1,
5255             l_style_id
5256        from po_headers_all
5257       where po_header_id = x_autocreated_doc_id;
5258 
5259 
5260      po_wf_util_pkg.SetItemAttrText   (itemtype   => itemtype,
5261                                   itemkey    => itemkey,
5262                                   aname      => 'DOC_NUM_CREATED',
5263                                   avalue     => x_segment1);
5264 
5265      /* Bug 2982867, Assigning the proper Command and setting the item
5266      attribute to open PO or Release Form depending on what
5267      document was created. */
5268      -- <HTML Orders R12 Start >
5269      -- Set the URL and form link attributes based on doc style and type
5270      l_view_po_url := PO_REQAPPROVAL_INIT1.get_po_url(
5271                                     p_po_header_id    => x_autocreated_doc_id,
5272                                     p_doc_subtype  => x_doc_type_to_create,
5273                                     p_mode         => 'viewOnly');
5274 
5275      l_edit_po_url := PO_REQAPPROVAL_INIT1.get_po_url(
5276                                     p_po_header_id    => x_autocreated_doc_id,
5277                                     p_doc_subtype  => x_doc_type_to_create,
5278                                     p_mode         => 'update');
5279 
5280      IF PO_DOC_STYLE_GRP.is_standard_doc_style(l_style_id) = 'Y' THEN
5281        l_open_form := 'PO_POXPOEPO:PO_HEADER_ID="' || '&' ||
5282                       'AUTOCREATED_DOC_ID"' ||
5283                       ' ACCESS_LEVEL_CODE="MODIFY"' ||
5284                       ' POXPOEPO_CALLING_FORM="POXSTNOT"';
5285      ELSE
5286        l_open_form := null;
5287      END IF;
5288      -- <HTML Orders R12 End >
5289    else
5290      /* RELEASE */
5291 
5292      select poh.segment1,
5293       por.release_num
5294        into x_segment1,
5295       x_release_num
5296        from po_headers_all  poh,    -- <R12 MOAC>
5297             po_releases por
5298       where por.po_release_id = x_autocreated_doc_id
5299   and por.po_header_id  = poh.po_header_id;
5300 
5301       /* Append the release num to blanket po num */
5302 
5303       po_wf_util_pkg.SetItemAttrText (itemtype   => itemtype,
5304                                   itemkey    => itemkey,
5305                                   aname      => 'DOC_NUM_CREATED',
5306                                   avalue     => x_segment1 || ', ' ||
5307                   to_char (x_release_num));
5308 
5309      -- HTML Orders R12: HTML URLs not applicable for releases
5310      l_view_po_url := '';
5311      l_edit_po_url := '';
5312      -- Bug 2982867
5313      l_open_form := 'PO_POXPOERL:PO_RELEASE_ID="' || '&' ||
5314                     'AUTOCREATED_DOC_ID"' ||
5315                     ' ACCESS_LEVEL_CODE="MODIFY"' ||
5316                     ' POXPOERL_CALLING_FORM="POXSTNOT"';
5317    end if;
5318 
5319     PO_WF_UTIL_PKG.SetItemAttrText   (itemtype   => itemtype,
5320                                   itemkey    => itemkey,
5321                                   aname      => 'OPEN_FORM_COMMAND',
5322                                   avalue     =>l_open_form );
5323 
5324     -- HTML Orders R12
5325     -- Set the URL and form attributes
5326     PO_WF_UTIL_PKG.SetItemAttrText ( itemtype   => itemtype,
5327                               itemkey    => itemkey,
5328                               aname      => 'VIEW_DOC_URL' ,
5329                               avalue     => l_view_po_url);
5330 
5331     PO_WF_UTIL_PKG.SetItemAttrText ( itemtype   => itemtype,
5332                               itemkey    => itemkey,
5333                               aname      => 'EDIT_DOC_URL' ,
5334                               avalue     => l_edit_po_url);
5335 
5336    /* Need to get the username (webuser) of the person we want to send
5337     * to send the notification to.
5338     *
5339     * Call po_req_approval_init1.get_user_name which then calls
5340     * wf.GetUserName to get the info.
5341     *
5342     * The agent_id in the po_agents table has the same value as the employee_id
5343     * in the HR_EMPLOYEES view for the corresponding employee so we can pass in
5344     * agent_id
5345     */
5346 
5347    x_progress := '10: setup_notification_data: Got the doc num created.' ||
5348      'Just before call to get_user_name';
5349    IF (g_po_wf_debug = 'Y') THEN
5350       po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
5351    END IF;
5352 
5353    po_reqapproval_init1.get_user_name(x_agent_id, x_username, x_user_display_name);
5354 
5355    po_wf_util_pkg.SetItemAttrText   (itemtype   => itemtype,
5356                                 itemkey    => itemkey,
5357                                 aname      => 'BUYER_USERNAME',
5358                                 avalue     => x_username);
5359 
5360 
5361    x_progress := '20: setup_notification_data: Username = ' || x_username ||
5362      'End of setup_notification_data';
5363    IF (g_po_wf_debug = 'Y') THEN
5364       po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
5365    END IF;
5366 
5367 exception
5368   when others then
5369     wf_core.context('po_autosetup_notification_data','setup_notification_data',x_progress);
5370     raise;
5371 end setup_notification_data;
5372 
5373 
5374 /***************************************************************************
5375  *
5376  *  Procedure:  should_doc_be_approved
5377  *
5378  *  Description:  Decides whether document approval process should
5379  *      be kicked off or not.
5380  *
5381  **************************************************************************/
5382 procedure should_doc_be_approved (itemtype   IN   VARCHAR2,
5383                                   itemkey    IN   VARCHAR2,
5384                                   actid      IN   NUMBER,
5385                                   funcmode   IN   VARCHAR2,
5386                                   resultout  OUT NOCOPY  VARCHAR2 ) is
5387 
5388 x_auto_approve_doc   varchar2(1);
5389 x_progress           varchar2(300);
5390 
5391 --<R12 eTax Integration Start>
5392 l_doc_type_to_create    po_headers_all.type_lookup_code%TYPE;
5393 l_doc_type              po_document_types_all_b.document_type_code%TYPE;
5394 l_doc_subtype           po_document_types_all_b.document_subtype%TYPE;
5395 l_po_header_id          po_headers_all.po_header_id%TYPE;
5396 l_po_release_id         po_releases_all.po_release_id%TYPE;
5397 l_return_status         VARCHAR2(1);
5398 --<R12 eTax Integration End>
5399 
5400 --start of code changes for the bug 14243104 fix
5401 l_document_id NUMBER;
5402 l_document_number VARCHAR2(80);
5403 l_emailaddress VARCHAR2(2000);
5404 l_default_method VARCHAR2(25);
5405 l_fax_number VARCHAR2(25);
5406 l_preparer_id NUMBER;
5407 --end of code changes for the bug 14243104 fix
5408 
5409 begin
5410 
5411    /* This decision is made by simply looking at an item atrribute,
5412     * which has a default value. All the user needs to do is change
5413     * that attribute according to their needs.
5414     */
5415    --<R12 eTax Integration Start>
5416    --
5417    -- Calculate tax before launching approval process
5418    -- If tax calculation is success and the wf attribute AUTO_APPROVE_DOC
5419    -- is set to Y then this function would return Y else it would
5420    -- return N. So Approval depends on tax calculation being successful
5421    --
5422    --<R12 eTax Integration End>
5423    x_auto_approve_doc := po_wf_util_pkg.GetItemAttrText (itemtype => itemtype,
5424                                                     itemkey  => itemkey,
5425                                                     aname    => 'AUTO_APPROVE_DOC');
5426 
5427 
5428    --<R12 eTax Integration Start>
5429    l_po_release_id      := null;
5430    l_po_header_id       := null;
5431    l_doc_type_to_create := null;
5432    l_doc_type           := null;
5433    l_doc_subtype        := null;
5434    l_return_status      := null;
5435    l_doc_type_to_create := po_wf_util_pkg.GetItemAttrText (itemtype => itemtype,
5436                                                            itemkey  => itemkey,
5437                                                            aname    => 'DOC_TYPE_TO_CREATE');
5438 
5439    IF (l_doc_type_to_create = PO_CONSTANTS_SV.RELEASE) THEN
5440       l_doc_type      := PO_CONSTANTS_SV.RELEASE;
5441       l_doc_subtype   := PO_CONSTANTS_SV.BLANKET;
5442       l_po_release_id := po_wf_util_pkg.GetItemAttrNumber
5443                               (itemtype => itemtype,
5444                                itemkey  => itemkey,
5445                                aname    => 'AUTOCREATED_DOC_ID');
5446       l_document_id := l_po_release_id;  --bug 14243104 fix
5447    ELSE
5448       l_doc_type      := PO_CONSTANTS_SV.PO;
5449       l_doc_subtype   := PO_CONSTANTS_SV.STANDARD;
5450       l_po_header_id  := po_wf_util_pkg.GetItemAttrNumber
5451                               (itemtype => itemtype,
5452                                itemkey  => itemkey,
5453                                aname    => 'AUTOCREATED_DOC_ID');
5454       l_document_id := l_po_header_id;  --bug 14243104 fix
5455    END IF;
5456 
5457    po_tax_interface_pvt.calculate_tax(p_po_header_id    => l_po_header_id,
5458                                       p_po_release_id   => l_po_release_id,
5459                                       x_return_status   => l_return_status,
5460                                       p_calling_program => 'AUTOCREATED_DOC_WF');
5461 
5462    IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5463      IF (g_po_wf_debug = 'Y') THEN
5464         po_wf_debug_pkg.insert_debug(itemtype,itemkey,'Tax Errored ');
5465      END IF;
5466    END IF;
5467 
5468    --<R12 eTax Integration End>
5469 
5470    if (x_auto_approve_doc = 'Y' AND l_return_status = FND_API.G_RET_STS_SUCCESS) then   --<R12 eTax Integration>
5471 
5472      --Start of code changes for the bug 14243104 fix
5473       l_preparer_id := NULL;
5474       PO_VENDOR_SITES_SV.Get_Transmission_Defaults(
5475           p_document_id => l_document_id,
5476           p_document_type => l_doc_type,
5477           p_document_subtype => l_doc_subtype,
5478           p_preparer_id => l_preparer_id,
5479           x_default_method => l_default_method,
5480           x_email_address => l_emailaddress,
5481           x_fax_number => l_fax_number,
5482           x_document_num => l_document_number);
5483 
5484       IF l_default_method = 'EMAIL' AND (l_emailaddress IS NULL OR l_emailaddress='') THEN
5485           resultout := wf_engine.eng_completed || ':' ||  'N';
5486 
5487           x_progress:= '0: should_doc_be_approved: result = N';
5488           IF (g_po_wf_debug = 'Y') THEN
5489             po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
5490           END IF;
5491 
5492       ELSIF l_default_method = 'FAX' AND (l_fax_number IS NULL OR l_fax_number='') THEN
5493           resultout := wf_engine.eng_completed || ':' ||  'N';
5494 
5495           x_progress:= '1: should_doc_be_approved: result = N';
5496           IF (g_po_wf_debug = 'Y') THEN
5497             po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
5498           END IF;
5499       ELSE
5500           resultout := wf_engine.eng_completed || ':' ||  'Y';
5501 
5502           x_progress:= '10: should_doc_be_approved: result = Y';
5503           IF (g_po_wf_debug = 'Y') THEN
5504               po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
5505           END IF;
5506       END IF;
5507       --END of code changes for the bug 14243104 fix
5508 
5509    else
5510      resultout := wf_engine.eng_completed || ':' ||  'N';
5511 
5512      x_progress:= '20: should_doc_be_approved: result = N';
5513      IF (g_po_wf_debug = 'Y') THEN
5514         po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
5515      END IF;
5516 
5517    end if;
5518 
5519 exception
5520   when others then
5521     wf_core.context('po_autocreate_doc','should_doc_be_approved',x_progress);
5522     raise;
5523 end should_doc_be_approved;
5524 
5525 
5526 
5527 /***************************************************************************
5528  *
5529  *  Procedure:  launch_po_approval
5530  *
5531  *  Description:  Kicks off the po approval workflow to approve the
5532  *      po or the release
5533  *
5534  **************************************************************************/
5535 procedure launch_po_approval (itemtype   IN   VARCHAR2,
5536                               itemkey    IN   VARCHAR2,
5537                               actid      IN   NUMBER,
5538                               funcmode   IN   VARCHAR2,
5539                               resultout  OUT NOCOPY  VARCHAR2 ) is
5540 
5541 x_ItemType              varchar2(20) := null;
5542 x_ItemKey               varchar2(60) := null;
5543 x_workflow_process      varchar2(40) := null;
5544 /*Bug8937846 Set the action_orig_from parameter as "CREATEDOC" */
5545 x_action_orig_from      varchar2(30) := 'CREATEDOC';
5546 x_doc_id                number       := null;
5547 x_doc_num         po_headers.segment1%type := null; -- Bug 3152167
5548 x_preparer_id           number       := null;
5549 x_doc_type              varchar2(25) := null;
5550 x_doc_subtype           varchar2(25) := null;
5551 x_submitter_action      varchar2(25) := null;
5552 x_forward_to_id         number       := null;
5553 x_forward_from_id       number       := null;
5554 x_def_approval_path_id  number       := null;
5555 x_note      varchar2(240):= null;
5556 x_seq_for_item_key  varchar2(25)  := null;  --Bug14305923
5557 x_doc_type_to_create    varchar2(25);
5558 x_org_id    number;
5559 x_progress        varchar2(300);
5560 x_printflag             varchar2(1)    := 'N';
5561 x_faxflag               varchar2(1)    := 'N';
5562 x_faxnum                varchar2(30)   := null;
5563 x_fax_area              varchar2(10)   := null;  -- bug 2567900
5564 x_emailflag             varchar2(1)    := 'N';
5565 x_emailaddress          varchar2(2000) := null;
5566 x_default_method        varchar2(25)   := null;
5567 
5568 
5569   /* RETROACTIVE FPI START */
5570   l_document_num      po_headers.segment1%type;
5571   /* RETROACTIVE FPI END */
5572 
5573 /* <SUP_CON FPI START> */
5574 l_consigned_consumption_flag po_headers_all.consigned_consumption_flag%TYPE;
5575 /* <SUP_CON FPI END> */
5576 
5577 l_purchasing_org_id       po_headers_all.org_id%TYPE; --<Shared Proc FPJ>
5578 
5579 /* BUG 4638656 */
5580 l_tp_header_id             ece_tp_details.tp_header_id%TYPE;
5581 l_edi_flag                 ece_tp_details.edi_flag%TYPE;
5582 l_transaction_subtype      ece_tp_details.document_id%TYPE;
5583 /* BUG 4638656 */
5584 
5585 
5586 
5587 begin
5588 
5589     x_org_id := po_wf_util_pkg.GetItemAttrNumber
5590           (itemtype => itemtype,
5591                                          itemkey  => itemkey,
5592                                          aname    => 'ORG_ID');
5593 
5594     --<Shared Proc FPJ START>
5595     l_purchasing_org_id := po_wf_util_pkg.GetItemAttrNumber
5596                                         (itemtype => itemtype,
5597                                          itemkey  => itemkey,
5598                                          aname    => 'PURCHASING_ORG_ID');
5599     --<Shared Proc FPJ END>
5600 
5601 
5602 /* Bug: 1479382 set the org contect */
5603 
5604    IF x_org_id IS NOT NULL THEN
5605 
5606      po_moac_utils_pvt.set_org_context(x_org_id); --<R12 MOAC>
5607 
5608      --<Shared Proc FPJ START>
5609      IF x_org_id <> l_purchasing_org_id THEN
5610        --Set the org_id to be the purchasing org for PO Approval Process
5611        po_wf_util_pkg.SetItemAttrNumber(itemtype   => itemtype,
5612                                         itemkey    => itemkey,
5613                                         aname      => 'ORG_ID',
5614                                         avalue     => l_purchasing_org_id);
5615        --Set the context to be that of purchasing org
5619      --<Shared Proc FPJ END>
5616        po_moac_utils_pvt.set_org_context(l_purchasing_org_id); --<R12 MOAC>
5617 
5618      END IF;
5620    END IF;
5621 
5622     x_doc_type_to_create := po_wf_util_pkg.GetItemAttrText
5623           (itemtype => itemtype,
5624                                          itemkey  => itemkey,
5625                                          aname    => 'DOC_TYPE_TO_CREATE');
5626 
5627    if (x_doc_type_to_create = 'RELEASE') then
5628       x_doc_type    := 'RELEASE';
5629       x_doc_subtype := 'BLANKET';
5630    else
5631       /* STANDARD */
5632       x_doc_type    := 'PO';
5633       x_doc_subtype := 'STANDARD';
5634    end if;
5635 
5636    /* Need to get item_type and workflow process from po_document_types.
5637     * They may be different based on the doc/org.
5638     */
5639 
5640 
5641    /* Get the doc_id, doc_num and preparer_id */
5642 
5643    /* The preparer is the same as the buyer */
5644 
5645    x_preparer_id:= po_wf_util_pkg.GetItemAttrNumber
5646           (itemtype => itemtype,
5647                                          itemkey  => itemkey,
5648                                          aname    => 'SUGGESTED_BUYER_ID');
5649 
5650    x_doc_id:= po_wf_util_pkg.GetItemAttrNumber
5651           (itemtype => itemtype,
5652                                          itemkey  => itemkey,
5653                                          aname    => 'AUTOCREATED_DOC_ID');
5654 
5655 
5656    /* Bug 712718 ecso 08/17/98
5657    ** PO Approval workflow requires a non-null preparer_id
5658    ** Since attributes from another child workflow is not
5659    ** passed to this child workflow, hit the table to get
5660    ** the agent id.
5661     */
5662 
5663     /* RETROACTIVE FPI START.
5664     * Deleted  the  code which gets teh preparer_id if it is null since
5665     * x_preparer_id will be derived in get_transmission_defaults if
5666     * this is null. Also deleted the code which gets the default supplier
5667     * communication method from po_vendor_site and now call
5668     * PO_VENDOR_SITES_SV.Get_Transmission_Defaults which does this now.
5669     * Also deleted the code which defaults item_type, item_key from
5670     * po_document_types as this is done now in start_wf_process.
5671    */
5672 
5673     /* Bug 1845764 :
5674     Check if the supplier site on the PO/Release has email as the default notification method.
5675     if so set the email flag and address from the site */
5676 
5677   -- included bug fix 2342323. Modified changes are to retrieve segment1
5678         -- from po_headers into x_doc_num
5679   -- for release, PO or PA
5680         -- Bug 2567900 Included fax number in the select
5681 
5682         /* bug 4638656 - start */
5683         /* We donot consider transaction subtype POCO, since a
5684            document cannot be in requires reapproval when
5685            launching approval from create doc */
5686 
5687          --Bug4956479 Included agent_id/preparer_id in select/into clause
5688          --for doctype of both PO/RELEASE
5689 
5690         l_transaction_subtype := 'POO';
5691 
5692         IF x_doc_type = 'PO' THEN
5693            BEGIN
5694              select      pvs.tp_header_id,
5695                          nvl(etd.edi_flag,'N'),
5696                          ph.agent_id
5697              into        l_tp_header_id,
5698                          l_edi_flag,
5699                          x_preparer_id
5700              from        ece_tp_details etd,
5701                          po_vendor_sites pvs,
5702                          po_vendors pv,
5703                          po_headers ph
5704              where       pv.vendor_id       = pvs.vendor_id
5705              and         pvs.tp_header_id   = etd.tp_header_id
5706              and         etd.document_id    = l_transaction_subtype
5707              and         ph.vendor_id       = pv.vendor_id
5708              and         ph.vendor_site_id  = pvs.vendor_site_id
5709              and         ph.po_header_id    = x_doc_id
5710              and         ph.type_lookup_code= x_doc_subtype
5711              and         etd.document_type  = ph.type_lookup_code;
5712 
5713            EXCEPTION
5714            WHEN NO_DATA_FOUND THEN
5715               l_edi_flag := 'N';
5716            END;
5717 
5718         ELSIF x_doc_type = 'RELEASE' THEN
5719 
5720            BEGIN
5721 
5722              select      pvs.tp_header_id,
5723                          nvl(etd.edi_flag,'N'),
5724                          pr.agent_id
5725              into        l_tp_header_id,
5726                          l_edi_flag,
5727                          x_preparer_id
5728              from        ece_tp_details etd,
5729                          po_vendor_sites pvs,
5730                          po_vendors pv,
5731                          po_headers ph ,
5732                          po_releases pr
5733              where       pv.vendor_id       = pvs.vendor_id
5734              and         pvs.tp_header_id   = etd.tp_header_id
5735              and         etd.document_id    = l_transaction_subtype
5736              and         ph.vendor_id       = pv.vendor_id
5737              and         ph.vendor_site_id  = pvs.vendor_site_id
5738              and         pr.po_header_id    = ph.po_header_id
5739              and         pr.po_release_id   = x_doc_id
5740              and         etd.document_type  = 'RELEASE';
5741 
5742           EXCEPTION
5743             WHEN NO_DATA_FOUND THEN
5744               l_edi_flag := 'N';
5745           END;
5746         END IF;
5747 
5748         if (l_edi_flag ='Y') and (l_tp_header_id is not null) then
5749            x_printflag   := 'N';
5750            x_faxflag     := 'N';
5751            x_faxnum      := null;
5752            x_emailflag   := 'N';
5753            x_emailaddress:= null;
5754 
5755            --Bug4956479
5756            po_wf_util_pkg.SetItemAttrNumber(itemtype   => itemtype,
5757                                  itemkey    => itemkey,
5758                                  aname      => 'SUGGESTED_BUYER_ID',
5759                                  avalue     => x_preparer_id);
5760 
5761         else
5762            /* bug 4638656 - end */
5763 
5764 
5765   PO_VENDOR_SITES_SV.Get_Transmission_Defaults(
5766                                         p_document_id => x_doc_id,
5767                                         p_document_type => x_doc_type,
5768                                         p_document_subtype => x_doc_subtype,
5769                                         p_preparer_id => x_preparer_id,
5770                                         x_default_method => x_default_method,
5771                                         x_email_address => x_emailaddress,
5772                                         x_fax_number => x_faxnum,
5773                                         x_document_num => x_doc_num );
5774 
5775            -- Bug 3152167 Get the document number and pass the same to start po approval.
5776 
5777      po_wf_util_pkg.SetItemAttrNumber(itemtype   => itemtype,
5778                                  itemkey    => itemkey,
5779                                  aname      => 'SUGGESTED_BUYER_ID',
5780                                  avalue     => x_preparer_id);
5781 
5782 
5783         If (x_default_method = 'EMAIL' ) and (x_emailaddress is not null) then
5784             x_emailflag := 'Y';
5785         elsif  x_default_method  = 'FAX'  and (x_faxnum is not null) then
5786             x_emailaddress := null;
5787             x_faxnum := x_fax_area || x_faxnum;
5788 
5789             x_faxflag := 'Y';
5790         elsif  x_default_method  = 'PRINT' then
5791             x_emailaddress := null;
5792             x_faxnum := null;
5793 
5794             x_printflag := 'Y';
5795         else
5796             x_emailaddress := null;
5797             x_faxnum := null;
5798         end if;
5799 
5800         end if; -- if l_edi_flag..
5801 
5802 
5803         /* <SUP_CON FPI START> */
5804         /* Add code to check if document is Consumption PO/Release.
5805          * If it is, transmission method will be disabled.
5806          */
5807 
5808    /* Kick off the po approval workflow */
5809 
5810    x_progress:= '18: launch_po_approval: Kicking off start_wf_process with' ||
5811      'item_type = ' || x_ItemType || '/ ' || 'item_key = ' ||
5812      x_ItemKey || '/ ' || 'workflow_process = ' || x_workflow_process ||
5813                  '/ ' || 'doc_type = ' || x_doc_type || '/ ' || 'x_doc_id = ' ||
5814      to_char(x_doc_id);
5815 
5816    IF (g_po_wf_debug = 'Y') THEN
5817       po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
5818    END IF;
5819 
5820    po_reqapproval_init1.start_wf_process(
5821                              x_ItemType,
5822                              x_ItemKey,
5823                              x_workflow_process,
5824                              x_action_orig_from,
5825                              x_doc_id,
5826                              x_doc_num,          -- pass in as null since id exists
5827                              x_preparer_id,
5828                              x_doc_type,
5829                              x_doc_subtype,
5830                              x_submitter_action,
5831                              x_forward_to_id,
5832                              x_forward_from_id,
5833                              x_def_approval_path_id,
5834            x_note,
5835                              x_printflag,
5836                              x_faxflag,
5837                              x_faxnum,
5838                              x_emailflag,
5839                              x_emailaddress);
5840 
5841    resultout := wf_engine.eng_completed || ':' ||  'ACTIVITY_PERFORMED';
5842 
5843    x_progress:= '20: launch_po_approval: result = ACTIVITY_PERFORMED';
5844    IF (g_po_wf_debug = 'Y') THEN
5845       po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
5846    END IF;
5847 
5848    --<BUG 5044645 START>  set the org context back to original one
5849    IF x_org_id <> l_purchasing_org_id THEN
5850      po_moac_utils_pvt.set_org_context(x_org_id);
5851    END IF;
5852    --<BUG 5044645 END>
5853 
5854 exception
5855   when others then
5856     wf_core.context('po_autocreate_doc','launch_po_approval',x_progress);
5857     raise;
5858 end launch_po_approval;
5859 
5860 
5861 /***************************************************************************
5862  *
5863  *  Procedure:  purge_rows_from_temp_table
5867  *
5864  *
5865  *  Description:  This purges the rows from the temp table for the
5866  *      group id associated with this workflow run.
5868  **************************************************************************/
5869 procedure purge_rows_from_temp_table (itemtype   IN   VARCHAR2,
5870                                       itemkey    IN   VARCHAR2,
5871                                       actid      IN   NUMBER,
5872                                       funcmode   IN   VARCHAR2,
5873                                       resultout  OUT NOCOPY  VARCHAR2 ) is
5874 x_group_id   number;
5875 x_progress   varchar2(300);
5876 
5877 begin
5878 
5879   x_group_id:= po_wf_util_pkg.GetItemAttrNumber
5880           (itemtype => itemtype,
5881                                      itemkey  => itemkey,
5882                                      aname    => 'GROUP_ID');
5883 
5884 
5885   /* Delete all rows belonging to this group_id */
5886 
5887   delete from po_wf_candidate_req_lines_temp
5888         where group_id = x_group_id;
5889 
5890  /* Calling process should do the commit, so comment out here.
5891   * COMMIT;
5892   */
5893 
5894   resultout := wf_engine.eng_completed || ':' ||  'ACTIVITY_PERFORMED';
5895 
5896   x_progress:= '10: purge_rows_from_temp_table: result = ACTIVITY_PERFORMED';
5897   IF (g_po_wf_debug = 'Y') THEN
5898      po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
5899   END IF;
5900 
5901 
5902 
5903 exception
5904   when others then
5905     wf_core.context('po_autocreate_doc','purge_rows_from_temp_table',x_progress);
5906     raise;
5907 end purge_rows_from_temp_table;
5908 
5909 /***************************************************************************
5910  *
5911  *  Procedure:  is_this_emergency_req
5912  *
5913  *  Description:  This check if there is a reserved PO number
5914  *
5915  **************************************************************************/
5916 procedure is_this_emergency_req(itemtype   IN   VARCHAR2,
5917                                 itemkey    IN   VARCHAR2,
5918                                 actid      IN   NUMBER,
5919                                 funcmode   IN   VARCHAR2,
5920                                 resultout  OUT NOCOPY  VARCHAR2 ) is
5921 
5922 x_req_header_id    number;
5923 x_emergency_po_num varchar2(20);
5924 x_progress         varchar2(300);
5925 
5926 begin
5927 
5928    x_req_header_id := po_wf_util_pkg.GetItemAttrText (itemtype => itemtype,
5929                                           itemkey  => itemkey,
5930                                           aname    => 'REQ_HEADER_ID');
5931    BEGIN
5932      SELECT   emergency_po_num
5933      INTO     x_emergency_po_num
5934      FROM     po_requisition_headers
5935      WHERE    requisition_header_id=x_req_header_id;
5936    EXCEPTION
5937      WHEN OTHERS THEN
5938 
5939      resultout := wf_engine.eng_completed || ':' ||  'N';
5940      x_progress:= '10: is_this_emergency_req: result = N';
5941      IF (g_po_wf_debug = 'Y') THEN
5942         po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
5943      END IF;
5944 
5945    END;
5946 
5947    IF x_emergency_po_num IS NOT NULL THEN
5948      resultout := wf_engine.eng_completed || ':' ||  'Y';
5949      x_progress:= '20: is_this_emergency_req: result = Y';
5950      IF (g_po_wf_debug = 'Y') THEN
5951         po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
5952      END IF;
5953 
5954    ELSE
5955      resultout := wf_engine.eng_completed || ':' ||  'N';
5956      x_progress:= '30: is_this_emergency_req: result = N';
5957      IF (g_po_wf_debug = 'Y') THEN
5958         po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
5959      END IF;
5960 
5961    END IF;
5962 
5963 exception
5964   when others then
5965     wf_core.context('po_autocreate_doc','is_this_emergency_req',x_progress);
5966     raise;
5967 end is_this_emergency_req;
5968 
5969 /***************************************************************************
5970  *
5971  *  Procedure:  put_on_one_po
5972  *
5973  *  Description:  Group all req lines into one po and
5974  *                      insert into the interface tables
5975  *      Remark:         This is for processing emergency requisitions
5976  *                      where only one PO number is reserved for each
5977  *                      requisition
5978  *
5979  **************************************************************************/
5980 procedure put_on_one_po(itemtype   IN   VARCHAR2,
5981                         itemkey    IN   VARCHAR2,
5982                         actid      IN   NUMBER,
5983                         funcmode   IN   VARCHAR2,
5984                         resultout  OUT NOCOPY  VARCHAR2 ) is
5985 
5986 c_rowid       rowid;
5987 c_group_id      number;
5988 c_req_header_id   number;
5989 c_req_line_id     number;
5990 c_suggested_buyer_id    number;
5991 c_source_doc_type_code    varchar2(25);
5992 c_source_doc_id   number;
5993 c_source_doc_line   number;
5994 c_suggested_vendor_id     number;
5995 c_suggested_vendor_site_id  number;
5996 c_currency_code   varchar2(15);
5997 c_rate_type     varchar2(30);
5998 c_rate_date     date;
5999 c_rate        number;
6000 c_process_code      varchar2(30);
6001 c_rel_gen_method    varchar2(25);
6002 c_item_id     number;
6003 c_pcard_id      number;
6004 c_contract_id     number;
6005 
6006 l_enable_vmi_flag       po_asl_attributes.enable_vmi_flag%TYPE;               -- Consigned FPI
6007 l_last_billing_date     po_asl_attributes.last_billing_date%TYPE;             -- Consigned FPI
6011 c_dest_type_code           po_requisition_lines.destination_type_code%TYPE;   -- Consigned FPI
6008 l_cons_billing_cycle    po_asl_attributes.consigned_billing_cycle%TYPE;       -- Consigned FPI
6009 
6010 c_dest_org_id              number;                                            -- Consigned FPI
6012 c_cons_from_supp_flag      varchar2(1);                                       -- Consigned FPI
6013 
6014 x_progress          varchar2(300);
6015 x_group_id      number;
6016 x_first_time_for_this_comb  varchar2(5);
6017 x_interface_header_id   number;
6018 x_suggested_vendor_contact_id   number;
6019 x_suggested_vendor_contact      varchar2(240);
6020 x_prev_sug_vendor_contact_id    number;
6021 x_carry_contact_to_po_flag      varchar2(10);
6022 
6023 l_return_status         varchar2(1)    := NULL;
6024 l_msg_count             number         := NULL;
6025 l_msg_data              varchar2(2000) := NULL;
6026 
6027 l_style_id            PO_DOC_STYLE_HEADERS.style_id%TYPE; --<R12 STYLES PHASE II>
6028 /* Define the cursor which picks up records from the temp table.
6029  * We need the 'for update' since we are going to update the
6030  * process_code.
6031  */
6032 
6033 /* Bug # 1721991.
6034    The 'for update' clause was added to update the row which was processed
6035    in the Cursor c1 but this led to another problem in Oracle 8.1.6.3 or above
6036    where you can't have a commit inside a 'for update' Cursor loop.
6037    This let to the Runtime Error 'fetch out of sequence'
6038    The commit was actually issued in the procedure insert_into_header_interface.
6039    To solve this we removed the for update in the cursor and instead used rowid
6040    to update the row processed by the Cursor.
6041 */
6042 
6043 cursor c1  is       /* x_group_id is a parameter */
6044   select prlt.rowid, -- Bug# 1721991, Added rowid to update row
6045          prlt.group_id,
6046          prlt.requisition_header_id,
6047          prlt.requisition_line_id,
6048    prlt.suggested_buyer_id,
6049          prlt.source_doc_type_code,
6050    prlt.source_doc_id,
6051    prlt.source_doc_line,
6052    prlt.suggested_vendor_id,
6053          prlt.suggested_vendor_site_id,
6054    prlt.currency_code,
6055          prlt.rate_type,
6056    prlt.rate_date,
6057    prlt.rate,
6058    prlt.process_code,
6059    prlt.release_generation_method,
6060    prlt.item_id,
6061    prlt.pcard_id,
6062          prlt.contract_id,
6063    prl.suggested_vendor_contact,
6064    prl.vendor_contact_id,
6065          prl.destination_organization_id,
6066          prl.destination_type_code
6067     from po_wf_candidate_req_lines_temp  prlt,
6068    po_requisition_lines prl
6069    where prlt.process_code = 'PENDING'
6070      and prlt.group_id     = x_group_id
6071      and prlt.requisition_header_id = prl.requisition_header_id
6072      and prlt.requisition_line_id = prl.requisition_line_id;
6073      --Bug # 1721991, for update;
6074 
6075     x_ga_flag VARCHAR2(1); --Bugfix#14305183
6076 
6077 begin
6078 
6079 
6080    /* Get the group_id since we only want to process lines belonging
6081     * to the same group. We need to get the group_id before opening
6082     * the cursor since it is a parameter to the cursor.
6083     */
6084    l_style_id := PO_DOC_STYLE_GRP.get_standard_doc_style;  --<R12 STYLES PHASE II>
6085 
6086    x_group_id := po_wf_util_pkg.GetItemAttrNumber (itemtype => itemtype,
6087                                               itemkey  => itemkey,
6088                                               aname    => 'GROUP_ID');
6089 
6090    x_progress := '10: put_on_one_po : group_id '||
6091                  to_char(x_group_id);
6092    IF (g_po_wf_debug = 'Y') THEN
6093       po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
6094    END IF;
6095 
6096 
6097    /* only true for the first line */
6098    x_first_time_for_this_comb := 'TRUE';
6099    x_suggested_vendor_contact_id := NULL;
6100    x_carry_contact_to_po_flag := 'TRUE';
6101 
6102    /* Open the cursor with that group_id */
6103    open c1;   /* Based on x_group_id */
6104    loop
6105       fetch c1 into c_rowid, --Bug# 1721991, Added rowid
6106                     c_group_id,
6107         c_req_header_id,
6108               c_req_line_id,
6109               c_suggested_buyer_id,
6110               c_source_doc_type_code,
6111               c_source_doc_id,
6112               c_source_doc_line,
6113               c_suggested_vendor_id,
6114               c_suggested_vendor_site_id,
6115               c_currency_code,
6116               c_rate_type,
6117               c_rate_date,
6118               c_rate,
6119               c_process_code,
6120               c_rel_gen_method,
6121               c_item_id,
6122         c_pcard_id,
6123                     c_contract_id,
6124         x_suggested_vendor_contact,
6125         x_suggested_vendor_contact_id,
6126                     c_dest_org_id,
6127                     c_dest_type_code;
6128         exit when c1%NOTFOUND;
6129 
6130      update po_wf_candidate_req_lines_temp
6131      set process_code = 'PROCESSED'
6132      where rowid=c_rowid;
6133      --Bug# 1721991, where current of c1;
6134 
6135      if (x_suggested_vendor_contact_id is null) then
6136   x_suggested_vendor_contact_id := get_contact_id(x_suggested_vendor_contact, c_suggested_vendor_site_id);
6137      end if;
6138 
6139    /* Bug#14305183: For emergency requisition, outcome document is always a
6140     * STANDARD purchase order.
6141     * Customer in bug13496442 had requested that, if the emergency requisition
6142     * creating into the PO, retains the source document reference on the PO.
6146     */
6143     * If source document is local Blanket. we should still create a Standard PO
6144     * without having the source document reference. Hence x_doc_type_to_create
6145     * should be STANDARD in this case.
6147 
6148     -- Bug#14305183: Start
6149      IF (c_source_doc_id is not null)
6150      THEN
6151        Begin
6152          select NVL(global_agreement_flag,'N')
6153            into x_ga_flag
6154            from po_headers_all
6155           where po_header_id = c_source_doc_id;
6156          Exception
6157            when others then
6158              x_ga_flag := 'N';
6159         End;
6160      END IF;
6161 
6162      IF (x_ga_flag = 'N')
6163      THEN
6164        c_source_doc_type_code := NULL;
6165        c_source_doc_line := NULL;
6166        c_source_doc_id := NULL;
6167      END IF;
6168 
6169      -- Bug#14305183: End
6170      /* Consigned FPI start */
6171         PO_THIRD_PARTY_STOCK_GRP.Get_Asl_Attributes
6172        ( p_api_version                  => 1.0
6173        , p_init_msg_list                => NULL
6174        , x_return_status                => l_return_status
6175        , x_msg_count                    => l_msg_count
6176        , x_msg_data                     => l_msg_data
6177        , p_inventory_item_id            => c_item_id
6178        , p_vendor_id                    => c_suggested_vendor_id
6179        , p_vendor_site_id               => c_suggested_vendor_site_id
6180        , p_using_organization_id        => c_dest_org_id
6181        , x_consigned_from_supplier_flag => c_cons_from_supp_flag
6182        , x_enable_vmi_flag              => l_enable_vmi_flag
6183        , x_last_billing_date            => l_last_billing_date
6184        , x_consigned_billing_cycle      => l_cons_billing_cycle
6185       );
6186 
6187        if nvl(c_dest_type_code,'INVENTORY') = 'EXPENSE' then
6188            c_cons_from_supp_flag := 'N';
6189        end if;
6190 
6191      /* Consigned FPI end */
6192 
6193      /** Bug 956730
6194       *  bgu, Aug. 11, 1999
6195       *  For Emergency Requisition, don't need to populate sourcing
6196       *  information.
6197       */
6198      if (x_first_time_for_this_comb  = 'TRUE') then
6199 
6200        if(po_autocreate_doc.insert_into_headers_interface
6201                (itemtype,
6202                 itemkey,
6203                 c_group_id,
6204                 c_suggested_vendor_id,
6205                 c_suggested_vendor_site_id,
6206                 c_suggested_buyer_id,
6207 		--Bug 13496442/14305183 , replace null values only if global BPA
6208 				c_source_doc_type_code,
6209                 c_source_doc_id,
6210                 --End bug 13496442
6211                 c_currency_code,
6212                 c_rate_type,
6213                 c_rate_date,
6214                 c_rate,
6215                 c_pcard_id,
6216                             l_style_id,  --<R12 STYLES PHASE II>
6217                 x_interface_header_id) = FALSE) then
6218                     return; --bug 3401653: po creation failed
6219         end if;
6220 
6221 
6222 
6223 
6224         po_autocreate_doc.insert_into_lines_interface (itemtype,
6225                 itemkey,
6226                 x_interface_header_id,
6227                 c_req_line_id,
6228                 --Bug 13496442, replace null values
6229 				c_source_doc_line,
6230 				c_source_doc_type_code,
6231                 null,
6232                 c_source_doc_id,
6233 				c_cons_from_supp_flag);        -- Consigned FPI
6234 
6235         x_progress := '20: put_on_one_po: inserted header'||
6236                 ' and line for req line = ' ||
6237                       to_char(c_req_line_id);
6238   IF (g_po_wf_debug = 'Y') THEN
6239     po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
6240   END IF;
6241 
6242         x_first_time_for_this_comb := 'FALSE';
6243 
6244   if (x_suggested_vendor_contact_id is NULL) then
6245     x_carry_contact_to_po_flag := 'FALSE';
6246   end if;
6247 
6248   x_prev_sug_vendor_contact_id := x_suggested_vendor_contact_id;
6249 
6250      else  /*  ie. x_first_time_for_this_comb  = FALSE */
6251 
6252         /* The line will be put onto the same header
6253          * as a previous one, so only insert a new line into the
6254          * po_lines_interface table.
6255          */
6256 
6257         po_autocreate_doc.insert_into_lines_interface (itemtype,
6258                 itemkey,
6259                 x_interface_header_id,
6260                 c_req_line_id,
6261                 --Bug 13496442, replace null values
6262 				c_source_doc_line,
6263 				c_source_doc_type_code,
6264                 null,
6265                 c_source_doc_id,
6266 				--End bug 13496442
6267                 c_cons_from_supp_flag);   -- Consigned FPI
6268 
6269 
6270   if (x_carry_contact_to_po_flag = 'TRUE' and x_suggested_vendor_contact_id is not null) then
6271     if (x_suggested_vendor_contact_id <> x_prev_sug_vendor_contact_id) then
6272       x_carry_contact_to_po_flag := 'FALSE';
6273     end if;
6274    else
6275     x_carry_contact_to_po_flag := 'FALSE';
6276    end if;
6277 
6278          x_progress := '30: put_on_one_po: inserted just line for '||
6279              'req line = ' || to_char(c_req_line_id);
6280    IF (g_po_wf_debug = 'Y') THEN
6281      po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
6282    END IF;
6283 
6284      end if;
6285 
6286      x_prev_sug_vendor_contact_id := x_suggested_vendor_contact_id;
6287 
6288    end loop;
6289    close c1;
6290 
6291    if (x_carry_contact_to_po_flag = 'TRUE' and
6292        valid_contact(c_suggested_vendor_site_id, x_suggested_vendor_contact_id)) then
6293             begin
6294                     x_progress := '55: group_req_lines: updating header with vendor contact :'||x_interface_header_id;
6295 
6296                     update po_headers_interface
6297                     set vendor_contact_id = x_suggested_vendor_contact_id
6298                     where interface_header_id = x_interface_header_id;
6299 
6300             exception
6301                     when others then
6302                     IF (g_po_wf_debug = 'Y') THEN
6303                        po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
6304                     END IF;
6305       end;
6306    end if;
6307 
6308 
6309    resultout := wf_engine.eng_completed || ':' ||  'ACTIVITY_PERFORMED';
6310 
6311    x_progress := '40: put_on_one_po: result = ACTIVITY_PERFORMED ';
6312    IF (g_po_wf_debug = 'Y') THEN
6313       po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
6314    END IF;
6315 
6316 exception
6317   when others then
6318     close c1;
6319     wf_core.context('po_autocreate_doc','put_on_one_po',x_progress);
6320     raise;
6321 end put_on_one_po;
6322 
6323 /***************************************************************************
6324  *
6325  *  Procedure:  send_return_notif
6326  *
6327  *  Description:  Send notification to preparer when the requisition
6328  *                      is returned.
6329  *
6330  **************************************************************************/
6331 
6332 /* Bug# 1694064: kagarwal
6333 ** Desc: Calling Req Approval wf to send return Notification
6334 ** instead of the wf API.
6335 */
6336 
6337 procedure send_return_notif(p_req_header_id IN number,
6338                             p_agent_id      IN number,
6339                             p_reason        IN VARCHAR2) is
6340 
6341   l_doc_type           varchar2(240); /* Bug# 2681512: kagarwal */
6342   --Bug# 3268971: sbgeorge
6343   l_doc_type_code      varchar2(80);
6344   l_doc_subtype        varchar2(80);
6345   doc_subtype          varchar2(25);
6346 
6347   l_req_num            varchar2(40);
6348   --l_agent_name         varchar2(240);--<BUG 7650916>
6349   l_preparer_id        number;
6350   l_preparer_disp_name varchar2(240);
6351   l_preparer_user_name varchar2(200);
6352 
6353   l_nid                number;
6354   l_seq                varchar2(25);  --Bug14305923
6355   ItemType             varchar2(8);
6356   ItemKey              varchar2(240);
6357 
6358   l_update_req_url     varchar2(1000);
6359   l_open_req_url       varchar2(1000);
6360   l_resubmit_req_url   varchar2(1000);
6361 
6362   l_org_id             number;
6363 
6364   x_progress           varchar2(300);
6365 
6366   -- bug 5657496 variable addition <START>
6367   l_responsibility_id  NUMBER;
6368   l_user_id            NUMBER;
6369   l_application_id     NUMBER;
6370   -- bug 5657496 variable addition <END>
6371   --<BUG 7650916 START>
6372   l_approver_disp_name per_people_f.full_name%TYPE;
6373   l_approver_user_name fnd_user.user_name%TYPE;
6374   --<BUG 7650916 END>
6375 
6376 begin
6377 
6378      x_progress :=  'PO_AUTOCREATE_DOC.send_return_notif: 001';
6379 
6380 /* Bug# 2681512: kagarwal
6381 ** We will get the document type display value from
6382 ** po document types for correct translation.
6383 */
6384 /*
6385     select st.DISPLAYED_FIELD,
6386            ty.DISPLAYED_FIELD,
6387            hd.SEGMENT1,
6388            hd.ORG_ID,
6389            hd.PREPARER_ID,
6390            hd.TYPE_LOOKUP_CODE
6391       into l_doc_subtype,
6392            l_doc_type,
6393            l_req_num,
6394            l_org_id,
6395            l_preparer_id,
6396            doc_subtype
6397       from po_requisition_headers hd,
6398            po_lookup_codes ty,
6399            po_lookup_codes st
6400      where hd.requisition_header_id = p_req_header_id
6401        and ty.lookup_type = 'DOCUMENT TYPE'
6402        and ty.lookup_code = 'REQUISITION'
6403        and st.lookup_type = 'REQUISITION TYPE'
6404        and st.lookup_code = hd.TYPE_LOOKUP_CODE;
6405 */
6406 
6407     select ty.DOCUMENT_TYPE_CODE,
6408            ty.DOCUMENT_SUBTYPE,
6409            ty.type_name,
6410            nvl2(hd.par_draft_id,
6411                 pod.modification_number,
6412                 hd.segment1)  SEGMENT1,
6413            hd.ORG_ID,
6414            hd.PREPARER_ID,
6415            hd.TYPE_LOOKUP_CODE,
6416            ty.wf_approval_itemtype
6417       into l_doc_type_code,
6418            l_doc_subtype,
6419            l_doc_type,
6420            l_req_num,
6421            l_org_id,
6422            l_preparer_id,
6423            doc_subtype,
6424            ItemType
6425       from po_requisition_headers hd,
6426            po_document_types ty ,
6427            po_drafts pod
6428      where hd.requisition_header_id = p_req_header_id
6429        and ty.document_type_code = 'REQUISITION'
6430        and ty.document_subtype = hd.TYPE_LOOKUP_CODE
6431        AND hd.par_draft_id = pod.draft_id(+);
6432 
6433 
6434      -- Get Req Approval process.
6435 
6436      x_progress :=  'PO_AUTOCREATE_DOC.send_return_notif: 010';
6437 
6438 /* Bug# 2681512: kagarwal
6439 ** Getting the wf_item_type in the SQL above
6440 
6441      begin
6442 
6443      select wf_approval_itemtype
6444        into ItemType
6445        from PO_DOCUMENT_TYPES
6446       where DOCUMENT_TYPE_CODE = 'REQUISITION'
6447         and DOCUMENT_SUBTYPE =  doc_subtype;
6448 
6449      exception
6450            when others then
6451            null;
6452      end;
6453 */
6454 
6455      IF ItemType IS NULL THEN
6456         x_progress :=  'PO_AUTOCREATE_DOC.send_return_notif: 020';
6457         return;
6458      END IF;
6459 
6460      -- Build the links.
6461 
6462      l_open_req_url := por_util_pkg.jumpIntoFunction(
6463              p_application_id        => 178,
6464              p_function_code         => 'POR_OPEN_REQ',
6465              p_parameter1            => to_char(p_req_header_id),
6466              p_parameter11           => to_char(l_org_id) );
6467 
6468      l_update_req_url := por_util_pkg.jumpIntoFunction(
6469                      p_application_id=> 178,
6470                      p_function_code => 'POR_UPDATE_REQ',
6471                      p_parameter1    => to_char(p_req_header_id),
6472                      p_parameter11   => to_char(l_org_id) );
6473 
6474      l_resubmit_req_url := por_util_pkg.jumpIntoFunction(
6475                      p_application_id=> 178,
6476                      p_function_code => 'POR_RESUBMIT_URL',
6477                      p_parameter1    => to_char(p_req_header_id),
6478                      p_parameter11   => to_char(l_org_id) );
6479 
6480      PO_REQAPPROVAL_INIT1.get_user_name(l_preparer_id, l_preparer_user_name,
6481                                         l_preparer_disp_name);
6482      --<BUG 7650916 commented the following code since the same as been performed later>
6483      /*select pr.FULL_NAME
6484        into l_agent_name
6485        from per_people_f pr
6486       where pr.person_id = p_agent_id
6487         and trunc(sysdate) between pr.effective_start_date
6488                                and pr.effective_end_date;*/
6489 
6490      -- Create wf process.
6491 
6492      x_progress :=  'PO_AUTOCREATE_DOC.send_return_notif: 040';
6493 
6494      select to_char(PO_WF_ITEMKEY_S.NEXTVAL) into l_seq from sys.dual;
6495      ItemKey := to_char(p_req_header_id) || '-' || l_seq;
6496 
6497      wf_engine.CreateProcess( ItemType => ItemType,
6498                               ItemKey  => ItemKey,
6499                               process  => 'NOTIFY_RETURN_REQ');
6500 
6501      x_progress :=  'PO_AUTOCREATE_DOC.send_return_notif: 050 - '||
6502                      'itemtype: ' || ItemType || 'itemkey: ' || ItemKey;
6503      IF (g_po_wf_debug = 'Y') THEN
6504         /* DEBUG */  PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,x_progress);
6505      END IF;
6506 
6507      -- Set the attributes
6508      po_wf_util_pkg.SetItemAttrText ( itemtype   => itemtype,
6509                                  itemkey    => itemkey,
6510                                  aname      => 'DOCUMENT_NUMBER',
6511                                  avalue     =>  l_req_num);
6512      --
6513      po_wf_util_pkg.SetItemAttrNumber ( itemtype   => itemtype,
6514                                    itemkey    => itemkey,
6515                                    aname      => 'DOCUMENT_ID',
6516                                    avalue     => p_req_header_id);
6517      --
6518      po_wf_util_pkg.SetItemAttrText ( itemtype        => itemtype,
6519                                  itemkey         => itemkey,
6520                                  aname           => 'DOCUMENT_TYPE_DISP',
6521                                  avalue          =>  l_doc_type);
6522      --
6523      -- Bug 2942228. The org id was getting changed from 458 to 204 when requisition was
6524      --     returned in Vision Services because it is not set as Workflow Attribute here.
6525      po_wf_util_pkg.SetItemAttrNumber ( itemtype        => itemtype,
6526                                  itemkey         => itemkey,
6527                                  aname           => 'ORG_ID',
6528                                  avalue          =>  l_org_id);
6529 
6530 /* Bug# 2681512: kagarwal
6531 ** Desc: We will only be using one display attribute for type and
6532 ** subtype - DOCUMENT_TYPE_DISP, hence commenting the code below
6533 */
6534 /*
6535      po_wf_util_pkg.SetItemAttrText ( itemtype        => itemtype,
6536                                  itemkey         => itemkey,
6537                                  aname           => 'DOCUMENT_SUBTYPE_DISP',
6538                                  avalue          =>  l_doc_subtype);
6539      --
6540 */
6541 /* Bug# 3268971: sbgeorge
6542 ** Need to call PO_REQAPPROVAL_INIT1.Get_Req_Attributes after start in
6543 ** NOTIFY_RETURN_REQ process, since the notification needs to be similar to
6544 ** other Req Approval Notifications.
6545 ** Setting the item attributes for DOCUMENT_TYPE and DOCUMENT_SUBTYPE with
6546 ** document_type_code and document_subtype from po_document_types, because
6547 ** these are used in PO_REQAPPROVAL_INIT1.SetReqHdrAttributes to set the item
6548 ** attribute for DOCUMENT_TYPE_DISP.
6549 */
6550      po_wf_util_pkg.SetItemAttrText ( itemtype        => itemtype,
6551                                  itemkey         => itemkey,
6552                                  aname           => 'DOCUMENT_TYPE',
6553                                  avalue          =>  l_doc_type_code);
6554      --
6555      po_wf_util_pkg.SetItemAttrText ( itemtype        => itemtype,
6556                                  itemkey         => itemkey,
6557                                  aname           => 'DOCUMENT_SUBTYPE',
6558                                  avalue          =>  l_doc_subtype);
6559      --
6560 
6561      po_wf_util_pkg.SetItemAttrText ( itemtype        => itemtype,
6562                                  itemkey         => itemkey,
6563                                  aname           => 'NOTE',
6564                                  avalue          =>  p_reason);
6565      --
6566      po_wf_util_pkg.SetItemAttrText ( itemtype   => itemType,
6567                                  itemkey    => itemkey,
6568                                  aname      => 'PREPARER_USER_NAME' ,
6569                                  avalue     => l_preparer_user_name);
6570 
6571      po_wf_util_pkg.SetItemAttrText ( itemtype   => itemType,
6572                                  itemkey    => itemkey,
6573                                  aname      => 'PREPARER_DISPLAY_NAME' ,
6574                                  avalue     => l_preparer_disp_name);
6575 
6576      --<BUG 7650916 Fetching the Buyer username and display name and
6577      -- setting the values in corresponding workflow attributes.
6578      PO_REQAPPROVAL_INIT1.get_user_name(p_agent_id, l_approver_user_name,
6579                                         l_approver_disp_name);
6580 
6581      po_wf_util_pkg.SetItemAttrText ( itemtype   => itemtype,
6582                                  itemkey    => itemkey,
6583                                  aname      => 'APPROVER_DISPLAY_NAME' ,
6584                                  avalue     => l_approver_disp_name);
6585 
6586      po_wf_util_pkg.SetItemAttrText ( itemtype   => itemtype,
6587                                  itemkey    => itemkey,
6588                                  aname      => 'APPROVER_USER_NAME' ,
6589                                  avalue     => l_approver_user_name);
6590 
6591      po_wf_util_pkg.SetItemAttrText ( itemtype   => itemtype,
6592                                  itemkey    => itemkey,
6593                                  aname      => 'REQ_URL' ,
6594                                  avalue     => l_open_req_url);
6595 
6596      po_wf_util_pkg.SetItemAttrText ( itemtype   => itemtype,
6597                                  itemkey    => itemkey,
6598                                  aname      => 'REQ_UPDATE_URL' ,
6599                                  avalue     => l_update_req_url);
6600 
6601      po_wf_util_pkg.SetItemAttrText ( itemtype   => itemtype,
6602                                  itemkey    => itemkey,
6603                                  aname      => 'REQ_RESUBMIT_URL' ,
6604                                  avalue     => l_resubmit_req_url);
6605 
6606     -- bug 5657496 <START>
6607     -- Need to set the context variables also, else the selector function in
6608     -- the workflow will set a null context.
6609     l_user_id := FND_GLOBAL.user_id;
6610     l_responsibility_id := FND_GLOBAL.resp_id;
6611     l_application_id := FND_GLOBAL.resp_appl_id;
6612           wf_engine.SetItemAttrNumber ( itemtype        => itemtype,
6613                                      itemkey         => itemkey,
6614                                      aname           => 'USER_ID',
6615                                      avalue          =>  l_user_id);
6616 
6617        wf_engine.SetItemAttrNumber ( itemtype        => itemtype,
6618                                      itemkey         => itemkey,
6619                                      aname           => 'APPLICATION_ID',
6620                                      avalue          =>  l_application_id);
6621 
6622        wf_engine.SetItemAttrNumber ( itemtype        => itemtype,
6623                                      itemkey         => itemkey,
6624                                      aname           => 'RESPONSIBILITY_ID',
6625                                      avalue          =>  l_responsibility_id);
6626     -- bug 5657496 <END>
6627 
6628    -- Start Process
6629     x_progress :=  'PO_AUTOCREATE_DOC.send_return_notif: 055';
6630     IF (g_po_wf_debug = 'Y') THEN
6631        /* DEBUG */  PO_WF_DEBUG_PKG.insert_debug(itemtype,itemkey,x_progress);
6632     END IF;
6633 
6634     wf_engine.StartProcess(itemtype        => itemtype,
6635                            itemkey         => itemkey );
6636 
6637 
6638 EXCEPTION
6639  WHEN OTHERS THEN
6640    po_message_s.sql_error('Error: send_return_notif()', x_progress, sqlcode);
6641    RAISE;
6642 
6643 end send_return_notif;
6644 
6645 /***************************************************************************
6646  *
6647  *      function:       get_document_num
6648  *
6649  *      Description:    get the next document number in the OU specified
6650  *                      from po_unique_identifier_cont_all table
6651  *
6652  **************************************************************************/
6653 
6654 FUNCTION  get_document_num (
6655   p_purchasing_org_id IN NUMBER --<Shared Proc FPJ>
6656 ) RETURN VARCHAR2
6657 IS
6658 
6659 -- Bug # 1869409
6660 -- Created a function get_document_num as an  autonomous transaction
6661 -- to avoid the COMMIT for the Workflow transactions.
6662 
6663 -- bug5176308
6664 -- No need to be an autonomous transaction as the number generation is now
6665 -- through an API which itself is an autonomous transaction.
6666 
6667 --  pragma AUTONOMOUS_TRANSACTION;
6668 
6669   x_document_num varchar2(25);
6670   x_progress    varchar2(300);
6671 
6672 begin
6673 
6674   x_progress := '10: get_document_num: Just before get doc' ||
6675                  'num from po_unique_identifier_control';
6676 
6677   -- bug5176308
6678   -- Call API to get the next document num
6679   x_document_num :=
6680     PO_CORE_SV1.default_po_unique_identifier
6681     ( p_table_name => 'PO_HEADERS',
6682       p_org_id     => p_purchasing_org_id
6683     );
6684 
6685 
6686   x_progress := '20: get_document_num: Just after get doc' ||
6687                  'num from po_unique_identifier_control';
6688 
6689   return x_document_num;
6690 
6691 exception
6692   when others then
6693    wf_core.context('po_autocreate_doc','get_document_num',x_progress);
6694    raise;
6695 
6696 end get_document_num;
6697 
6698 /***************************************************************************
6699  *
6700  *      function:       is_contract_required_on_req_line
6701  *
6702  *      Description:    check the worlflow options for the autocreate po
6703  *                      document.
6704  *
6705  **************************************************************************/
6706 
6707 procedure is_contract_required_on_req(itemtype   IN   VARCHAR2,
6708                         itemkey    IN   VARCHAR2,
6709                         actid      IN   NUMBER,
6710                         funcmode   IN   VARCHAR2,
6711                         resultout  OUT NOCOPY  VARCHAR2 ) is
6712 
6713 x_contract_required_flag varchar2(1);
6714 
6715 x_progress                varchar2(300) := '000';
6716 
6717 begin
6718 
6719    x_contract_required_flag := po_wf_util_pkg.GetItemAttrText
6720           (itemtype   => itemtype,
6721                                        itemkey    => itemkey,
6722                                        aname      => 'CONTRACT_REQUIRED_FLAG');
6723 
6724    if nvl(x_contract_required_flag, 'N') <> 'Y' then
6725 
6726         resultout := wf_engine.eng_completed || ':' ||  'N';
6727 
6728         return;
6729    else
6730         resultout:= wf_engine.eng_completed || ':' || 'Y';
6731 
6732   return;
6733 
6734    end if;
6735 
6736 exception
6737 
6738   when others then
6739     wf_core.context('po_autocreate_doc','is_contract_required_on_req',x_progress);
6740     raise;
6741 
6742 end is_contract_required_on_req;
6743 
6744 /***************************************************************************
6745  *
6746  *      function:       should_contract_be_used
6747  *
6748  *      Description:    check whether contract be used to create document
6749  *
6750  **************************************************************************/
6751  procedure should_contract_be_used(itemtype   IN   VARCHAR2,
6752                         itemkey    IN   VARCHAR2,
6753                         actid      IN   NUMBER,
6754                         funcmode   IN   VARCHAR2,
6755          resultout  OUT NOCOPY  VARCHAR2 ) is
6756 x_use_contract_flag varchar2(1);
6757 
6758 x_progress                varchar2(300) := '000';
6759 
6760 begin
6761 
6762    x_use_contract_flag := po_wf_util_pkg.GetItemAttrText
6763           (itemtype   => itemtype,
6764                                        itemkey    => itemkey,
6765                                        aname      => 'USE_CONTRACT_FLAG');
6766 
6767    if nvl(x_use_contract_flag, 'N') <> 'Y' then
6768 
6769         resultout := wf_engine.eng_completed || ':' ||  'N';
6770 
6771         return;
6772    else
6773         resultout:= wf_engine.eng_completed || ':' || 'Y';
6774 
6775   return;
6776 
6777    end if;
6778 
6779 exception
6780 
6781   when others then
6782     wf_core.context('po_autocreate_doc','should_contract_be_used',x_progress);
6783     raise;
6784 
6785 end should_contract_be_used;
6786 
6787 /***************************************************************************
6788  *
6789  *  Procedure:  non_catalog_item_check
6790  *
6791  *  Description:  Checks if this is a non_catalog_item  req line
6792  *      (ie. non_catalog item)
6793  *
6794  **************************************************************************/
6795 
6796 procedure non_catalog_item_check (itemtype   IN   VARCHAR2,
6797                                   itemkey    IN   VARCHAR2,
6798                                   actid      IN   NUMBER,
6799                                   funcmode   IN   VARCHAR2,
6800                                   resultout  OUT NOCOPY  VARCHAR2 ) is
6801 
6802 x_item_id   number;
6803 x_progress        varchar2(300);
6804 x_catalog_type    varchar2(30);
6805 
6806 begin
6810                                             aname    => 'CATALOG_TYPE');
6807 
6808   x_catalog_type := po_wf_util_pkg.GetItemAttrText (itemtype => itemtype,
6809                                             itemkey  => itemkey,
6811 
6812   if nvl(x_catalog_type, 'CATALOG') <> 'NONCATALOG' then
6813 
6814     resultout := wf_engine.eng_completed || ':' || 'N';
6815 
6816   else
6817     resultout := wf_engine.eng_completed || ':' || 'Y';
6818 
6819     x_progress:= '10: non_catalog_item_check: result = '|| resultout;
6820     IF (g_po_wf_debug = 'Y') THEN
6821        po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
6822     END IF;
6823 
6824   end if;
6825 
6826 
6827 exception
6828   when others then
6829     wf_core.context('po_autocreate_doc','non_catalog_item_check',x_progress);
6830     raise;
6831 end non_catalog_item_check;
6832 
6833 
6834 /***************************************************************************
6835  *
6836  *      function:       is_contract_info_ok
6837  *
6838  *      Description:    check the source contract number is okay
6839  *
6840  *
6841  **************************************************************************/
6842 procedure is_contract_doc_info_ok(itemtype   IN   VARCHAR2,
6843                         itemkey    IN   VARCHAR2,
6844                         actid      IN   NUMBER,
6845                         funcmode   IN   VARCHAR2,
6846               resultout  OUT NOCOPY  VARCHAR2 ) is
6847 
6848 x_source_doc_type_code    varchar2(25);
6849 x_source_doc_po_header_id number;
6850 x_source_doc_line_num   number;
6851 x_progress                varchar2(300) := '000';
6852 x_contract_id_valid   number;
6853 x_contract_currency_code        varchar2(25);
6854 x_source_currency_code          PO_REQUISITION_LINES_ALL.currency_code%TYPE;
6855 l_vendor_site_id                PO_HEADERS_ALL.vendor_site_id%TYPE; -- <GC FPJ>
6856 l_base_currency                 PO_HEADERS_ALL.currency_code%TYPE;
6857 x_is_clm_flow VARCHAR2(1);
6858 begin
6859 
6860       /* When the source doc and source line was put onto the req line
6861        * it was all validated to make sure it was ok.
6862        * Ie. docs were within effectivity dates, not canelled or closed etc.
6863        * So not doing the check here again.
6864        * We just need to make sure the source_doc_type,  source_doc  and
6865        * source_line have been populated.
6866        * here it will just validate the contract docuement type.
6867        */
6868 
6869        x_source_doc_type_code := po_wf_util_pkg.GetItemAttrText
6870             (itemtype   => itemtype,
6871                                    itemkey    => itemkey,
6872                                      aname      => 'SOURCE_DOCUMENT_TYPE_CODE');
6873 
6874        x_source_doc_po_header_id := po_wf_util_pkg.GetItemAttrNumber
6875           (itemtype   => itemtype,
6876                                        itemkey    => itemkey,
6877                                        aname      => 'SOURCE_DOCUMENT_ID');
6878 
6879        x_source_doc_line_num := po_wf_util_pkg.GetItemAttrNumber
6880           (itemtype   => itemtype,
6881                                        itemkey    => itemkey,
6882                                        aname      => 'SOURCE_DOCUMENT_LINE_NUM');
6883        x_source_currency_code := po_wf_util_pkg.GetItemAttrText
6884           (itemtype   => itemtype,
6885                                        itemkey    => itemkey,
6886                                        aname      => 'CURRENCY_CODE');
6887 
6888        -- <GC FPJ START>
6889        l_vendor_site_id := po_wf_util_pkg.GetItemAttrNumber
6890                                     (itemtype   => itemtype,
6891                                      itemkey    => itemkey,
6892                                      aname      => 'SUGGESTED_VENDOR_SITE_ID');
6893        -- <GC FPJ END>
6894 
6895        x_is_clm_flow := po_wf_util_pkg.GetItemAttrText
6896                                      (itemtype   => itemtype,
6897                                        itemkey    => itemkey,
6898                                        aname      => 'IS_CLM_FLOW');
6899 
6900      if ((x_source_doc_line_num is NULL) and (x_source_doc_type_code = 'CONTRACT') and x_source_doc_po_header_id is not NULL) then
6901 
6902     --<Bug 14314684 Create Doc WF Project START>
6903     --Check if CLM req is referring a commercial contract
6904     IF (NVL(x_is_clm_flow,'N')= 'Y') THEN
6905 	 IF NOT (is_doc_clm_enabled(x_source_doc_po_header_id))  THEN
6906 		resultout := wf_engine.eng_completed || ':' ||  'N';
6907          	return;
6908         END IF;
6909     END IF;
6910     --<Bug 14314684 Create Doc WF Project END>
6911 
6912     -- validate the contract reference is active, bug 2076945
6913 
6914      begin
6915 
6916      x_progress  := '001';
6917 
6918      -- <GC FPJ START>
6919 
6920      -- SQL What: Validate the contract ref. If it is a GC, make sure
6921      --           that it is still enabled for purchasing against the suggested
6922      --           vendor site
6923      -- SQL Why: The reference cannot be used if it is not valid
6924 
6925      select POH.po_header_id, poh.currency_code
6926        into x_contract_id_valid, x_contract_currency_code
6927        from po_headers_all POH        -- <GC FPJ>: Use ALL table
6928       where
6929         POH.po_header_id = x_source_doc_po_header_id
6930         and POH.type_lookup_code = 'CONTRACT'
6931         and nvl(POH.cancel_flag,'N') = 'N'
6932         and TRUNC(sysdate) between nvl(TRUNC(start_date), sysdate - 1)
6933                            and     nvl(TRUNC(end_date), sysdate + 1)
6934         and POH.authorization_status = 'APPROVED'
6935         and nvl(POH.closed_code,'OPEN') = 'OPEN'
6936         AND NVL(POH.frozen_flag, 'N') = 'N'
6940                                po_system_parameters  PSP
6937         AND (NVL(POH.global_agreement_flag, 'N') = 'N'
6938              OR EXISTS (SELECT 1
6939                         FROM   po_ga_org_assignments PGOA,
6941                         WHERE  PGOA.po_header_id = POH.po_header_id
6942                         AND    PGOA.organization_id = PSP.org_id
6943                         AND    PGOA.vendor_site_id = decode(Nvl(poh.Enable_All_Sites,'N'),'Y',PGOA.vendor_site_id, l_vendor_site_id)	-- Bug#14157987
6944                         AND    PGOA.enabled_flag = 'Y'));
6945      -- <GC FPJ END>
6946 
6947    exception
6948 
6949      when others then
6950        x_contract_id_valid := -1;
6951 
6952      end;
6953 
6954      if (x_contract_id_valid =  -1 ) then
6955 
6956         resultout := wf_engine.eng_completed || ':' ||  'N';
6957 
6958      elsif (x_contract_id_valid = x_source_doc_po_header_id) then
6959         -- bug 3079146
6960         -- if currency didn't match, don't create po
6961         -- so set the flag to true so that po won't be created.
6962         l_base_currency := PO_CORE_S2.get_base_currency;
6963 
6964         if ((x_source_currency_code is not null and x_contract_currency_code = x_source_currency_code) or (x_source_currency_code is null and x_contract_currency_code = l_base_currency)) then
6965 
6966            resultout := wf_engine.eng_completed || ':' ||  'Y';
6967 
6968   else
6969      resultout := wf_engine.eng_completed || ':' || 'N';
6970             po_wf_util_pkg.SetItemAttrText (itemtype   => itemtype,
6971                                itemkey    => itemkey,
6972                                aname      => 'CONTRACT_REQUIRED_FLAG',
6973                                avalue     => 'Y');
6974   end if;
6975 
6976      else
6977 
6978     resultout := wf_engine.eng_completed || ':' || 'N';
6979 
6980      end if;
6981 
6982         x_progress:= '10: is_source_doc_info_ok: result = ' || resultout;
6983         IF (g_po_wf_debug = 'Y') THEN
6984            po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
6985         END IF;
6986 
6987         po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
6988                                itemkey    => itemkey,
6989                                aname      => 'CONTRACT_ID',
6990                                avalue     => x_source_doc_po_header_id);
6991 
6992         -- <GC FPJ START>
6993         -- Since the ref is a contract and is stored in attr CONTRACT_ID,
6994         -- null out the reference in SOURCE_DOCUMENT_ID
6995 
6996         po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
6997                                itemkey    => itemkey,
6998                                aname      => 'SOURCE_DOCUMENT_ID',
6999                                avalue     => NULL);
7000 
7001         -- <GC FPJ END>
7002      else
7003         resultout := wf_engine.eng_completed || ':' ||  'N';
7004 
7005         x_progress:= '20: is_contract_info_ok: result = N';
7006         IF (g_po_wf_debug = 'Y') THEN
7007            po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
7008         END IF;
7009 
7010      end if;
7011 
7012 exception
7013   when others then
7014     wf_core.context('po_autocreate_doc','is_contract_doc_info_ok',x_progress);
7015     raise;
7016 
7017 end is_contract_doc_info_ok;
7018 
7019  /***************************************************************************
7020  *
7021  *      function:       should_nctlog_src_frm_contract
7022  *
7023  *      Description:    check the workflow options on whether to include the
7024  *                      non_catalog_request in the autosource
7025  *
7026  *
7027  **************************************************************************/
7028 
7029 procedure should_nctlog_src_frm_contract(itemtype   IN   VARCHAR2,
7030                         itemkey    IN   VARCHAR2,
7031                         actid      IN   NUMBER,
7032                         funcmode   IN   VARCHAR2,
7033           resultout  OUT NOCOPY  VARCHAR2 ) is
7034 
7035 x_incl_non_ctlg_req_flag varchar2(1);
7036 
7037 x_progress                varchar2(300) := '000';
7038 
7039 begin
7040 
7041 
7042    x_incl_non_ctlg_req_flag := po_wf_util_pkg.GetItemAttrText
7043           (itemtype   => itemtype,
7044                                        itemkey    => itemkey,
7045                                        aname      => 'INCLUDE_NON_CATALOG_REQ_FLAG');
7046 
7047 
7048    if nvl(x_incl_non_ctlg_req_flag, 'N') <> 'Y' then
7049 
7050         resultout := wf_engine.eng_completed || ':' ||  'N';
7051 
7052         return;
7053    else
7054         resultout:= wf_engine.eng_completed || ':' || 'Y';
7055 
7056   return;
7057 
7058    end if;
7059 
7060 exception
7061 
7062   when others then
7063     wf_core.context('po_autocreate_doc','should_nctlog_src_frm_cntrct',x_progress);
7064     raise;
7065 
7066 end should_nctlog_src_frm_contract;
7067 
7068 /* Private Procedure/Functions */
7069 
7070 FUNCTION valid_contact(p_vendor_site_id number, p_vendor_contact_id number) RETURN BOOLEAN
7071 is
7072    x_count number;
7073 begin
7074   if (p_vendor_site_id is null or p_vendor_contact_id is null) then
7075     return false;
7076   else
7077     -- check if contact on req. lines is valid
7078     select count(*) into x_count
7079     from po_vendor_contacts
7080     where vendor_site_id = p_vendor_site_id
7081     and vendor_contact_id = p_vendor_contact_id
7082     and nvl(inactive_date, sysdate+1) > sysdate;
7083 
7084     if (x_count > 0) then
7085       return true;
7086     else
7087       return false;
7088     end if;
7089   end if;
7090 end;
7091 
7092 FUNCTION get_contact_id(p_contact_name varchar2, p_vendor_site_id number) RETURN NUMBER
7093 IS
7094      x_first_name varchar2(60);
7095      x_last_name  varchar2(60);
7096      x_comma_pos  number;
7097      x_contact_id number := null;
7098 BEGIN
7099 
7100   begin
7101     select max(vendor_contact_id)
7102     into x_contact_id
7103     from po_supplier_contacts_val_v
7104     where vendor_site_id = p_vendor_site_id
7105     and contact = p_contact_name;
7106   exception
7107     when others then
7108     x_contact_id := null;
7109   end;
7110 
7111   return x_contact_id;
7112 END;
7113 
7114 /***************************************************************************
7115  *
7116  *  Procedure:  is_src_doc_ga_frm_other_ou
7117  *
7118  *  Description:  Checks if the source doc is a GA from another OU
7119  *                      Added for global Agreements project in FPI (FPI GA)
7120  *
7121  **************************************************************************/
7122 procedure is_src_doc_ga_frm_other_ou (itemtype   IN   VARCHAR2,
7123                                   itemkey    IN   VARCHAR2,
7124                                   actid      IN   NUMBER,
7125                                   funcmode   IN   VARCHAR2,
7126                                   resultout  OUT NOCOPY  VARCHAR2 ) is
7127 
7128 x_org_id      number;
7129 x_owning_org_id     number;
7130 x_progress          varchar2(300);
7131 x_ga_flag                 varchar2(1);
7132 x_source_doc_po_header_id number;
7133 
7134 begin
7135 
7136   /* Set org context */
7137   x_org_id := po_wf_util_pkg.GetItemAttrNumber (itemtype => itemtype,
7138                                                itemkey  => itemkey,
7139                                                aname    => 'ORG_ID');
7140 
7141   po_moac_utils_pvt.set_org_context(x_org_id); --<R12 MOAC>
7142 
7143   x_source_doc_po_header_id := po_wf_util_pkg.GetItemAttrNumber
7144           (itemtype   => itemtype,
7145                                        itemkey    => itemkey,
7146                                        aname      => 'SOURCE_DOCUMENT_ID');
7147 
7148   x_ga_flag := po_wf_util_pkg.GetItemAttrText
7149            (itemtype => itemtype,
7150                                          itemkey  => itemkey,
7151                                          aname    => 'SOURCE_DOC_GA_FLAG');
7152 
7153   /* get the owning org of the source document */
7154 
7155    if x_source_doc_po_header_id is not null then
7156      select org_id
7157      into x_owning_org_id
7158      from po_headers_all
7159      where po_header_id = x_source_doc_po_header_id;
7160    end if;
7161 
7162   if nvl(x_ga_flag,'N') = 'Y'  and
7163      x_owning_org_id <> x_org_id  then
7164 
7165      x_progress := '10: is_src_doc_ga_frm_other_ou: result = Y';
7166      resultout  := wf_engine.eng_completed || ':' ||  'Y';
7167   else
7168      x_progress := '20: is_src_doc_ga_frm_other_ou: result = N';
7169      resultout  := wf_engine.eng_completed || ':' ||  'N';
7170   end if;
7171 
7172      IF (g_po_wf_debug = 'Y') THEN
7173         po_wf_debug_pkg.insert_debug(Itemtype,Itemkey,x_progress);
7174      END IF;
7175 
7176 exception
7177   when others then
7178     wf_core.context('po_autocreate_doc','is_src_doc_ga_other_ou',x_progress);
7179     raise;
7180 end is_src_doc_ga_frm_other_ou;
7181 
7182 
7183 /*****************************************************************************/
7184 
7185 --<Bug 2745549 mbhargav START>
7186 --Checks whether the referenced document is not cancelled or finally closed
7187 PROCEDURE is_ga_still_valid(p_ga_po_header_id   IN NUMBER,
7188                             x_ref_is_valid          OUT NOCOPY VARCHAR2) IS
7189 
7190 BEGIN
7191          x_ref_is_valid := 'N';
7192 
7193          --Check the referenced GA for cancel/finally closed status
7194          select 'Y'
7195          into   x_ref_is_valid
7196          from   po_headers_all poh
7197          where  poh.po_header_id = p_ga_po_header_id and
7198                 nvl(poh.cancel_flag, 'N') = 'N' and
7199                 nvl(poh.closed_code, 'OPEN')  <> 'FINALLY CLOSED';
7200 
7201 EXCEPTION
7202    WHEN OTHERS THEN
7203        x_ref_is_valid := 'N';
7204 END;
7205 --<Bug 2745549 mbhargav END>
7206 
7207 
7208 -- bug2821542
7209 -- validate_buyer is created to make sure that the derived buyer
7210 -- is a valid buyer
7211 
7212 /**
7213 * Private Procedure: validate_buyer
7214 * Requires: N/A
7215 * Modifies: N/A
7216 * Effects: Validates that p_agent_id is a valid buyer
7217 * Retunrs:
7218 * x_result: FND_API.G_TRUE if validation suceeds
7219 *           FND_API.G_FALSE if validation fails
7220 */
7221 
7222 PROCEDURE validate_buyer (p_agent_id IN NUMBER,
7223                           x_result   OUT NOCOPY VARCHAR2) IS
7224 
7225 l_result VARCHAR2(1);
7226 
7227 l_progress VARCHAR2(300);
7228 BEGIN
7229 
7230   l_progress := '10: validate buyer';
7231 
7232   SELECT 'Y'
7233   INTO   l_result
7234   FROM   po_buyers_v
7235   WHERE  employee_id = p_agent_id;
7236 
7237   x_result := FND_API.G_TRUE;
7238 EXCEPTION
7239   WHEN NO_DATA_FOUND THEN
7240     x_result := FND_API.G_FALSE;
7241   WHEN OTHERS THEN
7242     x_result := FND_API.G_FALSE;
7243     WF_CORE.context('po_autocreate_doc','validate_buyer',l_progress);
7244     raise;
7245 END validate_buyer;
7246 
7247 --<Shared Proc FPJ START>
7248 
7249 ----------------------------------------------------------------
7250 --Start of Comments
7251 --Name: buyer_on_src_doc_ok
7252 --Pre-reqs:
7253 --  None
7254 --Modifies:
7255 --  None
7256 --Locks:
7257 --  None
7258 --Function:
7259 --  Checks the buyer on source document.
7260 --Parameters:
7261 --IN:
7262 --itemtype
7263 --  internal name for the item type
7264 --itemkey
7265 --  primary key generated by the workflow for the item type
7266 --actid
7267 --  id number of the activity from which this procedure is called
7268 --funcmode
7269 --  execution mode of the function activity (RUN or CANCEL)
7270 --OUT:
7271 --resultout
7272 --  result returned to the workflow
7273 --     YES if 1) The buyer on the global agreement is in the same
7274 --               business group as the requesting operating unit OR
7275 --            2) The HR:Cross Business Group profile is set to 'Y'
7276 --     NO otherwise
7277 --Notes:
7278 --  Added for Shared Procurement Services Project in FPJ
7279 --Testing:
7280 --  None
7281 --End of Comments
7282 ----------------------------------------------------------------
7283 
7284 
7285 PROCEDURE buyer_on_src_doc_ok (
7286    itemtype    IN              VARCHAR2,
7287    itemkey     IN              VARCHAR2,
7288    actid       IN              NUMBER,
7289    funcmode    IN              VARCHAR2,
7290    resultout   OUT NOCOPY      VARCHAR2
7291 ) IS
7292    x_org_id                     PO_HEADERS_ALL.org_id%TYPE;
7293    x_progress                   VARCHAR2(300);
7294    x_source_doc_po_header_id    PO_HEADERS_ALL.po_header_id%TYPE;
7295    x_source_doc_type_code     PO_HEADERS_ALL.from_type_lookup_code%TYPE;
7296    x_ga_flag                    PO_HEADERS_ALL.global_agreement_flag%TYPE;
7297    l_return_status              VARCHAR2(1)   := 'N';
7298 
7299 BEGIN
7300    --Set org context
7301    x_org_id := po_wf_util_pkg.GetItemAttrNumber (itemtype      => itemtype,
7302                                            itemkey       => itemkey,
7303                                            aname         => 'ORG_ID');
7304 
7305    IF x_org_id IS NOT NULL THEN
7306       po_moac_utils_pvt.set_org_context(x_org_id); --<R12 MOAC>
7307    END IF;
7308 
7309    IF (NVL(hr_general.get_xbg_profile, 'N') = 'Y') THEN
7310       x_progress := '10: buyer_on_src_doc_ok: result = Y';
7311       resultout := wf_engine.eng_completed || ':' || 'Y';
7312    ELSE --cross business group profile is 'N'
7313       x_source_doc_po_header_id := po_wf_util_pkg.GetItemAttrNumber
7314             (itemtype      => itemtype,
7315                                            itemkey       => itemkey,
7316                                            aname         => 'SOURCE_DOCUMENT_ID');
7317 
7318       x_source_doc_type_code := po_wf_util_pkg.GetItemAttrText
7319                                           (itemtype      => itemtype,
7320                                            itemkey       => itemkey,
7321                                            aname         => 'SOURCE_DOCUMENT_TYPE_CODE');
7322 
7323       x_ga_flag := po_wf_util_pkg.GetItemAttrText
7324             (itemtype      => itemtype,
7325                                            itemkey       => itemkey,
7326                                            aname         => 'SOURCE_DOC_GA_FLAG');
7327 
7328       IF ((x_source_doc_type_code = 'QUOTATION') OR
7329           (x_source_doc_type_code = 'BLANKET' AND NVL(x_ga_flag, 'N') = 'N')) THEN
7330 
7331          x_progress := '20: buyer_on_src_doc_ok: result = Y';
7332          resultout := wf_engine.eng_completed || ':' || 'Y';
7333 
7334       ELSIF x_source_doc_type_code = 'BLANKET' AND NVL(x_ga_flag, 'N') = 'Y' THEN
7335 
7336          BEGIN
7337 
7338             SELECT 'Y'
7339             INTO   l_return_status
7340             FROM   po_headers_all poh,
7341                    per_all_people_f ppf, --Bug 16249921. Changed per_people_f ppf to per_all_people_f
7342                    financials_system_parameters fsp
7343             WHERE  poh.agent_id = ppf.person_id
7344                    AND ppf.business_group_id = fsp.business_group_id
7345                    AND TRUNC(SYSDATE) BETWEEN ppf.effective_start_date
7346                                                AND NVL (ppf.effective_end_date, SYSDATE + 1)
7347             AND    poh.po_header_id = x_source_doc_po_header_id;
7348 
7349          EXCEPTION
7350             WHEN NO_DATA_FOUND THEN
7351                l_return_status := 'N';
7352          END;
7353 
7354          IF l_return_status = 'Y' THEN
7355             x_progress := '30: buyer_on_src_doc_ok: result = Y';
7356             resultout := wf_engine.eng_completed || ':' || 'Y';
7357          ELSE
7358             x_progress := '40: buyer_on_src_doc_ok: result = N';
7359             resultout := wf_engine.eng_completed || ':' || 'N';
7360          END IF;
7361 
7362       ELSE
7363          x_progress := '50: buyer_on_src_doc_ok: result = N';
7364          resultout := wf_engine.eng_completed || ':' || 'N';
7365 
7366       END IF; --source doc check
7367    END IF; --check profile option
7368 
7369    IF (g_po_wf_debug = 'Y') THEN
7370       po_wf_debug_pkg.insert_debug (itemtype, itemkey, x_progress);
7371    END IF;
7372 
7373 EXCEPTION
7374    WHEN OTHERS THEN
7375       wf_core.CONTEXT ('po_autocreate_doc', 'buyer_on_src_doc_ok',
7376                        x_progress);
7377       RAISE;
7378 END buyer_on_src_doc_ok;
7379 
7380 
7384 --Pre-reqs:
7381 ----------------------------------------------------------------
7382 --Start of Comments
7383 --Name: buyer_on_contract_ok
7385 --  None
7386 --Modifies:
7387 --  None
7388 --Locks:
7389 --  None
7390 --Function:
7391 --  Checks the buyer on contract.
7392 --Parameters:
7393 --IN:
7394 --itemtype
7395 --  internal name for the item type
7396 --itemkey
7397 --  primary key generated by the workflow for the item type
7398 --actid
7399 --  id number of the activity from which this procedure is called
7400 --funcmode
7401 --  execution mode of the function activity (RUN or CANCEL)
7402 --OUT:
7403 --resultout
7404 --  result returned to the workflow
7405 --     YES if 1) The buyer on the global contract is in the same
7406 --               business group as the requesting operating unit OR
7407 --            2)  The HR:Cross Business Group profile is set to 'Y'
7408 --     NO otherwise
7409 --Notes:
7410 --  Added for Shared Procurement Services Project in FPJ
7411 --Testing:
7412 --  None
7413 --End of Comments
7414 ----------------------------------------------------------------
7415 
7416 
7417 PROCEDURE buyer_on_contract_ok (
7418    itemtype     IN              VARCHAR2,
7419    itemkey      IN              VARCHAR2,
7420    actid        IN              NUMBER,
7421    funcmode     IN              VARCHAR2,
7422    resultout    OUT NOCOPY      VARCHAR2
7423 ) IS
7424    x_org_id          PO_HEADERS_ALL.org_id%TYPE;
7425    x_progress        VARCHAR2(300);
7426    x_contract_id     NUMBER;
7427    l_return_status   VARCHAR2(1)   := 'N';
7428 
7429 BEGIN
7430    --Set org context
7431    x_org_id := po_wf_util_pkg.GetItemAttrNumber (itemtype      => itemtype,
7432                                            itemkey       => itemkey,
7433                                            aname         => 'ORG_ID');
7434 
7435    IF x_org_id IS NOT NULL THEN
7436      po_moac_utils_pvt.set_org_context(x_org_id); --<R12 MOAC>
7437    END IF;
7438 
7439    IF (NVL(hr_general.get_xbg_profile, 'N') = 'Y') THEN
7440      x_progress := '10: buyer_on_contract_ok: result = Y';
7441      resultout := wf_engine.eng_completed || ':' || 'Y';
7442    ELSE -- cross business group profile is 'N'
7443      x_contract_id := po_wf_util_pkg.GetItemAttrNumber
7444             (itemtype      => itemtype,
7445                                            itemkey       => itemkey,
7446                                            aname         => 'CONTRACT_ID');
7447 
7448      IF x_contract_id IS NOT NULL THEN
7449        BEGIN
7450          SELECT 'Y'
7451          INTO   l_return_status
7452          FROM   po_headers_all poh,
7453                 per_all_people_f ppf,----Bug16249921 Changed per_people_f to per_all_people_f
7454                                      --as per_people_f is security profile restricted.
7455                 financials_system_parameters fsp
7456          WHERE  poh.agent_id = ppf.person_id
7457                 AND ppf.business_group_id = fsp.business_group_id
7458                 AND TRUNC(SYSDATE) BETWEEN ppf.effective_start_date
7459                                               AND NVL(ppf.effective_end_date, SYSDATE + 1)
7460                 AND poh.po_header_id = x_contract_id;
7461        EXCEPTION
7462          WHEN NO_DATA_FOUND THEN
7463            l_return_status := 'N';
7464        END;
7465 
7466        IF l_return_status = 'Y' THEN
7467           x_progress := '20: buyer_on_contract_ok: result = Y';
7468           resultout := wf_engine.eng_completed || ':' || 'Y';
7469        ELSE
7470           x_progress := '30: buyer_on_contract_ok: result = N';
7471           resultout := wf_engine.eng_completed || ':' || 'N';
7472 
7473           purge_expense_lines(itemtype, itemkey);  -- <SERVICES FPJ>
7474 
7475        END IF;
7476 
7477      ELSE --contract id is null
7478 
7479        x_progress := '40: buyer_on_contract_ok: result = N';
7480        resultout := wf_engine.eng_completed || ':' || 'N';
7481 
7482        purge_expense_lines(itemtype, itemkey);  -- <SERVICES FPJ>
7483 
7484      END IF; --contract_id check
7485    END IF; --check profile option
7486 
7487    IF (g_po_wf_debug = 'Y') THEN
7488      po_wf_debug_pkg.insert_debug (itemtype, itemkey, x_progress);
7489    END IF;
7490 
7491 EXCEPTION
7492    WHEN OTHERS THEN
7493      wf_core.CONTEXT('po_autocreate_doc', 'buyer_on_contract_ok', x_progress);
7494      RAISE;
7495 END buyer_on_contract_ok;
7496 
7497 
7498 ----------------------------------------------------------------
7499 --Start of Comments
7500 --Name: purchasing_ou_check
7501 --Pre-reqs:
7502 --  None
7503 --Modifies:
7504 --  None
7505 --Locks:
7506 --  None
7507 --Function:
7508 --  Checks the purchasing org of the Requisition.
7509 --Parameters:
7510 --IN:
7511 --itemtype
7512 --  internal name for the item type
7513 --itemkey
7514 --  primary key generated by the workflow for the item type
7515 --actid
7516 --  id number of the activity from which this procedure is called
7517 --funcmode
7518 --  execution mode of the function activity (RUN or CANCEL)
7519 --OUT:
7520 --resultout
7521 --  result returned to the workflow
7522 --     YES if the operating unit associated with vendor_site_id
7523 --         is different from requesting operating unit.
7524 --     NO otherwise
7525 --Notes:
7526 --  Added for Shared Procurement Services Project in FPJ
7527 --Testing:
7528 --  None
7529 --End of Comments
7530 ----------------------------------------------------------------
7531 
7532 
7533 PROCEDURE purchasing_ou_check (
7537   funcmode    IN           VARCHAR2,
7534   itemtype    IN           VARCHAR2,
7535   itemkey     IN           VARCHAR2,
7536   actid       IN           NUMBER,
7538   resultout   OUT NOCOPY   VARCHAR2
7539 ) IS
7540 
7541   l_org_id              PO_HEADERS_ALL.org_id%TYPE;
7542   l_purchasing_org_id   PO_HEADERS_ALL.org_id%TYPE;
7543   x_progress            VARCHAR2(300);
7544 
7545 BEGIN
7546 
7547   l_org_id := po_wf_util_pkg.GetItemAttrNumber
7548                (itemtype      => itemtype,
7549                                         itemkey       => itemkey,
7550                                         aname         => 'ORG_ID');
7551 
7552   l_purchasing_org_id := po_wf_util_pkg.GetItemAttrNumber
7553                (itemtype      => itemtype,
7554                                         itemkey       => itemkey,
7555                                         aname         => 'PURCHASING_ORG_ID');
7556 
7557   IF l_org_id = l_purchasing_org_id THEN
7558     x_progress := '10: purchasing_ou_check: result = N';
7559     resultout := wf_engine.eng_completed || ':' || 'N';
7560   ELSE
7561     x_progress := '20: purchasing_ou_check: result = Y';
7562     resultout := wf_engine.eng_completed || ':' || 'Y';
7563   END IF;
7564 
7565   IF (g_po_wf_debug = 'Y') THEN
7566     po_wf_debug_pkg.insert_debug (itemtype, itemkey, x_progress);
7567   END IF;
7568 
7569 EXCEPTION
7570 
7571   WHEN OTHERS THEN
7572     wf_core.CONTEXT ('po_autocreate_doc', 'purchasing_ou_check', x_progress);
7573     RAISE;
7574 
7575 END purchasing_ou_check;
7576 
7577 
7578 ----------------------------------------------------------------
7579 --Start of Comments
7580 --Name: ok_to_create_in_diff_ou
7581 --Pre-reqs:
7582 --  None
7583 --Modifies:
7584 --  None
7585 --Locks:
7586 --  None
7587 --Function:
7588 --  This procedure calls an API which does the following checks:
7589 --  1) Transaction Flow existance
7590 --  2) PA Exclusion: Expense Dest with Project and Task Information
7591 --  3) VMI Enabled Check
7592 --  4) Consigned Relationship exists
7593 --  5) Check if we need to do following checks:
7594 --     a) Item validation and Revision checks
7595 --     b) Encumbrance check
7596 --     c) Destination Inv Org OPM enabled check
7597 --Parameters:
7598 --IN:
7599 --itemtype
7600 --  internal name for the item type
7601 --itemkey
7602 --  primary key generated by the workflow for the item type
7603 --actid
7604 --  id number of the activity from which this procedure is called
7605 --funcmode
7606 --  execution mode of the function activity (RUN or CANCEL)
7607 --OUT:
7608 --resultout
7609 --  result returned to the workflow
7610 --     YES if it is ok to create document in different operating unit
7611 --     NO otherwise
7612 --Notes:
7613 --  Added for Shared Procurement Services Project in FPJ
7614 --Testing:
7615 --  None
7616 --End of Comments
7617 ----------------------------------------------------------------
7618 
7619 
7620 PROCEDURE ok_to_create_in_diff_ou (
7621    itemtype    IN              VARCHAR2,
7622    itemkey     IN              VARCHAR2,
7623    actid       IN              NUMBER,
7624    funcmode    IN              VARCHAR2,
7625    resultout   OUT NOCOPY      VARCHAR2
7626 )
7627 IS
7628    l_requesting_org_id          NUMBER;
7629    l_purchasing_org_id    NUMBER;
7630    l_dest_org_id    NUMBER;
7631    l_req_line_id        NUMBER;
7632    l_source_doc_id    NUMBER;
7633    l_item_id      NUMBER;
7634    l_cons_from_supp_flag    VARCHAR2(1);
7635    x_progress             VARCHAR2 (300);
7636    l_suggested_vendor_id        PO_VENDORS.vendor_id%TYPE;
7637    l_suggested_vendor_site_id PO_VENDOR_SITES_ALL.vendor_site_id%TYPE;
7638 
7639    l_return_status    VARCHAR2(1);
7640    l_msg_count      NUMBER;
7641    l_msg_data     VARCHAR2(2000);
7642    l_error_msg_name   VARCHAR2(30);
7643    l_vmi_flag       po_asl_attributes.enable_vmi_flag%TYPE;
7644    l_cons_from_supplier_flag  VARCHAR2(1);
7645    l_last_billing_date    po_asl_attributes.last_billing_date%TYPE;
7646    l_cons_billing_cycle         po_asl_attributes.consigned_billing_cycle%TYPE;
7647 BEGIN
7648 
7649       l_req_line_id :=
7650       po_wf_util_pkg.getitemattrnumber (itemtype      => itemtype,
7651                                         itemkey       => itemkey,
7652                                         aname         => 'REQ_LINE_ID'
7653                                        );
7654       l_item_id :=
7655       po_wf_util_pkg.getitemattrnumber (itemtype      => itemtype,
7656                                         itemkey       => itemkey,
7657                                         aname         => 'ITEM_ID'
7658                                        );
7659 
7660       l_source_doc_id :=
7661       po_wf_util_pkg.getitemattrnumber (itemtype      => itemtype,
7662                                         itemkey       => itemkey,
7663                                         aname         => 'SOURCE_DOCUMENT_ID'
7664                                        );
7665 
7666       l_requesting_org_id :=
7667       po_wf_util_pkg.getitemattrnumber (itemtype      => itemtype,
7668                                         itemkey       => itemkey,
7669                                         aname         => 'ORG_ID'
7670                                        );
7671       l_purchasing_org_id :=
7672       po_wf_util_pkg.getitemattrnumber (itemtype      => itemtype,
7673                                         itemkey       => itemkey,
7674                                         aname         => 'PURCHASING_ORG_ID'
7675                                        );
7676 
7677        l_suggested_vendor_id :=
7681                                        );
7678        po_wf_util_pkg.getitemattrnumber (itemtype      => itemtype,
7679                                         itemkey       => itemkey,
7680                                         aname         => 'SUGGESTED_VENDOR_ID'
7682 
7683        l_suggested_vendor_site_id :=
7684        po_wf_util_pkg.getitemattrnumber (itemtype      => itemtype,
7685                                         itemkey       => itemkey,
7686                                         aname         => 'SUGGESTED_VENDOR_SITE_ID'
7687                                        );
7688 
7689 
7690       --SQL WHAT:Get the destination inventory org from the req line
7691       --SQL WHY: This information is needed for passing dest inv org
7692       --         to get_asl_attributes
7693       SELECT prl.destination_organization_id
7694       INTO l_dest_org_id
7695       FROM po_requisition_lines_all prl
7696       WHERE requisition_line_id = l_req_line_id;
7697 
7698       PO_THIRD_PARTY_STOCK_GRP.Get_Asl_Attributes(
7699              p_api_version                => 1.0
7700            , p_init_msg_list                  => NULL
7701            , x_return_status                  => l_return_status
7702            , x_msg_count                      => l_msg_count
7703            , x_msg_data                       => l_msg_data
7704            , p_inventory_item_id              => l_item_id
7705            , p_vendor_id                      => l_suggested_vendor_id
7706            , p_vendor_site_id                 => l_suggested_vendor_site_id
7707            , p_using_organization_id          => l_dest_org_id
7708            , x_consigned_from_supplier_flag   => l_cons_from_supp_flag
7709            , x_enable_vmi_flag                => l_vmi_flag
7710            , x_last_billing_date              =>  l_last_billing_date
7711            , x_consigned_billing_cycle        => l_cons_billing_cycle
7712         );
7713 
7714       PO_SHARED_PROC_PVT.validate_cross_ou_purchasing(
7715     p_api_version =>1.0,
7716     p_requisition_line_id =>l_req_line_id,
7717     p_requesting_org_id =>l_requesting_org_id,
7718     p_purchasing_org_id =>l_purchasing_org_id,
7719                 p_item_id   =>l_item_id,
7720                 p_source_doc_id   =>l_source_doc_id,
7721                 p_vmi_flag    =>l_vmi_flag,
7722                 p_cons_from_supp_flag =>l_cons_from_supp_flag,
7723     x_return_status   =>l_return_status,
7724     x_error_msg_name  =>l_error_msg_name
7725       );
7726 
7727    IF l_return_status = FND_API.G_RET_STS_SUCCESS
7728    THEN
7729       x_progress := '10:ok_to_create_in_diff_ou: result = Y';
7730       resultout := wf_engine.eng_completed || ':' || 'Y';
7731    ELSE
7732       x_progress := '20: ok_to_create_in_diff_ou: result = N'
7733         || ' error msg: ' || l_error_msg_name;
7734       resultout := wf_engine.eng_completed || ':' || 'N';
7735    END IF;
7736 
7737    IF (g_po_wf_debug = 'Y')
7738    THEN
7739       po_wf_debug_pkg.insert_debug (itemtype, itemkey, x_progress);
7740    END IF;
7741 EXCEPTION
7742    WHEN OTHERS
7743    THEN
7744       wf_core.CONTEXT ('po_autocreate_doc',
7745                        'ok_to_create_in_diff_ou',
7746                        x_progress
7747                       );
7748       RAISE;
7749 END ok_to_create_in_diff_ou;
7750 
7751 ----------------------------------------------------------------
7752 --Start of Comments
7753 --Name: set_purchasing_org_id
7754 --Pre-reqs:
7755 --  None
7756 --Modifies:
7757 --  None
7758 --Locks:
7759 --  None
7760 --Function:
7761 --  Helper function to set the PURCHASING_ORG_ID workflow
7762 --  attribute.
7763 --Parameters:
7764 --IN:
7765 --itemtype
7766 --  internal name for the item type
7767 --itemkey
7768 --  primary key generated by the workflow for the item type
7769 --p_org_id
7770 --  org_id of the operating unit where the requisition in
7771 --  question was created
7772 --p_suggested_vendor_site_id
7773 --  id of the suggested vendor site for the requisition in
7774 --  question
7775 --Notes:
7776 --  Added for Shared Procurement Services Project in FPJ
7777 --Testing:
7778 --  None
7779 --End of Comments
7780 ----------------------------------------------------------------
7781 
7782 PROCEDURE set_purchasing_org_id(
7783   itemtype        IN VARCHAR2,
7784   itemkey         IN VARCHAR2,
7785   p_org_id      IN NUMBER,
7786   p_suggested_vendor_site_id    IN NUMBER
7787 )
7788 IS
7789 
7790   l_purchasing_org_id PO_HEADERS_ALL.org_id%TYPE;
7791   l_progress      VARCHAR2(300);
7792 
7793 BEGIN
7794 
7795   --Get the purchasing_org_id
7796 
7797   l_progress:= '10: set_purchasing_org_id: org_id = ' || to_char(p_org_id);
7798 
7799   IF p_suggested_vendor_site_id IS NOT NULL THEN
7800     BEGIN
7801       SELECT org_id
7802       INTO l_purchasing_org_id
7803       FROM po_vendor_sites_all
7804       WHERE vendor_site_id = p_suggested_vendor_site_id;
7805     EXCEPTION
7806       WHEN NO_DATA_FOUND THEN
7807         l_purchasing_org_id := p_org_id;
7808     END;
7809   ELSE
7810     --suggested_vendor_site_id is null
7811     l_purchasing_org_id := p_org_id;
7812   END IF;
7813 
7814 
7815   l_progress:= '20: set_purchasing_org_id: org_id = ' || to_char(p_org_id)
7816                 || ' purchasing_org_id = ' || to_char(l_purchasing_org_id);
7817 
7818 
7819   --Set purchasing_org_id workflow attribute
7820   po_wf_util_pkg.SetItemAttrNumber (itemtype   => itemtype,
7821                               itemkey    => itemkey,
7822                               aname      => 'PURCHASING_ORG_ID',
7823                               avalue     => l_purchasing_org_id);
7824 
7825 EXCEPTION
7826 
7827   WHEN OTHERS THEN
7828     wf_core.context('po_autocreate_doc', 'set_purchasing_org_id', l_progress);
7829     RAISE;
7830 
7831 END set_purchasing_org_id;
7832 
7833 
7834 --<Shared Proc FPJ END>
7835 
7836 
7837 
7838 -- <SERVICES FPJ START>
7839 
7840 -------------------------------------------------------------
7841 --Start of Comments
7842 --Name        : is_expense_line
7843 --
7844 --Pre-reqs    : IN parameters need to be passed in with valid values
7845 --
7846 --Modifies    : None
7847 --
7848 --Locks       : None
7849 --
7850 --Function    : This procedure checks whether a given line is an
7851 --              expense line
7852 --
7853 --Parameter(s):
7854 --
7855 --IN          : itemtype   IN   VARCHAR2,
7856 --              itemkey    IN   VARCHAR2,
7857 --              actid      IN   NUMBER,
7858 --              funcmode   IN   VARCHAR2,
7859 --
7860 --IN OUT:     : None
7861 --
7862 --OUT         : resultout  OUT NOCOPY  VARCHAR2
7863 --
7864 --Returns     : resultout  OUT NOCOPY  VARCHAR2
7865 --
7866 --Notes       : None
7867 --
7868 --Testing     : None
7869 --
7870 --End of Comments
7871 -------------------------------------------------------------
7872 
7873 PROCEDURE is_expense_line(itemtype  IN         VARCHAR2,
7874                           itemkey   IN         VARCHAR2,
7875                           actid     IN         NUMBER,
7876                           funcmode  IN         VARCHAR2,
7877                           resultout OUT NOCOPY VARCHAR2) IS
7878 
7879 l_org_id            number := null;
7880 l_labor_req_line_id number := null;
7881 l_progress          varchar2(300) := null;
7882 
7883 BEGIN
7884 
7885     /* Set org context */
7886     l_progress := '000';
7887     l_org_id := po_wf_util_pkg.GetItemAttrNumber(itemtype => itemtype,
7888                                                  itemkey  => itemkey,
7889                                                  aname    => 'ORG_ID');
7890     po_moac_utils_pvt.set_org_context(l_org_id); --<R12 MOAC>
7891 
7892     /* Get the expense line grouping ID */
7893     l_labor_req_line_id := po_wf_util_pkg.GetItemAttrNumber(
7894                                           itemtype => itemtype,
7895                                           itemkey  => itemkey,
7896                                           aname    => 'LABOR_REQ_LINE_ID');
7897 
7898     IF (l_labor_req_line_id is not null) THEN
7899         resultout := wf_engine.eng_completed || ':' ||  'Y';
7900 
7901         l_progress:= '10: is_expense_line: result = Y';
7902 
7903         IF (g_po_wf_debug = 'Y') THEN
7904             po_wf_debug_pkg.insert_debug(itemtype,
7905                                          itemkey,
7906                                          l_progress);
7907         END IF;
7908 
7909     ELSE
7910         resultout := wf_engine.eng_completed || ':' ||  'N';
7911 
7912         l_progress:= '20: is_expense_line: result = N';
7913 
7914         IF (g_po_wf_debug = 'Y') THEN
7915             po_wf_debug_pkg.insert_debug(itemtype,
7916                                          itemkey,
7917                                          l_progress);
7918         END IF;
7919     END IF;
7920 
7921 EXCEPTION
7922     WHEN OTHERS THEN
7923         wf_core.context('po_autocreate_doc',
7924                         'is_expense_line',
7925                         l_progress);
7926         RAISE;
7927 END is_expense_line;
7928 
7929 
7930 
7931 -------------------------------------------------------------
7932 --Start of Comments
7933 --Name        : purge_expense_lines
7934 --
7935 --Pre-reqs    : IN parameters need to be passed in with valid values
7936 --
7937 --Modifies    : None
7938 --
7939 --Locks       : None
7940 --
7941 --Function    : This procedure deletes all expense lines associated with a
7942 --              Temp Labor Requisition line that fails validations.
7943 --
7944 --Parameter(s):
7945 --
7946 --IN          : itemtype IN VARCHAR2,
7947 --              itemkey  IN VARCHAR2
7948 --
7949 --IN OUT:     : None
7950 --
7951 --OUT         : None
7952 --
7953 --Returns     : None
7954 --
7955 --Notes       : None
7956 --
7957 --Testing     : None
7958 --
7959 --End of Comments
7963                               itemkey  IN VARCHAR2) IS
7960 -------------------------------------------------------------
7961 
7962 PROCEDURE purge_expense_lines(itemtype IN VARCHAR2,
7964 
7965 l_req_line_id number := null;
7966 l_progress    varchar2(300) := null;
7967 
7968 BEGIN
7969 
7970     l_progress := '000';
7971     -- Get the requisition line ID
7972     l_req_line_id := po_wf_util_pkg.GetItemAttrNumber(
7973                                     itemtype => itemtype,
7974                                     itemkey  => itemkey,
7975                                     aname    => 'REQ_LINE_ID');
7976 
7977     l_progress := '010';
7978     -- Delete all expense lines from the temporary table
7979     DELETE FROM po_wf_candidate_req_lines_temp
7980     WHERE       requisition_line_id = (
7981                     SELECT requisition_line_id
7982                     FROM   po_requisition_lines
7983                     WHERE  labor_req_line_id = l_req_line_id);
7984 
7985 EXCEPTION
7986     WHEN OTHERS THEN
7987         wf_core.context('po_autocreate_doc',
7988                         'purge_expense_lines',
7989                         l_progress);
7990         RAISE;
7991 END purge_expense_lines;
7992 -- <SERVICES FPJ END>
7993 
7994 /***************************************************************************
7995  *
7996  *  Procedure:  temp_labor_item_check
7997  *
7998  *  Description:  Checks if this is a temp_labor req line (ie. no
7999  *      item num)
8000  *
8001  **************************************************************************/
8002 procedure temp_labor_item_check  (itemtype   IN   VARCHAR2,
8003                                   itemkey    IN   VARCHAR2,
8004                                   actid      IN   NUMBER,
8005                                   funcmode   IN   VARCHAR2,
8006                                   resultout  OUT NOCOPY  VARCHAR2 ) IS
8007 x_progress        varchar2(300);
8008 x_catalog_type    varchar2(30);
8009 
8010 begin
8011 
8012   x_catalog_type := po_wf_util_pkg.GetItemAttrText (itemtype => itemtype,
8013                                             itemkey  => itemkey,
8014                                             aname    => 'CATALOG_TYPE');
8015 
8016   if nvl(x_catalog_type, 'CATALOG') <> 'TEMP_LABOR' then
8017 
8018     resultout := wf_engine.eng_completed || ':' || 'N';
8019 
8020   else
8021     resultout := wf_engine.eng_completed || ':' || 'Y';
8022 
8023     x_progress:= '10: temp_labor_item_check: result = '|| resultout;
8024     IF (g_po_wf_debug = 'Y') THEN
8025        po_wf_debug_pkg.insert_debug(itemtype,itemkey,x_progress);
8026     END IF;
8027 
8028   end if;
8029 
8030 
8031 exception
8032   when others then
8033     wf_core.context('po_autocreate_doc','temp_labor_item_check',x_progress);
8034     raise;
8035 end temp_labor_item_check;
8036 
8037 /***************************************************************************
8038  *
8039  *      function:       should_tmplbr_src_frm_contract
8040  *
8041  *      Description:    check the workflow options on whether to include the
8042  *                      temp_labor_request in the autosource
8043  *
8044  *
8045  **************************************************************************/
8046 
8047 procedure should_tmplbr_src_frm_contract(itemtype   IN   VARCHAR2,
8048                         itemkey    IN   VARCHAR2,
8049                         actid      IN   NUMBER,
8050                         funcmode   IN   VARCHAR2,
8051               resultout  OUT NOCOPY  VARCHAR2 ) IS
8052 x_incl_temp_labor_flag varchar2(1);
8053 x_progress       varchar2(300) := '000';
8054 
8055 begin
8056 
8057    x_incl_temp_labor_flag := po_wf_util_pkg.GetItemAttrText
8058           (itemtype   => itemtype,
8059                                        itemkey    => itemkey,
8060                                        aname      => 'INCLUDE_TEMP_LABOR_FLAG');
8061 
8062 
8063    if nvl(x_incl_temp_labor_flag, 'N') <> 'Y' then
8064 
8065         resultout := wf_engine.eng_completed || ':' ||  'N';
8066 
8067         return;
8068    else
8069         resultout:= wf_engine.eng_completed || ':' || 'Y';
8070 
8071   return;
8072 
8073    end if;
8074 
8075 exception
8076 
8077   when others then
8078     wf_core.context('po_autocreate_doc','should_tmplbr_src_frm_cntract',x_progress);
8079     raise;
8080 
8081 end should_tmplbr_src_frm_contract;
8082 
8083 ----------------------------------------------------------------
8084 --Start of Comments
8085 --Name: is_doc_clm_enabled
8086 --Function:
8087 --  Checks if it is clm document.
8088 --Parameters:
8089 --IN:
8090 --po_header_id
8091 --  id of the document
8092 --End of Comments
8093 ----------------------------------------------------------------
8094 function is_doc_clm_enabled (l_po_header_id IN NUMBER)
8095 RETURN BOOLEAN IS
8096 
8097 l_clm_flag VARCHAR2(1);
8098 
8099 BEGIN
8100 
8101     BEGIN
8102 
8103       SELECT clm_flag
8104       INTO l_clm_flag
8105       FROM po_doc_style_headers
8106       WHERE style_id = (SELECT style_id
8107                         FROM po_headers_all
8108                         WHERE po_header_id = l_po_header_id );
8109       EXCEPTION
8110       WHEN No_Data_Found THEN
8111       NULL;
8112 
8113     END;
8114 
8115 IF Nvl(l_clm_flag,'N') = 'Y' THEN
8116 RETURN TRUE;
8117 ELSE
8118 RETURN FALSE;
8119 END IF;
8120 
8121 end   is_doc_clm_enabled;
8122 
8123 
8124 
8125 
8126 END PO_AUTOCREATE_DOC;