invoiceninja/app/Repositories/ClientRepository.php

146 lines
4.1 KiB
PHP
Raw Permalink Normal View History

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