2018-11-22 12:12:41 +01:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
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;
|
2018-11-22 12:12:41 +01:00
|
|
|
use App\Repositories\ClientContactRepository;
|
2019-05-27 12:48:52 +02:00
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
2018-11-27 07:59:16 +01:00
|
|
|
use Illuminate\Http\Request;
|
2018-11-22 12:12:41 +01:00
|
|
|
|
|
|
|
/**
|
2019-05-10 08:08:33 +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;
|
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
|
|
|
}
|
|
|
|
|
2019-05-10 08:08:33 +02:00
|
|
|
/**
|
|
|
|
* Gets the class name.
|
|
|
|
*
|
|
|
|
* @return string The class name.
|
|
|
|
*/
|
2019-01-19 11:35:21 +01:00
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return Client::class;
|
|
|
|
}
|
2019-05-10 08:08:33 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/**
|
2019-05-10 08:08:33 +02:00
|
|
|
* Saves the client and its contacts
|
|
|
|
*
|
|
|
|
* @param array $data The data
|
|
|
|
* @param \App\Models\Client $client The client
|
|
|
|
*
|
|
|
|
* @return Client|\App\Models\Client|null Client Object
|
|
|
|
*/
|
|
|
|
public function save(array $data, Client $client) : ?Client
|
2019-12-30 22:59:12 +01:00
|
|
|
{
|
2019-05-10 08:08:33 +02:00
|
|
|
$client->fill($data);
|
|
|
|
|
|
|
|
$client->save();
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($client->id_number == "" || !$client->id_number) {
|
|
|
|
$client->id_number = $this->getNextClientNumber($client);
|
|
|
|
} //todo write tests for this and make sure that custom client numbers also works as expected from here
|
2019-05-10 08:08:33 +02:00
|
|
|
|
2018-11-27 07:59:16 +01:00
|
|
|
$client->save();
|
2018-11-22 12:12:41 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (isset($data['contacts'])) {
|
2019-05-10 08:08:33 +02:00
|
|
|
$contacts = $this->contact_repo->save($data['contacts'], $client);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-12-17 11:58:23 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (empty($data['name'])) {
|
2019-11-12 12:36:24 +01:00
|
|
|
$data['name'] = $client->present()->name();
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-11-12 12:36:24 +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
|
|
|
{
|
|
|
|
return $this->save(
|
2019-12-30 22:59:12 +01:00
|
|
|
$client,
|
|
|
|
ClientFactory::create(auth()->user()->company()->id, auth()->user()->id)
|
2019-12-17 11:58:23 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|