133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
|
# File 'app/services/merger/customer_merger.rb', line 133
def perform_merge!
return false if @customer_master == @customer_duplicate
raise 'Incompatible catalogs' if @customer_master.catalog_id != @customer_duplicate.catalog_id
previous_state = @customer_duplicate.state
@customer_duplicate.merge_lock!
Customer.transaction do
address_remap = {}
contact_point_remap = {}
contact_remap = {}
tax_exemptions_remap = {}
contact_remap[@customer_duplicate.id] = @customer_master.id
@customer_master.merged_from_ids ||= []
@customer_master.merged_from_ids << @customer_duplicate.id
new_contact = nil
if @customer_master.is_organization? && @customer_duplicate.is_person?
new_contact = self.class.create_contact_from_customer(@customer_duplicate)
@results << "#{@customer_duplicate} is a person, creating as contact of #{@customer_master}"
end
yield(3, 31, 'Merging Contacts') if block_given?
@customer_duplicate.contacts.each do |contact|
if detected_contact_dupe = @customer_master.contacts.detect { |cnt| cnt.name.upcase.strip == contact.name.upcase.strip }
cnt_success, cnt_results = Merger::ContactMerger.new(detected_contact_dupe, contact).perform_merge!
append_results cnt_results if cnt_success
contact_remap[contact.id] = detected_contact_dupe.id
else
append_results "Contact #{contact.id} is being added to Customer id #{@customer_master.id} #{@customer_master.name}"
contact_remap[contact.id] = contact.id
contact.customer_id = @customer_master.id
contact.save!
end
end
yield(4, 31, 'Merging Adresses') if block_given?
@customer_duplicate.addresses.each do |o|
if found_address = @customer_master.addresses.detect { |a| a == o }
@results << "Address #{o.id} will not be moved as it already exists in customer id #{@customer_master.id} as address id #{found_address.id}"
address_remap[o.id] = found_address.id
if o.jde_number
if found_address.jde_number
@warnings << "Address #{found_address.id} with jde number #{found_address.jde_number} cannot be merged with address #{o.id} with jde number #{o.jde_number}. Manual intervention is required to evaluate the surviving jde number, update #{found_address.id} with the final jde number."
else
append_results "Address #{found_address.id} will be updated with Address id #{o.id}'s jde number #{o.jde_number}"
found_address.update_column(:jde_number, o.jde_number)
end
end
else
append_results "Address #{o.id} will be added to Customer #{@customer_master.id}"
address_remap[o.id] = o.id
o.update_column(:party_id, @customer_master.id)
end
end
@customer_master.addresses.each do |address|
address_remap[address.id] = address.id
end
yield(5, 31, 'Merging Contact Points') if block_given?
@customer_duplicate.contact_points.each do |cp|
if detected_dupe = @customer_master.contact_points.detect { |cp2| cp2.detail == cp.detail and cp2.category == cp.category }
append_results "Contact Point #{cp.id} #{cp.category} #{cp.detail} will be ignored as it exists already in Customer #{@customer_master.id}"
contact_point_remap[cp.id] = detected_dupe.id
else
append_results "Contact Point #{cp.id} #{cp.category} #{cp.detail} will be added to Customer #{@customer_master.id}"
contact_point_remap[cp.id] = cp.id
@customer_master.contact_points << cp
end
end
yield(6, 31, 'Merging Opportunities') if block_given?
@customer_duplicate.opportunities.each do |o|
append_results "Opportunity #{o.id} #{o.name} will be moved to Customer #{@customer_master.id}"
o.customer = @customer_master
o.contact_id = contact_remap[o.contact_id]
o.name = "#{o.name} (#{o.created_at.to_fs(:crm_default)})" if @customer_master.opportunities.where('name ILIKE ?', o.name).exists?
raise "Cannot save opportunity id #{o.id}. #{o.errors_to_s}" unless o.save
o.quotes.each do |q|
freeze_quote = q.complete? || q.cancelled?
@results << "Quote #{q.id} #{q.reference_number} will be moved to Customer #{@customer_master.id}"
if q.shipping_address
q.shipping_address_id = address_remap[q.shipping_address_id] if q.shipping_address.try(:party_id) q.shipping_address_id ||= @customer_master.shipping_address_id || @customer_master.main_address.try(:id)
q.deliveries.each do |dq|
dq.update_column(:destination_address_id, q.shipping_address_id)
end
q.deliveries.reload q.do_not_set_totals = freeze_quote raise "Cannot save quote ref #{q.reference_number}. #{q.errors_to_s}" unless q.save
else
q.deliveries.delete_all
end
q.do_not_detect_shipping = true
q.fix_catalog
q.reload
q.do_not_detect_shipping = true
q.do_not_set_totals = freeze_quote
unless freeze_quote
res = q.reset_discount
raise "Cannot reset discount on quote ref #{q.reference_number}. #{q.errors_to_s}" unless res
end
end
end
yield(7, 31, 'Merging Tax Exemptions') if block_given?
@customer_duplicate.tax_exemptions.each do |te|
if master_tax_exemption = @customer_master.tax_exemptions.detect { |te2| te2.state_code == te.state_code && te2.tax_type == te.tax_type }
te.orders.each { |o| o.update_column(:tax_exemption_id, master_tax_exemption.id) }
else @customer_master.tax_exemptions << te
tax_exemptions_remap[te.id] = te.id
end
end
yield(8, 31, 'Merging Orders') if block_given?
@customer_duplicate.orders.each do |o|
append_results "Order #{o.id} #{o.reference_number} will be moved to Customer #{@customer_master.id}"
freeze_order = !o.is_sales_order? || o.invoiced?
o.customer = @customer_master
o.contact_id = contact_remap[o.contact_id]
o.spiff_rep_id = contact_remap[o.spiff_rep_id]
o.shipping_address_id = address_remap[o.shipping_address_id] if o.shipping_address.try(:party_id)
o.shipping_address_id ||= @customer_master.shipping_address_id || @customer_master.main_address.try(:id)
o.do_not_detect_shipping = true
o.non_commissionable = false if !o.invoiced? and @customer_master.primary_sales_rep.present?
o.deliveries.each do |dq|
dq.destination_address_id = o.shipping_address_id
if dq.destination_address.reload and dq.valid?
dq.save!
else
dq.destroy
end
end
o.deliveries.reload o.do_not_set_totals = true if freeze_order
raise "Cannot save order ref #{o.reference_number}. #{o.errors_to_s}" unless o.save
end
yield(9, 31, 'Merging Invoices') if block_given?
invoices = Invoice.where('customer_id = ? or billing_customer_id = ?', @customer_duplicate.id, @customer_duplicate.id)
invoices.each do |i|
append_results "Invoice #{i.id} will be moved to customer #{i.customer_id}"
if i.billing_address.party_id == @customer_duplicate.id i.billing_address_id = address_remap[i.billing_address_id] if i.billing_address && i.billing_address.party_id i.billing_customer_id = @customer_master.id
end
i.shipping_address_id = address_remap[i.shipping_address_id] if i.shipping_address && i.shipping_address.party_id i.customer_id = @customer_master.id
i.do_not_set_totals = true
i.save!
end
yield(10, 31, 'Merging Credit Memos') if block_given?
@customer_duplicate.credit_memos.each do |cm|
if cm.billing_address.party_id == cm.customer_id cm.update_column(:billing_address_id, address_remap[cm.billing_address_id]) unless cm.billing_address.party_id.nil? end
cm.update_column(:billing_customer_id, @customer_master.id) if cm.customer_id == cm.billing_customer_id
cm.update_column(:customer_id, @customer_master.id)
cm.update_column(:shipping_address_id, address_remap[cm.shipping_address_id]) unless cm.shipping_address.party_id.nil? end
yield(11, 31, 'Merging RMAs') if block_given?
@customer_duplicate.rmas.each do |rma|
rma.update_column(:customer_id, @customer_master.id)
rma.update_column(:ship_from_address_id, address_remap[rma.ship_from_address_id]) unless rma.ship_from_address.party_id.nil? end
yield(12, 30, 'Merging Outgoing Payments') if block_given?
@customer_duplicate.outgoing_payments.each do |payment|
payment.update_column(:supplier_id, @customer_master.id)
end
yield(13, 31, 'Merging Vouchers') if block_given?
@customer_duplicate.vouchers.each do |v|
v.update_column(:supplier_id, @customer_master.id)
end
yield(14, 31, 'Merging Receipts') if block_given?
@customer_duplicate.receipts.each do |r|
r.update_column(:customer_id, @customer_master.id)
end
yield(15, 31, 'Merging Purchase Orders') if block_given?
@customer_duplicate.purchase_orders.each do |po|
po.update_column(:supplier_id, @customer_master.id)
end
yield(16, 31, 'Merging Credit Applications') if block_given?
@customer_duplicate.credit_applications.each do |ca|
ca.update_column(:customer_id, @customer_master.id)
end
praises = Praise.where(originating_party_id: @customer_duplicate.id)
praises.update_all(originating_party_id: @customer_master.id)
yield(18, 31, 'Merging Survey Enrollments') if block_given?
@customer_duplicate.survey_enrollments.each do |se|
se.update_column(:party_id, @customer_master.id)
se.customer_topics.each do |ct|
ct.update_column(:party_id, @customer_master.id)
end
end
yield(19, 31, 'Merging Courses, Certification and Liability Insurance') if block_given?
@customer_duplicate.course_enrollments.each do |ce|
ce.update_column(:party_id, @customer_master.id)
ce.customer_topics.each do |cet|
cet.update_column(:party_id, @customer_master.id)
end
end
@customer_duplicate.certifications.each do |cert|
cert.update_column(:party_id, @customer_master.id)
end
@customer_duplicate.liability_insurances.each do |l|
l.update_column(:customer_id, @customer_master.id)
end
yield(20, 31, 'Merging Support Case Participants') if block_given?
@customer_duplicate.support_case_participants.each do |scp|
old_party_id = scp.party_id
role = SupportCaseParticipant::ROLES.detect { |k, v| v == @customer_master.profile_id && k != 'iso' && k != 'unknown' }&.first || 'unknown'
if new_contact.present?
scp.update_columns(party_id: new_contact.id)
Communication.where(recipient_party_id: old_party_id).update_all(recipient_party_id: new_contact.id)
elsif existing_scp = scp.support_case.support_case_participants.where(party_id: @customer_master.id).first
existing_scp.email ||= scp.email
existing_scp.phone ||= scp.phone
existing_scp.fax ||= scp.fax
existing_scp.save! if existing_scp.changed?
existing_scp.update_column(:role, role)
Communication.where(resource_type: 'SupportCaseParticipant', resource_id: scp.id).update_all(resource_id: existing_scp.id)
Communication.where(recipient_party_id: old_party_id).update_all(recipient_party_id: existing_scp.party_id)
scp.destroy
else
scp.update_columns(party_id: @customer_master.id, role: role)
Communication.where(recipient_party_id: old_party_id).update_all(recipient_party_id: @customer_master.id)
end
end
yield(21, 31, 'Merging Opportunity Participants') if block_given?
@customer_duplicate.opportunity_participants.update_all(party_id: @customer_master.id)
yield(22, 31, 'Merging Activities') if block_given?
Activity.where(party_id: contact_remap.keys).each do |a|
append_results "Customer Activity #{a.id} will be moved to Customer #{@customer_master.id}"
a.update(
party_id: contact_remap[a.party_id],
customer_id: @customer_master.id,
notes: a.notes.presence || a.description || '-'
)
end
customer_activities = Activity.where(customer_id: @customer_duplicate.id)
customer_activities.update_all(customer_id: @customer_master.id)
@customer_duplicate.child_organizations.each do |co|
new_parent = nil
new_parent = @customer_master.id if co.id != @customer_master.id
co.update_column(:parent_id, new_parent)
append_results "Child Org id #{co.id} will be moved to #{new_parent}"
end
yield(23, 31, 'Merging Stored Cards') if block_given?
if !@customer_master.credit_card_vaults.any? and @customer_duplicate.credit_card_vaults.any?
@customer_master.stripe_customer_id = @customer_duplicate.stripe_customer_id
@customer_duplicate.credit_card_vaults.each { |ccv| @customer_master.credit_card_vaults << ccv }
end
@customer_duplicate.shipping_account_numbers.each do |san|
@customer_master.shipping_account_numbers << san unless @customer_master.shipping_account_numbers.detect do |san2|
san2.account_number = san.account_number and san2.carrier = san.carrier
end
end
@customer_duplicate.identification_numbers.each { |idn| @customer_master.identification_numbers << idn unless @customer_master.identification_numbers.detect { |idn2| idn2.number == idn.number and idn2.category == idn.category } }
@customer_duplicate.notification_channels.each do |nc|
@customer_master.notification_channels << nc unless @customer_master.notification_channels.detect do |nc2|
nc2.notification_type == nc.notification_type and nc2.contact_point_id == nc.contact_point_id
end
end
@customer_duplicate.quick_estimators.each { |qe| @customer_master.quick_estimators << qe }
@customer_master.data_import_row = @customer_duplicate.data_import_row
if @customer_duplicate.accounts.present?
append_results "Accounts #{@customer_duplicate.account_ids} were relocated to customer id #{@customer_master.id}. "
@customer_duplicate.accounts.update_all(party_id: @customer_master.id)
end
@customer_master.profile_id ||= @customer_duplicate.profile_id
@customer_master.buying_group_id ||= @customer_duplicate.buying_group_id
@customer_master.tier2_program_pricing = @customer_duplicate.tier2_program_pricing if @customer_duplicate.pricing_program_discount > @customer_master.pricing_program_discount
if @customer_master.parent_id.nil? && @customer_duplicate.parent_id && @customer_duplicate.parent_id != @customer_master.id
@customer_master.parent_id = @customer_duplicate.parent_id
if @customer_duplicate.billing_address.party == @customer_duplicate.parent_id
@customer_master.billing_address == @customer_duplicate.billing_address
end
else
@customer_duplicate.parent_id = nil
end
yield(24, 31, 'Merging Payments') if block_given?
@customer_duplicate.payments.each do |payment|
payment.update_column(:customer_id, @customer_master.id)
end
CustomerDropEvent.where(customer_id: @customer_duplicate).each do |customer_drop_event|
customer_drop_event.update_column(:customer_id, @customer_master.id)
end
ExportedCatalogItemPacket.where(customer_id: @customer_duplicate).each do |exported_catalog_item_packet|
exported_catalog_item_packet.update_column(:customer_id, @customer_master.id)
end
yield(25, 31, 'Merging Locator Record') if block_given?
if @customer_duplicate.locator_record && @customer_master.locator_record.nil?
if @customer_duplicate.locator_record.confirmed?
locator_record = @customer_duplicate.locator_record
locator_record.customer = @customer_master
locator_record.address_id = address_remap[@customer_master.locator_record.address_id]
locator_record.save!
else @customer_duplicate.locator_record.destroy
end
end
@customer_duplicate.preset_jobs.each do |pj|
append_results "Preset Job #{pj.id} #{pj.name} will be moved to Customer #{@customer_master.id}"
pj.customer = @customer_master
pj.contact_id = contact_remap[pj.contact_id]
pj.save!
end
LocatorBlackListParty.where(customer_id: @customer_duplicate).each do |locator_black_list_party|
locator_black_list_party.update_column(:customer_id, @customer_master.id)
end
LocatorWhiteListParty.where(customer_id: @customer_duplicate).each do |locator_white_list_party|
locator_white_list_party.update_column(:customer_id, @customer_master.id)
end
yield(26, 31, 'Merging Visits') if block_given?
@customer_duplicate.visits.update_all(user_id: @customer_master.id)
SchedulerBooking.where(party_id: @customer_duplicate.id).update_all(party_id: @customer_master.id)
@customer_master.feeds += @customer_duplicate.feeds
@customer_duplicate.reload
@customer_master.attributes = @customer_duplicate.attributes.merge(@customer_master.attributes) { |k, oldval, newval| newval.presence || oldval }
@customer_master.store_original_source
if @customer_duplicate.source_id.present? && !@customer_duplicate.source.unknown_source?
@customer_master.source = @customer_duplicate.source unless @customer_master.source_locked?
end
if @customer_duplicate.consent_preferences.present?
if @customer_master.consent_preferences.blank?
@customer_master.consent_preferences = @customer_duplicate.consent_preferences
append_results 'Consent preferences copied from duplicate customer'
elsif @customer_duplicate.consent_updated_at.present? &&
(@customer_master.consent_updated_at.blank? || @customer_duplicate.consent_updated_at > @customer_master.consent_updated_at)
@customer_master.consent_preferences = @customer_duplicate.consent_preferences
append_results 'Consent preferences updated from duplicate customer (more recent)'
else
append_results 'Consent preferences retained from master customer'
end
end
if @customer_master.lead_qualify?
@customer_master.state = 'lead' end
yield(27, 31, 'Merging Sales Commissions') if block_given?
SalesCommissionNetBaseDetail.where(customer_id: @customer_duplicate).each do |sales_detail|
sales_detail.update_columns(customer_id: @customer_master.id, customer_name: @customer_master.full_name)
end
update_sales_commission_detail = <<-SQL
update sales_commission_details set customer_id = #{@customer_master.id}, customer_name = (select full_name from parties where parties.id = #{@customer_master.id}) where customer_id = #{@customer_duplicate.id};
SQL
ActiveRecord::Base.connection.execute(update_sales_commission_detail)
@customer_duplicate.service_jobs.update_all(customer_id: @customer_master.id)
@customer_duplicate.showcases.update_all(customer_id: @customer_master.id)
@customer_duplicate.statement_of_accounts.update_all(customer_id: @customer_master.id)
@customer_duplicate.votes.update_all(customer_id: @customer_master.id)
yield(28, 31, 'Merging Subscriber data') if block_given?
duplicate_subscribers = Subscriber.where(customer_id: @customer_duplicate.id)
duplicate_subscribers.each do |dsl|
master_subscriber = Subscriber.where(customer_id: @customer_master.id).where(subscriber_list_id: dsl.subscriber_list_id)
if master_subscriber.present?
master_subscriber.update_all(active: dsl.active)
dsl.destroy
else
dsl.update_column(:customer_id, @customer_master.id)
end
end
yield(29, 31, 'Merging Room Plans') if block_given?
@customer_duplicate.room_plans.update_all(party_id: @customer_master.id)
append_results merge_destination_call_records(@customer_duplicate, @customer_master)
append_results merge_origin_call_records(@customer_duplicate, @customer_master)
append_results merge_origin_call_logs(@customer_duplicate, @customer_master)
append_results merge_destination_call_logs(@customer_duplicate, @customer_master)
append_results merge_queue_call_logs(@customer_duplicate, @customer_master)
append_results merge_inbound_communications(@customer_duplicate, @customer_master)
append_results merge_party_topics(@customer_duplicate, @customer_master)
append_results merge_sms_messages(@customer_duplicate, @customer_master)
append_results "Other changes: \n #{@customer_master.changes.to_yaml}"
if @customer_master.profile_image_id.present? && !Image.exists?(@customer_master.profile_image_id)
@customer_master.profile_image_id = nil
end
@customer_master.save!
@customer_master.reload
Pricing::DiscountLevelChangedHandler.call_for(@customer_master)
@customer_master.activities.create(new_note: "Customer Merge Results.\n#{@results.join('\n')}\n\nWarnings: #{@warnings.join('\n')}")
@customer_duplicate.reload
@customer_duplicate.activities.reload
detach_shared_profile_image(@customer_duplicate)
raise "Could not complete merge, duplicate cannot be deleted: #{@customer_duplicate.errors_to_s}" unless @customer_duplicate.destroy
@customer_master.activities.open_activities.each do |a|
a.auto_assign
a.save!
end
end
begin
@customer_duplicate.update_column(:state, previous_state)
rescue StandardError
nil
end yield(30, 31, 'Removing Duplicate Addresses') if block_given?
am = Merger::AddressMerger.new(@customer_master.id)
am.perform_merge!
yield(31, 31, 'Removing Duplicate Contact Points') if block_given?
cpm = Merger::ContactPointMerge.new(@customer_master.id)
cpm.perform_merge!
@customer_master
end
|