2019-03-28 22:34:58 +01:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +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)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2019-03-28 22:34:58 +01:00
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2020-03-30 08:40:21 +02:00
|
|
|
use App\Models\Account;
|
2019-09-29 10:46:53 +02:00
|
|
|
use App\Models\Company;
|
2019-09-27 06:31:13 +02:00
|
|
|
use App\Models\User;
|
2019-03-28 22:34:58 +01:00
|
|
|
use App\Transformers\ArraySerializer;
|
|
|
|
use App\Transformers\EntityTransformer;
|
2020-03-25 09:36:47 +01:00
|
|
|
use App\Utils\Ninja;
|
2019-09-11 02:37:53 +02:00
|
|
|
use App\Utils\Statics;
|
2020-03-18 10:40:15 +01:00
|
|
|
use App\Utils\Traits\AppSetup;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Illuminate\Contracts\Container\BindingResolutionException;
|
2020-11-13 10:09:20 +01:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2019-03-28 22:34:58 +01:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use League\Fractal\Manager;
|
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
|
|
|
use League\Fractal\Resource\Collection;
|
|
|
|
use League\Fractal\Resource\Item;
|
|
|
|
use League\Fractal\Serializer\JsonApiSerializer;
|
|
|
|
|
2019-06-24 02:13:53 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class BaseController.
|
2019-06-24 02:13:53 +02:00
|
|
|
*/
|
2019-03-28 22:34:58 +01:00
|
|
|
class BaseController extends Controller
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
use AppSetup;
|
2019-06-24 02:13:53 +02:00
|
|
|
/**
|
|
|
|
* Passed from the parent when we need to force
|
2019-12-30 22:59:12 +01:00
|
|
|
* includes internally rather than externally via
|
2019-09-11 02:37:53 +02:00
|
|
|
* the $_REQUEST 'include' variable.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-06-24 02:13:53 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $forced_includes;
|
|
|
|
|
2019-06-25 07:08:07 +02:00
|
|
|
/**
|
|
|
|
* Passed from the parent when we need to force
|
2020-09-06 11:38:10 +02:00
|
|
|
* the key of the response object.
|
2019-06-25 07:08:07 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $forced_index;
|
|
|
|
|
2019-06-24 02:13:53 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Fractal manager.
|
2019-06-24 02:13:53 +02:00
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
protected $manager;
|
|
|
|
|
2020-07-26 10:30:55 +02:00
|
|
|
private $first_load = [
|
|
|
|
'account',
|
2020-10-26 01:58:08 +01:00
|
|
|
'user.company_user',
|
|
|
|
'token.company_user',
|
2020-07-26 10:30:55 +02:00
|
|
|
'company.activities',
|
2020-10-20 02:53:54 +02:00
|
|
|
'company.designs.company',
|
2020-10-25 21:56:02 +01:00
|
|
|
'company.task_statuses',
|
|
|
|
'company.expense_categories',
|
2020-10-20 02:53:54 +02:00
|
|
|
'company.documents',
|
2020-12-26 09:03:24 +01:00
|
|
|
//'company.users',
|
|
|
|
'company.users.company_user',
|
2020-10-26 01:58:08 +01:00
|
|
|
'company.clients.contacts.company',
|
2020-08-12 01:04:39 +02:00
|
|
|
'company.clients.gateway_tokens',
|
2020-09-19 04:05:54 +02:00
|
|
|
'company.clients.documents',
|
2020-10-20 02:53:54 +02:00
|
|
|
'company.company_gateways.gateway',
|
|
|
|
'company.credits.invitations.contact',
|
|
|
|
'company.credits.invitations.company',
|
|
|
|
'company.credits.documents',
|
|
|
|
'company.expenses.documents',
|
|
|
|
'company.groups',
|
2020-07-26 10:30:55 +02:00
|
|
|
'company.invoices.invitations.contact',
|
|
|
|
'company.invoices.invitations.company',
|
|
|
|
'company.invoices.documents',
|
2020-10-20 02:53:54 +02:00
|
|
|
'company.products',
|
|
|
|
'company.products.documents',
|
|
|
|
'company.payments.paymentables',
|
|
|
|
'company.payments.documents',
|
|
|
|
'company.payment_terms.company',
|
|
|
|
'company.projects.documents',
|
2020-09-19 04:05:54 +02:00
|
|
|
'company.recurring_invoices',
|
|
|
|
'company.recurring_invoices.invitations.contact',
|
|
|
|
'company.recurring_invoices.invitations.company',
|
|
|
|
'company.recurring_invoices.documents',
|
2020-07-26 10:30:55 +02:00
|
|
|
'company.quotes.invitations.contact',
|
|
|
|
'company.quotes.invitations.company',
|
|
|
|
'company.quotes.documents',
|
2020-10-18 09:46:10 +02:00
|
|
|
'company.tasks.documents',
|
2020-10-20 02:53:54 +02:00
|
|
|
'company.tax_rates',
|
2020-09-06 11:38:10 +02:00
|
|
|
'company.tokens_hashed',
|
2020-10-26 01:58:08 +01:00
|
|
|
'company.vendors.contacts.company',
|
|
|
|
'company.vendors.documents',
|
2020-10-20 02:53:54 +02:00
|
|
|
'company.webhooks',
|
2020-07-26 10:30:55 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
private $mini_load = [
|
|
|
|
'account',
|
|
|
|
'user.company_user',
|
|
|
|
'token',
|
|
|
|
'company.activities',
|
2020-11-13 10:09:20 +01:00
|
|
|
//'company.users.company_user',
|
2020-07-26 10:30:55 +02:00
|
|
|
'company.tax_rates',
|
|
|
|
'company.groups',
|
|
|
|
'company.payment_terms',
|
|
|
|
];
|
2019-03-28 22:34:58 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
public function __construct()
|
2019-03-28 22:34:58 +01:00
|
|
|
{
|
|
|
|
$this->manager = new Manager();
|
|
|
|
|
2019-06-24 02:13:53 +02:00
|
|
|
$this->forced_includes = [];
|
|
|
|
|
2019-06-25 07:08:07 +02:00
|
|
|
$this->forced_index = 'data';
|
2019-06-24 02:13:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function buildManager()
|
|
|
|
{
|
2019-06-24 13:05:47 +02:00
|
|
|
$include = '';
|
2019-06-24 02:13:53 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if (request()->has('first_load') && request()->input('first_load') == 'true') {
|
2020-09-06 11:38:10 +02:00
|
|
|
$include = implode(',', array_merge($this->forced_includes, $this->getRequestIncludes([])));
|
2020-03-21 06:37:30 +01:00
|
|
|
} elseif (request()->input('include') !== null) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$include = array_merge($this->forced_includes, explode(',', request()->input('include')));
|
|
|
|
$include = implode(',', $include);
|
2019-12-30 22:59:12 +01:00
|
|
|
} elseif (count($this->forced_includes) >= 1) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$include = implode(',', $this->forced_includes);
|
2019-06-24 13:05:47 +02:00
|
|
|
}
|
2019-06-24 02:13:53 +02:00
|
|
|
|
|
|
|
$this->manager->parseIncludes($include);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-03-28 22:34:58 +01:00
|
|
|
$this->serializer = request()->input('serializer') ?: EntityTransformer::API_SERIALIZER_ARRAY;
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($this->serializer === EntityTransformer::API_SERIALIZER_JSON) {
|
2019-03-28 22:34:58 +01:00
|
|
|
$this->manager->setSerializer(new JsonApiSerializer());
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2019-03-28 22:34:58 +01:00
|
|
|
$this->manager->setSerializer(new ArraySerializer());
|
2019-06-24 02:13:53 +02:00
|
|
|
}
|
2019-03-28 22:34:58 +01:00
|
|
|
}
|
|
|
|
|
2019-03-30 10:30:41 +01:00
|
|
|
/**
|
2019-12-30 22:59:12 +01:00
|
|
|
* Catch all fallback route
|
2020-09-06 11:38:10 +02:00
|
|
|
* for non-existant route.
|
2019-03-30 10:30:41 +01:00
|
|
|
*/
|
|
|
|
public function notFound()
|
|
|
|
{
|
2019-09-11 02:37:53 +02:00
|
|
|
return response()->json(['message' => '404 | Nothing to see here!'], 404)
|
2020-06-21 23:30:25 +02:00
|
|
|
->header('X-API-VERSION', config('ninja.minimum_client_version'))
|
2019-12-29 23:06:42 +01:00
|
|
|
->header('X-APP-VERSION', config('ninja.app_version'));
|
2019-03-30 10:30:41 +01:00
|
|
|
}
|
|
|
|
|
2020-04-04 12:32:42 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* 404 for the client portal.
|
2020-04-04 12:32:42 +02:00
|
|
|
* @return Response 404 response
|
|
|
|
*/
|
2019-07-17 00:59:09 +02:00
|
|
|
public function notFoundClient()
|
|
|
|
{
|
|
|
|
return abort(404);
|
|
|
|
}
|
|
|
|
|
2020-04-04 12:32:42 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* API Error response.
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param string $message The return error message
|
|
|
|
* @param int $httpErrorCode 404/401/403 etc
|
2020-04-04 12:32:42 +02:00
|
|
|
* @return Response The JSON response
|
2020-10-28 11:10:49 +01:00
|
|
|
* @throws BindingResolutionException
|
2020-04-04 12:32:42 +02:00
|
|
|
*/
|
|
|
|
protected function errorResponse($message, $httpErrorCode = 400)
|
2019-03-28 22:34:58 +01:00
|
|
|
{
|
2020-04-04 12:32:42 +02:00
|
|
|
$error['error'] = $message;
|
2020-02-10 10:53:02 +01:00
|
|
|
|
2019-03-28 22:34:58 +01:00
|
|
|
$error = json_encode($error, JSON_PRETTY_PRINT);
|
2020-02-10 10:53:02 +01:00
|
|
|
|
2019-03-28 22:34:58 +01:00
|
|
|
$headers = self::getApiHeaders();
|
|
|
|
|
|
|
|
return response()->make($error, $httpErrorCode, $headers);
|
|
|
|
}
|
|
|
|
|
2020-07-10 01:28:09 +02:00
|
|
|
protected function refreshResponse($query)
|
|
|
|
{
|
2020-07-26 10:30:55 +02:00
|
|
|
$this->manager->parseIncludes($this->first_load);
|
2020-07-24 11:39:43 +02:00
|
|
|
|
|
|
|
$this->serializer = request()->input('serializer') ?: EntityTransformer::API_SERIALIZER_ARRAY;
|
|
|
|
|
|
|
|
if ($this->serializer === EntityTransformer::API_SERIALIZER_JSON) {
|
|
|
|
$this->manager->setSerializer(new JsonApiSerializer());
|
|
|
|
} else {
|
|
|
|
$this->manager->setSerializer(new ArraySerializer());
|
|
|
|
}
|
2020-07-10 01:28:09 +02:00
|
|
|
|
2020-07-26 07:12:40 +02:00
|
|
|
$transformer = new $this->entity_transformer($this->serializer);
|
2020-07-10 01:28:09 +02:00
|
|
|
$updated_at = request()->has('updated_at') ? request()->input('updated_at') : 0;
|
2020-08-02 08:31:55 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (auth()->user()->getCompany()->is_large && ! request()->has('updated_at')) {
|
|
|
|
return response()->json(['message' => 'Cannot load a large account without a updated_at parameter', 'errors' =>[]], 401);
|
|
|
|
}
|
2020-08-02 08:31:55 +02:00
|
|
|
|
2020-07-10 01:28:09 +02:00
|
|
|
$updated_at = date('Y-m-d H:i:s', $updated_at);
|
|
|
|
|
|
|
|
$query->with(
|
2020-11-25 15:19:52 +01:00
|
|
|
[
|
2020-09-06 11:38:10 +02:00
|
|
|
'company' => function ($query) use ($updated_at) {
|
2020-09-19 12:05:29 +02:00
|
|
|
$query->whereNotNull('updated_at')->with('documents');
|
2020-07-26 12:21:55 +02:00
|
|
|
},
|
2020-09-06 11:38:10 +02:00
|
|
|
'company.clients' => function ($query) use ($updated_at) {
|
2020-11-25 15:19:52 +01:00
|
|
|
$query->where('clients.updated_at', '>=', $updated_at)->with('contacts.company', 'gateway_tokens', 'documents');
|
2020-07-26 12:21:55 +02:00
|
|
|
},
|
2020-09-06 11:38:10 +02:00
|
|
|
'company.company_gateways' => function ($query) {
|
|
|
|
$query->whereNotNull('updated_at');
|
2020-07-26 12:21:55 +02:00
|
|
|
},
|
2020-10-19 12:59:58 +02:00
|
|
|
'company.credits'=> function ($query) use ($updated_at) {
|
2020-11-11 09:55:53 +01:00
|
|
|
$query->where('updated_at', '>=', $updated_at)->with('invitations', 'documents');
|
2020-07-26 12:21:55 +02:00
|
|
|
},
|
2020-10-19 12:59:58 +02:00
|
|
|
'company.designs'=> function ($query) use ($updated_at) {
|
2020-09-10 03:05:42 +02:00
|
|
|
$query->where('updated_at', '>=', $updated_at)->with('company');
|
|
|
|
},
|
2020-10-19 12:59:58 +02:00
|
|
|
'company.documents'=> function ($query) use ($updated_at) {
|
|
|
|
$query->where('updated_at', '>=', $updated_at);
|
2020-07-26 12:21:55 +02:00
|
|
|
},
|
2020-10-19 12:59:58 +02:00
|
|
|
'company.expenses'=> function ($query) use ($updated_at) {
|
2020-11-11 09:55:53 +01:00
|
|
|
$query->where('updated_at', '>=', $updated_at)->with('documents');
|
2020-09-19 04:05:54 +02:00
|
|
|
},
|
2020-10-19 12:59:58 +02:00
|
|
|
'company.groups' => function ($query) use ($updated_at) {
|
|
|
|
$query->where('updated_at', '>=', $updated_at);
|
2020-07-26 12:21:55 +02:00
|
|
|
},
|
2020-10-19 12:59:58 +02:00
|
|
|
'company.invoices'=> function ($query) use ($updated_at) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$query->where('updated_at', '>=', $updated_at)->with('invitations', 'documents');
|
2020-07-26 12:21:55 +02:00
|
|
|
},
|
2020-10-19 12:59:58 +02:00
|
|
|
'company.payments'=> function ($query) use ($updated_at) {
|
2020-11-25 15:19:52 +01:00
|
|
|
$query->where('updated_at', '>=', $updated_at)->with('paymentables', 'documents');
|
2020-07-26 12:21:55 +02:00
|
|
|
},
|
2020-09-06 11:38:10 +02:00
|
|
|
'company.payment_terms'=> function ($query) use ($updated_at) {
|
|
|
|
$query->where('updated_at', '>=', $updated_at);
|
2020-07-26 12:21:55 +02:00
|
|
|
},
|
2020-10-19 12:59:58 +02:00
|
|
|
'company.products' => function ($query) use ($updated_at) {
|
|
|
|
$query->where('updated_at', '>=', $updated_at)->with('documents');
|
2020-07-26 12:21:55 +02:00
|
|
|
},
|
2020-10-19 12:59:58 +02:00
|
|
|
'company.projects'=> function ($query) use ($updated_at) {
|
2020-11-25 15:19:52 +01:00
|
|
|
$query->where('updated_at', '>=', $updated_at)->with('documents');
|
2020-10-19 12:59:58 +02:00
|
|
|
},
|
|
|
|
'company.quotes'=> function ($query) use ($updated_at) {
|
2020-11-11 09:55:53 +01:00
|
|
|
$query->where('updated_at', '>=', $updated_at)->with('invitations', 'documents');
|
2020-10-19 12:59:58 +02:00
|
|
|
},
|
|
|
|
'company.recurring_invoices'=> function ($query) use ($updated_at) {
|
|
|
|
$query->where('updated_at', '>=', $updated_at)->with('invitations', 'documents');
|
2020-07-26 12:21:55 +02:00
|
|
|
},
|
2020-09-06 11:38:10 +02:00
|
|
|
'company.tasks'=> function ($query) use ($updated_at) {
|
2020-11-25 15:19:52 +01:00
|
|
|
$query->where('updated_at', '>=', $updated_at)->with('documents');
|
2020-07-26 12:21:55 +02:00
|
|
|
},
|
2020-10-19 12:59:58 +02:00
|
|
|
'company.tax_rates' => function ($query) use ($updated_at) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$query->where('updated_at', '>=', $updated_at);
|
2020-07-26 12:21:55 +02:00
|
|
|
},
|
2020-10-19 12:59:58 +02:00
|
|
|
'company.vendors'=> function ($query) use ($updated_at) {
|
2020-11-25 15:19:52 +01:00
|
|
|
$query->where('updated_at', '>=', $updated_at)->with('contacts', 'documents');
|
2020-07-26 12:21:55 +02:00
|
|
|
},
|
2020-10-25 21:56:02 +01:00
|
|
|
'company.expense_categories'=> function ($query) use ($updated_at) {
|
|
|
|
$query->where('updated_at', '>=', $updated_at);
|
|
|
|
},
|
|
|
|
'company.task_statuses'=> function ($query) use ($updated_at) {
|
|
|
|
$query->where('updated_at', '>=', $updated_at);
|
2020-10-28 11:10:49 +01:00
|
|
|
},
|
2020-07-26 12:21:55 +02:00
|
|
|
]
|
2020-07-10 01:28:09 +02:00
|
|
|
);
|
|
|
|
|
2020-11-13 10:09:20 +01:00
|
|
|
if ($query instanceof Builder) {
|
|
|
|
$limit = request()->input('per_page', 20);
|
2020-07-24 11:39:43 +02:00
|
|
|
|
|
|
|
$paginator = $query->paginate($limit);
|
|
|
|
$query = $paginator->getCollection();
|
|
|
|
$resource = new Collection($query, $transformer, $this->entity_type);
|
|
|
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
|
|
|
} else {
|
|
|
|
$resource = new Collection($query, $transformer, $this->entity_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->response($this->manager->createData($resource)->toArray());
|
2020-07-10 01:28:09 +02:00
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
protected function listResponse($query)
|
2019-03-28 22:34:58 +01:00
|
|
|
{
|
2019-06-24 02:13:53 +02:00
|
|
|
$this->buildManager();
|
|
|
|
|
2020-11-13 10:09:20 +01:00
|
|
|
$transformer = new $this->entity_transformer(request()->input('serializer'));
|
2019-03-28 22:34:58 +01:00
|
|
|
|
|
|
|
$includes = $transformer->getDefaultIncludes();
|
2019-12-29 23:06:42 +01:00
|
|
|
|
2019-03-28 22:34:58 +01:00
|
|
|
$includes = $this->getRequestIncludes($includes);
|
|
|
|
|
|
|
|
$query->with($includes);
|
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
if (auth()->user() && ! auth()->user()->hasPermission('view_'.lcfirst(class_basename($this->entity_type)))) {
|
2020-04-04 12:32:42 +02:00
|
|
|
$query->where('user_id', '=', auth()->user()->id);
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2019-09-27 06:31:13 +02:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
if (request()->has('updated_at') && request()->input('updated_at') > 0) {
|
2020-11-13 10:09:20 +01:00
|
|
|
$query->where('updated_at', '>=', date('Y-m-d H:i:s', intval(request()->input('updated_at'))));
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-02-28 13:11:56 +01:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
if ($this->serializer && $this->serializer != EntityTransformer::API_SERIALIZER_JSON) {
|
2020-11-13 10:09:20 +01:00
|
|
|
$this->entity_type = null;
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2019-03-28 22:34:58 +01:00
|
|
|
|
2020-11-13 10:09:20 +01:00
|
|
|
if ($query instanceof Builder) {
|
|
|
|
$limit = request()->input('per_page', 20);
|
2019-03-28 22:34:58 +01:00
|
|
|
$paginator = $query->paginate($limit);
|
|
|
|
$query = $paginator->getCollection();
|
2020-11-13 10:09:20 +01:00
|
|
|
$resource = new Collection($query, $transformer, $this->entity_type);
|
2019-03-28 22:34:58 +01:00
|
|
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
|
|
|
} else {
|
2020-11-13 10:09:20 +01:00
|
|
|
$resource = new Collection($query, $transformer, $this->entity_type);
|
2019-03-28 22:34:58 +01:00
|
|
|
}
|
|
|
|
|
2020-11-13 10:09:20 +01:00
|
|
|
return $this->response($this->manager->createData($resource)->toArray());
|
2019-03-28 22:34:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function response($response)
|
|
|
|
{
|
2019-06-25 07:08:07 +02:00
|
|
|
$index = request()->input('index') ?: $this->forced_index;
|
2019-03-28 22:34:58 +01:00
|
|
|
|
|
|
|
if ($index == 'none') {
|
|
|
|
unset($response['meta']);
|
|
|
|
} else {
|
|
|
|
$meta = isset($response['meta']) ? $response['meta'] : null;
|
|
|
|
$response = [
|
|
|
|
$index => $response,
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($meta) {
|
|
|
|
$response['meta'] = $meta;
|
|
|
|
unset($response[$index]['meta']);
|
|
|
|
}
|
2019-09-18 08:02:05 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (request()->include_static) {
|
2019-09-18 08:02:05 +02:00
|
|
|
$response['static'] = Statics::company(auth()->user()->getCompany()->getLocale());
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-03-28 22:34:58 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-09-23 13:29:30 +02:00
|
|
|
ksort($response);
|
2019-03-28 22:34:58 +01:00
|
|
|
|
|
|
|
$response = json_encode($response, JSON_PRETTY_PRINT);
|
2020-02-10 10:53:02 +01:00
|
|
|
|
2019-03-28 22:34:58 +01:00
|
|
|
$headers = self::getApiHeaders();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-03-28 22:34:58 +01:00
|
|
|
return response()->make($response, 200, $headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function itemResponse($item)
|
|
|
|
{
|
2019-06-24 02:13:53 +02:00
|
|
|
$this->buildManager();
|
2019-03-28 22:34:58 +01:00
|
|
|
|
2020-11-13 10:09:20 +01:00
|
|
|
$transformer = new $this->entity_transformer(request()->input('serializer'));
|
2019-03-28 22:34:58 +01:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
if ($this->serializer && $this->serializer != EntityTransformer::API_SERIALIZER_JSON) {
|
2020-11-13 10:09:20 +01:00
|
|
|
$this->entity_type = null;
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-11-13 10:09:20 +01:00
|
|
|
|
2020-11-13 11:42:06 +01:00
|
|
|
$resource = new Item($item, $transformer, $this->entity_type);
|
2019-03-28 22:34:58 +01:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
if (auth()->user() && request()->include_static) {
|
2019-09-11 02:37:53 +02:00
|
|
|
$data['static'] = Statics::company(auth()->user()->getCompany()->getLocale());
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-11-13 10:09:20 +01:00
|
|
|
return $this->response($this->manager->createData($resource)->toArray());
|
2019-03-28 22:34:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function getApiHeaders($count = 0)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'Content-Type' => 'application/json',
|
2020-06-21 23:30:25 +02:00
|
|
|
'X-Api-Version' => config('ninja.minimum_client_version'),
|
2019-09-11 07:32:47 +02:00
|
|
|
'X-App-Version' => config('ninja.app_version'),
|
2019-03-28 22:34:58 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getRequestIncludes($data)
|
|
|
|
{
|
2020-02-06 13:00:22 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
/*
|
2020-02-10 10:53:02 +01:00
|
|
|
* Thresholds for displaying large account on first load
|
|
|
|
*/
|
2020-03-21 06:37:30 +01:00
|
|
|
if (request()->has('first_load') && request()->input('first_load') == 'true') {
|
2020-07-21 13:19:21 +02:00
|
|
|
if (auth()->user()->getCompany()->is_large && request()->missing('updated_at')) {
|
2020-07-26 10:30:55 +02:00
|
|
|
$data = $this->mini_load;
|
2020-03-21 06:37:30 +01:00
|
|
|
} else {
|
2020-07-26 10:30:55 +02:00
|
|
|
$data = $this->first_load;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$included = request()->input('include');
|
|
|
|
$included = explode(',', $included);
|
|
|
|
|
|
|
|
foreach ($included as $include) {
|
|
|
|
if ($include == 'clients') {
|
|
|
|
$data[] = 'clients.contacts';
|
|
|
|
} elseif ($include) {
|
|
|
|
$data[] = $include;
|
|
|
|
}
|
|
|
|
}
|
2019-03-28 22:34:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-02-13 12:27:42 +01:00
|
|
|
public function flutterRoute()
|
|
|
|
{
|
2020-10-26 01:58:08 +01:00
|
|
|
if ((bool) $this->checkAppSetup() !== false && $account = Account::first()) {
|
2020-09-06 11:38:10 +02:00
|
|
|
if (config('ninja.require_https') && ! request()->isSecure()) {
|
|
|
|
return redirect()->secure(request()->getRequestUri());
|
|
|
|
}
|
2020-06-28 05:47:13 +02:00
|
|
|
|
2020-03-26 04:23:57 +01:00
|
|
|
$data = [];
|
2020-03-18 10:40:15 +01:00
|
|
|
|
2020-05-28 10:54:13 +02:00
|
|
|
if (Ninja::isSelfHost()) {
|
2020-03-26 04:23:57 +01:00
|
|
|
$data['report_errors'] = $account->report_errors;
|
|
|
|
} else {
|
|
|
|
$data['report_errors'] = true;
|
|
|
|
}
|
2020-03-25 09:36:47 +01:00
|
|
|
|
2020-10-09 08:55:03 +02:00
|
|
|
$this->buildCache();
|
|
|
|
|
2020-03-26 04:23:57 +01:00
|
|
|
return view('index.index', $data);
|
2020-03-25 09:36:47 +01:00
|
|
|
}
|
|
|
|
|
2020-03-26 04:23:57 +01:00
|
|
|
return redirect('/setup');
|
2020-02-13 12:27:42 +01:00
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|