From 6efe373c55b6be7c78322686cb4661a2defd0b0b Mon Sep 17 00:00:00 2001 From: = Date: Sun, 1 Aug 2021 15:46:40 +1000 Subject: [PATCH] Client merge --- app/Models/Client.php | 26 +++++++- app/Services/Client/ClientService.php | 6 ++ app/Services/Client/Merge.php | 86 +++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 app/Services/Client/Merge.php diff --git a/app/Models/Client.php b/app/Models/Client.php index 5bbfcc8af071..894d928a036d 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -15,7 +15,11 @@ use App\DataMapper\ClientSettings; use App\DataMapper\CompanySettings; use App\DataMapper\FeesAndLimits; use App\Models\CompanyGateway; +use App\Models\Expense; use App\Models\Presenters\ClientPresenter; +use App\Models\Project; +use App\Models\Quote; +use App\Models\Task; use App\Services\Client\ClientService; use App\Utils\Traits\AppSetup; use App\Utils\Traits\GeneratesCounter; @@ -153,6 +157,16 @@ class Client extends BaseModel implements HasLocalePreference return $this->hasMany(ClientGatewayToken::class); } + public function expenses() + { + return $this->hasMany(Expense::class)->withTrashed(); + } + + public function projects() + { + return $this->hasMany(Project::class)->withTrashed(); + } + /** * Retrieves the specific payment token per * gateway - per payment method. @@ -217,6 +231,16 @@ class Client extends BaseModel implements HasLocalePreference return $this->hasMany(Invoice::class)->withTrashed(); } + public function quotes() + { + return $this->hasMany(Quote::class)->withTrashed(); + } + + public function tasks() + { + return $this->hasMany(Task::class)->withTrashed(); + } + public function recurring_invoices() { return $this->hasMany(RecurringInvoice::class)->withTrashed(); @@ -774,7 +798,7 @@ class Client extends BaseModel implements HasLocalePreference public function payments() { - return $this->hasMany(Payment::class); + return $this->hasMany(Payment::class)->withTrashed(); } public function timezone_offset() diff --git a/app/Services/Client/ClientService.php b/app/Services/Client/ClientService.php index 2355639f70d2..12cbcf7b3a11 100644 --- a/app/Services/Client/ClientService.php +++ b/app/Services/Client/ClientService.php @@ -12,6 +12,7 @@ namespace App\Services\Client; use App\Models\Client; +use App\Services\Client\Merge; use App\Services\Client\PaymentMethod; use App\Utils\Number; use Illuminate\Database\Eloquent\Collection; @@ -77,6 +78,11 @@ class ClientService return (new PaymentMethod($this->client, $amount))->run(); } + public function merge(Client $mergable_client) + { + return (new Merge($this->client))->run($mergable_client); + } + public function save() :Client { $this->client->save(); diff --git a/app/Services/Client/Merge.php b/app/Services/Client/Merge.php new file mode 100644 index 000000000000..03fe5b866d5c --- /dev/null +++ b/app/Services/Client/Merge.php @@ -0,0 +1,86 @@ +client = $client; + } + + public function run(Client $mergable_client) + { + + $this->client->balance += $mergable_client->balance; + $this->client->paid_to_date += $mergable_client->paid_to_date; + $this->client->save(); + + $this->updateLedger($mergable_client->balance); + + $mergable_client->activities()->update(['client_id' => $this->client->id]); + $mergable_client->contacts()->update(['client_id' => $this->client->id]); + $mergable_client->gateway_tokens()->update(['client_id' => $this->client->id]); + $mergable_client->credits()->update(['client_id' => $this->client->id]); + $mergable_client->expenses()->update(['client_id' => $this->client->id]); + $mergable_client->invoices()->update(['client_id' => $this->client->id]); + $mergable_client->payments()->update(['client_id' => $this->client->id]); + $mergable_client->projects()->update(['client_id' => $this->client->id]); + $mergable_client->quotes()->update(['client_id' => $this->client->id]); + $mergable_client->recurring_invoices()->update(['client_id' => $this->client->id]); + $mergable_client->tasks()->update(['client_id' => $this->client->id]); + $mergable_client->contacts()->update(['client_id' => $this->client->id]); + $mergable_client->documents()->update(['client_id' => $this->client->id]); + + $mergable_client->forceDelete(); + + return $this; + } + + private function updateLedger($adjustment) + { + $balance = 0; + + $company_ledger = CompanyLedger::whereClientId($this->client->id) + ->orderBy('id', 'DESC') + ->first(); + + $company_ledger = $this->ledger(); + + if ($company_ledger) { + $balance = $company_ledger->balance; + } + + $company_ledger = CompanyLedgerFactory::create($this->client->company_id, $this->client->user_id); + $company_ledger->client_id = $this->client->id; + $company_ledger->adjustment = $adjustment; + $company_ledger->notes = "Balance update after merging " . $mergable_client->present()->name(); + $company_ledger->balance = $balance + $adjustment; + $company_ledger->activity_id = Activity::UPDATE_CLIENT + $company_ledger->save(); + + } + +} \ No newline at end of file