DBA Data[Home] [Help]

PACKAGE BODY: APPS.ARP_CMERGE_ARCUS

Source


4 g_count		           NUMBER := 0;
1 PACKAGE BODY arp_cmerge_arcus AS
2 /*$Header: ARHCMGNB.pls 120.52.12020000.3 2013/03/01 08:42:47 vsegu ship $*/
3 
5 
6 --migrating contacts and contact points
7 procedure do_merge_contacts (
8         p_level                      VARCHAR2,
9         p_from_account_id            NUMBER,
10         p_org_party_id               NUMBER,
11         p_org_party_rel_id           NUMBER,
12         p_org_contact_id             NUMBER,
16 
13         p_to_party_id                NUMBER,
14         x_org_party_id           OUT NOCOPY NUMBER,
15         p_to_party_site_id           NUMBER DEFAULT NULL );
17 --account level migrating org contacts
18 procedure do_cust_merge_contacts (
19         p_from_party_id              NUMBER,
20         p_to_party_id                NUMBER,
21         p_from_account_id            NUMBER,
22         p_to_account_id              NUMBER
23 );
24 
25 --site level migrating org contacts
26 procedure do_site_merge_contacts (
27         p_from_party_id              NUMBER,
28         p_to_party_id                NUMBER,
29         p_from_account_id            NUMBER,
30         p_to_account_id              NUMBER,
31         p_req_id                     NUMBER,
32         p_set_num                    NUMBER
33 );
34 
35 --migrating contact points
36 procedure do_copy_contact_points (
37         p_owner_table_name           VARCHAR2,
38         p_from_id                    NUMBER,
39         p_to_id                      NUMBER,
40         p_from_account_id            NUMBER
41 );
42 
43 --check duplicate org contact
44 function check_org_contact_dup (
45         p_from_org_contact_id        NUMBER,
46         p_from_party_rel_id          NUMBER,
47         p_to_party_id                NUMBER,
48         x_org_contact_id        OUT NOCOPY  NUMBER ,
49         p_from_account_id            NUMBER)
50 return VARCHAR2;
51 
52 --check duplicate contact point
53 function check_contact_point_dup (
54         p_from_contact_point_id      NUMBER,
55         p_to_owner_table_id          NUMBER,
56         x_contact_point_id      OUT NOCOPY  NUMBER )
57 return VARCHAR2;
58 
59 --account level migrating contact points
60 procedure do_cust_merge_cpoint (
61         p_from_party_id              NUMBER,
62         p_to_party_id                NUMBER,
63         p_from_account_id            NUMBER,
64         p_to_account_id              NUMBER
65 );
66 
67 --site level migrating contact points
68 procedure do_site_merge_cpoint (
69         p_from_party_site_id         NUMBER,
70         p_to_party_site_id           NUMBER,
71         p_from_account_id            NUMBER,
72         p_to_account_id              NUMBER
73 );
74 
75 --migrate contacts
76 procedure copy_contacts (
77         req_id                       NUMBER,
78         set_num                      NUMBER,
79         process_mode                 VARCHAR2
80 );
81 
82 --migrate contacts in sites
83 procedure copy_contacts_in_sites (
84         req_id                       NUMBER,
85         set_num                      NUMBER,
86         process_mode                 VARCHAR2
87 );
88 procedure ra_bill_to_location (
89         req_id                       NUMBER,
90         set_num                      NUMBER,
91         process_mode                 VARCHAR2
92 );
93 --merge account site uses.
94 procedure ra_su (
95         req_id                       NUMBER,
96         set_num                      NUMBER,
97         process_mode                 VARCHAR2
98 );
99 
100 --merge account sites.
101 procedure ra_addr (
102         req_id                       NUMBER,
103         set_num                      NUMBER,
104         process_mode                 VARCHAR2
105 );
106 
107 --merge accounts.
108 procedure ra_cust (
109         req_id                       NUMBER,
110         set_num                      NUMBER,
111         process_mode                 VARCHAR2
112 );
113 
114 --merge customer account roles.
115 procedure ra_cont (
116         req_id                       NUMBER,
117         set_num                      NUMBER,
118         process_mode                 VARCHAR2
119 );
120 
121 --merge account relate.
122 procedure ra_cr (
123         req_id                       NUMBER,
124         set_num                      NUMBER,
125         process_mode                 VARCHAR2
126 );
127 
128 --merge customer receipt methods.
129 procedure ra_crm (
130         req_id                       NUMBER,
131         set_num                      NUMBER,
132         process_mode                 VARCHAR2
133 );
134 
135 --merge customer contact points.
136 procedure ra_ph (
137         req_id                       NUMBER,
138         set_num                      NUMBER,
139         process_mode                 VARCHAR2
140 );
141 
142 --Inactivate CUSTOMER usages.
143 procedure ra_usg (
144         req_id                       NUMBER,
145         set_num                      NUMBER,
146         process_mode                 VARCHAR2
147 );
148 
149 /*===========================================================================+
150  | PROCEDURE
151  |              merge
152  |
153  | DESCRIPTION
154  |              main merge routine.
155  |
156  | SCOPE - PUBLIC
157  |
158  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED
159  |
160  | ARGUMENTS  : IN:
161  |                    req_id
162  |                    set_num
163  |                    process_mod
164  |          IN/ OUT:
165  |
166  | RETURNS    : NONE
167  |
168  | NOTES
169  |
170  | MODIFICATION HISTORY
171  |     Jianying Huang  26-OCT-00  Bug 1415529: call copy_contacts before
172  |                        ra_addr and ra_cust. Otherwise, ra_cont and
173  |                        ra_ph throw 'no data found' exception when
174  |                        they try to use account id get party id or
175  |                        use account site id get party site id because
179  |                        delete rows till the end of merge, we can move
176  |                        account site id has been deleted by ra_addr
177  |                        and account id has been deleted by ra_cust.
178  |     Jianying Huang  17-DEC-00  Bug 1535542: Since we will not physically
180  |                        the call of 'copy_contacts' right before we migrate
181  |                        org contacts and contact points.
182  |
183  +===========================================================================*/
184 
185 PROCEDURE merge (
186           req_id                NUMBER,
187           set_num               NUMBER,
188           process_mode          VARCHAR2
189 ) IS
190 
191 BEGIN
192 
193     arp_message.set_line( 'ARP_CMERGE_ARCUS.MERGE()+' );
194     --merge account site uses
195     ra_su( req_id, set_num, process_mode );
196 
197     --merge account sites
198     ra_addr( req_id, set_num, process_mode );
199 
200     --merge accounts
201     ra_cust( req_id, set_num, process_mode );
202 
203     --merge account relate
204     ra_cr( req_id, set_num, process_mode );
205 
206     -- For bill_to_location
207     ra_bill_to_location(req_id, set_num, process_mode );
208 
209     --merge customer receipt methods
210     ra_crm( req_id, set_num, process_mode );
211 
212     --4307679 Inactivate usages 'CUSTOMER'
213     ra_usg(req_id, set_num, process_mode );
214 
215 --because of bug 1535542, we will not physically delete rows in
216 --account tables until merge is done for one set.
217 
218 /**N/A
219 --Bug 1415529: call copy_contacts before ra_addr and ra_cust.
220 --Otherwise, ra_cont and ra_ph throw 'no data found' exception
221 --when they try to use account id get party id or use
222 --account site id get party site id because account site id
223 --has been deleted by ra_addr and account id has been deleted
224 --by ra_cust .
225 **/
226 
227     --should not be run in 'LOCK' mood
228     IF ( process_mode <> 'LOCK' )
229     THEN
230        --migrate contact points for org contact and account contacts.
231        copy_contacts( req_id, set_num, process_mode );
232 
233        --migrate contact points in site level.
234        copy_contacts_in_sites( req_id, set_num, process_mode );
235     END IF;
236 
237     --merge org contact
238     ra_cont( req_id, set_num, process_mode );
239 
240     --the procedure ra_ph is not being called anymore because table
241     --hz_cust_contact_points has been obsoleted.
242     --merge contact points: phone
243     --ra_ph( req_id, set_num, process_mode );
244 
245     arp_message.set_line( 'ARP_CMERGE_ARCUS.MERGE()-' );
246 
247 END merge;
248 /*===========================================================================+
249  | PROCEDURE
250  |              ra_bill_to_location
251  |
252  | DESCRIPTION
253  |              merge in HZ_CUST_SITE_USES.
254  |
255  | SCOPE - PRIVATE
256  |
257  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED
258  |
259  | ARGUMENTS  : IN:
260  |                    req_id
261  |                    set_num
262  |                    process_mod
263  |          IN/ OUT:
264  |
265  | RETURNS    : NONE
266  |
267  | NOTES
268  |
269  | MODIFICATION HISTORY
270  |     P.Suresh         03-25-02              Bug No : 2272750 Created.
271  |     P.Suresh         04-05-02              Bug No : 2272750. Modified the
272  |                                            update statements for better
273  |                                            performance.
274  +===========================================================================*/
275  PROCEDURE ra_bill_to_location (
276           req_id                NUMBER,
277           set_num               NUMBER,
278           process_mode          VARCHAR2
279 ) IS
280 
281     CURSOR c1 IS
282         SELECT merge.duplicate_site_id,merge.customer_site_id,
283                cust.bill_to_site_use_id,merge.duplicate_site_code,
284                merge.customer_createsame,cust.org_id --SSUptake
285         FROM hz_cust_site_uses_all cust, ra_customer_merges merge --SSUptake
286         WHERE merge.request_id = req_id
287         AND   merge.set_number = set_num
288 	AND   merge.process_flag = 'N'
289 	AND   cust.site_use_id = merge.duplicate_site_id
290         AND   cust.org_id      = merge.org_id    --SSUptake
291         FOR UPDATE NOWAIT;
292    l_dup_site_id           NUMBER;
293    l_cust_site_id          NUMBER;
294    l_site_use_code         VARCHAR2(30);
295    l_bill_to_site_use_id      NUMBER;
296    l_create_same_site      VARCHAR2(30);
297    l_ra_bill_to_site_use_id   NUMBER;
298 
299    l_org_id                NUMBER(15);
300 
301 BEGIN
302 
303     arp_message.set_line( 'ARP_CMERGE_ARCUS.RA_BILL_TO_LOCATION()+' );
304  OPEN c1;
305     LOOP
306         FETCH c1 INTO l_dup_site_id,l_cust_site_id,l_bill_to_site_use_id,
307                       l_site_use_code,l_create_same_site,l_org_id; --SSUptake
308        EXIT WHEN c1%NOTFOUND;
309 
310       IF l_site_use_code = 'SHIP_TO' and l_create_same_site = 'Y' THEN
311          IF l_bill_to_site_use_id IS NOT NULL THEN
312              BEGIN
313                   select customer_site_id into l_ra_bill_to_site_use_id
314                   from   ra_customer_merges
315                   where  duplicate_site_id  = l_bill_to_site_use_id
316                   and    process_flag = 'N'
317                   and    request_id   = req_id
318                   and    set_number   = set_num;
319              EXCEPTION
320                   WHEN NO_DATA_FOUND THEN
321                        l_ra_bill_to_site_use_id := l_bill_to_site_use_id;
322              END;
323              update  hz_cust_site_uses_all --SSUptake
324              set     bill_to_site_use_id = l_ra_bill_to_site_use_id
325              where   site_use_id = l_cust_site_id
326 	     and     org_id      = l_org_id; --SSUptake
327          END IF;
328       ELSIF  l_site_use_code = 'BILL_TO' THEN
329              update  hz_cust_site_uses_all --SSUptake
330              set     bill_to_site_use_id = l_cust_site_id
331 	     where   org_id = l_org_id --SSUptake
332              and     site_use_id in (
333                           SELECT site_use_id
334                           FROM   hz_cust_site_uses_all su, --SSUptake
335                                  hz_cust_acct_sites_all site --SSUptake
336                           WHERE su.org_id         = l_org_id --SSUptake
337 			  AND   su.org_id         = site.org_id --SSUptake
338                           AND   site.cust_acct_site_id = su.cust_acct_site_id
339                           AND   su.site_use_code='SHIP_TO'
340                           AND   su.bill_to_site_use_id = l_dup_site_id
341 			  AND   site.cust_account_id in (
342                                   SELECT unique(customer_id)
343                                   FROM ra_customer_merges merge
344                                   WHERE merge.process_flag = 'N'
345                                   and merge.request_id = req_id
346                                   and merge.set_number = set_num
347 				  and merge.org_id     = site.org_id --SSUptake
348 				  UNION
349 				  SELECT related_cust_account_id
350 				  FROM   hz_cust_acct_relate_all rel --SSUptake
351 				  WHERE  rel.org_id = l_org_id --SSUptake
352 				  AND    rel.cust_account_id in (
353 					 select unique(customer_id)
354 					 from ra_customer_merges merge
355 					 where merge.process_flag = 'N'
356 					 and merge.request_id = req_id
357 					 and merge.set_number = set_num
358 					 and merge.org_id     = rel.org_id )  --SSUptake
359                                 )
360 		     );
361       END IF;
362 
363 
364   END LOOP;
365  CLOSE c1;
366     arp_message.set_line( 'ARP_CMERGE_ARCUS.RA_BILL_TO_LOCATION()-' );
367 END;
368 /*===========================================================================+
369  | PROCEDURE
370  |              ra_su
371  |
372  | DESCRIPTION
373  |              merge in HZ_CUST_SITE_USES.
374  |
375  | SCOPE - PRIVATE
376  |
377  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED
378  |
379  | ARGUMENTS  : IN:
380  |                    req_id
381  |                    set_num
382  |                    process_mod
383  |          IN/ OUT:
384  |
385  | RETURNS    : NONE
386  |
387  | NOTES
388  |
389  | MODIFICATION HISTORY
390  |     Jianying Huang  25-OCT-00  Modified cursor c2 and c4.
391  |                        Since we only allow merging active sites uses,
392  |                        the cursors always returned 'no row'.
393  |     Jianying Huang  12-DEC-00  Remove cursor c1 and c3. They are sub-cursor
394  |                        of c2 and c4.
395  |     Jianying Huang  20-DEC-00  Bug 1535542: Since we need to change
399  |                        set in 'delete_rows'.
396  |                        the merging order, merge HZ tables before merging
397  |                        products, we need to mark deleted rows here
398  |                        first and physically delete them after merging one
400  |     Jianying Huang  08-MAR-01  Bug 1610924: Modified the procedure based on
401  |                        the new om enhancement: allow merging all of the site uses
402  |                        of a customer.
403  |
404  +===========================================================================*/
405 
406 PROCEDURE ra_su (
407           req_id                NUMBER,
408           set_num               NUMBER,
409           process_mode          VARCHAR2
410 ) IS
411 
412     l_orig_system_ref_rec          HZ_ORIG_SYSTEM_REF_PUB.ORIG_SYS_REFERENCE_REC_TYPE;
413     x_object_version_number   	   NUMBER;
414     x_return_status   VARCHAR2(30);
415     x_msg_count 	  NUMBER;
416     x_msg_data		  VARCHAR2(2000);
417     x_site_use_id      NUMBER;
418     x_orig_system_reference VARCHAR2(255);
419     x_orig_system     VARCHAR2(30);
420     x_orig_system_ref_id      NUMBER;
421     x_from_site_id     NUMBER;
422     x_to_site_id       NUMBER;
423 
424     CURSOR c1 IS
425         SELECT site_use_id
426         FROM hz_cust_site_uses_all su, ra_customer_merges m --SSUptake
427 	WHERE m.request_id = req_id
428         AND   m.process_flag = 'N'
429         AND   m.set_number = set_num
430         AND   m.org_id     = su.org_id --SSUptake
431 	AND   su.cust_acct_site_id = m.duplicate_address_id
432         FOR UPDATE NOWAIT;
433 
434 
435         CURSOR c2 is
436                 SELECT distinct(m.customer_site_id), m.duplicate_site_id
437                 FROM   ra_customer_merges m
438                 WHERE  m.process_flag = 'N'
439 		AND    m.request_id = req_id
440                 AND    m.set_number = set_num
441                 AND    (m.delete_duplicate_flag = 'N' OR m.delete_duplicate_flag = 'Y');
442 
443 BEGIN
444 
445     arp_message.set_line( 'ARP_CMERGE_ARCUS.RA_SU()+' );
446 
447     /* locking tables by opening and closing cursors */
448     IF process_mode = 'LOCK' THEN
449 
450        arp_message.set_name( 'AR', 'AR_LOCKING_TABLE' );
451        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_SITE_USES', FALSE );
452 
453        OPEN c1;
454        CLOSE c1;
455 
456     ELSE
457 
458        /*************** 'inactivate' mode ***************/
459 
460        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
461        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_SITE_USES', FALSE );
462 
463        --inactivate customer account site uses.
464 
465        UPDATE HZ_CUST_SITE_USES_ALL yt  --SSUptake
466        SET status = 'I',
467            last_update_date = sysdate,
468            last_updated_by =hz_utility_v2pub.user_id,-- arp_standard.profile.user_id,
469            last_update_login =hz_utility_v2pub.last_update_login,-- arp_standard.profile.last_update_login,
470            request_id =  req_id,
471            program_application_id =hz_utility_v2pub.program_application_id,-- arp_standard.profile.program_application_id,
472            program_id =hz_utility_v2pub.program_id,-- arp_standard.profile.program_id,
473            program_update_date = sysdate
474        WHERE EXISTS  (
475                         SELECT 'Y'
476                         FROM   ra_customer_merges m
477                         WHERE  m.duplicate_site_id = yt.site_use_id
478 			AND    m.org_id  = yt.org_id
479 			AND    m.process_flag = 'N'
480 			AND    m.request_id = req_id
481                         AND    m.set_number = set_num
482                         AND    m.delete_duplicate_flag = 'N' );
483 
484        g_count := sql%rowcount;
485 
486        arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
487        arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
488 
489 --Bug 7758559
490 
491 	arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
492        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_SITE_USES', FALSE );
493 
494        UPDATE hz_cust_site_uses_all asu
495       SET primary_flag = 'Y',
496           last_update_date = SYSDATE,
497           last_updated_by = arp_standard.PROFILE.user_id,
498           last_update_login = arp_standard.PROFILE.last_update_login,
499           request_id =  req_id,
500           program_application_id = arp_standard.PROFILE.program_application_id,
501           program_id = arp_standard.PROFILE.program_id,
502           program_update_date = SYSDATE
503       WHERE asu.site_use_id IN( SELECT customer_site_id
504 			        FROM ra_customer_merges m
505                                 WHERE m.request_id = req_id
506 	      		        AND   m.set_number = set_num
507 			        AND  m.customer_createsame = 'N'
508 				AND  m.org_id = asu.org_id
509 			        AND  m.process_flag = 'N'
510 				AND m.duplicate_primary_flag = 'Y'
511                                 AND not exists (SELECT 'EXISTS'
512 	                     			FROM hz_cust_acct_sites_all s, hz_cust_site_uses_all su
513 			      			WHERE s.cust_account_id = m.customer_id
514 			     			AND su.cust_acct_site_id = s.cust_acct_site_id
515 					        AND s.org_id = m.org_id
516 						AND su.org_id = s.org_id
517 			      			AND su.site_use_code = m.duplicate_site_code
518 			      			AND su.primary_flag = 'Y'
519                               			AND su.status = 'A'));
520 	g_count := sql%rowcount;
521 	arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
522 	arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
523 
524 
525 /** Bug 1610924: comment out the following code because of new om enhancement:
526     allow merging all of the site uses of a customer.
527 
531        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
528        --Inactivate non-transaction site uses if no active ship_to,
529        --bill_to or market site uses remain for the account site.
530 
532        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_SITE_USES', FALSE );
533 
534        UPDATE HZ_CUST_SITE_USES su1
535        SET status = 'I',
536            last_update_date = sysdate,
537            last_updated_by = arp_standard.profile.user_id,
538            last_update_login = arp_standard.profile.last_update_login,
539            request_id =  req_id,
540            program_application_id = arp_standard.profile.program_application_id,
541            program_id = arp_standard.profile.program_id,
542            program_update_date = sysdate
543        WHERE cust_acct_site_id IN (
544                             SELECT m.duplicate_address_id
545 			    FROM   ra_customer_merges m
546                             WHERE  m.process_flag = 'N'
547 			    AND    m.request_id = req_id
548                             AND    m.set_number = set_num
549 			    AND    m.delete_duplicate_flag = 'N' )
550        AND site_use_code NOT IN ('BILL_TO', 'SHIP_TO', 'MARKET' )
551        AND NOT EXISTS (
552                             SELECT 'active bill/ship/market site uses exist'
553 			    FROM   HZ_CUST_SITE_USES su
554 		            WHERE  su.cust_acct_site_id = su1.cust_acct_site_id
555 			    AND    su.site_use_code IN
556                                        ( 'BILL_TO','SHIP_TO', 'MARKET' )
557 			    AND    status = 'A' );
558 
559        g_count := sql%rowcount;
560 
561        arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
562        arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
563 
564 **/
565 
566        /*************** 'delete' mode ***************/
567 
568 --Bug 1535542: Mark the rows need to be deleted by setting status to 'D'.
569 --Physically delete them after merge.
570 
571        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
572        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_SITE_USES', FALSE );
573 
574        UPDATE HZ_CUST_SITE_USES_ALL yt
575        SET status = 'D'
576        WHERE EXISTS  (
577                         SELECT 'Y'
578                         FROM   ra_customer_merges m
579                         WHERE  m.duplicate_site_id = yt.site_use_id
580 			AND    m.org_id  = yt.org_id
581 			AND    m.process_flag = 'N'
582 			AND    m.request_id = req_id
583                         AND    m.set_number = set_num
584                         AND    m.delete_duplicate_flag = 'Y' );
585 
586        g_count := sql%rowcount;
587 
588        arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
589        arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
590 
591 /** Bug 1610924: comment out the following code because of new om enhancement:
592     allow merging all of the site uses of a customer.
593 
594        --'Delete' non-transaction site uses if no ship_to,
595        --bill_to, market site uses remain for the account site.
596 
597        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
598        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_SITE_USES', FALSE );
599 
600        UPDATE HZ_CUST_SITE_USES su1
601        SET status = 'D'
602        WHERE cust_acct_site_id IN (
603                             SELECT m.duplicate_address_id
604 			    FROM   ra_customer_merges m
605                             WHERE  m.process_flag = 'N'
606 			    AND    m.request_id = req_id
607                             AND    m.set_number = set_num
608 			    AND    m.delete_duplicate_flag = 'Y' )
609        AND site_use_code NOT IN ('BILL_TO', 'SHIP_TO', 'MARKET' )
610        AND NOT EXISTS (
611                             SELECT 'bill/ship/market site uses exist'
612 			    FROM   HZ_CUST_SITE_USES su
613 		            WHERE  su.cust_acct_site_id = su1.cust_acct_site_id
614 			    AND    su.site_use_code IN
615                                        ( 'BILL_TO', 'SHIP_TO', 'MARKET' )
616 			    AND    status <> 'D' );
617 
618        g_count := sql%rowcount;
619 
620        arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
621        arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
622 **/
623 
624     END IF;
625     open c2;
626      loop
627      fetch c2 into x_to_site_id, x_from_site_id;
628      EXIT WHEN c2%NOTFOUND;
629       HZ_ORIG_SYSTEM_REF_PVT.remap_internal_identifier(
630                     p_init_msg_list => FND_API.G_FALSE,
631                     p_validation_level     => FND_API.G_VALID_LEVEL_NONE,
632                     p_old_owner_table_id   => x_from_site_id,
633 	                p_new_owner_table_id   => x_to_site_id,
634                     p_owner_table_name  =>'HZ_CUST_SITE_USES_ALL',
635                     p_orig_system => null,
636                     p_orig_system_reference => null,
637                     p_reason_code => 'MERGED',
638                     x_return_status => x_return_status,
639                     x_msg_count =>x_msg_count,
640                     x_msg_data  =>x_msg_data);
641              --Handle error message
642              IF x_msg_count = 1 THEN
643                 x_msg_data := x_msg_data || '**remap internal id**';
644                 arp_message.set_line(
645                     'MOSR:remap internal id  ERROR '||
646                     x_msg_data);
647              ELSIF x_msg_count > 1 THEN
648 
649                 FOR x IN 1..x_msg_count LOOP
650                     x_msg_data := fnd_msg_pub.get(p_encoded => fnd_api.g_false);
651                     x_msg_data := x_msg_data || '**remap internal id**';
652                     arp_message.set_line(
653                         'MOSR:remap internal id  ERROR ' ||
654                         x_msg_data );
655                 END LOOP;
656              END IF;
657 
661 
658              IF x_return_status <> fnd_api.g_ret_sts_success THEN
659                 RAISE fnd_api.g_exc_error;
660              END IF;
662      end loop;
663     close c2;
664 
665     arp_message.set_line( 'ARP_CMERGE_ARCUS.RA_SU()-' );
666 
667 EXCEPTION
668 
669     WHEN OTHERS THEN
670       arp_message.set_error( 'ARP_CMERGE_ARCUS.RA_SU' );
671       RAISE;
672 
673 END ra_su;
674 
675 /*===========================================================================+
676  | PROCEDURE
677  |              ra_addr
678  |
679  | DESCRIPTION
680  |              merge in HZ_CUST_SITES.
681  |
682  | SCOPE - PRIVATE
683  |
684  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED
685  |
686  | ARGUMENTS  : IN:
687  |                    req_id
688  |                    set_num
689  |                    process_mod
690  |          IN/ OUT:
691  |
692  | RETURNS    : NONE
693  |
694  | NOTES
695  |
696  | MODIFICATION HISTORY
697  |     Jianying Huang  25-OCT-00  Remove cursor c1. Use c3 replace c1.
698  |                        c1 requires sub-set rows of c3.
699  |     Jianying Huang  20-DEC-00  Bug 1535542: Since we need to change
700  |                        the merging order, merge HZ tables before merging
701  |                        products, we need to mark deleted rows here
702  |                        first and physically delete them after merging one
703  |                        set in 'delete_rows'.
704  |
705  +===========================================================================*/
706 
707 PROCEDURE ra_addr (
708           req_id                NUMBER,
709           set_num               NUMBER,
710           process_mode          VARCHAR2
711 ) IS
712 
713     l_orig_system_ref_rec          HZ_ORIG_SYSTEM_REF_PUB.ORIG_SYS_REFERENCE_REC_TYPE;
714     x_object_version_number   	   NUMBER;
715     x_return_status   VARCHAR2(30);
716     x_msg_count 	  NUMBER;
717     x_msg_data		  VARCHAR2(2000);
718     x_cust_acct_site_id      NUMBER;
719     x_orig_system_reference VARCHAR2(255);
720     x_orig_system     VARCHAR2(30);
721     x_orig_system_ref_id      NUMBER;
722     x_to_address_id              NUMBER;
723     x_from_address_id            NUMBER;
724 
725     --cursor c1 is used in 'inactivate' mode.
726     CURSOR c1 IS
727         SELECT yt.cust_acct_site_id
728         FROM   hz_cust_acct_sites_all yt, ra_customer_merges m --SSUptake
729         WHERE  yt.cust_acct_site_id = m.duplicate_address_id
730 	AND    m.org_id = yt.org_id --SSUptake
731 	AND    m.request_id = req_id
732 	AND    m.process_flag = 'N'
733 	AND    m.set_number = set_num
734         AND    m.delete_duplicate_flag = 'N'
735         AND NOT EXISTS (
736                          SELECT 'active site uses exist'
737                          FROM   HZ_CUST_SITE_USES_ALL su --SSUptake
738                          WHERE  su.org_id = yt.org_id --SSUptake
739 			 AND    su.cust_acct_site_id = yt.cust_acct_site_id
740                          AND    su.status = 'A' )
741         FOR UPDATE NOWAIT;
742 
743     --cursor c2 is used in 'delete' mode.
744     CURSOR c2 IS
745         SELECT yt.cust_acct_site_id
746         FROM   hz_cust_acct_sites_all yt, ra_customer_merges m --SSUptake
747         WHERE  m.request_id = req_id
748         AND    m.process_flag = 'N'
749         AND    m.set_number = set_num
750         AND    m.delete_duplicate_flag = 'Y'
751 	AND    m.org_id = yt.org_id --SSUptake
752 	AND    yt.cust_acct_site_id = m.duplicate_address_id
753         AND NOT EXISTS (
754                          SELECT 'site uses exist'
755                          FROM   HZ_CUST_SITE_USES_ALL su --SSUptake
756                          WHERE  su.cust_acct_site_id = yt.cust_acct_site_id
757 			 AND    su.org_id = yt.org_id --SSUptake
758                          AND    su.status <> 'D' )
759         FOR UPDATE NOWAIT;
760 
761         cursor c3 is
762          SELECT distinct(m.customer_address_id), m.duplicate_address_id
763          FROM   ra_customer_merges m
764          WHERE  m.process_flag = 'N'
765 	 AND    m.request_id = req_id
766 	 AND    m.set_number = set_num
767          AND    (m.delete_duplicate_flag = 'N' or m.delete_duplicate_flag = 'Y') --5571559
768          AND    NOT EXISTS (SELECT 'site uses exist'
769                          FROM   HZ_CUST_SITE_USES_ALL su --SSUptake
770                          WHERE  su.org_id  = m.org_id --SSUptake
771 			 AND    su.cust_acct_site_id = m.duplicate_address_id
772                          AND    su.status = 'A' );
773 
774 
775 
776 BEGIN
777 
778     arp_message.set_line( 'ARP_CMERGE_ARCUS.RA_ADDR()+' );
779 
780     /* locking tables by opening and closing cursors */
781     IF process_mode = 'LOCK' THEN
782 
783        arp_message.set_name( 'AR', 'AR_LOCKING_TABLE' );
784        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCT_SITES', FALSE );
785 
786        OPEN c1;
787        CLOSE c1;
788 
789        OPEN c2;
790        CLOSE c2;
791 
792     ELSE
793 
794        /*************** 'inactivate' mode ***************/
795 
796        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
797        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCT_SITES', FALSE );
798 
799        --inactivate customer account site
800 
801        UPDATE HZ_CUST_ACCT_SITES_ALL yt --SSUptake
802        SET status = 'I',
803            last_update_date = sysdate,
804            last_updated_by = hz_utility_v2pub.user_id,--arp_standard.profile.user_id,
805            last_update_login =hz_utility_v2pub.last_update_login,-- arp_standard.profile.last_update_login,
806            request_id =  req_id,
810        WHERE EXISTS  (
807            program_application_id =hz_utility_v2pub.program_application_id,-- arp_standard.profile.program_application_id,
808            program_id =hz_utility_v2pub.program_id,-- arp_standard.profile.program_id,
809            program_update_date = sysdate
811                       SELECT 'Y'
812                       FROM   ra_customer_merges m
813                       WHERE  m.process_flag = 'N'
814 	              AND    m.request_id = req_id
815 		      AND    m.set_number = set_num
816                       AND    m.delete_duplicate_flag = 'N'
817 		      AND    m.duplicate_address_id = yt.cust_acct_site_id
818 		      AND    m.org_id    = yt.org_id) --SSUptake
819        AND NOT EXISTS (
820                       SELECT 'active site uses exist'
821                       FROM   HZ_CUST_SITE_USES_ALL su
822                       WHERE  su.cust_acct_site_id = yt.cust_acct_site_id
823 		      AND    su.org_id   = yt.org_id
824                       AND    su.status = 'A' );
825 
826        g_count := sql%rowcount;
827 
828        arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
829        arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
830 
831        --Update bill_to_flag
832 
833        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE');
834        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCT_SITES', FALSE );
835 
836        UPDATE HZ_CUST_ACCT_SITES_ALL yt --SSUptake
837        SET bill_to_flag = null,
838            last_update_date = sysdate,
839            last_updated_by = hz_utility_v2pub.user_id,--arp_standard.profile.user_id,
840            last_update_login =hz_utility_v2pub.last_update_login,-- arp_standard.profile.last_update_login,
841            request_id =  req_id,
842            program_application_id =hz_utility_v2pub.program_application_id,-- arp_standard.profile.program_application_id,
843            program_id =hz_utility_v2pub.program_id,-- arp_standard.profile.program_id,
844            program_update_date = sysdate
845        WHERE EXISTS (
846                       SELECT 'Y'
847                       FROM   ra_customer_merges m
848                       WHERE  m.process_flag = 'N'
849 		      AND    m.request_id = req_id
850 		      AND    m.set_number = set_num
851 		      AND    m.duplicate_address_id = yt.cust_acct_site_id
852 		      AND    m.org_id  = yt.org_id
853                       AND    m.delete_duplicate_flag = 'N' )
854        AND NOT EXISTS (
855                       SELECT 'no active bill to'
856                       FROM   HZ_CUST_SITE_USES_ALL s --SSUptake
857                       WHERE  s.cust_acct_site_id = yt.cust_acct_site_id
858 		      AND    s.org_id  = yt.org_id
859                       AND    s.site_use_code = 'BILL_TO'
860                       AND    status = 'A' )
861        AND NVL(bill_to_flag, 'N') <> 'N';
862 
863        g_count := sql%rowcount;
864 
865        arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
866        arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
867 
868        --Update ship_to_flag
869 
870        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
871        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCT_SITES', FALSE );
872 
873        UPDATE HZ_CUST_ACCT_SITES_ALL yt --SSUptake
874        SET ship_to_flag = null,
875            last_update_date = sysdate,
876            last_updated_by = hz_utility_v2pub.user_id,--arp_standard.profile.user_id,
877            last_update_login =hz_utility_v2pub.last_update_login,-- arp_standard.profile.last_update_login,
878            request_id =  req_id,
879            program_application_id =hz_utility_v2pub.program_application_id,-- arp_standard.profile.program_application_id,
880            program_id =hz_utility_v2pub.program_id,-- arp_standard.profile.program_id,
881            program_update_date = sysdate
882        WHERE  EXISTS (
883                       SELECT 'Y'
884                       FROM   ra_customer_merges m
885                       WHERE  m.process_flag = 'N'
886 		      AND    m.request_id = req_id
887 		      AND    m.set_number = set_num
888                       AND    m.delete_duplicate_flag = 'N'
889 		      AND    m.duplicate_address_id = yt.cust_acct_site_id
890 		      AND    m.org_id   = yt.org_id )
891        AND NOT EXISTS (
892                       SELECT 'no active ship to'
893                       FROM   HZ_CUST_SITE_USES_ALL s --SSUptake
894                       WHERE  s.cust_acct_site_id = yt.cust_acct_site_id
895 		      AND    s.org_id = yt.org_id
896                       AND    s.site_use_code = 'SHIP_TO'
897                       AND    status = 'A' )
898        AND NVL(ship_to_flag, 'N') <> 'N';
899 
900        g_count := sql%rowcount;
901 
902        arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
903        arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
904 
905        --Update market_flag
906 
907        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
908        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCT_SITES', FALSE );
909 
910        UPDATE HZ_CUST_ACCT_SITES_ALL yt --SSUptake
911        SET market_flag = null,
912            last_update_date = sysdate,
913            last_updated_by = hz_utility_v2pub.user_id,--arp_standard.profile.user_id,
914            last_update_login = hz_utility_v2pub.last_update_login,--arp_standard.profile.last_update_login,
915            request_id =  req_id,
916            program_application_id =hz_utility_v2pub.program_application_id,-- arp_standard.profile.program_application_id,
917            program_id =hz_utility_v2pub.program_id,-- arp_standard.profile.program_id,
918            program_update_date = sysdate
919        WHERE EXISTS (
920                       SELECT 'Y'
921                       FROM   ra_customer_merges m
922                       WHERE  m.process_flag = 'N'
923 		      AND    m.request_id = req_id
927 		      AND    m.duplicate_address_id = yt.cust_acct_site_id)
924 		      AND    m.set_number = set_num
925                       AND    m.delete_duplicate_flag = 'N'
926 		      AND    m.org_id = yt.org_id
928        AND NOT EXISTS (
929                       SELECT 'no active market site'
930                       FROM   HZ_CUST_SITE_USES_ALL s --SSUptake
931                       WHERE  s.cust_acct_site_id = yt.cust_acct_site_id
932 		      AND    s.org_id = yt.org_id
933                       AND    s.site_use_code = 'MARKET'
934                       AND    status = 'A' )
935        AND NVL(market_flag, 'N') <> 'N';
936 
937        g_count := sql%rowcount;
938 
939        arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
940        arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
941 
942 --Bug 7758559
943 
944                UPDATE hz_cust_acct_sites_all yt
945 	SET bill_to_flag = 'P',
946 	    last_update_date = sysdate,
947             last_updated_by = arp_standard.profile.user_id,
948             last_update_login = arp_standard.profile.last_update_login,
949             request_id =  req_id,
950             program_application_id = arp_standard.profile.program_application_id,
951             program_id = arp_standard.profile.program_id,
952             program_update_date = sysdate
953 	WHERE EXISTS (	SELECT 1
954  	    	      	FROM hz_cust_site_uses_all su, ra_customer_merges m
955 	              	WHERE m.request_id = req_id
956 		        AND   m.set_number = set_num
957 	     		AND  m.customer_createsame = 'N'
958 	     		AND  m.process_flag = 'N'
959              		AND m.duplicate_primary_flag = 'Y'
960              		AND yt.cust_acct_site_id = m.customer_address_id
961 	     		AND su.site_use_code = 'BILL_TO'
962 	     		AND su.site_use_code = m.customer_site_code
963 	     		AND su.site_use_id = m.customer_site_id
964 	     		AND su.primary_flag = 'Y'
965             		AND su.request_id = req_id
966 			AND yt.org_id = m.org_id
967 		        AND su.org_id = m.org_id);
968 
969 	g_count := sql%rowcount;
970 
971 	arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
972 	arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
973 
974 	arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
975 	arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCT_SITES', FALSE );
976 
977 	UPDATE hz_cust_acct_sites_all yt
978 	SET ship_to_flag = 'P',
979 	    last_update_date = sysdate,
980             last_updated_by = arp_standard.profile.user_id,
981             last_update_login = arp_standard.profile.last_update_login,
982             request_id =  req_id,
983             program_application_id = arp_standard.profile.program_application_id,
984             program_id = arp_standard.profile.program_id,
985             program_update_date = sysdate
986 	WHERE EXISTS (	SELECT 1
987  	     		FROM hz_cust_site_uses_all su, ra_customer_merges m
988 	     		WHERE m.request_id = req_id
989              		AND   m.set_number = set_num
990 	     		AND  m.customer_createsame = 'N'
991 	     		AND  m.process_flag = 'N'
992              		AND m.duplicate_primary_flag = 'Y'
993              		AND yt.cust_acct_site_id = m.customer_address_id
994 	     		AND su.site_use_code = 'SHIP_TO'
995 	     		AND su.site_use_code = m.customer_site_code
996 	     		AND su.site_use_id = m.customer_site_id
997 	    		AND su.primary_flag = 'Y'
998 	     		AND su.request_id = req_id
999 			AND su.org_id = m.org_id
1000 			AND yt.org_id = m.org_id);
1001 
1002 	g_count := sql%rowcount;
1003 
1004 	arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
1005 	arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
1006 
1007 	arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
1008 	arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCT_SITES', FALSE );
1009 
1010 	UPDATE hz_cust_acct_sites_all yt
1011 	SET market_flag = 'P',
1012 	    last_update_date = sysdate,
1013             last_updated_by = arp_standard.profile.user_id,
1014             last_update_login = arp_standard.profile.last_update_login,
1015             request_id =  req_id,
1016             program_application_id = arp_standard.profile.program_application_id,
1017             program_id = arp_standard.profile.program_id,
1018             program_update_date = sysdate
1019 	WHERE EXISTS (  SELECT 1
1020  	    		FROM hz_cust_site_uses_all su, ra_customer_merges m
1021 	     		WHERE m.request_id = req_id
1022              		AND   m.set_number = set_num
1023 	     		AND  m.customer_createsame = 'N'
1024 	     		AND  m.process_flag = 'N'
1025              		AND m.duplicate_primary_flag = 'Y'
1026              		AND yt.cust_acct_site_id = m.customer_address_id
1027 	    		AND su.site_use_code = 'MARKET'
1028 	     		AND su.site_use_code = m.customer_site_code
1029 	     		AND su.site_use_id = m.customer_site_id
1030 	     		AND su.primary_flag = 'Y'
1031 	     		AND su.request_id = req_id
1032 			AND su.org_id = m.org_id
1033 			AND yt.org_id = m.org_id);
1034 
1035 	g_count := sql%rowcount;
1036 	arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
1037 	arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
1038 
1039 --Bug 7758559
1040 
1041        /*************** 'delete' mode ***************/
1042 
1043 --Bug 1535542: Mark the rows need to be deleted by setting status to 'D'.
1044 --Physically delete them after merge.
1045 
1046        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
1047        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCT_SITES', FALSE );
1048 
1049        --'delete' customer account site
1050 
1051        UPDATE HZ_CUST_ACCT_SITES_ALL yt --SSUptake
1052        SET status = 'D'
1053        WHERE EXISTS  (
1054                       SELECT 'Y'
1055                       FROM   ra_customer_merges m
1056                       WHERE  m.process_flag = 'N'
1057 		      AND    m.request_id = req_id
1061 		      AND    m.org_id = yt.org_id)
1058                       AND    m.set_number = set_num
1059                       AND    m.delete_duplicate_flag = 'Y'
1060 		      AND    m.duplicate_address_id = yt.cust_acct_site_id
1062        AND NOT EXISTS (
1063                       SELECT 'site uses exist'
1064                       FROM   HZ_CUST_SITE_USES_ALL su --SSUptake
1065                       WHERE  su.cust_acct_site_id = yt.cust_acct_site_id
1066 		      AND    su.org_id = yt.org_id
1067                       AND    su.status <> 'D' );
1068 
1069        g_count := sql%rowcount;
1070 
1071        arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
1072        arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
1073 
1074     END IF;
1075 
1076      open c3;
1077      loop
1078      fetch c3 into x_to_address_id, x_from_address_id;
1079      EXIT WHEN c3%NOTFOUND;
1080       HZ_ORIG_SYSTEM_REF_PVT.remap_internal_identifier(
1081                     p_init_msg_list => FND_API.G_FALSE,
1082                     p_validation_level     => FND_API.G_VALID_LEVEL_NONE,
1083                     p_old_owner_table_id   => x_from_address_id,
1084 	                p_new_owner_table_id   => x_to_address_id,
1085                     p_owner_table_name  =>'HZ_CUST_ACCT_SITES_ALL',
1086                     p_orig_system => null,
1087                     p_orig_system_reference => null,
1088                     p_reason_code => 'MERGED',
1089                     x_return_status => x_return_status,
1090                     x_msg_count =>x_msg_count,
1091                     x_msg_data  =>x_msg_data);
1092 	 --Handle error message
1093              IF x_msg_count = 1 THEN
1094                 x_msg_data := x_msg_data || '**remap internal id**';
1095                 arp_message.set_line(
1096                     'MOSR:remap internal id  ERROR '||
1097                     x_msg_data);
1098              ELSIF x_msg_count > 1 THEN
1099 
1100                 FOR x IN 1..x_msg_count LOOP
1101                     x_msg_data := fnd_msg_pub.get(p_encoded => fnd_api.g_false);
1102                     x_msg_data := x_msg_data || '**remap internal id**';
1103                     arp_message.set_line(
1104                         'MOSR:remap internal id  ERROR ' ||
1105                         x_msg_data );
1106                 END LOOP;
1107              END IF;
1108 
1109              IF x_return_status <> fnd_api.g_ret_sts_success THEN
1110                 RAISE fnd_api.g_exc_error;
1111              END IF;
1112 
1113      end loop;
1114     close c3;
1115 
1116 
1117      arp_message.set_line( 'ARP_CMERGE_ARCUS.RA_ADDR()-' );
1118 
1119 EXCEPTION
1120 
1121     WHEN OTHERS THEN
1122       arp_message.set_error( 'ARP_CMERGE_ARCUS.RA_ADDR' );
1123       RAISE;
1124 
1125 END ra_addr;
1126 
1127 /*===========================================================================+
1128  | PROCEDURE
1129  |              ra_cust
1130  |
1131  | DESCRIPTION
1132  |              merge in HZ_CUST_ACCOUNTS.
1133  |
1134  | SCOPE - PRIVATE
1135  |
1136  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED
1137  |
1138  | ARGUMENTS  : IN:
1139  |                    req_id
1140  |                    set_num
1141  |                    process_mod
1142  |          IN/ OUT:
1143  |
1144  | RETURNS    : NONE
1145  |
1146  | NOTES
1147  |
1148  | MODIFICATION HISTORY
1149  |     Jianying Huang  25-OCT-00  Customer account is global while account
1150  |                        site is stripped by operating unit. We need to
1151  |                        check if this account has (active)sites in
1152  |                        HZ_CUST_ACCT_SITES_ALL.
1153  |     Jianying Huang  20-DEC-00  Bug 1535542: Since we need to change
1154  |                        the merging order, merge HZ tables before merging
1155  |                        products, we need to mark deleted rows here
1156  |                        first and physically delete them after merging one
1157  |                        set in 'delete_rows'.
1158  |
1159  +===========================================================================*/
1160 
1161 PROCEDURE ra_cust (
1162           req_id                NUMBER,
1163           set_num               NUMBER,
1164           process_mode          VARCHAR2
1165 ) IS
1166 
1167     l_orig_system_ref_rec          HZ_ORIG_SYSTEM_REF_PUB.ORIG_SYS_REFERENCE_REC_TYPE;
1168     x_object_version_number   	   NUMBER;
1169     x_return_status   VARCHAR2(30);
1170     x_msg_count 	  NUMBER;
1171     x_msg_data		  VARCHAR2(2000);
1172     x_cust_account_id      NUMBER;
1173     x_orig_system_reference VARCHAR2(255);
1174     x_orig_system     VARCHAR2(30);
1175     x_orig_system_ref_id      NUMBER;
1176     x_from_cust_id         NUMBER;
1177     x_to_cust_id           NUMBER;
1178 
1179     --cursor c1 is used in 'inactivate' mode.
1180     CURSOR c1 IS
1181         SELECT yt.cust_account_id
1182         FROM   hz_cust_accounts yt, ra_customer_merges m
1183         WHERE  cust_account_id = m.duplicate_id
1184         AND    m.process_flag = 'N'
1185 	AND    m.request_id = req_id
1186         AND    m.set_number = set_num
1187         AND    m.delete_duplicate_flag = 'N'
1188         /* no active addresses */
1189         AND NOT EXISTS (
1190                     SELECT 'active addresses exist'
1191                     FROM   hz_cust_acct_sites_all addr
1192                     WHERE  addr.cust_account_id = yt.cust_account_id
1193                     AND    addr.status = 'A' )
1194         FOR UPDATE NOWAIT;
1195 
1196     --cursor c2 is used in 'delete' mode.
1197     CURSOR c2 IS
1198         SELECT cust_account_id
1199         FROM   hz_cust_accounts yt, ra_customer_merges m
1200         WHERE  cust_account_id = m.duplicate_id
1201         AND    m.process_flag = 'N'
1202 	AND    m.request_id = req_id
1203         AND    m.set_number = set_num
1204         AND    m.delete_duplicate_flag = 'Y'
1205         /* no addresses */
1206         AND NOT EXISTS (
1207                     SELECT 'addresses exist'
1208                     FROM   hz_cust_acct_sites_all addr
1209                     WHERE  addr.cust_account_id = yt.cust_account_id
1210                     AND    addr.status <> 'D' )
1211         FOR UPDATE NOWAIT;
1212 
1213       cursor c3 is
1214             SELECT distinct(m.customer_id), m.duplicate_id
1215                  FROM   ra_customer_merges m
1216                  WHERE  m.process_flag = 'N'
1217 		 AND    m.request_id =req_id
1218                  AND    m.set_number = set_num
1219                  AND    (m.delete_duplicate_flag = 'N' OR m.delete_duplicate_flag = 'Y')
1220                  AND NOT EXISTS (
1221                     SELECT 'addresses exist'
1222                     FROM   hz_cust_acct_sites_all addr
1223                     WHERE  addr.cust_account_id = m.duplicate_id
1224                     AND    addr.status = 'A');
1225 
1226 
1227 BEGIN
1228 
1229     arp_message.set_line( 'ARP_CMERGE_ARCUS.RA_CUST()+' );
1230 
1231     /* locking tables by opening and closing cursors */
1232     IF process_mode = 'LOCK' THEN
1233 
1234        arp_message.set_name( 'AR', 'AR_LOCKING_TABLE' );
1235        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCOUNTS', FALSE );
1236 
1237        OPEN c1;
1238        CLOSE c1;
1239 
1240        OPEN c2;
1241        CLOSE c2;
1242 
1243     ELSE
1244 
1245        /*************** 'inactivate' mode ***************/
1246 
1247        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
1248        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCOUNTS', FALSE );
1249 
1250        --inactivate customer account
1251 
1252        UPDATE HZ_CUST_ACCOUNTS yt
1253        SET status = 'I',
1254            last_update_date = sysdate,
1255            last_updated_by = hz_utility_v2pub.user_id,--arp_standard.profile.user_id,
1256            last_update_login =hz_utility_v2pub.last_update_login,-- arp_standard.profile.last_update_login,
1257            request_id =  req_id,
1258            program_application_id =hz_utility_v2pub.program_application_id,-- arp_standard.profile.program_application_id,
1259            program_id = hz_utility_v2pub.program_id,--arp_standard.profile.program_id,
1260            program_update_date = sysdate
1261        WHERE cust_account_id IN (
1262                  SELECT m.duplicate_id
1263                  FROM   ra_customer_merges m
1264                  WHERE  m.process_flag = 'N'
1265 		 AND    m.request_id = req_id
1266                  AND    m.set_number = set_num
1267                  AND    m.delete_duplicate_flag = 'N' )
1268        /* no active addresses */
1269        AND NOT EXISTS (
1270                 SELECT 'active addresses exist'
1271                 FROM   hz_cust_acct_sites_all addr
1272                 WHERE  addr.cust_account_id = yt.cust_account_id
1273                 AND    addr.status = 'A' );
1274 
1275        g_count := sql%rowcount;
1276 
1277        arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
1278        arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
1279 
1280        /*************** 'delete' mode ***************/
1281 
1282 --Bug 1535542: Mark the rows need to be deleted by setting status to 'D'.
1283 --Physically delete them after merge.
1284 
1285        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
1286        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCOUNTS', FALSE );
1287 
1288        --'delete' customer account
1289 
1290        UPDATE HZ_CUST_ACCOUNTS yt
1291        SET status = 'D'
1292        WHERE cust_account_id IN (
1293                  SELECT m.duplicate_id
1294                  FROM   ra_customer_merges m
1295                  WHERE  m.process_flag = 'N'
1296 		 AND    m.request_id = req_id
1297                  AND    m.set_number = set_num
1298                  AND    m.delete_duplicate_flag = 'Y' )
1299        /* no addresses */
1300        AND NOT EXISTS (
1301                  SELECT 'addresses exist'
1302                  FROM   hz_cust_acct_sites_all addr
1303                  WHERE  addr.cust_account_id = yt.cust_account_id
1304                  AND    addr.status <> 'D' );
1305 
1306        g_count := sql%rowcount;
1307 
1308        arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
1309        arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
1310 
1311     END IF;
1312 
1313  open c3;
1314      loop
1315      fetch c3 into x_to_cust_id, x_from_cust_id;
1316      EXIT WHEN c3%NOTFOUND;
1317       HZ_ORIG_SYSTEM_REF_PVT.remap_internal_identifier(
1318                     p_init_msg_list => FND_API.G_FALSE,
1319                     p_validation_level     => FND_API.G_VALID_LEVEL_NONE,
1320                     p_old_owner_table_id   => x_from_cust_id,
1321 	                p_new_owner_table_id   => x_to_cust_id,
1322                     p_owner_table_name  =>'HZ_CUST_ACCOUNTS',
1323                     p_orig_system => null,
1324                     p_orig_system_reference => null,
1325                     p_reason_code => 'MERGED',
1326                     x_return_status => x_return_status,
1327                     x_msg_count =>x_msg_count,
1328                     x_msg_data  =>x_msg_data);
1329 	 --Handle error message
1330              IF x_msg_count = 1 THEN
1331                 x_msg_data := x_msg_data || '**remap internal id**';
1332                 arp_message.set_line(
1333                     'MOSR:remap internal id  ERROR '||
1334                     x_msg_data);
1335              ELSIF x_msg_count > 1 THEN
1336 
1337                 FOR x IN 1..x_msg_count LOOP
1338                     x_msg_data := fnd_msg_pub.get(p_encoded => fnd_api.g_false);
1339                     x_msg_data := x_msg_data || '**remap internal id**';
1340                     arp_message.set_line(
1341                         'MOSR:remap internal id  ERROR ' ||
1342                         x_msg_data );
1343                 END LOOP;
1344              END IF;
1345 
1346              IF x_return_status <> fnd_api.g_ret_sts_success THEN
1347                 RAISE fnd_api.g_exc_error;
1348              END IF;
1349 
1350      end loop;
1351     close c3;
1352 
1353     arp_message.set_line( 'ARP_CMERGE_ARCUS.RA_CUST()-' );
1354 
1355 EXCEPTION
1356 
1357     WHEN OTHERS THEN
1358       arp_message.set_error( 'ARP_CMERGE_ARCUS.RA_CUST' );
1359       RAISE;
1360 
1361 END ra_cust;
1362 
1363 --4527935 Private procedure to create cust account relationships
1364 PROCEDURE create_acct_relate(p_cust_acct_relate_id NUMBER,p_customer_id NUMBER, p_cust_account_id NUMBER,
1365                              p_related_cust_account_id NUMBER,p_rowid ROWID,
1366 			     p_reciprocal_flag boolean);
1367 /*===========================================================================+
1368  | PROCEDURE
1369  |              ra_cr
1370  |
1371  | DESCRIPTION
1372  |              merge in HZ_CUST_ACCT_RELATE.
1373  |
1374  | SCOPE - PRIVATE
1375  |
1376  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED
1377  |
1378  | ARGUMENTS  : IN:
1379  |                    req_id
1380  |                    set_num
1381  |                    process_mod
1382  |          IN/ OUT:
1383  |
1384  | RETURNS    : NONE
1385  |
1386  | NOTES
1387  |        This procedure performs a merge (as happens for a product's
1388  |        tables) as opposed to a delete/inactivate.
1389  |
1390  | MODIFICATION HISTORY
1391  |     Jianying Huang  25-OCT-00  Customer account is global while account
1392  |                        site is stripped by operating unit. We need to
1393  |                        check if this account has (active)sites in
1394  |                        HZ_CUST_ACCT_SITES_ALL.
1395  |     Jianying Huang  12-DEC-00  Modified cursor c1 and c2. Replace 'active
1396  |                        account sites exist' with 'active accounts exist'
1397  |     Jianying Huang  20-DEC-00  Bug 1535542: Since we need to change
1398  |                        the merging order, merge HZ tables before merging
1399  |                        products, we need to mark deleted rows here
1400  |                        first and physically delete them after merging one
1404  |
1401  |                        set in 'delete_rows'.
1402  |     S V Sowjanya    11-APR-06 Bug No: 4527935. Modified the code to handle the customer
1403  |                                 account relationship merge properly.
1405  +===========================================================================*/
1406 
1407 PROCEDURE ra_cr (
1408           req_id                NUMBER,
1409           set_num               NUMBER,
1410           process_mode          VARCHAR2
1411 ) IS
1412 
1413     --cursor c1 is for from cust account
1414     CURSOR c1 IS
1415         SELECT yt.cust_account_id
1416         FROM hz_cust_acct_relate_all yt, ra_customer_merges  m --SSUptake
1417         WHERE
1418          --NOT EXISTS (
1419          --              SELECT 'active accounts exist'
1420          --             FROM   hz_cust_accounts acct
1421          --               WHERE  acct.cust_account_id = yt.cust_account_id
1422          --              AND    acct.status = 'A' )
1423         m.request_id = req_id
1424 	AND m.process_flag = 'N'
1425         AND m.set_number = set_num
1426 	AND m.org_id  = yt.org_id --SSUptake
1427 	AND yt.cust_account_id = m.duplicate_id
1428         FOR UPDATE NOWAIT;
1429 
1430     --cursor c2 is for to cust account
1431     CURSOR c2 IS
1432         SELECT yt.related_cust_account_id
1433         FROM hz_cust_acct_relate_all yt, ra_customer_merges   m --SSUptake
1434         WHERE
1435         --NOT EXISTS (
1436         --               SELECT 'active accounts exist'
1437         --               FROM   hz_cust_accounts acct
1438         --               WHERE  acct.cust_account_id = yt.related_cust_account_id
1439         --               AND    acct.status = 'A' )
1440         m.request_id = req_id
1441 	AND m.process_flag = 'N'
1442         AND m.set_number = set_num
1443 	AND m.org_id = yt.org_id --SSUptake
1444 	AND yt.related_cust_account_id = m.duplicate_id
1445         FOR UPDATE NOWAIT;
1446 
1447 
1448  --Start of Bug No: 4527935
1449   CURSOR c_from_rel_cust_id IS
1450       SELECT unique yt.cust_acct_relate_id,cm.customer_id,yt.cust_account_id,yt.related_cust_account_id,
1451              yt.customer_reciprocal_flag,nvl(yt.bill_to_flag,'N') bill_to_flag,
1452 	     nvl(yt.ship_to_flag,'N') ship_to_flag,yt.rowid,yt.org_id
1453       FROM   hz_cust_acct_relate_all yt, ra_customer_merges cm
1454       WHERE  cm.request_id = req_id
1455       AND    cm.process_flag = 'N'
1456       AND    cm.set_number = set_num
1457       AND    cm.duplicate_id <> cm.customer_id             --merging sites for same customer
1458       AND    cm.duplicate_id = yt.cust_account_id
1459       AND    cm.customer_id  <> yt.related_cust_account_id --relationship to self not allowed
1460       AND    yt.status ='A'
1461       AND    cm.org_id = yt.org_id;
1462   CURSOR c_from_cust_rel_id IS
1463       SELECT unique yt.cust_acct_relate_id,cm.customer_id,yt.cust_account_id,yt.related_cust_account_id,
1464              yt.customer_reciprocal_flag,nvl(yt.bill_to_flag,'N') bill_to_flag,
1465 	     nvl(yt.ship_to_flag,'N') ship_to_flag,yt.rowid,yt.org_id
1466       FROM   hz_cust_acct_relate_all yt, ra_customer_merges cm
1467       WHERE  cm.request_id = req_id
1468       AND    cm.process_flag = 'N'
1469       AND    cm.set_number = set_num
1470       AND    cm.duplicate_id <> cm.customer_id             --merging sites for same customer
1471       AND    cm.duplicate_id = yt.related_cust_account_id
1472       AND    cm.customer_id  <> yt.cust_account_id --relationship to self not allowed
1473       AND    yt.status ='A'
1474       AND    cm.org_id = yt.org_id;
1475   CURSOR c_to_rel_cust_id(p_cust_account_id NUMBER,p_related_cust_account_id NUMBER,p_org_id NUMBER) IS
1476       SELECT cust_account_id,related_cust_account_id,customer_reciprocal_flag,
1477 	        nvl(bill_to_flag,'N') bill_to_flag,nvl(ship_to_flag,'N') ship_to_flag
1478       FROM   hz_cust_acct_relate_all yt
1479       WHERE  yt.cust_account_id = p_cust_account_id
1480       AND    yt.related_cust_account_id = p_related_cust_account_id
1481       AND    yt.status = 'A'
1482       AND    yt.org_id = p_org_id
1483       AND    ROWNUM =1;
1484   CURSOR c_acct_num(p_acct_id NUMBER) IS
1485     SELECT account_number FROM hz_cust_accounts
1486     WHERE  cust_account_id=p_acct_id;
1487 
1488   l_to_cust_account_id             NUMBER(15);
1489   l_to_related_cust_account_id     NUMBER(15);
1490   l_to_customer_reciprocal_flag    VARCHAR2(1);
1491   l_to_bill_to_flag                VARCHAR2(1);
1492   l_to_ship_to_flag		   VARCHAR2(1);
1493   l_update_flag                    BOOLEAN;
1494   l_from_acct_no                   VARCHAR2(30);
1495   l_to_acct_no                     VARCHAR2(30);
1496 --End of Bug No: 4527935
1497 
1498 BEGIN
1499 
1500     arp_message.set_line( 'ARP_CMERGE_ARCUS.RA_CR()+' );
1501 
1502     /* locking tables by opening and closing cursors */
1503     IF process_mode = 'LOCK' THEN
1504 
1505        arp_message.set_name( 'AR', 'AR_LOCKING_TABLE' );
1506        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCT_RELATE', FALSE );
1507 
1508        OPEN c1;
1509        CLOSE c1;
1510 
1511        OPEN c2;
1512        CLOSE c2;
1513 
1514     ELSE
1515 
1516 --Start of Bug No: 4558774
1517 
1518        /************** from account update ************/
1519        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
1520        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCT_RELATE', FALSE );
1521 
1522        g_count := 0;
1523        FOR from_rec in c_from_rel_cust_id
1524        LOOP
1525          l_to_cust_account_id             := null;
1526 	 l_to_related_cust_account_id     := null;
1527 	 l_to_customer_reciprocal_flag    := null;
1528 	 l_to_bill_to_flag                := null;
1529 	 l_to_ship_to_flag		  := null;
1530 	 l_update_flag                    := false;
1531          OPEN c_to_rel_cust_id(from_rec.customer_id,from_rec.related_cust_account_id,from_rec.org_id);
1535          IF(l_to_cust_account_id IS NULL) THEN ---No Relationship Exists.Create a new one
1532 	 FETCH  c_to_rel_cust_id INTO l_to_cust_account_id,l_to_related_cust_account_id,
1533 	        l_to_customer_reciprocal_flag,l_to_bill_to_flag,l_to_ship_to_flag ;
1534          CLOSE c_to_rel_cust_id;
1536 	   create_acct_relate(from_rec.cust_acct_relate_id,from_rec.customer_id, from_rec.cust_account_id,
1537                              from_rec.related_cust_account_id,from_rec.rowid,
1538 			     false);
1539 	 ELSIF(l_to_customer_reciprocal_flag <> from_rec.customer_reciprocal_flag) THEN --- Display the warning message in the log file.
1540 	   OPEN c_acct_num(from_rec.cust_account_id);
1541 	   FETCH c_acct_num into l_from_acct_no;
1542 	   CLOSE c_acct_num;
1543 	   OPEN c_acct_num(l_to_cust_account_id);
1544 	   FETCH c_acct_num into l_to_acct_no;
1545 	   CLOSE c_acct_num;
1546            arp_message.set_name( 'AR', 'HZ_ACCT_MERGE_REL_WARNING' );
1547 	   arp_message.set_token( 'MERGE_FROM_ACCT',l_from_acct_no, FALSE );
1548 	   arp_message.set_token( 'MERGE_TO_ACCT',l_to_acct_no, FALSE );
1549 	 ELSE -- blend the two relationships
1550 	   IF(l_to_bill_to_flag <> 'Y' AND l_to_bill_to_flag <> from_rec.bill_to_flag) THEN
1551 	      l_to_bill_to_flag := from_rec.bill_to_flag;
1552 	      l_update_flag := true;
1553 	   END IF;
1554 	   IF(l_to_ship_to_flag <> 'Y' AND l_to_ship_to_flag <> from_rec.ship_to_flag) THEN
1555 	      l_to_ship_to_flag := from_rec.ship_to_flag;
1556 	      l_update_flag := true;
1557 	   END IF;
1558 	   IF(l_update_flag) THEN
1559              UPDATE hz_cust_acct_relate_all SET bill_to_flag = l_to_bill_to_flag,ship_to_flag=l_to_ship_to_flag
1560    	     WHERE cust_account_id = l_to_cust_account_id
1561 	     AND   related_cust_account_id = l_to_related_cust_account_id
1562 	     AND   org_id = from_rec.org_id
1563 	     AND   STATUS = 'A';
1564            END IF;
1565 	 END IF;
1566 	 --Inactivate the from account relationship
1567 	 UPDATE hz_cust_acct_relate_all yt SET
1568 	      status = 'I',
1569              last_update_date = sysdate,
1570              last_updated_by = hz_utility_v2pub.user_id,
1571              last_update_login = hz_utility_v2pub.last_update_login,
1572              request_id =  req_id,
1573              program_application_id = hz_utility_v2pub.program_application_id,
1574              program_id = hz_utility_v2pub.program_id,
1575              program_update_date = sysdate
1576          WHERE status = 'A'
1577          AND   yt.cust_account_id = from_rec.cust_account_id
1578 	 AND   yt.related_cust_account_id = from_rec.related_cust_account_id
1579 	 AND   yt.org_id = from_rec.org_id;
1580 	 g_count := g_count + sql%rowcount;
1581        END LOOP;
1582 
1583         /************** to account update ************/
1584 
1585        FOR from_rec in c_from_cust_rel_id
1586        LOOP
1587          l_to_cust_account_id             := null;
1588 	 l_to_related_cust_account_id     := null;
1589 	 l_to_customer_reciprocal_flag    := null;
1590 	 l_to_bill_to_flag                := null;
1591 	 l_to_ship_to_flag		  := null;
1592 	 l_update_flag                    := false;
1593          OPEN c_to_rel_cust_id(from_rec.cust_account_id,from_rec.customer_id,from_rec.org_id);
1594 	 FETCH  c_to_rel_cust_id INTO l_to_cust_account_id,l_to_related_cust_account_id,
1595 	        l_to_customer_reciprocal_flag,l_to_bill_to_flag,l_to_ship_to_flag ;
1596          CLOSE c_to_rel_cust_id;
1597          IF(l_to_related_cust_account_id IS NULL) THEN ---No Relationship Exists.Create a new one
1598 	   create_acct_relate(from_rec.cust_acct_relate_id,from_rec.customer_id, from_rec.cust_account_id,
1599                              from_rec.related_cust_account_id,from_rec.rowid,
1600 			     true);
1601 	 ELSIF(from_rec.customer_reciprocal_flag <> 'Y' and l_to_customer_reciprocal_flag <> from_rec.customer_reciprocal_flag) THEN --- Display the warning message in the log file.
1602 	   OPEN c_acct_num(from_rec.related_cust_account_id);
1603 	   FETCH c_acct_num into l_from_acct_no;
1604 	   CLOSE c_acct_num;
1605 	   OPEN c_acct_num(l_to_related_cust_account_id);
1606 	   FETCH c_acct_num into l_to_acct_no;
1607 	   CLOSE c_acct_num;
1608            arp_message.set_name( 'AR', 'HZ_ACCT_MERGE_REL_WARNING' );
1609 	   arp_message.set_token( 'MERGE_FROM_ACCT',l_from_acct_no, FALSE );
1610 	   arp_message.set_token( 'MERGE_TO_ACCT',l_to_acct_no, FALSE );
1611 	 ELSE -- blend the two relationships
1612 	   IF(l_to_bill_to_flag <> 'Y' AND l_to_bill_to_flag <> from_rec.bill_to_flag) THEN
1613 	      l_to_bill_to_flag := from_rec.bill_to_flag;
1614 	      l_update_flag := true;
1615 	   END IF;
1616 	   IF(l_to_ship_to_flag <> 'Y' AND l_to_ship_to_flag <> from_rec.ship_to_flag) THEN
1617 	      l_to_ship_to_flag := from_rec.ship_to_flag;
1618 	      l_update_flag := true;
1619 	   END IF;
1620 	   IF(l_update_flag) THEN
1621              UPDATE hz_cust_acct_relate_all SET bill_to_flag = l_to_bill_to_flag,ship_to_flag=l_to_ship_to_flag
1622    	     WHERE cust_account_id = l_to_cust_account_id
1623 	     AND   related_cust_account_id = l_to_related_cust_account_id
1624 	     AND   org_id = from_rec.org_id
1625 	     AND   STATUS = 'A';
1626            END IF;
1627 	 END IF;
1628 	 --Inactivate the from account relationships
1629 	 UPDATE hz_cust_acct_relate_all yt SET
1630 	     status = 'I',
1631              last_update_date = sysdate,
1632              last_updated_by =hz_utility_v2pub.user_id,
1633              last_update_login = hz_utility_v2pub.last_update_login,
1634              request_id =  req_id,
1635              program_application_id =hz_utility_v2pub.program_application_id,
1636              program_id = hz_utility_v2pub.program_id,
1637              program_update_date = sysdate
1638          WHERE status = 'A'
1639          AND   yt.cust_account_id = from_rec.cust_account_id
1640 	 AND   yt.related_cust_account_id = from_rec.related_cust_account_id
1641 	 AND   yt.org_id = from_rec.org_id;
1642 	 g_count := g_count + sql%rowcount;
1643        END LOOP;
1644        arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
1645        arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
1646 
1647        /***************  'inactivate' mode *************/
1648        --inactivate self relationships
1649        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
1650        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCT_RELATE', FALSE );
1651 
1652        --Start bug 7192302
1653        /*UPDATE HZ_CUST_ACCT_RELATE_ALL yt
1654        SET status = 'I',
1655            last_update_date = sysdate,
1656            last_updated_by = arp_standard.profile.user_id,
1657            last_update_login = arp_standard.profile.last_update_login,
1658            request_id =  req_id,
1659            program_application_id =arp_standard.profile.program_application_id,
1660            program_id = arp_standard.profile.program_id,
1661            program_update_date = sysdate
1662        WHERE EXISTS (
1663 	       SELECT 'relationship to self not allowed'
1664                FROM   ra_customer_merges m
1665                WHERE  m.customer_id = yt.related_cust_account_id
1666                AND    m.duplicate_id = yt.cust_account_id
1667                AND    m.process_flag = 'N'
1668                AND    m.request_id = req_id
1669                AND    m.set_number = set_num
1670 	       AND    m.org_id = yt.org_id
1671                AND    m.delete_duplicate_flag = 'N' )
1672 	OR   EXISTS (
1673                SELECT 'relationship to self not allowed'
1674                FROM   ra_customer_merges m
1675                WHERE  m.customer_id = yt.cust_account_id
1676                AND    m.duplicate_id = yt.related_cust_account_id
1677                AND    m.process_flag = 'N'
1678                AND    m.request_id = req_id
1679                AND    m.set_number = set_num
1680 	       AND    m.org_id = yt.org_id
1681                AND    m.delete_duplicate_flag = 'N' );*/
1682 
1683 	UPDATE	HZ_CUST_ACCT_RELATE_ALL YT
1684 	SET	STATUS = 'I',
1685 		LAST_UPDATE_DATE = SYSDATE,
1686 		LAST_UPDATED_BY = hz_utility_v2pub.user_id ,
1687 		LAST_UPDATE_LOGIN = hz_utility_v2pub.last_update_login ,
1688 		REQUEST_ID = req_id,
1689 		PROGRAM_APPLICATION_ID = hz_utility_v2pub.program_application_id ,
1690 		PROGRAM_ID = hz_utility_v2pub.program_id ,
1691 		PROGRAM_UPDATE_DATE = SYSDATE
1692 	WHERE	(RELATED_CUST_ACCOUNT_ID,CUST_ACCOUNT_ID,ORG_ID ) IN
1693 		(SELECT	CUSTOMER_ID,
1694 			DUPLICATE_ID,
1695 			ORG_ID
1696 		FROM	RA_CUSTOMER_MERGES M
1697 		WHERE	M.PROCESS_FLAG = 'N'
1698 		AND	M.REQUEST_ID = req_id
1699 		AND	M.SET_NUMBER = set_num
1700 		AND	M.DELETE_DUPLICATE_FLAG = 'N' );
1701 
1702        g_count := sql%rowcount;
1703 
1704        UPDATE	HZ_CUST_ACCT_RELATE_ALL YT
1705 	SET	STATUS = 'I',
1706 		LAST_UPDATE_DATE = SYSDATE,
1707 		LAST_UPDATED_BY = hz_utility_v2pub.user_id ,
1708 		LAST_UPDATE_LOGIN = hz_utility_v2pub.last_update_login ,
1709 		REQUEST_ID = req_id,
1710 		PROGRAM_APPLICATION_ID = hz_utility_v2pub.program_application_id ,
1711 		PROGRAM_ID = hz_utility_v2pub.program_id ,
1712 		PROGRAM_UPDATE_DATE = SYSDATE
1713 	WHERE	(CUST_ACCOUNT_ID,RELATED_CUST_ACCOUNT_ID,ORG_ID ) IN
1714 		(SELECT	CUSTOMER_ID,
1715 			DUPLICATE_ID,
1716 			ORG_ID
1717 		FROM	RA_CUSTOMER_MERGES M
1718 		WHERE	M.PROCESS_FLAG = 'N'
1719 		AND	M.REQUEST_ID = req_id
1720 		AND	M.SET_NUMBER = set_num
1721 		AND	M.DELETE_DUPLICATE_FLAG = 'N' );
1722 
1723 	g_count :=g_count + sql%rowcount;
1724 	--End bug 7192302
1725        arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
1726        arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
1727 
1728  --End of Bug No: 4527935
1729        /*************** for 'delete' mode *************/
1730        --delete those relationships that could not be updated
1731        --because it would produce:
1732               --duplicate relationships
1733               --self-relationship
1734 
1735 --Bug 1535542: Mark the rows need to be deleted by setting status to 'D'.
1736 --Physically delete them after merge.
1737 
1738        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
1739        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCT_RELATE', FALSE );
1740 
1741        /************** from account update ************/
1742 
1743        UPDATE HZ_CUST_ACCT_RELATE_ALL yt --SSUptake
1744        SET status = 'D'
1745        WHERE
1746        --NOT EXISTS (
1747        --        SELECT 'accounts exist'
1748        --        FROM   hz_cust_accounts  acct
1749        --        WHERE  acct.cust_account_id = yt.cust_account_id
1750        --        AND    acct.status <> 'D' )
1751        EXISTS  ( --SSUptake
1755 	       AND    m.process_flag = 'N'
1752                SELECT 'Y'
1753                FROM   ra_customer_merges m
1754                WHERE  m.duplicate_id = yt.cust_account_id
1756                AND    m.request_id = req_id
1757                AND    m.set_number = set_num
1758                AND    m.delete_duplicate_flag = 'Y'
1759 	       AND    m.org_id    = yt.org_id ) --SSUptake
1760        AND ( EXISTS (
1761                SELECT 'relationship already exists, cannot update'
1762                FROM   HZ_CUST_ACCT_RELATE_ALL r, --SSUptake
1763                       ra_customer_merges m
1764                WHERE  m.customer_id = r.cust_account_id
1765                AND    m.duplicate_id = yt.cust_account_id
1766                AND    r.related_cust_account_id = yt.related_cust_account_id
1767 	       AND    r.org_id  = yt.org_id --SSUptake
1768                AND    m.process_flag = 'N'
1769                AND    m.request_id = req_id
1770                AND    m.set_number = set_num
1771                AND    m.delete_duplicate_flag = 'Y'
1772 	       AND    m.org_id = r.org_id ) --SSUptake
1773             OR EXISTS (
1774                SELECT 'relationship to self not allowed'
1775                FROM   ra_customer_merges m
1776                WHERE  m.customer_id = yt.related_cust_account_id
1777                AND    m.duplicate_id = yt.cust_account_id
1778                AND    m.process_flag = 'N'
1779                AND    m.request_id = req_id
1780                AND    m.set_number = set_num
1781                AND    m.delete_duplicate_flag = 'Y'
1782 	       AND    m.org_id  = yt.org_id ) ) --SSUptake
1783 --Bug fix 2909303
1784         AND NOT EXISTS(
1785                 select 'merging sites for same customer, cannot update'
1786                 from ra_customer_merges m
1787                 where m.duplicate_id = m.customer_id
1788                 and   m.duplicate_id = yt.cust_account_id
1789                 AND   m.process_flag = 'N'
1790                 AND   m.request_id = req_id
1791                 AND   m.set_number = set_num
1792 		AND   m.org_id     = yt.org_id); --SSUptake
1793 
1794        g_count := sql%rowcount;
1795 
1796        arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
1797        arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
1798 
1799        /************** to account update ************/
1800 
1801        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
1802        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCT_RELATE', FALSE );
1803 
1804        UPDATE HZ_CUST_ACCT_RELATE_ALL yt
1805        SET status = 'D'
1806        WHERE
1807        --NOT EXISTS (
1808        --        SELECT 'accounts exist'
1809        --        FROM   hz_cust_accounts  acct
1810        --        WHERE  acct.cust_account_id = yt.related_cust_account_id
1811        --        AND    acct.status <> 'D' )
1812         EXISTS  (
1813                SELECT 'Y'
1814                FROM   ra_customer_merges m
1815                WHERE  yt.related_cust_account_id = m.duplicate_id
1816 	       AND    m.org_id   = yt.org_id --SSUptake
1817 	       AND    m.process_flag = 'N'
1818                AND    m.request_id = req_id
1819                AND    m.set_number = set_num
1820                AND    m.delete_duplicate_flag = 'Y')
1821        AND ( EXISTS (
1822                SELECT 'relationship already exists, cannot update'
1823                FROM   HZ_CUST_ACCT_RELATE_ALL r,
1824                       ra_customer_merges m
1825                WHERE  m.customer_id = r.related_cust_account_id
1826                AND    m.duplicate_id = yt.related_cust_account_id
1827                AND    r.cust_account_id = yt.cust_account_id
1828 	       AND    r.org_id       = yt.org_id --SSUptake
1829 	       AND    m.org_id       = r.org_id
1830                AND    m.process_flag = 'N'
1831                AND    m.request_id = req_id
1832                AND    m.set_number = set_num
1833                AND    m.delete_duplicate_flag = 'Y')--SSUptake
1834             OR EXISTS (
1835                SELECT 'relationship to self not allowed'
1836                FROM   ra_customer_merges m
1837                WHERE  m.customer_id = yt.cust_account_id
1838                AND    m.duplicate_id = yt.related_cust_account_id
1839 	       AND    m.org_id  = yt.org_id --SSUptake
1840                AND    m.process_flag = 'N'
1841                AND    m.request_id = req_id
1842                AND    m.set_number = set_num
1843                AND    m.delete_duplicate_flag = 'Y' ) )
1844 --Bug fix 2909303
1845         AND NOT EXISTS(
1846                 select 'merging sites for same customer, cannot update'
1847                 from ra_customer_merges m
1848                 where m.duplicate_id = m.customer_id
1849                 and   m.duplicate_id = yt.related_cust_account_id
1850 		AND   m.org_id = yt.org_id  --SSUptake
1851                 AND   m.process_flag = 'N'
1852                 AND   m.request_id = req_id
1853                 AND   m.set_number = set_num );
1854 
1855        g_count := sql%rowcount;
1856 
1857        arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
1858        arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
1859 
1860     END IF;
1861 
1862     arp_message.set_line( 'ARP_CMERGE_ARCUS.RA_CR()-' );
1863 
1864 EXCEPTION
1865 
1866     WHEN OTHERS THEN
1867       arp_message.set_error( 'ARP_CMERGE_ARCUS.RA_CR' );
1868       RAISE;
1869 
1870 END ra_cr;
1871 
1872 /*===========================================================================+
1873  | PROCEDURE
1874  |              ra_crm
1875  |
1876  | DESCRIPTION
1877  |              merge in RA_CUST_RECEIPT_METHODS
1878  |
1879  | SCOPE - PRIVATE
1880  |
1881  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED
1882  |
1883  | ARGUMENTS  : IN:
1884  |                    req_id
1885  |                    set_num
1886  |                    process_mod
1887  |          IN/ OUT:
1888  |
1889  | RETURNS    : NONE
1890  |
1891  | NOTES
1892  |
1893  | MODIFICATION HISTORY
1894  |     Jianying Huang  25-OCT-00  Customer account is global while account
1895  |                        site is stripped by operating unit. We need to
1896  |                        check if this account has (active)sites in
1897  |                        HZ_CUST_ACCT_SITES_ALL.
1898  |     Jianying Huang  12-DEC-00  Modified cursor c2. Replace 'active addresses
1899  |                        exist' with 'active accounts exist'
1900  |     Jianying Huang  20-DEC-00  Bug 1535542: Since we need to change
1901  |                        the merging order, merge HZ tables before merging
1902  |                        products and RA_CUST_RECEIPT_METHODS does
1903  |                        not have status column, we need to move the delete part
1904  |                        to 'delete_rows' procedure.
1905  |     Victoria Crisostomo   01-FEB-01
1906  |                        Bug 1611619 : include customer_id in where condition
1907  |                        of update statement to force use of an existing index
1908  |     S.V.Sowjanya  29-JUL-2004  Bug No 3786802: Declared 3 pl/sql tables "header_id,receipt_id,end_date"
1909  |                                and 1 local variable "new_date".
1910  |                                Customer_merge_header_id , Cust_receipt_method_id and
1911  |                                end_date are bulk collected into the pl/sql tables
1912  |                                header_id,receipt_id,end_date.
1913  |                                While inserting auditing values into the table
1914  |                                HZ_CUSTOMER_MERGE_LOG and updating RA_CUST_RECEIPT_METHODS
1915  |                                , values stored in pl/sql tables are used.
1916  |                                Commented the insert statement and update statements.
1917 |
1918  +===========================================================================*/
1919 
1920 PROCEDURE ra_crm (
1921           req_id                NUMBER,
1922           set_num               NUMBER,
1923           process_mode          VARCHAR2
1924 ) IS
1925 
1926   l_count NUMBER;
1927 
1928     --cursor c1 and c2 are used in 'inactivate' mode
1929     CURSOR c1 IS
1930         SELECT CUST_RECEIPT_METHOD_ID
1931         FROM   RA_CUST_RECEIPT_METHODS ra, ra_customer_merges m
1932         WHERE ra.customer_id = m.duplicate_id
1933         AND   site_use_id = m.duplicate_site_id
1934         AND   m.process_flag = 'N'
1935 	AND   m.request_id = req_id
1936         AND   m.set_number = set_num
1937         AND   m.delete_duplicate_flag = 'N'
1938         FOR UPDATE NOWAIT;
1939 
1940     CURSOR c2 IS
1941         SELECT CUST_RECEIPT_METHOD_ID
1942         FROM RA_CUST_RECEIPT_METHODS yt, ra_customer_merges m
1943         WHERE yt.customer_id = m.duplicate_id
1944         AND   m.process_flag = 'N'
1945 	AND   m.request_id = req_id
1946         AND   m.set_number = set_num
1947         AND   m.delete_duplicate_flag = 'N'
1948         AND site_use_id IS NULL
1949         AND NOT EXISTS (
1950                     SELECT 'active accounts exist'
1951                     FROM   hz_cust_accounts acct
1952                     WHERE  acct.cust_account_id = yt.customer_id
1953                     AND    acct.status = 'A' )
1954         FOR UPDATE NOWAIT;
1955 
1956     TYPE customer_merge_header_id_tab IS TABLE OF RA_CUSTOMER_MERGES.CUSTOMER_MERGE_HEADER_ID%TYPE INDEX BY BINARY_INTEGER;
1957     TYPE cust_receipt_method_id_tab IS TABLE OF RA_CUST_RECEIPT_METHODS.CUST_RECEIPT_METHOD_ID%TYPE INDEX BY BINARY_INTEGER;
1958     TYPE end_date_tab   IS TABLE OF RA_CUST_RECEIPT_METHODS.END_DATE%TYPE INDEX BY BINARY_INTEGER;
1959 
1960     header_id customer_merge_header_id_tab;
1961     receipt_id cust_receipt_method_id_tab;
1962     end_date end_date_tab;
1963     new_date date;
1964 BEGIN
1965 
1966     arp_message.set_line( 'ARP_CMERGE_ARCUS.RA_CRM()+' );
1967 
1968     /* locking tables by opening and closing cursors */
1969     IF process_mode = 'LOCK' THEN
1970 
1971        arp_message.set_name( 'AR', 'AR_LOCKING_TABLE');
1972        arp_message.set_token( 'TABLE_NAME', 'RA_CUST_RECEIPT_METHODS', FALSE );
1973 
1974        OPEN c1;
1975        CLOSE c1;
1976 
1977        OPEN c2;
1978        CLOSE c2;
1979 
1980     ELSE
1981 
1982        /************** account site level inactivate ************/
1983 
1984        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
1985        arp_message.set_token( 'TABLE_NAME', 'RA_CUST_RECEIPT_METHODS', FALSE );
1986 
1987 --- bug 3786802
1988 
1989      SELECT distinct CUSTOMER_MERGE_HEADER_ID,
1990              CUST_RECEIPT_METHOD_ID,
1991              END_DATE
1992      BULK COLLECT INTO header_id,receipt_id,end_date
1993      FROM RA_CUST_RECEIPT_METHODS yt, ra_customer_merges m
1994      WHERE   (yt.CUSTOMER_ID = m.DUPLICATE_ID
1995               AND ( ( yt.SITE_USE_ID IS NULL
1996                       AND NOT EXISTS (
1997                      SELECT 'active accounts exist'
1998                      FROM   hz_cust_accounts acct
1999                      WHERE  acct.cust_account_id = yt.customer_id
2000                      AND    acct.status = 'A' )
2001                     )
2002                  OR (yt.site_use_id = m.DUPLICATE_SITE_ID)
2003                  )
2004               )
2005             AND    m.process_flag = 'N'
2006             AND    m.request_id = req_id
2007             AND    m.set_number = set_num;
2008 
2009        ---Inserting in the log table for Auditing
2010 
2011         new_date := sysdate;
2012         FORALL i IN 1..header_id.count
2013 
2014        INSERT INTO HZ_CUSTOMER_MERGE_LOG (
2015        MERGE_LOG_ID,
2016        MERGE_HEADER_ID,
2017        TABLE_NAME,
2018        PRIMARY_KEY_ID,
2019        DATE_COL1_ORIG,
2020        DATE_COL1_NEW,
2021        REQUEST_ID,
2022 
2023 -- Bug 2707587 : Added standard who columns (created_by, creation_date,
2024 --		 last_update_by, last_update_date, last_update_login)
2025 --		 and ACTION_FLAG column into insert statemnt.
2026 
2027 	CREATED_BY,
2028 	CREATION_DATE,
2029 	LAST_UPDATED_BY,
2030 	LAST_UPDATE_DATE,
2031 	LAST_UPDATE_LOGIN,
2032 	ACTION_FLAG
2033       )
2034 
2035       VALUES (
2036        HZ_CUSTOMER_MERGE_LOG_s.nextval,
2037        header_id(i),
2038       'RA_CUST_RECEIPT_METHODS',
2039        receipt_id(i),
2040        end_date(i),
2041        new_date,
2042        req_id,
2043        hz_utility_v2pub.CREATED_BY,
2044        hz_utility_v2pub.CREATION_DATE,
2045        hz_utility_v2pub.LAST_UPDATED_BY,
2046        hz_utility_v2pub.LAST_UPDATE_DATE,
2047        hz_utility_v2pub.LAST_UPDATE_LOGIN,
2048        'U'
2049        ) ;
2050 
2051 -- Commented for bug 3786802
2052     /*   INSERT INTO HZ_CUSTOMER_MERGE_LOG (
2053        MERGE_LOG_ID,
2054        MERGE_HEADER_ID,
2055        TABLE_NAME,
2056        PRIMARY_KEY_ID,
2057        DATE_COL1_ORIG,
2058        DATE_COL1_NEW,
2059        REQUEST_ID,
2060 
2061 -- Bug 2707587 : Added standard who columns (created_by, creation_date,
2062 --		 last_update_by, last_update_date, last_update_login)
2063 --		 and ACTION_FLAG column into insert statemnt.
2064 
2065 	CREATED_BY,
2066 	CREATION_DATE,
2067 	LAST_UPDATED_BY,
2068 	LAST_UPDATE_DATE,
2069 	LAST_UPDATE_LOGIN,
2070 	ACTION_FLAG
2071 
2072 
2073     ) SELECT HZ_CUSTOMER_MERGE_LOG_s.nextval,
2074              CUSTOMER_MERGE_HEADER_ID,
2075              'RA_CUST_RECEIPT_METHODS',
2076              CUST_RECEIPT_METHOD_ID,
2077              END_DATE,
2078              SYSDATE,
2079              req_id,
2080 	     hz_utility_v2pub.CREATED_BY,
2081 	     hz_utility_v2pub.CREATION_DATE,
2082 	     hz_utility_v2pub.LAST_UPDATED_BY,
2083 	     hz_utility_v2pub.LAST_UPDATE_DATE,
2084 	     hz_utility_v2pub.LAST_UPDATE_LOGIN,
2085 	     'U'
2086 
2087      FROM (
2088        SELECT distinct CUSTOMER_MERGE_HEADER_ID,
2089              CUST_RECEIPT_METHOD_ID,
2090              END_DATE
2091      FROM RA_CUST_RECEIPT_METHODS yt, ra_customer_merges m
2092      WHERE   (yt.CUSTOMER_ID = m.DUPLICATE_ID
2093               AND ( ( yt.SITE_USE_ID IS NULL
2094                       AND NOT EXISTS (
2095                      SELECT 'active accounts exist'
2096                      FROM   hz_cust_accounts acct
2097                      WHERE  acct.cust_account_id = yt.customer_id
2098                      AND    acct.status = 'A' )
2099                     )
2100                  OR (yt.site_use_id = m.DUPLICATE_SITE_ID)
2101                  )
2102               )
2103             AND    m.process_flag = 'N'
2104             AND    m.request_id = req_id
2105             AND    m.set_number = set_num
2106 	);*/
2107 
2108      /*
2109      |  --- bug 1611619 : put customer_id in where clause to use index
2110      |  UPDATE RA_CUST_RECEIPT_METHODS yt
2111      |  SET end_date = sysdate,
2112      |      last_update_date = sysdate,
2113      |      last_updated_by = arp_standard.profile.user_id,
2114      |      last_update_login = arp_standard.profile.last_update_login,
2115      |      request_id =  req_id,
2116      |      program_application_id =arp_standard.profile.program_application_id,
2117      |      program_id = arp_standard.profile.program_id,
2118      |      program_update_date = sysdate
2119      |  WHERE (customer_id, site_use_id) IN (
2120      |            SELECT m.duplicate_id, m.duplicate_site_id
2121      |          	 FROM   ra_customer_merges m
2122      |            WHERE  m.process_flag = 'N'
2123      |   AND    m.request_id = req_id
2124      |            AND    m.set_number = set_num
2125      |            AND    m.delete_duplicate_flag = 'N' ); */
2126 
2127 -- Commented for bug 3786802
2128 /*
2129       UPDATE RA_CUST_RECEIPT_METHODS yt SET (
2130       END_DATE) = (
2131            SELECT DATE_COL1_NEW
2132            FROM HZ_CUSTOMER_MERGE_LOG l
2133            WHERE l.REQUEST_ID = req_id
2134            AND l.TABLE_NAME = 'RA_CUST_RECEIPT_METHODS'
2135            AND l.PRIMARY_KEY_ID = CUST_RECEIPT_METHOD_ID
2136       )
2137        , LAST_UPDATE_DATE=SYSDATE
2138        , last_updated_by=arp_standard.profile.user_id
2139        , last_update_login=arp_standard.profile.last_update_login
2140        , REQUEST_ID=req_id
2141        , PROGRAM_APPLICATION_ID=arp_standard.profile.program_application_id
2142        , PROGRAM_ID=arp_standard.profile.program_id
2143        , PROGRAM_UPDATE_DATE=SYSDATE
2144       WHERE (CUST_RECEIPT_METHOD_ID) in (
2145          SELECT PRIMARY_KEY_ID
2146          FROM HZ_CUSTOMER_MERGE_LOG l1, RA_CUSTOMER_MERGES h
2147          WHERE h.CUSTOMER_MERGE_HEADER_ID = l1.MERGE_HEADER_ID
2148          AND l1.TABLE_NAME = 'RA_CUST_RECEIPT_METHODS'
2149          AND l1.REQUEST_ID = req_id
2150          AND h.set_number = set_num);
2151 
2152 */
2153 
2154 
2155       FORALL i in 1..receipt_id.count
2156 
2157 
2158       UPDATE RA_CUST_RECEIPT_METHODS yt SET
2159       END_DATE = new_date
2160       , LAST_UPDATE_DATE=sysdate
2161       , last_updated_by=hz_utility_v2pub.user_id--arp_standard.profile.user_id
2162       , last_update_login=hz_utility_v2pub.last_update_login--arp_standard.profile.last_update_login
2163       , REQUEST_ID=req_id
2164       , PROGRAM_APPLICATION_ID=hz_utility_v2pub.program_application_id--arp_standard.profile.program_application_id
2165       , PROGRAM_ID=hz_utility_v2pub.program_id--arp_standard.profile.program_id
2166       , PROGRAM_UPDATE_DATE=SYSDATE
2167       WHERE CUST_RECEIPT_METHOD_ID = receipt_id(i);
2168 
2169        g_count := sql%rowcount;
2170 
2171        arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
2172        arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
2173 
2174        /************** account level inactivate ************/
2175 
2176     /*   arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
2177     |   arp_message.set_token( 'TABLE_NAME', 'RA_CUST_RECEIPT_METHODS', FALSE );
2178 
2179     |
2180     |  UPDATE RA_CUST_RECEIPT_METHODS yt
2181     |   set end_date = sysdate,
2182     |       last_update_date = sysdate,
2183     |       last_updated_by = arp_standard.profile.user_id,
2184     |       last_update_login = arp_standard.profile.last_update_login,
2185     |       request_id =  req_id,
2186     |       program_application_id =arp_standard.profile.program_application_id,
2187     |       program_id = arp_standard.profile.program_id,
2188     |       program_update_date = sysdate
2189     |   WHERE customer_id IN (
2190     |            SELECT m.duplicate_id
2191     |            FROM   ra_customer_merges m
2192     |            WHERE  m.process_flag = 'N'
2193     |            AND    m.request_id = req_id
2194     |            AND    m.set_number = set_num
2195     |            AND    m.delete_duplicate_flag = 'N' )
2196     |   AND site_use_id IS NULL
2197     |   AND NOT EXISTS (
2198     |            SELECT 'active accounts exist'
2199     |            FROM   hz_cust_accounts acct
2200     |            WHERE  acct.cust_account_id = yt.customer_id
2201     |            AND    acct.status = 'A' );
2202 
2203     |   g_count := sql%rowcount;
2204 
2205     |   arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
2206     |   arp_message.set_token( 'NUM_ROWS', to_char(g_count) ); */
2207 
2208     END IF;
2209 
2210     arp_message.set_line( 'ARP_CMERGE_ARCUS.RA_CRM()-' );
2211 
2212 EXCEPTION
2213 
2214     WHEN OTHERS THEN
2215       arp_message.set_error( 'ARP_CMERGE_ARCUS.RA_CRM' );
2216       RAISE;
2217 
2218 END ra_crm;
2219 
2220 /*===========================================================================+
2221  | PROCEDURE
2222  |              copy_contacts
2223  |
2224  | DESCRIPTION
2225  |              Migrate contacts and contact points.
2226  |
2227  | SCOPE - PRIVATE
2228  |
2229  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED
2230  |
2231  | ARGUMENTS  : IN:
2232  |                    req_id
2233  |                    set_num
2234  |                    process_mod
2235  |          IN/ OUT:
2236  |
2237  | RETURNS    : NONE
2238  |
2239  | NOTES
2240  |
2241  | MODIFICATION HISTORY
2242  |     Jianying Huang  26-OCT-00  Bug 1415529: The procedure has been added
2243  |                        to move out do_cust(site)_merge_contacts(cpoint)
2244  |                        from ra_cont and ra_ph. It should be called before
2245  |                        ra_addr and ra_cust. The procedure should be called
2246  |                        after locking tables.
2247  |     Jianying Huang  17-DEC-00  Bug 1535542: Since we will not physically
2248  |                        delete rows till the end of merge, we can move
2249  |                        the call of 'copy_contacts' right before we migrate
2250  |                        org contacts and contact points.
2251  |
2252  +===========================================================================*/
2253 
2254 PROCEDURE copy_contacts (
2255           req_id                NUMBER,
2256           set_num               NUMBER,
2257           process_mode          VARCHAR2
2258 ) IS
2259 
2260     --party ids
2261     from_party_id               NUMBER;
2262     to_party_id                 NUMBER;
2263 
2264     --account ids
2265     from_account_id             NUMBER;
2266     to_account_id               NUMBER;
2267     from_account_status         VARCHAR2(1);
2268 
2269     CURSOR accounts IS
2270         SELECT DISTINCT duplicate_id, customer_id
2271         FROM   ra_customer_merges
2272         WHERE  process_flag = 'N'
2273         AND    request_id = req_id
2274         AND    set_number = set_num
2275         AND    duplicate_id <> customer_id;
2276 
2277 BEGIN
2278 
2279     arp_message.set_line( 'ARP_CMERGE_ARCUS.copy_contacts()+' );
2280 
2281     OPEN accounts;
2282     LOOP
2283        FETCH accounts INTO from_account_id, to_account_id;
2284        EXIT WHEN accounts%NOTFOUND;
2285 
2286 /**
2287        arp_message.set_line(
2288            'ARP_CMERGE_ARCUS:copy_contacts():from_account_id ' ||
2289            from_account_id );
2290        arp_message.set_line(
2291            'ARP_CMERGE_ARCUS:copy_contacts():to_account_id  ' ||
2292            to_account_id );
2293 **/
2294 
2295        SELECT party_id, status into from_party_id, from_account_status
2296        FROM   hz_cust_accounts
2297        WHERE  cust_account_id = from_account_id;
2298 
2299        SELECT party_id into to_party_id
2300        FROM   hz_cust_accounts
2301        WHERE  cust_account_id = to_account_id;
2302 
2303 /**
2304        arp_message.set_line(
2305            'ARP_CMERGE_ARCUS:copy_contacts():from_party_id '||
2306            from_party_id );
2307        arp_message.set_line(
2308            'ARP_CMERGE_ARCUS:copy_contacts():to_party_id '||
2309            to_party_id );
2310 **/
2311 
2312        --Based on customer merge high level design:
2313        --org contact or contact points that the customer contact
2314        --record refers to must be copied, if only if the merge-from
2315        --party and merge-to party are different.
2316 
2317        IF to_party_id = from_party_id THEN
2318 
2319 /**
2320           arp_message.set_line (
2321               'ARP_CMERGE_ARCUS:copy_contacts(): ' ||
2322               'merge_from party and merge_to party are same. ' ||
2323               'Do not need copy org contact and contact points in account level.' );
2324 **/
2325           NULL;
2326 
2327        ELSE
2328 
2329           IF (FROM_ACCOUNT_STATUS <> 'A' OR G_TRANSFER_ACCOUNT_CONTACTS IS NULL OR G_TRANSFER_ACCOUNT_CONTACTS = 'N' ) THEN
2330 
2331           --merge cust account roles in account level
2332           do_cust_merge_contacts (
2333                   from_party_id, to_party_id,
2334                   from_account_id, to_account_id );
2335           END IF;
2336 
2337           --merge cust account roles in account site level.
2338           do_site_merge_contacts (
2339                   from_party_id, to_party_id,
2340                   from_account_id, to_account_id,
2341                   req_id, set_num );
2342 
2343           --merge cust contact points in account level
2344           do_cust_merge_cpoint(
2345                   from_party_id, to_party_id,
2346                   from_account_id, to_account_id );
2347        END IF;
2348 
2349     END LOOP;
2350     CLOSE accounts;
2351 
2352     arp_message.set_line( 'ARP_CMERGE_ARCUS.copy_contacts()-' );
2353 
2354 EXCEPTION
2355 
2356     WHEN OTHERS THEN
2357       arp_message.set_error( 'ARP_CMERGE_ARCUS.copy_contacts' );
2358       RAISE;
2359 
2360 END copy_contacts;
2361 
2362 /*===========================================================================+
2363  | PROCEDURE
2364  |              copy_contacts_in_sites
2365  |
2366  | DESCRIPTION
2367  |              Migrate contact points in site level.
2368  |
2369  | SCOPE - PRIVATE
2370  |
2371  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED
2372  |
2373  | ARGUMENTS  : IN:
2374  |                    req_id
2375  |                    set_num
2376  |                    process_mod
2377  |          IN/ OUT:
2378  |
2379  | RETURNS    : NONE
2380  |
2381  | NOTES
2382  |
2383  | MODIFICATION HISTORY
2384  |     Jianying Huang  29-DEC-00  Created. We need to copy contact points
2385  |                        as long as we are merging different party sites.
2386  |
2387  +===========================================================================*/
2388 
2389 PROCEDURE copy_contacts_in_sites (
2390           req_id                NUMBER,
2391           set_num               NUMBER,
2392           process_mode          VARCHAR2
2393 ) IS
2394 
2395     --party ids
2396     from_party_site_id               NUMBER;
2397     to_party_site_id                 NUMBER;
2398 
2399     --account ids
2400     from_account_id                  NUMBER;
2401     to_account_id                    NUMBER;
2402 
2403     --account sites
2404     from_site_id                     NUMBER;
2405     to_site_id                       NUMBER;
2406 
2407     CURSOR sites IS
2408         SELECT DISTINCT duplicate_id, customer_id,
2409                duplicate_address_id, customer_address_id
2410         FROM   ra_customer_merges
2411         WHERE  process_flag = 'N'
2412         AND    request_id = req_id
2413         AND    set_number = set_num
2414 	AND    duplicate_address_id <> -1; --4693912
2415 
2416 BEGIN
2417 
2418     arp_message.set_line( 'ARP_CMERGE_ARCUS.copy_contacts_in_sites()+' );
2419 
2420     OPEN sites;
2421     LOOP
2422        FETCH sites INTO
2423           from_account_id, to_account_id, from_site_id, to_site_id;
2424        EXIT WHEN sites%NOTFOUND;
2425 
2426 /**
2427        arp_message.set_line(
2428            'ARP_CMERGE_ARCUS:copy_contacts_in_sites():from_site_id ' ||
2429            from_site_id );
2430        arp_message.set_line(
2431            'ARP_CMERGE_ARCUS:copy_contacts_in_sites():to_site_id  ' ||
2432            to_site_id );
2433 **/
2434 
2435        SELECT party_site_id into from_party_site_id
2436        FROM   hz_cust_acct_sites_all
2437        WHERE  cust_acct_site_id = from_site_id;
2438 
2439        SELECT party_site_id into to_party_site_id
2440        FROM   hz_cust_acct_sites_all
2441        WHERE  cust_acct_site_id = to_site_id;
2442 
2443 /**
2444        arp_message.set_line(
2445            'ARP_CMERGE_ARCUS:copy_contacts():from_party_site_id '||
2446            from_party_site_id );
2447        arp_message.set_line(
2448            'ARP_CMERGE_ARCUS:copy_contacts():to_party_site_id '||
2449            to_party_site_id );
2450 **/
2451 
2452        --Based on customer merge high level design:
2453        --org contact or contact points that the customer contact
2454        --record refers to must be copied, if only if the merge-from
2455        --party_site and merge-to party_site are different.
2456 
2457        IF to_party_site_id = from_party_site_id THEN
2458 
2459 /**
2460           arp_message.set_line (
2461               'ARP_CMERGE_ARCUS:copy_contacts(): ' ||
2462               'merge_from party and merge_to party site are same. ' ||
2463               'Do not need copy contact points.' );
2464 **/
2465 
2466           NULL;
2467 
2468        ELSE
2469 
2470           --merge cust contact points in account site level
2471           do_site_merge_cpoint(
2472                   from_party_site_id, to_party_site_id,
2473                   from_account_id, to_account_id );
2474        END IF;
2475 
2476     END LOOP;
2477     CLOSE sites;
2478 
2479     arp_message.set_line( 'ARP_CMERGE_ARCUS.copy_contacts_in_sites()-' );
2480 
2481 EXCEPTION
2482 
2483     WHEN OTHERS THEN
2484       arp_message.set_error( 'ARP_CMERGE_ARCUS.copy_contacts_in_sites' );
2485       RAISE;
2486 
2487 END copy_contacts_in_sites;
2488 
2489 /*===========================================================================+
2490  | PROCEDURE
2491  |              ra_cont
2492  |
2493  | DESCRIPTION
2494  |              merge in HZ_CUST_ACCOUNT_ROLES with
2495  |              role_type = 'CONTACT'
2496  |
2497  | SCOPE - PRIVATE
2498  |
2499  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED
2500  |
2501  | ARGUMENTS  : IN:
2502  |                    req_id
2503  |                    set_num
2504  |                    process_mod
2505  |          IN/ OUT:
2506  |
2507  | RETURNS    : NONE
2508  |
2509  | NOTES
2510  |
2511  | MODIFICATION HISTORY
2512  |
2513  +===========================================================================*/
2514 
2515 PROCEDURE ra_cont (
2516           req_id                NUMBER,
2517           set_num               NUMBER,
2518           process_mode          VARCHAR2
2519 ) IS
2520 
2521     l_party_id       HZ_CUST_ACCOUNT_ROLES.PARTY_ID%TYPE;
2522 
2523     CURSOR c1 IS
2524         SELECT cust_account_role_id
2525         FROM   hz_cust_account_roles yt, ra_customer_merges m
2526         WHERE  cust_acct_site_id = m.duplicate_address_id
2527         AND    m.process_flag = 'N'
2528 	AND    m.request_id = req_id
2529 	AND    m.set_number = set_num
2530         AND    role_type = 'CONTACT'
2531         FOR UPDATE NOWAIT;
2532 
2533     CURSOR c2 IS
2534         SELECT cust_account_role_id
2535         FROM hz_cust_account_roles yt, ra_customer_merges m
2536         WHERE cust_account_id = m.duplicate_id
2537         AND   m.process_flag = 'N'
2538 	AND   m.request_id = req_id
2539         AND   m.set_number = set_num
2540         AND   cust_acct_site_id IS NULL
2541         AND   role_type = 'CONTACT'
2542         FOR UPDATE NOWAIT;
2543 
2544         -- Begin Bug No : 14201229
2545 
2546    CURSOR c2_1 IS
2547         SELECT cust_account_role_id
2548         FROM hz_cust_account_roles yt, ra_customer_merges m, hz_cust_accounts c
2549         WHERE yt.cust_account_id = m.duplicate_id
2550         AND   c.cust_account_id = m.duplicate_id
2551         AND   c.status <> 'A'
2552         AND   m.process_flag = 'N'
2553 	AND   m.request_id = req_id
2554         AND   m.set_number = set_num
2555         AND   yt.cust_acct_site_id IS NULL
2556         AND   yt.role_type = 'CONTACT'
2557         FOR UPDATE NOWAIT;
2558 
2559    CURSOR c3_1 is
2560         SELECT yt.party_id
2561         FROM HZ_CUST_ACCOUNT_ROLES yt,ra_customer_merges m,HZ_CUST_ACCOUNTS ca,hz_relationships rel
2562         WHERE m.customer_id=ca.cust_account_id
2563         AND ((ca.status = 'A' and yt.cust_acct_site_id is not null) or (ca.status <> 'A'))
2564         AND m.duplicate_id = yt.cust_account_id
2565         AND rel.party_id = yt.party_id
2566         AND rel.subject_type = 'PERSON'
2567         AND rel.subject_id = ca.party_id
2568         AND yt.role_type = 'CONTACT'
2569         AND m.request_id = req_id
2570         AND m.process_flag ='N'
2571         AND m.set_number =set_num
2572         AND rownum =1;
2573 
2574 -- End Bug No : 14201229
2575  ----Bug No: 5067291
2576    CURSOR c3 is
2577         SELECT yt.party_id
2578         FROM HZ_CUST_ACCOUNT_ROLES yt,ra_customer_merges m,HZ_CUST_ACCOUNTS ca,hz_relationships rel
2579         WHERE m.customer_id=ca.cust_account_id
2580         AND m.duplicate_id = yt.cust_account_id
2581         AND rel.party_id = yt.party_id
2582         AND rel.subject_type = 'PERSON'
2583         AND rel.subject_id = ca.party_id
2584         AND yt.role_type = 'CONTACT'
2585         AND m.request_id = req_id
2586         AND m.process_flag ='N'
2587         AND m.set_number =set_num
2588         AND rownum =1;
2589 
2590 ---Bug NO: 5067291
2591 
2592 BEGIN
2593 
2594     arp_message.set_line( 'ARP_CMERGE_ARCUS.RA_CONT()+' );
2595 
2596     /* locking tables by opening and closing cursors */
2597     IF process_mode = 'LOCK' then
2598 
2599        arp_message.set_name( 'AR', 'AR_LOCKING_TABLE' );
2600        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCOUNT_ROLES', FALSE );
2601 
2602        OPEN c1;
2603        CLOSE c1;
2604 
2605 
2606       IF (g_transfer_account_contacts = 'Y') THEN
2607 
2608        OPEN c2_1;
2609        CLOSE c2_1;
2610 
2611       ELSE
2612 
2613        OPEN c2;
2614        CLOSE c2;
2615 
2616       END IF;
2617     ELSE
2618 
2619      IF G_TRANSFER_ACCOUNT_CONTACTS IS NULL OR G_TRANSFER_ACCOUNT_CONTACTS = 'N' THEN
2620 
2621     OPEN c3;--Bug NO: 5067291
2622        FETCH c3 INTO l_party_id;
2623        IF c3%FOUND THEN
2624           /*********************Inactivate account site/account role...Bug No. 5067291*********/
2625      arp_message.set_name( 'AR', 'AR_UPDATING_TABLE');
2626        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCOUNT_ROLES', FALSE );
2627        UPDATE HZ_CUST_ACCOUNT_ROLES yt
2628       SET  status = 'I',
2629            last_update_date = sysdate,
2630            last_updated_by =  hz_utility_v2pub.user_id,
2631            last_update_login = hz_utility_v2pub.last_update_login,
2632            request_id =  req_id,
2633            program_application_id =  hz_utility_v2pub.program_application_id,
2634            program_id = hz_utility_v2pub.program_id,
2635            program_update_date = sysdate
2636        WHERE party_id = l_party_id
2637        AND nvl(status,'A') ='A';
2638 
2639        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE');
2640        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCOUNT_ROLES', FALSE );
2641 
2642 
2643        g_count := sql%rowcount;
2644 
2645        arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
2646        arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
2647 
2648     END IF;
2649 
2650        /************** account site level update ************/
2651 
2652        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE');
2653        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCOUNT_ROLES', FALSE );
2654 
2655        UPDATE HZ_CUST_ACCOUNT_ROLES yt
2656        SET (cust_account_id, cust_acct_site_id) = (
2657                 SELECT MIN(m.customer_id), MIN(m.customer_address_id)
2658                 FROM   ra_customer_merges m
2659                 WHERE  yt.cust_account_id = m.duplicate_id
2660                 AND    yt.cust_acct_site_id = m.duplicate_address_id
2661                 AND    m.request_id = req_id
2662                 AND    m.set_number = set_num
2663                 AND    m.process_flag = 'N' ),
2664            last_update_date = sysdate,
2665            last_updated_by = hz_utility_v2pub.user_id,--arp_standard.profile.user_id,
2666            last_update_login =hz_utility_v2pub.last_update_login,-- arp_standard.profile.last_update_login,
2667            request_id =  req_id,
2668            program_application_id =hz_utility_v2pub.program_application_id,-- arp_standard.profile.program_application_id,
2669            program_id =hz_utility_v2pub.program_id,-- arp_standard.profile.program_id,
2670            program_update_date = sysdate
2671        WHERE cust_acct_site_id IN (
2672                 SELECT m.duplicate_address_id
2673                 FROM   ra_customer_merges m
2674                 WHERE  m.process_flag = 'N'
2675 		AND    m.request_id = req_id
2676 		AND    m.set_number = set_num )
2677        AND role_type = 'CONTACT'
2678       AND party_id <> nvl(l_party_id,-99);---Bug No. 5067291
2679 
2680        g_count := sql%rowcount;
2681 
2682        arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
2683        arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
2684 
2685        /************** account level update ************/
2686 
2687        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
2688        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCOUNT_ROLES', FALSE );
2689 
2690        UPDATE HZ_CUST_ACCOUNT_ROLES yt
2691        SET cust_account_id = (
2692                SELECT m.customer_id
2693                FROM   ra_customer_merges m
2694                WHERE  yt.cust_account_id = m.duplicate_id
2695 	       AND    m.request_id = req_id
2696                AND    m.process_flag = 'N'
2697 	       AND    m.set_number = set_num
2698                AND    ROWNUM = 1 ),
2699            last_update_date = sysdate,
2700            last_updated_by = hz_utility_v2pub.user_id,--arp_standard.profile.user_id,
2701            last_update_login =hz_utility_v2pub.last_update_login,-- arp_standard.profile.last_update_login,
2702            request_id =  req_id,
2703            program_application_id =hz_utility_v2pub.program_application_id,-- arp_standard.profile.program_application_id,
2704            program_id =hz_utility_v2pub.program_id,-- arp_standard.profile.program_id,
2705            program_update_date = sysdate
2706        WHERE cust_account_id IN (
2707                SELECT m.duplicate_id
2708                FROM   ra_customer_merges m
2709                WHERE  m.process_flag = 'N'
2710 	       AND    m.request_id = req_id
2711                AND    m.set_number = set_num)
2712        AND cust_acct_site_id IS NULL
2713        AND role_type = 'CONTACT'
2714       AND party_id <> nvl(l_party_id,-99);--Bug NO. 5067291
2715 
2716 
2717      -- Start Bug 4712462
2718      FOR rec IN (
2719        SELECT min(cust_account_role_id) cust_account_role_id, cust_account_id,
2720               nvl(cust_acct_site_id,-1) cust_acct_site_id,party_id
2721        FROM   hz_cust_account_roles r, ra_customer_merges m
2722        where m.request_id = req_id AND    m.set_number = set_num
2723        AND r.cust_account_id = m.customer_id AND m.process_flag = 'N'
2724        GROUP BY cust_account_id,cust_acct_site_id,status,party_id
2725        HAVING NVL(STATUS,'A') ='A' AND count(1) > 1) LOOP
2726        UPDATE hz_cust_account_roles SET status ='I'
2727        WHERE  cust_account_role_id     <> rec.cust_account_role_id
2728        AND    cust_account_id           = rec.cust_account_id
2729        AND    party_id                  = rec.party_id
2730        AND    nvl(cust_acct_site_id,-1) = rec.cust_acct_site_id
2731        AND    nvl(status,'A')           = 'A';
2732 
2733      END LOOP;
2734 
2735     -- End Bug 4712462
2736 
2737        g_count := sql%rowcount;
2738 
2739        arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
2740        arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
2741    CLOSE C3;--5067291
2742 
2743    elsif g_transfer_account_contacts = 'Y' THEN
2744 
2745        OPEN c3_1;--Bug NO: 5067291
2746        FETCH c3_1 INTO l_party_id;
2747        IF c3_1%FOUND THEN
2748           /*********************Inactivate account site/account role...Bug No. 5067291*********/
2749      arp_message.set_name( 'AR', 'AR_UPDATING_TABLE');
2750        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCOUNT_ROLES', FALSE );
2751        UPDATE HZ_CUST_ACCOUNT_ROLES yt
2752       SET  status = 'I',
2753            last_update_date = sysdate,
2754            last_updated_by =  hz_utility_v2pub.user_id,
2755            last_update_login = hz_utility_v2pub.last_update_login,
2756            request_id =  req_id,
2757            program_application_id =  hz_utility_v2pub.program_application_id,
2758            program_id = hz_utility_v2pub.program_id,
2759            program_update_date = sysdate
2760        WHERE party_id = l_party_id
2761        AND nvl(status,'A') ='A';
2762 
2763        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE');
2764        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCOUNT_ROLES', FALSE );
2765 
2766        g_count := sql%rowcount;
2767 
2768        arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
2769        arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
2770 
2771       END IF;
2772 
2773              /************** account site level update ************/
2774 
2775        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE');
2776        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCOUNT_ROLES', FALSE );
2777 
2778        UPDATE HZ_CUST_ACCOUNT_ROLES yt
2779        SET (cust_account_id, cust_acct_site_id) = (
2780                 SELECT MIN(m.customer_id), MIN(m.customer_address_id)
2781                 FROM   ra_customer_merges m
2782                 WHERE  yt.cust_account_id = m.duplicate_id
2783                 AND    yt.cust_acct_site_id = m.duplicate_address_id
2784                 AND    m.request_id = req_id
2785                 AND    m.set_number = set_num
2786                 AND    m.process_flag = 'N' ),
2787            last_update_date = sysdate,
2788            last_updated_by = hz_utility_v2pub.user_id,--arp_standard.profile.user_id,
2789            last_update_login =hz_utility_v2pub.last_update_login,-- arp_standard.profile.last_update_login,
2790            request_id =  req_id,
2794        WHERE cust_acct_site_id IN (
2791            program_application_id =hz_utility_v2pub.program_application_id,-- arp_standard.profile.program_application_id,
2792            program_id =hz_utility_v2pub.program_id,-- arp_standard.profile.program_id,
2793            program_update_date = sysdate
2795                 SELECT m.duplicate_address_id
2796                 FROM   ra_customer_merges m
2797                 WHERE  M.PROCESS_FLAG = 'N'
2798 		            AND    m.request_id = req_id
2799                 AND    M.SET_NUMBER = SET_NUM )
2800                 AND role_type = 'CONTACT'
2801                 AND party_id <> nvl(l_party_id,-99);---Bug No. 5067291
2802 
2803        g_count := sql%rowcount;
2804 
2805        ARP_MESSAGE.SET_NAME( 'AR', 'AR_ROWS_UPDATED' );
2806        ARP_MESSAGE.SET_TOKEN( 'NUM_ROWS', TO_CHAR(G_COUNT) );
2807 
2808               /************** account level update ************/
2809 
2810        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
2811        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCOUNT_ROLES', FALSE );
2812 
2813        UPDATE HZ_CUST_ACCOUNT_ROLES yt
2814        SET cust_account_id = (
2815                SELECT m.customer_id
2816                FROM   ra_customer_merges m
2817                WHERE  yt.cust_account_id = m.duplicate_id
2818 	       AND    m.request_id = req_id
2819                AND    m.process_flag = 'N'
2820 	       AND    m.set_number = set_num
2821                AND    ROWNUM = 1 ),
2822            last_update_date = sysdate,
2823            last_updated_by = hz_utility_v2pub.user_id,--arp_standard.profile.user_id,
2824            last_update_login =hz_utility_v2pub.last_update_login,-- arp_standard.profile.last_update_login,
2825            request_id =  req_id,
2826            program_application_id =hz_utility_v2pub.program_application_id,-- arp_standard.profile.program_application_id,
2827            program_id =hz_utility_v2pub.program_id,-- arp_standard.profile.program_id,
2828            program_update_date = sysdate
2829        WHERE cust_account_id IN (
2830                SELECT m.duplicate_id
2831                FROM   ra_customer_merges m, hz_cust_accounts c
2832                WHERE  m.process_flag = 'N'
2833 	       AND    m.request_id = req_id
2834                AND    m.set_number = set_num
2835                AND    c.cust_account_id = m.duplicate_id
2836                AND    c.status <> 'A')
2837        AND cust_acct_site_id IS NULL
2838        AND ROLE_TYPE = 'CONTACT'
2839       AND party_id <> nvl(l_party_id,-99);--Bug NO. 5067291
2840 
2841        -- Start Bug 4712462
2842      FOR rec IN (
2843        SELECT min(cust_account_role_id) cust_account_role_id, cust_account_id,
2844               nvl(cust_acct_site_id,-1) cust_acct_site_id,party_id
2845        FROM   hz_cust_account_roles r, ra_customer_merges m
2846        where m.request_id = req_id AND    m.set_number = set_num
2847        AND r.cust_account_id = m.customer_id AND m.process_flag = 'N'
2848        GROUP BY cust_account_id,cust_acct_site_id,status,party_id
2849        HAVING NVL(STATUS,'A') ='A' AND count(1) > 1) LOOP
2850        UPDATE hz_cust_account_roles SET status ='I'
2851        WHERE  cust_account_role_id     <> rec.cust_account_role_id
2852        AND    cust_account_id           = rec.cust_account_id
2853        AND    party_id                  = rec.party_id
2854        AND    nvl(cust_acct_site_id,-1) = rec.cust_acct_site_id
2855        AND    nvl(status,'A')           = 'A';
2856 
2857      END LOOP;
2858 
2859     -- End Bug 4712462
2860 
2861        g_count := sql%rowcount;
2862 
2863        arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
2864        arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
2865    CLOSE C3_1;--5067291
2866 
2867     END IF; --g_tranfer_account_contacts
2868     END IF; --process_mode
2869    arp_message.set_line( 'ARP_CMERGE_ARCUS.RA_CONT()-' );
2870 
2871 EXCEPTION
2872 
2873     WHEN OTHERS THEN
2874       arp_message.set_error( 'ARP_CMERGE_ARCUS.RA_CONT' );
2875       RAISE;
2876 
2877 END ra_cont;
2878 
2879 /*===========================================================================+
2880  | PROCEDURE
2881  |              ra_ph
2882  |
2883  | DESCRIPTION
2884  |              merge in HZ_CUST_CONTACT POINTS.
2885  |
2886  | SCOPE - PRIVATE
2887  |
2888  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED
2889  |
2890  | ARGUMENTS  : IN:
2891  |                    req_id
2892  |                    set_num
2893  |                    process_mod
2894  |          IN/ OUT:
2895  |
2896  | RETURNS    : NONE
2897  |
2898  | NOTES
2899  |
2900  | MODIFICATION HISTORY
2901  |     Jianying Huang  25-OCT-00  Customer account is global while account
2902  |                        site is stripped by operating unit. We need to
2903  |                        check if this account has (active)sites in
2904  |                        HZ_CUST_ACCT_SITES_ALL.
2905  |     Jianying Huang  12-DEC-00  Remove cursor c2 and c4 because we
2906  |                        do not delete rows in hz_cust_contact_points.
2907  |     Jianying Huang  16-DEC-00  As per discussion with Gautam Prothia,
2908  |                        since we changed veiw RA_PHONES (see bug 1487607),
2909  |                        we will migrate phones and org contacts no matter the
2910  |                        account is active or inactive after merge. Also, we
2911  |                        will migrate these contact points in party level, not
2912  |                        in account level.
2913  |     Jianying Huang  03-APR-01  The procedure is not being calling anymore
2914  |                        because table hz_cust_contact_points has been obsoleted.
2915  |
2916  +===========================================================================*/
2917 
2918 PROCEDURE ra_ph (
2919           req_id                NUMBER,
2920           set_num               NUMBER,
2921           process_mode          VARCHAR2
2922 ) IS
2923 
2924     --cursor c1 and c2 are used in 'inactivate' mode
2925     CURSOR c1 IS
2926         SELECT cust_contact_point_id
2927         FROM   hz_cust_contact_points yt, ra_customer_merges m
2928         WHERE  cust_account_site_id = m.duplicate_address_id
2929         AND    m.process_flag = 'N'
2930 	AND    m.request_id = req_id
2931 	AND    m.set_number = set_num
2932         FOR UPDATE NOWAIT;
2933 
2934     CURSOR c2 is
2935         SELECT cust_contact_point_id
2936         FROM   hz_cust_contact_points yt, ra_customer_merges m
2937         WHERE  cust_account_id = m.duplicate_id
2938         AND    m.process_flag = 'N'
2939         AND    m.request_id = req_id
2940         AND    m.set_number = set_num
2941         AND    cust_account_site_id IS NULL
2942         FOR UPDATE NOWAIT;
2943 
2944 BEGIN
2945 
2946     arp_message.set_line( 'ARP_CMERGE_ARCUS.RA_PH()+' );
2947 
2948     /* locking tables by opening and closing cursors */
2949     IF process_mode = 'LOCK' THEN
2950 
2951        arp_message.set_name( 'AR', 'AR_LOCKING_TABLE' );
2952        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_CONTACT_POINTS', FALSE );
2953 
2954        OPEN c1;
2955        CLOSE c1;
2956 
2957        OPEN c2;
2958        CLOSE c2;
2959 
2960     ELSE
2961 
2962        /************** account site level inactivate ************/
2963 
2964        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
2965        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_CONTACT_POINTS', FALSE );
2966 
2967        UPDATE HZ_CUST_CONTACT_POINTS yt
2968        SET (cust_account_id, cust_account_site_id) = (
2969                 SELECT min(m.customer_id), min(m.customer_address_id)
2970                 FROM   ra_customer_merges m
2971                 WHERE  yt.cust_account_id = m.duplicate_id
2972                 AND    yt.cust_account_site_id = m.duplicate_address_id
2973                 AND    m.request_id = req_id
2974                 AND    m.set_number = set_num
2975                 AND    m.process_flag = 'N' ),
2976            last_update_date = sysdate,
2977            last_updated_by = hz_utility_v2pub.user_id,--arp_standard.profile.user_id,
2978            last_update_login =hz_utility_v2pub.last_update_login,-- arp_standard.profile.last_update_login,
2979            request_id =  req_id,
2980            program_application_id =hz_utility_v2pub.program_application_id,-- arp_standard.profile.program_application_id,
2981            program_id =hz_utility_v2pub.program_id,-- arp_standard.profile.program_id,
2982            program_update_date = sysdate
2983        WHERE cust_account_site_id IN (
2984                 SELECT m.duplicate_address_id
2985                 FROM   ra_customer_merges m
2986                 WHERE  m.process_flag = 'N'
2987 		AND    m.request_id = req_id
2988 		AND    m.set_number = set_num );
2989 
2990        g_count := sql%rowcount;
2991 
2992        arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
2993        arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
2994 
2995        /************** account level inactivate ************/
2996 
2997        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
2998        arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_CONTACT_POINTS', FALSE );
2999 
3000        UPDATE HZ_CUST_CONTACT_POINTS yt
3001        SET cust_account_id = (
3002                 SELECT DISTINCT m.customer_id
3003                 FROM   ra_customer_merges m
3004                 WHERE  yt.cust_account_id = m.duplicate_id
3005                 AND    m.request_id = req_id
3006                 AND    m.process_flag = 'N'
3007                 AND    m.set_number = set_num),
3008            last_update_date = sysdate,
3009            last_updated_by = hz_utility_v2pub.user_id,--arp_standard.profile.user_id,
3010            last_update_login =hz_utility_v2pub.last_update_login,-- arp_standard.profile.last_update_login,
3011            request_id =  req_id,
3012            program_application_id =hz_utility_v2pub.program_application_id,-- arp_standard.profile.program_application_id,
3013            program_id =hz_utility_v2pub.program_id,-- arp_standard.profile.program_id,
3014            program_update_date = sysdate
3015        WHERE cust_account_id IN (
3016                 SELECT m.duplicate_id
3017                 FROM   ra_customer_merges m
3018                 WHERE  m.process_flag = 'N'
3019 		AND    m.request_id = req_id
3020                 AND    m.set_number = set_num)
3021        AND cust_account_site_id IS NULL;
3022 
3023        g_count := sql%rowcount;
3024 
3025        arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
3026        arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
3027 
3028     END IF;
3029 
3030     arp_message.set_line( 'ARP_CMERGE_ARCUS.RA_PH()-' );
3031 
3032 EXCEPTION
3033 
3034     WHEN OTHERS THEN
3035       arp_message.set_error( 'ARP_CMERGE_ARCUS.RA_PH' );
3036       RAISE;
3037 
3038 END ra_ph;
3039 
3040 --4307679
3041 /*===========================================================================+
3042  | PROCEDURE
3043  |              ra_usg
3044  |
3045  | DESCRIPTION
3046  |              Inactivate CUSTOMER usage of merge-from party.
3047  |
3048  | SCOPE - PRIVATE
3049  |
3050  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED
3051  |
3052  | ARGUMENTS  : IN:
3053  |                    req_id
3054  |                    set_num
3055  |                    process_mod
3056  |          IN/ OUT:
3057  |
3058  | RETURNS    : NONE
3059  |
3060  | NOTES
3061  |
3062  | MODIFICATION HISTORY
3063  |     S V Sowjanya  27-JUL-2005  Created.
3064  |     S V Sowjanya  17-AUG-2005  Bug No:4558247. Changed the WHERE clause of second UPDATE statement
3065  |                                               so that it will update only if m.delete_duplicate_flag='Y'
3066  |
3067  |
3068  |      28-OCT-2005  Anuj Singhal Bug No:4558392. In procedure ra_usg,added an update to inactivate active 'CUSTOMER' usage
3069  |                                of merge-from party when delete after merge is checked for the following condition.If
3070  |                                there exists any other Inactive accounts AND doesnot exist any active accounts associated
3071  |                                with the merge-from-party AND there is no other customer usage associated
3072  |                                with that party. Also changed the where clause of the third update of the                  |                                same procedure so that it deletes the customer usage there are any other
3073  |                                customer usages associated with that party.
3074  |                                Also updated the who columns in the HZ_PARTY_USG_ASSIGNMENTS table
3075  |                                while inactivating the party usg assignment.
3076  |
3077  |
3078  +===========================================================================*/
3079 PROCEDURE ra_usg (
3080           req_id                NUMBER,
3081           set_num               NUMBER,
3082           process_mode          VARCHAR2
3083 ) IS
3084 l_dummy varchar2(20);
3085 
3086 CURSOR c1 IS
3087 
3088 SELECT party_usg_assignment_id
3089 FROM   hz_party_usg_assignments pu
3090 WHERE  party_usage_code = 'CUSTOMER'
3091 AND    EXISTS   (SELECT 'Y'
3092         	 FROM  ra_customer_merges m, hz_cust_accounts c1
3093 	         WHERE m.duplicate_id = c1.cust_account_id
3094 	         AND c1.party_id = pu.party_id
3095 	         AND m.process_flag = 'N'
3096                  AND m.request_id = req_id
3097 	         AND m.set_number = set_num
3098 		);
3099 
3100 
3101 BEGIN
3102 
3103  arp_message.set_line( 'ARP_CMERGE_ARCUS.RA_USG()+' );
3104 
3105     /* locking tables by opening and closing cursors */
3106     IF process_mode = 'LOCK' THEN
3107 
3108        arp_message.set_name( 'AR', 'AR_LOCKING_TABLE' );
3109        arp_message.set_token( 'TABLE_NAME', 'HZ_PARTY_USG_ASSIGNMENTS', FALSE );
3110 
3111        OPEN c1;
3112        CLOSE c1;
3113 
3114     ELSE
3115        /*************** 'inactivate' mode ***************/
3116 
3117        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
3118        arp_message.set_token( 'TABLE_NAME', 'HZ_PARTY_USG_ASSIGNMENTS', FALSE );
3119 
3120        --inactivate active 'CUSTOMER' usage  of merge-from party if the merge-from account is the only active account
3121 
3122 
3123 	UPDATE hz_party_usg_assignments pu
3124         SET    effective_end_date = trunc(sysdate),
3125                status_flag = 'I',
3126                --Bug No. 4558392
3127                last_update_date=sysdate,
3128                last_updated_by = hz_utility_v2pub.user_id,--arp_standard.profile.user_id,
3129                last_update_login =hz_utility_v2pub.last_update_login,-- arp_standard.profile.last_update_login,
3130                request_id =  req_id,
3131                program_application_id =hz_utility_v2pub.program_application_id,-- arp_standard.profile.program_application_id,
3132                program_id =hz_utility_v2pub.program_id-- arp_standard.profile.program_id
3133                --Bug No. 4558392
3134         WHERE  party_usage_code = 'CUSTOMER'
3135         AND   nvl(effective_end_date,sysdate+1)>=sysdate
3136         AND    status_flag = 'A'
3137 	AND NOT EXISTS( SELECT 'Y' FROM hz_cust_accounts c
3138 	                 WHERE c.party_id = pu.party_id
3139                          AND   c.status ='A'
3140 		       )
3141         AND  EXISTS (SELECT 'Y'
3142                        FROM  ra_customer_merges m, hz_cust_accounts c1
3143                        WHERE m.request_id = req_id
3144 		       AND   m.process_flag = 'N'
3145 		       AND   m.set_number = set_num
3146 		       AND   m.delete_duplicate_flag = 'N'
3147 		       AND   m.customer_id <> c1.cust_account_id --Site merge
3148 		       AND   m.duplicate_id =  c1.cust_account_id
3149 		       AND   c1.party_ID    =  pu.party_id
3150                        AND   c1.status      =  'I'
3151 		       );
3152         g_count := sql%rowcount;
3153 
3154         arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
3155         arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
3156 
3157         /*************** 'delete' mode ***************/
3158  ---Bug No. 4558392
3159        arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
3160        arp_message.set_token( 'TABLE_NAME', 'HZ_PARTY_USG_ASSIGNMENTS', FALSE );
3161 
3162        --inactivate active 'CUSTOMER' usage  of merge-from party when delete after merge is checked if
3163        -- There exists any other Inactive accounts AND doesnot exist any active accounts associated with the merge-from
3164        --party AND there is no other customer usage associated with that party.
3165 
3166 
3167 	UPDATE hz_party_usg_assignments pu
3168         SET    effective_end_date = trunc(sysdate),
3169                status_flag = 'I',
3170 
3171                last_update_date=sysdate,
3172                last_updated_by = hz_utility_v2pub.user_id,--arp_standard.profile.user_id,
3173                last_update_login =hz_utility_v2pub.last_update_login,-- arp_standard.profile.last_update_login,
3174                request_id =  req_id,
3175                program_application_id =hz_utility_v2pub.program_application_id,-- arp_standard.profile.program_application_id,
3176                program_id =hz_utility_v2pub.program_id-- arp_standard.profile.program_id
3177 
3178         WHERE  party_usage_code = 'CUSTOMER'
3179         AND   nvl(effective_end_date,sysdate+1)>=sysdate
3180         AND    status_flag = 'A'
3181 	AND EXISTS( SELECT 'Y' FROM hz_cust_accounts c
3182 	                 WHERE c.party_id = pu.party_id
3183                          AND   c.status ='I'
3184                          AND rownum=1
3185                        )
3186  	AND NOT EXISTS( SELECT 'Y' FROM hz_cust_accounts c
3187 	                 WHERE c.party_id = pu.party_id
3188                          AND   c.status ='A'
3189                          AND rownum=1
3190                        )
3191 
3192         AND NOT EXISTS (SELECT 'Y'
3193                                   from hz_party_usg_assignments
3194                                   where party_id=pu.party_id
3195                                   and party_usage_code='CUSTOMER'
3196                                   and party_usg_assignment_id <> pu.party_usg_assignment_id
3197                                   and rownum=1)
3198 
3199 
3200         AND  EXISTS (SELECT 'Y'
3201                        FROM  ra_customer_merges m, hz_cust_accounts c1
3202                        WHERE m.request_id = req_id
3203 		       AND   m.process_flag = 'N'
3204 		       AND   m.set_number = set_num
3205 		       AND   m.delete_duplicate_flag = 'Y'--Bug No. 4558392
3206 		       AND   m.customer_id <> c1.cust_account_id --Site merge
3207 		       AND   m.duplicate_id =  c1.cust_account_id
3208 		       AND   c1.party_ID    =  pu.party_id
3209                        AND   c1.status      =  'D'--Bug No. 4558392
3210 		       );
3211         g_count := sql%rowcount;
3212 
3213         arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
3214         arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
3215 
3216 -----Bug No. 4558392
3217               /*************** 'delete' mode ***************/
3218 
3219 
3220 
3221         arp_message.set_name( 'AR', 'AR_UPDATING_TABLE' );
3222         arp_message.set_token( 'TABLE_NAME', 'HZ_PARTY_USG_ASSIGNMENTS', FALSE );
3223 
3224 	--Delete all 'CUSTOMER' usage of merge-from party
3225 	--if the merge-from account is the only account for merge-from party OR there are any other customer usages associated with that party.
3226 
3227 	UPDATE hz_party_usg_assignments pu
3228         SET    effective_end_date = trunc(sysdate),
3229                status_flag = 'D'
3230         WHERE  pu.party_usage_code = 'CUSTOMER'
3231 
3232 	AND    (
3233                  NOT EXISTS (SELECT 'Y'
3234                              FROM hz_cust_accounts c  --delete if from account is the only account for merge-from party
3235 	                     WHERE c.party_id = pu.party_id
3236 	                     AND   c.status in ('A','I')
3237                             )
3238 --Bug No.4558392
3239                 OR  (   pu.status_flag='A' AND  nvl(pu.effective_end_date,sysdate+1)>=sysdate AND
3240                            exists (SELECT 'Y'
3241                                   from hz_party_usg_assignments
3242                                   where party_id=pu.party_id
3243                                   and party_usage_code='CUSTOMER'
3244                                   and party_usg_assignment_id <> pu.party_usg_assignment_id
3245                                   and rownum=1)
3246                      )
3247                   )
3248 
3249 
3250                 /* OR (pu.status_flag = 'A' AND trunc(sysdate) < pu.effective_end_date  --delete only active usage if merge-from party has inactive accounts
3251                      AND NOT EXISTS (SELECT 'Y' FROM hz_cust_accounts c
3252 	                             WHERE c.party_id = pu.party_id
3253 		                     AND   c.status = 'A')
3254                     )*/
3255 ----Bug No.4558392
3256         AND    EXISTS (SELECT 'Y'
3257                        FROM  ra_customer_merges m, hz_cust_accounts c1
3258                        WHERE m.request_id = req_id
3259 		       AND   m.process_flag = 'N'
3260 		       AND   m.set_number = set_num
3261 		       AND   m.delete_duplicate_flag = 'Y'
3262 		       AND   m.customer_id <> c1.cust_account_id --Site merge
3263 		       AND   m.duplicate_id =  c1.cust_account_id
3264 		       AND   c1.party_ID    =  pu.party_id
3265                        AND   c1.status      =  'D'
3266 		       );
3267 
3268         g_count := sql%rowcount;
3269 
3270         arp_message.set_name( 'AR', 'AR_ROWS_UPDATED' );
3271         arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
3272 
3273 
3274     END IF;
3275     arp_message.set_line( 'ARP_CMERGE_ARCUS.RA_USG()-' );
3276 
3277 EXCEPTION
3278 
3279     WHEN OTHERS THEN
3280       arp_message.set_error( 'ARP_CMERGE_ARCUS.RA_USG' );
3281       RAISE;
3282 END ra_usg;
3283 --4307679
3284 
3285 
3286 /*===========================================================================+
3287  | PROCEDURE
3288  |              do_cust_merge_contacts
3289  |
3290  | DESCRIPTION
3291  |              merge in HZ_CUST_ACCOUNT_ROLES with
3292  |              role_type = 'CONTACT' in account level
3293  |
3294  | SCOPE - PRIVATE
3295  |
3296  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED
3297  |
3298  | ARGUMENTS  : IN:
3299  |                    p_from_party_id
3300  |                    p_to_party_id
3301  |                    p_from_account_id
3302  |                    p_to_account_id
3303  |          IN/ OUT:
3304  |
3305  | RETURNS    : NONE
3306  |
3307  | NOTES
3308  |
3309  | MODIFICATION HISTORY
3310  |     Jianying Huang  25-OCT-00  Based on customer merge high level
3311  |                        design (page 12): if party auto-numbering is
3312  |                        turned on, a new party number should be generated
3313  |                        for the copy (this has been taken care of by
3314  |                        HZ_PARTY_PUB API). If auto-numbering is turned
3315  |                        off, a new party number can be created by
3316  |                        taking the existing party number and appending
3317  |                        '-C' to the end of represent the copy.
3318  |     Jianying Huang  25-OCT-00  Remove call
3319  |                                  do_update_dup_party_rec
3320  |                                  do_update_dup_party_rel_rec
3321  |                        because HZ_PARTY_PUB.create_org_contact will
3322  |                        create party_relationship and party automatically.
3323  |                        The new party_relationship_rec =
3327  |     Jianying Huang  25-OCT-00  After call create_org_contact API,
3324  |                              org_contact_rec.party_rel_rec ) and
3325  |                        the new party_rec =
3326  |                              org_contact_rec.party_rel_rec.party_rec
3328  |                        if return_status is not 'success', we need to
3329  |                        populate an exception.
3330  |     Jianying Huangn 27-OCT-00  Since contacts of 'phone' type also goes
3331  |                        as customer contact points, we'd better to migrate
3332  |                        it here because it should be a phone number for
3333  |                        contacts as well as for accounts.
3334  |     Jianying Huang  16-DEC-00  Move common code to do_merge_contacts.
3335  |
3336  |
3337  +===========================================================================*/
3338 
3339 PROCEDURE do_cust_merge_contacts (
3340           p_from_party_id          NUMBER,
3341           p_to_party_id            NUMBER,
3342           p_from_account_id        NUMBER,
3343           p_to_account_id          NUMBER
3344 ) IS
3345 
3346     l_org_contact_id               NUMBER;
3347     l_cust_acct_role_id            NUMBER;
3348     l_org_contact_party_id         NUMBER;
3349     l_org_contact_party_rel_id     NUMBER;
3350 
3351     x_org_party_id                 NUMBER;
3352 
3353     --select party, party_relationships, and org_contact ID
3354     --will call API: HZ_PARTY_PUB.get_current_* to get the records
3355     --This is for the consistence purpose in case of data model changes.
3356     CURSOR c IS
3357         SELECT
3358            --Account Role
3359            cust_account_role_id,
3360            --FOR PARTY REC
3361            rel.party_id,
3362            --FOR PARTY REL REC
3363            rel.relationship_id,
3364            -- FOR ORG-CONTACT REC
3365 	   org.org_contact_id
3366         FROM  hz_cust_account_roles acct_role,
3367               hz_org_contacts org,
3368               hz_relationships rel,
3369               hz_cust_accounts acct
3370         WHERE acct_role.role_type = 'CONTACT'
3371         AND   acct_role.cust_account_id = p_from_account_id
3372         AND   acct_role.cust_acct_site_id IS NULL
3373         AND   acct_role.party_id = rel.party_id
3374         AND   org.party_relationship_id = rel.relationship_id
3375         AND   rel.subject_table_name = 'HZ_PARTIES'
3376         AND   rel.object_table_name = 'HZ_PARTIES'
3377         AND   acct_role.cust_account_id = acct.cust_account_id
3378         AND   acct.party_id   = rel.object_id
3379         AND  rel.subject_id <> p_to_party_id ;--5067291
3380 /*      AND   rel.directional_flag = 'F';   */  /* Bug No : 2359461 */
3381 
3382 BEGIN
3383 
3384     arp_message.set_line( 'ARP_CMERGE_ARCUS.do_cust_merge_contacts()+' );
3385 
3386     /* Copy org contact */
3387     OPEN c;
3388     LOOP
3389       FETCH c INTO
3390          l_cust_acct_role_id,
3391          -- FOR PARTY REC
3392          l_org_contact_party_id,
3393          -- FOR PARTY REL REC
3394          l_org_contact_party_rel_id,
3395          -- FOR ORG-CONTACT REC
3396 	 l_org_contact_id;
3397 
3398       EXIT WHEN c%NOTFOUND;
3399 
3400       do_merge_contacts( 'ACCOUNT_LEVEL',
3401                          p_from_account_id,
3402                          l_org_contact_party_id,
3403                          l_org_contact_party_rel_id,
3404                          l_org_contact_id,
3405                          p_to_party_id,
3406                          x_org_party_id );
3407 
3408       -- Update the customer org contact with the new org contact id.
3409       UPDATE hz_cust_account_roles
3410       SET    party_id = x_org_party_id
3411       WHERE  cust_account_role_id = l_cust_acct_role_id;
3412 
3413     END LOOP;
3414     CLOSE c;
3415 
3416     arp_message.set_line( 'ARP_CMERGE_ARCUS.do_cust_merge_contacts()-' );
3417 
3418 EXCEPTION
3419 
3420     WHEN OTHERS THEN
3421       arp_message.set_error( 'ARP_CMERGE_ARCUS.do_cust_merge_contacts' );
3422       RAISE;
3423 
3424 END do_cust_merge_contacts;
3425 
3426 /*===========================================================================+
3427  | PROCEDURE
3428  |              do_site_merge_contacts
3429  |
3430  | DESCRIPTION
3431  |              merge in HZ_CUST_ACCOUNT_ROLES with
3432  |              role_type = 'CONTACT' in account site level
3433  |
3434  | SCOPE - PRIVATE
3435  |
3436  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED
3437  |
3438  | ARGUMENTS  : IN:
3439  |                    p_from_party_id
3440  |                    p_to_party_id
3441  |                    p_from_account_id
3442  |                    p_to_account_id
3443  |                    p_req_id
3444  |                    p_set_num
3445  |          IN/ OUT:
3446  |
3447  | RETURNS    : NONE
3448  |
3449  | NOTES
3450  |
3451  | MODIFICATION HISTORY
3452  |     Jianying Huang  25-OCT-00  Based on customer merge high level
3453  |                        design (page 12): if party auto-numbering is
3454  |                        turned on, a new party number should be generated
3455  |                        for the copy (this has been taken care of by
3456  |                        HZ_PARTY_PUB API). If auto-numbering is turned
3457  |                        off, a new party number can be created by
3458  |                        taking the existing party number and appending
3459  |                        '-C' to the end of represent the copy.
3460  |     Jianying Huang  25-OCT-00  Remove call
3461  |                                  do_update_dup_party_rec
3462  |                                  do_update_dup_party_rel_rec
3466  |                              org_contact_rec.party_rel_rec ) and
3463  |                        because HZ_PARTY_PUB.create_org_contact will
3464  |                        create party_relationship and party automatically.
3465  |                        The new party_relationship_rec =
3467  |                        the new party_rec =
3468  |                              org_contact_rec.party_rel_rec.party_rec
3469  |     Jianying Huang  25-OCT-00  After call create_org_contact API,
3470  |                        if return_status is not 'success', we need to
3471  |                        populate an exception.
3472  |     Jianying Huangn 27-OCT-00  Since contacts of 'phone' type also goes
3473  |                        as customer contact points, we'd better to migrate
3474  |                        it here because it should be a phone number for
3475  |                        contacts as well as for accounts.
3476  |     Jianying Huang  16-DEC-00  Move common code to do_merge_contacts.
3477  |     Jianying Huang  28-DEC-00  Since we ignore hz_org_contacts.party_site_id
3478  |                        in account merge context, we do not need select merge-to's
3479  |                        address id.
3480  |
3481  +===========================================================================*/
3482 
3483 PROCEDURE do_site_merge_contacts(
3484           p_from_party_id         NUMBER,
3485           p_to_party_id           NUMBER,
3486           p_from_account_id       NUMBER,
3487           p_to_account_id         NUMBER,
3488           p_req_id                NUMBER,
3489           p_set_num               NUMBER
3490 ) IS
3491 
3492     --account site ids
3493     from_site_id                  NUMBER;
3494     to_site_id                    NUMBER;
3495 
3496     --party site ids
3497     from_party_site_id            NUMBER;
3498     to_party_site_id              NUMBER;
3499     l_to_party_site_id            NUMBER;
3500 
3501     l_org_contact_party_id        NUMBER;
3502     l_org_contact_party_rel_id    NUMBER;
3503     l_org_contact_id              NUMBER;
3504     l_cust_acct_role_id           NUMBER;
3505     l_org_contact_party_site_id   NUMBER;
3506 
3507     x_org_party_id                NUMBER;
3508 
3509     --select party, party_relationships, and org_contact ID
3510     --will call API: HZ_PARTY_PUB.get_current_* to get the records
3511     --This is for the consistence purpose in case of data model changes.
3512     CURSOR c IS
3513         SELECT
3514            -- Account Role
3515            acct_role.cust_account_role_id,
3516            -- FOR PARTY REC
3517            rel.party_id,
3518            -- FOR PARTY REL REC
3519 	   rel.relationship_id,
3520            -- FOR ORG-CONTACT REC
3521 	   org.org_contact_id
3522 --as per discussion with Gautam Prothia, we ignore party_site_id in customer
3523 --merge context
3524 --         org.party_site_id
3525         FROM hz_cust_account_roles acct_role,
3526              hz_org_contacts org,
3527              hz_relationships rel,
3528              hz_cust_accounts acct
3529         WHERE acct_role.role_type = 'CONTACT'
3530         AND   acct_role.cust_account_id = p_from_account_id
3531         AND   acct_role.cust_acct_site_id = from_site_id
3532         AND   acct_role.party_id = rel.party_id
3533         AND   org.party_relationship_id = rel.relationship_id
3534         AND   rel.subject_table_name = 'HZ_PARTIES'
3535         AND   rel.object_table_name = 'HZ_PARTIES'
3536         AND   acct_role.cust_account_id = acct.cust_account_id
3537         AND   acct.party_id   = rel.object_id
3538         AND   rel.subject_id <> p_to_party_id;--5067291
3539 /*      AND   rel.directional_flag = 'F'; */ /* Bug No : 2359461 */
3540 
3541     --select merge_from/to account site id.
3542     --we will no longer select merge-to address id because
3543     --party_site_id in hz_org_contacts is ignored in customer
3544     --merge context.
3545 
3546     CURSOR d IS
3547         SELECT DISTINCT duplicate_address_id    --, customer_address_id
3548         FROM   ra_customer_merges
3549         WHERE  duplicate_id = p_from_account_id
3550         AND    customer_id = p_to_account_id
3551         AND    process_flag = 'N'
3552         AND    request_id = p_req_id
3553         AND    set_number = p_set_num;
3554 
3555 BEGIN
3556 
3557     arp_message.set_line( 'ARP_CMERGE_ARCUS.do_site_merge_contacts()+' );
3558 
3559     /* For each site level org contact do the following */
3560     OPEN d;
3561     LOOP
3562       FETCH d INTO from_site_id;   --, to_site_id;
3563       EXIT WHEN d%NOTFOUND;
3564 
3565 /* as per discussion with Gautam Prothia, we ignore party_site_id in customer
3566    merge context
3567 
3568       SELECT ass.party_site_id into from_party_site_id
3569       FROM hz_cust_acct_sites ass
3570       WHERE cust_acct_site_id = from_site_id;
3571 
3572       SELECT ass.party_site_id into to_party_site_id
3573       FROM hz_cust_acct_sites ass
3574       WHERE cust_acct_site_id = to_site_id;
3575 */
3576 
3577       --sitet level org contact merge */
3578       OPEN c;
3579       LOOP
3580         FETCH c INTO
3581            l_cust_acct_role_id,
3582            -- FOR PARTY REC
3583            l_org_contact_party_id,
3584            -- FOR PARTY REL REC
3585            l_org_contact_party_rel_id,
3586            -- FOR ORG-CONTACT REC
3587 	   l_org_contact_id;
3588 
3589 --as per discussion with Gautam Prothia, we ignore party_site_id in customer
3590 --merge context
3591 --         l_org_contact_party_site_id;
3592 
3593         EXIT WHEN c%NOTFOUND;
3594 
3595 --as per discussion with Gautam Prothia, we ignore party_site_id in customer
3596 --merge context
3597 /*
3598         IF org_contact_party_site_id IS NOT NULL THEN
3599            l_to_party_site_id := to_party_site_id;
3600         END IF;
3601 */
3602 
3603         do_merge_contacts( 'SITE_LEVEL',
3604                            p_from_account_id,
3605                            l_org_contact_party_id,
3606                            l_org_contact_party_rel_id,
3607                            l_org_contact_id,
3608                            p_to_party_id,
3609                            x_org_party_id );
3610 
3611         -- Update the customer org contact with the new org contact id.
3612         UPDATE hz_cust_account_roles
3613         SET party_id = x_org_party_id
3614         WHERE cust_account_role_id = l_cust_acct_role_id;
3615 
3616       END LOOP;
3617       CLOSE c;
3618 
3619     END LOOP;
3620     CLOSE d;
3621 
3622     arp_message.set_line( 'ARP_CMERGE_ARCUS.do_site_merge_contacts()-' );
3623 
3624 EXCEPTION
3625 
3626     WHEN OTHERS THEN
3627       arp_message.set_error( 'ARP_CMERGE_ARCUS.do_site_merge_contacts' );
3628       RAISE;
3629 
3630 END do_site_merge_contacts;
3631 
3632 /*===========================================================================+
3633  | PROCEDURE
3634  |              do_merge_contacts
3635  |
3636  | DESCRIPTION
3637  |              Common part of do_cust/site_merge_contacts
3638  |
3639  | SCOPE - PRIVATE
3640  |
3641  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED
3642  |
3643  | ARGUMENTS  : IN:
3644  |                    p_level
3645  |                    p_from_account_id
3646  |                    p_org_party_id
3647  |                    p_org_party_rel_id
3648  |                    p_org_contact_id
3649  |                    p_to_party_id
3650  |                    p_to_party_site_id
3651  |
3652  |          IN/ OUT:
3653  |              OUT:
3654  |                    x_org_party_id
3655  |
3656  | RETURNS    : NONE
3657  |
3658  | NOTES
3659  |
3660  | MODIFICATION HISTORY
3661  |     Jianying Huang  16-DEC-00  Creaded. Move the common part of
3662  |                        do_cust_merge_contacts and do_site_merge_contacts into
3663  |                        this procedure.
3664  |     Jianying Huang  16-DEC-00  Use API to get party, party_rel, org_contact
3665  |                        records. This is for the consistence purpose in case of
3666  |                        data model changes.
3667  |     Jianying Huang  17-DEC-00  Check duplicate org contact before create a new one.
3668  |     Jianying Huang  17-DEC-00  As discussion with Gautam Prothia, we should copy
3669  |                        all types of contact points in party level
3670  |     Jianying Huang  20-DEC-00  Bug 1535542: Use global temporary table
3671  |                        to store the mapping of old org_contact_id and new
3672  |                        org_contact_id when migrate org contacts.
3673  |     Jyoti Pandey    06-NOV-01 Bug:2098728 Changing all API call outs to call
3674  |                        Package HZ_CUST_ACCOUNT_MERGE_V2PVT
3675  |                        Eliminated calls to get_party_rec and get_relationship_rec
3676  |                        Instead call to get_org_contact gets party and relationship recs
3677  |
3678  +===========================================================================*/
3679 
3680 PROCEDURE do_merge_contacts (
3681           p_level                   VARCHAR2,
3682           p_from_account_id         NUMBER,
3683           p_org_party_id            NUMBER,
3684           p_org_party_rel_id        NUMBER,
3685           p_org_contact_id          NUMBER,
3686           p_to_party_id             NUMBER,
3687           x_org_party_id        OUT NOCOPY NUMBER,
3688           p_to_party_site_id        NUMBER DEFAULT NULL
3689 ) IS
3690     ---|Bug:2098728 don't need this as we internally call relationship and party
3691     ---|from org_contacts
3692     ---|l_party_rec                  HZ_PARTY_V2PUB.PARTY_REC_TYPE;
3693     ---|l_party_rel_rec              HZ_RELATIONSHIP_V2PUB.relationship_rec_type ;
3694     l_org_contact_rec                 HZ_PARTY_CONTACT_V2PUB.ORG_CONTACT_REC_TYPE;
3695 
3696     --returned by calling create_org_contact API.
3697     x_org_contact_id             NUMBER;
3698     x_party_rel_id               NUMBER;
3699     x_party_number               VARCHAR2(30);
3700 
3701     --error message handling.
3702     x_msg_count                  NUMBER := 0;
3703     x_return_status              VARCHAR2(100);
3704     x_msg_data                   VARCHAR2(2000);
3705 
3706     l_dup_exists                 VARCHAR2(10) := FND_API.G_FALSE;
3707     l_insert                     VARCHAR2(10) := FND_API.G_FALSE;
3708     l_sql                        VARCHAR2(1000);
3709     l_direction_code             VARCHAR2(30);
3710      --Start of Bug No: 4387523
3711     CURSOR c_party_type(p_party_id NUMBER) IS SELECT party_type from hz_parties
3712     where party_id = p_party_id;
3713     l_party_type varchar2(30);
3714     --End of Bug No: 4387523
3715 BEGIN
3716 
3717     arp_message.set_line( 'ARP_CMERGE_ARCUS.do_merge_contacts()+' );
3718 
3719     --check if the org contact has been migrated
3720     BEGIN
3721        l_sql := 'SELECT new_id ' ||
3722                 'FROM ' || g_table_name || ' ' ||
3723                 'WHERE old_id = :id' || ' ' ||
3724                 'AND type = ''ORG_CONTACT''';
3725 
3726        EXECUTE IMMEDIATE l_sql INTO x_org_contact_id USING p_org_contact_id;
3727 
3728     EXCEPTION
3729        WHEN NO_DATA_FOUND THEN
3730          NULL;
3731     END;
3732 
3733     --the org contact has been migrated
3734     IF x_org_contact_id IS NOT NULL THEN
3735 
3736        l_dup_exists := FND_API.G_TRUE;
3737     ELSE
3738        --the org contact has not been migrated
3739 
3740        l_dup_exists := check_org_contact_dup (
3741                              p_org_contact_id,
3742                              p_org_party_rel_id,
3743                              p_to_party_id,
3744                              x_org_contact_id ,
3745                              p_from_account_id);
3746 
3747 --arp_message.set_line( '****' || to_char(x_org_contact_id) ||
3748 --'****' || l_dup_exists );
3749 
3750        IF l_dup_exists = FND_API.G_TRUE THEN
3751           l_insert := FND_API.G_TRUE;
3752 
3753        END IF;
3754 
3755     END IF;
3756 
3757     IF l_dup_exists = FND_API.G_TRUE THEN
3758        SELECT party_id INTO x_org_party_id
3759        FROM hz_relationships
3760        WHERE relationship_id = (
3761           SELECT party_relationship_id
3762           FROM hz_org_contacts
3763           WHERE org_contact_id = x_org_contact_id )
3764             AND subject_table_name =  'HZ_PARTIES'
3765             AND object_table_name = 'HZ_PARTIES'
3766             AND rownum = 1;
3767 /*          AND directional_flag = 'F';  */ /* Bug No : 2359461 */
3768 
3769     ELSE
3770        --duplicate not exist. Create new org contacts.
3771        --call API to get merge-from's org contact info.
3772 
3773       ---Bug:2098728
3774        --get org contact info.
3775         HZ_CUST_ACCOUNT_MERGE_V2PVT.get_org_contact_rec (
3776                               FND_API.G_TRUE,
3777                               p_org_contact_id,
3778                               l_org_contact_rec,
3779                               l_direction_code,
3780                               x_return_status,
3781                               x_msg_count,
3782                               x_msg_data );
3783 
3784       IF x_msg_count = 1 THEN
3785           x_msg_data := x_msg_data || '**GET_CURRENT_ORG_CONTACT**';
3786           arp_message.set_line(
3787               'do_merge_contacts:get_org_contact_rec ERROR ' ||
3788               x_msg_data );
3789        ELSIF x_msg_count > 1 THEN
3790 
3791          FOR x IN 1..x_msg_count LOOP
3792               x_msg_data := FND_MSG_PUB.GET(p_encoded => fnd_api.g_false);
3793               x_msg_data := x_msg_data || '**GET_CURRENT_ORG_CONTACT**';
3794               arp_message.set_line(
3795                   'do_merge_contacts:get_org_contact_rec ERROR ' ||
3796                   x_msg_data );
3797           END LOOP;
3798        END IF;
3799 
3800        --- After call create_org_contact API, if return_status is not
3801        --- 'success', we need to populate an exception.
3802        IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3803           RAISE FND_API.G_EXC_ERROR;
3804        END IF;
3805 
3806        -------Bug:2098728 Relationship ID is set to null in PKG
3807        -------Genereating the new party number for new records
3808        l_org_contact_rec.org_contact_id := NULL;
3809        l_org_contact_rec.party_rel_rec.party_rec.party_number := NULL;
3810 
3811        -------Update the new party relationship rec with the merge-to party
3812        l_org_contact_rec.party_rel_rec.object_id :=  p_to_party_id;
3813         --Start of Bug No: 4387523
3814 	if(l_org_contact_rec.party_rel_rec.object_table_name ='HZ_PARTIES') then
3815 	  open c_party_type(l_org_contact_rec.party_rel_rec.object_id);
3816 	  fetch c_party_type into l_party_type;
3817 	  close c_party_type;
3818 	  l_org_contact_rec.party_rel_rec.object_type := l_party_type;
3819 	end if;
3820 	--End of Bug No: 4387523
3821 
3822       /** Based on the customer merge high level design:
3823          if party auto-numbering is turned on, a new party number should
3824          be generated for the copy (this has been taken care of by
3825          HZ_PARTY_PUB API). If auto-numbering is turned off, a
3826          new party number can be created by taking the existing
3827          party number and appending '-C' to the end of represent the copy.*/
3828      /*|  IF fnd_profile.value('HZ_GENERATE_PARTY_NUMBER') = 'N' THEN
3829        |  l_org_contact_rec.party_rel_rec.party_rec.party_number :=
3830        |                   l_org_contact_rec.party_rel_rec.party_rec.party_number || '-C';
3831        |ELSE
3832        |   l_org_contact_rec.party_rel_rec.party_rec.party_number := NULL;
3833        |END IF;*/
3834 
3835 
3836        ---as per discussion with Gautam Prothia, we ignore party_site_id in customer
3837        ---merge context
3838        l_org_contact_rec.party_site_id := NULL;
3839 
3840 
3841         ---Bug:2098728
3842        /* Create the org contact record */
3843         HZ_CUST_ACCOUNT_MERGE_V2PVT.create_org_contact(
3844                               p_init_msg_list   =>FND_API.G_TRUE,
3845                               p_org_contact_rec => l_org_contact_rec,
3846                               p_direction_code => l_direction_code,
3847                               x_org_contact_id  => x_org_contact_id,
3848                               x_party_rel_id    => x_party_rel_id,
3849                               x_party_id        => x_org_party_id,
3850                               x_party_number    => x_party_number,
3851                               x_return_status   => x_return_status,
3852                               x_msg_count       => x_msg_count,
3853                               x_msg_data        => x_msg_data);
3854 
3855        IF x_msg_count = 1 THEN
3856           x_msg_data := x_msg_data || '**CREATE_ORG_CONTACT**';
3857           arp_message.set_line(
3858               'do_merge_contacts:create_org_contact ERROR ' ||
3859                x_msg_data );
3860        ELSIF x_msg_count > 1 THEN
3861 
3862           FOR x IN 1..x_msg_count LOOP
3863               x_msg_data := FND_MSG_PUB.GET(p_encoded => fnd_api.g_false);
3864               x_msg_data := x_msg_data || '**CREATE_ORG_CONTACT**';
3865               arp_message.set_line(
3866                   'do_cust_merge_contacts:create_org_contact ERROR ' ||
3867                   x_msg_data );
3868           END LOOP;
3869        END IF;
3870 
3871        /** After call create_org_contact API, if return_status is not
3872         'success', we need to populate an exception. */
3873        IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3874           RAISE FND_API.G_EXC_ERROR;
3875        END IF;
3876 
3877        l_insert := FND_API.G_TRUE;
3878 
3879     END IF;
3880 
3881     IF l_insert = FND_API.G_TRUE THEN
3882 
3883 --Bug 1535542: insert the mapping of old org_contact_id and new org_contact_id
3884 
3885        BEGIN
3886           l_sql := 'INSERT INTO ' || g_table_name || ' ' ||
3887                    'VALUES (' ||
3888                    '''ORG_CONTACT''' || ',' ||
3889                    to_char(p_org_contact_id) || ', ' ||
3890                    to_char(x_org_contact_id) || ')';
3891 
3892           EXECUTE IMMEDIATE l_sql;
3893 
3894        END;
3895     END IF;
3896 
3897 /**N/A
3898     --Since contacts of 'phone' type also goes as customer contact
3899     --points, we'd better to migrate it here because it should be
3900     --a phone number for contacts as well as for accounts.
3901 **/
3902     --As discussion with Gautam Prothia, we should copy all types of
3903     --contact points in party level
3904 
3905 
3906 
3907     do_copy_contact_points ( 'HZ_PARTIES',
3908                              p_org_party_id,
3909                              x_org_party_id,
3910                              p_from_account_id );
3911 
3912     arp_message.set_line( 'ARP_CMERGE_ARCUS.do_merge_contacts()-' );
3913 
3914 EXCEPTION
3915 
3916     WHEN OTHERS THEN
3917       arp_message.set_error( 'ARP_CMERGE_ARCUS.do_merge_contacts' );
3918       RAISE;
3919 
3920 END do_merge_contacts;
3921 
3922 /*===========================================================================+
3923  | PROCEDURE
3924  |              do_cust_merge_cpoint
3925  |
3926  | DESCRIPTION
3927  |              merge customer contact points in account level.
3928  |
3929  | SCOPE - PRIVATE
3930  |
3931  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED
3932  |
3933  | ARGUMENTS  : IN:
3934  |                    p_from_party_id
3935  |                    p_to_party_id
3936  |                    p_from_account_id
3937  |                    p_to_account_id
3938  |          IN/ OUT:
3939  |
3940  | RETURNS    : NONE
3941  |
3942  | NOTES
3943  |
3944  | MODIFICATION HISTORY
3945  |     Jianying Huang  25-OCT-00  After call create_contact_points API,
3946  |                        if return_status is not 'success', we need to
3947  |                        populate an exception.
3948  |     Jianying Huang  27-OCT-00  Bug 1415529: If both raw_phone_number and
3949  |                        phone_number have value, use phone_number and set
3950  |                        raw_phone_number to NULL. Otherwise, API would
3951  |                        error out.
3952  |     Jianying Huang  27-OCT-00  We should migrate those phones only for
3953  |                        accounts/sites here, not the phones for contact
3954  |                        persons. Those phones have been taken care of
3955  |                        by do_cust(site)_merge_contacts.
3956  |     Jianying Huang  17-DEC-00  As discussion with Gautam Prothia, we
3957  |                        should copy all types of contact points in party level
3958  |
3959  +===========================================================================*/
3960 
3961 PROCEDURE do_cust_merge_cpoint (
3962           p_from_party_id         NUMBER,
3963           p_to_party_id           NUMBER,
3964           p_from_account_id       NUMBER,
3965           p_to_account_id         NUMBER
3966 ) IS
3967 
3968 BEGIN
3969 
3973                              p_from_party_id,
3970     arp_message.set_line( 'ARP_CMERGE_ARCUS.do_cust_merge_cpoint()+' );
3971 
3972     do_copy_contact_points ( 'HZ_PARTIES',
3974                              p_to_party_id,
3975                              p_from_account_id );
3976 
3977     arp_message.set_line( 'ARP_CMERGE_ARCUS.do_cust_merge_cpoint()-' );
3978 
3979 EXCEPTION
3980 
3981     WHEN OTHERS THEN
3982       arp_message.set_error( 'ARP_CMERGE_ARCUS.do_cust_merge_cpoint' );
3983       RAISE;
3984 
3985 END do_cust_merge_cpoint;
3986 
3987 /*===========================================================================+
3988  | PROCEDURE
3989  |              do_site_merge_cpoint
3990  |
3991  | DESCRIPTION
3992  |              merge customer contact points in account site level.
3993  |
3994  | SCOPE - PRIVATE
3995  |
3996  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED
3997  |
3998  | ARGUMENTS  : IN:
3999  |                    from_party_id
4000  |                    to_party_id
4001  |                    from_account_id
4002  |                    to_account_id
4003  |          IN/ OUT:
4004  |
4005  | RETURNS    : NONE
4006  |
4007  | NOTES
4008  |
4009  | MODIFICATION HISTORY
4010  |     Jianying Huang  25-OCT-00  After call create_contact_points API,
4011  |                        if return_status is not 'success', we need to
4012  |                        populate an exception.
4013  |     Jianying Huang  27-OCT-00  Bug 1415529: If both raw_phone_number and
4014  |                        phone_number have value, use phone_number and set
4015  |                        raw_phone_number to NULL. Otherwise, API would
4016  |                        error out.
4017  |     Jianying Huang  27-OCT-00  We should migrate those phones only for
4018  |                        accounts/sites here, not the phones for contact
4019  |                        persons. Those phones have been taken care of
4020  |                        by do_cust(site)_merge_contacts.
4021  |     Jianying Huang  17-DEC-00  As discussion with Gautam Prothia, we
4022  |                        should copy all types of contact points in party level
4023  |
4024  +===========================================================================*/
4025 
4026 PROCEDURE do_site_merge_cpoint (
4027           p_from_party_site_id         NUMBER,
4028           p_to_party_site_id           NUMBER,
4029           p_from_account_id            NUMBER,
4030           p_to_account_id              NUMBER
4031 ) IS
4032 
4033 BEGIN
4034 
4035     arp_message.set_line( 'ARP_CMERGE_ARCUS.do_site_merge_cpoint()+' );
4036 
4037     do_copy_contact_points ( 'HZ_PARTY_SITES',
4038                              p_from_party_site_id,
4039                              p_to_party_site_id,
4040                              p_from_account_id );
4041 
4042     arp_message.set_line( 'ARP_CMERGE_ARCUS.do_site_merge_cpoint()-' );
4043 
4044 EXCEPTION
4045 
4046     WHEN OTHERS THEN
4047       arp_message.set_error( 'ARP_CMERGE_ARCUS.do_site_merge_cpoint' );
4048       RAISE;
4049 
4050 END do_site_merge_cpoint;
4051 
4052 /*===========================================================================+
4053  | PROCEDURE
4054  |              do_copy_contact_points
4055  |
4056  | DESCRIPTION
4057  |              copy contact points
4058  |
4059  | SCOPE - PRIVATE
4060  |
4061  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED
4062  |
4063  | ARGUMENTS  : IN:
4064  |                    p_owner_table_name
4065  |                    p_from_id
4066  |                    p_to_id
4067  |                    p_from_account_id
4068  |
4069  |          IN/ OUT:
4070  |
4071  | RETURNS    : NONE
4072  |
4073  | NOTES
4074  |
4075  | MODIFICATION HISTORY
4076  |
4077  |     Jianying Huang  25-OCT-00  Remove parameter orgContactId. from_party_id
4078  |                        should be used by cursor. Add new condition:
4079  |                        hz_contact_points.owner_table_name = 'HZ_PARTIES'
4080  |     Jianying Huang  25-OCT-00  After call create_contact_point API,
4081  |                        if return_status is not 'success', we need to
4082  |                        populate an exception.
4083  |     Jianying Huang  27-OCT-00  Since contacts of 'phone' type also goes
4084  |                        as customer contact points, we'd better to migrate
4085  |                        it here because it should be a phone number for
4086  |                        contacts as well as for accounts.
4087  |     Jianying Huang  16-DEC-00  As per discussion with Gautam Prothia,
4088  |                        since we changed veiw RA_PHONES (see bug 1487607),
4089  |                        we will migrate all types of contact points in party
4090  |                        level, not in account level.
4091  |
4092  +===========================================================================*/
4093 
4094   PROCEDURE do_copy_contact_points (
4095     p_owner_table_name         VARCHAR2,
4096     p_from_id                  NUMBER,
4097     p_to_id                    NUMBER,
4098     p_from_account_id          NUMBER
4099   ) IS
4100 
4101     l_contact_point_id         NUMBER;
4102 
4103     ----Bug:2098728 Changing to V2
4104     l_contact_point_rec        hz_contact_point_v2pub.contact_point_rec_type;
4105     l_edi_rec                  hz_contact_point_v2pub.edi_rec_type;
4106     l_eft_rec                  hz_contact_point_v2pub.eft_rec_type;
4107     l_email_rec                hz_contact_point_v2pub.email_rec_type;
4108     l_phone_rec                hz_contact_point_v2pub.phone_rec_type;
4109     l_telex_rec                hz_contact_point_v2pub.telex_rec_type;
4110     l_web_rec                  hz_contact_point_v2pub.web_rec_type;
4111 
4112     --error message handling.
4116     x_msg_data                 VARCHAR2(2000);
4113     x_contact_point_id         NUMBER;
4114     x_msg_count                NUMBER := 0;
4115     x_return_status            VARCHAR2(100);
4117 
4118     --select contact points records.
4119     --will call API to get records. This is for the consistence purpose
4120     --in case of data model changes
4121 
4122     CURSOR c IS
4123       SELECT contact_point_id
4124       FROM   hz_contact_points
4125       WHERE  owner_table_name = p_owner_table_name
4126              AND owner_table_id = p_from_id;
4127 
4128     l_dup_exists                 VARCHAR2(10) := fnd_api.g_false;
4129 
4130   BEGIN
4131 
4132     arp_message.set_line( 'ARP_CMERGE_ARCUS.do_copy_contact_points ()+' );
4133 
4134     OPEN c;
4135     LOOP
4136       FETCH c INTO l_contact_point_id;
4137 
4138       EXIT WHEN c%NOTFOUND;
4139 
4140       l_dup_exists := check_contact_point_dup (l_contact_point_id,
4141                                                p_to_id,
4142                                                x_contact_point_id);
4143 
4144       --arp_message.set_line( '****' || to_char(x_contact_point_id) ||
4145       --'****' || l_dup_exists );
4146 
4147       IF l_dup_exists <> fnd_api.g_true THEN
4148 
4149         --duplicate not exist. Create new org contacts.
4150         --call API to get contact points info.
4151         ----Bug:2098728 Changing to V2
4152         ----Bug 2116225: Added support for banks (EFT).
4153         hz_cust_account_merge_v2pvt.get_contact_point_rec (
4154           p_init_msg_list =>  FND_API.G_TRUE,
4155           p_contact_point_id  => l_contact_point_id,
4156           x_contact_point_rec => l_contact_point_rec,
4157           x_edi_rec           => l_edi_rec,
4158           x_eft_rec           => l_eft_rec,
4159           x_email_rec         => l_email_rec,
4160           x_phone_rec         => l_phone_rec,
4161           x_telex_rec         => l_telex_rec,
4162           x_web_rec           => l_web_rec,
4163           x_return_status     => x_return_status,
4164           x_msg_count         => x_msg_count,
4165           x_msg_data          => x_msg_data);
4166         arp_message.set_line('ID ' || l_contact_point_rec.owner_table_id);
4167 
4168         IF x_msg_count = 1 THEN
4169           x_msg_data := x_msg_data || '**GET_CURRENT_CONTACT_POINTS**';
4170           arp_message.set_line(
4171             'do_copy_contact_points:get_current_contact_points ERROR ' ||
4172             x_msg_data );
4173         ELSIF x_msg_count > 1 THEN
4174 
4175           FOR x IN 1..x_msg_count LOOP
4176             x_msg_data := fnd_msg_pub.get(p_encoded => fnd_api.g_false);
4177             x_msg_data := x_msg_data || '**GET_CURRENT_CONTACT_POINTS**';
4178             arp_message.set_line(
4179               'do_copy_contact_points:get_current_contact_points ERROR ' ||
4180               x_msg_data );
4181           END LOOP;
4182         END IF;
4183 
4184         -- After call create_org_contact API, if return_status is not
4185         -- 'success', we need to populate an exception.
4186         IF x_return_status <> fnd_api.g_ret_sts_success THEN
4187           RAISE fnd_api.g_exc_error;
4188         END IF;
4189 
4190         l_contact_point_rec.contact_point_id :=  NULL;
4191         l_contact_point_rec.primary_flag :=  NULL;
4192         l_contact_point_rec.owner_table_id := p_to_id;
4193 
4194         arp_message.set_line('ID ' || l_contact_point_rec.owner_table_id);
4195 
4196         --We should not copy the following columns. They are supposed to be
4197         --unique
4198         l_edi_rec.edi_tp_header_id := NULL;
4199         l_edi_rec.edi_ece_tp_location_code :=  NULL;
4200 
4201         --create contact point with email type
4202         ----Bug:2098728 Changing to V2
4203         ----Bug 2116225: Added support for banks (EFT).
4204         hz_cust_account_merge_v2pvt.create_contact_point (
4205           fnd_api.g_true,
4206           l_contact_point_rec,
4207           l_edi_rec,
4208           l_eft_rec,
4209           l_email_rec,
4210           l_phone_rec,
4211           l_telex_rec,
4212           l_web_rec,
4213           x_contact_point_id,
4214           x_return_status,
4215           x_msg_count,
4216           x_msg_data);
4217 
4218         IF x_msg_count = 1 THEN
4219            x_msg_data := x_msg_data || '**CREATE_CONTACT_POINTS**';
4220           arp_message.set_line(
4221             'do_copy_contact_points:create_contact_points  Error '||
4222              x_msg_data);
4223         ELSIF x_msg_count > 1 THEN
4224 
4225           FOR x IN 1..x_msg_count LOOP
4226             x_msg_data := fnd_msg_pub.get(p_encoded => fnd_api.g_false);
4227             x_msg_data := x_msg_data || '**CREATE_CONTACT_POINTS**';
4228             arp_message.set_line(
4229               'do_copy_contact_points:create_contact_points ERROR ' ||
4230               x_msg_data );
4231           END LOOP;
4232         END IF;
4233 
4234         -- After call create_org_contact API, if return_status is not
4235         --  'success', we need to populate an exception.
4236         IF x_return_status <> fnd_api.g_ret_sts_success THEN
4237           RAISE fnd_api.g_exc_error;
4238         END IF;
4239 
4240 /**
4241         arp_message.set_line(
4242           'ARP_CMERGE_ARCUS:do_copy_contact_points():old contact point id =' ||
4243           l_contact_point_id );
4244 
4245         arp_message.set_line(
4246           'ARP_CMERGE_ARCUS.do_copy_contact_points:contact_point_id = ' ||
4247           x_contact_point_id );
4248 **/
4249 
4250       END IF;
4251 
4252 /**
4253       --comments out the following statement because table hz_cust_contact_points
4257       SET    contact_point_id = x_contact_point_id
4254       --has been obsoleted.
4255       -- Update the customer contact points with the new contact point id.
4256       UPDATE hz_cust_contact_points
4258       WHERE  contact_point_id = l_contact_point_id
4259       AND    cust_account_id = p_from_account_id;
4260 **/
4261     END LOOP;
4262     CLOSE c;
4263 
4264     arp_message.set_line( 'ARP_CMERGE_ARCUS.do_copy_contact_points()-' );
4265 
4266   EXCEPTION
4267 
4268     WHEN OTHERS THEN
4269       arp_message.set_error( 'ARP_CMERGE_ARCUS.do_copy_contact_points' );
4270       RAISE;
4271 
4272   END do_copy_contact_points;
4273 
4274 /*===========================================================================+
4275  | FUNCTION
4276  |              check_org_contact_dup
4277  |
4278  | DESCRIPTION
4279  |              Since org contact can be shared by different accounts, account
4280  |              sites, when migrate org contacts, before create a new org contact,
4281  |              we should check if there is an identical one already exist. If yes,
4282  |              pick it up.
4283  |
4284  | SCOPE - PRIVATE
4285  |
4286  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED
4287  |
4288  | ARGUMENTS  : IN:
4289  |                    p_from_org_contact_id
4290  |                    p_from_party_rel_id
4291  |                    p_to_party_id
4292  |              OUT:
4293  |                    x_org_contact_id
4294  |          IN/ OUT:
4295  |
4296  | RETURNS    : NONE
4297  |
4298  | NOTES
4299  |
4300  | MODIFICATION HISTORY
4301  |
4302  |     Jianying Huang  17-DEC-00  Created to check if there is an identical org
4303  |                        contact exist in database.
4304  |
4305  +===========================================================================*/
4306 
4307 FUNCTION check_org_contact_dup (
4308         p_from_org_contact_id         NUMBER,
4309         p_from_party_rel_id           NUMBER,
4310         p_to_party_id                 NUMBER,
4311         x_org_contact_id         OUT NOCOPY  NUMBER,
4312         p_from_account_id             NUMBER) RETURN VARCHAR2
4313 IS
4314 
4315     CURSOR dupcheck IS
4316        SELECT
4317            MIN(ORG_CONTACT_ID)
4318        FROM HZ_ORG_CONTACTS org
4319        WHERE DEPARTMENT_CODE ||
4320              DEPARTMENT ||
4321              TITLE ||
4322              JOB_TITLE ||
4323              MAIL_STOP ||
4324              DECISION_MAKER_FLAG ||
4325              JOB_TITLE_CODE ||
4326              TO_CHAR(MANAGED_BY) ||
4327              REFERENCE_USE_FLAG ||
4328              RANK ||
4329              STATUS = (
4330                 SELECT DEPARTMENT_CODE ||
4331                        DEPARTMENT ||
4332                        TITLE ||
4333                        JOB_TITLE ||
4334                        MAIL_STOP ||
4335                        DECISION_MAKER_FLAG ||
4336                        JOB_TITLE_CODE ||
4337                        TO_CHAR(MANAGED_BY) ||
4338                        REFERENCE_USE_FLAG ||
4339                        RANK ||
4340                        STATUS
4341                 FROM HZ_ORG_CONTACTS
4342                 WHERE ORG_CONTACT_ID = p_from_org_contact_id )
4343        AND EXISTS (
4344                     SELECT 'same relationships'
4345                       FROM HZ_RELATIONSHIPS rel
4346                      WHERE rel.RELATIONSHIP_ID = org.PARTY_RELATIONSHIP_ID
4347                        AND rel.OBJECT_ID = p_to_party_id
4348                        AND SUBJECT_TABLE_NAME = 'HZ_PARTIES'
4349                        AND OBJECT_TABLE_NAME = 'HZ_PARTIES'
4350                   /*   AND DIRECTIONAL_FLAG = 'F'  */ /* Bug No : 2359461 */
4351                        AND TO_CHAR(SUBJECT_ID) ||
4352                                    RELATIONSHIP_CODE = (
4353                                        SELECT TO_CHAR(SUBJECT_ID) ||
4354                                                          RELATIONSHIP_CODE
4355                                          FROM HZ_RELATIONSHIPS,HZ_CUST_ACCOUNTS ACCT
4356                                         WHERE RELATIONSHIP_ID =
4357  				                    p_from_party_rel_id
4358                                           AND OBJECT_ID = ACCT.PARTY_ID
4359                                           AND ACCT.CUST_ACCOUNT_ID = p_from_account_id
4360                                           AND SUBJECT_TABLE_NAME = 'HZ_PARTIES'
4361                                           AND OBJECT_TABLE_NAME = 'HZ_PARTIES'
4362                                        /* AND DIRECTIONAL_FLAG = 'F' */ /* Bug No : 2359461 */
4363                                          ) )
4364        AND ORG_CONTACT_ID <> p_from_org_contact_id;
4365 
4366        l_record_id NUMBER;
4367 
4368 BEGIN
4369     x_org_contact_id := FND_API.G_MISS_NUM;
4370 
4371     OPEN dupcheck;
4372     FETCH dupcheck INTO l_record_id;
4373     IF dupcheck%NOTFOUND OR l_record_id IS NULL THEN
4374        CLOSE dupcheck;
4375        RETURN FND_API.G_FALSE;
4376     END IF;
4377 
4378     x_org_contact_id := l_record_id;
4379     CLOSE dupcheck;
4380     RETURN FND_API.G_TRUE;
4381 
4382 EXCEPTION
4383 
4384     WHEN OTHERS THEN
4385       arp_message.set_error( 'ARP_CMERGE_ARCUS.check_org_contact_dup' );
4386       RAISE;
4387 
4388 END check_org_contact_dup;
4389 
4390 /*===========================================================================+
4391  | FUNCTION
4392  |              check_contact_point_dup
4393  |
4394  | DESCRIPTION  When migrate contact points, before create a new contact point,
4395  |              we should check if there is an identical one already exist. If yes,
4399  |
4396  |              pick it up.
4397  |
4398  | SCOPE - PRIVATE
4400  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED
4401  |
4402  | ARGUMENTS  : IN:
4403  |                    p_from_contact_point_id
4404  |                    p_to_owner_table_id
4405  |              OUT:
4406  |                    x_contact_point_id
4407  |          IN/ OUT:
4408  |
4409  | RETURNS    : NONE
4410  |
4411  | NOTES
4412  |
4413  | MODIFICATION HISTORY
4414  |     Jianying Huang  17-DEC-00  Created to check if there is a duplicate contact
4415  |                        point exist in database.
4416  |
4417  +===========================================================================*/
4418 
4419 FUNCTION check_contact_point_dup (
4420         p_from_contact_point_id       NUMBER,
4421         p_to_owner_table_id           NUMBER,
4422         x_contact_point_id       OUT NOCOPY  NUMBER ) RETURN VARCHAR2
4423 IS
4424 
4425     CURSOR dupcheck IS
4426        SELECT
4427           MIN(CONTACT_POINT_ID)
4428        FROM HZ_CONTACT_POINTS
4429        WHERE OWNER_TABLE_ID = p_to_owner_table_id
4430        AND
4431          OWNER_TABLE_NAME ||
4432          CONTACT_POINT_TYPE ||
4433          STATUS ||
4434          EDI_TRANSACTION_HANDLING ||
4435          EDI_ID_NUMBER ||
4436          EDI_PAYMENT_METHOD ||
4437          EDI_PAYMENT_FORMAT ||
4438          EDI_REMITTANCE_METHOD ||
4439          EDI_REMITTANCE_INSTRUCTION ||
4440          EMAIL_FORMAT ||
4441          TO_CHAR(BEST_TIME_TO_CONTACT_START, 'DD-MON-YYYY') ||
4442          TO_CHAR(BEST_TIME_TO_CONTACT_END, 'DD-MON-YYYY') ||
4443          PHONE_CALLING_CALENDAR ||
4444          DECLARED_BUSINESS_PHONE_FLAG ||
4445 -- phone_referred_order has been obsoleted.
4446          -- PHONE_PREFERRED_ORDER ||
4447          TELEPHONE_TYPE ||
4448          TIME_ZONE ||
4449          PHONE_TOUCH_TONE_TYPE_FLAG ||
4450          PHONE_AREA_CODE ||
4451          PHONE_COUNTRY_CODE ||
4452          PHONE_NUMBER ||
4453          PHONE_EXTENSION ||
4454          PHONE_LINE_TYPE ||
4455          TELEX_NUMBER ||
4456          CONTENT_SOURCE_TYPE ||
4457          WEB_TYPE ||
4458 	 CONTACT_POINT_PURPOSE
4459              = (SELECT
4460                       OWNER_TABLE_NAME ||
4461                       CONTACT_POINT_TYPE ||
4462                       STATUS ||
4463                       EDI_TRANSACTION_HANDLING ||
4464                       EDI_ID_NUMBER ||
4465                       EDI_PAYMENT_METHOD ||
4466                       EDI_PAYMENT_FORMAT ||
4467                       EDI_REMITTANCE_METHOD ||
4468                       EDI_REMITTANCE_INSTRUCTION ||
4469                       EMAIL_FORMAT ||
4470                       TO_CHAR(BEST_TIME_TO_CONTACT_START, 'DD-MON-YYYY') ||
4471                       TO_CHAR(BEST_TIME_TO_CONTACT_END, 'DD-MON-YYYY') ||
4472                       PHONE_CALLING_CALENDAR ||
4473                       DECLARED_BUSINESS_PHONE_FLAG ||
4474                       -- PHONE_PREFERRED_ORDER ||
4475                       TELEPHONE_TYPE ||
4476                       TIME_ZONE ||
4477                       PHONE_TOUCH_TONE_TYPE_FLAG ||
4478                       PHONE_AREA_CODE ||
4479                       PHONE_COUNTRY_CODE ||
4480                       PHONE_NUMBER ||
4481                       PHONE_EXTENSION ||
4482                       PHONE_LINE_TYPE ||
4483                       TELEX_NUMBER ||
4484                       CONTENT_SOURCE_TYPE ||
4485                       WEB_TYPE ||
4486 		      CONTACT_POINT_PURPOSE
4487                  FROM HZ_CONTACT_POINTS
4488                  WHERE CONTACT_POINT_ID = p_from_contact_point_id)
4489        AND nvl(EMAIL_ADDRESS,'NOEMAIL') = (
4490                SELECT nvl(EMAIL_ADDRESS,'NOEMAIL')
4491                FROM HZ_CONTACT_POINTS
4492                WHERE CONTACT_POINT_ID = p_from_contact_point_id)
4493        AND nvl(URL, 'NOURL') = (
4494                SELECT nvl(URL, 'NOURL')
4495                FROM HZ_CONTACT_POINTS
4496                WHERE CONTACT_POINT_ID = p_from_contact_point_id)
4497        AND CONTACT_POINT_ID <> p_from_contact_point_id;
4498 
4499        l_record_id NUMBER;
4500 
4501 BEGIN
4502 
4503     x_contact_point_id := FND_API.G_MISS_NUM;
4504 
4505     OPEN dupcheck;
4506     FETCH dupcheck INTO l_record_id;
4507     IF dupcheck%NOTFOUND OR l_record_id IS NULL THEN
4508        CLOSE dupcheck;
4509        RETURN FND_API.G_FALSE;
4510     END IF;
4511 
4512     x_contact_point_id := l_record_id;
4513     CLOSE dupcheck;
4514     RETURN FND_API.G_TRUE;
4515 
4516 EXCEPTION
4517     WHEN OTHERS THEN
4518       arp_message.set_error( 'ARP_CMERGE_ARCUS.check_contact_point_dup' );
4519       RAISE;
4520 
4521 END check_contact_point_dup;
4522 
4523 /*===========================================================================+
4524  | PROCEDURE
4525  |              create_temporary_table
4526  |
4527  | DESCRIPTION
4528  |              create global temporary table.
4529  |
4530  | SCOPE - PUBLIC
4531  |
4532  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED
4533  |
4534  | ARGUMENTS  : IN:
4535  |          IN/ OUT:
4536  |
4537  | RETURNS    : NONE
4538  |
4539  | NOTES
4540  |
4541  | MODIFICATION HISTORY
4542  |     Jianying Huang  20-DEC-00  Created. Bug 1535542: Create global temporary
4543  |                        table to store the mapping of old org_contact_id and new
4544  |                        org_contact_id when migrate org contacts.
4545  |
4546  +===========================================================================*/
4547 
4548 PROCEDURE create_temporary_table
4549 IS
4550 
4551           l_exist                VARCHAR2(1);
4555 
4552           l_sql                  VARCHAR2(1000);
4553 
4554 BEGIN
4556     arp_message.set_line( 'ARP_CMERGE_ARCUS.create_temporary_table ( ' ||
4557                            g_table_name || ' )()+' );
4558 
4559     BEGIN
4560 
4561        SELECT 'Y' INTO l_exist
4562        FROM user_tables
4563        WHERE table_name = g_table_name
4564        AND   ROWNUM = 1;
4565 
4566     EXCEPTION
4567        WHEN NO_DATA_FOUND THEN
4568           l_exist := 'N';
4569     END;
4570 
4571     --table does not exist
4572     IF l_exist = 'N' THEN
4573 
4574        --create session-based global temporary table.
4575        l_sql := 'CREATE GLOBAL TEMPORARY TABLE ' ||
4576                 g_table_name ||
4577                 '(type VARCHAR2(30), ' ||
4578                 'old_id NUMBER, ' ||
4579                 'new_id NUMBER) ' ||
4580                 'ON COMMIT PRESERVE ROWS';
4581     ELSE
4582 
4583        l_sql := 'DELETE ' || g_table_name;
4584     END IF;
4585 
4586     EXECUTE IMMEDIATE l_sql;
4587 
4588     arp_message.set_line( 'ARP_CMERGE_ARCUS.create_temporary_table ( ' ||
4589                            g_table_name || ' )()-' );
4590 
4591 EXCEPTION
4592 
4593      WHEN OTHERS THEN
4594        arp_message.set_error( 'ARP_CMERGE_ARCUS.create_temporary_table ( ' ||
4595                               g_table_name || ' )' );
4596        RAISE;
4597 
4598 END create_temporary_table;
4599 
4600 /*===========================================================================+
4601  | PROCEDURE
4602  |              delete_rows
4603  |
4604  | DESCRIPTION  physically delete the rows we marked in customer tables after
4605  |              we merging eact set.
4606  |
4607  | SCOPE - PUBLIC
4608  |
4609  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED
4610  |
4611  | ARGUMENTS  : IN:
4612  |              OUT:
4613  |          IN/ OUT:
4614  |
4615  | RETURNS    : NONE
4616  |
4617  | NOTES
4618  |
4619  | MODIFICATION HISTORY
4620  |     Jianying Huang  19-DEC-00  Bug 1535542: physically delete rows in
4621  |                        customer tables after merging each set.
4622  |     Jianying Huang  29-DEC-00  Modified 'delete_rows' for performance issue.
4623  |     Jianying Huang  09-APR-01  Bug 1725662: rewrite sql statement on delete
4624  |                        ra_cust_receipt_methods to use index.
4625  |
4626  +===========================================================================*/
4627 
4628 PROCEDURE delete_rows(
4629           req_id                    NUMBER,
4630           set_num                   NUMBER
4631 ) IS
4632 
4633     CURSOR cust_site_uses IS
4634        SELECT site_use_id
4635        FROM HZ_CUST_SITE_USES_ALL su, ra_customer_merges m --SSUptake
4636        WHERE cust_acct_site_id = m.duplicate_address_id
4637        AND   m.org_id   = su.org_id --SSUptake
4638        AND   m.process_flag = 'N'
4639        AND   m.request_id = req_id
4640        AND   m.set_number = set_num
4641        AND   m.delete_duplicate_flag = 'Y'
4642        AND   su.status = 'D'
4643        FOR UPDATE NOWAIT;
4644 
4645     CURSOR cust_sites IS
4646        SELECT cust_acct_site_id
4647        FROM HZ_CUST_ACCT_SITES_ALL addr, ra_customer_merges m --SSUptake
4648        WHERE cust_acct_site_id = m.duplicate_address_id
4649        AND   m.org_id = addr.org_id --SSUptake
4650        AND   m.process_flag = 'N'
4651        AND   m.request_id = req_id
4652        AND   m.set_number = set_num
4653        AND   m.delete_duplicate_flag = 'Y'
4654        AND   addr.status = 'D'
4655        FOR UPDATE NOWAIT;
4656 
4657     CURSOR cust_accounts IS
4658        SELECT cust_account_id
4659        FROM HZ_CUST_ACCOUNTS acct, ra_customer_merges m
4660        WHERE cust_account_id = m.duplicate_id
4661        AND   m.process_flag = 'N'
4662        AND   m.request_id = req_id
4663        AND   m.set_number = set_num
4664        AND   m.delete_duplicate_flag = 'Y'
4665        AND   acct.status = 'D'
4666        FOR UPDATE NOWAIT;
4667 
4668     CURSOR cust_rel1 IS
4669        SELECT rel.cust_account_id
4670        FROM HZ_CUST_ACCT_RELATE_ALL rel, ra_customer_merges m --SSUptake
4671        WHERE cust_account_id = m.duplicate_id
4672        AND   m.org_id = rel.org_id --SSUptake
4673        AND   m.process_flag = 'N'
4674        AND   m.request_id = req_id
4675        AND   m.set_number = set_num
4676        AND   m.delete_duplicate_flag = 'Y'
4677        AND   rel.status = 'D'
4678        FOR UPDATE NOWAIT;
4679 
4680     CURSOR cust_rel2 IS
4681        SELECT rel.related_cust_account_id
4682        FROM HZ_CUST_ACCT_RELATE_ALL rel, ra_customer_merges m --SSUptake
4683        WHERE related_cust_account_id =  m.duplicate_id
4684        AND   m.org_id  = rel.org_id --SSUptake
4685        AND   m.process_flag = 'N'
4686        AND   m.request_id = req_id
4687        AND   m.set_number = set_num
4688        AND   m.delete_duplicate_flag = 'Y'
4689        AND   rel.status = 'D'
4690        FOR UPDATE NOWAIT;
4691 
4692     CURSOR cust_receipt_methods_site IS
4693        SELECT CUST_RECEIPT_METHOD_ID
4694        FROM RA_CUST_RECEIPT_METHODS yt, ra_customer_merges m
4695        WHERE yt.customer_id = m.duplicate_id
4696        AND   yt.site_use_id = m.duplicate_site_id
4697        AND   m.process_flag = 'N'
4698        AND   m.request_id = req_id
4699        AND   m.set_number = set_num
4700        AND   m.delete_duplicate_flag = 'Y'
4701       FOR UPDATE NOWAIT;
4702 
4703     CURSOR cust_receipt_methods_acct IS
4704         SELECT CUST_RECEIPT_METHOD_ID
4705         FROM RA_CUST_RECEIPT_METHODS yt, ra_customer_merges m
4706         WHERE yt.customer_id = m.duplicate_id
4707         AND   m.process_flag = 'N'
4711         AND   site_use_id IS NULL
4708         AND   m.request_id = req_id
4709         AND   m.set_number = set_num
4710         AND   m.delete_duplicate_flag = 'Y'
4712         AND NOT EXISTS (
4713                     SELECT 'accounts exist'
4714                     FROM   hz_cust_accounts acct
4715                     WHERE  acct.cust_account_id = yt.customer_id
4716                     AND    acct.status <> 'D' )
4717         FOR UPDATE NOWAIT;
4718 
4719     --bug 4307679
4720     CURSOR cust_usage IS
4721         SELECT party_usg_assignment_id
4722         FROM   hz_party_usg_assignments u
4723         WHERE  party_usage_code = 'CUSTOMER'
4724         AND    status_flag = 'D'
4725         AND    party_id in (SELECT DISTINCT c.party_id from hz_cust_accounts c, ra_customer_merges m
4726                             WHERE c.cust_account_id = m.duplicate_id
4727                             AND m.process_flag = 'N'
4728                             AND m.request_id = req_id
4729                             AND m.set_number = set_num
4730                             AND m.delete_duplicate_flag = 'Y');
4731 
4732 BEGIN
4733 
4734     arp_message.set_line( 'ARP_CMERGE_ARCUS.delete_rows()+' );
4735 
4736     /*****************************************************/
4737 
4738 --4307679
4739     arp_message.set_name( 'AR', 'AR_DELETING_TABLE' );
4740     arp_message.set_token( 'TABLE_NAME', 'HZ_PARTY_USG_ASSIGNMENTS', FALSE );
4741 
4742     --lock rows
4743     OPEN cust_usage;
4744     CLOSE cust_usage;
4745 
4746     DELETE FROM hz_party_usg_assignments u
4747     WHERE   party_usage_code = 'CUSTOMER'
4748     AND    status_flag = 'D'
4749     AND    party_id in (SELECT DISTINCT c.party_id from hz_cust_accounts c, ra_customer_merges m
4750                             WHERE c.cust_account_id = m.duplicate_id
4751                             AND m.process_flag = 'N'
4752                             AND m.request_id = req_id
4753                             AND m.set_number = set_num
4754                             AND m.delete_duplicate_flag = 'Y');
4755 
4756     g_count := sql%rowcount;
4757 
4758     arp_message.set_name( 'AR', 'AR_ROWS_DELETED' );
4759     arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
4760 
4761 --4397679
4762 
4763     arp_message.set_name( 'AR', 'AR_DELETING_TABLE' );
4764     arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_SITE_USES', FALSE );
4765 
4766     --lock rows
4767     OPEN cust_site_uses;
4768     CLOSE cust_site_uses;
4769 
4770     delete from hz_orig_sys_references where
4771                owner_table_name = 'HZ_CUST_SITE_USES_ALL' and
4772                owner_table_id in (
4773                  select site_use_id from hz_cust_site_uses_all su
4774                  where  status = 'D'
4775                  and exists ( SELECT 'Y'
4776                               FROM   ra_customer_merges m
4777                               WHERE  su.cust_acct_site_id = m.duplicate_address_id
4778                               AND    su.org_id   = m.org_id
4779                               AND    m.process_flag = 'N'
4780                               AND    m.request_id = req_id
4781                               AND    m.set_number = set_num
4782                               AND    m.delete_duplicate_flag = 'Y'
4783                             )
4784                 );
4785 
4786     DELETE FROM HZ_CUST_SITE_USES_ALL su
4787     WHERE EXISTS  (
4788                       SELECT 'Y'
4789                       FROM   ra_customer_merges m
4790                       WHERE  m.duplicate_address_id = su.cust_acct_site_id
4791 		      AND    m.org_id = su.org_id
4792 		      AND    m.process_flag = 'N'
4793                       AND    m.request_id = req_id
4794                       AND    m.set_number = set_num
4795                       AND    m.delete_duplicate_flag = 'Y' )
4796     AND status = 'D';
4797 
4798 
4799     g_count := sql%rowcount;
4800 
4801     arp_message.set_name( 'AR', 'AR_ROWS_DELETED' );
4802     arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
4803 
4804     /*****************************************************/
4805 
4806     arp_message.set_name( 'AR', 'AR_DELETING_TABLE' );
4807     arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCT_SITES', FALSE );
4808 
4809     --lock rows
4810     OPEN cust_sites;
4811     CLOSE cust_sites;
4812 
4813     delete from hz_orig_sys_references where
4814                owner_table_name = 'HZ_CUST_ACCT_SITES_ALL' and
4815                owner_table_id in (
4816                select cust_acct_site_id from hz_cust_acct_sites_all sites --SSuptake
4817                where  status = 'D'
4818                and EXISTS
4819                      ( SELECT 'Y'
4820                       FROM   ra_customer_merges m
4821                       WHERE  m.duplicate_address_id = sites.cust_acct_site_id
4822                       AND    m.org_id = sites.org_id
4823                       AND    m.process_flag = 'N'
4824                       AND    m.request_id = req_id
4825                       AND    m.set_number = set_num
4826                       AND    m.delete_duplicate_flag = 'Y' ));
4827 
4828     DELETE FROM HZ_CUST_ACCT_SITES_ALL yt --SSUptake
4829     WHERE EXISTS (
4830                       SELECT 'Y'
4831                       FROM   ra_customer_merges m
4832                       WHERE  m.duplicate_address_id = yt.cust_acct_site_id
4833 		      AND    m.org_id = yt.org_id
4834 		      AND    m.process_flag = 'N'
4835                       AND    m.request_id = req_id
4836                       AND    m.set_number = set_num
4837                       AND    m.delete_duplicate_flag = 'Y' )
4838     AND status = 'D';
4839 
4840     arp_message.set_name( 'AR', 'AR_ROWS_DELETED' );
4841     arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
4842 
4843     /*****************************************************/
4844 
4845     arp_message.set_name( 'AR', 'AR_DELETING_TABLE' );
4846     arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCOUNTS', FALSE );
4847 
4848     --lock rows
4849     OPEN cust_accounts;
4850     CLOSE cust_accounts;
4851 
4852     delete from hz_orig_sys_references where
4853                owner_table_name = 'HZ_CUST_ACCOUNTS' and
4854                owner_table_id in (
4855                select cust_account_id from hz_cust_accounts where
4856                status = 'D' and cust_account_id in
4857                ( SELECT m.duplicate_id
4858                       FROM   ra_customer_merges m
4859                       WHERE  m.process_flag = 'N'
4860                       AND    m.request_id = req_id
4861                       AND    m.set_number = set_num
4862                       AND    m.delete_duplicate_flag = 'Y' ));
4863 
4864     DELETE FROM HZ_CUST_ACCOUNTS
4865     WHERE cust_account_id IN (
4866                  SELECT m.duplicate_id
4867                  FROM   ra_customer_merges m
4868                  WHERE  m.process_flag = 'N'
4869                  AND    m.request_id = req_id
4870                  AND    m.set_number = set_num
4871                  AND    m.delete_duplicate_flag = 'Y' )
4872     AND status = 'D';
4873 
4874     g_count := sql%rowcount;
4875 
4876     arp_message.set_name( 'AR', 'AR_ROWS_DELETED' );
4877     arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
4878 
4879     /*****************************************************/
4880 
4881     arp_message.set_name( 'AR', 'AR_DELETING_TABLE' );
4882     arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCT_RELATE', FALSE );
4883 
4884     --lock rows
4885     OPEN cust_rel1;
4886     CLOSE cust_rel1;
4887 
4888     DELETE FROM HZ_CUST_ACCT_RELATE_ALL rel --SSUptake
4889     WHERE  EXISTS (
4890                SELECT 'Y'
4891                FROM   ra_customer_merges m
4892                WHERE  m.duplicate_id = rel.cust_account_id
4893 	       AND    m.org_id  = rel.org_id
4894 	       AND    m.process_flag = 'N'
4895                AND    m.request_id = req_id
4896                AND    m.set_number = set_num
4897                AND    m.delete_duplicate_flag = 'Y'
4898 	       AND    m.org_id  = rel.org_id) --SSUptake
4899     AND status = 'D';
4900 
4901     g_count := sql%rowcount;
4902 
4903     arp_message.set_name( 'AR', 'AR_ROWS_DELETED' );
4904     arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
4905 
4906     arp_message.set_name( 'AR', 'AR_DELETING_TABLE' );
4907     arp_message.set_token( 'TABLE_NAME', 'HZ_CUST_ACCT_RELATE', FALSE );
4908 
4909     --lock rows
4910     OPEN cust_rel2;
4911     CLOSE cust_rel2;
4912 
4913     DELETE FROM HZ_CUST_ACCT_RELATE_ALL rel --SSUptake
4914     WHERE EXISTS (
4915                SELECT 'Y'
4916                FROM   ra_customer_merges m
4917                WHERE  m.duplicate_id = rel.related_cust_account_id
4918 	       AND    m.org_id = rel.org_id
4919 	       AND    m.process_flag = 'N'
4920                AND    m.request_id = req_id
4921                AND    m.set_number = set_num
4922                AND    m.delete_duplicate_flag = 'Y') --SSUptake
4923     AND status = 'D';
4924 
4925     g_count := sql%rowcount;
4926 
4927     arp_message.set_name( 'AR', 'AR_ROWS_DELETED' );
4928     arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
4929 
4930     /************** account site level delete ************/
4931 
4932     arp_message.set_name( 'AR', 'AR_DELETING_TABLE' );
4933     arp_message.set_token( 'TABLE_NAME', 'RA_CUST_RECEIPT_METHODS', FALSE );
4934 
4935     OPEN cust_receipt_methods_site;
4936     CLOSE cust_receipt_methods_site;
4937 
4938 --Bug 1725662: Rewrite the query to use index.
4939 
4940     DELETE FROM RA_CUST_RECEIPT_METHODS yt
4941     WHERE (customer_id, site_use_id) in (
4942                 SELECT m.duplicate_id, m.duplicate_site_id
4943                 FROM   ra_customer_merges m
4944                 WHERE  m.process_flag = 'N'
4945                 AND    m.request_id = req_id
4946                 AND    m.set_number = set_num
4947                 AND    m.delete_duplicate_flag = 'Y');
4948 
4949     g_count := sql%rowcount;
4950 
4951     arp_message.set_name( 'AR', 'AR_ROWS_DELETED' );
4952     arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
4953 
4954     /************** account level delete ************/
4955 
4956     arp_message.set_name( 'AR', 'AR_DELETING_TABLE' );
4957     arp_message.set_token( 'TABLE_NAME', 'RA_CUST_RECEIPT_METHODS', FALSE );
4958 
4959     OPEN cust_receipt_methods_acct;
4960     CLOSE cust_receipt_methods_acct;
4961 
4962     DELETE FROM RA_CUST_RECEIPT_METHODS yt
4963     WHERE customer_id in (
4964                 SELECT m.duplicate_id
4965                 FROM   ra_customer_merges m
4966                 WHERE  m.process_flag = 'N'
4967                 AND    m.request_id = req_id
4968                 AND    m.set_number = set_num )
4969     AND site_use_id IS NULL
4970     AND NOT EXISTS (
4971                 SELECT 'accounts exist'
4972                 FROM   hz_cust_accounts acct
4973                 WHERE  acct.cust_account_id = yt.customer_id
4974                 AND    status <> 'D' );
4975 
4976     g_count := sql%rowcount;
4977 
4978     arp_message.set_name( 'AR', 'AR_ROWS_DELETED' );
4979     arp_message.set_token( 'NUM_ROWS', to_char(g_count) );
4980 
4981     arp_message.set_line( 'ARP_CMERGE_ARCUS.delete_rows()-' );
4982 
4983 EXCEPTION
4984 
4985     WHEN OTHERS THEN
4986       arp_message.set_error( 'ARP_CMERGE_ARCUS.delete_rows' );
4987       RAISE;
4988 
4989 END delete_rows;
4990 
4991 /*===========================================================================+
4992  | PROCEDURE
4993  |              create_same_sites
4994  |
4995  | DESCRIPTION
4996  |
4997  | SCOPE - PUBLIC
4998  |
4999  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED
5000  |
5001  | ARGUMENTS  : IN:
5002  |                    req_id
5003  |                    set_num
5004  |              OUT:
5005  |          IN/ OUT:
5006  |
5007  | RETURNS    : NONE
5008  |
5009  | NOTES
5010  |          Conditions when create new site/site uses.
5011  |    1. If user tries to create same location
5012  |       1) if there exsits the same site use, for instance,
5013  |          in database we have,
5014  |               merge-from                   merge-to
5015  |            Address1, Bill-To            Address1, Bill-To
5016  |          User tries to merge: address1, Bill-To with 'create
5017  |          same site' checked, In this case, since merge-to
5018  |          already has account site on address1 and has the
5019  |          desired business purpose, Bill-To, we should not
5020  |          create new account site/site use, instead, we need
5021  |          to merge these two account site use and unset the
5022  |          value of 'create same site'.
5023  |       2) if there not exists the same site use, for instance,
5024  |          in database we have,
5025  |               merge-from                   merge-to
5026  |            Address1, Bill-To,           Address1, Ship-To
5027  |            Address1, Ship-To
5028  |          User tries to merge: Address1->Address1 with 'create
5029  |          same site' for Bill-To checked. In this case, since
5030  |          merge-to already has account site on address1, we should
5031  |          not create new account site, instead, we need to create
5032  |          a new account site use, which is for Bill-To purpose.
5033  |    2. If user tries to create different location, we need to
5034  |       create (party site)/account site, (party site use)/account site use
5035  |
5036  |       The following flow is for merge-to customer. For instance, 'Account
5037  |       Site not have' means merge-to does not have account site on the
5038  |       merge-from's location. 'Party Site Use have' means the merge-to party
5039  |       has a party site use on the merge-from's location with same site use
5040  |       code. The same meaning is appliable for Party Site etc.
5041  |
5042  |                             Account Site
5043  |                             /          \
5044  |                  not have  /            \  have
5045  |                           /              \
5046  |                     Party Site            Account Site Use
5047  |                        /   \                      |  \
5048  |              not have /     \ have       not have |   \ have
5049  |                      /       \                    |    \
5050  |         create Party Site   Party Site Use        |   need to do merge
5051  |               \                  /   \            |
5052  |                \       not have /     \ have      |
5053  |                 \              /       \          |
5054  |               create Party Site Use     \         |
5055  |                            \            /         |
5056  |                           create Account Site     |
5057  |                                    \              /
5058  |                                 create Account Site Use
5059  |                                 create Customer Profile
5060  |                                 create Customer Profile Amount
5061  |
5062  |          One of the merge rules is we should not update data in party level.
5063  |    So we should not update, for example, merge-to's party site with merge-from's
5064  |    party site if there exists a party site on the location for merge-to's party.
5065  |
5066  |          After create account site for merge-to customer,
5067  |    we need to fill out the columns of ra_customer_merges for later query.
5068  |         -- customer_address_id    <--  cust_acct_site_id
5069  |         -- customer_site_id       <--  site_use_id
5070  |         -- customer_ref           <--  orig_system_reference
5071  |         -- customer_primary_flag  <--  primary_flag
5072  |         -- customer_location      <--  location
5073  |         -- customer_site_code is created by merge form.
5074  |
5075  | MODIFICATION HISTORY
5076  |    Jianying Huang  27-NOV-00  After call create_* API,
5077  |                       if return_status is not 'success', we need to
5078  |                       populate an exception.
5079  |    Jianying Huang  27-NOV-00  Remove 'UPDATE hz_org_contacts ..'
5080  |                       and 'UPDATE hz_contact_points..' part. We should
5081  |                       not modify contacts data in party level. Instead,
5082  |                       we should migrate those data during merge process.
5083  |    Jianying Huang  27-NOV-00  Added condition that we create profile amts
5084  |                       if only if there is profile exist for the site use.
5085  |    Jianying Huang  07-DEC-OO  Bug 1391134: Move the call of createSites
5086  |                       in set-based merge from merge-form becuase we
5087  |                       have to commit data for every set.
5088  |    Jianying Huang  07-DEC-00  Bug 1512300: Modify createSites to copy GL
5089  |                       accounts.
5090  |    Jianying Huang  07-DEC-00  Bug 1472578: Modify createSites to
5091  |                       'create site/site use' in different scenario.
5092  |    Jianying Huang  07-DEC-00  Bug 1227593: Added column 'ADDRESSEE'
5093  |                       when we create new party site.
5094  |    Jianying Huang  07-DEC-OO  Should not copy tp_header_id. It is an unique
5095  |                       column in hz_cust_acct_sites_all.
5096  |    Jianying Huang  12-DEC-00  Check 'x_return_status' after call
5097  |                       'create_cust_prof_amt'
5098  |    Jianying Huang  20-DEC-00  Bug 1535542: Since we will call customer merge
5099  |                       before merging prEoducts, we should move 'createSites'
5100  |                       in arplbmst.sql's merge_customers procedure. However,
5101  |                       to avoid later calling order change, I rename it to '
5102  |                       create_same_sites', move it to here(because it is related
5103  |                       to customer tables), make it public and call it from
5104  |                       merge report.
5105  |    Jianying Huang  28-DEC-00  When create new account site use, should not
5106  |                       copy location. Instead, should enforce the API
5107  |                       generate location.
5108  |    Jianying Huang  09-APR-01  When copy site use, should copy 'contact_id'
5109  |                       because contact_id which references to cust_account_role_id
5110  |                       will to move to merge-to customer after merge.
5111  |    Jianying Huang  22-JUL-01  Removed 'FOR UPDATE NOWAIT' for cursor
5112  |                       'sites_need_to_create'. This is a workaround of
5113  |                       'fetch out of sequence error' (see bug 1375214)
5114  |                       on 8.1.6.2 onwards (the fix are done in 8.1.7.2).
5115  |    Jianying Huang  19-OCT-01  Bug 2062466: Modified procedure 'create_same_sites'
5116  |                       to reset initial value of local varibles.
5117  |    Jianying Huang  26-OCT-01  Bug 2077604: Modified procedure 'create_same_sites'
5118  |                       to add 'cust_account_profile_id = l_cust..' when
5119  |                       select merge-from site use's profile amounts.
5120  |    Jyoti Pandey    06-NOV-01 Bug:2098728 Changing all API call outs to call
5121  |                        Package hz_cust_account_merge_v2pvt
5122  |    Sisir           13-MAR-02 Bug:2241033;Written code for creating Payment
5123  |                       Method for Customer Site Use
5124  |    P.Suresh        05-APR-02 Bug No : 2272750. Populated bill_to_site_use_id.
5125  |    Rajeshwari P    12-APR-02 Bug 2183072.Handled exception for Select from
5126  |                              ar_system_parameters.
5127  |    Jyoti Pandey    20-MAY-02 Bug:2376975 create site use,profiles, amts etc. only
5128  |                              is dup_site_use_id <> -99 . Form sets to -99 if
5129  |                              there is no site use for a given site.
5130  |    P.Suresh        13-JUN-02 Bug No : 2403263. Added contact_id to
5131  |                              hz_cust_site_uses.
5132  |    Rajeshwari P    10-OCT-02 Bug No:2529143.Added another parameter 'Credit_classification'
5133  |                              during creation of Profile for a customer
5134  |                              in create_same_sites procedure.
5135  |   Dhaval Mehta     28-JUL-03 Bug 2971149. Added the code back to copy
5136  |				party_site_uses when the create_same_site
5137  |				flag is checked. Added a call to
5138  |				hz_cust_account_merge_v2pvt.create_party_site_use
5139  |				in procedure create_same_sites.
5140  |    S.V.Sowjanya    02-DEC-04 Bug No: 3959776. Updated column customer_site_number
5141  |                                in ra_customer_merges.
5142  |    S.V.Sowjanya    04-JAN-05 Bug No: 4018346. Assigned null values to l_customer_location,
5143  |                              l_customer_site_id in the beginning of the loop for
5144  |				cursor sites_need_to_create and removed nvl condition
5145  |                              for l_customer_location in the update statement of ra_customer_merges
5146  |                              at the end  of the loop for cursor sites_need_to_create.
5147  |   S V Sowjanya     10-AUG-05 Bug No:4492628. Moved code that copies party_site_uses
5148  |                                to copy party_site_use after the creation of account_site
5149  |   Kalyana	      12-Oct-07	Bug No: 6469732 Modified the procedure create_same_site so that if already an Active Customer
5150  |   Chakravarthy		site use of Dunning or Statement exists for TO Party and FROM Party also have an Active use of
5151  |				Dunning or statement then it create the site use for TO Party in Status Inactive.
5152  +===========================================================================*/
5153 
5154 PROCEDURE create_same_sites(
5155           req_id                NUMBER,
5156           set_num               NUMBER,
5157           status            OUT NOCOPY NUMBER,
5158 	  error_text        OUT NOCOPY VARCHAR2
5159 ) IS
5160 
5161     --The rows selected in the cursor are rows we need to create
5162     --new sites.
5163     CURSOR sites_need_to_create IS
5164        SELECT duplicate_id, duplicate_address_id, duplicate_site_id,
5165               duplicate_site_code, customer_id,org_id, delete_duplicate_flag  --SSUptake
5166        FROM   ra_customer_merges
5167        WHERE  duplicate_id <> customer_id
5168        AND    process_flag = 'N'
5169        AND    request_id = req_id
5170        AND    set_number = set_num
5171        AND    customer_createsame = 'Y'
5172        ORDER BY duplicate_site_code desc;
5173 
5174     l_delete_flag VARCHAR2(1);
5175     l_duplicate_id                 NUMBER;
5176     l_duplicate_address_id         NUMBER;
5177     l_duplicate_site_id            NUMBER;
5178     l_duplicate_site_code          VARCHAR2(40);
5179     l_customer_id                  NUMBER;
5180     m_org_id                       NUMBER(15); --SSUptake
5181 
5182     l_customer_address_id          NUMBER;
5183     l_customer_site_id             NUMBER;
5184     l_customer_ref                 VARCHAR2(240);
5185     l_customer_primary_flag        VARCHAR2(1);
5186     l_customer_location            VARCHAR2(240);
5187 
5188     party_site_rec         HZ_PARTY_SITE_V2PUB.party_site_rec_type;
5189     party_site_use_rec     HZ_PARTY_SITE_V2PUB.party_site_use_rec_type;
5190     cust_site_rec          HZ_CUST_ACCOUNT_SITE_V2PUB.cust_acct_site_rec_type;
5191     cust_site_use_rec      HZ_CUST_ACCOUNT_SITE_V2PUB.cust_site_use_rec_type;
5192     cust_prof_rec          HZ_CUSTOMER_PROFILE_V2PUB.customer_profile_rec_type;
5193     cust_prof_amt          HZ_CUSTOMER_PROFILE_V2PUB.cust_profile_amt_rec_type;
5194 
5195     l_party_site_id                NUMBER;
5196     l_party_site_number            VARCHAR2(30);
5197     l_party_site_use_id            NUMBER;
5198     l_to_cust_account_profile_id   NUMBER;
5199     l_cust_acct_profile_amt_id     NUMBER;
5200     l_cust_account_profile_id      NUMBER;
5201 
5202     CURSOR merge_from_prof_amt IS
5203        SELECT
5204           currency_code,
5205           trx_credit_limit,
5206           overall_credit_limit,
5207           min_dunning_amount,
5208           min_dunning_invoice_amount,
5209           max_interest_charge,
5210           min_statement_amount,
5211           auto_rec_min_receipt_amount,
5212           interest_rate,
5213           attribute_category,
5214           attribute1,
5215           attribute2,
5216           attribute3,
5217           attribute4,
5218           attribute5,
5219           attribute6,
5220           attribute7,
5221           attribute8,
5222           attribute9,
5223           attribute10,
5224           attribute11,
5225           attribute12,
5226           attribute13,
5227           attribute14,
5228           attribute15,
5229           min_fc_balance_amount,
5230           min_fc_invoice_amount,
5231           expiration_date,
5232            --Bug:2098728 obsoleted wh_update_date,
5233           jgzz_attribute_category,
5234           jgzz_attribute1,
5235           jgzz_attribute2,
5236           jgzz_attribute3,
5237           jgzz_attribute4,
5238           jgzz_attribute5,
5239           jgzz_attribute6,
5240           jgzz_attribute7,
5241           jgzz_attribute8,
5242           jgzz_attribute9,
5243           jgzz_attribute10,
5244           jgzz_attribute11,
5245           jgzz_attribute12,
5246           jgzz_attribute13,
5247           jgzz_attribute14,
5248           jgzz_attribute15,
5249           global_attribute1,
5250           global_attribute2,
5251           global_attribute3,
5252           global_attribute4,
5253           global_attribute5,
5254           global_attribute6,
5255           global_attribute7,
5256           global_attribute8,
5257           global_attribute9,
5258           global_attribute10,
5259           global_attribute11,
5260           global_attribute12,
5261           global_attribute13,
5262           global_attribute14,
5263           global_attribute15,
5264           global_attribute16,
5265           global_attribute17,
5266           global_attribute18,
5267           global_attribute19,
5268           global_attribute20,
5269           global_attribute_category,
5270 --Bug 5040679 - AR new columns
5271 	exchange_rate_type,
5272     	min_fc_invoice_overdue_type,
5273     	min_fc_invoice_percent,
5274     	min_fc_balance_overdue_type,
5275     	min_fc_balance_percent,
5276     	interest_type,
5277     	interest_fixed_amount,
5278     	interest_schedule_id,
5279     	penalty_type,
5280     	penalty_rate,
5281     	min_interest_charge,
5282     	penalty_fixed_amount,
5283     	penalty_schedule_id
5284 
5285        FROM hz_cust_profile_amts
5286        WHERE cust_account_id = l_duplicate_id
5287        AND   site_use_id     = l_duplicate_site_id
5288        AND   cust_account_profile_id = l_cust_account_profile_id;
5289 
5290  CURSOR merge_from_pay_method IS
5291 	select
5292 	CUST_RECEIPT_METHOD_ID,
5293 	CUSTOMER_ID,
5294 	RECEIPT_METHOD_ID,
5295 	PRIMARY_FLAG,
5296 	LAST_UPDATE_DATE,
5297 	LAST_UPDATED_BY,
5298 	CREATION_DATE,
5299 	CREATED_BY,
5300 	LAST_UPDATE_LOGIN,
5301 --	REQUEST_ID,
5302 --	PROGRAM_APPLICATION_ID,
5303 --	PROGRAM_ID,
5304 --	PROGRAM_UPDATE_DATE,
5305 	SITE_USE_ID,
5306 	START_DATE,
5307 	END_DATE,
5308 	ATTRIBUTE_CATEGORY,
5309 	ATTRIBUTE1,
5310 	ATTRIBUTE2,
5311 	ATTRIBUTE3,
5312 	ATTRIBUTE4,
5313 	ATTRIBUTE5,
5314 	ATTRIBUTE6,
5315 	ATTRIBUTE7,
5316 	ATTRIBUTE8,
5317 	ATTRIBUTE9,
5318 	ATTRIBUTE10,
5319 	ATTRIBUTE11,
5320 	ATTRIBUTE12,
5321 	ATTRIBUTE13,
5322 	ATTRIBUTE14,
5323 	ATTRIBUTE15
5324  	from 	RA_CUST_RECEIPT_METHODS
5325 	WHERE 	CUSTOMER_ID = l_duplicate_id
5326 	AND   	site_use_id     = l_duplicate_site_id;
5327 	l_row_id 						varchar2(240);
5328 	l_Cust_Receipt_Method_Id 		varchar2(15);
5329 	merge_from_pay_method_row		merge_from_pay_method%rowtype;
5330 
5331     l_profile_option               VARCHAR2(1) := 'Y';
5332     l_create_profile               VARCHAR2(1) := 'N';
5333     l_converted_create_profile     VARCHAR2(1);
5334 
5335     l_duplicate_party_site_id      NUMBER;
5336     l_location_id                  NUMBER;
5337     l_merge_to_party_id            NUMBER;
5338     l_create_party_site_use        VARCHAR2(1) := 'N';
5339     l_create_acct_site_use         VARCHAR2(1) := 'N';
5340     l_gen_loc                      VARCHAR2(1) := 'Y';
5341 
5342     l_exist                        VARCHAR2(1) := 'N';
5343     x_return_status                VARCHAR2(20);
5344     x_msg_data                     VARCHAR2(2000);
5345     x_msg_count                    NUMBER := 0;
5346     x_customer_site_id             NUMBER;
5347     l_count                        NUMBER;
5348     l_actual_cont_source	   VARCHAR2(30);
5349 
5350     ---to get the value from hz_cust_acct_site and hz_cust_site_uses
5351     ---create a new one with same org_id
5352 
5353     l_org_id                       NUMBER := NULL ;
5354     site_use_org_id                NUMBER := NULL;
5355 
5356 -- Bug 2971149. Added a local varialbe to store party_site_use_id
5357 
5358     l_duplicate_party_site_use_id  NUMBER := fnd_api.g_miss_num;
5359 
5360     l_dun_exists VARCHAR2(1); --Added for Bug 6469732
5361     l_error_text                   VARCHAR2(2000); --Bug 11824312
5362 
5363 BEGIN
5364 
5365     arp_message.set_line( 'ARP_CMERGE_ARCUS.create_same_sites()+' );
5366 
5367     OPEN sites_need_to_create;
5368     LOOP
5369        FETCH sites_need_to_create INTO
5370          l_duplicate_id, l_duplicate_address_id, l_duplicate_site_id,
5371          l_duplicate_site_code, l_customer_id,m_org_id, l_delete_flag;
5372        EXIT WHEN sites_need_to_create%NOTFOUND;
5373 
5374        --Bug 2062466: Modified procedure 'create_same_sites'
5375        --             to reset initial value of local varibles.
5376        l_customer_site_id := null;  -- bug 4018346
5377        l_customer_location := null;
5378        l_profile_option := 'Y';
5379        l_create_profile := 'N';
5380        l_create_party_site_use := 'N';
5381        l_create_acct_site_use := 'N';
5382        l_gen_loc := 'Y';
5383        l_exist := 'N';
5384        --Select merge-from's party site id.
5385        SELECT party_site_id INTO l_duplicate_party_site_id
5386        FROM hz_cust_acct_sites_all --SSUptake
5387        WHERE cust_acct_site_id = l_duplicate_address_id
5388        and   org_id            = m_org_id; --SSUptake
5389 
5390        --Select merge-from's address
5391        SELECT location_id INTO l_location_id
5392        FROM hz_party_sites
5393        WHERE party_site_id = l_duplicate_party_site_id;
5394 
5395        --Select merge-to's party
5396        SELECT party_id INTO l_merge_to_party_id
5397        FROM hz_cust_accounts
5398        WHERE cust_account_id = l_customer_id;
5399 
5400        --Check if user tries to create new address which he/she already has.
5401        --Since account sites has been stripped by org, we do this check in
5402        --current org context by using hz_cust_acct_sites
5403        BEGIN
5404           --Check if merge-to customer already has the account site
5405           --on merge-from's address. If yes, do not need to create
5406           --new party and account site. If not, check if we need to
5407           --create party site.
5408 
5409 
5410           BEGIN
5411 
5412              SELECT 'Y' INTO l_exist
5413              FROM hz_cust_acct_sites_all --SSUptake
5414              WHERE cust_account_id = l_customer_id
5415 	     AND   org_id          = m_org_id --SSUptake
5416              AND party_site_id IN (
5417                  SELECT party_site_id
5418                  FROM hz_party_sites
5419                  WHERE location_id = l_location_id
5420                  AND party_id = l_merge_to_party_id )
5421              AND ROWNUM = 1;
5422 
5423           EXCEPTION
5424              WHEN NO_DATA_FOUND THEN
5425                 l_exist := 'N';
5426           END;
5427 
5428           --No account site exsit. Need to check if there exist party site.
5429           IF l_exist = 'N' THEN
5430 
5431              BEGIN
5432 
5433                 SELECT party_site_id INTO l_party_site_id
5434                 FROM hz_party_sites
5435                 WHERE party_id = l_merge_to_party_id
5436                 AND location_id = l_location_id
5437                 AND ROWNUM = 1;
5438 
5439                 l_exist := 'Y';
5440 
5441              EXCEPTION
5442                 WHEN NO_DATA_FOUND THEN
5443                    l_exist := 'N';
5444              END;
5445 
5446              --Need to create new party site.
5447              IF l_exist = 'N' THEN
5448 
5449                 arp_message.set_line('create_same_sites:create_party_site');
5450 
5451                 --Need to create new party site.
5452                 --Build party site records
5453 
5454                 --select only ID fields. Will call API: HZ_PARTY_PUB.get_current_*
5455                 --to get the records. This is for the consistence purpose in
5456                 --case of data model changes.
5457                ---Bug : 2098728 Changing to V2
5458                 hz_cust_account_merge_v2pvt.get_party_site_rec (
5459                              p_init_msg_list => 'T',
5460                              p_party_site_id => l_duplicate_party_site_id,
5461                              x_party_site_rec => party_site_rec,
5462  			     x_actual_cont_source => l_actual_cont_source,
5463                              x_return_status => x_return_status,
5464                              x_msg_count => x_msg_count,
5465                              x_msg_data => x_msg_data );
5466 
5467                 IF x_msg_count = 1 THEN
5468                    x_msg_data := x_msg_data || '**GET_CURRENT_PARTY_SITE**';
5469                    arp_message.set_line(
5470                        'do_merge_contacts:get_current_party_site ERROR ' ||
5471                        x_msg_data );
5472                 ELSIF x_msg_count > 1 THEN
5473 
5474                    FOR x IN 1..x_msg_count LOOP
5475                     x_msg_data := fnd_msg_pub.get(p_encoded => fnd_api.g_false);
5476                     x_msg_data := x_msg_data || '**GET_CURRENT_PARTY_SITE**';
5477                      arp_message.set_line(
5478                            'do_merge_contacts:get_current_party_site ERROR ' ||
5479                     x_msg_data );
5480                    END LOOP;
5481                 END IF;
5482 
5483                 /* After call create_org_contact API, if return_status is not
5484                  'success', we need to populate an exception. */
5485 
5486                 IF x_return_status <> fnd_api.g_ret_sts_success THEN
5487 		   l_error_text := substr(x_msg_data,1,2000); --Bug 11824312
5488                    RAISE fnd_api.g_exc_error;
5489                 END IF;
5490 
5491                 party_site_rec.party_site_id := FND_API.G_MISS_NUM;
5492                 party_site_rec.party_id := l_merge_to_party_id;
5493                 party_site_rec.party_site_number := FND_API.G_MISS_CHAR;
5494 
5495                 --We should not set primary flag in customer merge context
5496                 party_site_rec.identifying_address_flag := FND_API.G_MISS_CHAR;
5497 
5498                 /* Bug 1365410. Enforce 'create_party_site' API to generate
5499                    party_site_number even if profile option is 'N'.
5500                  */
5501 
5502                 IF fnd_profile.value('HZ_GENERATE_PARTY_SITE_NUMBER') = 'N' THEN
5503                    l_profile_option := 'N';
5504                    fnd_profile.put('HZ_GENERATE_PARTY_SITE_NUMBER', 'Y');
5505                 END IF;
5506 
5507                   ---Bug : 2098728 Changing to V2
5508                 --Create new party site.
5509                    hz_cust_account_merge_v2pvt.create_party_site(
5510                              p_init_msg_list => 'T',
5511                              p_party_site_rec => party_site_rec,
5512 		    	     p_actual_cont_source => l_actual_cont_source,
5513                              x_party_site_id => l_party_site_id,
5514                              x_party_site_number => l_party_site_number,
5515                              x_return_status => x_return_status,
5516                              x_msg_count => x_msg_count,
5517                              x_msg_data => x_msg_data );
5518 
5519                 --Reset profile option
5520                 IF l_profile_option = 'N' THEN
5521                    fnd_profile.put('HZ_GENERATE_PARTY_SITE_NUMBER', 'N');
5522                 END IF;
5523 
5524                 --Handle error message
5525                 IF x_msg_count = 1 THEN
5526                    x_msg_data := x_msg_data || '**CREATE_PARTY_SITE**';
5527                    arp_message.set_line(
5528                        'create_same_sites:create_party_site  ERROR '||
5529                        x_msg_data);
5530                 ELSIF x_msg_count > 1 THEN
5531 
5532                    FOR x IN 1..x_msg_count LOOP
5533                        x_msg_data := fnd_msg_pub.get(p_encoded => fnd_api.g_false);
5534                        x_msg_data := x_msg_data || '**CREATE_PARTY_SITE**';
5535                        arp_message.set_line(
5536                            'create_same_sites:create_party_site  ERROR ' ||
5537                            x_msg_data );
5538                    END LOOP;
5539                 END IF;
5540 
5541                 /** After call create_* API, if return_status is not
5542                   'success', we need to populate an exception. */
5543                 IF x_return_status <> fnd_api.g_ret_sts_success THEN
5544 		   l_error_text := substr(x_msg_data,1,2000); --Bug 11824312
5545                    RAISE fnd_api.g_exc_error;
5546                 END IF;
5547 
5548 -- Bug 2971149. Added following code back that was removed due to bug 1722556.
5549 
5550 -- START
5551 
5552 	     l_create_party_site_use := 'Y';
5553 --Commented out for bug 4492628
5554 /*             ELSE  --Have party site on the location. Do not need to create.
5555 
5556                 --Check if there exists party site use with same purpose
5557                 --In case of there exist multiple party site on the address, we
5558                 --pick up the one with mininum id (and same business purpose, if exists)
5559 
5560                 SELECT MIN(party_site_id) INTO l_party_site_id
5561                 FROM hz_party_sites ps
5562                 WHERE party_id = l_merge_to_party_id
5563                 AND location_id = l_location_id
5564                 AND EXISTS ( --'same site usage'
5565                     SELECT party_site_use_id
5566                     FROM hz_party_site_uses su
5567                     WHERE su.party_site_id = ps.party_site_id
5568                     AND site_use_type = l_duplicate_site_code );
5569 
5570                 --No corresponding party site use. We need to create a new one.
5571                 IF l_party_site_id IS NULL THEN
5572 
5573                    SELECT MIN(party_site_id) INTO l_party_site_id
5574                    FROM hz_party_sites
5575                    WHERE party_id = l_merge_to_party_id
5576                    AND location_id = l_location_id;
5577 
5578                    l_create_party_site_use := 'Y';
5579 
5580                 END IF;
5581 */
5582 -- END
5583 
5584              END IF;  --l_exist Check party site.
5585 
5586 
5587 -- Bug 2971149. Added following code back that was removed due to bug 1722556.
5588 -- check for l_create_party_site_use flag and call hz_cust_account_merge_v2pvt.create_party_site_use.
5589 
5590 -- START of create_party_site_use
5591 /*--Commented out for bug 4492628
5592 	IF l_create_party_site_use = 'Y' THEN
5593 	   BEGIN
5594 
5595         	arp_message.set_line('create_same_sites:create_party_site_use');
5596 
5597 		-- Build party site use record.
5598 
5599 		select party_site_use_id into l_duplicate_party_site_use_id
5600 		from hz_party_site_uses
5601 		where site_use_type = l_duplicate_site_code
5602 		and party_site_id = l_duplicate_party_site_id
5603 		and ROWNUM=1;
5604 
5605 
5606 		 HZ_CUST_ACCOUNT_MERGE_V2PVT.get_party_site_use_rec(
5607 			p_init_msg_list => 'T',
5608 			p_party_site_use_id => l_duplicate_party_site_use_id,
5609 			x_party_site_use_rec => party_site_use_rec,
5610 			x_return_status => x_return_status,
5611 			x_msg_count => x_msg_count,
5612 			x_msg_data => x_msg_data );
5613 
5614 		party_site_use_rec.party_site_id := l_party_site_id;
5618 			p_init_msg_list => 'T',
5615 		party_site_use_rec.primary_per_type := 'N'; --Bug No:3560167
5616 
5617 		hz_cust_account_merge_v2pvt.create_party_site_use(
5619 			p_party_site_use_rec => party_site_use_rec,
5620 			x_party_site_use_id => l_party_site_use_id,
5621 			x_return_status => x_return_status,
5622 			x_msg_count => x_msg_count,
5623 			x_msg_data => x_msg_data );
5624 
5625 		--Handle error message.
5626 
5627 		IF x_msg_count = 1 THEN
5628                    x_msg_data := x_msg_data || '**CREATE_PARTY_SITE_USE**';
5629                    arp_message.set_line( 'create_same_sites:create_party_site_use  ERROR '|| x_msg_data);
5630                 ELSIF x_msg_count > 1 THEN
5631 
5632                    FOR x IN 1..x_msg_count LOOP
5633                        x_msg_data := FND_MSG_PUB.GET(p_encoded => fnd_api.g_false);
5634                        x_msg_data := x_msg_data || '**CREATE_PARTY_SITE_USE**';
5635                        arp_message.set_line( 'create_same_sites:create_party_site_use  ERROR ' || x_msg_data );
5636                    END LOOP;
5637                 END IF;
5638 
5639                 -- After call create_* API, if return_status is not
5640                 -- 'success', we need to populate an exception.
5641                 IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5642                    l_error_text := substr(x_msg_data,1,2000); --Bug 11824312
5643                    RAISE FND_API.G_EXC_ERROR;
5644                 END IF;
5645 
5646              	EXCEPTION
5647 
5648                 --The merge-from account site use might not have corresponding
5649                 --party site use.
5650 	                WHEN NO_DATA_FOUND THEN
5651         	           NULL;
5652 
5653 	   END;
5654         END IF;
5655 -- END of create_party_site_use
5656 */--Commented out for bug 4492628
5657 
5658              arp_message.set_line('create_same_sites:create_account_site');
5659 
5660              --Create account site.
5661              --Build account site records
5662              SELECT  --Bug:2098728 obsoleted wh_update_date,
5663                 attribute_category,
5664                 attribute1,
5665                 attribute2,
5666                 attribute3,
5667                 attribute4,
5668                 attribute5,
5669                 attribute6,
5670                 attribute7,
5671                 attribute8,
5672                 attribute9,
5673                 attribute10,
5674                 attribute11,
5675                 attribute12,
5676                 attribute13,
5677                 attribute14,
5678                 attribute15,
5679                 attribute16,
5680                 attribute17,
5681                 attribute18,
5682                 attribute19,
5683                 attribute20,
5684                 global_attribute_category,
5685                 global_attribute1,
5686                 global_attribute2,
5687                 global_attribute3,
5688                 global_attribute4,
5689                 global_attribute5,
5690                 global_attribute6,
5691                 global_attribute7,
5692                 global_attribute8,
5693                 global_attribute9,
5694                 global_attribute10,
5695                 global_attribute11,
5696                 global_attribute12,
5697                 global_attribute13,
5698                 global_attribute14,
5699                 global_attribute15,
5700                 global_attribute16,
5701                 global_attribute17,
5702                 global_attribute18,
5703                 global_attribute19,
5704                 global_attribute20,
5705 
5706 --Cannot copy, org_system_reference has unique index.
5707 --If no input, orig_system_reference is defaulted to cust_acct_site_id.
5708                 orig_system_reference,
5709 
5710                 status,
5711                 customer_category_code,
5712                 language,
5713                 key_account_flag,
5714 
5715 --Should not copy tp related columns. They are unique columns in hz_cust_acct_sites_all.
5716 --              tp_header_id,
5717 --              ece_tp_location_code,
5718 
5719                  --Bug:2098728 obsoleted service_territory_id,
5720                 primary_specialist_id,
5721                 secondary_specialist_id,
5722                 territory_id,
5723                 territory,
5724                 org_id  ---To pass this org_id to create_cust_acct_site
5725 
5726 --Should not copy. The customer name should be merge-to's.
5727 --              translated_customer_name
5728 
5729              INTO
5730                  --Bug:2098728 obsoleted cust_site_rec.wh_update_date,
5731                 cust_site_rec.attribute_category,
5732                 cust_site_rec.attribute1,
5733                 cust_site_rec.attribute2,
5734                 cust_site_rec.attribute3,
5735                 cust_site_rec.attribute4,
5736                 cust_site_rec.attribute5,
5737                 cust_site_rec.attribute6,
5738                 cust_site_rec.attribute7,
5739                 cust_site_rec.attribute8,
5740                 cust_site_rec.attribute9,
5741                 cust_site_rec.attribute10,
5742                 cust_site_rec.attribute11,
5743                 cust_site_rec.attribute12,
5744                 cust_site_rec.attribute13,
5745                 cust_site_rec.attribute14,
5746                 cust_site_rec.attribute15,
5747                 cust_site_rec.attribute16,
5748                 cust_site_rec.attribute17,
5749                 cust_site_rec.attribute18,
5750                 cust_site_rec.attribute19,
5751                 cust_site_rec.attribute20,
5752                 cust_site_rec.global_attribute_category,
5753                 cust_site_rec.global_attribute1,
5754                 cust_site_rec.global_attribute2,
5758                 cust_site_rec.global_attribute6,
5755                 cust_site_rec.global_attribute3,
5756                 cust_site_rec.global_attribute4,
5757                 cust_site_rec.global_attribute5,
5759                 cust_site_rec.global_attribute7,
5760                 cust_site_rec.global_attribute8,
5761                 cust_site_rec.global_attribute9,
5762                 cust_site_rec.global_attribute10,
5763                 cust_site_rec.global_attribute11,
5764                 cust_site_rec.global_attribute12,
5765                 cust_site_rec.global_attribute13,
5766                 cust_site_rec.global_attribute14,
5767                 cust_site_rec.global_attribute15,
5768                 cust_site_rec.global_attribute16,
5769                 cust_site_rec.global_attribute17,
5770                 cust_site_rec.global_attribute18,
5771                 cust_site_rec.global_attribute19,
5772                 cust_site_rec.global_attribute20,
5773                 cust_site_rec.orig_system_reference,
5774                 cust_site_rec.status,
5775                 cust_site_rec.customer_category_code,
5776                 cust_site_rec.language,
5777                 cust_site_rec.key_account_flag,
5778 --              cust_site_rec.tp_header_id,
5779 --              cust_site_rec.ece_tp_location_code,
5780                  --Bug:2098728 obsoleted cust_site_rec.service_territory_id,
5781                 cust_site_rec.primary_specialist_id,
5782                 cust_site_rec.secondary_specialist_id,
5783                 cust_site_rec.territory_id,
5784                 cust_site_rec.territory,
5785                 l_org_id
5786 --              cust_site_rec.translated_customer_name
5787              FROM hz_cust_acct_sites_all
5788              WHERE  cust_account_id = l_duplicate_id
5789              AND    cust_acct_site_id = l_duplicate_address_id;
5790 
5791              cust_site_rec.party_site_id := l_party_site_id;
5792              cust_site_rec.cust_account_id := l_customer_id;
5793 
5794 --Bug 10119069
5795              IF l_delete_flag = 'Y' THEN
5796                    UPDATE  hz_cust_acct_sites
5797                    SET orig_system_reference = substr(l_duplicate_address_id||sysdate,1,240)
5798                    WHERE cust_account_id = l_duplicate_id
5799                    AND   cust_acct_site_id = l_duplicate_address_id;
5800 
5801              ELSE
5802              cust_site_rec.orig_system_reference := NULL;
5803 
5804              END IF;
5805 
5806              --Create account site
5807              hz_cust_account_merge_v2pvt.create_cust_acct_site(
5808                                  p_init_msg_list => 'T',
5809                                  p_cust_acct_site_rec => cust_site_rec,
5810                                  p_org_id  => l_org_id,
5811                                  x_cust_acct_site_id  => l_customer_address_id,
5812                                  x_return_status => x_return_status,
5813                                  x_msg_count => x_msg_count,
5814                                  x_msg_data => x_msg_data);
5815 
5816              --Handle error message
5817              IF x_msg_count = 1 THEN
5818                 x_msg_data := x_msg_data || '**CREATE_ACCOUNT_SITE**';
5819                 arp_message.set_line(
5820                     'create_same_sites:create_account_site  ERROR '||
5821                     x_msg_data);
5822              ELSIF x_msg_count > 1 THEN
5823 
5824                 FOR x IN 1..x_msg_count LOOP
5825                     x_msg_data := fnd_msg_pub.get(p_encoded => fnd_api.g_false);
5826                     x_msg_data := x_msg_data || '**CREATE_ACCOUNT_SITE**';
5827                     arp_message.set_line(
5828                         'create_same_sites:create_account_site  ERROR ' ||
5829                         x_msg_data );
5830                 END LOOP;
5831              END IF;
5832 
5833              /** After call create_* API, if return_status is not
5834                'success', we need to populate an exception. */
5835              IF x_return_status <> fnd_api.g_ret_sts_success THEN
5836 		l_error_text := substr(x_msg_data,1,2000); --Bug 11824312
5837                 RAISE fnd_api.g_exc_error;
5838              END IF;
5839 
5840              l_create_acct_site_use := 'Y';
5841 
5842           ELSE  --l_exist check for  account site.
5843 
5844           --Check if there exists account site use with same purpose
5845           --In case of there exist multiple account site on the address, we
5846           --pick up the one with minum id and same business purpose, if exists
5847 
5848 
5849              SELECT MIN(cust_acct_site_id) INTO l_customer_address_id
5850              FROM hz_cust_acct_sites_all cas --SSUptake
5851              WHERE cust_account_id = l_customer_id
5852 	     AND   org_id =  m_org_id --SSUptake
5853              AND party_site_id IN (
5854                  SELECT party_site_id
5855                  FROM hz_party_sites
5856                  WHERE location_id = l_location_id
5857                  AND party_id = l_merge_to_party_id )
5858              AND EXISTS ( --'same site usage'
5859                  SELECT site_use_id
5860                  FROM HZ_CUST_SITE_USES_ALL csu --SSUptake
5861                  WHERE cas.cust_acct_site_id = csu.cust_acct_site_id
5862                  AND   site_use_code = l_duplicate_site_code
5863 		 AND   csu.org_id = cas.org_id ); --SSUptake
5864 
5865              --No corresponding account site use. We need to create a new one.
5866 
5867              IF l_customer_address_id IS NULL THEN
5868 
5869                 SELECT MIN(cust_acct_site_id) INTO l_customer_address_id
5870                 FROM hz_cust_acct_sites_all cas --SSUptake
5871                 WHERE cust_account_id = l_customer_id
5872 		and   org_id = m_org_id --SSUptake
5873                 AND party_site_id IN (
5874                     SELECT party_site_id
5875                     FROM hz_party_sites
5876                     WHERE location_id = l_location_id
5877                     AND party_id = l_merge_to_party_id );
5878 
5879                 l_create_acct_site_use := 'Y';
5880 
5881              ELSE --have account site. Do not need to create.
5882 
5883                 --In case of multiple usages exist, select the one
5884                 --with mininum id.
5885                 SELECT MIN(site_use_id) INTO l_customer_site_id
5886                 FROM HZ_CUST_SITE_USES_ALL --SSUptake
5887                 WHERE cust_acct_site_id = l_customer_address_id
5888 		AND   org_id  = m_org_id --SSUptake
5889                 AND site_use_code = l_duplicate_site_code;
5890 
5891              END IF;
5892 
5893           END IF; --l_exist Check for account site
5894 --Bug 4492628
5895 --START
5896           IF l_create_party_site_use = 'N' THEN
5897 		--Check if there exists party site use with same purpose
5898                 --In case of there exist multiple party site on the address, we
5899                 --pick up the one with mininum id (and same business purpose, if exists)
5900 
5901                 SELECT MIN(party_site_id) INTO l_party_site_id
5902                 FROM hz_party_sites ps
5903                 WHERE party_id = l_merge_to_party_id
5904                 AND location_id = l_location_id
5905                 AND EXISTS ( --'same site usage'
5906                     SELECT party_site_use_id
5907                     FROM hz_party_site_uses su
5908                     WHERE su.party_site_id = ps.party_site_id
5909                     AND site_use_type = l_duplicate_site_code );
5910 
5911                 --No corresponding party site use. We need to create a new one.
5912                 IF l_party_site_id IS NULL THEN
5913 
5914                    SELECT MIN(party_site_id) INTO l_party_site_id
5915                    FROM hz_party_sites
5916                    WHERE party_id = l_merge_to_party_id
5917                    AND location_id = l_location_id;
5918 
5919                    l_create_party_site_use := 'Y';
5920 
5921                 END IF;
5922            END IF;
5923 
5924               -- check for l_create_party_site_use flag and call hz_cust_account_merge_v2pvt.create_party_site_use.
5925               -- START of create_party_site_use
5926 
5927 	   IF l_create_party_site_use = 'Y' THEN
5928 	   BEGIN
5929 
5930         	arp_message.set_line('create_same_sites:create_party_site_use');
5931 
5932 		-- Build party site use record.
5933 
5934 		select party_site_use_id into l_duplicate_party_site_use_id
5935 		from hz_party_site_uses
5936 		where site_use_type = l_duplicate_site_code
5937 		and party_site_id = l_duplicate_party_site_id
5938 		and ROWNUM=1;
5939 
5940 
5941 		 HZ_CUST_ACCOUNT_MERGE_V2PVT.get_party_site_use_rec(
5942 			p_init_msg_list => 'T',
5943 			p_party_site_use_id => l_duplicate_party_site_use_id,
5944 			x_party_site_use_rec => party_site_use_rec,
5945 			x_return_status => x_return_status,
5946 			x_msg_count => x_msg_count,
5947 			x_msg_data => x_msg_data );
5948 
5949 		party_site_use_rec.party_site_id := l_party_site_id;
5950 		party_site_use_rec.primary_per_type := 'N'; --Bug No:3560167
5951 
5952 		hz_cust_account_merge_v2pvt.create_party_site_use(
5953 			p_init_msg_list => 'T',
5954 			p_party_site_use_rec => party_site_use_rec,
5955 			x_party_site_use_id => l_party_site_use_id,
5956 			x_return_status => x_return_status,
5957 			x_msg_count => x_msg_count,
5958 			x_msg_data => x_msg_data );
5959 
5960 		--Handle error message.
5961 
5962 		IF x_msg_count = 1 THEN
5963                    x_msg_data := x_msg_data || '**CREATE_PARTY_SITE_USE**';
5964                    arp_message.set_line( 'create_same_sites:create_party_site_use  ERROR '|| x_msg_data);
5965                 ELSIF x_msg_count > 1 THEN
5966 
5967                    FOR x IN 1..x_msg_count LOOP
5968                        x_msg_data := FND_MSG_PUB.GET(p_encoded => fnd_api.g_false);
5969                        x_msg_data := x_msg_data || '**CREATE_PARTY_SITE_USE**';
5970                        arp_message.set_line( 'create_same_sites:create_party_site_use  ERROR ' || x_msg_data );
5971                    END LOOP;
5972                 END IF;
5973 
5974                 -- After call create_* API, if return_status is not
5975                 -- 'success', we need to populate an exception.
5976                 IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5977 		   l_error_text := substr(x_msg_data,1,2000); --Bug 11824312
5978                    RAISE FND_API.G_EXC_ERROR;
5979                 END IF;
5980 
5981              	EXCEPTION
5982 
5983                 --The merge-from account site use might not have corresponding
5984                 --party site use.
5985 	                WHEN NO_DATA_FOUND THEN
5986         	           NULL;
5987 
5988 	   END;
5989         END IF;
5990 	 -- END of create_party_site_use
5991 --END 4492628
5992 
5993           -----Bug:2376975 create only if duplicate_site_id <> -99
5994           IF l_create_acct_site_use = 'Y' AND l_duplicate_site_id <> -99 THEN
5995           BEGIN
5996 
5997              arp_message.set_line('create_same_sites:create_account_site_use');
5998 
5999 
6000              --Create customer profile and customer profile amts.
6004 
6001              --at site levels. profile is mandatory in account level,
6002              --and optional in site use level. Every account/account
6003              --site use can not have more than one profile.
6005              --Build customer profile.
6006              SELECT
6007                 cust_account_profile_id,
6008                 status,
6009                 collector_id,
6010                 credit_analyst_id,
6011                 credit_checking,
6012                 next_credit_review_date,
6013                 tolerance,
6014                 discount_terms,
6015                 dunning_letters,
6016                 interest_charges,
6017                 send_statements,
6018                 credit_balance_statements,
6019                 credit_hold,
6020                 profile_class_id,
6021                 credit_rating,
6022                 risk_code,
6023                 standard_terms,
6024                 override_terms,
6025                 dunning_letter_set_id,
6026                 interest_period_days,
6027                 payment_grace_days,
6028                 discount_grace_days,
6029                 statement_cycle_id,
6030                 account_status,
6031                 percent_collectable,
6032                 autocash_hierarchy_id,
6033                 attribute_category,
6034                 attribute1,
6035                 attribute2,
6036                 attribute3,
6037                 attribute4,
6038                 attribute5,
6039                 attribute6,
6040                 attribute7,
6041                 attribute8,
6042                 attribute9,
6043                 attribute10,
6044                 attribute11,
6045                 attribute12,
6046                 attribute13,
6047                 attribute14,
6048                 attribute15,
6049                  --Bug:2098728 obsoleted wh_update_date,
6050                 auto_rec_incl_disputed_flag,
6051                 tax_printing_option,
6052                 charge_on_finance_charge_flag,
6053                 grouping_rule_id,
6054                 clearing_days,
6055                 jgzz_attribute_category,
6056                 jgzz_attribute1,
6057                 jgzz_attribute2,
6058                 jgzz_attribute3,
6059                 jgzz_attribute4,
6060                 jgzz_attribute5,
6061                 jgzz_attribute6,
6062                 jgzz_attribute7,
6063                 jgzz_attribute8,
6064                 jgzz_attribute9,
6065                 jgzz_attribute10,
6066                 jgzz_attribute11,
6067                 jgzz_attribute12,
6068                 jgzz_attribute13,
6069                 jgzz_attribute14,
6070                 jgzz_attribute15,
6071                 global_attribute1,
6072                 global_attribute2,
6073                 global_attribute3,
6074                 global_attribute4,
6075                 global_attribute5,
6076                 global_attribute6,
6077                 global_attribute7,
6078                 global_attribute8,
6079                 global_attribute9,
6080                 global_attribute10,
6081                 global_attribute11,
6082                 global_attribute12,
6083                 global_attribute13,
6084                 global_attribute14,
6085                 global_attribute15,
6086                 global_attribute16,
6087                 global_attribute17,
6088                 global_attribute18,
6089                 global_attribute19,
6090                 global_attribute20,
6091                 global_attribute_category,
6092                 cons_inv_flag,
6093                 cons_inv_type,
6094                 autocash_hierarchy_id_for_adr,
6095                 lockbox_matching_option,
6096                 review_cycle,
6097                 last_credit_review_date,
6098                 party_id,
6099                 credit_classification,
6100 	--Bug 5040679 - AR new columns
6101 		cons_bill_level,
6102     		late_charge_calculation_trx,
6103     		credit_items_flag,
6104     		disputed_transactions_flag,
6105     		late_charge_type,
6106     		late_charge_term_id,
6107     		interest_calculation_period,
6108     		hold_charged_invoices_flag,
6109     		message_text_id,
6110     		multiple_interest_rates_flag,
6111     		charge_begin_date,
6112 		automatch_set_id       --bug 8477178 . AR new column
6113              INTO
6114                 l_cust_account_profile_id,
6115                 cust_prof_rec.status,
6116                 cust_prof_rec.collector_id,
6117                 cust_prof_rec.credit_analyst_id,
6118                 cust_prof_rec.credit_checking,
6119                 cust_prof_rec.next_credit_review_date,
6120                 cust_prof_rec.tolerance,
6121                 cust_prof_rec.discount_terms,
6122                 cust_prof_rec.dunning_letters,
6123                 cust_prof_rec.interest_charges,
6124                 cust_prof_rec.send_statements,
6125                 cust_prof_rec.credit_balance_statements,
6126                 cust_prof_rec.credit_hold,
6127                 cust_prof_rec.profile_class_id,
6128                 cust_prof_rec.credit_rating,
6129                 cust_prof_rec.risk_code,
6130                 cust_prof_rec.standard_terms,
6131                 cust_prof_rec.override_terms,
6132                 cust_prof_rec.dunning_letter_set_id,
6133                 cust_prof_rec.interest_period_days,
6134                 cust_prof_rec.payment_grace_days,
6135                 cust_prof_rec.discount_grace_days,
6136                 cust_prof_rec.statement_cycle_id,
6137                 cust_prof_rec.account_status,
6138                 cust_prof_rec.percent_collectable,
6139                 cust_prof_rec.autocash_hierarchy_id,
6140                 cust_prof_rec.attribute_category,
6141                 cust_prof_rec.attribute1,
6142                 cust_prof_rec.attribute2,
6143                 cust_prof_rec.attribute3,
6144                 cust_prof_rec.attribute4,
6145                 cust_prof_rec.attribute5,
6146                 cust_prof_rec.attribute6,
6147                 cust_prof_rec.attribute7,
6148                 cust_prof_rec.attribute8,
6149                 cust_prof_rec.attribute9,
6150                 cust_prof_rec.attribute10,
6151                 cust_prof_rec.attribute11,
6152                 cust_prof_rec.attribute12,
6153                 cust_prof_rec.attribute13,
6154                 cust_prof_rec.attribute14,
6155                 cust_prof_rec.attribute15,
6156                  --Bug:2098728 obsoleted cust_prof_rec.wh_update_date,
6157                 cust_prof_rec.auto_rec_incl_disputed_flag,
6158                 cust_prof_rec.tax_printing_option,
6159                 cust_prof_rec.charge_on_finance_charge_flag,
6160                 cust_prof_rec.grouping_rule_id,
6161                 cust_prof_rec.clearing_days,
6162                 cust_prof_rec.jgzz_attribute_category,
6163                 cust_prof_rec.jgzz_attribute1,
6164                 cust_prof_rec.jgzz_attribute2,
6165                 cust_prof_rec.jgzz_attribute3,
6166                 cust_prof_rec.jgzz_attribute4,
6167                 cust_prof_rec.jgzz_attribute5,
6168                 cust_prof_rec.jgzz_attribute6,
6169                 cust_prof_rec.jgzz_attribute7,
6170                 cust_prof_rec.jgzz_attribute8,
6171                 cust_prof_rec.jgzz_attribute9,
6172                 cust_prof_rec.jgzz_attribute10,
6173                 cust_prof_rec.jgzz_attribute11,
6174                 cust_prof_rec.jgzz_attribute12,
6175                 cust_prof_rec.jgzz_attribute13,
6176                 cust_prof_rec.jgzz_attribute14,
6177                 cust_prof_rec.jgzz_attribute15,
6178                 cust_prof_rec.global_attribute1,
6179                 cust_prof_rec.global_attribute2,
6180                 cust_prof_rec.global_attribute3,
6181                 cust_prof_rec.global_attribute4,
6182                 cust_prof_rec.global_attribute5,
6183                 cust_prof_rec.global_attribute6,
6184                 cust_prof_rec.global_attribute7,
6185                 cust_prof_rec.global_attribute8,
6186                 cust_prof_rec.global_attribute9,
6187                 cust_prof_rec.global_attribute10,
6188                 cust_prof_rec.global_attribute11,
6189                 cust_prof_rec.global_attribute12,
6190                 cust_prof_rec.global_attribute13,
6191                 cust_prof_rec.global_attribute14,
6192                 cust_prof_rec.global_attribute15,
6193                 cust_prof_rec.global_attribute16,
6194                 cust_prof_rec.global_attribute17,
6195                 cust_prof_rec.global_attribute18,
6196                 cust_prof_rec.global_attribute19,
6197                 cust_prof_rec.global_attribute20,
6198                 cust_prof_rec.global_attribute_category,
6199                 cust_prof_rec.cons_inv_flag,
6200                 cust_prof_rec.cons_inv_type,
6201                 cust_prof_rec.autocash_hierarchy_id_for_adr,
6202                 cust_prof_rec.lockbox_matching_option,
6203                 cust_prof_rec.review_cycle,
6204                 cust_prof_rec.last_credit_review_date,
6205                 cust_prof_rec.party_id,
6206                 cust_prof_rec.credit_classification,
6207 --Bug 5040679 - AR new columns
6208 		cust_prof_rec.cons_bill_level,
6209     		cust_prof_rec.late_charge_calculation_trx,
6210     		cust_prof_rec.credit_items_flag,
6211     		cust_prof_rec.disputed_transactions_flag,
6212     		cust_prof_rec.late_charge_type,
6213     		cust_prof_rec.late_charge_term_id,
6214     		cust_prof_rec.interest_calculation_period,
6215     		cust_prof_rec.hold_charged_invoices_flag,
6216     		cust_prof_rec.message_text_id,
6217     		cust_prof_rec.multiple_interest_rates_flag,
6218     		cust_prof_rec.charge_begin_date,
6219 	        cust_prof_rec.automatch_set_id      --8477178
6220 
6221              FROM hz_customer_profiles
6222              WHERE cust_account_id = l_duplicate_id
6223              AND   site_use_id     = l_duplicate_site_id
6224              AND   ROWNUM = 1; -- in case of data problem: one site use has 2 profiles.
6225 
6226              -- The API will fill in the site_use_id.
6227              cust_prof_rec.cust_account_id := l_customer_id;
6228 
6229              l_create_profile := 'Y';
6230 
6231           EXCEPTION
6232              --The merge-from account site might not have profile.
6233              WHEN NO_DATA_FOUND THEN
6234                 l_cust_account_profile_id := null;
6235                 l_create_profile := 'N';
6236 
6237           END;
6238 
6239              --Build account site use records.
6240              SELECT site_use_code,
6241 
6242 --We should set primary in customer merge context if the two customer doesnt have a same primary site usage in that org_id
6243               primary_flag,--Bug No.5211233
6244 
6245 --Set the merge-to site uses status to 'Active'
6246 --
6247 --Bug 2071810: keep the old status
6248                 status,
6249 
6250 --Bug fix 2588321 Changed the logic for copying value into location field
6251 --depending upon certain conditions.
6252 --location is unique per customer+business purpose combination. We should
6253 --not copy it in customer merge context
6254 --              location,
6255 
6256                  contact_id,
6257 
6258 --We should not set bill_to_site_use_id in customer merge context
6259                 bill_to_site_use_id,
6260                 orig_system_reference,
6261                 sic_code,
6262                 payment_term_id,
6263                 gsa_indicator,
6264                 ship_partial,
6265                 ship_via,
6266                 fob_point,
6267                 order_type_id,
6268                 price_list_id,
6269                 freight_term,
6270                 warehouse_id,
6271                 territory_id,
6272                 attribute_category,
6273                 attribute1,
6274                 attribute2,
6275                 attribute3,
6276                 attribute4,
6277                 attribute5,
6278                 attribute6,
6279                 attribute7,
6280                 attribute8,
6281                 attribute9,
6282                 attribute10,
6283                 attribute11,
6284                 attribute12,
6285                 attribute13,
6286                 attribute14,
6287                 attribute15,
6288                 attribute16,
6289                 attribute17,
6290                 attribute18,
6291                 attribute19,
6292                 attribute20,
6293                 attribute21,
6294                 attribute22,
6295                 attribute23,
6296                 attribute24,
6297                 attribute25,
6298                 global_attribute_category,
6299                 global_attribute1,
6300                 global_attribute2,
6301                 global_attribute3,
6302                 global_attribute4,
6303                 global_attribute5,
6304                 global_attribute6,
6305                 global_attribute7,
6306                 global_attribute8,
6307                 global_attribute9,
6308                 global_attribute10,
6309                 global_attribute11,
6310                 global_attribute12,
6311                 global_attribute13,
6312                 global_attribute14,
6313                 global_attribute15,
6314                 global_attribute16,
6315                 global_attribute17,
6316                 global_attribute18,
6317                 global_attribute19,
6318                 global_attribute20,
6319                 tax_reference,
6320                 sort_priority,
6321                 tax_code,
6322                 --Bug:2098728 obsoleted last_accrue_charge_date,
6323                 --Bug:2098728 obsoleted second_last_accrue_charge_date,
6324                 --Bug:2098728 obsoleted last_unaccrue_charge_date,
6325                 --Bug:2098728 obsoleted second_last_unaccrue_chrg_date,
6326                 demand_class_code,
6327                 tax_header_level_flag,
6328                 tax_rounding_rule,
6329                  --Bug:2098728 obsoleted wh_update_date,
6330                 primary_salesrep_id,
6331                 finchrg_receivables_trx_id,
6332                 dates_negative_tolerance,
6333                 dates_positive_tolerance,
6334                 date_type_preference,
6335                 over_shipment_tolerance,
6336                 under_shipment_tolerance,
6337                 item_cross_ref_pref,
6338                 ship_sets_include_lines_flag,
6339                 arrivalsets_include_lines_flag,
6340                 sched_date_push_flag,
6341                 invoice_quantity_rule,
6342                 over_return_tolerance,
6343                 under_return_tolerance,
6344                 pricing_event,
6345 
6346 --Bug 1512300: Modify create_same_sites to copy GL accounts.
6347 
6348                 gl_id_rec,
6349                 gl_id_rev,
6350                 gl_id_tax,
6351                 gl_id_freight,
6352                 gl_id_clearing,
6353                 gl_id_unbilled,
6354                 gl_id_unearned,
6355                 gl_id_unpaid_rec,
6356                 gl_id_remittance,
6357                 gl_id_factor,
6358                 tax_classification,
6359                 org_id     --To pass org_id while creating cust_site_use
6360              INTO
6361                 cust_site_use_rec.site_use_code,
6362                 cust_site_use_rec.primary_flag,--Bug No. 5211233
6363                 cust_site_use_rec.status,
6364 --              cust_site_use_rec.location,
6365                  --Bug:2098728 obsoleted cust_site_use_rec.contact_id,
6366                  --Bug:2403263 Added cust_site_use_rec.contact_id.
6367                 cust_site_use_rec.contact_id,
6368 --              cust_site_use_rec.bill_to_site_use_id,
6369                 cust_site_use_rec.bill_to_site_use_id,
6370                 cust_site_use_rec.orig_system_reference,
6371                 cust_site_use_rec.sic_code,
6372                 cust_site_use_rec.payment_term_id,
6373                 cust_site_use_rec.gsa_indicator,
6374                 cust_site_use_rec.ship_partial,
6375                 cust_site_use_rec.ship_via,
6376                 cust_site_use_rec.fob_point,
6377                 cust_site_use_rec.order_type_id,
6381                 cust_site_use_rec.territory_id,
6378                 cust_site_use_rec.price_list_id,
6379                 cust_site_use_rec.freight_term,
6380                 cust_site_use_rec.warehouse_id,
6382                 cust_site_use_rec.attribute_category,
6383                 cust_site_use_rec.attribute1,
6384                 cust_site_use_rec.attribute2,
6385                 cust_site_use_rec.attribute3,
6386                 cust_site_use_rec.attribute4,
6387                 cust_site_use_rec.attribute5,
6388                 cust_site_use_rec.attribute6,
6389                 cust_site_use_rec.attribute7,
6390                 cust_site_use_rec.attribute8,
6391                 cust_site_use_rec.attribute9,
6392                 cust_site_use_rec.attribute10,
6393                 cust_site_use_rec.attribute11,
6394                 cust_site_use_rec.attribute12,
6395                 cust_site_use_rec.attribute13,
6396                 cust_site_use_rec.attribute14,
6397                 cust_site_use_rec.attribute15,
6398                 cust_site_use_rec.attribute16,
6399                 cust_site_use_rec.attribute17,
6400                 cust_site_use_rec.attribute18,
6401                 cust_site_use_rec.attribute19,
6402                 cust_site_use_rec.attribute20,
6403                 cust_site_use_rec.attribute21,
6404                 cust_site_use_rec.attribute22,
6405                 cust_site_use_rec.attribute23,
6406                 cust_site_use_rec.attribute24,
6407                 cust_site_use_rec.attribute25,
6408                 cust_site_use_rec.global_attribute_category,
6409                 cust_site_use_rec.global_attribute1,
6410                 cust_site_use_rec.global_attribute2,
6411                 cust_site_use_rec.global_attribute3,
6412                 cust_site_use_rec.global_attribute4,
6413                 cust_site_use_rec.global_attribute5,
6414                 cust_site_use_rec.global_attribute6,
6415                 cust_site_use_rec.global_attribute7,
6416                 cust_site_use_rec.global_attribute8,
6417                 cust_site_use_rec.global_attribute9,
6418                 cust_site_use_rec.global_attribute10,
6419                 cust_site_use_rec.global_attribute11,
6420                 cust_site_use_rec.global_attribute12,
6421                 cust_site_use_rec.global_attribute13,
6422                 cust_site_use_rec.global_attribute14,
6423                 cust_site_use_rec.global_attribute15,
6424                 cust_site_use_rec.global_attribute16,
6425                 cust_site_use_rec.global_attribute17,
6426                 cust_site_use_rec.global_attribute18,
6427                 cust_site_use_rec.global_attribute19,
6428                 cust_site_use_rec.global_attribute20,
6429                 cust_site_use_rec.tax_reference,
6430                 cust_site_use_rec.sort_priority,
6431                 cust_site_use_rec.tax_code,
6432               --Bug:2098728 obsoleted cust_site_use_rec.last_accrue_charge_date,
6433               --Bug:2098728 obsoleted cust_site_use_rec.second_last_accrue_charge_date,
6434               --Bug:2098728 obsoleted cust_site_use_rec.last_unaccrue_charge_date,
6435               --Bug:2098728 obsoleted cust_site_use_rec.second_last_unaccrue_chrg_date,
6436                 cust_site_use_rec.demand_class_code,
6437                 cust_site_use_rec.tax_header_level_flag,
6438                 cust_site_use_rec.tax_rounding_rule,
6439                  --Bug:2098728 obsoleted cust_site_use_rec.wh_update_date,
6440                 cust_site_use_rec.primary_salesrep_id,
6441                 cust_site_use_rec.finchrg_receivables_trx_id,
6442                 cust_site_use_rec.dates_negative_tolerance,
6443                 cust_site_use_rec.dates_positive_tolerance,
6444                 cust_site_use_rec.date_type_preference,
6445                 cust_site_use_rec.over_shipment_tolerance,
6446                 cust_site_use_rec.under_shipment_tolerance,
6447                 cust_site_use_rec.item_cross_ref_pref,
6448                 cust_site_use_rec.ship_sets_include_lines_flag,
6449                 cust_site_use_rec.arrivalsets_include_lines_flag,
6450                 cust_site_use_rec.sched_date_push_flag,
6451                 cust_site_use_rec.invoice_quantity_rule,
6452                 cust_site_use_rec.over_return_tolerance,
6453                 cust_site_use_rec.under_return_tolerance,
6454                 cust_site_use_rec.pricing_event,
6455 
6456 --Bug 1512300: Modify create_same_sites to copy GL accounts.
6457 
6458                 cust_site_use_rec.gl_id_rec,
6459                 cust_site_use_rec.gl_id_rev,
6460                 cust_site_use_rec.gl_id_tax,
6461                 cust_site_use_rec.gl_id_freight,
6462                 cust_site_use_rec.gl_id_clearing,
6463                 cust_site_use_rec.gl_id_unbilled,
6464                 cust_site_use_rec.gl_id_unearned,
6465                 cust_site_use_rec.gl_id_unpaid_rec,
6466                 cust_site_use_rec.gl_id_remittance,
6467                 cust_site_use_rec.gl_id_factor,
6468                 cust_site_use_rec.tax_classification,
6469                 site_use_org_id
6470              FROM  HZ_CUST_SITE_USES_ALL --SSUptake
6471              WHERE site_use_id = l_duplicate_site_id
6472 	     AND   org_id = m_org_id; --SSUptake
6473 
6474 	   ----Bug 5211233
6475 	   IF cust_site_use_rec.primary_flag = 'Y' THEN
6476 	   BEGIN
6477 	                Select NULL INTO cust_site_use_rec.primary_flag
6478 	                from hz_cust_site_uses_all
6479 	                where CUST_ACCT_SITE_ID in (select CUST_ACCT_SITE_ID from hz_cust_acct_sites_all
6480 	                                             Where cust_account_id = l_customer_id
6481 	                                             AND org_id =  site_use_org_id)
6482 	                AND SITE_USE_CODE = cust_site_use_rec.site_use_code
6483 	                AND PRIMARY_FLAG = 'Y'
6487 
6484 	                AND nvl(status,'A') = 'A'
6485 	                AND org_id = site_use_org_id;
6486 	                EXCEPTION
6488 	                WHEN NO_DATA_FOUND THEN
6489 	   	                         cust_site_use_rec.primary_flag := 'Y';
6490              END;
6491              END IF;
6492              ---------Bug 5211233
6493 
6494             -- Bug Fix : 2272750
6495             if cust_site_use_rec.bill_to_site_use_id is not null then
6496                -- Check whether the bill_to_site_use_id is valid.
6497                BEGIN
6498                   select 1
6499                   into   l_count
6500                   from   HZ_CUST_SITE_USES_ALL
6501                   where  site_use_id   = cust_site_use_rec.bill_to_site_use_id
6502                   and    site_use_code = 'BILL_TO';
6503                EXCEPTION
6504                   WHEN NO_DATA_FOUND THEN
6505                     cust_site_use_rec.bill_to_site_use_id := NULL;
6506                END;
6507            end if;
6508 
6509 --Bug Fix 2588321
6510              --since location is madatory. We have to enforce system generate.
6511              --Only if the customer_location value is NULL.
6512 
6513              SELECT customer_location into l_customer_location
6514              FROM ra_customer_merges
6515              WHERE customer_id = l_customer_id
6516              AND duplicate_site_id = l_duplicate_site_id
6517 --Bug Fix 2929527
6518              AND ROWNUM=1;
6519 
6520        IF l_customer_location = NULL THEN
6521            null;
6522            /* --SSUptake
6523 
6524             BEGIN
6525                SELECT auto_site_numbering INTO l_gen_loc
6526                FROM ar_system_parameters;
6527                l_gen_loc := hz_mo_global_cache.get_auto_site_numbering(cust_site_use_rec.org_id);
6528 --Bug Fix 2183072
6529             EXCEPTION
6530                 WHEN NO_DATA_FOUND THEN
6531                      --arp_message.set_name( 'AR','AR_NO_ROW_IN_SYSTEM_PARAMETERS');
6532                      RAISE fnd_api.g_exc_error;
6533             END ;
6534 
6535             IF l_gen_loc = 'N' THEN
6536                 UPDATE ar_system_parameters
6537                 SET auto_site_numbering = 'Y';
6538             END IF;
6539          */
6540 
6541 --Bug Fix 2588321
6542       ELSE
6543               cust_site_use_rec.location := l_customer_location;
6544       END IF;
6545 
6546              cust_site_use_rec.cust_acct_site_id := l_customer_address_id;
6547 
6548              IF l_create_profile = 'Y' THEN
6549                 l_converted_create_profile := FND_API.G_TRUE;
6550              ELSE
6551                 l_converted_create_profile := FND_API.G_FALSE;
6552              END IF;
6553 
6554              cust_prof_rec.party_id := l_merge_to_party_id;
6555 -- bug 6469732 Start
6556 l_dun_exists:='N';
6557 IF (l_duplicate_site_code = 'DUN' or l_duplicate_site_code = 'STMTS') and cust_site_use_rec.status = 'A' THEN
6558   BEGIN
6559 	SELECT 'Y' INTO l_dun_exists
6560 	FROM hz_cust_acct_sites_all as1, hz_cust_site_uses_all asu
6561 	WHERE as1.cust_account_id = l_customer_id
6562 	AND   asu.cust_acct_site_id = as1.cust_acct_site_id
6563         AND   as1.org_id = m_org_id
6564 	AND   asu.site_use_code = l_duplicate_site_code
6565         AND   asu.org_id = m_org_id
6566 	AND   asu.status = 'A'
6567         AND ROWNUM = 1 ;
6568   EXCEPTION
6569   	WHEN NO_DATA_FOUND THEN
6570      	l_dun_exists := 'N';
6571   END;
6572 END IF;
6573 
6574 
6575     IF l_dun_exists = 'Y' THEN
6576 
6577         cust_site_use_rec.status := 'I';
6578 
6579     END IF;
6580 --bug 6469732 end
6581 
6582              --Create account site use.
6583              --Create account site use.
6584              hz_cust_account_merge_v2pvt.create_cust_site_use(
6585                               p_init_msg_list => 'T',
6586                               p_cust_site_use_rec => cust_site_use_rec,
6587                               p_customer_profile_rec => cust_prof_rec,
6588                               p_create_profile => l_converted_create_profile,
6589                               p_create_profile_amt => 'F', ----no profile amt
6590                               p_org_id => site_use_org_id,
6591                               x_site_use_id => l_customer_site_id ,
6592                               x_return_status => x_return_status,
6593                               x_msg_count => x_msg_count,
6594                               x_msg_data => x_msg_data);
6595 
6596              --reset system parameter
6597 	     --SSUptake
6598              /*IF l_gen_loc = 'N' THEN
6599                 UPDATE ar_system_parameters
6600                 SET auto_site_numbering = 'N';
6601              END IF;*/
6602 
6603              --Handle error message.
6604              IF x_msg_count = 1 THEN
6605                 x_msg_data := x_msg_data || '**CREATE_ACCOUNT_SITE_USE**';
6606                 arp_message.set_line(
6607                     'create_same_sites:create_acct_site_uses  ERROR '||
6608                     x_msg_data);
6609              ELSIF x_msg_count > 1 THEN
6610 
6611                 FOR x IN 1..x_msg_count LOOP
6612                     x_msg_data := fnd_msg_pub.get(p_encoded => fnd_api.g_false);
6613                     x_msg_data := x_msg_data || '**CREATE_ACCOUNT_SITE_USE**';
6614                     arp_message.set_line(
6615                         'create_same_sites:create_acct_site_uses  ERROR ' ||
6616                         x_msg_data );
6617                 END LOOP;
6618              END IF;
6619 
6620              /** After call create_* API, if return_status is not
6621                'success', we need to populate an exception. */
6622              IF x_return_status <> fnd_api.g_ret_sts_success THEN
6626 
6623 		l_error_text := substr(x_msg_data,1,2000); --Bug 11824312
6624                 RAISE fnd_api.g_exc_error;
6625              END IF;
6627              -- Find out the customer profile id.
6628              IF l_create_profile = 'Y' THEN
6629 
6630                 arp_message.set_line('create_same_sites:create_profile_amount');
6631 
6632                 SELECT cust_account_profile_id INTO l_to_cust_account_profile_id
6633                 FROM   hz_customer_profiles
6634                 WHERE  cust_account_id = l_customer_id
6635                 AND    site_use_id     = l_customer_site_id;
6636 
6637                 -- Create Customer Profile Amts for customer id
6638                 OPEN merge_from_prof_amt;
6639                 LOOP
6640                   FETCH merge_from_prof_amt INTO
6641                      cust_prof_amt.currency_code,
6642                      cust_prof_amt.trx_credit_limit,
6643                      cust_prof_amt.overall_credit_limit,
6644                      cust_prof_amt.min_dunning_amount,
6645                      cust_prof_amt.min_dunning_invoice_amount,
6646                      cust_prof_amt.max_interest_charge,
6647                      cust_prof_amt.min_statement_amount,
6648                      cust_prof_amt.auto_rec_min_receipt_amount,
6649                      cust_prof_amt.interest_rate,
6650                      cust_prof_amt.attribute_category,
6651                      cust_prof_amt.attribute1,
6652                      cust_prof_amt.attribute2,
6653                      cust_prof_amt.attribute3,
6654                      cust_prof_amt.attribute4,
6655                      cust_prof_amt.attribute5,
6656                      cust_prof_amt.attribute6,
6657                      cust_prof_amt.attribute7,
6658                      cust_prof_amt.attribute8,
6659                      cust_prof_amt.attribute9,
6660                      cust_prof_amt.attribute10,
6661                      cust_prof_amt.attribute11,
6662                      cust_prof_amt.attribute12,
6663                      cust_prof_amt.attribute13,
6664                      cust_prof_amt.attribute14,
6665                      cust_prof_amt.attribute15,
6666                      cust_prof_amt.min_fc_balance_amount,
6667                      cust_prof_amt.min_fc_invoice_amount,
6668                      cust_prof_amt.expiration_date,
6669                       --Bug:2098728 obsoleted cust_prof_amt.wh_update_date,
6670                      cust_prof_amt.jgzz_attribute_category,
6671                      cust_prof_amt.jgzz_attribute1,
6672                      cust_prof_amt.jgzz_attribute2,
6673                      cust_prof_amt.jgzz_attribute3,
6674                      cust_prof_amt.jgzz_attribute4,
6675                      cust_prof_amt.jgzz_attribute5,
6676                      cust_prof_amt.jgzz_attribute6,
6677                      cust_prof_amt.jgzz_attribute7,
6678                      cust_prof_amt.jgzz_attribute8,
6679                      cust_prof_amt.jgzz_attribute9,
6680                      cust_prof_amt.jgzz_attribute10,
6681                      cust_prof_amt.jgzz_attribute11,
6682                      cust_prof_amt.jgzz_attribute12,
6683                      cust_prof_amt.jgzz_attribute13,
6684                      cust_prof_amt.jgzz_attribute14,
6685                      cust_prof_amt.jgzz_attribute15,
6686                      cust_prof_amt.global_attribute1,
6687                      cust_prof_amt.global_attribute2,
6688                      cust_prof_amt.global_attribute3,
6689                      cust_prof_amt.global_attribute4,
6690                      cust_prof_amt.global_attribute5,
6691                      cust_prof_amt.global_attribute6,
6692                      cust_prof_amt.global_attribute7,
6693                      cust_prof_amt.global_attribute8,
6694                      cust_prof_amt.global_attribute9,
6695                      cust_prof_amt.global_attribute10,
6696                      cust_prof_amt.global_attribute11,
6697                      cust_prof_amt.global_attribute12,
6698                      cust_prof_amt.global_attribute13,
6699                      cust_prof_amt.global_attribute14,
6700                      cust_prof_amt.global_attribute15,
6701                      cust_prof_amt.global_attribute16,
6702                      cust_prof_amt.global_attribute17,
6703                      cust_prof_amt.global_attribute18,
6704                      cust_prof_amt.global_attribute19,
6705                      cust_prof_amt.global_attribute20,
6706                      cust_prof_amt.global_attribute_category,
6707 	--Bug 5040679 - AR new columns
6708 		cust_prof_amt.exchange_rate_type,
6709     		cust_prof_amt.min_fc_invoice_overdue_type,
6710     		cust_prof_amt.min_fc_invoice_percent,
6711     		cust_prof_amt.min_fc_balance_overdue_type,
6712     		cust_prof_amt.min_fc_balance_percent,
6713     		cust_prof_amt.interest_type,
6714     		cust_prof_amt.interest_fixed_amount,
6715     		cust_prof_amt.interest_schedule_id,
6716     		cust_prof_amt.penalty_type,
6717     		cust_prof_amt.penalty_rate,
6718     		cust_prof_amt.min_interest_charge,
6719     		cust_prof_amt.penalty_fixed_amount,
6720     		cust_prof_amt.penalty_schedule_id;
6721                    EXIT WHEN merge_from_prof_amt%NOTFOUND;
6722 
6723                    cust_prof_amt.cust_account_profile_id  := l_to_cust_account_profile_id;
6724                    cust_prof_amt.cust_account_id          := l_customer_id;
6725                    cust_prof_amt.site_use_id              := l_customer_site_id;
6726 
6727                    --create custom profile amounts
6728                    ---Bug:2098728 Calling V2
6729 
6730                       hz_cust_account_merge_v2pvt.create_cust_profile_amt(
6731                              p_init_msg_list => 'T',
6732                              p_check_foreign_key =>'T',
6736                              x_msg_count => x_msg_count,
6733                              p_cust_profile_amt_rec => cust_prof_amt,
6734                              x_cust_acct_profile_amt_id => l_cust_acct_profile_amt_id,
6735                              x_return_status => x_return_status,
6737                              x_msg_data => x_msg_data);
6738 
6739                    IF x_msg_count = 1 THEN
6740                       x_msg_data := x_msg_data || '**CREATE_CUST_PROF_AMT**';
6741                       arp_message.set_line(
6742                           'create_same_sites:create_cust_prof_amt  ERROR '||
6743                           x_msg_data);
6744                    ELSIF x_msg_count > 1 THEN
6745 
6746                       FOR x IN 1..x_msg_count LOOP
6747                           x_msg_data := fnd_msg_pub.get(p_encoded => fnd_api.g_false);
6748                           x_msg_data := x_msg_data || '**CREATE_CUST_PROF_AMT**';
6749                           arp_message.set_line(
6750                               'create_same_sites:create_cust_prof_amt  ERROR ' ||
6751                               x_msg_data );
6752                       END LOOP;
6753                    END IF;
6754 
6755                   /** After call create_* API, if return_status is not
6756                    'success', we need to populate an exception. */
6757                   IF x_return_status <> fnd_api.g_ret_sts_success THEN
6758 		     l_error_text := substr(x_msg_data,1,2000); --Bug 11824312
6759                      RAISE fnd_api.g_exc_error;
6760                   END IF;
6761 
6762                 END LOOP;
6763                 CLOSE merge_from_prof_amt;
6764 
6765              END IF;   --there is profile exist for this site use.
6766 
6767 /* Bug:2241033; Creating Payment Method for customer site use   */
6768       begin
6769     	arp_message.set_line('create_same_sites:create_cust_site_payment_method');
6770     	savepoint merge_from_pay_method_point;
6771 
6772     	open merge_from_pay_method;
6773     	loop
6774       	fetch merge_from_pay_method into merge_from_pay_method_row;
6775 	   EXIT WHEN merge_from_pay_method%NOTFOUND;
6776 	   l_row_id := null;
6777 	   l_Cust_Receipt_Method_Id := null;
6778 
6779        	   arp_CRM_PKG.Insert_Row(X_Rowid        => l_row_id ,
6780               X_Cust_Receipt_Method_Id  => l_Cust_Receipt_Method_Id,
6781                X_Created_By         => hz_utility_v2pub.user_id,--arp_standard.profile.user_id ,
6782                X_Creation_Date      => sysdate,
6783                X_Customer_Id        => l_customer_id,
6784                X_Last_Updated_By    =>hz_utility_v2pub.user_id,-- arp_standard.profile.user_id,
6785                X_Last_Update_Date   => sysdate,
6786                X_Primary_Flag       => merge_from_pay_method_row.Primary_Flag,
6787                X_Receipt_Method_Id  => merge_from_pay_method_row.Receipt_Method_Id,
6788                X_Start_Date         => merge_from_pay_method_row.Start_Date,
6789                X_End_Date           => merge_from_pay_method_row.End_Date,
6790                X_Last_Update_Login  =>hz_utility_v2pub.last_update_login,-- arp_standard.profile.last_update_login,
6791                X_Site_Use_Id        => l_customer_site_id,
6792                X_Attribute_Category => merge_from_pay_method_row.Attribute_Category,
6793                X_Attribute1         => merge_from_pay_method_row.Attribute1,
6794                X_Attribute2         => merge_from_pay_method_row.Attribute2,
6795                X_Attribute3         => merge_from_pay_method_row.Attribute3,
6796                X_Attribute4         => merge_from_pay_method_row.Attribute4,
6797                X_Attribute5         => merge_from_pay_method_row.Attribute5,
6798                X_Attribute6         => merge_from_pay_method_row.Attribute6,
6799                X_Attribute7         => merge_from_pay_method_row.Attribute7,
6800                X_Attribute8         => merge_from_pay_method_row.Attribute8,
6801                X_Attribute9         => merge_from_pay_method_row.Attribute9,
6802                X_Attribute10        => merge_from_pay_method_row.Attribute10,
6803                X_Attribute11        => merge_from_pay_method_row.Attribute11,
6804                X_Attribute12        => merge_from_pay_method_row.Attribute12,
6805                X_Attribute13        => merge_from_pay_method_row.Attribute13,
6806                X_Attribute14        => merge_from_pay_method_row.Attribute14,
6807                X_Attribute15        => merge_from_pay_method_row.Attribute15
6808                        );
6809 		--The above table handler does not insert all the columns of
6810                 --the table.So the following update statement is created to
6811                 --update rest of the fields.
6812 
6813 	      if l_row_id is not null then
6814                	update 	RA_CUST_RECEIPT_METHODS
6815 	       	set
6816                 last_update_date = sysdate,
6817     	        last_updated_by =hz_utility_v2pub.user_id,--arp_standard.profile.user_id,
6818                 last_update_login =hz_utility_v2pub.last_update_login,-- arp_standard.profile.last_update_login,
6819 	        request_id = req_id,
6820                 program_application_id =hz_utility_v2pub.program_application_id,-- arp_standard.profile.program_application_id,
6821 	       	program_id =hz_utility_v2pub.program_id,-- arp_standard.profile.program_id,
6822 	   	program_update_date = sysdate
6823 	        where   rowid = l_row_id;
6824               end if;
6825 
6826 	 end loop;
6827 	 CLOSE merge_from_pay_method;
6828    exception
6829      when others then
6830 	rollback to  merge_from_pay_method_point;
6831 
6832         arp_message.set_line('create_same_sites:create_cust_site_payment_method error');
6833 	arp_message.set_line('Error : '||SQLERRM);
6834         status := -1;
6835 	l_error_text := substr(SQLERRM,1,2000); --Bug 11824312
6836         RAISE fnd_api.g_exc_error;
6837     end;
6838 /* --end of creating payment method for customer site use */
6839 
6840           END IF; --end of create account site
6841 
6842        END; --end of creating
6843 
6844        --Will migrate the contacts (phones and emails) during merge process
6845 
6846        --Update ra_customer_merges table with the new info. we created.
6847        --Select customer orig system reference.
6848        SELECT orig_system_reference
6849        INTO l_customer_ref
6850        FROM hz_cust_acct_sites_all
6851        WHERE cust_acct_site_id = l_customer_address_id;
6852 
6853       ---Bug: 2376975 Acct merge should happen even if there is no site use
6854       ---l_customer_site_id is null when site_use is not created
6855       ---and that happens when duplicate_site_code is null/NONE
6856 
6857       if l_customer_site_id is not null then
6858        --Select primary flag and location
6859        SELECT primary_flag, location
6860        INTO l_customer_primary_flag, l_customer_location
6861        FROM HZ_CUST_SITE_USES_ALL
6862        WHERE site_use_id = l_customer_site_id;
6863       end if;
6864 --bug 3959776
6865        SELECT party_site_number INTO l_party_site_number
6866               FROM hz_party_sites
6867               WHERE party_site_id = ( SELECT party_site_id
6868                                       FROM hz_cust_acct_sites_all
6869                                       WHERE cust_acct_site_id = l_customer_address_id);
6870        UPDATE ra_customer_merges
6874            customer_site_id = nvl(l_customer_site_id,-99),
6871        SET customer_address_id = l_customer_address_id,
6872            customer_ref = l_customer_ref,
6873            customer_primary_flag = nvl(l_customer_primary_flag,'N'),
6875            customer_location = l_customer_location,    --bug 4018346 removed nvl condition
6876 	   customer_site_number = nvl(l_party_site_number,-99),              ---bug 3959776 updated customer_site_number
6877            last_update_date = sysdate,
6878            last_updated_by = hz_utility_v2pub.user_id,--arp_standard.profile.user_id,
6879            last_update_login =hz_utility_v2pub.last_update_login,-- arp_standard.profile.last_update_login,
6880            program_application_id =hz_utility_v2pub.program_application_id,-- arp_standard.profile.program_application_id,
6881            program_id =hz_utility_v2pub.program_id,-- arp_standard.profile.program_id,
6882            program_update_date = sysdate
6883        WHERE duplicate_id = l_duplicate_id
6884        AND duplicate_site_id = l_duplicate_site_id
6885        AND duplicate_address_id = l_duplicate_address_id -- bug 7851438
6886        AND customer_id = l_customer_id
6887        AND process_flag = 'N'
6888        AND request_id = req_id
6889        AND set_number = set_num
6890        AND customer_createsame = 'Y';
6891 
6892     END LOOP;
6893     CLOSE sites_need_to_create;
6894 
6895     status := 0;
6896 
6897     arp_message.set_line( 'ARP_CMERGE_ARCUS.create_same_sites()-' );
6898 
6899 EXCEPTION
6900 
6901     WHEN OTHERS THEN
6902       arp_message.set_error( 'ARP_CMERGE_ARCUS.create_same_sites' );
6903       IF l_error_text IS NULL THEN
6904          error_text := substr(SQLERRM,1,2000);
6905       ELSE
6906          error_text := l_error_text;
6907       END IF;
6908       status := -1;
6909 
6910 END create_same_sites;
6911 
6912 
6913 /*===========================================================================+
6914  | PROCEDURE
6915  |              merge_history
6916  |
6917  | DESCRIPTION
6918  |          For recording the data in the tables modified in TCA registry
6919  |
6920  | SCOPE - PUBLIC
6921  |
6922  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED
6923  |
6924  | ARGUMENTS  : IN:
6925  |                    req_id , set_num
6926  |              OUT:  status
6927  |          IN/ OUT:
6928  |
6929  | RETURNS    : NONE
6930  |
6931  | NOTES
6932  |
6933  | MODIFICATION HISTORY -
6934  |                       Jyoti Pandey 05-20-2002 Created.
6935  |
6936  +===========================================================================*/
6937 
6938 PROCEDURE merge_history(req_id  NUMBER,
6939                         set_num NUMBER,
6940                         status  OUT NOCOPY NUMBER) IS
6941 
6942 l_customer_merge_header_id RA_CUSTOMER_MERGES.customer_merge_header_id%TYPE;
6943 
6944 
6945 BEGIN
6946 
6947    arp_message.set_line( 'ARP_CMERGE_ARCUS.Merge_History()+' );
6948 
6949   ---------Insert into HZ_CUST_ACCOUNTS_M--------------
6950   INSERT INTO HZ_CUST_ACCOUNTS_M(
6951   customer_merge_header_id,
6952   cust_account_id,
6953   party_id      ,
6954   last_update_date ,
6955   account_number   ,
6956   last_updated_by  ,
6957   creation_date   ,
6958   created_by     ,
6959   last_update_login,
6960   request_id       ,
6961   program_application_id,
6962   program_id            ,
6963   program_update_date   ,
6964   attribute_category    ,
6965   attribute1            ,
6966   attribute2            ,
6967   attribute3            ,
6968   attribute4            ,
6969   attribute5            ,
6970   attribute6            ,
6971   attribute7,
6972   attribute8,
6973   attribute9 ,
6974   attribute10,
6975   attribute11,
6976   attribute12,
6977   attribute13,
6978   attribute14,
6979   attribute15,
6980   attribute16,
6981   attribute17,
6982   attribute18,
6983   attribute19,
6984   attribute20,
6985   global_attribute_category,
6986   global_attribute1,
6987   global_attribute2,
6988   global_attribute3,
6989   global_attribute4,
6990   global_attribute5,
6991   global_attribute6,
6992   global_attribute7,
6993   global_attribute8,
6994   global_attribute9,
6995   global_attribute10,
6996   global_attribute11,
6997   global_attribute12,
6998   global_attribute13,
6999   global_attribute14,
7000   global_attribute15,
7001   global_attribute16,
7002   global_attribute17,
7003   global_attribute18,
7004   global_attribute19,
7005   global_attribute20,
7006   orig_system_reference,
7007   status,
7008   customer_type,
7009   customer_class_code,
7010   primary_salesrep_id,
7011   sales_channel_code ,
7012   order_type_id      ,
7013   price_list_id      ,
7014   tax_code           ,
7015   fob_point          ,
7016   freight_term      ,
7017   ship_partial     ,
7018   ship_via         ,
7019   warehouse_id     ,
7020   tax_header_level_flag,
7021   tax_rounding_rule    ,
7022   coterminate_day_month,
7023   primary_specialist_id,
7024   secondary_specialist_id ,
7025   account_liable_flag    ,
7026   current_balance          ,
7027   account_established_date,
7028   account_termination_date   ,
7029   account_activation_date    ,
7030   department               ,
7031   held_bill_expiration_date,
7032   hold_bill_flag           ,
7033   realtime_rate_flag      ,
7034   acct_life_cycle_status,
7035   account_name          ,
7036   deposit_refund_method ,
7037   dormant_account_flag  ,
7038   npa_number            ,
7039   suspension_date       ,
7040   Source_code             ,
7041   competitor_type         ,
7042   comments                ,
7043   dates_negative_tolerance,
7044   dates_positive_tolerance,
7045   date_type_preference    ,
7046   over_shipment_tolerance ,
7047   under_shipment_tolerance,
7048   over_return_tolerance   ,
7049   under_return_tolerance  ,
7050   item_cross_ref_pref     ,
7051   ship_sets_include_lines_flag ,
7052   arrivalsets_include_lines_flag,
7053   sched_date_push_flag      ,
7054   invoice_quantity_rule     ,
7055   pricing_event             ,
7056   status_update_date        ,
7057   autopay_flag              ,
7058   notify_flag               ,
7059   last_batch_id             ,
7060   org_id                    ,
7061   object_version_number     ,
7062   created_by_module         ,
7063   application_id           ,
7064   selling_party_id         ,
7065   federal_entity_type	   ,
7066   trading_partner_agency_id,
7067   duns_extension           ,
7068   advance_payment_indicator,
7069   merge_request_id
7070 )
7071   SELECT
7072   customer_merge_header_id,
7073   cust_account_id,
7074   party_id      ,
7075   c.last_update_date ,
7076   c.account_number   ,
7077   c.last_updated_by  ,
7078   c.creation_date   ,
7079   c.created_by     ,
7080   c.last_update_login,
7081   c.request_id       ,
7082   c.program_application_id,
7083   c.program_id            ,
7084   c.program_update_date   ,
7085   c.attribute_category    ,
7086   c.attribute1            ,
7087   c.attribute2            ,
7088   c.attribute3            ,
7089   c.attribute4            ,
7090   c.attribute5            ,
7091   c.attribute6            ,
7092   c.attribute7,
7093   c.attribute8,
7094   c.attribute9 ,
7095   c.attribute10,
7096   c.attribute11,
7097   c.attribute12,
7098   c.attribute13,
7099   c.attribute14,
7100   c.attribute15,
7101   c.attribute16,
7102   c.attribute17,
7103   c.attribute18,
7104   c.attribute19,
7105   c.attribute20,
7106   c.global_attribute_category,
7107   c.global_attribute1,
7108   c.global_attribute2,
7109   c.global_attribute3,
7110   c.global_attribute4,
7111   c.global_attribute5,
7112   c.global_attribute6,
7113   c.global_attribute7,
7114   c.global_attribute8,
7115   c.global_attribute9,
7116   c.global_attribute10,
7117   c.global_attribute11,
7118   c.global_attribute12,
7119   c.global_attribute13,
7120   c.global_attribute14,
7121   c.global_attribute15,
7122   c.global_attribute16,
7123   c.global_attribute17,
7124   c.global_attribute18,
7125   c.global_attribute19,
7126   c.global_attribute20,
7127   c.orig_system_reference,
7128   c.status,
7129   c.customer_type,
7130   c.customer_class_code,
7131   c.primary_salesrep_id,
7132   c.sales_channel_code ,
7133   c.order_type_id      ,
7134   c.price_list_id      ,
7135   c.tax_code           ,
7136   c.fob_point          ,
7137   c.freight_term      ,
7138   c.ship_partial     ,
7139   c.ship_via         ,
7140   c.warehouse_id     ,
7141   c.tax_header_level_flag,
7142   c.tax_rounding_rule    ,
7143   c.coterminate_day_month,
7144   c.primary_specialist_id,
7145   c.secondary_specialist_id ,
7146   c.account_liable_flag    ,
7147   c.current_balance          ,
7148   c.account_established_date,
7149   c.account_termination_date   ,
7150   c.account_activation_date    ,
7151   c.department               ,
7152   c.held_bill_expiration_date,
7153   c.hold_bill_flag           ,
7154   c.realtime_rate_flag      ,
7155   c.acct_life_cycle_status,
7156   c.account_name          ,
7157   c.deposit_refund_method ,
7158   c.dormant_account_flag  ,
7159   c.npa_number            ,
7160   c.suspension_date       ,
7161   c.source_code             ,
7162   c.competitor_type         ,
7163   c.comments                ,
7164   c.dates_negative_tolerance,
7165   c.dates_positive_tolerance,
7166   c.date_type_preference    ,
7167   c.over_shipment_tolerance ,
7168   c.under_shipment_tolerance,
7169   c.over_return_tolerance   ,
7170   c.under_return_tolerance  ,
7171   c.item_cross_ref_pref     ,
7172   c.ship_sets_include_lines_flag ,
7173   c.arrivalsets_include_lines_flag,
7174   c.sched_date_push_flag      ,
7175   c.invoice_quantity_rule     ,
7176   c.pricing_event             ,
7177   c.status_update_date        ,
7178   c.autopay_flag              ,
7179   c.notify_flag               ,
7180   c.last_batch_id             ,
7181   c.org_id                    ,
7182   c.object_version_number     ,
7183   c.created_by_module         ,
7184   c.application_id           ,
7185   c.selling_party_id         ,
7186   c.federal_entity_type	     ,
7187   c.trading_partner_agency_id,
7188   c.duns_extension           ,
7189   c.advance_payment_indicator,
7190   req_id
7191 FROM (select distinct duplicate_id , customer_merge_header_id , customer_id
7192       from ra_customer_merges cm
7193       where cm.process_flag = 'N'
7194       and   cm.request_id = req_id
7195       and   cm.set_number = set_num) , HZ_CUST_ACCOUNTS c
7196 WHERE  c.cust_account_id = duplicate_id
7197 AND    duplicate_id <> customer_id;
7198 
7199  arp_message.set_line(SQL%ROWCOUNT||' '|| 'Row(s) inserted in HZ_CUST_ACCOUNTS_M');
7200 
7201 
7202 
7203   ---------Insert into hz_cust_account_roles_m--------------
7204   --Because roles can be set up at acct and site level, we need to select
7205   --distict duplicate and customer_merge_header_id if acct with multiple sites
7206   --have roles set up at account level.
7207 
7208   INSERT INTO hz_cust_account_roles_m(
7209   customer_merge_header_id,
7210   cust_account_role_id      ,
7211   party_id                  ,
7212   cust_account_id           ,
7213   cust_acct_site_id          ,
7214   primary_flag               ,
7215   role_type                  ,
7216   last_update_date           ,
7217   source_code                ,
7218   last_updated_by            ,
7219   creation_date              ,
7220   created_by                 ,
7221   last_update_login          ,
7222   request_id                 ,
7223   program_application_id     ,
7224   program_id                 ,
7225   program_update_date        ,
7226   attribute_category         ,
7227   attribute1                 ,
7228   attribute2                 ,
7229   attribute3                 ,
7230   attribute4                 ,
7231   attribute5                 ,
7232   attribute6                 ,
7233   attribute7                 ,
7234   attribute8                ,
7235   attribute9                ,
7236   attribute10               ,
7237   attribute11               ,
7238   attribute12               ,
7239   attribute13               ,
7240   attribute14              ,
7241   attribute15              ,
7242   attribute16              ,
7243   attribute17              ,
7244   attribute18              ,
7245   attribute19              ,
7246   attribute20              ,
7247   attribute21              ,
7248   attribute22              ,
7249   attribute23,
7250   attribute24,
7251   global_attribute_category ,
7252   global_attribute1         ,
7253   global_attribute2         ,
7254   global_attribute3         ,
7255   global_attribute4         ,
7256   global_attribute5        ,
7257   global_attribute6        ,
7258   global_attribute7        ,
7259   global_attribute8        ,
7260   global_attribute9        ,
7261   global_attribute10       ,
7262   global_attribute11       ,
7263   global_attribute12       ,
7264   global_attribute13       ,
7265   global_attribute14      ,
7266   global_attribute15      ,
7267   global_attribute16      ,
7268   global_attribute17      ,
7269   global_attribute18      ,
7270   global_attribute19      ,
7271   global_attribute20      ,
7272   orig_system_reference  ,
7273   attribute25           ,
7274   status               ,
7275   object_version_number,
7276   created_by_module     ,
7277   application_id         ,
7278   merge_request_id
7279  )
7280  SELECT distinct
7281   customer_merge_header_id,
7282   ar.cust_account_role_id      ,
7283   ar.party_id                  ,
7284   ar.cust_account_id           ,
7285   ar.cust_acct_site_id          ,
7286   ar.primary_flag               ,
7287   ar.role_type                  ,
7288   ar.last_update_date           ,
7289   ar.source_code                ,
7290   ar.last_updated_by            ,
7291   ar.creation_date              ,
7292   ar.created_by                 ,
7293   ar.last_update_login          ,
7294   ar.request_id                 ,
7295   ar.program_application_id     ,
7296   ar.program_id                 ,
7297   ar.program_update_date        ,
7298   ar.attribute_category         ,
7299   ar.attribute1                 ,
7300   ar.attribute2                 ,
7301   ar.attribute3                 ,
7302   ar.attribute4                 ,
7303   ar.attribute5                 ,
7304   ar.attribute6                 ,
7305   ar.attribute7                 ,
7306   ar.attribute8                ,
7307   ar.attribute9                ,
7308   ar.attribute10               ,
7309   ar.attribute11               ,
7310   ar.attribute12               ,
7311   ar.attribute13               ,
7312   ar.attribute14              ,
7313   ar.attribute15              ,
7314   ar.attribute16              ,
7315   ar.attribute17              ,
7316   ar.attribute18              ,
7317   ar.attribute19              ,
7318   ar.attribute20              ,
7319   ar.attribute21              ,
7320   ar.attribute22              ,
7321   ar.attribute23,
7322   ar.attribute24,
7323   ar.global_attribute_category ,
7324   ar.global_attribute1         ,
7325   ar.global_attribute2         ,
7326   ar.global_attribute3         ,
7327   ar.global_attribute4         ,
7328   ar.global_attribute5        ,
7329   ar.global_attribute6        ,
7330   ar.global_attribute7        ,
7331   ar.global_attribute8        ,
7332   ar.global_attribute9        ,
7333   ar.global_attribute10       ,
7334   ar.global_attribute11       ,
7335   ar.global_attribute12       ,
7336   ar.global_attribute13       ,
7337   ar.global_attribute14      ,
7338   ar.global_attribute15      ,
7339   ar.global_attribute16      ,
7340   ar.global_attribute17      ,
7341   ar.global_attribute18      ,
7342   ar.global_attribute19      ,
7343   ar.global_attribute20      ,
7344   ar.orig_system_reference  ,
7345   ar.attribute25           ,
7346   ar.status               ,
7347   ar.object_version_number,
7348   ar.created_by_module     ,
7349   ar.application_id         ,
7350   req_id
7351  FROM(select distinct duplicate_id,duplicate_address_id,customer_merge_header_id
7352       from ra_customer_merges cm
7353       where cm.process_flag = 'N'
7354       and cm.request_id = req_id
7355       and cm.set_number = set_num
7356       and cm.duplicate_id <> cm.customer_id), hz_cust_account_roles ar
7357  WHERE ( ar.cust_account_id   = duplicate_id  OR
7358          ar.cust_acct_site_id = duplicate_address_id  )
7359  AND ar.role_type = 'CONTACT';
7360 
7361  arp_message.set_line(SQL%ROWCOUNT||' '|| 'Row(s) inserted in HZ_CUST_ACCOUNT_ROLES_M');
7362 
7363   ---------Insert into hz_customer_profiles_m--------------
7364   --Because profiles can be set up at acct and site use level, we need to select
7365   --distict duplicate and customer_merge_header_id if acct with multiple sites
7366   --have profiles set up at account level.
7367 
7368   INSERT INTO hz_customer_profiles_m(
7369   customer_merge_header_id,
7370   cust_account_profile_id,
7371   last_updated_by     ,
7372   last_update_date    ,
7373   last_update_login   ,
7374   created_by          ,
7375   creation_date       ,
7376   cust_account_id     ,
7377   status              ,
7378   collector_id        ,
7379   credit_analyst_id   ,
7380   credit_checking     ,
7381   next_credit_review_date ,
7382   tolerance           ,
7383   discount_terms      ,
7384   dunning_letters     ,
7385   interest_charges    ,
7386   send_statements     ,
7387   credit_balance_statements,
7388   credit_hold         ,
7389   profile_class_id    ,
7390   site_use_id         ,
7391   credit_rating      ,
7392   risk_code           ,
7393   standard_terms      ,
7394   override_terms      ,
7395   dunning_letter_set_id    ,
7396   interest_period_days     ,
7397   payment_grace_days       ,
7398   discount_grace_days      ,
7399   statement_cycle_id       ,
7400   account_status           ,
7401   percent_collectable      ,
7402   autocash_hierarchy_id    ,
7403   attribute_category       ,
7404   attribute1               ,
7405   attribute2               ,
7406   attribute3               ,
7407   attribute4               ,
7408   attribute5               ,
7409   attribute6               ,
7410   attribute7              ,
7411   attribute8              ,
7412   attribute9              ,
7413   attribute10             ,
7414   program_application_id  ,
7415   program_id              ,
7416   program_update_date     ,
7417   request_id              ,
7418   wh_update_date         ,
7419   attribute11           ,
7420   attribute12          ,
7421   attribute13         ,
7422   attribute14        ,
7423   attribute15       ,
7424   auto_rec_incl_disputed_flag ,
7425   tax_printing_option       ,
7426   charge_on_finance_charge_flag ,
7427   grouping_rule_id          ,
7428   clearing_days            ,
7429   jgzz_attribute_category  ,
7430   jgzz_attribute1          ,
7431   jgzz_attribute2          ,
7432   jgzz_attribute3          ,
7433   jgzz_attribute4         ,
7434   jgzz_attribute5         ,
7435   jgzz_attribute6        ,
7436   jgzz_attribute7       ,
7437   jgzz_attribute8      ,
7438   jgzz_attribute9     ,
7439   jgzz_attribute10    ,
7440   jgzz_attribute11  ,
7441   jgzz_attribute12 ,
7442   jgzz_attribute13,
7443   jgzz_attribute14 ,
7444   jgzz_attribute15,
7445   global_attribute1,
7446   global_attribute2,
7447   global_attribute3,
7448   global_attribute4,
7449   global_attribute5,
7450   global_attribute6,
7451   global_attribute7,
7452   global_attribute8,
7453   global_attribute9,
7454   global_attribute10,
7455   global_attribute11 ,
7456   global_attribute12  ,
7457   global_attribute13   ,
7458   global_attribute14    ,
7459   global_attribute15   ,
7460   global_attribute16    ,
7461   global_attribute17     ,
7462   global_attribute18    ,
7463   global_attribute19   ,
7464   global_attribute20  ,
7465   global_attribute_category  ,
7466   cons_inv_flag             ,
7467   cons_inv_type            ,
7468   autocash_hierarchy_id_for_adr ,
7469   lockbox_matching_option    ,
7470   object_version_number     ,
7471   created_by_module          ,
7472   application_id            ,
7473   review_cycle             ,
7474   party_id                ,
7475   last_credit_review_date ,
7476   merge_request_id,
7477   automatch_set_id      --8477178
7478  )
7479  SELECT distinct
7480   customer_merge_header_id,
7481   cp.cust_account_profile_id,
7482   cp.last_updated_by     ,
7483   cp.last_update_date    ,
7484   cp.last_update_login   ,
7485   cp.created_by          ,
7486   cp.creation_date       ,
7487   cp.cust_account_id     ,
7488   cp.status              ,
7489   cp.collector_id        ,
7490   cp.credit_analyst_id   ,
7491   cp.credit_checking     ,
7492   cp.next_credit_review_date ,
7493   cp.tolerance           ,
7494   cp.discount_terms      ,
7495   cp.dunning_letters     ,
7496   cp.interest_charges    ,
7497   cp.send_statements     ,
7498   cp.credit_balance_statements,
7499   cp.credit_hold         ,
7500   cp.profile_class_id    ,
7501   cp.site_use_id         ,
7502   cp.credit_rating      ,
7503   cp.risk_code           ,
7504   cp.standard_terms      ,
7505   cp.override_terms      ,
7506   cp.dunning_letter_set_id    ,
7507   cp.interest_period_days     ,
7508   cp.payment_grace_days       ,
7509   cp.discount_grace_days      ,
7510   cp.statement_cycle_id       ,
7511   cp.account_status           ,
7512   cp.percent_collectable      ,
7513   cp.autocash_hierarchy_id    ,
7514   cp.attribute_category       ,
7515   cp.attribute1               ,
7516   cp.attribute2               ,
7517   cp.attribute3               ,
7518   cp.attribute4               ,
7519   cp.attribute5               ,
7520   cp.attribute6               ,
7521   cp.attribute7              ,
7522   cp.attribute8              ,
7523   cp.attribute9              ,
7524   cp.attribute10             ,
7525   cp.program_application_id  ,
7526   cp.program_id              ,
7527   cp.program_update_date     ,
7528   cp.request_id              ,
7529   cp.wh_update_date         ,
7530   cp.attribute11           ,
7531   cp.attribute12          ,
7532   cp.attribute13         ,
7533   cp.attribute14        ,
7534   cp.attribute15       ,
7535   cp.auto_rec_incl_disputed_flag ,
7536   cp.tax_printing_option       ,
7537   cp.charge_on_finance_charge_flag ,
7538   cp.grouping_rule_id          ,
7539   cp.clearing_days            ,
7540   cp.jgzz_attribute_category  ,
7541   cp.jgzz_attribute1          ,
7542   cp.jgzz_attribute2          ,
7543   cp.jgzz_attribute3          ,
7544   cp.jgzz_attribute4         ,
7545   cp.jgzz_attribute5         ,
7546   cp.jgzz_attribute6        ,
7547   cp.jgzz_attribute7       ,
7548   cp.jgzz_attribute8      ,
7549   cp.jgzz_attribute9     ,
7550   cp.jgzz_attribute10    ,
7551   cp.jgzz_attribute11  ,
7552   cp.jgzz_attribute12 ,
7553   cp.jgzz_attribute13,
7554   cp.jgzz_attribute14 ,
7555   cp.jgzz_attribute15,
7556   cp.global_attribute1,
7557   cp.global_attribute2,
7558   cp.global_attribute3,
7559   cp.global_attribute4,
7560   cp.global_attribute5,
7561   cp.global_attribute6,
7562   cp.global_attribute7,
7563   cp.global_attribute8,
7564   cp.global_attribute9,
7565   cp.global_attribute10,
7566   cp.global_attribute11 ,
7567   cp.global_attribute12  ,
7568   cp.global_attribute13   ,
7569   cp.global_attribute14    ,
7570   cp.global_attribute15   ,
7571   cp.global_attribute16    ,
7572   cp.global_attribute17     ,
7573   cp.global_attribute18    ,
7574   cp.global_attribute19   ,
7575   cp.global_attribute20  ,
7576   cp.global_attribute_category  ,
7577   cp.cons_inv_flag             ,
7578   cp.cons_inv_type            ,
7579   cp.autocash_hierarchy_id_for_adr ,
7580   cp.lockbox_matching_option    ,
7581   cp.object_version_number     ,
7582   cp.created_by_module          ,
7583   cp.application_id            ,
7584   cp.review_cycle             ,
7585   cp.party_id                ,
7586   cp.last_credit_review_date ,
7587   req_id,
7588   cp.automatch_set_id    --bug 8477178
7589  FROM(select distinct duplicate_id,duplicate_site_id,customer_merge_header_id
7590       from ra_customer_merges cm
7591       where cm.process_flag = 'N'
7592       and cm.request_id = req_id
7593       and cm.set_number = set_num
7594       and cm.duplicate_id <> cm.customer_id ), hz_customer_profiles cp
7595  WHERE  ( cp.cust_account_id = duplicate_id AND cp.site_use_id is NULL)
7596        OR (cp.site_use_id = duplicate_site_id );
7597 
7598  arp_message.set_line(SQL%ROWCOUNT||' '|| 'Row(s) inserted in HZ_CUSTOMER_PROFILES_M');
7599 
7600   ---------Insert into hz_cust_profile_amts_m--------------
7601   --Because profiles can be set up at acct and site use level, we need to select
7602   --distict duplicate and customer_merge_header_id if acct with multiple sites
7603   --have profiles set up at account level.
7604 
7605   INSERT INTO hz_cust_profile_amts_m(
7606   customer_merge_header_id,
7607   cust_acct_profile_amt_id   ,
7608   last_updated_by            ,
7609   last_update_date           ,
7610   created_by                 ,
7611   creation_date              ,
7612   cust_account_profile_id    ,
7613   currency_code              ,
7614   last_update_login          ,
7615   trx_credit_limit           ,
7616   overall_credit_limit       ,
7617   min_dunning_amount         ,
7618   min_dunning_invoice_amount ,
7619   max_interest_charge        ,
7620   min_statement_amount       ,
7621   auto_rec_min_receipt_amount,
7622   interest_rate              ,
7623   attribute_category         ,
7624   attribute1                 ,
7625   attribute2                 ,
7626   attribute3                 ,
7627   attribute4                 ,
7628   attribute5                 ,
7629   attribute6                 ,
7630   attribute7                 ,
7631   attribute8                 ,
7632   attribute9                 ,
7633   attribute10                ,
7634   attribute11                ,
7635   attribute12                ,
7636   attribute13                ,
7637   attribute14                ,
7638   attribute15                ,
7639   min_fc_balance_amount      ,
7640   min_fc_invoice_amount      ,
7641   cust_account_id            ,
7642   site_use_id                ,
7643   expiration_date            ,
7644   request_id                 ,
7645   program_application_id     ,
7646   program_id                 ,
7647   program_update_date        ,
7648   wh_update_date             ,
7649   jgzz_attribute_category    ,
7650   jgzz_attribute1            ,
7651   jgzz_attribute2            ,
7652   jgzz_attribute3            ,
7653   jgzz_attribute4            ,
7654   jgzz_attribute5            ,
7655   jgzz_attribute6            ,
7656   jgzz_attribute7            ,
7657   jgzz_attribute8            ,
7658   jgzz_attribute9            ,
7659   jgzz_attribute10           ,
7660   jgzz_attribute11           ,
7661   jgzz_attribute12           ,
7662   jgzz_attribute13           ,
7663   jgzz_attribute14           ,
7664   jgzz_attribute15           ,
7665   global_attribute1          ,
7666   global_attribute2          ,
7667   global_attribute3          ,
7668   global_attribute4          ,
7669   global_attribute5          ,
7670   global_attribute6          ,
7671   global_attribute7          ,
7672   global_attribute8          ,
7673   global_attribute9          ,
7674   global_attribute10         ,
7675   global_attribute11         ,
7676   global_attribute12         ,
7677   global_attribute13         ,
7678   global_attribute14         ,
7679   global_attribute15         ,
7680   global_attribute16         ,
7681   global_attribute17         ,
7682   global_attribute18         ,
7683   global_attribute19         ,
7684   global_attribute20         ,
7685   global_attribute_category  ,
7686   object_version_number      ,
7687   created_by_module          ,
7688   application_id             ,
7689   merge_request_id
7690  )
7691  select distinct
7692   customer_merge_header_id,
7693   pa.cust_acct_profile_amt_id   ,
7694   pa.last_updated_by            ,
7695   pa.last_update_date           ,
7696   pa.created_by                 ,
7697   pa.creation_date              ,
7698   pa.cust_account_profile_id    ,
7699   pa.currency_code              ,
7700   pa.last_update_login          ,
7701   pa.trx_credit_limit           ,
7702   pa.overall_credit_limit       ,
7703   pa.min_dunning_amount         ,
7704   pa.min_dunning_invoice_amount ,
7705   pa.max_interest_charge        ,
7706   pa.min_statement_amount       ,
7707   pa.auto_rec_min_receipt_amount,
7708   pa.interest_rate              ,
7709   pa.attribute_category         ,
7710   pa.attribute1                 ,
7711   pa.attribute2                 ,
7712   pa.attribute3                 ,
7713   pa.attribute4                 ,
7714   pa.attribute5                 ,
7715   pa.attribute6                 ,
7716   pa.attribute7                 ,
7717   pa.attribute8                 ,
7718   pa.attribute9                 ,
7719   pa.attribute10                ,
7720   pa.attribute11                ,
7721   pa.attribute12                ,
7722   pa.attribute13                ,
7723   pa.attribute14                ,
7724   pa.attribute15                ,
7725   pa.min_fc_balance_amount      ,
7726   pa.min_fc_invoice_amount      ,
7727   pa.cust_account_id            ,
7728   pa.site_use_id                ,
7729   pa.expiration_date            ,
7730   pa.request_id                 ,
7731   pa.program_application_id     ,
7732   pa.program_id                 ,
7733   pa.program_update_date        ,
7734   pa.wh_update_date             ,
7735   pa.jgzz_attribute_category    ,
7736   pa.jgzz_attribute1            ,
7737   pa.jgzz_attribute2            ,
7738   pa.jgzz_attribute3            ,
7739   pa.jgzz_attribute4            ,
7740   pa.jgzz_attribute5            ,
7741   pa.jgzz_attribute6            ,
7742   pa.jgzz_attribute7            ,
7743   pa.jgzz_attribute8            ,
7744   pa.jgzz_attribute9            ,
7745   pa.jgzz_attribute10           ,
7746   pa.jgzz_attribute11           ,
7747   pa.jgzz_attribute12           ,
7748   pa.jgzz_attribute13           ,
7749   pa.jgzz_attribute14           ,
7750   pa.jgzz_attribute15           ,
7751   pa.global_attribute1          ,
7752   pa.global_attribute2          ,
7753   pa.global_attribute3          ,
7754   pa.global_attribute4          ,
7755   pa.global_attribute5          ,
7756   pa.global_attribute6          ,
7757   pa.global_attribute7          ,
7758   pa.global_attribute8          ,
7759   pa.global_attribute9          ,
7760   pa.global_attribute10         ,
7761   pa.global_attribute11         ,
7762   pa.global_attribute12         ,
7763   pa.global_attribute13         ,
7764   pa.global_attribute14         ,
7765   pa.global_attribute15         ,
7766   pa.global_attribute16         ,
7767   pa.global_attribute17         ,
7768   pa.global_attribute18         ,
7769   pa.global_attribute19         ,
7770   pa.global_attribute20         ,
7771   pa.global_attribute_category  ,
7772   pa.object_version_number      ,
7773   pa.created_by_module          ,
7774   pa.application_id             ,
7775   req_id
7776 FROM (select distinct duplicate_id,duplicate_site_id,customer_merge_header_id
7777       from ra_customer_merges cm
7778       where cm.process_flag = 'N'
7779       and cm.request_id = req_id
7780       and cm.set_number = set_num ),hz_cust_profile_amts pa
7781 WHERE (pa.cust_account_id = duplicate_id and pa.site_use_id is NULL)
7782 OR    (pa.site_use_id  =  duplicate_site_id);
7783 
7784  arp_message.set_line(SQL%ROWCOUNT||' '|| 'Row(s) inserted in HZ_CUST_PROFILE_AMTS_M');
7785 
7786   ---------Insert into hz_cust_acct_sites_all_m--------------
7787   INSERT INTO hz_cust_acct_sites_all_m(
7788   customer_merge_header_id,
7789   cust_acct_site_id          ,
7790   cust_account_id            ,
7791   party_site_id              ,
7792   last_update_date           ,
7793   last_updated_by            ,
7794   creation_date              ,
7795   created_by                 ,
7796   last_update_login          ,
7797   request_id                 ,
7798   program_application_id     ,
7799   program_id                 ,
7800   program_update_date        ,
7801 --wh_update_date             ,
7802   attribute_category         ,
7803   attribute1                 ,
7804   attribute2                 ,
7805   attribute3                 ,
7806   attribute4                 ,
7807   attribute5                 ,
7808   attribute6                 ,
7809   attribute7                 ,
7810   attribute8                 ,
7811   attribute9                 ,
7812   attribute10                ,
7813   attribute11                ,
7814   attribute12                ,
7815   attribute13                ,
7816   attribute14                ,
7817   attribute15                ,
7818   attribute16                ,
7819   attribute17                ,
7820   attribute18                ,
7821   attribute19                ,
7822   attribute20                ,
7823   global_attribute_category  ,
7824   global_attribute1          ,
7825   global_attribute2          ,
7826   global_attribute3          ,
7827   global_attribute4          ,
7828   global_attribute5          ,
7829   global_attribute6          ,
7830   global_attribute7          ,
7831   global_attribute8          ,
7832   global_attribute9          ,
7833   global_attribute10         ,
7834   global_attribute11         ,
7835   global_attribute12         ,
7836   global_attribute13         ,
7837   global_attribute14         ,
7838   global_attribute15         ,
7839   global_attribute16         ,
7840   global_attribute17         ,
7841   global_attribute18         ,
7842   global_attribute19         ,
7843   global_attribute20         ,
7844   orig_system_reference      ,
7845   status                     ,
7846   org_id                     ,
7847   bill_to_flag               ,
7848   market_flag                ,
7849   ship_to_flag               ,
7850   customer_category_code     ,
7851   language                   ,
7852   key_account_flag           ,
7853   tp_header_id               ,
7854   ece_tp_location_code       ,
7855 --service_territory_id       ,
7856   primary_specialist_id      ,
7857   secondary_specialist_id    ,
7858   territory_id               ,
7859   address_text               ,
7860   territory                  ,
7861   translated_customer_name   ,
7862   object_version_number      ,
7863   created_by_module          ,
7864   application_id             ,
7865   merge_request_id
7866 )
7867  select
7868   customer_merge_header_id,
7869   acs.cust_acct_site_id          ,
7870   acs.cust_account_id            ,
7871   acs.party_site_id              ,
7872   acs.last_update_date           ,
7873   acs.last_updated_by            ,
7874   acs.creation_date              ,
7875   acs.created_by                 ,
7876   acs.last_update_login          ,
7877   acs.request_id                 ,
7878   acs.program_application_id     ,
7879   acs.program_id                 ,
7880   acs.program_update_date        ,
7881 --wh_update_date             ,
7882   acs.attribute_category         ,
7883   acs.attribute1                 ,
7884   acs.attribute2                 ,
7885   acs.attribute3                 ,
7886   acs.attribute4                 ,
7887   acs.attribute5                 ,
7888   acs.attribute6                 ,
7889   acs.attribute7                 ,
7890   acs.attribute8                 ,
7891   acs.attribute9                 ,
7892   acs.attribute10                ,
7893   acs.attribute11                ,
7894   acs.attribute12                ,
7895   acs.attribute13                ,
7896   acs.attribute14                ,
7897   acs.attribute15                ,
7898   acs.attribute16                ,
7899   acs.attribute17                ,
7900   acs.attribute18                ,
7901   acs.attribute19                ,
7902   acs.attribute20                ,
7903   acs.global_attribute_category  ,
7904   acs.global_attribute1          ,
7905   acs.global_attribute2          ,
7906   acs.global_attribute3          ,
7907   acs.global_attribute4          ,
7908   acs.global_attribute5          ,
7909   acs.global_attribute6          ,
7910   acs.global_attribute7          ,
7911   acs.global_attribute8          ,
7912   acs.global_attribute9          ,
7913   acs.global_attribute10         ,
7914   acs.global_attribute11         ,
7915   acs.global_attribute12         ,
7916   acs.global_attribute13         ,
7917   acs.global_attribute14         ,
7918   acs.global_attribute15         ,
7919   acs.global_attribute16         ,
7920   acs.global_attribute17         ,
7921   acs.global_attribute18         ,
7922   acs.global_attribute19         ,
7923   acs.global_attribute20         ,
7924   acs.orig_system_reference      ,
7925   acs.status                     ,
7926   acs.org_id                     ,
7927   acs.bill_to_flag               ,
7928   acs.market_flag                ,
7929   acs.ship_to_flag               ,
7930   acs.customer_category_code     ,
7931   acs.language                   ,
7932   acs.key_account_flag           ,
7933   acs.tp_header_id               ,
7934   acs.ece_tp_location_code       ,
7935 --service_territory_id       ,
7936   acs.primary_specialist_id      ,
7937   acs.secondary_specialist_id    ,
7938   acs.territory_id               ,
7939   acs.address_text               ,
7940   acs.territory                  ,
7941   acs.translated_customer_name   ,
7942   acs.object_version_number      ,
7943   acs.created_by_module          ,
7944   acs.application_id             ,
7945   req_id
7946 FROM (select distinct duplicate_id,duplicate_address_id,customer_merge_header_id,org_id
7947       from ra_customer_merges cm
7948       where cm.process_flag = 'N'
7949       and cm.request_id = req_id
7950       and cm.set_number = set_num ) m,hz_cust_acct_sites_all acs
7951 WHERE acs.cust_acct_site_id = duplicate_address_id
7952 AND   acs.org_id  = m.org_id ;
7953 
7954  arp_message.set_line(SQL%ROWCOUNT||' '|| 'Row(s) inserted in HZ_CUST_ACCT_SITES_ALL_M');
7955 
7956   ---------Insert into hz_cust_site_uses_all_m--------------
7957   INSERT INTO hz_cust_site_uses_all_m(
7958   customer_merge_header_id,
7959   site_use_id                ,
7960   cust_acct_site_id          ,
7961   last_update_date           ,
7962   last_updated_by            ,
7963   creation_date              ,
7964   created_by                 ,
7965   site_use_code              ,
7966   primary_flag               ,
7967   status                     ,
7968   location                   ,
7969   last_update_login          ,
7970   contact_id                 ,
7971   bill_to_site_use_id        ,
7972   orig_system_reference      ,
7973   sic_code                   ,
7974   payment_term_id            ,
7975   gsa_indicator              ,
7976   ship_partial               ,
7977   ship_via                   ,
7978   fob_point                  ,
7979   order_type_id              ,
7980   price_list_id              ,
7981   freight_term               ,
7982   warehouse_id               ,
7983   territory_id               ,
7984   attribute_category         ,
7985   attribute1                 ,
7986   attribute2                 ,
7987   attribute3                 ,
7988   attribute4                 ,
7989   attribute5                 ,
7990   attribute6                 ,
7991   attribute7                 ,
7992   attribute8                 ,
7993   attribute9                 ,
7994   attribute10                ,
7995   request_id                 ,
7996   program_application_id     ,
7997   program_id                 ,
7998   program_update_date        ,
7999   tax_reference              ,
8000   sort_priority              ,
8001   tax_code                   ,
8002   attribute11                ,
8003   attribute12                ,
8004   attribute13                ,
8005   attribute14                ,
8006   attribute15                ,
8007   attribute16                ,
8008   attribute17                ,
8009   attribute18                ,
8010   attribute19                ,
8011   attribute20                ,
8012   attribute21                ,
8013   attribute22                ,
8014   attribute23                ,
8015   attribute24                ,
8016   attribute25                ,
8017   last_accrue_charge_date    ,
8018   second_last_accrue_charge_date  ,
8019   last_unaccrue_charge_date  ,
8020   second_last_unaccrue_chrg_date  ,
8021   demand_class_code          ,
8022   org_id,
8023   tax_header_level_flag      ,
8024   tax_rounding_rule          ,
8025   --wh_update_date           ,
8026   global_attribute1          ,
8027   global_attribute2          ,
8028   global_attribute3          ,
8029   global_attribute4          ,
8030   global_attribute5          ,
8031   global_attribute6          ,
8032   global_attribute7          ,
8033   global_attribute8          ,
8034   global_attribute9          ,
8035   global_attribute10         ,
8036   global_attribute11         ,
8037   global_attribute12         ,
8038   global_attribute13         ,
8039   global_attribute14         ,
8040   global_attribute15         ,
8041   global_attribute16         ,
8042   global_attribute17         ,
8043   global_attribute18         ,
8044   global_attribute19         ,
8048   finchrg_receivables_trx_id ,
8045   global_attribute20         ,
8046   global_attribute_category  ,
8047   primary_salesrep_id        ,
8049   dates_negative_tolerance   ,
8050   dates_positive_tolerance   ,
8051   date_type_preference       ,
8052   over_shipment_tolerance    ,
8053   under_shipment_tolerance   ,
8054   item_cross_ref_pref        ,
8055   over_return_tolerance      ,
8056   under_return_tolerance     ,
8057   ship_sets_include_lines_flag  ,
8058   arrivalsets_include_lines_flag  ,
8059   sched_date_push_flag       ,
8060   invoice_quantity_rule      ,
8061   pricing_event              ,
8062   gl_id_rec                  ,
8063   gl_id_rev                  ,
8064   gl_id_tax                  ,
8065   gl_id_freight              ,
8066   gl_id_clearing             ,
8067   gl_id_unbilled             ,
8068   gl_id_unearned             ,
8069   gl_id_unpaid_rec           ,
8070   gl_id_remittance           ,
8071   gl_id_factor               ,
8072   tax_classification         ,
8073   object_version_number      ,
8074   created_by_module          ,
8075   application_id             ,
8076   merge_request_id
8077 )
8078 select
8079   customer_merge_header_id,
8080   su.site_use_id                ,
8081   su.cust_acct_site_id          ,
8082   su.last_update_date           ,
8083   su.last_updated_by            ,
8084   su.creation_date              ,
8085   su.created_by                 ,
8086   su.site_use_code              ,
8087   su.primary_flag               ,
8088   su.status                     ,
8089   su.location                   ,
8090   su.last_update_login          ,
8091   su.contact_id                 ,
8092   su.bill_to_site_use_id        ,
8093   su.orig_system_reference      ,
8094   su.sic_code                   ,
8095   su.payment_term_id            ,
8096   su.gsa_indicator              ,
8097   su.ship_partial               ,
8098   su.ship_via                   ,
8099   su.fob_point                  ,
8100   su.order_type_id              ,
8101   su.price_list_id              ,
8102   su.freight_term               ,
8103   su.warehouse_id               ,
8104   su.territory_id               ,
8105   su.attribute_category         ,
8106   su.attribute1                 ,
8107   su.attribute2                 ,
8108   su.attribute3                 ,
8109   su.attribute4                 ,
8110   su.attribute5                 ,
8111   su.attribute6                 ,
8112   su.attribute7                 ,
8113   su.attribute8                 ,
8114   su.attribute9                 ,
8115   su.attribute10                ,
8116   su.request_id                 ,
8117   su.program_application_id     ,
8118   su.program_id                 ,
8119   su.program_update_date        ,
8120   su.tax_reference              ,
8121   su.sort_priority              ,
8122   su.tax_code                   ,
8123   su.attribute11                ,
8124   su.attribute12                ,
8125   su.attribute13                ,
8126   su.attribute14                ,
8127   su.attribute15                ,
8128   su.attribute16                ,
8129   su.attribute17                ,
8130   su.attribute18                ,
8131   su.attribute19                ,
8132   su.attribute20                ,
8133   su.attribute21                ,
8134   su.attribute22                ,
8135   su.attribute23                ,
8136   su.attribute24                ,
8137   su.attribute25                ,
8138   su.last_accrue_charge_date    ,
8139   su.second_last_accrue_charge_date  ,
8140   su.last_unaccrue_charge_date  ,
8141   su.second_last_unaccrue_chrg_date  ,
8142   su.demand_class_code          ,
8143   su.org_id,
8144   su.tax_header_level_flag      ,
8145   su.tax_rounding_rule          ,
8146   --wh_update_date           ,
8147   su.global_attribute1          ,
8148   su.global_attribute2          ,
8149   su.global_attribute3          ,
8150   su.global_attribute4          ,
8151   su.global_attribute5          ,
8152   su.global_attribute6          ,
8153   su.global_attribute7          ,
8154   su.global_attribute8          ,
8155   su.global_attribute9          ,
8156   su.global_attribute10         ,
8157   su.global_attribute11         ,
8158   su.global_attribute12         ,
8159   su.global_attribute13         ,
8160   su.global_attribute14         ,
8161   su.global_attribute15         ,
8162   su.global_attribute16         ,
8163   su.global_attribute17         ,
8164   su.global_attribute18         ,
8165   su.global_attribute19         ,
8166   su.global_attribute20         ,
8167   su.global_attribute_category  ,
8168   su.primary_salesrep_id        ,
8169   su.finchrg_receivables_trx_id ,
8170   su.dates_negative_tolerance   ,
8171   su.dates_positive_tolerance   ,
8172   su.date_type_preference       ,
8173   su.over_shipment_tolerance    ,
8174   su.under_shipment_tolerance   ,
8175   su.item_cross_ref_pref        ,
8176   su.over_return_tolerance      ,
8177   su.under_return_tolerance     ,
8178   su.ship_sets_include_lines_flag  ,
8179   su.arrivalsets_include_lines_flag  ,
8180   su.sched_date_push_flag       ,
8181   su.invoice_quantity_rule      ,
8182   su.pricing_event              ,
8183   su.gl_id_rec                  ,
8184   su.gl_id_rev                  ,
8185   su.gl_id_tax                  ,
8186   su.gl_id_freight              ,
8187   su.gl_id_clearing             ,
8188   su.gl_id_unbilled             ,
8189   su.gl_id_unearned             ,
8190   su.gl_id_unpaid_rec           ,
8191   su.gl_id_remittance           ,
8192   su.gl_id_factor               ,
8193   su.tax_classification         ,
8194   su.object_version_number      ,
8195   su.created_by_module          ,
8196   su.application_id             ,
8197   req_id
8198 FROM (select distinct duplicate_site_id,customer_merge_header_id,org_id
8199       from ra_customer_merges cm
8200       where cm.process_flag = 'N'
8201       and cm.request_id = req_id
8202       and cm.set_number = set_num ) m,hz_cust_site_uses_all su --SSUptake
8203 WHERE su.site_use_id = duplicate_site_id
8204 AND   su.org_id  = m.org_id; --SSUptake
8205 
8206  arp_message.set_line(SQL%ROWCOUNT||' '|| 'Row(s) inserted in HZ_CUST_SITE_USES_ALL_M');
8207 
8208 ---------Insert into hz_cust_acct_relate_all_m--------------
8209 INSERT INTO hz_cust_acct_relate_all_m(
8210   customer_merge_header_id,
8211   cust_account_id            ,
8212   related_cust_account_id    ,
8213   last_update_date           ,
8214   last_updated_by            ,
8215   creation_date              ,
8216   created_by                 ,
8217   last_update_login          ,
8218   relationship_type          ,
8219   comments                   ,
8220   attribute_category         ,
8221   attribute1                 ,
8222   attribute2                 ,
8223   attribute3                 ,
8224   attribute4                 ,
8225   attribute5                 ,
8226   attribute6                 ,
8227   attribute7                 ,
8228   attribute8                 ,
8229   attribute9                 ,
8230   attribute10                ,
8231   request_id                 ,
8232   program_application_id     ,
8233   program_id                 ,
8234   program_update_date        ,
8235   customer_reciprocal_flag   ,
8236   status                     ,
8237   attribute11                ,
8238   attribute12                ,
8239   attribute13                ,
8240   attribute14                ,
8241   attribute15                ,
8242   org_id                     ,
8243   bill_to_flag               ,
8244   ship_to_flag               ,
8245   object_version_number      ,
8246   created_by_module          ,
8247   application_id             ,
8248   merge_request_id	     ,
8249   cust_acct_relate_id            --bug 7593763
8250 )
8251  SELECT
8252   customer_merge_header_id,
8253   yt.cust_account_id            ,
8254   yt.related_cust_account_id    ,
8255   yt.last_update_date           ,
8256   yt.last_updated_by            ,
8257   yt.creation_date              ,
8258   yt.created_by                 ,
8259   yt.last_update_login          ,
8260   yt.relationship_type          ,
8261   yt.comments                   ,
8262   yt.attribute_category         ,
8263   yt.attribute1                 ,
8267   yt.attribute5                 ,
8264   yt.attribute2                 ,
8265   yt.attribute3                 ,
8266   yt.attribute4                 ,
8268   yt.attribute6                 ,
8269   yt.attribute7                 ,
8270   yt.attribute8                 ,
8271   yt.attribute9                 ,
8272   yt.attribute10                ,
8273   yt.request_id                 ,
8274   yt.program_application_id     ,
8275   yt.program_id                 ,
8276   yt.program_update_date        ,
8277   yt.customer_reciprocal_flag   ,
8278   yt.status                     ,
8279   yt.attribute11                ,
8280   yt.attribute12                ,
8281   yt.attribute13                ,
8282   yt.attribute14                ,
8283   yt.attribute15                ,
8284   yt.org_id                     ,
8285   yt.bill_to_flag               ,
8286   yt.ship_to_flag               ,
8287   yt.object_version_number      ,
8288   yt.created_by_module          ,
8289   yt.application_id             ,
8290   req_id			,
8291   cust_acct_relate_id
8292 FROM (select distinct duplicate_id, customer_merge_header_id,org_id
8293       from ra_customer_merges cm
8294       where cm.process_flag = 'N'
8295       and cm.request_id = req_id
8296       and cm.set_number = set_num
8297       and cm.duplicate_id <> cm.customer_id) m,hz_cust_acct_relate_all yt --SSUptake
8298 WHERE ( yt.cust_account_id = duplicate_id OR
8299         yt.related_cust_account_id = duplicate_id )
8300 AND   m.org_id = yt.org_id	; --SSUptake
8301 
8302  arp_message.set_line(SQL%ROWCOUNT||' '|| 'Row(s) inserted in HZ_CUST_ACCT_RELATE_ALL_M');
8303 
8304 
8305   ---After storing the merge data sucessfully initialize the status to 0
8306   status := 0;
8307 
8308  arp_message.set_line( 'ARP_CMERGE_ARCUS.Merge_History()-' );
8309 
8310 EXCEPTION
8311  WHEN NO_DATA_FOUND THEN
8312   NULL;
8313 
8314  WHEN DUP_VAL_ON_INDEX THEN
8315  arp_message.set_line('Index Violation: ' || SQLERRM );
8316 
8317 
8318  WHEN OTHERS THEN
8319    arp_message.set_error('ARP_CMERGE_ARCUS.Merge_History');
8320    status := -1;
8321 
8322 
8323 END merge_history;
8324 
8325 --4527935
8326 PROCEDURE create_acct_relate(p_cust_acct_relate_id NUMBER, p_customer_id NUMBER, p_cust_account_id NUMBER,p_related_cust_account_id NUMBER,p_rowid ROWID,p_reciprocal_flag boolean)
8327 IS
8328 p_cust_acct_relate_rec HZ_CUST_ACCOUNT_V2PUB.CUST_ACCT_RELATE_REC_TYPE;
8329 x_return_status VARCHAR2(1);
8330 x_msg_count NUMBER;
8331 x_msg_data  varchar2(2000);
8332 l_cust_acct_relate_id HZ_CUST_ACCT_RELATE_ALL.CUST_ACCT_RELATE_ID%TYPE;
8333 BEGIN
8334 HZ_CUST_ACCOUNT_V2PUB.get_cust_acct_relate_rec (
8335 			       FND_API.G_FALSE,
8336 			       p_cust_account_id,
8337 			       p_related_cust_account_id,
8338 			       p_cust_acct_relate_id,
8339 			       p_rowid,
8340 			       p_cust_acct_relate_rec,
8341 			       x_return_status,
8342 			       x_msg_count,
8343 			       x_msg_data
8344 			       );
8345 IF(p_reciprocal_flag) THEN
8346  p_cust_acct_relate_rec.related_cust_account_id := p_customer_id;
8347 ELSE
8348  p_cust_acct_relate_rec.cust_account_id := p_customer_id;
8349 END IF;
8350 p_cust_acct_relate_rec.created_by_module     := 'HZ_TCA_CUSTOMER_MERGE';
8351 p_cust_acct_relate_rec.application_id        :=  hz_utility_v2pub.program_application_id;
8352 l_cust_acct_relate_id := NULL;
8353 -- Call table-handler.
8354     HZ_CUST_ACCT_RELATE_PKG.Insert_Row (
8355         X_CUST_ACCOUNT_ID                       => p_cust_acct_relate_rec.cust_account_id,
8356         X_RELATED_CUST_ACCOUNT_ID               => p_cust_acct_relate_rec.related_cust_account_id,
8357         X_RELATIONSHIP_TYPE                     => p_cust_acct_relate_rec.relationship_type,
8358         X_COMMENTS                              => p_cust_acct_relate_rec.comments,
8359         X_ATTRIBUTE_CATEGORY                    => p_cust_acct_relate_rec.attribute_category,
8360         X_ATTRIBUTE1                            => p_cust_acct_relate_rec.attribute1,
8361         X_ATTRIBUTE2                            => p_cust_acct_relate_rec.attribute2,
8362         X_ATTRIBUTE3                            => p_cust_acct_relate_rec.attribute3,
8363         X_ATTRIBUTE4                            => p_cust_acct_relate_rec.attribute4,
8364         X_ATTRIBUTE5                            => p_cust_acct_relate_rec.attribute5,
8365         X_ATTRIBUTE6                            => p_cust_acct_relate_rec.attribute6,
8366         X_ATTRIBUTE7                            => p_cust_acct_relate_rec.attribute7,
8367         X_ATTRIBUTE8                            => p_cust_acct_relate_rec.attribute8,
8368         X_ATTRIBUTE9                            => p_cust_acct_relate_rec.attribute9,
8369         X_ATTRIBUTE10                           => p_cust_acct_relate_rec.attribute10,
8370         X_CUSTOMER_RECIPROCAL_FLAG              => p_cust_acct_relate_rec.customer_reciprocal_flag,
8371         X_STATUS                                => p_cust_acct_relate_rec.status,
8372         X_ATTRIBUTE11                           => p_cust_acct_relate_rec.attribute11,
8373         X_ATTRIBUTE12                           => p_cust_acct_relate_rec.attribute12,
8374         X_ATTRIBUTE13                           => p_cust_acct_relate_rec.attribute13,
8375         X_ATTRIBUTE14                           => p_cust_acct_relate_rec.attribute14,
8376         X_ATTRIBUTE15                           => p_cust_acct_relate_rec.attribute15,
8377         X_BILL_TO_FLAG                          => p_cust_acct_relate_rec.bill_to_flag,
8378         X_SHIP_TO_FLAG                          => p_cust_acct_relate_rec.ship_to_flag,
8379         X_OBJECT_VERSION_NUMBER                 => 1,
8380         X_CREATED_BY_MODULE                     => p_cust_acct_relate_rec.created_by_module,
8381         X_APPLICATION_ID                        => p_cust_acct_relate_rec.application_id,
8382 	X_ORG_ID				=> p_cust_acct_relate_rec.org_id,
8383 	X_CUST_ACCT_RELATE_ID			=> l_cust_acct_relate_id
8384     );
8385 
8386 END;
8387 
8388 END arp_cmerge_arcus;