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

95 lines
2.9 KiB
PHP
Raw Normal View History

2020-07-28 13:19:51 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
2020-07-28 13:19:51 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2020-07-28 13:19:51 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-07-28 13:19:51 +02:00
*/
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-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
{
2023-08-04 10:13:26 +02:00
/** @var \App\Models\Company $company */
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
if (! $company->enable_shop_api) {
2024-01-14 05:05:00 +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:19:51 +02:00
$invitation = InvoiceInvitation::with(['invoice'])
->where('company_id', $company->id)
->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
{
2023-08-04 10:13:26 +02:00
/** @var \App\Models\Company $company */
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
if (! $company->enable_shop_api) {
2024-01-14 05:05:00 +01:00
return response()->json(['message' => 'Shop is disabled', 'errors' => new stdClass()], 403);
}
2020-07-28 14:12:33 +02:00
app('queue')->createPayloadUsing(function () use ($company) {
return ['db' => $company->db];
});
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();
2021-04-13 05:09:47 +02:00
event(new InvoiceWasCreated($invoice, $company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
2021-03-13 07:45:41 +01:00
2020-07-28 13:19:51 +02:00
return $this->itemResponse($invoice);
}
}