DBA Data[Home] [Help]

PACKAGE BODY: APPS.AR_INVOICE_UTILS

Source


1 PACKAGE BODY AR_INVOICE_UTILS AS
2 /* $Header: ARXVINUB.pls 120.92.12020000.2 2012/07/26 03:05:47 jatian ship $ */
3 -- declare global variables.
4 
5 g_set_of_books_id       NUMBER;
6 g_org_id                NUMBER;
7 
8 /*Bug 5122918/4709987/3752043*/
9 pg_so_org_id             NUMBER := to_number(oe_profile.value('SO_ORGANIZATION_ID'));
10 
11 pg_debug VARCHAR2(1) := nvl(fnd_profile.value('AFLOG_ENABLED'),'N');
12 
13 
14 PROCEDURE debug (
15   p_message     IN VARCHAR2,
16   p_log_level   IN NUMBER default fnd_log.level_statement,
17   p_module_name IN VARCHAR2 default 'ar.plsql.InvoiceAPI') IS
18 
19 BEGIN
20 
21 /*Bug 3736074*/
22 /*
23 if ( (pg_debug = 'Y') and  ( p_log_level >= FND_LOG.G_CURRENT_RUNTIME_LEVEL )) then
24   FND_LOG.string(p_log_level,p_module_name, p_message);
25   --arp_util.debug( p_message);
26 end if;
27 */
28 
29 arp_standard.debug(p_message);
30 
31 END debug;
32 
33 PROCEDURE validate_gdf(
34       p_request_id           NUMBER,
35       x_errmsg               OUT NOCOPY VARCHAR2,
36       x_return_status        OUT NOCOPY VARCHAR2) IS
37 
38       l_return_code         NUMBER;
39 BEGIN
40     IF pg_debug = 'Y' THEN
41      debug ('ar_invoice_utils.validate_gdf(+)' );
42     END IF;
43     x_return_status := FND_API.G_RET_STS_SUCCESS;
44 
45     l_return_code := jg_zz_invoice_create.validate_gdff
46                 (p_request_id   =>  p_request_id);
47 
48     IF pg_debug = 'Y' THEN
49      debug ('ar_invoice_utils.validate_gdf(-)' );
50     END IF;
51     EXCEPTION
52         WHEN OTHERS THEN
53             x_errmsg := 'error in ar_invoice_utils.validate_gdf ' || sqlerrm;
54             x_return_status := fnd_api.g_ret_sts_unexp_error;
55             RETURN;
56 END;
57 
58 -- Bug # 3099975
59 -- ORASHID
60 -- 21-AUG-2003
61 
62 PROCEDURE validate_remit_to_address_id (
63   x_errmsg                    OUT NOCOPY  VARCHAR2,
64   x_return_status             OUT NOCOPY  VARCHAR2) AS
65 
66 BEGIN
67 
68   IF pg_debug = 'Y' THEN
69      debug ('ar_invoice_utils.validate_remit_to_address_id(+)' );
70   END IF;
71 
72   x_return_status := FND_API.G_RET_STS_SUCCESS;
73 
74   INSERT INTO ar_trx_errors_gt (
75     trx_header_id,
76     error_message,
77     invalid_value)
78     SELECT trx_header_id,
79            arp_standard.fnd_message('AR_INAPI_INVALID_REMIT_ADDR_ID'),
80            remit_to_address_id
81     FROM   ar_trx_header_gt gt
82     WHERE  gt.remit_to_address_id IS NOT NULL
83     AND    NOT EXISTS
84       (SELECT 'X'
85        FROM ar_active_remit_to_addresses_v arta
86        WHERE arta.address_id = gt.remit_to_address_id);
87 
88     IF pg_debug = 'Y' THEN
89       debug ('ar_invoice_utils.validate_remit_to_address_id(-)' );
90     END IF;
91 
92   EXCEPTION
93     WHEN OTHERS THEN
94       x_errmsg := 'error in ar_invoice_utils.validate_remit_to_address_id '
95         || sqlerrm;
96       x_return_status := fnd_api.g_ret_sts_unexp_error;
97       RETURN;
98 
99 END validate_remit_to_address_id;
100 
101 
102 PROCEDURE validate_trx_class (
103   x_errmsg                    OUT NOCOPY  VARCHAR2,
104   x_return_status             OUT NOCOPY  VARCHAR2) AS
105 
106 BEGIN
107 
108   IF pg_debug = 'Y' THEN
109      debug ('ar_invoice_utils.validate_trx_class(+)' );
110   END IF;
111 
112   x_return_status := FND_API.G_RET_STS_SUCCESS;
113 
114   INSERT INTO ar_trx_errors_gt (
115     trx_header_id,
116     error_message,
117     invalid_value)
118     SELECT trx_header_id,
119            arp_standard.fnd_message('AR_INAPI_INVALID_TRX_CLASS'),
120            trx_class
121     FROM   ar_trx_header_gt gt
122     WHERE  gt.trx_class NOT IN ('INV', 'DM' , 'CM')  -- added CM for ER 5869149
123     AND    gt.trx_class IS NOT NULL;
124 
125     IF pg_debug = 'Y' THEN
126       debug ('ar_invoice_utils.validate_trx_class(-)' );
127     END IF;
128 
129   EXCEPTION
130     WHEN OTHERS THEN
131       x_errmsg := 'Error in ar_invoice_utils.validate_trx_class '||sqlerrm;
132       x_return_status := fnd_api.g_ret_sts_unexp_error;
133       RETURN;
134 
135 END validate_trx_class;
136 
137 
138 PROCEDURE validate_ship_via (
139   x_errmsg                    OUT NOCOPY  VARCHAR2,
140   x_return_status             OUT NOCOPY  VARCHAR2) AS
141 
142 BEGIN
143 
144   IF pg_debug = 'Y' THEN
145      debug ('ar_invoice_utils.validate_ship_via(+)' );
146   END IF;
147 
148   x_return_status := FND_API.G_RET_STS_SUCCESS;
149 
150   INSERT INTO ar_trx_errors_gt (
151     trx_header_id,
152     error_message,
153     invalid_value)
154     SELECT trx_header_id,
155            arp_standard.fnd_message('AR_INAPI_INVALID_SHIP_VIA'),
156            ship_via
157     FROM   ar_trx_header_gt gt
158     WHERE  gt.ship_via IS NOT NULL
159     AND NOT EXISTS
160       (SELECT 'X'
161        FROM  org_freight orf
162        WHERE orf.freight_code = gt.ship_via
163        AND   orf.organization_id = pg_so_org_id  /*Bug4709987*/
164        AND   gt.trx_date <= nvl(trunc(orf.disable_date), gt.trx_date));
165 
166     IF pg_debug = 'Y' THEN
167       debug ('ar_invoice_utils.validate_ship_via(-)' );
168     END IF;
169 
170   EXCEPTION
171     WHEN OTHERS THEN
172       x_errmsg := 'error in ar_invoice_utils.validate_ship_via '|| sqlerrm;
173       x_return_status := fnd_api.g_ret_sts_unexp_error;
174       RETURN;
175 
176 END validate_ship_via;
177 
178 
179 PROCEDURE validate_fob_point (
180   x_errmsg                    OUT NOCOPY  VARCHAR2,
181   x_return_status             OUT NOCOPY  VARCHAR2) AS
182 
183 BEGIN
184 
185   IF pg_debug = 'Y' THEN
186      debug ('ar_invoice_utils.validate_fob_point(+)' );
187   END IF;
188 
189   x_return_status := FND_API.G_RET_STS_SUCCESS;
190 
191   INSERT INTO ar_trx_errors_gt (
192     trx_header_id,
193     error_message,
194     invalid_value)
195     SELECT trx_header_id,
196            arp_standard.fnd_message('AR_INAPI_INVALID_FOB'),
197            fob_point
198     FROM   ar_trx_header_gt gt
199     WHERE  gt.fob_point IS NOT NULL
200     AND NOT EXISTS
201       (SELECT 'X'
202        FROM   ar_lookups
203        WHERE  lookup_code = gt.fob_point
204        AND    lookup_type = 'FOB'
205        AND    gt.trx_date
206          BETWEEN start_date_active and nvl(end_date_active, gt.trx_date));
207 
208     IF pg_debug = 'Y' THEN
209       debug ('ar_invoice_utils.validate_fob_point(-)' );
210     END IF;
211 
212   EXCEPTION
213     WHEN OTHERS THEN
214       x_errmsg := 'error in ar_invoice_utils.validate_fob_point '|| sqlerrm;
215       x_return_status := fnd_api.g_ret_sts_unexp_error;
216       RETURN;
217 
218 END validate_fob_point;
219 
220 
221 PROCEDURE validate_ussgl_code (
222   x_errmsg                    OUT NOCOPY  VARCHAR2,
223   x_return_status             OUT NOCOPY  VARCHAR2) AS
224 
225 BEGIN
226 
227   IF pg_debug = 'Y' THEN
228     debug ('ar_invoice_utils.validate_ussgl_code(+)' );
229   END IF;
230 
231   x_return_status := FND_API.G_RET_STS_SUCCESS;
232 
233   INSERT INTO ar_trx_errors_gt (
234     trx_header_id,
235     error_message,
236     invalid_value)
237     SELECT trx_header_id,
238            arp_standard.fnd_message('AR_INAPI_INVALID_USSGL_CODE'),
239            default_ussgl_transaction_code
240     FROM   ar_trx_header_gt gt
241     WHERE  gt.default_ussgl_transaction_code IS NOT NULL
242     AND NOT EXISTS
243       (SELECT 'X'
244        FROM  gl_ussgl_transaction_codes gutc
245        WHERE gutc.ussgl_transaction_code = gt.default_ussgl_transaction_code
246        AND   gutc.chart_of_accounts_id = arp_global.chart_of_accounts_id
247        AND   gt.trx_date
248          BETWEEN NVL(gutc.start_date_active, gt.trx_date)
249          AND NVL(gutc.end_date_active, gt.trx_date));
250 
251     IF pg_debug = 'Y' THEN
252       debug ('ar_invoice_utils.validate_ussgl_code(-)' );
253     END IF;
254 
255   EXCEPTION
256     WHEN OTHERS THEN
257       x_errmsg := 'error in ar_invoice_utils.validate_ussgl_code '|| sqlerrm;
258       x_return_status := fnd_api.g_ret_sts_unexp_error;
259       RETURN;
260 
261 END validate_ussgl_code;
262 
263 
264 PROCEDURE validate_payment_method (
265   x_errmsg                    OUT NOCOPY  VARCHAR2,
266   x_return_status             OUT NOCOPY  VARCHAR2) AS
267 
268 BEGIN
269 
270   IF pg_debug = 'Y' THEN
271      debug ('ar_invoice_utils.validate_payment_method(+)' );
272   END IF;
273 
274   x_return_status := FND_API.G_RET_STS_SUCCESS;
275 
276   INSERT INTO ar_trx_errors_gt (
277     trx_header_id,
278     error_message,
279     invalid_value)
280     SELECT trx_header_id,
281            arp_standard.fnd_message('AR_INVALID_PAYMENT_METHOD'),
282            receipt_method_id
283     FROM   ar_trx_header_gt gt
284     WHERE  gt.receipt_method_id IS NOT NULL
285     AND NOT EXISTS
286       (SELECT 'X'
287        FROM   ar_receipt_methods rm
288        WHERE  rm.receipt_method_id = gt.receipt_method_id
289        AND    gt.trx_date BETWEEN NVL(rm.start_date, gt.trx_date)
290               AND NVL(rm.end_date, gt.trx_date));
291 
292   INSERT INTO ar_trx_errors_gt (
293     trx_header_id,
294     error_message,
295     invalid_value)
296     SELECT trx_header_id,
297            arp_standard.fnd_message('AR_BOE_OBSOLETE'),
298            receipt_method_id
299     FROM   ar_trx_header_gt gt
300     WHERE  gt.receipt_method_id IS NOT NULL
301       AND  arpt_sql_func_util.check_boe_paymeth(gt.receipt_method_id) = 'Y';
302 
303 /* PAYMENT UPTAKE */
304 
305   INSERT INTO ar_trx_errors_gt (
306     trx_header_id,
307     error_message,
308     invalid_value)
309     SELECT trx_header_id,
310            arp_standard.fnd_message('AR_INVALID_PAYMENT_METHOD'),
311            receipt_method_id
312     FROM   ar_trx_header_gt gt
313     WHERE  gt.payment_trxn_extension_id  IS  NOT NULL
314     AND   EXISTS
315       (SELECT 'X'
316        FROM   ar_receipt_methods rm,
317               ar_receipt_classes rc
318        WHERE  rm.receipt_method_id = gt.receipt_method_id
319        AND    rm.receipt_class_id  = rc.receipt_class_id
320        AND    rc.creation_method_code = 'MANUAL' );
321 
322   INSERT INTO ar_trx_errors_gt (
323     trx_header_id,
324     error_message,
325     invalid_value)
326     SELECT trx_header_id,
327            arp_standard.fnd_message('AR_INVALID_PAYMENT_METHOD'),
328            receipt_method_id
329     FROM   ar_trx_header_gt gt
330     WHERE  gt.payment_trxn_extension_id  IS  NOT NULL
331     AND   EXISTS
332       (SELECT 'X'
333        FROM   ar_receipt_methods rm,
334               iby_trxn_extensions_v iby
335        WHERE  rm.receipt_method_id = gt.receipt_method_id
336        AND    iby.trxn_extension_id= gt.payment_trxn_extension_id
337        AND    iby.PAYMENT_CHANNEL_CODE <> rm.payment_channel_code );
338 
339   INSERT INTO ar_trx_errors_gt (
340     trx_header_id,
341     error_message,
342     invalid_value)
343     SELECT trx_header_id,
344            arp_standard.fnd_message('AR_INVALID_PAYMENT_METHOD'),
345            receipt_method_id
346     FROM   ar_trx_header_gt gt
347     WHERE  gt.receipt_method_id IS NOT NULL
348     AND    gt.payment_trxn_extension_id IS NULL
349     AND   EXISTS
350       (SELECT 'X'
351        FROM   ar_receipt_methods rm,
352               ar_receipt_classes rc
353        WHERE  rm.receipt_method_id = gt.receipt_method_id
354        AND    rm.receipt_class_id  = rc.receipt_class_id
355        AND    rc.creation_method_code = 'AUTOMATIC' );
356 
357   INSERT INTO ar_trx_errors_gt (
358     trx_header_id,
359     error_message,
360     invalid_value)
361     SELECT trx_header_id,
362            arp_standard.fnd_message('AR_INVALID_PAYMENT_METHOD'),
363            receipt_method_id
364     FROM   ar_trx_header_gt gt
365     WHERE  gt.receipt_method_id IS NULL
366     AND    gt.payment_trxn_extension_id IS NOT NULL;
367 
368     IF pg_debug = 'Y' THEN
369       debug ('ar_invoice_utils.validate_payment_method(-)' );
370     END IF;
371 
372   EXCEPTION
373     WHEN OTHERS THEN
374       x_errmsg := 'error in ar_invoice_utils.validate_payment_method '
375        || sqlerrm;
376       x_return_status := fnd_api.g_ret_sts_unexp_error;
377       RETURN;
378 
379 END validate_payment_method;
380 
381 /* 4188835 etax uptake */
382 PROCEDURE validate_legal_entity (
383   x_errmsg                    OUT NOCOPY  VARCHAR2,
384   x_return_status             OUT NOCOPY  VARCHAR2) AS
385 
386 BEGIN
387 
388   IF pg_debug = 'Y' THEN
389      debug ('ar_invoice_utils.validate_legal_entity(+)' );
390   END IF;
391 
392   x_return_status := FND_API.G_RET_STS_SUCCESS;
393 
394   INSERT INTO ar_trx_errors_gt (
395     trx_header_id,
396     error_message,
397     invalid_value)
398     SELECT trx_header_id,
399            arp_standard.fnd_message('AR_INVALID_LEGAL_ENTITY'),
400            legal_entity_id
401     FROM   ar_trx_header_gt gt
402     WHERE  gt.legal_entity_id IS NOT NULL
403     AND NOT EXISTS (
404                SELECT 'valid LE'
405                FROM   XLE_LE_OU_LEDGER_V LE
406                WHERE  LE.legal_entity_id = GT.legal_entity_id
407                AND    LE.operating_unit_id = GT.org_id);
408 
409     IF pg_debug = 'Y' THEN
410       debug ('ar_invoice_utils.validate_legal_entity(-)' );
411     END IF;
412 
413   EXCEPTION
414     WHEN OTHERS THEN
415       x_errmsg := 'error in ar_invoice_utils.validate_legal_entity '
416        || sqlerrm;
417       x_return_status := fnd_api.g_ret_sts_unexp_error;
418       RETURN;
419 
420 END validate_legal_entity;
421 
422 PROCEDURE validate_cust_bank_account_id (
423   x_errmsg           OUT NOCOPY  VARCHAR2,
424   x_return_status    OUT NOCOPY  VARCHAR2) AS
425 
426 BEGIN
427 
428   IF pg_debug = 'Y' THEN
429      debug ('ar_invoice_utils.validate_cust_bank_account_id(+)' );
430   END IF;
431 
432   x_return_status := FND_API.G_RET_STS_SUCCESS;
433 /* payment uptake removed the validation for cust_bank_account_id */
434 
435 END validate_cust_bank_account_id;
436 
437 
438 PROCEDURE validate_paying_customer_id (
439   p_trx_system_param_rec ar_invoice_default_pvt.trx_system_parameters_rec_type,
440   x_errmsg               OUT NOCOPY  VARCHAR2,
441   x_return_status        OUT NOCOPY  VARCHAR2) AS
442 
443 BEGIN
444 
445   IF pg_debug = 'Y' THEN
446      debug ('ar_invoice_utils.validate_paying_customer_id(+)' );
447   END IF;
448 
449   x_return_status := FND_API.G_RET_STS_SUCCESS;
450   -- Removed the prospect join as per the bug 3310138
451   IF p_trx_system_param_rec.pay_unrelated_invoices_flag = 'Y'
452   THEN
453         INSERT INTO ar_trx_errors_gt (
454             trx_header_id,
455             error_message,
456             invalid_value)
457         SELECT trx_header_id,
458             arp_standard.fnd_message('AR_INAPI_INVALID_PAYING_CUS_ID'),
459             paying_customer_id
460         FROM   ar_trx_header_gt gt
461         WHERE  gt.paying_customer_id IS NOT NULL
462         AND    NOT EXISTS (
463                     SELECT 'X'
464                     FROM  hz_cust_accounts cust_acct
465                     WHERE cust_acct.cust_account_id = gt.paying_customer_id
466                     AND   cust_acct.status = 'A' );
467    ELSIF p_trx_system_param_rec.pay_unrelated_invoices_flag = 'N'
468    THEN
469         INSERT INTO ar_trx_errors_gt (
470             trx_header_id,
471             error_message,
472             invalid_value)
473         SELECT trx_header_id,
474            arp_standard.fnd_message('AR_INAPI_INVALID_PAYING_CUS_ID'),
475            paying_customer_id
476         FROM   ar_trx_header_gt gt
477         WHERE  gt.paying_customer_id IS NOT NULL
478         AND NOT EXISTS
479             (   SELECT 'X'
480                 FROM  hz_cust_accounts cust_acct
481                 WHERE cust_acct.cust_account_id = gt.paying_customer_id
482                 AND   cust_acct.status = 'A'
483                 AND cust_acct.cust_account_id IN
484                     (
485                         SELECT cr.cust_account_id
486                         FROM hz_cust_acct_relate cr
487                         WHERE cr.related_cust_account_id = gt.bill_to_customer_id
488                         AND cr.status = 'A'
489                         AND cr.bill_to_flag ='Y'
490                         UNION
491                         SELECT to_number(gt.bill_to_customer_id)
492                         FROM DUAL
493                         UNION
494                         SELECT acc.cust_account_id
495                         FROM ar_paying_relationships_v rel, hz_cust_accounts acc
496                         WHERE rel.party_id = acc.party_id
497                         AND rel.related_cust_account_id = gt.bill_to_customer_id
498                         AND gt.trx_date BETWEEN effective_start_date
499                         AND effective_end_date)
500             );
501     END IF;
502     IF pg_debug = 'Y' THEN
503       debug ('ar_invoice_utils.validate_paying_customer_id(-)' );
504     END IF;
505 
506   EXCEPTION
507     WHEN OTHERS THEN
508       x_errmsg := 'error in ar_invoice_utils.validate_paying_customer_id '||
509         sqlerrm;
510       x_return_status := fnd_api.g_ret_sts_unexp_error;
511       RETURN;
512 
513 END validate_paying_customer_id;
514 
515 
516 PROCEDURE validate_paying_site_use_id (
517   p_trx_system_param_rec ar_invoice_default_pvt.trx_system_parameters_rec_type,
518   x_errmsg               OUT NOCOPY  VARCHAR2,
519   x_return_status        OUT NOCOPY  VARCHAR2) AS
520 
521 BEGIN
522 
523   IF pg_debug = 'Y' THEN
524      debug ('ar_invoice_utils.validate_paying_site_use_id(+)' );
525   END IF;
526 
527   x_return_status := FND_API.G_RET_STS_SUCCESS;
528 
529   INSERT INTO ar_trx_errors_gt (
530     trx_header_id,
531     error_message,
532     invalid_value)
533     SELECT trx_header_id,
534            arp_standard.fnd_message('AR_INAPI_INVALID_PAYING_SIT_ID'),
535            paying_site_use_id
536     FROM   ar_trx_header_gt gt
537     WHERE  gt.paying_site_use_id IS NOT NULL
538     AND NOT EXISTS
539       (SELECT 'X'
540        FROM   hz_cust_acct_sites acct_site,
541               hz_cust_site_uses site_uses
542        WHERE  acct_site.cust_acct_site_id = site_uses.cust_acct_site_id
543        AND    acct_site.cust_account_id = gt.paying_customer_id
544        AND    site_uses.site_use_id = gt.paying_site_use_id
545        AND    site_uses.site_use_code IN
546                ('BILL_TO', decode(p_trx_system_param_rec.br_enabled_flag,
547                 'DRAWEE', 'PAYING')));
548 
549   IF pg_debug = 'Y' THEN
550     debug ('ar_invoice_utils.validate_paying_site_use_id(-)' );
551   END IF;
552 
553   EXCEPTION
554     WHEN OTHERS THEN
555       x_errmsg := 'error in ar_invoice_utils.validate_paying_site_use_id '||
556         sqlerrm;
557       x_return_status := fnd_api.g_ret_sts_unexp_error;
558       RETURN;
559 
560 END validate_paying_site_use_id;
561 
562 
563 PROCEDURE validate_sold_to_customer_id (
564   x_errmsg               OUT NOCOPY  VARCHAR2,
565   x_return_status        OUT NOCOPY  VARCHAR2) AS
566 
567 BEGIN
568 
569   IF pg_debug = 'Y' THEN
570      debug ('ar_invoice_utils.validate_sold_to_customer_id(+)' );
571   END IF;
572 
573   x_return_status := FND_API.G_RET_STS_SUCCESS;
574 
575   INSERT INTO ar_trx_errors_gt (
576     trx_header_id,
577     error_message,
578     invalid_value)
579     SELECT trx_header_id,
580            arp_standard.fnd_message('AR_DAPI_SOLD_CUST_ID_INVALID'),
581            sold_to_customer_id
582     FROM   ar_trx_header_gt gt
583     WHERE  gt.sold_to_customer_id IS NOT NULL
584     AND NOT EXISTS
585       (SELECT 'X'
586        FROM  hz_cust_accounts cust_acct
587        WHERE cust_acct.cust_account_id = gt.sold_to_customer_id
588        AND   cust_acct.status = 'A');
589 
590   IF pg_debug = 'Y' THEN
591     debug ('ar_invoice_utils.validate_sold_to_customer_id(-)' );
592   END IF;
593 
594   EXCEPTION
595     WHEN OTHERS THEN
596       x_errmsg := 'error in ar_invoice_utils.validate_sold_to_customer_id '||
597         sqlerrm;
598       x_return_status := fnd_api.g_ret_sts_unexp_error;
599       RETURN;
600 
601 END validate_sold_to_customer_id;
602 
603 
604 PROCEDURE validate_ship_to_customer_name (
605   x_errmsg               OUT NOCOPY  VARCHAR2,
606   x_return_status        OUT NOCOPY  VARCHAR2) AS
607 
608 BEGIN
609 
610   IF pg_debug = 'Y' THEN
611      debug ('ar_invoice_utils.validate_ship_to_customer_name(+)' );
612   END IF;
613 
614   x_return_status := FND_API.G_RET_STS_SUCCESS;
615 
616   INSERT INTO ar_trx_errors_gt (
617     trx_header_id,
618     error_message,
619     invalid_value)
620     SELECT trx_header_id,
621            arp_standard.fnd_message('AR_INAPI_INV_SHIP_TO_CUST_NAME'),
622            ship_to_customer_name
623     FROM   ar_trx_header_gt gt
624     WHERE  gt.ship_to_customer_name IS NOT NULL
625     AND NOT EXISTS
626       (SELECT 'X'
627        FROM  hz_cust_accounts cust_acct,
628              hz_parties party
629        WHERE cust_acct.party_id = party.party_id
630        AND   party.party_name = gt.ship_to_customer_name);
631 
632   IF pg_debug = 'Y' THEN
633     debug ('ar_invoice_utils.validate_ship_to_customer_name(-)' );
634   END IF;
635 
636   EXCEPTION
637     WHEN OTHERS THEN
638       x_errmsg := 'error in ar_invoice_utils.validate_ship_to_customer_name '
639         || sqlerrm;
640       x_return_status := fnd_api.g_ret_sts_unexp_error;
641       RETURN;
642 
643 END validate_ship_to_customer_name;
644 
645 
646 PROCEDURE validate_ship_to_cust_number (
647   x_errmsg               OUT NOCOPY  VARCHAR2,
648   x_return_status        OUT NOCOPY  VARCHAR2) AS
649 
650 BEGIN
651 
652   IF pg_debug = 'Y' THEN
653      debug ('ar_invoice_utils.validate_ship_to_cust_number(+)' );
654   END IF;
655 
656   x_return_status := FND_API.G_RET_STS_SUCCESS;
657 
658   INSERT INTO ar_trx_errors_gt (
659     trx_header_id,
660     error_message,
661     invalid_value)
662     SELECT trx_header_id,
663            arp_standard.fnd_message('AR_INAPI_INV_SHIP_TO_CUST_NUM'),
664            ship_to_account_number
665     FROM   ar_trx_header_gt gt
666     WHERE  gt.ship_to_account_number IS NOT NULL
667     AND NOT EXISTS
668       (SELECT 'X'
669        FROM  hz_cust_accounts cust_acct
670        WHERE cust_acct.account_number = gt.ship_to_account_number);
671 
672   IF pg_debug = 'Y' THEN
673     debug ('ar_invoice_utils.validate_ship_to_cust_number(-)' );
674   END IF;
675 
676   EXCEPTION
677     WHEN OTHERS THEN
678       x_errmsg := 'error in ar_invoice_utils.validate_ship_to_cust_number '
679         || sqlerrm;
680       x_return_status := fnd_api.g_ret_sts_unexp_error;
681       RETURN;
682 
683 END validate_ship_to_cust_number;
684 
685 
686 PROCEDURE validate_bill_to_customer_name (
687   x_errmsg               OUT NOCOPY  VARCHAR2,
688   x_return_status        OUT NOCOPY  VARCHAR2) AS
689 
690 BEGIN
691 
692   IF pg_debug = 'Y' THEN
693      debug ('ar_invoice_utils.validate_bill_to_customer_name(+)' );
694   END IF;
695 
696   x_return_status := FND_API.G_RET_STS_SUCCESS;
697 
698   INSERT INTO ar_trx_errors_gt (
699     trx_header_id,
700     error_message,
701     invalid_value)
702     SELECT trx_header_id,
703            arp_standard.fnd_message('AR_INAPI_INV_BILL_TO_CUST_NAME'),
704            bill_to_customer_name
705     FROM   ar_trx_header_gt gt
706     WHERE  gt.bill_to_customer_name IS NOT NULL
707     AND NOT EXISTS
708       (SELECT 'X'
709        FROM  hz_cust_accounts cust_acct,
710              hz_parties party
711        WHERE cust_acct.party_id = party.party_id
712        AND   party.party_name = gt.bill_to_customer_name);
713 
714   IF pg_debug = 'Y' THEN
715     debug ('ar_invoice_utils.validate_bill_to_customer_name(-)' );
716   END IF;
717 
718   EXCEPTION
719     WHEN OTHERS THEN
720       x_errmsg := 'error in ar_invoice_utils.validate_bill_to_customer_name '
721         || sqlerrm;
722       x_return_status := fnd_api.g_ret_sts_unexp_error;
723       RETURN;
724 
725 END validate_bill_to_customer_name;
726 
727 
728 PROCEDURE validate_bill_to_cust_number (
729   x_errmsg               OUT NOCOPY  VARCHAR2,
730   x_return_status        OUT NOCOPY  VARCHAR2) AS
731 
732 BEGIN
733 
734   IF pg_debug = 'Y' THEN
735      debug ('ar_invoice_utils.validate_bill_to_cust_number(+)' );
736   END IF;
737 
738   x_return_status := FND_API.G_RET_STS_SUCCESS;
739 
740   INSERT INTO ar_trx_errors_gt (
741     trx_header_id,
742     error_message,
743     invalid_value)
744     SELECT trx_header_id,
745            arp_standard.fnd_message('AR_INAPI_INV_BILL_TO_CUST_NUM'),
746            bill_to_account_number
747     FROM   ar_trx_header_gt gt
748     WHERE  gt.bill_to_account_number IS NOT NULL
749     AND NOT EXISTS
750       (SELECT 'X'
751        FROM  hz_cust_accounts cust_acct
752        WHERE cust_acct.account_number = gt.bill_to_account_number);
753 
754   IF pg_debug = 'Y' THEN
755     debug ('ar_invoice_utils.validate_bill_to_cust_number(-)' );
756   END IF;
757 
758   EXCEPTION
759     WHEN OTHERS THEN
760       x_errmsg := 'error in ar_invoice_utils.validate_bill_to_cust_number '
761         || sqlerrm;
762       x_return_status := fnd_api.g_ret_sts_unexp_error;
763       RETURN;
764 
765 END validate_bill_to_cust_number;
766 
767 
768 PROCEDURE validate_bill_to_contact_id (
769   x_errmsg               OUT NOCOPY  VARCHAR2,
770   x_return_status        OUT NOCOPY  VARCHAR2) AS
771 
772 BEGIN
773 
774   IF pg_debug = 'Y' THEN
775      debug ('ar_invoice_utils.validate_bill_to_contact_id(+)' );
776   END IF;
777 
778   x_return_status := FND_API.G_RET_STS_SUCCESS;
779 
780   INSERT INTO ar_trx_errors_gt (
781     trx_header_id,
782     error_message,
783     invalid_value)
784     SELECT trx_header_id,
785            arp_standard.fnd_message('AR_INAPI_BAD_BLL_TO_CONTACT_ID'),
786            bill_to_contact_id
787     FROM   ar_trx_header_gt gt
788     WHERE  gt.bill_to_contact_id IS NOT NULL
789     AND NOT EXISTS
790       (SELECT 'X'
791        FROM hz_cust_account_roles acct_role,
792             hz_parties       	  party,
793             hz_relationships 	  rel,
794             hz_org_contacts  	  org_cont,
795             hz_parties       	  rel_party
796        WHERE acct_role.party_id = rel.party_id
797        AND   acct_role.role_type = 'CONTACT'
798        and org_cont.party_relationship_id = rel.relationship_id
799        and rel.subject_id = party.party_id
800        and rel.party_id = rel_party.party_id
801        and rel.subject_table_name = 'HZ_PARTIES'
802        and rel.object_table_name = 'HZ_PARTIES'
803        and rel.directional_flag = 'F'
804        and acct_role.cust_account_id = gt.bill_to_customer_id
805        and ( acct_role.cust_acct_site_id = gt.bill_to_address_id
806              or acct_role.cust_acct_site_id IS NULL )
807        AND acct_role.status = 'A');
808 
809   IF pg_debug = 'Y' THEN
810     debug ('ar_invoice_utils.validate_bill_to_contact_id(-)' );
811   END IF;
812 
813   EXCEPTION
814     WHEN OTHERS THEN
815       x_errmsg := 'error in ar_invoice_utils.validate_bill_to_contact_id '||
816         sqlerrm;
817       x_return_status := fnd_api.g_ret_sts_unexp_error;
818       RETURN;
819 
820 END validate_bill_to_contact_id;
821 
822 
823 PROCEDURE validate_ship_to_contact_id (
824   x_errmsg               OUT NOCOPY  VARCHAR2,
825   x_return_status        OUT NOCOPY  VARCHAR2) AS
826 
827 BEGIN
828 
829   IF pg_debug = 'Y' THEN
830      debug ('ar_invoice_utils.validate_ship_to_contact_id(+)' );
831   END IF;
832 
833   x_return_status := FND_API.G_RET_STS_SUCCESS;
834 
835   INSERT INTO ar_trx_errors_gt (
836     trx_header_id,
837     error_message,
838     invalid_value)
839     SELECT trx_header_id,
840            arp_standard.fnd_message('AR_INAPI_BAD_SHP_TO_CONTACT_ID'),
841            ship_to_contact_id
842     FROM   ar_trx_header_gt gt
843     WHERE  gt.ship_to_contact_id IS NOT NULL
844     AND NOT EXISTS
845       (SELECT 'X'
846        FROM hz_cust_account_roles acct_role,
847             hz_parties       	  party,
848             hz_relationships 	  rel,
849             hz_org_contacts  	  org_cont,
850             hz_parties       	  rel_party
851        WHERE acct_role.party_id = rel.party_id
852        AND   acct_role.role_type = 'CONTACT'
853        and org_cont.party_relationship_id = rel.relationship_id
854        and rel.subject_id = party.party_id
855        and rel.party_id = rel_party.party_id
856        and rel.subject_table_name = 'HZ_PARTIES'
857        and rel.object_table_name = 'HZ_PARTIES'
858        and rel.directional_flag = 'F'
859        and acct_role.cust_account_id = gt.ship_to_customer_id
860        and ( acct_role.cust_acct_site_id = gt.ship_to_address_id
861              or acct_role.cust_acct_site_id IS NULL )
862        AND acct_role.status = 'A');
863 
864   IF pg_debug = 'Y' THEN
865     debug ('ar_invoice_utils.validate_ship_to_contact_id(-)' );
866   END IF;
867 
868   EXCEPTION
869     WHEN OTHERS THEN
870       x_errmsg := 'error in ar_invoice_utils.validate_ship_to_contact_id '||
871         sqlerrm;
872       x_return_status := fnd_api.g_ret_sts_unexp_error;
873       RETURN;
874 
875 END validate_ship_to_contact_id;
876 
877 
878 PROCEDURE validate_exchange_rate_type (
879   x_errmsg               OUT NOCOPY  VARCHAR2,
880   x_return_status        OUT NOCOPY  VARCHAR2) AS
881 
882 BEGIN
883 
884   IF pg_debug = 'Y' THEN
885      debug ('ar_invoice_utils.validate_exchange_rate_type(+)' );
886   END IF;
887 
888   x_return_status := FND_API.G_RET_STS_SUCCESS;
889 
890   INSERT INTO ar_trx_errors_gt (
891     trx_header_id,
892     error_message,
893     invalid_value)
894     SELECT trx_header_id,
895            arp_standard.fnd_message('AR_INAPI_INV_XCHNG_RATE_TYPE'),
896            exchange_rate_type
897     FROM   ar_trx_header_gt gt
898     WHERE  gt.exchange_rate_type IS NOT NULL
899     AND NOT EXISTS
900       (SELECT 'X'
901        FROM   gl_daily_conversion_types
902        WHERE  conversion_type <> 'EMU FIXED'
903        AND    conversion_type = gt.exchange_rate_type); /*Bug 4517001*/
904 
905   IF pg_debug = 'Y' THEN
906     debug ('ar_invoice_utils.validate_exchange_rate_type(-)' );
907   END IF;
908 
909   EXCEPTION
910     WHEN OTHERS THEN
911       x_errmsg := 'error in ar_invoice_utils.validate_exchange_rate_type '||
912         sqlerrm;
913       x_return_status := fnd_api.g_ret_sts_unexp_error;
914       RETURN;
915 
916 END validate_exchange_rate_type;
917 
918 
919 PROCEDURE validate_doc_sequence_value (
920   p_trx_system_param_rec ar_invoice_default_pvt.trx_system_parameters_rec_type,
921   p_trx_profile_rec      ar_invoice_default_pvt.trx_profile_rec_type,
922   x_errmsg               OUT NOCOPY VARCHAR2,
923   x_return_status        OUT NOCOPY VARCHAR2 ) IS
924 
925   CURSOR header_rows IS
926     SELECT *
927     FROM  ar_trx_header_gt gt
928     WHERE gt.doc_sequence_value IS NOT NULL;
929 
930   l_seq_num_profile fnd_profile_option_values.profile_option_value%type;
931 
932   l_doc_seq_ret_stat   	NUMBER;
933   l_doc_sequence_id  	NUMBER;
934   l_doc_sequence_name  	VARCHAR2(50);
935   l_doc_sequence_type  	VARCHAR2(50);
936   l_doc_sequence_value 	NUMBER;
937   l_db_sequence_name  	VARCHAR2(50);
938   l_seq_ass_id  	NUMBER;
939   l_prd_tab_name  	VARCHAR2(50);
940   l_aud_tab_name  	VARCHAR2(50);
941   l_msg_flag      	VARCHAR2(1);
942 
943 BEGIN
944 
945   -- Accept document sequence value only when sequence numbering
946   -- is manual and sequence numbering turned on.
947 
948   l_seq_num_profile := NVL( p_trx_profile_rec.ar_unique_seq_numbers, 'N');
949 
950   FOR header_rec IN header_rows LOOP
951 
952     l_doc_seq_ret_stat:= fnd_seqnum.get_seq_info (
953       222,
954       header_rec.cust_trx_type_name,
955       p_trx_system_param_rec.set_of_books_id,
956       'M',
957       trunc(header_rec.trx_date),
958       l_doc_sequence_id,
959       l_doc_sequence_type,
960       l_doc_sequence_name,
961       l_db_sequence_name,
962       l_seq_ass_id,
963       l_prd_tab_name,
964       l_aud_tab_name,
965       l_msg_flag,
966       'Y',
967       'Y');
968 
969     IF (l_seq_num_profile = 'N')
970         AND SUBSTRB(l_doc_sequence_type,1,1) IN ( 'A', 'G') THEN
971 
972       INSERT INTO ar_trx_errors_gt (
973         trx_header_id,
974         error_message,
975         invalid_value)
976       VALUES
977         (header_rec.trx_header_id,
978          arp_standard.fnd_message('AR', 'AR_RAPI_DOC_SEQ_AUTOMATIC'),
979          header_rec.doc_sequence_value);
980 
981     END IF;
982 
983   END LOOP;
984 
985 END validate_doc_sequence_value;
986 
987 PROCEDURE validate_bfb IS
988 BEGIN
989 
990   IF pg_debug = 'Y' THEN
991      debug ('validate_bfb(+)');
992   END IF;
993 
994   -- R12:BFB : billing_date is required if BFB is event-based
995   INSERT INTO ar_trx_errors_gt (
996     trx_header_id,
997     error_message,
998     invalid_value)
999     SELECT trx_header_id,
1000            arp_standard.fnd_message('AR_TAPI_BFB_BILLING_DATE_REQD'),
1001            billing_date
1002     FROM ar_trx_header_gt
1003     WHERE term_id IS NOT NULL
1004     AND billing_date IS NULL
1005     AND ar_bfb_utils_pvt.is_payment_term_bfb(term_id) = 'Y'
1006     AND nvl(ar_bfb_utils_pvt.get_cycle_type (ar_bfb_utils_pvt.get_billing_cycle(term_id)),'XXX') = 'EVENT';
1007 
1008   -- R12:BFB : BFB is not allowed with open Rec = No
1009   INSERT INTO ar_trx_errors_gt (
1010     trx_header_id,
1011     error_message,
1012     invalid_value)
1013     SELECT trx_header_id,
1014            arp_standard.fnd_message('AR_TAPI_BFB_OPEN_REC'),
1015            term_id
1016     FROM ar_trx_header_gt
1017     WHERE term_id IS NOT NULL
1018     AND cust_trx_type_id IS NOT NULL
1019     AND ar_bfb_utils_pvt.is_payment_term_bfb(term_id) = 'Y'
1020     AND ar_bfb_utils_pvt.get_open_rec(cust_trx_type_id) = 'N';
1021 
1022   -- R12:BFB : billing_date validation
1023   INSERT INTO ar_trx_errors_gt (
1024     trx_header_id,
1025     error_message,
1026     invalid_value)
1027     SELECT trx_header_id,
1028            arp_standard.fnd_message('AR_TAPI_BFB_BILLING_DATE_INV'),
1029            billing_date
1030     FROM ar_trx_header_gt
1031     WHERE term_id IS NOT NULL
1032     AND billing_date IS NOT NULL
1033     AND
1034     (
1035        (-- billing_date should be null for non-BFB
1036         ar_bfb_utils_pvt.is_payment_term_bfb(term_id) = 'N'
1037        )
1038        OR
1039        (-- BFB is not valid for open_rec = No, so there should be NO billing date either
1040         cust_trx_type_id IS NOT NULL
1041         AND ar_bfb_utils_pvt.is_payment_term_bfb(term_id) = 'Y'
1042         AND ar_bfb_utils_pvt.get_open_rec(cust_trx_type_id) = 'N'
1043        )
1044     );
1045 
1046   IF pg_debug = 'Y' THEN
1047      debug ('validate_bfb(-)');
1048   END IF;
1049 
1050 END validate_bfb;
1051 
1052 PROCEDURE validate_dependent_parameters (
1053   p_trx_system_param_rec ar_invoice_default_pvt.trx_system_parameters_rec_type,
1054   x_errmsg          OUT NOCOPY  VARCHAR2,
1055   x_return_status   OUT NOCOPY  VARCHAR2 ) IS
1056 
1057 BEGIN
1058 
1059   -- Reject Trx Number as input if the batch source passed has
1060   -- automatic transaction numbering:
1061 
1062 /* 4536358 - both trx_number validations tested using
1063    ra_batch_sources_all without an org_Id join.  This resulted
1064    in false error returns if even a single batch source of that
1065    ID was set Y (or N) respectively.  In other words, the EXISTS
1066    would test all batch sources of that ID and fail if even one
1067    had the opposite setting as the record for the current org.
1068 
1069    REMOVED _ALL from ra_batch_sources in exists clauses */
1070 
1071   INSERT INTO ar_trx_errors_gt (
1072     trx_header_id,
1073     error_message,
1074     invalid_value)
1075     SELECT trx_header_id,
1076            DECODE(b.auto_trx_numbering_flag, 'Y',
1077                arp_standard.fnd_message('AR_INAPI_TRX_NUM_NOT_REQUIRED'),
1078                arp_standard.fnd_message('AR_TW_NULL_TRX_NUMBER')),
1079            trx_number
1080     FROM   ar_trx_header_gt gt,
1081            ra_batch_sources b
1082     WHERE  b.batch_source_id = gt.batch_source_id
1083     AND   ((gt.trx_number IS NULL AND
1084             NVL(b.auto_trx_numbering_flag,'N') = 'N')
1085     OR     (gt.trx_number IS NOT NULL AND
1086             b.auto_trx_numbering_flag = 'Y'));
1087 
1088   -- Accept Customer Bank Account ID only when payment method
1089   -- is passed and it is automatic.
1090 
1091   INSERT INTO ar_trx_errors_gt (
1092     trx_header_id,
1093     error_message,
1094     invalid_value)
1095     SELECT trx_header_id,
1096            arp_standard.fnd_message('AR_INAPI_BANK_ACC_NOT_REQUIRED'),
1097            customer_bank_account_id
1098     FROM   ar_trx_header_gt gt
1099     WHERE  gt.customer_bank_account_id IS NOT NULL
1100     AND    NOT EXISTS (
1101            SELECT 'X'
1102            FROM   ar_receipt_methods rm,
1103                   ar_receipt_classes rc
1104            WHERE  rm.receipt_class_id     = rc.receipt_class_id
1105            AND    rm.receipt_method_id    = gt.receipt_method_id
1106            AND    rc.creation_method_code = 'AUTOMATIC');
1107 
1108   -- If paying_site_use_id must be entered when paying_customer_id is
1109   -- entered.
1110 
1111   INSERT INTO ar_trx_errors_gt (
1112     trx_header_id,
1113     error_message,
1114     invalid_value)
1115     SELECT trx_header_id,
1116       arp_standard.fnd_message('AR_INAPI_PAYING_SITE_REQUIRED'),
1117       paying_customer_id
1118     FROM   ar_trx_header_gt gt
1119     WHERE  gt.paying_customer_id IS NOT NULL
1120     AND    gt.paying_site_use_id IS NULL;
1121 
1122 
1123   -- Purchase Order Reveision and Date should be accepted only
1124   -- when purchase order is passed.
1125 
1126   INSERT INTO ar_trx_errors_gt (
1127     trx_header_id,
1128     error_message,
1129     invalid_value)
1130     SELECT trx_header_id,
1131       arp_standard.fnd_message('AR_INAPI_PO_REQUIRED'),
1132       paying_customer_id
1133     FROM   ar_trx_header_gt gt
1134     WHERE  (gt.purchase_order_revision IS NOT NULL
1135             OR gt.purchase_order_date IS NOT NULL)
1136     AND purchase_order IS NULL;
1137 
1138   -- Accounting Rule ID and Duration should be entered  only
1139   -- invoicing rule id is populated at header.
1140 
1141   INSERT INTO ar_trx_errors_gt (
1142     trx_header_id,
1143     trx_line_id,
1144     error_message,
1145     invalid_value)
1146     SELECT lgt.trx_header_id,
1147            lgt.trx_line_id,
1148            arp_standard.fnd_message('AR_INAPI_RULE_INFO_UNNECESSARY'),
1149            NVL(lgt.accounting_rule_id, lgt.accounting_rule_duration)
1150     FROM   ar_trx_lines_gt lgt,
1151            ar_trx_header_gt hgt
1152     WHERE  (lgt.accounting_rule_id IS NOT NULL
1153             OR lgt.accounting_rule_duration IS NOT NULL)
1154     AND    lgt.trx_header_id = hgt.trx_header_id
1155     AND    hgt.invoicing_rule_id IS NULL;
1156 
1157   -- Link_to_line_id should always point to a line of type LINE
1158   INSERT INTO ar_trx_errors_gt (
1159     trx_header_id,
1160     trx_line_id,
1161     error_message,
1162     invalid_value)
1163     SELECT trx_header_id,
1164            trx_line_id,
1165            arp_standard.fnd_message('AR_INAPI_INV_MASTER_LINE_TYPE'),
1166            link_to_trx_line_id
1167     FROM   ar_trx_lines_gt gt
1168     WHERE  gt.link_to_trx_line_id IS NOT NULL
1169     AND    NOT EXISTS
1170       (SELECT 'X'
1171        FROM  ar_trx_lines_gt gt2
1172        WHERE gt2.trx_line_id = gt.link_to_trx_line_id
1173        AND   gt2.line_type = 'LINE');
1174 
1175   -- Sub Lines can not have a line type of 'LINE'
1176   INSERT INTO ar_trx_errors_gt (
1177     trx_header_id,
1178     trx_line_id,
1179     error_message,
1180     invalid_value)
1181     SELECT trx_header_id,
1182            trx_line_id,
1183            arp_standard.fnd_message('AR_INAPI_INVALID_SUB_LINE_TYPE'),
1184            link_to_trx_line_id
1185     FROM   ar_trx_lines_gt gt
1186     WHERE  gt.link_to_trx_line_id IS NOT NULL
1187     AND    gt.line_type NOT IN ('TAX', 'FREIGHT');
1188 
1189 
1190   -- Exchange rate info is required only if exchange rate type
1191   -- is 'User'
1192 
1193   INSERT INTO ar_trx_errors_gt (
1194     trx_header_id,
1195     error_message,
1196     invalid_value)
1197     SELECT trx_header_id,
1198            arp_standard.fnd_message('AR_TAPI_EXCHANGE_RATE_REQUIRED'),
1199            gt.trx_currency
1200     FROM   ar_trx_header_gt gt
1201     WHERE  gt.trx_currency IS NOT NULL
1202     AND    ( exchange_rate IS NULL
1203        OR    exchange_rate <= 0 )
1204     AND    exchange_rate_type = 'User'
1205     AND    gt.trx_currency <> p_trx_system_param_rec.base_currency_code;
1206 
1207   -- Exchange rate should not be entered for rate type != 'User'.
1208 
1209   INSERT INTO ar_trx_errors_gt (
1210     trx_header_id,
1211     error_message,
1212     invalid_value)
1213     SELECT trx_header_id,
1214            arp_standard.fnd_message('AR_INAPI_RATE_NOT_REQUIRED2'),
1215            exchange_rate
1216     FROM ar_trx_header_gt
1217     WHERE exchange_rate IS NOT NULL
1218     AND exchange_rate_type <> 'User';
1219 
1220   -- Exchange rate cannot be specified for base currency trxns.
1221 
1222   INSERT INTO ar_trx_errors_gt (
1223     trx_header_id,
1224     error_message,
1225     invalid_value)
1226     SELECT trx_header_id,
1227            arp_standard.fnd_message('AR_TAPI_EXCHG_INFO_NOT_ALLOWED'),
1228            trx_currency
1229     FROM ar_trx_header_gt
1230     WHERE  ( exchange_rate IS NOT NULL
1231         OR   exchange_rate_type IS NOT NULL
1232         OR   exchange_date      IS NOT NULL )
1233     AND trx_currency = p_trx_system_param_rec.base_currency_code;
1234 
1235   -- amount includes tax flag should be NULL, Y or N
1236 
1237   INSERT INTO ar_trx_errors_gt (
1238     trx_header_id,
1239     trx_line_id,
1240     error_message,
1241     invalid_value)
1242     SELECT trx_header_id,
1243            trx_line_id,
1244            arp_standard.fnd_message('AR_TAX_CHOOSE_YES_NO'),
1245            amount_includes_tax_flag
1246     FROM ar_trx_lines_gt lgt
1247     WHERE amount_includes_tax_flag IS NOT NULL
1248     AND   amount_includes_tax_flag NOT IN ('Y', 'N');
1249 
1250   -- validate_bfb;
1251 
1252   EXCEPTION
1253     WHEN OTHERS THEN
1254       x_errmsg := 'ar_invoice_utils.validate_dependent_parameters: '||sqlerrm;
1255       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1256       RETURN;
1257 
1258 END validate_dependent_parameters;
1259 
1260 
1261 PROCEDURE validate_master_detail (
1262   x_errmsg                    OUT NOCOPY  VARCHAR2,
1263   x_return_status             OUT NOCOPY  VARCHAR2) AS
1264 
1265 BEGIN
1266 
1267   IF pg_debug = 'Y' THEN
1268      debug ('ar_invoice_utils.validate_master_detail(+)' );
1269   END IF;
1270 
1271   x_return_status := FND_API.G_RET_STS_SUCCESS;
1272 
1273   -- Every header level row should have atleast one line level row.
1274   INSERT INTO ar_trx_errors_gt (
1275     trx_header_id,
1276     error_message,
1277     invalid_value)
1278     SELECT trx_header_id,
1279            arp_standard.fnd_message('AR_TW_NO_LINES'),
1280            null
1281     FROM   ar_trx_header_gt gt
1282     WHERE  NOT EXISTS
1283       (SELECT 'X'
1284        FROM ar_trx_lines_gt lgt
1285        WHERE lgt.trx_header_id = gt.trx_header_id);
1286 
1287   -- Every line level row should have a corresponding header level row.
1288   INSERT INTO ar_trx_errors_gt (
1289     trx_header_id,
1290     trx_line_id,
1291     error_message,
1292     invalid_value)
1293     SELECT trx_header_id,
1294            trx_line_id,
1295            arp_standard.fnd_message('AR_INAPI_ORPHAN_LINES'),
1296            null
1297     FROM   ar_trx_lines_gt lgt
1298     WHERE  NOT EXISTS
1299       (SELECT 'X'
1300        FROM ar_trx_header_gt hgt
1301        WHERE hgt.trx_header_id = lgt.trx_header_id);
1302 
1303   -- Line numbers must be unique
1304   INSERT INTO ar_trx_errors_gt (
1305     trx_header_id,
1306     trx_line_id,
1307     error_message,
1308     invalid_value)
1309     SELECT trx_header_id,
1310            trx_line_id,
1311            arp_standard.fnd_message('AR_INAPI_LINE_NUMS_NOT_UNIQUE'),
1312            lgt.line_number
1313     FROM   ar_trx_lines_gt lgt
1314     WHERE  EXISTS
1315       (SELECT 'X'
1316        FROM  ar_trx_lines_gt lgt2
1317        WHERE lgt2.trx_header_id = lgt.trx_header_id
1318        AND   lgt2.line_number = lgt.line_number
1319        AND   lgt2.line_type = lgt.line_type
1320        GROUP BY lgt2.trx_header_id, lgt2.line_number
1321        HAVING count(*) > 1);
1322 
1323   -- Line IDs must be unique
1324   INSERT INTO ar_trx_errors_gt (
1325     trx_header_id,
1326     trx_line_id,
1327     error_message,
1328     invalid_value)
1329     SELECT trx_header_id,
1330            trx_line_id,
1331            arp_standard.fnd_message('AR_INAPI_LINE_ID_NOT_UNIQUE'),
1332            null
1333     FROM   ar_trx_lines_gt lgt
1334     WHERE  EXISTS
1335       (SELECT 'X'
1336        FROM  ar_trx_lines_gt lgt2
1337        WHERE lgt2.trx_header_id = lgt.trx_header_id
1338        AND   lgt2.trx_line_id = lgt.trx_line_id
1339        GROUP BY lgt2.trx_header_id, lgt2.trx_line_id
1340        HAVING count(*) > 1);
1341 
1342   -- Header IDs must be unique
1343   INSERT INTO ar_trx_errors_gt (
1344     trx_header_id,
1345     error_message,
1346     invalid_value)
1347     SELECT trx_header_id,
1348            arp_standard.fnd_message('AR_INAPI_HEADER_ID_NOT_UNIQUE'),
1349            null
1350     FROM   ar_trx_header_gt hgt
1351     WHERE  EXISTS
1352       (SELECT 'X'
1353        FROM  ar_trx_header_gt hgt2
1354        WHERE hgt2.trx_header_id = hgt.trx_header_id
1355        GROUP BY hgt2.trx_header_id
1356        HAVING count(*) > 1);
1357 
1358 
1359     IF pg_debug = 'Y' THEN
1360       debug ('ar_invoice_utils.validate_master_detail(-)' );
1361     END IF;
1362 
1363   EXCEPTION
1364     WHEN OTHERS THEN
1365       x_errmsg := 'error in ar_invoice_utils.validate_master_detail '
1366         || sqlerrm;
1367       x_return_status := fnd_api.g_ret_sts_unexp_error;
1368       RETURN;
1369 
1370 END validate_master_detail;
1371 
1372 
1373 -- Bug # 3099975
1374 -- ORASHID
1375 -- 21-AUG-2003 (END)
1376 
1377 
1378 PROCEDURE validate_trx_number (
1379     x_errmsg                    OUT NOCOPY  VARCHAR2,
1380     x_return_status             OUT NOCOPY  VARCHAR2)    AS
1381 BEGIN
1382     IF pg_debug = 'Y'
1383     THEN
1384         debug ('AR_INVOICE_UTILS.validate_trx_number(+)' );
1385     END IF;
1386 
1387     x_return_status := FND_API.G_RET_STS_SUCCESS;
1388 
1389     INSERT INTO ar_trx_errors_gt (
1390             trx_header_id,
1391             error_message,
1392             invalid_value)
1393         SELECT trx_header_id,
1394                arp_standard.fnd_message('AR_TW_INVALID_TRX_NUMBER'),
1395                trx_number
1396         FROM ar_trx_header_gt gt
1397         WHERE  gt.trx_number IS NOT NULL
1398         AND    gt.batch_source_id IS NOT NULL
1399         AND    EXISTS (
1400                     SELECT 'X'
1401                     FROM ra_batch_sources batch,
1402                          ra_customer_trx trx
1403                     WHERE  trx.batch_source_id   = gt.batch_source_id
1404                     AND    trx.trx_number        = gt.trx_number
1405                     AND    trx.customer_trx_id  <> NVL(gt.customer_trx_id, -99)
1406                     AND    trx.batch_source_id = batch.batch_source_id
1407                     AND    nvl(batch.copy_doc_number_flag,'N') = 'N'
1408                     AND    nvl(batch.allow_duplicate_trx_num_flag,'N') = 'N'
1409                     UNION
1410                     SELECT 'X'
1411                     FROM   ra_recur_interim  ri,
1412                            ra_customer_trx   ct,
1413                            ra_batch_sources batch
1414                     WHERE  ct.customer_trx_id       = ri.customer_trx_id
1415                     AND    ct.batch_source_id       = gt.batch_source_id
1416                     AND    ri.trx_number            = gt.trx_number
1417                     AND    NVL(ri.new_customer_trx_id, -98)
1418                                              <> NVL(gt.customer_trx_id, -99)
1419                     AND    ct.batch_source_id = batch.batch_source_id
1420                     AND    nvl(batch.copy_doc_number_flag,'N') = 'N'
1421                     AND    nvl(batch.allow_duplicate_trx_num_flag,'N') = 'N'
1422                     UNION
1423                     SELECT 'X'
1424                     FROM   ra_batch_sources    bs,
1425                            ar_trx_header_gt  ril
1426                     WHERE  ril.batch_source_id = bs.batch_source_id
1427                     AND    bs.batch_source_id    = gt.batch_source_id
1428                     AND    ril.trx_number        = gt.trx_number
1429                     AND    ril.customer_trx_id  <> NVL(gt.customer_trx_id, -99)
1430                     AND    nvl(bs.copy_doc_number_flag,'N') = 'N'
1431                     AND    nvl(bs.allow_duplicate_trx_num_flag,'N') = 'N'
1432                     );
1433 
1434     IF pg_debug = 'Y'
1435     THEN
1436         debug ('AR_INVOICE_UTILS.validate_trx_number(-)' );
1437     END IF;
1438     EXCEPTION
1439         WHEN OTHERS THEN
1440             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_trx_number '||sqlerrm;
1441             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1442             RETURN;
1443 END;
1444 
1445 /* Changes for SEPA */
1446 PROCEDURE validate_mandate_flag(
1447     x_errmsg                    OUT NOCOPY  VARCHAR2,
1448     x_return_status             OUT NOCOPY  VARCHAR2) AS
1449 BEGIN
1450     IF pg_debug = 'Y' THEN
1451         debug ('AR_INVOICE_UTILS.validate_mandate_flag(+)' );
1452     END IF;
1453 
1454     x_return_status := FND_API.G_RET_STS_SUCCESS;
1455 
1456     INSERT INTO ar_trx_errors_gt (
1457                   trx_header_id,
1458                   error_message,
1459                   invalid_value)
1460             SELECT trx_header_id,
1461                    arp_standard.fnd_message('AR_INVALID_MANDATE_FLAG_VALUE'),
1462                    trx_number
1463             FROM ar_trx_header_gt gt
1464             WHERE nvl(upper(mandate_last_trx_flag),'N') not in ('Y','N');
1465 
1466 
1467     IF pg_debug = 'Y'
1468     THEN
1469         debug ('AR_INVOICE_UTILS.validate_mandate_flag(-)' );
1470     END IF;
1471     EXCEPTION
1472 	WHEN OTHERS THEN
1473             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_mandate_flag'||sqlerrm;
1474             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1475             RETURN;
1476 
1477 END;
1478 
1479 
1480 PROCEDURE validate_batch_source (
1481     x_errmsg                    OUT NOCOPY  VARCHAR2,
1482     x_return_status             OUT NOCOPY  VARCHAR2) AS
1483 
1484 BEGIN
1485     -- validate the batch source id
1486     IF pg_debug = 'Y'
1487     THEN
1488         debug ('AR_INVOICE_UTILS.validate_batch_source(+)' );
1489     END IF;
1490 
1491     x_return_status := FND_API.G_RET_STS_SUCCESS;
1492 
1493     BEGIN
1494         INSERT INTO ar_trx_errors_gt (
1495             trx_header_id,
1496             error_message,
1497             invalid_value)
1498         SELECT trx_header_id,
1499                arp_standard.fnd_message('AR_INAPI_INVALID_BATCH_SOURCE'),
1500                batch_source_id
1501         FROM   ar_trx_header_gt gt
1502         WHERE  NOT EXISTS (
1503             SELECT 'X'
1504             FROM ra_batch_sources bs
1505             where nvl(gt.trx_date, trunc(sysdate)) between
1506                 nvl(bs.start_date, nvl(gt.trx_date, trunc(sysdate)))
1507                 and nvl(bs.end_date, nvl(gt.trx_date, trunc(sysdate)))
1508             and nvl(bs.status, 'A') = 'A'
1509             --and bs.batch_source_type = 'INV' -- means manual batch
1510             and bs.batch_source_id not in (11, 12)
1511             and gt.batch_source_id = bs.batch_source_id);
1512     END;
1513     IF pg_debug = 'Y'
1514     THEN
1515         debug ('AR_INVOICE_UTILS.validate_batch_source(-)' );
1516     END IF;
1517 
1518     EXCEPTION
1519         WHEN OTHERS THEN
1520             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_batch_source '||sqlerrm;
1521             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1522             RETURN;
1523 
1524 END;
1525 
1526 PROCEDURE validate_currency
1527     (
1528     x_errmsg                    OUT NOCOPY  VARCHAR2,
1529     x_return_status             OUT NOCOPY  VARCHAR2) AS
1530 
1531 BEGIN
1532 
1533     IF pg_debug = 'Y'
1534     THEN
1535         debug ('AR_INVOICE_UTILS.validate_currency(+)' );
1536     END IF;
1537 
1538     x_return_status := FND_API.G_RET_STS_SUCCESS;
1539 
1540     INSERT INTO ar_trx_errors_gt
1541         ( trx_header_id,
1542           error_message,
1543           invalid_value)
1544     SELECT trx_header_id,
1545            arp_standard.fnd_message('AR_INVALID_CURRENCY'),
1546            trx_currency
1547     FROM   ar_trx_header_gt gt
1548     WHERE  gt.trx_currency IS NOT NULL
1549     AND NOT EXISTS (
1550       SELECT 'X'
1551       FROM  fnd_currencies c
1552       WHERE c.currency_code = gt.trx_currency
1553       AND   gt.trx_date BETWEEN NVL(c.start_date_active, gt.trx_date)
1554             AND NVL(c.end_date_active, gt.trx_date) );
1555 
1556     IF pg_debug = 'Y'
1557     THEN
1558         debug ('AR_INVOICE_UTILS.validate_currency(-)' );
1559     END IF;
1560     EXCEPTION
1561         WHEN OTHERS THEN
1562             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_currency '||sqlerrm;
1563             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1564             RETURN;
1565 END;
1566 
1567 PROCEDURE validate_transaction_type
1568     (
1569     x_errmsg                    OUT NOCOPY  VARCHAR2,
1570     x_return_status             OUT NOCOPY  VARCHAR2) AS
1571 BEGIN
1572     IF pg_debug = 'Y'
1573     THEN
1574         debug ('AR_INVOICE_UTILS.validate_transaction_type(+)' );
1575     END IF;
1576 
1577     x_return_status := FND_API.G_RET_STS_SUCCESS;
1578     INSERT INTO ar_trx_errors_gt (
1579             trx_header_id,
1580             error_message,
1581             invalid_value)
1582         SELECT trx_header_id,
1583                arp_standard.fnd_message('AR_BR_TRX_TYPE_NULL'),
1584                cust_trx_type_id
1585         FROM   ar_trx_header_gt gt
1586         WHERE  gt.cust_trx_type_id IS NULL;
1587 
1588     INSERT INTO ar_trx_errors_gt (
1589             trx_header_id,
1590             error_message,
1591             invalid_value)
1592         SELECT trx_header_id,
1593                arp_standard.fnd_message('AR_INVALID_TRX_TYPE'),
1594                cust_trx_type_id
1595         FROM   ar_trx_header_gt gt
1596         WHERE  gt.cust_trx_type_id IS NOT NULL
1597         AND    NOT EXISTS (
1598             SELECT 'X'
1599             FROM ra_cust_trx_types ctt
1600             where nvl(gt.trx_date, trunc(sysdate)) between
1601                   ctt.start_date and nvl(ctt.end_date, nvl(gt.trx_date, trunc(sysdate)))
1602             and   type IN ('INV', 'DM', 'CM') -- added CM for ER 5869149
1603             and   ctt.cust_trx_type_id = gt.cust_trx_type_id);
1604 
1605     IF pg_debug = 'Y'
1606     THEN
1607         debug ('AR_INVOICE_UTILS.validate_transaction_type(-)' );
1608     END IF;
1609 
1610     EXCEPTION
1611             WHEN OTHERS THEN
1612             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_transaction_type '||sqlerrm;
1613             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1614             RETURN;
1615 
1616 END;
1617 
1618 PROCEDURE validate_bill_to_customer_id (
1619     x_errmsg                    OUT NOCOPY  VARCHAR2,
1620     x_return_status             OUT NOCOPY  VARCHAR2) AS
1621 BEGIN
1622     IF pg_debug = 'Y'
1623     THEN
1624         debug ('AR_INVOICE_UTILS.validate_bill_to_customer_id(+)' );
1625     END IF;
1626 
1627     x_return_status := FND_API.G_RET_STS_SUCCESS;
1628 
1629     INSERT INTO ar_trx_errors_gt (
1630             trx_header_id,
1631             error_message,
1632             invalid_value)
1633         SELECT trx_header_id,
1634                arp_standard.fnd_message('AR_INAPI_INV_BILL_TO_CUST_ID'),
1635                bill_to_customer_id
1636         FROM   ar_trx_header_gt gt
1637         WHERE  gt.bill_to_customer_id IS NOT NULL
1638         AND    NOT EXISTS (
1639             SELECT 'X'
1640             FROM hz_cust_accounts ct
1641             WHERE ct.cust_account_id = gt.bill_to_customer_id
1642             AND   status = 'A');
1643 
1644     IF pg_debug = 'Y'
1645     THEN
1646         debug ('AR_INVOICE_UTILS.validate_bill_to_customer_id(-)' );
1647     END IF;
1648     EXCEPTION
1649         WHEN OTHERS THEN
1650             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_bill_to_customer_id '||sqlerrm;
1651             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1652             RETURN;
1653 
1654 END;
1655 
1656 PROCEDURE validate_bill_to_site_use_id (
1657     x_errmsg                    OUT NOCOPY  VARCHAR2,
1658     x_return_status             OUT NOCOPY  VARCHAR2) AS
1659 BEGIN
1660 
1661     IF pg_debug = 'Y'
1662     THEN
1663         debug ('AR_INVOICE_UTILS.validate_bill_to_site_use_id(+)' );
1664     END IF;
1665         x_return_status := FND_API.G_RET_STS_SUCCESS;
1666 
1667 if(nvl(FND_PROFILE.value('AR_TRX_DEFAULT_PRIM_SITE_USE'),'BILL_SHIP_TO') not in ('BILL_TO','BILL_SHIP_TO')) then
1668         INSERT INTO ar_trx_errors_gt (
1669             trx_header_id,
1670             error_message,
1671             invalid_value)
1672         SELECT trx_header_id,
1673                arp_standard.fnd_message('AR_INAPI_BILL_TO_SITE_ID_REQ'),
1674                bill_to_site_use_id
1675         FROM   ar_trx_header_gt gt
1676         WHERE  gt.bill_to_site_use_id IS NULL
1677 	and gt.bill_to_customer_id is not null;
1678 end if;
1679 
1680         INSERT INTO ar_trx_errors_gt (
1681             trx_header_id,
1682             error_message,
1683             invalid_value)
1684         SELECT trx_header_id,
1685                arp_standard.fnd_message('AR_INAPI_INV_BILL_TO_SITE_USE'),
1686                bill_to_site_use_id
1687         FROM   ar_trx_header_gt gt
1688         WHERE  gt.bill_to_site_use_id IS NOT NULL
1689         AND    NOT EXISTS (
1690             SELECT 'X'
1691             FROM hz_cust_site_uses ct
1692             WHERE site_use_code = 'BILL_TO'
1693             AND   cust_acct_site_id in (
1694                     SELECT cust_acct_site_id
1695                     FROM   hz_cust_acct_sites
1696                     WHERE  cust_account_id = gt.bill_to_customer_id)
1697             AND site_use_id = gt.bill_to_site_use_id);
1698 
1699     IF pg_debug = 'Y'
1700     THEN
1701         debug ('AR_INVOICE_UTILS.validate_bill_to_site_use_id(-)' );
1702     END IF;
1703 
1704     EXCEPTION
1705       WHEN OTHERS THEN
1706         x_errmsg := 'Error in AR_INVOICE_UTILS.validate_bill_to_site_use_id '
1707           ||sqlerrm;
1708         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1709         RETURN;
1710 
1711 END validate_bill_to_site_use_id;
1712 
1713 
1714 PROCEDURE validate_bill_to_address_id (
1715     x_errmsg                    OUT NOCOPY  VARCHAR2,
1716     x_return_status             OUT NOCOPY  VARCHAR2) AS
1717 
1718 BEGIN
1719     IF pg_debug = 'Y'
1720     THEN
1721         debug ('AR_INVOICE_UTILS.validate_bill_to_address_id(+)' );
1722     END IF;
1723 
1724     x_return_status := FND_API.G_RET_STS_SUCCESS;
1725 
1726     INSERT INTO ar_trx_errors_gt (
1727             trx_header_id,
1728             error_message,
1729             invalid_value)
1730         SELECT trx_header_id,
1731                arp_standard.fnd_message('AR_INAPI_INVALID_BILL_ADDR_ID'),
1732                bill_to_address_id
1733         FROM   ar_trx_header_gt gt
1734         WHERE  gt.bill_to_address_id IS NOT NULL
1735         AND     NOT EXISTS (
1736             SELECT 'X'
1737               FROM HZ_CUST_ACCT_SITES ACCT_SITE,
1738                    HZ_PARTY_SITES PARTY_SITE,
1739                    --HZ_LOCATIONS LOC,
1740                    HZ_CUST_SITE_USES SU
1741                    -- FND_TERRITORIES_VL T
1742               WHERE  ACCT_SITE.CUST_ACCT_SITE_ID = SU.CUST_ACCT_SITE_ID
1743               AND    ACCT_SITE.PARTY_SITE_ID = PARTY_SITE.PARTY_SITE_ID
1744               --AND    LOC.LOCATION_ID =  PARTY_SITE.LOCATION_ID
1745               --AND    LOC.COUNTRY = T.TERRITORY_CODE
1746               AND    ACCT_SITE.CUST_ACCOUNT_ID = gt.bill_to_customer_id
1747               AND    SU.SITE_USE_ID = NVL(gt.bill_to_site_use_id, SU.SITE_USE_ID)
1748               AND    SU.SITE_USE_CODE = 'BILL_TO'
1749               AND    (SU.SITE_USE_ID =   gt.bill_to_site_use_id
1750                    OR (gt.bill_to_site_use_id IS NULL
1751                    AND SU.STATUS = 'A'  AND ACCT_SITE.STATUS = 'A' ))
1752               AND SU.CUST_ACCT_SITE_ID = gt.bill_to_address_id );
1753 
1754     IF pg_debug = 'Y'
1755     THEN
1756         debug ('AR_INVOICE_UTILS.validate_bill_to_address_id(-)' );
1757     END IF;
1758     EXCEPTION
1759         WHEN OTHERS THEN
1760             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_bill_to_address_id '||sqlerrm;
1761             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1762             RETURN;
1763 
1764 END;
1765 
1766 PROCEDURE validate_ship_to_customer_id (
1767     x_errmsg                    OUT NOCOPY  VARCHAR2,
1768     x_return_status             OUT NOCOPY  VARCHAR2)  AS
1769 
1770    l_dummy varchar2(240);
1771    l_dummyn number;
1772 
1773 BEGIN
1774     IF pg_debug = 'Y'
1775     THEN
1776         debug ('AR_INVOICE_UTILS.validate_ship_to_customer_id(+)' );
1777     END IF;
1778 
1779     x_return_status := FND_API.G_RET_STS_SUCCESS;
1780 
1781     INSERT INTO ar_trx_errors_gt (
1782             trx_header_id,
1783             error_message,
1784             invalid_value)
1785         SELECT trx_header_id,
1786                arp_standard.fnd_message('AR_INAPI_INV_SHIP_TO_CUST_ID'),
1787                ship_to_customer_id
1788         FROM   ar_trx_header_gt gt
1789         WHERE gt.ship_to_customer_id IS NOT NULL
1790         AND  NOT EXISTS (
1791             SELECT 'X'
1792             FROM hz_cust_accounts ct
1793             WHERE ct.cust_account_id = gt.ship_to_customer_id
1794             AND   ct.status = 'A');
1795             -- OR    gt.ship_to_customer_id IS NOT NULL);
1796 
1797     IF pg_debug = 'Y'
1798     THEN
1799         debug ('AR_INVOICE_UTILS.validate_ship_to_customer_id(-)' );
1800     END IF;
1801 
1802     EXCEPTION
1803         WHEN OTHERS THEN
1804           x_errmsg := 'Error in AR_INVOICE_UTILS.validate_ship_to_customer_id '
1805             ||sqlerrm;
1806             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1807             RETURN;
1808 END;
1809 
1810 PROCEDURE validate_ship_to_site_use_id (
1811      x_errmsg                    OUT NOCOPY  VARCHAR2,
1812     x_return_status             OUT NOCOPY  VARCHAR2,
1813     x_called			in varchar2  default null) AS
1814 BEGIN
1815     IF pg_debug = 'Y'
1816     THEN
1817         debug ('AR_INVOICE_UTILS.validate_ship_to_site_use_id(+)' );
1818     END IF;
1819 
1820     x_return_status := FND_API.G_RET_STS_SUCCESS;
1821 
1822 --  The below if condition is added to validate ship_to_site_use_id before defaulting ship_to details
1823     if(x_called = 'BEFORE') then
1824     if( nvl(FND_PROFILE.value('AR_TRX_DEFAULT_PRIM_SITE_USE'),'BILL_SHIP_TO') not in ('SHIP_TO','BILL_SHIP_TO')) then
1825 
1826         INSERT INTO ar_trx_errors_gt (
1827             trx_header_id,
1828             error_message,
1829             invalid_value)
1830         SELECT trx_header_id,
1831                arp_standard.fnd_message('AR_INAPI_SHIP_TO_SITE_ID_REQ'),
1832                ship_to_site_use_id
1833         FROM   ar_trx_header_gt gt
1834         WHERE   gt.ship_to_site_use_id IS NULL
1835         AND    gt.ship_to_customer_id IS NOT NULL;
1836     end if;
1837 
1838         INSERT INTO ar_trx_errors_gt (
1839             trx_header_id,
1840             error_message,
1841             invalid_value)
1842         SELECT trx_header_id,
1843                arp_standard.fnd_message('AR_INAPI_INVALID_SHIP_SITE_USE'),
1844                ship_to_site_use_id
1845         FROM   ar_trx_header_gt gt
1846         WHERE  gt.ship_to_site_use_id IS NOT NULL
1847         AND    gt.ship_to_customer_id IS NOT NULL
1848         AND    NOT EXISTS (
1849             SELECT 'X'
1850             FROM hz_cust_site_uses ct
1851             WHERE site_use_code = 'SHIP_TO'
1852               and cust_acct_site_id in (
1853                       select cust_acct_site_id from hz_cust_acct_sites
1854                      where cust_account_id = gt.ship_to_customer_id)
1855               and site_use_id = gt.ship_to_site_use_id);
1856 
1857    else
1858          INSERT INTO ar_trx_errors_gt (
1859             trx_header_id,
1860             error_message,
1861             invalid_value)
1862         SELECT trx_header_id,
1863                arp_standard.fnd_message('AR_INAPI_NULL_SHIP_TO_SITE'),
1864                ship_to_site_use_id
1865         FROM   ar_trx_header_gt gt
1866         WHERE  gt.ship_to_site_use_id IS NULL
1867         AND    gt.ship_to_customer_id IS NOT NULL;
1868 
1869    end if;
1870 
1871     IF pg_debug = 'Y'
1872     THEN
1873         debug ('AR_INVOICE_UTILS.validate_ship_to_site_use_id(-)' );
1874     END IF;
1875     EXCEPTION
1876         WHEN OTHERS THEN
1877             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_ship_to_site_use_id '||sqlerrm;
1878             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1879             RETURN;
1880 END;
1881 
1882 PROCEDURE validate_ship_to_location (
1883     x_errmsg                    OUT NOCOPY  VARCHAR2,
1884     x_return_status             OUT NOCOPY  VARCHAR2) AS
1885 BEGIN
1886     IF pg_debug = 'Y'
1887     THEN
1888         debug ('AR_INVOICE_UTILS.validate_ship_to_location(+)' );
1889     END IF;
1890 
1891     x_return_status := FND_API.G_RET_STS_SUCCESS;
1892 
1893     INSERT INTO ar_trx_errors_gt (
1894             trx_header_id,
1895             error_message,
1896             invalid_value)
1897     SELECT trx_header_id,
1898            arp_standard.fnd_message('AR_INAPI_INVALID_SHIP_SITE_USE'),
1899            ship_to_site_use_id
1900     FROM   ar_trx_header_gt gt
1901     WHERE gt.ship_to_site_use_id IS NOT NULL
1902       AND NOT EXISTS (
1903         select 'X'
1904         from
1905         (
1906           SELECT
1907             A.CUST_ACCOUNT_ID CUSTOMER_ID ,
1908             A.STATUS A_STATUS ,
1909             SU.STATUS SU_STATUS ,
1910             SU.SITE_USE_ID SITE_USE_ID
1911           FROM
1912             HZ_CUST_ACCT_SITES A,
1913             HZ_CUST_SITE_USES SU
1914           WHERE
1915             A.CUST_ACCT_SITE_ID = SU.CUST_ACCT_SITE_ID
1916             AND SU.SITE_USE_CODE = 'SHIP_TO'
1917         ) asa
1918         where asa.customer_id = gt.ship_to_customer_id
1919         and ( asa.site_use_id = gt.ship_to_site_use_id
1920         or ( asa.su_status = 'A' and asa.a_status = 'A' ) ));
1921 
1922     IF pg_debug = 'Y'
1923     THEN
1924         debug ('AR_INVOICE_UTILS.validate_ship_to_location(-)' );
1925     END IF;
1926     EXCEPTION
1927         WHEN OTHERS THEN
1928             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_ship_to_location '||sqlerrm;
1929             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1930             RETURN;
1931 END;
1932 
1933 
1934 PROCEDURE populate_ship_to_site_use_id (
1935         x_errmsg                    OUT NOCOPY  VARCHAR2,
1936         x_return_status             OUT NOCOPY  VARCHAR2)  IS
1937 
1938 BEGIN
1939 
1940   IF pg_debug = 'Y' THEN
1941     debug ('AR_INVOICE_UTILS.populate_ship_to_site_use_id (+)' );
1942   END IF;
1943 
1944   x_return_status := FND_API.G_RET_STS_SUCCESS;
1945 
1946   -- We are here to populate ship_to_site_use_id column.   We should only
1947   -- do it for rows where it is not already populated.  Moreover, first we
1948   -- should see if ship_to_address_id is populated then we should derive the
1949   -- ship_to_site_use_id from that.
1950 
1951   UPDATE ar_trx_header_gt gt
1952   SET    ship_to_site_use_id = (
1953            SELECT site_use_id
1954            FROM   hz_cust_site_uses
1955            WHERE  site_use_code = 'SHIP_TO'
1956            AND    status = 'A' /* 10041924, try active first */
1957            AND    cust_acct_site_id = gt.ship_to_address_id
1958            AND    gt.ship_to_site_use_id IS NULL
1959            AND    gt.ship_to_address_id IS NOT NULL)
1960   WHERE  gt.ship_to_site_use_id IS NULL
1961   AND    gt.ship_to_address_id IS NOT NULL;
1962 
1963   /* 10041924 for those addresses that did not have active
1964      sites, use the max inactive one */
1965   UPDATE ar_trx_header_gt gt
1966   SET    ship_to_site_use_id = (
1967            SELECT MAX(site_use_id)
1968            FROM   hz_cust_site_uses
1969            WHERE  site_use_code = 'SHIP_TO'
1970            AND    status = 'I' /* 10041924, try inactive */
1971            AND    cust_acct_site_id = gt.ship_to_address_id
1972            AND    gt.ship_to_site_use_id IS NULL
1973            AND    gt.ship_to_address_id IS NOT NULL
1974            GROUP BY cust_acct_site_id)
1975   WHERE  gt.ship_to_site_use_id IS NULL
1976   AND    gt.ship_to_address_id IS NOT NULL;
1977 
1978   -- Now we will worry about cases where only customer id
1979   -- is populated and we must derive the primary ship to site id.
1980 
1981   UPDATE ar_trx_header_gt gt
1982   SET    ship_to_site_use_id = (
1983            SELECT site_use_id
1984            FROM   hz_cust_site_uses
1985            WHERE  primary_flag = 'Y'
1986            AND    site_use_code = 'SHIP_TO'
1987            AND    cust_acct_site_id IN (
1988                     SELECT cust_acct_site_id
1989                     FROM   hz_cust_acct_sites
1990                    WHERE  cust_account_id = gt.ship_to_customer_id))
1991   WHERE gt.ship_to_site_use_id IS NULL;
1992 
1993   IF pg_debug = 'Y' THEN
1994     debug ('AR_INVOICE_UTILS.populate_ship_to_site_use_id (-)' );
1995   END IF;
1996 
1997   EXCEPTION
1998     WHEN OTHERS THEN
1999       x_errmsg := 'Error in AR_INVOICE_UTILS.populate_ship_to_site_use_id '
2000         ||sqlerrm;
2001       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2002       RETURN;
2003 
2004 END populate_ship_to_site_use_id;
2005 
2006 
2007 PROCEDURE validate_ship_to_address (
2008     x_errmsg                    OUT NOCOPY  VARCHAR2,
2009     x_return_status             OUT NOCOPY  VARCHAR2) AS
2010 
2011    l_dummy varchar2(240);
2012    l_dummyn number;
2013 
2014 BEGIN
2015     IF pg_debug = 'Y'
2016     THEN
2017         debug ('AR_INVOICE_UTILS.validate_ship_to_address(+)' );
2018     END IF;
2019 
2020     populate_ship_to_site_use_id(
2021         x_errmsg                    => x_errmsg,
2022         x_return_status             => x_return_status);
2023 
2024     x_return_status := FND_API.G_RET_STS_SUCCESS;
2025 
2026     INSERT INTO ar_trx_errors_gt (
2027             trx_header_id,
2028             error_message,
2029             invalid_value)
2030     SELECT trx_header_id,
2031            arp_standard.fnd_message('AR_INAPI_INVALID_SHIP_SITE_USE'),
2032            gt.ship_to_site_use_id
2033     FROM   ar_trx_header_gt gt
2034     WHERE gt.ship_to_customer_id IS NOT NULL
2035     AND   gt.ship_to_site_use_id IS NOT NULL
2036     AND   NOT EXISTS (
2037         select 'X'
2038         from
2039         (
2040           SELECT
2041             A.CUST_ACCOUNT_ID CUSTOMER_ID ,
2042             A.STATUS A_STATUS ,
2043             SU.STATUS SU_STATUS ,
2044             SU.SITE_USE_ID SITE_USE_ID
2045           FROM
2046             HZ_CUST_ACCT_SITES A,
2047             HZ_CUST_SITE_USES SU
2048           WHERE
2049             A.CUST_ACCT_SITE_ID = SU.CUST_ACCT_SITE_ID
2050             AND SU.SITE_USE_CODE = 'SHIP_TO'
2051         )asa
2052         where asa.customer_id = gt.ship_to_customer_id
2053         and ( asa.site_use_id = gt.ship_to_site_use_id
2054         or ( gt.customer_trx_id is null
2055              and asa.su_status = 'A' and asa.a_status = 'A' ) ) );
2056 
2057     IF pg_debug = 'Y'
2058     THEN
2059         debug ('AR_INVOICE_UTILS.validate_ship_to_address(-)' );
2060     END IF;
2061 
2062     EXCEPTION
2063         WHEN OTHERS THEN
2064             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_ship_to_address '||sqlerrm;
2065             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2066             RETURN;
2067 END validate_ship_to_address;
2068 
2069 
2070 PROCEDURE validate_terms (
2071     x_errmsg                    OUT NOCOPY  VARCHAR2,
2072     x_return_status             OUT NOCOPY  VARCHAR2) AS
2073 
2074 BEGIN
2075     IF pg_debug = 'Y'
2076     THEN
2077         debug ('AR_INVOICE_UTILS.validate_terms(+)' );
2078     END IF;
2079     x_return_status := FND_API.G_RET_STS_SUCCESS;
2080 
2081      INSERT INTO ar_trx_errors_gt (
2082             trx_header_id,
2083             error_message,
2084             invalid_value)
2085         SELECT gt.trx_header_id,
2086                arp_standard.fnd_message('AR_INVALID_TERM'),
2087                gt.term_id
2088         FROM   ar_trx_header_gt gt,
2089 	       ra_cust_trx_types ctt
2090         WHERE  gt.term_id IS NOT NULL
2091 	AND    gt.trx_class <> 'CM' -- added for ER 5869149
2092         AND    ctt.cust_trx_type_id = gt.cust_trx_type_id -- ER 5869149
2093         AND    ctt.type <> 'CM'  -- ER 5869149
2094         AND   NOT EXISTS (
2095             SELECT 'X'
2096             from ra_terms_lines tl, ra_terms t
2097             where nvl(gt.trx_date, trunc(sysdate))
2098             between t.start_date_active and nvl(t.end_date_active, nvl( gt.trx_date, trunc(sysdate) ) )
2099             and t.term_id = tl.term_id
2100             and t.term_id = gt.term_id );
2101 
2102      -- Term id not allowed for Credit Memo
2103      -- added for ER 5869149
2104      INSERT INTO ar_trx_errors_gt (
2105             trx_header_id,
2106             error_message,
2107             invalid_value)
2108         SELECT gt.trx_header_id,
2109                arp_standard.fnd_message('AR_INAPI_TERM_NOT_ALLOWED'),
2110                gt.term_id
2111         FROM   ar_trx_header_gt gt,
2112                ra_cust_trx_types ctt
2113         WHERE  gt.term_id IS NOT NULL
2114         AND    gt.cust_trx_type_id = ctt.cust_trx_type_id
2115         AND    ctt.type = 'CM';
2116 
2117     IF pg_debug = 'Y'
2118     THEN
2119         debug ('AR_INVOICE_UTILS.validate_terms(-)' );
2120     END IF;
2121     EXCEPTION
2122         WHEN OTHERS THEN
2123             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_terms '||sqlerrm;
2124             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2125             RETURN;
2126 
2127 END validate_terms;
2128 
2129 
2130 PROCEDURE validate_salesrep (
2131   p_trx_system_param_rec ar_invoice_default_pvt.trx_system_parameters_rec_type,
2132   x_errmsg                    OUT NOCOPY  VARCHAR2,
2133   x_return_status             OUT NOCOPY  VARCHAR2) AS
2134 
2135   l_message_name VARCHAR2(30);
2136 
2137 BEGIN
2138     IF pg_debug = 'Y'
2139     THEN
2140         debug ('AR_INVOICE_UTILS.validate_salesrep(+)' );
2141     END IF;
2142 
2143     x_return_status := FND_API.G_RET_STS_SUCCESS;
2144 
2145 /*
2146     INSERT INTO ar_trx_errors_gt (
2147             trx_header_id,
2148             error_message,
2149             invalid_value)
2150         SELECT trx_header_id,
2151                arp_standard.fnd_message('AR_INVALID_PRIMARY_SALESREP'),
2152                primary_salesrep_id
2153         FROM   ar_trx_header_gt gt
2154         WHERE  gt.primary_salesrep_id IS NOT NULL
2155         AND     NOT EXISTS (
2156             SELECT 'X'
2157             FROM HZ_CUST_ACCT_SITES ACCT_SITE,
2158                  HZ_PARTY_SITES PARTY_SITE,
2159                  HZ_LOCATIONS LOC,
2160                  HZ_CUST_SITE_USES SU,
2161                  FND_TERRITORIES_VL T
2162             WHERE ACCT_SITE.CUST_ACCT_SITE_ID = SU.CUST_ACCT_SITE_ID
2163             AND   ACCT_SITE.PARTY_SITE_ID = PARTY_SITE.PARTY_SITE_ID
2164             AND   LOC.LOCATION_ID =  PARTY_SITE.LOCATION_ID
2165             AND   LOC.COUNTRY = T.TERRITORY_CODE
2166             AND   ACCT_SITE.CUST_ACCOUNT_ID = gt.bill_to_customer_id
2167             AND SU.SITE_USE_ID = NVL(gt.bill_to_site_use_id, SU.SITE_USE_ID)
2168             AND SU.SITE_USE_CODE = 'BILL_TO'
2169             AND (SU.SITE_USE_ID =  gt.bill_to_site_use_id
2170                OR (gt.bill_to_site_use_id IS NULL
2171                    AND SU.STATUS = 'A'
2172                    AND ACCT_SITE.STATUS = 'A' ))
2173             -- AND SU.PRIMARY_FLAG = 'Y'
2174             AND SU.primary_salesrep_id = gt.primary_salesrep_id);
2175 */
2176 
2177     -- Bug # 3099975
2178     -- ORASHID
2179     -- 21-AUG-2003 (END)
2180 
2181 
2182     l_message_name := gl_public_sector.get_message_name (
2183       p_message_name => 'AR_INVALID_PRIMARY_SALESREP',
2184       p_app_short_name => 'AR');
2185 
2186     INSERT INTO ar_trx_errors_gt (
2187            trx_header_id,
2188            error_message,
2189            invalid_value)
2190     SELECT trx_header_id,
2191            arp_standard.fnd_message(l_message_name),
2192            primary_salesrep_id
2193     FROM   ar_trx_header_gt gt
2194     WHERE  gt.primary_salesrep_id IS NOT NULL
2195     AND     NOT EXISTS (
2196               (SELECT  'X'
2197                FROM    ra_salesreps rs
2198                WHERE   rs.salesrep_id = gt.primary_salesrep_id
2199                AND     gt.trx_date
2200                BETWEEN rs.start_date_active
2201                AND     NVL(rs.end_date_active, gt.trx_date)));
2202 
2203     l_message_name := gl_public_sector.get_message_name (
2204       p_message_name => 'AR_INAPI_SALESREP_REQUIRED',
2205       p_app_short_name => 'AR');
2206 
2207     INSERT INTO ar_trx_errors_gt (
2208            trx_header_id,
2209            error_message,
2210            invalid_value)
2211     SELECT trx_header_id,
2212            arp_standard.fnd_message(l_message_name),
2213            primary_salesrep_id
2214     FROM   ar_trx_header_gt gt
2215     WHERE  gt.primary_salesrep_id IS NULL
2216     AND    p_trx_system_param_rec.salesrep_required_flag = 'Y';
2217 
2218     IF pg_debug = 'Y'
2219     THEN
2220         debug ('AR_INVOICE_UTILS.validate_salesrep(-)' );
2221     END IF;
2222 
2223   EXCEPTION
2224     WHEN OTHERS THEN
2225       x_errmsg := 'Error in AR_INVOICE_UTILS.validate_salesrep '||sqlerrm;
2226       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2227       RETURN;
2228 
2229 END validate_salesrep;
2230 
2231 
2232 PROCEDURE validate_invoicing_rule_id (
2233     x_errmsg                    OUT NOCOPY  VARCHAR2,
2234     x_return_status             OUT NOCOPY  VARCHAR2) AS
2235 BEGIN
2236     IF pg_debug = 'Y'
2237     THEN
2238         debug ('AR_INVOICE_UTILS.validate_invoicing_rule_id(+)' );
2239     END IF;
2240 
2241     x_return_status := FND_API.G_RET_STS_SUCCESS;
2242 
2243     -- in case of debit memo invoice rule id is not required
2244     -- in case of credit memo invoice rule id is not required ER 5869149
2245     INSERT INTO ar_trx_errors_gt (
2246             trx_header_id,
2247             error_message,
2248             invalid_value)
2249         SELECT trx_header_id,
2250                arp_standard.fnd_message('AR_INAPI_INV_RULE_NOT_REQUIRED'),
2251                invoicing_rule_id
2252         FROM ar_trx_header_gt gt
2253         WHERE  gt.invoicing_rule_id IS NOT NULL
2254         AND    gt.cust_trx_type_id NOT IN  (
2255                 SELECT tt.cust_trx_type_id
2256                 FROM   ra_cust_trx_types tt
2257                 WHERE  tt.cust_trx_type_id = gt.cust_trx_type_id
2258                 AND    tt.type = 'INV' );
2259 
2260     INSERT INTO ar_trx_errors_gt (
2261             trx_header_id,
2262             error_message,
2263             invalid_value)
2264         SELECT trx_header_id,
2265                arp_standard.fnd_message('AR_INAPI_INVALID_INV_RULE_ID'),
2266                invoicing_rule_id
2267         FROM ar_trx_header_gt gt
2268         WHERE  gt.invoicing_rule_id IS NOT NULL
2269         AND    gt.invoicing_rule_id not in ( -2, -3);
2270 
2271     IF pg_debug = 'Y'
2272     THEN
2273         debug ('AR_INVOICE_UTILS.validate_invoicing_rule_id(-)' );
2274     END IF;
2275     EXCEPTION
2276       WHEN OTHERS THEN
2277         x_errmsg := 'Error in AR_INVOICE_UTILS.validate_invoicing_rule_id '
2278           ||sqlerrm;
2279         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2280        RETURN;
2281 
2282 END validate_invoicing_rule_id;
2283 
2284 
2285 PROCEDURE validate_print_option (
2286     x_errmsg                    OUT NOCOPY  VARCHAR2,
2287     x_return_status             OUT NOCOPY  VARCHAR2) AS
2288 
2289 BEGIN
2290 
2291     IF pg_debug = 'Y'
2292     THEN
2293         debug ('AR_INVOICE_UTILS.validate_print_option(+)' );
2294     END IF;
2295 
2296     x_return_status := FND_API.G_RET_STS_SUCCESS;
2297 
2298     INSERT INTO ar_trx_errors_gt (
2299             trx_header_id,
2300             error_message,
2301             invalid_value)
2302         SELECT trx_header_id,
2303                arp_standard.fnd_message('AR_INAPI_INVALID_PRINT_OPTION'),
2304                printing_option
2305         FROM   ar_trx_header_gt gt
2306         WHERE  gt.printing_option IS NOT NULL
2307         AND    gt.printing_option NOT IN ( 'PRI', 'NOT');
2308     IF pg_debug = 'Y'
2309     THEN
2310         debug ('AR_INVOICE_UTILS.validate_print_option(-)' );
2311     END IF;
2312 
2313     EXCEPTION
2314       WHEN OTHERS THEN
2315         x_errmsg := 'Error in AR_INVOICE_UTILS.validate_print_option '
2316           ||sqlerrm;
2317         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2318         RETURN;
2319 
2320 END validate_print_option;
2321 
2322 
2323 /*bug4369585-4589309*/
2324 
2325 PROCEDURE populate_printing_pending (
2326     x_errmsg                    OUT NOCOPY  VARCHAR2,
2327     x_return_status             OUT NOCOPY  VARCHAR2) IS
2328 
2329 BEGIN
2330     IF pg_debug = 'Y'
2331     THEN
2332         debug ('AR_INVOICE_UTILS.populate_printing_pendings(+)' );
2333     END IF;
2334 
2335     x_return_status := FND_API.G_RET_STS_SUCCESS;
2336 
2337 update ar_trx_header_gt gt
2338     set gt.printing_pending=decode(gt.PRINTING_OPTION,'PRI','Y','N');
2339 
2340 IF pg_debug = 'Y'
2341     THEN
2342         debug ('AR_INVOICE_UTILS.populate_printing_pending(-)' );
2343     END IF;
2344 
2345     EXCEPTION
2346         WHEN OTHERS THEN
2347             x_errmsg := 'Error in AR_INVOICE_UTILS.populate_printing_pending '||sqlerrm;
2348             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2349             RETURN;
2350 
2351 End populate_printing_pending;
2352 
2353 
2354 PROCEDURE validate_default_tax (
2355     x_errmsg                    OUT NOCOPY  VARCHAR2,
2356     x_return_status             OUT NOCOPY  VARCHAR2) AS
2357     l_tax_use_exempt_flag       zx_product_options.tax_use_customer_exempt_flag%type;
2358 
2359 BEGIN
2360     IF pg_debug = 'Y'
2361     THEN
2362         debug ('AR_INVOICE_UTILS.validate_default_tax(+)' );
2363     END IF;
2364 
2365     x_return_status := FND_API.G_RET_STS_SUCCESS;
2366     BEGIN
2367         /* 4257557 - changed to zx_product_options */
2368         select nvl(tax_use_customer_exempt_flag,'N')
2369         into  l_tax_use_exempt_flag
2370         from  zx_product_options
2371         where application_id = 222;
2372 
2373     EXCEPTION
2374       WHEN NO_DATA_FOUND THEN
2375         IF pg_debug = 'Y'
2376         THEN
2377             debug ('  No rows in zx_product_options' );
2378             debug ('AR_INVOICE_UTILS.validate_default_tax(-)' );
2379         END IF;
2380         RETURN;
2381     END;
2382 
2383     INSERT INTO ar_trx_errors_gt (
2384             trx_header_id,
2385             error_message,
2386             invalid_value)
2387         SELECT trx_header_id,
2388                arp_standard.fnd_message('AR_INAPI_INV_TAX_EXEMPT_FLAG'),
2389                default_tax_exempt_flag
2390         FROM  ar_trx_header_gt gt
2391         WHERE default_tax_exempt_flag IS NOT NULL
2392         AND   NOT EXISTS (
2393             select 'X'
2394             from ar_lookups AL1
2395             where AL1.lookup_type = 'TAX_CONTROL_FLAG'
2396             and (AL1.lookup_code in ('R','S')
2397             or (AL1.lookup_code = 'E' and
2398                 l_tax_use_exempt_flag = 'Y'))
2399             and gt.default_tax_exempt_flag = AL1.lookup_code );
2400 
2401     IF pg_debug = 'Y'
2402     THEN
2403         debug ('AR_INVOICE_UTILS.validate_default_tax(-)' );
2404     END IF;
2405 
2406     EXCEPTION
2407       WHEN OTHERS THEN
2408         x_errmsg := 'Error in AR_INVOICE_UTILS.validate_default_tax '||sqlerrm;
2409         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2410         RETURN;
2411 
2412 END validate_default_tax;
2413 
2414 
2415 PROCEDURE validate_status (
2416     x_errmsg                    OUT NOCOPY  VARCHAR2,
2417     x_return_status             OUT NOCOPY  VARCHAR2) AS
2418 
2419 BEGIN
2420     IF pg_debug = 'Y'
2421     THEN
2422         debug ('AR_INVOICE_UTILS.validate_Status(+)' );
2423     END IF;
2424 
2425     x_return_status := FND_API.G_RET_STS_SUCCESS;
2426 
2427     INSERT INTO ar_trx_errors_gt (
2428             trx_header_id,
2429             error_message,
2430             invalid_value)
2431         SELECT trx_header_id,
2432                arp_standard.fnd_message('AR_INAPI_INVALID_TRX_STATUS'),
2433                status_trx
2434         FROM  ar_trx_header_gt
2435         WHERE status_trx  IS NOT NULL
2436         AND   status_trx not in ( 'OP','CL','PEN','VD');
2437 
2438     IF pg_debug = 'Y'
2439     THEN
2440         debug ('AR_INVOICE_UTILS.validate_Status(-)' );
2441     END IF;
2442 
2443     EXCEPTION
2444         WHEN OTHERS THEN
2445             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_Status '||sqlerrm;
2446             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2447             RETURN;
2448 
2449 END validate_status;
2450 
2451 
2452 PROCEDURE validate_finance_charges (
2453     x_errmsg                    OUT NOCOPY  VARCHAR2,
2454     x_return_status             OUT NOCOPY  VARCHAR2) IS
2455 
2456 BEGIN
2457     IF pg_debug = 'Y'
2458     THEN
2459         debug ('AR_INVOICE_UTILS.validate_finance_charges(+)' );
2460     END IF;
2461 
2462     x_return_status := FND_API.G_RET_STS_SUCCESS;
2463 
2464     INSERT INTO ar_trx_errors_gt (
2465             trx_header_id,
2466             error_message,
2467             invalid_value)
2468         SELECT trx_header_id,
2469                arp_standard.fnd_message('AR_INAPI_INVALID_FIN_CHARGE'),
2470                finance_charges
2471         FROM  ar_trx_header_gt
2472         WHERE finance_charges  IS NOT NULL
2473         AND   finance_charges not in ( 'Y','N');
2474 
2475     IF pg_debug = 'Y'
2476     THEN
2477         debug ('AR_INVOICE_UTILS.validate_finance_charges(-)' );
2478     END IF;
2479 
2480     EXCEPTION
2481       WHEN OTHERS THEN
2482         x_errmsg := 'Error in AR_INVOICE_UTILS.validate_finance_charges '
2483           ||sqlerrm;
2484         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2485         RETURN;
2486 
2487 END validate_finance_charges;
2488 
2489 
2490 PROCEDURE validate_related_cust_trx_id (
2491     x_errmsg                    OUT NOCOPY  VARCHAR2,
2492     x_return_status             OUT NOCOPY  VARCHAR2) IS
2493 
2494 BEGIN
2495 
2496     IF pg_debug = 'Y'
2497     THEN
2498         debug ('AR_INVOICE_UTILS.validate_related_cust_trx_id(+)' );
2499     END IF;
2500 
2501     x_return_status := FND_API.G_RET_STS_SUCCESS;
2502 
2503     INSERT INTO ar_trx_errors_gt (
2504             trx_header_id,
2505             error_message,
2506             invalid_value)
2507         SELECT trx_header_id,
2508                arp_standard.fnd_message('AR_INAPI_INAVLID_CROSS_REF'),
2509                related_customer_trx_id
2510         FROM  ar_trx_header_gt gt
2511         WHERE gt.related_customer_trx_id IS NOT NULL
2512         AND   NOT EXISTS (
2513             SELECT 'X'
2514             FROM   ra_customer_trx trx, ra_batch_sources bs, ar_lookups look,
2515                    ra_cust_trx_types types
2516             where trx.batch_source_id = bs.batch_source_id
2517             and trx.cust_trx_type_id = types.cust_trx_type_id
2518             and look.lookup_type = 'INV/CM'
2519             and types.type = look.lookup_code
2520             and types.type <> 'BR'
2521             and trx.complete_flag = 'Y'
2522             and trx.customer_trx_id = gt.related_customer_trx_id
2523             and trx.bill_to_customer_id  IN
2524                 (
2525                 select distinct cr.cust_account_id
2526                 from hz_cust_acct_relate cr
2527                 where cr.related_cust_account_id = gt.bill_to_customer_id
2528                 and cr.status = 'A'
2529                 and cr.bill_to_flag ='Y'
2530                 --union
2531                 --select to_number(gt.bill_to_customer_id)
2532                 --from dual
2533                 UNION
2534                 SELECT acc.cust_account_id
2535                 FROM ar_paying_relationships_v rel,
2536                      hz_cust_accounts acc
2537                 WHERE rel.party_id = acc.party_id
2538                 AND rel.related_cust_account_id = gt.bill_to_customer_id
2539                 AND gt.trx_date BETWEEN effective_start_date
2540                                   AND effective_end_date));
2541 
2542     IF pg_debug = 'Y'
2543     THEN
2544         debug ('AR_INVOICE_UTILS.validate_related_cust_trx_id(-)' );
2545     END IF;
2546 
2547     EXCEPTION
2548       WHEN OTHERS THEN
2549         x_errmsg := 'Error in AR_INVOICE_UTILS.validate_related_cust_trx_id '
2550           ||sqlerrm;
2551         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2552         RETURN;
2553 
2554 END validate_related_cust_trx_id;
2555 
2556 PROCEDURE validate_gl_date (
2557     x_errmsg                    OUT NOCOPY  VARCHAR2,
2558     x_return_status             OUT NOCOPY  VARCHAR2) IS
2559 
2560     /* 5921925 - added post_to_gl logic */
2561     CURSOR cglDate IS
2562         select hdr.trx_header_id, hdr.gl_date, hdr.invoicing_rule_id,
2563                NVL(tt.post_to_gl, 'N') post_to_gl
2564         from ar_trx_header_gt hdr,
2565              ra_cust_trx_types tt
2566         where tt.cust_trx_type_id = hdr.cust_trx_type_id;
2567 
2568     l_period_status  gl_period_statuses.closing_status%type  DEFAULT 'U';
2569 BEGIN
2570     IF pg_debug = 'Y'
2571     THEN
2572         debug ('AR_INVOICE_UTILS.validate_gl_date(+)' );
2573     END IF;
2574 
2575     x_return_status := FND_API.G_RET_STS_SUCCESS;
2576 
2577     FOR cglDateRec IN cglDate
2578     LOOP
2579 
2580       IF cglDateRec.post_to_gl = 'Y'
2581       THEN
2582         /* Validate the gl_date for open period, etc */
2583         BEGIN
2584             SELECT closing_status
2585             INTO l_period_status
2586             FROM   gl_period_statuses g,
2587                   gl_sets_of_books   b
2588             WHERE  b.set_of_books_id         = g.set_of_books_id
2589             AND    g.set_of_books_id         = arp_global.set_of_books_id
2590             AND    g.period_type             = b.accounted_period_type
2591             AND    g.application_id          = 222
2592             AND    g.adjustment_period_flag  = 'N'
2593             AND    closing_status           <> 'C'
2594             AND    trunc(cglDateRec.gl_date) between start_date and end_date;
2595 
2596             IF l_period_status NOT IN ( 'O', 'F')
2597             THEN
2598                 INSERT INTO ar_trx_errors_gt (
2599                     trx_header_id,
2600                     error_message,
2601                     invalid_value)
2602                     values
2603                     ( cglDateRec.trx_header_id,
2604                     arp_standard.fnd_message('AR_INAPI_NO_PERIOD_FOR_GL_DATE'),
2605                     cglDateRec.gl_date );
2606             END IF;
2607 
2608             IF cglDateRec.invoicing_rule_id IS NOT NULL
2609             THEN
2610                 IF cglDateRec.invoicing_rule_id = -2
2611                 THEN
2612                     IF l_period_status not in ('O', 'F' , 'U')
2613                     THEN
2614                         INSERT INTO ar_trx_errors_gt (
2615                             trx_header_id,
2616                             error_message,
2617                             invalid_value)
2618                         values
2619                             ( cglDateRec.trx_header_id,
2620                             arp_standard.fnd_message('AR_INAPI_BAD_GL_DATE_FOR_ADV'),
2621                             cglDateRec.gl_date );
2622                     END IF;
2623                 ELSIF cglDateRec.invoicing_rule_id = -3
2624                 THEN
2625                     IF l_period_status not in ('O', 'F' , 'U', 'N')
2626                     THEN
2627                         INSERT INTO ar_trx_errors_gt (
2628                             trx_header_id,
2629                             error_message,
2630                             invalid_value)
2631                         values
2632                             ( cglDateRec.trx_header_id,
2633                             arp_standard.fnd_message('AR_INAPI_BAD_GL_DATE_FOR_ARR'),
2634                             cglDateRec.gl_date );
2635                     END IF;
2636                 END IF;
2637             END IF;
2638 
2639         EXCEPTION
2640             WHEN NO_DATA_FOUND THEN
2641                 INSERT INTO ar_trx_errors_gt (
2642                     trx_header_id,
2643                     error_message,
2644                     invalid_value)
2645                     values
2646                     ( cglDateRec.trx_header_id,
2647                     arp_standard.fnd_message('AR_TAPI_NO_PERIOD_FOR_GL_DATE'),
2648                     cglDateRec.gl_date );
2649 
2650         END;
2651       ELSE
2652          /* post_to_gl = 'N'.. if a gl_date is supplied,
2653             then it's an error */
2654          IF cglDateRec.gl_date IS NOT NULL
2655          THEN
2656                 INSERT INTO ar_trx_errors_gt (
2657                     trx_header_id,
2658                     error_message,
2659                     invalid_value)
2660                     values
2661                     ( cglDateRec.trx_header_id,
2662                     arp_standard.fnd_message('AR_RAXTRX-1785'),
2663                     cglDateRec.gl_date );
2664 
2665          END IF;
2666       END IF;
2667     END LOOP;
2668 
2669     IF pg_debug = 'Y'
2670     THEN
2671         debug ('AR_INVOICE_UTILS.validate_gl_date(-)' );
2672     END IF;
2673 
2674     EXCEPTION
2675         WHEN OTHERS THEN
2676         x_errmsg := 'Error in AR_INVOICE_UTILS.validate_gl_date '||sqlerrm;
2677         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2678         RETURN;
2679 END;
2680 
2681 PROCEDURE validate_agreement_id (
2682     x_errmsg                    OUT NOCOPY  VARCHAR2,
2683     x_return_status             OUT NOCOPY  VARCHAR2) IS
2684 BEGIN
2685     IF pg_debug = 'Y'
2686     THEN
2687         debug ('AR_INVOICE_UTILS.validate_agreement_id(+)' );
2688     END IF;
2689 
2690     x_return_status := FND_API.G_RET_STS_SUCCESS;
2691 
2692     INSERT INTO ar_trx_errors_gt (
2693             trx_header_id,
2694             error_message,
2695             invalid_value)
2696         SELECT trx_header_id,
2697                arp_standard.fnd_message('AR_INAPI_INVALID_AGREEMENT_ID'),
2698                agreement_id
2699         FROM  ar_trx_header_gt gt
2700         WHERE  gt.agreement_id IS NOT NULL
2701         AND    NOT EXISTS (
2702                 SELECT 'X'
2703                 from hz_cust_accounts cust_acct,
2704                 hz_parties  party,
2705                 so_agreements a,
2706                 qp_lookups sl
2707                 where a.agreement_type_code = sl.lookup_code
2708                 and sl.lookup_type = 'QP_AGREEMENT_TYPE'
2709                 and a.customer_id = cust_acct.cust_account_id(+)
2710                 and cust_acct.party_id = party.party_id(+)
2711                 and a.customer_id in ( select cr.cust_account_id
2712                                         from hz_cust_acct_relate cr
2713                                         where related_cust_account_id =
2714                                                 gt.bill_to_customer_id
2715                                         and cr.status = 'A'
2716                                         and cr.bill_to_flag='Y'
2717                                         union
2718                                         select to_number(gt.bill_to_customer_id)
2719                                         from dual
2720                                         union
2721                                         select -1 /* no customer case */ from dual )
2722                 and gt.trx_date between
2723                         nvl( trunc( a.start_date_active ), gt.trx_date )
2724                         and nvl( trunc( a.end_date_active ), gt.trx_date )
2725                 and gt.agreement_id = a.agreement_id);
2726 
2727      IF pg_debug = 'Y'
2728     THEN
2729         debug ('AR_INVOICE_UTILS.validate_agreement_id(-)' );
2730     END IF;
2731 
2732     EXCEPTION
2733       WHEN OTHERS THEN
2734         x_errmsg := 'Error in AR_INVOICE_UTILS.validate_agreement_id '||
2735           sqlerrm;
2736         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2737         RETURN;
2738 
2739 END validate_agreement_id;
2740 
2741 PROCEDURE Get_batch_source_details (
2742     x_errmsg                    OUT NOCOPY  VARCHAR2,
2743     x_return_status             OUT NOCOPY  VARCHAR2) IS
2744 
2745     l_bs_batch_auto_num_flag            ra_batch_sources.auto_batch_numbering_flag%type;
2746     l_bs_auto_trx_num_flag              ra_batch_sources.auto_trx_numbering_flag%type;
2747     l_dft_ref                           ra_batch_sources.default_reference%type;
2748     l_cust_trx_type_id                  ra_batch_sources.default_inv_trx_type%type;
2749     l_copy_doc_number_flag              ra_batch_sources.copy_doc_number_flag%type;
2750 BEGIN
2751     IF pg_debug = 'Y'
2752     THEN
2753         debug ('AR_INVOICE_UTILS.Get_batch_source_details(+)' );
2754     END IF;
2755 
2756     x_return_status := FND_API.G_RET_STS_SUCCESS;
2757 
2758     SELECT   bs.auto_batch_numbering_flag,
2759              bs.auto_trx_numbering_flag,
2760              bs.default_reference,
2761              bs.default_inv_trx_type, -- trx_type_id,
2762              bs.copy_doc_number_flag
2763       INTO   l_bs_batch_auto_num_flag,
2764              l_bs_auto_trx_num_flag,
2765              l_dft_ref,
2766              l_cust_trx_type_id,
2767              l_copy_doc_number_flag
2768       FROM   RA_BATCH_SOURCES bs
2769       WHERE  batch_source_id = (SELECT gt.batch_source_id
2770                              FROM   ar_trx_header_gt gt
2771                              WHERE  rownum =1 );
2772 
2773         IF pg_debug = 'Y'
2774         THEN
2775             debug ('Cust Trx Type Id ' || l_cust_trx_type_id);
2776             debug ('Auto Batch Numbering Flag ' || l_bs_batch_auto_num_flag);
2777             debug ('Auto Trx Numbering Flag ' || l_bs_auto_trx_num_flag);
2778         END IF;
2779 
2780       -- Bug # 3099975
2781       -- ORASHID
2782       -- 21-AUG-2003 (END)
2783 
2784 
2785       UPDATE ar_trx_header_gt
2786         SET  auto_batch_numbering_flag =  l_bs_batch_auto_num_flag,
2787              auto_trx_numbering_flag = l_bs_auto_trx_num_flag,
2788              copy_doc_number_flag  = l_copy_doc_number_flag,
2789              cust_trx_type_id = NVL(cust_trx_type_id, l_cust_trx_type_id),
2790              ct_reference = decode(l_dft_ref,
2791                                    1, interface_header_attribute1,
2792                                    2, interface_header_attribute2,
2793                                    3, interface_header_attribute3,
2794                                    4, interface_header_attribute4,
2795                                    5, interface_header_attribute5,
2796                                    6, interface_header_attribute6,
2797                                    7, interface_header_attribute7,
2798                                    8, interface_header_attribute8,
2799                                    9, interface_header_attribute9,
2800                                    10, interface_header_attribute10,
2801                                    11, interface_header_attribute11,
2802                                    12, interface_header_attribute12,
2803                                    13, interface_header_attribute13,
2804                                    14, interface_header_attribute14,
2805                                    15, interface_header_attribute15,
2806                                    NULL );
2807 
2808       IF pg_debug = 'Y'
2809       THEN
2810         debug ('AR_INVOICE_UTILS.Get_batch_source_details(-)' );
2811       END IF;
2812 
2813       EXCEPTION
2814         WHEN NO_DATA_FOUND THEN
2815 	       INSERT INTO ar_trx_errors_gt (
2816                 trx_header_id,
2817                 error_message,
2818                 invalid_value)
2819             SELECT gt.trx_header_id,
2820                    arp_standard.fnd_message('AR_INAPI_INVALID_BATCH_SOURCE'),
2821                    gt.batch_source_id
2822             FROM  ar_trx_header_gt gt;
2823         WHEN OTHERS THEN
2824             x_errmsg := 'Error in AR_INVOICE_UTILS.Get_batch_source_details '||sqlerrm;
2825             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2826             RETURN;
2827 
2828 END get_batch_source_details;
2829 
2830 
2831 PROCEDURE Get_trx_type_details (
2832     x_errmsg                    OUT NOCOPY  VARCHAR2,
2833     x_return_status             OUT NOCOPY  VARCHAR2) IS
2834 
2835     l_default_printing_option          ra_cust_trx_types.default_printing_option%type;
2836     l_default_status                   ra_cust_trx_types.default_status%type;
2837     l_allow_freight_flag               ra_cust_trx_types.allow_freight_flag%type;
2838     l_tax_calculation_flag             ra_cust_trx_types.tax_calculation_flag%type;
2839     l_allow_overapplication_flag       ra_cust_trx_types.allow_overapplication_flag%type;
2840     l_creation_sign                    ra_cust_trx_types.creation_sign%type;
2841     l_natural_application_flag         ra_cust_trx_types.natural_application_only_flag%type;
2842     l_accounting_affect_flag           ra_cust_trx_types.accounting_affect_flag%type;
2843     l_cust_trx_type_name               ra_cust_trx_types.name%type;
2844     l_trx_type                         ra_cust_trx_types.type%type;
2845 
2846     CURSOR cust_trx_type_c IS
2847         SELECT distinct cust_trx_type_id
2848         FROM  ar_trx_header_gt;
2849 BEGIN
2850 
2851     IF pg_debug = 'Y'
2852     THEN
2853         debug ('AR_INVOICE_UTILS.Get_trx_type_details(+)' );
2854     END IF;
2855     x_return_status := FND_API.G_RET_STS_SUCCESS;
2856 
2857     FOR cust_trx_type_rec IN cust_trx_type_c
2858     LOOP
2859         BEGIN
2860             SELECT default_printing_option,
2861                    default_status,
2862                    allow_freight_flag,
2863                    tax_calculation_flag,
2864                    allow_overapplication_flag,
2865                    creation_sign,
2866                    natural_application_only_flag,
2867                    accounting_affect_flag,
2868                    name,
2869                    type
2870             INTO   l_default_printing_option,
2871                    l_default_status,
2872                    l_allow_freight_flag,
2873                    l_tax_calculation_flag,
2874                    l_allow_overapplication_flag,
2875                    l_creation_sign,
2876                    l_natural_application_flag,
2877                    l_accounting_affect_flag,
2878                    l_cust_trx_type_name,
2879                    l_trx_type
2880             FROM   ra_cust_trx_types
2881             WHERE  cust_trx_type_id = cust_trx_type_rec.cust_trx_type_id;
2882 
2883 
2884             IF pg_debug = 'Y'
2885             THEN
2886                 debug ('Default Priniting Option '|| l_default_printing_option );
2887                 debug ('Default Status '|| l_default_status );
2888                 debug ('Tax Calculation Flag '|| l_tax_calculation_flag );
2889                 debug ('Over Application Flag '|| l_allow_overapplication_flag );
2890                 debug ('Creation Sign '|| l_creation_sign );
2891                 debug ('Natural Application Flag '|| l_natural_application_flag );
2892                 debug ('Accounting affect flag  '|| l_accounting_affect_flag );
2893                 debug ('Cust Trx Type Name  '|| l_cust_trx_type_name );
2894             END IF;
2895 
2896             -- Bug # 3099975
2897             -- ORASHID
2898             -- 21-AUG-2003 (END)
2899             UPDATE ar_trx_header_gt
2900               SET  printing_option = nvl(printing_option,l_default_printing_option),
2901                    status_trx = NVL(status_trx,l_default_status),
2902                    allow_freight_flag  = l_allow_freight_flag,
2903                    tax_calculation_flag = l_tax_calculation_flag,
2904                    allow_overapplication_flag = l_allow_overapplication_flag,
2905                    creation_sign = l_creation_sign,
2906                    natural_application_only_flag = l_natural_application_flag,
2907                    accounting_affect_flag = l_accounting_affect_flag,
2908                    cust_trx_type_name    = l_cust_trx_type_name,
2909                    trx_class = l_trx_type
2910               WHERE cust_trx_type_id =  cust_trx_type_rec.cust_trx_type_id;
2911 
2912               EXCEPTION
2913                 WHEN NO_DATA_FOUND THEN
2914                      INSERT INTO ar_trx_errors_gt (
2915                         trx_header_id,
2916                         error_message,
2917                         invalid_value)
2918                      SELECT gt.trx_header_id,
2919                         arp_standard.fnd_message('ARTA_INVALID_TRX_TYPE'),
2920                         gt.cust_trx_type_id
2921                      FROM  ar_trx_header_gt gt
2922                      WHERE gt.cust_trx_type_id = cust_trx_type_rec.cust_trx_type_id;
2923 
2924         END;
2925     END LOOP;
2926     IF pg_debug = 'Y'
2927     THEN
2928         debug ('AR_INVOICE_UTILS.Get_trx_type_details(-)' );
2929     END IF;
2930 
2931     EXCEPTION
2932 
2933 
2934         WHEN OTHERS THEN
2935             x_errmsg := 'Error in AR_INVOICE_UTILS.Get_trx_type_details '||sqlerrm;
2936             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2937             RETURN;
2938 
2939 END get_trx_type_details;
2940 
2941 
2942 PROCEDURE populate_salesreps (
2943     x_errmsg                    OUT NOCOPY  VARCHAR2,
2944     x_return_status             OUT NOCOPY  VARCHAR2) IS
2945 
2946 BEGIN
2947     IF pg_debug = 'Y'
2948     THEN
2949         debug ('AR_INVOICE_UTILS.populate_salesreps(+)' );
2950     END IF;
2951 
2952     x_return_status := FND_API.G_RET_STS_SUCCESS;
2953 /*Bug8266696*/
2954     UPDATE ar_trx_header_gt gt
2955         SET gt.primary_salesrep_id =
2956             ( SELECT SU.PRIMARY_SALESREP_ID
2957               FROM HZ_CUST_ACCT_SITES ACCT_SITE,
2958                    HZ_PARTY_SITES PARTY_SITE,
2959                    HZ_LOCATIONS LOC,
2960                    HZ_CUST_SITE_USES SU,
2961                    FND_TERRITORIES_VL T
2962               WHERE  ACCT_SITE.CUST_ACCT_SITE_ID = SU.CUST_ACCT_SITE_ID
2963               AND    ACCT_SITE.PARTY_SITE_ID = PARTY_SITE.PARTY_SITE_ID
2964               AND    LOC.LOCATION_ID =  PARTY_SITE.LOCATION_ID
2965               AND    LOC.COUNTRY = T.TERRITORY_CODE
2966               AND    ACCT_SITE.CUST_ACCOUNT_ID = gt.bill_to_customer_id
2967               AND    SU.SITE_USE_ID = NVL(gt.bill_to_site_use_id, SU.SITE_USE_ID)
2968               AND    SU.SITE_USE_CODE = 'BILL_TO'
2969               AND    (SU.SITE_USE_ID =   gt.bill_to_site_use_id
2970                    OR (gt.bill_to_site_use_id IS NULL
2971                    AND SU.STATUS = 'A'  AND ACCT_SITE.STATUS = 'A' ))
2972               AND SU.PRIMARY_FLAG = 'Y' )
2973      WHERE    gt.primary_salesrep_id IS NULL
2974      AND NVL(gt.late_charges_assessed,'N') <> 'Y';
2975 
2976     IF pg_debug = 'Y'
2977     THEN
2978         debug ('AR_INVOICE_UTILS.populate_salesreps(-)' );
2979     END IF;
2980 
2981     EXCEPTION
2982         WHEN OTHERS THEN
2983             x_errmsg := 'Error in AR_INVOICE_UTILS.populate_salesreps '||sqlerrm;
2984             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2985             RETURN;
2986 
2987 END populate_salesreps;
2988 
2989 
2990 PROCEDURE populate_bill_to_customer_id (
2991     x_errmsg                    OUT NOCOPY  VARCHAR2,
2992     x_return_status             OUT NOCOPY  VARCHAR2) IS
2993 
2994 BEGIN
2995 
2996   IF pg_debug = 'Y' THEN
2997     debug ('AR_INVOICE_UTILS.populate_bill_to_customer_id(+)' );
2998   END IF;
2999 
3000   x_return_status := FND_API.G_RET_STS_SUCCESS;
3001 
3002   -- if bill to customer number is passed then that should be used
3003   -- derive the bill to customer id.
3004 
3005   UPDATE ar_trx_header_gt gt
3006   SET    gt.bill_to_customer_id =
3007            (SELECT cust_acct.cust_account_id
3008             FROM   hz_cust_accounts cust_acct
3009             WHERE  cust_acct.account_number = gt.bill_to_account_number
3010             AND    cust_acct.status = 'A'
3011             AND   gt.bill_to_customer_id IS NULL
3012             AND   gt.bill_to_account_number IS NOT NULL
3013            )
3014   WHERE  gt.bill_to_customer_id IS NULL
3015   AND    gt.bill_to_account_number IS NOT NULL;
3016 
3017   -- if ship to customer number is passed then that should be used
3018   -- derive the bill to customer id.
3019 
3020   UPDATE ar_trx_header_gt gt
3021   SET    gt.bill_to_customer_id =
3022            (SELECT cust_acct.cust_account_id
3023             FROM   hz_cust_accounts cust_acct
3024             WHERE  cust_acct.account_number = gt.ship_to_account_number
3025             AND    cust_acct.status = 'A'
3026             AND   gt.bill_to_customer_id IS NULL
3027             AND   gt.ship_to_account_number IS NOT NULL
3028            )
3029   WHERE  gt.bill_to_customer_id IS NULL
3030   AND    gt.bill_to_account_number IS NOT NULL;
3031 
3032   -- if ship to customer id is passed then that should be used
3033   -- derive the bill to customer id.
3034 
3035   UPDATE ar_trx_header_gt gt
3036   SET    gt.bill_to_customer_id = gt.ship_to_customer_id
3037   WHERE  gt.bill_to_customer_id IS NULL
3038   AND    gt.ship_to_customer_id IS NOT NULL;
3039 
3040 
3041   -- for the remaining rows bill to customer name should be used
3042   -- derive the bill to customer id.
3043 
3044   UPDATE ar_trx_header_gt gt
3045   SET    gt.bill_to_customer_id =
3046            (SELECT cust_acct.cust_account_id
3047             FROM  hz_cust_accounts cust_acct,
3048                   hz_parties party
3049             WHERE cust_acct.party_id = party.party_id
3050             AND   party.party_name = gt.bill_to_customer_name
3051             AND   gt.bill_to_customer_id IS NULL
3052             AND   gt.bill_to_customer_name IS NOT NULL
3053            )
3054   WHERE  gt.bill_to_customer_id IS NULL
3055   AND    gt.bill_to_customer_name IS NOT NULL;
3056 
3057 
3058   IF pg_debug = 'Y' THEN
3059     debug ('AR_INVOICE_UTILS.populate_bill_to_customer_id(-)' );
3060   END IF;
3061 
3062   EXCEPTION
3063     WHEN OTHERS THEN
3064       x_errmsg := 'Error in AR_INVOICE_UTILS.populate_bill_to_customer_id '
3065         ||sqlerrm;
3066       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3067       RETURN;
3068 
3069 END populate_bill_to_customer_id;
3070 
3071 
3072 PROCEDURE populate_bill_to_site_use_id (
3073         x_errmsg                    OUT NOCOPY  VARCHAR2,
3074         x_return_status             OUT NOCOPY  VARCHAR2)  IS
3075 
3076 BEGIN
3077 
3078   IF pg_debug = 'Y' THEN
3079     debug ('AR_INVOICE_UTILS.populate_bill_to_site_use_id (+)' );
3080   END IF;
3081 
3082   x_return_status := FND_API.G_RET_STS_SUCCESS;
3083 
3084   -- We are here to populate bill_to_site_use_id column.   We should only
3085   -- do it for rows where it is not already populated.  Moreover, first we
3086   -- should see if bill_to_address_id is populated then we should derive the
3087   -- bill_to_site_use_id from that.
3088 
3089   UPDATE ar_trx_header_gt gt
3090   SET    bill_to_site_use_id = (
3091            SELECT site_use_id
3092            FROM   hz_cust_site_uses
3093            WHERE  site_use_code = 'BILL_TO'
3094            AND    status = 'A'
3095            AND    cust_acct_site_id = gt.bill_to_address_id
3096            AND    gt.bill_to_site_use_id IS NULL
3097            AND    gt.bill_to_address_id IS NOT NULL)
3098   WHERE  gt.bill_to_site_use_id IS NULL
3099   AND    gt.bill_to_address_id IS NOT NULL;
3100 
3101   /* 10041924 for those addresses that did not have active
3102      sites, use the max inactive one */
3103   UPDATE ar_trx_header_gt gt
3104   SET    bill_to_site_use_id = (
3105            SELECT MAX(site_use_id)
3106            FROM   hz_cust_site_uses
3107            WHERE  site_use_code = 'BILL_TO'
3108            AND    status = 'I' /* 10041924, try inactive */
3109            AND    cust_acct_site_id = gt.bill_to_address_id
3110            AND    gt.bill_to_site_use_id IS NULL
3111            AND    gt.bill_to_address_id IS NOT NULL
3112            GROUP BY cust_acct_site_id)
3113   WHERE  gt.bill_to_site_use_id IS NULL
3114   AND    gt.bill_to_address_id IS NOT NULL;
3115 
3116   -- Now we will worry about cases where only customer id
3117   -- is populated and we must derive the primary ship to site id.
3118 
3119   UPDATE ar_trx_header_gt gt
3120   SET    bill_to_site_use_id = (
3121            SELECT site_use_id
3122            FROM   hz_cust_site_uses
3123            WHERE  primary_flag = 'Y'
3124            AND    site_use_code = 'BILL_TO'
3125            AND    cust_acct_site_id IN (
3126                     SELECT cust_acct_site_id
3127                     FROM   hz_cust_acct_sites
3128                    WHERE  cust_account_id = gt.bill_to_customer_id))
3129   WHERE gt.bill_to_site_use_id IS NULL;
3130 
3131   IF pg_debug = 'Y' THEN
3132     debug ('AR_INVOICE_UTILS.populate_bill_to_site_use_id (-)' );
3133   END IF;
3134 
3135   EXCEPTION
3136     WHEN OTHERS THEN
3137       x_errmsg := 'Error in AR_INVOICE_UTILS.populate_bill_to_site_use_id '
3138         ||sqlerrm;
3139       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3140       RETURN;
3141 
3142 END populate_bill_to_site_use_id;
3143 
3144 PROCEDURE populate_paying_site_use_id (
3145     x_errmsg                    OUT NOCOPY  VARCHAR2,
3146     x_return_status             OUT NOCOPY  VARCHAR2) IS
3147 
3148     CURSOR cSiteUSe IS
3149          select sgt.bill_to_site_use_id,  sgt.bill_to_customer_id,
3150                 sgt.paying_customer_id
3151             from ar_trx_header_gt sgt
3152             WHERE sgt.bill_to_customer_id = sgt.paying_customer_id;
3153 
3154 BEGIN
3155   IF pg_debug = 'Y' THEN
3156     debug ('AR_INVOICE_UTILS.populate_paying_site_use_id(+)' );
3157   END IF;
3158 
3159   -- first populate paying_customer_id if it is null
3160   UPDATE ar_trx_header_gt
3161     set  paying_customer_id = bill_to_customer_id
3162   WHERE  paying_customer_id IS NULL;
3163 
3164   FOR cSiteUSeRec IN cSiteUSe
3165   LOOP
3166         UPDATE ar_trx_header_gt ugt
3167             set ugt.paying_site_use_id = cSiteUSeRec.bill_to_site_use_id
3168         WHERE  ugt.paying_site_use_id IS NULL
3169         AND    ugt.paying_customer_id = cSiteUSeRec.paying_customer_id;
3170 
3171   END LOOP;
3172 
3173 
3174   -- incase paying_customer_id and bill_to_customer_id is
3175   -- different
3176 
3177   UPDATE ar_trx_header_gt gt
3178   SET    paying_site_use_id = (
3179            SELECT site_use_id
3180            FROM   hz_cust_site_uses
3181            WHERE  primary_flag = 'Y'
3182            AND    site_use_code = 'BILL_TO'
3183            AND    cust_acct_site_id IN (
3184                     SELECT cust_acct_site_id
3185                     FROM   hz_cust_acct_sites
3186                    WHERE  cust_account_id = gt.paying_customer_id))
3187   WHERE gt.paying_site_use_id IS NULL;
3188 
3189   IF pg_debug = 'Y' THEN
3190     debug ('AR_INVOICE_UTILS.populate_paying_site_use_id(-)' );
3191   END IF;
3192 END;
3193 
3194 PROCEDURE populate_bill_to_address_id (
3195     x_errmsg                    OUT NOCOPY  VARCHAR2,
3196     x_return_status             OUT NOCOPY  VARCHAR2) IS
3197 
3198 BEGIN
3199 
3200   IF pg_debug = 'Y' THEN
3201     debug ('AR_INVOICE_UTILS.populate_bill_to_address_id(+)' );
3202   END IF;
3203 
3204   x_return_status := FND_API.G_RET_STS_SUCCESS;
3205 
3206   -- We are here to populate bill_to_address_id column.   We should only
3207   -- do it for rows where it is not already populated.  Moreover, first we
3208   -- should see if bill_to_site_use_id is populated then we should derive the
3209   -- bill_to_address_id from that.
3210 
3211   UPDATE ar_trx_header_gt gt
3212   SET    bill_to_address_id = (
3213            SELECT site_use_id
3214            FROM   hz_cust_site_uses
3215            WHERE  site_use_code = 'BILL_TO'
3216            AND    site_use_id= gt.bill_to_site_use_id
3217            AND    gt.bill_to_address_id IS NULL
3218            AND    gt.bill_to_site_use_id IS NOT NULL)
3219   WHERE  gt.bill_to_address_id IS NULL
3220   AND    gt.bill_to_site_use_id IS NOT NULL;
3221 
3222   -- Now if it is still not populated then you default it from
3223   -- primary bill to.
3224 
3225   UPDATE ar_trx_header_gt gt
3226   SET gt.bill_to_address_id = (
3227       SELECT su.cust_acct_site_id
3228       FROM   hz_cust_acct_sites acct_site,
3229              hz_party_sites party_site,
3230              hz_locations loc,
3231              hz_cust_site_uses su,
3232              fnd_territories_vl t
3233       WHERE  acct_site.cust_acct_site_id = su.cust_acct_site_id
3234       AND    acct_site.party_site_id = party_site.party_site_id
3235       AND    loc.location_id =  party_site.location_id
3236       AND    loc.country = t.territory_code
3237       AND    acct_site.cust_account_id = gt.bill_to_customer_id
3238       AND    SU.SITE_USE_ID = NVL(gt.bill_to_site_use_id, SU.SITE_USE_ID)
3239       AND    SU.SITE_USE_CODE = 'BILL_TO'
3240       AND    (SU.SITE_USE_ID =   gt.bill_to_site_use_id
3241               OR (gt.bill_to_site_use_id IS NULL
3242       AND SU.STATUS = 'A'  AND ACCT_SITE.STATUS = 'A' ))
3243       AND SU.PRIMARY_FLAG = 'Y' )
3244   WHERE gt.bill_to_address_id IS NULL;
3245 
3246   IF pg_debug = 'Y' THEN
3247     debug ('AR_INVOICE_UTILS.populate_bill_to_address_id(-)' );
3248   END IF;
3249 
3250   EXCEPTION
3251     WHEN OTHERS THEN
3252       x_errmsg := 'Error in AR_INVOICE_UTILS.populate_bill_to_address_id '
3253         ||sqlerrm;
3254       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3255       RETURN;
3256 
3257 END populate_bill_to_address_id;
3258 
3259 
3260 PROCEDURE populate_remit_to_address_id (
3261     x_errmsg                    OUT NOCOPY  VARCHAR2,
3262     x_return_status             OUT NOCOPY  VARCHAR2) IS
3263 
3264 l_site_use_id               NUMBER;
3265 
3266 CURSOR HdrGtc IS
3267     SELECT distinct bill_to_site_use_id
3268     FROM   ar_trx_header_gt
3269     WHERE  bill_to_site_use_id IS NOT NULL
3270     AND    remit_to_address_id IS NULL;
3271 
3272 -- Get country, state, and zip code info. for each bill to site
3273 CURSOR bill_to_site_c IS
3274     SELECT su.site_use_id,
3275            loc.state,
3276            loc.country,
3277            loc.postal_code
3278     FROM   hz_cust_acct_sites acct_site,
3279            hz_party_sites party_site,
3280            hz_locations loc,
3281            hz_cust_site_uses   su
3282     WHERE  acct_site.cust_acct_site_id  = su.cust_acct_site_id
3283     AND    su.site_use_id = l_site_use_id
3284     AND    acct_site.party_site_id = party_site.party_site_id
3285     AND    loc.location_id = party_site.location_id;
3286 BEGIN
3287     IF pg_debug = 'Y'
3288     THEN
3289         debug ('AR_INVOICE_UTILS.populate_remit_to_address_id (+)' );
3290     END IF;
3291 
3292     x_return_status := FND_API.G_RET_STS_SUCCESS;
3293     FOR HdrGtRec IN HdrGtc
3294     LOOP
3295         l_site_use_id := HdrGtRec.bill_to_site_use_id;
3296         FOR bill_to_site_rec IN bill_to_site_c
3297         LOOP
3298             IF pg_debug = 'Y'
3299             THEN
3300                 debug ('Site Use Id ' || bill_to_site_rec.site_use_id);
3301                 debug ('State ' || bill_to_site_rec.state);
3302                 debug ('Country ' || bill_to_site_rec.country);
3303                 debug ('Postal code  ' || bill_to_site_rec.postal_code);
3304             END IF;
3305 
3306             UPDATE ar_trx_header_gt gt
3307             SET  remit_to_address_id = (
3308                     SELECT acct_site.cust_acct_site_id
3309                     FROM   hz_cust_acct_sites acct_site,
3310                            hz_party_sites party_site,
3311                            hz_locations loc,
3312                            fnd_territories_vl territory,
3313                            ra_remit_tos  rt
3314                     WHERE  NVL( acct_site.status, 'A' )  = 'A'
3315                     AND    acct_site.cust_acct_site_id  = rt.address_id
3316                     AND    acct_site.party_site_id = party_site.party_site_id
3317                     AND    loc.location_id = party_site.location_id
3318                     AND    rt.status             = 'A'
3319                     AND    rt.country            = bill_to_site_rec.country
3320                     AND    loc.country = territory.territory_code
3321                     AND    (
3322                             bill_to_site_rec.state = NVL( rt.state, bill_to_site_rec.state )
3323                             OR
3324                                 (
3325                                 bill_to_site_rec.state IS NULL   AND
3326                                 rt.state      IS NULL
3327                                 )
3328                             OR  (
3329                                 bill_to_site_rec.state IS NULL         AND
3330                                 bill_to_site_rec.postal_code <= NVL( rt.postal_code_high,
3331                                                 bill_to_site_rec.postal_code )   AND
3332                                 bill_to_site_rec.postal_code >= NVL( rt.postal_code_low,
3333                                                 bill_to_site_rec.postal_code )   AND
3334                                     (
3335                                     postal_code_low  IS NOT NULL
3336                                     OR  postal_code_high IS NOT NULL
3337                                     )
3338                                 )
3339                             )
3340                     AND    (
3341                             (
3342                                 bill_to_site_rec.postal_code <= NVL( rt.postal_code_high,
3343                                                  bill_to_site_rec.postal_code )  AND
3344                                 bill_to_site_rec.postal_code >= NVL( rt.postal_code_low,
3345                                                  bill_to_site_rec.postal_code )
3346                             )
3347                     OR      (
3348                                 bill_to_site_rec.postal_code IS NULL  AND
3349                                 rt.postal_code_low  IS NULL  AND
3350                                 rt.postal_code_high IS NULL
3351                             )
3352                            ) and rownum = 1)
3353             WHERE gt.bill_to_site_use_id = bill_to_site_rec.site_use_id
3354             AND   gt.remit_to_address_id IS NULL;
3355         END LOOP;
3356     END LOOP;
3357     IF pg_debug = 'Y'
3358     THEN
3359         debug ('AR_INVOICE_UTILS.populate_remit_to_address_id (-)' );
3360     END IF;
3361 
3362     EXCEPTION
3363         WHEN OTHERS THEN
3364             x_errmsg := 'Error in AR_INVOICE_UTILS.populate_remit_to_address_id '||sqlerrm;
3365             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3366             RETURN;
3367 
3368 END populate_remit_to_address_id;
3369 
3370 
3371 PROCEDURE populate_bill_to_contact_id (
3372     x_errmsg                    OUT NOCOPY  VARCHAR2,
3373     x_return_status             OUT NOCOPY  VARCHAR2) IS
3374 
3375     CURSOR bill_to_customer_id_c IS
3376         SELECT distinct bill_to_customer_id,
3377                bill_to_address_id
3378         FROM   ar_trx_header_gt
3379         WHERE  bill_to_contact_id IS NULL;
3380     l_contact_id            HZ_CUST_ACCOUNT_ROLES.CUST_ACCOUNT_ROLE_ID%type;
3381 BEGIN
3382     IF pg_debug = 'Y'
3383     THEN
3384         debug ('AR_INVOICE_UTILS.populate_bill_to_contact_id(+)' );
3385     END IF;
3386     x_return_status := FND_API.G_RET_STS_SUCCESS;
3387     -- the cursor is required becoz the select to get the contact id
3388     -- returns more than one row. In case it returns more than one row
3389     -- then contact id will be null.
3390     FOR bill_to_customer_id_rec IN bill_to_customer_id_c
3391     LOOP
3392         BEGIN
3393             SELECT distinct ACCT_ROLE.CUST_ACCOUNT_ROLE_ID  -- CONTACT_ID
3394             INTO   l_contact_id
3395               FROM HZ_CUST_ACCOUNT_ROLES ACCT_ROLE,HZ_PARTIES PARTY,HZ_RELATIONSHIPS REL,
3396                    HZ_ORG_CONTACTS ORG_CONT,HZ_PARTIES REL_PARTY
3397               WHERE ACCT_ROLE.PARTY_ID = REL.PARTY_ID
3398               AND ACCT_ROLE.ROLE_TYPE = 'CONTACT'
3399               AND  ORG_CONT.PARTY_RELATIONSHIP_ID = REL.RELATIONSHIP_ID
3400               AND REL.SUBJECT_ID =  PARTY.PARTY_ID
3401               AND REL.PARTY_ID = REL_PARTY.PARTY_ID
3402               AND  REL.SUBJECT_TABLE_NAME = 'HZ_PARTIES'
3403               AND REL.OBJECT_TABLE_NAME =  'HZ_PARTIES'
3404               AND REL.DIRECTIONAL_FLAG = 'F'
3405               AND ACCT_ROLE.CUST_ACCOUNT_ID  = bill_to_customer_id_rec.bill_to_customer_id
3406               AND ACCT_ROLE.CUST_ACCT_SITE_ID =  bill_to_customer_id_rec.bill_to_address_id
3407               AND ACCT_ROLE.STATUS = 'A' ;
3408 
3409         IF pg_debug = 'Y'
3410         THEN
3411             debug ('Bill to contact Id '|| l_contact_id);
3412             debug ('Bill to customer Id ' || bill_to_customer_id_rec.bill_to_customer_id);
3413             debug ('Bill to Address Id ' || bill_to_customer_id_rec.bill_to_address_id);
3414         END IF;
3415         UPDATE ar_trx_header_gt
3416             SET  bill_to_contact_id = l_contact_id
3417         WHERE bill_to_customer_id = bill_to_customer_id_rec.bill_to_customer_id
3418         AND   bill_to_address_id = bill_to_customer_id_rec.bill_to_address_id
3419         AND   bill_to_contact_id IS NOT NULL;
3420 
3421         EXCEPTION
3422             WHEN NO_DATA_FOUND THEN
3423                 NULL;
3424             WHEN TOO_MANY_ROWS THEN
3425                 NULL;
3426         END;
3427     END LOOP;
3428     IF pg_debug = 'Y'
3429     THEN
3430         debug ('AR_INVOICE_UTILS.populate_bill_to_contact_id(-)' );
3431     END IF;
3432     EXCEPTION
3433       WHEN OTHERS THEN
3434         x_errmsg := 'Error in AR_INVOICE_UTILS.populate_bill_to_contact_id '
3435           ||sqlerrm;
3436         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3437         RETURN;
3438 
3439 END populate_bill_to_contact_id;
3440 
3441 
3442 PROCEDURE populate_ship_to_customer_id (
3443     x_errmsg                    OUT NOCOPY  VARCHAR2,
3444     x_return_status             OUT NOCOPY  VARCHAR2) IS
3445 
3446 BEGIN
3447 
3448   IF pg_debug = 'Y' THEN
3449     debug ('AR_INVOICE_UTILS.populate_ship_to_customer_id(+)' );
3450   END IF;
3451 
3452   x_return_status := FND_API.G_RET_STS_SUCCESS;
3453 
3454   -- if bill to customer number is passed then that should be used
3455   -- derive the bill to customer id.
3456 
3457   UPDATE ar_trx_header_gt gt
3458   SET    gt.ship_to_customer_id =
3459            (SELECT cust_acct.cust_account_id
3460             FROM   hz_cust_accounts cust_acct
3461             WHERE  cust_acct.account_number = gt.ship_to_account_number
3462             AND    cust_acct.status = 'A'
3463             AND   gt.ship_to_customer_id IS NULL
3464             AND   gt.ship_to_account_number IS NOT NULL
3465            )
3466   WHERE  gt.ship_to_customer_id IS NULL
3467   AND    gt.ship_to_account_number IS NOT NULL;
3468 
3469   -- for the remaining rows bill to customer name should be used
3470   -- derive the bill to customer id.
3471 
3472   UPDATE ar_trx_header_gt gt
3473   SET    gt.ship_to_customer_id =
3474            (SELECT cust_acct.cust_account_id
3475             FROM  hz_cust_accounts cust_acct,
3476                   hz_parties party
3477             WHERE cust_acct.party_id = party.party_id
3478             AND   party.party_name = gt.ship_to_customer_name
3479             AND    cust_acct.status = 'A'
3480             AND   gt.ship_to_customer_id IS NULL
3481             AND   gt.ship_to_customer_name IS NOT NULL
3482            )
3483   WHERE  gt.ship_to_customer_id IS NULL
3484   AND    gt.ship_to_customer_name IS NOT NULL;
3485 
3486 
3487   IF pg_debug = 'Y' THEN
3488     debug ('AR_INVOICE_UTILS.populate_ship_to_customer_id(-)' );
3489   END IF;
3490 
3491   EXCEPTION
3492     WHEN OTHERS THEN
3493       x_errmsg := 'Error in AR_INVOICE_UTILS.populate_ship_to_customer_id '
3494         ||sqlerrm;
3495       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3496       RETURN;
3497 
3498 END populate_ship_to_customer_id;
3499 
3500 
3501 PROCEDURE populate_ship_to_address_id (
3502     x_errmsg                    OUT NOCOPY  VARCHAR2,
3503     x_return_status             OUT NOCOPY  VARCHAR2)  IS
3504 
3505 BEGIN
3506 
3507   IF pg_debug = 'Y' THEN
3508     debug ('AR_INVOICE_UTILS.populate_ship_to_address_id(+)' );
3509   END IF;
3510 
3511   x_return_status := FND_API.G_RET_STS_SUCCESS;
3512 
3513   -- We are here to populate ship_to_address_id column.   We should only
3514   -- do it for rows where it is not already populated.  Moreover, first we
3515   -- should see if ship_to_site_use_id is populated then we should derive the
3516   -- ship_to_address_id from that.
3517 
3518   UPDATE ar_trx_header_gt gt
3519   SET    ship_to_address_id = (
3520            SELECT site_use_id
3521            FROM   hz_cust_site_uses
3522            WHERE  site_use_code = 'SHIP_TO'
3523            AND    site_use_id= gt.ship_to_site_use_id
3524            AND    gt.ship_to_address_id IS NULL
3525            AND    gt.ship_to_site_use_id IS NOT NULL)
3526   WHERE  gt.ship_to_address_id IS NULL
3527   AND    gt.ship_to_site_use_id IS NOT NULL;
3528 
3529   -- Now we will worry about cases where only customer id
3530   -- is populated and we must derive the primary ship to site id.
3531 
3532 
3533   UPDATE ar_trx_header_gt gt
3534   SET gt.ship_to_address_id = (
3535         SELECT su.cust_acct_site_id
3536         FROM   hz_cust_acct_sites acct_site,
3537                hz_party_sites party_site,
3538                hz_locations loc,
3539                hz_cust_site_uses su,
3540                fnd_territories_vl t
3541         WHERE  acct_site.cust_acct_site_id = su.cust_acct_site_id
3542         AND    acct_site.party_site_id = party_site.party_site_id
3543         AND    loc.location_id =  party_site.location_id
3544         AND    loc.country = t.territory_code
3545         AND    acct_site.cust_account_id = gt.ship_to_customer_id
3546         AND    su.site_use_id = nvl(gt.ship_to_site_use_id, su.site_use_id)
3547         AND    su.site_use_code = 'ship_to'
3548         AND    (su.site_use_id =   gt.ship_to_site_use_id
3549                 or (gt.ship_to_site_use_id IS NULL
3550         AND su.status = 'a'  AND acct_site.status = 'a' ))
3551         AND su.primary_flag = 'y' )
3552   WHERE gt.ship_to_address_id IS NULL;
3553 
3554   IF pg_debug = 'Y' THEN
3555     debug ('AR_INVOICE_UTILS.populate_ship_to_address_id(-)' );
3556   END IF;
3557 
3558   EXCEPTION
3559     WHEN OTHERS THEN
3560       x_errmsg := 'Error in AR_INVOICE_UTILS.populate_ship_to_address_id '
3561         ||sqlerrm;
3562       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3563       RETURN;
3564 
3565 END populate_ship_to_address_id;
3566 
3567 
3568 PROCEDURE populate_ship_to_contact_id (
3569     x_errmsg                    OUT NOCOPY  VARCHAR2,
3570     x_return_status             OUT NOCOPY  VARCHAR2)   IS
3571 
3572     CURSOR ship_to_customer_id_c IS
3573         SELECT distinct ship_to_customer_id,
3574                ship_to_address_id
3575         FROM   ar_trx_header_gt
3576         WHERE  ship_to_contact_id IS NULL;
3577     l_contact_id            HZ_CUST_ACCOUNT_ROLES.CUST_ACCOUNT_ROLE_ID%type;
3578 BEGIN
3579     IF pg_debug = 'Y'
3580     THEN
3581         debug ('AR_INVOICE_UTILS.populate_ship_to_contact_id(+)' );
3582     END IF;
3583 
3584     x_return_status := FND_API.G_RET_STS_SUCCESS;
3585 
3586     -- the cursor is required becoz the select to get the contact id
3587     -- returns more than one row. In case it returns more than one row
3588     -- then contact id will be null.
3589     FOR ship_to_customer_id_rec IN ship_to_customer_id_c
3590     LOOP
3591         BEGIN
3592             SELECT distinct ACCT_ROLE.CUST_ACCOUNT_ROLE_ID  -- CONTACT_ID
3593             INTO   l_contact_id
3594               FROM HZ_CUST_ACCOUNT_ROLES ACCT_ROLE,HZ_PARTIES PARTY,HZ_RELATIONSHIPS REL,
3595                    HZ_ORG_CONTACTS ORG_CONT,HZ_PARTIES REL_PARTY
3596               WHERE ACCT_ROLE.PARTY_ID = REL.PARTY_ID
3597               AND ACCT_ROLE.ROLE_TYPE = 'CONTACT'
3598               AND  ORG_CONT.PARTY_RELATIONSHIP_ID = REL.RELATIONSHIP_ID
3599               AND REL.SUBJECT_ID =  PARTY.PARTY_ID
3600               AND REL.PARTY_ID = REL_PARTY.PARTY_ID
3601               AND  REL.SUBJECT_TABLE_NAME = 'HZ_PARTIES'
3602               AND REL.OBJECT_TABLE_NAME =  'HZ_PARTIES'
3603               AND REL.DIRECTIONAL_FLAG = 'F'
3604               AND ACCT_ROLE.CUST_ACCOUNT_ID  = ship_to_customer_id_rec.ship_to_customer_id
3605               AND ACCT_ROLE.CUST_ACCT_SITE_ID =  ship_to_customer_id_rec.ship_to_address_id
3606               AND ACCT_ROLE.STATUS = 'A' ;
3607 
3608         IF pg_debug = 'Y'
3609         THEN
3610             debug ('Ship to Contact Id ' || l_contact_id);
3611             debug ('Ship to Customer Id ' || ship_to_customer_id_rec.ship_to_customer_id);
3612             debug ('Ship to Address Id ' || ship_to_customer_id_rec.ship_to_address_id);
3613         END IF;
3614         UPDATE ar_trx_header_gt
3615             SET  ship_to_contact_id = l_contact_id
3616         WHERE ship_to_customer_id = ship_to_customer_id_rec.ship_to_customer_id
3617         AND   ship_to_address_id = ship_to_customer_id_rec.ship_to_address_id;
3618 
3619         EXCEPTION
3620             WHEN NO_DATA_FOUND THEN
3621                 NULL;
3622             WHEN TOO_MANY_ROWS THEN
3623                 NULL;
3624         END;
3625     END LOOP;
3626     IF pg_debug = 'Y'
3627     THEN
3628         debug ('AR_INVOICE_UTILS.populate_ship_to_contact_id(-)' );
3629     END IF;
3630 
3631     EXCEPTION
3632         WHEN OTHERS THEN
3633             x_errmsg := 'Error in AR_INVOICE_UTILS.populate_ship_to_contact_id '||sqlerrm;
3634             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3635             RETURN;
3636 
3637 END;
3638 
3639 procedure populate_territory (
3640     p_trx_system_param_rec      IN  AR_INVOICE_DEFAULT_PVT.trx_system_parameters_rec_type,
3641     x_errmsg                    OUT NOCOPY  VARCHAR2,
3642     x_return_status             OUT NOCOPY  VARCHAR2)  IS
3643 
3644     l_trx_date                  DATE;
3645     l_salesrep_id               NUMBER;
3646     CURSOR cSalesTer IS
3647         SELECT /*+ LEADING(gt) */ st.territory_id territory_id, gt.primary_salesrep_id
3648         FROM   ra_salesrep_territories st, ar_trx_header_gt gt
3649         WHERE  st.salesrep_id = gt.primary_salesrep_id
3650         AND    'A'            = NVL(st.status(+), 'A')
3651         AND    gt.trx_date BETWEEN NVL(st.start_date_active(+), gt.trx_date  )
3652                           AND NVL(st.end_date_active(+), gt.trx_date );
3653 
3654     CURSOR cBillTo IS
3655         SELECT /*+ LEADING(gt) */ hz.territory_id, gt.bill_to_site_use_id
3656         FROM   HZ_CUST_SITE_USES hz, ar_trx_header_gt gt
3657         WHERE  hz.site_use_id = gt.bill_to_site_use_id;
3658 
3659     CURSOR cShipTo IS
3660         SELECT /*+ LEADING(gt) */ site_uses.territory_id, gt.ship_to_site_use_id,
3661                gt.ship_to_customer_id
3662         FROM   HZ_CUST_SITE_USES site_uses,
3663 	       ar_trx_header_gt gt
3664 	WHERE  site_uses.SITE_USE_CODE = 'SHIP_TO'
3665         AND    site_uses.site_use_id = gt.ship_to_site_use_id
3666         AND    site_uses.primary_flag = 'Y';
3667 
3668 BEGIN
3669     IF pg_debug = 'Y'
3670     THEN
3671         debug ('AR_INVOICE_UTILS.populate_populate_territory  (+)' );
3672     END IF;
3673     x_return_status := FND_API.G_RET_STS_SUCCESS;
3674 
3675      /*----------------------------------------------------------------+
3676      |  Default the territory flexfield from                           |
3677      |  - The Bill To site use                                         |
3678      |  - The Ship To Site Use                                         |
3679      |  - The Primary Salesrep's territory                             |
3680      |  depending on the value of the DEFAULT_TERRITORY system option  |
3681      +-----------------------------------------------------------------*/
3682     IF p_trx_system_param_rec.default_territory = 'BILL'
3683     THEN
3684         FOR cBillToRec IN cBillTo
3685         LOOP
3686             UPDATE ar_trx_header_gt
3687                 SET territory_id = cBillToRec.territory_id
3688              WHERE bill_to_site_use_id = cBillToRec.bill_to_site_use_id;
3689         END LOOP;
3690     ELSIF p_trx_system_param_rec.default_territory = 'SHIP'
3691     THEN
3692         FOR cShipToRec IN cShipTo
3693         LOOP
3694             UPDATE ar_trx_header_gt
3695                 SET territory_id = cShipToRec.territory_id
3696              WHERE ship_to_site_use_id = cShipToRec.ship_to_site_use_id
3697              AND   ship_to_customer_id  = cShipToRec.ship_to_customer_id;
3698         END LOOP;
3699     ELSIF p_trx_system_param_rec.default_territory = 'SALES'
3700     THEN
3701         FOR cSalesTerRec IN cSalesTer
3702         LOOP
3703             UPDATE ar_trx_header_gt
3704                 SET territory_id = cSalesTerRec.territory_id
3705              WHERE primary_salesrep_id = cSalesTerRec.primary_salesrep_id;
3706         END LOOP;
3707     END IF;
3708 
3709     IF pg_debug = 'Y'
3710     THEN
3711         debug ('AR_INVOICE_UTILS.populate_populate_territory  (-)' );
3712     END IF;
3713     EXCEPTION
3714         WHEN OTHERS THEN
3715             x_errmsg := 'Error in AR_INVOICE_UTILS.populate_territory '||sqlerrm;
3716             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3717             RETURN;
3718 END;
3719 
3720 /* 4188835 - call legal_entity function to default it for
3721    and trx rows that do not have one */
3722 procedure populate_legal_entity (
3723     x_errmsg                    OUT NOCOPY  VARCHAR2,
3724     x_return_status             OUT NOCOPY  VARCHAR2)  IS
3725 
3726 BEGIN
3727     IF pg_debug = 'Y'
3728     THEN
3729         debug ('AR_INVOICE_UTILS.populate_legal_entity(+)' );
3730     END IF;
3731 
3732       /* single update to default value */
3733       UPDATE ar_trx_header_gt
3734       SET  legal_entity_id = arp_legal_entity_util.get_default_le(
3735                 sold_to_customer_id,
3736                 bill_to_customer_id,
3737                 cust_trx_type_id,
3738                 batch_source_id)
3739       WHERE legal_entity_id is NULL;
3740 
3741     IF pg_debug = 'Y'
3742     THEN
3743         debug ('AR_INVOICE_UTILS.populate_legal_entity(-)' );
3744     END IF;
3745 
3746     EXCEPTION
3747         WHEN OTHERS THEN
3748             x_errmsg := 'Error in AR_INVOICE_UTILS.populate_legal_entity '||sqlerrm;
3749             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3750             RETURN;
3751 END populate_legal_entity;
3752 
3753 procedure populate_customer_attributes (
3754     x_errmsg                    OUT NOCOPY  VARCHAR2,
3755     x_return_status             OUT NOCOPY  VARCHAR2)  IS
3756 
3757 BEGIN
3758     -- first get the site use id
3759     IF pg_debug = 'Y'
3760     THEN
3761         debug ('AR_INVOICE_UTILS.populate_customer_attributes  (+)' );
3762     END IF;
3763 
3764     IF pg_debug = 'Y'
3765     THEN
3766         debug ('Getting bill_to_site_use_id(+)' );
3767     END IF;
3768 
3769     x_return_status := FND_API.G_RET_STS_SUCCESS;
3770 
3771 	populate_bill_to_site_use_id (
3772 	x_errmsg            =>  x_errmsg,
3773 	x_return_status     =>  x_return_status );
3774 
3775 	IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
3776 	THEN
3777 		RETURN;
3778 	END IF;
3779 
3780     -- Get the Term Id
3781     IF pg_debug = 'Y'
3782     THEN
3783         debug ('Getting bill_to_site_use_id(-)' );
3784         debug ('Getting term_id(+)' );
3785     END IF;
3786 
3787     -- Get term_id for NON-BFB enabled customers
3788     UPDATE ar_trx_header_gt gt
3789     SET    term_id = (SELECT tl.term_id
3790                      FROM   ra_terms              t_su,
3791                             ra_terms              t_cp1,
3792                             ra_terms              t_cp2,
3793                             ra_terms              tl, /*Bug 3984916*/
3794                             hz_customer_profiles  cp1,
3795                             hz_customer_profiles  cp2,
3796                             hz_cust_site_uses     su
3797                      WHERE  gt.bill_to_customer_id  = cp1.cust_account_id(+)
3798                      AND    su.site_use_id    = gt.bill_to_site_use_id
3799                      AND    cp2.cust_account_id   = gt.bill_to_customer_id
3800                      AND    su.site_use_id    = cp1.site_use_id(+)
3801                      AND    cp2.site_use_id   IS NULL
3802                      AND    su.payment_term_id = t_su.term_id(+)
3803                      AND    cp1.standard_terms = t_cp1.term_id(+)
3804                      AND    cp2.standard_terms = t_cp2.term_id(+)
3805                      AND    NVL(
3806                                   t_su.term_id,
3807                                   NVL(
3808                                        t_cp1.term_id,
3809                                        t_cp2.term_id
3810                                      )
3811                                )             = tl.term_id
3812                      AND gt.trx_date BETWEEN t_su.start_date_active(+)
3813                                         AND NVL(t_su.end_date_active(+),
3814                                                 gt.trx_date)
3815                      AND gt.trx_date BETWEEN t_cp1.start_date_active(+)
3816                                         AND NVL(t_cp1.end_date_active(+),
3817                                                 gt.trx_date)
3818                      AND gt.trx_date BETWEEN t_cp2.start_date_active(+)
3819                                         AND NVL(t_cp2.end_date_active(+),
3820                                                 gt.trx_date))
3821     WHERE gt.term_id IS NULL
3822     AND gt.trx_class in ('INV','DM')  -- added for ER 5869149
3823     AND ar_bfb_utils_pvt.get_bill_level(gt.bill_to_customer_id) = 'N';
3824 
3825     -- R12:BFB : get term_id for BFB-enabled customers
3826     UPDATE ar_trx_header_gt gt
3827     SET term_id =  ar_bfb_utils_pvt.get_default_term(
3828                          gt.cust_trx_type_id,
3829                          gt.trx_date,
3830                          gt.org_id,
3831                          gt.bill_to_site_use_id,
3832                          gt.bill_to_customer_id)
3833     WHERE gt.term_id IS NULL
3834     AND gt.trx_class in ('INV','DM')  -- added for ER 5869149 **addtional condition**
3835     AND ar_bfb_utils_pvt.get_bill_level(gt.bill_to_customer_id) in ('A','S');
3836 
3837     IF pg_debug = 'Y'
3838     THEN
3839       debug ('Getting term_id(-)' );
3840       debug ('Getting billing_date(+)');
3841     END IF;
3842 
3843     UPDATE ar_trx_header_gt gt
3844     SET billing_date =  ar_bfb_utils_pvt.get_billing_date
3845                            (ar_bfb_utils_pvt.get_billing_cycle(gt.term_id),
3846                            nvl(gt.trx_date,sysdate))
3847     WHERE gt.term_id IS NOT NULL
3848     AND ar_bfb_utils_pvt.is_payment_term_bfb(gt.term_id) = 'Y'
3849     AND gt.billing_date IS NULL
3850     AND ar_bfb_utils_pvt.get_bill_level(gt.bill_to_customer_id) in ('A','S')
3851     AND nvl(ar_bfb_utils_pvt.get_cycle_type
3852        (ar_bfb_utils_pvt.get_billing_cycle(term_id)),'XXX') = 'RECURRING';
3853 
3854     -- override incorrect billing dates
3855     UPDATE ar_trx_header_gt gt
3856     SET billing_date =  ar_bfb_utils_pvt.get_billing_date
3857                            (ar_bfb_utils_pvt.get_billing_cycle(gt.term_id),
3858                            gt.billing_date)
3859     WHERE gt.term_id IS NOT NULL
3860     AND ar_bfb_utils_pvt.is_payment_term_bfb(gt.term_id) = 'Y'
3861     AND gt.billing_date IS NOT NULL
3862     AND billing_date <> ar_bfb_utils_pvt.get_billing_date(
3863                         ar_bfb_utils_pvt.get_billing_cycle(term_id),
3864                         gt.billing_date)
3865     AND nvl(ar_bfb_utils_pvt.get_cycle_type
3866        (ar_bfb_utils_pvt.get_billing_cycle(term_id)),'XXX') = 'RECURRING';
3867 
3868     IF pg_debug = 'Y'
3869     THEN
3870       debug ('Getting billing_date(-)');
3871       debug ('Getting term_due_date(+)' );
3872     END IF;
3873 
3874     -- Bug # 3099975
3875     -- ORASHID
3876     -- 21-AUG-2003 (END)
3877 
3878     -- get due_date for NON-BFB enabled
3879     UPDATE ar_trx_header_gt gt
3880     SET    term_due_date = trunc(arpt_sql_func_util.get_First_Due_Date(
3881                                gt.term_id, NVL(gt.trx_date,sysdate)))
3882     WHERE  gt.term_id IS NOT NULL
3883     AND    ar_bfb_utils_pvt.is_payment_term_bfb(gt.term_id) = 'N'
3884     AND NOT EXISTS
3885       (SELECT 'X'
3886        FROM ar_trx_errors_gt errgt
3887        WHERE errgt.trx_header_id = gt.trx_header_id);
3888 
3889     -- get due_date for BFB enabled
3890     UPDATE ar_trx_header_gt gt
3891     SET    term_due_date = ar_bfb_utils_pvt.get_due_date(gt.billing_date, gt.term_id)
3892     WHERE  gt.term_id IS NOT NULL
3893     AND    ar_bfb_utils_pvt.is_payment_term_bfb(gt.term_id) = 'Y'
3894     AND    gt.billing_date IS NOT NULL
3895     AND NOT EXISTS
3896       (SELECT 'X'
3897        FROM ar_trx_errors_gt errgt
3898        WHERE errgt.trx_header_id = gt.trx_header_id);
3899 
3900     IF pg_debug = 'Y'
3901     THEN
3902        debug ('Getting term_due_date(-)' );
3903 
3904     END IF;
3905 
3906     IF pg_debug = 'Y'
3907     THEN
3908        debug ('Populate Salesrep Id(+)' );
3909     END IF;
3910       populate_salesreps (
3911             x_errmsg            =>  x_errmsg,
3912             x_return_status     =>  x_return_status );
3913       IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
3914       THEN
3915             RETURN;
3916       END IF;
3917       IF pg_debug = 'Y'
3918       THEN
3919         debug ('Populate Salesrep Id(-)' );
3920         debug ('Populate Bill to address Id(+)' );
3921       END IF;
3922 
3923       populate_bill_to_customer_id (
3924             x_errmsg            =>  x_errmsg,
3925             x_return_status     =>  x_return_status );
3926       IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3927         RETURN;
3928       ELSE
3929         INSERT INTO ar_trx_errors_gt (
3930           trx_header_id,
3931           error_message,
3932           invalid_value)
3933         SELECT trx_header_id,
3934                arp_standard.fnd_message('AR_INAPI_INV_BILL_TO_CUST_ID'),
3935                bill_to_address_id
3936         FROM   ar_trx_header_gt gt
3937         WHERE  gt.bill_to_customer_id IS NULL;
3938       END IF;
3939       populate_ship_to_site_use_id(
3940         x_errmsg                    => x_errmsg,
3941         x_return_status             => x_return_status);
3942 
3943       IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3944         RETURN;
3945       END IF;
3946       populate_paying_site_use_id (
3947              x_errmsg            =>  x_errmsg,
3948             x_return_status     =>  x_return_status );
3949 
3950       IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3951         RETURN;
3952       END IF;
3953       populate_bill_to_address_id (
3954             x_errmsg            =>  x_errmsg,
3955             x_return_status     =>  x_return_status );
3956 
3957       IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3958         RETURN;
3959       ELSE
3960         INSERT INTO ar_trx_errors_gt (
3961           trx_header_id,
3962           error_message,
3963           invalid_value)
3964         SELECT trx_header_id,
3965                arp_standard.fnd_message('AR_INAPI_INVALID_BILL_ADDR_ID'),
3966                bill_to_address_id
3967         FROM   ar_trx_header_gt gt
3968         WHERE  gt.bill_to_address_id IS NULL;
3969       END IF;
3970       IF pg_debug = 'Y'
3971       THEN
3972         debug ('Populate Bill to address Id(-)' );
3973         debug ('Populate remit to address Id(+)' );
3974       END IF;
3975 
3976       populate_remit_to_address_id (
3977             x_errmsg            =>  x_errmsg,
3978             x_return_status     =>  x_return_status );
3979 
3980       IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
3981       THEN
3982             RETURN;
3983       END IF;
3984 
3985       IF pg_debug = 'Y'
3986       THEN
3987         debug ('Populate remit to address Id(-)' );
3988         debug ('Populate bill to contact id(+)' );
3989       END IF;
3990 
3991       populate_bill_to_contact_id (
3992             x_errmsg            =>  x_errmsg,
3993             x_return_status     =>  x_return_status );
3994 
3995       IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
3996       THEN
3997             RETURN;
3998       END IF;
3999 
4000       IF pg_debug = 'Y'
4001       THEN
4002         debug ('Populate bill to contact id(-)' );
4003       END IF;
4004 
4005       IF pg_debug = 'Y'
4006       THEN
4007         debug ('AR_INVOICE_UTILS.populate_customer_attributes  (-)' );
4008       END IF;
4009 
4010       EXCEPTION
4011         WHEN OTHERS THEN
4012             x_errmsg := 'Error in AR_INVOICE_UTILS.populate_customer_attributes '||sqlerrm;
4013             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4014             RETURN;
4015 
4016 END populate_customer_attributes;
4017 
4018 
4019 PROCEDURE populate_ref_hdr_attributes (
4020         x_errmsg                    OUT NOCOPY  VARCHAR2,
4021         x_return_status             OUT NOCOPY  VARCHAR2) IS
4022 
4023 BEGIN
4024     IF pg_debug = 'Y'
4025     THEN
4026         debug ('AR_INVOICE_UTILS.populate_ref_hdr_attributes (+)' );
4027     END IF;
4028 
4029     x_return_status := FND_API.G_RET_STS_SUCCESS;
4030 
4031     IF pg_debug = 'Y'
4032     THEN
4033         debug ('AR_INVOICE_UTILS.Get_batch_source_details (+)' );
4034     END IF;
4035     Get_batch_source_details (
4036             x_errmsg            =>  x_errmsg,
4037             x_return_status     =>  x_return_status );
4038 
4039       IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
4040       THEN
4041             RETURN;
4042       END IF;
4043     -- validate the trx type.
4044     IF pg_debug = 'Y'
4045     THEN
4046         debug ('AR_INVOICE_UTILS.Get_batch_source_details (-)' );
4047         debug ('AR_INVOICE_UTILS.validate_transaction_type (+)' );
4048     END IF;
4049     validate_transaction_type(
4050         x_errmsg                    => x_errmsg,
4051         x_return_status             => x_return_status);
4052     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
4053     THEN
4054         RETURN;
4055     END IF;
4056     IF pg_debug = 'Y'
4057     THEN
4058         debug ('AR_INVOICE_UTILS.validate_transaction_type (-)' );
4059     END IF;
4060     IF pg_debug = 'Y'
4061     THEN
4062         debug ('AR_INVOICE_UTILS.Get_trx_type_details (+)' );
4063     END IF;
4064     Get_trx_type_details (
4065             x_errmsg            =>  x_errmsg,
4066             x_return_status     =>  x_return_status );
4067 
4068       IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
4069       THEN
4070             RETURN;
4071       END IF;
4072       IF pg_debug = 'Y'
4073       THEN
4074         debug ('AR_INVOICE_UTILS.Get_trx_type_details (-)' );
4075         debug ('AR_INVOICE_DEFAULT_PVT.Default_gl_date (+)' );
4076       END IF;
4077     AR_INVOICE_DEFAULT_PVT.Default_gl_date;
4078 
4079       IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
4080       THEN
4081             RETURN;
4082       END IF;
4083       IF pg_debug = 'Y'
4084       THEN
4085         debug ('AR_INVOICE_DEFAULT_PVT.Default_gl_date (-)' );
4086         debug ('AR_INVOICE_UTILS.populate_customer_attributes (+)' );
4087       END IF;
4088     populate_customer_attributes (
4089             x_errmsg            =>  x_errmsg,
4090             x_return_status     =>  x_return_status );
4091 
4092     validate_bfb;
4093 
4094       IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
4095       THEN
4096             RETURN;
4097       END IF;
4098      IF pg_debug = 'Y'
4099       THEN
4100         debug ('AR_INVOICE_UTILS.populate_customer_attributes (-)' );
4101       END IF;
4102     IF pg_debug = 'Y'
4103     THEN
4104         debug ('AR_INVOICE_UTILS.populate_ref_hdr_attributes (-)' );
4105     END IF;
4106 
4107     EXCEPTION
4108         WHEN OTHERS THEN
4109             x_errmsg := 'Error in AR_INVOICE_UTILS.populate_ref_hdr_attributes '||sqlerrm;
4110             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4111             RETURN;
4112 
4113 END populate_ref_hdr_attributes;
4114 
4115 
4116 PROCEDURE validate_UOM_CODE (
4117         x_errmsg                    OUT NOCOPY  VARCHAR2,
4118         x_return_status             OUT NOCOPY  VARCHAR2) IS
4119 
4120 	/*Bug 3752043*/
4121 	l_so_org_id  NUMBER;
4122 BEGIN
4123     IF pg_debug = 'Y'
4124     THEN
4125         debug ('AR_INVOICE_UTILS.validate_UOM_CODE (+)' );
4126     END IF;
4127 
4128     x_return_status := FND_API.G_RET_STS_SUCCESS;
4129 
4130     l_so_org_id := to_number(oe_profile.value('SO_ORGANIZATION_ID'));  /*Bug 3752043*/
4131 
4132     INSERT INTO ar_trx_errors_gt
4133         ( trx_header_id,
4134           trx_line_id,
4135           error_message,
4136           invalid_value )
4137           SELECT trx_header_id,
4138                  trx_line_id,
4139                  arp_standard.fnd_message('AR_INAPI_UOM_NOT_REQ'),
4140                  gt.uom_code
4141           FROM ar_trx_lines_gt gt
4142           WHERE  gt.uom_code IS NOT NULL
4143           AND    gt.line_type <> 'LINE';
4144 
4145       -- UOM Code not allowed for CM
4146 
4147       INSERT INTO ar_trx_errors_gt
4148         ( trx_header_id,
4149           trx_line_id,
4150           error_message,
4151           invalid_value )
4152           SELECT gt.trx_header_id,
4153                  gt.trx_line_id,
4154                  arp_standard.fnd_message('AR_INAPI_CM_UOM_NOT_ALLOWED'),
4155                  gt.uom_code
4156           FROM ar_trx_lines_gt gt, ar_trx_header_gt gt2
4157           WHERE  gt.uom_code IS NOT NULL
4158           AND    gt.line_type = 'LINE'
4159           AND   gt2.trx_header_id = gt.trx_header_id
4160           AND   gt2.trx_class = 'CM'; -- added for ER 5869149
4161 
4162     INSERT INTO ar_trx_errors_gt
4163         ( trx_header_id,
4164           trx_line_id,
4165           error_message,
4166           invalid_value )
4167           SELECT gt.trx_header_id,
4168                  gt.trx_line_id,
4169                  arp_standard.fnd_message('AR_INAPI_INVALID_UOM'),
4170                  gt.uom_code
4171           FROM ar_trx_lines_gt gt , ar_trx_header_gt gt2
4172           WHERE gt.uom_code IS NOT NULL
4173           AND   gt.line_type = 'LINE'
4174 	  AND   gt2.trx_header_id = gt.trx_header_id
4175 	  AND   gt2.trx_class <> 'CM' -- added for ER 5869149
4176           AND   NOT EXISTS (
4177                 SELECT 'X'
4178                 FROM mtl_item_uoms_view uom
4179                 where organization_id = nvl(gt.warehouse_id,l_so_org_id) /*Bug 3752043*/
4180                 and inventory_item_id = gt.inventory_item_id
4181                 and  uom.uom_code = gt.uom_code
4182                 union
4183                 select 'X'
4184                 from mtl_units_of_measure uom
4185                 where sysdate <= nvl(trunc(uom.disable_date), sysdate)
4186                 and gt.inventory_item_id is null
4187                 and gt.uom_code = uom.uom_code);
4188 
4189     IF pg_debug = 'Y'
4190     THEN
4191         debug ('AR_INVOICE_UTILS.validate_UOM_CODE (-)' );
4192     END IF;
4193     EXCEPTION
4194         WHEN OTHERS THEN
4195             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_UOM_CODE '||sqlerrm;
4196             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4197             RETURN;
4198 
4199 END validate_uom_code;
4200 
4201 
4202 PROCEDURE validate_tax_code (
4203         x_errmsg                    OUT NOCOPY  VARCHAR2,
4204         x_return_status             OUT NOCOPY  VARCHAR2) IS
4205 
4206 BEGIN
4207     IF pg_debug = 'Y'
4208     THEN
4209         debug ('AR_INVOICE_UTILS.validate_tax_code (+)' );
4210     END IF;
4211     x_return_status := FND_API.G_RET_STS_SUCCESS;
4212 
4213     /* With etax, they are now responsible for insuring that
4214        - tax is valid
4215        - not a location tax
4216        - rate is overridden
4217        - inclusive flag mismatch
4218 
4219        Because of this, we no longer have to perform these
4220        validations here. */
4221 
4222     IF pg_debug = 'Y'
4223     THEN
4224         debug ('AR_INVOICE_UTILS.validate_tax_code (-)' );
4225     END IF;
4226 
4227     EXCEPTION
4228         WHEN OTHERS THEN
4229             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_tax_code '||sqlerrm;
4230             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4231             RETURN;
4232 
4233 END validate_tax_code;
4234 
4235 PROCEDURE validate_tax_freight_lines (
4236     x_errmsg                    OUT NOCOPY  VARCHAR2,
4237     x_return_status             OUT NOCOPY  VARCHAR2) IS
4238 BEGIN
4239 
4240     IF pg_debug = 'Y'
4241     THEN
4242         debug ('AR_INVOICE_UTILS.validate_tax_lines (+)' );
4243     END IF;
4244 
4245     x_return_status := FND_API.G_RET_STS_SUCCESS;
4246 
4247      INSERT INTO ar_trx_errors_gt
4248         ( trx_header_id,
4249           trx_line_id,
4250           error_message,
4251           invalid_value )
4252           SELECT trx_header_id,
4253                  trx_line_id,
4254                  arp_standard.fnd_message('AR_INAPI_INVALID_TAX_LINE'),
4255                  gt.vat_tax_id
4256           FROM ar_trx_lines_gt gt
4257           WHERE extended_amount IS NULL
4258           AND   line_type in ('TAX', 'FREIGHT');
4259 
4260      INSERT INTO ar_trx_errors_gt
4261         ( trx_header_id,
4262           trx_line_id,
4263           error_message,
4264           invalid_value )
4265           SELECT trx_header_id,
4266                  trx_line_id,
4267                  arp_standard.fnd_message('AR_INAPI_QTY_NOT_ALLOWED'),
4268                  gt.vat_tax_id
4269           FROM ar_trx_lines_gt gt
4270           WHERE  line_type in ('TAX', 'FREIGHT')
4271           AND    (quantity_invoiced IS NOT NULL
4272                OR unit_selling_price IS NOT NULL );
4273 
4274      /* In etax, a manual tax line must have regime, tax, juris,
4275         rate, and status -- no exceptions */
4276      INSERT INTO ar_trx_errors_gt
4277         ( trx_header_id,
4278           trx_line_id,
4279           error_message,
4280           invalid_value )
4281           SELECT trx_header_id,
4282                  trx_line_id,
4283                  arp_standard.fnd_message('AR_RAXTRX-1706B'),
4284                  gt.tax_regime_code || ', ' ||
4285                  gt.tax || ', ' ||
4286                  gt.tax_status_code || ', ' ||
4287                  gt.tax_rate_code || ', ' ||
4288                  gt.tax_jurisdiction_code
4289           FROM ar_trx_lines_gt gt
4290           WHERE  line_type = 'TAX'
4291           AND    gt.tax_regime_code ||
4292                  gt.tax ||
4293                  gt.tax_status_code ||
4294                  gt.tax_rate_code ||
4295                  gt.tax_jurisdiction_code IS NULL;
4296 
4297     IF pg_debug = 'Y'
4298     THEN
4299         debug ('AR_INVOICE_UTILS.validate_tax_lines (-)' );
4300     END IF;
4301 
4302      EXCEPTION
4303         WHEN OTHERS THEN
4304             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_tax_freight_lines '||sqlerrm;
4305             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4306             RETURN;
4307 END;
4308 
4309 PROCEDURE validate_tax_exemption (
4310     x_errmsg                    OUT NOCOPY  VARCHAR2,
4311     x_return_status             OUT NOCOPY  VARCHAR2) IS
4312     l_tax_use_exempt_flag       zx_product_options.tax_use_customer_exempt_flag%type;
4313 BEGIN
4314     -- tax handling
4315     -- if tax_exempt flag is 'E' then tax exempt reason is mandatory.
4316     -- tax reason is alterable only if profile ar_allow_trx_line_exemptions
4317     -- is set to Yes. If tax_exempt falg is 'E then only Reason and
4318     -- certificate is allowed.
4319 
4320     -- tax handling
4321     IF pg_debug = 'Y'
4322     THEN
4323         debug ('AR_INVOICE_UTILS.validate_tax_exemption (+)' );
4324     END IF;
4325 
4326     x_return_status := FND_API.G_RET_STS_SUCCESS;
4327     BEGIN
4328         /* 4257557 - changed to zx_product_options */
4329         select nvl(tax_use_customer_exempt_flag,'N')
4330         into  l_tax_use_exempt_flag
4331         from  zx_product_options
4332         where application_id = 222;
4333 
4334     EXCEPTION             /*7448838-Adding Exception Block*/
4335       WHEN NO_DATA_FOUND THEN
4336         IF pg_debug = 'Y'
4337         THEN
4338             debug ('  No rows in zx_product_options' );
4339             debug ('AR_INVOICE_UTILS.validate_tax_exemption(-)' );
4340         END IF;
4341         RETURN;
4342     END;
4343 
4344     IF pg_debug = 'Y'
4345     THEN
4346         debug ('Tax use customer exempt Flag ' || l_tax_use_exempt_flag);
4347     END IF;
4348     INSERT INTO ar_trx_errors_gt
4349         ( trx_header_id,
4350           trx_line_id,
4351           error_message,
4352           invalid_value )
4353     SELECT trx_header_id,
4354            trx_line_id,
4355            arp_standard.fnd_message('AR_INAPI_INV_TAX_EXEMPT_FLAG'),
4356            tax_exempt_flag
4357     FROM ar_trx_lines_gt
4358     WHERE tax_exempt_flag IS NOT NULL
4359     AND   NOT EXISTS
4360         ( select 'X'
4361           from ar_lookups AL1
4362           where AL1.lookup_type = 'TAX_CONTROL_FLAG'
4363              and (AL1.lookup_code in ('R','S')
4364              or (AL1.lookup_code = 'E'
4365              and  'Y' = l_tax_use_exempt_flag))
4366 /*           4257557 - This logic is temporarily disabled
4367              or (AL1.lookup_code = 'O'
4368              and exists (select 1 from ar_system_parameters where tax_database_view_set in ('_V','_A') ))) */
4369              and tax_exempt_flag = lookup_code);
4370 
4371    -- now validate the reson code in case the exempt flag = 'E'
4372    -- if exempt flag is E then reason code must be supplied.
4373    INSERT INTO ar_trx_errors_gt
4374         ( trx_header_id,
4375           trx_line_id,
4376           error_message)
4377     SELECT trx_header_id,
4378            trx_line_id,
4379            arp_standard.fnd_message('AR_INAPI_REASON_NOT_REQ')
4380     FROM ar_trx_lines_gt
4381     WHERE tax_exempt_flag = 'E'
4382     AND   TAX_EXEMPT_REASON_CODE IS NULL;
4383 
4384     -- Also in case of 'R' and 'S' reason code and tax_exempt_number is not required.
4385     UPDATE  ar_trx_lines_gt
4386         SET TAX_EXEMPT_REASON_CODE = null,
4387             tax_exempt_number = null
4388     WHERE tax_exempt_flag in ( 'R', 'S');
4389 
4390     -- Validate the reason code
4391     INSERT INTO ar_trx_errors_gt
4392         ( trx_header_id,
4393           trx_line_id,
4394           error_message,
4395           invalid_value)
4396     SELECT trx_header_id,
4397            trx_line_id,
4398            arp_standard.fnd_message('AR_INAPI_INVALID_TAX_EXEMPT_CODE'),
4399            TAX_EXEMPT_REASON_CODE
4400     FROM ar_trx_lines_gt
4401     WHERE  TAX_EXEMPT_REASON_CODE IS NOT null
4402     AND    NOT EXISTS (
4403             SELECT 'X'
4404             FROM   ar_lookups
4405             WHERE  lookup_type = 'TAX_REASON'
4406             AND    enabled_flag = 'Y'
4407             AND    trx_date between start_date_active and nvl(end_date_active,trx_date)
4408             AND    lookup_code = TAX_EXEMPT_REASON_CODE );
4409 
4410     IF pg_debug = 'Y'
4411     THEN
4412         debug ('AR_INVOICE_UTILS.validate_tax_exemption (-)' );
4413     END IF;
4414    EXCEPTION
4415      WHEN OTHERS THEN
4416        x_errmsg := 'Error in AR_INVOICE_UTILS.validate_tax_exemption '
4417          ||sqlerrm;
4418        x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4419        RETURN;
4420 
4421 END validate_tax_exemption;
4422 
4423 
4424 PROCEDURE validate_more_tab (
4425      x_errmsg                    OUT NOCOPY  VARCHAR2,
4426      x_return_status             OUT NOCOPY  VARCHAR2) IS
4427     l_trx_date Date := sysdate; --:= g_trx_date;
4428 BEGIN
4429     IF pg_debug = 'Y'
4430     THEN
4431         debug ('AR_INVOICE_UTILS.validate_more_tab (+)' );
4432     END IF;
4433 
4434     x_return_status := FND_API.G_RET_STS_SUCCESS;
4435 
4436     -- reason code
4437     INSERT INTO ar_trx_errors_gt
4438         ( trx_header_id,
4439           trx_line_id,
4440           error_message,
4441           invalid_value )
4442           SELECT trx_header_id,
4443                  trx_line_id,
4444                  arp_standard.fnd_message('AR_INAPI_REASON_NOT_REQ'),
4445                  gt.reason_code
4446           FROM ar_trx_lines_gt gt
4447           WHERE  gt.reason_code IS NOT NULL
4448           AND    gt.line_type = 'TAX';
4449 
4450 
4451     INSERT INTO ar_trx_errors_gt
4452         ( trx_header_id,
4453           trx_line_id,
4454           error_message,
4455           invalid_value )
4456           SELECT trx_header_id,
4457                  trx_line_id,
4458                  arp_standard.fnd_message('AR_INAPI_INVALID_REASON'),
4459                  gt.reason_code
4460           FROM ar_trx_lines_gt gt
4461           WHERE gt.reason_code IS NOT NULL
4462           AND   NOT EXISTS (
4463                 select 'X'
4464                 from ar_lookups
4465                 where lookup_type =  'INVOICING_REASON'
4466                 and enabled_flag = 'Y'
4467                 and lookup_code = gt.reason_code
4468                 and sysdate -- g_trx_date
4469                 between start_date_active and nvl(end_date_active, sysdate));
4470     IF pg_debug = 'Y'
4471     THEN
4472         debug ('AR_INVOICE_UTILS.validate_more_tab (-)' );
4473     END IF;
4474 
4475     EXCEPTION
4476         WHEN OTHERS THEN
4477             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_more_tab '||sqlerrm;
4478             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4479             RETURN;
4480 
4481 END validate_more_tab;
4482 
4483 
4484 PROCEDURE validate_line_description (
4485     x_errmsg                    OUT NOCOPY  VARCHAR2,
4486     x_return_status             OUT NOCOPY  VARCHAR2) IS
4487 
4488 BEGIN
4489 
4490     IF pg_debug = 'Y' THEN
4491       debug ('AR_INVOICE_UTILS.validate_line_description (+)' );
4492     END IF;
4493 
4494     x_return_status := FND_API.G_RET_STS_SUCCESS;
4495 
4496     INSERT INTO ar_trx_errors_gt
4497         ( trx_header_id,
4498           error_message)
4499           SELECT trx_header_id,
4500                  arp_standard.fnd_message('AR_INAPI_INV_DESC_NULL')
4501           FROM ar_trx_lines_gt gt
4502           WHERE gt.description IS NULL
4503           AND   gt.inventory_item_id IS NULL
4504           AND   gt.memo_line_id IS NULL
4505           AND   gt.line_type = 'LINE';
4506 
4507     INSERT INTO ar_trx_errors_gt
4508         ( trx_header_id,
4509           trx_line_id,
4510           error_message)
4511           SELECT trx_header_id,
4512                  trx_line_id,
4513                  arp_standard.fnd_message('AR_DAPI_MEMO_NAME_INVALID')
4514           FROM ar_trx_lines_gt gt
4515           WHERE gt.memo_line_id IS NOT NULL
4516           AND   gt.line_type = 'LINE'
4517           AND   NOT EXISTS (
4518                 SELECT 'X'
4519                 FROM ar_memo_lines m
4520                 WHERE m.memo_line_id = gt.memo_line_id
4521                 AND   m.line_type = 'LINE'
4522                 AND   sysdate between start_date and nvl(end_date,sysdate) );
4523 
4524     /*Bug 3844408*/
4525     /*This update should be fired only when the description isn't populated
4526       initially*/
4527 
4528    /* 4536358 - changed ar_memo_lines_all_tl to
4529       ar_memo_lines_tl.  Removed rownum = 1.  This was
4530       just masking issues that would arise due to
4531       cartesian join.  */
4532 
4533     UPDATE ar_trx_lines_gt gt
4534     SET description = ( SELECT description
4535                         FROM   ar_memo_lines_tl
4536                         WHERE  memo_line_id = gt.memo_line_id
4537                         AND    language = USERENV('LANG')
4538                         AND    rownum = 1)
4539     WHERE gt.memo_line_id IS NOT NULL
4540     AND   description is NULL;
4541 
4542     IF pg_debug = 'Y'
4543     THEN
4544         debug ('AR_INVOICE_UTILS.validate_line_description (-)' );
4545     END IF;
4546 
4547     EXCEPTION
4548       WHEN OTHERS THEN
4549         x_errmsg := 'Error in AR_INVOICE_UTILS.validate_line_description '||
4550           sqlerrm;
4551         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4552         RETURN;
4553 
4554 END validate_line_description;
4555 
4556 
4557 PROCEDURE validate_quantity_invoiced (
4558     x_errmsg                    OUT NOCOPY  VARCHAR2,
4559     x_return_status             OUT NOCOPY  VARCHAR2) IS
4560 BEGIN
4561     IF pg_debug = 'Y'
4562     THEN
4563         debug ('AR_INVOICE_UTILS.validate_quantity_invoiced (+)' );
4564     END IF;
4565 
4566     x_return_status := FND_API.G_RET_STS_SUCCESS;
4567 
4568     INSERT INTO ar_trx_errors_gt
4569         ( trx_header_id,
4570           error_message)
4571           SELECT gt.trx_header_id,
4572                  arp_standard.fnd_message('AR_INAPI_QTY_NOT_NULL')
4573           FROM ar_trx_lines_gt gt ,
4574 	       ar_trx_header_gt gt2
4575           WHERE gt.quantity_invoiced IS NULL
4576 	  AND   gt.trx_header_id = gt2.trx_header_id
4577           AND   gt2.trx_class <> 'CM' -- added for ER 5869149
4578           AND   gt.line_type = 'LINE';
4579     IF pg_debug = 'Y'
4580     THEN
4581         debug ('AR_INVOICE_UTILS.validate_quantity_invoiced (-)' );
4582     END IF;
4583 
4584     EXCEPTION
4585         WHEN OTHERS THEN
4586             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_quantity_invoiced '||sqlerrm;
4587             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4588             RETURN;
4589 
4590 END;
4591 
4592 PROCEDURE validate_unit_selling_price (
4593     x_errmsg                    OUT NOCOPY  VARCHAR2,
4594     x_return_status             OUT NOCOPY  VARCHAR2) IS
4595 BEGIN
4596     IF pg_debug = 'Y'
4597     THEN
4598         debug ('AR_INVOICE_UTILS.validate_unit_selling_price (+)' );
4599     END IF;
4600 
4601     x_return_status := FND_API.G_RET_STS_SUCCESS;
4602 
4603     INSERT INTO ar_trx_errors_gt
4604         ( trx_header_id,
4605           error_message)
4606           SELECT gt.trx_header_id,
4607                  arp_standard.fnd_message('AR_INAPI_UNIT_PRICE_NOT_NULL')
4608           FROM ar_trx_lines_gt gt ,
4609 	       ar_trx_header_gt gt2
4610           WHERE gt.unit_selling_price IS NULL
4611 	  AND   gt.trx_header_id = gt2.trx_header_id
4612           AND   gt2.trx_class <> 'CM' -- Added for ER 5869149
4613           AND   gt.line_type = 'LINE';
4614     IF pg_debug = 'Y'
4615     THEN
4616         debug ('AR_INVOICE_UTILS.validate_unit_selling_price (-)' );
4617     END IF;
4618     EXCEPTION
4619         WHEN OTHERS THEN
4620             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_unit_selling_price '||sqlerrm;
4621             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4622             RETURN;
4623 END;
4624 
4625 PROCEDURE validate_line_type (
4626     x_errmsg                    OUT NOCOPY  VARCHAR2,
4627     x_return_status             OUT NOCOPY  VARCHAR2) IS
4628 BEGIN
4629     IF pg_debug = 'Y'
4630     THEN
4631         debug ('AR_INVOICE_UTILS.validate_line_type (+)' );
4632     END IF;
4633     x_return_status := FND_API.G_RET_STS_SUCCESS;
4634 
4635     INSERT INTO ar_trx_errors_gt
4636         ( trx_header_id,
4637           error_message,
4638           invalid_value )
4639           SELECT trx_header_id,
4640                  arp_standard.fnd_message('AR_INAPI_INAVLID_LINE_TYPE'),
4641                  gt.line_type
4642           FROM ar_trx_lines_gt gt
4643           WHERE gt.line_type not in ('LINE', 'TAX', 'FREIGHT');
4644     IF pg_debug = 'Y'
4645     THEN
4646         debug ('AR_INVOICE_UTILS.validate_line_type (-)' );
4647     END IF;
4648 
4649     EXCEPTION
4650         WHEN OTHERS THEN
4651             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_line_type '||sqlerrm;
4652             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4653             RETURN;
4654 END;
4655 
4656 PROCEDURE check_dup_line_number (
4657     x_errmsg                    OUT NOCOPY  VARCHAR2,
4658     x_return_status             OUT NOCOPY  VARCHAR2)  IS
4659 
4660 BEGIN
4661     IF pg_debug = 'Y'
4662     THEN
4663         debug ('AR_INVOICE_UTILS.check_dup_line_number (+)' );
4664     END IF;
4665     x_return_status := FND_API.G_RET_STS_SUCCESS;
4666     -- first check if line number is null or not
4667     INSERT INTO ar_trx_errors_gt
4668         ( trx_header_id,
4669           error_message)
4670           SELECT trx_header_id,
4671                  arp_standard.fnd_message('AR_INAPI_LINE_NUM_NOT_NULL')
4672           FROM ar_trx_lines_gt gt
4673           WHERE gt.line_number IS NULL;
4674 
4675     -- check for duplicate line number
4676    /* INSERT INTO ar_trx_errors_gt
4677         ( trx_header_id,
4678           error_message
4679           SELECT trx_header_id,
4680                  'Duplicate Line Number'
4681           FROM ar_trx_lines_gt gt
4682           WHERE gt.line_number IS NOT NULL
4683           AND   ; */
4684     IF pg_debug = 'Y'
4685     THEN
4686         debug ('AR_INVOICE_UTILS.check_dup_line_number (-)' );
4687     END IF;
4688 
4689     EXCEPTION
4690         WHEN OTHERS THEN
4691             x_errmsg := 'Error in AR_INVOICE_UTILS.check_dup_line_number '||sqlerrm;
4692             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4693             RETURN;
4694 END;
4695 
4696 PROCEDURE validate_line_integrity (
4697     x_errmsg                    OUT NOCOPY  VARCHAR2,
4698     x_return_status             OUT NOCOPY  VARCHAR2) IS
4699 
4700 -- first get all the freight lines
4701 l_header_freight_count          NUMBER;
4702 l_line_freight_count            NUMBER;
4703 l_allow_freight_flag            ra_cust_trx_types.allow_freight_flag%type;
4704 l_cust_trx_type_id              ra_cust_trx_types.cust_trx_type_id%type;
4705 CURSOR c_freight IS
4706     SELECT trx_header_id, count(*) number_of_freight_lines
4707     FROM   ar_trx_lines_gt
4708     WHERE  line_type = 'FREIGHT'
4709     group by trx_header_id;
4710 
4711 BEGIN
4712     IF pg_debug = 'Y'
4713     THEN
4714         debug ('AR_INVOICE_UTILS.validate_line_integrity (+)' );
4715     END IF;
4716 
4717     x_return_status := FND_API.G_RET_STS_SUCCESS;
4718 
4719     -- first check whether all  lines have line and header id
4720     INSERT INTO ar_trx_errors_gt
4721         ( trx_header_id,
4722           trx_line_id,
4723           error_message)
4724           SELECT trx_header_id,
4725                  trx_line_id,
4726                  arp_standard.fnd_message('AR_INAPI_HDR_ID_NOT_NULL')
4727           FROM ar_trx_lines_gt gt
4728           WHERE gt.trx_header_id IS NULL
4729           OR    gt.trx_line_id IS NULL;
4730 
4731     -- Now check if the line is of Tax type then
4732     -- link_to link_to_trx_line_id not null
4733     INSERT INTO ar_trx_errors_gt
4734         ( trx_header_id,
4735           trx_line_id,
4736           error_message)
4737           SELECT trx_header_id,
4738                  trx_line_id,
4739                  arp_standard.fnd_message('AR_INAPI_LINK_LINE_ID_NOT_NULL')
4740           FROM ar_trx_lines_gt gt
4741           WHERE gt.line_type = 'TAX'
4742           AND   gt.link_to_trx_line_id IS NULL;
4743 
4744     -- Vlaidate if line type is freight then whether
4745     -- it is associated with header or lines. It is allowed
4746     -- either with header or line level. Not allowed with both levels.
4747 
4748     /* 5921925 - This could be reduced to a single sql verifiying
4749        that all freight lines for a given trx are either linked or not linked */
4750 
4751     FOR  c_freight_rec IN c_freight
4752     LOOP
4753         l_header_freight_count := 0;
4754         l_line_freight_count := 0;
4755         -- first check whether freight is allowed for this transaction type
4756         -- or not
4757 
4758         BEGIN
4759             SELECT nvl(gt.allow_freight_flag, 'N'), gt.cust_trx_type_id
4760             INTO   l_allow_freight_flag, l_cust_trx_type_id
4761             FROM   ar_trx_header_gt gt
4762             WHERE  gt.trx_header_id = c_freight_rec.trx_header_id;
4763 
4764             IF l_allow_freight_flag = 'N'
4765             THEN
4766                 INSERT INTO ar_trx_errors_gt
4767                     ( trx_header_id,
4768                       error_message)
4769                 VALUES
4770                     ( c_freight_rec.trx_header_id,
4771                       arp_standard.fnd_message('AR_TAPI_FREIGHT_NOT_ALLOWED'));
4772             END IF;
4773         END;
4774 
4775         IF  ( c_freight_rec.number_of_freight_lines > 1 )
4776         THEN
4777             -- now check what type of freight record it has
4778             BEGIN
4779                 SELECT  count(*)
4780                 INTO    l_header_freight_count
4781                 FROM    ar_trx_lines_gt
4782                 WHERE   trx_header_id = c_freight_rec.trx_header_id
4783                 AND     link_to_trx_line_id IS NULL;
4784             EXCEPTION
4785                 WHEN OTHERS THEN
4786                     NULL; -- need to put error message
4787             END;
4788             BEGIN
4789                 SELECT  count(*)
4790                 INTO    l_line_freight_count
4791                 FROM    ar_trx_lines_gt
4792                 WHERE   trx_header_id = c_freight_rec.trx_header_id
4793                 AND     link_to_trx_line_id IS NOT NULL;
4794             EXCEPTION
4795                 WHEN OTHERS THEN
4796                     NULL; -- need to put error message
4797             END;
4798             IF ( nvl(l_header_freight_count, 0) >= 1 and
4799                  nvl(l_line_freight_count, 0) >= 1 )
4800             THEN
4801                 INSERT INTO ar_trx_errors_gt
4802                     ( trx_header_id,
4803                       error_message)
4804                         SELECT trx_header_id,
4805                         arp_standard.fnd_message('AR_TAPI_TOO_MANY_FREIGHT_LINE')
4806                         FROM ar_trx_header_gt gt
4807                         WHERE gt.trx_header_id = c_freight_rec.trx_header_id;
4808             END IF;
4809         END IF;
4810     END LOOP;
4811     IF pg_debug = 'Y'
4812     THEN
4813         debug ('AR_INVOICE_UTILS.validate_line_integrity (-)' );
4814     END IF;
4815 
4816     EXCEPTION
4817         WHEN OTHERS THEN
4818             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_line_integrity '||sqlerrm;
4819             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4820             RETURN;
4821 END;
4822 
4823 PROCEDURE validate_freight (
4824      x_errmsg                    OUT NOCOPY  VARCHAR2,
4825      x_return_status             OUT NOCOPY  VARCHAR2)  IS
4826 BEGIN
4827     -- validate ship via
4828     IF pg_debug = 'Y'
4829     THEN
4830         debug ('AR_INVOICE_UTILS.validate_freight (+)' );
4831     END IF;
4832     x_return_status := FND_API.G_RET_STS_SUCCESS;
4833 
4834     INSERT INTO ar_trx_errors_gt
4835          ( trx_header_id,
4836            trx_line_id,
4837            error_message)
4838     SELECT trx_header_id,
4839            trx_line_id,
4840            arp_standard.fnd_message('AR_INAPI_INVALID_SHIP_VIA')
4841     FROM    ar_trx_lines_gt gt
4842     WHERE  gt.line_type = 'FREIGHT'
4843     AND    gt.ship_via IS NOT NULL
4844     AND    NOT EXISTS (
4845                 SELECT 'X' FROM
4846                 org_freight fr
4847                 WHERE fr.organization_id =  gt.org_id
4848                 and   gt.trx_date <= nvl(trunc(disable_date), gt.trx_date)
4849                 and   freight_code = gt.ship_via);
4850 
4851     -- validate FOB
4852     INSERT INTO ar_trx_errors_gt
4853          ( trx_header_id,
4854            trx_line_id,
4855            error_message)
4856     SELECT trx_header_id,
4857            trx_line_id,
4858            arp_standard.fnd_message('AR_INAPI_INVALID_FOB')
4859     FROM    ar_trx_lines_gt gt
4860     WHERE  gt.line_type = 'FREIGHT'
4861     AND    gt.fob_point IS NOT NULL
4862     AND    NOT EXISTS (
4863                 SELECT 'X' FROM
4864                 ar_lookups
4865                 WHERE  lookup_type = 'FOB'
4866                 and    lookup_code = gt.fob_point
4867                 and    gt.trx_date between start_date_active and nvl(end_date_active, gt.trx_date));
4868 
4869    -- error if extended_amount is null
4870    INSERT INTO ar_trx_errors_gt
4871          ( trx_header_id,
4872            trx_line_id,
4873            error_message)
4874     SELECT trx_header_id,
4875            trx_line_id,
4876            arp_standard.fnd_message('AR_INAPI_EXT_AMT_NOT_NULL')
4877     FROM    ar_trx_lines_gt gt
4878     WHERE  gt.line_type = 'FREIGHT'
4879     AND    gt.extended_amount IS NULL;
4880 
4881     IF pg_debug = 'Y'
4882     THEN
4883         debug ('AR_INVOICE_UTILS.validate_freight (-)' );
4884     END IF;
4885     EXCEPTION
4886         WHEN OTHERS THEN
4887             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_freight '||sqlerrm;
4888             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4889             RETURN;
4890 END;
4891 
4892 PROCEDURE populate_extended_amount (
4893     x_errmsg                    OUT NOCOPY  VARCHAR2,
4894      x_return_status             OUT NOCOPY  VARCHAR2) IS
4895 
4896 -- Added for ER 5869149
4897 
4898     CURSOR c_line IS
4899     SELECT gt.trx_header_id, gt.trx_line_id,
4900            gt2.creation_sign, gt.extended_amount,
4901            gt.revenue_amount, gt.quantity_invoiced,
4902            gt.unit_selling_price,
4903 	   gt.currency_code -- Bug 13635700, FP bug of 9063570
4904       FROM ar_trx_lines_gt gt,
4905            ar_trx_header_gt gt2
4906      WHERE gt.line_type ='LINE'
4907        AND gt2.trx_header_id = gt.trx_header_id;
4908        --AND gt2.trx_class = 'CM';  --Commented Since the below Check has to be performed for both CM and INV
4909 
4910 /*    CURSOR c_line_amount_sign IS
4911     SELECT gt.trx_header_id,  gt2.creation_sign,
4912 	   sum(gt.extended_amount) extended_amount,
4913 	   sum(gt.revenue_amount)  revenue_amount
4914       FROM ar_trx_lines_gt gt,
4915            ar_trx_header_gt gt2
4916      WHERE gt.line_type ='LINE'
4917        AND gt2.trx_header_id = gt.trx_header_id
4918      group by gt.trx_header_id,  gt2.creation_sign ;
4919 --commented for bug 8731646
4920 */
4921 /* Bug 6397348  Set the amount precision as per currency precision*/
4922 
4923 ext_amt         NUMBER;
4924 
4925 Cursor  c_ext_amt is
4926 select
4927 trx_line_id,extended_amount,quantity_invoiced,unit_selling_price,currency_code
4928 from ar_trx_lines_gt
4929 where extended_amount IS NULL;
4930 
4931 BEGIN
4932     IF pg_debug = 'Y'
4933     THEN
4934         debug ('AR_INVOICE_UTILS.populate_extended_amount (+)' );
4935     END IF;
4936 
4937     x_return_status := FND_API.G_RET_STS_SUCCESS;
4938 
4939     -- Added for ER 5869149
4940     -- Credit memos should be created with correct sign for amounts
4941 
4942 /*    FOR c_line_rec IN c_line_amount_sign
4943     LOOP
4944       debug ('c_line_rec.creation_sign = ' || c_line_rec.creation_sign);
4945       debug ('c_line_rec.extended_amount = '|| c_line_rec.extended_amount );
4946       debug ('c_line_rec.revenue_amount = '|| c_line_rec.revenue_amount );
4947 
4948       -- check if amount matches creation sign for Transaction Type
4949       IF ( c_line_rec.creation_sign = 'A' ) THEN
4950         NULL;
4951       ELSIF ( c_line_rec.creation_sign  = 'P' ) THEN
4952         IF ( (NVL( c_line_rec.extended_amount, 0 ) < 0)
4953             OR ( NVL( c_line_rec.revenue_amount, 0 ) < 0 )) THEN
4954           INSERT INTO ar_trx_errors_gt
4955              ( trx_header_id,
4956                error_message)
4957           VALUES
4958              ( c_line_rec.trx_header_id,
4959                arp_standard.fnd_message('AR_INAPI_AMT_SIGN_INVALID'));
4960         END IF;
4961       ELSIF ( c_line_rec.creation_sign = 'N' ) THEN
4962         IF ( (NVL( c_line_rec.extended_amount, 0 ) > 0)
4963             OR ( NVL( c_line_rec.revenue_amount, 0 ) > 0 )) THEN
4964           INSERT INTO ar_trx_errors_gt
4965              ( trx_header_id,
4966                error_message)
4967           VALUES
4968              ( c_line_rec.trx_header_id,
4969                arp_standard.fnd_message('AR_INAPI_AMT_SIGN_INVALID'));
4970         END IF;
4971       END IF;
4972     END LOOP;
4973 */
4974 /* Moved this validation to ARXVINTB.pls
4975  *  For Bug 8731646
4976  *  */
4977 
4978     FOR c_line_rec IN c_line
4979     LOOP
4980      -- Check if amount matches quantity_invoiced*unit_selling_price
4981      -- Check 1*-20 = -20
4982      IF (c_line_rec.quantity_invoiced IS NOT NULL AND
4983          c_line_rec.unit_selling_price IS NOT NULL AND
4984          c_line_rec.extended_amount IS NOT NULL) THEN
4985 		 /* Bug 13635700, FP bug of 9063570, Set the amount precision as per currency precision*/
4986          --IF (c_line_rec.quantity_invoiced*c_line_rec.unit_selling_price <>
4987 		 IF (arpcurr.currround((c_line_rec.quantity_invoiced*c_line_rec.unit_selling_price),c_line_rec.currency_code) <>
4988              c_line_rec.extended_amount) THEN
4989              INSERT INTO ar_trx_errors_gt
4990              ( trx_header_id,
4991                trx_line_id,
4992                error_message)
4993              VALUES
4994              ( c_line_rec.trx_header_id,
4995                c_line_rec.trx_line_id,
4996                arp_standard.fnd_message('AR_INAPI_AMT_INVALID'));
4997          END IF;
4998       END IF;
4999 
5000     END LOOP;
5001 
5002 	/* Bug 6397348  Set the amount precision as per currency precision*/
5003 
5004 	For I in c_ext_amt
5005 	Loop
5006 	    ext_amt := arpcurr.currround(I.quantity_invoiced *
5007 	I.unit_selling_price,I.CURRENCY_CODE);
5008 	    UPDATE ar_trx_lines_gt
5009 		SET extended_amount =ext_amt
5010 	    WHERE extended_amount IS NULL
5011 	    AND trx_line_id= I.trx_line_id;
5012 	End Loop;
5013 
5014 	UPDATE ar_trx_lines_gt
5015 	SET    revenue_amount = extended_amount
5016 	WHERE  revenue_amount IS NULL
5017     	AND    line_type <> 'TAX';
5018 
5019     IF pg_debug = 'Y'
5020     THEN
5021         debug ('AR_INVOICE_UTILS.populate_extended_amount (-)' );
5022     END IF;
5023 
5024    EXCEPTION
5025         WHEN OTHERS THEN
5026             x_errmsg := 'Error in AR_INVOICE_UTILS.populate_extended_amount '||sqlerrm;
5027             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5028             RETURN;
5029 END;
5030 
5031 PROCEDURE validate_doc_sequence(
5032      x_errmsg                    OUT NOCOPY  VARCHAR2,
5033      x_return_status             OUT NOCOPY  VARCHAR2)
5034      IS
5035 
5036 BEGIN
5037     IF pg_debug = 'Y'
5038     THEN
5039             debug ('AR_INVOICE_UTILS.validate_doc_sequence (+)' );
5040     END IF;
5041     x_return_status := FND_API.G_RET_STS_SUCCESS;
5042 
5043     INSERT INTO ar_trx_errors_gt
5044          ( trx_header_id,
5045            error_message)
5046     SELECT trx_header_id,
5047            arp_standard.fnd_message('AR_INAPI_DUP_DOC_SEQUENCE')
5048     FROM    ar_trx_header_gt gt
5049     WHERE  gt.cust_trx_type_id IS NOT NULL
5050     AND    gt.doc_sequence_value IS NOT NULL
5051     AND    EXISTS (
5052               SELECT 'Y'   --already exists
5053               FROM   ra_recur_interim  ri,
5054                      ra_customer_trx   ct
5055               WHERE  ct.customer_trx_id       = ri.customer_trx_id
5056               AND    ct.cust_trx_type_id      = gt.cust_trx_type_id
5057               AND    ri.doc_sequence_value    = gt.doc_sequence_value
5058               AND    NVL(ri.new_customer_trx_id, -98)
5059                             <> NVL(gt.customer_trx_id, -99)
5060               UNION
5061         	SELECT 'Y'   --already exists /*Bug 4080107*/
5062 		FROM   ra_cust_trx_types   ctt,
5063                        ra_interface_lines  ril
5064                 WHERE  ril.cust_trx_type_name     = ctt.name(+)
5065                 AND    NVL(ril.cust_trx_type_id,
5066                            ctt.cust_trx_type_id)  = gt.cust_trx_type_id
5067                 AND    ril.document_number        = gt.doc_sequence_value
5068 	        AND    NVL(ril.customer_trx_id, -98)       <> NVL(gt.customer_trx_id, -99));
5069 
5070 
5071     IF pg_debug = 'Y'
5072     THEN
5073         debug ('AR_INVOICE_UTILS.validate_doc_sequence (-)' );
5074     END IF;
5075 
5076     EXCEPTION
5077         WHEN OTHERS THEN
5078             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_doc_sequence '||sqlerrm;
5079             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5080             RETURN;
5081 END;
5082 
5083 
5084 PROCEDURE populate_doc_sequence (
5085      p_trx_system_param_rec      IN          AR_INVOICE_DEFAULT_PVT.trx_system_parameters_rec_type,
5086      p_trx_profile_rec           IN          AR_INVOICE_DEFAULT_PVT.trx_profile_rec_type,
5087      x_errmsg                    OUT NOCOPY  VARCHAR2,
5088      x_return_status             OUT NOCOPY  VARCHAR2) IS
5089 
5090      CURSOR ctrxHeader IS
5091         SELECT * FROM
5092           ar_trx_header_gt
5093           WHERE trx_header_id NOT IN (
5094                 SELECT trx_header_id FROM
5095                   ar_trx_errors_gt);
5096 
5097      l_sequence_name            FND_DOCUMENT_SEQUENCES.NAME%TYPE;
5098      l_doc_sequence_id          ra_customer_trx.doc_sequence_id%type;
5099      l_doc_sequence_value       ra_customer_trx.doc_sequence_value%type;
5100      l_dummy                    BINARY_INTEGER;
5101 
5102      l_status                   number;
5103 
5104 
5105 
5106 
5107     l_seq_assign_id       fnd_doc_sequence_assignments.doc_sequence_assignment_id%TYPE;
5108     l_sequence_type       fnd_document_sequences.type%TYPE;
5109     l_db_sequence_name    fnd_document_sequences.db_sequence_name%TYPE;
5110     l_prod_table_name     fnd_document_sequences.table_name%TYPE;
5111     l_audit_table_name    fnd_document_sequences.audit_table_name%TYPE;
5112     l_mesg_flag           fnd_document_sequences.message_flag%TYPE;
5113     l_update_trx          boolean := FALSE;
5114     l_seq_err             boolean := FALSE;
5115     l_trx_str             VARCHAR2(2000);
5116     l_org_str             VARCHAR2(30);
5117     l_trx_number          ra_customer_trx.trx_number%type;
5118     l_seq_num_profile     fnd_profile_option_values.profile_option_value%type;
5119 BEGIN
5120     IF pg_debug = 'Y'
5121     THEN
5122             debug ('AR_INVOICE_UTILS.populate_doc_sequence (+)' );
5123     END IF;
5124     x_return_status := FND_API.G_RET_STS_SUCCESS;
5125 
5126         l_seq_num_profile := NVL(p_trx_profile_rec.ar_unique_seq_numbers, 'N');
5127 
5128         FOR ctrxHeaderRec IN ctrxHeader
5129         LOOP
5130             l_update_trx := FALSE;
5131 
5132           IF l_seq_num_profile <> 'N'
5133           THEN
5134             l_status :=  fnd_seqnum.get_seq_info (
5135                                                   222,
5136                                                   ctrxHeaderRec.cust_trx_type_name,
5137                                                   p_trx_system_param_rec.set_of_books_id,
5138                                                   'M',
5139                                                   trunc(ctrxHeaderRec.trx_date),
5140                                                   l_doc_sequence_id,
5141                                                   l_sequence_type,
5142                                                   l_sequence_name,
5143                                                   l_db_sequence_name,
5144                                                   l_seq_assign_id,
5145                                                   l_prod_table_name,
5146                                                   l_audit_table_name,
5147                                                   l_mesg_flag,'y','y');
5148 
5149             l_doc_sequence_value := ctrxHeaderRec.doc_sequence_value;
5150 
5151             IF l_sequence_type IS NULL
5152                AND l_seq_num_profile = 'A'
5153             THEN
5154                INSERT into ar_trx_errors_gt (
5155                         trx_header_id,
5156                         error_message )
5157                     VALUES
5158                         ( ctrxHeaderRec.trx_header_id,
5159                           'UNIQUE-ALWAYS USED');
5160 
5161             ELSIF (
5162                 ( l_doc_sequence_value IS NOT NULL
5163                   AND  l_sequence_type = 'M' )
5164                 OR
5165                 (
5166                   l_doc_sequence_value IS NULL
5167                   AND  l_sequence_type IN ( 'A', 'G') )
5168                  )
5169             THEN
5170 	      BEGIN  /*Bug 4080107*/
5171                 l_status := FND_SEQNUM.GET_SEQ_VAL(222,
5172                                  ctrxHeaderRec.cust_trx_type_name,
5173                                  p_trx_system_param_rec.set_of_books_id,
5174                                  'M',
5175                                   trunc(ctrxHeaderRec.trx_date),
5176                                   l_doc_sequence_value,
5177                                   l_doc_sequence_id);
5178 
5179                 l_update_trx := TRUE;
5180               EXCEPTION
5181 	        WHEN OTHERS THEN
5182                     INSERT into ar_trx_errors_gt (
5183                         trx_header_id,
5184                         error_message,
5185                         invalid_value)
5186                      VALUES
5187                         ( ctrxHeaderRec.trx_header_id,
5188                         arp_standard.fnd_message('AR_INAPI_DUP_DOC_SEQUENCE'),
5189                         l_doc_sequence_value );
5190 
5191                     l_seq_err := TRUE;
5192                END;
5193             END IF;
5194           END IF; /* end if seq_num <> N */
5195 
5196             /* 5921925 - moved update outside of doc sequence code so I could
5197                use it for trx_number also */
5198             IF ctrxHeaderRec.auto_trx_numbering_flag = 'Y' AND
5199                ctrxHeaderRec.trx_number IS NULL AND
5200                NOT l_seq_err
5201             THEN
5202                /* pull trx_number from sequence dynamically */
5203                IF (ctrxHeaderRec.org_id IS NOT NULL)
5204                THEN
5205                   l_org_str := '_'||to_char(ctrxHeaderRec.org_id);
5206                ELSE
5207                   l_org_str := NULL;
5208                END IF;
5209 
5210                l_trx_str :=  'select ra_trx_number_' ||
5211                              REPLACE(ctrxHeaderRec.batch_source_id, '-', 'N') ||
5212                              l_org_str||
5213                              '_s.nextval trx_number from dual';
5214 
5215                IF pg_debug = 'Y'
5216                THEN
5217                    debug ('Sql String l_trx_str ' || l_trx_str );
5218                END IF;
5219 
5220                EXECUTE IMMEDIATE l_trx_str
5221                   INTO l_trx_number;
5222 
5223                IF l_trx_number IS NOT NULL
5224                THEN
5225                   l_update_trx := TRUE;
5226                   IF pg_debug = 'Y'
5227                   THEN
5228                      debug('trx_number from bs sequence is ' || l_trx_number);
5229                   END IF;
5230                END IF;
5231 
5232             END IF;
5233 
5234             IF l_update_trx
5235             THEN
5236 
5237 		UPDATE ar_trx_header_gt
5238                     SET doc_sequence_value = l_doc_sequence_value,
5239                         doc_sequence_id    = l_doc_sequence_id,
5240                         trx_number =   DECODE(ctrxHeaderRec.copy_doc_number_flag,
5241                                             'Y',NVL(to_char(l_doc_sequence_value),
5242                                                  NVL(l_trx_number,trx_number)),
5243                                            DECODE(ctrxHeaderRec.auto_trx_numbering_flag,
5244                                             'Y',l_trx_number,trx_number))
5245                 WHERE   trx_header_id = ctrxHeaderRec.trx_header_id;
5246             END IF;
5247 
5248             l_trx_number := NULL;
5249             l_update_trx := FALSE;
5250             l_seq_err    := FALSE;
5251         END LOOP;
5252 
5253     validate_doc_sequence ( x_errmsg , x_return_status);
5254     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
5255     THEN
5256         RETURN;
5257     END IF;
5258     IF pg_debug = 'Y'
5259     THEN
5260             debug ('AR_INVOICE_UTILS.populate_doc_sequence (-)' );
5261     END IF;
5262 	 EXCEPTION
5263             WHEN OTHERS THEN
5264                   x_errmsg := 'Error in AR_INVOICE_UTILS.populate_doc_sequence '||sqlerrm ;
5265                   x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5266                   RETURN;
5267 
5268 END;
5269 
5270 /* PAYMENT UPTAKE  start */
5271 
5272 PROCEDURE copy_pmt_extension(
5273      x_errmsg                    OUT NOCOPY  VARCHAR2,
5274      x_return_status             OUT NOCOPY  VARCHAR2) IS
5275 
5276      CURSOR ctrxHeader IS
5277           SELECT * FROM
5278           ar_trx_header_gt
5279           WHERE trx_header_id NOT IN (
5280                 SELECT trx_header_id FROM
5281                   ar_trx_errors_gt)
5282           AND   payment_trxn_extension_id is not null;
5283 
5284              l_payer_rec                     IBY_FNDCPT_COMMON_PUB.PayerContext_rec_type;
5285              l_cpy_msg_data                  VARCHAR2(2000);
5286              l_trxn_attribs_rec              IBY_FNDCPT_TRXN_PUB.TrxnExtension_rec_type;
5287              p_trxn_entity_id                RA_CUSTOMER_TRX.PAYMENT_TRXN_EXTENSION_ID%TYPE;
5288              l_response_rec                  IBY_FNDCPT_COMMON_PUB.Result_rec_type;
5289              l_party_id                      NUMBER;
5290              l_pmt_trxn_extension_id          IBY_FNDCPT_COMMON_PUB.Id_tbl_type;
5291              o_payment_trxn_extension_id         RA_CUSTOMER_TRX.PAYMENT_TRXN_EXTENSION_ID%TYPE;
5292 
5293             l_return_status VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
5294             l_assignment_id                 NUMBER;
5295 
5296             l_msg_count                     NUMBER;
5297             l_msg_data                      VARCHAR2(2000);
5298 
5299 
5300 
5301 BEGIN
5302 
5303 
5304 arp_standard.debug ( 'inside Copy payment trxn ');
5305 
5306     IF pg_debug = 'Y'
5307     THEN
5308         arp_standard.debug ( 'inside Copy payment trxn ');
5309     END IF;
5310     x_return_status := FND_API.G_RET_STS_SUCCESS;
5311 
5312         FOR ctrxHeaderRec IN ctrxHeader
5313         LOOP
5314                    SELECT party.party_id
5315                    INTO   l_party_id
5316                    FROM   hz_cust_accounts hca,
5317                           hz_parties    party
5318                    WHERE  hca.party_id = party.party_id
5319                    AND    hca.cust_account_id = ctrxHeaderRec.paying_customer_id ;
5320 
5321             SELECT INSTR_ASSIGNMENT_ID
5322             INTO  l_assignment_id
5323             from  iby_fndcpt_tx_extensions
5324             where trxn_extension_id = ctrxHeaderRec.payment_trxn_extension_id;
5325 
5326         -- set up payer (=customer) record:
5327 
5328         l_payer_rec.Payment_Function := 'CUSTOMER_PAYMENT';
5329         l_payer_rec.Party_Id :=   l_party_id;
5330         l_payer_rec.org_id   := ctrxHeaderRec.org_id ;
5331         l_payer_rec.org_type := 'OPERATING_UNIT';
5332         l_payer_rec.Cust_Account_Id := ctrxHeaderRec.paying_customer_id;
5333         l_payer_rec.Account_Site_Id := ctrxHeaderRec.paying_site_use_id;
5334 
5335 
5336         if  ctrxHeaderRec.paying_site_use_id is NULL  THEN
5337 
5338           l_payer_rec.org_id := NULL;
5339           l_payer_rec.org_type := NULL;
5340 
5341         end if;
5342 
5343         l_trxn_attribs_rec.Originating_Application_Id := arp_standard.application_id;
5344         l_trxn_attribs_rec.order_id := ctrxHeaderRec.customer_trx_id ;
5345         l_trxn_attribs_rec.Trxn_Ref_Number1 := 'INVOICE';
5346         l_trxn_attribs_rec.Trxn_Ref_Number2 := ctrxHeaderRec.trx_number;
5347         l_trxn_attribs_rec.seq_type_last        := ctrxHeaderRec.mandate_last_trx_flag;
5348         l_assignment_id := l_assignment_id;
5349         l_pmt_trxn_extension_id(1) := ctrxHeaderRec.payment_trxn_extension_id;
5350 
5351 
5352              arp_standard.debug('l_payer.payment_function :<' || l_payer_rec.payment_function  || '>');
5353              arp_standard.debug('l_payer.Party_Id         :<' || l_payer_rec.Party_Id || '>');
5354              arp_standard.debug('l_payer.Org_Type         :<' || l_payer_rec.Org_Type || '>');
5355              arp_standard.debug('l_payer.Org_id           :<' || l_payer_rec.Org_id || '>');
5356              arp_standard.debug('l_payer.Cust_Account_Id  :<' || l_payer_rec.Cust_Account_Id || '>');
5357    		arp_standard.debug('l_trxn_attribs.order_id  :<'|| l_trxn_attribs_rec.order_id || '>');
5358    		arp_standard.debug('l_assignment_id          :<'|| l_assignment_id || '>');
5359    		arp_standard.debug('payment_trx_extension_id          :<'|| l_pmt_trxn_extension_id(1) || '>');
5360 
5361                   IBY_FNDCPT_TRXN_PUB.Copy_Transaction_Extension
5362                      ( p_api_version        => 1.0,
5363                        p_init_msg_list      => FND_API.G_TRUE,
5364                        p_commit             => FND_API.G_FALSE,
5365                        x_return_status      => l_return_status,
5366                        x_msg_count          => l_msg_count,
5367                        x_msg_data           => l_msg_data,
5368                        p_payer              => l_payer_rec,
5369                        p_payer_equivalency  => IBY_FNDCPT_COMMON_PUB.G_PAYER_EQUIV_UPWARD,
5370                        p_entities           => l_pmt_trxn_extension_id,
5371                        p_trxn_attribs       => l_trxn_attribs_rec,
5372                        x_entity_id          => p_trxn_entity_id,          -- out parm
5373                        x_response           => l_response_rec             -- out
5374                       );
5375 
5376 
5377                  IF l_return_status = FND_API.G_RET_STS_SUCCESS  THEN
5378 
5379                          o_payment_trxn_extension_id  := p_trxn_entity_id ;
5380 
5381                    arp_standard.debug('the copied value of trx_entn is ' || o_payment_trxn_extension_id );
5382 
5383                     UPDATE ar_trx_header_gt
5384                     SET payment_trxn_extension_id = o_payment_trxn_extension_id
5385                     WHERE trx_header_id =  ctrxHeaderRec.trx_header_id;
5386                  END IF;
5387 
5388 
5389 
5390                  IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
5391 
5392 
5393                     l_cpy_msg_data := substrb( l_response_rec.Result_Code || ': '||
5394                                    l_response_rec.Result_Message , 1, 240);
5395 
5396 
5397                      INSERT into ar_trx_errors_gt (
5398                         trx_header_id,
5399                         error_message,
5400                         invalid_value)
5401                      VALUES
5402                         ( ctrxHeaderRec.trx_header_id,
5403                          arp_standard.fnd_message('AR_CC_AUTH_FAILED'),
5404                          ctrxHeaderRec.payment_trxn_extension_id );
5405 
5406                  END IF;
5407 
5408        END LOOP;
5409 
5410          EXCEPTION
5411             WHEN OTHERS THEN
5412                   x_errmsg := 'Error in AR_INVOICE_UTILS.copy_pmt_extension '||sqlerrm ;
5413                   x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5414                   RETURN;
5415 
5416 END;
5417 
5418 
5419 /* payment uptake end */
5420 PROCEDURE  validate_item_kflex (
5421         x_errmsg                    OUT NOCOPY  VARCHAR2,
5422         x_return_status             OUT NOCOPY  VARCHAR2) IS
5423 
5424         l_so_id_flex_code   fnd_profile_option_values.profile_option_value%type;
5425         l_so_org_id         fnd_profile_option_values.profile_option_value%type;
5426 
5427         CURSOR cItemFlex IS
5428             SELECT trx_header_id, trx_line_id,inventory_item_id, org_id
5429             FROM   ar_trx_lines_gt
5430             WHERE  inventory_item_id IS NOT NULL;
5431 
5432 BEGIN
5433         IF pg_debug = 'Y'
5434         THEN
5435             debug ('AR_INVOICE_UTILS.validate_item_kflex (+)' );
5436         END IF;
5437 
5438         x_return_status := FND_API.G_RET_STS_SUCCESS;
5439         oe_profile.get('SO_ID_FLEX_CODE', l_so_id_flex_code);
5440 
5441 
5442 
5443 
5444         FOR cItemFlexRec IN cItemFlex
5445         LOOP
5446             IF (
5447                 fnd_flex_keyval.validate_ccid(
5448                              appl_short_name   => 'INV',
5449                              key_flex_code     => l_so_id_flex_code,
5450                              structure_number  => 101,
5451                              data_set          => pg_so_org_id,
5452                              get_columns       => 'invoice_enabled_flag',
5453                              combination_id    => cItemFlexRec.inventory_item_id)
5454                                  = TRUE    )
5455            THEN
5456                 -- check whether invoice_enable_flag is 'Y'
5457                 IF    fnd_flex_keyval.column_value(1) <> 'Y'
5458                 THEN
5459                     INSERT into ar_trx_errors_gt (
5460                         trx_header_id,
5461                         trx_line_id,
5462                         error_message,
5463                         invalid_value)
5464                     VALUES
5465                         ( cItemFlexRec.trx_header_id,
5466                         cItemFlexRec.trx_line_id,
5467                         arp_standard.fnd_message('AR_INAPI_INVALID_ITEM_D'),
5468                         cItemFlexRec.inventory_item_id );
5469 		ELSE
5470 		/*Bug 3844408*/
5471 		/*This update should be fired only when the description isn't populated
5472 		  initially*/
5473 		   -- get the description
5474 		   update ar_trx_lines_gt
5475 			SET description = (
5476 				select description
5477 				from mtl_system_items_vl
5478 				WHERE  inventory_item_id = cItemFlexRec.inventory_item_id
5479 				AND    organization_id = pg_so_org_id)
5480 		   WHERE  trx_line_id = cItemFlexRec.trx_line_id
5481 		   AND    description is NULL;
5482                 END IF;
5483            ELSE
5484                     INSERT into ar_trx_errors_gt (
5485                         trx_header_id,
5486                         trx_line_id,
5487                         error_message,
5488                         invalid_value)
5489                     VALUES
5490                         ( cItemFlexRec.trx_header_id,
5491                         cItemFlexRec.trx_line_id,
5492                         arp_standard.fnd_message('AR_INAPI_INVALID_ITEM_ID'),
5493                         cItemFlexRec.inventory_item_id );
5494            END IF;
5495        END LOOP;
5496 
5497        IF pg_debug = 'Y'
5498         THEN
5499             debug ('AR_INVOICE_UTILS.validate_item_kflex (-)' );
5500         END IF;
5501          EXCEPTION
5502             WHEN OTHERS THEN
5503                   x_errmsg := 'Error in AR_INVOICE_UTILS.validate_item_kflex '||sqlerrm ;
5504                   x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5505                   RETURN;
5506 END;
5507 
5508 PROCEDURE validate_territory_id (
5509     x_errmsg                    OUT NOCOPY  VARCHAR2,
5510     x_return_status             OUT NOCOPY  VARCHAR2) IS
5511 
5512     Cursor cTerritory IS
5513         SELECT territory_id, trx_header_id
5514         FROm   ar_trx_header_gt
5515         WHERE  territory_id IS NOT NULL;
5516 
5517 BEGIN
5518     IF pg_debug = 'Y'
5519     THEN
5520         debug ('AR_INVOICE_UTILS.validate_territory_id (+)' );
5521     END IF;
5522     x_return_status := FND_API.G_RET_STS_SUCCESS;
5523 
5524     FOR cTerritoryRec IN cTerritory
5525     LOOP
5526         IF ( fnd_flex_keyval.validate_ccid(
5527                                 appl_short_name   => 'AR',
5528                                 key_flex_code     => 'CT#',
5529                                 structure_number  => 101,
5530                                 combination_id    => cTerritoryRec.territory_id)
5531                           <> TRUE )
5532         THEN
5533             INSERT into ar_trx_errors_gt (
5534                         trx_header_id,
5535                         error_message,
5536                         invalid_value)
5537                     VALUES
5538                         ( cTerritoryRec.trx_header_id,
5539                         arp_standard.fnd_message('AR_INAPI_INVALID_TERRITORY'),
5540                         cTerritoryRec.territory_id );
5541         END IF;
5542     END LOOP;
5543     IF pg_debug = 'Y'
5544     THEN
5545         debug ('AR_INVOICE_UTILS.validate_territory_id (-)' );
5546     END IF;
5547     EXCEPTION
5548             WHEN OTHERS THEN
5549                   x_errmsg := 'Error in AR_INVOICE_UTILS.validate_territory_id '||sqlerrm ;
5550                   x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5551                   RETURN;
5552 END;
5553 
5554 PROCEDURE validate_warehouse_id (
5555     x_errmsg                    OUT NOCOPY  VARCHAR2,
5556     x_return_status             OUT NOCOPY  VARCHAR2) IS
5557 BEGIN
5558     IF pg_debug = 'Y'
5559     THEN
5560         debug ('AR_INVOICE_UTILS.validate_warehouse_id (+)' );
5561     END IF;
5562 
5563     x_return_status := FND_API.G_RET_STS_SUCCESS;
5564 
5565     INSERT INTO ar_trx_errors_gt
5566      ( trx_line_id,
5567        trx_header_id,
5568        error_message,
5569        Invalid_value )
5570      select l.trx_line_id,
5571             l.trx_header_id,
5572             arp_standard.fnd_message('AR_RAXTRX_INV_WAREHOUSE'),
5573             l.warehouse_id
5574      from   ar_trx_lines_gt l
5575      where  l.line_type = 'LINE'
5576         and l.warehouse_id is not null
5577         and not exists (select 'x'
5578                      from hr_organization_units hou,
5579                           hr_organization_information hoi1,
5580                           hr_organization_information hoi2,
5581                           mtl_parameters mp,
5582                           gl_sets_of_books gsob
5583                      where hou.organization_id = hoi1.organization_id
5584                      and hou.organization_id = hoi2.organization_id
5585                      and hou.organization_id = mp.organization_id
5586                      and hoi1.org_information1 = 'INV'
5587                      and hoi1.org_information2 = 'Y'
5588                      and ( hoi1.org_information_context || '') = 'CLASS'
5589                      and ( hoi2.org_information_context || '') ='Accounting Information'
5590                      and to_number(hoi2.org_information1) = gsob.set_of_books_id
5591                      and l.warehouse_id = hou.organization_id
5592                      and l.trx_date <= nvl(hou.date_to, l.trx_date));
5593 
5594 
5595     IF pg_debug = 'Y'
5596     THEN
5597         debug ('AR_INVOICE_UTILS.validate_warehouse_id (-)' );
5598     END IF;
5599     EXCEPTION
5600             WHEN OTHERS THEN
5601                   x_errmsg := 'Error in AR_INVOICE_UTILS.validate_warehouse_id '||sqlerrm ;
5602                   x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5603                   RETURN;
5604 END;
5605 
5606 
5607 PROCEDURE validate_accounting_rules (
5608     x_errmsg                    OUT NOCOPY  VARCHAR2,
5609      x_return_status             OUT NOCOPY  VARCHAR2) IS
5610 
5611 
5612  --PPRR Added rule_end_date in the select
5613     CURSOR c_invoicing_rule_c IS
5614     SELECT line.trx_header_id, line.trx_line_id, hdr.invoicing_rule_id,
5615            line.ACCOUNTING_RULE_ID, line.ACCOUNTING_RULE_DURATION,
5616            line.RULE_START_DATE,line.RULE_END_DATE,line.set_of_books_id,hdr.trx_date,
5617            rr.type, rr.frequency, rr.occurrences
5618     FROM   ar_trx_lines_gt line, ar_trx_header_gt hdr,
5619            ra_rules rr
5620     WHERE  hdr.invoicing_rule_id IS NOT NULL
5621     AND    line.accounting_rule_id IS NOT NULL
5622     AND    hdr.trx_header_id = line.trx_header_id
5623     AND    hdr.trx_class <> 'CM' -- Added for ER 5869149
5624     AND    line.line_type = 'LINE'
5625     AND    line.accounting_rule_id = rr.rule_id
5626     AND    rr.status = 'A';
5627 BEGIN
5628     -- validate the rule ID
5629     IF pg_debug = 'Y'
5630     THEN
5631         debug ('AR_INVOICE_UTILS.validate_accounting_rules (+)' );
5632     END IF;
5633 
5634     x_return_status := FND_API.G_RET_STS_SUCCESS;
5635 
5636 	--PPRR Added PP rule types in the type condition
5637         INSERT INTO ar_trx_errors_gt
5638             (   trx_header_id,
5639                 trx_line_id,
5640                 error_message)
5641                 SELECT gt.trx_header_id,
5642                        gt.trx_line_id,
5643                        arp_standard.fnd_message('AR_INAPI_INVALID_RULE_NAME')
5644                 FROM ar_trx_lines_gt gt , ar_trx_header_gt gt2
5645                 WHERE gt.accounting_rule_id IS NOT NULL
5646 		AND   gt2.trx_header_id = gt.trx_header_id
5647                 AND   gt2.trx_class <> 'CM' -- Added for ER 5869149
5648                 AND    NOT EXISTS (
5649                         SELECT 'X'
5650                         FROM   ra_rules rr
5651                         WHERE  rr.type in ('A', 'ACC_DUR','PP_DR_ALL','PP_DR_PP')
5652                         and    rr.status = 'A'
5653                         AND    rr.rule_id = gt.accounting_rule_id);
5654 
5655          INSERT INTO ar_trx_errors_gt
5656             (   trx_header_id,
5657                 trx_line_id,
5658                 error_message)
5659                 SELECT gt.trx_header_id,
5660                        gt.trx_line_id,
5661                        arp_standard.fnd_message('AR_INAPI_RULE_NAME_NOT_NULL')
5662                 FROM ar_trx_lines_gt gt, ar_trx_header_gt gth
5663                 WHERE gt.accounting_rule_id IS NULL
5664                 AND   gt.trx_header_id = gth.trx_header_id
5665                 AND   gth.invoicing_rule_id IS NOT NULL
5666 		AND   gth.trx_class <> 'CM' -- Added for ER 5869149
5667                 AND   gt.line_type = 'LINE';
5668 
5669 	-- Rule name is not allowed for Credit Memos
5670 
5671          INSERT INTO ar_trx_errors_gt
5672             (   trx_header_id,
5673                 trx_line_id,
5674                 error_message)
5675                 SELECT gt.trx_header_id,
5676                        gt.trx_line_id,
5677                        arp_standard.fnd_message('AR_INAPI_RULE_NOT_ALLOWED')
5678                 FROM ar_trx_lines_gt gt, ar_trx_header_gt gth
5679                 WHERE (gt.accounting_rule_id IS  NOT NULL
5680                       OR gth.invoicing_rule_id IS NOT NULL)
5681                 AND   gt.trx_header_id = gth.trx_header_id
5682                 AND   gth.trx_class = 'CM' -- Added for ER 5257046
5683                 AND   gt.line_type = 'LINE';
5684 
5685     FOR c_invoicing_rule_rec IN c_invoicing_rule_c
5686     LOOP
5687         -- first check whether accounting rule name is supplied or not
5688         -- in case it is not supplied then log a error
5689         IF ( c_invoicing_rule_rec.accounting_rule_id IS NULL )
5690         THEN
5691             INSERT INTO ar_trx_errors_gt
5692             (   trx_header_id,
5693                 trx_line_id,
5694                 error_message) VALUES
5695                 ( c_invoicing_rule_rec.trx_header_id,
5696                   c_invoicing_rule_rec.trx_line_id,
5697                   arp_standard.fnd_message('AR_INAPI_RULE_NAME_NOT_NULL') );
5698            --PPRR Accounting rule  Validations
5699          ELSIF c_invoicing_rule_rec.type in ('PP_DR_ALL','PP_DR_PP') THEN
5700                 IF c_invoicing_rule_rec.RULE_START_DATE IS NULL THEN
5701                       INSERT INTO ar_trx_errors_gt
5702 		            (   trx_header_id,
5703 		                trx_line_id,
5704 		                error_message) VALUES
5705                 	    ( c_invoicing_rule_rec.trx_header_id,
5706 	                      c_invoicing_rule_rec.trx_line_id,
5707 			     arp_standard.fnd_message('AR_RAXTRX_RULE_START_DT_NULL'));
5708                  END IF;
5709 
5710 		 IF c_invoicing_rule_rec.RULE_END_DATE IS NULL THEN
5711                       INSERT INTO ar_trx_errors_gt
5712                             (   trx_header_id,
5713                                 trx_line_id,
5714                                 error_message) VALUES
5715                             ( c_invoicing_rule_rec.trx_header_id,
5716                               c_invoicing_rule_rec.trx_line_id,
5717                              arp_standard.fnd_message('AR_RAXTRX_RULE_END_DT_NULL'));
5718                  END IF;
5719 
5720                  IF c_invoicing_rule_rec.RULE_END_DATE <  c_invoicing_rule_rec.RULE_START_DATE
5721                  THEN
5722                     INSERT INTO ar_trx_errors_gt
5723                             (   trx_header_id,
5724                                 trx_line_id,
5725                                 error_message) VALUES
5726                             ( c_invoicing_rule_rec.trx_header_id,
5727                               c_invoicing_rule_rec.trx_line_id,
5728 			      arp_standard.fnd_message('AR_RAXTRX_RSD_LT_RED'));
5729 		ELSE
5730                    UPDATE ar_trx_lines_gt a
5731                         SET ACCOUNTING_RULE_DURATION =
5732 					 (SELECT COUNT(*)
5733 					  FROM   ar_periods gps,
5734 						 ra_rules rr2,
5735 				                 ar_system_parameters sys,
5736 				                 gl_sets_of_books gl
5737 				         WHERE
5738 		                        rr2.rule_id        =  a.ACCOUNTING_RULE_ID
5739 	                                AND    rr2.frequency          = gps.period_type
5740                                         AND    rr2.type               NOT IN ('A', 'ACC_DUR')
5741                                         AND    sys.set_of_books_id    = gl.set_of_books_id
5742                                         AND    gl.period_set_name     = gps.period_set_name
5743                                         AND (  a.RULE_START_DATE  BETWEEN gps.start_date
5744                                                 AND     gps.end_date
5745                                             OR a.RULE_end_DATE  BETWEEN gps.start_date
5746 			                       AND gps.end_date
5747 				            OR    gps.start_date   BETWEEN a.RULE_START_DATE
5748                                                 AND  a.RULE_end_DATE ))
5749                     WHERE  trx_line_id = c_invoicing_rule_rec.trx_line_id
5750                     AND    trx_header_id = c_invoicing_rule_rec.trx_header_id;
5751                  END IF;
5752           ELSE
5753 
5754             -- validate the duration and rule start date
5755             IF c_invoicing_rule_rec.accounting_rule_duration IS NULL
5756             THEN
5757                 IF c_invoicing_rule_rec.type = 'ACC_DUR'
5758                 THEN
5759                     INSERT INTO ar_trx_errors_gt
5760                     (  trx_header_id,
5761                        trx_line_id,
5762                        error_message) VALUES
5763                     (   c_invoicing_rule_rec.trx_header_id,
5764                         c_invoicing_rule_rec.trx_line_id,
5765                         arp_standard.fnd_message('AR_INAPI_RULE_DUR_NOT_NULL' ));
5766                 ELSE
5767                     UPDATE ar_trx_lines_gt
5768                         SET ACCOUNTING_RULE_DURATION = c_invoicing_rule_rec.occurrences
5769                     WHERE  trx_line_id = c_invoicing_rule_rec.trx_line_id
5770                     AND    trx_header_id = c_invoicing_rule_rec.trx_header_id;
5771                 END IF;
5772             ELSE
5773                 IF c_invoicing_rule_rec.type = 'A'
5774                 THEN
5775                     -- validate the user supplied value
5776                     IF c_invoicing_rule_rec.ACCOUNTING_RULE_DURATION <>
5777                         c_invoicing_rule_rec.occurrences
5778                     THEN
5779                         UPDATE ar_trx_lines_gt
5780                         SET ACCOUNTING_RULE_DURATION = c_invoicing_rule_rec.occurrences
5781                         WHERE  trx_line_id = c_invoicing_rule_rec.trx_line_id
5782                         AND    trx_header_id = c_invoicing_rule_rec.trx_header_id;
5783                     END IF;
5784                 END IF;
5785             END IF;
5786 
5787             -- depending on the type validate the dependents field
5788             -- and populate the date columns.
5789             DECLARE
5790             l_period_exist	NUMBER;
5791             l_set_of_books_id   NUMBER;
5792             l_frequency         VARCHAR2(30);
5793             l_rule_start_date   DATE;
5794             l_accounting_rule_id NUMBER;
5795             --
5796             CURSOR c_special IS
5797                SELECT MIN(rs.rule_date)
5798                FROM   ra_rule_schedules rs
5799                WHERE  rs.rule_id = l_accounting_rule_id;
5800             BEGIN
5801             l_set_of_books_id := c_invoicing_rule_rec.set_of_books_id;
5802             l_frequency := c_invoicing_rule_rec.frequency;
5803             l_accounting_rule_id := c_invoicing_rule_rec.accounting_rule_id;
5804 
5805             IF l_frequency = 'SPECIFIC'  THEN
5806                OPEN c_special;
5807                FETCH c_special INTO l_rule_start_date;
5808                CLOSE c_special;
5809             ELSE
5810                IF ( c_invoicing_rule_rec.invoicing_rule_id = -2 ) THEN
5811                   l_rule_start_date := c_invoicing_rule_rec.trx_date;
5812                ELSIF ( c_invoicing_rule_rec.invoicing_rule_id = -3 ) THEN
5813                   l_rule_start_date := trunc(sysdate);
5814                END IF;
5815             END IF;
5816             --
5817             -- if the default date is not in an existing period, do not set the default.
5818             --
5819             IF ( l_rule_start_date IS NOT NULL ) THEN
5820                IF (l_frequency = 'SPECIFIC') THEN
5821                  IF c_invoicing_rule_rec.rule_start_date IS NOT NULL
5822                  THEN
5823                     IF c_invoicing_rule_rec.rule_start_date <>
5824                         l_rule_start_date
5825                     THEN
5826                         UPDATE ar_trx_lines_gt
5827                         SET rule_start_date = l_rule_start_date
5828                         WHERE  trx_line_id = c_invoicing_rule_rec.trx_line_id
5829                         AND    trx_header_id = c_invoicing_rule_rec.trx_header_id;
5830                     END IF;
5831                  END IF;
5832                ELSE -- frequency <> 'SPECIFIC'
5833                   /* Bug 5444387 - Checked for line rule_start_date to be in a GL period instead of trx_date
5834                      and also added check for the status of the gl_period with a join to gl_period_statuses */
5835                   SELECT COUNT(*)
5836                   INTO   l_period_exist
5837                   FROM   gl_periods gp,
5838                          gl_sets_of_books sob,
5839                          gl_period_statuses gps
5840                   WHERE  sob.set_of_books_id = l_set_of_books_id
5841                   AND    gp.adjustment_period_flag = 'N'
5842                   AND    gp.period_set_name = sob.period_set_name
5843                   AND    gp.period_type = l_frequency
5844                   AND    gp.period_name  = gps.period_name
5845                   AND    gp.period_type  = gps.period_type
5846                   AND    gp.period_year  = gps.period_year
5847                   AND    gp.period_num  = gps.period_num
5848                   AND    gp.quarter_num  = gps.quarter_num
5849                   AND    gp.year_start_date = gps.year_start_date
5850                   AND    gp.quarter_start_date = gps.quarter_start_date
5851                   AND    gp.start_date = gps.start_date
5852                   AND    gp.end_date  = gps.end_date
5853                   AND    gps.application_id  = 222
5854                   AND    gps.adjustment_period_flag = 'N'
5855                   AND    sob.set_of_books_id = gps.set_of_books_id
5856                   AND    gps.period_type = sob.accounted_period_type
5857                   AND    c_invoicing_rule_rec.rule_start_date BETWEEN gp.start_date AND gp.end_date;
5858 
5859                   /* Bug 5444387 - Removed the check for trx_date to be equal to rule_start_date of each line
5860                      as it is unnecessory and not always satisfied */
5861                   IF ( l_period_exist <> 1 ) THEN
5862                          INSERT INTO ar_trx_errors_gt
5863                             (  trx_header_id,
5864                                trx_line_id,
5865                                error_message) VALUES
5866                             (   c_invoicing_rule_rec.trx_header_id,
5867                                 c_invoicing_rule_rec.trx_line_id,
5868                                 arp_standard.fnd_message('AR_INAPI_RULE_START_DT_NO_NULL') );
5869                   END IF;
5870                END IF;
5871             END IF;
5872          END;
5873 
5874         END IF;
5875 
5876 
5877 
5878     END LOOP;
5879     IF pg_debug = 'Y'
5880     THEN
5881         debug ('AR_INVOICE_UTILS.validate_accounting_rules (-)' );
5882     END IF;
5883 
5884     EXCEPTION
5885         WHEN OTHERS THEN
5886             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_accounting_rules '||sqlerrm;
5887             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5888             RETURN;
5889 END;
5890 
5891 PROCEDURE validate_line_ussgl_code (
5892   x_errmsg                    OUT NOCOPY  VARCHAR2,
5893   x_return_status             OUT NOCOPY  VARCHAR2) AS
5894 
5895 BEGIN
5896 
5897   IF pg_debug = 'Y' THEN
5898     debug ('ar_invoice_utils.validate_line_ussgl_code(+)' );
5899   END IF;
5900 
5901   x_return_status := FND_API.G_RET_STS_SUCCESS;
5902 
5903   INSERT INTO ar_trx_errors_gt (
5904     trx_header_id,
5905     trx_line_id,
5906     error_message,
5907     invalid_value)
5908     SELECT trx_header_id,
5909            trx_line_id,
5910            arp_standard.fnd_message('AR_INAPI_INVALID_USSGL_CODE'),
5911            default_ussgl_transaction_code
5912     FROM   ar_trx_lines_gt gt
5913     WHERE  gt.default_ussgl_transaction_code IS NOT NULL
5914     AND NOT EXISTS
5915       (SELECT 'X'
5916        FROM  gl_ussgl_transaction_codes gutc
5917        WHERE gutc.ussgl_transaction_code = gt.default_ussgl_transaction_code
5918        AND   gutc.chart_of_accounts_id = arp_global.chart_of_accounts_id
5919        AND   gt.trx_date
5920          BETWEEN NVL(gutc.start_date_active, gt.trx_date)
5921          AND NVL(gutc.end_date_active, gt.trx_date));
5922 
5923     IF pg_debug = 'Y' THEN
5924       debug ('ar_invoice_utils.validate_line_ussgl_code(-)' );
5925     END IF;
5926 
5927   EXCEPTION
5928     WHEN OTHERS THEN
5929       x_errmsg := 'error in ar_invoice_utils.validate_line_ussgl_code '|| sqlerrm;
5930       x_return_status := fnd_api.g_ret_sts_unexp_error;
5931       RETURN;
5932 
5933 END validate_line_ussgl_code;
5934 
5935 
5936 PROCEDURE populate_line_attributes (
5937     x_errmsg                    OUT NOCOPY  VARCHAR2,
5938     x_return_status             OUT NOCOPY  VARCHAR2) IS
5939 
5940 BEGIN
5941     IF pg_debug = 'Y'
5942     THEN
5943         debug ('AR_INVOICE_UTILS.populate_line_attributes (+)' );
5944     END IF;
5945     x_return_status := FND_API.G_RET_STS_SUCCESS;
5946 
5947     -- check if memo line is passed and any unit of measue and unit price.
5948     BEGIN
5949         UPDATE ar_trx_lines_gt gt
5950             set gt.unit_selling_price =
5951                     ( SELECT m.unit_std_price
5952                       FROM ar_memo_lines_vl m
5953                       WHERE  m.memo_line_id = gt.memo_line_id
5954                       AND    gt.memo_line_id IS NOT NULL
5955                       AND    sysdate between m.start_date and nvl(m.end_date,sysdate))
5956         WHERE gt.unit_selling_price IS NULL;
5957 
5958         UPDATE ar_trx_lines_gt gt
5959             set gt.uom_code =
5960                     ( SELECT m.uom_code
5961                       FROM ar_memo_lines_vl m
5962                       WHERE  m.memo_line_id = gt.memo_line_id
5963                       AND    gt.memo_line_id IS NOT NULL
5964                       AND    sysdate between m.start_date and nvl(m.end_date,sysdate))
5965         WHERE gt.uom_code IS NULL;
5966     END;
5967     populate_extended_amount (
5968         x_errmsg            =>  x_errmsg,
5969         x_return_status     =>  x_return_status );
5970 
5971     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
5972     THEN
5973          RETURN;
5974     END IF;
5975 
5976     IF pg_debug = 'Y'
5977     THEN
5978         debug ('AR_INVOICE_UTILS.populate_line_attributes (-)' );
5979     END IF;
5980 
5981     EXCEPTION
5982         WHEN OTHERS THEN
5983             x_errmsg := 'Error in AR_INVOICE_UTILS.populate_line_attributes '||sqlerrm;
5984             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5985             RETURN;
5986 END;
5987 
5988 PROCEDURE populate_exchange_rate (
5989         p_trx_system_parameters_rec     IN  AR_INVOICE_DEFAULT_PVT.trx_system_parameters_rec_type,
5990         p_trx_profile_rec               IN  AR_INVOICE_DEFAULT_PVT.trx_profile_rec_type,
5991         x_errmsg                        OUT NOCOPY  VARCHAR2,
5992         x_return_status                 OUT NOCOPY  VARCHAR2) IS
5993 
5994 
5995         l_relation                    VARCHAR2(1);
5996         l_exchange_rate               ra_customer_trx.exchange_rate%type;
5997         l_default_exchange_rate_type  ra_customer_trx.exchange_rate_type%type;
5998         CURSOR cExchangeRate IS
5999             SELECT trx_header_id, trx_currency,
6000                    nvl(exchange_rate_type,
6001                    p_trx_profile_rec.default_exchange_rate_type) exchange_rate_type,
6002                    trunc(nvl(exchange_date,trx_date)) exchange_date, exchange_rate
6003             FROM   ar_trx_header_gt gt
6004             WHERE  p_trx_system_parameters_rec.base_currency_code <>
6005                    trx_currency
6006             AND    nvl(exchange_rate_type,
6007                     p_trx_profile_rec.default_exchange_rate_type)  <> 'User'
6008             AND NOT EXISTS
6009               (SELECT 'X'
6010                FROM   ar_trx_errors_gt errgt
6011                WHERE  errgt.trx_header_id = gt.trx_header_id
6012                AND    errgt.invalid_value = gt.trx_currency);
6013 
6014 BEGIN
6015     IF pg_debug = 'Y'
6016     THEN
6017         debug ('AR_INVOICE_UTILS.populate_exchange_rate (+)' );
6018     END IF;
6019     x_return_status := FND_API.G_RET_STS_SUCCESS;
6020 
6021     FOR cExchangeRateRec IN cExchangeRate
6022     LOOP
6023       BEGIN
6024         l_relation := gl_currency_api.is_fixed_rate
6025             ( cExchangeRateRec.trx_currency,
6026               p_trx_system_parameters_rec.base_currency_code,
6027               cExchangeRateRec.exchange_date);
6028         IF l_relation = 'Y'
6029         THEN
6030             IF pg_debug = 'Y'
6031             THEN
6032                  debug ( 'in Y ');
6033                  debug ( 'trx curr  ' || cExchangeRateRec.trx_currency);
6034                  debug ( 'Base  curr  ' || p_trx_system_parameters_rec.base_currency_code);
6035                  debug ( 'Exch date  ' || cExchangeRateRec.exchange_date);
6036                  debug ( 'Rate type ' || cExchangeRateRec.exchange_rate_type);
6037             END IF;
6038 
6039 
6040              l_exchange_rate :=
6041                     gl_currency_api.get_rate(cExchangeRateRec.trx_currency,
6042                                              p_trx_system_parameters_rec.base_currency_code,
6043                                              cExchangeRateRec.exchange_date,
6044                                              cExchangeRateRec.exchange_rate_type );
6045         ELSIF ( cExchangeRateRec.exchange_rate_type <> 'User')
6046         THEN
6047             IF pg_debug = 'Y'
6048             THEN
6049                  debug ( 'in Y ');
6050                  debug ( 'trx curr  ' || cExchangeRateRec.trx_currency);
6051                  debug ( 'Base  curr  ' || p_trx_system_parameters_rec.base_currency_code);
6052                  debug ( 'Exch date  ' || cExchangeRateRec.exchange_date);
6053                  debug ( 'Rate type ' || cExchangeRateRec.exchange_rate_type);
6054             END IF;
6055             l_exchange_rate :=
6056                 gl_currency_api.get_rate(cExchangeRateRec.trx_currency,
6057                                              p_trx_system_parameters_rec.base_currency_code,
6058                                              cExchangeRateRec.exchange_date,
6059                                              cExchangeRateRec.exchange_rate_type );
6060         END IF;
6061         IF pg_debug = 'Y'
6062         THEN
6063                  debug ( ' Exchange Rate ' || l_exchange_rate);
6064         END IF;
6065 
6066         UPDATE ar_trx_header_gt
6067             SET exchange_rate = l_exchange_rate,
6068                 exchange_date = cExchangeRateRec.exchange_date,
6069                 exchange_rate_type = cExchangeRateRec.exchange_rate_type
6070         WHERE trx_header_id = cExchangeRateRec.trx_header_id;
6071     EXCEPTION
6072         WHEN OTHERS THEN
6073                 -- in case gl_api fails it will insert record
6074                 -- into error table
6075             INSERT INTO ar_trx_errors_gt
6076                     (   trx_header_id,
6077                         error_message,
6078                         invalid_value)
6079                 VALUES
6080                     ( cExchangeRateRec.trx_header_id,
6081                       arp_standard.fnd_message('AR_INAPI_NO_EXCH_DEFINE'),
6082                       cExchangeRateRec.trx_currency);
6083 
6084       END;
6085     END LOOP;
6086 
6087     -- Update the exchange_date  in case exchange_rate_type is 'User'
6088     -- and no exchange date has been provided.
6089     UPDATE ar_trx_header_gt
6090      SET   exchange_date = trunc(trx_date)
6091     WHERE  exchange_rate_type = 'User'
6092     AND    exchange_date IS NULL;
6093 
6094     -- Now validates whether all exchange information is populated
6095     -- in case trxn currecny <> func currency
6096     INSERT INTO ar_trx_errors_gt (
6097     trx_header_id,
6098     error_message,
6099     invalid_value)
6100     SELECT trx_header_id,
6101            arp_standard.fnd_message('AR_EXCHANGE_RATE_NEEDED'),
6102            gt.trx_currency
6103     FROM   ar_trx_header_gt gt
6104     WHERE  gt.trx_currency IS NOT NULL
6105     AND    ( exchange_rate IS NULL
6106        OR    exchange_rate <= 0
6107        OR    exchange_date IS NULL)
6108     AND    gt.trx_currency <> p_trx_system_parameters_rec.base_currency_code;
6109 
6110     IF pg_debug = 'Y'
6111     THEN
6112         debug ('AR_INVOICE_UTILS.populate_exchange_rate (-)' );
6113     END IF;
6114 
6115     EXCEPTION
6116         WHEN OTHERS
6117             THEN
6118                 x_errmsg := 'Error in AR_INVOICE_UTILS.populate_exchange_rate '||sqlerrm;
6119                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6120                 RETURN;
6121 
6122 END;
6123 
6124 Procedure populate_payment_method (
6125          x_errmsg                   OUT NOCOPY VARCHAR2,
6126          x_return_status            OUT NOCOPY VARCHAR2 ) AS
6127 
6128     CURSOR cCustDetails IS
6129         SELECT trx_currency, paying_customer_id,
6130                paying_site_use_id, bill_to_customer_id,
6131                bill_to_site_use_id, trx_date, trx_header_id,
6132 	       payment_trxn_extension_id
6133         FROM ar_trx_header_gt
6134         WHERE receipt_method_id IS NULL;
6135 	--AND   payment_trxn_extension_id IS NOT NULL;
6136     l_receipt_method_name             ar_receipt_methods.name%type;
6137     l_receipt_method_id               ar_receipt_methods.receipt_method_id%type;
6138     l_creation_method_code            ar_receipt_classes.creation_method_code%type;
6139     l_bank_account_id                 ce_bank_accounts.bank_account_id%type;
6140     l_bank_account_num                ce_bank_accounts.bank_account_num%type;
6141     l_bank_name                       ce_bank_branches_v.bank_name%type;
6142     l_bank_branch_name                ce_bank_branches_v.bank_branch_name%type;
6143     l_bank_branch_id                  ce_bank_accounts.bank_branch_id%type;
6144 
6145 BEGIN
6146     FOR cCustDetailsRec IN cCustDetails
6147     LOOP
6148         arp_trx_defaults_3.get_pay_method_and_bank_deflts (
6149                p_trx_date           => cCustDetailsRec.trx_date,
6150                p_currency_code      => cCustDetailsRec.trx_currency,
6151                p_paying_customer_id => cCustDetailsRec.paying_customer_id,
6152                p_paying_site_use_id => cCustDetailsRec.paying_site_use_id,
6153                p_bill_to_customer_id => cCustDetailsRec.bill_to_customer_id,
6154                p_bill_to_site_use_id => cCustDetailsRec.bill_to_site_use_id,
6155                p_payment_type_code   => null,
6156                p_payment_method_name => l_receipt_method_name,
6157                p_receipt_method_id   => l_receipt_method_id,
6158                p_creation_method_code => l_creation_method_code,
6159                p_customer_bank_account_id  => l_bank_account_id,
6160                p_bank_account_num     => l_bank_account_num,
6161                p_bank_name            => l_bank_name,
6162                p_bank_branch_name     => l_bank_branch_name,
6163                p_bank_branch_id       => l_bank_branch_id
6164                                    );
6165 
6166 	 IF l_creation_method_code = 'AUTOMATIC'
6167            AND l_receipt_method_id IS NOT NULL
6168 	   AND cCustDetailsRec.payment_trxn_extension_id IS NOT NULL
6169          THEN
6170             UPDATE ar_trx_header_gt
6171                 set receipt_method_id = l_receipt_method_id
6172             WHERE trx_header_id = cCustDetailsRec.trx_header_id;
6173          END IF;
6174 
6175 	 IF l_creation_method_code = 'MANUAL'
6176 	    AND l_receipt_method_id IS NOT NULL
6177          THEN
6178             UPDATE ar_trx_header_gt
6179                 set receipt_method_id = l_receipt_method_id
6180             WHERE trx_header_id = cCustDetailsRec.trx_header_id;
6181          END IF;
6182 
6183     END LOOP;
6184 END;
6185 
6186 
6187 /*Bug 8622438 Populate default payment method. Populates default data when both receipt method and
6188   payment trxn extension id passed ar enull*/
6189 
6190 PROCEDURE populate_default_pay_method (
6191          x_errmsg                   OUT NOCOPY VARCHAR2,
6192          x_return_status            OUT NOCOPY VARCHAR2 ) AS
6193 
6194     CURSOR cCustDetails IS
6195         SELECT trx_currency, paying_customer_id, paying_site_use_id,
6196                bill_to_customer_id, bill_to_site_use_id, trx_date,
6197                trx_header_id, org_id, trx_number, customer_trx_id,
6198                mandate_last_trx_flag
6199         FROM ar_trx_header_gt
6200         WHERE receipt_method_id IS NULL
6201         AND   payment_trxn_extension_id IS NULL;
6202 
6203     l_receipt_method_name             ar_receipt_methods.name%type;
6204     l_receipt_method_id               ar_receipt_methods.receipt_method_id%type;
6205     l_creation_method_code            ar_receipt_classes.creation_method_code%type;
6206     l_bank_account_id                 ce_bank_accounts.bank_account_id%type;
6207     l_bank_account_num                ce_bank_accounts.bank_account_num%type;
6208     l_bank_name                       ce_bank_branches_v.bank_name%type;
6209     l_bank_branch_name                ce_bank_branches_v.bank_branch_name%type;
6210     l_bank_branch_id                  ce_bank_accounts.bank_branch_id%type;
6211 
6212     l_instrument_assignment_id        iby_trxn_extensions_v.instr_assignment_id%type;
6213     l_instrument_type                 iby_fndcpt_payer_assgn_instr_v.instrument_type%type;
6214     l_payment_channel_code            varchar2(30);
6215 
6216     l_return_status        VARCHAR2(1);
6217     l_msg_count            NUMBER;
6218     l_msg_data             VARCHAR2(500);
6219     l_payer                IBY_FNDCPT_COMMON_PUB.payercontext_rec_type;
6220     l_payer_equivalency    VARCHAR2(500);
6221     l_trxn_attribs         IBY_FNDCPT_TRXN_PUB.trxnextension_rec_type;
6222     l_entity_id            NUMBER;
6223     l_response             IBY_FNDCPT_COMMON_PUB.result_rec_type;
6224 
6225     -- This values based on global variables from FND_API G_TRUE and G_FALSE;
6226     l_true                 VARCHAR2(1) := 'T';
6227     l_false                 VARCHAR2(1) := 'F';
6228 
6229 
6230 BEGIN
6231 
6232     FOR cCustDetailsRec IN cCustDetails
6233     LOOP
6234         arp_trx_defaults_3.get_pay_method_and_bank_deflts (
6235                p_trx_date                  => cCustDetailsRec.trx_date,
6236                p_currency_code             => cCustDetailsRec.trx_currency,
6237                p_paying_customer_id        => cCustDetailsRec.paying_customer_id,
6238                p_paying_site_use_id        => cCustDetailsRec.paying_site_use_id,
6239                p_bill_to_customer_id       => cCustDetailsRec.bill_to_customer_id,
6240                p_bill_to_site_use_id       => cCustDetailsRec.bill_to_site_use_id,
6241                p_payment_type_code         => null,
6242                p_payment_method_name       => l_receipt_method_name,
6243                p_receipt_method_id         => l_receipt_method_id,
6244                p_creation_method_code      => l_creation_method_code,
6245                p_customer_bank_account_id  => l_bank_account_id,
6246                p_bank_account_num          => l_bank_account_num,
6247                p_bank_name                 => l_bank_name,
6248                p_bank_branch_name          => l_bank_branch_name,
6249                p_bank_branch_id            => l_bank_branch_id
6250                                    );
6251 
6252          IF l_creation_method_code = 'AUTOMATIC'
6253                 AND l_receipt_method_id IS NOT NULL
6254          THEN
6255 
6256              -- Set IN parameters for API from the block.
6257                 l_payer.payment_function                  := 'CUSTOMER_PAYMENT';
6258                 l_payer.party_id                          := ARP_TRX_DEFAULTS_3.get_party_id(cCustDetailsRec.paying_customer_id);
6259                 l_payer.org_type                          := 'OPERATING_UNIT';
6260                 l_payer.org_id                            := cCustDetailsRec.org_id;
6261                 l_payer.cust_account_id                   := cCustDetailsRec.paying_customer_id;
6262                 l_payer.account_site_id                   := cCustDetailsRec.paying_site_use_id;
6263 
6264                 l_payer_equivalency                       := 'UPWARD';
6265 
6266                 SELECT payment_channel_code,decode(payment_channel_code,'BANK_ACCT_XFER','BANKACCOUNT',
6267                                         'BILLS_RECEIVABLE','BANKACCOUNT','CREDIT_CARD','CREDITCARD','BANKACCOUNT')
6268                 INTO     l_payment_channel_code,l_instrument_type
6269                 FROM ar_receipt_methods
6270                 WHERE receipt_method_id = l_receipt_method_id;
6271 
6272 
6273                 arp_trx_defaults_3.get_instr_defaults(
6274                                         cCustDetailsRec.org_id,
6275                                         cCustDetailsRec.paying_customer_id,
6276                                         cCustDetailsRec.paying_site_use_id,
6277                                         l_instrument_type,
6278                                         cCustDetailsRec.trx_currency,
6279                                         l_instrument_assignment_id
6280                                          );
6281 
6282                 l_trxn_attribs.originating_application_id := 222;
6283                 l_trxn_attribs.order_id                   := cCustDetailsRec.trx_number;
6284                 l_trxn_attribs.po_number                  := NULL;
6285                 l_trxn_attribs.po_line_number             := NULL;
6286                 l_trxn_attribs.trxn_ref_number1           := 'TRANSACTION';
6287                 l_trxn_attribs.trxn_ref_number2           := cCustDetailsRec.customer_trx_id;
6288                 l_trxn_attribs.instrument_security_code   := NULL;
6289                 l_trxn_attribs.voiceauth_flag             := NULL;
6290                 l_trxn_attribs.voiceauth_code             := NULL;
6291                 l_trxn_attribs.voiceauth_date             := NULL;
6292                 l_trxn_attribs.additional_info            := NULL;
6293                 l_trxn_attribs.seq_type_last              := cCustDetailsRec.mandate_last_trx_flag;
6294 
6295 
6296                 -- Call to insert the transaction extension through Payments PL/SQL API
6297                   IBY_FNDCPT_TRXN_PUB.create_transaction_extension(
6298                   p_api_version           => 1.0,
6299                   p_init_msg_list         => l_false,
6300                   p_commit                => l_true,
6301                   x_return_status         => l_return_status,
6302                   x_msg_count             => l_msg_count,
6303                   x_msg_data              => l_msg_data,
6304                   p_payer                 => l_payer,
6305                   p_payer_equivalency     => l_payer_equivalency,
6306                   p_pmt_channel           => l_payment_channel_code,
6307                   p_instr_assignment      => l_instrument_assignment_id,
6308                   p_trxn_attribs          => l_trxn_attribs,
6309                   x_entity_id             => l_entity_id,
6310                   x_response              => l_response);
6311 
6312                 -- The values are based on FND_API.  S, E, U (Success, Error, Unexpected
6313                 IF (l_return_status = 'S') THEN
6314                     UPDATE ar_trx_header_gt
6315                     set receipt_method_id = l_receipt_method_id,
6316                     payment_trxn_extension_id=l_entity_id
6317                     WHERE trx_header_id = cCustDetailsRec.trx_header_id;
6318 
6319                 ELSE
6320                         arp_standard.debug('Processing customer_trx_id :- '||cCustDetailsRec.customer_trx_id);
6321                         arp_standard.debug('result_code :- '||l_response.result_code);
6322                         arp_standard.debug('result_category :- '||l_response.result_category);
6323                         arp_standard.debug('result_message :- '||l_response.result_message);
6324                         arp_standard.debug('l_return_status :- '||l_return_status);
6325                         arp_standard.debug('l_msg_count :- '||l_msg_count);
6326                         arp_standard.debug('l_msg_data :- '||l_msg_data);
6327                 END IF;
6328 
6329 
6330          END IF;
6331   END LOOP;
6332 
6333  EXCEPTION
6334  WHEN OTHERS THEN
6335    arp_standard.debug('Error in Default Payment Transaction Extension ID');
6336    arp_standard.debug('[' || SQLERRM(SQLCODE) || ']');
6337    x_errmsg := 'Error in Default Payment Transaction Extension ID' ||  SQLERRM;
6338    x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6339 
6340    RAISE;
6341 
6342 
6343 END populate_default_pay_method;
6344 
6345 PROCEDURE validate_header (
6346     p_trx_system_param_rec      IN          AR_INVOICE_DEFAULT_PVT.trx_system_parameters_rec_type,
6347     p_trx_profile_rec           IN          AR_INVOICE_DEFAULT_PVT.trx_profile_rec_type,
6348     x_errmsg                    OUT NOCOPY  VARCHAR2,
6349     x_return_status             OUT NOCOPY  VARCHAR2) AS
6350 
6351 BEGIN
6352 
6353     IF pg_debug = 'Y'
6354     THEN
6355         debug ('AR_INVOICE_UTILS.validate_header (+)' );
6356     END IF;
6357 
6358     x_return_status := FND_API.G_RET_STS_SUCCESS;
6359 
6360 
6361     validate_trx_number (
6362         x_errmsg                    => x_errmsg,
6363         x_return_status             => x_return_status);
6364     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6365     THEN
6366         RETURN;
6367     END IF;
6368 
6369     --validate_no_of_batch_sources;
6370     validate_batch_source(
6371         x_errmsg                    => x_errmsg,
6372         x_return_status             => x_return_status);
6373     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6374     THEN
6375         RETURN;
6376     END IF;
6377 
6378     validate_currency(
6379         x_errmsg                    => x_errmsg,
6380         x_return_status             => x_return_status);
6381     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6382     THEN
6383         RETURN;
6384     END IF;
6385 
6386     -- moving this check to populate_ref_header_attribute
6387     -- becoz transaction type can be populated from
6388     -- batch source in case user does not pass any.
6389     /*validate_transaction_type(
6390         x_errmsg                    => x_errmsg,
6391         x_return_status             => x_return_status);
6392     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6393     THEN
6394         RETURN;
6395     END IF; */
6396 
6397 
6398 
6399 
6400 
6401     populate_bill_to_customer_id (
6402       x_errmsg            =>  x_errmsg,
6403       x_return_status     =>  x_return_status );
6404     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6405       RETURN;
6406     END IF;
6407 
6408     validate_bill_to_customer_id(
6409         x_errmsg                    => x_errmsg,
6410         x_return_status             => x_return_status);
6411     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6412     THEN
6413         RETURN;
6414     END IF;
6415 
6416     validate_bill_to_customer_name(
6417         x_errmsg                    => x_errmsg,
6418         x_return_status             => x_return_status);
6419     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6420     THEN
6421         RETURN;
6422     END IF;
6423 
6424     validate_bill_to_cust_number(
6425         x_errmsg                    => x_errmsg,
6426         x_return_status             => x_return_status);
6427     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6428     THEN
6429         RETURN;
6430     END IF;
6431 
6432     populate_ship_to_customer_id (
6433       x_errmsg            =>  x_errmsg,
6434       x_return_status     =>  x_return_status );
6435 
6436     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6437       RETURN;
6438     END IF;
6439 
6440     validate_bill_to_site_use_id(
6441         x_errmsg                    => x_errmsg,
6442         x_return_status             => x_return_status);
6443     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6444     THEN
6445         RETURN;
6446     END IF;
6447 
6448     validate_bill_to_address_id(
6449         x_errmsg                    => x_errmsg,
6450         x_return_status             => x_return_status);
6451     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6452     THEN
6453         RETURN;
6454     END IF;
6455 
6456     validate_bill_to_contact_id(
6457         x_errmsg                    => x_errmsg,
6458         x_return_status             => x_return_status);
6459     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6460     THEN
6461         RETURN;
6462     END IF;
6463 
6464 
6465     validate_sold_to_customer_id(
6466         x_errmsg                    => x_errmsg,
6467         x_return_status             => x_return_status);
6468     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6469     THEN
6470         RETURN;
6471     END IF;
6472 
6473     validate_terms(
6474         x_errmsg                    => x_errmsg,
6475         x_return_status             => x_return_status);
6476     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6477     THEN
6478         RETURN;
6479     END IF;
6480 
6481     validate_salesrep(
6482         p_trx_system_param_rec => p_trx_system_param_rec,
6483         x_errmsg                => x_errmsg,
6484         x_return_status         => x_return_status);
6485     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6486     THEN
6487         RETURN;
6488     END IF;
6489 
6490     validate_invoicing_rule_id(
6491         x_errmsg                    => x_errmsg,
6492         x_return_status             => x_return_status);
6493     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6494     THEN
6495         RETURN;
6496     END IF;
6497 
6498     validate_print_option(
6499         x_errmsg                    => x_errmsg,
6500         x_return_status             => x_return_status);
6501     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6502     THEN
6503         RETURN;
6504     END IF;
6505 
6506     validate_default_tax(
6507         x_errmsg                    => x_errmsg,
6508         x_return_status             => x_return_status);
6509     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6510     THEN
6511         RETURN;
6512     END IF;
6513 
6514     validate_status(
6515         x_errmsg                    => x_errmsg,
6516         x_return_status             => x_return_status);
6517     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6518     THEN
6519         RETURN;
6520     END IF;
6521 
6522     validate_finance_charges(
6523         x_errmsg                    => x_errmsg,
6524         x_return_status             => x_return_status);
6525     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6526     THEN
6527         RETURN;
6528     END IF;
6529 
6530     validate_related_cust_trx_id(
6531         x_errmsg                    => x_errmsg,
6532         x_return_status             => x_return_status);
6533     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6534     THEN
6535         RETURN;
6536     END IF;
6537 
6538     validate_agreement_id(
6539         x_errmsg                    => x_errmsg,
6540         x_return_status             => x_return_status);
6541     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6542     THEN
6543         RETURN;
6544     END IF;
6545 
6546     validate_trx_class (
6547       x_errmsg         => x_errmsg,
6548       x_return_status  => x_return_status);
6549     IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
6550       RETURN;
6551     END IF;
6552 
6553     validate_ship_via (
6554       x_errmsg         => x_errmsg,
6555       x_return_status  => x_return_status);
6556     IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
6557       RETURN;
6558     END IF;
6559 
6560     validate_fob_point (
6561       x_errmsg         => x_errmsg,
6562       x_return_status  => x_return_status);
6563     IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
6564       RETURN;
6565     END IF;
6566 
6567     validate_remit_to_address_id (
6568       x_errmsg         => x_errmsg,
6569       x_return_status  => x_return_status);
6570     IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
6571       RETURN;
6572     END IF;
6573 
6574     validate_ussgl_code (
6575       x_errmsg         => x_errmsg,
6576       x_return_status  => x_return_status);
6577     IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
6578       RETURN;
6579     END IF;
6580 
6581     validate_cust_bank_account_id (
6582       x_errmsg         => x_errmsg,
6583       x_return_status  => x_return_status);
6584     IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
6585       RETURN;
6586     END IF;
6587 
6588     validate_doc_sequence_value (
6589       p_trx_system_param_rec => p_trx_system_param_rec,
6590       p_trx_profile_rec      => p_trx_profile_rec,
6591       x_errmsg               => x_errmsg,
6592       x_return_status        => x_return_status);
6593     IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
6594       RETURN;
6595     END IF;
6596 
6597     validate_exchange_rate_type (
6598       x_errmsg               => x_errmsg,
6599       x_return_status        => x_return_status);
6600     IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
6601       RETURN;
6602     END IF;
6603 
6604     validate_ship_to_site_use_id(
6605          x_errmsg                    => x_errmsg,
6606          x_return_status             => x_return_status,
6607 	 x_called		     => 'BEFORE');
6608     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6609     THEN
6610        RETURN;
6611     END IF;
6612 
6613     -- After validation of user input values
6614     -- now populate all the related values.
6615 
6616     populate_ref_hdr_attributes(
6617         x_errmsg                    => x_errmsg,
6618         x_return_status             => x_return_status);
6619     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6620     THEN
6621         RETURN;
6622     END IF;
6623 
6624     validate_gl_date (
6625         x_errmsg               => x_errmsg,
6626         x_return_status        => x_return_status);
6627     IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
6628       RETURN;
6629     END IF;
6630 
6631      validate_paying_customer_id (
6632       p_trx_system_param_rec => p_trx_system_param_rec,
6633       x_errmsg               => x_errmsg,
6634       x_return_status        => x_return_status);
6635     IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
6636       RETURN;
6637     END IF;
6638 
6639     validate_paying_site_use_id (
6640       p_trx_system_param_rec => p_trx_system_param_rec,
6641       x_errmsg               => x_errmsg,
6642       x_return_status        => x_return_status);
6643     IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
6644       RETURN;
6645     END IF;
6646 
6647     populate_doc_sequence (
6648         p_trx_system_param_rec      =>  p_trx_system_param_rec,
6649         p_trx_profile_rec           =>   p_trx_profile_rec,
6650         x_errmsg                    => x_errmsg,
6651         x_return_status             => x_return_status);
6652     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6653     THEN
6654         RETURN;
6655     END IF;
6656 
6657 /* PAYMENT UPTAKE */
6658 
6659      copy_pmt_extension(
6660         x_errmsg                    => x_errmsg,
6661         x_return_status             => x_return_status);
6662     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6663     THEN
6664         RETURN;
6665     END IF;
6666 
6667 
6668     populate_ship_to_site_use_id(
6669         x_errmsg                    => x_errmsg,
6670         x_return_status             => x_return_status);
6671     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6672     THEN
6673         RETURN;
6674     END IF;
6675 
6676     /* 10041924 - removed extra call to populate_ship_to_site_use_id */
6677 
6678     populate_ship_to_address_id(
6679         x_errmsg                    => x_errmsg,
6680         x_return_status             => x_return_status);
6681     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6682     THEN
6683         RETURN;
6684     END IF;
6685 
6686     populate_ship_to_contact_id(
6687         x_errmsg                    => x_errmsg,
6688         x_return_status             => x_return_status);
6689     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6690     THEN
6691         RETURN;
6692     END IF;
6693     validate_ship_to_customer_name(
6694         x_errmsg                    => x_errmsg,
6695         x_return_status             => x_return_status);
6696     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6697     THEN
6698         RETURN;
6699     END IF;
6700 
6701     validate_ship_to_cust_number(
6702         x_errmsg                    => x_errmsg,
6703         x_return_status             => x_return_status);
6704     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6705     THEN
6706         RETURN;
6707     END IF;
6708 
6709     validate_ship_to_customer_id(
6710         x_errmsg                    => x_errmsg,
6711         x_return_status             => x_return_status);
6712     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6713     THEN
6714         RETURN;
6715     END IF;
6716 
6717 
6718     validate_ship_to_site_use_id(
6719         x_errmsg                    => x_errmsg,
6720         x_return_status             => x_return_status);
6721     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6722     THEN
6723         RETURN;
6724     END IF;
6725 
6726     validate_ship_to_location(
6727         x_errmsg                    => x_errmsg,
6728         x_return_status             => x_return_status);
6729     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6730     THEN
6731         RETURN;
6732     END IF;
6733 
6734     validate_ship_to_address(
6735         x_errmsg                    => x_errmsg,
6736         x_return_status             => x_return_status);
6737     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6738     THEN
6739         RETURN;
6740     END IF;
6741 
6742     validate_ship_to_contact_id(
6743         x_errmsg                    => x_errmsg,
6744         x_return_status             => x_return_status);
6745     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6746     THEN
6747         RETURN;
6748     END IF;
6749 
6750     populate_exchange_rate (
6751         p_trx_system_parameters_rec      =>  p_trx_system_param_rec,
6752         p_trx_profile_rec            => p_trx_profile_rec,
6753         x_errmsg                    => x_errmsg,
6754         x_return_status             => x_return_status);
6755     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6756     THEN
6757         RETURN;
6758     END IF;
6759 
6760     populate_territory (
6761          p_trx_system_param_rec      => p_trx_system_param_rec,
6762          x_errmsg                    => x_errmsg,
6763          x_return_status             => x_return_status);
6764 
6765     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6766     THEN
6767         RETURN;
6768     END IF;
6769 
6770     validate_territory_id (
6771         x_errmsg                    => x_errmsg,
6772         x_return_status             => x_return_status);
6773     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6774     THEN
6775         RETURN;
6776     END IF;
6777 
6778     populate_payment_method (
6779          x_errmsg                    => x_errmsg,
6780          x_return_status             => x_return_status);
6781 
6782     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6783     THEN
6784         RETURN;
6785     END IF;
6786 
6787    /*8622438*/
6788 
6789     IF (ar_late_charge_pkg.g_invoice_api_called_from_lc = 'Y' AND
6790         nvl(p_trx_system_param_rec.default_rct_mthd_for_lc_inv,'N') = 'Y' )
6791        OR
6792        (ar_late_charge_pkg.g_invoice_api_called_from_lc = 'N')
6793        THEN
6794          populate_default_pay_method (
6795          x_errmsg                    => x_errmsg,
6796          x_return_status             => x_return_status);
6797 
6798     END IF;
6799 
6800     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6801     THEN
6802         RETURN;
6803     END IF;
6804 
6805 
6806     validate_payment_method (
6807       x_errmsg         => x_errmsg,
6808       x_return_status  => x_return_status);
6809     IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
6810       RETURN;
6811     END IF;
6812 
6813     /* SEPA changes */
6814     validate_mandate_flag (
6815       x_errmsg         => x_errmsg,
6816       x_return_status  => x_return_status);
6817     IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
6818       RETURN;
6819     END IF;
6820 
6821     /* 4188835 - Default legal_entity_id */
6822     populate_legal_entity (
6823          x_errmsg                    => x_errmsg,
6824          x_return_status             => x_return_status);
6825     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6826     THEN
6827         RETURN;
6828     END IF;
6829 
6830     /* 4188835 - validate legal entity */
6831     validate_legal_entity(
6832         x_errmsg                    => x_errmsg,
6833         x_return_status             => x_return_status);
6834     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6835     THEN
6836         RETURN;
6837     END IF;
6838 
6839     IF pg_debug = 'Y'
6840     THEN
6841         debug ('AR_INVOICE_UTILS.validate_desc_flex for header (+)' );
6842     END IF;
6843 
6844     ar_invoice_validate_flex.validate_desc_flex (
6845         p_validation_type   => 'HEADER',
6846         x_errmsg            => x_errmsg,
6847         x_return_status     => x_return_status);
6848     IF pg_debug = 'Y'
6849     THEN
6850         debug ('AR_INVOICE_UTILS.validate_desc_flex for header (-)' );
6851         debug ('AR_INVOICE_UTILS.validate_header (-)' );
6852     END IF;
6853 
6854 /*4369585-4589309*/
6855  populate_printing_pending(
6856         x_errmsg                    => x_errmsg,
6857         x_return_status             => x_return_status);
6858     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6859     THEN
6860         RETURN;
6861     END IF;
6862 
6863 END validate_header;
6864 
6865 
6866 PROCEDURE validate_lines (
6867     x_errmsg                    OUT NOCOPY  VARCHAR2,
6868     x_return_status             OUT NOCOPY  VARCHAR2 ) IS
6869 
6870 BEGIN
6871     IF pg_debug = 'Y'
6872     THEN
6873         debug ('AR_INVOICE_UTILS.validate_lines (+)' );
6874     END IF;
6875 
6876     x_return_status := FND_API.G_RET_STS_SUCCESS;
6877 
6878     populate_line_attributes(
6879         x_errmsg                    => x_errmsg,
6880         x_return_status             => x_return_status);
6881     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6882     THEN
6883         RETURN;
6884     END IF;
6885 
6886     validate_line_integrity(
6887         x_errmsg                    => x_errmsg,
6888         x_return_status             => x_return_status);
6889     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6890     THEN
6891         RETURN;
6892     END IF;
6893 
6894     validate_tax_freight_lines (
6895         x_errmsg                    => x_errmsg,
6896         x_return_status             => x_return_status);
6897     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6898     THEN
6899         RETURN;
6900     END IF;
6901     validate_item_kflex (
6902         x_errmsg                    => x_errmsg,
6903         x_return_status             => x_return_status);
6904     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6905     THEN
6906         RETURN;
6907     END IF;
6908     validate_line_description(
6909         x_errmsg                    => x_errmsg,
6910         x_return_status             => x_return_status);
6911     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6912     THEN
6913         RETURN;
6914     END IF;
6915     validate_quantity_invoiced(
6916         x_errmsg                    => x_errmsg,
6917         x_return_status             => x_return_status);
6918     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6919     THEN
6920         RETURN;
6921     END IF;
6922     validate_unit_selling_price(
6923         x_errmsg                    => x_errmsg,
6924         x_return_status             => x_return_status);
6925     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6926     THEN
6927         RETURN;
6928     END IF;
6929     validate_accounting_rules(
6930         x_errmsg                    => x_errmsg,
6931         x_return_status             => x_return_status);
6932     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6933     THEN
6934         RETURN;
6935     END IF;
6936     validate_freight(
6937         x_errmsg                    => x_errmsg,
6938         x_return_status             => x_return_status);
6939     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6940     THEN
6941         RETURN;
6942     END IF;
6943     -- populate extended and revenue amount
6944     --ar_invoice_default_pvt.get_extended_amount;
6945 
6946     validate_line_type(
6947         x_errmsg                    => x_errmsg,
6948         x_return_status             => x_return_status);
6949     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6950     THEN
6951         RETURN;
6952     END IF;
6953     check_dup_line_number(
6954         x_errmsg                    => x_errmsg,
6955         x_return_status             => x_return_status);
6956     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6957     THEN
6958         RETURN;
6959     END IF;
6960 
6961     validate_warehouse_id (
6962         x_errmsg                    => x_errmsg,
6963         x_return_status             => x_return_status);
6964     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6965     THEN
6966         RETURN;
6967     END IF;
6968 
6969     validate_uom_code(
6970         x_errmsg                    => x_errmsg,
6971         x_return_status             => x_return_status);
6972     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6973     THEN
6974         RETURN;
6975     END IF;
6976 
6977     /* eTax now handles these validations, no need to call this
6978        routine
6979     validate_tax_code (
6980     x_errmsg                    => x_errmsg,
6981     x_return_status             => x_return_status);
6982     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6983     THEN
6984         RETURN;
6985     END IF;
6986     */
6987 
6988     validate_tax_exemption(
6989         x_errmsg                    => x_errmsg,
6990         x_return_status             => x_return_status);
6991     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
6992     THEN
6993         RETURN;
6994     END IF;
6995 
6996     validate_more_tab (
6997         x_errmsg                    => x_errmsg,
6998         x_return_status             => x_return_status);
6999     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
7000     THEN
7001         RETURN;
7002     END IF;
7003 
7004     validate_line_ussgl_code  (
7005         x_errmsg                    => x_errmsg,
7006         x_return_status             => x_return_status);
7007 
7008     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
7009     THEN
7010         RETURN;
7011     END IF;
7012 
7013     IF pg_debug = 'Y'
7014     THEN
7015         debug ('AR_INVOICE_UTILS.validate_desc_flex for lines (+)' );
7016     END IF;
7017 
7018     ar_invoice_validate_flex.validate_desc_flex (
7019         p_validation_type   => 'LINES',
7020         x_errmsg            => x_errmsg,
7021         x_return_status     => x_return_status);
7022     IF pg_debug = 'Y'
7023     THEN
7024         debug ('AR_INVOICE_UTILS.validate_desc_flex for lines (-)' );
7025         debug ('AR_INVOICE_UTILS.validate_lines (-)' );
7026     END IF;
7027 
7028 END validate_lines;
7029 
7030 PROCEDURE validate_accounting_flex (
7031      x_errmsg                    OUT NOCOPY  VARCHAR2,
7032      x_return_status             OUT NOCOPY  VARCHAR2 ) IS
7033 
7034 CURSOR Cccid IS
7035     SELECT code_combination_id, trx_dist_id, trx_line_id, trx_header_id
7036     FROM   ar_trx_dist_gt
7037     WHERE  code_combination_id IS NOT NULL;
7038 BEGIN
7039     -- first validate the ccid, if user has passed any ccid
7040     IF pg_debug = 'Y'
7041     THEN
7042         debug ('AR_INVOICE_UTILS.validate_accounting_flex (+)' );
7043     END IF;
7044     x_return_status := FND_API.G_RET_STS_SUCCESS;
7045     FOR CccidRec IN Cccid
7046     LOOP
7047         IF NOT fnd_flex_keyval.validate_ccid (
7048                 appl_short_name     => 'SQLGL',
7049                 key_flex_code       => 'GL#',
7050                 structure_number    => ARP_GLOBAL.chart_of_accounts_id,
7051                 combination_id      => CccidRec.code_combination_id )
7052         THEN
7053             INSERT INTO ar_trx_errors_gt
7054             (   trx_header_id,
7055                 trx_line_id,
7056                 trx_dist_id,
7057                 error_message,
7058                 invalid_value)
7059                 VALUES
7060                 ( CccidRec.trx_header_id,
7061                   CccidRec.trx_line_id,
7062                   CccidRec.trx_dist_id,
7063                   arp_standard.fnd_message('AR_INVALID_CCID'),
7064                   CccidRec.code_combination_id);
7065         END IF;
7066     END LOOP;
7067     IF pg_debug = 'Y'
7068     THEN
7069         debug ('AR_INVOICE_UTILS.validate_accounting_flex (-)' );
7070     END IF;
7071 
7072     EXCEPTION
7073         WHEN OTHERS THEN
7074             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_accounting_flex '||sqlerrm;
7075             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7076             RETURN;
7077 END;
7078 
7079 PROCEDURE validate_distributions (
7080     p_trx_system_parameters_rec AR_INVOICE_DEFAULT_PVT.trx_system_parameters_rec_type,
7081     x_errmsg                    OUT NOCOPY  VARCHAR2,
7082     x_return_status             OUT NOCOPY  VARCHAR2 ) IS
7083 
7084     l_precision                 fnd_currencies.precision%type;
7085     l_min_acc_unit              fnd_currencies.minimum_accountable_unit%type;
7086  -- bug 6429861
7087 	    l_per  number := 1;
7088 	    l_amt  number := 1;
7089 
7090 
7091 BEGIN
7092     IF pg_debug = 'Y'
7093     THEN
7094         debug ('AR_INVOICE_UTILS.validate_distributions (+)' );
7095         debug ('Percentage Is null for Account Class: REC (+)' );
7096     END IF;
7097 
7098     x_return_status := FND_API.G_RET_STS_SUCCESS;
7099 
7100     l_precision := p_trx_system_parameters_rec.precision;
7101     l_min_acc_unit :=
7102             p_trx_system_parameters_rec.MINIMUM_ACCOUNTABLE_UNIT;
7103 
7104     INSERT INTO ar_trx_errors_gt
7105             (   trx_header_id,
7106                 trx_line_id,
7107                 trx_dist_id,
7108                 error_message)
7109      SELECT  d.trx_header_id,
7110              d.trx_line_id,
7111              D.trx_DIST_ID,
7112              arp_standard.fnd_message('AR_INAPI_CCID_NULL')
7113      FROM   ar_trx_DIST_gt D
7114      WHERE  code_combination_id IS NULL;
7115     -- the user can pass either amount or percent. If both amount and percent
7116     -- is passed then amount will take precendence.
7117 
7118     --First check whether user passed either amounts or percent.
7119     INSERT INTO ar_trx_errors_gt
7120             (   trx_header_id,
7121                 trx_line_id,
7122                 trx_dist_id,
7123                 error_message)
7124      SELECT  d.trx_header_id,
7125              d.trx_line_id,
7126              D.trx_DIST_ID,
7127              arp_standard.fnd_message('AR_INAPI_AMT_PER_REQUIRED')
7128      FROM   ar_trx_DIST_gt D
7129      WHERE  d.PERCENT IS NULL
7130       AND   d.amount IS NULL;
7131 
7132     --If accounting rule id is passed
7133     -- then all the amounts must be passed as percentage. All the amounts will be
7134     -- ignored.
7135     -- first check if account _class is 'REC' and if any accounting rule is passed
7136     -- then only percent is  allowed. Amount is not allowed.
7137 
7138     INSERT INTO ar_trx_errors_gt
7139             (   trx_header_id,
7140                 trx_line_id,
7141                 trx_dist_id,
7142                 error_message)
7143      SELECT  d.trx_header_id,
7144              L.trx_line_id,
7145              D.trx_DIST_ID,
7146              arp_standard.fnd_message('AR_INAPI_ONLY_PER_ALLOWED')
7147         FROM   ar_trx_DIST_gt D,
7148                ar_trx_lines_GT L
7149         WHERE  (L.ACCOUNTING_RULE_ID IS NOT NULL
7150                 OR D.ACCOUNT_CLASS = 'REC')
7151         AND    d.PERCENT IS NULL
7152         --AND    L.trx_line_ID = D.trx_line_ID;
7153         AND    L.trx_header_ID = D.trx_header_ID;
7154 
7155      IF pg_debug = 'Y'
7156     THEN
7157         debug ('Percentage Is null for Account Class: REC (-)' );
7158     END IF;
7159 
7160 
7161 
7162     -- check whether line type and account class matches
7163 
7164      INSERT INTO ar_trx_errors_gt
7165         (trx_header_id,
7166          trx_line_id,
7167          trx_dist_ID,
7168          error_message)
7169         SELECT d.trx_header_id,
7170                L.trx_LINE_ID,
7171                D.trx_DIST_ID,
7172                arp_standard.fnd_message('AR_INAPI_INAVLID_LINE_TYPE')
7173         FROM  ar_trx_dist_gt D,
7174               ar_trx_LINES_GT L
7175         WHERE  L.trx_LINE_ID = D.trx_LINE_ID
7176         AND    DECODE(D.ACCOUNT_CLASS,
7177                           'TAX',    'TAX',
7178                           'FREIGHT','FREIGHT',
7179                           'CHARGES','CHARGES',
7180                                     'LINE')
7181                 <> L.LINE_TYPE;
7182 
7183     /*------------------------------------------------------------+
7184      | Update distribution lines with rounded amount              |
7185      | for account class: 'REV', 'TAX', 'FREIGHT', 'CHARGES'      |
7186      | For lines w/o accounting rules.                            |
7187     +------------------------------------------------------------*/
7188 
7189     UPDATE ar_trx_dist_gt D
7190         SET AMOUNT =
7191         (
7192          SELECT
7193          DECODE(C.MINIMUM_ACCOUNTABLE_UNIT,
7194                 NULL, ROUND(((D.PERCENT * L.extended_AMOUNT) / 100), C.PRECISION),
7195                 ROUND(((D.PERCENT * L.extended_AMOUNT) / 100) /
7196                       C.MINIMUM_ACCOUNTABLE_UNIT) *
7197                   C.MINIMUM_ACCOUNTABLE_UNIT
7198                 )
7199           FROM   FND_CURRENCIES C,
7200                  ar_trx_LINES_GT L
7201           WHERE  L.CURRENCY_CODE = C.CURRENCY_CODE
7202           AND    L.trx_LINE_ID = D.trx_LINE_ID
7203         )
7204         WHERE  ACCOUNT_CLASS IN ('REV', 'TAX', 'FREIGHT','CHARGES')
7205         AND   D.PERCENT IS NOT NULL
7206         AND   d.amount IS NULL
7207         AND   EXISTS
7208              (SELECT 'X'
7209               FROM   ar_trx_LINES_GT L
7210               WHERE  L.trx_LINE_ID = D.trx_LINE_ID
7211               AND    L.ACCOUNTING_RULE_ID IS NULL);
7212 
7213              -- bug 6429861
7214 	     if(sql%rowcount=0) then
7215  	         l_amt :=0;
7216  	       end if;
7217 
7218     /*-----------------------------------------------------------+
7219      |  Calculate percents from amounts in GL Distributions for  |
7220      |  account classes REV, TAX, FREIGHT, CHARGES.              |
7221      |  If the line amount is 0, then percent is set to 0        |
7222      |  Precision = 4 digits to be consistent with the Invoice   |
7223      |              Entry form.                                  |
7224      |  For lines w/o accounting rules only.                     |
7225      +-----------------------------------------------------------*/
7226 
7227       UPDATE ar_trx_dist_gt D
7228         SET d.PERCENT =
7229         (
7230           SELECT DECODE(L.extended_AMOUNT,
7231                         0, 0,   /* set percent =0 if line amt = 0 */
7232                         ROUND(100 * (D.AMOUNT / L.extended_AMOUNT), 4))
7233           FROM   FND_CURRENCIES C,
7234                  ar_trx_LINES_GT L
7235           WHERE  L.CURRENCY_CODE = C.CURRENCY_CODE
7236           AND    L.trx_LINE_ID = D.trx_LINE_ID
7237         )
7238         WHERE ACCOUNT_CLASS in ('REV', 'TAX', 'FREIGHT', 'CHARGES')
7239         AND   D.AMOUNT IS NOT NULL
7240         AND   d.percent IS NULL
7241         AND   EXISTS
7242              (SELECT 'X'
7243               FROM   ar_trx_LINES_GT L
7244               WHERE  L.trx_LINE_ID = D.trx_LINE_ID
7245               AND    L.ACCOUNTING_RULE_ID IS NULL);
7246 
7247 	      -- bug 6429861
7248 	      if(sql%rowcount=0) then
7249  	         l_per :=0;
7250  	       end if;
7251 
7252     /* Bug-5532061 Begin*/
7253     /*------------------------------------------------------------+
7254      | Update distribution lines with rounded acctd_amount        |
7255      | for account class: 'REV', 'TAX', 'FREIGHT', 'CHARGES'      |
7256      | For lines w/o accounting rules.                            |
7257     +------------------------------------------------------------*/
7258 
7259       UPDATE ar_trx_dist_gt D
7260         SET ACCTD_AMOUNT =
7261         (
7262          SELECT
7263          DECODE(C.MINIMUM_ACCOUNTABLE_UNIT,
7264                 NULL, ROUND(((D.PERCENT * L.extended_AMOUNT * nvl(h.exchange_rate,1)) / 100), C.PRECISION),
7265                 ROUND(((D.PERCENT * L.extended_AMOUNT * nvl(h.exchange_rate,1)) / 100) /
7266                       C.MINIMUM_ACCOUNTABLE_UNIT) *
7267                   C.MINIMUM_ACCOUNTABLE_UNIT
7268                 )
7269           FROM   FND_CURRENCIES C,
7270                  ar_trx_LINES_GT L,
7271                  ar_trx_header_gt h
7272           WHERE  C.CURRENCY_CODE = p_trx_system_parameters_rec.base_currency_code
7273           AND    L.trx_LINE_ID = D.trx_LINE_ID
7274           AND    L.trx_header_id = h.trx_header_id
7275         )
7276         WHERE  ACCOUNT_CLASS IN ('REV', 'TAX', 'FREIGHT','CHARGES')
7277         AND   d.acctd_amount IS NULL
7278         AND   D.PERCENT IS NOT NULL
7279         AND   EXISTS
7280              (SELECT 'X'
7281               FROM   ar_trx_LINES_GT L
7282               WHERE  L.trx_LINE_ID = D.trx_LINE_ID
7283               AND    L.ACCOUNTING_RULE_ID IS NULL);
7284 
7285      /* Bug-5340103 End*/
7286 
7287      /*---------------------------------------------------------------+
7288      | Requirements for total amount of the gl distribution by        |
7289      | account class:                                                 |
7290      | REV must equal the amount at the line of line type LINE        |
7291      | TAX must equal the amount at the line of line type TAX         |
7292      | FREIGHT must equal the amount at the line of line type FREIGHT |
7293      | CHARGES must equal the amount at the line of line type CHARGES |
7294      +----------------------------------------------------------------*/
7295      /* 4652982 - fixed message name */
7296      INSERT INTO ar_trx_errors_gt
7297             (   trx_header_id,
7298                 trx_line_id,
7299                 error_message,
7300                 invalid_value)
7301         SELECT l.trx_header_id,
7302                L.trx_LINE_ID,
7303                arp_standard.fnd_message('AR_INAPI_NVALID_SUM_DIST_AMT'),
7304                d.account_class || ':'||SUM(d.amount)
7305         FROM   ar_trx_DIST_gt D,
7306                ar_trx_lines_GT L
7307         WHERE  L.ACCOUNTING_RULE_ID IS NULL
7308         AND    L.LINE_TYPE = DECODE(D.ACCOUNT_CLASS,
7309                                     'REV', 'LINE',
7310                                     'TAX', 'TAX',
7311                                     'FREIGHT', 'FREIGHT',
7312                                     'CHARGES', 'CHARGES',
7313                                     'INVALID_TYPE')
7314         AND    L.trx_LINE_ID = D.trx_LINE_ID
7315         GROUP BY l.trx_header_id,
7316                  L.trx_LINE_ID,
7317                  L.LINE_TYPE,
7318                  L.extended_AMOUNT,
7319                  D.ACCOUNT_CLASS
7320         HAVING   L.extended_AMOUNT <> SUM(D.AMOUNT);
7321 
7322 	--bug 6429861
7323 	if(sql%rowcount=0 and l_per=0 and l_amt=0) then
7324 
7325      /*-----------------------------------------------------------------+
7326          | Percent should total to 100 for each account class              |
7327          +-----------------------------------------------------------------*/
7328     INSERT INTO ar_trx_errors_gt
7329         (trx_header_id,
7330          trx_LINE_ID,
7331          error_message,
7332          invalid_value)
7333         SELECT d.trx_header_id,
7334                d.trx_line_ID,
7335                arp_standard.fnd_message('AR_INAPI_100_PERCENT'),
7336                sum(d.percent)
7337         FROM   ar_trx_dist_gt d, ar_trx_lines_gt L, ar_trx_header_gt h
7338         WHERE  d.trx_line_id = l.trx_line_id
7339         AND    l.trx_header_id = h.trx_header_id
7340         GROUP BY d.trx_header_id,d.trx_line_ID, ACCOUNT_CLASS
7341         HAVING   SUM(d.PERCENT) <> 100;
7342 
7343      /*-----------------------------------------------------------------+
7344          | Percent should total to 100 for REC class in case it is passed |
7345          +-----------------------------------------------------------------*/
7346     INSERT INTO ar_trx_errors_gt
7347         (trx_header_id,
7348          error_message,
7349          invalid_value)
7350         SELECT d.trx_header_id,
7351                arp_standard.fnd_message('AR_INAPI_100_PERCENT'),
7352                sum(d.percent)
7353         FROM   ar_trx_dist_gt d
7354         WHERE  d.account_class = 'REC'
7355         GROUP BY d.trx_header_id
7356         HAVING   SUM(d.PERCENT) <> 100;
7357 
7358 	end if;
7359 
7360     /*********************************************************************
7361     ** The following checks are for  percent if the distribution is passed
7362     ** percent instead of Amount.
7363     ***********************************************************************/
7364 
7365     -- This is temporary code. First need to find out whether user will send
7366     -- the line  amount or percent.
7367 
7368     /* INSERT INTO RA_INTERFACE_ERRORS
7369         (INTERFACE_LINE_ID,
7370          INTERFACE_DISTRIBUTION_ID,
7371          MESSAGE_TEXT,
7372          INVALID_VALUE)
7373         SELECT INTERFACE_LINE_ID,
7374                INTERFACE_DISTRIBUTION_ID,
7375                DECODE(PERCENT, NULL, :error_message, :message2),
7376                NVL(TO_CHAR(PERCENT), :nil_message)
7377         FROM   RA_INTERFACE_DISTRIBUTIONS
7378         WHERE  REQUEST_ID = :request_id
7379         AND    ( PERCENT IS NULL
7380                 OR
7381                  (NVL(PERCENT, 0) - ROUND(NVL(PERCENT,0), :pct_precision)) <> 0
7382                 ); */
7383 
7384     -- update accounting set flag
7385     UPDATE ar_trx_dist_gt dgt
7386         SET dgt.account_set_flag =
7387                     (SELECT DECODE(hgt.invoicing_rule_id,null,'N','Y')
7388                       FROM ar_trx_header_gt hgt, ar_trx_lines_gt lgt
7389                       WHERE  hgt.trx_header_id = lgt.trx_header_id
7390                       AND    lgt.trx_line_id = dgt.trx_line_id
7391                       AND    dgt.account_class <> 'REC'
7392                       UNION
7393                       SELECT DECODE(h.invoicing_rule_id,null,'N','Y')
7394                       FROM ar_trx_header_gt h
7395                       WHERE h.trx_header_id = dgt.trx_header_id
7396                       AND   dgt.account_class = 'REC');
7397 
7398      -- Now update the amount
7399     /* UPDATE ar_trx_dist_gt D
7400      set (amount,acctd_amount) =
7401         (Select DECODE(D.ACCOUNT_CLASS,
7402                    'REC', NULL,
7403                    'TAX', DECODE(NVL(PREV_TRX.INVOICING_RULE_ID,
7404                             NVL(L_PARENT.INVOICING_RULE_ID,
7405                             L.INVOICING_RULE_ID)), NULL,
7406                             DECODE(D.AMOUNT, NULL,
7407                             DECODE(L.TAX_PRECEDENCE, NULL,
7408                               DECODE(L_PARENT.QUANTITY *
7409                                 L_PARENT.UNIT_SELLING_PRICE, NULL,
7410                               DECODE(l_min_acc_unit, NULL,
7411                                      ROUND(NVL(l_parent.amount,0) *
7412                                            (NVL(l.tax_rate, 0)/100),
7413                                            l_precision),
7414                                      ROUND(NVL(l_parent.amount,0) *
7415                                            (NVL(l.tax_rate, 0)/100)/
7416                                            l_min_acc_unit) *
7417                                            l_min_acc_unit),
7418                               DECODE(l_min_acc_unit, NULL,
7419                                      ROUND(l_parent.quantity *
7420                                            l_parent.unit_selling_price *
7421                                            (NVL(l.tax_rate, 0)/100),
7422                                            l_precision),
7423                                      ROUND(l_parent.quantity *
7424                                      l_parent.unit_selling_price *
7425                                            (nvl(l.tax_rate, 0)/100)/
7426                                            l_min_acc_unit) *
7427                                            l_min_acc_unit)),0),
7428                                     D.AMOUNT), NULL ),
7429                             DECODE(NVL(PREV_TRX.INVOICING_RULE_ID,
7430                                    NVL(L_PARENT.INVOICING_RULE_ID,
7431                                     L.INVOICING_RULE_ID)),
7432                                     NULL, D.AMOUNT,
7433                                     NULL)), -- amount
7434                             DECODE(D.ACCOUNT_CLASS,
7435                             'REC', null,
7436                             'TAX', DECODE(NVL(PREV_TRX.INVOICING_RULE_ID,
7437                               NVL(L_PARENT.INVOICING_RULE_ID,
7438                                   L.INVOICING_RULE_ID)),
7439                           NULL, DECODE(D.ACCTD_AMOUNT, NULL,
7440                                        DECODE(L.CURRENCY_CODE,
7441                                               G.CURRENCY_CODE,
7442                        DECODE(D.AMOUNT, NULL, DECODE(L.TAX_PRECEDENCE, NULL,
7443                          DECODE(L_PARENT.QUANTITY * L_PARENT.UNIT_SELLING_PRICE,
7444                               NULL, DECODE(l_min_acc_unit, NULL,
7445                                       ROUND(NVL(l_parent.amount,0) *
7446                                            (NVL(l.tax_rate, 0)/100), l_precision)
7447 ,
7448                                       ROUND(NVL(l_parent.amount,0) *
7449                                            (NVL(l.tax_rate, 0)/100)/
7450                                            l_min_acc_unit) * l_min_acc_unit),
7451                                     DECODE(l_min_acc_unit, NULL,
7452                                       ROUND(l_parent.quantity *
7453                                             l_parent.unit_selling_price *
7454                                             (NVL(l.tax_rate, 0)/100),l_precision)
7455 ,
7456                                       ROUND(l_parent.quantity *
7457                                        l_parent.unit_selling_price *
7458                                             (nvl(l.tax_rate, 0)/100)/
7459                                             l_min_acc_unit) * l_min_acc_unit)),0),
7460                        D.amount) ,
7461                                               DECODE(l_min_acc_unit,
7462                                                      NULL,
7463                                                      ROUND(
7464                        DECODE(D.AMOUNT, NULL, DECODE(L.TAX_PRECEDENCE, NULL,
7465                          DECODE(L_PARENT.QUANTITY * L_PARENT.UNIT_SELLING_PRICE,
7466                               NULL, DECODE(l_min_acc_unit, NULL,
7467                                       ROUND(NVL(l_parent.amount,0) *
7468                                            (NVL(l.tax_rate, 0)/100), l_precision)
7469 ,
7470                                       ROUND(NVL(l_parent.amount,0) *
7471                                            (NVL(l.tax_rate, 0)/100)/
7472                                            l_min_acc_unit) * l_min_acc_unit),
7473                                     DECODE(l_min_acc_unit, NULL,
7474                                       ROUND(l_parent.quantity *
7475                                             l_parent.unit_selling_price *
7476                                             (NVL(l.tax_rate, 0)/100),l_precision)
7477 ,
7478                                       ROUND(l_parent.quantity *
7479                                             l_parent.unit_selling_price * --
7480                                             (nvl(l.tax_rate, 0)/100)/
7481                                             l_min_acc_unit) * l_min_acc_unit)),0),
7482                        D.amount) * h.exchange_rate , l_precision ), --L.CONVERSION_RATE, l_precision,
7483                                                      ROUND(
7484                        DECODE(D.AMOUNT, NULL, DECODE(L.TAX_PRECEDENCE, NULL,
7485                          DECODE(L_PARENT.QUANTITY * L_PARENT.UNIT_SELLING_PRICE,
7486                               NULL, DECODE(l_min_acc_unit, NULL,
7487                                       ROUND(NVL(l_parent.amount,0) *
7488                                            (NVL(l.tax_rate, 0)/100), l_precision)
7489 ,
7490                                       ROUND(NVL(l_parent.amount,0) *
7491                                            (NVL(l.tax_rate, 0)/100)/
7492                                            l_min_acc_unit) * l_min_acc_unit),
7493                                     DECODE(l_min_acc_unit, NULL,
7494                                       ROUND(l_parent.quantity *
7495                                             l_parent.unit_selling_price *
7496                                             (NVL(l.tax_rate, 0)/100),l_precision)
7497 ,
7498                                       ROUND(l_parent.quantity *
7499                                             l_parent.unit_selling_price *
7500                                             (nvl(l.tax_rate, 0)/100)/
7501                                             l_min_acc_unit) * l_min_acc_unit)),0),
7502                        D.amount) * h.exchange_rate / l_min_acc_unit ) * --L.CONVERSION_RATE / l_min_acc_unit *
7503                        l_min_acc_unit)),
7504                                        D.ACCTD_AMOUNT), NULL),
7505                    DECODE(NVL(PREV_TRX.INVOICING_RULE_ID,
7506                               NVL(L_PARENT.INVOICING_RULE_ID,
7507                                   L.INVOICING_RULE_ID)),
7508                           NULL, DECODE(D.ACCTD_AMOUNT, NULL,
7509                                        DECODE(L.CURRENCY_CODE,
7510                                               G.CURRENCY_CODE, D.AMOUNT,
7511                                               DECODE(l_min_acc_unit,
7512                                                      NULL,
7513                                                      ROUND(D.AMOUNT *
7514                                                            h.exchange_rate , --L.CONVERSION_RATE,
7515                                                            l_precision),
7516                                                      ROUND(D.AMOUNT *
7517                                                            h.exchange_rate ,--L.CONVERSION_RATE /
7518                                                       l_min_acc_unit) *
7519                                                      l_min_acc_unit)),
7520                                        D.ACCTD_AMOUNT),
7521                           NULL))
7522                           FROM   ar_trx_DIST_gt D,
7523                                  ar_trx_header_gt h
7524                                  --RA_CUST_TRX_TYPES TYPE,
7525                                  FND_CURRENCIES C,
7526                                  GL_SETS_OF_BOOKS G,
7527                                  --RA_CUSTOMER_TRX PREV_TRX,
7528                                  ar_trx_LINES_GT L_PARENT,
7529                                  ar_trx_LINES_GT L
7530                           WHERE  --L.REQUEST_ID = :request_id
7531                                  --L.PREVIOUS_CUSTOMER_TRX_ID = PREV_TRX.CUSTOMER_TRX_ID(+)
7532                           --AND    L.CUST_TRX_TYPE_ID = TYPE.CUST_TRX_TYPE_ID
7533                                  h.trx_header_id = l_trx_header_id
7534                           AND    L.CURRENCY_CODE = C.CURRENCY_CODE
7535                           AND    L.SET_OF_BOOKS_ID = G.SET_OF_BOOKS_ID
7536                           AND    L.CUSTOMER_TRX_ID IS NOT NULL
7537                           AND    L.LINK_TO_cust_trx_LINE_ID = L_PARENT.trx_LINE_ID (+)
7538                           AND    L.trx_LINE_ID = D.trx_LINE_ID
7539                           AND    D.ACCOUNT_CLASS = 'TAX'); */
7540 
7541     validate_accounting_flex (
7542         x_errmsg        => x_errmsg,
7543         x_return_status => x_return_status );
7544     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
7545     THEN
7546          RETURN;
7547     END IF;
7548 
7549     -- validate desc flex
7550 
7551     ar_invoice_validate_flex.validate_desc_flex (
7552         p_validation_type   => 'DISTRIBUTIONS',
7553         x_errmsg            => x_errmsg,
7554         x_return_status     => x_return_status);
7555 
7556     IF pg_debug = 'Y'
7557     THEN
7558         debug ('AR_INVOICE_UTILS.validate_desc_flex for distributions(-)' );
7559     END IF;
7560 
7561     IF pg_debug = 'Y'
7562     THEN
7563         debug ('AR_INVOICE_UTILS.validate_distributions (-)' );
7564     END IF;
7565 
7566     /*4673387*/
7567     UPDATE ar_trx_dist_gt
7568        SET gl_date = NULL
7569        WHERE trx_header_id IN  (SELECT trx_header_id
7570                                     FROM ar_trx_header_gt gt,
7571                                          ra_cust_trx_types ctt
7572                                     WHERE ctt.cust_trx_type_id = gt.cust_trx_type_id
7573                                     AND   ctt.post_to_gl = 'N');
7574 
7575     EXCEPTION
7576         WHEN OTHERS THEN
7577             x_errmsg := 'Error in AR_INVOICE_UTILS.validate_distributions '||sqlerrm;
7578             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7579             RETURN;
7580 END validate_distributions;
7581 
7582 
7583 PROCEDURE validate_salescredits (
7584   p_trx_system_param_rec ar_invoice_default_pvt.trx_system_parameters_rec_type,
7585   x_errmsg               OUT NOCOPY  VARCHAR2,
7586   x_return_status        OUT NOCOPY  VARCHAR2 ) IS
7587 
7588     pct_precision       NUMBER := 4;
7589     CURSOR cRounding IS
7590         SELECT trx_line_id, (100 - SUM(revenue_percent_split)) rounding_error,
7591                MAX(SC.trx_SALESCREDIT_ID) max_trx_salescredit_id
7592         FROM   ar_trx_SALESCREDITS_gt SC,
7593                SO_SALES_CREDIT_TYPES CR
7594         WHERE  SC.SALES_CREDIT_TYPE_ID = CR.SALES_CREDIT_TYPE_ID
7595         AND    CR.QUOTA_FLAG = 'Y'
7596         GROUP BY trx_LINE_ID
7597         HAVING SUM(revenue_percent_split) <> 100;
7598 
7599      CURSOR   cRoundingAmt IS
7600      SELECT  l.trx_line_id, MIN(L.trx_LINE_ID) min_trx_line_id,
7601                 MIN(DECODE(L.QUANTITY_invoiced * L.UNIT_SELLING_PRICE,
7602                            NULL, L.extended_AMOUNT,
7603                            DECODE(C.MINIMUM_ACCOUNTABLE_UNIT,
7604                                   NULL,
7605                                   ROUND(L.quantity_invoiced * L.UNIT_SELLING_PRICE,
7606                                         C.PRECISION),
7607                                   ROUND(L.quantity_invoiced *
7608                                         L.UNIT_SELLING_PRICE/
7609                                         C.MINIMUM_ACCOUNTABLE_UNIT)
7610                                   * C.MINIMUM_ACCOUNTABLE_UNIT
7611                                   )
7612                            )
7613                     ) line_amount,
7614                 MAX(SC.trx_SALESCREDIT_ID) max_trx_salescredit_id,
7615                 SUM(revenue_amount_split) sales_credit_amount
7616         FROM    FND_CURRENCIES C,
7617                 ar_trx_SALESCREDITS_gt SC,
7618                 SO_SALES_CREDIT_TYPES CR,
7619                 ar_trx_LINES_GT L
7620         WHERE   L.LINE_TYPE = 'LINE'
7621         AND     L.CURRENCY_CODE = C.CURRENCY_CODE
7622         and     L.trx_LINE_ID = SC.trx_LINE_ID
7623         and     SC.SALES_CREDIT_TYPE_ID = CR.SALES_CREDIT_TYPE_ID
7624         AND     CR.QUOTA_FLAG = 'Y'
7625         GROUP BY L.trx_LINE_ID;
7626 
7627   l_message_name VARCHAR2(30);
7628 
7629 BEGIN
7630     IF pg_debug = 'Y'
7631     THEN
7632         debug ('AR_INVOICE_UTILS.validate_salescredits (+)' );
7633     END IF;
7634 
7635     x_return_status := FND_API.G_RET_STS_SUCCESS;
7636 
7637     l_message_name := gl_public_sector.get_message_name (
7638       p_message_name => 'AR_INAPI_INVALID_SALESREP_ID',
7639       p_app_short_name => 'AR');
7640 
7641     -- validate  salesrep_id
7642     INSERT INTO ar_trx_errors_gt
7643         (trx_header_id,
7644          trx_LINE_ID,
7645          trx_salescredit_id,
7646          error_MESSAGE,
7647          INVALID_VALUE)
7648         SELECT trx_header_id,
7649                trx_LINE_ID,
7650                trx_SALESCREDIT_ID,
7651                arp_standard.fnd_message(l_message_name),
7652                sc.salesrep_id
7653         FROM   ar_trx_salescredits_gt SC
7654         WHERE  salesrep_id IS NOT NULL
7655         AND    NOT EXISTS
7656               (SELECT 'X'
7657                FROM   RA_SALESREPS REP
7658                WHERE  REP.SALESREP_ID = SC.SALESREP_ID);
7659 
7660     l_message_name := gl_public_sector.get_message_name (
7661       p_message_name => 'AR_INAPI_NVALID_SALESREP_NUM',
7662       p_app_short_name => 'AR');
7663 
7664     -- validate salesrep number
7665      INSERT INTO ar_trx_errors_gt
7666         (trx_header_id,
7667          trx_LINE_ID,
7668          trx_salescredit_id,
7669          error_MESSAGE,
7670          INVALID_VALUE)
7671         SELECT trx_header_id,
7672                trx_LINE_ID,
7673                trx_salescredit_id,
7674                arp_standard.fnd_message(l_message_name),
7675                sc.salesrep_num
7676         FROM   ar_trx_salescredits_gt SC
7677         WHERE  sc.salesrep_num IS NOT NULL
7678         AND    NOT EXISTS
7679               (SELECT 'X'
7680                FROM   RA_SALESREPS REP
7681                WHERE  REP.SALESREP_NUMber = SC.SALESREP_NUM);
7682 
7683        UPDATE ar_trx_salescredits_gt SC
7684         SET    SALESREP_ID = (SELECT SALESREP_ID
7685                               FROM   RA_SALESREPS REP
7686                               WHERE  REP.SALESREP_NUMBER = SC.SALESREP_NUM)
7687         WHERE  salesrep_id IS NULL;
7688 
7689     l_message_name := gl_public_sector.get_message_name (
7690       p_message_name => 'AR_INAPI_INVALID_SCR_TYPE_ID',
7691       p_app_short_name => 'AR');
7692 
7693     -- validate sales credit type ID
7694     INSERT INTO ar_trx_errors_gt
7695         (trx_header_id,
7696          trx_LINE_ID,
7697          trx_salescredit_id,
7698          error_MESSAGE,
7699          INVALID_VALUE)
7700         SELECT trx_header_id,
7701                trx_LINE_ID,
7702                trx_SALESCREDIT_ID,
7703                arp_standard.fnd_message(l_message_name),
7704                sc.sales_credit_type_id
7705         FROM   ar_trx_salescredits_gt SC
7706         WHERE  sales_credit_type_id IS NOT NULL
7707         AND    NOT EXISTS
7708               (SELECT 'X'
7709                FROM   SO_SALES_CREDIT_TYPES CR
7710                WHERE  CR.SALES_CREDIT_TYPE_ID = SC.SALES_CREDIT_TYPE_ID);
7711 
7712     l_message_name := gl_public_sector.get_message_name (
7713       p_message_name => 'AR_INAPI_INVLD_SCR_TYPE_NAME',
7714       p_app_short_name => 'AR');
7715 
7716     -- validate sales credit type name
7717     INSERT INTO ar_trx_errors_gt
7718         (trx_header_id,
7719          trx_LINE_ID,
7720          trx_salescredit_id,
7721          error_MESSAGE,
7722          INVALID_VALUE)
7723         SELECT trx_header_id,
7724                trx_line_id,
7725                trx_salescredit_id,
7726                arp_standard.fnd_message(l_message_name),
7727                sc.sales_credit_type_name
7728         FROM   ar_trx_salescredits_gt SC
7729         WHERE  sales_credit_type_name IS NOT NULL
7730         AND    NOT EXISTS
7731               (SELECT 'X'
7732                FROM   SO_SALES_CREDIT_TYPES CR
7733                WHERE  CR.NAME = SC.SALES_CREDIT_TYPE_NAME);
7734 
7735     -- update the sales credit type id
7736     UPDATE ar_trx_salescredits_gt SC
7737     SET    SALES_CREDIT_TYPE_ID =
7738                       (SELECT SALES_CREDIT_TYPE_ID
7739                        FROM   SO_SALES_CREDIT_TYPES CR
7740                        WHERE  CR.NAME = SC.SALES_CREDIT_TYPE_NAME)
7741          WHERE  SALES_CREDIT_TYPE_ID IS NULL;
7742 
7743     l_message_name := gl_public_sector.get_message_name (
7744       p_message_name => 'AR_INAPI_SCR_AMT_PERCENT_REQ',
7745       p_app_short_name => 'AR');
7746 
7747     -- validate either amount or percent has been passed by user
7748      INSERT INTO ar_trx_errors_gt
7749         (trx_header_id,
7750          trx_LINE_ID,
7751          trx_salescredit_id,
7752          error_MESSAGE)
7753          SELECT trx_header_id,
7754                 trx_LINE_ID,
7755                 trx_SALESCREDIT_ID,
7756                 arp_standard.fnd_message(l_message_name)
7757          FROM   ar_trx_salescredits_gt SC,
7758                so_sales_credit_types CR				 --Bug8202014
7759         WHERE  sc.sales_credit_type_id = cr.sales_credit_type_id --Bug8202014
7760         AND  revenue_amount_split IS NULL
7761         AND  revenue_percent_split IS NULL
7762         AND  cr.quota_flag = 'Y';	--Bug8202014
7763 
7764 
7765     l_message_name := gl_public_sector.get_message_name (
7766       p_message_name => 'AR_INAPI_INVALID_SCR',
7767       p_app_short_name => 'AR');
7768 
7769     /*--------------------------------------------------------------+
7770      | Verify that sales credit assignments are for line type LINE  |
7771      +--------------------------------------------------------------*/
7772    INSERT INTO ar_trx_errors_gt
7773         (trx_header_id,
7774          trx_LINE_ID,
7775          trx_salescredit_id,
7776          error_MESSAGE,
7777          INVALID_VALUE)
7778     SELECT S.trx_header_id,
7779            S.trx_LINE_ID,
7780            S.trx_SALESCREDIT_ID,
7781            arp_standard.fnd_message(l_message_name),
7782            L.LINE_TYPE
7783     FROM   ar_trx_LINES_GT L,
7784            ar_trx_SALESCREDITS_gt S
7785     WHERE  S.trx_LINE_ID = L.trx_LINE_ID
7786     AND    L.LINE_TYPE <> 'LINE';
7787 
7788 
7789 
7790     -- Depending on the amount passed update the percent split
7791     UPDATE ar_trx_SALESCREDITS_gt S1
7792         SET revenue_percent_split =
7793         (
7794           SELECT DECODE(DECODE(L.QUANTITY_invoiced * L.UNIT_SELLING_PRICE,
7795                                NULL, L.extended_AMOUNT,
7796                                DECODE(C.MINIMUM_ACCOUNTABLE_UNIT,
7797                                       NULL, ROUND(QUANTITY_invoiced *
7798                                                   UNIT_SELLING_PRICE,
7799                                                   C.PRECISION),
7800                                       ROUND(L.QUANTITY_invoiced *
7801                                             L.UNIT_SELLING_PRICE/
7802                                             C.MINIMUM_ACCOUNTABLE_UNIT)
7803                                       * C.MINIMUM_ACCOUNTABLE_UNIT
7804                                       )
7805                                ),
7806                         0, 0,
7807                         ROUND(S2.revenue_amount_split * 100 /
7808                               DECODE(L.QUANTITY_invoiced * L.UNIT_SELLING_PRICE,
7809                                      NULL, L.extended_AMOUNT,
7810                                      DECODE(C.MINIMUM_ACCOUNTABLE_UNIT,
7811                                             NULL, ROUND(QUANTITY_invoiced *
7812                                                         UNIT_SELLING_PRICE,
7813                                                         C.PRECISION),
7814                                             ROUND(L.QUANTITY_invoiced *
7815                                                   L.UNIT_SELLING_PRICE/
7816                                                   C.MINIMUM_ACCOUNTABLE_UNIT)
7817                                             * C.MINIMUM_ACCOUNTABLE_UNIT
7818                                             )
7819                                      ),
7820                               pct_precision))
7821         FROM   ar_trx_LINES_GT L,
7822                 FND_CURRENCIES C,
7823                 ar_trx_SALESCREDITS_gt S2
7824         WHERE  L.trx_LINE_ID = S2.trx_LINE_ID
7825               AND    L.LINE_TYPE = 'LINE'
7826             AND    L.CURRENCY_CODE = C.CURRENCY_CODE
7827             AND    S2.ROWID = S1.ROWID
7828             )
7829         WHERE  S1.revenue_amount_split IS NOT NULL
7830         AND    S1.revenue_percent_split IS NULL;
7831 
7832 
7833     -- update amount split if percent has been passed
7834     UPDATE ar_trx_salescredits_gt SC
7835         SET    revenue_amount_split =
7836               (SELECT DECODE(C.MINIMUM_ACCOUNTABLE_UNIT,
7837                              NULL,
7838                              ROUND(SC.revenue_percent_split *
7839                                    DECODE(L.QUANTITY_invoiced * L.UNIT_SELLING_PRICE,
7840                                           NULL, L.extended_AMOUNT,
7841                                           DECODE(C.MINIMUM_ACCOUNTABLE_UNIT,
7842                                                  NULL,
7843                                                  ROUND(L.QUANTITY_invoiced *
7844                                                        L.UNIT_SELLING_PRICE,
7845                                                        C.PRECISION),
7846                                                  ROUND(L.QUANTITY_invoiced *
7847                                                        L.UNIT_SELLING_PRICE/
7848                                                     C.MINIMUM_ACCOUNTABLE_UNIT)
7849                                                  * C.MINIMUM_ACCOUNTABLE_UNIT
7850                                                  )
7851                                           ) / 100,
7852                                    C.PRECISION),
7853                              ROUND((SC.revenue_percent_split *
7854                                     DECODE(L.QUANTITY_invoiced *
7855                                            L.UNIT_SELLING_PRICE,
7856                                            NULL, L.extended_amount,
7857                                            DECODE(C.MINIMUM_ACCOUNTABLE_UNIT,
7858                                                   NULL,
7859                                                   ROUND(L.QUANTITY_invoiced *
7860                                                         L.UNIT_SELLING_PRICE,
7861                                                         C.PRECISION),
7862                                                   ROUND(L.QUANTITY_invoiced *
7863                                                         L.UNIT_SELLING_PRICE/
7864                                                     C.MINIMUM_ACCOUNTABLE_UNIT)
7865                                                   * C.MINIMUM_ACCOUNTABLE_UNIT
7866                                                   )
7867                                            ) / 100) /
7868                                    C.MINIMUM_ACCOUNTABLE_UNIT)
7869                              * C.MINIMUM_ACCOUNTABLE_UNIT
7870                              )
7871                FROM   ar_trx_LINES_GT L,
7872                       FND_CURRENCIES C
7873                WHERE  L.trx_LINE_ID = SC.trx_LINE_ID
7874                AND    L.CURRENCY_CODE = C.CURRENCY_CODE)
7875         WHERE  SC.revenue_percent_split IS NOT NULL
7876         AND    SC.revenue_amount_split IS NULL;
7877 
7878         /******************************************************************************
7879          Bug8202014 Changes Start Here
7880          If non_revenue_percent_split is passed, calculate the non_revenue_amount_split
7881          If non_revenue_amount_split is passed, calculate the non_revenue_percent_split
7882          Added the following 2 updated statements for this.
7883          *******************************************************************************/
7884 
7885     -- update non_revenue_percent_split if non_revenue_amount_split has been passed
7886     UPDATE ar_trx_SALESCREDITS_gt S1
7887         SET non_revenue_percent_split =
7888         (
7889           SELECT DECODE(DECODE(L.QUANTITY_invoiced * L.UNIT_SELLING_PRICE,
7890                                NULL, L.extended_AMOUNT,
7891                                DECODE(C.MINIMUM_ACCOUNTABLE_UNIT,
7892                                       NULL, ROUND(QUANTITY_invoiced *
7893                                                   UNIT_SELLING_PRICE,
7894                                                   C.PRECISION),
7895                                       ROUND(L.QUANTITY_invoiced *
7896                                             L.UNIT_SELLING_PRICE/
7897                                             C.MINIMUM_ACCOUNTABLE_UNIT)
7898                                       * C.MINIMUM_ACCOUNTABLE_UNIT
7899                                       )
7900                                ),
7901                         0, 0,
7902                         ROUND(S2.non_revenue_amount_split * 100 /
7903                               DECODE(L.QUANTITY_invoiced * L.UNIT_SELLING_PRICE,
7904                                      NULL, L.extended_AMOUNT,
7905                                      DECODE(C.MINIMUM_ACCOUNTABLE_UNIT,
7906                                             NULL, ROUND(QUANTITY_invoiced *
7907                                                         UNIT_SELLING_PRICE,
7908                                                         C.PRECISION),
7909                                             ROUND(L.QUANTITY_invoiced *
7910                                                   L.UNIT_SELLING_PRICE/
7911                                                   C.MINIMUM_ACCOUNTABLE_UNIT)
7912                                             * C.MINIMUM_ACCOUNTABLE_UNIT
7913                                             )
7914                                      ),
7915                               pct_precision))
7916         FROM   ar_trx_LINES_GT L,
7917                 FND_CURRENCIES C,
7918                 ar_trx_SALESCREDITS_gt S2
7919         WHERE  L.trx_LINE_ID = S2.trx_LINE_ID
7920               AND    L.LINE_TYPE = 'LINE'
7921             AND    L.CURRENCY_CODE = C.CURRENCY_CODE
7922             AND    S2.ROWID = S1.ROWID
7923             )
7924         WHERE  S1.non_revenue_amount_split IS NOT NULL
7925         AND    S1.non_revenue_percent_split IS NULL;
7926 
7927 
7928     -- update non_revenue_amount_split if non_revenue_percent_split has been passed
7929     UPDATE ar_trx_salescredits_gt SC
7930         SET    non_revenue_amount_split =
7931               (SELECT DECODE(C.MINIMUM_ACCOUNTABLE_UNIT,
7932                              NULL,
7933                              ROUND(SC.non_revenue_percent_split *
7934                                    DECODE(L.QUANTITY_invoiced * L.UNIT_SELLING_PRICE,
7935                                           NULL, L.extended_AMOUNT,
7936                                           DECODE(C.MINIMUM_ACCOUNTABLE_UNIT,
7937                                                  NULL,
7938                                                  ROUND(L.QUANTITY_invoiced *
7939                                                        L.UNIT_SELLING_PRICE,
7940                                                        C.PRECISION),
7941                                                  ROUND(L.QUANTITY_invoiced *
7942                                                        L.UNIT_SELLING_PRICE/
7943                                                     C.MINIMUM_ACCOUNTABLE_UNIT)
7944                                                  * C.MINIMUM_ACCOUNTABLE_UNIT
7945                                                  )
7946                                           ) / 100,
7947                                    C.PRECISION),
7948                              ROUND((SC.non_revenue_percent_split *
7949                                     DECODE(L.QUANTITY_invoiced *
7950                                            L.UNIT_SELLING_PRICE,
7951                                            NULL, L.extended_amount,
7952                                            DECODE(C.MINIMUM_ACCOUNTABLE_UNIT,
7953                                                   NULL,
7954                                                   ROUND(L.QUANTITY_invoiced *
7955                                                         L.UNIT_SELLING_PRICE,
7956                                                         C.PRECISION),
7957                                                   ROUND(L.QUANTITY_invoiced *
7958                                                         L.UNIT_SELLING_PRICE/
7959                                                     C.MINIMUM_ACCOUNTABLE_UNIT)
7960                                                   * C.MINIMUM_ACCOUNTABLE_UNIT
7961                                                   )
7962                                            ) / 100) /
7963                                    C.MINIMUM_ACCOUNTABLE_UNIT)
7964                              * C.MINIMUM_ACCOUNTABLE_UNIT
7965                              )
7966                FROM   ar_trx_LINES_GT L,
7967                       FND_CURRENCIES C
7968                WHERE  L.trx_LINE_ID = SC.trx_LINE_ID
7969                AND    L.CURRENCY_CODE = C.CURRENCY_CODE)
7970         WHERE  SC.non_revenue_percent_split IS NOT NULL
7971         AND    SC.non_revenue_amount_split IS NULL;
7972 
7973         /*********************************************************
7974          Bug8202014 Changes End Here
7975          ********************************************************/
7976 
7977     -- fix rounding error if any in percent
7978     FOR cRoundingRec IN cRounding
7979     LOOP
7980         UPDATE ar_trx_SALESCREDITS_gt
7981         SET    revenue_percent_split = revenue_percent_split + cRoundingRec.rounding_error
7982         WHERE  trx_SALESCREDIT_ID = cRoundingRec.max_trx_salescredit_id;
7983     END LOOP;
7984 
7985     -- fix rounding error in amount if any
7986     For cRoundingAmtRec IN cRoundingAmt
7987     LOOP
7988          UPDATE ar_trx_SALESCREDITS_gt
7989          SET    revenue_amount_split = revenue_amount_split +
7990                                           (cRoundingAmtRec.line_amount -
7991                                             cRoundingAmtRec.sales_credit_amount)
7992             WHERE   trx_SALESCREDIT_ID = cRoundingAmtRec.max_trx_salescredit_id;
7993 
7994 
7995     END LOOP;
7996 
7997     -- validate amount precision
7998      INSERT INTO ar_trx_errors_gt
7999         (trx_header_id,
8000          trx_LINE_ID,
8001          trx_salescredit_id,
8002          error_MESSAGE,
8003          invalid_value)
8004      SELECT S.trx_header_id,
8005             S.trx_LINE_ID,
8006             S.trx_SALESCREDIT_ID,
8007             arp_standard.fnd_message('AR_INAPI_INVALID_PRECISION'),
8008             S.revenue_amount_split
8009      FROM   ar_trx_salescredits_gt  S,
8010             ar_trx_LINES_GT L,
8011             FND_CURRENCIES C
8012      WHERE  S.trx_LINE_ID = L.trx_LINE_ID
8013      AND    L.CURRENCY_CODE = C.CURRENCY_CODE
8014      AND    s.revenue_amount_split IS NOT NULL
8015      GROUP BY S.trx_header_id, S.trx_LINE_ID,
8016                   S.trx_SALESCREDIT_ID,
8017                   S.revenue_amount_split,
8018                   C.MINIMUM_ACCOUNTABLE_UNIT,
8019                   C.PRECISION
8020      HAVING DECODE(C.MINIMUM_ACCOUNTABLE_UNIT,
8021                        NULL, ROUND(S.revenue_amount_split,
8022                                    C.PRECISION),
8023                        ROUND(S.revenue_amount_split /
8024                              C.MINIMUM_ACCOUNTABLE_UNIT) *
8025                        C.MINIMUM_ACCOUNTABLE_UNIT) -
8026                            S.revenue_amount_split <> 0;
8027 
8028     /*------------------------------------------------------------+
8029          | Identify null or invalid sales credit percent split        |
8030          +------------------------------------------------------------*/
8031 
8032     l_message_name := gl_public_sector.get_message_name (
8033       p_message_name => 'AR_INAPI_INVALID_PERCENT_SPLIT',
8034       p_app_short_name => 'AR');
8035 
8036         INSERT INTO ar_trx_errors_gt
8037         (trx_header_id,
8038          trx_LINE_ID,
8039          trx_salescredit_id,
8040          error_MESSAGE,
8041          invalid_value)
8042         SELECT SC.trx_header_id,
8043                SC.trx_LINE_ID,
8044                SC.trx_SALESCREDIT_ID,
8045                arp_standard.fnd_message(l_message_name),
8046                SC.revenue_percent_split
8047         FROM   ar_trx_salescredits_gt SC,
8048                SO_SALES_CREDIT_TYPES SCT
8049         WHERE  SC.SALES_CREDIT_TYPE_ID = SCT.SALES_CREDIT_TYPE_ID
8050      	AND    SCT.QUOTA_FLAG = 'Y'					--Bug8202014
8051         AND    ( SC.revenue_percent_split IS NULL
8052                 OR
8053                  (NVL(SC.revenue_percent_split, 0) -
8054                   ROUND(NVL(SC.revenue_percent_split, 0),
8055                         pct_precision) <> 0)
8056                 );
8057 
8058         /*------------------------------------------------------------+
8059          | Identify un-foot sales credit                              |
8060          +------------------------------------------------------------*/
8061         INSERT INTO ar_trx_errors_gt
8062         (trx_header_id,
8063          trx_line_id,
8064          error_MESSAGE)
8065         SELECT SC.trx_header_id,
8066                SC.trx_LINE_ID,
8067                arp_standard.fnd_message(l_message_name)
8068         FROM
8069                ar_trx_salescredits_gt SC,
8070                SO_SALES_CREDIT_TYPES CR
8071         WHERE  SC.SALES_CREDIT_TYPE_ID = CR.SALES_CREDIT_TYPE_ID
8072         AND    cr.quota_flag = 'Y'					--Bug8202014
8073         GROUP BY
8074                sc.trx_header_id, sc.trx_LINE_ID
8075         HAVING
8076                 SUM(DECODE(CR.QUOTA_FLAG,
8077                            'Y',  SC.revenue_percent_split,
8078                            'N', 0,
8079                            NULL, 0
8080                            ) )  <> 100;
8081 
8082     l_message_name := gl_public_sector.get_message_name (
8083       p_message_name => 'AR_INAPI_SUM_NOT_EQUAL_LINE',
8084       p_app_short_name => 'AR');
8085 
8086     -- identify un-foot sales credit for amount
8087     -- Sum of sales credit does not equal to line amount
8088     INSERT INTO ar_trx_errors_gt
8089         (trx_header_id,
8090          trx_line_id,
8091          error_message,
8092          invalid_value)
8093          SELECT s.trx_header_id,
8094                 s.trx_line_id,
8095                 arp_standard.fnd_message(l_message_name),
8096                 SUM(S.revenue_amount_split)
8097          FROM   ar_trx_salescredits_gt s,
8098                 so_sales_credit_types t
8099          WHERE  s.sales_credit_type_id = t.sales_credit_type_id
8100          AND    s.revenue_amount_split IS NOT NULL
8101          AND    t.quota_flag = 'Y'
8102          GROUP BY S.trx_header_id, S.trx_line_id
8103          HAVING SUM(S.revenue_amount_split) <>
8104            (SELECT DECODE(l.quantity_invoiced * l.unit_selling_price,
8105                           NULL, l.extended_amount,
8106                           DECODE(c.minimum_accountable_unit, NULL,
8107                             ROUND(quantity_invoiced * unit_selling_price,
8108                                   c.precision),
8109                              ROUND(l.quantity_invoiced * l.unit_selling_price
8110                                / c.minimum_accountable_unit) *
8111                                c.minimum_accountable_unit))
8112             FROM  ar_trx_lines_gt l,
8113                   fnd_currencies c
8114             WHERE l.trx_line_id = s.trx_line_id
8115             AND   l.line_type = 'LINE'
8116             AND   l.currency_code = c.currency_code);
8117 
8118 
8119     -- if the require salesrep option is checked in the system options form,
8120     -- then salescredit information must be passed.
8121     --
8122 
8123     IF (p_trx_system_param_rec.salesrep_required_flag = 'Y') THEN
8124 
8125       l_message_name := gl_public_sector.get_message_name (
8126         p_message_name => 'AR_INAPI_ONE_SALESCREDIT_ROW',
8127         p_app_short_name => 'AR');
8128 
8129       INSERT INTO ar_trx_errors_gt
8130         (trx_header_id,
8131          trx_line_id,
8132          error_message)
8133        SELECT lgt.trx_header_id,
8134               lgt.trx_line_id,
8135               arp_standard.fnd_message(l_message_name)
8136        FROM   ar_trx_lines_gt lgt
8137        WHERE  lgt.line_type = 'LINE'
8138        AND    NOT EXISTS
8139          (SELECT 'X'
8140           FROM   ar_trx_salescredits_gt scgt,
8141                  so_sales_credit_types type
8142           WHERE  scgt.trx_header_id = lgt.trx_header_id
8143           AND    scgt.trx_line_id   = lgt.trx_line_id
8144           AND    scgt.sales_credit_type_id = type.sales_credit_type_id
8145           AND    type.quota_flag = 'Y' );
8146 
8147     END IF;
8148 
8149     IF pg_debug = 'Y'
8150     THEN
8151         debug ('AR_INVOICE_UTILS.validate_salescredits (-)' );
8152     END IF;
8153 
8154     -- validate desc flex
8155     ar_invoice_validate_flex.validate_desc_flex (
8156         p_validation_type   => 'SALESREPS',
8157         x_errmsg            => x_errmsg,
8158         x_return_status     => x_return_status);
8159 
8160     IF pg_debug = 'Y'
8161     THEN
8162         debug ('AR_INVOICE_UTILS.validate_desc_flex in Salesereps(-)' );
8163     END IF;
8164 
8165     EXCEPTION
8166       WHEN OTHERS THEN
8167         x_errmsg := 'AR_INVOICE_UTILS.validate_salescredits '||sqlerrm;
8168         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8169         RETURN;
8170 
8171 END validate_salescredits;
8172 
8173 END AR_INVOICE_UTILS;