1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-05 18:52:44 +01:00
invoiceninja/app/Http/Controllers/Shop/InvoiceController.php

94 lines
2.8 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
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @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;
use App\Http\Requests\Invoice\StoreInvoiceRequest;
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\CompanyToken;
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;
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.
*
* @param \App\Repositories\InvoiceRepository $invoice_repo The invoice repo
*/
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
if (! $company->enable_shop_api) {
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
{
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) {
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
event(new InvoiceWasCreated($invoice, $company, Ninja::eventVars()));
2020-07-28 13:19:51 +02:00
$invoice = $invoice->service()->triggeredActions($request)->save();
2020-07-28 13:19:51 +02:00
return $this->itemResponse($invoice);
}
}