DBA Data[Home] [Help]

PACKAGE BODY: APPS.ARP_CMERGE_ARCUS

Source


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