1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/Services/Client/Merge.php

91 lines
3.4 KiB
PHP
Raw Normal View History

2021-08-01 07:46:40 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2021-08-01 07:46:40 +02:00
*
* @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\CompanyLedger;
use App\Services\AbstractService;
class Merge extends AbstractService
{
2021-08-01 09:21:08 +02:00
public $client;
2021-08-01 07:46:40 +02:00
2021-08-01 09:21:08 +02:00
public $mergable_client;
public function __construct(Client $client, Client $mergable_client)
2021-08-01 07:46:40 +02:00
{
$this->client = $client;
2021-08-01 09:21:08 +02:00
$this->mergable_client = $mergable_client;
2021-08-01 07:46:40 +02:00
}
2021-08-01 09:21:08 +02:00
public function run()
2021-08-01 07:46:40 +02:00
{
2021-08-01 09:21:08 +02:00
$this->client->balance += $this->mergable_client->balance;
$this->client->paid_to_date += $this->mergable_client->paid_to_date;
2021-08-01 07:46:40 +02:00
$this->client->save();
2021-08-01 09:21:08 +02:00
$this->updateLedger($this->mergable_client->balance);
$this->mergable_client->activities()->update(['client_id' => $this->client->id]);
$this->mergable_client->contacts()->update(['client_id' => $this->client->id]);
$this->mergable_client->gateway_tokens()->update(['client_id' => $this->client->id]);
$this->mergable_client->credits()->update(['client_id' => $this->client->id]);
$this->mergable_client->expenses()->update(['client_id' => $this->client->id]);
$this->mergable_client->invoices()->update(['client_id' => $this->client->id]);
$this->mergable_client->payments()->update(['client_id' => $this->client->id]);
$this->mergable_client->projects()->update(['client_id' => $this->client->id]);
$this->mergable_client->quotes()->update(['client_id' => $this->client->id]);
$this->mergable_client->recurring_invoices()->update(['client_id' => $this->client->id]);
$this->mergable_client->tasks()->update(['client_id' => $this->client->id]);
$this->mergable_client->documents()->update(['documentable_id' => $this->client->id]);
/* 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) {
return $client_contact->email == $contact->email || empty($contact->email) || $contact->email == ' ';
2021-08-01 09:21:08 +02:00
});
if ($exist) {
2021-08-01 13:03:30 +02:00
$contact->delete();
2021-08-01 09:21:08 +02:00
$contact->save();
}
2021-08-01 09:21:08 +02:00
});
$this->mergable_client->forceDelete();
return $this->client;
2021-08-01 07:46:40 +02:00
}
private function updateLedger($adjustment)
{
$balance = 0;
2023-08-06 09:35:19 +02:00
$company_ledger = CompanyLedger::query()->whereClientId($this->client->id)
2021-08-01 07:46:40 +02:00
->orderBy('id', 'DESC')
->first();
2021-08-01 07:46:40 +02:00
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 '.$this->mergable_client->present()->name();
2021-08-01 07:46:40 +02:00
$company_ledger->balance = $balance + $adjustment;
2021-08-01 09:21:08 +02:00
$company_ledger->activity_id = Activity::UPDATE_CLIENT;
2021-08-01 07:46:40 +02:00
$company_ledger->save();
}
}