1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-18 00:53:10 +01:00
invoiceninja/app/Repositories/ClientRepository.php

92 lines
2.2 KiB
PHP
Raw Normal View History

<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Repositories;
use App\Factory\ClientFactory;
use App\Models\Client;
use App\Utils\Traits\GeneratesCounter;
2020-06-22 13:36:39 +02:00
use App\Utils\Traits\SavesDocuments;
/**
* ClientRepository.
*/
class ClientRepository extends BaseRepository
{
use GeneratesCounter;
2020-06-22 13:36:39 +02:00
use SavesDocuments;
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
}
2020-11-03 14:27:41 +01: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-10-28 11:10:49 +01:00
* @throws \Laracasts\Presenter\Exceptions\PresenterException
* @todo Write tests to make sure that custom client numbers work as expected.
2019-05-10 08:08:33 +02:00
*/
public function save(array $data, Client $client) : ?Client
{
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*/
if (array_key_exists('documents', $data) && count($data['documents']) >=1) {
$this->saveDocuments($data['documents'], $client);
return $client;
}
2019-05-10 08:08:33 +02:00
2020-11-09 11:17:20 +01:00
$client->fill($data);
2021-01-25 11:34:12 +01:00
if (!isset($client->number) || empty($client->number)) {
$client->number = $this->getNextClientNumber($client);
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
}
2020-11-09 11:17:20 +01:00
$client->save();
$this->contact_repo->save($data, $client);
2019-01-26 22:24:16 +01: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)
);
}
}