2020-07-28 13:19:51 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-07-28 13:19:51 +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:19:51 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Shop;
|
|
|
|
|
2020-07-28 13:30:11 +02:00
|
|
|
use App\Events\Invoice\InvoiceWasCreated;
|
2020-07-28 13:19:51 +02:00
|
|
|
use App\Factory\InvoiceFactory;
|
|
|
|
use App\Http\Controllers\BaseController;
|
2020-07-28 15:41:56 +02:00
|
|
|
use App\Http\Requests\Shop\StoreShopInvoiceRequest;
|
2020-07-28 13:19:51 +02:00
|
|
|
use App\Models\Client;
|
2020-07-28 15:41:56 +02:00
|
|
|
use App\Models\Company;
|
2020-07-28 13:19:51 +02:00
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\InvoiceInvitation;
|
|
|
|
use App\Repositories\InvoiceRepository;
|
|
|
|
use App\Transformers\InvoiceTransformer;
|
2020-07-28 13:30:11 +02:00
|
|
|
use App\Utils\Ninja;
|
2020-07-28 13:19:51 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Http\Request;
|
2020-10-28 11:10:49 +01:00
|
|
|
use stdClass;
|
2020-07-28 13:19:51 +02:00
|
|
|
|
|
|
|
class InvoiceController extends BaseController
|
|
|
|
{
|
|
|
|
use MakesHash;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-07-28 13:19:51 +02:00
|
|
|
protected $entity_type = Invoice::class;
|
|
|
|
|
|
|
|
protected $entity_transformer = InvoiceTransformer::class;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var InvoiceRepository
|
|
|
|
*/
|
|
|
|
protected $invoice_repo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* InvoiceController constructor.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param InvoiceRepository $invoice_repo The invoice repo
|
2020-07-28 13:19:51 +02:00
|
|
|
*/
|
|
|
|
public function __construct(InvoiceRepository $invoice_repo)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->invoice_repo = $invoice_repo;
|
|
|
|
}
|
|
|
|
|
2020-07-28 15:47:41 +02:00
|
|
|
public function show(Request $request, string $invitation_key)
|
2020-07-28 13:19:51 +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:19:51 +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:19:51 +02:00
|
|
|
$invitation = InvoiceInvitation::with(['invoice'])
|
2020-07-28 13:58:15 +02:00
|
|
|
->where('company_id', $company->id)
|
2020-09-06 11:38:10 +02:00
|
|
|
->where('key', $invitation_key)
|
2020-07-28 13:19:51 +02:00
|
|
|
->firstOrFail();
|
|
|
|
|
|
|
|
return $this->itemResponse($invitation->invoice);
|
|
|
|
}
|
|
|
|
|
2020-07-28 15:41:56 +02:00
|
|
|
public function store(StoreShopInvoiceRequest $request)
|
2020-07-28 13:19:51 +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:19:51 +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
|
|
|
|
|
|
|
app('queue')->createPayloadUsing(function () use ($company) {
|
|
|
|
return ['db' => $company->db];
|
|
|
|
});
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-07-28 13:19:51 +02:00
|
|
|
$client = Client::find($request->input('client_id'));
|
|
|
|
|
2020-07-28 15:41:56 +02:00
|
|
|
$invoice = $this->invoice_repo->save($request->all(), InvoiceFactory::create($company->id, $company->owner()->id));
|
2020-07-28 13:19:51 +02:00
|
|
|
|
|
|
|
$invoice = $invoice->service()->triggeredActions($request)->save();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2021-03-13 07:45:41 +01:00
|
|
|
event(new InvoiceWasCreated($invoice, $company, Ninja::eventVars()));
|
|
|
|
|
2020-07-28 13:19:51 +02:00
|
|
|
return $this->itemResponse($invoice);
|
|
|
|
}
|
|
|
|
}
|