2020-07-28 13:30:11 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-07-28 13:30:11 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-07-28 13:30:11 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Shop;
|
|
|
|
|
|
|
|
use App\Events\Client\ClientWasCreated;
|
|
|
|
use App\Factory\ClientFactory;
|
|
|
|
use App\Http\Controllers\BaseController;
|
2020-07-28 15:41:56 +02:00
|
|
|
use App\Http\Requests\Shop\StoreShopClientRequest;
|
2020-07-28 13:30:11 +02:00
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\ClientContact;
|
2020-07-28 14:05:17 +02:00
|
|
|
use App\Models\Company;
|
2020-07-28 13:30:11 +02:00
|
|
|
use App\Repositories\ClientRepository;
|
|
|
|
use App\Transformers\ClientTransformer;
|
|
|
|
use App\Utils\Ninja;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
2020-07-28 15:41:56 +02:00
|
|
|
use App\Utils\Traits\Uploadable;
|
2020-07-28 13:30:11 +02:00
|
|
|
use Illuminate\Http\Request;
|
2020-10-28 11:10:49 +01:00
|
|
|
use stdClass;
|
2020-07-28 13:30:11 +02:00
|
|
|
|
|
|
|
class ClientController extends BaseController
|
|
|
|
{
|
|
|
|
use MakesHash;
|
2020-07-28 15:41:56 +02:00
|
|
|
use Uploadable;
|
|
|
|
|
2020-07-28 13:30:11 +02:00
|
|
|
protected $entity_type = Client::class;
|
|
|
|
|
|
|
|
protected $entity_transformer = ClientTransformer::class;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ClientRepository
|
|
|
|
*/
|
|
|
|
protected $client_repo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ClientController constructor.
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param ClientRepository $client_repo
|
2020-07-28 13:30:11 +02:00
|
|
|
*/
|
|
|
|
public function __construct(ClientRepository $client_repo)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->client_repo = $client_repo;
|
|
|
|
}
|
|
|
|
|
2020-07-28 14:05:17 +02:00
|
|
|
public function show(Request $request, string $contact_key)
|
2020-07-28 13:30:11 +02:00
|
|
|
{
|
2020-07-28 15:24:01 +02:00
|
|
|
$company = Company::where('company_key', $request->header('X-API-COMPANY-KEY'))->first();
|
2020-07-28 13:30:11 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $company->enable_shop_api) {
|
2020-10-28 11:10:49 +01:00
|
|
|
return response()->json(['message' => 'Shop is disabled', 'errors' => new stdClass], 403);
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-07-28 14:12:33 +02:00
|
|
|
|
2020-07-28 13:30:11 +02:00
|
|
|
$contact = ClientContact::with('client')
|
2020-07-28 13:58:15 +02:00
|
|
|
->where('company_id', $company->id)
|
2020-07-28 13:30:11 +02:00
|
|
|
->where('contact_key', $contact_key)
|
|
|
|
->firstOrFail();
|
|
|
|
|
|
|
|
return $this->itemResponse($contact->client);
|
|
|
|
}
|
|
|
|
|
2020-07-28 15:41:56 +02:00
|
|
|
public function store(StoreShopClientRequest $request)
|
2020-07-28 13:30:11 +02:00
|
|
|
{
|
2020-07-28 15:24:01 +02:00
|
|
|
$company = Company::where('company_key', $request->header('X-API-COMPANY-KEY'))->first();
|
2020-07-28 13:30:11 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $company->enable_shop_api) {
|
2020-10-28 11:10:49 +01:00
|
|
|
return response()->json(['message' => 'Shop is disabled', 'errors' => new stdClass], 403);
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
|
|
|
|
2020-07-28 13:58:15 +02:00
|
|
|
app('queue')->createPayloadUsing(function () use ($company) {
|
|
|
|
return ['db' => $company->db];
|
|
|
|
});
|
|
|
|
|
|
|
|
$client = $this->client_repo->save($request->all(), ClientFactory::create($company->id, $company->owner()->id));
|
2020-07-28 13:30:11 +02:00
|
|
|
|
|
|
|
$client->load('contacts', 'primary_contact');
|
|
|
|
|
2020-07-28 13:58:15 +02:00
|
|
|
$this->uploadLogo($request->file('company_logo'), $company, $client);
|
2020-07-28 13:30:11 +02:00
|
|
|
|
2020-07-28 13:58:15 +02:00
|
|
|
event(new ClientWasCreated($client, $company, Ninja::eventVars()));
|
2020-07-28 13:30:11 +02:00
|
|
|
|
|
|
|
return $this->itemResponse($client);
|
|
|
|
}
|
|
|
|
}
|