DBA Data[Home] [Help]

PACKAGE BODY: APPS.AP_PAY_IN_FULL_PKG

Source


1 PACKAGE BODY AP_PAY_IN_FULL_PKG AS
2 /* $Header: apayfulb.pls 120.34.12020000.7 2013/01/23 06:50:54 harsanan ship $ */
3 
4   ---------------------------------------------------------------------
5   -- Procedure AP_Lock_Invoices parses the P_invoice_id_list and locks all
6   -- the invoices in the list.  This procedure also returns information
7   -- needed by the Single Payment workbench to pay these invoices in full.
8   --
9 
10 G_CURRENT_RUNTIME_LEVEL     NUMBER       := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
11 G_LEVEL_UNEXPECTED CONSTANT NUMBER       := FND_LOG.LEVEL_UNEXPECTED;
12 G_LEVEL_ERROR      CONSTANT NUMBER       := FND_LOG.LEVEL_ERROR;
13 G_LEVEL_EXCEPTION  CONSTANT NUMBER       := FND_LOG.LEVEL_EXCEPTION;
14 G_LEVEL_EVENT      CONSTANT NUMBER       := FND_LOG.LEVEL_EVENT;
15 G_LEVEL_PROCEDURE  CONSTANT NUMBER       := FND_LOG.LEVEL_PROCEDURE;
16 G_LEVEL_STATEMENT  CONSTANT NUMBER       := FND_LOG.LEVEL_STATEMENT;
17 G_MODULE_NAME      CONSTANT VARCHAR2(50) :='AP.PLSQL.AP_ACCOUNTING_EVENT_PKG.';
18 
19    --Modified below procedure parameter types for bug #7721348/7758448
20   PROCEDURE AP_Lock_Invoices(
21 	   P_invoice_id_list     IN  VARCHAR2,
22            P_payment_num_list    IN  VARCHAR2,
23            P_currency_code       OUT NOCOPY VARCHAR2,
24            P_payment_method      OUT NOCOPY AP_PAYMENT_SCHEDULES.PAYMENT_METHOD_CODE%TYPE,  --VARCHAR2,
25            P_vendor_id           OUT NOCOPY AP_SUPPLIERS.VENDOR_ID%TYPE,             --NUMBER,
26            P_vendor_site_id      OUT NOCOPY AP_SUPPLIER_SITES.VENDOR_SITE_ID%TYPE,   --NUMBER,
27            P_party_id            OUT NOCOPY AP_SUPPLIERS.PARTY_ID%TYPE,              --NUMBER,
28            P_party_site_id       OUT NOCOPY AP_SUPPLIER_SITES.PARTY_SITE_ID%TYPE,    --NUMBER,
29            P_org_id              OUT NOCOPY NUMBER,
30            P_payment_function    OUT NOCOPY AP_INVOICES.PAYMENT_FUNCTION%TYPE,          --VARCHAR2, -- 4965233
31            P_proc_trxn_type      OUT NOCOPY AP_INVOICES.PAY_PROC_TRXN_TYPE_CODE%TYPE,   --VARCHAR2, -- 4965233
32            P_num_payments        OUT NOCOPY NUMBER,
33            P_le_id               OUT NOCOPY NUMBER,   -- 5617689
34 	    --Added below variables for the bug 7662240
35 	   P_remit_vendor_id        OUT NOCOPY AP_SUPPLIERS.VENDOR_ID%TYPE,            --NUMBER,
36            P_remit_vendor_site_id   OUT NOCOPY AP_SUPPLIER_SITES.VENDOR_SITE_ID%TYPE,   --NUMBER,
37            P_remit_party_id         OUT NOCOPY AP_SUPPLIERS.PARTY_ID%TYPE,              --NUMBER,
38            P_remit_party_site_id    OUT NOCOPY AP_SUPPLIER_SITES.PARTY_SITE_ID%TYPE,    --NUMBER,
39            P_remit_vendor_name      OUT NOCOPY AP_SUPPLIERS.VENDOR_NAME%TYPE,           --VARCHAR2,
40            P_remit_vendor_site_name OUT NOCOPY AP_SUPPLIER_SITES.VENDOR_SITE_CODE%TYPE, --VARCHAR2,
41            P_calling_sequence       IN  VARCHAR2,
42 	   --Added below parameter for 7688200
43 	   p_relationship_id	OUT NOCOPY NUMBER)
44 IS
45     l_invoice_id    NUMBER;
46     l_inv_pos     NUMBER;
47     l_inv_next      NUMBER;
48     l_pay_pos     NUMBER;
49     l_pay_next      NUMBER;
50     l_num_payments    NUMBER := 0;
51     l_payment_num   NUMBER;
52     l_log_msg    VARCHAR2(240);
53     l_curr_calling_sequence VARCHAR2(2000);
54 
55     -- Bug 8209483
56     l_result varchar2(1) := FND_API.G_TRUE;
57     -- Bug 8209483
58 
59   BEGIN
60     l_curr_calling_sequence := 'AP_PAY_IN_FULL_PKG.AP_LOCK_INVOICES<-' ||
61              P_calling_sequence;
62 
63     -- Parse P_invoice_id_list and lock invoices
64     --
65     l_inv_pos := 1;
66 
67     LOOP
68       l_inv_next := INSTR(P_invoice_id_list, ' ', l_inv_pos);
69       IF (l_inv_next = 0) THEN
70         l_inv_next := LENGTH(P_invoice_id_list) + 1;
71       END IF;
72 
73       l_invoice_id := TO_NUMBER(SUBSTR(P_invoice_id_list,
74                                        l_inv_pos,
75                l_inv_next - l_inv_pos));
76 
77       l_log_msg := 'Locking invoice_id:' || to_char(l_invoice_id);
78       AP_INVOICES_PKG.LOCK_ROW(l_invoice_id,
79              l_curr_calling_sequence);
80 
81       -- Determine the number of payments for this invoice
82       --
83       IF (P_payment_num_list IS NULL) THEN
84 
85   l_log_msg := 'Get number of payments for invoice_id:' ||
86       to_char(l_invoice_id);
87 
88         SELECT count(*) + l_num_payments
89         INTO   l_num_payments
90         FROM   ap_invoices_ready_to_pay_v
91         WHERE  invoice_id = l_invoice_id;
92 
93       ELSE
94         -- Parse and count P_payment_num_list
95         --
96   l_pay_pos := 1;
97 
98         LOOP
99     l_pay_next := INSTR(P_payment_num_list, ' ', l_pay_pos);
100     IF (l_pay_next = 0) THEN
101       l_pay_next := LENGTH(P_payment_num_list) + 1;
102     END IF;
103 
104     l_num_payments := l_num_payments + 1;
105 
106     EXIT WHEN (l_pay_next = LENGTH(P_payment_num_list) + 1);
107     l_pay_pos := l_pay_next + 1;
108 
109         END LOOP;
110 
111       END IF;
112 
113       EXIT WHEN (l_inv_next > LENGTH(P_invoice_id_list));
114       l_inv_pos := l_inv_next + 1;
115 
116     END LOOP;
117 
118     l_log_msg := 'Get vendor and currency info for invoice_id:' ||
119         to_char(l_invoice_id);
120 
121 
122 
123     -- Perf bugfix 5052493
124     -- Go to base table AP_INVOICES_ALL to reduce shared memory usage
125     SELECT a.payment_currency_code,
126      b.payment_method_code, --4552701
127      a.vendor_id,
128      a.vendor_site_id,
129      a.party_id,
130      a.party_site_id,
131      a.org_id,
132      l_num_payments,
133      a.payment_function,
134      a.pay_proc_trxn_type_code,
135      a.legal_entity_id,
136      /* commented as part of bug 7688200
137      ,         -- Bug 5617689 */--Bug 7860631 Uncommented the commeted code.
138      b.remit_to_supplier_id,    --Bug 7662240
139      b.remit_to_supplier_site_id,
140      b.remit_to_supplier_name,
141      b.remit_to_supplier_site,
142      b.relationship_id
143     INTO   P_currency_code,
144      P_payment_method,
145      P_vendor_id,
146      P_vendor_site_id,
147      P_party_id,
148      P_party_site_id,
149      P_org_id,
150      P_num_payments,
151      P_payment_function,
152      P_proc_trxn_type,
153      p_le_id,
154      /* commented as part of bug 7688200
155      ,                -- Bug 5617689 */--Bug 7860631 Uncommented the commented code.
156      P_remit_vendor_id,      -- Bug 7662240
157      P_remit_vendor_site_id,
158      p_remit_vendor_name,
159      p_remit_vendor_site_name,
160      p_relationship_id
161      FROM    ap_invoices_all a, ap_payment_schedules_all b  --Bug 7662240
162      WHERE  a.invoice_id = l_invoice_id
163       and    a.invoice_id = b.invoice_id
164       and    rownum<2;
165 
166     /* Need to get payment method if paying from the payment schedule level */
167     If  (P_Payment_num_list IS NOT NULL) then
168       -- get the first payment num.
169       l_pay_next := INSTR(P_payment_num_list,' ',1);
170       If l_pay_next = 0 then
171        l_pay_next := length(p_payment_num_list) +1;
172       End if;
173       l_payment_num := to_number(substr(p_payment_num_list,1,l_pay_next));
174 
175       --7662240 Added all remit related variables in below select statement
176       SELECT payment_method_code,
177       /* commented as part of bug 7688200
178 	     ,      --4552701  */--Bug 7860631 Uncommented the commeted code.
179              remit_to_supplier_id,     --7662240
180              remit_to_supplier_site_id,
181              remit_to_supplier_name,
182              remit_to_supplier_site,
183              relationship_id
184       INTO   p_payment_method,
185       /*  commented as part of bug 7688200
186 	     ,*/--Bug 7860631 Uncommented the commented code.
187              P_remit_vendor_id,      -- Bug 7662240
188              P_remit_vendor_site_id,
189              p_remit_vendor_name,
190              p_remit_vendor_site_name,
191              p_relationship_id
192       FROM   ap_payment_schedules
193       WHERE  invoice_id = l_invoice_id
194       and    payment_num = l_payment_num;
195      End if;
196 
197      /* Bug 7860631 Added If condition to avoid the call to IBY and the query Payment request type invoices */
198      If (p_remit_vendor_id > 0 and p_remit_vendor_site_id > 0) then
199       --Begin 7662240
200       -- modified as part of bug 7688200. start
201 
202       -- modified as part of bug 8209483. start
203       SELECT APS.party_id
204       INTO p_remit_party_id
205       FROM AP_SUPPLIERS APS
206       WHERE APS.vendor_id = p_remit_vendor_id;
207 
208       IBY_EXT_PAYEE_RELSHIPS_PKG.import_Ext_Payee_Relationship(
209 	   p_party_id => p_party_id,
210 	   p_supplier_site_id => p_vendor_site_id,
211 	   p_date => sysdate,
212 	   x_result  => l_result,
213 	   x_remit_party_id => p_remit_party_id,
214 	   x_remit_supplier_site_id => p_remit_vendor_site_id,
215 	   x_relationship_id	=> p_relationship_id
216       );
217 
218        IF (l_result = FND_API.G_FALSE) THEN
219 
220         IBY_EXT_PAYEE_RELSHIPS_PKG.default_Ext_Payee_Relationship(
221 	   p_party_id => p_party_id,
222 	   p_supplier_site_id => P_vendor_site_id,
223 	   p_date => sysdate,
224 	   x_remit_party_id => p_remit_party_id,
225 	   x_remit_supplier_site_id => P_remit_vendor_site_id,
226 	   x_relationship_id => p_relationship_id
227         );
228 
229        End if;
230        -- modified as part of bug 8209483. end
231 
232        --bug 8345877 if no relationship exists as on current date
233        --then need to null out all remit-to columns
234 
235        IF p_relationship_id = -1 then --bug 8345877
236 
237           P_remit_vendor_id        := NULL;
238 	  P_remit_vendor_site_id   := NULL;
239 	  P_remit_party_id         := NULL;
240 	  P_remit_party_site_id    := NULL;
241 	  P_remit_vendor_name      := NULL;
242 	  P_remit_vendor_site_name := NULL;
243 	  p_relationship_id        := NULL;
244 
245        ELSE --bug 8345877
246           SELECT APS.vendor_id,
247 	         APS.vendor_name,
248 		 APSS.party_site_id,
249 		 APSS.vendor_site_code
250           INTO p_remit_vendor_id,
251 	       p_remit_vendor_name,
252 	       p_remit_party_site_id,
253 	       p_remit_vendor_site_name
254        FROM AP_SUPPLIERS APS,  AP_SUPPLIER_SITES APSS
255        WHERE APS.party_id = p_remit_party_id
256              AND APS.vendor_id =APSS.vendor_id
257              AND APSS.vendor_site_id = p_remit_vendor_site_id;
258 
259        END IF; --bug 8345877
260        -- modified as part of bug 7688200. end
261        --End 7662240
262      -- 7860631. To handle payment request type invoices
263      ELSIF (p_remit_party_id is null AND
264 		p_remit_party_site_id is null AND
265 		(p_remit_vendor_id = p_vendor_id) AND
266 		(p_remit_vendor_site_id = p_vendor_site_id) ) THEN
267 
268 	   SELECT party_id, party_site_id
269 	   INTO p_remit_party_id, p_remit_party_site_id
270 	   FROM ap_invoices_all
271 	   WHERE invoice_id = l_invoice_id;
272 
273      end if; --7860631
274 
275   EXCEPTION
276     WHEN OTHERS THEN
277       IF (SQLCODE <> -20001) THEN
278         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
279         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
280         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
281         FND_MESSAGE.SET_TOKEN('PARAMETERS',
282             ' P_invoice_id_list = ' || P_invoice_id_list);
283         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_log_msg);
284       END IF;
285       APP_EXCEPTION.RAISE_EXCEPTION;
286   END AP_Lock_Invoices;
287 
288 
289   ---------------------------------------------------------------------
290   -- Function AP_Discount_Available determines whether or not discounts
291   -- are available for the invoices in P_invoice_id_list
292   --
293   FUNCTION AP_Discount_Available(P_invoice_id_list   IN  VARCHAR2,
294                P_payment_num_list  IN  VARCHAR2,
295                P_check_date        IN  DATE,
296                P_currency_code     IN  VARCHAR2,
297                P_calling_sequence  IN  VARCHAR2)
298     RETURN BOOLEAN
299   IS
300     l_invoice_id    NUMBER;
301     l_payment_num   NUMBER;
302     l_pos     NUMBER;
303     l_next      NUMBER;
304     l_discount_available  NUMBER := 0;
305     l_log_msg    VARCHAR2(240);
306     l_curr_calling_sequence VARCHAR2(2000);
307   BEGIN
308     l_curr_calling_sequence := 'AP_PAY_IN_FULL_PKG.AP_DISCOUNT_AVAILABLE<-' ||
309              P_calling_sequence;
310     l_pos := 1;
311 
312     IF (P_payment_num_list IS NOT NULL) THEN
313 
314       l_invoice_id := TO_NUMBER(P_invoice_id_list);
315       --
316       -- Parse P_payment_num_list
317       --
318       LOOP
319         l_next := INSTR(P_payment_num_list, ' ', l_pos);
320         IF (l_next = 0) THEN
321           l_next := LENGTH(P_payment_num_list) + 1;
322         END IF;
323 
324         l_payment_num := TO_NUMBER(SUBSTR(P_payment_num_list,
325                                           l_pos,
326                   l_next - l_pos));
327 
328   l_log_msg := 'Get discount available for invoice_id:' ||
329       to_char(l_invoice_id) || ' payment_num:' ||
330       to_char(l_payment_num);
331 
332   SELECT ap_payment_schedules_pkg.get_discount_available(
333        invoice_id,
334        payment_num,
335        P_check_date,
336        P_currency_code)
337   INTO   l_discount_available
338   FROM   ap_invoices_ready_to_pay_v
339         WHERE  invoice_id = l_invoice_id
340   AND    payment_num = l_payment_num;
341 
342         EXIT WHEN (l_discount_available > 0 OR
343        l_next > LENGTH(P_payment_num_list));
344         l_pos := l_next + 1;
345 
346       END LOOP;
347 
348     ELSIF (P_invoice_id_list IS NOT NULL) THEN
349       --
350       -- Parse P_invoice_id_list
351       --
352       LOOP
353         l_next := INSTR(P_invoice_id_list, ' ', l_pos);
354         IF (l_next = 0) THEN
355           l_next := LENGTH(P_invoice_id_list) + 1;
356         END IF;
357 
358         l_invoice_id := TO_NUMBER(SUBSTR(P_invoice_id_list,
359                                        l_pos,
360                l_next - l_pos));
361 
362   l_log_msg := 'Get discount available for invoice_id:' ||
363       to_char(l_invoice_id);
364 
365   SELECT SUM(ap_payment_schedules_pkg.get_discount_available(
366       invoice_id,
367       payment_num,
368       P_check_date,
369       P_currency_code))
370   INTO   l_discount_available
371   FROM   ap_invoices_ready_to_pay_v
372         WHERE  invoice_id = l_invoice_id;
373 
374         EXIT WHEN (l_discount_available > 0 OR
375        l_next > LENGTH(P_invoice_id_list));
376         l_pos := l_next + 1;
377 
378       END LOOP;
379 
380     END IF;
381 
382     -- Fix for 962271. For refunds the l_discount_available would be negative,
383     -- so the following if condition should be: IF(l_discount_available <> 0) (instead of > 0)
384     -- since it can be positive or negative depending upon the payment.
385     --
386     IF (l_discount_available <> 0) THEN
387       RETURN TRUE;
388     ELSE
389       RETURN FALSE;
390     END IF;
391 
392   EXCEPTION
393     WHEN OTHERS THEN
394       IF (SQLCODE <> -20001) THEN
395         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
396         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
397         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
398         FND_MESSAGE.SET_TOKEN('PARAMETERS',
399             ' P_invoice_id_list = '  ||P_invoice_id_list ||
400             ' P_payment_num_list = ' ||P_payment_num_list ||
401             ' P_check_date = '       ||P_check_date ||
402             ' P_currency_code = '    ||P_currency_code);
403         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_log_msg);
404       END IF;
405       APP_EXCEPTION.RAISE_EXCEPTION;
406   END AP_Discount_Available;
407 
408   ---------------------------------------------------------------------
409   -- Function Get_Single_Payment_Amount is called by Get_Check_Amount
410   -- to compute the amount of a single payment
411   --
412   FUNCTION Get_Single_Payment_Amount(P_invoice_id             IN  NUMBER,
413              P_payment_num        IN  NUMBER,
414                  P_payment_type_flag      IN  VARCHAR2,
415                  P_check_date             IN  DATE,
416                  P_currency_code          IN  VARCHAR2,
417                  P_take_discount          IN  VARCHAR2,
418                  P_sys_auto_calc_int_flag IN  VARCHAR2,
419                  P_auto_calc_int_flag     IN  VARCHAR2,
420                  P_calling_sequence       IN  VARCHAR2)
421     RETURN NUMBER
422   IS
423     l_payment_num   NUMBER;
424     l_total_amount    NUMBER := 0;
425     l_amount_remaining    NUMBER;
426     l_discount_available  NUMBER;
427     l_discount_taken    NUMBER;
428     l_interest_amount   NUMBER;
429     l_payment_amount            NUMBER;
430     l_due_date      DATE;
431     l_interest_invoice_num  VARCHAR2(50);
432     l_log_msg    VARCHAR2(240);
433     l_curr_calling_sequence VARCHAR2(2000);
434 
435     -------------------------------------------------------------------
436     -- Declare cursor to compute single payment amount
437     --
438     CURSOR payments_cursor IS
439     SELECT payment_num,
440      amount_remaining,
441      ap_payment_schedules_pkg.get_discount_available(
442        invoice_id,
443        payment_num,
444        P_check_date,
445        P_currency_code)
446     FROM   ap_invoices_ready_to_pay_v
447     WHERE  invoice_id = P_invoice_id
448     AND    payment_num = nvl(P_payment_num, payment_num);
449 
450   BEGIN
451     l_curr_calling_sequence := 'AP_PAY_IN_FULL_PKG.GET_SINGLE_PAYMENT_AMOUNT<-' ||
452              P_calling_sequence;
453 
454     l_log_msg := 'Open payments_cursor';
455     OPEN payments_cursor;
456 
457     LOOP
458       l_log_msg := 'Fetch payments_cursor';
459       FETCH payments_cursor
460       INTO  l_payment_num,
461       l_amount_remaining,
462       l_discount_available;
463 
464       EXIT WHEN payments_cursor%NOTFOUND;
465 
466       -- For pay-in-full payment amount = amount remaining
467 
468       l_payment_amount := l_amount_remaining;
469       --
470       -- Calculate discount taken
471       --
472       IF (P_take_discount = 'Y') THEN
473         l_discount_taken := l_discount_available;
474       ELSE
475         l_discount_taken := 0;
476       END IF;
477 
478       --
479       -- Calculate interest invoice amount
480       --
481       IF ((P_payment_type_flag = 'Q')
482          AND (P_auto_calc_int_flag = 'Y')) THEN --Bug 2119368: AND condition added
483   l_log_msg := 'Calulate interest invoice amount for invoice_id:' ||
484       to_char(P_invoice_id) || ' payment_num:' ||
485       to_char(l_payment_num);
486         AP_INTEREST_INVOICE_PKG.AP_CALCULATE_INTEREST(
487                   P_invoice_id,
488                   P_sys_auto_calc_int_flag,
489                   P_auto_calc_int_flag,
490                   P_check_date,
491                   l_payment_num,
492                   l_amount_remaining,
493                   l_discount_taken,
494                   l_discount_available,
495                   P_currency_code,
496                   l_interest_amount,
497                   l_due_date,
498                   l_interest_invoice_num,
499                   l_payment_amount,
500                   l_curr_calling_sequence);
501       ELSE
502         l_interest_amount := 0;
503       END IF;
504 
505       --
506       -- Calculate total amount
507       --
508       l_total_amount := l_total_amount + l_amount_remaining
509                + l_interest_amount
510                - l_discount_taken;
511     END LOOP;
512 
513     l_log_msg := 'Close payments_cursor';
514     CLOSE payments_cursor;
515 
516     RETURN l_total_amount;
517 
518   EXCEPTION
519     WHEN OTHERS THEN
520       IF (SQLCODE <> -20001) THEN
521         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
522         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
523         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
524         FND_MESSAGE.SET_TOKEN('PARAMETERS',
525     ' P_invoice_id = '            || P_invoice_id ||
526     ' P_payment_num = '           || P_payment_num ||
527     ' P_payment_type_flag = '     || P_payment_type_flag ||
528     ' P_check_date = '            || P_check_date ||
529     ' P_currency_code = '         || P_currency_code ||
530     ' P_take_discount = '         || P_take_discount ||
531     ' P_sys_auto_calc_int_flag = '|| P_sys_auto_calc_int_flag ||
532     ' P_auto_calc_int_flag = '    || P_auto_calc_int_flag);
533          FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_log_msg);
534       END IF;
535       APP_EXCEPTION.RAISE_EXCEPTION;
536   END Get_Single_Payment_Amount;
537 
538 
539   ---------------------------------------------------------------------
540   -- Function AP_Get_Check_Amount computes the total check amount including
541   -- discount and interest amounts (if applicable) for the invoices in
542   -- P_invoice_id_list
543   --
544   FUNCTION AP_Get_Check_Amount(P_invoice_id_list  IN  VARCHAR2,
545              P_payment_num_list       IN  VARCHAR2,
546              P_payment_type_flag      IN  VARCHAR2,
547              P_check_date             IN  DATE,
548              P_currency_code          IN  VARCHAR2,
549              P_take_discount          IN  VARCHAR2,
550              P_sys_auto_calc_int_flag IN  VARCHAR2,
551              P_auto_calc_int_flag     IN  VARCHAR2,
552              P_calling_sequence       IN  VARCHAR2)
553     RETURN NUMBER
554   IS
555     l_invoice_id    NUMBER;
556     l_payment_num   NUMBER;
557     l_pos     NUMBER;
558     l_next      NUMBER;
559     l_total_check_amount  NUMBER := 0;
560     l_log_msg    VARCHAR2(240);
561     l_curr_calling_sequence VARCHAR2(2000);
562 
563   BEGIN
564     l_curr_calling_sequence := 'AP_PAY_IN_FULL_PKG.AP_GET_CHECK_AMOUNT<-' ||
565              P_calling_sequence;
566     l_pos := 1;
567 
568     IF (P_payment_num_list IS NOT NULL) THEN
569 
570       l_invoice_id := TO_NUMBER(P_invoice_id_list);
571       --
572       -- Parse P_payment_num_list
573       --
574       LOOP
575         l_next := INSTR(P_payment_num_list, ' ', l_pos);
576         IF (l_next = 0) THEN
577           l_next := LENGTH(P_payment_num_list) + 1;
578         END IF;
579 
580         l_payment_num := TO_NUMBER(SUBSTR(P_payment_num_list,
581                                           l_pos,
582                   l_next - l_pos));
583 
584         l_total_check_amount := l_total_check_amount +
585           Get_Single_Payment_Amount(
586           l_invoice_id,
587           l_payment_num,
588           P_payment_type_flag,
589           P_check_date,
590           P_currency_code,
591           P_take_discount,
592           P_sys_auto_calc_int_flag,
593           P_auto_calc_int_flag,
594           l_curr_calling_sequence);
595 
596         EXIT WHEN (l_next > LENGTH(P_payment_num_list));
597         l_pos := l_next + 1;
598 
599       END LOOP;
600 
601     ELSIF (P_invoice_id_list IS NOT NULL) THEN
602       --
603       -- Parse P_invoice_id_list
604       --
605       LOOP
606         l_next := INSTR(P_invoice_id_list, ' ', l_pos);
607         IF (l_next = 0) THEN
608           l_next := LENGTH(P_invoice_id_list) + 1;
609         END IF;
610 
611         l_invoice_id := TO_NUMBER(SUBSTR(P_invoice_id_list,
612                                        l_pos,
613                l_next - l_pos));
614 
615   l_log_msg := 'Get discount available for invoice_id:' ||
616       to_char(l_invoice_id);
617 
618         l_total_check_amount := l_total_check_amount +
619           Get_Single_Payment_Amount(
620           l_invoice_id,
621           NULL,
622           P_payment_type_flag,
623           P_check_date,
624           P_currency_code,
625           P_take_discount,
626           P_sys_auto_calc_int_flag,
627           P_auto_calc_int_flag,
628           l_curr_calling_sequence);
629 
630         EXIT WHEN (l_next > LENGTH(P_invoice_id_list));
631         l_pos := l_next + 1;
632 
633       END LOOP;
634 
635     END IF;
636 
637     RETURN l_total_check_amount;
638 
639   EXCEPTION
640     WHEN OTHERS THEN
641       IF (SQLCODE <> -20001) THEN
642         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
643         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
644         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
645         FND_MESSAGE.SET_TOKEN('PARAMETERS',
646     ' P_invoice_id_list = '       || P_invoice_id_list ||
647     ' P_payment_num_list = '      || P_payment_num_list ||
648     ' P_payment_type_flag = '     || P_payment_type_flag ||
649     ' P_check_date = '            || P_check_date ||
650     ' P_currency_code = '         || P_currency_code ||
651     ' P_take_discount = '         || P_take_discount ||
652     ' P_sys_auto_calc_int_flag = '|| P_sys_auto_calc_int_flag ||
653     ' P_auto_calc_int_flag = '    || P_auto_calc_int_flag);
654          FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_log_msg);
655       END IF;
656       APP_EXCEPTION.RAISE_EXCEPTION;
657   END AP_Get_Check_Amount;
658 
659 
660   ---------------------------------------------------------------------
661   -- Procedure Create_Single_Payment is called by Create_Payments to
662   -- create the payment(s) for a single invoice
663   --
664   PROCEDURE Create_Single_Payment(P_invoice_id        IN  NUMBER,
665                 P_payment_num       IN  NUMBER,
666                 P_check_id        IN  NUMBER,
667                 P_payment_type_flag     IN  VARCHAR2,
668                 P_payment_method      IN  VARCHAR2,
669                 P_ce_bank_acct_use_id     IN  NUMBER,
670                 P_bank_account_num      IN  VARCHAR2,
671                 P_bank_account_type     IN  VARCHAR2,
672                 P_bank_num        IN  VARCHAR2,
673                 P_check_date              IN  DATE,
674                 P_period_name       IN  VARCHAR2,
675                 P_currency_code           IN  VARCHAR2,
676                 P_base_currency_code      IN  VARCHAR2,
677                 P_checkrun_name     IN  VARCHAR2,
678                 P_doc_sequence_value      IN  NUMBER,
679                 P_doc_sequence_id     IN  NUMBER,
680                 P_exchange_rate     IN  NUMBER,
681                 P_exchange_rate_type      IN  VARCHAR2,
682                 P_exchange_date     IN  DATE,
683                 P_take_discount           IN  VARCHAR2,
684                 P_sys_auto_calc_int_flag  IN  VARCHAR2,
685                 P_auto_calc_int_flag      IN  VARCHAR2,
686                 P_set_of_books_id     IN  NUMBER,
687                 P_future_pay_ccid         IN  NUMBER,
688                 P_last_updated_by     IN  NUMBER,
689                 P_last_update_login     IN  NUMBER,
690                 P_calling_sequence      IN  VARCHAR2,
691                 P_sequential_numbering    IN  VARCHAR2,
692                 P_accounting_event_id     IN  NUMBER, --Events
693                 P_org_id                IN  NUMBER)
694   IS
695     l_invoice_payment_id  NUMBER;
696     l_payment_num   NUMBER;
697     l_invoice_type    VARCHAR2(25);
698     l_invoice_num   VARCHAR2(50);
699     l_vendor_id     NUMBER;
700     l_vendor_site_id    NUMBER;
701     l_exclusive_payment_flag  VARCHAR2(1);
702     l_future_pay_posted_flag  VARCHAR2(1);
703     l_accts_pay_ccid    NUMBER;
704     l_amount      NUMBER;
705     l_amount_remaining    NUMBER;
706     l_discount_available  NUMBER;
707     l_discount_taken    NUMBER;
708     l_interest_invoice_id NUMBER;
709     l_interest_invoice_pay_id   NUMBER;
710     l_interest_amount   NUMBER;
711     l_payment_amount            NUMBER;
712     l_due_date      DATE;
713     l_interest_invoice_num  VARCHAR2(50);
714     l_invoice_description       VARCHAR2(240);
715     l_attribute1    VARCHAR2(150);
716     l_attribute2    VARCHAR2(150);
717     l_attribute3    VARCHAR2(150);
718     l_attribute4    VARCHAR2(150);
719     l_attribute5    VARCHAR2(150);
720     l_attribute6    VARCHAR2(150);
721     l_attribute7    VARCHAR2(150);
722     l_attribute8    VARCHAR2(150);
723     l_attribute9    VARCHAR2(150);
724     l_attribute10   VARCHAR2(150);
725     l_attribute11   VARCHAR2(150);
726     l_attribute12   VARCHAR2(150);
727     l_attribute13   VARCHAR2(150);
728     l_attribute14   VARCHAR2(150);
729     l_attribute15   VARCHAR2(150);
730     l_attribute_category  VARCHAR2(150);
731     l_log_msg    VARCHAR2(240);
732     l_curr_calling_sequence VARCHAR2(2000);
733     l_int_inv_doc_seq_v   NUMBER;                   --1724353
734     l_int_inv_doc_seq_id  NUMBER;
735     l_int_inv_doc_seq_nm  FND_DOCUMENT_SEQUENCES.NAME%TYPE;
736     l_pay_amt             NUMBER;  --Bug9539935
737     l_prepay_amt          NUMBER;
738     l_count               NUMBER;
739 
740     -------------------------------------------------------------------
741     -- Declare cursor to pay single invoice
742     --
743     CURSOR payments_cursor IS
744     SELECT AIRP.payment_num,
745      AIRP.invoice_type,
746      AIRP.invoice_num,
747      AIRP.vendor_id,
748      AIRP.vendor_site_id,
749      AIRP.exclusive_payment_flag,
750      AIRP.accts_pay_code_combi_id,
751      AIRP.amount_remaining,
752      ap_payment_schedules_pkg.get_discount_available(
753        AIRP.invoice_id,
754        AIRP.payment_num,
755        P_check_date,
756        P_currency_code),
757      APS.attribute1,
758      APS.attribute2,
759      APS.attribute3,
760      APS.attribute4,
761      APS.attribute5,
762      APS.attribute6,
763      APS.attribute7,
764      APS.attribute8,
765      APS.attribute9,
766      APS.attribute10,
767      APS.attribute11,
768      APS.attribute12,
769      APS.attribute13,
770      APS.attribute14,
771      APS.attribute15,
772      APS.attribute_category
773     FROM   ap_invoices_ready_to_pay_v AIRP,
774      ap_payment_schedules       APS
775     WHERE  AIRP.invoice_id = P_invoice_id
776     AND    AIRP.payment_num = nvl(P_payment_num, AIRP.payment_num)
777     AND    APS.invoice_id = AIRP.invoice_id
778     AND    APS.payment_num = AIRP.payment_num;
779 
780   BEGIN
781     l_curr_calling_sequence := 'AP_PAY_IN_FULL_PKG.CREATE_SINGLE_PAYMENT<-' ||
782              P_calling_sequence;
783 
784 
785     l_log_msg := 'Open payments_cursor';
786     OPEN payments_cursor;
787 
788     LOOP
789       l_log_msg := 'Fetch payments_cursor';
790       FETCH payments_cursor
791       INTO  l_payment_num,
792       l_invoice_type,
793       l_invoice_num,
794       l_vendor_id,
795       l_vendor_site_id,
796       l_exclusive_payment_flag,
797       l_accts_pay_ccid,
798       l_amount_remaining,
799       l_discount_available,
800       l_attribute1,
801       l_attribute2,
802       l_attribute3,
803       l_attribute4,
804       l_attribute5,
805       l_attribute6,
806       l_attribute7,
807       l_attribute8,
808       l_attribute9,
809       l_attribute10,
810       l_attribute11,
811       l_attribute12,
812       l_attribute13,
813       l_attribute14,
814       l_attribute15,
815       l_attribute_category;
816 
817       EXIT WHEN payments_cursor%NOTFOUND;
818 
819       --
820       -- Calculate discount taken and amount
821       --
822       IF (P_take_discount = 'Y') THEN
823         l_discount_taken := l_discount_available;
824       ELSE
825         l_discount_taken := 0;
826       END IF;
827 
828       l_amount := l_amount_remaining - l_discount_taken;
829 
830       -- For pay-in-full payment_amount = amount_remaining
831 
832       l_payment_amount := l_amount_remaining;
833 
834       --
835       -- Get next invoice_payment_id
836       --
837       l_log_msg := 'Get next invoice_payment_id';
838       SELECT ap_invoice_payments_s.nextval
839       INTO   l_invoice_payment_id
840       FROM   sys.dual;
841 
842       --
843       -- Bug: 661558
844       -- DO AUTOMATIC WITHHOLDING
845       --
846       declare
847          l_subject_amount          NUMBER;
848          l_withholding_amount      NUMBER;
849          l_awt_success             VARCHAR2(2000);
850          l_include_discount        VARCHAR2(1);
851          l_awt_flag                VARCHAR2(1);
852          l_awt_invoices_exists     VARCHAR2(1);
853          l_before_invoice_amount   NUMBER;
854          l_inv_exchange_rate       NUMBER;
855          l_pay_cross_rate          NUMBER;
856 	 --5145239
857          l_awt_applied              VARCHAR2(1);
858          l_create_awt_dists_type    VARCHAR2(25);
859          l_create_awt_invoices_type VARCHAR2(25);
860          --Bug6660355 AWT PROJ
861          l_amount_payable            NUMBER;
862          l_total_inv_amount          NUMBER;
863          l_total_awt_amount          NUMBER;
864       begin
865       -- Bug 8406393
866       -- Added Condition for Invoice type PAYMENT REQUEST
867       -- Withholding should not be calculated for Invoice type PAYMENT REQUEST originated from AR
868       -- having vendor_site_id as negative value (-222)
869 
870       IF (P_payment_type_flag = 'Q' AND l_vendor_site_id <> '-222') THEN
871 
872           l_log_msg := 'Get system parameter for tax withholding';
873 
874 	  IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL ) THEN
875 	    FND_LOG.STRING(G_LEVEL_PROCEDURE,
876                    G_MODULE_NAME||'AP_PAY_IN_FULL_PKG.DO_AUTOMATIC_WITHHOLDING.begin',
877                    l_log_msg);
878 	  END IF;
879 
880           SELECT   nvl(awt_include_discount_amt, 'N'),
881                    nvl(allow_awt_flag, 'N'),
882 		   create_awt_dists_type,
883                    create_awt_invoices_type
884 
885           INTO     l_include_discount,
886                    l_awt_flag,
887 		   l_create_awt_dists_type, --5745239
888                    l_create_awt_invoices_type
889           FROM     ap_system_parameters;
890 
891           begin
892              l_log_msg := 'Check if tax should be withheld from invoice:'||
893                       to_char(P_invoice_id);
894             IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL ) THEN
895 	      FND_LOG.STRING(G_LEVEL_PROCEDURE,
896                    G_MODULE_NAME||'AP_PAY_IN_FULL_PKG.DO_AUTOMATIC_WITHHOLDING.begin',
897                    l_log_msg);
898 	    END IF;
899              SELECT 'Y',awt_flag
900              INTO   l_awt_invoices_exists,l_awt_applied --5745239
901              FROM   ap_invoices AI
902              WHERE  AI.invoice_id = p_invoice_id
903              AND EXISTS (SELECT 'At least 1 dist has an AWT Group'
904                          FROM   ap_invoice_distributions AID1
905                          WHERE  AID1.invoice_id = AI.invoice_id
906                          AND   ( AID1.pay_awt_group_id is not null --Bug6660355
907 			 OR     AID1.awt_group_id is not null))    --Bug7685907
908              AND   NOT EXISTS (SELECT 'Manual AWT lines exist'
909                                FROM   ap_invoice_distributions AID
910                                WHERE  AID.invoice_id = AI.invoice_id
911                                AND    AID.line_type_lookup_code = 'AWT'
912                                AND    AID.awt_flag in ('M', 'O'));
913 
914                 l_log_msg := 'Distributions Exists -- l_awt_invoices_exists -- '||l_awt_invoices_exists
915 				||' l_awt_applied -- '||l_awt_applied;
916 		 IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL ) THEN
917 		   FND_LOG.STRING(G_LEVEL_PROCEDURE,
918 			   G_MODULE_NAME||'AP_PAY_IN_FULL_PKG.DO_AUTOMATIC_WITHHOLDING.begin',
919 	                   l_log_msg);
920 		  END IF;
921           exception
922             when no_data_found then
923               l_awt_invoices_exists := 'N';
924           end;
925           --Bug6660355
926           SELECT sum(nvl(base_amount,amount))
927           INTO   l_total_inv_amount
928           FROM   ap_invoice_distributions
929           WHERE  invoice_id =p_invoice_id
930           AND    line_type_lookup_code <> 'AWT';   /*Bug 14530960*/
931 
932 	  --Bug7707630: Added NVL condition since the awt_group_id
933 	  --can be null at invoice header level
934          /* SELECT  sum(nvl(aid.base_amount,aid.amount))         --query is commented for Bug:14696775
935           INTO   l_total_awt_amount
936           FROM   ap_invoice_distributions aid,ap_invoices ai
937           WHERE  aid.invoice_id =p_invoice_id
938           AND    aid.invoice_id    = ai.invoice_id
939           AND    aid.line_type_lookup_code in ('AWT')
940           AND    aid.awt_origin_group_id = NVL(ai.awt_group_id, aid.awt_origin_group_id)  --Bug7707630
941           AND    aid.awt_invoice_payment_id IS NULL;    */
942           --bug 8898537, should consider only invoice time AWT for calculating l_amount_payable
943 
944 
945          --query is added for Bug:14696775
946           SELECT  sum(nvl(aid.base_amount,aid.amount))
947           INTO   l_total_awt_amount
948           FROM   ap_invoice_distributions aid
949           WHERE  aid.invoice_id =p_invoice_id
950           AND    aid.line_type_lookup_code in ('AWT')
951           AND    aid.awt_invoice_payment_id is null;
952 
953 	  l_amount_payable := l_total_inv_amount + nvl(l_total_awt_amount,0); --7022001
954 
955           if l_awt_flag = 'Y' and
956              l_awt_invoices_exists = 'Y' then
957 
958              --get invoice amount before withholding
959              l_log_msg := 'Get the invoice amount for awt, invoice_id:'||
960                       to_char(P_invoice_id);
961 
962              -- Bug 906732
963              -- Multiply the witholding subject amount by the exchange rate as the
964              -- witholding procedure expects the amount in base currency.
965 
966              SELECT invoice_amount,exchange_rate,nvl(payment_cross_rate,1)
967              INTO   l_before_invoice_amount,l_inv_exchange_rate,l_pay_cross_rate
968              FROM   ap_invoices
969              WHERE  invoice_id = p_invoice_id;
970 /*
971              if (l_include_discount = 'Y') then
972                 l_subject_amount := (((l_amount + l_discount_taken))
973                             / l_pay_cross_rate) * nvl(l_inv_exchange_rate,1);
974              else
975                 l_subject_amount := l_amount / l_pay_cross_rate
976                             * nvl(l_inv_exchange_rate,1);
977              end if;
978 */
979 --changes for bug 8590059
980 
981              if (l_include_discount = 'Y') then
982                 l_subject_amount := (l_amount + l_discount_taken)
983 		  		    * nvl(P_exchange_rate,1);
984              else
985                 l_subject_amount := l_amount * nvl(P_exchange_rate,1);
986              end if;
987              /* Added if for the bug 14683271*/
988 if(nvl(l_amount_payable,0)<>0) then
989              l_subject_amount := l_subject_amount * l_total_inv_amount/l_amount_payable; --Bug6660355
990    end if;
991        l_log_msg := 'Call the AP_DO_WITHHOLD procedure, invoice_id:'||
992                       to_char(P_invoice_id);
993          --Bug5745239
994 
995 	 l_log_msg := 'l_awt_applied -- '||nvl(l_awt_applied,'NULL')||' l_create_awt_dists_type -- '
996 			 ||l_create_awt_dists_type||' l_create_awt_invoices_type -- '||l_create_awt_invoices_type;
997 	  IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL ) THEN
998 	    FND_LOG.STRING(G_LEVEL_PROCEDURE,
999                    G_MODULE_NAME||'AP_PAY_IN_FULL_PKG.DO_AUTOMATIC_WITHHOLDING.begin',
1000                    l_log_msg);
1001 	  END IF;
1002 
1003          IF (nvl(l_awt_applied,'N') <> 'Y')
1004              OR (l_create_awt_dists_type='APPROVAL' and l_create_awt_invoices_type ='PAYMENT')
1005              OR (l_create_awt_dists_type ='BOTH')  Then --Bug6660355
1006 
1007 	 l_log_msg := 'call to AP_DO_WITHHOLDING with parameters -- p_invoice_id -- '||p_invoice_id
1008 			 ||' p_calling_module -- QUICKCHECK '||'p_amount -- '||l_subject_amount;
1009 	  IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL ) THEN
1010 	    FND_LOG.STRING(G_LEVEL_PROCEDURE,
1011                    G_MODULE_NAME||'AP_PAY_IN_FULL_PKG.DO_AUTOMATIC_WITHHOLDING.begin',
1012                    l_log_msg);
1013 	  END IF;
1014 
1015 	     /*
1016 	     bug13606808, added call to AP_WITHHOLDING_PKG.Ap_Undo_Temp_Withholding
1017 	     to prevent duplicate AWT distributions creation from payment workbench
1018 	     */
1019 	     DECLARE
1020                    undo_output VARCHAR2(2000);
1021              BEGIN
1022                     AP_WITHHOLDING_PKG.Ap_Undo_Temp_Withholding
1023                             (P_Invoice_Id             => p_invoice_id
1024                             ,P_vendor_Id              => l_vendor_id
1025                             ,P_Payment_Num            => l_payment_num
1026                             ,P_Checkrun_Name          => null
1027                             ,P_Undo_Awt_Date          => SYSDATE
1028                             ,P_Calling_Module         => 'QUICKCHECK'
1029                             ,P_Last_Updated_By        => p_last_updated_by
1030                             ,P_Last_Update_Login      => p_last_update_login
1031                             ,P_Program_Application_Id => null
1032                             ,P_Program_Id             => null
1033                             ,P_Request_Id             => null
1034                             ,P_Awt_Success            => undo_output
1035                             ,P_checkrun_id            => null);
1036 
1037              END;
1038 
1039              AP_WITHHOLDING_PKG.AP_Do_Withholding
1040                 (P_Invoice_Id             => p_invoice_id
1041                 ,P_Awt_Date               => P_check_date
1042                 ,P_Calling_Module         => 'QUICKCHECK'
1043                 ,P_Amount                 => l_subject_amount
1044                 ,P_Payment_Num            => l_payment_num
1045                 ,P_Checkrun_Name          => null
1046                 ,P_Last_Updated_By        => p_last_updated_by
1047                 ,P_Last_Update_Login      => p_last_update_login
1048                 ,P_Program_Application_Id => null
1049                 ,P_Program_Id             => null
1050                 ,P_Request_Id             => null
1051                 ,P_Awt_Success            => l_awt_success
1052 	        ,P_Invoice_Payment_ID     => l_invoice_payment_id
1053  	        ,P_Check_Id		  => P_check_id		--bug 8590059
1054                 );
1055          End if;
1056                 if ((l_awt_success = 'SUCCESS') OR
1057                     (l_awt_success IS NULL)) then
1058 
1059                    -- get amount withheld for this particular invoice payment.
1060                    -- (this will be a negative number for STD inv and
1061        -- positive for CM
1062                    SELECT nvl(sum(ap_utilities_pkg.ap_round_currency(
1063              AID.amount * AI.payment_cross_rate,
1064           AI.payment_currency_code)),0)
1065                    INTO  l_withholding_amount
1066                    FROM ap_invoice_distributions AID,
1067                   ap_invoices AI
1068                    WHERE AID.awt_invoice_payment_id = l_invoice_payment_id
1069                AND AID.invoice_id = AI.invoice_id;
1070 
1071                    l_amount := l_amount + l_withholding_amount;
1072                    l_amount_remaining := l_amount_remaining +
1073                                          l_withholding_amount;
1074                 else
1075                    FND_MESSAGE.SET_NAME('SQLAP', 'AP_AWT_PROB_PLSQL');
1076                    FND_MESSAGE.SET_TOKEN('INVOICE', to_char(P_invoice_id));
1077                    FND_MESSAGE.SET_TOKEN('PROBLEM', l_awt_success );
1078                    APP_EXCEPTION.RAISE_EXCEPTION;
1079                 end if;
1080 
1081           end if;
1082 
1083       --END WITHHOLDING HANDLING
1084       end if; -- quick check
1085 
1086 
1087 --Bug 9539935
1088 --Overpayment code
1089 
1090 SELECT nvl(SUM(aip.amount),0)
1091   INTO l_pay_amt
1092   FROM ap_invoice_payments_all aip
1093  WHERE aip.invoice_id = p_invoice_id;
1094 
1095  SELECT nvl(SUM(aid.amount),0)
1096    INTO l_prepay_amt
1097    FROM ap_invoice_distributions_all aid,ap_invoice_lines_all ail,ap_invoices_all ai
1098   WHERE ail.invoice_id=aid.invoice_id
1099         AND ail.invoice_id=ai.invoice_id
1100         AND ail.line_number=aid.invoice_line_number
1101         AND ai.invoice_id=p_invoice_id
1102     AND aid.prepay_distribution_id is not null
1103     AND nvl(ail.invoice_includes_prepay_flag,'N')<>'Y';
1104 
1105 
1106  BEGIN
1107    l_count:=0;
1108 
1109    SELECT count(ai.invoice_id)
1110      INTO l_count
1111      FROM ap_invoices_all ai
1112     WHERE ai.invoice_id=p_invoice_id
1113     GROUP BY ai.invoice_id,ai.invoice_amount
1114             ,ai.discount_amount_taken,  decode(ai.net_of_retainage_flag,'Y',0,
1115                 nvl(AP_INVOICES_UTILITY_PKG.GET_RETAINED_TOTAL(ai.invoice_id,ai.org_id),0))
1116      HAVING (abs(nvl(ai.invoice_amount,0) -nvl(ai.discount_amount_taken,0)
1117                   - nvl(AP_INVOICES_UTILITY_PKG.get_amount_withheld(ai.invoice_id),0)
1118                   + decode(ai.net_of_retainage_flag, 'Y', 0,
1119                    nvl(AP_INVOICES_UTILITY_PKG.GET_RETAINED_TOTAL(ai.invoice_id, ai.org_id),0))
1120                )
1121             < abs((nvl(l_pay_amt,0)-nvl(l_prepay_amt,0))
1122                                 +((l_amount) + (l_discount_taken))));
1123 
1124  EXCEPTION
1125      WHEN NO_DATA_FOUND THEN
1126      l_count:=0;
1127  END;
1128 
1129 IF(l_count>0) THEN
1130    DECLARE
1131       undo_output VARCHAR2(2000);
1132    BEGIN
1133       AP_WITHHOLDING_PKG.Ap_Undo_Temp_Withholding
1134                             (P_Invoice_Id             => p_invoice_id
1135                             ,P_vendor_Id              => l_vendor_id
1136                             ,P_Payment_Num            => l_payment_num
1137                             ,P_Checkrun_Name          => null
1138                             ,P_Undo_Awt_Date          => SYSDATE
1139                             ,P_Calling_Module         => 'QUICKCHECK'
1140                             ,P_Last_Updated_By        => p_last_updated_by
1141                                 ,P_Last_Update_Login      => p_last_update_login
1142                             ,P_Program_Application_Id => null
1143                             ,P_Program_Id             => null
1144                             ,P_Request_Id             => null
1145                             ,P_Awt_Success            => undo_output
1146                             ,P_checkrun_id            => null);
1147 
1148    END;
1149 
1150    FND_MESSAGE.SET_NAME('SQLAP', 'AP_OVERPAID_INVOICE');
1151    FND_MESSAGE.SET_TOKEN('INVOICE', to_char(P_invoice_id));
1152    APP_EXCEPTION.RAISE_EXCEPTION;
1153 
1154 END IF;
1155 
1156 --End of Overpayment code
1157 --End of bug 9539935
1158       end;
1159 
1160       --
1161       -- Calculate interest invoice amount
1162       -- added below 'and' condition for 7612309/7668747
1163       IF ((P_payment_type_flag = 'Q') AND (P_auto_calc_int_flag = 'Y')) THEN
1164     l_log_msg := 'Calulate interest invoice amount for invoice_id:' ||
1165       to_char(P_invoice_id) || ' payment_num:' ||
1166       to_char(l_payment_num);
1167         AP_INTEREST_INVOICE_PKG.AP_CALCULATE_INTEREST(
1168                   P_invoice_id,
1169                   P_sys_auto_calc_int_flag,
1170                   P_auto_calc_int_flag,
1171                   P_check_date,
1172                   l_payment_num,
1173                   --bug1905384 Interest should be calculated for payment amount
1174                   --including withholding amount.
1175                   l_payment_amount,
1176                   --l_amount_remaining,
1177                   l_discount_taken,
1178                   l_discount_available,
1179                   P_currency_code,
1180                   l_interest_amount,
1181                   l_due_date,
1182                   l_interest_invoice_num,
1183                   l_payment_amount,
1184                   l_curr_calling_sequence);
1185       ELSE
1186         l_interest_amount := 0;
1187       END IF;
1188 
1189 
1190       l_log_msg := 'Create invoice payment for invoice_id:' ||
1191           to_char(P_invoice_id) || ' payment_num:' ||
1192           to_char(l_payment_num);
1193 
1194       AP_PAY_INVOICE_PKG.AP_PAY_INVOICE(
1195           P_invoice_id              =>    P_invoice_id,
1196           P_check_id                =>    P_check_id,
1197           P_payment_num             =>    l_payment_num,
1198           P_invoice_payment_id      =>    l_invoice_payment_id,
1199           P_old_invoice_payment_id  =>    NULL,
1200           P_period_name             =>    P_period_name,
1201           P_invoice_type            =>    l_invoice_type,
1202           P_accounting_date         =>    P_check_date,
1203           P_amount                  =>    l_amount,
1204           P_discount_taken          =>    l_discount_taken,
1205           P_discount_lost           =>    '',
1206           P_invoice_base_amount     =>    '',
1207           P_payment_base_amount     =>    '',
1208           P_accrual_posted_flag     =>    'N',
1209           P_cash_posted_flag        =>    'N',
1210           P_posted_flag             =>    'N',
1211           P_set_of_books_id         =>    P_set_of_books_id,
1212           P_last_updated_by         =>    P_last_updated_by,
1213           P_last_update_login       =>    P_last_update_login,
1214           P_currency_code           =>    P_currency_code,
1215           P_base_currency_code      =>    P_base_currency_code,
1216           P_exchange_rate           =>    P_exchange_rate,
1217           P_exchange_rate_type      =>    P_exchange_rate_type,
1218           P_exchange_date           =>    P_exchange_date,
1219           P_ce_bank_acct_use_id     =>    P_ce_bank_acct_use_id,
1220           P_bank_account_num        =>    P_bank_account_num,
1221           P_bank_account_type       =>    P_bank_account_type,
1222           P_bank_num                =>    P_bank_num,
1223           P_future_pay_posted_flag  =>    l_future_pay_posted_flag,
1224           P_exclusive_payment_flag  =>    l_exclusive_payment_flag,
1225           P_accts_pay_ccid          =>    l_accts_pay_ccid,
1226           P_gain_ccid               =>    '',
1227           P_loss_ccid               =>    '',
1228           P_future_pay_ccid         =>    P_future_pay_ccid,
1229           P_asset_ccid              =>    NULL,
1230           P_payment_dists_flag      =>    'N',
1231           P_payment_mode            =>    'PAY',
1232           P_replace_flag            =>    'N',
1233           P_attribute1              =>    l_attribute1,
1234           P_attribute2              =>    l_attribute2,
1235           P_attribute3              =>    l_attribute3,
1236           P_attribute4              =>    l_attribute4,
1237           P_attribute5              =>    l_attribute5,
1238           P_attribute6              =>    l_attribute6,
1239           P_attribute7              =>    l_attribute7,
1240           P_attribute8              =>    l_attribute8,
1241           P_attribute9              =>    l_attribute9,
1242           P_attribute10             =>    l_attribute10,
1243           P_attribute11             =>    l_attribute11,
1244           P_attribute12             =>    l_attribute12,
1245           P_attribute13             =>    l_attribute13,
1246           P_attribute14             =>    l_attribute14,
1247           P_attribute15             =>    l_attribute15,
1248           P_attribute_category      =>    l_attribute_category,
1249           P_calling_sequence        =>    l_curr_calling_sequence,
1250           -- Events Project - 4 - Added following parameter
1251           P_accounting_event_id     =>    P_accounting_event_id,
1252           P_org_id                  =>    P_org_id);
1253 
1254       --Bug2993905 We will call events package to update the accounting
1255       --event id on awt distributions created during Payment time with
1256       --the payment time account event id after invoice payments have been
1257       --created.
1258       IF (p_payment_type_flag = 'Q') THEN
1259 
1260         -- Events Project
1261         -- Bug 2751466
1262         -- This call is happening for the Pay-In-Full case,
1263         -- (also a type of Quick Payment)
1264         --
1265         -- This code will work ONLY when the the wiholding options
1266         -- are set to:-
1267         --
1268         -- o Apply Witholding Tax      ==> At Payment Time
1269         -- o Create Witholding Invoice ==> At Payment Time
1270         --
1271         -- We want to stamp the Accounting_Event_ID for the Payment
1272         -- Event on all the AWT distributions that have been created
1273         -- as a result of this check.
1274 
1275         AP_ACCOUNTING_EVENTS_PKG.UPDATE_AWT_INT_DISTS
1276         (
1277           p_event_type => 'PAYMENT CREATED',
1278           p_check_id => p_check_id,
1279           p_event_id => p_accounting_event_id,
1280           p_calling_sequence => l_curr_calling_sequence
1281         );
1282 
1283       END IF;
1284       --Bug2993905 End.
1285 
1286       --
1287       -- Create interest invoice if QuickCheck
1288       -- Added below 'AND' condition for 7612309/7668747
1289       IF ((P_payment_type_flag = 'Q') AND (P_auto_calc_int_flag = 'Y'))THEN
1290 
1291         --
1292         -- Get next interest invoice_id
1293         --
1294         l_log_msg := 'Get next interest invoice_id';
1295         SELECT ap_invoices_s.nextval
1296         INTO   l_interest_invoice_id
1297         FROM   sys.dual;
1298 
1299         --
1300         -- Get next interest invoice_payment_id
1301         --
1302         l_log_msg := 'Get next interest invoice_payment_id';
1303         SELECT ap_invoice_payments_s.nextval
1304         INTO   l_interest_invoice_pay_id
1305         FROM   sys.dual;
1306 
1307         --
1308         -- Create interest invoice
1309   --
1310         l_log_msg := 'Create interest invoice for invoice_id:' ||
1311             to_char(P_invoice_id) || ' payment_num:' ||
1312             to_char(l_payment_num);
1313 
1314         --
1315         -- Bug: 622377
1316         -- This block will compose the Invoice Description field by retrieving the
1317         -- Due Date and Annual interest rate and the filling words...
1318         --
1319         DECLARE
1320            l_rate              NUMBER;
1321            l_due_date          DATE;
1322            l_int_invoice_days  NUMBER;
1323            /* Datatype for following variables changed for MLS */
1324            l_nls_interest      ap_lookup_codes.displayed_field%TYPE;  -- **1**
1325            l_nls_days          ap_lookup_codes.displayed_field%TYPE;  -- **1**
1326            l_nls_percent       ap_lookup_codes.displayed_field%TYPE;  -- **1**
1327 
1328         BEGIN
1329           -- Get the Translatable Words for filling the description
1330           SELECT l1.displayed_field,
1331                  l2.displayed_field,
1332                  l3.displayed_field
1333             INTO l_nls_interest,
1334                  l_nls_days,
1335                  l_nls_percent
1336             FROM ap_lookup_codes l1,
1337                  ap_lookup_codes l2,
1338                  ap_lookup_codes l3
1339            WHERE l1.lookup_type = 'NLS TRANSLATION'
1340              AND l1.lookup_code = 'INTEREST'
1341              AND l2.lookup_type = 'NLS TRANSLATION'
1342              AND l2.lookup_code = 'DAYS'
1343              AND l3.lookup_type = 'NLS TRANSLATION'
1344              AND l3.lookup_code = 'PERCENT';
1345 
1346            IF l_interest_amount = 0 THEN
1347               l_invoice_description := '0 ' || l_nls_interest;
1348            ELSE
1349               BEGIN
1350               SELECT  annual_interest_rate , due_date
1351                  INTO    l_rate, l_due_date
1352                  FROM    ap_payment_schedules, ap_interest_periods
1353                  WHERE   payment_num = l_payment_num
1354                  AND     invoice_id = P_invoice_id
1355                  AND     trunc(due_date+1) BETWEEN trunc(start_date) AND trunc(end_date);
1356 
1357               l_int_invoice_days:=
1358                  LEAST(TRUNC(P_check_date),ADD_MONTHS(TRUNC(l_due_date),12))
1359                        - TRUNC(l_due_date);
1360 
1361               l_invoice_description :=
1362                  l_nls_interest|| ' ' || to_char(l_int_invoice_days) || ' ' || l_nls_days ||
1363                  to_char(l_rate) || l_nls_percent;
1364 
1365               EXCEPTION
1366               WHEN NO_DATA_FOUND then
1367                   -- If no interest found, treat as ZERO interest
1368                   l_invoice_description := '0 ' || l_nls_interest;
1369               END;
1370            END IF;
1371         END;
1372 
1373 
1374              --1724353, START OF CODE
1375 
1376         BEGIN
1377 
1378             IF P_sequential_numbering = 'A' and l_interest_amount > 0 THEN
1379 
1380 
1381                 l_int_inv_doc_seq_v :=   FND_SEQNUM.GET_NEXT_SEQUENCE(
1382                                          APPID    =>'200',
1383                                          CAT_CODE => 'INT INV',
1384                                          SOBID    => P_set_of_books_id,
1385                                          MET_CODE => 'A',
1386                                          TRX_DATE => SYSDATE,
1387                                          DBSEQNM  => l_int_inv_doc_seq_nm,
1388                                          DBSEQID  => l_int_inv_doc_seq_id );
1389             END IF;
1390 
1391         EXCEPTION
1392 
1393               WHEN OTHERS THEN
1394 
1395                 IF (SQLCODE <> -20001) THEN
1396                     FND_MESSAGE.SET_NAME('FND','UNIQUE-ALWAYS USED');
1397                 END IF;
1398 
1399                 APP_EXCEPTION.RAISE_EXCEPTION;
1400        END;
1401 
1402 
1403         BEGIN
1404 
1405             IF P_sequential_numbering = 'P' and l_interest_amount > 0 THEN
1406 
1407                 l_int_inv_doc_seq_v :=   FND_SEQNUM.GET_NEXT_SEQUENCE(
1408                                          APPID    =>'200',
1409                                          CAT_CODE => 'INT INV',
1410                                          SOBID    => P_set_of_books_id,
1411                                          MET_CODE => 'A',
1412                                          TRX_DATE => SYSDATE,
1413                                          DBSEQNM  => l_int_inv_doc_seq_nm,
1414                                          DBSEQID  => l_int_inv_doc_seq_id );
1415             END IF;
1416 
1417         EXCEPTION
1418           when others then
1419           NULL;
1420 
1421         END;
1422 
1423        --1724353, END OF CODE
1424 
1425 
1426 
1427 
1428   AP_INTEREST_INVOICE_PKG.AP_CREATE_INTEREST_INVOICE(
1429             P_invoice_id                  =>    P_invoice_id,
1430             P_int_invoice_id              =>    l_interest_invoice_id,
1431             P_check_id                    =>    P_check_id,
1432             P_payment_num                 =>    l_payment_num,
1433             P_int_invoice_payment_id      =>    l_interest_invoice_pay_id,
1434             P_old_invoice_payment_id      =>    NULL,
1435             P_period_name                 =>    P_period_name,
1436             P_invoice_type                =>    l_invoice_type,
1437             P_accounting_date             =>    P_check_date,
1438             P_amount                      =>    l_amount,
1439             P_discount_taken              =>    l_discount_taken,
1440             P_discount_lost               =>    '',
1441             P_invoice_base_amount         =>    '',
1442             P_payment_base_amount         =>    '',
1443             P_vendor_id                   =>    l_vendor_id,
1444             P_vendor_site_id              =>    l_vendor_site_id,
1445             P_int_invoice_num             =>    l_interest_invoice_num,
1446             P_old_invoice_num             =>    l_invoice_num,
1447             P_interest_amount             =>    l_interest_amount,
1448             P_payment_method_code         =>    P_payment_method, --4552701
1449             P_doc_sequence_value          =>    l_int_inv_doc_seq_v, --1724353
1450             P_doc_sequence_id             =>    l_int_inv_doc_seq_id,--1724353
1451             P_checkrun_name               =>    P_checkrun_name,
1452             P_payment_priority            =>    '',
1453             P_accrual_posted_flag         =>    'N',
1454             P_cash_posted_flag            =>    'N',
1455             P_posted_flag                 =>    'N',
1456             P_set_of_books_id             =>    P_set_of_books_id,
1457             P_last_updated_by             =>    P_last_updated_by,
1458             P_last_update_login           =>    P_last_update_login,
1459             P_currency_code               =>    P_currency_code,
1460             P_base_currency_code          =>    P_base_currency_code,
1461             P_exchange_rate               =>    P_exchange_rate,
1462             P_exchange_rate_type          =>    P_exchange_rate_type,
1463             P_exchange_date               =>    P_exchange_date,
1464             P_bank_account_id             =>    P_ce_bank_acct_use_id,
1465             P_bank_account_num            =>    P_bank_account_num,
1466             P_bank_account_type           =>    P_bank_account_type,
1467             P_bank_num                    =>    P_bank_num,
1468             P_exclusive_payment_flag      =>    l_exclusive_payment_flag,
1469             P_accts_pay_ccid              =>    l_accts_pay_ccid,
1470             P_gain_ccid                   =>    '',
1471             P_loss_ccid                   =>    '',
1472             P_future_pay_ccid             =>    P_future_pay_ccid,
1473             P_asset_ccid                  =>    NULL,
1474             P_payment_dists_flag          =>    'N',
1475             P_payment_mode                =>    'PAY',
1476             P_replace_flag                =>    'N',
1477             P_invoice_description         =>    l_invoice_description,
1478             P_attribute1                  =>    l_attribute1,
1479             P_attribute2                  =>    l_attribute2,
1480             P_attribute3                  =>    l_attribute3,
1481             P_attribute4                  =>    l_attribute4,
1482             P_attribute5                  =>    l_attribute5,
1483             P_attribute6                  =>    l_attribute6,
1484             P_attribute7                  =>    l_attribute7,
1485             P_attribute8                  =>    l_attribute8,
1486             P_attribute9                  =>    l_attribute9,
1487             P_attribute10                 =>    l_attribute10,
1488             P_attribute11                 =>    l_attribute11,
1489             P_attribute12                 =>    l_attribute12,
1490             P_attribute13                 =>    l_attribute13,
1491             P_attribute14                 =>    l_attribute14,
1492             P_attribute15                 =>    l_attribute15,
1493             P_attribute_category          =>    l_attribute_category,
1494             P_calling_sequence            =>    l_curr_calling_sequence,
1495             P_org_id                      =>    P_org_id,  /* Bug 4742671 */
1496             P_accounting_event_id         =>    P_accounting_event_id); --Events
1497 
1498       END IF;
1499 
1500     END LOOP;
1501 
1502     l_log_msg := 'Close payments_cursor';
1503     CLOSE payments_cursor;
1504 
1505 
1506   EXCEPTION
1507     WHEN OTHERS THEN
1508       IF (SQLCODE <> -20001) THEN
1509         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
1510         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
1511         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
1512         FND_MESSAGE.SET_TOKEN('PARAMETERS',
1513     ' P_invoice_id = '        || P_invoice_id      ||
1514     ' P_payment_num = '       || P_payment_num     ||
1515     ' P_check_id = '                || P_check_id        ||
1516     ' P_payment_type_flag = '   || P_payment_type_flag     ||
1517     ' P_payment_method = '    || P_payment_method    ||
1518     ' P_bank_account_id = '   || P_ce_bank_acct_use_id   ||
1519     ' P_bank_account_num = '  || P_bank_account_num      ||
1520     ' P_bank_account_type = '   || P_bank_account_type     ||
1521     ' P_bank_num = '    || P_bank_num      ||
1522     ' P_check_date = '    || P_check_date      ||
1523     ' P_period_name = '     || P_period_name     ||
1524     ' P_currency_code = '     || P_currency_code       ||
1525     ' P_base_currency_code = '  || P_base_currency_code    ||
1526     ' P_checkrun_name = '     || P_checkrun_name     ||
1527     ' P_doc_sequence_value = '  || P_doc_sequence_value    ||
1528     ' P_doc_sequence_id = '   || P_doc_sequence_id     ||
1529     ' P_exchange_rate = '     || P_exchange_rate     ||
1530     ' P_exchange_rate_type = '  || P_exchange_rate_type    ||
1531     ' P_exchange_date = '     || P_exchange_date     ||
1532     ' P_take_discount = '     || P_take_discount         ||
1533     ' P_sys_auto_calc_int_flag = '  || P_sys_auto_calc_int_flag||
1534     ' P_auto_calc_int_flag = '  || P_auto_calc_int_flag    ||
1535     ' P_set_of_books_id = '   || P_set_of_books_id     ||
1536     ' P_future_pay_ccid = '   || P_future_pay_ccid     ||
1537     ' P_last_updated_by = '   || P_last_updated_by     ||
1538     ' P_last_update_login = '   || P_last_update_login
1539     );
1540   FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_log_msg);
1541       END IF;
1542       APP_EXCEPTION.RAISE_EXCEPTION;
1543   END Create_Single_Payment;
1544 
1545 
1546   ---------------------------------------------------------------------
1547   -- Procedure AP_Create_Payments create an invoice payment for all payable
1548   -- payment schedules belonging to invoices in P_invoice_id_list and
1549   -- related interest invoices if Quickcheck.
1550   --
1551   PROCEDURE AP_Create_Payments(P_invoice_id_list  IN  VARCHAR2,
1552              P_payment_num_list IN  VARCHAR2,
1553              P_check_id   IN  NUMBER,
1554              P_payment_type_flag  IN  VARCHAR2,
1555              P_payment_method   IN  VARCHAR2,
1556              P_ce_bank_acct_use_id  IN  NUMBER,
1557              P_bank_account_num IN  VARCHAR2,
1558              P_bank_account_type  IN  VARCHAR2,
1559              P_bank_num   IN  VARCHAR2,
1560              P_check_date             IN  DATE,
1561              P_period_name    IN  VARCHAR2,
1562              P_currency_code          IN  VARCHAR2,
1563              P_base_currency_code IN  VARCHAR2,
1564              P_checkrun_name    IN  VARCHAR2,
1565              P_doc_sequence_value IN  NUMBER,
1566              P_doc_sequence_id  IN  NUMBER,
1567              P_exchange_rate    IN  NUMBER,
1568              P_exchange_rate_type IN  VARCHAR2,
1569              P_exchange_date    IN  DATE,
1570              P_take_discount          IN  VARCHAR2,
1571              P_sys_auto_calc_int_flag IN  VARCHAR2,
1572              P_auto_calc_int_flag     IN  VARCHAR2,
1573              P_set_of_books_id  IN  NUMBER,
1574              P_future_pay_ccid  IN  NUMBER,
1575              P_last_updated_by  IN  NUMBER,
1576              P_last_update_login  IN  NUMBER,
1577              P_calling_sequence IN  VARCHAR2,
1578              P_sequential_numbering   IN  VARCHAR2 DEFAULT 'N', -- 1724353
1579              P_accounting_event_id  IN  NUMBER, -- Events
1580              P_org_id           IN  NUMBER)
1581   IS
1582     l_invoice_id    NUMBER;
1583     l_payment_num   NUMBER;
1584     l_pos     NUMBER;
1585     l_next      NUMBER;
1586     l_log_msg    VARCHAR2(240);
1587     l_curr_calling_sequence VARCHAR2(2000);
1588 
1589   BEGIN
1590     l_curr_calling_sequence := 'AP_PAY_IN_FULL_PKG.AP_CREATE_PAYMENTS<-' ||
1591              P_calling_sequence;
1592     l_pos := 1;
1593 
1594     IF (P_payment_num_list IS NOT NULL) THEN
1595 
1596       l_invoice_id := TO_NUMBER(P_invoice_id_list);
1597       --
1598       -- Parse P_payment_num_list
1599       --
1600       LOOP
1601         l_next := INSTR(P_payment_num_list, ' ', l_pos);
1602         IF (l_next = 0) THEN
1603           l_next := LENGTH(P_payment_num_list) + 1;
1604         END IF;
1605 
1606         l_payment_num := TO_NUMBER(SUBSTR(P_payment_num_list,
1607                                           l_pos,
1608                   l_next - l_pos));
1609 
1610   Create_Single_Payment(l_invoice_id,
1611             l_payment_num,
1612             P_check_id,
1613             P_payment_type_flag,
1614             P_payment_method,
1615             P_ce_bank_acct_use_id,
1616             P_bank_account_num,
1617             P_bank_account_type,
1618             P_bank_num,
1619             P_check_date,
1620             P_period_name,
1621             P_currency_code,
1622             P_base_currency_code,
1623             P_checkrun_name,
1624             P_doc_sequence_value,
1625             P_doc_sequence_id,
1626             P_exchange_rate,
1627             P_exchange_rate_type,
1628             P_exchange_date,
1629             P_take_discount,
1630             P_sys_auto_calc_int_flag,
1631             P_auto_calc_int_flag,
1632             P_set_of_books_id,
1633             P_future_pay_ccid,
1634             P_last_updated_by,
1635             P_last_update_login,
1636             l_curr_calling_sequence,
1637             P_sequential_numbering, -- 1724353
1638             P_accounting_event_id,  -- Events Project
1639             P_org_id);
1640 
1641         EXIT WHEN (l_next > LENGTH(P_payment_num_list));
1642         l_pos := l_next + 1;
1643 
1644       END LOOP;
1645 
1646     ELSIF (P_invoice_id_list IS NOT NULL) THEN
1647       --
1648       -- Parse P_invoice_id_list
1649       --
1650       LOOP
1651         l_next := INSTR(P_invoice_id_list, ' ', l_pos);
1652         IF (l_next = 0) THEN
1653           l_next := LENGTH(P_invoice_id_list) + 1;
1654         END IF;
1655 
1656         l_invoice_id := TO_NUMBER(SUBSTR(P_invoice_id_list,
1657                                        l_pos,
1658                l_next - l_pos));
1659 
1660   l_log_msg := 'Get discount available for invoice_id:' ||
1661       to_char(l_invoice_id);
1662 
1663   Create_Single_Payment(l_invoice_id,
1664             NULL,
1665             P_check_id,
1666             P_payment_type_flag,
1667             P_payment_method,
1668             P_ce_bank_acct_use_id,
1669             P_bank_account_num,
1670             P_bank_account_type,
1671             P_bank_num,
1672             P_check_date,
1673             P_period_name,
1674             P_currency_code,
1675             P_base_currency_code,
1676             P_checkrun_name,
1677             P_doc_sequence_value,
1678             P_doc_sequence_id,
1679             P_exchange_rate,
1680             P_exchange_rate_type,
1681             P_exchange_date,
1682             P_take_discount,
1683             P_sys_auto_calc_int_flag,
1684             P_auto_calc_int_flag,
1685             P_set_of_books_id,
1686             P_future_pay_ccid,
1687             P_last_updated_by,
1688             P_last_update_login,
1689             l_curr_calling_sequence,
1690             P_sequential_numbering,
1691             P_accounting_event_id,   -- Events Project
1692             P_org_id);
1693 
1694         EXIT WHEN (l_next > LENGTH(P_invoice_id_list));
1695         l_pos := l_next + 1;
1696 
1697       END LOOP;
1698 
1699     END IF;
1700 
1701   EXCEPTION
1702     WHEN OTHERS THEN
1703       IF (SQLCODE <> -20001) THEN
1704         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
1705         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
1706         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
1707         FND_MESSAGE.SET_TOKEN('PARAMETERS',
1708     ' P_invoice_id_list = '         || P_invoice_id_list     ||
1709     ' P_payment_num_list = '        || P_payment_num_list      ||
1710     ' P_check_id = '                || P_check_id        ||
1711     ' P_payment_type_flag = '   || P_payment_type_flag     ||
1712     ' P_payment_method = '    || P_payment_method    ||
1713     ' P_bank_account_id = '   || P_ce_bank_acct_use_id   ||
1714     ' P_bank_account_num = '  || P_bank_account_num      ||
1715     ' P_bank_account_type = '   || P_bank_account_type     ||
1716     ' P_bank_num = '    || P_bank_num      ||
1717     ' P_check_date = '    || P_check_date      ||
1718     ' P_period_name = '     || P_period_name     ||
1719     ' P_currency_code = '     || P_currency_code       ||
1720     ' P_base_currency_code = '  || P_base_currency_code    ||
1721     ' P_checkrun_name = '     || P_checkrun_name     ||
1722     ' P_doc_sequence_value = '  || P_doc_sequence_value    ||
1723     ' P_doc_sequence_id = '   || P_doc_sequence_id     ||
1724     ' P_exchange_rate = '     || P_exchange_rate     ||
1725     ' P_exchange_rate_type = '  || P_exchange_rate_type    ||
1726     ' P_exchange_date = '     || P_exchange_date     ||
1727     ' P_take_discount = '     || P_take_discount         ||
1728     ' P_sys_auto_calc_int_flag = '  || P_sys_auto_calc_int_flag||
1729     ' P_auto_calc_int_flag = '  || P_auto_calc_int_flag    ||
1730     ' P_set_of_books_id = '   || P_set_of_books_id     ||
1731     ' P_future_pay_ccid = '   || P_future_pay_ccid     ||
1732     ' P_last_updated_by = '   || P_last_updated_by     ||
1733     ' P_last_update_login = '   || P_last_update_login
1734     );
1735   FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_log_msg);
1736       END IF;
1737       APP_EXCEPTION.RAISE_EXCEPTION;
1738   END AP_Create_Payments;
1739 
1740 END AP_PAY_IN_FULL_PKG;