1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Http/Controllers/Shop/ClientController.php

93 lines
2.7 KiB
PHP
Raw Normal View History

2020-07-28 13:30:11 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
2020-07-28 13:30:11 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
2020-07-28 13:30:11 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-07-28 13:30:11 +02:00
*/
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
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-07-28 14:12:33 +02:00
2020-07-28 13:30:11 +02:00
$contact = ClientContact::with('client')
->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
if (! $company->enable_shop_api) {
2020-10-28 11:10:49 +01:00
return response()->json(['message' => 'Shop is disabled', 'errors' => new stdClass], 403);
}
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');
$this->uploadLogo($request->file('company_logo'), $company, $client);
2020-07-28 13:30:11 +02:00
event(new ClientWasCreated($client, $company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
2020-07-28 13:30:11 +02:00
return $this->itemResponse($client);
}
}