@@ -454,7 +454,7 @@ class Customer extends Model
454
454
*
455
455
* @var [type]
456
456
*/
457
- protected $ fillable = ['first_name ' , 'last_name ' , 'company ' , 'job_title ' , 'address ' , 'city ' , 'state ' , 'zip ' , 'country ' , 'photo_url ' , 'age ' , ' gender ' , ' notes ' , 'channel ' , 'channel_id ' , 'social_profiles ' ];
457
+ protected $ fillable = ['first_name ' , 'last_name ' , 'company ' , 'job_title ' , 'address ' , 'city ' , 'state ' , 'zip ' , 'country ' , 'photo_url ' , 'notes ' , 'channel ' , 'channel_id ' , 'social_profiles ' ];
458
458
459
459
/**
460
460
* Fields stored as JSON.
@@ -1287,6 +1287,84 @@ public static function getPhotoUrlByFileName($file_name)
1287
1287
return Storage::url (self ::PHOTO_DIRECTORY .DIRECTORY_SEPARATOR .$ file_name );
1288
1288
}
1289
1289
1290
+ /**
1291
+ * Merge customerю
1292
+ */
1293
+ public function mergeWith (Customer $ customer2 )
1294
+ {
1295
+ $ user = auth ()->user ();
1296
+
1297
+ $ customer2 ->conversations ()->update (['customer_id ' => $ this ->id ]);
1298
+
1299
+ // do {
1300
+ // $conversations = $customer2->conversations()->limit(1000);
1301
+ // foreach ($conversations as $conversation) {
1302
+ // $conversation->changeCustomer($customer_email, $this, $user);
1303
+ // }
1304
+ // } while (count($conversations) > 0);
1305
+
1306
+ // Move emails.
1307
+ $ customer2 ->emails ()->update (['customer_id ' => $ this ->id ]);
1308
+
1309
+ // Merge attributes
1310
+ // if (in_array('phone', $keepAttributes) && !$this->phone) {
1311
+ // $this->phone = $customerToMerge->phone;
1312
+ // }
1313
+ foreach ($ this ->fillable as $ attr_name ) {
1314
+ // Skip some attribues.
1315
+ if (in_array ($ attr_name , ['channel ' , 'channel_id ' ])) {
1316
+ continue ;
1317
+ }
1318
+ if (!$ this ->$ attr_name ) {
1319
+ $ this ->$ attr_name = $ customer2 ->$ attr_name ;
1320
+ }
1321
+ }
1322
+
1323
+ // Merge Phones.
1324
+ $ phones = self ::mergeTypeValueLists ($ this ->getPhones (), $ customer2 ->getPhones ());
1325
+ if (count ($ phones ) != count ($ this ->getPhones ())) {
1326
+ $ this ->setPhones ($ phones );
1327
+ }
1328
+
1329
+ // Merge websites.
1330
+ $ websites = array_merge ($ this ->getWebsites (), $ customer2 ->getWebsites ());
1331
+ $ this ->setWebsites (array_unique ($ websites ));
1332
+
1333
+ // Merge social profiles.
1334
+ $ social = self ::mergeTypeValueLists ($ this ->getSocialProfiles (), $ customer2 ->getSocialProfiles ());
1335
+ if (count ($ social ) != count ($ this ->getSocialProfiles ())) {
1336
+ $ this ->setSocialProfiles ($ social );
1337
+ }
1338
+
1339
+ $ this ->save ();
1340
+
1341
+ \Eventy::action ('customer.merged ' , $ this , $ customer2 , $ user );
1342
+
1343
+ $ customer2 ->delete ();
1344
+ }
1345
+
1346
+ public static function mergeTypeValueLists ($ list1 , $ list2 )
1347
+ {
1348
+ foreach ($ list2 as $ data2 ) {
1349
+ if (empty ($ data2 ['type ' ]) || empty ($ data2 ['value ' ])) {
1350
+ continue ;
1351
+ }
1352
+ $ exists = false ;
1353
+ foreach ($ list1 as $ data ) {
1354
+ if (!empty ($ data ['type ' ]) && !empty ($ data ['value ' ])
1355
+ && $ data ['type ' ] == $ data2 ['type ' ] && $ data ['value ' ] == $ data2 ['value ' ]
1356
+ ) {
1357
+ $ exists = true ;
1358
+ break ;
1359
+ }
1360
+ }
1361
+ if (!$ exists ) {
1362
+ $ list1 [] = $ data2 ;
1363
+ }
1364
+ }
1365
+ return $ list1 ;
1366
+ }
1367
+
1290
1368
/**
1291
1369
* Resize and save photo.
1292
1370
*/
0 commit comments