2017-01-30 21:40:43 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Requests;
|
2016-05-01 22:30:39 +03:00
|
|
|
|
2016-06-06 19:04:24 +03:00
|
|
|
use App\Models\Invoice;
|
2018-07-27 16:14:45 +03:00
|
|
|
use App\Libraries\HistoryUtils;
|
2016-06-06 19:04:24 +03:00
|
|
|
|
2017-01-30 18:05:31 +02:00
|
|
|
class InvoiceRequest extends EntityRequest
|
|
|
|
|
{
|
2016-05-01 22:30:39 +03:00
|
|
|
protected $entityType = ENTITY_INVOICE;
|
|
|
|
|
|
2019-01-30 22:00:26 +11:00
|
|
|
/**
|
2018-07-24 22:30:54 +10:00
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function authorize()
|
|
|
|
|
{
|
|
|
|
|
|
2019-01-30 22:00:26 +11:00
|
|
|
$invoice = parent::entity();
|
|
|
|
|
$entity = $invoice ? $invoice->subEntityType() : ENTITY_INVOICE;
|
|
|
|
|
|
|
|
|
|
switch($entity)
|
|
|
|
|
{
|
|
|
|
|
case ENTITY_INVOICE:
|
|
|
|
|
$crossCloneEntity = ENTITY_QUOTE;
|
|
|
|
|
break;
|
|
|
|
|
case ENTITY_QUOTE:
|
|
|
|
|
$crossCloneEntity = ENTITY_INVOICE;
|
|
|
|
|
break;
|
|
|
|
|
case ENTITY_RECURRING_INVOICE:
|
|
|
|
|
$crossCloneEntity = ENTITY_RECURRING_QUOTE;
|
|
|
|
|
break;
|
|
|
|
|
case ENTITY_RECURRING_QUOTE:
|
|
|
|
|
$crossCloneEntity = ENTITY_RECURRING_INVOICE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-07-24 22:30:54 +10:00
|
|
|
|
2019-01-30 22:00:26 +11:00
|
|
|
if(request()->is('invoices/create*') && $this->user()->can('createEntity', ENTITY_INVOICE))
|
2018-10-05 00:19:01 +10:00
|
|
|
return true;
|
|
|
|
|
|
2019-01-30 22:00:26 +11:00
|
|
|
if(request()->is('recurring_invoices/create*') && $this->user()->can('createEntity', ENTITY_INVOICE))
|
2018-10-05 00:19:01 +10:00
|
|
|
return true;
|
|
|
|
|
|
2019-01-30 22:00:26 +11:00
|
|
|
if(request()->is('quotes/create*') && $this->user()->can('createEntity', ENTITY_QUOTE))
|
2018-10-05 00:19:01 +10:00
|
|
|
return true;
|
|
|
|
|
|
2019-01-30 22:00:26 +11:00
|
|
|
if(request()->is('recurring_quotes/create*') && $this->user()->can('createEntity', ENTITY_QUOTE))
|
|
|
|
|
return true;
|
2018-07-24 22:30:54 +10:00
|
|
|
|
2019-04-30 12:53:13 +02:00
|
|
|
if($invoice && $invoice->isType(INVOICE_TYPE_STANDARD) && request()->is('*invoices/*/edit') && request()->isMethod('put') && $this->user()->can('edit', $invoice))
|
2019-01-30 22:00:26 +11:00
|
|
|
return true;
|
2018-07-24 22:30:54 +10:00
|
|
|
|
2019-04-30 12:53:13 +02:00
|
|
|
if($invoice && $invoice->isType(INVOICE_TYPE_QUOTE) && request()->is('*quotes/*/edit') && request()->isMethod('put') && $this->user()->can('edit', $invoice))
|
2019-01-30 22:00:26 +11:00
|
|
|
return true;
|
2018-07-24 22:30:54 +10:00
|
|
|
|
2019-01-30 22:00:26 +11:00
|
|
|
// allow cross clone quote to invoice
|
2019-04-30 12:53:13 +02:00
|
|
|
if($invoice && $invoice->isType(INVOICE_TYPE_QUOTE) && request()->is('*invoices/*/clone') && request()->isMethod('get') && $this->user()->can('view', $invoice, $crossCloneEntity))
|
2019-01-30 22:00:26 +11:00
|
|
|
return true;
|
2018-07-24 22:30:54 +10:00
|
|
|
|
2019-01-30 22:00:26 +11:00
|
|
|
// allow cross clone invoice to quote
|
2019-04-30 12:53:13 +02:00
|
|
|
if($invoice && $invoice->isType(INVOICE_TYPE_STANDARD) && request()->is('*quotes/*/clone') && request()->isMethod('get') && $this->user()->can('view', $invoice, $crossCloneEntity))
|
2019-01-30 22:00:26 +11:00
|
|
|
return true;
|
2018-07-24 22:30:54 +10:00
|
|
|
|
2019-04-30 12:53:13 +02:00
|
|
|
if($invoice && $invoice->isType(INVOICE_TYPE_STANDARD) && request()->is('*invoices/*') && request()->isMethod('get') && $this->user()->can('view', $invoice, $entity))
|
2019-01-30 22:00:26 +11:00
|
|
|
return true;
|
2018-07-24 22:30:54 +10:00
|
|
|
|
2019-04-30 12:53:13 +02:00
|
|
|
if($invoice && $invoice->isType(INVOICE_TYPE_QUOTE) && request()->is('*quotes/*') && request()->isMethod('get') && $this->user()->can('view', $invoice, $entity))
|
2019-01-30 22:00:26 +11:00
|
|
|
return true;
|
2018-07-24 22:30:54 +10:00
|
|
|
|
2018-07-27 16:58:32 +03:00
|
|
|
if ($invoice) {
|
|
|
|
|
HistoryUtils::trackViewed($invoice);
|
|
|
|
|
}
|
2018-07-27 16:14:45 +03:00
|
|
|
|
2019-01-30 22:00:26 +11:00
|
|
|
return false;
|
2018-07-24 22:30:54 +10:00
|
|
|
}
|
2018-07-27 16:14:45 +03:00
|
|
|
|
2019-01-30 22:00:26 +11:00
|
|
|
|
2016-05-01 22:30:39 +03:00
|
|
|
public function entity()
|
|
|
|
|
{
|
2016-05-01 23:55:13 +03:00
|
|
|
$invoice = parent::entity();
|
2016-06-06 19:04:24 +03:00
|
|
|
|
|
|
|
|
// support loading an invoice by its invoice number
|
|
|
|
|
if ($this->invoice_number && ! $invoice) {
|
|
|
|
|
$invoice = Invoice::scope()
|
|
|
|
|
->whereInvoiceNumber($this->invoice_number)
|
|
|
|
|
->withTrashed()
|
2017-03-20 13:55:38 +02:00
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
if (! $invoice) {
|
|
|
|
|
abort(404);
|
|
|
|
|
}
|
2016-06-06 19:04:24 +03:00
|
|
|
}
|
|
|
|
|
|
2016-05-05 10:15:51 +03:00
|
|
|
// eager load the invoice items
|
|
|
|
|
if ($invoice && ! $invoice->relationLoaded('invoice_items')) {
|
2016-05-01 23:55:13 +03:00
|
|
|
$invoice->load('invoice_items');
|
2016-05-01 22:30:39 +03:00
|
|
|
}
|
2016-06-06 19:04:24 +03:00
|
|
|
|
2016-05-01 23:55:13 +03:00
|
|
|
return $invoice;
|
2016-05-01 22:30:39 +03:00
|
|
|
}
|
2016-06-06 19:04:24 +03:00
|
|
|
}
|