DBA Data[Home] [Help]

PACKAGE BODY: APPS.AR_INVOICE_UTILS

Source


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