DBA Data[Home] [Help]

PACKAGE BODY: APPS.AP_WORKFLOW_PKG

Source


1 PACKAGE BODY AP_WORKFLOW_PKG AS
2 /* $Header: aphanwfb.pls 120.96.12020000.11 2013/04/09 08:32:39 nbshaik ship $ */
3 --------------------------------------------------------------
4 --                    Global Variables                      --
5 --------------------------------------------------------------
6 G_CURRENT_RUNTIME_LEVEL CONSTANT NUMBER := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
7 G_LEVEL_STATEMENT       CONSTANT NUMBER := FND_LOG.LEVEL_STATEMENT;
8 G_MODULE_NAME           CONSTANT VARCHAR2(100) := 'AP_WORKFLOW_PKG';
9 G_LEVEL_PROCEDURE       CONSTANT NUMBER := FND_LOG.LEVEL_PROCEDURE;
10 
11 --  Public Procedure Specifications
12 
13 -- Procedure Definitions
14 PROCEDURE insert_history_table(p_hist_rec IN AP_INV_APRVL_HIST%ROWTYPE) IS
15 l_api_name              CONSTANT VARCHAR2(200) := 'insert_history_table';
16 l_debug_info            VARCHAR2(2000);
17 l_hist_id               AP_INV_APRVL_HIST_ALL.APPROVAL_HISTORY_ID%TYPE;
18 l_iteration             NUMBER;
19 
20 -- Bug 9861713 PRAGMA AUTONOMOUS_TRANSACTION;  bug 8450681
21 BEGIN
22    SELECT AP_INV_APRVL_HIST_S.nextval
23    INTO l_hist_id
24    FROM dual;
25 
26    --insert into the history table
27    INSERT INTO  AP_INV_APRVL_HIST_ALL
28      (APPROVAL_HISTORY_ID
29      ,HISTORY_TYPE
30      ,INVOICE_ID
31      ,ITERATION
32      ,RESPONSE
33      ,APPROVER_ID
34      ,APPROVER_NAME
35      ,CREATED_BY
36      ,CREATION_DATE
37      ,LAST_UPDATE_DATE
38      ,LAST_UPDATED_BY
39      ,LAST_UPDATE_LOGIN
40      ,ORG_ID
41      ,AMOUNT_APPROVED
42      ,HOLD_ID
43      ,LINE_NUMBER
44      ,APPROVER_COMMENTS
45      ,NOTIFICATION_ORDER)
46    VALUES (
47      l_hist_id
48      ,p_hist_rec.HISTORY_TYPE
49      ,p_hist_rec.INVOICE_ID
50      ,p_hist_rec.ITERATION
51      ,p_hist_rec.RESPONSE
52      ,p_hist_rec.APPROVER_ID
53      ,p_hist_rec.APPROVER_NAME
54      ,p_hist_rec.CREATED_BY
55      ,p_hist_rec.CREATION_DATE
56      ,p_hist_rec.LAST_UPDATE_DATE
57      ,p_hist_rec.LAST_UPDATED_BY
58      ,p_hist_rec.LAST_UPDATE_LOGIN
59      ,p_hist_rec.ORG_ID
60      ,p_hist_rec.AMOUNT_APPROVED
61      ,p_hist_rec.HOLD_ID
62      ,p_hist_rec.LINE_NUMBER
63      ,p_hist_rec.APPROVER_COMMENTS
64      ,p_hist_rec.NOTIFICATION_ORDER);
65    l_debug_info := 'After Insert into AP_INV_APRVL_HIST_ALL';
66    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
67       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
68                                         l_debug_info);
69    END IF;
70    -- Bug 9861713 commit;
71 END insert_history_table;
72 PROCEDURE recreate_pay_scheds(
73           p_invoice_id                  IN            NUMBER,
74           p_calling_sequence            IN            VARCHAR2)
75 
76 IS
77   l_item_sum               ap_invoices_all.invoice_amount%TYPE;
78   l_tax_sum                ap_invoices_all.invoice_amount%TYPE;
79   l_misc_sum               ap_invoices_all.invoice_amount%TYPE;
80   l_frt_sum                ap_invoices_all.invoice_amount%TYPE;
81   l_retained_sum           ap_invoices_all.invoice_amount%TYPE;
82   l_curr_calling_sequence  VARCHAR2(2000);
83   l_debug_info             VARCHAR2(500);
84   l_api_name               VARCHAR2(50);
85   l_hold_count             NUMBER;
86   l_line_count             NUMBER;
87   l_line_total             NUMBER;
88   l_Sched_Hold_count       NUMBER;
89   l_inv_currency_code      ap_invoices_all.invoice_currency_code%TYPE;
90   l_invoice_date           ap_invoices_all.invoice_date%TYPE;
91 
92 
93 BEGIN
94   -- Update the calling sequence
95 
96   l_curr_calling_sequence := 'recreate_pay_scheds <-'||P_calling_sequence;
97 
98   l_api_name := 'recreate_pay_scheds';
99 
100   IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
101       FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_ISP_UTILITIES_PKG.update_invoice_header2(+)');
102   END IF;
103 
104   l_debug_info := 'Step 1. update invoice amount: invoice_id = ';
105   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
106       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
107   END IF;
108 
109   BEGIN
110 
111        SELECT SUM(DECODE(line_type_lookup_code,'ITEM',NVL(amount, 0) - NVL(included_tax_amount, 0) ,0))  ITEM_SUM,
112               SUM(DECODE(line_type_lookup_code,'TAX',amount,0)) + SUM(NVL(included_tax_amount, 0)) TAX_SUM,
113               SUM(DECODE(line_type_lookup_code,'MISCELLANEOUS',NVL(amount, 0) - NVL(included_tax_amount, 0),0)) MISC_SUM,
114               SUM(DECODE(line_type_lookup_code,'FREIGHT',NVL(amount, 0) - NVL(included_tax_amount, 0),0)) FREIGHT_SUM,
115               sum(decode(line_type_lookup_code, 'ITEM', NVL(retained_amount, 0), 0)) RETAINAGE_SUM
116        INTO   l_item_sum, l_tax_sum, l_misc_sum, l_frt_sum, l_retained_sum
117        FROM   ap_invoice_lines_all
118       WHERE  invoice_id = p_invoice_id;
119 
120   EXCEPTION
121     WHEN NO_DATA_FOUND THEN
122       l_debug_info := 'no lines found for the invoice id = '|| p_invoice_id;
123       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
124           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
125       END IF;
126   END;
127 
128 
129     update ap_invoices_all
130     set    invoice_amount = l_item_sum + l_tax_sum + l_misc_sum + l_frt_sum + l_retained_sum,
131            amount_applicable_to_discount = l_item_sum + l_tax_sum + l_misc_sum + l_frt_sum + l_retained_sum,
132            net_of_retainage_flag =  DECODE(l_retained_sum, 0, 'N', 'Y')
133     where  invoice_id = p_invoice_id;
134 
135 
136 
137   l_debug_info := 'Creating Pay Schedules ';
138   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
139       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
140   END IF;
141 
142   AP_INVOICES_POST_PROCESS_PKG.insert_children (
143             X_invoice_id               => p_invoice_id,
144             X_Payment_Priority         => 99,
145             X_Hold_count               => l_hold_count,
146             X_Line_count               => l_line_count,
147             X_Line_Total               => l_line_total,
148             X_calling_sequence         => l_curr_calling_sequence,
149             X_Sched_Hold_count         => l_Sched_Hold_count);
150 
151 EXCEPTION
152     WHEN OTHERS THEN
153       IF (SQLCODE <> -20001) THEN
154         rollback;
155         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
156         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
157         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
158         FND_MESSAGE.SET_TOKEN('PARAMETERS',
159              ' P_invoice_id = '     || p_invoice_id
160           ||', P_calling_sequence = ' || l_curr_calling_sequence);
161         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
162       END IF;
163 
164       APP_EXCEPTION.RAISE_EXCEPTION;
165 
166 END recreate_pay_scheds;
167 
168 PROCEDURE get_approver(itemtype IN VARCHAR2,
169                         itemkey IN VARCHAR2,
170                         actid   IN NUMBER,
171                         funcmode IN VARCHAR2,
172                         resultout  OUT NOCOPY VARCHAR2 ) IS
173    l_invoice_id            NUMBER;
174    l_hold_id 		   NUMBER;
175    l_next_approver         ame_util.approverRecord;
176    l_api_name              CONSTANT VARCHAR2(200) := 'Get_Approver';
177    l_debug_info            VARCHAR2(2000);
178    l_org_id                NUMBER;
179    l_role                  VARCHAR2(50);
180    l_role_display          VARCHAR2(150);
181    l_display_name          VARCHAR2(150);
182    l_hist_id               AP_INV_APRVL_HIST.APPROVAL_HISTORY_ID%TYPE;
183    l_name                  wf_users.name%TYPE; --bug 8620671
184    l_hist_rec              AP_INV_APRVL_HIST%ROWTYPE;
185    l_iteration             NUMBER;
186    l_notf_iteration        NUMBER;
187 
188 
189 
190 BEGIN
191    l_org_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
192                         itemkey,
193                         'ORG_ID');
194 
195    l_invoice_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
196                         itemkey,
197                         'INVOICE_ID');
198 
199    l_hold_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
200                         itemkey,
201                         'HOLD_ID');
202    l_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
203                         itemkey,
204                         'ITERATION');
205 
206    l_notf_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
207                         itemkey,
208                         'NOTF_ITERATION');
209 
210    l_debug_info := l_api_name || ': get variables from workflow' ||
211                 ', l_invoice_id = ' || l_invoice_id ||
212                 ', l_hold_id = ' || l_hold_id ||
213                 ', l_org_id = ' || l_org_id;
214    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
215       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
216                                         l_debug_info);
217    END IF;
218 
219 
220    --get the next approver
221 
222    AME_API.getNextApprover(applicationIdIn => 200,
223                         transactionTypeIn => 'APHLD',
224                         transactionIdIn => to_char(l_hold_id),
225                         nextApproverOut => l_next_approver
226                         );
227    l_debug_info := l_api_name || ': after call to ame';
228    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
229        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
230          l_api_name,l_debug_info);
231    END IF;
232    IF l_next_approver.approval_status = ame_util.exceptionStatus THEN
233       l_debug_info := 'Error in AME_API.getNextApprover call';
234       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
235           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
236             l_api_name,l_debug_info);
237       END IF;
238       APP_EXCEPTION.RAISE_EXCEPTION;
239    END IF;
240    IF l_next_approver.person_id is null THEN /*no approver on the list*/
241     -- added for bug 8671976
242     update  ap_holds_all
243     set  wf_status = 'TERMINATED'
244     where hold_id = l_hold_id ;
245     -- added for bug 8671976
246       resultout := wf_engine.eng_completed||':'||'N';
247    ELSE -- have approver
248       WF_DIRECTORY.GetRoleName('PER',
249                                l_next_approver.person_id,l_role,
250                                l_role_display);
251       WF_DIRECTORY.GetUserName('PER',
252                                 l_next_approver.person_id,
253                                 l_name,
254                                 l_display_name);
255       WF_ENGINE.SetItemAttrText(itemtype,
256                         itemkey,
257                         'INTERNAL_REP_ROLE',
258                         l_role);
259 
260       WF_ENGINE.SetItemAttrText(itemtype,
261                                   itemkey,
262                                   'ORIG_SYSTEM',
263                                   'PER');
264 
265       WF_ENGINE.SetItemAttrText(itemtype,
266                                   itemkey,
267                                   'INTERNAL_REP_DISPLAY_NAME',
268                                   l_display_name);
269 
270       WF_ENGINE.SetItemAttrNumber(itemtype,
271                         itemkey,
272                         'INTERNAL_REP_PERSON_ID',
273                         l_next_approver.person_id);
274       --Now set the environment
275       MO_GLOBAL.INIT ('SQLAP');
276       MO_GLOBAL.set_policy_context('S',l_org_id);
277 
278       l_hist_rec.HISTORY_TYPE := 'HOLDAPPROVAL';
279       l_hist_rec.INVOICE_ID   := l_invoice_id;
280       l_hist_rec.ITERATION    := l_iteration;
281       l_hist_rec.RESPONSE     := 'SENT';
282       l_hist_rec.APPROVER_ID  := l_next_approver.person_id;
283       l_hist_rec.APPROVER_NAME:= l_display_name;
284       l_hist_rec.CREATED_BY   := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
285       l_hist_rec.CREATION_DATE:= sysdate;
286       l_hist_rec.LAST_UPDATE_DATE := sysdate;
287       l_hist_rec.LAST_UPDATED_BY  := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
288       l_hist_rec.LAST_UPDATE_LOGIN := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),-1);
289       l_hist_rec.ORG_ID            := l_org_id;
290       l_hist_rec.AMOUNT_APPROVED   := 0;
291       l_hist_rec.HOLD_ID           := l_hold_id;
292       l_hist_rec.NOTIFICATION_ORDER := l_notf_iteration;
293 
294       insert_history_table(p_hist_rec => l_hist_rec);
295 
296       resultout := wf_engine.eng_completed||':'||'Y';
297    END IF;
298 
299 EXCEPTION
300 
301 WHEN OTHERS
302    THEN
303         WF_CORE.CONTEXT('APINVHDN','GET_APPROVER',itemtype, itemkey,
304                         to_char(actid), funcmode);
305         RAISE;
306 END get_approver;
307 
308 PROCEDURE is_negotiable_flow(itemtype IN VARCHAR2,
309                         itemkey IN VARCHAR2,
310                         actid   IN NUMBER,
311                         funcmode IN VARCHAR2,
312                         resultout  OUT NOCOPY VARCHAR2 ) IS
313 
314    l_org_id NUMBER;
315    l_invoice_id NUMBER;
316    l_hold_id NUMBER;
317    l_num number;
318    l_debug_info            VARCHAR2(2000);
319    l_api_name              CONSTANT VARCHAR2(200) := 'is_negotiable_flow';
320 BEGIN
321    l_org_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
322                         itemkey,
323                         'ORG_ID');
324 
325    l_invoice_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
326                         itemkey,
327                         'INVOICE_ID');
328 
329    l_hold_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
330                         itemkey,
331                         'HOLD_ID');
332    l_debug_info := 'Before select';
333    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
334           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
335                          l_api_name,l_debug_info);
336    END IF;
337    select count(1)
338    into   l_num
339    from   ap_invoice_lines_all ail
340    where  ail.invoice_id = l_invoice_id
341    and    ail.org_id = l_org_id
342    and    ail.line_type_lookup_code = 'ITEM'
343    and    exists (
344                      (select h.line_location_id
345                       from   ap_holds_all h
346                       where  h.invoice_id = l_invoice_id
347                       and    h.org_id = l_org_id
348                       and    h.hold_id = l_hold_id
349                       and    ail.po_line_location_id = h.line_location_id
350                       and    h.status_flag = 'S'
351                       and    h.hold_lookup_code in ('PRICE', 'QTY ORD', 'QTY REC', 'AMT ORD', 'AMT REC')));
352    IF l_num > 0 THEN
353       resultout := wf_engine.eng_completed||':'||'Y';
354       WF_ENGINE.SetItemAttrText(itemtype,
355                                   itemkey,
356                                   'NOTF_CONTEXT',
357                                   'HOLDNEGOTIABLE');
358    ELSE
359       WF_ENGINE.SetItemAttrText(itemtype,
360                                   itemkey,
361                                   'NOTF_CONTEXT',
362                                   'HOLDNONNEGOTIABLE');
363       resultout := wf_engine.eng_completed||':'||'N';
364    END IF;
365    l_debug_info := 'After select, reultout : ' || resultout;
366    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
367           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
368                          l_api_name,l_debug_info);
369    END IF;
370 EXCEPTION
371 
372 WHEN OTHERS
373    THEN
374         WF_CORE.CONTEXT('APINVHDN','is_negotiable_flow',itemtype, itemkey,
375                         to_char(actid), funcmode);
376         RAISE;
377 END is_negotiable_flow;
378 
379 PROCEDURE process_ack_pomatched(itemtype IN VARCHAR2,
380                         itemkey IN VARCHAR2,
381                         actid   IN NUMBER,
382                         funcmode IN VARCHAR2,
383                         resultout  OUT NOCOPY VARCHAR2 ) IS
384 
385    l_org_id NUMBER;
386    l_invoice_id NUMBER;
387    l_hold_id NUMBER;
388    l_approver_id NUMBER;
389    l_display_name          VARCHAR2(150);
390    l_hist_rec              AP_INV_APRVL_HIST%ROWTYPE;
391    l_api_name              CONSTANT VARCHAR2(200) := 'process_ack_pomatched';
392    l_debug_info            VARCHAR2(2000);
393    l_iteration             NUMBER;
394    l_notf_iteration        NUMBER;
395  -- bug 8940578
396     l_comments      VARCHAR2(240);
397 BEGIN
398    l_org_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
399                         itemkey,
400                         'ORG_ID');
401 
402    l_invoice_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
403                         itemkey,
404                         'INVOICE_ID');
405 
406    l_hold_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
407                         itemkey,
408                         'HOLD_ID');
409 
410    l_approver_id  := WF_ENGINE.GETITEMATTRNumber(itemtype,
411                         itemkey,
412                         'INTERNAL_REP_PERSON_ID');
413 
414    l_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
415                         itemkey,
416                         'ITERATION');
417 
418    l_notf_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
419                         itemkey,
420                         'NOTF_ITERATION');
421    l_display_name := WF_ENGINE.getItemAttrText(itemtype, itemkey,
422                                        'INTERNAL_REP_DISPLAY_NAME');
423      -- bug 8940578
424    l_comments := WF_ENGINE.GetItemAttrText(itemtype,
425                              itemkey,
426                              'WF_NOTE');
427  -- bug 8940578
428 
429 
430    --Now set the environment
431    MO_GLOBAL.INIT ('SQLAP');
432    MO_GLOBAL.set_policy_context('S',l_org_id);
433 
434    l_hist_rec.HISTORY_TYPE := 'HOLDAPPROVAL';
435    l_hist_rec.INVOICE_ID   := l_invoice_id;
436    l_hist_rec.ITERATION    := l_iteration;
437    l_hist_rec.NOTIFICATION_ORDER := l_notf_iteration;
438    l_hist_rec.RESPONSE     := 'ACKNOWLEDGE';
439    l_hist_rec.APPROVER_ID  := l_approver_id;
440    l_hist_rec.APPROVER_NAME:= l_display_name;
441    l_hist_rec.CREATED_BY   := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
442    l_hist_rec.CREATION_DATE:= sysdate;
443    l_hist_rec.LAST_UPDATE_DATE := sysdate;
444    l_hist_rec.LAST_UPDATED_BY  := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
445    l_hist_rec.LAST_UPDATE_LOGIN := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),-1);
446    l_hist_rec.ORG_ID            := l_org_id;
447    l_hist_rec.AMOUNT_APPROVED   := null;
448    l_hist_rec.HOLD_ID           := l_hold_id;
449     -- bug 8940578
450     l_hist_rec.APPROVER_COMMENTS := l_comments;
451 
452    l_debug_info := 'Before insert_history_table';
453    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
454           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
455                          l_api_name,l_debug_info);
456    END IF;
457    insert_history_table(p_hist_rec => l_hist_rec);
458 
459 
460    l_debug_info := 'Before AME_API.updateApprovalStatus2';
461    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
462           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
463                          l_api_name,l_debug_info);
464    END IF;
465    AME_API.updateApprovalStatus2(applicationIdIn => 200,
466                                 transactionIdIn     => to_char(l_hold_id),
467                                 approvalStatusIn    => AME_UTIL.approvedStatus,
468                                 approverPersonIdIn  => l_approver_id,
469                                 approverUserIdIn    => NULL,
470                                 transactionTypeIn =>  'APHLD');
471 
472    resultout := wf_engine.eng_completed||':'||'Y';
473 EXCEPTION
474 
475 WHEN OTHERS
476    THEN
477         WF_CORE.CONTEXT('APINVHDN','process_ack_pomatched',itemtype, itemkey,
478                         to_char(actid), funcmode);
479         RAISE;
480 END process_ack_pomatched;
481 PROCEDURE process_rel_pomatched(itemtype IN VARCHAR2,
482                         itemkey IN VARCHAR2,
483                         actid   IN NUMBER,
484                         funcmode IN VARCHAR2,
485                         resultout  OUT NOCOPY VARCHAR2 ) IS
486    l_org_id                NUMBER;
487    l_invoice_id            NUMBER;
488    l_hold_id               NUMBER;
489    l_approver_id           NUMBER;
490    l_display_name          VARCHAR2(150);
491    l_hist_rec              AP_INV_APRVL_HIST%ROWTYPE;
492    l_api_name              CONSTANT VARCHAR2(200) := 'process_rel_pomatched';
493    l_debug_info            VARCHAR2(2000);
494    l_iteration             NUMBER;
495    l_notf_iteration        NUMBER;
496    l_comments              VARCHAR2(240);  --Bug9069200
497 BEGIN
498    l_org_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
499                         itemkey,
500                         'ORG_ID');
501 
502    l_invoice_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
503                         itemkey,
504                         'INVOICE_ID');
505 
506    l_hold_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
507                         itemkey,
508                         'HOLD_ID');
509 
510    l_approver_id  := WF_ENGINE.GETITEMATTRNumber(itemtype,
511                         itemkey,
512                         'INTERNAL_REP_PERSON_ID');
513 
514    l_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
515                         itemkey,
516                         'ITERATION');
517 
518    l_notf_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
519                         itemkey,
520                         'NOTF_ITERATION');
521 
522    l_display_name := WF_ENGINE.getItemAttrText(itemtype, itemkey,
523                                        'INTERNAL_REP_DISPLAY_NAME');
524 
525    --Bug9069200
526    l_comments := WF_ENGINE.GetItemAttrText(itemtype,
527                              itemkey,'WF_NOTE');
528    --Now set the environment
529    MO_GLOBAL.INIT ('SQLAP');
530    MO_GLOBAL.set_policy_context('S',l_org_id);
531 
532    l_hist_rec.HISTORY_TYPE := 'HOLDAPPROVAL';
533    l_hist_rec.INVOICE_ID   := l_invoice_id;
534    l_hist_rec.ITERATION    := l_iteration;
535    l_hist_rec.NOTIFICATION_ORDER := l_notf_iteration;
536    l_hist_rec.RESPONSE     := 'RELEASE';
537    l_hist_rec.APPROVER_ID  := l_approver_id;
538    l_hist_rec.APPROVER_NAME:= l_display_name;
539    l_hist_rec.CREATED_BY   := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
540    l_hist_rec.CREATION_DATE:= sysdate;
541    l_hist_rec.LAST_UPDATE_DATE := sysdate;
542    l_hist_rec.LAST_UPDATED_BY  := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
543    l_hist_rec.LAST_UPDATE_LOGIN := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),-1);
544    l_hist_rec.ORG_ID            := l_org_id;
545    l_hist_rec.AMOUNT_APPROVED   := null;
546    l_hist_rec.HOLD_ID           := l_hold_id;
547    l_hist_rec.APPROVER_COMMENTS := l_comments;  --Bug9069200
548    l_debug_info := 'Before ap_isp_utilities_pkg.release_hold';
549    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
550           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
551                          l_api_name,l_debug_info);
552    END IF;
553    ap_isp_utilities_pkg.release_hold(p_hold_id => l_hold_id);
554 
555    l_debug_info := 'Before insert_history_table';
556    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
557           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
558                          l_api_name,l_debug_info);
559    END IF;
560    insert_history_table(p_hist_rec => l_hist_rec);
561 
562 
563    l_debug_info := 'Before AME_API.updateApprovalStatus2';
564    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
565           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
566                          l_api_name,l_debug_info);
567    END IF;
568    AME_API.updateApprovalStatus2(applicationIdIn => 200,
569                                 transactionIdIn     => to_char(l_hold_id),
570                                 approvalStatusIn    => AME_UTIL.approvedStatus,
571                                 approverPersonIdIn  => l_approver_id,
572                                 approverUserIdIn    => NULL,
573                                 transactionTypeIn =>  'APHLD');
574 
575    UPDATE ap_holds_all
576    SET    wf_status = 'RELEASED'
577    WHERE  hold_id = l_hold_id;
578 
579    resultout := wf_engine.eng_completed||':'||'Y';
580 EXCEPTION
581 
582 WHEN OTHERS
583    THEN
584         WF_CORE.CONTEXT('APINVHDN','process_rel_pomatched',itemtype, itemkey,
585                         to_char(actid), funcmode);
586         RAISE;
587 END process_rel_pomatched;
588 PROCEDURE process_ack_pounmatched(itemtype IN VARCHAR2,
589                         itemkey IN VARCHAR2,
590                         actid   IN NUMBER,
591                         funcmode IN VARCHAR2,
592                         resultout  OUT NOCOPY VARCHAR2 ) IS
593    l_org_id                NUMBER;
594    l_invoice_id            NUMBER;
595    l_hold_id               NUMBER;
596    l_approver_id           NUMBER;
597    l_display_name          VARCHAR2(150);
598    l_hist_rec              AP_INV_APRVL_HIST%ROWTYPE;
599    l_api_name              CONSTANT VARCHAR2(200) := 'process_ack_pounmatched';
600    l_debug_info            VARCHAR2(2000);
601    l_iteration             NUMBER;
602    l_notf_iteration        NUMBER;
603  -- bug 8940578
604     l_comments      VARCHAR2(240);
605 
606 BEGIN
607    l_org_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
608                         itemkey,
609                         'ORG_ID');
610 
611    l_invoice_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
612                         itemkey,
613                         'INVOICE_ID');
614 
615    l_hold_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
616                         itemkey,
617                         'HOLD_ID');
618 
619    l_approver_id  := WF_ENGINE.GETITEMATTRNumber(itemtype,
620                         itemkey,
621                         'INTERNAL_REP_PERSON_ID');
622 
623    l_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
624                         itemkey,
625                         'ITERATION');
626 
627    l_notf_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
628                         itemkey,
629                         'NOTF_ITERATION');
630 
631    l_display_name := WF_ENGINE.getItemAttrText(itemtype, itemkey,
632                                        'INTERNAL_REP_DISPLAY_NAME');
633  -- bug 8940578
634    l_comments := WF_ENGINE.GetItemAttrText(itemtype,
635                              itemkey,
636                              'WF_NOTE');
637  -- bug 8940578
638 
639    --Now set the environment
640    MO_GLOBAL.INIT ('SQLAP');
641    MO_GLOBAL.set_policy_context('S',l_org_id);
642 
643    l_hist_rec.HISTORY_TYPE := 'HOLDAPPROVAL';
644    l_hist_rec.INVOICE_ID   := l_invoice_id;
645    l_hist_rec.ITERATION    := l_iteration;
646    l_hist_rec.NOTIFICATION_ORDER := l_notf_iteration;
647    l_hist_rec.RESPONSE     := 'ACKNOWLEDGE';
648    l_hist_rec.APPROVER_ID  := l_approver_id;
649    l_hist_rec.APPROVER_NAME:= l_display_name;
650    l_hist_rec.CREATED_BY   := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
651    l_hist_rec.CREATION_DATE:= sysdate;
652    l_hist_rec.LAST_UPDATE_DATE := sysdate;
653    l_hist_rec.LAST_UPDATED_BY  := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
654    l_hist_rec.LAST_UPDATE_LOGIN := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),-1);
655    l_hist_rec.ORG_ID            := l_org_id;
656    l_hist_rec.AMOUNT_APPROVED   := null;
657    l_hist_rec.HOLD_ID           := l_hold_id;
658    l_hist_rec.APPROVER_COMMENTS := l_comments; -- bug 8940578
659 
660    l_debug_info := 'Before insert_history_table';
661    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
662           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
663                          l_api_name,l_debug_info);
664    END IF;
665    insert_history_table(p_hist_rec => l_hist_rec);
666 
667 
668    l_debug_info := 'Before AME_API.updateApprovalStatus2';
669    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
670           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
671                          l_api_name,l_debug_info);
672    END IF;
673    AME_API.updateApprovalStatus2(applicationIdIn => 200,
674                                 transactionIdIn     => to_char(l_hold_id),
675                                 approvalStatusIn    => AME_UTIL.approvedStatus,
676                                 approverPersonIdIn  => l_approver_id,
677                                 approverUserIdIn    => NULL,
678                                 transactionTypeIn =>  'APHLD');
679 
680    resultout := wf_engine.eng_completed||':'||'Y';
681 EXCEPTION
682 
683 WHEN OTHERS
684    THEN
685         WF_CORE.CONTEXT('APINVHDN','process_ack_pounmatched',itemtype, itemkey,
686                         to_char(actid), funcmode);
687         RAISE;
688 END process_ack_pounmatched;
689 PROCEDURE process_rel_pounmatched(itemtype IN VARCHAR2,
690                         itemkey IN VARCHAR2,
691                         actid   IN NUMBER,
692                         funcmode IN VARCHAR2,
693                         resultout  OUT NOCOPY VARCHAR2 ) IS
694    l_org_id                NUMBER;
695    l_invoice_id            NUMBER;
696    l_hold_id               NUMBER;
697    l_approver_id           NUMBER;
698    l_display_name          VARCHAR2(150);
699    l_hist_rec              AP_INV_APRVL_HIST%ROWTYPE;
700    l_api_name              CONSTANT VARCHAR2(200) := 'process_rel_pounmatched';
701    l_debug_info            VARCHAR2(2000);
702    l_iteration             NUMBER;
703    l_notf_iteration        NUMBER;
704    l_comments              VARCHAR2(240);  --Bug9069200
705 BEGIN
706    l_org_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
707                         itemkey,
708                         'ORG_ID');
709 
710    l_invoice_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
711                         itemkey,
712                         'INVOICE_ID');
713 
714    l_hold_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
715                         itemkey,
716                         'HOLD_ID');
717 
718    l_approver_id  := WF_ENGINE.GETITEMATTRNumber(itemtype,
719                         itemkey,
720                         'INTERNAL_REP_PERSON_ID');
721 
722    l_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
723                         itemkey,
724                         'ITERATION');
725 
726    l_notf_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
727                         itemkey,
728                         'NOTF_ITERATION');
729 
730    l_display_name := WF_ENGINE.getItemAttrText(itemtype, itemkey,
731                                        'INTERNAL_REP_DISPLAY_NAME');
732 
733    --Bug9069200
734    l_comments := WF_ENGINE.GetItemAttrText(itemtype,
735                              itemkey,'WF_NOTE');
736    --Now set the environment
737    MO_GLOBAL.INIT ('SQLAP');
738    MO_GLOBAL.set_policy_context('S',l_org_id);
739 
740    l_hist_rec.HISTORY_TYPE := 'HOLDAPPROVAL';
741    l_hist_rec.INVOICE_ID   := l_invoice_id;
742    l_hist_rec.ITERATION    := l_iteration;
743    l_hist_rec.NOTIFICATION_ORDER := l_notf_iteration;
744    l_hist_rec.RESPONSE     := 'RELEASE';
745    l_hist_rec.APPROVER_ID  := l_approver_id;
746    l_hist_rec.APPROVER_NAME:= l_display_name;
747    l_hist_rec.CREATED_BY   := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
748    l_hist_rec.CREATION_DATE:= sysdate;
749    l_hist_rec.LAST_UPDATE_DATE := sysdate;
750    l_hist_rec.LAST_UPDATED_BY  := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
751    l_hist_rec.LAST_UPDATE_LOGIN := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),-1);
752    l_hist_rec.ORG_ID            := l_org_id;
753    l_hist_rec.AMOUNT_APPROVED   := null;
754    l_hist_rec.HOLD_ID           := l_hold_id;
755    l_hist_rec.APPROVER_COMMENTS := l_comments; --Bug9069200
756 
757    l_debug_info := 'Before ap_isp_utilities_pkg.release_hold';
758    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
759           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
760                          l_api_name,l_debug_info);
761    END IF;
762    ap_isp_utilities_pkg.release_hold(p_hold_id => l_hold_id);
763 
764    l_debug_info := 'Before insert_history_table';
765    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
766           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
767                          l_api_name,l_debug_info);
768    END IF;
769    insert_history_table(p_hist_rec => l_hist_rec);
770 
771 
772    l_debug_info := 'Before AME_API.updateApprovalStatus2';
773    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
774           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
775                          l_api_name,l_debug_info);
776    END IF;
777    AME_API.updateApprovalStatus2(applicationIdIn => 200,
778                                 transactionIdIn     => to_char(l_hold_id),
779                                 approvalStatusIn    => AME_UTIL.approvedStatus,
780                                 approverPersonIdIn  => l_approver_id,
781                                 approverUserIdIn    => NULL,
782                                 transactionTypeIn =>  'APHLD');
783 
784    UPDATE ap_holds_all
785    SET    wf_status = 'RELEASED'
786    WHERE  hold_id = l_hold_id;
787 
788    resultout := wf_engine.eng_completed||':'||'Y';
789 EXCEPTION
790 
791 WHEN OTHERS
792    THEN
793         WF_CORE.CONTEXT('APINVHDN','process_rel_pounmatched',itemtype, itemkey,
794                         to_char(actid), funcmode);
795         RAISE;
796 END process_rel_pounmatched;
797 
798 PROCEDURE is_it_internal(itemtype IN VARCHAR2,
799                         itemkey IN VARCHAR2,
800                         actid   IN NUMBER,
801                         funcmode IN VARCHAR2,
802                         resultout  OUT NOCOPY VARCHAR2 ) IS
803 l_notf_receipient_type VARCHAR2(50);
804 BEGIN
805    l_notf_receipient_type := WF_ENGINE.getItemAttrText(itemtype, itemkey,
806                                        'NOTF_RECEIPIENT_TYPE');
807    IF l_notf_receipient_type = 'INTERNAL' THEN
808       resultout := wf_engine.eng_completed||':'||'Y';
809    ELSE
810       resultout := wf_engine.eng_completed||':'||'N';
811    END IF;
812 EXCEPTION
813 
814 WHEN OTHERS
815    THEN
816         WF_CORE.CONTEXT('APINVHNE','is_it_internal',itemtype, itemkey,
817                         to_char(actid), funcmode);
818         RAISE;
819 END is_it_internal;
820 PROCEDURE get_supplier_contact(itemtype IN VARCHAR2,
821                         itemkey IN VARCHAR2,
822                         actid   IN NUMBER,
823                         funcmode IN VARCHAR2,
824                         resultout  OUT NOCOPY VARCHAR2 ) IS
825 
826 BEGIN
827    resultout := wf_engine.eng_completed||':'||'Y';
828 EXCEPTION
829 
830 WHEN OTHERS
831    THEN
832         WF_CORE.CONTEXT('APINVHNE','get_supplier_contact',itemtype, itemkey,
833                         to_char(actid), funcmode);
834         RAISE;
835 END get_supplier_contact;
836 
837 PROCEDURE process_accept_ext(itemtype IN VARCHAR2,
838                         itemkey IN VARCHAR2,
839                         actid   IN NUMBER,
840                         funcmode IN VARCHAR2,
841                         resultout  OUT NOCOPY VARCHAR2 ) IS
842    l_org_id NUMBER;
843    l_invoice_id NUMBER;
844    l_hold_id NUMBER;
845    l_approver_id NUMBER;
846    l_internal_approver_id  NUMBER;
847    l_display_name          VARCHAR2(150);
848    l_hist_rec              AP_INV_APRVL_HIST%ROWTYPE;
849    l_api_name              CONSTANT VARCHAR2(200) := 'process_accept_ext';
850    l_debug_info            VARCHAR2(2000);
851    l_parentkey             VARCHAR2(50);
852    l_iteration             NUMBER;
853    l_notf_iteration        NUMBER;
854    l_success               BOOLEAN;
855    l_error_code            VARCHAR2(4000);
856    l_curr_calling_sequence VARCHAR2(2000);
857 
858 
859 BEGIN
860    l_org_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
861                         itemkey,
862                         'ORG_ID');
863 
864    l_invoice_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
865                         itemkey,
866                         'INVOICE_ID');
867 
868    l_hold_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
869                         itemkey,
870                         'HOLD_ID');
871 
872    l_approver_id  := WF_ENGINE.GETITEMATTRNumber(itemtype,
873                         itemkey,
874                         'SUPPLIER_PERSON_ID');
875    l_internal_approver_id  := WF_ENGINE.GETITEMATTRNumber(itemtype,
876                         itemkey,
877                         'INTERNAL_REP_PERSON_ID');
878    l_display_name  := WF_ENGINE.GETITEMATTRText(itemtype,
879                         itemkey,
880                         'SUPPLIER_DISPLAY_NAME');
881    l_parentkey  := WF_ENGINE.GETITEMATTRText(itemtype,
882                         itemkey,
883                         'PARENT_KEY');
884 
885    l_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
886                         itemkey,
887                         'ITERATION');
888 
889    l_notf_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
890                         itemkey,
891                         'NOTF_ITERATION');
892 
893    --Now set the environment
894    MO_GLOBAL.INIT ('SQLAP');
895    MO_GLOBAL.set_policy_context('S',l_org_id);
896 
897    l_curr_calling_sequence := 'ap_workflow_pkg.process_accept_ext';
898    l_success := ap_etax_pkg.calling_etax(
899                      p_invoice_id         => l_invoice_id,
900                      p_calling_mode       => 'CALCULATE',
901                      p_all_error_messages => 'N',
902                      p_error_code         => l_error_code,
903                      p_calling_sequence   => l_curr_calling_sequence);
904 
905    l_hist_rec.HISTORY_TYPE := 'HOLDNEGOTIATION';
906    l_hist_rec.INVOICE_ID   := l_invoice_id;
907    l_hist_rec.ITERATION    := l_iteration;
908    l_hist_rec.NOTIFICATION_ORDER := l_notf_iteration;
909    l_hist_rec.RESPONSE     := 'ACCEPT';
910    l_hist_rec.APPROVER_ID  := l_approver_id;
911    l_hist_rec.APPROVER_NAME:= l_display_name;
912    l_hist_rec.CREATED_BY   := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
913    l_hist_rec.CREATION_DATE:= sysdate;
914    l_hist_rec.LAST_UPDATE_DATE := sysdate;
915    l_hist_rec.LAST_UPDATED_BY  := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
916    l_hist_rec.LAST_UPDATE_LOGIN := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),-1);
917    l_hist_rec.ORG_ID            := l_org_id;
918    l_hist_rec.AMOUNT_APPROVED   := null;
919    l_hist_rec.HOLD_ID           := l_hold_id;
920 
921    l_debug_info := 'Before ap_isp_utilities_pkg.release_hold';
922    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
923           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
924                          l_api_name,l_debug_info);
925    END IF;
926    ap_isp_utilities_pkg.release_hold(p_hold_id => l_hold_id);
927 
928    l_debug_info := 'Before insert_history_table';
929    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
930           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
931                          l_api_name,l_debug_info);
932    END IF;
933    insert_history_table(p_hist_rec => l_hist_rec);
934 
935 
936    l_debug_info := 'Before AME_API.updateApprovalStatus2';
937    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
938           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
939                          l_api_name,l_debug_info);
940    END IF;
941    AME_API.updateApprovalStatus2(applicationIdIn => 200,
942                                 transactionIdIn     => to_char(l_hold_id),
943                                 approvalStatusIn    => AME_UTIL.approvedStatus,
944                                 approverPersonIdIn  => l_internal_approver_id,
945                                 approverUserIdIn    => NULL,
946                                 transactionTypeIn =>  'APHLD');
947 
948    l_debug_info := 'Before wf_engine.CompleteActivity';
949    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
950           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
951                          l_api_name,l_debug_info);
952    END IF;
953    wf_engine.CompleteActivity(
954                      itemType => 'APINVHDN',
955                      itemKey  => l_parentkey,
956                      activity => 'HOLD_MAIN:WAITNEGOTIABLE',
957                      result   => 'NULL');
958 
959    UPDATE ap_holds_all
960    SET    wf_status = 'RELEASED'
961    WHERE  hold_id = l_hold_id;
962 
963    resultout := wf_engine.eng_completed||':'||'Y';
964 EXCEPTION
965 
966 WHEN OTHERS
967    THEN
968         WF_CORE.CONTEXT('APINVHNE','process_accept_ext',itemtype, itemkey,
969                         to_char(actid), funcmode);
970         RAISE;
971 END process_accept_ext;
972 
973 PROCEDURE get_first_approver(itemtype IN VARCHAR2,
974                         itemkey IN VARCHAR2,
975                         actid   IN NUMBER,
976                         funcmode IN VARCHAR2,
977                         resultout  OUT NOCOPY VARCHAR2 ) IS
978 
979 BEGIN
980    resultout := wf_engine.eng_completed||':'||'Y';
981 EXCEPTION
982 
983 WHEN OTHERS
984    THEN
985         WF_CORE.CONTEXT('APINVHNE','get_first_approver',itemtype, itemkey,
986                         to_char(actid), funcmode);
987         RAISE;
988 END get_first_approver;
989 
990 PROCEDURE process_cancel_inv_by_sup(itemtype IN VARCHAR2,
991                         itemkey IN VARCHAR2,
992                         actid   IN NUMBER,
993                         funcmode IN VARCHAR2,
994                         resultout  OUT NOCOPY VARCHAR2 ) IS
995    l_org_id NUMBER;
996    l_invoice_id NUMBER;
997    l_hold_id NUMBER;
998    l_approver_id NUMBER;
999    l_display_name          VARCHAR2(150);
1000    l_hist_rec              AP_INV_APRVL_HIST%ROWTYPE;
1001    l_api_name              CONSTANT VARCHAR2(200) := 'process_cancel_inv_by_sup';
1002    l_debug_info            VARCHAR2(2000);
1003    l_parentkey             VARCHAR2(50);
1004    l_result                BOOLEAN;
1005    l_last_updated_by number;
1006    l_last_update_login number;
1007    l_accounting_date date;
1008    l_message_name varchar2(30);
1009    l_invoice_amount number;
1010    l_base_amount number;
1011    l_temp_cancelled_amount number;
1012    l_cancelled_by number;
1013    l_cancelled_amount number;
1014    l_cancelled_date date;
1015    l_last_update_date date;
1016    l_original_prepayment_amount number;
1017    l_pay_curr_invoice_amount number;
1018    l_token varchar2(30);
1019    l_internal_approver_id  NUMBER;
1020    l_iteration             NUMBER;
1021    l_notf_iteration        NUMBER;
1022 
1023 
1024    cursor invoice is
1025                 select  gl_date,
1026                         last_updated_by,
1027                         last_update_login
1028                 from    ap_invoices_all
1029                 where   invoice_id = l_invoice_id
1030                 and     org_id = l_org_id;
1031 
1032 
1033 
1034 BEGIN
1035    l_org_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
1036                         itemkey,
1037                         'ORG_ID');
1038 
1039    l_invoice_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
1040                         itemkey,
1041                         'INVOICE_ID');
1042 
1043    l_hold_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
1044                         itemkey,
1045                         'HOLD_ID');
1046 
1047    l_approver_id  := WF_ENGINE.GETITEMATTRNumber(itemtype,
1048                         itemkey,
1049                         'SUPPLIER_PERSON_ID');
1050    l_internal_approver_id  := WF_ENGINE.GETITEMATTRNumber(itemtype,
1051                         itemkey,
1052                         'INTERNAL_REP_PERSON_ID');
1053    l_display_name  := WF_ENGINE.GETITEMATTRText(itemtype,
1054                         itemkey,
1055                         'SUPPLIER_DISPLAY_NAME');
1056    l_parentkey  := WF_ENGINE.GETITEMATTRText(itemtype,
1057                         itemkey,
1058                         'PARENT_KEY');
1059 
1060    l_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
1061                         itemkey,
1062                         'ITERATION');
1063 
1064    l_notf_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
1065                         itemkey,
1066                         'NOTF_ITERATION');
1067 
1068    --Now set the environment
1069    MO_GLOBAL.INIT ('SQLAP');
1070    MO_GLOBAL.set_policy_context('S',l_org_id);
1071 
1072    l_hist_rec.HISTORY_TYPE := 'HOLDNEGOTIATION';
1073    l_hist_rec.INVOICE_ID   := l_invoice_id;
1074    l_hist_rec.ITERATION    := l_iteration;
1075    l_hist_rec.NOTIFICATION_ORDER := l_notf_iteration;
1076    l_hist_rec.RESPONSE     := 'CANCEL';
1077    l_hist_rec.APPROVER_ID  := l_approver_id;
1078    l_hist_rec.APPROVER_NAME:= l_display_name;
1079    l_hist_rec.CREATED_BY   := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
1080    l_hist_rec.CREATION_DATE:= sysdate;
1081    l_hist_rec.LAST_UPDATE_DATE := sysdate;
1082    l_hist_rec.LAST_UPDATED_BY  := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
1083    l_hist_rec.LAST_UPDATE_LOGIN := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),-1);
1084    l_hist_rec.ORG_ID            := l_org_id;
1085    l_hist_rec.AMOUNT_APPROVED   := null;
1086    l_hist_rec.HOLD_ID           := l_hold_id;
1087    l_debug_info := 'Before Ap_Cancel_Single_Invoice';
1088    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1089           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
1090                          l_api_name,l_debug_info);
1091    END IF;
1092 
1093    open invoice;
1094    fetch invoice into l_accounting_date, l_last_updated_by, l_last_update_login;
1095    close invoice;
1096 
1097    l_result := ap_cancel_pkg.ap_cancel_single_invoice(
1098                 l_invoice_id,
1099                 l_last_updated_by,
1100                 l_last_update_login,
1101                 sysdate,                                -- accounting_date
1102                 l_message_name,
1103                 l_invoice_amount,
1104                 l_base_amount,
1105                 l_temp_cancelled_amount,
1106                 l_cancelled_by,
1107                 l_cancelled_amount,
1108                 l_cancelled_date,
1109                 l_last_update_date,
1110                 l_original_prepayment_amount,
1111                 l_pay_curr_invoice_amount,
1112                 l_token,
1113                 null);
1114 
1115 
1116    l_debug_info := 'Before insert_history_table';
1117    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1118           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
1119                          l_api_name,l_debug_info);
1120    END IF;
1121    insert_history_table(p_hist_rec => l_hist_rec);
1122 
1123 
1124    l_debug_info := 'Before AME_API.updateApprovalStatus2';
1125    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1126           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
1127                          l_api_name,l_debug_info);
1128    END IF;
1129    AME_API.updateApprovalStatus2(applicationIdIn => 200,
1130                                 transactionIdIn     => to_char(l_hold_id),
1131                                 approvalStatusIn    => AME_UTIL.rejectStatus,
1132                                 approverPersonIdIn  => l_internal_approver_id,
1133                                 approverUserIdIn    => NULL,
1134                                 transactionTypeIn =>  'APHLD');
1135 
1136    l_debug_info := 'Before wf_engine.CompleteActivity';
1137    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1138           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
1139                          l_api_name,l_debug_info);
1140    END IF;
1141    wf_engine.CompleteActivity(
1142                      itemType => 'APINVHDN',
1143                      itemKey  => l_parentkey,
1144                      activity => 'HOLD_MAIN:WAITNEGOTIABLE',
1145                      result   => 'NULL');
1146 
1147    resultout := wf_engine.eng_completed||':'||'Y';
1148 EXCEPTION
1149 
1150 WHEN OTHERS
1151    THEN
1152         WF_CORE.CONTEXT('APINVHNE','process_cancel_inv_by_sup',itemtype,
1153                         itemkey, to_char(actid), funcmode);
1154         RAISE;
1155 END process_cancel_inv_by_sup;
1156 
1157 PROCEDURE process_accept_int(itemtype IN VARCHAR2,
1158                         itemkey IN VARCHAR2,
1159                         actid   IN NUMBER,
1160                         funcmode IN VARCHAR2,
1161                         resultout  OUT NOCOPY VARCHAR2 ) IS
1162    l_org_id NUMBER;
1163    l_invoice_id NUMBER;
1164    l_hold_id NUMBER;
1165    l_approver_id NUMBER;
1166    l_display_name          VARCHAR2(150);
1167    l_hist_rec              AP_INV_APRVL_HIST%ROWTYPE;
1168    l_api_name              CONSTANT VARCHAR2(200) := 'process_accept_int';
1169    l_debug_info            VARCHAR2(2000);
1170    l_parentkey             VARCHAR2(50);
1171    l_iteration             NUMBER;
1172    l_notf_iteration        NUMBER;
1173    l_success               BOOLEAN;
1174    l_error_code            VARCHAR2(4000);
1175    l_curr_calling_sequence VARCHAR2(2000);
1176 BEGIN
1177    l_org_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
1178                         itemkey,
1179                         'ORG_ID');
1180 
1181    l_invoice_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
1182                         itemkey,
1183                         'INVOICE_ID');
1184 
1185    l_hold_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
1186                         itemkey,
1187                         'HOLD_ID');
1188 
1189    l_approver_id  := WF_ENGINE.GETITEMATTRNumber(itemtype,
1190                         itemkey,
1191                         'INTERNAL_REP_PERSON_ID');
1192    l_display_name  := WF_ENGINE.GETITEMATTRText(itemtype,
1193                         itemkey,
1194                         'INTERNAL_REP_DISPLAY_NAME');
1195    l_parentkey  := WF_ENGINE.GETITEMATTRText(itemtype,
1196                         itemkey,
1197                         'PARENT_KEY');
1198 
1199    l_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
1200                         itemkey,
1201                         'ITERATION');
1202 
1203    l_notf_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
1204                         itemkey,
1205                         'NOTF_ITERATION');
1206 
1207 
1208    --Now set the environment
1209    MO_GLOBAL.INIT ('SQLAP');
1210    MO_GLOBAL.set_policy_context('S',l_org_id);
1211 
1212    l_curr_calling_sequence := 'ap_workflow_pkg.process_accept_ext';
1213    l_success := ap_etax_pkg.calling_etax(
1214                      p_invoice_id         => l_invoice_id,
1215                      p_calling_mode       => 'CALCULATE',
1216                      p_all_error_messages => 'N',
1217                      p_error_code         => l_error_code,
1218                      p_calling_sequence   => l_curr_calling_sequence);
1219    l_hist_rec.HISTORY_TYPE := 'LINESNEGOTIATION';
1220    l_hist_rec.INVOICE_ID   := l_invoice_id;
1221    l_hist_rec.ITERATION    := l_iteration;
1222    l_hist_rec.NOTIFICATION_ORDER := l_notf_iteration;
1223    l_hist_rec.RESPONSE     := 'ACCEPT';
1224    l_hist_rec.APPROVER_ID  := l_approver_id;
1225    l_hist_rec.APPROVER_NAME:= l_display_name;
1226    l_hist_rec.CREATED_BY   := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
1227    l_hist_rec.CREATION_DATE:= sysdate;
1228    l_hist_rec.LAST_UPDATE_DATE := sysdate;
1229    l_hist_rec.LAST_UPDATED_BY  := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
1230    l_hist_rec.LAST_UPDATE_LOGIN := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),-1);
1231    l_hist_rec.ORG_ID            := l_org_id;
1232    l_hist_rec.AMOUNT_APPROVED   := null;
1233    l_hist_rec.HOLD_ID           := l_hold_id;
1234 
1235    l_debug_info := 'Before ap_isp_utilities_pkg.release_hold';
1236    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1237           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
1238                          l_api_name,l_debug_info);
1239    END IF;
1240    ap_isp_utilities_pkg.release_hold(p_hold_id => l_hold_id);
1241 
1242    l_debug_info := 'Before insert_history_table';
1243    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1244           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
1245                          l_api_name,l_debug_info);
1246    END IF;
1247    insert_history_table(p_hist_rec => l_hist_rec);
1248 
1249 
1250    l_debug_info := 'Before AME_API.updateApprovalStatus2';
1251    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1252           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
1253                          l_api_name,l_debug_info);
1254    END IF;
1255 
1256    AME_API.updateApprovalStatus2(applicationIdIn => 200,
1257                                 transactionIdIn     => to_char(l_hold_id),
1258                                 approvalStatusIn    => AME_UTIL.approvedStatus,
1259                                 approverPersonIdIn  => l_approver_id,
1260                                 approverUserIdIn    => NULL,
1261                                 transactionTypeIn =>  'APHLD');
1262 
1263    l_debug_info := 'Before wf_engine.CompleteActivity';
1264    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1265           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
1266                          l_api_name,l_debug_info);
1267    END IF;
1268    wf_engine.CompleteActivity(
1269                      itemType => 'APINVHDN',
1270                      itemKey  => l_parentkey,
1271                      activity => 'HOLD_MAIN:WAITNEGOTIABLE',
1272                      result   => 'NULL');
1273 
1274 
1275    UPDATE ap_holds_all
1276    SET    wf_status = 'RELEASED'
1277    WHERE  hold_id = l_hold_id;
1278 
1279    resultout := wf_engine.eng_completed||':'||'Y';
1280 EXCEPTION
1281 
1282 WHEN OTHERS
1283    THEN
1284         WF_CORE.CONTEXT('APINVHNE','process_accept_int',itemtype,
1285                         itemkey, to_char(actid), funcmode);
1286         RAISE;
1287 END process_accept_int;
1288 
1289 /*
1290 APINVAPR - Main Approval Process
1291 */
1292 
1293 
1294 --------------------------------------------------------------
1295 --  Public Procedures called from WF process
1296 --------------------------------------------------------------
1297 
1298 PROCEDURE Check_Header_Requirements(itemtype IN VARCHAR2,
1299                         itemkey IN VARCHAR2,
1300                         actid   IN NUMBER,
1301                         funcmode IN VARCHAR2,
1302                         resultout OUT NOCOPY VARCHAR2) IS
1303 
1304 	l_result 	       ame_util.stringlist;
1305 	l_reason 	       ame_util.stringlist;
1306 	l_invoice_id	 NUMBER;
1307       l_hist_rec         AP_INV_APRVL_HIST%ROWTYPE;
1308 	l_tr_reason	       VARCHAR2(240);
1309 	l_api_name	       CONSTANT VARCHAR2(200) := 'Check_Header_Requirements';
1310 	l_org_id	       NUMBER;
1311 	l_rejected_check	 BOOLEAN  := FALSE;
1312 	l_required_check	 BOOLEAN  := TRUE;
1313 	l_iteration	       NUMBER;
1314 	l_amount	       NUMBER;
1315 	l_debug_info	 VARCHAR2(2000);
1316 
1317 BEGIN
1318 
1319         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1320           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
1321                          l_api_name,
1322                          'AP_IAW_PKG.'|| l_api_name);
1323         END IF;
1324 
1325 	l_debug_info := 'set variables from workflow';
1326         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1327           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
1328 			l_api_name,l_debug_info);
1329         END IF;
1330 
1331 	l_org_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
1332                         itemkey,
1333                         'ORG_ID');
1334 
1335 	l_invoice_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
1336                         itemkey,
1337                         'INVOICE_ID');
1338 
1339 	l_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
1340                         itemkey,
1341                         'ITERATION');
1342 
1343 	l_debug_info := 'get invoice amount';
1344 	IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1345           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
1346 			l_api_name,l_debug_info);
1347         END IF;
1348 
1349 	SELECT invoice_amount
1350 	INTO l_amount
1351 	FROM ap_invoices_all
1352 	WHERE invoice_id = l_invoice_id;
1353 
1354 	l_debug_info := 'check AME if production rules should prevent approval';
1355 	IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1356           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
1357 		l_api_name,l_debug_info);
1358         END IF;
1359 
1360 	ame_api2.getTransactionProductions(
1361                 applicationIdIn     => 200,
1362 		    transactionIdIn     => to_char(l_invoice_id),
1363 		    transactionTypeIn   => 'APINV',
1364 		    variableNamesOut    => l_result,
1365 		    variableValuesOut   => l_reason);
1366 
1367 	IF l_result IS NOT NULL THEN
1368 		l_debug_info := 'loop through production results';
1369 		IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1370           		FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
1371 						l_api_name,l_debug_info);
1372         	END IF;
1373 		FOR i IN 1..l_result.count LOOP
1374 
1375 			IF l_result(i) = 'NO INVOICE APPROVAL REQUIRED' THEN
1376 
1377 
1378 				--set required flag
1379 				l_required_check := FALSE;
1380 
1381 
1382 				l_debug_info := 'get translation of reason';
1383 				IF (G_LEVEL_STATEMENT >=
1384 					G_CURRENT_RUNTIME_LEVEL) THEN
1385           				FND_LOG.STRING(G_LEVEL_STATEMENT,
1386 					G_MODULE_NAME||
1387 					l_api_name,l_debug_info);
1388         			END IF;
1389 
1390 				SELECT displayed_field
1391 				into l_tr_reason
1392                        		FROM   ap_lookup_codes
1393                        		WHERE  lookup_code = l_result(i)
1394                        		and    lookup_type = 'NLS TRANSLATION';
1395 
1396 
1397 				l_debug_info := 'populate history record Variables';
1398 				IF (G_LEVEL_STATEMENT >=
1399 					G_CURRENT_RUNTIME_LEVEL) THEN
1400           				FND_LOG.STRING(G_LEVEL_STATEMENT,
1401 					G_MODULE_NAME||
1402 					l_api_name,l_debug_info);
1403         			END IF;
1404 
1405 				l_hist_rec.invoice_id        := l_invoice_id;
1406 				l_hist_rec.iteration         := l_iteration;
1407 				l_hist_rec.response          := 'APPROVED';
1408 				l_hist_rec.approver_comments := l_tr_reason;
1409 				l_hist_rec.approver_id       :=
1410 					FND_PROFILE.VALUE('AP_IAW_USER');
1411 				l_hist_rec.org_id            := l_org_id;
1412 				l_hist_rec.created_by        :=
1413 					FND_PROFILE.VALUE('AP_IAW_USER');
1414 				l_hist_rec.creation_date     := sysdate;
1415 				l_hist_rec.last_update_date  := sysdate;
1416 				l_hist_rec.last_updated_by   :=
1417 					FND_PROFILE.VALUE('AP_IAW_USER');
1418 				l_hist_rec.last_update_login := -1;
1419 				l_hist_rec.amount_approved   := l_amount;
1420                         l_hist_rec.history_type      := 'DOCUMENTAPPROVAL';
1421 
1422 				l_debug_info := 'populate history record';
1423 				IF (G_LEVEL_STATEMENT >=
1424 					G_CURRENT_RUNTIME_LEVEL) THEN
1425           				FND_LOG.STRING(G_LEVEL_STATEMENT,
1426 					G_MODULE_NAME||
1427 					l_api_name,l_debug_info);
1428         			END IF;
1429 
1430                         insert_history_table(p_hist_rec => l_hist_rec);
1431 
1432 				l_debug_info := 'Set transaction statuses';
1433 				IF (G_LEVEL_STATEMENT >=
1434                                                 G_CURRENT_RUNTIME_LEVEL) THEN
1435                                         FND_LOG.STRING(G_LEVEL_STATEMENT,
1436 					G_MODULE_NAME||
1437                                         l_api_name,l_debug_info);
1438                                 END IF;
1439 
1440 				UPDATE AP_INVOICES_ALL
1441 				SET WFApproval_Status = 'NOT REQUIRED'
1442 				WHERE Invoice_Id = l_invoice_id
1443 				AND WFApproval_Status = 'INITIATED';
1444 
1445 				UPDATE AP_INVOICE_LINES_ALL
1446 				SET WFApproval_Status = 'NOT REQUIRED'
1447 				WHERE Invoice_Id = l_invoice_id
1448 				AND WFApproval_Status = 'INITIATED';
1449 
1450 				resultout := wf_engine.eng_completed||':'||'N';
1451 
1452 				--we do not care if there are anymore
1453 				--productions
1454 				EXIT;
1455 
1456 			ELSIF l_result(i) = 'INVOICE NOT READY' THEN
1457 
1458 				--we need to know if header was rejected by
1459 				--check
1460 				l_rejected_check := TRUE;
1461 
1462 				l_debug_info := 'get translated reason value';
1463 				IF (G_LEVEL_STATEMENT >=
1464                                                 G_CURRENT_RUNTIME_LEVEL) THEN
1465                                         FND_LOG.STRING(G_LEVEL_STATEMENT,
1466 					G_MODULE_NAME||
1467                                         l_api_name,l_debug_info);
1468                                 END IF;
1469 
1470 				SELECT displayed_field
1471                                 into l_tr_reason
1472                                 FROM   ap_lookup_codes
1473                                 WHERE  lookup_code = l_result(i)
1474                                 and    lookup_type = 'NLS TRANSLATION';
1475 
1476 			END IF; --results
1477 		END LOOP; -- production string lists
1478 
1479 		IF l_required_check = TRUE and l_rejected_check = TRUE THEN
1480 
1481        			l_debug_info := 'populate history record Variables';
1482                         IF (G_LEVEL_STATEMENT >=
1483                                   G_CURRENT_RUNTIME_LEVEL) THEN
1484                        	            FND_LOG.STRING(G_LEVEL_STATEMENT,
1485 						G_MODULE_NAME||
1486                                              l_api_name,l_debug_info);
1487                         END IF;
1488                         l_hist_rec.invoice_id         := l_invoice_id;
1489                         l_hist_rec.iteration          := l_iteration;
1490                         l_hist_rec.response           := 'REJECTED';
1491                         l_hist_rec.approver_comments  := l_tr_reason;
1492                         l_hist_rec.approver_id        :=
1493                                 FND_PROFILE.VALUE('AP_IAW_USER');
1494                         l_hist_rec.org_id             := l_org_id;
1495 			      l_hist_rec.created_by               :=
1496                                         FND_PROFILE.VALUE('AP_IAW_USER');
1497                         l_hist_rec.creation_date      := sysdate;
1498                         l_hist_rec.last_update_date   := sysdate;
1499                         l_hist_rec.last_updated_by    :=
1500                                         FND_PROFILE.VALUE('AP_IAW_USER');
1501                         l_hist_rec.last_update_login  := -1;
1502                         l_hist_rec.amount_approved    := l_amount;
1503                         l_hist_rec.history_type       := 'DOCUMENTAPPROVAL';
1504 
1505        			l_debug_info := 'populate history record';
1506                         IF (G_LEVEL_STATEMENT >=
1507                                   G_CURRENT_RUNTIME_LEVEL) THEN
1508                        	            FND_LOG.STRING(G_LEVEL_STATEMENT,
1509 						G_MODULE_NAME||
1510                                              l_api_name,l_debug_info);
1511                         END IF;
1512 
1513                         insert_history_table(p_hist_rec => l_hist_rec);
1514 
1515                         UPDATE AP_INVOICES_ALL
1516                         SET WFApproval_Status = 'REJECTED'
1517                         WHERE Invoice_Id = l_invoice_id
1518                         AND WFApproval_Status = 'INITIATED';
1519 
1520                         UPDATE AP_INVOICE_LINES_ALL
1521                         SET WFApproval_Status = 'REJECTED'
1522                         WHERE Invoice_Id = l_invoice_id
1523                         AND WFApproval_Status = 'INITIATED';
1524 
1525 			resultout := wf_engine.eng_completed||':'||'N';
1526 		END IF; --required and rejected
1527 
1528 	ELSE --there were no production results
1529 
1530 		l_debug_info := 'continue with workflow';
1531                 IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1532                 	FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
1533                                                l_api_name,l_debug_info);
1534                 END IF;
1535 		resultout := wf_engine.eng_completed||':'||'Y';
1536 
1537 	END IF;
1538 
1539 	resultout := nvl(resultout, wf_engine.eng_completed||':'||'Y');
1540 
1541 EXCEPTION
1542 WHEN OTHERS
1543         THEN
1544           WF_CORE.CONTEXT('APINVLDP','Check_Header_Requirements',itemtype, itemkey,
1545                                   to_char(actid), funcmode);
1546           RAISE;
1547 
1548 END Check_Header_Requirements;
1549 
1550 PROCEDURE check_line_requirements(itemtype IN VARCHAR2,
1551                         itemkey IN VARCHAR2,
1552                         actid   IN NUMBER,
1553                         funcmode IN VARCHAR2,
1554                         resultout  OUT NOCOPY VARCHAR2 ) IS
1555 
1556 	CURSOR matched_lines (l_invoice_id IN VARCHAR2) IS
1557 	   SELECT line_number, amount
1558 	   FROM ap_invoice_lines_all
1559 	   WHERE po_header_id is not null
1560 	   AND invoice_id = l_invoice_id
1561 	   AND wfapproval_status = 'INITIATED';
1562 
1563 	l_result 	         ame_util.stringlist;
1564 	l_reason 	         ame_util.stringlist;
1565 	l_invoice_id	   NUMBER;
1566 	l_l_hist	         AP_INV_APRVL_HIST%ROWTYPE;
1567 	l_tr_reason	         VARCHAR2(240);
1568 	l_api_name	         CONSTANT VARCHAR2(200) := 'Check_Line_Requirements';
1569 	l_org_id	         NUMBER;
1570 	l_required_check	   BOOLEAN  := TRUE;
1571 	l_iteration	         NUMBER;
1572 	l_amount	         NUMBER;
1573 	l_debug_info	   VARCHAR2(2000);
1574 	l_line_number        NUMBER;
1575       l_lines_require_approval NUMBER;
1576 
1577 BEGIN
1578 
1579         IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
1580           FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||
1581                          l_api_name,
1582                          'AP_WORKFLOW_PKG.'|| l_api_name);
1583         END IF;
1584 
1585 	/*
1586 	l_debug_info := 'set variables from workflow';
1587         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1588           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
1589 			l_api_name,l_debug_info);
1590         END IF;
1591 
1592 	l_org_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
1593                         itemkey,
1594                         'ORG_ID');
1595 
1596 	l_invoice_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
1597                         itemkey,
1598                         'INVOICE_ID');
1599 
1600 	l_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
1601                         itemkey,
1602                         'ITERATION');
1603 
1604 	--check AME if any production rules should prevent approval
1605 	ame_api2.getTransactionProductions(applicationIdIn => 200,
1606 		transactionIdIn     =>   to_char(l_invoice_id),
1607 		transactionTypeIn   =>   'APINV',
1608 		variableNamesOut    =>   l_result,
1609 		variableValuesOut   =>   l_reason);
1610 
1611 
1612 	--current hack because AME allows us to set production conditions
1613 	--at the line level, but the production results are always at the
1614 	--transaction level.
1615 	--So we are looking for line level production pairs, but
1616 	--we will still need to identify which lines apply.
1617 
1618 	IF l_result IS NOT NULL THEN
1619 	   --loop through production results
1620 	   FOR i IN 1..l_result.count LOOP
1621 		IF l_result(i) = 'NO LINE APPROVAL REQUIRED' THEN
1622 
1623 		   IF l_reason(i) = 'LINE MATCHED' THEN
1624 
1625 		  	l_debug_info := 'get translation';
1626 			IF (G_LEVEL_STATEMENT >=
1627 						G_CURRENT_RUNTIME_LEVEL) THEN
1628           		   FND_LOG.STRING(G_LEVEL_STATEMENT,
1629 					G_MODULE_NAME||
1630 					l_api_name,l_debug_info);
1631         		END IF;
1632 
1633 			SELECT displayed_field
1634 			into l_tr_reason
1635                		FROM   ap_lookup_codes
1636                		WHERE  lookup_code = l_result(i)
1637                		and    lookup_type = 'NLS TRANSLATION';
1638 
1639 		  	l_debug_info := 'Open Lines Cursor to Update History Tables';
1640 			IF (G_LEVEL_STATEMENT >=
1641 						G_CURRENT_RUNTIME_LEVEL) THEN
1642           		   FND_LOG.STRING(G_LEVEL_STATEMENT,
1643 					G_MODULE_NAME||
1644 					l_api_name,l_debug_info);
1645         		END IF;
1646 
1647 			OPEN matched_lines(l_invoice_id);
1648 			LOOP
1649 
1650 		  	   l_debug_info := 'After Open Into Fetch Lines Cursor';
1651 			   IF (G_LEVEL_STATEMENT >=
1652 						G_CURRENT_RUNTIME_LEVEL) THEN
1653           		       FND_LOG.STRING(G_LEVEL_STATEMENT,
1654 				   	    G_MODULE_NAME||
1655 					    l_api_name,l_debug_info);
1656         		   END IF;
1657 
1658 			   FETCH matched_lines
1659 			   INTO l_line_number, l_amount;
1660 			   EXIT WHEN matched_lines%NOTFOUND;
1661 
1662 		  	   l_debug_info := 'Populate History Variables';
1663 			   IF (G_LEVEL_STATEMENT >=
1664 						G_CURRENT_RUNTIME_LEVEL) THEN
1665           		       FND_LOG.STRING(G_LEVEL_STATEMENT,
1666 				   	    G_MODULE_NAME||
1667 					    l_api_name,l_debug_info);
1668         		   END IF;
1669 
1670 				--populate history record
1671 				l_l_hist.invoice_id           := l_invoice_id;
1672 				l_l_hist.iteration            := l_iteration;
1673 				l_l_hist.response             := 'APPROVED';
1674 				l_l_hist.approver_comments    := l_tr_reason;
1675 				l_l_hist.approver_id          :=
1676 			        	FND_PROFILE.VALUE('AP_IAW_USER');
1677 				l_l_hist.org_id               := l_org_id;
1678 				l_l_hist.line_number          := l_line_number;
1679 				l_l_hist.amount_approved      :=
1680 						l_amount;
1681 			 	l_l_hist.created_by           :=
1682 					FND_PROFILE.VALUE('AP_IAW_USER');
1683 				l_l_hist.creation_date        := sysdate;
1684 				l_l_hist.last_updated_by      :=
1685 					FND_PROFILE.VALUE('AP_IAW_USER');
1686 				l_l_hist.last_update_date     := sysdate;
1687 				l_l_hist.last_update_login    := -1;
1688 				l_l_hist.item_class           := 'APINV';
1689 				l_l_hist.item_id              := l_invoice_id;
1690                         l_l_hist.history_type         := 'LINEAPPROVAL';
1691 
1692  		  	      l_debug_info := 'Populate History';
1693 			      IF (G_LEVEL_STATEMENT >=
1694 				   		G_CURRENT_RUNTIME_LEVEL) THEN
1695           		          FND_LOG.STRING(G_LEVEL_STATEMENT,
1696 				   	    G_MODULE_NAME||
1697 					    l_api_name,l_debug_info);
1698         		      END IF;
1699 
1700                         insert_history_table(
1701                               p_hist_rec => l_l_hist);
1702 
1703 			END LOOP; --matched lines
1704                         CLOSE matched_lines;
1705 
1706  		  	l_debug_info := 'Update Lines Approval Status';
1707 			IF (G_LEVEL_STATEMENT >=
1708 			     G_CURRENT_RUNTIME_LEVEL) THEN
1709           		     FND_LOG.STRING(G_LEVEL_STATEMENT,
1710 				   	    G_MODULE_NAME||
1711 					    l_api_name,l_debug_info);
1712         		END IF;
1713 
1714 			--Set transaction statuses
1715 			UPDATE AP_INVOICE_LINES_ALL
1716 			SET WFApproval_Status = 'NOT REQUIRED'
1717 			WHERE Invoice_Id = l_invoice_id
1718 			AND PO_Header_Id IS NOT NULL
1719 			AND WFApproval_Status = 'INITIATED';
1720 
1721 			--setting counter to end because we get
1722 			--production pairs at the transaction level
1723 			--but are checking for line requirements
1724 			--Therefore, once we fix all the lines above
1725 			--we do not need to check for any other line
1726 			--production pairs.
1727 			EXIT;
1728 		   END IF; --reason
1729 		END IF; --results
1730 	   END LOOP; -- production string lists
1731 	END IF; --productions
1732 
1733       l_debug_info := 'Check if any more lines require approval';
1734 	IF (G_LEVEL_STATEMENT >=
1735 	     G_CURRENT_RUNTIME_LEVEL) THEN
1736            FND_LOG.STRING(G_LEVEL_STATEMENT,
1737                           G_MODULE_NAME||l_api_name,l_debug_info);
1738       END IF;
1739 
1740       SELECT count(*)
1741       INTO   l_lines_require_approval
1742       FROM   ap_invoice_lines_all
1743       WHERE  invoice_id = l_invoice_id
1744       AND WFApproval_Status = 'INITIATED';
1745 
1746       IF l_lines_require_approval > 0  THEN
1747          l_debug_info := 'Still some lines require approval';
1748 	   IF (G_LEVEL_STATEMENT >=
1749 	        G_CURRENT_RUNTIME_LEVEL) THEN
1750              FND_LOG.STRING(G_LEVEL_STATEMENT,
1751                           G_MODULE_NAME||l_api_name,l_debug_info);
1752          END IF;
1753          resultout := wf_engine.eng_completed||':'||'Y';
1754       ELSE
1755          l_debug_info := 'No More Lines require approval in this invoice';
1756 	   IF (G_LEVEL_STATEMENT >=
1757 	        G_CURRENT_RUNTIME_LEVEL) THEN
1758              FND_LOG.STRING(G_LEVEL_STATEMENT,
1759                           G_MODULE_NAME||l_api_name,l_debug_info);
1760          END IF;
1761          resultout := wf_engine.eng_completed||':'||'N';
1762       END IF;
1763       */
1764          resultout := wf_engine.eng_completed||':'||'Y';
1765 
1766 EXCEPTION
1767 
1768 WHEN OTHERS
1769    THEN
1770         WF_CORE.CONTEXT('APINVAPR','check_line_requirements',itemtype,
1771                         itemkey, to_char(actid), funcmode);
1772         IF matched_lines%ISOPEN THEN
1773            CLOSE matched_lines;
1774         END IF;
1775         RAISE;
1776 END check_line_requirements;
1777 
1778 PROCEDURE get_approvers(itemtype IN VARCHAR2,
1779                         itemkey IN VARCHAR2,
1780                         actid   IN NUMBER,
1781                         funcmode IN VARCHAR2,
1782                         resultout  OUT NOCOPY VARCHAR2 ) IS
1783 
1784       l_invoice_id      NUMBER;
1785       l_complete        VARCHAR2(1);
1786       l_next_approvers  ame_util.approversTable2;
1787       l_next_approver   ame_util.approverRecord2;
1788       l_index           ame_util.idList;
1789       l_ids             ame_util.stringList;
1790       l_class           ame_util.stringList;
1791       l_source          ame_util.longStringList;
1792       l_line_num        NUMBER;
1793       l_api_name        CONSTANT VARCHAR2(200) := 'Get_Approvers';
1794       l_iteration       NUMBER;
1795       l_debug_info      VARCHAR2(2000);
1796       l_org_id          NUMBER;
1797       l_hist_rec        AP_INV_APRVL_HIST%ROWTYPE;
1798 
1799 BEGIN
1800   IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
1801      FND_LOG.STRING(G_LEVEL_PROCEDURE,
1802                     G_MODULE_NAME||l_api_name,
1803                     'AP_WORKFLOW_PKG.'|| l_api_name);
1804   END IF;
1805   l_debug_info := l_api_name ||
1806                   ': get variables from workflow: itemtype = ' ||
1807                    itemtype ||
1808 			', itemkey = ' || itemkey;
1809   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1810       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
1811                      l_api_name,l_debug_info);
1812   END IF;
1813 
1814   l_invoice_id := WF_ENGINE.GETITEMATTRNumber
1815                   (itemtype,
1816                    itemkey,
1817                    'INVOICE_ID');
1818   l_iteration :=  WF_ENGINE.GETITEMATTRNumber
1819                   (itemtype,
1820                    itemkey,
1821                    'ITERATION');
1822   l_org_id := WF_ENGINE.GETITEMATTRNumber
1823               (itemtype,
1824                itemkey,
1825                'ORG_ID');
1826   l_debug_info := l_api_name ||
1827                   ': get variables from workflow' ||
1828                   ', l_invoice_id = ' || l_invoice_id ||
1829                   ', l_iteration = ' || l_iteration ||
1830                   ', l_org_id = ' || l_org_id;
1831   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1832       FND_LOG.STRING(G_LEVEL_STATEMENT,
1833                      G_MODULE_NAME||l_api_name,
1834                      l_debug_info);
1835   END IF;
1836 
1837   --get the next layer (stage) of approvers
1838   AME_API2.getNextApprovers1
1839            (applicationIdIn               => 200,
1840             transactionTypeIn             => 'APINV',
1841             transactionIdIn               => to_char(l_invoice_id),
1842             flagApproversAsNotifiedIn     => ame_util.booleanFalse,
1843             approvalProcessCompleteYNOut  => l_complete,
1844             nextApproversOut              => l_next_approvers,
1845             itemIndexesOut                => l_index,
1846             itemIdsOut                    => l_ids,
1847             itemClassesOut                => l_class,
1848             itemSourcesOut                => l_source);
1849 
1850   -- More values in the approver list
1851   l_debug_info := l_api_name || ': after call to ame';
1852   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1853       FND_LOG.STRING(G_LEVEL_STATEMENT,
1854                      G_MODULE_NAME||l_api_name,
1855                      l_debug_info);
1856   END IF;
1857 
1858   IF l_complete = ame_util.booleanFalse THEN
1859      -- Loop through approvers' table returned by AME
1860      l_debug_info := l_api_name || ': more approvers';
1861      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1862         FND_LOG.STRING(G_LEVEL_STATEMENT,
1863                        G_MODULE_NAME||l_api_name,
1864                        l_debug_info);
1865      END IF;
1866      l_debug_info := l_api_name ||
1867                      ': looping through approvers'||
1868                      ', next_approvers.count = ' ||
1869                      l_next_approvers.count;
1870      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1871         FND_LOG.STRING(G_LEVEL_STATEMENT,
1872                        G_MODULE_NAME||l_api_name,
1873                        l_debug_info);
1874      END IF;
1875      --
1876      FOR l_table IN 1..l_next_approvers.count LOOP
1877         l_next_approver := l_next_approvers(l_table);
1878         IF nvl(l_next_approver.item_class,'line item') <>
1879            ame_util.headerItemClassName
1880 	THEN
1881         l_debug_info := l_api_name || ': item_id = '|| l_next_approver.item_id;
1882         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1883            FND_LOG.STRING(G_LEVEL_STATEMENT,
1884                           G_MODULE_NAME||l_api_name,
1885                           l_debug_info);
1886         END IF;
1887 
1888       --Bug 14743008
1889       -- calling AME to update the notified status once per approver
1890       -- not once per the invoice line even all lines have same approver
1891       -- which is redundant
1892               AME_API2.updateApprovalStatus2(applicationIdIn => 200,
1893                            transactionIdIn     => to_char(l_invoice_id),
1894                            approvalStatusIn    => AME_UTIL.notifiedStatus,
1895                            approverNameIn  => l_next_approver.NAME,
1896                            transactionTypeIn =>  'APINV',
1897                            itemClassIn     => l_next_approver.item_class,
1898                            itemIdIn        => l_next_approver.item_id);
1899 
1900         --
1901         --if the approver record does not have a value for item_id,
1902         --we need to use the item lists returned by AME to determine
1903         --the items associated with this approver.
1904         --
1905         IF l_next_approver.item_id IS NULL THEN
1906            l_debug_info := 'item_id is null';
1907            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1908               FND_LOG.STRING(G_LEVEL_STATEMENT,
1909                              G_MODULE_NAME||l_api_name,
1910                              l_debug_info);
1911            END IF;
1912            FOR l_rec IN 1..l_index.count LOOP
1913              --l_index contains the mapping between
1914              --approvers and items
1915 	     l_line_num := null;       --bug 16414516
1916              l_debug_info := 'looping through l_rec';
1917              IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1918                 FND_LOG.STRING(G_LEVEL_STATEMENT,
1919                                G_MODULE_NAME||l_api_name,
1920                                l_debug_info);
1921              END IF;
1922              IF l_index(l_rec) = l_table THEN
1923                 l_debug_info := 'check type of item class';
1924                 IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1925                    FND_LOG.STRING(G_LEVEL_STATEMENT,
1926                                   G_MODULE_NAME||l_api_name,
1927                                   l_debug_info);
1928                 END IF;
1929                 --Depending on the type of item class, we need to set
1930                 --some variables. Need correction once project/dist seeded
1931                 IF l_class(l_rec) = ame_util.lineItemItemClassName THEN
1932                   l_line_num := l_ids(l_rec);
1933                 ELSIF l_class(l_rec) = 'project code' THEN
1934                   -- Manoj:
1935                   -- Need to have an array of lines
1936                   --
1937                   /*SELECT Invoice_Line_Number
1938                   INTO   l_line_num
1939                   FROM   AP_INVOICE_DISTRIBUTIONS_ALL
1940                   WHERE project_id =l_ids(l_rec);*/
1941                   null;
1942                 ELSIF l_class(l_rec) = ame_util.costCenterItemClassName THEN
1943                   -- Manoj:
1944                   -- Need to have an array of lines
1945                   --
1946                   /*SELECT Invoice_Line_Number
1947                   INTO   l_line_num
1948                   FROM   AP_INVOICE_DISTRIBUTIONS_ALL
1949                   WHERE  project_id =l_ids(l_rec);*/
1950                   null;
1951                 ELSIF l_class(l_rec) <>
1952                       ame_util.lineItemItemClassName
1953                       AND l_class(l_rec) <>
1954                       ame_util.headerItemClassName THEN
1955                   SELECT Invoice_Line_Number
1956                   INTO   l_line_num
1957                   FROM   AP_INVOICE_DISTRIBUTIONS_ALL
1958                   WHERE  invoice_distribution_id = l_ids(l_rec);
1959                 END IF; --l_class
1960 /**************/
1961                 --IF l_class(l_rec) <>
1962                 --      ame_util.headerItemClassName THEN
1963 	   --commented for bug 14743008
1964            /*     AME_API2.updateApprovalStatus2(applicationIdIn => 200,
1965                            transactionIdIn     => to_char(l_invoice_id),
1966                            approvalStatusIn    => AME_UTIL.notifiedStatus,
1967                            approverNameIn  => l_next_approver.NAME,
1968                            transactionTypeIn =>  'APINV',
1969                            itemClassIn     => l_class(l_rec),
1970                            itemIdIn        => l_ids(l_rec));  */
1971 	  --commented for bug 14743008 end
1972                 --END IF;
1973 /**************/
1974 
1975 --  bug 13919019 start
1976 -- These values have to be cleared of before starting as the residue values will cause issue
1977 -- in case of approver groups which has more than one approver and
1978 -- the notification is escalated in the previous level
1979            WF_ENGINE.SetItemAttrText(itemtype,
1980                    itemkey,
1981                    'ESCALATED',
1982                    null);
1983            WF_ENGINE.SetItemAttrText(itemtype,
1984                    itemkey,
1985                    'FORWARD_ROLE',
1986                    null);
1987            WF_ENGINE.SetItemAttrText(itemtype,
1988                    itemkey,
1989                    'ESC_ROLE_NAME',
1990                    null);
1991 
1992            WF_ENGINE.SetItemAttrText(itemtype,
1993                    itemkey,
1994                    'ESC_ROLE_ACTUAL',
1995                    null);
1996 
1997  --  bug 13919019 end
1998 
1999                 --Insert record into ap_apinv_approvers
2000                 --
2001                 INSERT INTO AP_APINV_APPROVERS(
2002 				INVOICE_ID,
2003 				INVOICE_ITERATION,
2004 				INVOICE_KEY,
2005 				LINE_NUMBER,
2006 				NOTIFICATION_STATUS,
2007 				ROLE_NAME,
2008 				ORIG_SYSTEM,
2009 				ORIG_SYSTEM_ID,
2010 				DISPLAY_NAME,
2011 				APPROVER_CATEGORY,
2012 				API_INSERTION,
2013 				AUTHORITY,
2014 				APPROVAL_STATUS,
2015 				ACTION_TYPE_ID,
2016 				GROUP_OR_CHAIN_ID,
2017 				OCCURRENCE,
2018 				SOURCE,
2019 				ITEM_CLASS,
2020 				ITEM_ID,
2021 				ITEM_CLASS_ORDER_NUMBER,
2022 				ITEM_ORDER_NUMBER,
2023 				SUB_LIST_ORDER_NUMBER,
2024 				ACTION_TYPE_ORDER_NUMBER,
2025 				GROUP_OR_CHAIN_ORDER_NUMBER,
2026 				MEMBER_ORDER_NUMBER,
2027 				APPROVER_ORDER_NUMBER,
2028 				LAST_UPDATED_BY,
2029 				LAST_UPDATE_DATE,
2030 				LAST_UPDATE_LOGIN,
2031 				CREATED_BY,
2032 				CREATION_DATE,
2033 				PROGRAM_APPLICATION_ID,
2034 				PROGRAM_ID,
2035 				PROGRAM_UPDATE_DATE,
2036 				REQUEST_ID )
2037 				VALUES(
2038 				l_invoice_id,
2039 				l_iteration,
2040 				itemkey,
2041 				l_line_num,
2042 				'PEND',
2043 				l_next_approver.NAME,
2044 				l_next_approver.ORIG_SYSTEM,
2045 				l_next_approver.ORIG_SYSTEM_ID,
2046 				l_next_approver.DISPLAY_NAME,
2047 				l_next_approver.APPROVER_CATEGORY,
2048 				l_next_approver.API_INSERTION,
2049 				l_next_approver.AUTHORITY,
2050 				l_next_approver.APPROVAL_STATUS,
2051 				l_next_approver.ACTION_TYPE_ID,
2052 				l_next_approver.GROUP_OR_CHAIN_ID,
2053 				l_next_approver.OCCURRENCE,
2054 				l_next_approver.SOURCE,
2055 				l_class(l_rec),
2056 				l_ids(l_rec),
2057 				l_next_approver.ITEM_CLASS_ORDER_NUMBER,
2058 				l_next_approver.ITEM_ORDER_NUMBER,
2059 				l_next_approver.SUB_LIST_ORDER_NUMBER,
2060 				l_next_approver.ACTION_TYPE_ORDER_NUMBER,
2061 				l_next_approver.GROUP_OR_CHAIN_ORDER_NUMBER,
2062 				l_next_approver.MEMBER_ORDER_NUMBER,
2063 				l_next_approver.APPROVER_ORDER_NUMBER,
2064 				nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1),
2065 								sysdate,
2066 				nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),
2067 								-1),
2068 				nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1),
2069 				sysdate,
2070 				200,
2071 				0,
2072 				sysdate,
2073 				0);
2074                 l_debug_info := 'after insert';
2075                 IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2076                    FND_LOG.STRING(G_LEVEL_STATEMENT,
2077                                   G_MODULE_NAME||l_api_name,
2078                                   l_debug_info);
2079                 END IF;
2080                 l_debug_info := 'Before calling insert_history_table';
2081                 IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2082                    FND_LOG.STRING(G_LEVEL_STATEMENT,
2083                                   G_MODULE_NAME||l_api_name,
2084                                   l_debug_info);
2085                 END IF;
2086                 l_hist_rec.HISTORY_TYPE := 'LINESAPPROVAL';
2087                 l_hist_rec.INVOICE_ID   := l_invoice_id;
2088                 l_hist_rec.line_number   := l_line_num;
2089                 l_hist_rec.amount_approved := 0;
2090 
2091                 l_hist_rec.ITERATION    := l_iteration;
2092                 l_hist_rec.RESPONSE     := 'SENT';
2093                 l_hist_rec.APPROVER_ID  := l_next_approver.ORIG_SYSTEM_ID;
2094                 l_hist_rec.APPROVER_NAME:= l_next_approver.DISPLAY_NAME;
2095                 l_hist_rec.CREATED_BY   := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
2096                 l_hist_rec.CREATION_DATE:= sysdate;
2097                 l_hist_rec.LAST_UPDATE_DATE := sysdate;
2098                 l_hist_rec.LAST_UPDATED_BY  := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
2099                 l_hist_rec.LAST_UPDATE_LOGIN := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),-1);
2100                 l_hist_rec.ORG_ID            := l_org_id;
2101 
2102 
2103                 insert_history_table(p_hist_rec => l_hist_rec);
2104 
2105                 l_debug_info := 'After calling insert_history_table';
2106                 IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2107                    FND_LOG.STRING(G_LEVEL_STATEMENT,
2108                                   G_MODULE_NAME||l_api_name,
2109                                   l_debug_info);
2110                 END IF;
2111              END IF; --l_index mapping
2112            END LOOP; -- l_index mapping
2113         ELSE  --only one item_id per approver
2114           l_debug_info := 'only one item_id per approver';
2115           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2116              FND_LOG.STRING(G_LEVEL_STATEMENT,
2117                             G_MODULE_NAME||l_api_name,
2118                             l_debug_info);
2119           END IF;
2120           --
2121           --Depending on the type of item class, we need to set
2122           --some variables:
2123           --
2124 	    IF l_next_approver.item_class =
2125              ame_util.lineItemItemClassName THEN
2126              l_line_num := l_next_approver.item_id;
2127           ELSIF l_next_approver.item_class = 'project code' THEN
2128              -- Manoj:
2129              -- Need to have an array of lines
2130              --
2131              /*SELECT Invoice_Line_Number
2132              INTO   l_line_num
2133              FROM   AP_INVOICE_DISTRIBUTIONS_ALL
2134              WHERE  project_id = l_next_approver.item_id;*/
2135              null;
2136           ELSIF l_next_approver.item_class =
2137                 ame_util.costCenterItemClassName THEN
2138              -- Manoj:
2139              -- Need to have an array of lines
2140              --
2141              /*SELECT Invoice_Line_Number
2142              INTO   l_line_num
2143              FROM   AP_INVOICE_DISTRIBUTIONS_ALL
2144              WHERE  project_id = l_next_approver.item_id;*/
2145              null;
2146           ELSIF l_next_approver.item_class <>
2147                 ame_util.lineItemItemClassName
2148                 AND l_next_approver.item_class <>
2149                 ame_util.headerItemClassName THEN
2150              SELECT Invoice_Line_Number
2151              INTO   l_line_num
2152              FROM   AP_INVOICE_DISTRIBUTIONS_ALL
2153              WHERE  invoice_distribution_id = l_next_approver.item_id;
2154           END IF; --l_class
2155 
2156 /********/
2157                 --IF l_next_approver.item_class <>
2158                 --ame_util.headerItemClassName THEN
2159      --commented for bug 14743008
2160      /*           AME_API2.updateApprovalStatus2(applicationIdIn => 200,
2161                            transactionIdIn     => to_char(l_invoice_id),
2162                            approvalStatusIn    => AME_UTIL.notifiedStatus,
2163                            approverNameIn  => l_next_approver.NAME,
2164                            transactionTypeIn =>  'APINV',
2165                            itemClassIn     => l_next_approver.item_class,
2166                            itemIdIn        => l_next_approver.item_id);  */
2167                 --END IF;
2168 /*********/
2169 
2170 --  bug 13919019 start
2171 -- These values have to be cleared of before starting as the residue values will cause issue
2172 -- in case of approver groups which has more than one approver and
2173 -- the notification is escalated in the previous level
2174            WF_ENGINE.SetItemAttrText(itemtype,
2175                    itemkey,
2176                    'ESCALATED',
2177                    null);
2178            WF_ENGINE.SetItemAttrText(itemtype,
2179                    itemkey,
2180                    'FORWARD_ROLE',
2181                    null);
2182            WF_ENGINE.SetItemAttrText(itemtype,
2183                    itemkey,
2184                    'ESC_ROLE_NAME',
2185                    null);
2186 
2187            WF_ENGINE.SetItemAttrText(itemtype,
2188                    itemkey,
2189                    'ESC_ROLE_ACTUAL',
2190                    null);
2191 
2192 --  bug 13919019 ends
2193 
2194 
2195           --Insert record into ap_apinv_approvers
2196           INSERT INTO AP_APINV_APPROVERS(
2197 				INVOICE_ID,
2198 				INVOICE_ITERATION,
2199 				INVOICE_KEY,
2200 				LINE_NUMBER,
2201 				NOTIFICATION_STATUS,
2202 				ROLE_NAME,
2203 				ORIG_SYSTEM,
2204 				ORIG_SYSTEM_ID,
2205 				DISPLAY_NAME,
2206 				APPROVER_CATEGORY,
2207 				API_INSERTION,
2208 				AUTHORITY,
2209 				APPROVAL_STATUS,
2210 				ACTION_TYPE_ID,
2211 				GROUP_OR_CHAIN_ID,
2212 				OCCURRENCE,
2213 				SOURCE,
2214 				ITEM_CLASS,
2215 				ITEM_ID,
2216 				ITEM_CLASS_ORDER_NUMBER,
2217 				ITEM_ORDER_NUMBER,
2218 				SUB_LIST_ORDER_NUMBER,
2219 				ACTION_TYPE_ORDER_NUMBER,
2220 				GROUP_OR_CHAIN_ORDER_NUMBER,
2221 				MEMBER_ORDER_NUMBER,
2222 				APPROVER_ORDER_NUMBER,
2223 				LAST_UPDATED_BY,
2224 				LAST_UPDATE_DATE,
2225 				LAST_UPDATE_LOGIN,
2226 				CREATED_BY,
2227 				CREATION_DATE,
2228 				PROGRAM_APPLICATION_ID,
2229 				PROGRAM_ID,
2230 				PROGRAM_UPDATE_DATE,
2231 				REQUEST_ID )
2232 				VALUES(
2233 				l_invoice_id,
2234 				l_iteration,
2235 				itemkey,
2236 				l_line_num,
2237 				'PEND',
2238 				l_next_approver.NAME,
2239 				l_next_approver.ORIG_SYSTEM,
2240 				l_next_approver.ORIG_SYSTEM_ID,
2241 				l_next_approver.DISPLAY_NAME,
2242 				l_next_approver.APPROVER_CATEGORY,
2243 				l_next_approver.API_INSERTION,
2244 				l_next_approver.AUTHORITY,
2245 				l_next_approver.APPROVAL_STATUS,
2246 				l_next_approver.ACTION_TYPE_ID,
2247 				l_next_approver.GROUP_OR_CHAIN_ID,
2248 				l_next_approver.OCCURRENCE,
2249 				l_next_approver.SOURCE,
2250 				l_next_approver.item_class,
2251 				l_next_approver.item_id,
2252 				l_next_approver.ITEM_CLASS_ORDER_NUMBER,
2253 				l_next_approver.ITEM_ORDER_NUMBER,
2254 				l_next_approver.SUB_LIST_ORDER_NUMBER,
2255 				l_next_approver.ACTION_TYPE_ORDER_NUMBER,
2256 				l_next_approver.GROUP_OR_CHAIN_ORDER_NUMBER,
2257 				l_next_approver.MEMBER_ORDER_NUMBER,
2258 				l_next_approver.APPROVER_ORDER_NUMBER,
2259 				nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1),
2260                                 				sysdate,
2261                                 nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),
2262 								-1),
2263                                 nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1),
2264                                 sysdate,
2265                                 200,
2266                                 0,
2267                                 sysdate,
2268                                 0);
2269            l_debug_info := 'Before calling insert_history_table';
2270            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2271                    FND_LOG.STRING(G_LEVEL_STATEMENT,
2272                                   G_MODULE_NAME||l_api_name,
2273                                   l_debug_info);
2274            END IF;
2275            l_hist_rec.HISTORY_TYPE := 'LINESAPPROVAL';
2276            l_hist_rec.INVOICE_ID   := l_invoice_id;
2277            l_hist_rec.line_number   := l_line_num;
2278            l_hist_rec.amount_approved := 0;
2279 
2280            l_hist_rec.ITERATION    := l_iteration;
2281            l_hist_rec.RESPONSE     := 'SENT';
2282            l_hist_rec.APPROVER_ID  := l_next_approver.ORIG_SYSTEM_ID;
2283            l_hist_rec.APPROVER_NAME:= l_next_approver.DISPLAY_NAME;
2284            l_hist_rec.CREATED_BY   := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID
2285         ')),-1);
2286            l_hist_rec.CREATION_DATE:= sysdate;
2287            l_hist_rec.LAST_UPDATE_DATE := sysdate;
2288            l_hist_rec.LAST_UPDATED_BY  := nvl(TO_NUMBER(FND_PROFILE.VALUE('USE
2289         R_ID')),-1);
2290            l_hist_rec.LAST_UPDATE_LOGIN := nvl(TO_NUMBER(FND_PROFILE.VALUE('LO
2291         GIN_ID')),-1);
2292            l_hist_rec.ORG_ID            := l_org_id;
2293 
2294 
2295            insert_history_table(p_hist_rec => l_hist_rec);
2296            l_debug_info := 'After calling insert_history_table';
2297            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2298                    FND_LOG.STRING(G_LEVEL_STATEMENT,
2299                                   G_MODULE_NAME||l_api_name,
2300                                   l_debug_info);
2301            END IF;
2302         END IF; --more than one item_id per approver
2303         END IF; -- end if for IF nvl(l_next_approver.item_class,
2304 	        -- 'line item') <> ame_util.headerItemClassName
2305      END LOOP; --nextApprovers table
2306   END IF; --complete
2307   resultout := wf_engine.eng_completed;
2308 EXCEPTION
2309    WHEN OTHERS THEN
2310         WF_CORE.CONTEXT('APINVAPR','get_approvers',itemtype,
2311                         itemkey, to_char(actid), funcmode);
2312         RAISE;
2313 END get_approvers;
2314 
2315 PROCEDURE identify_approvers(itemtype IN VARCHAR2,
2316                         itemkey IN VARCHAR2,
2317                         actid   IN NUMBER,
2318                         funcmode IN VARCHAR2,
2319                         resultout  OUT NOCOPY VARCHAR2 ) IS
2320   l_invoice_id	NUMBER;
2321   l_iteratation	NUMBER;
2322   l_not_iteration	NUMBER;
2323   l_pend		NUMBER;
2324   l_sent		NUMBER;
2325   l_comp		NUMBER;
2326   l_name		VARCHAR2(320);
2327   l_api_name	CONSTANT VARCHAR2(200) := 'Identify_Approvers';
2328   l_iteration	NUMBER;
2329   l_debug_info	VARCHAR2(2000);
2330 
2331   l_org_id                      NUMBER;
2332   l_role                        VARCHAR2(50);
2333   l_orig_id                     NUMBER;
2334   l_invoice_type_lookup_code    ap_invoices_all.invoice_type_lookup_code%TYPE;
2335 
2336   CURSOR Group_Approvers IS
2337   SELECT distinct role_name
2338   FROM   ap_apinv_approvers
2339   WHERE  notification_status = 'PEND'
2340   AND    invoice_key = itemkey
2341   AND    line_number IS NOT NULL;
2342 
2343 BEGIN
2344 
2345   l_pend := 0;
2346 
2347   IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
2348      FND_LOG.STRING(G_LEVEL_PROCEDURE,
2349                     G_MODULE_NAME||l_api_name,
2350                     'AP_WORKFLOW_PKG.'|| l_api_name);
2351   END IF;
2352   l_debug_info := l_api_name ||
2353                   ': get variables from workflow: itemtype = ' ||
2354                    itemtype ||
2355 			', itemkey = ' || itemkey;
2356   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2357       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2358                      l_api_name,l_debug_info);
2359   END IF;
2360 
2361   l_invoice_id := WF_ENGINE.GETITEMATTRNumber
2362                   (itemtype,
2363                    itemkey,
2364                    'INVOICE_ID');
2365   l_iteration := WF_ENGINE.GETITEMATTRNumber
2366                  (itemtype,
2367                   itemkey,
2368                   'ITERATION');
2369 
2370   l_debug_info := l_api_name ||': invoice_id = ' ||
2371                   l_invoice_id ||
2372 			', iteration = ' ||
2373                   l_iteration;
2374   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2375      FND_LOG.STRING(G_LEVEL_STATEMENT,
2376                     G_MODULE_NAME||l_api_name,
2377                     l_debug_info);
2378   END IF;
2379 
2380   l_debug_info := 'Check for Pending Approvers to be notified';
2381   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2382      FND_LOG.STRING(G_LEVEL_STATEMENT,
2383                     G_MODULE_NAME||l_api_name,
2384                     l_debug_info);
2385   END IF;
2386 
2387   l_debug_info := 'Process Group Approvers Cursor';
2388   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2389      FND_LOG.STRING(G_LEVEL_STATEMENT,
2390                     G_MODULE_NAME||l_api_name,
2391                     l_debug_info);
2392   END IF;
2393 
2394   FOR Group_Approvers_Rec IN Group_Approvers LOOP
2395 
2396     l_debug_info := 'Inside Group Approvers Loop for Role: '||
2397                     Group_Approvers_Rec.role_name;
2398     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2399        FND_LOG.STRING(G_LEVEL_STATEMENT,
2400                       G_MODULE_NAME||l_api_name,
2401                       l_debug_info);
2402     END IF;
2403 
2404     l_pend := 1;
2405 
2406     l_debug_info := 'Get Notification Iteration';
2407     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2408        FND_LOG.STRING(G_LEVEL_STATEMENT,
2409                       G_MODULE_NAME||l_api_name,
2410                       l_debug_info);
2411     END IF;
2412 
2413     SELECT nvl(max(notification_iteration),0) + 1
2414     INTO   l_not_iteration
2415     FROM   ap_apinv_approvers
2416     WHERE  invoice_key = itemkey;
2417 
2418     l_debug_info := 'Notification Iteration:'||l_not_iteration;
2419     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2420        FND_LOG.STRING(G_LEVEL_STATEMENT,
2421                       G_MODULE_NAME||l_api_name,
2422                       l_debug_info);
2423     END IF;
2424 
2425     l_debug_info := 'Update Iteration Value in ap_apinv_approvers';
2426     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2427        FND_LOG.STRING(G_LEVEL_STATEMENT,
2428                       G_MODULE_NAME||l_api_name,
2429                       l_debug_info);
2430     END IF;
2431 
2432     UPDATE ap_apinv_approvers
2433     SET    notification_iteration = l_not_iteration,
2434            notification_key = itemkey || '_' || l_not_iteration,
2435            child_process_item_key = itemkey || '_' || l_not_iteration,
2436 	   child_process_item_type = 'APINVAPR',
2437            item_key = itemkey || '_' || l_not_iteration,
2438 	   item_type = 'APINVAPR'
2439     WHERE  role_Name = Group_Approvers_Rec.role_name
2440     AND    invoice_key = itemkey
2441     AND    line_number IS NOT NULL
2442     AND    notification_status = 'PEND';
2443 
2444   END LOOP;
2445 
2446   IF l_pend <> 0 THEN
2447     resultout := wf_engine.eng_completed||':'||'MORE';
2448   ELSE
2449     l_debug_info := 'Into the No Pending Case';
2450     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2451        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2452                       l_api_name,l_debug_info);
2453     END IF;
2454 
2455     BEGIN
2456       SELECT SUM(DECODE(notification_status, 'SENT', 1, 0)),
2457              SUM(DECODE(notification_status, 'COMP', 1, 0))
2458       INTO   l_sent,
2459              l_comp
2460       FROM   ap_apinv_approvers
2461       WHERE  invoice_key = itemkey
2462       GROUP BY invoice_key;
2463     EXCEPTION
2464       WHEN OTHERS THEN
2465         l_sent := 0;
2466         l_comp := 0;
2467     END;
2468     l_debug_info := l_api_name ||
2469                     ': sent = ' || l_sent ||
2470 			  ', complete = ' || l_comp;
2471     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2472        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2473                       l_api_name,l_debug_info);
2474     END IF;
2475 
2476     --None sent at all
2477     IF l_sent = 0 and l_comp = 0 THEN
2478 
2479       l_debug_info := 'Into l_sent = 0 and l_comp = 0 case';
2480       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2481          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2482                         l_api_name,l_debug_info);
2483       END IF;
2484 
2485       l_debug_info := 'Setting Approval Status for Line Records';
2486       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2487          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2488                         l_api_name,l_debug_info);
2489       END IF;
2490 
2491       /* For Invoices whose source is ISP there should always be
2492          an approver. So if there are no approvers we should mark
2493 	 them as Rejected. */
2494 
2495       SELECT invoice_type_lookup_code
2496       INTO   l_invoice_type_lookup_code
2497       FROM   ap_invoices_all
2498       WHERE  invoice_id = l_invoice_id;
2499 
2500       UPDATE AP_INVOICE_LINES_ALL
2501       SET WFApproval_Status = DECODE(l_invoice_type_lookup_code
2502                                     ,'INVOICE REQUEST','REJECTED'
2503                                     ,'CREDIT MEMO REQUEST','REJECTED'
2504                                     ,'NOT REQUIRED')
2505       WHERE Invoice_Id = l_invoice_id
2506       AND WFApproval_Status = 'INITIATED';
2507 
2508       l_debug_info := 'Cleanup ap_apinv_approvers';
2509       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2510          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2511                         l_api_name,l_debug_info);
2512       END IF;
2513 
2514 
2515       DELETE FROM AP_APINV_APPROVERS
2516       WHERE Invoice_Id = l_invoice_id;
2517 
2518 
2519       l_debug_info := 'Set the Result to Finish';
2520       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2521          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2522                         l_api_name,l_debug_info);
2523       END IF;
2524 
2525       resultout := wf_engine.eng_completed||':'||'FINISH';
2526 
2527     ELSIF l_sent >0 THEN
2528 
2529       l_debug_info := 'Set the Result to Finish';
2530       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2531          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2532                         l_api_name,l_debug_info);
2533       END IF;
2534       resultout := wf_engine.eng_completed||':'||'WAIT';
2535 
2536     ELSIF l_sent = 0 AND l_comp >0 THEN
2537 
2538       l_debug_info := l_api_name ||': all complete but none sent';
2539       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2540          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2541                         l_api_name,l_debug_info);
2542       END IF;
2543 
2544       l_debug_info := 'Set the Invoice Line Approval Status to Approved if it was part of the approval process';
2545       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2546          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2547                         l_api_name,l_debug_info);
2548       END IF;
2549 
2550       UPDATE   AP_INVOICE_LINES_ALL
2551       SET      WFApproval_Status = 'WFAPPROVED'
2552       WHERE    Invoice_Id = l_invoice_id
2553       AND      WFApproval_Status = 'INITIATED'
2554       AND      Line_Number IN (SELECT DISTINCT Line_Number
2555                                FROM   AP_APINV_APPROVERS
2556                                WHERE  invoice_id = l_invoice_id
2557                                AND    Invoice_Iteration = l_iteration
2558 			       AND    NOTIFICATION_STATUS = 'COMP');
2559 
2560       l_debug_info := 'Set the Invoice Line Approval Status to Not Required if it was not part of the approval process';
2561       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2562          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2563                         l_api_name,l_debug_info);
2564       END IF;
2565 
2566       UPDATE AP_INVOICE_LINES_ALL
2567       SET WFApproval_Status = 'NOT REQUIRED'
2568       WHERE Invoice_Id = l_invoice_id
2569       AND WFApproval_Status = 'INITIATED';
2570 
2571       l_debug_info := 'Clear the ap_apinv_approvers';
2572       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2573          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2574                         l_api_name,l_debug_info);
2575       END IF;
2576 
2577 
2578       DELETE FROM AP_APINV_APPROVERS
2579       WHERE Invoice_Id = l_invoice_id;
2580 
2581 
2582 
2583       resultout := wf_engine.eng_completed||':'||'FINISH';
2584     END IF; -- sent/complete checks
2585   END IF;
2586 
2587 EXCEPTION
2588 
2589 WHEN OTHERS
2590    THEN
2591        IF Group_Approvers%ISOpen THEN
2592          Close Group_Approvers;
2593        END IF;
2594         WF_CORE.CONTEXT('APINVAPR','identify_approvers',itemtype,
2595                         itemkey, to_char(actid), funcmode);
2596         RAISE;
2597 END identify_approvers;
2598 
2599 PROCEDURE launch_approval_notifications(itemtype IN VARCHAR2,
2600                         itemkey IN VARCHAR2,
2601                         actid   IN NUMBER,
2602                         funcmode IN VARCHAR2,
2603                         resultout  OUT NOCOPY VARCHAR2 ) IS
2604 
2605   l_invoice_id number;
2606   l_iteration  number;
2607   l_rowid rowid := null;
2608   l_notification_key varchar2(320);
2609   l_org_id number;
2610 
2611   l_invoice_supplier_name       VARCHAR2(240);
2612   l_invoice_supplier_site       VARCHAR2(15);
2613   l_invoice_number              VARCHAR2(50);
2614   l_invoice_date                DATE;
2615   l_invoice_description         VARCHAR2(240);
2616   l_invoice_total               NUMBER;
2617   l_invoice_currency_code       VARCHAR2(15);
2618   l_role_name                   VARCHAR2(320);
2619   l_orig_system                 ap_apinv_approvers.orig_system%TYPE;
2620   l_orig_system_id              number(15);
2621   l_invoice_iteration           number;
2622   l_notification_iteration      number;
2623   l_approver_name               varchar2(360);
2624 
2625   CURSOR Notif_Process IS
2626   SELECT distinct role_name
2627   FROM   ap_apinv_approvers
2628   WHERE  notification_status = 'PEND'
2629   AND    invoice_key = itemKey
2630   AND    line_number is NOT NULL
2631   AND    notification_key IS NOT NULL;
2632 
2633   l_api_name	CONSTANT VARCHAR2(200) := 'launch_approval_notifications';
2634   l_debug_info		VARCHAR2(2000);
2635 
2636 BEGIN
2637 
2638   IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
2639      FND_LOG.STRING(G_LEVEL_PROCEDURE,
2640                     G_MODULE_NAME||l_api_name,
2641                     'AP_WORKFLOW_PKG.'|| l_api_name);
2642   END IF;
2643   l_debug_info := l_api_name ||
2644                   ': get variables from workflow: itemtype = ' ||
2645                    itemtype ||
2646 			', itemkey = ' || itemkey;
2647   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2648       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2649                      l_api_name,l_debug_info);
2650   END IF;
2651 
2652   l_invoice_id := WF_ENGINE.GETITEMATTRNumber
2653                   (itemtype,
2654                    itemkey,
2655                    'INVOICE_ID');
2656   l_org_id := WF_ENGINE.GETITEMATTRNumber
2657                  (itemtype,
2658                   itemkey,
2659                   'ORG_ID');
2660 
2661   l_debug_info := l_api_name ||': invoice_id = ' ||
2662                   l_invoice_id ||
2663 			', Org ID = ' ||
2664                   l_org_id;
2665   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2666      FND_LOG.STRING(G_LEVEL_STATEMENT,
2667                     G_MODULE_NAME||l_api_name,
2668                     l_debug_info);
2669   END IF;
2670 
2671 -- Bug5892455 added the union for 'Payment Request' type invoices
2672 -- For payment Request type invoices the vendor is a customer whose
2673 -- data wil be there in hz_party tables
2674 SELECT     vendor_name,
2675            vendor_site_code,
2676            invoice_num,
2677            invoice_date,
2678            description,
2679            invoice_amount,
2680            invoice_currency_code
2681 INTO
2682             l_invoice_supplier_name,
2683             l_invoice_supplier_site,
2684             l_invoice_number,
2685             l_invoice_date,
2686             l_invoice_description,
2687             l_invoice_total,
2688             l_invoice_currency_code
2689 FROM
2690 (SELECT
2691              PV.vendor_name vendor_name,
2692              PVS.vendor_site_code vendor_site_code,
2693              AI.invoice_num,
2694              AI.invoice_date,
2695              AI.description,
2696              NVL(AI.invoice_amount, 0) invoice_amount,
2697              AI.invoice_currency_code
2698   FROM
2699              ap_invoices_all AI,
2700              po_vendors PV,
2701              po_vendor_sites_all PVS
2702   WHERE
2703              AI.invoice_id = l_invoice_id AND
2704              AI.vendor_id = PV.vendor_id AND
2705              AI.invoice_type_lookup_code  <> 'PAYMENT REQUEST' AND
2706              AI.vendor_site_id = PVS.vendor_site_id(+)
2707 UNION ALL
2708   SELECT
2709              HZP.party_name vendor_name,
2710              HZPS.party_site_name vendor_site_code,
2711              AI.invoice_num,
2712              AI.invoice_date,
2713              AI.description,
2714              NVL(AI.invoice_amount, 0) invoice_amount,
2715              AI.invoice_currency_code
2716    FROM
2717              ap_invoices_all AI,
2718              hz_parties HZP,
2719              hz_party_sites HZPS
2720    WHERE
2721              AI.invoice_id = l_invoice_id AND
2722              AI.party_id = HZP.party_id AND
2723              AI.invoice_type_lookup_code  = 'PAYMENT REQUEST' and
2724              AI.party_site_id = HZPS.party_site_id(+))
2725 ;
2726   l_debug_info := l_api_name ||
2727                   ': supplier_name ' || l_invoice_supplier_name ||
2728                   ', invoice_num = '|| l_invoice_number ||
2729                   ', invoice_total = '|| l_invoice_total;
2730   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2731       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2732                      l_api_name,l_debug_info);
2733   END IF;
2734 
2735 
2736 
2737   l_debug_info := 'Process Notif_Process Cursor';
2738   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2739       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2740                      l_api_name,l_debug_info);
2741   END IF;
2742 
2743   FOR Notif_Process_Rec IN Notif_Process LOOP
2744 
2745 
2746     IF Notif_Process_Rec.role_name IS NOT NULL THEN
2747 
2748       l_debug_info := l_api_name ||
2749                       ': Role Name ' || Notif_Process_Rec.role_name;
2750       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2751           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2752                          l_api_name,l_debug_info);
2753       END IF;
2754 
2755       l_debug_info := 'Before: Create The Lines Sub Process';
2756       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2757           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2758                          l_api_name,l_debug_info);
2759       END IF;
2760 
2761       SELECT notification_key,
2762              invoice_iteration,
2763              notification_iteration,
2764              orig_system,
2765              orig_system_id
2766       INTO   l_notification_key,
2767              l_invoice_iteration,
2768              l_notification_iteration,
2769              l_orig_system,
2770              l_orig_system_id
2771       FROM   ap_apinv_approvers
2772       WHERE  role_name = Notif_Process_Rec.role_name
2773       AND    notification_status = 'PEND'
2774       AND    invoice_key = itemKey
2775       AND    rownum = 1
2776       AND    notification_key is not null; --bug 16414516
2777 
2778       l_debug_info := l_api_name ||
2779                       ': Notification Key ' || l_notification_key;
2780       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2781           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2782                          l_api_name,l_debug_info);
2783       END IF;
2784 
2785       WF_ENGINE.createProcess('APINVAPR',
2786                                l_notification_key,
2787                                'APPROVAL_INVOICE_LINES');
2788 
2789       l_debug_info := 'After: Create The Lines Sub Process';
2790       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2791           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2792                          l_api_name,l_debug_info);
2793       END IF;
2794 
2795       l_debug_info := 'Set the attributes';
2796       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2797           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2798                          l_api_name,l_debug_info);
2799       END IF;
2800 
2801       WF_DIRECTORY.GetUserName(l_orig_system,
2802                                 l_orig_system_id,
2803                                 Notif_Process_Rec.role_name,
2804                                 l_approver_name);
2805 
2806       WF_ENGINE.SetItemAttrNumber('APINVAPR',
2807                                   l_notification_key,
2808                                   'ORG_ID',
2809                                   l_org_id);
2810       WF_ENGINE.SetItemAttrNumber('APINVAPR',
2811                                   l_notification_key,
2812                                   'INVOICE_ID',
2813                                   l_invoice_id);
2814       WF_ENGINE.SetItemAttrNumber('APINVAPR',
2815                                   l_notification_key,
2816                                   'INVOICE_ITERATION',
2817                                   l_invoice_iteration);
2818       WF_ENGINE.SetItemAttrNumber('APINVAPR',
2819                                   l_notification_key,
2820                                   'NOTF_ITERATION',
2821                                   l_notification_iteration);
2822       WF_ENGINE.SetItemAttrNumber('APINVAPR',
2823                                   l_notification_key,
2824                                   'APPROVER_ID',
2825                                   l_orig_system_id);
2826       WF_ENGINE.SETITEMATTRTEXT('APINVAPR',
2827                                 l_notification_key,
2828                                 'ROLE_NAME',
2829                                 Notif_Process_Rec.role_name);
2830       WF_ENGINE.SETITEMATTRTEXT('APINVAPR',
2831                                  l_notification_key,
2832                                  'APPROVER_NAME',
2833                                  l_approver_name);
2834       WF_ENGINE.SETITEMATTRTEXT('APINVAPR',
2835                                 l_notification_key,
2836                                 'INVOICE_SUPPLIER_NAME',
2837                                 l_invoice_supplier_name);
2838       WF_ENGINE.SETITEMATTRTEXT('APINVAPR',
2839                                 l_notification_key,
2840                                 'INVOICE_SUPPLIER_SITE',
2841                                 l_invoice_supplier_site);
2842       WF_ENGINE.SETITEMATTRTEXT('APINVAPR',
2843                                 l_notification_key,
2844                                 'INVOICE_NUMBER',
2845                                 l_invoice_number);
2846       WF_ENGINE.SETITEMATTRDATE('APINVAPR',
2847                                 l_notification_key,
2848                                 'INVOICE_DATE',
2849                                 l_invoice_date);
2850       WF_ENGINE.SETITEMATTRTEXT('APINVAPR',
2851                                 l_notification_key,
2852                                 'INVOICE_DESCRIPTION',
2853                                 l_invoice_description);
2854       WF_ENGINE.SETITEMATTRTEXT('APINVAPR',
2855                                 l_notification_key,
2856                                 'NOTIFICATION_KEY',
2857                                 l_notification_key);
2858       WF_ENGINE.SETITEMATTRNUMBER
2859     		(
2860       		itemtype => 'APINVAPR',
2861       		itemkey => l_notification_key,
2862       		aname => 'INVOICE_TOTAL',
2863       		avalue => l_invoice_total
2864     		);
2865       WF_ENGINE.SETITEMATTRTEXT
2866     		(
2867       		itemtype => 'APINVAPR',
2868       		itemkey => l_notification_key,
2869       		aname => 'INVOICE_CURRENCY_CODE',
2870       		avalue => l_invoice_currency_code
2871     		);
2872 
2873       l_debug_info := 'Select the Role and Approver information';
2874       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2875           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2876                          l_api_name,l_debug_info);
2877       END IF;
2878       l_debug_info := 'Define Parent Child Association';
2879       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2880           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2881                          l_api_name,l_debug_info);
2882       END IF;
2883 
2884       WF_ENGINE.setItemParent('APINVAPR',
2885                               l_notification_key,
2886                               'APINVAPR',
2887                               itemKey,
2888                               null);
2889 
2890       l_debug_info := 'Start the child process';
2891       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2892           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2893                          l_api_name,l_debug_info);
2894       END IF;
2895 
2896       wf_engine.startProcess('APINVAPR', l_notification_key);
2897 
2898       l_debug_info := 'Update the ap_apinv_approvers notification status';
2899       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2900           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2901                          l_api_name,l_debug_info);
2902       END IF;
2903 
2904       UPDATE ap_apinv_approvers
2905       SET    notification_status = 'SENT'
2906       WHERE  notification_key = l_notification_key;
2907 
2908     END IF;
2909 
2910   END LOOP;
2911 
2912   resultout := wf_engine.eng_completed||':'||'Y';
2913 
2914 EXCEPTION
2915 
2916 WHEN OTHERS
2917    THEN
2918         IF Notif_Process%ISOpen THEN
2919           CLOSE Notif_Process;
2920         END IF;
2921         WF_CORE.CONTEXT('APINVAPR','launch_approval_notifications',itemtype,
2922                         itemkey, to_char(actid), funcmode);
2923         RAISE;
2924 END launch_approval_notifications;
2925 
2926 PROCEDURE launch_neg_notifications(itemtype IN VARCHAR2,
2927                         itemkey IN VARCHAR2,
2928                         actid   IN NUMBER,
2929                         funcmode IN VARCHAR2,
2930                         resultout  OUT NOCOPY VARCHAR2 ) IS
2931 
2932 BEGIN
2933    resultout := wf_engine.eng_completed||':'||'Y';
2934 EXCEPTION
2935 
2936 WHEN OTHERS
2937    THEN
2938         WF_CORE.CONTEXT('APINVAPR','launch_neg_notifications',itemtype,
2939                         itemkey, to_char(actid), funcmode);
2940         RAISE;
2941 END launch_neg_notifications;
2942 
2943 PROCEDURE process_doc_rejection(itemtype IN VARCHAR2,
2944                         itemkey IN VARCHAR2,
2945                         actid   IN NUMBER,
2946                         funcmode IN VARCHAR2,
2947                         resultout  OUT NOCOPY VARCHAR2 ) IS
2948    l_invoice_id    NUMBER(15);
2949    l_iteration     NUMBER(15);
2950    l_comments      VARCHAR2(240);
2951    l_amount       ap_invoices_all.invoice_amount%TYPE;
2952    l_status        VARCHAR2(50);
2953    l_org_id        NUMBER(15);
2954    l_user_id       NUMBER(15);
2955    l_login_id      NUMBER(15);
2956    l_api_name      CONSTANT VARCHAR2(200) := 'process_doc_rejection';
2957    l_debug_info    VARCHAR2(2000);
2958    l_hist_rec      AP_INV_APRVL_HIST%ROWTYPE;
2959    l_approver_name VARCHAR2(150);
2960    l_approver_id   NUMBER;
2961    l_esc_flag      VARCHAR2(1);
2962    l_esc_approver_name VARCHAR2(150);
2963    l_document_approver VARCHAR2(150);
2964    l_role VARCHAR2(150);
2965    l_esc_role_name VARCHAR2(150);
2966    l_esc_approver_id   NUMBER;
2967    l_invoice_total     NUMBER;
2968 
2969 
2970 BEGIN
2971 
2972    l_debug_info := 'Start';
2973    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2974           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
2975                          l_api_name,l_debug_info);
2976    END IF;
2977 
2978    l_invoice_id := WF_ENGINE.GetItemAttrNumber(itemtype,
2979                              itemkey,
2980                              'INVOICE_ID');
2981 
2982    l_comments := WF_ENGINE.GetItemAttrText(itemtype,
2983                              itemkey,
2984                              'WF_NOTE');
2985 
2986    l_org_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
2987                    itemkey,
2988                    'ORG_ID');
2989 
2990    l_invoice_total := WF_ENGINE.GETITEMATTRNumber(itemtype,
2991                    itemkey,
2992                    'INVOICE_TOTAL');
2993 
2994    l_approver_name := WF_ENGINE.GetItemAttrText(itemtype,
2995                              itemkey,
2996                              'APPROVER_NAME');
2997 
2998    l_esc_role_name := WF_ENGINE.GetItemAttrText(itemtype,
2999                              itemkey,
3000                              'ESC_ROLE_NAME');
3001 
3002    l_document_approver := WF_ENGINE.GetItemAttrText(itemtype,
3003                              itemkey,
3004                              'DOCUMENT_APPROVER');
3005 
3006    l_approver_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
3007                    itemkey,
3008                    'APPROVER_ID');
3009 
3010    l_esc_flag  := WF_ENGINE.GetItemAttrText(itemtype,
3011                              itemkey,
3012                              'ESCALATED');
3013 
3014    l_esc_approver_name := WF_ENGINE.GetItemAttrText(itemtype,
3015                              itemkey,
3016                              'ESC_APPROVER_NAME');
3017 
3018    l_esc_approver_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
3019                    itemkey,
3020                    'ESC_APPROVER_ID');
3021 
3022    l_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
3023                         itemkey,
3024                         'ITERATION');
3025 
3026 
3027    l_user_id := nvl(to_number(fnd_profile.value('USER_ID')),-1);
3028    l_login_id := nvl(to_number(fnd_profile.value('LOGIN_ID')),-1);
3029    IF nvl(l_esc_flag,'N') = 'N' THEN
3030       l_role := l_document_approver;
3031    ELSE
3032       l_role := l_esc_role_name;
3033    END IF;
3034 
3035    l_debug_info := 'Before calling AME esc_flag : ' || l_esc_flag;
3036    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3037           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
3038                          l_api_name,l_debug_info);
3039    END IF;
3040    AME_API2.updateApprovalStatus2(applicationIdIn => 200,
3041              transactionIdIn     => to_char(l_invoice_id),
3042              approvalStatusIn    => AME_UTIL.rejectStatus,
3043              approverNameIn  => l_role,
3044              transactionTypeIn =>  'APINV',
3045              itemClassIn     => ame_util.headerItemClassName,
3046              itemIdIn        => to_char(l_invoice_id));
3047 
3048    fnd_client_info.set_org_context(l_org_id);
3049 
3050    l_hist_rec.HISTORY_TYPE := 'DOCUMENTAPPROVAL';
3051    l_hist_rec.INVOICE_ID   := l_invoice_id;
3052    l_hist_rec.ITERATION    := l_iteration;
3053    l_hist_rec.RESPONSE     := 'REJECT';
3054    l_hist_rec.APPROVER_COMMENTS     := l_comments;
3055    IF nvl(l_esc_flag,'N') = 'N' THEN
3056       l_hist_rec.APPROVER_ID  := l_approver_id;
3057       l_hist_rec.APPROVER_NAME:= l_approver_name;
3058    ELSE
3059       l_hist_rec.APPROVER_ID  := l_esc_approver_id;
3060       l_hist_rec.APPROVER_NAME:= l_esc_approver_name;
3061    END IF;
3062    l_hist_rec.CREATED_BY   := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
3063    l_hist_rec.CREATION_DATE:= sysdate;
3064    l_hist_rec.LAST_UPDATE_DATE := sysdate;
3065    l_hist_rec.LAST_UPDATED_BY  := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
3066    l_hist_rec.LAST_UPDATE_LOGIN := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),-1);
3067    l_hist_rec.ORG_ID            := l_org_id;
3068    l_hist_rec.AMOUNT_APPROVED   := l_invoice_total;
3069 
3070    l_debug_info := 'Before calling insert_history_table';
3071    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3072           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
3073                          l_api_name,l_debug_info);
3074    END IF;
3075 
3076    insert_history_table(p_hist_rec => l_hist_rec);
3077    l_debug_info := 'Before UPDATE AP_INVOICES_ALL';
3078    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3079           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
3080                          l_api_name,l_debug_info);
3081    END IF;
3082 
3083    UPDATE AP_INVOICES_ALL
3084    SET    wfapproval_status = 'REJECTED'
3085    WHERE invoice_id = l_invoice_id;
3086 
3087    l_debug_info := 'End';
3088    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3089           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
3090                          l_api_name,l_debug_info);
3091    END IF;
3092 
3093    resultout := wf_engine.eng_completed||':'||'Y';
3094 EXCEPTION
3095 WHEN OTHERS
3096    THEN
3097         WF_CORE.CONTEXT('APINVAPR','process_doc_rejection',itemtype,
3098                         itemkey, to_char(actid), funcmode);
3099         RAISE;
3100 END process_doc_rejection;
3101 PROCEDURE process_doc_approval(itemtype IN VARCHAR2,
3102                         itemkey IN VARCHAR2,
3103                         actid   IN NUMBER,
3104                         funcmode IN VARCHAR2,
3105                         resultout  OUT NOCOPY VARCHAR2 ) IS
3106    l_invoice_id    NUMBER(15);
3107    l_comments      VARCHAR2(240);
3108    l_amount       ap_invoices_all.invoice_amount%TYPE;
3109    l_status        VARCHAR2(50);
3110    l_org_id        NUMBER(15);
3111    l_user_id       NUMBER(15);
3112    l_login_id      NUMBER(15);
3113    l_api_name      CONSTANT VARCHAR2(200) := 'process_doc_approval';
3114    l_debug_info    VARCHAR2(2000);
3115    l_hist_rec      AP_INV_APRVL_HIST%ROWTYPE;
3116    l_approver_name VARCHAR2(150);
3117    l_approver_id   NUMBER;
3118    l_esc_flag      VARCHAR2(1);
3119    l_esc_approver_name  VARCHAR2(150);
3120    l_document_approver  VARCHAR2(150);
3121    l_role               VARCHAR2(150);
3122    l_esc_role_name      VARCHAR2(150);
3123    l_esc_approver_id    NUMBER;
3124    l_invoice_total      NUMBER;
3125    l_iteration             NUMBER;
3126    l_notf_iteration        NUMBER;
3127 -- added for the delegation enhancement
3128    l_role_name             VARCHAR2(50);
3129    l_esc_role_actual     VARCHAR2(50);
3130 
3131 
3132 
3133 
3134 BEGIN
3135 
3136    l_debug_info := 'Start';
3137    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3138           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
3139                          l_api_name,l_debug_info);
3140    END IF;
3141 
3142    l_invoice_id := WF_ENGINE.GetItemAttrNumber(itemtype,
3143                              itemkey,
3144                              'INVOICE_ID');
3145 
3146    l_comments := WF_ENGINE.GetItemAttrText(itemtype,
3147                              itemkey,
3148                              'WF_NOTE');
3149 
3150    l_org_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
3151                    itemkey,
3152                    'ORG_ID');
3153 
3154    l_invoice_total := WF_ENGINE.GETITEMATTRNumber(itemtype,
3155                    itemkey,
3156                    'INVOICE_TOTAL');
3157 
3158    l_approver_name := WF_ENGINE.GetItemAttrText(itemtype,
3159                              itemkey,
3160                              'APPROVER_NAME');
3161 
3162    l_esc_role_name := WF_ENGINE.GetItemAttrText(itemtype,
3163                              itemkey,
3164                              'ESC_ROLE_NAME');
3165 
3166    l_document_approver := WF_ENGINE.GetItemAttrText(itemtype,
3167                              itemkey,
3168                              'DOCUMENT_APPROVER');
3169 
3170    l_approver_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
3171                    itemkey,
3172                    'APPROVER_ID');
3173 
3174    l_esc_flag  := WF_ENGINE.GetItemAttrText(itemtype,
3175                              itemkey,
3176                              'ESCALATED');
3177 
3178    l_esc_approver_name := WF_ENGINE.GetItemAttrText(itemtype,
3179                              itemkey,
3180                              'ESC_APPROVER_NAME');
3181 
3182    l_esc_approver_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
3183                    itemkey,
3184                    'ESC_APPROVER_ID');
3185 
3186    l_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
3187                         itemkey,
3188                         'ITERATION');
3189 
3190    l_notf_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
3191                         itemkey,
3192                         'NOTF_ITERATION');
3193 	-- added for the delegation enhancement
3194 	  l_role_name      :=WF_ENGINE.GetItemAttrText(itemtype,
3195                                     itemkey,
3196                                     'ROLE_ACTUAL');
3197    l_esc_role_actual := WF_ENGINE.GetItemAttrText(itemtype,
3198                                             itemkey,
3199                                             'ESC_ROLE_ACTUAL');
3200 -- added for the delegation enhancement
3201    l_user_id := nvl(to_number(fnd_profile.value('USER_ID')),-1);
3202    l_login_id := nvl(to_number(fnd_profile.value('LOGIN_ID')),-1);
3203    IF nvl(l_esc_flag,'N') = 'N' THEN
3204       l_role := l_role_name; -- added for the delegation enhancement
3205       --l_role := l_document_approver;
3206    ELSE
3207       l_role := l_esc_role_actual ;
3208       --l_role := l_esc_role_name;
3209    END IF;
3210 
3211    l_debug_info := 'Before calling AME esc_flag : ' || l_esc_flag;
3212    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3213           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
3214                          l_api_name,l_debug_info);
3215    END IF;
3216    AME_API2.updateApprovalStatus2(applicationIdIn => 200,
3217              transactionIdIn     => to_char(l_invoice_id),
3218              approvalStatusIn    => AME_UTIL.approvedStatus,
3219              approverNameIn  => l_role,
3220              transactionTypeIn =>  'APINV',
3221              itemClassIn     => ame_util.headerItemClassName,
3222              itemIdIn        => to_char(l_invoice_id));
3223 
3224    fnd_client_info.set_org_context(l_org_id);
3225 
3226    l_hist_rec.HISTORY_TYPE := 'DOCUMENTAPPROVAL';
3227    l_hist_rec.INVOICE_ID   := l_invoice_id;
3228    l_hist_rec.ITERATION    := l_iteration;
3229    l_hist_rec.NOTIFICATION_ORDER := l_notf_iteration;
3230    l_hist_rec.RESPONSE     := 'APPROVED';
3231    IF nvl(l_esc_flag,'N') = 'N' THEN
3232       l_hist_rec.APPROVER_ID  := l_approver_id;
3233       l_hist_rec.APPROVER_NAME:= l_approver_name;
3234    ELSE
3235       l_hist_rec.APPROVER_ID  := l_esc_approver_id;
3236       l_hist_rec.APPROVER_NAME:= l_esc_approver_name;
3237    END IF;
3238    l_hist_rec.CREATED_BY   := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
3239    l_hist_rec.CREATION_DATE:= sysdate;
3240    l_hist_rec.LAST_UPDATE_DATE := sysdate;
3241    l_hist_rec.LAST_UPDATED_BY  := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
3242    l_hist_rec.LAST_UPDATE_LOGIN := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),-1);
3243    l_hist_rec.ORG_ID            := l_org_id;
3244    l_hist_rec.AMOUNT_APPROVED   := l_invoice_total;
3245    l_hist_rec.APPROVER_COMMENTS := l_comments;
3246 
3247    l_debug_info := 'Before calling insert_history_table';
3248    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3249           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
3250                          l_api_name,l_debug_info);
3251    END IF;
3252 
3253    insert_history_table(p_hist_rec => l_hist_rec);
3254 
3255 
3256 -- Set WF_NOTE to null
3257 -- 01/24/2007
3258 
3259   WF_ENGINE.SetItemAttrText('APINVAPR',
3260                         itemkey,
3261                         'WF_NOTE',
3262                         null);
3263 -- added for the delegation enhancement
3264 
3265   WF_ENGINE.SetItemAttrText(itemtype,
3266                             itemkey,
3267                             'FORWARD_ROLE',
3268                             null);
3269 -- added for the delegation enhancement
3270 
3271 
3272 
3273    l_debug_info := 'End';
3274    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3275           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
3276                          l_api_name,l_debug_info);
3277    END IF;
3278 
3279 
3280    resultout := wf_engine.eng_completed||':'||'Y';
3281 EXCEPTION
3282 WHEN OTHERS
3283    THEN
3284         WF_CORE.CONTEXT('APINVAPR','process_doc_approval',itemtype,
3285                         itemkey, to_char(actid), funcmode);
3286         RAISE;
3287 END process_doc_approval;
3288 
3289 PROCEDURE process_lines_rejection(itemtype IN VARCHAR2,
3290                         itemkey IN VARCHAR2,
3291                         actid   IN NUMBER,
3292                         funcmode IN VARCHAR2,
3293                         resultout  OUT NOCOPY VARCHAR2 ) IS
3294 
3295    --Define cursor for lines affected by notification
3296    --Note that Invoice_Key s/b the same for all records in the cursor
3297    --but I want to avoid another select on the table
3298    CURSOR   Items_Cur(itemkey IN VARCHAR2) IS
3299    SELECT invap.Item_Class, invap.Item_Id, invap.Role_Name,
3300           invap.Invoice_Key, al.line_number, al.amount
3301    FROM   AP_APINV_APPROVERS invap, AP_INVOICE_LINES_ALL al
3302    WHERE  invap.Notification_Key = itemkey
3303    AND    al.line_number = invap.line_number
3304    AND    al.invoice_id  = invap.invoice_id;
3305 
3306    l_api_name      CONSTANT VARCHAR2(200) := 'process_lines_rejection';
3307    l_invoice_id    NUMBER;
3308    l_level         VARCHAR2(20);
3309    l_result        VARCHAR2(20);
3310    l_invoice_key   VARCHAR2(50);
3311    l_name          AP_APINV_APPROVERS.ROLE_NAME%TYPE;
3312    l_item_class    AP_APINV_APPROVERS.ITEM_CLASS%TYPE;
3313    l_item_id       AP_APINV_APPROVERS.ITEM_ID%TYPE;
3314    l_debug_info    VARCHAR2(2000);
3315    l_user_id       NUMBER(15);
3316    l_login_id      NUMBER(15);
3317    l_hist_rec      AP_INV_APRVL_HIST%ROWTYPE;
3318    l_approver_name VARCHAR2(150);
3319    l_approver_id   NUMBER;
3320    l_esc_flag      VARCHAR2(1);
3321    l_esc_approver_name VARCHAR2(150);
3322    l_role_name     VARCHAR2(150);
3323    l_role VARCHAR2(150);
3324    l_esc_role_name VARCHAR2(150);
3325    l_esc_approver_id   NUMBER;
3326    l_line_total     NUMBER;
3327    l_iteration     NUMBER(15);
3328    l_comments      VARCHAR2(240);
3329    l_org_id        NUMBER(15);
3330    l_line_number   ap_invoice_lines_all.line_number%TYPE;
3331    l_line_amount   ap_invoice_lines_all.amount%TYPE;
3332    l_notf_iteration        NUMBER;
3333 
3334 
3335 
3336 BEGIN
3337    l_debug_info := 'Start';
3338    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3339           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
3340                          l_api_name,l_debug_info);
3341    END IF;
3342    l_invoice_id := WF_ENGINE.GetItemAttrNumber(itemtype,
3343                                   itemkey,
3344                                   'INVOICE_ID');
3345 
3346 --commented for bug 13566970
3347   /* l_iteration := WF_ENGINE.GetItemAttrNumber(itemtype,
3348                              itemkey,
3349                              'ITERATION');  */
3350 -- added for bug 13566970
3351    l_iteration := WF_ENGINE.GetItemAttrNumber(itemtype,
3352                              itemkey,
3353                              'INVOICE_ITERATION');
3354  --end of bug 13566970
3355 
3356    l_comments := WF_ENGINE.GetItemAttrText(itemtype,
3357                              itemkey,
3358                              'WF_NOTE');
3359 
3360    l_org_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
3361                    itemkey,
3362                    'ORG_ID');
3363 
3364    l_approver_name := WF_ENGINE.GetItemAttrText(itemtype,
3365                              itemkey,
3366                              'APPROVER_NAME');
3367 
3368    l_esc_role_name := WF_ENGINE.GetItemAttrText(itemtype,
3369                              itemkey,
3370                              'ESC_ROLE_NAME');
3371 
3372    l_role_name := WF_ENGINE.GetItemAttrText(itemtype,
3373                              itemkey,
3374                              'ROLE_NAME');
3375 
3376    l_approver_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
3377                    itemkey,
3378                    'APPROVER_ID');
3379 
3380    l_esc_flag  := WF_ENGINE.GetItemAttrText(itemtype,
3381                              itemkey,
3382                              'ESCALATED');
3383    l_esc_approver_name := WF_ENGINE.GetItemAttrText(itemtype,
3384                              itemkey,
3385                              'ESC_APPROVER_NAME');
3386 
3387    l_esc_approver_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
3388                    itemkey,
3389                    'ESC_APPROVER_ID');
3390 
3391    l_notf_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
3392                         itemkey,
3393                         'NOTF_ITERATION');
3394 
3395    l_user_id := nvl(to_number(fnd_profile.value('USER_ID')),-1);
3396    l_login_id := nvl(to_number(fnd_profile.value('LOGIN_ID')),-1);
3397    IF nvl(l_esc_flag,'N') = 'N' THEN
3398       l_role := l_role_name;
3399    ELSE
3400       l_role := l_esc_role_name;
3401    END IF;
3402 
3403    l_debug_info := 'Before Update Approvers table';
3404    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3405           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
3406                          l_api_name,l_debug_info);
3407    END IF;
3408    --Update Approvers table
3409    UPDATE AP_APINV_APPROVERS
3410    SET Notification_status = 'COMP'
3411    WHERE Notification_Key = itemkey;
3412 
3413    fnd_client_info.set_org_context(l_org_id);
3414 
3415    l_hist_rec.HISTORY_TYPE := 'LINESAPPROVAL';
3416    l_hist_rec.INVOICE_ID   := l_invoice_id;
3417    l_hist_rec.ITERATION    := l_iteration;
3418    l_hist_rec.NOTIFICATION_ORDER := l_notf_iteration;
3419    l_hist_rec.APPROVER_COMMENTS := l_comments;
3420    l_hist_rec.RESPONSE     := 'REJECT';
3421    IF nvl(l_esc_flag,'N') = 'N' THEN
3422       l_hist_rec.APPROVER_ID  := l_approver_id;
3423       l_hist_rec.APPROVER_NAME:= l_approver_name;
3424    ELSE
3425       l_hist_rec.APPROVER_ID  := l_esc_approver_id;
3426       l_hist_rec.APPROVER_NAME:= l_esc_approver_name;
3427    END IF;
3428    l_hist_rec.APPROVER_COMMENTS := l_comments;
3429    l_hist_rec.CREATED_BY   := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
3430    l_hist_rec.CREATION_DATE:= sysdate;
3431    l_hist_rec.LAST_UPDATE_DATE := sysdate;
3432    l_hist_rec.LAST_UPDATED_BY  := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
3433    l_hist_rec.LAST_UPDATE_LOGIN := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),-1);
3434    l_hist_rec.ORG_ID            := l_org_id;
3435    --update AME status
3436    --For Future  check with ame as to when updateApprovalStatuses will be
3437    --available so there  will not be a need to loop.
3438    OPEN Items_Cur(itemkey);
3439    LOOP
3440 
3441       FETCH Items_Cur INTO l_item_class, l_item_id, l_name,
3442                                    l_invoice_key,l_line_number, l_line_amount;
3443       EXIT WHEN Items_Cur%NOTFOUND OR Items_Cur%NOTFOUND IS NULL;
3444 
3445       --update AME with response
3446       AME_API2.updateApprovalStatus2(applicationIdIn => 200,
3447                            transactionIdIn     => to_char(l_invoice_id),
3448                            approvalStatusIn    => AME_UTIL.rejectStatus,
3449                            approverNameIn  => l_name,
3450                            transactionTypeIn =>  'APINV',
3451                            itemClassIn     => l_item_class,
3452                            itemIdIn        => l_item_id);
3453       l_hist_rec.line_number   := l_line_number;
3454       l_hist_rec.AMOUNT_APPROVED   := l_line_amount;
3455 
3456       l_debug_info := 'Before calling insert_history_table for Line'
3457                        || l_line_number;
3458       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3459              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
3460                             l_api_name,l_debug_info);
3461       END IF;
3462 
3463       insert_history_table(p_hist_rec => l_hist_rec);
3464    END LOOP;
3465    CLOSE Items_Cur;
3466 
3467 
3468    l_user_id := nvl(to_number(fnd_profile.value('USER_ID')),-1);
3469    l_login_id := nvl(to_number(fnd_profile.value('LOGIN_ID')),-1);
3470    UPDATE AP_INVOICE_LINES_ALL  -- Bug 9388962
3471    SET    wfapproval_status = 'REJECTED'
3472           ,Last_Update_Date = sysdate
3473           ,Last_Updated_By = l_user_id
3474           ,Last_Update_Login = l_login_id
3475    WHERE invoice_id = l_invoice_id
3476    AND wfapproval_status <> 'MANUALLY APPROVED'
3477    AND line_number in (SELECT line_number
3478                        FROM ap_apinv_approvers
3479                        WHERE notification_key = itemkey);
3480    BEGIN
3481 
3482       SELECT invoice_key
3483       INTO   l_invoice_key
3484       FROM   AP_APINV_APPROVERS
3485       WHERE  Notification_Key = itemkey
3486       AND    rownum = 1;
3487    EXCEPTION
3488       WHEN NO_DATA_FOUND THEN
3489          l_debug_info := 'No Data Found in SELECT from AP_APINV_APPROVERS' ;
3490       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3491              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
3492                             l_api_name,l_debug_info);
3493       END IF;
3494       RAISE;
3495 
3496    END;
3497    l_debug_info := 'Before CompleteActivity APPROVAL_MAIN:BLOCK' ||
3498                    'l_invoice_key = ' || l_invoice_key;
3499    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3500           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
3501                          l_api_name,l_debug_info);
3502    END IF;
3503    wf_engine.CompleteActivity(
3504                         itemType => 'APINVAPR',
3505                         itemKey  => l_invoice_key,
3506                         activity => 'APPROVAL_MAIN:BLOCK',
3507                         result   => 'NULL');
3508 
3509    l_debug_info := 'End';
3510    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3511           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
3512                          l_api_name,l_debug_info);
3513    END IF;
3514    resultout := wf_engine.eng_completed||':'||'Y';
3515 EXCEPTION
3516 
3517 WHEN OTHERS
3518    THEN
3519         WF_CORE.CONTEXT('APINVAPR','process_lines_rejection',itemtype,
3520                         itemkey, to_char(actid), funcmode);
3521         RAISE;
3522 END process_lines_rejection;
3523 PROCEDURE process_lines_approval(itemtype IN VARCHAR2,
3524                         itemkey IN VARCHAR2,
3525                         actid   IN NUMBER,
3526                         funcmode IN VARCHAR2,
3527                         resultout  OUT NOCOPY VARCHAR2 ) IS
3528 
3529    --Define cursor for lines affected by notification
3530    --Note that Invoice_Key s/b the same for all records in the cursor
3531    --but I want to avoid another select on the table
3532    CURSOR   Items_Cur(itemkey IN VARCHAR2) IS
3533    SELECT invap.Item_Class, invap.Item_Id, invap.Role_Name,
3534           invap.Invoice_Key, al.line_number, al.amount
3535    FROM   AP_APINV_APPROVERS invap, AP_INVOICE_LINES_ALL al
3536    WHERE  invap.Notification_Key = itemkey
3537    AND    al.line_number = invap.line_number
3538    AND    al.invoice_id  = invap.invoice_id;
3539 
3540    l_api_name      CONSTANT VARCHAR2(200) := 'process_lines_approval';
3541    l_invoice_id    NUMBER;
3542    l_level         VARCHAR2(20);
3543    l_result        VARCHAR2(20);
3544    l_invoice_key   VARCHAR2(50);
3545    l_name          AP_APINV_APPROVERS.ROLE_NAME%TYPE;
3546    l_item_class    AP_APINV_APPROVERS.ITEM_CLASS%TYPE;
3547    l_item_id       AP_APINV_APPROVERS.ITEM_ID%TYPE;
3548    l_debug_info    VARCHAR2(2000);
3549    l_user_id       NUMBER(15);
3550    l_login_id      NUMBER(15);
3551    l_hist_rec      AP_INV_APRVL_HIST%ROWTYPE;
3552    l_approver_name VARCHAR2(150);
3553    l_approver_id   NUMBER;
3554    l_esc_flag      VARCHAR2(1);
3555    l_esc_approver_name VARCHAR2(150);
3556    l_role_name     VARCHAR2(150);
3557    l_role VARCHAR2(150);
3558    l_esc_role_name VARCHAR2(150);
3559    l_esc_approver_id   NUMBER;
3560    l_line_total     NUMBER;
3561    l_iteration     NUMBER(15);
3562    l_comments      VARCHAR2(240);
3563    l_org_id        NUMBER(15);
3564    l_line_number   ap_invoice_lines_all.line_number%TYPE;
3565    l_line_amount   ap_invoice_lines_all.amount%TYPE;
3566    l_notf_iteration        NUMBER;
3567 
3568 
3569 
3570 BEGIN
3571    l_debug_info := 'Start';
3572    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3573           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
3574                          l_api_name,l_debug_info);
3575    END IF;
3576    l_invoice_id := WF_ENGINE.GetItemAttrNumber(itemtype,
3577                                   itemkey,
3578                                   'INVOICE_ID');
3579 
3580 --commented for bug 13566970
3581   /* l_iteration := WF_ENGINE.GetItemAttrNumber(itemtype,
3582                              itemkey,
3583                              'ITERATION'); */
3584   --added for bug 13566970
3585     l_iteration := WF_ENGINE.GetItemAttrNumber(itemtype,
3586                              itemkey,
3587                              'INVOICE_ITERATION');
3588   --end of bug 13566970
3589 
3590    l_notf_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
3591                         itemkey,
3592                         'NOTF_ITERATION');
3593 
3594    l_comments := WF_ENGINE.GetItemAttrText(itemtype,
3595                              itemkey,
3596                              'WF_NOTE');
3597 
3598    l_org_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
3599                    itemkey,
3600                    'ORG_ID');
3601 
3602    l_approver_name := WF_ENGINE.GetItemAttrText(itemtype,
3603                              itemkey,
3604                              'APPROVER_NAME');
3605 
3606    l_esc_role_name := WF_ENGINE.GetItemAttrText(itemtype,
3607                              itemkey,
3608                              'ESC_ROLE_NAME');
3609 
3610    l_role_name := WF_ENGINE.GetItemAttrText(itemtype,
3611                              itemkey,
3612                              'ROLE_NAME');
3613 
3614    l_approver_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
3615                    itemkey,
3616                    'APPROVER_ID');
3617 
3618    l_esc_flag  := WF_ENGINE.GetItemAttrText(itemtype,
3619                              itemkey,
3620                              'ESCALATED');
3621    l_esc_approver_name := WF_ENGINE.GetItemAttrText(itemtype,
3622                              itemkey,
3623                              'ESC_APPROVER_NAME');
3624 
3625    l_esc_approver_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
3626                    itemkey,
3627                    'ESC_APPROVER_ID');
3628 	/*-- added for the delegation enhancement
3629 	  l_role_name      :=WF_ENGINE.GetItemAttrText(itemtype,
3630                                     itemkey,
3631                                     'ROLE_ACTUAL');
3632    l_esc_role_actual := WF_ENGINE.GetItemAttrText(itemtype,
3633                                             itemkey,
3634                                             'ESC_ROLE_ACTUAL');
3635 -- added for the delegation enhancement
3636 
3637    l_user_id := nvl(to_number(fnd_profile.value('USER_ID')),-1);
3638    l_login_id := nvl(to_number(fnd_profile.value('LOGIN_ID')),-1);
3639    IF nvl(l_esc_flag,'N') = 'N' THEN
3640       l_role := l_role_name; -- added for the delegation enhancement
3641       --l_role := l_document_approver;
3642    ELSE
3643       l_role := l_esc_role_actual ;
3644       --l_role := l_esc_role_name;
3645    END IF;  */
3646 
3647    l_user_id := nvl(to_number(fnd_profile.value('USER_ID')),-1);
3648    l_login_id := nvl(to_number(fnd_profile.value('LOGIN_ID')),-1);
3649    IF nvl(l_esc_flag,'N') = 'N' THEN
3650       l_role := l_role_name;
3651    ELSE
3652       l_role := l_esc_role_name;
3653    END IF;
3654 
3655    l_debug_info := 'Before Update Approvers table';
3656    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3657           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
3658                          l_api_name,l_debug_info);
3659    END IF;
3660    --Update Approvers table
3661    UPDATE AP_APINV_APPROVERS
3662    SET Notification_status = 'COMP'
3663    WHERE Notification_Key = itemkey;
3664 
3665    fnd_client_info.set_org_context(l_org_id);
3666 
3667    l_hist_rec.HISTORY_TYPE := 'LINESAPPROVAL';
3668    l_hist_rec.INVOICE_ID   := l_invoice_id;
3669    l_hist_rec.ITERATION    := l_iteration;
3670    l_hist_rec.NOTIFICATION_ORDER := l_notf_iteration;
3671    l_hist_rec.APPROVER_COMMENTS := l_comments;
3672    l_hist_rec.RESPONSE     := 'APPROVED';
3673    IF nvl(l_esc_flag,'N') = 'N' THEN
3674       l_hist_rec.APPROVER_ID  := l_approver_id;
3675       l_hist_rec.APPROVER_NAME:= l_approver_name;
3676    ELSE
3677       l_hist_rec.APPROVER_ID  := l_esc_approver_id;
3678       l_hist_rec.APPROVER_NAME:= l_esc_approver_name;
3679    END IF;
3680    l_hist_rec.CREATED_BY   := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
3681    l_hist_rec.CREATION_DATE:= sysdate;
3682    l_hist_rec.LAST_UPDATE_DATE := sysdate;
3683    l_hist_rec.LAST_UPDATED_BY  := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
3684    l_hist_rec.LAST_UPDATE_LOGIN := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),-1);
3685    l_hist_rec.ORG_ID            := l_org_id;
3686    --update AME status
3687    --For Future  check with ame as to when updateApprovalStatuses will be
3688    --available so there  will not be a need to loop.
3689 
3690       --bug 14369128
3691          AME_API2.updateApprovalStatus2(applicationIdIn => 200,
3692                            transactionIdIn     => to_char(l_invoice_id),
3693                            approvalStatusIn    => AME_UTIL.approvedStatus,
3694                            approverNameIn  => l_role, --bug 13919019
3695                            transactionTypeIn =>  'APINV',
3696                            itemClassIn     => l_item_class,
3697                            itemIdIn        => l_item_id);
3698 
3699    OPEN Items_Cur(itemkey);
3700    LOOP
3701 
3702       FETCH Items_Cur INTO l_item_class, l_item_id, l_name,
3703                                    l_invoice_key,l_line_number, l_line_amount;
3704       EXIT WHEN Items_Cur%NOTFOUND OR Items_Cur%NOTFOUND IS NULL;
3705 
3706   --bug 13919019 input variable l_name  changed to l_role
3707 
3708    /* --bug 14369128 commented
3709       --update AME with response
3710       AME_API2.updateApprovalStatus2(applicationIdIn => 200,
3711                            transactionIdIn     => to_char(l_invoice_id),
3712                            approvalStatusIn    => AME_UTIL.approvedStatus,
3713                            approverNameIn  => l_role, --bug 13919019
3714                            transactionTypeIn =>  'APINV',
3715                            itemClassIn     => l_item_class,
3716                            itemIdIn        => l_item_id);  */
3717 
3718       l_hist_rec.line_number   := l_line_number;
3719       l_hist_rec.AMOUNT_APPROVED   := l_line_amount;
3720 
3721       l_debug_info := 'Before calling insert_history_table for Line'
3722                        || l_line_number;
3723       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3724              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
3725                             l_api_name,l_debug_info);
3726       END IF;
3727 
3728       insert_history_table(p_hist_rec => l_hist_rec);
3729    END LOOP;
3730    CLOSE Items_Cur;
3731 
3732 
3733    l_user_id := nvl(to_number(fnd_profile.value('USER_ID')),-1);
3734    l_login_id := nvl(to_number(fnd_profile.value('LOGIN_ID')),-1);
3735    BEGIN
3736 
3737       SELECT invoice_key
3738       INTO   l_invoice_key
3739       FROM   AP_APINV_APPROVERS
3740       WHERE  Notification_Key = itemkey
3741       AND    rownum = 1;
3742    EXCEPTION
3743       WHEN NO_DATA_FOUND THEN
3744          l_debug_info := 'No Data Found in SELECT from AP_APINV_APPROVERS' ;
3745       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3746              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
3747                             l_api_name,l_debug_info);
3748       END IF;
3749       RAISE;
3750 
3751    END;
3752 
3753    l_debug_info := 'Before CompleteActivity APPROVAL_MAIN:BLOCK' ||
3754                    'l_invoice_key = ' || l_invoice_key;
3755    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3756           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
3757                          l_api_name,l_debug_info);
3758    END IF;
3759    wf_engine.CompleteActivity(
3760                         itemType => 'APINVAPR',
3761                         itemKey  => l_invoice_key,
3762                         activity => 'APPROVAL_MAIN:BLOCK',
3763                         result   => 'NULL');
3764 
3765    l_debug_info := 'End';
3766    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3767           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
3768                          l_api_name,l_debug_info);
3769    END IF;
3770    resultout := wf_engine.eng_completed||':'||'Y';
3771 EXCEPTION
3772 
3773 WHEN OTHERS
3774    THEN
3775         WF_CORE.CONTEXT('APINVAPR','process_lines_approval',itemtype,
3776                         itemkey, to_char(actid), funcmode);
3777         RAISE;
3778 END process_lines_approval;
3779 
3780 PROCEDURE set_document_approver(itemtype IN VARCHAR2,
3781                         itemkey IN VARCHAR2,
3782                         actid   IN NUMBER,
3783                         funcmode IN VARCHAR2,
3784                         resultout  OUT NOCOPY VARCHAR2 ) IS
3785 
3786 BEGIN
3787    resultout := wf_engine.eng_completed||':'||'Y';
3788 EXCEPTION
3789 
3790 WHEN OTHERS
3791    THEN
3792         WF_CORE.CONTEXT('APINVAPR','set_document_approver',itemtype,
3793                         itemkey, to_char(actid), funcmode);
3794         RAISE;
3795 END set_document_approver;
3796 
3797 PROCEDURE set_lines_approver(itemtype IN VARCHAR2,
3798                         itemkey IN VARCHAR2,
3799                         actid   IN NUMBER,
3800                         funcmode IN VARCHAR2,
3801                         resultout  OUT NOCOPY VARCHAR2 ) IS
3802 
3803 BEGIN
3804    resultout := wf_engine.eng_completed||':'||'Y';
3805 EXCEPTION
3806 
3807 WHEN OTHERS
3808    THEN
3809         WF_CORE.CONTEXT('APINVAPR','set_lines_approver',itemtype,
3810                         itemkey, to_char(actid), funcmode);
3811         RAISE;
3812 END set_lines_approver;
3813 
3814 /*
3815 APINVNEG - AP Invoice Approval Negotiation
3816 */
3817 
3818 PROCEDURE get_last_approver(itemtype IN VARCHAR2,
3819                         itemkey IN VARCHAR2,
3820                         actid   IN NUMBER,
3821                         funcmode IN VARCHAR2,
3822                         resultout  OUT NOCOPY VARCHAR2 ) IS
3823 
3824 BEGIN
3825    resultout := wf_engine.eng_completed||':'||'Y';
3826 EXCEPTION
3827 
3828 WHEN OTHERS
3829    THEN
3830         WF_CORE.CONTEXT('APINVNEG','get_last_approver',itemtype,
3831                         itemkey, to_char(actid), funcmode);
3832         RAISE;
3833 END get_last_approver;
3834 PROCEDURE aprvl_get_supplier_contact(itemtype IN VARCHAR2,
3835                         itemkey IN VARCHAR2,
3836                         actid   IN NUMBER,
3837                         funcmode IN VARCHAR2,
3838                         resultout  OUT NOCOPY VARCHAR2 ) IS
3839 
3840 BEGIN
3841    resultout := wf_engine.eng_completed||':'||'Y';
3842 EXCEPTION
3843 
3844 WHEN OTHERS
3845    THEN
3846         WF_CORE.CONTEXT('APINVNEG','aprvl_get_supplier_contact',itemtype,
3847                         itemkey, to_char(actid), funcmode);
3848         RAISE;
3849 END aprvl_get_supplier_contact;
3850 
3851 PROCEDURE aprvl_process_accept_ext(itemtype IN VARCHAR2,
3852                         itemkey IN VARCHAR2,
3853                         actid   IN NUMBER,
3854                         funcmode IN VARCHAR2,
3855                         resultout  OUT NOCOPY VARCHAR2 ) IS
3856    l_api_name      CONSTANT VARCHAR2(200) := 'aprvl_process_accept_ext';
3857    l_invoice_id    NUMBER;
3858    l_level         VARCHAR2(20);
3859    l_result        VARCHAR2(20);
3860    l_invoice_key   VARCHAR2(50);
3861    l_name          AP_APINV_APPROVERS.ROLE_NAME%TYPE;
3862    l_item_class    AP_APINV_APPROVERS.ITEM_CLASS%TYPE;
3863    l_item_id       AP_APINV_APPROVERS.ITEM_ID%TYPE;
3864    l_debug_info    VARCHAR2(2000);
3865    l_user_id       NUMBER(15);
3866    l_login_id      NUMBER(15);
3867    l_hist_rec      AP_INV_APRVL_HIST%ROWTYPE;
3868    l_approver_name VARCHAR2(150);
3869    l_approver_id   NUMBER;
3870    l_role_name     VARCHAR2(150);
3871    l_role VARCHAR2(150);
3872    l_supplier_name VARCHAR2(150);
3873    l_supplier_role VARCHAR2(150);
3874    l_supplier_person_id   NUMBER;
3875    l_line_total     NUMBER;
3876    l_iteration     NUMBER(15);
3877    l_comments      VARCHAR2(240);
3878    l_org_id        NUMBER(15);
3879    l_line_number   ap_invoice_lines_all.line_number%TYPE;
3880    l_line_amount   ap_invoice_lines_all.amount%TYPE;
3881    l_parent_key    VARCHAR2(150);
3882    l_notf_iteration        NUMBER;
3883    l_curr_calling_sequence  VARCHAR2(2000);
3884    l_invoice_type_lookup_code ap_invoices_all.invoice_type_lookup_code%TYPE;
3885 
3886 BEGIN
3887 
3888    l_curr_calling_sequence
3889              := 'AP_WORKFLOW_PKG.aprvl_process_accept_ext';
3890    l_debug_info := 'Start';
3891    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3892           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
3893                          l_api_name,l_debug_info);
3894    END IF;
3895    l_invoice_id := WF_ENGINE.GetItemAttrNumber(itemtype,
3896                                   itemkey,
3897                                   'INVOICE_ID');
3898 
3899    l_iteration := WF_ENGINE.GetItemAttrNumber(itemtype,
3900                              itemkey,
3901                              'ITERATION');
3902 
3903    l_parent_key := WF_ENGINE.GetItemAttrText(itemtype,
3904                              itemkey,
3905                              'PARENT_KEY');
3906 
3907    l_comments := WF_ENGINE.GetItemAttrText(itemtype,
3908                              itemkey,
3909                              'WF_NOTE');
3910 
3911    l_org_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
3912                    itemkey,
3913                    'ORG_ID');
3914 
3915    MO_GLOBAL.INIT ('SQLAP');
3916    MO_GLOBAL.set_policy_context('S',l_org_id);
3917 
3918 
3919    l_approver_name := WF_ENGINE.GetItemAttrText(itemtype,
3920                              itemkey,
3921                              'INTERNAL_REP_DISPLAY_NAME');
3922 
3923    l_role_name := WF_ENGINE.GetItemAttrText(itemtype,
3924                              itemkey,
3925                              'INTERNAL_REP_ROLE');
3926 
3927    l_approver_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
3928                    itemkey,
3929                    'INTERNAL_REP_PERSON_ID');
3930 
3931 
3932    l_supplier_name := WF_ENGINE.GetItemAttrText(itemtype,
3933                              itemkey,
3934                              'SUPPLIER_DISPLAY_NAME');
3935 
3936    l_supplier_role := WF_ENGINE.GetItemAttrText(itemtype,
3937                              itemkey,
3938                              'SUPPLIER_ROLE');
3939 
3940    l_supplier_person_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
3941                              itemkey,
3942                              'SUPPLIER_PERSON_ID');
3943 
3944    l_notf_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
3945                         itemkey,
3946                         'NOTF_ITERATION');
3947 
3948 
3949    l_user_id := nvl(to_number(fnd_profile.value('USER_ID')),-1);
3950    l_login_id := nvl(to_number(fnd_profile.value('LOGIN_ID')),-1);
3951 
3952 
3953    l_hist_rec.HISTORY_TYPE := 'DOCUMENTAPPROVAL';
3954    l_hist_rec.INVOICE_ID   := l_invoice_id;
3955    l_hist_rec.ITERATION    := l_iteration;
3956    l_hist_rec.NOTIFICATION_ORDER := l_notf_iteration;
3957    l_hist_rec.APPROVER_COMMENTS := l_comments;
3958    l_hist_rec.RESPONSE     := 'ACCEPT';
3959    l_hist_rec.APPROVER_ID  := l_supplier_person_id;
3960    l_hist_rec.APPROVER_NAME:= l_supplier_name;
3961    l_hist_rec.CREATED_BY   := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
3962    l_hist_rec.CREATION_DATE:= sysdate;
3963    l_hist_rec.LAST_UPDATE_DATE := sysdate;
3964    l_hist_rec.LAST_UPDATED_BY  := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
3965    l_hist_rec.LAST_UPDATE_LOGIN := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),-1);
3966    l_hist_rec.ORG_ID            := l_org_id;
3967    --update AME status
3968    --For Future  check with ame as to when updateApprovalStatuses will be
3969    --available so there  will not be a need to loop.
3970 
3971       --update AME with response
3972    /*
3973    AME_API2.updateApprovalStatus2(applicationIdIn => 200,
3974              transactionIdIn     => to_char(l_invoice_id),
3975              approvalStatusIn    => AME_UTIL.approvedStatus,
3976              approverNameIn  => l_role_name,
3977              transactionTypeIn =>  'APINV',
3978              itemClassIn     => ame_util.headerItemClassName,
3979              itemIdIn        => to_char(l_invoice_id));
3980    */
3981    l_hist_rec.AMOUNT_APPROVED   := null;
3982 
3983    l_debug_info := 'Before calling insert_history_table for Line'
3984                        || l_line_number;
3985    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3986       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
3987                             l_api_name,l_debug_info);
3988    END IF;
3989    insert_history_table(p_hist_rec => l_hist_rec);
3990 
3991 
3992    l_debug_info := 'Before calling recreate_pay_scheds';
3993    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3994           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
3995                          l_api_name,l_debug_info);
3996    END IF;
3997    SELECT invoice_type_lookup_code
3998    INTO   l_invoice_type_lookup_code
3999    FROM   AP_INVOICES_ALL
4000    WHERE  invoice_id = l_invoice_id;
4001    IF l_invoice_type_lookup_code IN ('INVOICE REQUEST'
4002                                     ,'CREDIT MEMO REQUEST') THEN
4003       recreate_pay_scheds(l_invoice_id,l_curr_calling_sequence);
4004    END IF;
4005 
4006    wf_engine.CompleteActivity(
4007                         itemType => 'APINVAPR',
4008                         itemKey  => l_parent_key,
4009                         activity => 'APPROVAL_INVOICE:DOCUMENT_APPROVAL_REQUEST',
4010                         result   => 'APPROVED');
4011 
4012    l_debug_info := 'End';
4013    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4014           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
4015                          l_api_name,l_debug_info);
4016    END IF;
4017 
4018    resultout := wf_engine.eng_completed||':'||'Y';
4019 EXCEPTION
4020 
4021 WHEN OTHERS
4022    THEN
4023         WF_CORE.CONTEXT('APINVNEG','aprvl_process_accept_ext',itemtype,
4024                         itemkey, to_char(actid), funcmode);
4025         RAISE;
4026 END aprvl_process_accept_ext;
4027 
4028 PROCEDURE aprvl_process_accept_int(itemtype IN VARCHAR2,
4029                         itemkey IN VARCHAR2,
4030                         actid   IN NUMBER,
4031                         funcmode IN VARCHAR2,
4032                         resultout  OUT NOCOPY VARCHAR2 ) IS
4033    l_api_name      CONSTANT VARCHAR2(200) := 'aprvl_process_accept_int';
4034    l_invoice_id    NUMBER;
4035    l_level         VARCHAR2(20);
4036    l_result        VARCHAR2(20);
4037    l_invoice_key   VARCHAR2(50);
4038    l_name          AP_APINV_APPROVERS.ROLE_NAME%TYPE;
4039    l_item_class    AP_APINV_APPROVERS.ITEM_CLASS%TYPE;
4040    l_item_id       AP_APINV_APPROVERS.ITEM_ID%TYPE;
4041    l_debug_info    VARCHAR2(2000);
4042    l_user_id       NUMBER(15);
4043    l_login_id      NUMBER(15);
4044    l_hist_rec      AP_INV_APRVL_HIST%ROWTYPE;
4045    l_approver_name VARCHAR2(150);
4046    l_approver_id   NUMBER;
4047    l_role_name     VARCHAR2(150);
4048    l_role VARCHAR2(150);
4049    l_supplier_name VARCHAR2(150);
4050    l_supplier_role VARCHAR2(150);
4051    l_supplier_person_id   NUMBER;
4052    l_line_total     NUMBER;
4053    l_iteration     NUMBER(15);
4054    l_comments      VARCHAR2(240);
4055    l_org_id        NUMBER(15);
4056    l_line_number   ap_invoice_lines_all.line_number%TYPE;
4057    l_line_amount   ap_invoice_lines_all.amount%TYPE;
4058    l_parent_key    VARCHAR2(150);
4059    l_notf_iteration        NUMBER;
4060    l_curr_calling_sequence  VARCHAR2(2000);
4061    l_invoice_type_lookup_code ap_invoices_all.invoice_type_lookup_code%TYPE;
4062 
4063 BEGIN
4064    l_debug_info := 'Start';
4065    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4066           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
4067                          l_api_name,l_debug_info);
4068    END IF;
4069    l_curr_calling_sequence := 'AP_WORKFLOW_PKG.aprvl_process_accept_int';
4070    l_invoice_id := WF_ENGINE.GetItemAttrNumber(itemtype,
4071                                   itemkey,
4072                                   'INVOICE_ID');
4073 
4074    l_iteration := WF_ENGINE.GetItemAttrNumber(itemtype,
4075                              itemkey,
4076                              'ITERATION');
4077 
4078    l_parent_key := WF_ENGINE.GetItemAttrText(itemtype,
4079                              itemkey,
4080                              'PARENT_KEY');
4081 
4082    l_comments := WF_ENGINE.GetItemAttrText(itemtype,
4083                              itemkey,
4084                              'WF_NOTE');
4085 
4086    l_org_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
4087                    itemkey,
4088                    'ORG_ID');
4089 
4090    MO_GLOBAL.INIT ('SQLAP');
4091    MO_GLOBAL.set_policy_context('S',l_org_id);
4092 
4093 
4094    l_approver_name := WF_ENGINE.GetItemAttrText(itemtype,
4095                              itemkey,
4096                              'INTERNAL_REP_DISPLAY_NAME');
4097 
4098    l_role_name := WF_ENGINE.GetItemAttrText(itemtype,
4099                              itemkey,
4100                              'INTERNAL_REP_ROLE');
4101 
4102    l_approver_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
4103                    itemkey,
4104                    'INTERNAL_REP_PERSON_ID');
4105 
4106 
4107    l_supplier_name := WF_ENGINE.GetItemAttrText(itemtype,
4108                              itemkey,
4109                              'SUPPLIER_DISPLAY_NAME');
4110 
4111    l_supplier_role := WF_ENGINE.GetItemAttrText(itemtype,
4112                              itemkey,
4113                              'SUPPLIER_ROLE');
4114 
4115    l_supplier_person_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
4116                              itemkey,
4117                              'SUPPLIER_PERSON_ID');
4118 
4119    l_notf_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
4120                         itemkey,
4121                         'NOTF_ITERATION');
4122 
4123 
4124    l_user_id := nvl(to_number(fnd_profile.value('USER_ID')),-1);
4125    l_login_id := nvl(to_number(fnd_profile.value('LOGIN_ID')),-1);
4126 
4127    l_hist_rec.HISTORY_TYPE := 'DOCUMENTAPPROVAL';
4128    l_hist_rec.INVOICE_ID   := l_invoice_id;
4129    l_hist_rec.ITERATION    := l_iteration;
4130    l_hist_rec.NOTIFICATION_ORDER := l_notf_iteration;
4131    l_hist_rec.APPROVER_COMMENTS := l_comments;
4132    l_hist_rec.RESPONSE     := 'ACCEPT';
4133    l_hist_rec.APPROVER_ID  := l_approver_id;
4134    l_hist_rec.APPROVER_NAME:= l_approver_name;
4135    l_hist_rec.CREATED_BY   := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
4136    l_hist_rec.CREATION_DATE:= sysdate;
4137    l_hist_rec.LAST_UPDATE_DATE := sysdate;
4138    l_hist_rec.LAST_UPDATED_BY  := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
4139    l_hist_rec.LAST_UPDATE_LOGIN := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),-1);
4140    l_hist_rec.ORG_ID            := l_org_id;
4141    --update AME status
4142    --For Future  check with ame as to when updateApprovalStatuses will be
4143    --available so there  will not be a need to loop.
4144 
4145 
4146    l_debug_info := 'Before calling recreate_pay_scheds';
4147    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4148           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
4149                          l_api_name,l_debug_info);
4150    END IF;
4151    SELECT invoice_type_lookup_code
4152    INTO   l_invoice_type_lookup_code
4153    FROM   AP_INVOICES_ALL
4154    WHERE  invoice_id = l_invoice_id;
4155    IF l_invoice_type_lookup_code IN ('INVOICE REQUEST'
4156                                     ,'CREDIT MEMO REQUEST') THEN
4157       recreate_pay_scheds(l_invoice_id,l_curr_calling_sequence);
4158    END IF;
4159    l_debug_info := 'End';
4160    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4161           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
4162                          l_api_name,l_debug_info);
4163    END IF;
4164    wf_engine.CompleteActivity(
4165                         itemType => 'APINVAPR',
4166                         itemKey  => l_parent_key,
4167                         activity => 'APPROVAL_INVOICE:DOCUMENT_APPROVAL_REQUEST',
4168                         result   => 'APPROVED');
4169 
4170    resultout := wf_engine.eng_completed||':'||'Y';
4171 EXCEPTION
4172 
4173 WHEN OTHERS
4174    THEN
4175         WF_CORE.CONTEXT('APINVNEG','aprvl_process_accept_int',itemtype,
4176                         itemkey, to_char(actid), funcmode);
4177         RAISE;
4178 END aprvl_process_accept_int;
4179 
4180 PROCEDURE aprvl_process_cancel_inv_sup(itemtype IN VARCHAR2,
4181                         itemkey IN VARCHAR2,
4182                         actid   IN NUMBER,
4183                         funcmode IN VARCHAR2,
4184                         resultout  OUT NOCOPY VARCHAR2 ) IS
4185 
4186    --Define cursor for lines affected by notification
4187    --Note that Invoice_Key s/b the same for all records in the cursor
4188    --but I want to avoid another select on the table
4189    CURSOR   Items_Cur(itemkey IN VARCHAR2) IS
4190    SELECT invap.Item_Class, invap.Item_Id, invap.Role_Name,
4191           invap.Invoice_Key, al.line_number, al.amount
4192    FROM   AP_APINV_APPROVERS invap, AP_INVOICE_LINES_ALL al
4193    WHERE  invap.child_process_item_key = itemkey
4194    AND    al.line_number = invap.line_number
4195    AND    al.invoice_id  = invap.invoice_id;
4196    l_api_name      CONSTANT VARCHAR2(200) := 'aprvl_process_cancel_inv_sup';
4197    l_invoice_id    NUMBER;
4198    l_level         VARCHAR2(20);
4199    l_invoice_key   VARCHAR2(50);
4200    l_name          AP_APINV_APPROVERS.ROLE_NAME%TYPE;
4201    l_item_class    AP_APINV_APPROVERS.ITEM_CLASS%TYPE;
4202    l_item_id       AP_APINV_APPROVERS.ITEM_ID%TYPE;
4203    l_debug_info    VARCHAR2(2000);
4204    l_user_id       NUMBER(15);
4205    l_login_id      NUMBER(15);
4206    l_hist_rec      AP_INV_APRVL_HIST%ROWTYPE;
4207    l_approver_name VARCHAR2(150);
4208    l_approver_id   NUMBER;
4209    l_role_name     VARCHAR2(150);
4210    l_role VARCHAR2(150);
4211    l_supplier_name VARCHAR2(150);
4212    l_supplier_role VARCHAR2(150);
4213    l_supplier_person_id   NUMBER;
4214    l_line_total     NUMBER;
4215    l_iteration     NUMBER(15);
4216    l_comments      VARCHAR2(240);
4217    l_org_id        NUMBER(15);
4218    l_line_number   ap_invoice_lines_all.line_number%TYPE;
4219    l_line_amount   ap_invoice_lines_all.amount%TYPE;
4220    l_parent_key    VARCHAR2(150);
4221    l_result                BOOLEAN;
4222    l_last_updated_by number;
4223    l_last_update_login number;
4224    l_accounting_date date;
4225    l_message_name varchar2(30);
4226    l_invoice_amount number;
4227    l_base_amount number;
4228    l_temp_cancelled_amount number;
4229    l_cancelled_by number;
4230    l_cancelled_amount number;
4231    l_cancelled_date date;
4232    l_last_update_date date;
4233    l_original_prepayment_amount number;
4234    l_pay_curr_invoice_amount number;
4235    l_token varchar2(30);
4236    l_notf_iteration        NUMBER;
4237 
4238    cursor invoice is
4239                 select  gl_date,
4240                         last_updated_by,
4241                         last_update_login
4242                 from    ap_invoices_all
4243                 where   invoice_id = l_invoice_id
4244                 and     org_id = l_org_id;
4245 BEGIN
4246 
4247    l_debug_info := 'Start';
4248    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4249           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
4250                          l_api_name,l_debug_info);
4251    END IF;
4252    l_invoice_id := WF_ENGINE.GetItemAttrNumber(itemtype,
4253                                   itemkey,
4254                                   'INVOICE_ID');
4255 
4256    l_iteration := WF_ENGINE.GetItemAttrNumber(itemtype,
4257                              itemkey,
4258                              'ITERATION');
4259 
4260    l_parent_key := WF_ENGINE.GetItemAttrText(itemtype,
4261                              itemkey,
4262                              'PARENT_KEY');
4263 
4264    l_comments := WF_ENGINE.GetItemAttrText(itemtype,
4265                              itemkey,
4266                              'WF_NOTE');
4267 
4268    l_org_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
4269                    itemkey,
4270                    'ORG_ID');
4271 
4272    MO_GLOBAL.INIT ('SQLAP');
4273    MO_GLOBAL.set_policy_context('S',l_org_id);
4274 
4275 
4276    l_approver_name := WF_ENGINE.GetItemAttrText(itemtype,
4277                              itemkey,
4278                              'INTERNAL_REP_DISPLAY_NAME');
4279 
4280    l_role_name := WF_ENGINE.GetItemAttrText(itemtype,
4281                              itemkey,
4282                              'INTERNAL_REP_ROLE');
4283 
4284    l_approver_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
4285                    itemkey,
4286                    'INTERNAL_REP_PERSON_ID');
4287 
4288 
4289    l_supplier_name := WF_ENGINE.GetItemAttrText(itemtype,
4290                              itemkey,
4291                              'INTERNAL_REP_DISPLAY_NAME');
4292 
4293    l_supplier_role := WF_ENGINE.GetItemAttrText(itemtype,
4294                              itemkey,
4295                              'INTERNAL_REP_ROLE');
4296 
4297    l_supplier_person_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
4298                              itemkey,
4299                              'INTERNAL_REP_PERSON_ID');
4300 
4301    l_notf_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
4302                         itemkey,
4303                         'NOTF_ITERATION');
4304 
4305 
4306    l_user_id := nvl(to_number(fnd_profile.value('USER_ID')),-1);
4307    l_login_id := nvl(to_number(fnd_profile.value('LOGIN_ID')),-1);
4308 
4309    l_debug_info := 'Before Update Approvers table';
4310    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4311           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
4312                          l_api_name,l_debug_info);
4313    END IF;
4314 
4315    l_debug_info := 'Before Ap_Cancel_Single_Invoice';
4316    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4317           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
4318                          l_api_name,l_debug_info);
4319    END IF;
4320 
4321 
4322    l_hist_rec.HISTORY_TYPE := 'LINESNEGOTIATION';
4323    l_hist_rec.INVOICE_ID   := l_invoice_id;
4324    l_hist_rec.ITERATION    := l_iteration;
4325    l_hist_rec.NOTIFICATION_ORDER := l_notf_iteration;
4326    l_hist_rec.APPROVER_COMMENTS := l_comments;
4327    l_hist_rec.RESPONSE     := 'CANCEL';
4328    l_hist_rec.APPROVER_ID  := l_approver_id;
4329    l_hist_rec.APPROVER_NAME:= l_approver_name;
4330    l_hist_rec.CREATED_BY   := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
4331    l_hist_rec.CREATION_DATE:= sysdate;
4332    l_hist_rec.LAST_UPDATE_DATE := sysdate;
4333    l_hist_rec.LAST_UPDATED_BY  := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
4334    l_hist_rec.LAST_UPDATE_LOGIN := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),-1);
4335    l_hist_rec.ORG_ID            := l_org_id;
4336    --update AME status
4337    --For Future  check with ame as to when updateApprovalStatuses will be
4338    --available so there  will not be a need to loop.
4339 
4340    /* Not needed since we are completing the parent activity
4341    --update AME with response
4342    AME_API2.updateApprovalStatus2(applicationIdIn => 200,
4343              transactionIdIn     => to_char(l_invoice_id),
4344              approvalStatusIn    => AME_UTIL.rejectStatus,
4345              approverNameIn  => l_role_name,
4346              transactionTypeIn =>  'APINV',
4347              itemClassIn     => ame_util.headerItemClassName,
4348              itemIdIn        => to_char(l_invoice_id));
4349    */
4350    l_debug_info := 'Before calling insert_history_table for Line'
4351                        || l_line_number;
4352    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4353              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
4354                             l_api_name,l_debug_info);
4355    END IF;
4356    insert_history_table(p_hist_rec => l_hist_rec);
4357 
4358    open invoice;
4359    fetch invoice into l_accounting_date, l_last_updated_by, l_last_update_login;
4360    close invoice;
4361 
4362 
4363    l_result := ap_cancel_pkg.ap_cancel_single_invoice(
4364                 l_invoice_id,
4365                 l_last_updated_by,
4366                 l_last_update_login,
4367                 sysdate,                                -- accounting_date
4368                 l_message_name,
4369                 l_invoice_amount,
4370                 l_base_amount,
4371                 l_temp_cancelled_amount,
4372                 l_cancelled_by,
4373                 l_cancelled_amount,
4374                 l_cancelled_date,
4375                 l_last_update_date,
4376                 l_original_prepayment_amount,
4377                 l_pay_curr_invoice_amount,
4378                 l_token,
4379                 null);
4380 
4381    wf_engine.CompleteActivity(
4382                         itemType => 'APINVAPR',
4383                         itemKey  => l_parent_key,
4384                         activity => 'APPROVAL_INVOICE:DOCUMENT_APPROVAL_REQUEST',
4385                         result   => 'REJECTED');
4386 
4387 
4388    l_debug_info := 'End';
4389    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4390           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
4391                          l_api_name,l_debug_info);
4392    END IF;
4393 
4394    resultout := wf_engine.eng_completed||':'||'Y';
4395 EXCEPTION
4396 
4397 WHEN OTHERS
4398    THEN
4399         WF_CORE.CONTEXT('APINVNEG','aprvl_process_cancel_inv_sup',itemtype,
4400                         itemkey, to_char(actid), funcmode);
4401         RAISE;
4402 END aprvl_process_cancel_inv_sup;
4403 PROCEDURE create_hold_neg_process(p_hold_id IN NUMBER,
4404                                   p_ext_contact_id IN NUMBER,
4405                                   parentkey IN VARCHAR2,
4406                                   childkey  IN VARCHAR2,
4407 				  int_ext_indicator IN VARCHAR2,
4408 				  newchildprocess OUT NOCOPY VARCHAR2) IS
4409 /*-----------------------------------------------------------------------+
4410  | Cursor Declarations                                                   |
4411  +-----------------------------------------------------------------------*/
4412 CURSOR csr_ap_hold_neg_details IS
4413 SELECT PV.vendor_name,
4414        AI.invoice_num,
4415        AI.invoice_date,
4416        AI.description,
4417        AI.org_id,
4418        AI.invoice_id,
4419        AI.approval_iteration,
4420        nvl(p_ext_contact_id, AI.vendor_contact_id),
4421        NVL(AI.invoice_amount, 0),
4422        ahc.hold_instruction
4423 FROM   ap_holds_all ah,
4424        ap_invoices_all AI,
4425        po_vendors PV,
4426        ap_hold_codes ahc
4427 WHERE  ah.hold_id = p_hold_id
4428 AND    AI.invoice_id = ah.invoice_id
4429 AND    AI.vendor_id = PV.vendor_id
4430 AND    AH.hold_lookup_code = AHC.hold_lookup_code;
4431 l_vendor_name po_vendors.vendor_name%TYPE;
4432 l_invoice_num ap_invoices_all.invoice_num%TYPE;
4433 l_invoice_date  ap_invoices_all.invoice_date%TYPE;
4434 l_invoice_description ap_invoices_all.description%TYPE;
4435 l_invoice_id ap_invoices_all.invoice_id%TYPE;
4436 l_org_id ap_invoices_all.org_id%TYPE;
4437 l_name                  wf_users.name%TYPE; --bug 8620671
4438 l_display_name          VARCHAR2(150);
4439 l_role                  VARCHAR2(50);
4440 l_role_display          VARCHAR2(150);
4441 l_new_child_ItemKey     VARCHAR2(30);
4442 l_person_id             NUMBER(15);
4443 l_hist_rec              AP_INV_APRVL_HIST%ROWTYPE;
4444 l_notf_receipient_type  VARCHAR2(50);
4445 l_iteration             NUMBER;
4446 l_notf_iteration        NUMBER;
4447 l_ext_person_id         NUMBER(15);
4448 l_ext_user_id           NUMBER(15);
4449 l_hold_instr            ap_hold_codes.hold_instruction%TYPE;
4450 l_total                 ap_invoices_all.invoice_amount%TYPE;
4451 BEGIN
4452    l_notf_iteration := 1; /* For Now Hold Approval has only one round of
4453                              Approvers */
4454    SELECT AP_NEGOTIATION_HIST_S.nextval
4455    INTO   l_new_child_ItemKey
4456    FROM   dual;
4457 
4458    OPEN csr_ap_hold_neg_details;
4459    FETCH csr_ap_hold_neg_details INTO
4460          l_vendor_name,
4461 	 l_invoice_num,
4462 	 l_invoice_date,
4463 	 l_invoice_description,
4464 	 l_org_id,
4465 	 l_invoice_id,
4466 	 l_iteration,
4467 	 l_ext_user_id,
4468 	 l_total,
4469 	 l_hold_instr;
4470    CLOSE csr_ap_hold_neg_details;
4471 
4472    wf_engine.createProcess('APINVHNE', l_new_child_itemkey, 'HOLD_NEGOTIATION');
4473    WF_ENGINE.setItemParent('APINVHNE', l_new_child_itemkey,
4474 	                   'APINVHDN', parentkey, null);
4475    WF_ENGINE.SetItemAttrNumber('APINVHNE',
4476                         l_new_child_itemkey,
4477                         'HOLD_ID',
4478                         p_hold_id);
4479    WF_ENGINE.SetItemAttrNumber('APINVHNE',
4480                         l_new_child_itemkey,
4481                         'INVOICE_ID',
4482                         l_invoice_id);
4483    WF_ENGINE.SetItemAttrNumber('APINVHNE',
4484                         l_new_child_itemkey,
4485                         'ORG_ID',
4486                         l_org_id);
4487    WF_ENGINE.SetItemAttrNumber('APINVHNE',
4488                         l_new_child_itemkey,
4489                         'INVOICE_TOTAL',
4490                         l_total);
4491    WF_ENGINE.SetItemAttrText('APINVHNE',
4492                         l_new_child_itemkey,
4493                         'HOLD_INSTRUCTION',
4494                         l_hold_instr);
4495    WF_ENGINE.SetItemAttrText('APINVHNE',
4496                         l_new_child_itemkey,
4497                         'INVOICE_SUPPLIER_NAME',
4498                         l_vendor_name);
4499    WF_ENGINE.SetItemAttrText('APINVHNE',
4500                         l_new_child_itemkey,
4501                         'INVOICE_NUMBER',
4502                         l_invoice_num);
4503    WF_ENGINE.SetItemAttrDate('APINVHNE',
4504                         l_new_child_itemkey,
4505                         'INVOICE_DATE',
4506                         l_invoice_date);
4507    WF_ENGINE.SetItemAttrText('APINVHNE',
4508                         l_new_child_itemkey,
4509                         'INVOICE_DESCRIPTION',
4510                         l_invoice_description);
4511    WF_ENGINE.SetItemAttrText('APINVHNE',
4512                         l_new_child_itemkey,
4513                         'PARENT_KEY',
4514                         parentkey);
4515    WF_ENGINE.SetItemAttrText('APINVHNE',
4516                      l_new_child_itemkey,
4517                      'HOLD_TYPE',
4518                      WF_ENGINE.GETITEMATTRText('APINVHDN',
4519 		     parentkey,
4520 		     'HOLD_TYPE'));
4521    -- Bug 9509841
4522    WF_ENGINE.SetItemAttrText( 'APINVHNE',
4523                               l_new_child_itemkey,
4524                               'NOTIFICATION_KEY',
4525                               l_new_child_itemkey );
4526 
4527    /* Current context is Internal or External
4528       If Current Context is Internal that means that the negotiation is going
4529       out to External supplier and if Current context is External then the
4530       negotiation is going out to the internal Rep. */
4531    IF int_ext_indicator = 'I' and l_ext_user_id IS NOT NULL THEN
4532 
4533       WF_DIRECTORY.GetRoleName('FND_USR',l_ext_user_id,l_role,
4534                                l_role_display);
4535       l_person_id := l_ext_user_id;
4536       WF_DIRECTORY.GetUserName('FND_USR',
4537                                 l_ext_user_id,
4538                                 l_name,
4539                                 l_display_name);
4540       WF_ENGINE.SetItemAttrText('APINVHNE',
4541                         l_new_child_itemkey,
4542                         'SUPPLIER_ROLE',
4543                         l_role);
4544       WF_ENGINE.SetItemAttrText('APINVHNE',
4545                                   l_new_child_itemkey,
4546                                   'SUPPLIER_DISPLAY_NAME',
4547                                   l_display_name);
4548       WF_ENGINE.SetItemAttrNumber('APINVHNE',
4549                         l_new_child_itemkey,
4550                         'SUPPLIER_PERSON_ID',
4551                         l_ext_user_id);
4552 
4553       WF_ENGINE.SetItemAttrText('APINVHNE',
4554                         l_new_child_itemkey,
4555                         'NOTF_RECEIPIENT_TYPE',
4556                         'EXTERNAL');
4557       WF_ENGINE.SetItemAttrText('APINVHNE',
4558                                   l_new_child_itemkey,
4559                                   'INTERNAL_REP_DISPLAY_NAME',
4560                                   WF_ENGINE.GETITEMATTRText('APINVHDN',
4561 				                             parentkey,
4562 						             'INTERNAL_REP_DISPLAY_NAME'));
4563       WF_ENGINE.SetItemAttrNumber('APINVHNE',
4564                                   l_new_child_itemkey,
4565                                   'INTERNAL_REP_PERSON_ID',
4566                                   WF_ENGINE.GETITEMATTRNumber('APINVHDN',
4567 				                             parentkey,
4568 						             'INTERNAL_REP_PERSON_ID'));
4569       WF_ENGINE.SetItemAttrText('APINVHNE',
4570                                   l_new_child_itemkey,
4571                                   'ORIG_SYSTEM',
4572                                   WF_ENGINE.GETITEMATTRText('APINVHDN',
4573 				                             parentkey,
4574 						             'ORIG_SYSTEM'));
4575       WF_ENGINE.SetItemAttrText('APINVHNE',
4576                                   l_new_child_itemkey,
4577                                   'INTERNAL_REP_ROLE',
4578                                   WF_ENGINE.GETITEMATTRText('APINVHDN',
4579 				                             parentkey,
4580 						             'INTERNAL_REP_ROLE'));
4581    ELSIF int_ext_indicator = 'E' THEN
4582 
4583       l_role := WF_ENGINE.GETITEMATTRText('APINVHDN',
4584                                           parentkey,
4585 			                  'INTERNAL_REP_ROLE');
4586       WF_ENGINE.SetItemAttrText('APINVHNE',
4587                         l_new_child_itemkey,
4588                         'INTERNAL_REP_ROLE',
4589                         l_role);
4590       WF_ENGINE.SetItemAttrText('APINVHNE',
4591                         l_new_child_itemkey,
4592                         'DISP_NOT_RECEIVER',
4593                         l_role);
4594       l_display_name := WF_ENGINE.GETITEMATTRText('APINVHDN',
4595                                           parentkey,
4596 			                  'INTERNAL_REP_DISPLAY_NAME');
4597       WF_ENGINE.SetItemAttrText('APINVHNE',
4598                                   l_new_child_itemkey,
4599                                   'INTERNAL_REP_DISPLAY_NAME',
4600                                   l_display_name);
4601       l_person_id := WF_ENGINE.GETITEMATTRNumber('APINVHDN',
4602                                           parentkey,
4603 			                  'INTERNAL_REP_PERSON_ID');
4604       WF_ENGINE.SetItemAttrNumber('APINVHNE',
4605                         l_new_child_itemkey,
4606                         'INTERNAL_REP_PERSON_ID',
4607                         l_person_id);
4608       WF_ENGINE.SetItemAttrText('APINVHNE',
4609                         l_new_child_itemkey,
4610                         'NOTF_RECEIPIENT_TYPE',
4611                         'INTERNAL');
4612       WF_ENGINE.SetItemAttrText('APINVHNE',
4613                                   l_new_child_itemkey,
4614                                   'ORIG_SYSTEM',
4615                                   WF_ENGINE.GETITEMATTRText('APINVHDN',
4616 				                             parentkey,
4617 						             'ORIG_SYSTEM'));
4618       WF_ENGINE.SetItemAttrText('APINVHNE',
4619                                   l_new_child_itemkey,
4620                                   'SUPPLIER_DISPLAY_NAME',
4621                                   WF_ENGINE.GETITEMATTRText('APINVHNE',
4622 				                             childkey,
4623 						             'SUPPLIER_DISPLAY_NAME'));
4624 
4625    END IF;
4626    WF_ENGINE.startProcess('APINVHNE', l_new_child_itemkey);
4627    MO_GLOBAL.INIT ('SQLAP');
4628    MO_GLOBAL.set_policy_context('S',l_org_id);
4629 
4630    -- Complete the previous negotiation process if any.
4631 
4632    IF childkey IS NOT NULL THEN
4633       l_notf_receipient_type :=  WF_ENGINE.GetItemAttrText('APINVHNE',
4634                                childkey,
4635                                'NOTF_RECEIPIENT_TYPE');
4636       IF l_notf_receipient_type = 'INTERNAL' THEN
4637 
4638           wf_engine.CompleteActivity(
4639                      itemType => 'APINVHNE',
4640                      itemKey  => childkey,
4641                      activity => 'HOLD_NEGOTIATION:WAITINTERNAL',
4642                      result   => 'NULL');
4643       ELSE
4644           wf_engine.CompleteActivity(
4645                      itemType => 'APINVHNE',
4646                      itemKey  => childkey,
4647                      activity => 'HOLD_NEGOTIATION:WAITEXTERNAL',
4648                      result   => 'NULL');
4649       END IF;
4650 
4651    END IF;
4652 
4653    l_hist_rec.HISTORY_TYPE := 'HOLDNEGOTIATION';
4654    l_hist_rec.INVOICE_ID   := l_invoice_id;
4655    l_hist_rec.ITERATION    := l_iteration;
4656    l_hist_rec.NOTIFICATION_ORDER := l_notf_iteration;
4657    l_hist_rec.RESPONSE     := 'SENT';
4658    l_hist_rec.APPROVER_ID  := l_person_id;
4659    l_hist_rec.APPROVER_NAME:= l_display_name;
4660    l_hist_rec.CREATED_BY   := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
4661    l_hist_rec.CREATION_DATE:= sysdate;
4662    l_hist_rec.LAST_UPDATE_DATE := sysdate;
4663    l_hist_rec.LAST_UPDATED_BY  := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
4664    l_hist_rec.LAST_UPDATE_LOGIN := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),-1);
4665    l_hist_rec.ORG_ID            := l_org_id;
4666    l_hist_rec.AMOUNT_APPROVED   := 0;
4667    l_hist_rec.HOLD_ID           := p_hold_id;
4668 
4669    insert_history_table(p_hist_rec => l_hist_rec);
4670    newchildprocess := l_new_child_itemkey;
4671 
4672    UPDATE ap_holds_all
4673    SET    wf_status = 'NEGOTIATE'
4674    WHERE  hold_id = p_hold_id;
4675 
4676 EXCEPTION
4677 
4678 WHEN OTHERS
4679    THEN
4680         RAISE;
4681 
4682 END create_hold_neg_process;
4683 
4684 PROCEDURE create_hold_wf_process(p_hold_id IN NUMBER) IS
4685 /*-----------------------------------------------------------------------+
4686  | Cursor Declarations                                                   |
4687  +-----------------------------------------------------------------------*/
4688 CURSOR csr_ap_hold_details IS
4689 SELECT PV.vendor_name,
4690        AI.invoice_num,
4691        AI.invoice_date,
4692        AI.description,
4693        AI.org_id,
4694        AI.invoice_id,
4695        alk.displayed_field,
4696        NVL(AI.invoice_amount, 0),
4697        ahc.hold_instruction
4698 FROM   ap_holds_all ah,
4699        ap_invoices_all AI,
4700        po_vendors PV,
4701        ap_lookup_codes alk,
4702        ap_hold_codes ahc
4703 WHERE  ah.hold_id = p_hold_id
4704 AND    alk.lookup_code = ah.hold_lookup_code
4705 AND    AI.invoice_id = ah.invoice_id
4706 AND    AI.vendor_id = PV.vendor_id
4707 AND    AH.hold_lookup_code = AHC.hold_lookup_code;
4708 l_vendor_name po_vendors.vendor_name%TYPE;
4709 l_invoice_num ap_invoices_all.invoice_num%TYPE;
4710 l_invoice_date  ap_invoices_all.invoice_date%TYPE;
4711 l_invoice_description ap_invoices_all.description%TYPE;
4712 l_invoice_id ap_invoices_all.invoice_id%TYPE;
4713 l_hold_type ap_lookup_codes.displayed_field%TYPE;
4714 l_org_id ap_invoices_all.org_id%TYPE;
4715 l_itemkey VARCHAR2(50);
4716 l_hold_instr            ap_hold_codes.hold_instruction%TYPE;
4717 l_total                 ap_invoices_all.invoice_amount%TYPE;
4718 
4719 BEGIN
4720    /* For the main process itemkey has to be hold_id */
4721    l_itemkey := p_hold_id;
4722 
4723    OPEN csr_ap_hold_details;
4724    FETCH csr_ap_hold_details INTO
4725          l_vendor_name,
4726          l_invoice_num,
4727          l_invoice_date,
4728          l_invoice_description,
4729          l_org_id,
4730          l_invoice_id,
4731 	 l_hold_type,
4732          l_total,
4733 	 l_hold_instr;
4734 
4735    CLOSE csr_ap_hold_details;
4736 
4737 
4738    wf_engine.createProcess('APINVHDN', l_itemkey, 'HOLD_MAIN');
4739    WF_ENGINE.SetItemAttrNumber('APINVHDN',
4740                         l_itemkey,
4741                         'INVOICE_ID',
4742                         l_invoice_id);
4743    WF_ENGINE.SetItemAttrNumber('APINVHDN',
4744                         l_itemkey,
4745                         'HOLD_ID',
4746                         p_hold_id);
4747    WF_ENGINE.SetItemAttrNumber('APINVHDN',
4748                         l_itemkey,
4749                         'ORG_ID',
4750                         l_org_id);
4751    WF_ENGINE.SetItemAttrNumber('APINVHDN',
4752                         l_itemkey,
4753                         'INVOICE_TOTAL',
4754                         l_total);
4755    WF_ENGINE.SetItemAttrText('APINVHDN',
4756                         l_itemkey,
4757                         'HOLD_INSTRUCTION',
4758                         l_hold_instr);
4759    WF_ENGINE.SetItemAttrText('APINVHDN',
4760                         l_itemkey,
4761                         'INVOICE_SUPPLIER_NAME',
4762                         l_vendor_name);
4763    WF_ENGINE.SetItemAttrText('APINVHDN',
4764                         l_itemkey,
4765                         'INVOICE_NUMBER',
4766                         l_invoice_num);
4767    WF_ENGINE.SetItemAttrDate('APINVHDN',
4768                         l_itemkey,
4769                         'INVOICE_DATE',
4770                         l_invoice_date);
4771    WF_ENGINE.SetItemAttrText('APINVHDN',
4772                         l_itemkey,
4773                         'INVOICE_DESCRIPTION',
4774                         l_invoice_description);
4775    WF_ENGINE.SetItemAttrText('APINVHDN',
4776                         l_itemkey,
4777                         'HOLD_TYPE',
4778                         l_hold_type);
4779    WF_ENGINE.SetItemAttrText('APINVHDN',
4780                         l_itemkey,
4781                         'PARENT_KEY',
4782                         l_itemkey);
4783 
4784    UPDATE ap_holds_all
4785    SET    wf_status = 'STARTED'
4786    WHERE  hold_id = p_hold_id;
4787 
4788    WF_ENGINE.startProcess('APINVHDN', l_itemkey);
4789 
4790 
4791 
4792 EXCEPTION
4793 
4794 WHEN OTHERS
4795    THEN
4796         RAISE;
4797 
4798 END create_hold_wf_process;
4799 PROCEDURE get_header_approver(itemtype IN VARCHAR2,
4800                         itemkey IN VARCHAR2,
4801                         actid   IN NUMBER,
4802                         funcmode IN VARCHAR2,
4803                         resultout  OUT NOCOPY VARCHAR2 ) IS
4804 l_admin_approver AME_UTIL.approverRecord;
4805 l_ret_approver VARCHAR2(50);
4806 l_name          wf_users.name%TYPE; --bug 8620671
4807 l_display_name  VARCHAR2(150);
4808 l_debug_info    VARCHAR2(50);
4809 l_role          VARCHAR2(50);
4810 l_role_display  VARCHAR2(150);
4811 l_org_id        NUMBER(15);
4812 l_error_message               VARCHAR2(2000);
4813 l_invoice_id    NUMBER(15);
4814 l_iteration     NUMBER(9);
4815 l_count         NUMBER(9);
4816 l_hist_rec      AP_INV_APRVL_HIST%ROWTYPE;
4817 l_notf_iteration        NUMBER;
4818 l_complete              VARCHAR2(1);
4819 l_next_approvers        ame_util.approversTable2;
4820 l_next_approver         ame_util.approverRecord2;
4821 l_index                 ame_util.idList;
4822 l_ids                   ame_util.stringList;
4823 l_class                 ame_util.stringList;
4824 l_source                ame_util.longStringList;
4825 l_ampersand             varchar2(1);
4826 Cursor C_invoice (p_invoice_id IN ap_invoices_all.invoice_id%TYPE) IS
4827 SELECT invoice_type_lookup_code
4828       ,gl_date
4829       ,ap_utilities_pkg.get_gl_period_name(gl_date
4830                                           ,org_id)
4831 FROM   ap_invoices_all
4832 WHERE  invoice_id = p_invoice_id;
4833 l_invoice_type_lookup_code ap_invoices_all.invoice_type_lookup_code%TYPE;
4834 l_gl_date ap_invoices_all.gl_date%TYPE;
4835 l_new_gl_date ap_invoices_all.gl_date%TYPE;
4836 l_period_name ap_invoice_lines_all.period_name%TYPE;
4837 l_new_period_name ap_invoice_lines_all.period_name%TYPE;
4838 l_terms_id ap_invoices_all.terms_id%TYPE;
4839 l_terms_date ap_invoices_all.terms_date%TYPE;
4840 l_count_rejects number;
4841 l_inv_match_type VARCHAR2(80);
4842 
4843 BEGIN
4844 
4845    /* First Check if any of the lines got rejected. If so then
4846    Header status need to be set as Needs Reapproval and any lines
4847    in Initiated status need to be updated to Required. */
4848 
4849    l_invoice_id := substr(itemkey, 1, instr(itemkey,'_')-1);
4850    l_count_rejects := 0;
4851 
4852    SELECT count(*)
4853    into   l_count_rejects
4854    FROM   ap_invoice_lines_all
4855    WHERE  invoice_id = l_invoice_id
4856    AND    wfapproval_status = 'REJECTED';
4857 
4858    IF l_count_rejects = 0 THEN
4859    select '&'
4860    into   l_ampersand
4861    from   dual
4862    where  1 like 1 escape '&';
4863    l_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
4864                         itemkey,
4865                         'ITERATION');
4866 
4867    l_notf_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
4868                         itemkey,
4869                         'NOTF_ITERATION');
4870 
4871    /*
4872    AME_API.getNextApprover(200,
4873                            substr(itemkey, 1, instr(itemkey,'_')-1),
4874                            'APINV',
4875                            l_next_approver);
4876    */
4877    AME_API2.getNextApprovers1(applicationIdIn => 200,
4878                         transactionTypeIn => 'APINV',
4879                         transactionIdIn => substr(itemkey, 1, instr(itemkey,'_')-1),
4880                         flagApproversAsNotifiedIn => ame_util.booleanFalse,
4881                         approvalProcessCompleteYNOut => l_complete,
4882                         nextApproversOut => l_next_approvers,
4883                         itemIndexesOut => l_index,
4884                         itemIdsOut => l_ids,
4885                         itemClassesOut => l_class,
4886                         itemSourcesOut => l_source
4887                         );
4888 
4889 
4890 
4891    l_org_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
4892                    itemkey,
4893                    'ORG_ID');
4894 
4895    l_invoice_id := substr(itemkey, 1, instr(itemkey,'_')-1);
4896    l_iteration := substr(itemkey, instr(itemkey,'_')+1, length(itemkey));
4897 
4898    --Now set the environment
4899    MO_GLOBAL.INIT ('SQLAP');
4900    MO_GLOBAL.set_policy_context('S',l_org_id);
4901 
4902    --IF l_complete = ame_util.booleanTrue THEN /*no approver on the list*/
4903    IF l_next_approvers.count < 1 THEN
4904            resultout := wf_engine.eng_completed||':'||'FINISH';
4905 
4906            --check for prior approvers
4907            SELECT count(*)
4908            INTO l_count
4909            FROM ap_inv_aprvl_hist
4910            WHERE invoice_id = l_invoice_id
4911            AND iteration = l_iteration
4912            AND RESPONSE <> 'MANUALLY APPROVED'
4913            AND history_type = 'DOCUMENTAPPROVAL';
4914 
4915            IF l_count >0 THEN
4916                    --update invoice header status
4917                    UPDATE AP_INVOICES_ALL
4918                    SET wfapproval_status = 'WFAPPROVED'
4919                    WHERE invoice_id = l_invoice_id
4920                    AND wfapproval_status <> 'MANUALLY APPROVED';
4921            ELSE
4922                    UPDATE AP_INVOICES_ALL
4923                    SET wfapproval_status = decode(invoice_type_lookup_code,
4924 		                           'INVOICE REQUEST','REJECTED',
4925 					   'CREDIT MEMO REQUEST','REJECTED',
4926 					   'NOT REQUIRED')
4927                    WHERE invoice_id = l_invoice_id
4928                    AND wfapproval_status <> 'MANUALLY APPROVED';
4929            END IF;
4930            l_inv_match_type := WF_ENGINE.GetItemAttrText('APINVAPR',
4931                                                          itemkey,
4932                                                          'INV_MATCH_TYPE');
4933 	   IF l_inv_match_type = 'UNMATCHED' THEN
4934               UPDATE AP_INVOICE_LINES_ALL
4935               SET wfapproval_status = 'NOT REQUIRED'
4936               WHERE invoice_id = l_invoice_id
4937               AND wfapproval_status <> 'MANUALLY APPROVED';
4938 	   END IF;
4939            /* Logic for Converting ISP requests into Invoices */
4940            OPEN C_invoice(l_invoice_id);
4941            FETCH C_invoice
4942            INTO l_invoice_type_lookup_code
4943                ,l_gl_date
4944                ,l_period_name;
4945            CLOSE C_invoice;
4946            IF l_invoice_type_lookup_code IN ('INVOICE REQUEST'
4947                                           ,'CREDIT MEMO REQUEST')
4948 	   AND l_count > 0 THEN
4949 
4950               ap_utilities_pkg.get_open_gl_date(P_Date => l_gl_date
4951                                ,P_Period_Name => l_new_period_name
4952                                ,P_GL_Date     => l_new_gl_date
4953                                ,P_Org_Id      => l_org_id);
4954               ap_isp_utilities_pkg.get_payment_terms (
4955                        p_invoice_id => l_invoice_id,
4956                        p_terms_id   => l_terms_id,
4957                        p_terms_date => l_terms_date,
4958                        p_calling_sequence =>
4959                              'ap_workflow_pkg.get_header_approver');
4960 
4961 
4962               UPDATE AP_INVOICES_ALL
4963               SET    invoice_type_lookup_code =
4964                         DECODE(invoice_type_lookup_code
4965                               ,'INVOICE REQUEST','STANDARD'
4966                               ,'CREDIT MEMO REQUEST', 'CREDIT'
4967                               ,invoice_type_lookup_code),
4968                      terms_id   = l_terms_id,
4969                      terms_date = l_terms_date
4970               WHERE  invoice_id = l_invoice_id;
4971               IF l_period_name <> l_new_period_name THEN
4972 
4973                  UPDATE AP_INVOICES_ALL
4974                  SET    gl_date = l_new_gl_date
4975                  WHERE  invoice_id = l_invoice_id;
4976 
4977                  UPDATE ap_invoice_lines_all
4978                  SET    accounting_date = l_new_gl_date
4979                        ,period_name     = l_new_period_name
4980                  WHERE  invoice_id      = l_invoice_id;
4981 
4982                  UPDATE ap_invoice_distributions_all
4983                  SET    accounting_date = l_new_gl_date
4984                        ,period_name     = l_new_period_name
4985                  WHERE  invoice_id      = l_invoice_id;
4986 
4987               END IF;
4988            END IF;
4989 
4990    ELSE /*have approver*/
4991            l_next_approver := l_next_approvers(1);
4992            WF_DIRECTORY.GetRoleName(l_next_approver.ORIG_SYSTEM
4993 	    ,l_next_approver.ORIG_SYSTEM_ID,l_role,l_role_display);
4994 
4995            WF_DIRECTORY.GetUserName(l_next_approver.ORIG_SYSTEM,
4996                            l_next_approver.ORIG_SYSTEM_ID,
4997                            l_name,
4998                            l_display_name);
4999 
5000            WF_ENGINE.SetItemAttrText(itemtype,
5001                    itemkey,
5002                    'APPROVER_NAME',
5003                    l_display_name);
5004 
5005            WF_ENGINE.SetItemAttrNumber(itemtype,
5006                    itemkey,
5007                    'APPROVER_ID',
5008                    l_next_approver.ORIG_SYSTEM_ID); /****
5009 		              POTENTIAL BUG ************/
5010 
5011            WF_ENGINE.SetItemAttrText(itemtype,
5012                    itemkey,
5013                    'DOCUMENT_APPROVER',
5014                    l_role);
5015 
5016            WF_ENGINE.SetItemAttrText(itemtype,
5017                    itemkey,
5018                    'ORIG_SYSTEM',
5019                    l_next_approver.ORIG_SYSTEM);
5020 
5021            WF_ENGINE.SetItemAttrNumber(itemtype,
5022                         itemkey,
5023                         'NOTF_ITERATION',
5024                         nvl(l_notf_iteration,0) + 1);
5025 
5026 --  bug 8450681
5027 -- These values have to be cleared of before starting as the residue values will cause issue
5028 -- in case of approver groups which has more than one approver and
5029 -- the notification is escalated in the previous level
5030            WF_ENGINE.SetItemAttrText(itemtype,
5031                    itemkey,
5032                    'ESCALATED',
5033                    null);
5034            WF_ENGINE.SetItemAttrText(itemtype,
5035                    itemkey,
5036                    'FORWARD_ROLE',
5037                    null);
5038            WF_ENGINE.SetItemAttrText(itemtype,
5039                    itemkey,
5040                    'ROLE_ACTUAL',
5041                    null);
5042 
5043            WF_ENGINE.SetItemAttrText(itemtype,
5044                    itemkey,
5045                    'ESC_ROLE_ACTUAL',
5046                    null);
5047 
5048 
5049 --  bug 8450681
5050 	   /*
5051            WF_ENGINE.SETITEMATTRTEXT
5052                 (
5053                  itemtype => itemtype,
5054                  itemkey  => itemkey,
5055                  aname    => 'INVOICE_ATTACHMENTS',
5056                  avalue   => ('FND:entity=AP_INVOICES' || l_ampersand ||
5057                          'pk1name=INVOICE_ID' || l_ampersand
5058                                               || 'pk1value=' || l_invoice_id));
5059 	   */
5060            --call set attributes so that notification tokens will be correct
5061            --set_attribute_values(itemtype,itemkey);
5062       l_hist_rec.HISTORY_TYPE := 'DOCUMENTAPPROVAL';
5063       l_hist_rec.INVOICE_ID   := l_invoice_id;
5064       l_hist_rec.ITERATION    := l_iteration;
5065       l_hist_rec.NOTIFICATION_ORDER := nvl(l_notf_iteration,0) + 1;
5066       l_hist_rec.RESPONSE     := 'SENT';
5067       l_hist_rec.APPROVER_ID  := l_next_approver.ORIG_SYSTEM_ID;
5068       /********** POTENTIAL BUG ABOVE********************/
5069       l_hist_rec.APPROVER_NAME:= l_display_name;
5070       l_hist_rec.CREATED_BY   := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
5071       l_hist_rec.CREATION_DATE:= sysdate;
5072       l_hist_rec.LAST_UPDATE_DATE := sysdate;
5073       l_hist_rec.LAST_UPDATED_BY  := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
5074       l_hist_rec.LAST_UPDATE_LOGIN := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),-1);
5075       l_hist_rec.ORG_ID            := l_org_id;
5076       l_hist_rec.AMOUNT_APPROVED   := 0;
5077 
5078       insert_history_table(p_hist_rec => l_hist_rec);
5079            resultout := wf_engine.eng_completed||':'||'MORE';
5080 
5081    END IF;
5082 
5083    ELSE  /* For IF l_count_rejects = 0, so there are rejections */
5084       /* Should never go through following update since all lines
5085       should be either Rejected or approved by this time */
5086       UPDATE AP_INVOICE_LINES_ALL
5087       SET    wfapproval_status = 'REQUIRED'
5088       WHERE  invoice_id = l_invoice_id
5089       AND    wfapproval_status = 'INITIATED';
5090 
5091       UPDATE ap_invoices_all
5092       SET    wfapproval_status = 'NEEDS WFREAPPROVAL'
5093       WHERE  invoice_id = l_invoice_id;
5094 
5095       resultout := wf_engine.eng_completed||':'||'FINISH';
5096    END IF;
5097 EXCEPTION
5098 
5099 WHEN OTHERS
5100    THEN
5101         WF_CORE.CONTEXT('APINVAPR','get_header_approver',itemtype,
5102                         itemkey, to_char(actid), funcmode);
5103         RAISE;
5104 
5105 END get_header_approver;
5106 PROCEDURE escalate_doc_approval(itemtype IN VARCHAR2,
5107                         itemkey IN VARCHAR2,
5108                         actid   IN NUMBER,
5109                         funcmode IN VARCHAR2,
5110                         resultout  OUT NOCOPY VARCHAR2 ) IS
5111    l_esc_approver          AME_UTIL.approverRecord2;
5112    l_name                  wf_users.name%TYPE; --bug 8620671
5113    l_esc_approver_name     VARCHAR2(150);
5114    l_esc_approver_id       NUMBER(15);
5115    l_approver_id           NUMBER(15);
5116    l_invoice_id            NUMBER(15);
5117    l_hist_id               NUMBER(15);
5118    l_role                  VARCHAR2(50);
5119    l_esc_role              VARCHAR2(50);
5120    l_esc_role_display      VARCHAR2(150);
5121    l_org_id                NUMBER(15);
5122    l_level                 VARCHAR2(10);
5123    l_api_name              CONSTANT VARCHAR2(200) :=
5124                                    'escalate_doc_approval';
5125    l_debug_info            VARCHAR2(2000);
5126    l_iteration             NUMBER;
5127    l_invoice_total         NUMBER;
5128    l_hist_rec              AP_INV_APRVL_HIST%ROWTYPE;
5129    l_notf_iteration        NUMBER;
5130 -- bug 8450681 begin
5131       l_display_name           VARCHAR2(150);
5132       l_next_approvers  ame_util.approversTable2;
5133       l_next_approver   ame_util.approverRecord2;
5134       l_index           ame_util.idList;
5135       l_ids             ame_util.stringList;
5136       l_class           ame_util.stringList;
5137       l_source          ame_util.longStringList;
5138       l_complete        VARCHAR2(1);
5139 -- bug 8450681 end
5140 BEGIN
5141    l_debug_info := 'Start';
5142    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5143           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
5144                          l_api_name,l_debug_info);
5145    END IF;
5146 
5147    --Get the current approver info
5148    l_approver_id := WF_ENGINE.GetItemAttrNumber(itemtype,
5149                              itemkey,
5150                              'APPROVER_ID');
5151 
5152    l_invoice_id := WF_ENGINE.GetItemAttrNumber(itemtype,
5153                              itemkey,
5154                              'INVOICE_ID');
5155 
5156    l_invoice_total := WF_ENGINE.GetItemAttrNumber(itemtype,
5157                              itemkey,
5158                              'INVOICE_TOTAL');
5159 
5160    l_iteration := WF_ENGINE.GetItemAttrNumber(itemtype,
5161                              itemkey,
5162                              'ITERATION');
5163 
5164    l_notf_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
5165                         itemkey,
5166                         'NOTF_ITERATION');
5167 
5168    l_org_id := WF_ENGINE.GetItemAttrNumber(itemtype,
5169                              itemkey,
5170                              'ORG_ID');
5171 
5172    l_role  := WF_ENGINE.GetItemAttrText(itemtype,
5173                              itemkey,
5174                              'DOCUMENT_APPROVER');
5175 
5176 
5177    --Now set the environment
5178    MO_GLOBAL.INIT ('SQLAP');
5179    MO_GLOBAL.set_policy_context('S',l_org_id);
5180 
5181    -- see if we have an TCA/WF Directory api for this select
5182    /*
5183    SELECT supervisor_id
5184    INTO l_esc_approver_id
5185    FROM per_employees_current_x
5186    WHERE employee_id = l_approver_id;
5187    */
5188  -- bug#6837841 Changes  Starts
5189    /*SELECT user_id
5190    INTO   l_esc_approver_id
5191    FROM   fnd_user
5192    WHERE  employee_id =
5193           (SELECT supervisor_id
5194 	   FROM   per_employees_current_x
5195 	   WHERE  employee_id = l_approver_id);
5196 
5197    WF_DIRECTORY.GetUserName('FND_USR',
5198                    l_esc_approver_id,
5199                    l_name,
5200                    l_esc_approver_name);
5201    WF_DIRECTORY.GetRoleName('FND_USR',
5202                    l_esc_approver_id,
5203                    l_esc_role,
5204                    l_esc_role_display);*/
5205 
5206    SELECT supervisor_id   INTO   l_esc_approver_id
5207 	   FROM   per_employees_current_x
5208 	   WHERE  employee_id = l_approver_id;
5209 
5210    WF_DIRECTORY.GetUserName('PER',
5211                    l_esc_approver_id,
5212                    l_name,
5213                    l_esc_approver_name);
5214    WF_DIRECTORY.GetRoleName('PER',
5215                    l_esc_approver_id,
5216                    l_esc_role,
5217                    l_esc_role_display);
5218 -- bug#6837841 Changes End
5219    l_esc_approver.name := l_esc_role;
5220    l_esc_approver.api_insertion := ame_util.apiInsertion;
5221    l_esc_approver.authority := ame_util.authorityApprover;
5222    l_esc_approver.approval_status := ame_util.forwardStatus;
5223 
5224    --update AME
5225    AME_API2.updateApprovalStatus2(applicationIdIn => 200,
5226                       transactionTypeIn =>  'APINV',
5227                       transactionIdIn     => to_char(l_invoice_id),
5228                       approvalStatusIn    => AME_UTIL.noResponseStatus,
5229                       approverNameIn  => l_role,
5230                       itemClassIn    => ame_util.headerItemClassName,
5231                       itemIdIn    => to_char(l_invoice_id),
5232                       forwardeeIn       => l_esc_approver);
5233 -- bug 8450681  begins
5234 --get the next layer (stage) of approvers
5235   AME_API2.getNextApprovers1
5236            (applicationIdIn               => 200,
5237             transactionTypeIn             => 'APINV',
5238             transactionIdIn               => to_char(l_invoice_id),
5239             flagApproversAsNotifiedIn     => ame_util.booleanFalse,
5240             approvalProcessCompleteYNOut  => l_complete,
5241             nextApproversOut              => l_next_approvers,
5242             itemIndexesOut                => l_index,
5243             itemIdsOut                    => l_ids,
5244             itemClassesOut                => l_class,
5245             itemSourcesOut                => l_source);
5246 -- bug 8450681 ends
5247 
5248    --Set WF attributes
5249    WF_ENGINE.SetItemAttrText(itemtype,
5250                    itemkey,
5251                    'ESC_APPROVER_NAME',
5252                    l_esc_approver_name);
5253 
5254    WF_ENGINE.SetItemAttrNumber(itemtype,
5255                    itemkey,
5256                    'ESC_APPROVER_ID',
5257                    l_esc_approver_id);
5258 
5259    WF_ENGINE.SetItemAttrText(itemtype,
5260                    itemkey,
5261                    'ESC_ROLE_NAME',
5262                    l_esc_role);
5263 -- bug 8450681 begins
5264    WF_ENGINE.SetItemAttrText(itemtype,
5265                    itemkey,
5266                    'ESC_ROLE_ACTUAL',
5267                    l_esc_role);
5268 -- bug 8450681  ends
5269    WF_ENGINE.SetItemAttrText(itemtype,
5270                    itemkey,
5271                    'ESCALATED',
5272                    'Y');
5273    l_hist_rec.HISTORY_TYPE := 'DOCUMENTAPPROVAL';
5274    l_hist_rec.INVOICE_ID   := l_invoice_id;
5275    l_hist_rec.ITERATION    := l_iteration;
5276    l_hist_rec.NOTIFICATION_ORDER := l_notf_iteration;
5277    l_hist_rec.RESPONSE     := 'ESCALATED';
5278    l_hist_rec.APPROVER_ID  := l_esc_approver_id;
5279    l_hist_rec.APPROVER_NAME:= l_esc_approver_name;
5280    l_hist_rec.CREATED_BY   := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
5281    l_hist_rec.CREATION_DATE:= sysdate;
5282    l_hist_rec.LAST_UPDATE_DATE := sysdate;
5283    l_hist_rec.LAST_UPDATED_BY  := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
5284    l_hist_rec.LAST_UPDATE_LOGIN := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),-1);
5285    l_hist_rec.ORG_ID            := l_org_id;
5286    l_hist_rec.AMOUNT_APPROVED   := l_invoice_total;
5287 
5288    l_debug_info := 'Before insert_history_table';
5289    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5290           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
5291                          l_api_name,l_debug_info);
5292    END IF;
5293    insert_history_table(p_hist_rec => l_hist_rec);
5294 
5295 EXCEPTION
5296 WHEN OTHERS
5297    THEN
5298         WF_CORE.CONTEXT('APINVAPR','get_header_approver',itemtype,
5299                         itemkey, to_char(actid), funcmode);
5300         RAISE;
5301 END escalate_doc_approval;
5302 PROCEDURE escalate_lines_approval(itemtype IN VARCHAR2,
5303                         itemkey IN VARCHAR2,
5304                         actid   IN NUMBER,
5305                         funcmode IN VARCHAR2,
5306                         resultout  OUT NOCOPY VARCHAR2 ) IS
5307 
5308    --Define cursor for lines affected by notification
5309    CURSOR   Items_Cur(itemkey IN VARCHAR2) IS
5310    SELECT invap.Item_Class, invap.Item_Id, invap.Role_Name,
5311           invap.Invoice_Key, al.line_number, al.amount
5312    FROM   AP_APINV_APPROVERS invap, AP_INVOICE_LINES_ALL al
5313    WHERE  invap.Notification_Key = itemkey
5314    AND    al.line_number = invap.line_number
5315    AND    al.invoice_id  = invap.invoice_id;
5316 
5317    l_esc_approver       AME_UTIL.approverRecord2;
5318    l_name               wf_users.name%TYPE; --bug 8620671
5319    l_esc_approver_name  VARCHAR2(150);
5320    l_esc_approver_id    NUMBER(15);
5321    l_approver_id   	NUMBER(15);
5322    l_invoice_id    	NUMBER(15);
5323    l_hist_id       	NUMBER(15);
5324    l_role	        VARCHAR2(50);
5325    l_esc_role         	VARCHAR2(50);
5326    l_esc_role_display  	VARCHAR2(150);
5327    l_org_id        	NUMBER(15);
5328    l_api_name           CONSTANT VARCHAR2(200) :=
5329                                         'Escalate_Lines_approval';
5330    l_debug_info         VARCHAR2(2000);
5331    l_iteration          NUMBER;
5332    l_item_class		VARCHAR2(50);
5333    l_item_id		NUMBER;
5334    l_line_number        ap_invoice_lines_all.line_number%TYPE;
5335    l_line_amount        ap_invoice_lines_all.amount%TYPE;
5336    l_hist_rec           AP_INV_APRVL_HIST%ROWTYPE;
5337    l_comments           VARCHAR2(240);
5338    l_esc_flag           VARCHAR2(1);
5339    l_invoice_key        VARCHAR2(50);
5340    l_notf_iteration     NUMBER;
5341    -- bug 13919019 begin
5342       l_display_name           VARCHAR2(150);
5343       l_next_approvers  ame_util.approversTable2;
5344       l_next_approver   ame_util.approverRecord2;
5345       l_index           ame_util.idList;
5346       l_ids             ame_util.stringList;
5347       l_class           ame_util.stringList;
5348       l_source          ame_util.longStringList;
5349       l_complete        VARCHAR2(1);
5350   -- bug 13919019 ends
5351 
5352 
5353 BEGIN
5354 
5355    l_debug_info := 'Start';
5356    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5357           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
5358                          l_api_name,l_debug_info);
5359    END IF;
5360 
5361 
5362    --Get the current approver info
5363    l_approver_id := WF_ENGINE.GetItemAttrNumber(itemtype,
5364                                   itemkey,
5365                                   'APPROVER_ID');
5366 
5367    l_invoice_id := WF_ENGINE.GetItemAttrNumber(itemtype,
5368                                   itemkey,
5369                                   'INVOICE_ID');
5370 
5371 --commented for bug 13566970
5372   /* l_iteration := WF_ENGINE.GetItemAttrNumber(itemtype,
5373                                   itemkey,
5374                                   'ITERATION'); */
5375   --added for bug 13566970
5376   l_iteration := WF_ENGINE.GetItemAttrNumber(itemtype,
5377                                   itemkey,
5378                                   'INVOICE_ITERATION');
5379   --end og bug 13566970
5380 
5381    l_org_id := WF_ENGINE.GetItemAttrNumber(itemtype,
5382                                   itemkey,
5383                                   'ORG_ID');
5384 
5385    l_role  := WF_ENGINE.GetItemAttrText(itemtype,
5386                                   itemkey,
5387                                   'ROLE_NAME');
5388 
5389    l_notf_iteration := WF_ENGINE.GETITEMATTRNumber(itemtype,
5390                         itemkey,
5391                         'NOTF_ITERATION');
5392 
5393    --Now set the environment
5394    MO_GLOBAL.INIT ('SQLAP');
5395    MO_GLOBAL.set_policy_context('S',l_org_id);
5396 
5397    --amy see if we have an TCA/WF Directory api for this select
5398    /*
5399    SELECT supervisor_id
5400    INTO l_esc_approver_id
5401    FROM per_employees_current_x
5402    WHERE employee_id = l_approver_id;
5403    */
5404 /* -- commented for 8682244
5405    SELECT user_id
5406    INTO   l_esc_approver_id
5407    FROM   fnd_user
5408    WHERE  employee_id =
5409           (SELECT supervisor_id
5410            FROM   per_employees_current_x
5411            WHERE  employee_id = l_approver_id);
5412 
5413 
5414    WF_DIRECTORY.GetUserName('FND_USR',
5415                         l_esc_approver_id,
5416                         l_name,
5417                         l_esc_approver_name);
5418 
5419    WF_DIRECTORY.GetRoleName('FND_USR',
5420    			l_esc_approver_id,
5421    			l_esc_role,
5422    			l_esc_role_display);
5423 */
5424 SELECT supervisor_id   INTO   l_esc_approver_id
5425 	   FROM   per_employees_current_x
5426 	   WHERE  employee_id = l_approver_id;
5427 
5428    WF_DIRECTORY.GetUserName('PER',
5429                    l_esc_approver_id,
5430                    l_name,
5431                    l_esc_approver_name);
5432    WF_DIRECTORY.GetRoleName('PER',
5433                    l_esc_approver_id,
5434                    l_esc_role,
5435                    l_esc_role_display);
5436 -- modified for 8682244
5437 
5438    l_esc_approver.name := l_esc_role;
5439    l_esc_approver.api_insertion := ame_util.apiInsertion;
5440    l_esc_approver.authority := ame_util.authorityApprover;
5441    l_esc_approver.approval_status := ame_util.forwardStatus;
5442 
5443    l_hist_rec.HISTORY_TYPE := 'LINESAPPROVAL';
5444    l_hist_rec.INVOICE_ID   := l_invoice_id;
5445    l_hist_rec.ITERATION    := l_iteration;
5446    l_hist_rec.NOTIFICATION_ORDER := l_notf_iteration;
5447    l_hist_rec.APPROVER_COMMENTS := l_comments;
5448    l_hist_rec.RESPONSE     := 'ESCALATED';
5449    l_hist_rec.APPROVER_ID  := l_esc_approver_id;
5450    l_hist_rec.APPROVER_NAME:= l_esc_approver_name;
5451    l_hist_rec.CREATED_BY   := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
5452    l_hist_rec.CREATION_DATE:= sysdate;
5453    l_hist_rec.LAST_UPDATE_DATE := sysdate;
5454    l_hist_rec.LAST_UPDATED_BY  := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
5455    l_hist_rec.LAST_UPDATE_LOGIN := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),-1);
5456    l_hist_rec.ORG_ID            := l_org_id;
5457    --update AME status
5458    --For Future  check with ame as to when updateApprovalStatuses will be
5459    --available so there  will not be a need to loop.
5460    OPEN Items_Cur(itemkey);
5461    LOOP
5462 
5463       FETCH Items_Cur INTO l_item_class, l_item_id, l_name,
5464                                    l_invoice_key,l_line_number, l_line_amount;
5465       EXIT WHEN Items_Cur%NOTFOUND OR Items_Cur%NOTFOUND IS NULL;
5466 
5467       --update AME
5468       AME_API2.updateApprovalStatus2(applicationIdIn => 200,
5469               		   transactionTypeIn =>  'APINV',
5470                            transactionIdIn     => to_char(l_invoice_id),
5471                            approvalStatusIn    => AME_UTIL.noResponseStatus,
5472                            approverNameIn  => l_role,
5473                            itemClassIn    => l_item_class,
5474                            itemIdIn    => l_item_id,
5475                            forwardeeIn       => l_esc_approver);
5476 
5477       l_hist_rec.line_number   := l_line_number;
5478       l_hist_rec.AMOUNT_APPROVED   := l_line_amount;
5479 
5480       l_debug_info := 'Before calling insert_history_table for Line'
5481                        || l_line_number;
5482       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5483              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
5484                             l_api_name,l_debug_info);
5485       END IF;
5486 
5487       insert_history_table(p_hist_rec => l_hist_rec);
5488 
5489    END LOOP;
5490    CLOSE Items_Cur;
5491 
5492    -- bug 13919019  begins
5493   --get the next layer (stage) of approvers
5494   AME_API2.getNextApprovers1
5495            (applicationIdIn               => 200,
5496             transactionTypeIn             => 'APINV',
5497             transactionIdIn               => to_char(l_invoice_id),
5498             flagApproversAsNotifiedIn     => ame_util.booleanFalse,
5499             approvalProcessCompleteYNOut  => l_complete,
5500             nextApproversOut              => l_next_approvers,
5501             itemIndexesOut                => l_index,
5502             itemIdsOut                    => l_ids,
5503             itemClassesOut                => l_class,
5504             itemSourcesOut                => l_source);
5505   -- bug 13919019  ends
5506 
5507    --Set WF attributes
5508    WF_ENGINE.SetItemAttrText(itemtype,
5509                         itemkey,
5510                         'ESC_APPROVER_NAME',
5511                         l_esc_approver_name);
5512 
5513    WF_ENGINE.SetItemAttrNumber(itemtype,
5514                         itemkey,
5515                         'ESC_APPROVER_ID',
5516                         l_esc_approver_id);
5517 
5518    WF_ENGINE.SetItemAttrText(itemtype,
5519                         itemkey,
5520                         'ESC_ROLE_NAME',
5521                         l_esc_role);
5522 
5523    -- bug 13919019 begins
5524    WF_ENGINE.SetItemAttrText(itemtype,
5525                    itemkey,
5526                    'ESC_ROLE_ACTUAL',
5527                    l_esc_role);
5528   -- bug 13919019 ends
5529 
5530    WF_ENGINE.SetItemAttrText(itemtype,
5531                    itemkey,
5532                    'ESCALATED',
5533                    'Y');
5534 
5535 EXCEPTION
5536 WHEN OTHERS
5537    THEN
5538         WF_CORE.CONTEXT('APINVAPR','get_header_approver',itemtype,
5539                         itemkey, to_char(actid), funcmode);
5540         RAISE;
5541 END escalate_lines_approval;
5542 PROCEDURE awake_approval_main(itemtype IN VARCHAR2,
5543                         itemkey IN VARCHAR2,
5544                         actid   IN NUMBER,
5545                         funcmode IN VARCHAR2,
5546                         resultout  OUT NOCOPY VARCHAR2 ) IS
5547    l_api_name           CONSTANT VARCHAR2(200) :=
5548                                         'awake_approval_main';
5549    l_debug_info         VARCHAR2(2000);
5550    l_invoice_key        VARCHAR2(150);
5551 
5552 BEGIN
5553    l_debug_info := 'Start';
5554    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5555           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
5556                          l_api_name,l_debug_info);
5557    END IF;
5558    BEGIN
5559 
5560       SELECT invoice_key
5561       INTO   l_invoice_key
5562       FROM   AP_APINV_APPROVERS
5563       WHERE  Notification_Key = itemkey
5564       AND    rownum = 1;
5565    EXCEPTION
5566       WHEN NO_DATA_FOUND THEN
5567          l_debug_info := 'No Data Found in SELECT from AP_APINV_APPROVERS' ;
5568       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5569              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
5570                             l_api_name,l_debug_info);
5571       END IF;
5572       RAISE;
5573 
5574    END;
5575    l_debug_info := 'Before CompleteActivity APPROVAL_MAIN:BLOCK' ||
5576                    'l_invoice_key = ' || l_invoice_key;
5577    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5578           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
5579                          l_api_name,l_debug_info);
5580    END IF;
5581    wf_engine.CompleteActivity(
5582                         itemType => 'APINVAPR',
5583                         itemKey  => l_invoice_key,
5584                         activity => 'APPROVAL_MAIN:BLOCK',
5585                         result   => 'NULL');
5586 
5587    l_debug_info := 'End';
5588    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5589           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
5590                          l_api_name,l_debug_info);
5591    END IF;
5592 
5593 
5594 EXCEPTION
5595 WHEN OTHERS
5596    THEN
5597         WF_CORE.CONTEXT('APINVAPR','awake_approval_main',itemtype,
5598                         itemkey, to_char(actid), funcmode);
5599         RAISE;
5600 END awake_approval_main;
5601 
5602 PROCEDURE create_lineapp_neg_process(p_invoice_id IN NUMBER,
5603 					  p_ext_user_id IN NUMBER,
5604 					  p_invoice_amount IN NUMBER,
5605                                           parentkey IN VARCHAR2,
5606                                           childkey  IN VARCHAR2,
5607 					  int_ext_indicator IN VARCHAR2,
5608 					  p_wfitemkey OUT NOCOPY VARCHAR2) IS
5609 /*-----------------------------------------------------------------------+
5610  | Cursor Declarations                                                   |
5611  +-----------------------------------------------------------------------*/
5612    CURSOR csr_ap_aprvl_neg_details IS
5613    SELECT PV.vendor_name,
5614           AI.invoice_num,
5615           AI.invoice_date,
5616           AI.description,
5617           AI.org_id,
5618           AI.invoice_id,
5619           AI.approval_iteration,
5620           AI.vendor_contact_id,
5621           NVL(AI.invoice_amount, 0)
5622    FROM   ap_invoices_all AI,
5623           po_vendors PV
5624    WHERE  AI.invoice_id = p_invoice_id
5625    AND    AI.vendor_id = PV.vendor_id;
5626    l_api_name      CONSTANT VARCHAR2(200) := 'create_lineapproval_neg_process';
5627    l_vendor_name po_vendors.vendor_name%TYPE;
5628    l_invoice_num ap_invoices_all.invoice_num%TYPE;
5629    l_invoice_date  ap_invoices_all.invoice_date%TYPE;
5630    l_invoice_description ap_invoices_all.description%TYPE;
5631    l_invoice_id ap_invoices_all.invoice_id%TYPE;
5632    l_org_id ap_invoices_all.org_id%TYPE;
5633    l_name                  wf_users.name%TYPE; --bug 8620671
5634    l_display_name          VARCHAR2(150);
5635    l_role                  VARCHAR2(50);
5636    l_role_display          VARCHAR2(150);
5637    l_new_child_ItemKey     VARCHAR2(30);
5638    l_person_id             NUMBER(15);
5639    l_hist_rec              AP_INV_APRVL_HIST%ROWTYPE;
5640    l_notf_receipient_type  VARCHAR2(50);
5641    l_iteration             NUMBER;
5642    l_notf_iteration        NUMBER;
5643    l_invoice_key   VARCHAR2(50);
5644    l_role_name          AP_APINV_APPROVERS.ROLE_NAME%TYPE;
5645    l_item_class    AP_APINV_APPROVERS.ITEM_CLASS%TYPE;
5646    l_item_id       AP_APINV_APPROVERS.ITEM_ID%TYPE;
5647    l_debug_info    VARCHAR2(2000);
5648    l_line_number   ap_invoice_lines_all.line_number%TYPE;
5649    l_line_amount   ap_invoice_lines_all.amount%TYPE;
5650    l_ext_person_id NUMBER(15);
5651    l_ext_user_id NUMBER(15);
5652    l_total                 ap_invoices_all.invoice_amount%TYPE;
5653 
5654 BEGIN
5655    l_debug_info := 'Start';
5656    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5657           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
5658                          l_api_name,l_debug_info);
5659    END IF;
5660    SELECT AP_NEGOTIATION_HIST_S.nextval
5661    INTO   l_new_child_ItemKey
5662    FROM   dual;
5663 
5664    OPEN csr_ap_aprvl_neg_details;
5665    FETCH csr_ap_aprvl_neg_details INTO
5666          l_vendor_name,
5667 	 l_invoice_num,
5668 	 l_invoice_date,
5669 	 l_invoice_description,
5670 	 l_org_id,
5671 	 l_invoice_id,
5672 	 l_iteration,
5673 	 l_ext_user_id,
5674          l_total;
5675    CLOSE csr_ap_aprvl_neg_details;
5676 
5677    l_ext_user_id := p_ext_user_id;
5678 
5679    wf_engine.createProcess('APINVNEG', l_new_child_itemkey,
5680                            'APPROVAL_NEGOTIATION');
5681    WF_ENGINE.setItemParent('APINVNEG', l_new_child_itemkey,
5682 	                   'APINVAPR', parentkey, null);
5683    WF_ENGINE.SetItemAttrNumber('APINVNEG',
5684                         l_new_child_itemkey,
5685                         'INVOICE_ID',
5686                         l_invoice_id);
5687    WF_ENGINE.SetItemAttrNumber('APINVNEG',
5688                         l_new_child_itemkey,
5689                         'ORG_ID',
5690                         l_org_id);
5691    WF_ENGINE.SetItemAttrNumber('APINVNEG',
5692                         l_new_child_itemkey,
5693                         'INVOICE_TOTAL',
5694                         nvl(p_invoice_amount,l_total));
5695    WF_ENGINE.SetItemAttrNumber('APINVAPR',
5696                         parentkey,
5697                         'INVOICE_TOTAL',
5698                         nvl(p_invoice_amount,l_total));
5699    WF_ENGINE.SetItemAttrText('APINVNEG',
5700                         l_new_child_itemkey,
5701                         'INVOICE_SUPPLIER_NAME',
5702                         l_vendor_name);
5703    WF_ENGINE.SetItemAttrText('APINVNEG',
5704                         l_new_child_itemkey,
5705                         'INVOICE_NUMBER',
5706                         l_invoice_num);
5707    WF_ENGINE.SetItemAttrDate('APINVNEG',
5708                         l_new_child_itemkey,
5709                         'INVOICE_DATE',
5710                         l_invoice_date);
5711    WF_ENGINE.SetItemAttrText('APINVNEG',
5712                         l_new_child_itemkey,
5713                         'INVOICE_DESCRIPTION',
5714                         l_invoice_description);
5715    WF_ENGINE.SetItemAttrText('APINVNEG',
5716                         l_new_child_itemkey,
5717                         'PARENT_KEY',
5718                         parentkey);
5719    WF_ENGINE.SetItemAttrText('APINVNEG',
5720                         l_new_child_itemkey,
5721                         'NOTIFICATION_KEY',
5722                         l_new_child_itemkey);
5723 
5724    WF_ENGINE.SetItemAttrNumber('APINVNEG',
5725                         l_new_child_itemkey,
5726                         'ITERATION',
5727                         l_iteration);
5728    WF_ENGINE.SetItemAttrText('APINVNEG',
5729                                   l_new_child_itemkey,
5730                                   'ORIG_SYSTEM',
5731                                   WF_ENGINE.GETITEMATTRText('APINVAPR',
5732 				                             parentkey,
5733 						             'ORIG_SYSTEM'));
5734    /* Currently the sender is Internal  and sending to External*/
5735    IF int_ext_indicator = 'I' and l_ext_user_id IS NOT NULL THEN
5736 
5737       WF_DIRECTORY.GetRoleName('FND_USR',l_ext_user_id,l_role,
5738                                l_role_display);
5739       l_person_id := l_ext_user_id;
5740       WF_DIRECTORY.GetUserName('FND_USR',
5741                                 l_ext_user_id,
5742                                 l_name,
5743                                 l_display_name);
5744       WF_ENGINE.SetItemAttrText('APINVNEG',
5745                         l_new_child_itemkey,
5746                         'SUPPLIER_ROLE',
5747                         l_role);
5748       WF_ENGINE.SetItemAttrText('APINVNEG',
5749                                   l_new_child_itemkey,
5750                                   'SUPPLIER_DISPLAY_NAME',
5751                                   l_display_name);
5752       WF_ENGINE.SetItemAttrNumber('APINVNEG',
5753                         l_new_child_itemkey,
5754                         'SUPPLIER_PERSON_ID',
5755                         l_ext_user_id);
5756 
5757       WF_ENGINE.SetItemAttrText('APINVNEG',
5758                         l_new_child_itemkey,
5759                         'NOTF_RECEIPIENT_TYPE',
5760                         'EXTERNAL');
5761       WF_ENGINE.SetItemAttrText('APINVNEG',
5762                                   l_new_child_itemkey,
5763                                   'INTERNAL_REP_DISPLAY_NAME',
5764                                   WF_ENGINE.GETITEMATTRText('APINVAPR',
5765                                                              parentkey,
5766                                                              'APPROVER_NAME'));
5767       WF_ENGINE.SetItemAttrText('APINVNEG',
5768                                   l_new_child_itemkey,
5769                                   'INTERNAL_REP_ROLE',
5770                                   WF_ENGINE.GETITEMATTRText('APINVAPR',
5771                                                              parentkey,
5772                                                              'DOCUMENT_APPROVER'));
5773 
5774    /* Currently the sender is external (supplier) and sending to
5775       Internal Rep */
5776    ELSIF int_ext_indicator = 'E' THEN
5777 
5778       l_role := WF_ENGINE.GETITEMATTRText('APINVAPR',
5779                                           parentkey,
5780 			                  'DOCUMENT_APPROVER');
5781       WF_ENGINE.SetItemAttrText('APINVNEG',
5782                         l_new_child_itemkey,
5783                         'INTERNAL_REP_ROLE',
5784                         l_role);
5785       WF_ENGINE.SetItemAttrText('APINVNEG',
5786                         l_new_child_itemkey,
5787                         'DISP_NOT_RECEIVER',
5788                         l_role);
5789       l_display_name := WF_ENGINE.GETITEMATTRText('APINVAPR',
5790                                           parentkey,
5791 			                  'APPROVER_NAME');
5792       WF_ENGINE.SetItemAttrText('APINVNEG',
5793                                   l_new_child_itemkey,
5794                                   'INTERNAL_REP_DISPLAY_NAME',
5795                                   l_display_name);
5796       l_person_id := WF_ENGINE.GETITEMATTRNumber('APINVAPR',
5797                                           parentkey,
5798 			                  'APPROVER_ID');
5799       WF_ENGINE.SetItemAttrNumber('APINVNEG',
5800                         l_new_child_itemkey,
5801                         'INTERNAL_REP_PERSON_ID',
5802                         l_person_id);
5803       WF_ENGINE.SetItemAttrText('APINVNEG',
5804                         l_new_child_itemkey,
5805                         'NOTF_RECEIPIENT_TYPE',
5806                         'INTERNAL');
5807       WF_ENGINE.SetItemAttrText('APINVNEG',
5808                                   l_new_child_itemkey,
5809                                   'SUPPLIER_DISPLAY_NAME',
5810                                   WF_ENGINE.GETITEMATTRText('APINVNEG',
5811                                                              childkey,
5812                                                              'SUPPLIER_DISPLAY_NAME'));
5813       WF_ENGINE.SetItemAttrText('APINVNEG',
5814                                   l_new_child_itemkey,
5815                                   'SUPPLIER_ROLE',
5816                                   WF_ENGINE.GETITEMATTRText('APINVNEG',
5817                                                              childkey,
5818                                                              'SUPPLIER_ROLE'));
5819 
5820    END IF;
5821    WF_ENGINE.startProcess('APINVNEG', l_new_child_itemkey);
5822    MO_GLOBAL.INIT ('SQLAP');
5823    MO_GLOBAL.set_policy_context('S',l_org_id);
5824 
5825    IF childkey IS NOT NULL THEN
5826       l_notf_receipient_type :=  WF_ENGINE.GetItemAttrText('APINVNEG',
5827                                   childkey,
5828                                   'NOTF_RECEIPIENT_TYPE');
5829       IF l_notf_receipient_type = 'INTERNAL' THEN
5830 
5831           wf_engine.CompleteActivity(
5832                      itemType => 'APINVNEG',
5833                      itemKey  => childkey,
5834                      activity => 'APPROVAL_NEGOTIATION:WAITINTLINEAPRVL',
5835                      result   => 'NULL');
5836       ELSE
5837           wf_engine.CompleteActivity(
5838                      itemType => 'APINVNEG',
5839                      itemKey  => childkey,
5840                      activity => 'APPROVAL_NEGOTIATION:WAITEXTLINEAPRVL',
5841                      result   => 'NULL');
5842       END IF;
5843 
5844    END IF;
5845 
5846    p_wfitemkey := l_new_child_itemkey;
5847    l_debug_info := 'End';
5848    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5849           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
5850                          l_api_name,l_debug_info);
5851    END IF;
5852 
5853 
5854 EXCEPTION
5855 
5856 WHEN OTHERS
5857    THEN
5858         RAISE;
5859 
5860 END create_lineapp_neg_process;
5861 
5862 PROCEDURE create_invapp_process(p_invoice_id IN NUMBER
5863                        ,p_approval_iteration IN NUMBER DEFAULT NULL
5864                        ,p_wfitemkey OUT NOCOPY VARCHAR2) IS
5865 /*-----------------------------------------------------------------------+
5866  | Cursor Declarations                                                   |
5867  +-----------------------------------------------------------------------*/
5868 -- Bug5892455 added the union for 'Payment Request' type invoices
5869 -- For payment Request type invoices the vendor is a customer whose
5870 -- data wil be there in hz_party tables
5871 CURSOR csr_ap_inv_details IS
5872 SELECT PV.vendor_name,
5873        AI.invoice_num,
5874        AI.invoice_date,
5875        AI.description,
5876        AI.org_id,
5877        AI.invoice_id,
5878        NVL(AI.invoice_amount, 0)
5879 FROM   ap_invoices_all AI,
5880        ap_suppliers PV
5881 WHERE  AI.invoice_id = p_invoice_id
5882 AND    AI.invoice_type_lookup_code <> 'PAYMENT REQUEST'
5883 AND    AI.vendor_id = PV.vendor_id
5884 UNION ALL
5885 SELECT HZP.party_name,
5886        AI.invoice_num,
5887        AI.invoice_date,
5888        AI.description,
5889        AI.org_id,
5890        AI.invoice_id,
5891        NVL(AI.invoice_amount, 0)
5892 FROM   ap_invoices_all AI,
5893        hz_parties HZP
5894 WHERE  AI.invoice_id = p_invoice_id
5895 AND    AI.invoice_type_lookup_code = 'PAYMENT REQUEST'
5896 AND    AI.party_id = HZP.party_id;
5897 
5898 l_vendor_name po_vendors.vendor_name%TYPE;
5899 l_invoice_num ap_invoices_all.invoice_num%TYPE;
5900 l_invoice_date  ap_invoices_all.invoice_date%TYPE;
5901 l_invoice_description ap_invoices_all.description%TYPE;
5902 l_invoice_id ap_invoices_all.invoice_id%TYPE;
5903 l_org_id ap_invoices_all.org_id%TYPE;
5904 l_itemkey VARCHAR2(50);
5905 l_iteration AP_INVOICES_ALL.APPROVAL_ITERATION%TYPE;
5906 l_api_name      CONSTANT VARCHAR2(200) := 'create_invapp_process';
5907 l_debug_info    VARCHAR2(2000);
5908 l_total                 ap_invoices_all.invoice_amount%TYPE;
5909 l_calling_sequence      VARCHAR2(2000);
5910 l_num NUMBER;
5911 
5912 
5913 
5914 BEGIN
5915    l_debug_info := 'Start';
5916    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5917           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
5918                          l_api_name,l_debug_info);
5919    END IF;
5920    l_debug_info := 'Before UPDATE AP_INVOICES_ALL';
5921    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5922           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
5923                          l_api_name,l_debug_info);
5924    END IF;
5925    l_calling_sequence := l_api_name;
5926    UPDATE AP_INVOICES_ALL
5927    SET    WFAPPROVAL_STATUS = 'INITIATED'
5928    WHERE  invoice_id = p_invoice_id;
5929    /*
5930    UPDATE AP_INVOICE_LINES_ALL
5931    SET    WFAPPROVAL_STATUS = 'INITIATED'
5932    where  invoice_id = p_invoice_id;
5933    */
5934 
5935    l_debug_info := 'Before ame_api2.clearAllApprovals';
5936    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5937           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
5938                          l_api_name,l_debug_info);
5939    END IF;
5940 
5941    ame_api2.clearAllApprovals(
5942                               applicationidin => 200,
5943                               transactiontypein => 'APINV',
5944                               transactionidin => p_invoice_id);
5945    OPEN csr_ap_inv_details;
5946    FETCH csr_ap_inv_details INTO
5947          l_vendor_name,
5948          l_invoice_num,
5949          l_invoice_date,
5950          l_invoice_description,
5951          l_org_id,
5952          l_invoice_id,
5953          l_total;
5954    CLOSE csr_ap_inv_details;
5955    l_iteration := nvl(p_approval_iteration,1);
5956    l_itemkey := to_char(p_invoice_id) || '_' || to_char(l_iteration);
5957 
5958    l_debug_info := 'Before Calling WF_ENGINE.createProcess(APINVAPR,'
5959                    || l_itemkey || ',APPROVAL_MAIN);';
5960    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5961           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
5962                          l_api_name,l_debug_info);
5963    END IF;
5964 
5965    wf_engine.createProcess('APINVAPR', l_itemkey, 'APPROVAL_MAIN');
5966    l_debug_info := 'Before setting item attributes' ;
5967    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5968           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
5969                          l_api_name,l_debug_info);
5970    END IF;
5971 
5972    WF_ENGINE.SetItemAttrNumber('APINVAPR',
5973                         l_itemkey,
5974                         'INVOICE_ID',
5975                         l_invoice_id);
5976    WF_ENGINE.SetItemAttrNumber('APINVAPR',
5977                         l_itemkey,
5978                         'ORG_ID',
5979                         l_org_id);
5980    WF_ENGINE.SetItemAttrNumber('APINVAPR',
5981                         l_itemkey,
5982                         'INVOICE_TOTAL',
5983                         l_total);
5984    WF_ENGINE.SetItemAttrText('APINVAPR',
5985                         l_itemkey,
5986                         'INVOICE_SUPPLIER_NAME',
5987                         l_vendor_name);
5988    WF_ENGINE.SetItemAttrText('APINVAPR',
5989                         l_itemkey,
5990                         'INVOICE_NUMBER',
5991                         l_invoice_num);
5992    WF_ENGINE.SetItemAttrDate('APINVAPR',
5993                         l_itemkey,
5994                         'INVOICE_DATE',
5995                         l_invoice_date);
5996    WF_ENGINE.SetItemAttrText('APINVAPR',
5997                         l_itemkey,
5998                         'INVOICE_DESCRIPTION',
5999                         l_invoice_description);
6000    WF_ENGINE.SetItemAttrNumber('APINVAPR',
6001                         l_itemkey,
6002                         'ITERATION',
6003                         l_iteration);
6004    WF_ENGINE.SetItemAttrText('APINVAPR',
6005                         l_itemkey,
6006                         'NOTIFICATION_KEY',
6007                         l_itemkey);
6008 
6009 
6010    /* Set wfapproval status at the line level so that AME doesnt return
6011       any Line levcel approvers for the matched case even if the rules
6012       have been so set up. */
6013    l_debug_info := 'Before UPDATE AP_INVOICE_LINES_ALL';
6014    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6015           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
6016                          l_api_name,l_debug_info);
6017    END IF;
6018 
6019    SELECT COUNT(*)
6020      INTO l_num
6021      FROM ap_invoice_lines_all
6022     WHERE po_header_id IS NOT NULL
6023       AND invoice_id = l_invoice_id
6024       and nvl(discarded_flag,'N')='N'  /*bug 14748974 */
6025       ;
6026 
6027    IF l_num > 0 THEN
6028       WF_ENGINE.SetItemAttrText('APINVAPR',
6029                                   l_itemkey,
6030                                   'INV_MATCH_TYPE',
6031                                   'MATCHED');
6032 
6033      --bug 14748974 changed to add approval status for po matched lines only
6034 
6035       UPDATE AP_INVOICE_LINES_ALL
6036       SET    WFAPPROVAL_STATUS = 'INITIATED'
6037       where  invoice_id = p_invoice_id
6038       and po_header_id IS NOT NULL
6039       and nvl(discarded_flag,'N')='N';
6040 
6041       UPDATE AP_INVOICE_LINES_ALL
6042       SET    WFAPPROVAL_STATUS = 'NOT REQUIRED'
6043       where  invoice_id = p_invoice_id
6044       and (po_header_id IS  NULL or
6045        (po_header_id IS  NOT NULL and nvl(discarded_flag,'N')='Y'));
6046     --bug 14748974 ends
6047 
6048    ELSE
6049       WF_ENGINE.SetItemAttrText('APINVAPR',
6050                                   l_itemkey,
6051                                   'INV_MATCH_TYPE',
6052                                   'UNMATCHED');
6053       UPDATE AP_INVOICE_LINES_ALL
6054       SET    WFAPPROVAL_STATUS = 'NOT REQUIRED'
6055       where  invoice_id = p_invoice_id;
6056    END IF;
6057 
6058    l_debug_info := 'Before Calling WF_ENGINE.startProcess(APINVAPR,'
6059                    || l_itemkey || ');';
6060    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6061           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
6062                          l_api_name,l_debug_info);
6063    END IF;
6064    WF_ENGINE.startProcess('APINVAPR', l_itemkey);
6065    l_debug_info := 'After Calling WF_ENGINE.startProcess' ;
6066    p_wfitemkey := l_itemkey;
6067    l_debug_info := 'End';
6068    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6069           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
6070                          l_api_name,l_debug_info);
6071    END IF;
6072 
6073 
6074 EXCEPTION
6075 
6076 WHEN OTHERS
6077    THEN
6078 /* Bug#14804508 - Added debug messages to print in report log */
6079    fnd_file.put_line(FND_FILE.LOG,'Error is :'||SQLERRM);
6080    fnd_file.put_line(FND_FILE.LOG,'l_debug_info : '||l_debug_info);
6081     IF (SQLCODE <> -20001) THEN
6082       FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
6083       FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
6084       FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE', l_calling_sequence);
6085       FND_MESSAGE.SET_TOKEN('DEBUG_INFO', l_debug_info);
6086     END IF;
6087     APP_EXCEPTION.RAISE_EXCEPTION;
6088 END create_invapp_process;
6089 
6090 FUNCTION Stop_Approval(
6091                         p_invoice_id IN NUMBER,
6092 			p_line_number IN NUMBER,
6093 			p_calling_sequence IN VARCHAR2) RETURN BOOLEAN IS
6094 
6095 	--Define cursor for wf and ame records that need to be stopped
6096 	--bug 13441717 added iteration as cursor variable in both the cursors to fetch
6097 	--the details of that particular iteration only
6098 
6099 		CURSOR   Item_Cur(c_iteration in number) IS
6100 		SELECT Item_Class, Item_Id, Role_Name, Invoice_Key, Notification_Key
6101 		FROM AP_APINV_APPROVERS
6102 		WHERE Invoice_ID = p_invoice_id
6103 		AND NOTIFICATION_STATUS = 'SENT'
6104 		AND invoice_iteration = c_iteration  --bug 13441717
6105 		GROUP BY Item_Class, Item_Id, Role_Name, Invoice_Key, Notification_Key
6106 		ORDER BY Notification_Key;
6107 
6108 	CURSOR   Line_Item_Cur(c_iteration in number) IS
6109         SELECT Item_Class, Item_Id, Role_Name, Invoice_Key, Notification_Key
6110         FROM AP_APINV_APPROVERS
6111         WHERE Invoice_ID = p_invoice_id
6112 	AND Line_Number = p_line_number
6113         AND NOTIFICATION_STATUS = 'SENT'
6114 	AND invoice_iteration = c_iteration  --bug 13441717
6115         GROUP BY Item_Class, Item_Id, Role_Name, Invoice_Key, Notification_Key
6116         ORDER BY Notification_Key;
6117 
6118 	l_api_name      CONSTANT VARCHAR2(200) := 'Stop_Approval';
6119         l_item_class    AP_APINV_APPROVERS.ITEM_CLASS%TYPE;
6120         l_item_id       AP_APINV_APPROVERS.ITEM_ID%TYPE;
6121 	l_invoice_id	NUMBER;
6122 	l_invoice_key   AP_APINV_APPROVERS.INVOICE_KEY%TYPE;
6123         l_not_key       AP_APINV_APPROVERS.NOTIFICATION_KEY%TYPE;
6124 	l_old_not_key   AP_APINV_APPROVERS.NOTIFICATION_KEY%TYPE;
6125 	l_name	        AP_APINV_APPROVERS.ROLE_NAME%TYPE;
6126         l_debug_info    VARCHAR2(2000);
6127         l_wf_exist      BOOLEAN;
6128         l_approval_iteration AP_INVOICES.approval_iteration%type;
6129         l_end_date      DATE;
6130         l_calling_sequence      VARCHAR2(2000);
6131 
6132 BEGIN
6133 
6134         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6135           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'AP_IAW_PKG.'|| l_api_name);
6136         END IF;
6137 
6138         l_calling_sequence := l_api_name || ' <-' || p_calling_sequence;
6139 
6140         /*Bug4926114  Added the following part to check of code
6141                       to check whether workflow is active or not */
6142         select approval_iteration
6143         into   l_approval_iteration
6144         from   ap_invoices
6145         where  invoice_id=p_invoice_id;
6146 
6147         l_invoice_key := p_invoice_id||'_'||l_approval_iteration;
6148 
6149         BEGIN
6150           SELECT  end_date
6151           INTO    l_end_date
6152           FROM    wf_items
6153           WHERE   item_type = 'APINVAPR'
6154           AND     item_key  = l_invoice_key;
6155 
6156           l_wf_exist  := TRUE;
6157        EXCEPTION
6158        WHEN NO_DATA_FOUND THEN
6159          l_wf_exist  := FALSE;
6160        END;
6161 
6162        If not (l_wf_exist) OR l_end_date is NOT NULL then
6163               return TRUE;
6164        end if;
6165 
6166 	IF p_line_number IS NULL THEN
6167 	   --End WF processes
6168 	   WF_Engine.abortProcess(
6169 		itemType => 'APINVAPR',
6170 		itemKey  => l_invoice_key,
6171 		process => 'APPROVAL_MAIN');
6172 
6173 	   l_debug_info := 'opening item cursor';
6174            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6175               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
6176                                         l_debug_info);
6177            END IF;
6178 
6179            OPEN Item_Cur(l_approval_iteration);
6180            LOOP
6181 
6182                 FETCH Item_Cur INTO l_item_class, l_item_id, l_name,
6183 					l_invoice_key, l_not_key;
6184                 EXIT WHEN Item_Cur%NOTFOUND;
6185 
6186 		-- Bug 7710828. Now passing p_invoice_id instead of l_invoice_id.
6187 		AME_API2.updateApprovalStatus2(applicationIdIn => 200,
6188                                 transactionIdIn     => to_char(p_invoice_id),
6189                                 approvalStatusIn    => AME_UTIL.nullStatus,
6190                                 approverNameIn  => l_name,
6191                                 transactionTypeIn =>  'APINV',
6192                                 itemClassIn     => l_item_class,
6193                                 itemIdIn        => l_item_id);
6194 
6195 		IF l_not_key <> nvl(l_old_not_key, 'dummy') THEN
6196 
6197 			WF_Engine.abortProcess(
6198 			itemType => 'APINVAPR',
6199 			itemKey  => l_not_key,
6200 			process => 'APPROVAL_INVOICE_LINES');
6201 
6202 			l_old_not_key := l_not_key;
6203 		END IF;
6204 
6205            END LOOP;
6206            CLOSE Item_Cur;
6207 
6208 	ELSE --just a line
6209 
6210            l_debug_info := 'opening line item cursor';
6211            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6212               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
6213                                         l_debug_info);
6214            END IF;
6215 
6216            OPEN Line_Item_Cur(l_approval_iteration);
6217            LOOP
6218 
6219                 FETCH Line_Item_Cur INTO l_item_class, l_item_id, l_name,
6220                                         l_invoice_key, l_not_key;
6221                 EXIT WHEN Line_Item_Cur%NOTFOUND;
6222 
6223                 -- Bug 7710828. Now passing p_invoice_id instead of l_invoice_id.
6224                 AME_API2.updateApprovalStatus2(applicationIdIn => 200,
6225                                 transactionIdIn     => to_char(p_invoice_id),
6226                                 approvalStatusIn    => AME_UTIL.nullStatus,
6227                                 approverNameIn  => l_name,
6228                                 transactionTypeIn =>  'APINV',
6229                                 itemClassIn     => l_item_class,
6230                                 itemIdIn        => l_item_id);
6231 
6232                 IF l_not_key <> nvl(l_old_not_key, 'dummy') THEN
6233 
6234                         WF_Engine.abortProcess(
6235                         itemType => 'APINVAPR',
6236                         itemKey  => l_not_key,
6237                         process => 'APPROVAL_INVOICE_LINES');
6238 
6239                         l_old_not_key := l_not_key;
6240                 END IF;
6241 
6242            END LOOP;
6243            CLOSE Line_Item_Cur;
6244 	END IF; --just a line
6245 
6246 	return true;
6247 
6248 EXCEPTION
6249   WHEN OTHERS THEN
6250 
6251     IF (SQLCODE <> -20001) THEN
6252       FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
6253       FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
6254       FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE', l_calling_sequence);
6255       FND_MESSAGE.SET_TOKEN('DEBUG_INFO', l_debug_info);
6256     END IF;
6257     APP_EXCEPTION.RAISE_EXCEPTION;
6258 
6259 END Stop_Approval;
6260 PROCEDURE process_single_line_response(p_invoice_id IN NUMBER,
6261                                        p_line_number IN NUMBER,
6262                                        p_response IN VARCHAR2,
6263                                        p_itemkey  IN VARCHAR2,
6264                                        p_comments IN VARCHAR2) IS
6265    --Define cursor for lines affected by notification
6266    --Note that Invoice_Key s/b the same for all records in the cursor
6267    --but I want to avoid another select on the table
6268    CURSOR   Items_Cur IS
6269    SELECT invap.Item_Class, invap.Item_Id, invap.Role_Name,
6270           invap.Invoice_Key, al.line_number, al.amount
6271    FROM   AP_APINV_APPROVERS invap, AP_INVOICE_LINES_ALL al
6272    WHERE  invap.child_process_item_key = p_itemkey
6273    AND    invap.line_number = p_line_number
6274    AND    invap.invoice_id = p_invoice_id
6275    AND    al.line_number = invap.line_number
6276    AND    al.invoice_id  = invap.invoice_id;
6277 
6278    l_api_name      CONSTANT VARCHAR2(200) := 'process_single_line_response';
6279    l_debug_info    VARCHAR2(2000);
6280    l_parentkey     VARCHAR2(150);
6281    l_invoice_id    NUMBER;
6282    l_level         VARCHAR2(20);
6283    l_result        VARCHAR2(20);
6284    l_invoice_key   VARCHAR2(50);
6285    l_name          AP_APINV_APPROVERS.ROLE_NAME%TYPE;
6286    l_item_class    AP_APINV_APPROVERS.ITEM_CLASS%TYPE;
6287    l_item_id       AP_APINV_APPROVERS.ITEM_ID%TYPE;
6288    l_user_id       NUMBER(15);
6289    l_login_id      NUMBER(15);
6290    l_hist_rec      AP_INV_APRVL_HIST%ROWTYPE;
6291    l_approver_name VARCHAR2(150);
6292    l_approver_id   NUMBER;
6293    l_esc_flag      VARCHAR2(1);
6294    l_esc_approver_name VARCHAR2(150);
6295    l_role_name     VARCHAR2(150);
6296    l_role VARCHAR2(150);
6297    l_esc_role_name VARCHAR2(150);
6298    l_esc_approver_id   NUMBER;
6299    l_line_total     NUMBER;
6300    l_iteration     NUMBER(15);
6301    l_comments      VARCHAR2(240);
6302    l_org_id        NUMBER(15);
6303    l_line_number   ap_invoice_lines_all.line_number%TYPE;
6304    l_line_amount   ap_invoice_lines_all.amount%TYPE;
6305    l_notf_iteration        NUMBER;
6306    l_response      ap_inv_aprvl_hist_all.response%TYPE;
6307    l_sent           NUMBER;
6308 BEGIN
6309    l_debug_info := 'Start';
6310    l_sent := 0;
6311    IF p_response = 'APPROVE'
6312    THEN l_response := 'APPROVED';
6313    ELSIF p_response = 'REJECT'
6314    THEN l_response := 'REJECTED';
6315    ELSE l_response := p_response;
6316    END IF;
6317 
6318 
6319    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6320           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
6321                          l_api_name,l_debug_info);
6322    END IF;
6323 
6324    l_invoice_id := WF_ENGINE.GetItemAttrNumber('APINVAPR',
6325                                   p_itemkey,
6326                                   'INVOICE_ID');
6327 
6328    l_iteration := WF_ENGINE.GetItemAttrNumber('APINVAPR',
6329                              p_itemkey,
6330                              'ITERATION');
6331 
6332    l_notf_iteration := WF_ENGINE.GETITEMATTRNumber('APINVAPR',
6333                         p_itemkey,
6334                         'NOTF_ITERATION');
6335 
6336    l_comments := WF_ENGINE.GetItemAttrText('APINVAPR',
6337                              p_itemkey,
6338                              'WF_NOTE');
6339 
6340    l_org_id := WF_ENGINE.GETITEMATTRNumber('APINVAPR',
6341                    p_itemkey,
6342                    'ORG_ID');
6343 
6344    l_approver_name := WF_ENGINE.GetItemAttrText('APINVAPR',
6345                              p_itemkey,
6346                              'APPROVER_NAME');
6347 
6348    l_esc_role_name := WF_ENGINE.GetItemAttrText('APINVAPR',
6349                              p_itemkey,
6350                              'ESC_ROLE_NAME');
6351 
6352    l_role_name := WF_ENGINE.GetItemAttrText('APINVAPR',
6353                              p_itemkey,
6354                              'ROLE_NAME');
6355 
6356    l_approver_id := WF_ENGINE.GETITEMATTRNumber('APINVAPR',
6357                    p_itemkey,
6358                    'APPROVER_ID');
6359 
6360    l_esc_flag  := WF_ENGINE.GetItemAttrText('APINVAPR',
6361                              p_itemkey,
6362                              'ESCALATED');
6363    l_esc_approver_name := WF_ENGINE.GetItemAttrText('APINVAPR',
6364                              p_itemkey,
6365                              'ESC_APPROVER_NAME');
6366 
6367    l_esc_approver_id := WF_ENGINE.GETITEMATTRNumber('APINVAPR',
6368                    p_itemkey,
6369                    'ESC_APPROVER_ID');
6370 
6371    l_user_id := nvl(to_number(fnd_profile.value('USER_ID')),-1);
6372    l_login_id := nvl(to_number(fnd_profile.value('LOGIN_ID')),-1);
6373    IF nvl(l_esc_flag,'N') = 'N' THEN
6374       l_role := l_role_name;
6375    ELSE
6376       l_role := l_esc_role_name;
6377    END IF;
6378 
6379    l_debug_info := 'Before Update Approvers table';
6380    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6381           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
6382                          l_api_name,l_debug_info);
6383    END IF;
6384    --Update Approvers table
6385    IF l_response in ('APPROVED','REJECTED','ACCEPT') THEN
6386       UPDATE AP_APINV_APPROVERS
6387       SET Notification_status = 'COMP'
6388       WHERE CHILD_PROCESS_ITEM_KEY = p_itemkey
6389       AND   INVOICE_ID = p_invoice_id
6390       AND   LINE_NUMBER = p_line_number;
6391    END IF;
6392 
6393    fnd_client_info.set_org_context(l_org_id);
6394 
6395    l_hist_rec.HISTORY_TYPE := 'LINESAPPROVAL';
6396    l_hist_rec.INVOICE_ID   := l_invoice_id;
6397    l_hist_rec.ITERATION    := l_iteration;
6398    l_hist_rec.NOTIFICATION_ORDER := l_notf_iteration;
6399    l_hist_rec.APPROVER_COMMENTS := l_comments;
6400    l_hist_rec.RESPONSE     := l_response;
6401    IF nvl(l_esc_flag,'N') = 'N' THEN
6402       l_hist_rec.APPROVER_ID  := l_approver_id;
6403       l_hist_rec.APPROVER_NAME:= l_approver_name;
6404    ELSE
6405       l_hist_rec.APPROVER_ID  := l_esc_approver_id;
6406       l_hist_rec.APPROVER_NAME:= l_esc_approver_name;
6407    END IF;
6408    l_hist_rec.CREATED_BY   := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
6409    l_hist_rec.CREATION_DATE:= sysdate;
6410    l_hist_rec.LAST_UPDATE_DATE := sysdate;
6411    l_hist_rec.LAST_UPDATED_BY  := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
6412    l_hist_rec.LAST_UPDATE_LOGIN := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),-1);
6413    l_hist_rec.ORG_ID            := l_org_id;
6414    --update AME status
6415    --For Future  check with ame as to when updateApprovalStatuses will be
6416    --available so there  will not be a need to loop.
6417    OPEN Items_Cur;
6418 
6419    FETCH Items_Cur INTO l_item_class, l_item_id, l_name,
6420                                 l_invoice_key,l_line_number, l_line_amount;
6421    --update AME with response
6422    IF l_response IN ('APPROVED','ACCEPT') THEN
6423       AME_API2.updateApprovalStatus2(applicationIdIn => 200,
6424                            transactionIdIn     => to_char(l_invoice_id),
6425                            approvalStatusIn    => AME_UTIL.approvedStatus,
6426                            approverNameIn  => l_name,
6427                            transactionTypeIn =>  'APINV',
6428                            itemClassIn     => l_item_class,
6429                            itemIdIn        => l_item_id);
6430    ELSIF l_response = 'REJECTED' THEN
6431       AME_API2.updateApprovalStatus2(applicationIdIn => 200,
6432                            transactionIdIn     => to_char(l_invoice_id),
6433                            approvalStatusIn    => AME_UTIL.rejectStatus,
6434                            approverNameIn  => l_name,
6435                            transactionTypeIn =>  'APINV',
6436                            itemClassIn     => l_item_class,
6437                            itemIdIn        => l_item_id);
6438    END IF;
6439    l_hist_rec.line_number   := l_line_number;
6440    l_hist_rec.AMOUNT_APPROVED   := l_line_amount;
6441 
6442    l_debug_info := 'Before calling insert_history_table for Line'
6443                     || l_line_number;
6444    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6445           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
6446                          l_api_name,l_debug_info);
6447    END IF;
6448 
6449    insert_history_table(p_hist_rec => l_hist_rec);
6450    CLOSE Items_Cur;
6451 
6452 
6453    l_user_id := nvl(to_number(fnd_profile.value('USER_ID')),-1);
6454    l_login_id := nvl(to_number(fnd_profile.value('LOGIN_ID')),-1);
6455 
6456    IF l_response = 'REJECTED' THEN
6457       UPDATE AP_INVOICE_LINES_ALL
6458       SET    wfapproval_status = 'REJECTED'
6459              ,Last_Update_Date = sysdate
6460              ,Last_Updated_By = l_user_id
6461              ,Last_Update_Login = l_login_id
6462       WHERE invoice_id = p_invoice_id
6463       AND wfapproval_status <> 'MANUALLY APPROVED'
6464       AND line_number = p_line_number;
6465    END IF;
6466 
6467    BEGIN
6468 
6469       SELECT invoice_key
6470       INTO   l_invoice_key
6471       FROM   AP_APINV_APPROVERS
6472       WHERE  invoice_id = p_invoice_id
6473       AND    line_number = p_line_number
6474       AND    child_process_item_key = p_itemkey
6475       AND    rownum = 1;
6476    EXCEPTION
6477       WHEN NO_DATA_FOUND THEN
6478          l_debug_info := 'No Data Found in SELECT from AP_APINV_APPROVERS' ;
6479       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6480              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
6481                             l_api_name,l_debug_info);
6482       END IF;
6483       RAISE;
6484 
6485    END;
6486 
6487    /* The following is being called from OA layer after
6488    Initial Commit. so commenting it out here.
6489 
6490    SELECT count(*)
6491    INTO  l_sent
6492    FROM  AP_APINV_APPROVERS
6493    WHERE Notification_status = 'SENT'
6494    AND   child_process_item_key = p_itemkey
6495    AND   INVOICE_ID = p_invoice_id
6496    AND   LINE_NUMBER = p_line_number;
6497    IF l_sent = 0 THEN
6498       BEGIN
6499          wf_engine.CompleteActivity(
6500                         itemType => 'APINVAPR',
6501                         itemKey  => p_itemkey,
6502                         activity => 'APPROVAL_INVOICE_LINES:WAITLINEAPRVL',
6503                         result   => 'NULL');
6504 
6505       EXCEPTION
6506          WHEN OTHERS THEN NULL;
6507       END;
6508    END IF;
6509    */
6510    l_debug_info := 'End';
6511    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6512           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
6513                          l_api_name,l_debug_info);
6514    END IF;
6515 END process_single_line_response;
6516 
6517 /*get_attribute_value is called by AME when determining the value for more
6518 complicated attributes.  It can be called at the header or line level, and
6519 the p_attribute_name is used to determine what the return value should be.
6520 p_context is currently a miscellaneous parameter to be used as necessary in
6521 the future.  The goal with this function is to avoid adding a new function
6522 for each new AME attribute.*/
6523 
6524 FUNCTION Get_Attribute_Value(p_invoice_id IN NUMBER,
6525                    p_sub_class_id IN NUMBER DEFAULT NULL,
6526                    p_attribute_name IN VARCHAR2,
6527                    p_context IN VARCHAR2 DEFAULT NULL)
6528                                  RETURN VARCHAR2 IS
6529 
6530         l_debug_info    VARCHAR2(2000);
6531         l_return_val    VARCHAR2(2000);
6532         l_count_pa_rel  NUMBER;
6533         l_sum_matched   NUMBER;
6534         l_sum_calc      NUMBER;
6535         l_line_count    NUMBER;
6536         l_item_count    NUMBER;
6537         l_api_name      CONSTANT VARCHAR2(200) := 'Get_Attribute_Value';
6538 
6539 BEGIN
6540 
6541         IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
6542           FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_IAW_PKG.'|| l_api_name);
6543         END IF;
6544 
6545         IF p_context = 'header' THEN
6546                 --dealing with a header level attribute
6547                 IF p_attribute_name =
6548                         'SUPPLIER_INVOICE_EXPENDITURE_ORGANIZATION_NAME' THEN
6549 
6550                   SELECT organization
6551                     INTO l_return_val
6552                     FROM PA_EXP_ORGS_IT
6553                    WHERE organization_id=(SELECT expenditure_organization_id
6554                                        FROM ap_invoices_all
6555                                        WHERE invoice_id = p_invoice_id);
6556 
6557                 ELSIF p_attribute_name= 'SUPPLIER_INVOICE_PROJECT_RELATED' THEN
6558 
6559                         SELECT count(invoice_distribution_id)
6560                         INTO l_count_pa_rel
6561                         FROM ap_invoice_distributions_all
6562                         WHERE invoice_id = p_invoice_id
6563                         AND project_id is not null;
6564 
6565                         IF l_count_pa_rel >0 THEN
6566                                 l_return_val := 'Y';
6567                         ELSE
6568                                 l_return_val := 'N';
6569                         END IF;
6570 
6571                 ELSIF p_attribute_name= 'SUPPLIER_INVOICE_MATCHED' THEN
6572                         --an invoice is considered matched if all item
6573                         --lines are matched
6574 
6575                         SELECT sum(decode(po_header_id, null, 0, 1)),
6576                                         count(line_number)
6577                         INTO l_sum_matched, l_item_count
6578                         FROM ap_invoice_lines_all
6579                         WHERE invoice_id = p_invoice_id
6580                         AND line_type_lookup_code = 'ITEM'
6581                         and nvl(discarded_flag,'N')='N'; --bug 12846778
6582 
6583                         IF l_sum_matched >0
6584                                 and l_sum_matched = l_item_count THEN
6585                                 l_return_val := 'Y';
6586                         ELSE
6587                                 l_return_val := 'N';
6588                         END IF;
6589                 ELSIF  p_attribute_name= 'SUPPLIER_INVOICE_TAX_CALCULATED' THEN
6590 
6591                         SELECT sum(decode(tax_already_calculated_flag, 'Y',
6592                                         1, 0)), count(line_number)
6593                         INTO l_sum_calc, l_line_count
6594                         FROM ap_invoice_lines_all
6595                         WHERE invoice_id = p_invoice_id
6596                         AND line_type_lookup_code not in ('TAX','AWT');
6597 
6598                         IF l_sum_calc >0 and l_sum_matched = l_line_count THEN
6599                                 l_return_val := 'Y';
6600                         ELSE
6601                                 l_return_val := 'N';
6602                         END IF;
6603 
6604                 END IF;
6605 
6606         ELSIF p_context = 'distribution' THEN
6607                 IF p_attribute_name =
6608                         'SUPPLIER_INVOICE_DISTRIBUTION_PO_BUYER_EMP_NUM' THEN
6609 
6610                    SELECT employee_number
6611                      INTO l_return_val
6612                      FROM per_all_people_f pap
6613                     WHERE person_id = (SELECT ph.agent_id
6614                            FROM ap_invoice_distributions_all aid,
6615                                 po_distributions_all pd,
6616                                 po_headers_all ph
6617                            WHERE pd.po_distribution_id =
6618                                 aid.po_distribution_id
6619                            AND  pd.po_header_id = ph.po_header_id
6620                            AND aid.invoice_distribution_id =
6621                                                 p_sub_class_id
6622                            AND pd.creation_date >= pap.effective_start_date
6623                            AND pd.creation_date <=
6624                                       nvl(pap.effective_end_date,sysdate));
6625 
6626                 ELSIF p_attribute_name =
6627                   'SUPPLIER_INVOICE_DISTRIBUTION_PO_REQUESTER_EMP_NUM' THEN
6628 
6629                   SELECT employee_number
6630                     INTO l_return_val
6631                     FROM per_all_people_f pap
6632                    WHERE person_id = (
6633                         SELECT pd.deliver_to_person_id
6634                           FROM ap_invoice_distributions_all aid,
6635                                po_distributions_all pd
6636                          WHERE pd.po_distribution_id =
6637                                aid.po_distribution_id
6638                            AND aid.invoice_distribution_id =
6639                                       p_sub_class_id
6640                            AND pd.creation_date >= pap.effective_start_date
6641                            AND pd.creation_date <=
6642                                       nvl(pap.effective_end_date,sysdate));
6643                 END IF;
6644         ELSIF p_context = 'line item' THEN
6645 
6646                 IF p_attribute_name = 'SUPPLIER_INVOICE_LINE_MATCHED' THEN
6647                         SELECT decode(po_header_id, null, 'N', 'Y')
6648                         INTO l_return_val
6649                         FROM ap_invoice_lines_all
6650                         WHERE invoice_id = p_invoice_id
6651                         AND line_number = p_sub_class_id;
6652 
6653                 END IF;
6654         END IF;
6655 
6656         return l_return_val;
6657 
6658 EXCEPTION
6659   WHEN OTHERS THEN
6660     Wf_Core.Context('APINVLDP', 'get_attribute_value',
6661                     p_invoice_id , p_sub_class_id, p_attribute_name,
6662                                 l_debug_info);
6663     raise;
6664 
6665 END Get_Attribute_Value;
6666 
6667 /* This function is called from AME in order to provide the relevant segment
6668 of the account fexfield to the calling AME attribute usage*/
6669 FUNCTION AP_Dist_Accounting_Flex(p_seg_name IN VARCHAR2,
6670                                  p_dist_id IN NUMBER) RETURN VARCHAR2 IS
6671 
6672         l_segments                      FND_FLEX_EXT.SEGMENTARRAY;
6673         l_result                        BOOLEAN;
6674         l_chart_of_accounts_id          NUMBER;
6675         l_num_segments                  NUMBER;
6676         l_segment_num                   NUMBER;
6677         l_reason_flex                   VARCHAR2(2000):='';
6678         l_segment_delimiter             VARCHAR2(1);
6679         l_seg_val                       VARCHAR2(50);
6680         l_ccid                          NUMBER;
6681         l_sob                           NUMBER;
6682         l_debug_info                    VARCHAR2(2000);
6683         l_api_name      CONSTANT VARCHAR2(200) := 'AP_Dist_Accounting_Flex';
6684 
6685 BEGIN
6686         IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
6687           FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_IAW_PKG.'|| l_api_name);
6688         END IF;
6689 
6690         SELECT dist_code_combination_id,set_of_books_id
6691         INTO l_ccid,l_sob
6692         FROM ap_invoice_distributions_all
6693         WHERE invoice_distribution_id=p_dist_id;
6694 
6695         SELECT chart_of_accounts_id
6696         INTO l_chart_of_accounts_id
6697         FROM gl_sets_of_books
6698        WHERE set_of_books_id = l_sob;
6699 
6700         l_segment_delimiter := FND_FLEX_EXT.GET_DELIMITER(
6701                                                 'SQLGL',
6702                                                 'GL#',
6703                                                 l_chart_of_accounts_id);
6704         IF (NOT l_result) THEN
6705             l_reason_flex := FND_MESSAGE.GET;
6706         END IF;
6707 
6708         l_result := FND_FLEX_EXT.GET_SEGMENTS(
6709                                       'SQLGL',
6710                                       'GL#',
6711                                       l_chart_of_accounts_id,
6712                                       l_ccid,
6713                                       l_num_segments,
6714                                       l_segments);
6715 
6716         IF (NOT l_result) THEN
6717             l_reason_flex := FND_MESSAGE.GET;
6718         END IF;
6719 
6720         l_result := FND_FLEX_APIS.GET_QUALIFIER_SEGNUM(
6721                                     101,
6722                                     'GL#',
6723                                     l_chart_of_accounts_id,
6724                                     p_seg_name,
6725                                     l_segment_num);
6726         IF (NOT l_result) THEN
6727             l_reason_flex := FND_MESSAGE.GET;
6728         END IF;
6729 
6730         l_seg_val := l_segments(l_segment_num);
6731 
6732         return l_seg_val;
6733 
6734 EXCEPTION
6735   WHEN OTHERS THEN
6736     Wf_Core.Context('APINVLDP', 'p_dist_accounting_flex',
6737              p_seg_name , p_dist_id, l_debug_info);
6738     raise;
6739 END AP_Dist_Accounting_Flex;
6740 
6741 PROCEDURE continue_hold_workflow(itemtype IN VARCHAR2,
6742                         itemkey IN VARCHAR2,
6743                         actid   IN NUMBER,
6744                         funcmode IN VARCHAR2,
6745                         resultout  OUT NOCOPY VARCHAR2 ) IS
6746 
6747    l_org_id NUMBER;
6748    l_invoice_id NUMBER;
6749    l_hold_id NUMBER;
6750    l_num number;
6751    l_hold_release_code     ap_holds_all.release_lookup_code%TYPE;
6752    l_debug_info            VARCHAR2(2000);
6753    l_api_name              CONSTANT VARCHAR2(200) := 'continue_hold_workflow';
6754 BEGIN
6755    l_org_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
6756                         itemkey,
6757                         'ORG_ID');
6758 
6759    l_invoice_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
6760                         itemkey,
6761                         'INVOICE_ID');
6762 
6763    l_hold_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
6764                         itemkey,
6765                         'HOLD_ID');
6766    l_debug_info := 'Before select';
6767    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6768           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
6769                          l_api_name,l_debug_info);
6770    END IF;
6771    select aha.release_lookup_code
6772    into   l_hold_release_code
6773    from   ap_holds_all aha,
6774           ap_hold_codes ahc
6775    where  aha.invoice_id = l_invoice_id
6776    and    aha.org_id = l_org_id
6777    and    aha.hold_lookup_code = ahc.hold_lookup_code
6778    and    aha.hold_id = l_hold_id;
6779    IF l_hold_release_code IS NULL  THEN
6780       resultout := wf_engine.eng_completed||':'||'Y';
6781    ELSE
6782       resultout := wf_engine.eng_completed||':'||'N';
6783    END IF;
6784    l_debug_info := 'After select, reultout : ' || resultout;
6785    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6786           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
6787                          l_api_name,l_debug_info);
6788    END IF;
6789 EXCEPTION
6790 
6791 WHEN OTHERS
6792    THEN
6793         WF_CORE.CONTEXT('APINVHDN','continue_hold_workflow',itemtype, itemkey,
6794                         to_char(actid), funcmode);
6795         RAISE;
6796 END continue_hold_workflow;
6797 
6798 PROCEDURE exists_initial_wait(itemtype IN VARCHAR2,
6799                         itemkey IN VARCHAR2,
6800                         actid   IN NUMBER,
6801                         funcmode IN VARCHAR2,
6802                         resultout  OUT NOCOPY VARCHAR2 ) IS
6803 
6804    l_org_id NUMBER;
6805    l_invoice_id NUMBER;
6806    l_hold_id NUMBER;
6807    l_num number;
6808    l_wait_time             NUMBER;
6809    l_debug_info            VARCHAR2(2000);
6810    l_api_name              CONSTANT VARCHAR2(200) := 'exists_initial_wait';
6811 BEGIN
6812    l_org_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
6813                         itemkey,
6814                         'ORG_ID');
6815 
6816    l_invoice_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
6817                         itemkey,
6818                         'INVOICE_ID');
6819 
6820    l_hold_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
6821                         itemkey,
6822                         'HOLD_ID');
6823    l_debug_info := 'Before select';
6824    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6825           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
6826                          l_api_name,l_debug_info);
6827    END IF;
6828    select nvl(ahc.wait_before_notify_days,0) -- Bug 10090981   *24*60
6829    into   l_wait_time
6830    from   ap_holds_all aha,
6831           ap_hold_codes ahc
6832    where  aha.invoice_id = l_invoice_id
6833    and    aha.org_id = l_org_id
6834    and    aha.hold_lookup_code = ahc.hold_lookup_code
6835    and    aha.hold_id = l_hold_id;
6836    IF l_wait_time  > 0 THEN
6837       WF_ENGINE.SetItemAttrNumber(itemtype,
6838                         itemkey,
6839                         'INITIAL_WAIT_TIME',
6840                         l_wait_time);
6841 
6842       resultout := wf_engine.eng_completed||':'||'Y';
6843    ELSE
6844       resultout := wf_engine.eng_completed||':'||'N';
6845    END IF;
6846    l_debug_info := 'After select, reultout : ' || resultout;
6847    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6848           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
6849                          l_api_name,l_debug_info);
6850    END IF;
6851 EXCEPTION
6852 
6853 WHEN OTHERS
6854    THEN
6855         WF_CORE.CONTEXT('APINVHDN','exists_initial_wait',itemtype, itemkey,
6856                         to_char(actid), funcmode);
6857         RAISE;
6858 END exists_initial_wait;
6859 
6860 PROCEDURE is_hold_released(itemtype IN VARCHAR2,
6861                         itemkey IN VARCHAR2,
6862                         actid   IN NUMBER,
6863                         funcmode IN VARCHAR2,
6864                         resultout  OUT NOCOPY VARCHAR2 ) IS
6865 
6866    l_org_id NUMBER;
6867    l_invoice_id NUMBER;
6868    l_hold_id NUMBER;
6869    l_num number;
6870    l_hold_release_code     ap_holds_all.release_lookup_code%TYPE;
6871    l_wait_time             NUMBER;
6872    l_debug_info            VARCHAR2(2000);
6873    l_api_name              CONSTANT VARCHAR2(200) := 'is_hold_released';
6874 BEGIN
6875    l_org_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
6876                         itemkey,
6877                         'ORG_ID');
6878 
6879    l_invoice_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
6880                         itemkey,
6881                         'INVOICE_ID');
6882 
6883    l_hold_id := WF_ENGINE.GETITEMATTRNumber(itemtype,
6884                         itemkey,
6885                         'HOLD_ID');
6886    l_debug_info := 'Before select';
6887    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6888           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
6889                          l_api_name,l_debug_info);
6890    END IF;
6891    select aha.release_lookup_code,
6892           nvl(ahc.reminder_days,0)    -- Bug 10090981 *24*60 --Bug8839774
6893    into   l_hold_release_code,
6894           l_wait_time
6895    from   ap_holds_all aha,
6896           ap_hold_codes ahc
6897    where  aha.invoice_id = l_invoice_id
6898    and    aha.org_id = l_org_id
6899    and    aha.hold_lookup_code = ahc.hold_lookup_code
6900    and    aha.hold_id = l_hold_id;
6901    IF l_hold_release_code IS NOT NULL
6902    OR l_wait_time = 0 THEN
6903       resultout := wf_engine.eng_completed||':'||'Y';
6904    ELSE
6905       WF_ENGINE.SetItemAttrNumber(itemtype,
6906                         itemkey,
6907                         'REMINDER_WAIT_TIME',
6908                         l_wait_time);
6909       resultout := wf_engine.eng_completed||':'||'N';
6910    END IF;
6911    l_debug_info := 'After select, reultout : ' || resultout;
6912    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6913           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
6914                          l_api_name,l_debug_info);
6915    END IF;
6916 EXCEPTION
6917 
6918 WHEN OTHERS
6919    THEN
6920         WF_CORE.CONTEXT('APINVHDN','is_hold_released',itemtype, itemkey,
6921                         to_char(actid), funcmode);
6922         RAISE;
6923 END is_hold_released;
6924 
6925 PROCEDURE abort_holds_workflow(p_hold_id IN NUMBER) IS
6926    l_api_name      CONSTANT VARCHAR2(200) := 'abort_holds_workflow';
6927    l_debug_info    VARCHAR2(2000);
6928    l_status        VARCHAR2(15) ; -- Bug 8266290
6929    l_result        VARCHAR2(40) ; -- Bug 8266290
6930    l_success       BOOLEAN      ; -- Bug 9402921
6931 BEGIN
6932    l_debug_info := 'Start';
6933    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6934           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
6935                          l_api_name,l_debug_info);
6936    END IF;
6937 
6938    BEGIN
6939       -- Bug 8266290 : Added the below WF_Engine call and if condition
6940       WF_Engine.ItemStatus ( itemType => 'APINVHDN',
6941 	  		     itemKey  => to_char(p_hold_id),
6942             		     status   => l_status,
6943                              result   => l_result ) ;
6944       -- Bug 9402921 : Added l_success logic
6945       l_success := TRUE ;
6946 
6947       l_debug_info := 'WF_Engine call successful';
6948       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6949              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
6950                             l_api_name,l_debug_info);
6951       END IF;
6952    EXCEPTION
6953       WHEN OTHERS THEN
6954         l_success := FALSE ;
6955         l_debug_info := 'WF_Engine call failed' ;
6956         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6957                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
6958                                l_api_name,l_debug_info);
6959         END IF;
6960    END ;
6961 
6962    If ( NVL( l_status, 'N' ) <> 'COMPLETE' ) THEN --  Bug 9402921  : Added NVL
6963 	   UPDATE ap_holds_all
6964 	   SET    wf_status = 'MANUALLYRELEASED'
6965 	   WHERE  hold_id = p_hold_id;
6966 
6967        IF l_success THEN -- Bug 9402921 : Added if-end if
6968 	   -- Bug 7693776 begin
6969 	   WF_Engine.abortProcess( itemType => 'APINVHDN',
6970       				   itemKey  => to_char(p_hold_id) );
6971 
6972 	   AME_API2.clearAllApprovals( applicationIdIn   => 200,
6973         	                       transactionIdIn   => to_char(p_hold_id),
6974                 	               transactionTypeIn => 'APHLD' );
6975 	   -- Bug 7693776 end
6976        END IF ;
6977    End If;
6978 
6979    l_debug_info := 'End';
6980    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6981           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
6982                          l_api_name,l_debug_info);
6983    END IF;
6984 END;
6985 
6986 FUNCTION IS_INV_NEGOTIATED(
6987                  p_invoice_id IN ap_invoice_lines_all.invoice_id%TYPE
6988 		,p_org_id IN ap_invoice_lines_all.org_id%TYPE)
6989 		RETURN BOOLEAN IS
6990    l_num_lines_under_neg NUMBER;
6991    l_num_holds_under_neg NUMBER;
6992    l_debug_info       VARCHAR2(2000);
6993    l_api_name         CONSTANT VARCHAR2(200) := 'IS_INV_NEGOTIATED';
6994 
6995 BEGIN
6996    l_num_holds_under_neg := 0;
6997    l_num_lines_under_neg := 0;
6998 
6999    l_debug_info := 'Before select for l_num_holds_under_neg';
7000    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7001           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7002                          l_api_name,l_debug_info);
7003    END IF;
7004 
7005    SELECT count(*)
7006    INTO   l_num_holds_under_neg
7007    FROM   ap_holds_all aha
7008    WHERE  aha.invoice_id = p_invoice_id
7009    AND    aha.org_id = p_org_id
7010    AND    aha.wf_status = 'NEGOTIATE';
7011 
7012    l_debug_info := 'After select for l_num_holds_under_neg, l_num_holds_under_neg = ' || l_num_holds_under_neg;
7013    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7014           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7015                          l_api_name,l_debug_info);
7016    END IF;
7017    IF l_num_holds_under_neg > 0 THEN
7018       RETURN(TRUE);
7019    END IF;
7020 
7021    l_debug_info := 'Before select for l_num_lines_under_neg';
7022    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7023           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7024                          l_api_name,l_debug_info);
7025    END IF;
7026 
7027    SELECT COUNT(*)
7028    INTO   l_num_lines_under_neg
7029    FROM   ap_invoice_lines_all ail, ap_apinv_approvers aaa
7030    WHERE  ail.invoice_id = p_invoice_id
7031    AND    ail.org_id = p_org_id
7032    AND    aaa.invoice_id = ail.invoice_id
7033    AND    aaa.line_number = ail.line_number
7034    AND    aaa.approval_status = 'NEGOTIATE';
7035 
7036    l_debug_info := 'After select for l_num_lines_under_neg, l_num_lines_under_neg = ' || l_num_lines_under_neg;
7037    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7038           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7039                          l_api_name,l_debug_info);
7040    END IF;
7041    IF l_num_lines_under_neg > 0 THEN
7042       RETURN(TRUE);
7043    END IF;
7044    RETURN(FALSE);
7045 EXCEPTION
7046    WHEN OTHERS THEN
7047       APP_EXCEPTION.RAISE_EXCEPTION;
7048 END IS_INV_NEGOTIATED;
7049 
7050 /* Bug 5590138. Bring to from old apiawleb.pls */
7051 
7052 /*This procedure gets a table of approvers and their associated items, for
7053 the application history forms.*/
7054 
7055 PROCEDURE Get_All_Approvers(p_invoice_id IN NUMBER,
7056                         p_calling_sequence IN VARCHAR2) IS
7057 
7058         l_invoice_id            NUMBER;
7059         l_complete              VARCHAR2(1);
7060         l_next_approvers        ame_util.approversTable2;
7061         l_next_approver         ame_util.approverRecord2;
7062         l_index                 ame_util.idList;
7063         l_ids                   ame_util.stringList;
7064         l_class                 ame_util.stringList;
7065         l_source                ame_util.longStringList;
7066         l_line_num              NUMBER;
7067         l_api_name              CONSTANT VARCHAR2(200) := 'Get_All_Approvers';
7068         l_iteration             NUMBER;
7069         l_debug_info            VARCHAR2(2000);
7070         l_org_id                NUMBER;
7071         l_calling_sequence      VARCHAR2(2000);
7072         l_wfapproval_status     varchar2(30);
7073 
7074 BEGIN
7075 
7076         IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
7077           FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_IAW_PKG.'|| l_api_name);
7078         END IF;
7079 
7080         l_calling_sequence := 'AP_WORLFLOW_PKG.'||l_api_name || ' <-' || p_calling_sequence;
7081 
7082         l_debug_info := 'set variables from workflow';
7083         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7084           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
7085                                         l_debug_info);
7086         END IF;
7087 
7088         l_invoice_id := p_invoice_id;
7089         BEGIN
7090           SELECT NVL(wfapproval_status, 'NOT REQUIRED')
7091           INTO l_wfapproval_status
7092           FROM   ap_invoices_all
7093           WHERE  invoice_id = l_invoice_id;
7094         END;
7095 
7096 
7097         -- Bug 5590138. Add the PLSQL Block
7098 
7099         IF l_wfapproval_status IN ('REQUIRED', 'INITIATED',
7100                                    'STOPPED',  'NEEDS WFREAPPROVAL') THEN
7101           BEGIN
7102            --get all of the approvers
7103             AME_API2.getAllApprovers1(applicationIdIn => 200,
7104                         transactionTypeIn => 'APINV',
7105                         transactionIdIn => to_char(l_invoice_id),
7106                         approvalProcessCompleteYNOut => l_complete,
7107                         approversOut => l_next_approvers,
7108                         itemIndexesOut => l_index,
7109                         itemIdsOut => l_ids,
7110                         itemClassesOut => l_class,
7111                         itemSourcesOut => l_source
7112                         );
7113           EXCEPTION
7114             WHEN NO_DATA_FOUND THEN
7115               NULL;
7116             WHEN OTHERS THEN
7117               FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
7118               FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
7119               FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE', l_calling_sequence);
7120               FND_MESSAGE.SET_TOKEN('DEBUG_INFO', l_debug_info);
7121               APP_EXCEPTION.RAISE_EXCEPTION;
7122           END;
7123 
7124         END IF;
7125 
7126 
7127         --More values in the approver list
7128         l_debug_info := 'after call to ame';
7129         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7130           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7131                         l_api_name,l_debug_info);
7132         END IF;
7133         IF l_complete = ame_util.booleanFalse THEN
7134            --Loop through approvers' table returned by AME
7135            l_debug_info := 'more approvers';
7136            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7137                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7138                         l_api_name,l_debug_info);
7139            END IF;
7140 
7141            FOR l_table IN
7142                 nvl(l_next_approvers.First,0)..nvl(l_next_Approvers.Last,-1)
7143                                                                          LOOP
7144                 l_debug_info := 'looping through approvers';
7145                 IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7146                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7147                         l_api_name,l_debug_info);
7148                 END IF;
7149                 --set the record variable
7150                 l_next_approver := l_next_approvers(l_table);
7151 
7152                 --if the approver record does not have a value for item_id,
7153                 --we need to
7154                 --use the item lists returned by AME to determine
7155                 --the items associated
7156                 --with this approver.
7157                 IF l_next_approver.item_id IS NULL THEN
7158                    l_debug_info := 'item_id is null';
7159                    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7160                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7161                         l_api_name,l_debug_info);
7162                    END IF;
7163 
7164                    FOR l_rec IN 1..l_index.count LOOP
7165                         --l_index contains the mapping between
7166                         --approvers and items
7167                         l_debug_info := 'looping through l_rec';
7168                         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7169                                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7170                                 l_api_name,l_debug_info);
7171                         END IF;
7172                         IF l_index(l_rec) = l_table THEN
7173                            l_debug_info := 'check type of item class';
7174                            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7175                                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7176                                 l_api_name,l_debug_info);
7177                            END IF;
7178                            --Depending on the type of item class, we need to set
7179                            --some variables
7180                            --amy need correction once project/dist seeded
7181                            IF l_class(l_rec) =
7182                                 ame_util.lineItemItemClassName THEN
7183                                 l_line_num := l_ids(l_rec);
7184                            ELSIF l_class(l_rec) = 'project code' THEN
7185 
7186                                 SELECT Invoice_Line_Number
7187                                 INTO l_line_num
7188                                 FROM AP_INVOICE_DISTRIBUTIONS_ALL
7189                                 WHERE project_id =l_ids(l_rec);
7190                            ELSIF l_class(l_rec) =
7191                                 ame_util.costCenterItemClassName THEN
7192 
7193                                 SELECT Invoice_Line_Number
7194                                 INTO l_line_num
7195                                 FROM AP_INVOICE_DISTRIBUTIONS_ALL
7196                                 WHERE project_id =l_ids(l_rec);
7197                            --distributions
7198                            ELSIF l_class(l_rec) <>
7199                                         ame_util.lineItemItemClassName
7200                                 AND l_class(l_rec) <>
7201                                         ame_util.headerItemClassName THEN
7202 
7203                                 SELECT Invoice_Line_Number
7204                                 INTO l_line_num
7205                                 FROM AP_INVOICE_DISTRIBUTIONS_ALL
7206                                 WHERE invoice_distribution_id = l_ids(l_rec);
7207 
7208                            END IF; --l_class
7209 
7210                            --Insert record into ap_approvers_list_gt
7211                            INSERT INTO AP_APPROVERS_LIST_GT(
7212                                 TRANSACTION_ID,  -- Bug 5624200
7213                                 LINE_NUMBER,
7214                                 ROLE_NAME,
7215                                 ORIG_SYSTEM,
7216                                 ORIG_SYSTEM_ID,
7217                                 DISPLAY_NAME,
7218                                 APPROVER_CATEGORY,
7219                                 API_INSERTION,
7220                                 AUTHORITY,
7221                                 APPROVAL_STATUS,
7222                                 ITEM_CLASS,
7223                                 ITEM_ID,
7224                                 APPROVER_ORDER_NUMBER)
7225                                 VALUES(
7226                                 p_invoice_id,
7227                                 decode( l_class(l_rec),
7228 				       'header',null,
7229 				       l_line_num),
7230                                 l_next_approver.NAME,
7231                                 l_next_approver.ORIG_SYSTEM,
7232                                 l_next_approver.ORIG_SYSTEM_ID,
7233                                 l_next_approver.DISPLAY_NAME,
7234                                 l_next_approver.APPROVER_CATEGORY,
7235                                 l_next_approver.API_INSERTION,
7236                                 l_next_approver.AUTHORITY,
7237                                 l_next_approver.APPROVAL_STATUS,
7238                                 l_class(l_rec),
7239 				l_ids(l_rec),
7240                                 l_next_approver.APPROVER_ORDER_NUMBER);
7241 
7242                                 l_debug_info := 'after insert';
7243                                 IF (G_LEVEL_STATEMENT >=
7244                                         G_CURRENT_RUNTIME_LEVEL) THEN
7245                                         FND_LOG.STRING(G_LEVEL_STATEMENT,
7246                                                         G_MODULE_NAME||
7247                                                 l_api_name,l_debug_info);
7248                                 END IF;
7249                         END IF; --l_index mapping
7250                    END LOOP; -- l_index mapping
7251 
7252                 ELSE  --only one item_id per approver
7253 
7254                     l_debug_info := 'only one item_id per approver';
7255                    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7256                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7257                         l_api_name,l_debug_info);
7258                    END IF;
7259                    --Depending on the type of item class, we need to set
7260                    --some variables:
7261                    IF l_next_approver.item_class =
7262                                 ame_util.lineItemItemClassName THEN
7263                         l_line_num := l_next_approver.item_id;
7264                    ELSIF l_next_approver.item_class = 'project code' THEN
7265 
7266                         SELECT Invoice_Line_Number
7267                         INTO l_line_num
7268                         FROM AP_INVOICE_DISTRIBUTIONS_ALL
7269                         WHERE project_id = l_next_approver.item_id;
7270                    ELSIF l_next_approver.item_class =
7271                                 ame_util.costCenterItemClassName THEN
7272 
7273                         SELECT Invoice_Line_Number
7274                         INTO l_line_num
7275                         FROM AP_INVOICE_DISTRIBUTIONS_ALL
7276                         WHERE project_id = l_next_approver.item_id;
7277                    --distributions
7278                    ELSIF l_next_approver.item_class <>
7279                                         ame_util.lineItemItemClassName
7280                          AND l_next_approver.item_class <>
7281                                         ame_util.headerItemClassName THEN
7282 
7283                         SELECT Invoice_Line_Number
7284                         INTO l_line_num
7285                         FROM AP_INVOICE_DISTRIBUTIONS_ALL
7286                         WHERE invoice_distribution_id = l_next_approver.item_id;
7287 
7288                    END IF; --l_class
7289                         -- Bug 5590138. Modified table to AP_APPROVERS_LIST_GT
7290                         -- from AP_APINV_APPROVERS
7291                         --Insert record into ap_approvers_list_gt
7292                         INSERT INTO AP_APPROVERS_LIST_GT(
7293                                TRANSACTION_ID,  -- Bug 5624200
7294                                LINE_NUMBER,
7295                                 ROLE_NAME,
7296                                 ORIG_SYSTEM,
7297                                 ORIG_SYSTEM_ID,
7298                                 DISPLAY_NAME,
7299                                 APPROVER_CATEGORY,
7300                                 API_INSERTION,
7301                                 AUTHORITY,
7302                                  APPROVAL_STATUS,
7303                                 ITEM_CLASS,
7304                                 ITEM_ID,
7305                                 APPROVER_ORDER_NUMBER)
7306                                 VALUES(
7307                                 p_invoice_id,
7308                                 decode(l_next_approver.item_class,
7309 				       'header',null,
7310 				       l_line_num),
7311                                 l_next_approver.NAME,
7312                                 l_next_approver.ORIG_SYSTEM,
7313                                 l_next_approver.ORIG_SYSTEM_ID,
7314                                 l_next_approver.DISPLAY_NAME,
7315                                 l_next_approver.APPROVER_CATEGORY,
7316                                 l_next_approver.API_INSERTION,
7317                                 l_next_approver.AUTHORITY,
7318                                 l_next_approver.APPROVAL_STATUS,
7319                                 l_next_approver.item_class,
7320 				l_next_approver.item_id,
7321                                 l_next_approver.APPROVER_ORDER_NUMBER);
7322 
7323                 END IF; --more than one item_id per approver
7324 
7325            END LOOP; --nextApprovers table
7326 
7327         END IF; --complete
7328 
7329 EXCEPTION
7330 WHEN OTHERS
7331         THEN
7332     IF (SQLCODE <> -20001) THEN
7333       FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
7334       FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
7335       FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE', l_calling_sequence);
7336       FND_MESSAGE.SET_TOKEN('DEBUG_INFO', l_debug_info);
7337     END IF;
7338     APP_EXCEPTION.RAISE_EXCEPTION;
7339 
7340 END Get_All_Approvers;
7341 
7342 /*This function terminates any invoice approval workflow processes
7343  when a user turns off the 'Use Invoice Approval Workflow' payables
7344 option. */
7345 
7346 PROCEDURE Terminate_Approval(
7347                         errbuf  OUT NOCOPY VARCHAR2,
7348                         retcode OUT NOCOPY NUMBER,
7349                         p_org_id IN VARCHAR2  --bug 14492241
7350                             ) IS
7351 
7352 	--Define cursor for wf and ame records that need to be terminated
7353 	/* Order by Logic is needed since there is a parent/child
7354 	   relationship and Child process has to be aborted before
7355 	   the parent. */
7356     --cursor commented for bug 14492241 and added new
7357     /*	CURSOR   key_cur IS
7358         SELECT   item_key, item_type,root_activity,
7359 	         SUBSTR(item_key,1,INSTR(item_key,'_')-1) invoice_id
7360 	FROM     wf_items
7361 	WHERE    item_type IN ('APINVAPR','APINVNEG')
7362 	AND      end_date is NULL        -- Bug No 10172485
7363 	ORDER BY decode(root_activity,'APPROVAL_MAIN','3'
7364 	                             ,'APPROVAL_INVOICE_LINES','2'
7365 				     ,'APPROVAL_NEGOTIATION', '1') asc;
7366 
7367       */
7368        --bug 14492241 added
7369        CURSOR   key_cur IS
7370         SELECT   item_key, item_type,root_activity,
7371 	         ai.invoice_id
7372 	FROM     wf_items wf,
7373                  ap_invoices_all ai
7374 	WHERE    wf.item_type IN ('APINVAPR','APINVNEG')
7375 	AND      wf.end_date is NULL        -- Bug No 10172485
7376         and      ai.invoice_id||'_'||ai.approval_iteration = wf.item_key
7377         and      ai.org_id=p_org_id
7378 	ORDER BY decode(root_activity,'APPROVAL_MAIN','3'
7379 	                             ,'APPROVAL_INVOICE_LINES','2'
7380 				     ,'APPROVAL_NEGOTIATION', '1') asc;
7381 
7382 	l_api_name      CONSTANT VARCHAR2(200) := 'Terminate_Approval';
7383 	l_item_key      wf_items.item_key%TYPE;
7384 	l_item_type     wf_items.item_type%TYPE;
7385 	l_process       wf_items.root_activity%TYPE;
7386 	l_invoice_id	NUMBER;
7387         l_debug_info    VARCHAR2(2000);
7388         l_calling_sequence      VARCHAR2(2000);
7389 
7390 BEGIN
7391 
7392         IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
7393           FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_IAW_PKG.'|| l_api_name);
7394         END IF;
7395 
7396         l_calling_sequence := l_api_name;
7397 
7398 	l_debug_info := 'opening key cursor';
7399         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7400           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
7401                                         l_debug_info);
7402         END IF;
7403 
7404         OPEN key_Cur;
7405         LOOP
7406 
7407            FETCH key_Cur INTO l_item_key, l_item_type,
7408 		                   l_process, l_invoice_id;
7409 
7410            EXIT WHEN key_Cur%NOTFOUND OR key_Cur%NOTFOUND IS NULL;
7411 
7412 	   WF_Engine.abortProcess(
7413 			itemType => l_item_type,
7414 			itemKey  => l_item_key,
7415 			process => l_process);
7416 
7417 	   --we only need to clear for main process
7418 	   IF l_process = 'APPROVAL_MAIN' THEN
7419 
7420 	      AME_API2.clearAllApprovals(applicationIdIn => 200,
7421                        transactionIdIn     => to_char(l_invoice_id),
7422                        transactionTypeIn =>  'APINV');
7423 
7424            END IF;
7425 
7426         END LOOP;
7427         CLOSE key_Cur;
7428 
7429 	--Clear all iaw processing records
7430 
7431 	DELETE FROM AP_APINV_APPROVERS WHERE invoice_id in
7432 	(select invoice_id from ap_invoices_all ai
7433 	 where ai.org_id=p_org_id
7434 	 and wfapproval_status in ('INITIATED','REQUIRED'
7435 	                            ,'REJECTED','NEEDS WFREAPPROVAL'
7436 				    ,'STOPPED')
7437          );
7438 
7439 
7440 /*  --bug 14492241 commented the below and added new for performance
7441 	--Set the lines status
7442 	UPDATE  ap_invoice_lines_all
7443     	SET  wfapproval_status = 'NOT REQUIRED'
7444   	WHERE  wfapproval_status in ('INITIATED','REQUIRED'
7445 	                            ,'REJECTED','NEEDS WFREAPPROVAL'
7446 				    ,'STOPPED');
7447 
7448 	--Set the header status
7449 	UPDATE  ap_invoices_all
7450     	SET  wfapproval_status = 'NOT REQUIRED'
7451   	WHERE  wfapproval_status in ('INITIATED','REQUIRED'
7452 	                            ,'REJECTED','NEEDS WFREAPPROVAL'
7453 				    ,'STOPPED');
7454             */
7455 
7456   	--Set the lines status
7457      UPDATE  ap_invoice_lines_all ail
7458     	SET  ail.wfapproval_status = 'NOT REQUIRED'
7459   	WHERE  ail.invoice_id in
7460          ( select /*+unnest*/ invoice_id
7461 	   from ap_invoices_all ai
7462            where ai.org_id=p_org_id
7463            and ai.wfapproval_status in ('INITIATED','REQUIRED'
7464 	                            ,'REJECTED','NEEDS WFREAPPROVAL','STOPPED')
7465          )
7466      AND wfapproval_status in ('INITIATED','REQUIRED'
7467 	                            ,'REJECTED','NEEDS WFREAPPROVAL'
7468 				    ,'STOPPED');
7469 
7470   	--Set the header status
7471 	UPDATE  ap_invoices_all
7472     	SET  wfapproval_status = 'NOT REQUIRED'
7473   	WHERE org_id = p_org_id
7474          AND wfapproval_status in ('INITIATED','REQUIRED'
7475 	                            ,'REJECTED','NEEDS WFREAPPROVAL'
7476 				    ,'STOPPED');
7477 
7478 EXCEPTION
7479   WHEN OTHERS THEN
7480 
7481     IF (SQLCODE <> -20001) THEN
7482       FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
7483       FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
7484       FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE', l_calling_sequence);
7485       FND_MESSAGE.SET_TOKEN('DEBUG_INFO', l_debug_info);
7486     END IF;
7487     APP_EXCEPTION.RAISE_EXCEPTION;
7488 
7489 END Terminate_Approval;
7490 PROCEDURE wakeup_lineapproval_process( p_invoice_id IN NUMBER,
7491                                        p_itemkey  IN VARCHAR2) IS
7492 l_sent          NUMBER;
7493 l_invoice_key   VARCHAR2(50);
7494 l_api_name      CONSTANT VARCHAR2(200) := 'wakeup_lineapproval_process';
7495 l_debug_info    VARCHAR2(2000);
7496 
7497 BEGIN
7498    l_debug_info := 'Start';
7499    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7500           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7501                          l_api_name,l_debug_info);
7502    END IF;
7503    SELECT count(*)
7504    INTO  l_sent
7505    FROM  AP_APINV_APPROVERS
7506    WHERE Notification_status = 'SENT'
7507    AND   child_process_item_key = p_itemkey
7508    AND   invoice_id = p_invoice_id;
7509    IF l_sent = 0 THEN
7510       wf_engine.CompleteActivity(
7511                         itemType => 'APINVAPR',
7512                         itemKey  => p_itemkey,
7513                         activity => 'APPROVAL_INVOICE_LINES:WAITLINEAPRVL',
7514                         result   => 'NULL');
7515    ELSE
7516 
7517       BEGIN
7518 
7519          SELECT invoice_key
7520          INTO   l_invoice_key
7521          FROM   AP_APINV_APPROVERS
7522          WHERE  Notification_Key = p_itemkey
7523          AND    invoice_id = p_invoice_id
7524          AND    rownum = 1;
7525       EXCEPTION
7526          WHEN NO_DATA_FOUND THEN
7527             l_debug_info := 'No Data Found in SELECT from AP_APINV_APPROVERS' ;
7528          IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7529                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7530                                l_api_name,l_debug_info);
7531          END IF;
7532          RAISE;
7533 
7534       END;
7535       l_debug_info := 'Before CompleteActivity APPROVAL_MAIN:BLOCK' ||
7536                       'l_invoice_key = ' || l_invoice_key;
7537       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7538              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7539                             l_api_name,l_debug_info);
7540       END IF;
7541       BEGIN
7542          wf_engine.CompleteActivity(
7543                            itemType => 'APINVAPR',
7544                            itemKey  => l_invoice_key,
7545                            activity => 'APPROVAL_MAIN:BLOCK',
7546                            result   => 'NULL');
7547       EXCEPTION
7548       WHEN OTHERS THEN NULL;
7549       END;
7550    END IF;
7551    l_debug_info := 'End';
7552    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7553           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7554                          l_api_name,l_debug_info);
7555    END IF;
7556 EXCEPTION
7557    WHEN OTHERS THEN NULL;
7558 END wakeup_lineapproval_process;
7559 
7560 PROCEDURE is_invoice_matched(itemtype IN VARCHAR2,
7561                         itemkey IN VARCHAR2,
7562                         actid   IN NUMBER,
7563                         funcmode IN VARCHAR2,
7564                         resultout  OUT NOCOPY VARCHAR2 ) IS
7565    l_match_type VARCHAR2(80);
7566    l_debug_info            VARCHAR2(2000);
7567    l_api_name              CONSTANT VARCHAR2(200) := 'is_invoice_matched';
7568 BEGIN
7569    l_match_type := WF_ENGINE.GETITEMATTRText(itemtype,
7570                         itemkey,
7571                         'INV_MATCH_TYPE');
7572 
7573 
7574    IF l_match_type = 'MATCHED' THEN
7575       resultout := wf_engine.eng_completed||':'||'Y';
7576    ELSE
7577       resultout := wf_engine.eng_completed||':'||'N';
7578    END IF;
7579    l_debug_info := 'resultout : ' || resultout;
7580    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7581           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7582                          l_api_name,l_debug_info);
7583    END IF;
7584 EXCEPTION
7585 
7586 WHEN OTHERS
7587    THEN
7588         WF_CORE.CONTEXT('APINVAPR','is_invoice_matched',itemtype,
7589                         itemkey, to_char(actid), funcmode);
7590         RAISE;
7591 END is_invoice_matched;
7592 
7593 PROCEDURE aprvl_process_reject_int(itemtype IN VARCHAR2,
7594                         itemkey IN VARCHAR2,
7595                         actid   IN NUMBER,
7596                         funcmode IN VARCHAR2,
7597                         resultout  OUT NOCOPY VARCHAR2 ) IS
7598    l_api_name      CONSTANT VARCHAR2(200) := 'aprvl_process_reject_int';
7599    l_debug_info    VARCHAR2(2000);
7600    l_parent_key    VARCHAR2(150);
7601 BEGIN
7602    l_parent_key := WF_ENGINE.GetItemAttrText(itemtype,
7603                              itemkey,
7604                              'PARENT_KEY');
7605 
7606    wf_engine.CompleteActivity(
7607                         itemType => 'APINVAPR',
7608                         itemKey  => l_parent_key,
7609                         activity => 'APPROVAL_INVOICE:DOCUMENT_APPROVAL_REQUEST',
7610                         result   => 'REJECTED');
7611    resultout := wf_engine.eng_completed||':'||'Y';
7612 EXCEPTION
7613 
7614 WHEN OTHERS
7615    THEN
7616         WF_CORE.CONTEXT('APINVNEG','aprvl_process_reject_int',itemtype,
7617                         itemkey, to_char(actid), funcmode);
7618         RAISE;
7619 END aprvl_process_reject_int;
7620 
7621 -- Bug 8462325. Added parameter p_process_instance_label.
7622 PROCEDURE approve_button( p_itemkey  IN VARCHAR2,
7623                           p_process_instance_label IN VARCHAR2) IS
7624 l_api_name      CONSTANT VARCHAR2(200) := 'approve_button';
7625 l_debug_info    VARCHAR2(2000);
7626 l_invoice_id    NUMBER(15); -- Bug 6845397
7627 -- Bug 8462325.
7628 l_activity      VARCHAR2(100) := 'APPROVAL_INVOICE:' || p_process_instance_label ;
7629 
7630 BEGIN
7631    l_debug_info := 'Start';
7632    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7633           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7634                          l_api_name,l_debug_info);
7635    END IF;
7636    -- Bug 6845397. Getting invoice id.
7637    l_invoice_id := substr(p_itemkey, 1, instr(p_itemkey,'_')-1);
7638    BEGIN
7639       wf_engine.CompleteActivity(
7640                            itemType => 'APINVAPR',
7641                            itemKey  => p_itemkey,
7642                            activity => l_activity, -- Bug 8462325.
7643                            result   => 'APPROVED');
7644    EXCEPTION
7645    WHEN OTHERS THEN NULL;
7646    END;
7647    -- Bug 6845397.
7648    -- Added code to set the generate_dists flag of invoice lines to D, if the line has
7649    -- atleast one invoice distribution.
7650 
7651    l_debug_info := 'Updating the generate_dists of ap_invoice_lines. Invoice_id = ' || l_invoice_id;
7652    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7653           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7654                          l_api_name,l_debug_info);
7655    END IF;
7656    update ap_invoice_lines_all ail
7657    set generate_dists = decode(ail.generate_dists, 'Y', 'D', ail.generate_dists)
7658    where ail.invoice_id = l_invoice_id
7659      and exists( select 1 from ap_invoice_distributions_all aid
7660                  where aid.invoice_id = l_invoice_id
7661                    and aid.invoice_line_number = ail.line_number) ;
7662 
7663    -- End bug 6845397
7664    l_debug_info := 'End';
7665    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7666           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7667                          l_api_name,l_debug_info);
7668    END IF;
7669 EXCEPTION
7670    WHEN OTHERS THEN
7671    l_debug_info := 'Exception during update.';
7672    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7673           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7674                          l_api_name,l_debug_info);
7675    END IF ;
7676    APP_EXCEPTION.RAISE_EXCEPTION;
7677 END approve_button;
7678 
7679 --Bug 8689391. Added parameter processInstanceLabel  to procedure rejectButton.
7680 PROCEDURE reject_button( p_itemkey  IN VARCHAR2,
7681                          p_process_instance_label IN VARCHAR2) IS
7682 l_api_name      CONSTANT VARCHAR2(200) := 'reject_button';
7683 l_debug_info    VARCHAR2(2000);
7684 --for Bug 8689391
7685 l_activity      VARCHAR2(100) := 'APPROVAL_INVOICE:' || p_process_instance_label ;
7686 BEGIN
7687    l_debug_info := 'Start';
7688    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7689           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7690                          l_api_name,l_debug_info);
7691    END IF;
7692    BEGIN
7693       wf_engine.CompleteActivity(
7694                            itemType => 'APINVAPR',
7695                            itemKey  => p_itemkey,
7696                            activity => l_activity, -- for Bug 8689391
7697                            result   => 'REJECTED');
7698    EXCEPTION
7699    WHEN OTHERS THEN NULL;
7700    END;
7701    l_debug_info := 'End';
7702    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7703           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7704                          l_api_name,l_debug_info);
7705    END IF;
7706 EXCEPTION
7707       WHEN OTHERS THEN NULL;
7708 END reject_button;
7709 
7710 PROCEDURE accept_invoice_button( p_itemkey  IN VARCHAR2) IS
7711 l_api_name      CONSTANT VARCHAR2(200) := 'accept_invoice_button';
7712 l_debug_info    VARCHAR2(2000);
7713 
7714 BEGIN
7715    l_debug_info := 'Start';
7716    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7717           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7718                          l_api_name,l_debug_info);
7719    END IF;
7720    BEGIN
7721       wf_engine.CompleteActivity(
7722                           itemType => 'APINVNEG',
7723                           itemKey  => p_itemkey,
7724                           activity => 'APPROVAL_NEGOTIATION:NOTIFY_SUP_NEGOTIATION',
7725                           result   => 'ACCEPT');
7726    EXCEPTION
7727       WHEN OTHERS THEN NULL;
7728    END;
7729    l_debug_info := 'End';
7730    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7731           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7732                          l_api_name,l_debug_info);
7733    END IF;
7734 EXCEPTION
7735    WHEN OTHERS THEN NULL;
7736 END accept_invoice_button;
7737 PROCEDURE accept_invoice_int_button( p_itemkey  IN VARCHAR2) IS
7738 l_api_name      CONSTANT VARCHAR2(200) := 'accept_invoice_int_button';
7739 l_debug_info    VARCHAR2(2000);
7740 
7741 BEGIN
7742    l_debug_info := 'Start';
7743    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7744           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7745                          l_api_name,l_debug_info);
7746    END IF;
7747    BEGIN
7748       wf_engine.CompleteActivity(
7749                           itemType => 'APINVNEG',
7750                           itemKey  => p_itemkey,
7751                           activity => 'APPROVAL_NEGOTIATION:NOTIFY_INT_NEGOTIATION',
7752                           result   => 'ACCEPT');
7753    EXCEPTION
7754       WHEN OTHERS THEN NULL;
7755    END;
7756    l_debug_info := 'End';
7757    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7758           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7759                          l_api_name,l_debug_info);
7760    END IF;
7761 EXCEPTION
7762    WHEN OTHERS THEN NULL;
7763 END accept_invoice_int_button;
7764 
7765 PROCEDURE cancel_invoice_aprvl_button( p_itemkey  IN VARCHAR2) IS
7766 l_api_name      CONSTANT VARCHAR2(200) := 'cancel_invoice_aprvl_button';
7767 l_debug_info    VARCHAR2(2000);
7768 
7769 BEGIN
7770    l_debug_info := 'Start';
7771    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7772           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7773                          l_api_name,l_debug_info);
7774    END IF;
7775    BEGIN
7776       wf_engine.CompleteActivity(
7777                           itemType => 'APINVNEG',
7778                           itemKey  => p_itemkey,
7779                           activity => 'APPROVAL_NEGOTIATION:NOTIFY_SUP_NEGOTIATION',
7780                           result   => 'CANCELINVOICE');
7781    EXCEPTION
7782       WHEN OTHERS THEN NULL;
7783    END;
7784    l_debug_info := 'End';
7785    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7786           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7787                          l_api_name,l_debug_info);
7788    END IF;
7789 EXCEPTION
7790    WHEN OTHERS THEN NULL;
7791 END cancel_invoice_aprvl_button;
7792 
7793 PROCEDURE set_comments( p_itemkey  IN VARCHAR2,  p_notif_id IN VARCHAR2,  p_notes in VARCHAR2) IS
7794 l_api_name      CONSTANT VARCHAR2(200) := 'set_comments';
7795 l_debug_info    VARCHAR2(2000);
7796 BEGIN
7797 
7798    l_debug_info := 'Start';
7799    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7800           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7801                          l_api_name,l_debug_info);
7802    END IF;
7803 
7804    WF_ENGINE.SetItemAttrText('APINVAPR',
7805                         p_itemkey,
7806                         'WF_NOTE',
7807                         p_notes);
7808 
7809    l_debug_info := 'Inserting wf_notes for approval from product page';
7810    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7811           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7812                          l_api_name,l_debug_info);
7813    END IF;
7814 
7815 -- Save approval comments in wf_notification_attributes
7816 -- when approval action is triggered from product page (Inv Details)
7817 
7818    if p_notes is not null then
7819     WF_NOTIFICATION.setAttrText(	to_number(p_notif_id),
7820 				'WF_NOTE',
7821 				p_notes);
7822    end if;
7823 
7824    l_debug_info := 'End';
7825    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7826           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7827                          l_api_name,l_debug_info);
7828    END IF;
7829 
7830 EXCEPTION
7831    WHEN OTHERS THEN NULL;
7832 END;
7833 
7834 PROCEDURE is_payment_request(itemtype IN VARCHAR2,
7835                         itemkey IN VARCHAR2,
7836                         actid   IN NUMBER,
7837                         funcmode IN VARCHAR2,
7838                         resultout  OUT NOCOPY VARCHAR2 ) IS
7839    l_invoice_id ap_invoices_all.invoice_id%TYPE;
7840    l_inv_type ap_invoices_all.invoice_type_lookup_code%TYPE;
7841    l_debug_info            VARCHAR2(2000);
7842    l_api_name              CONSTANT VARCHAR2(200) := 'is_payment_request';
7843 BEGIN
7844    l_invoice_id := WF_ENGINE.GETITEMATTRNumber
7845                   (itemtype,
7846                    itemkey,
7847                    'INVOICE_ID');
7848    SELECT ai.invoice_type_lookup_code
7849    INTO   l_inv_type
7850    FROM   ap_invoices_all ai
7851    WHERE  invoice_id = l_invoice_id;
7852 
7853    IF l_inv_type = 'PAYMENT REQUEST' THEN
7854       resultout := wf_engine.eng_completed||':'||'Y';
7855    ELSE
7856       resultout := wf_engine.eng_completed||':'||'N';
7857    END IF;
7858    l_debug_info := 'resultout : ' || resultout;
7859    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7860           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
7861                          l_api_name,l_debug_info);
7862    END IF;
7863 EXCEPTION
7864 
7865 WHEN OTHERS
7866    THEN
7867         WF_CORE.CONTEXT('APINVAPR','is_payment_request',itemtype,
7868                         itemkey, to_char(actid), funcmode);
7869         RAISE;
7870 END is_payment_request;
7871 -- ANSETHUR 18-APR-2008
7872 /*
7873 PROCEDURE NAME : notification_handler
7874 PROJECT : R12 - Workflow Delegation enhancement
7875 PURPOSE : This procedure is attached to the notifications in order
7876           to populate the forward to role attribute during the delegation
7877           and to create history entries.Also the throws error when the
7878           notification ownership is transfered.
7879 */
7880 PROCEDURE notification_handler( itemtype  IN VARCHAR2,
7881                                 itemkey   IN VARCHAR2,
7882                                 actid     IN NUMBER,
7883                                 funcmode  IN VARCHAR2,
7884                                 resultout OUT NOCOPY VARCHAR2 ) IS
7885 l_invoice_id               NUMBER(15);
7886 l_iteration                NUMBER;
7887 l_status	                 VARCHAR2(50);
7888 l_response                 VARCHAR2(50);
7889 l_nid                      number;
7890 l_forward_to_person_id     number;
7891 l_result                   varchar2(100);
7892 l_orig_system              WF_ROLES.ORIG_SYSTEM%TYPE;
7893 l_orig_sys_id              WF_ROLES.ORIG_SYSTEM_ID%TYPE;
7894 l_role                     VARCHAR2(50);
7895 l_role_display             VARCHAR2(150);
7896 l_org_id                   NUMBER(15);
7897 l_name                     wf_users.name%TYPE; --bug 8620671
7898 l_display_name             VARCHAR2(150);
7899 l_forward_to_user_id       WF_ROLES.ORIG_SYSTEM_ID%TYPE;
7900 l_esc_approver             AME_UTIL.approverRecord;
7901 l_rec_role                 VARCHAR2(50);
7902 l_comments                 VARCHAR2(240);
7903 l_hist_id                  NUMBER(15);
7904 l_amount                   ap_invoices_all.invoice_amount%TYPE;
7905 l_user_id                  NUMBER(15);
7906 l_login_id                 NUMBER(15);
7907 l_hist_rec                 AP_INV_APRVL_HIST%ROWTYPE;
7908 l_notf_iteration           NUMBER;
7909 l_role_name                VARCHAR2(50);
7910 l_esc_flag                 VARCHAR2(1);
7911 l_esc_role                 VARCHAR2(50);
7912 l_esc_role_actual          VARCHAR2(50);
7913 l_role_actual              VARCHAR2(50);
7914 l_fwd_role                 VARCHAR2(50);
7915 l_invoice_total            NUMBER;
7916 
7917 CURSOR c_get_user ( p_rec_role IN VARCHAR2 ) IS
7918   SELECT user_id,employee_id
7919   FROM   FND_USER
7920   WHERE  USER_NAME=p_rec_role;
7921 
7922 CURSOR c_get_response ( p_invoice_id IN NUMBER) IS
7923   Select response
7924   From   ap_inv_aprvl_hist
7925   WHERE  approval_history_id=(select max(approval_history_id)from ap_inv_aprvl_hist
7926 		                         where invoice_id= p_invoice_id);
7927 
7928 BEGIN
7929     l_nid := WF_ENGINE.context_nid;
7930     l_invoice_id := WF_ENGINE.GetItemAttrNumber(itemtype,
7931                                                 itemkey,
7932                                                 'INVOICE_ID');
7933 
7934     l_org_id := WF_ENGINE.GETITEMATTRNumber(  itemtype,
7935                                               itemkey,
7936                                               'ORG_ID');
7937 
7938     l_iteration := WF_ENGINE.GetItemAttrNumber( itemtype,
7939                                                 itemkey,
7940                                                 'ITERATION');
7941     l_comments := WF_ENGINE.GetItemAttrText(itemtype,
7942                                             itemkey,
7943                                             'WF_NOTE');
7944     l_notf_iteration := WF_ENGINE.GetItemAttrnumber(itemtype,
7945                                                     itemkey,
7946                                                     'NOTF_ITERATION');
7947 -- bug # 8244218  begins
7948     l_esc_flag := WF_ENGINE.GetItemAttrnumber(itemtype,
7949                                                     itemkey,
7950                                                     'ESCALATED');
7951      /*WF_ENGINE.GetItemAttrnumber(itemtype,
7952                                                     itemkey,
7953                                                     'ESCALATED'); */
7954 -- bug # 8244218  ends
7955     l_role_actual := WF_ENGINE.GetItemAttrText(itemtype,
7956                                             itemkey,
7957                                             'ROLE_ACTUAL');
7958     l_esc_role_actual := WF_ENGINE.GetItemAttrText(itemtype,
7959                                             itemkey,
7960                                             'ESC_ROLE_ACTUAL');
7961     l_fwd_role := WF_ENGINE.GetItemAttrText(itemtype,
7962                                             itemkey,
7963                                             'FORWARD_ROLE');
7964    l_invoice_total := WF_ENGINE.GETITEMATTRNumber(itemtype,
7965                    itemkey,
7966                    'INVOICE_TOTAL');
7967 -- getting approver in the document_approver and setting in the ROLE_ACTUAL attribute
7968 -- So even if the notification is delegated the ame will be updated in the for the actual approver
7969 -- the further level of approval will be done by the owners supervisors and also the same
7970 -- notification won't be sent to  this approver again
7971          IF ((nvl(l_esc_flag,'N') = 'N')and (l_fwd_role is null) ) THEN
7972           l_role_name :=WF_ENGINE.GetItemAttrText(itemtype,
7973                                     itemkey,
7974                                     'DOCUMENT_APPROVER');
7975           WF_ENGINE.SetItemAttrText(itemtype,
7976                                     itemkey,
7977                                     'ROLE_ACTUAL',
7978                                     l_role_name);
7979 
7980           elsif ((nvl(l_esc_flag,'N') = 'Y')and (l_esc_role_actual is null) ) THEN
7981           l_esc_role :=WF_ENGINE.GetItemAttrText( itemtype,
7982                                                   itemkey,
7983                                                   'ESC_ROLE_NAME');
7984           WF_ENGINE.SetItemAttrText( itemtype,
7985                                      itemkey,
7986                                      'ESC_ROLE_ACTUAL',
7987                                      l_esc_role);
7988 
7989           end if ;
7990 -- end
7991 
7992 
7993   IF (funcmode ='FORWARD') then
7994          l_rec_role :=WF_ENGINE.context_text;
7995          l_status:='DELEGATED';
7996 
7997          --fnd_client_info.set_org_context(l_org_id);
7998           --Now set the environment
7999          MO_GLOBAL.INIT ('SQLAP');
8000          MO_GLOBAL.set_policy_context('S',l_org_id);
8001 
8002 
8003          /*insert_history(      l_invoice_id,
8004                               l_iteration,
8005                               l_org_id,
8006                               l_status);*/
8007 
8008 
8009 
8010           OPEN  c_get_user ( l_rec_role);
8011           FETCH c_get_user INTO l_forward_to_user_id,l_forward_to_person_id;
8012           CLOSE c_get_user;
8013 
8014           IF l_forward_to_person_id is not NULL then
8015                      l_orig_system := 'PER';
8016                      l_orig_sys_id := l_forward_to_person_id;
8017           ELSE
8018                      l_orig_system := 'FND_USR';
8019                      l_orig_sys_id := l_forward_to_user_id;
8020           END IF;
8021           WF_DIRECTORY.GetRoleName( l_orig_system,
8022                                     l_orig_sys_id,
8023                                     l_role,
8024                                     l_role_display);
8025 
8026           WF_DIRECTORY.GetUserName( l_orig_system,
8027                                     l_orig_sys_id,
8028                                     l_name,
8029                                     l_display_name);
8030 
8031 
8032           WF_ENGINE.SetItemAttrText(itemtype,
8033                                     itemkey,
8034                                     'FORWARD_ROLE',
8035                                     l_role);
8036         IF nvl(l_esc_flag,'N') = 'N' THEN
8037 
8038 
8039           WF_ENGINE.SetItemAttrText(itemtype,
8040                                     itemkey,
8041                                     'DOCUMENT_APPROVER',
8042                                     l_role);
8043           WF_ENGINE.SetItemAttrText( itemtype,
8044                                      itemkey,
8045                                      'APPROVER_NAME',
8046                                      l_display_name);
8047 
8048            WF_ENGINE.SetItemAttrNumber(itemtype,
8049                                        itemkey,
8050                                        'APPROVER_ID',
8051                                        l_orig_sys_id);
8052        /* WF_ENGINE.SetItemAttrText( itemtype,
8053                                      itemkey,
8054                                      'ORIG_SYSTEM',
8055                                      l_orig_system);     */
8056 
8057         else
8058           WF_ENGINE.SetItemAttrText(itemtype,
8059                                     itemkey,
8060                                     'ESC_ROLE_NAME',
8061                                     l_role);
8062           WF_ENGINE.SetItemAttrText( itemtype,
8063                                      itemkey,
8064                                      'ESC_APPROVER_NAME',
8065                                      l_display_name);
8066 
8067            WF_ENGINE.SetItemAttrNumber(itemtype,
8068                                        itemkey,
8069                                        'ESC_APPROVER_ID',
8070                                        l_orig_sys_id);
8071 
8072         end if ;
8073           -- Bug 10323137 starts
8074           l_comments                    := nvl(l_comments,NVL(WF_ENGINE.context_user_comment,'None'));
8075           -- Bug 10323137 ends
8076           l_comments                    :='To: '||l_display_name||' '||'    With Comment:'||l_comments;
8077 
8078           l_hist_rec.HISTORY_TYPE       := 'DOCUMENTAPPROVAL';
8079           l_hist_rec.INVOICE_ID         := l_invoice_id;
8080           l_hist_rec.ITERATION          := l_iteration;
8081           l_hist_rec.RESPONSE           := l_status;
8082 
8083           l_hist_rec.APPROVER_ID        := l_orig_sys_id;
8084           l_hist_rec.APPROVER_NAME      := l_display_name;
8085 
8086           l_hist_rec.CREATED_BY         := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
8087           l_hist_rec.CREATION_DATE      := sysdate;
8088           l_hist_rec.LAST_UPDATE_DATE   := sysdate;
8089           l_hist_rec.LAST_UPDATED_BY    := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
8090           l_hist_rec.LAST_UPDATE_LOGIN  := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),-1);
8091           l_hist_rec.ORG_ID             := l_org_id;
8092           l_hist_rec.AMOUNT_APPROVED    := l_invoice_total;--0;
8093           l_hist_rec.NOTIFICATION_ORDER := l_notf_iteration;
8094           l_hist_rec.APPROVER_COMMENTS  := l_comments;
8095 
8096           insert_history_table(p_hist_rec => l_hist_rec);
8097 
8098            /*--Bug6406901, added the approver_name to the update cmd to populate correct value in AP_INV_APRVL_HLIST
8099            UPDATE AP_INV_APRVL_HIST
8100            set   APPROVER_COMMENTS = l_comments, approver_name = l_display_name
8101            WHERE APPROVAL_HISTORY_ID =(select max(APPROVAL_HISTORY_ID) from AP_INV_APRVL_HIST
8102                                         where invoice_id=l_invoice_id);*/
8103            resultout := wf_engine.eng_completed || ':' || wf_engine.eng_null;
8104            return;
8105    End if;
8106 
8107       /* BEGIN
8108            Select response into l_response
8109            From   ap_inv_aprvl_hist
8110            WHERE  approval_history_id=(select max(approval_history_id)from ap_inv_aprvl_hist
8111     			                             where invoice_id=l_invoice_id);
8112        EXCEPTION
8113          WHEN OTHERS THEN
8114           NULL;
8115        END; */
8116           OPEN  c_get_response ( l_invoice_id);
8117           FETCH c_get_response INTO l_response;
8118           CLOSE c_get_response;
8119 
8120 	IF (funcmode = 'RESPOND' and l_response='DELEGATED') then
8121             /*l_result := wf_notification.GetAttrText(l_nid, 'RESULT');
8122 
8123             If (l_result='APPROVED') then
8124             l_result:='WFAPPROVED';
8125             End IF;
8126 
8127 
8128             insert_history(l_invoice_id,
8129                         l_iteration,
8130                         l_org_id,
8131                         l_result);
8132 
8133 
8134           WF_ENGINE.SetItemAttrText(  itemtype,
8135                                       itemkey,
8136                                       'FORWARD_ROLE',
8137                                       NULL);-- */
8138 
8139             resultout := wf_engine.eng_completed || ':' || wf_engine.eng_null;
8140             return;
8141   End if;
8142 
8143 
8144   -- Don't allow transfer
8145   if ( funcmode = 'TRANSFER' ) then
8146        --resultout := 'ERROR:WFSRV_NO_DELEGATE';
8147 	TRANSFER_NOTIFICATION(
8148     itemtype    => itemtype,
8149     itemkey =>     itemkey,
8150     actid   => actid,
8151     Funcmode => Funcmode,
8152     approvalType => 'DOCUMENTAPPROVAL',
8153     Resultout => Resultout );
8154        return;
8155   end if;
8156 
8157 return;
8158 EXCEPTION
8159 
8160 WHEN OTHERS
8161    THEN
8162         WF_CORE.CONTEXT('AP_WF',itemtype, itemkey, to_char(actid), funcmode);
8163         RAISE;
8164 
8165 END;
8166 /*
8167 PROCEDURE NAME : forward_check
8168 PROJECT : R12 - Workflow Delegation enhancement
8169 PURPOSE : This procedure is attached to the check_forward node.
8170           This procedure checks before escalation whether the notification
8171           is forwarded when there is a time out. If the result is N
8172           (i.e when the notification is not a forwarded one) ,
8173           then the escalation notification is sent.If the result is Y, then the
8174           document is rejected.
8175 */
8176 PROCEDURE forward_check(itemtype   IN VARCHAR2,
8177                         itemkey    IN VARCHAR2,
8178                         actid      IN NUMBER,
8179                         funcmode   IN VARCHAR2,
8180                         resultout  OUT NOCOPY VARCHAR2 ) IS
8181 l_forward_role_name   varchar2(50);
8182 l_current_role_name   varchar2(50);
8183 l_temp_invoice_id     NUMBER(15);     -- Bug 5037108
8184 l_error_message       VARCHAR2(2000); -- Bug 5037108
8185 l_debug_info 	        VARCHAR2(50);   -- Bug 5037108
8186 
8187 BEGIN
8188   l_forward_role_name :=     WF_ENGINE.GetItemAttrText(itemtype,
8189                               itemkey,
8190                               'FORWARD_ROLE');
8191   l_current_role_name :=     WF_ENGINE.GetItemAttrText(itemtype,
8192                               itemkey,
8193                               'DOCUMENT_APPROVER');
8194 
8195   IF  l_forward_role_name is NULL or l_forward_role_name=' ' THEN
8196             resultout := wf_engine.eng_completed||':'||'N';
8197   ELSE
8198         IF(nvl(l_forward_role_name,'') = nvl(l_current_role_name,'')) then
8199             resultout := wf_engine.eng_completed||':'||'Y';
8200        /* -- Bug 5037108 starts
8201              BEGIN
8202                 SELECT invoice_id
8203                 INTO l_temp_invoice_id
8204                 FROM ap_invoices
8205                 WHERE invoice_id = substr(itemkey, 1, instr(itemkey,'_')-1)
8206   	            AND  wfapproval_status <> 'MANUALLY APPROVED'
8207                 FOR UPDATE NOWAIT;
8208 
8209               EXCEPTION
8210                WHEN OTHERS THEN
8211                 l_debug_info := 'Invoice is in lock. Please try later';
8212                 FND_MESSAGE.Set_Name('SQLAP', 'AP_CARD_VERIFY_LOCK_FAILED');
8213                 l_error_message := FND_MESSAGE.Get;
8214                 APP_EXCEPTION.RAISE_EXCEPTION;
8215               END;
8216         -- Bug 5037108 Ends
8217 
8218                 UPDATE AP_INVOICES
8219                 SET wfapproval_status = 'REJECTED'
8220                 WHERE invoice_id = substr(itemkey, 1, instr(itemkey,'_')-1)
8221                 AND wfapproval_status <> 'MANUALLY APPROVED'; */
8222         ELSE
8223               resultout := wf_engine.eng_completed||':'||'N';
8224         END If;
8225 
8226   END IF;
8227 
8228 EXCEPTION
8229 
8230 WHEN FND_API.G_EXC_ERROR
8231         THEN
8232           WF_CORE.CONTEXT('APINV','SELECT_APPROVER',itemtype, itemkey,
8233                                   to_char(actid), funcmode);
8234           RAISE;
8235 WHEN FND_API.G_EXC_UNEXPECTED_ERROR
8236         THEN
8237           WF_CORE.CONTEXT('APINV','SELECT_APPROVER',itemtype, itemkey,
8238                                   to_char(actid), funcmode);
8239           RAISE;
8240 
8241 WHEN OTHERS
8242         THEN
8243           WF_CORE.CONTEXT('APINV','SELECT_APPROVER',itemtype, itemkey,
8244                                   to_char(actid), funcmode);
8245           RAISE;
8246 
8247 END;
8248 /*
8249 PROCEDURE NAME : notification_handler_lines
8250 PROJECT : R12 - Workflow Delegation enhancement
8251 PURPOSE : This procedure is attached to the notifications in the lines Approval
8252           in order to populate the forward to role attribute during the delegation
8253           and to create history entries.Also the throws error when the
8254           notification ownership is transfered.
8255 */
8256 PROCEDURE notification_handler_lines( itemtype  IN VARCHAR2,
8257                                 itemkey   IN VARCHAR2,
8258                                 actid     IN NUMBER,
8259                                 funcmode  IN VARCHAR2,
8260                                 resultout OUT NOCOPY VARCHAR2 ) IS
8261 l_invoice_id               NUMBER(15);
8262 l_iteration                NUMBER;
8263 l_status	                 VARCHAR2(50);
8264 l_response                 VARCHAR2(50);
8265 l_nid                      number;
8266 l_forward_to_person_id     number;
8267 l_result                   varchar2(100);
8268 l_orig_system              WF_ROLES.ORIG_SYSTEM%TYPE;
8269 l_orig_sys_id              WF_ROLES.ORIG_SYSTEM_ID%TYPE;
8270 l_role                     VARCHAR2(50);
8271 l_role_display             VARCHAR2(150);
8272 l_org_id                   NUMBER(15);
8273 l_name                     wf_users.name%TYPE; --bug 8620671
8274 l_display_name             VARCHAR2(150);
8275 l_forward_to_user_id       WF_ROLES.ORIG_SYSTEM_ID%TYPE;
8276 l_esc_approver             AME_UTIL.approverRecord;
8277 l_rec_role                 VARCHAR2(50);
8278 l_comments                 VARCHAR2(240);
8279 l_hist_id                  NUMBER(15);
8280 l_amount                   ap_invoices_all.invoice_amount%TYPE;
8281 l_user_id                  NUMBER(15);
8282 l_login_id                 NUMBER(15);
8283 l_hist_rec                 AP_INV_APRVL_HIST%ROWTYPE;
8284 l_notf_iteration           NUMBER;
8285 l_line_number              ap_invoice_lines_all.line_number%TYPE;
8286 l_line_amount              ap_invoice_lines_all.amount%TYPE;
8287 l_role_name                varchar2(50);
8288 l_esc_flag                 varchar2(1);
8289 l_esc_role                 varchar2(50);
8290 
8291 
8292 CURSOR c_get_user ( p_rec_role IN VARCHAR2 ) IS
8293   SELECT user_id,employee_id
8294   FROM   FND_USER
8295   WHERE  USER_NAME=p_rec_role;
8296 
8297 CURSOR c_get_response ( p_invoice_id IN NUMBER) IS
8298   Select response
8299   From   ap_inv_aprvl_hist
8300   WHERE  approval_history_id=(select max(approval_history_id)from ap_inv_aprvl_hist
8301 		                         where invoice_id= p_invoice_id);
8302 
8303 
8304    CURSOR   Items_Cur(itemkey IN VARCHAR2) IS
8305    SELECT  al.line_number, al.amount
8306    FROM   AP_APINV_APPROVERS invap, AP_INVOICE_LINES_ALL al
8307    WHERE  invap.Notification_Key = itemkey
8308    AND    al.line_number = invap.line_number
8309    AND    al.invoice_id  = invap.invoice_id;
8310 BEGIN
8311     l_nid := WF_ENGINE.context_nid;
8312     l_invoice_id := WF_ENGINE.GetItemAttrNumber(itemtype,
8313                                                 itemkey,
8314                                                 'INVOICE_ID');
8315 
8316     l_org_id := WF_ENGINE.GETITEMATTRNumber(  itemtype,
8317                                               itemkey,
8318                                               'ORG_ID');
8319 
8320     l_iteration := WF_ENGINE.GetItemAttrNumber( itemtype,
8321                                                 itemkey,
8322                                                 'ITERATION');
8323     l_comments := WF_ENGINE.GetItemAttrText(itemtype,
8324                                             itemkey,
8325                                             'WF_NOTE');
8326     l_notf_iteration := WF_ENGINE.GetItemAttrnumber(itemtype,
8327                                                     itemkey,
8328                                                     'NOTF_ITERATION');
8329     l_esc_flag := WF_ENGINE.GetItemAttrText(itemtype,
8330                                             itemkey,
8331                                             'ESCALATED');
8332 
8333 -- getting approver in the ROLE_NAME and setting in the ROLE_ACTUAL attribute
8334 -- on case of escalated notifications getting the approver from ESC_ROLE_NAME and setting it to ESC_ROLE_ACTUAL
8335 -- So even if the notification is delegated the ame will be updated in the for the actual approver
8336 -- the further level of approval will be done by the owners supervisors and also the same
8337 -- notification won't be sent to  this approver again
8338         IF nvl(l_esc_flag,'N') = 'N' THEN
8339           l_role_name :=WF_ENGINE.GetItemAttrText(itemtype,
8340                                     itemkey,
8341                                     'ROLE_NAME');
8342 
8343           WF_ENGINE.SetItemAttrText(itemtype,
8344                                     itemkey,
8345                                     'ROLE_ACTUAL',
8346                                     l_role_name);
8347 
8348         elsif nvl(l_esc_flag,'N') = 'Y' THEN
8349           l_esc_role :=WF_ENGINE.GetItemAttrText( itemtype,
8350                                                   itemkey,
8351                                                   'ESC_ROLE_NAME');
8352           WF_ENGINE.SetItemAttrText( itemtype,
8353                                      itemkey,
8354                                      'ESC_ROLE_ACTUAL',
8355                                      l_esc_role);
8356 
8357         end if ;
8358 -- end
8359   IF (funcmode ='FORWARD') then
8360          l_rec_role :=WF_ENGINE.context_text;
8361          l_status:='DELEGATED';
8362 
8363          --fnd_client_info.set_org_context(l_org_id);
8364           --Now set the environment
8365          MO_GLOBAL.INIT ('SQLAP');
8366          MO_GLOBAL.set_policy_context('S',l_org_id);
8367 
8368 
8369          /*insert_history(      l_invoice_id,
8370                               l_iteration,
8371                               l_org_id,
8372                               l_status);*/
8373 
8374 
8375 
8376           OPEN  c_get_user ( l_rec_role);
8377           FETCH c_get_user INTO l_forward_to_user_id,l_forward_to_person_id;
8378           CLOSE c_get_user;
8379 
8380           IF l_forward_to_person_id is not NULL then
8381                      l_orig_system := 'PER';
8382                      l_orig_sys_id := l_forward_to_person_id;
8383           ELSE
8384                      l_orig_system := 'FND_USR';
8385                      l_orig_sys_id := l_forward_to_user_id;
8386           END IF;
8387           WF_DIRECTORY.GetRoleName( l_orig_system,
8388                                     l_orig_sys_id,
8389                                     l_role,
8390                                     l_role_display);
8391 
8392           WF_DIRECTORY.GetUserName( l_orig_system,
8393                                     l_orig_sys_id,
8394                                     l_name,
8395                                     l_display_name);
8396 
8397           WF_ENGINE.SetItemAttrText(itemtype,
8398                                     itemkey,
8399                                     'FORWARD_ROLE_LINES',
8400                                     l_role);
8401 
8402 
8403      IF nvl(l_esc_flag,'N') = 'N' THEN
8404 
8405 
8406           WF_ENGINE.SetItemAttrText(itemtype,
8407                                     itemkey,
8408                                     'ROLE_NAME',
8409                                     l_role);
8410           WF_ENGINE.SetItemAttrText( itemtype,
8411                                      itemkey,
8412                                      'APPROVER_NAME',
8413                                      l_display_name);
8414 
8415            WF_ENGINE.SetItemAttrNumber(itemtype,
8416                                        itemkey,
8417                                        'APPROVER_ID',
8418                                        l_orig_sys_id);
8419        /* WF_ENGINE.SetItemAttrText( itemtype,
8420                                      itemkey,
8421                                      'ORIG_SYSTEM',
8422                                      l_orig_system);     */
8423 
8424         else
8425           WF_ENGINE.SetItemAttrText(itemtype,
8426                                     itemkey,
8427                                     'ESC_ROLE_NAME',
8428                                     l_role);
8429           WF_ENGINE.SetItemAttrText( itemtype,
8430                                      itemkey,
8431                                      'ESC_APPROVER_NAME',
8432                                      l_display_name);
8433 
8434            WF_ENGINE.SetItemAttrNumber(itemtype,
8435                                        itemkey,
8436                                        'ESC_APPROVER_ID',
8437                                        l_orig_sys_id);
8438 
8439         end if ;
8440 
8441           l_comments                    := nvl(l_comments,'None');
8442           l_comments                    :='To: '||l_display_name||' '||'    With Comment:'||l_comments;
8443 
8444           l_hist_rec.HISTORY_TYPE       := 'LINESAPPROVAL';
8445           l_hist_rec.INVOICE_ID         := l_invoice_id;
8446           l_hist_rec.ITERATION          := l_iteration;
8447           l_hist_rec.RESPONSE           := l_status;
8448           l_hist_rec.APPROVER_ID        := l_orig_sys_id;
8449           l_hist_rec.APPROVER_NAME      := l_display_name;
8450           l_hist_rec.CREATED_BY         := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
8451           l_hist_rec.CREATION_DATE      := sysdate;
8452           l_hist_rec.LAST_UPDATE_DATE   := sysdate;
8453           l_hist_rec.LAST_UPDATED_BY    := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
8454           l_hist_rec.LAST_UPDATE_LOGIN  := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),-1);
8455           l_hist_rec.ORG_ID             := l_org_id;
8456           l_hist_rec.AMOUNT_APPROVED    := 0;
8457           l_hist_rec.NOTIFICATION_ORDER := l_notf_iteration;
8458           l_hist_rec.APPROVER_COMMENTS  := l_comments;
8459           /*l_hist_rec.line_number        := l_line_number;
8460           l_hist_rec.AMOUNT_APPROVED    := l_line_amount;
8461 
8462           insert_history_table(p_hist_rec => l_hist_rec);*/
8463 
8464            OPEN Items_Cur(itemkey);
8465            LOOP
8466 
8467               FETCH Items_Cur INTO l_line_number, l_line_amount;
8468               EXIT WHEN Items_Cur%NOTFOUND OR Items_Cur%NOTFOUND IS NULL;
8469 
8470 
8471               l_hist_rec.line_number   := l_line_number;
8472               l_hist_rec.AMOUNT_APPROVED   := l_line_amount;
8473 
8474               insert_history_table(p_hist_rec => l_hist_rec);
8475            END LOOP;
8476            CLOSE Items_Cur;
8477            /*--Bug6406901, added the approver_name to the update cmd to populate correct value in AP_INV_APRVL_HLIST
8478            UPDATE AP_INV_APRVL_HIST
8479            set   APPROVER_COMMENTS = l_comments, approver_name = l_display_name
8480            WHERE APPROVAL_HISTORY_ID =(select max(APPROVAL_HISTORY_ID) from AP_INV_APRVL_HIST
8481                                         where invoice_id=l_invoice_id);*/
8482            resultout := wf_engine.eng_completed || ':' || wf_engine.eng_null;
8483            return;
8484    End if;
8485 
8486       /* BEGIN
8487            Select response into l_response
8488            From   ap_inv_aprvl_hist
8489            WHERE  approval_history_id=(select max(approval_history_id)from ap_inv_aprvl_hist
8490     			                             where invoice_id=l_invoice_id);
8491        EXCEPTION
8492          WHEN OTHERS THEN
8493           NULL;
8494        END; */
8495 
8496         /*  OPEN  c_get_response ( l_invoice_id);
8497           FETCH c_get_response INTO l_response;
8498           CLOSE c_get_response;
8499 
8500 	IF (funcmode = 'RESPOND' and l_response='DELEGATED') then
8501             l_result := wf_notification.GetAttrText(l_nid, 'RESULT');
8502 
8503             If (l_result='APPROVED') then
8504             l_result:='WFAPPROVED';
8505             End IF;
8506 
8507 
8508             insert_history(l_invoice_id,
8509                         l_iteration,
8510                         l_org_id,
8511                         l_result);
8512 
8513 
8514           WF_ENGINE.SetItemAttrText(  itemtype,
8515                                       itemkey,
8516                                       'FORWARD_ROLE',
8517                                       NULL);
8518 
8519             resultout := wf_engine.eng_completed || ':' || wf_engine.eng_null;
8520             return;
8521   End if;*/
8522   -- Don't allow transfer
8523   if ( funcmode = 'TRANSFER' ) then
8524        --resultout := 'ERROR:WFSRV_NO_DELEGATE';
8525 	TRANSFER_NOTIFICATION(
8526     itemtype    => itemtype,
8527     itemkey =>     itemkey,
8528     actid   => actid,
8529     funcmode => funcmode,
8530     approvalType => 'LINESAPPROVAL',
8531     resultout => resultout );
8532        return;
8533   end if;
8534 
8535 return;
8536 EXCEPTION
8537 
8538 WHEN OTHERS
8539    THEN
8540         WF_CORE.CONTEXT('AP_WF',itemtype, itemkey, to_char(actid), funcmode);
8541         RAISE;
8542 
8543 END;
8544 /*
8545 PROCEDURE NAME : forward_check_lines
8546 PROJECT : R12 - Workflow Delegation enhancement
8547 PURPOSE : This procedure is attached to the check_forward_lines node.
8548           This procedure checks before escalation whether the notification
8549           is forwarded when there is a time out. If the result is N
8550           (i.e when the notification is not a forwarded one) ,
8551           then the escalation notification is sent.If the result is Y, then the
8552           document is rejected.
8553 */
8554 PROCEDURE forward_check_lines(itemtype   IN VARCHAR2,
8555                         itemkey    IN VARCHAR2,
8556                         actid      IN NUMBER,
8557                         funcmode   IN VARCHAR2,
8558                         resultout  OUT NOCOPY VARCHAR2 ) IS
8559 l_forward_role_name   varchar2(50);
8560 l_current_role_name   varchar2(50);
8561 l_temp_invoice_id     NUMBER(15);
8562 l_error_message       VARCHAR2(2000);
8563 l_debug_info 	        VARCHAR2(50);
8564 
8565 BEGIN
8566   l_forward_role_name :=     WF_ENGINE.GetItemAttrText(itemtype,
8567                               itemkey,
8568                               'FORWARD_ROLE_LINES');
8569   l_current_role_name :=     WF_ENGINE.GetItemAttrText(itemtype,
8570                               itemkey,
8571                               'ROLE_NAME');
8572 
8573   IF  l_forward_role_name is NULL or l_forward_role_name=' ' THEN
8574             resultout := wf_engine.eng_completed||':'||'N';
8575   ELSE
8576         IF(nvl(l_forward_role_name,'') = nvl(l_current_role_name,'')) then
8577             resultout := wf_engine.eng_completed||':'||'Y';
8578        /* -- Bug 5037108 starts
8579              BEGIN
8580                 SELECT invoice_id
8581                 INTO l_temp_invoice_id
8582                 FROM ap_invoices
8583                 WHERE invoice_id = substr(itemkey, 1, instr(itemkey,'_')-1)
8584   	            AND  wfapproval_status <> 'MANUALLY APPROVED'
8585                 FOR UPDATE NOWAIT;
8586 
8587               EXCEPTION
8588                WHEN OTHERS THEN
8589                 l_debug_info := 'Invoice is in lock. Please try later';
8590                 FND_MESSAGE.Set_Name('SQLAP', 'AP_CARD_VERIFY_LOCK_FAILED');
8591                 l_error_message := FND_MESSAGE.Get;
8592                 APP_EXCEPTION.RAISE_EXCEPTION;
8593               END;
8594         -- Bug 5037108 Ends
8595 
8596                 UPDATE AP_INVOICES
8597                 SET wfapproval_status = 'REJECTED'
8598                 WHERE invoice_id = substr(itemkey, 1, instr(itemkey,'_')-1)
8599                 AND wfapproval_status <> 'MANUALLY APPROVED'; */
8600         ELSE
8601               resultout := wf_engine.eng_completed||':'||'N';
8602         END If;
8603 
8604   END IF;
8605 
8606 EXCEPTION
8607 
8608 WHEN FND_API.G_EXC_ERROR
8609         THEN
8610           WF_CORE.CONTEXT('APINV','SELECT_APPROVER',itemtype, itemkey,
8611                                   to_char(actid), funcmode);
8612           RAISE;
8613 WHEN FND_API.G_EXC_UNEXPECTED_ERROR
8614         THEN
8615           WF_CORE.CONTEXT('APINV','SELECT_APPROVER',itemtype, itemkey,
8616                                   to_char(actid), funcmode);
8617           RAISE;
8618 
8619 WHEN OTHERS
8620         THEN
8621           WF_CORE.CONTEXT('APINV','SELECT_APPROVER',itemtype, itemkey,
8622                                   to_char(actid), funcmode);
8623           RAISE;
8624 
8625 END;
8626 
8627 ------------------------------------------------------------------------------
8628 ----------------------------- Post_Notif_Handler -----------------------------
8629 ------------------------------------------------------------------------------
8630 /* Process post-notification activity to set the correct user context if
8631  * invoice hold is being released from email notification
8632  *
8633  * Parameters : As per standard API format for Workflow Engine as documented
8634  *              in Workflow Developer's Guide
8635  *
8636  * Logic : As per Workflow API Reference, the global engine variable
8637  *         WF_ENGINE.context_user will be set to 'email:<email_address>'
8638  *         if user is taking the RESPOND action through e-mail on his or
8639  *         her own behalf. The code has been written based on this to :
8640  *         1) Check whether WF_ENGINE.context_user is set to 'email:...'
8641  *         2) If yes, check how many times the email address exists in
8642  *            wf_roles
8643  *         3) If it does not exist or exists more than once, then do nothing.
8644  *            This will let the context to be set as per WF Engine ( which
8645  *            works under SYSADMIN context ). If the role does not exist then
8646  *            we have no way of setting the context and if it exists multiple
8647  *            times then we cannot set the context based on one particular
8648  *            occurrence of the record and in that case it will be better to
8649  *            let the context be as it is
8650  *         4) If the role exists only once, then check whether the context is
8651  *            already set to the same user.
8652  *         5) If so, then do nothing otherwise call FND_GLOBAL.Apps_Initialize
8653  *
8654  * History :
8655  * Date        Bug        User    Action
8656  * ----------------------------------------------------------------------------
8657  * 28-12-2010  9749367    ANARUN  Created
8658 */
8659 PROCEDURE Post_Notif_Handler(itemtype   IN VARCHAR2,
8660                              itemkey    IN VARCHAR2,
8661                              actid      IN NUMBER,
8662                              funcmode   IN VARCHAR2,
8663                              resultout  OUT NOCOPY VARCHAR2 ) IS
8664    l_user_id         FND_USER.user_id%TYPE;
8665    l_user_name       wf_users.name%TYPE;
8666    l_user_role_count NUMBER DEFAULT 0;
8667    l_debug_info      VARCHAR2(2000);
8668    l_api_name        CONSTANT VARCHAR2(200) := 'Post_Notif_Handler';
8669 BEGIN
8670 
8671    l_debug_info := 'Called with itemtype = ' || itemtype ||
8672                     ', itemkey = '           || itemkey  ||
8673                     ', actid = '             || actid    ||
8674                     ', funcmode = '          || funcmode ;
8675    IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL )
8676    THEN
8677        FND_LOG.string( G_LEVEL_STATEMENT, G_MODULE_NAME || l_api_name, l_debug_info);
8678    END IF;
8679 
8680    IF ( funcmode = 'RESPOND' ) then
8681        l_debug_info := 'Entered the IF block. FND_GLOBAL.user_id = ' || FND_GLOBAL.user_id ||
8682                        ', WF_ENGINE.context_user = ' || WF_ENGINE.context_user ;
8683        IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL )
8684        THEN
8685            FND_LOG.string( G_LEVEL_STATEMENT, G_MODULE_NAME || l_api_name, l_debug_info);
8686        END IF;
8687 
8688        IF SUBSTR( WF_ENGINE.context_user,  1,  6) = 'email:'
8689        THEN
8690            BEGIN
8691               SELECT COUNT( name )
8692                 INTO l_user_role_count
8693                 FROM wf_roles
8694                WHERE UPPER( email_address ) = UPPER( SUBSTR( WF_ENGINE.context_user, 7 ) )
8695                  AND orig_system = 'PER'
8696                  AND status      = 'ACTIVE' ;
8697            EXCEPTION
8698               WHEN OTHERS THEN
8699                 NULL ;
8700            END ;
8701 
8702            l_debug_info := 'l_user_role_count = ' || l_user_role_count ;
8703            IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL )
8704            THEN
8705                FND_LOG.string( G_LEVEL_STATEMENT, G_MODULE_NAME || l_api_name, l_debug_info);
8706            END IF;
8707 
8708            IF l_user_role_count = 1
8709            THEN
8710               SELECT name
8711                 INTO l_user_name
8712                 FROM wf_roles
8713                WHERE UPPER( email_address )  = UPPER( SUBSTR( WF_ENGINE.context_user, 7 ) )
8714                  AND orig_system = 'PER'
8715                  AND status      = 'ACTIVE' ;
8716 
8717               l_debug_info := 'l_user_name = '            || l_user_name         ||
8718                               ', fnd_global.user_name = ' || fnd_global.user_name ;
8719               IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL )
8720               THEN
8721                   FND_LOG.string( G_LEVEL_STATEMENT, G_MODULE_NAME || l_api_name, l_debug_info);
8722               END IF;
8723 
8724               IF l_user_name <> fnd_global.user_name
8725               THEN
8726                  -- check if the role is a valid FND user
8727                  SELECT user_id
8728                    INTO l_user_id
8729                    FROM fnd_user
8730                   WHERE user_name = l_user_name;
8731 
8732                  l_debug_info := 'l_user_id = ' || l_user_id ;
8733                  IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL )
8734                  THEN
8735                      FND_LOG.string( G_LEVEL_STATEMENT, G_MODULE_NAME || l_api_name, l_debug_info);
8736                  END IF;
8737 
8738                  FND_GLOBAL.Apps_Initialize( l_user_id, null, null, null, null );
8739 
8740                  l_debug_info := 'After call, FND_GLOBAL.user_id = ' || FND_GLOBAL.user_id ;
8741                  IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL )
8742                  THEN
8743                      FND_LOG.string( G_LEVEL_STATEMENT, G_MODULE_NAME || l_api_name, l_debug_info);
8744                  END IF;
8745               END IF ;
8746            END IF ;
8747        END IF ;
8748        resultout := 'COMPLETE';
8749        RETURN;
8750    END IF;
8751 EXCEPTION
8752    WHEN OTHERS THEN
8753       l_debug_info := SUBSTR( SQLERRM, 1, 2000 ) ;
8754       IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL )
8755       THEN
8756          FND_LOG.string( G_LEVEL_STATEMENT, G_MODULE_NAME || l_api_name, l_debug_info);
8757       END IF;
8758 
8759 END Post_Notif_Handler;
8760 
8761 /* Bug#14804508 - START - Added new procedure to execute the dynamic query for report */
8762 
8763 procedure Dynamic_sql_execute(p_query IN VARCHAR2, P_conc_id IN NUMBER) IS
8764 
8765   l_api_name varchar2(100) := 'DYNAMIC_SQL_EXECUTE';
8766   l_debug_info varchar2(200);
8767 
8768   TYPE REFCURSOR is REF CURSOR;
8769   Apporval_invoices REFCURSOR;
8770   l_inv_id number;
8771 
8772 BEGIN
8773 
8774    l_debug_info := 'inside dynamic_sql_execute procedure '||P_conc_id;
8775    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8776           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
8777                          l_api_name,l_debug_info);
8778    END IF;
8779      BEGIN
8780 
8781         OPEN Apporval_invoices for p_query;
8782         loop
8783             FETCH Apporval_invoices INTO l_inv_id;
8784             EXIT WHEN Apporval_invoices%NOTFOUND;
8785             update ap_invoices
8786             set validation_request_id = P_conc_id
8787             where invoice_id = l_inv_id;
8788         end loop;
8789         close Apporval_invoices;
8790 
8791 
8792 
8793      EXCEPTION
8794          WHEN OTHERS THEN
8795 fnd_file.put_line(FND_FILE.LOG,'Error Occurred in dynamic sql execution  '||SQLERRM);
8796      APP_EXCEPTION.RAISE_EXCEPTION;
8797     END;
8798 END Dynamic_sql_execute;
8799 
8800 
8801 /* Bug 16069896 - Start Workflow Transfer Enhancement */
8802 
8803 PROCEDURE TRANSFER_NOTIFICATION( --Proc name
8804     itemtype     IN VARCHAR2,              --Get itemType
8805     itemkey      IN VARCHAR2,              --Get itemkey
8806     actid        IN NUMBER,                --Get actid
8807     funcmode     IN VARCHAR2,              --Get funcmode
8808     approvalType IN VARCHAR2,              --Either DOCUMENTAPPROVAL or LINESAPPROVAL
8809     resultout OUT NOCOPY VARCHAR2 )        --Set resultout
8810 IS
8811   l_transfer_to_role   VARCHAR2(50);                                          --The variable stores the Forwardee role
8812   l_original_recipient VARCHAR2(50);                                          -- The variable stores the Forwarder role
8813   NxtApprover_rec ame_util.approverRecord2;                                   --The original approver record
8814   Fwd_Rec Ame_Util.Approverrecord2;                                           --The forwardee record
8815   G_MODULE_NAME           CONSTANT VARCHAR2(100) := 'TRANSFER_NOTIFICATION';   -- for fnd logs
8816   l_invoice_id            NUMBER(15);                                                --Variable used to store the invoice id
8817   l_org_id                NUMBER(15);                                                --Variable used to store the org id
8818   l_fwd_role              VARCHAR2(50);                                              -- Variable stored to use the forward role
8819   l_doc_approver          VARCHAR2(50);                                              --the variable used to store the doc approver
8820   l_line_approver         VARCHAR2(50);                                              -- the variable used to store the lines approver.
8821   l_role_actual           VARCHAR2(50);                                              -- the variable used to store the role actual
8822   l_forward_to_user_id       WF_ROLES.ORIG_SYSTEM_ID%TYPE;
8823   l_rec_role                 VARCHAR2(50);
8824   l_forward_to_person_id     number;
8825   l_orig_system              WF_ROLES.ORIG_SYSTEM%TYPE;
8826   l_orig_sys_id              WF_ROLES.ORIG_SYSTEM_ID%TYPE;
8827   l_notification_cxt   VARCHAR2(50);
8828   l_notification_key   VARCHAR2(50);
8829   l_forward_role_lines VARCHAR2(50);
8830   l_approver_name      VARCHAR2(50);
8831   l_disp_not_receiver  VARCHAR2(50);
8832   l_iteration          NUMBER;
8833   l_notf_iteration     NUMBER;
8834   l_line_number   ap_invoice_lines_all.line_number%TYPE;
8835   l_line_amount   ap_invoice_lines_all.amount%TYPE;
8836   l_invoice_key   VARCHAR2(50);
8837   l_name          AP_APINV_APPROVERS.ROLE_NAME%TYPE;
8838   l_item_class    AP_APINV_APPROVERS.ITEM_CLASS%TYPE;
8839   l_item_id       AP_APINV_APPROVERS.ITEM_ID%TYPE;
8840   approver ame_util.approverRecord2;
8841   l_hist_rec AP_INV_APRVL_HIST%ROWTYPE;
8842   --l_status	VARCHAR2(50);
8843   l_comments                 VARCHAR2(240);
8844   l_display_name		VARCHAR2(150);
8845   l_invoice_total		NUMBER;
8846 
8847   --Cursor Declaration
8848   CURSOR   Items_Cur(itemkey IN VARCHAR2) IS
8849    SELECT invap.Item_Class, invap.Item_Id, invap.Role_Name,
8850           invap.Invoice_Key, al.line_number, al.amount
8851    FROM   AP_APINV_APPROVERS invap, AP_INVOICE_LINES_ALL al
8852    WHERE  invap.Notification_Key = itemkey
8853    AND    al.line_number = invap.line_number
8854    AND    al.invoice_id  = invap.invoice_id;
8855 
8856   CURSOR c_get_user ( p_rec_role IN VARCHAR2 ) IS
8857   SELECT user_id,employee_id
8858   FROM   FND_USER
8859   WHERE  USER_NAME=p_rec_role;
8860 
8861 BEGIN
8862 --Getting data from WF Engine
8863 l_invoice_id         := WF_ENGINE.GetItemAttrNumber(itemtype, itemkey, 'INVOICE_ID');
8864 l_org_id             := WF_ENGINE.GETITEMATTRNumber( itemtype, itemkey, 'ORG_ID');
8865 l_fwd_role           := WF_ENGINE.GetItemAttrText(itemtype, itemkey, 'FORWARD_ROLE');
8866 l_doc_approver       := WF_ENGINE.GetItemAttrText(itemtype, itemkey, 'DOCUMENT_APPROVER');
8867 l_line_approver      := WF_ENGINE.GetItemAttrText(itemtype, itemkey, 'ROLE_NAME');
8868 l_role_actual        := WF_ENGINE.GetItemAttrText(itemtype, itemkey, 'ROLE_ACTUAL');
8869 l_notification_cxt   := WF_ENGINE.GetItemAttrText(itemtype, itemkey, 'NOTF_CONTEXT');
8870 l_notification_key   := WF_ENGINE.GetItemAttrText(itemtype, itemkey, 'NOTIFICATION_KEY');
8871 l_forward_role_lines := WF_ENGINE.GetItemAttrText(itemtype, itemkey, 'FORWARD_ROLE_LINES');
8872 l_approver_name      := WF_ENGINE.GetItemAttrText(itemtype, itemkey, 'APPROVER_NAME');
8873 l_disp_not_receiver  := WF_ENGINE.GetItemAttrText(itemtype, itemkey, 'DISP_NOT_RECEIVER');
8874 l_iteration          := WF_ENGINE.GetItemAttrText(itemtype, itemkey, 'ITERATION');
8875 l_notf_iteration     := WF_ENGINE.GetItemAttrText(itemtype, itemkey, 'NOTF_ITERATION');
8876 l_comments := WF_ENGINE.GetItemAttrText(itemtype,itemkey,'WF_NOTE');
8877 
8878 
8879 
8880   --Defaulting the org id
8881   MO_GLOBAL.INIT ('SQLAP');
8882   MO_GLOBAL.set_policy_context('S',l_org_id);
8883   --Checking if the mode is TRANSFER
8884   IF ( funcmode                      = 'TRANSFER' ) THEN
8885     l_transfer_to_role              :=WF_ENGINE.context_new_role;            --Obtaining the forwardees role
8886     l_original_recipient            := WF_ENGINE.context_original_recipient; -- Obtaining the forwarder's role
8887 	l_rec_role			 := WF_ENGINE.context_text;
8888     fwd_rec.name                    := l_transfer_to_role;                   --Setting the name of the forwardee record to the forwardee role
8889     NxtApprover_rec.name            :=l_original_recipient;                  --Setting the name of the forwarder record to the forwarder role
8890     NxtApprover_rec.approval_status := ame_util.ForwardStatus;               --Setting the status as ame_util.ForwardStatus which is 'FORWARD'
8891     WF_ENGINE.SetItemAttrText(itemtype, itemkey, 'ROLE_ACTUAL', l_transfer_to_role); --Setting the role_actual attribute to forwardee role
8892      Wf_Engine.Setitemattrtext(Itemtype, Itemkey, 'DOCUMENT_APPROVER', L_Transfer_To_Role); --Setting the DOCUMENT_APPROVER attribute to forwardee role
8893     WF_ENGINE.SetItemAttrText(itemtype, itemkey, 'FORWARD_ROLE', l_transfer_to_role);      --Setting the FORWARD_ROLE attribute to forwardee role
8894 
8895 		  l_hist_rec.INVOICE_ID         := l_invoice_id;
8896           l_hist_rec.ITERATION          := l_iteration;
8897           l_hist_rec.RESPONSE           := 'TRANSFERRED';
8898 
8899           l_hist_rec.APPROVER_ID        := l_orig_sys_id;
8900           l_hist_rec.CREATED_BY         := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
8901           l_hist_rec.CREATION_DATE      := sysdate;
8902           l_hist_rec.LAST_UPDATE_DATE   := sysdate;
8903           l_hist_rec.LAST_UPDATED_BY    := nvl(TO_NUMBER(FND_PROFILE.VALUE('USER_ID')),-1);
8904           l_hist_rec.LAST_UPDATE_LOGIN  := nvl(TO_NUMBER(FND_PROFILE.VALUE('LOGIN_ID')),-1);
8905           l_hist_rec.ORG_ID             := l_org_id;
8906           l_hist_rec.NOTIFICATION_ORDER := l_notf_iteration;
8907 
8908 
8909     IF (approvalType = 'DOCUMENTAPPROVAL') THEN
8910       ame_api2.updateApprovalStatus(
8911       applicationIdIn => 200 ,
8912       transactionTypeIn => 'APINV' ,
8913       transactionIdIn => TO_CHAR(l_invoice_id) ,
8914       approverIn => NxtApprover_rec ,
8915       Forwardeein => Fwd_Rec );
8916 
8917 	  l_hist_rec.HISTORY_TYPE       := 'DOCUMENTAPPROVAL';
8918 
8919 	  OPEN  c_get_user ( l_rec_role);
8920       FETCH c_get_user INTO l_forward_to_user_id,l_forward_to_person_id;
8921       CLOSE c_get_user;
8922 
8923       IF l_forward_to_person_id is not NULL then
8924                   l_orig_system := 'PER';
8925                   l_orig_sys_id := l_forward_to_person_id;
8926       ELSE
8927                    l_orig_system := 'FND_USR';
8928                    l_orig_sys_id := l_forward_to_user_id;
8929       END IF;
8930       WF_DIRECTORY.GetUserName( l_orig_system, l_orig_sys_id, l_name,l_display_name);
8931    l_invoice_total := WF_ENGINE.GETITEMATTRNumber(itemtype,
8932                    itemkey,
8933                    'INVOICE_TOTAL');
8934    l_hist_rec.APPROVER_NAME      := l_display_name;
8935    l_comments                    := nvl(l_comments,'None');
8936    l_comments                    :='To: '||l_display_name||' '||'    With Comment:'||l_comments;
8937    l_hist_rec.APPROVER_COMMENTS  := l_comments;
8938    AP_WORKFLOW_PKG.insert_history_table(p_hist_rec => l_hist_rec);
8939     Elsif (Approvaltype = 'LINESAPPROVAL') Then
8940 
8941 	l_hist_rec.HISTORY_TYPE       := 'LINESAPPROVAL';
8942 
8943       Wf_Engine.Setitemattrtext(Itemtype, Itemkey, 'ROLE_NAME', L_Transfer_To_Role);          --Setting the ROLE_NAME attribute to forwardee role
8944       Wf_Engine.Setitemattrtext(Itemtype, Itemkey, 'FORWARD_ROLE_LINES', L_Transfer_To_Role); --Setting the FORWARD_ROLE_LINES attribute to forwardee role
8945       WF_ENGINE.SetItemAttrText(itemtype, itemkey, 'DOCUMENT_APPROVER', l_transfer_to_role);  --Setting the DOCUMENT_APPROVER attribute to forwardee role
8946       WF_ENGINE.SetItemAttrText(itemtype, itemkey, 'FORWARD_ROLE', l_transfer_to_role);       --Setting the FORWARD_ROLE attribute to forwardee role
8947 
8948       OPEN Items_Cur(itemkey);
8949    LOOP
8950 
8951       FETCH Items_Cur INTO l_item_class, l_item_id, l_name,
8952                                    l_invoice_key,l_line_number, l_line_amount;
8953       EXIT WHEN Items_Cur%NOTFOUND OR Items_Cur%NOTFOUND IS NULL;
8954 
8955 	  NxtApprover_rec.item_class := l_item_class;
8956       NxtApprover_rec.item_id := l_item_id;
8957       fwd_rec.item_class := l_item_class;
8958       fwd_rec.item_id := l_item_id;
8959 
8960 
8961 	  approver.name := l_name;
8962 	  approver.item_class := l_item_class ;
8963       approver.item_id := l_item_id ;
8964       approver.approval_status := AME_UTIL.forwardStatus;
8965       approver.action_type_id :=actid ;
8966 
8967 	  ame_approver_type_pkg.getOrigSystemIdAndDisplayName(nameIn =>approver.name,
8968                                            origSystemOut => approver.orig_system,
8969                                            origSystemIdOut => approver.orig_system_id,
8970                                            displayNameOut => approver.display_name);
8971 
8972       AME_API2.updateApprovalStatus2(applicationIdIn => 200,
8973                            transactionIdIn     => to_char(l_invoice_id),
8974                            approvalStatusIn    => AME_UTIL.forwardStatus,
8975                            approverNameIn  => l_name,
8976                            transactionTypeIn =>  'APINV',
8977                            itemClassIn     => l_item_class,
8978                            itemIdIn        => l_item_id,
8979                            forwardeeIn      => fwd_rec,
8980                            updateItemIn     => true);
8981 
8982 	  OPEN  c_get_user ( l_rec_role);
8983       FETCH c_get_user INTO l_forward_to_user_id,l_forward_to_person_id;
8984       CLOSE c_get_user;
8985 
8986       IF l_forward_to_person_id is not NULL then
8987                   l_orig_system := 'PER';
8988                   l_orig_sys_id := l_forward_to_person_id;
8989       ELSE
8990                    l_orig_system := 'FND_USR';
8991                    l_orig_sys_id := l_forward_to_user_id;
8992       END IF;
8993 	WF_DIRECTORY.GetUserName( l_orig_system, l_orig_sys_id, l_name,l_display_name);
8994 	l_hist_rec.line_number   := l_line_number;
8995     l_hist_rec.AMOUNT_APPROVED   := l_line_amount;
8996 	l_hist_rec.APPROVER_NAME      := l_display_name;
8997 	l_comments                    := nvl(l_comments,'None');
8998     l_comments                    :='To: '||l_display_name||' '||'    With Comment:'||l_comments;
8999     l_hist_rec.APPROVER_COMMENTS  := l_comments;
9000     AP_WORKFLOW_PKG.insert_history_table(p_hist_rec => l_hist_rec);
9001     END LOOP;
9002    CLOSE Items_Cur;
9003 
9004     END IF;
9005     --Obtaining the roles to check id they are properly set
9006     l_fwd_role      := WF_ENGINE.GetItemAttrText(itemtype, itemkey, 'FORWARD_ROLE');
9007     l_doc_approver  :=WF_ENGINE.GetItemAttrText(itemtype, itemkey, 'DOCUMENT_APPROVER');
9008     l_role_actual   := WF_ENGINE.GetItemAttrText(itemtype, itemkey, 'ROLE_ACTUAL');
9009     L_Line_Approver :=Wf_Engine.Getitemattrtext(Itemtype, Itemkey, 'ROLE_NAME');
9010 
9011     --Setting the resultout variable
9012     resultout := wf_engine.eng_completed || ':' || wf_engine.eng_null;
9013     RETURN;
9014   END IF;
9015 
9016 
9017   RETURN;
9018   --EXCEPTION HANDLING
9019 EXCEPTION
9020 WHEN OTHERS THEN
9021   WF_CORE.CONTEXT('AP_WF',itemtype, itemkey, TO_CHAR(actid), funcmode);
9022   RAISE;
9023 END;
9024 
9025 
9026 END AP_WORKFLOW_PKG;