mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Client merge
This commit is contained in:
parent
408503ef8a
commit
6efe373c55
@ -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()
|
||||
|
@ -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();
|
||||
|
86
app/Services/Client/Merge.php
Normal file
86
app/Services/Client/Merge.php
Normal file
@ -0,0 +1,86 @@
|
||||
<?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 App\Services\Client;
|
||||
|
||||
use App\Factory\CompanyLedgerFactory;
|
||||
use App\Models\Activity;
|
||||
use App\Models\Client;
|
||||
use App\Models\CompanyGateway;
|
||||
use App\Models\CompanyLedger;
|
||||
use App\Models\GatewayType;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Payment;
|
||||
use App\Services\AbstractService;
|
||||
use App\Utils\Ninja;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
|
||||
class Merge extends AbstractService
|
||||
{
|
||||
|
||||
public function __construct(Client $client)
|
||||
{
|
||||
$this->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();
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user