1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/Http/Requests/InvoiceRequest.php

76 lines
2.2 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Http\Requests;
2016-05-01 21:30:39 +02:00
use App\Models\Invoice;
2017-01-30 17:05:31 +01:00
class InvoiceRequest extends EntityRequest
{
2016-05-01 21:30:39 +02:00
protected $entityType = ENTITY_INVOICE;
2018-07-24 14:30:54 +02:00
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
$invoice = parent::entity();
if ($invoice && $invoice->isQuote())
$standardOrRecurringInvoice = ENTITY_QUOTE;
elseif($invoice && $invoice->is_recurring)
$standardOrRecurringInvoice = ENTITY_RECURRING_INVOICE;
else
$standardOrRecurringInvoice = ENTITY_INVOICE;
if(request()->is('invoices/create') && !$this->user()->can('create', ENTITY_INVOICE))
return false;
if(request()->is('recurring_invoices/create') && !$this->user()->can('create', ENTITY_RECURRING_INVOICE))
return false;
if(request()->is('quotes/create') && !$this->user()->can('create', ENTITY_QUOTE))
return false;
if(request()->is('invoices/*/edit') && request()->isMethod('put') && !$this->user()->can('edit', $standardOrRecurringInvoice))
return false;
if(request()->is('quotes/*/edit') && request()->isMethod('put') && !$this->user()->can('edit', ENTITY_QUOTE))
return false;
if(request()->is('invoices/*') && request()->isMethod('get') && !$this->user()->can('view', $standardOrRecurringInvoice))
return false;
if(request()->is('quotes/*') && request()->isMethod('get') && !$this->user()->can('view', ENTITY_QUOTE))
return false;
return true;
}
2016-05-01 21:30:39 +02:00
public function entity()
{
2016-05-01 22:55:13 +02:00
$invoice = parent::entity();
// support loading an invoice by its invoice number
if ($this->invoice_number && ! $invoice) {
$invoice = Invoice::scope()
->whereInvoiceNumber($this->invoice_number)
->withTrashed()
2017-03-20 12:55:38 +01:00
->first();
if (! $invoice) {
abort(404);
}
}
// eager load the invoice items
if ($invoice && ! $invoice->relationLoaded('invoice_items')) {
2016-05-01 22:55:13 +02:00
$invoice->load('invoice_items');
2016-05-01 21:30:39 +02:00
}
2016-05-01 22:55:13 +02:00
return $invoice;
2016-05-01 21:30:39 +02:00
}
}