mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Merging Clients
This commit is contained in:
parent
6efe373c55
commit
b95b80fc32
@ -80,7 +80,9 @@ class ClientService
|
|||||||
|
|
||||||
public function merge(Client $mergable_client)
|
public function merge(Client $mergable_client)
|
||||||
{
|
{
|
||||||
return (new Merge($this->client))->run($mergable_client);
|
$this->client = (new Merge($this->client, $mergable_client))->run();
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function save() :Client
|
public function save() :Client
|
||||||
|
@ -25,38 +25,61 @@ use App\Utils\Traits\MakesHash;
|
|||||||
|
|
||||||
class Merge extends AbstractService
|
class Merge extends AbstractService
|
||||||
{
|
{
|
||||||
|
public $client;
|
||||||
|
|
||||||
public function __construct(Client $client)
|
public $mergable_client;
|
||||||
|
|
||||||
|
public function __construct(Client $client, Client $mergable_client)
|
||||||
{
|
{
|
||||||
$this->client = $client;
|
$this->client = $client;
|
||||||
|
$this->mergable_client = $mergable_client;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function run(Client $mergable_client)
|
public function run()
|
||||||
{
|
{
|
||||||
|
|
||||||
$this->client->balance += $mergable_client->balance;
|
$this->client->balance += $this->mergable_client->balance;
|
||||||
$this->client->paid_to_date += $mergable_client->paid_to_date;
|
$this->client->paid_to_date += $this->mergable_client->paid_to_date;
|
||||||
$this->client->save();
|
$this->client->save();
|
||||||
|
|
||||||
$this->updateLedger($mergable_client->balance);
|
$this->updateLedger($this->mergable_client->balance);
|
||||||
|
|
||||||
$mergable_client->activities()->update(['client_id' => $this->client->id]);
|
$this->mergable_client->activities()->update(['client_id' => $this->client->id]);
|
||||||
$mergable_client->contacts()->update(['client_id' => $this->client->id]);
|
$this->mergable_client->contacts()->update(['client_id' => $this->client->id]);
|
||||||
$mergable_client->gateway_tokens()->update(['client_id' => $this->client->id]);
|
$this->mergable_client->gateway_tokens()->update(['client_id' => $this->client->id]);
|
||||||
$mergable_client->credits()->update(['client_id' => $this->client->id]);
|
$this->mergable_client->credits()->update(['client_id' => $this->client->id]);
|
||||||
$mergable_client->expenses()->update(['client_id' => $this->client->id]);
|
$this->mergable_client->expenses()->update(['client_id' => $this->client->id]);
|
||||||
$mergable_client->invoices()->update(['client_id' => $this->client->id]);
|
$this->mergable_client->invoices()->update(['client_id' => $this->client->id]);
|
||||||
$mergable_client->payments()->update(['client_id' => $this->client->id]);
|
$this->mergable_client->payments()->update(['client_id' => $this->client->id]);
|
||||||
$mergable_client->projects()->update(['client_id' => $this->client->id]);
|
$this->mergable_client->projects()->update(['client_id' => $this->client->id]);
|
||||||
$mergable_client->quotes()->update(['client_id' => $this->client->id]);
|
$this->mergable_client->quotes()->update(['client_id' => $this->client->id]);
|
||||||
$mergable_client->recurring_invoices()->update(['client_id' => $this->client->id]);
|
$this->mergable_client->recurring_invoices()->update(['client_id' => $this->client->id]);
|
||||||
$mergable_client->tasks()->update(['client_id' => $this->client->id]);
|
$this->mergable_client->tasks()->update(['client_id' => $this->client->id]);
|
||||||
$mergable_client->contacts()->update(['client_id' => $this->client->id]);
|
$this->mergable_client->documents()->update(['documentable_id' => $this->client->id]);
|
||||||
$mergable_client->documents()->update(['client_id' => $this->client->id]);
|
|
||||||
|
|
||||||
$mergable_client->forceDelete();
|
nlog("count =" .$this->mergable_client->contacts->count());
|
||||||
|
|
||||||
return $this;
|
/* Loop through contacts an only merge distinct contacts by email */
|
||||||
|
$this->mergable_client->contacts->each(function ($contact){
|
||||||
|
|
||||||
|
$exist = $this->client->contacts->contains(function ($client_contact) use($contact){
|
||||||
|
nlog("{$client_contact->email} == {$contact->email}");
|
||||||
|
return $client_contact->email == $contact->email;
|
||||||
|
});
|
||||||
|
|
||||||
|
if(!$exist)
|
||||||
|
{
|
||||||
|
nlog("doesn't exist - merging");
|
||||||
|
$contact->client_id = $this->client->id;
|
||||||
|
$contact->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
nlog($this->client->contacts->fresh()->count());
|
||||||
|
$this->mergable_client->forceDelete();
|
||||||
|
|
||||||
|
return $this->client;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function updateLedger($adjustment)
|
private function updateLedger($adjustment)
|
||||||
@ -67,8 +90,6 @@ class Merge extends AbstractService
|
|||||||
->orderBy('id', 'DESC')
|
->orderBy('id', 'DESC')
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
$company_ledger = $this->ledger();
|
|
||||||
|
|
||||||
if ($company_ledger) {
|
if ($company_ledger) {
|
||||||
$balance = $company_ledger->balance;
|
$balance = $company_ledger->balance;
|
||||||
}
|
}
|
||||||
@ -76,9 +97,9 @@ class Merge extends AbstractService
|
|||||||
$company_ledger = CompanyLedgerFactory::create($this->client->company_id, $this->client->user_id);
|
$company_ledger = CompanyLedgerFactory::create($this->client->company_id, $this->client->user_id);
|
||||||
$company_ledger->client_id = $this->client->id;
|
$company_ledger->client_id = $this->client->id;
|
||||||
$company_ledger->adjustment = $adjustment;
|
$company_ledger->adjustment = $adjustment;
|
||||||
$company_ledger->notes = "Balance update after merging " . $mergable_client->present()->name();
|
$company_ledger->notes = "Balance update after merging " . $this->mergable_client->present()->name();
|
||||||
$company_ledger->balance = $balance + $adjustment;
|
$company_ledger->balance = $balance + $adjustment;
|
||||||
$company_ledger->activity_id = Activity::UPDATE_CLIENT
|
$company_ledger->activity_id = Activity::UPDATE_CLIENT;
|
||||||
$company_ledger->save();
|
$company_ledger->save();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
180
tests/Feature/Client/ClientMergeTest.php
Normal file
180
tests/Feature/Client/ClientMergeTest.php
Normal file
@ -0,0 +1,180 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoice Ninja (https://invoiceninja.com).
|
||||||
|
*
|
||||||
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
||||||
|
*
|
||||||
|
* @license https://www.elastic.co/licensing/elastic-license
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Tests\Feature\Client;
|
||||||
|
|
||||||
|
use App\DataMapper\ClientSettings;
|
||||||
|
use App\DataMapper\CompanySettings;
|
||||||
|
use App\Http\Livewire\CreditsTable;
|
||||||
|
use App\Models\Account;
|
||||||
|
use App\Models\Client;
|
||||||
|
use App\Models\ClientContact;
|
||||||
|
use App\Models\Company;
|
||||||
|
use App\Models\Credit;
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Utils\Traits\AppSetup;
|
||||||
|
use Faker\Factory;
|
||||||
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
|
use Livewire\Livewire;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class ClientMergeTest extends TestCase
|
||||||
|
{
|
||||||
|
use DatabaseTransactions;
|
||||||
|
use AppSetup;
|
||||||
|
|
||||||
|
private $user;
|
||||||
|
|
||||||
|
private $company;
|
||||||
|
|
||||||
|
private $account;
|
||||||
|
|
||||||
|
public $client;
|
||||||
|
|
||||||
|
private $primary_contact;
|
||||||
|
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->faker = Factory::create();
|
||||||
|
$this->buildCache(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSearchingForContacts()
|
||||||
|
{
|
||||||
|
|
||||||
|
$account = Account::factory()->create();
|
||||||
|
|
||||||
|
$this->user = User::factory()->create([
|
||||||
|
'account_id' => $account->id,
|
||||||
|
'email' => $this->faker->safeEmail
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->company = Company::factory()->create([
|
||||||
|
'account_id' => $account->id
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->client = Client::factory()->create([
|
||||||
|
'user_id' => $this->user->id,
|
||||||
|
'company_id' => $this->company->id
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->primary_contact = ClientContact::factory()->create([
|
||||||
|
'user_id' => $this->user->id,
|
||||||
|
'client_id' => $this->client->id,
|
||||||
|
'company_id' => $this->company->id,
|
||||||
|
'is_primary' => 1,
|
||||||
|
]);
|
||||||
|
|
||||||
|
ClientContact::factory()->count(2)->create([
|
||||||
|
'user_id' => $this->user->id,
|
||||||
|
'client_id' => $this->client->id,
|
||||||
|
'company_id' => $this->company->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
ClientContact::factory()->create([
|
||||||
|
'user_id' => $this->user->id,
|
||||||
|
'client_id' => $this->client->id,
|
||||||
|
'company_id' => $this->company->id,
|
||||||
|
'email' => 'search@gmail.com'
|
||||||
|
]);
|
||||||
|
|
||||||
|
|
||||||
|
$this->assertEquals(4, $this->client->contacts->count());
|
||||||
|
$this->assertTrue($this->client->contacts->contains(function ($contact) {
|
||||||
|
return $contact->email == 'search@gmail.com';
|
||||||
|
}));
|
||||||
|
|
||||||
|
$this->assertFalse($this->client->contacts->contains(function ($contact) {
|
||||||
|
return $contact->email == 'false@gmail.com';
|
||||||
|
}));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMergeClients()
|
||||||
|
{
|
||||||
|
|
||||||
|
$account = Account::factory()->create();
|
||||||
|
|
||||||
|
$user = User::factory()->create([
|
||||||
|
'account_id' => $account->id,
|
||||||
|
'email' => $this->faker->safeEmail
|
||||||
|
]);
|
||||||
|
|
||||||
|
$company = Company::factory()->create([
|
||||||
|
'account_id' => $account->id
|
||||||
|
]);
|
||||||
|
|
||||||
|
$client = Client::factory()->create([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'company_id' => $company->id
|
||||||
|
]);
|
||||||
|
|
||||||
|
$primary_contact = ClientContact::factory()->create([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'client_id' => $client->id,
|
||||||
|
'company_id' => $company->id,
|
||||||
|
'is_primary' => 1,
|
||||||
|
]);
|
||||||
|
|
||||||
|
ClientContact::factory()->count(2)->create([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'client_id' => $client->id,
|
||||||
|
'company_id' => $company->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
ClientContact::factory()->create([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'client_id' => $client->id,
|
||||||
|
'company_id' => $company->id,
|
||||||
|
'email' => 'search@gmail.com'
|
||||||
|
]);
|
||||||
|
//4contacts
|
||||||
|
|
||||||
|
$mergable_client = Client::factory()->create([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'company_id' => $company->id
|
||||||
|
]);
|
||||||
|
|
||||||
|
$primary_contact = ClientContact::factory()->create([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'client_id' => $mergable_client->id,
|
||||||
|
'company_id' => $company->id,
|
||||||
|
'is_primary' => 1,
|
||||||
|
]);
|
||||||
|
|
||||||
|
ClientContact::factory()->count(2)->create([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'client_id' => $mergable_client->id,
|
||||||
|
'company_id' => $company->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
ClientContact::factory()->create([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'client_id' => $mergable_client->id,
|
||||||
|
'company_id' => $company->id,
|
||||||
|
'email' => 'search@gmail.com'
|
||||||
|
]);
|
||||||
|
//4 contacts
|
||||||
|
|
||||||
|
$this->assertEquals(4, $client->contacts->count());
|
||||||
|
$this->assertEquals(4, $mergable_client->contacts->count());
|
||||||
|
|
||||||
|
$client = $client->service()->merge($mergable_client)->save();
|
||||||
|
|
||||||
|
// nlog($client->contacts->fresh()->toArray());
|
||||||
|
// $this->assertEquals(7, $client->fresh()->contacts->count());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user