2018-11-22 12:12:41 +01:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @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)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-05-11 05:32:07 +02:00
|
|
|
*/
|
2018-11-22 12:12:41 +01:00
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2019-12-17 11:58:23 +01:00
|
|
|
use App\Factory\ClientFactory;
|
2018-11-27 07:59:16 +01:00
|
|
|
use App\Models\Client;
|
2022-03-29 23:05:42 +02:00
|
|
|
use App\Models\Company;
|
2019-05-27 12:48:52 +02:00
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
2020-06-22 13:36:39 +02:00
|
|
|
use App\Utils\Traits\SavesDocuments;
|
2022-05-17 12:31:43 +02:00
|
|
|
use Illuminate\Database\QueryException;
|
2018-11-22 12:12:41 +01:00
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* ClientRepository.
|
2018-11-22 12:12:41 +01:00
|
|
|
*/
|
|
|
|
class ClientRepository extends BaseRepository
|
|
|
|
{
|
2019-05-27 12:48:52 +02:00
|
|
|
use GeneratesCounter;
|
2020-06-22 13:36:39 +02:00
|
|
|
use SavesDocuments;
|
|
|
|
|
2022-05-17 12:31:43 +02:00
|
|
|
private bool $completed = true;
|
|
|
|
|
2019-04-16 07:28:30 +02:00
|
|
|
/**
|
|
|
|
* @var ClientContactRepository
|
|
|
|
*/
|
2019-05-10 08:08:33 +02:00
|
|
|
protected $contact_repo;
|
2019-04-16 07:28:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ClientController constructor.
|
2019-05-10 08:08:33 +02:00
|
|
|
* @param ClientContactRepository $contact_repo
|
2019-04-16 07:28:30 +02:00
|
|
|
*/
|
2019-05-10 08:08:33 +02:00
|
|
|
public function __construct(ClientContactRepository $contact_repo)
|
2019-04-16 07:28:30 +02:00
|
|
|
{
|
2019-05-10 08:08:33 +02:00
|
|
|
$this->contact_repo = $contact_repo;
|
2019-04-16 07:28:30 +02:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Saves the client and its contacts.
|
2019-05-10 08:08:33 +02:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param array $data The data
|
|
|
|
* @param Client $client The client
|
2019-05-10 08:08:33 +02:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return Client|Client|null Client Object
|
2020-03-21 06:37:30 +01:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @throws \Laracasts\Presenter\Exceptions\PresenterException
|
2019-05-10 08:08:33 +02:00
|
|
|
*/
|
|
|
|
public function save(array $data, Client $client) : ?Client
|
2019-12-30 22:59:12 +01:00
|
|
|
{
|
2022-11-30 01:46:02 +01:00
|
|
|
$contact_data = $data;
|
|
|
|
unset($data['contacts']);
|
2019-05-10 08:08:33 +02:00
|
|
|
|
2020-11-09 11:17:20 +01:00
|
|
|
/* When uploading documents, only the document array is sent, so we must return early*/
|
2022-06-21 11:57:17 +02:00
|
|
|
if (array_key_exists('documents', $data) && count($data['documents']) >= 1) {
|
2020-11-09 11:17:20 +01:00
|
|
|
$this->saveDocuments($data['documents'], $client);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-11-09 11:17:20 +01:00
|
|
|
return $client;
|
|
|
|
}
|
2019-05-10 08:08:33 +02:00
|
|
|
|
2020-11-09 11:17:20 +01:00
|
|
|
$client->fill($data);
|
2022-03-24 09:55:57 +01:00
|
|
|
|
2022-04-03 11:39:55 +02:00
|
|
|
if (array_key_exists('settings', $data)) {
|
2022-11-30 01:46:02 +01:00
|
|
|
$client->settings = $client->saveSettings($data['settings'], $client);
|
2022-04-03 11:39:55 +02:00
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $client->country_id) {
|
2022-03-29 23:05:42 +02:00
|
|
|
$company = Company::find($client->company_id);
|
|
|
|
$client->country_id = $company->settings->country_id;
|
2022-03-25 04:49:51 +01:00
|
|
|
}
|
2022-03-24 09:55:57 +01:00
|
|
|
|
2022-11-30 01:46:02 +01:00
|
|
|
$client->save();
|
2022-03-25 04:49:51 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! isset($client->number) || empty($client->number) || strlen($client->number) == 0) {
|
|
|
|
$x = 1;
|
2022-05-17 12:31:43 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
do {
|
|
|
|
try {
|
2022-05-17 13:03:07 +02:00
|
|
|
$client->number = $this->getNextClientNumber($client);
|
|
|
|
$client->saveQuietly();
|
2022-05-17 12:31:43 +02:00
|
|
|
|
2022-05-17 13:03:07 +02:00
|
|
|
$this->completed = false;
|
2022-06-21 11:57:17 +02:00
|
|
|
} catch (QueryException $e) {
|
2022-05-17 13:03:07 +02:00
|
|
|
$x++;
|
2022-05-17 12:31:43 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($x > 10) {
|
2022-05-17 13:03:07 +02:00
|
|
|
$this->completed = false;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-05-17 12:31:43 +02:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
} while ($this->completed);
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2019-05-10 08:08:33 +02:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
if (empty($data['name'])) {
|
2020-11-09 11:17:20 +01:00
|
|
|
$data['name'] = $client->present()->name();
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2021-06-10 12:18:01 +02:00
|
|
|
|
2023-01-23 21:42:15 +01:00
|
|
|
//24-01-2023 when a logo is uploaded, no other data is set, so we need to catch here and not update
|
|
|
|
//the contacts array UNLESS there are no contacts and we need to maintain state.
|
2023-02-16 02:36:09 +01:00
|
|
|
if (array_key_exists('contacts', $contact_data) || $client->contacts()->count() == 0) {
|
2023-01-23 21:38:57 +01:00
|
|
|
$this->contact_repo->save($contact_data, $client);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2019-12-17 11:58:23 +01:00
|
|
|
|
2019-01-26 22:24:16 +01:00
|
|
|
return $client;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2018-11-22 12:12:41 +01:00
|
|
|
|
2019-12-17 11:58:23 +01:00
|
|
|
/**
|
|
|
|
* Store clients in bulk.
|
|
|
|
*
|
|
|
|
* @param array $client
|
|
|
|
* @return Client|null
|
|
|
|
*/
|
2019-12-30 22:59:12 +01:00
|
|
|
public function create($client): ?Client
|
2019-12-17 11:58:23 +01:00
|
|
|
{
|
2023-05-25 08:41:29 +02:00
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
|
2019-12-17 11:58:23 +01:00
|
|
|
return $this->save(
|
2019-12-30 22:59:12 +01:00
|
|
|
$client,
|
2023-05-25 08:41:29 +02:00
|
|
|
ClientFactory::create($user->company()->id, $user->id)
|
2019-12-17 11:58:23 +01:00
|
|
|
);
|
|
|
|
}
|
2022-01-30 00:46:39 +01:00
|
|
|
|
|
|
|
public function purge($client)
|
|
|
|
{
|
|
|
|
$client->contacts()->forceDelete();
|
|
|
|
$client->tasks()->forceDelete();
|
|
|
|
$client->invoices()->forceDelete();
|
|
|
|
$client->ledger()->forceDelete();
|
|
|
|
$client->gateway_tokens()->forceDelete();
|
|
|
|
$client->projects()->forceDelete();
|
|
|
|
$client->credits()->forceDelete();
|
|
|
|
$client->quotes()->forceDelete();
|
2023-07-16 08:36:52 +02:00
|
|
|
$client->purgeable_activities()->forceDelete();
|
2022-01-30 00:46:39 +01:00
|
|
|
$client->recurring_invoices()->forceDelete();
|
|
|
|
$client->expenses()->forceDelete();
|
|
|
|
$client->recurring_expenses()->forceDelete();
|
|
|
|
$client->system_logs()->forceDelete();
|
|
|
|
$client->documents()->forceDelete();
|
|
|
|
$client->payments()->forceDelete();
|
|
|
|
$client->forceDelete();
|
|
|
|
}
|
2019-12-17 11:58:23 +01:00
|
|
|
}
|