2020-01-20 02:31:58 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-01-20 02:31:58 +01: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-01-20 02:31:58 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-01-20 02:31:58 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2023-11-03 01:07:05 +01:00
|
|
|
use App\Events\Expense\ExpenseWasCreated;
|
|
|
|
use App\Events\Expense\ExpenseWasUpdated;
|
2023-11-26 08:41:42 +01:00
|
|
|
use App\Factory\ExpenseFactory;
|
|
|
|
use App\Filters\ExpenseFilters;
|
2023-11-03 01:07:05 +01:00
|
|
|
use App\Http\Requests\Expense\BulkExpenseRequest;
|
2023-11-26 08:41:42 +01:00
|
|
|
use App\Http\Requests\Expense\CreateExpenseRequest;
|
|
|
|
use App\Http\Requests\Expense\DestroyExpenseRequest;
|
2020-09-23 02:46:35 +02:00
|
|
|
use App\Http\Requests\Expense\EditExpenseRequest;
|
|
|
|
use App\Http\Requests\Expense\ShowExpenseRequest;
|
|
|
|
use App\Http\Requests\Expense\StoreExpenseRequest;
|
|
|
|
use App\Http\Requests\Expense\UpdateExpenseRequest;
|
2021-02-15 12:07:47 +01:00
|
|
|
use App\Http\Requests\Expense\UploadExpenseRequest;
|
2023-11-26 08:41:42 +01:00
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\Expense;
|
|
|
|
use App\Repositories\ExpenseRepository;
|
|
|
|
use App\Transformers\ExpenseTransformer;
|
|
|
|
use App\Utils\Ninja;
|
|
|
|
use App\Utils\Traits\BulkOptions;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use App\Utils\Traits\SavesDocuments;
|
|
|
|
use App\Utils\Traits\Uploadable;
|
|
|
|
use Illuminate\Http\Response;
|
2020-01-20 02:31:58 +01:00
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class ExpenseController.
|
2020-01-20 05:53:40 +01:00
|
|
|
* @covers App\Http\Controllers\ExpenseController
|
2020-01-20 02:31:58 +01:00
|
|
|
*/
|
2020-01-20 05:53:40 +01:00
|
|
|
class ExpenseController extends BaseController
|
2020-01-20 02:31:58 +01:00
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
use Uploadable;
|
|
|
|
use BulkOptions;
|
2021-02-15 12:07:47 +01:00
|
|
|
use SavesDocuments;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-01-20 05:53:40 +01:00
|
|
|
protected $entity_type = Expense::class;
|
2020-01-20 02:31:58 +01:00
|
|
|
|
2020-01-20 05:53:40 +01:00
|
|
|
protected $entity_transformer = ExpenseTransformer::class;
|
2020-01-20 02:31:58 +01:00
|
|
|
|
|
|
|
/**
|
2023-07-27 03:14:59 +02:00
|
|
|
* @var ExpenseRepository
|
2020-01-20 02:31:58 +01:00
|
|
|
*/
|
2020-01-20 05:53:40 +01:00
|
|
|
protected $expense_repo;
|
2020-01-20 02:31:58 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-20 05:53:40 +01:00
|
|
|
* ExpenseController constructor.
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param ExpenseRepository $expense_repo
|
2020-01-20 02:31:58 +01:00
|
|
|
*/
|
2020-01-20 05:53:40 +01:00
|
|
|
public function __construct(ExpenseRepository $expense_repo)
|
2020-01-20 02:31:58 +01:00
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
2020-01-20 05:53:40 +01:00
|
|
|
$this->expense_repo = $expense_repo;
|
2020-01-20 02:31:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-28 11:10:49 +01:00
|
|
|
* @OA\Get(
|
2020-01-20 05:53:40 +01:00
|
|
|
* path="/api/v1/expenses",
|
|
|
|
* operationId="getExpenses",
|
|
|
|
* tags={"expenses"},
|
|
|
|
* summary="Gets a list of expenses",
|
|
|
|
* description="Lists expenses, search and filters allow fine grained lists to be generated.
|
2020-01-20 02:31:58 +01:00
|
|
|
|
2023-07-27 03:14:59 +02:00
|
|
|
* Query parameters can be added to performed more fine grained filtering of the expenses, these are handled by the ExpenseFilters class which defines the methods available",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2020-01-20 02:31:58 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/include"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/index"),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
2020-01-20 05:53:40 +01:00
|
|
|
* description="A list of expenses",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2020-01-20 02:31:58 +01:00
|
|
|
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
|
|
|
|
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
|
2020-01-20 05:53:40 +01:00
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Expense"),
|
2020-01-20 02:31:58 +01:00
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param ExpenseFilters $filters
|
|
|
|
* @return Response|mixed
|
2020-01-20 02:31:58 +01:00
|
|
|
*/
|
2020-01-20 05:53:40 +01:00
|
|
|
public function index(ExpenseFilters $filters)
|
2020-01-20 02:31:58 +01:00
|
|
|
{
|
2020-01-20 05:53:40 +01:00
|
|
|
$expenses = Expense::filter($filters);
|
2020-01-20 02:31:58 +01:00
|
|
|
|
2020-01-20 05:53:40 +01:00
|
|
|
return $this->listResponse($expenses);
|
2020-01-20 02:31:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param ShowExpenseRequest $request
|
|
|
|
* @param Expense $expense
|
|
|
|
* @return Response
|
2020-01-20 02:31:58 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* @OA\Get(
|
2020-01-20 05:53:40 +01:00
|
|
|
* path="/api/v1/expenses/{id}",
|
|
|
|
* operationId="showExpense",
|
|
|
|
* tags={"expenses"},
|
2020-01-20 02:31:58 +01:00
|
|
|
* summary="Shows a client",
|
|
|
|
* description="Displays a client by id",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2020-01-20 02:31:58 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/include"),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="id",
|
|
|
|
* in="path",
|
2020-01-20 05:53:40 +01:00
|
|
|
* description="The Expense Hashed ID",
|
2020-01-20 02:31:58 +01:00
|
|
|
* example="D2J234DFA",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* format="string",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
2020-01-20 05:53:40 +01:00
|
|
|
* description="Returns the expense object",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2020-01-20 02:31:58 +01:00
|
|
|
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
|
|
|
|
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
|
2020-01-20 05:53:40 +01:00
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Expense"),
|
2020-01-20 02:31:58 +01:00
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
*
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*/
|
2020-01-20 05:53:40 +01:00
|
|
|
public function show(ShowExpenseRequest $request, Expense $expense)
|
2020-01-20 02:31:58 +01:00
|
|
|
{
|
2020-01-20 05:53:40 +01:00
|
|
|
return $this->itemResponse($expense);
|
2020-01-20 02:31:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param EditExpenseRequest $request
|
|
|
|
* @param Expense $expense
|
|
|
|
* @return Response
|
2020-01-20 02:31:58 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* @OA\Get(
|
2020-01-20 05:53:40 +01:00
|
|
|
* path="/api/v1/expenses/{id}/edit",
|
|
|
|
* operationId="editExpense",
|
|
|
|
* tags={"expenses"},
|
2020-01-20 02:31:58 +01:00
|
|
|
* summary="Shows a client for editting",
|
|
|
|
* description="Displays a client by id",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2020-01-20 02:31:58 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/include"),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="id",
|
|
|
|
* in="path",
|
2020-01-20 05:53:40 +01:00
|
|
|
* description="The Expense Hashed ID",
|
2020-01-20 02:31:58 +01:00
|
|
|
* example="D2J234DFA",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* format="string",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Returns the client object",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2020-01-20 02:31:58 +01:00
|
|
|
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
|
|
|
|
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
|
2020-01-20 05:53:40 +01:00
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Expense"),
|
2020-01-20 02:31:58 +01:00
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
*
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*/
|
2020-01-20 05:53:40 +01:00
|
|
|
public function edit(EditExpenseRequest $request, Expense $expense)
|
2020-01-20 02:31:58 +01:00
|
|
|
{
|
2020-01-20 05:53:40 +01:00
|
|
|
return $this->itemResponse($expense);
|
2020-01-20 02:31:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param UpdateExpenseRequest $request
|
|
|
|
* @param Expense $expense
|
|
|
|
* @return Response
|
2020-01-20 02:31:58 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @OA\Put(
|
2020-01-20 05:53:40 +01:00
|
|
|
* path="/api/v1/expenses/{id}",
|
|
|
|
* operationId="updateExpense",
|
|
|
|
* tags={"expenses"},
|
2020-01-20 02:31:58 +01:00
|
|
|
* summary="Updates a client",
|
|
|
|
* description="Handles the updating of a client by id",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2020-01-20 02:31:58 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/include"),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="id",
|
|
|
|
* in="path",
|
2020-01-20 05:53:40 +01:00
|
|
|
* description="The Expense Hashed ID",
|
2020-01-20 02:31:58 +01:00
|
|
|
* example="D2J234DFA",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* format="string",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Returns the client object",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2020-01-20 02:31:58 +01:00
|
|
|
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
|
|
|
|
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
|
2020-01-20 05:53:40 +01:00
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Expense"),
|
2020-01-20 02:31:58 +01:00
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
*
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*/
|
2020-01-20 05:53:40 +01:00
|
|
|
public function update(UpdateExpenseRequest $request, Expense $expense)
|
2020-01-20 02:31:58 +01:00
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($request->entityIsDeleted($expense)) {
|
2020-01-20 02:31:58 +01:00
|
|
|
return $request->disallowUpdate();
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-01-20 02:31:58 +01:00
|
|
|
|
2020-09-23 02:46:35 +02:00
|
|
|
$expense = $this->expense_repo->save($request->all(), $expense);
|
2020-01-20 02:31:58 +01:00
|
|
|
|
2020-01-20 05:53:40 +01:00
|
|
|
$this->uploadLogo($request->file('company_logo'), $expense->company, $expense);
|
2020-01-20 02:31:58 +01:00
|
|
|
|
2021-05-06 23:12:07 +02:00
|
|
|
event(new ExpenseWasUpdated($expense, $expense->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
|
2020-11-03 11:04:15 +01:00
|
|
|
|
2023-02-01 04:12:44 +01:00
|
|
|
event('eloquent.updated: App\Models\Expense', $expense);
|
|
|
|
|
2020-01-20 05:53:40 +01:00
|
|
|
return $this->itemResponse($expense->fresh());
|
2020-01-20 02:31:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param CreateExpenseRequest $request
|
|
|
|
* @return Response
|
2020-01-20 02:31:58 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @OA\Get(
|
2020-01-20 05:53:40 +01:00
|
|
|
* path="/api/v1/expenses/create",
|
|
|
|
* operationId="getExpensesCreate",
|
|
|
|
* tags={"expenses"},
|
2020-01-20 02:31:58 +01:00
|
|
|
* summary="Gets a new blank client object",
|
|
|
|
* description="Returns a blank object with default values",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2020-01-20 02:31:58 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/include"),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="A blank client object",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2020-01-20 02:31:58 +01:00
|
|
|
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
|
|
|
|
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
|
2020-01-20 05:53:40 +01:00
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Expense"),
|
2020-01-20 02:31:58 +01:00
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
*
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*/
|
2020-01-20 05:53:40 +01:00
|
|
|
public function create(CreateExpenseRequest $request)
|
2020-01-20 02:31:58 +01:00
|
|
|
{
|
2023-11-03 01:07:05 +01:00
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
|
|
|
|
$expense = ExpenseFactory::create($user->company()->id, $user->id);
|
2020-01-20 02:31:58 +01:00
|
|
|
|
2020-01-20 05:53:40 +01:00
|
|
|
return $this->itemResponse($expense);
|
2020-01-20 02:31:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param StoreExpenseRequest $request
|
|
|
|
* @return Response
|
2020-01-20 02:31:58 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @OA\Post(
|
2020-01-20 05:53:40 +01:00
|
|
|
* path="/api/v1/expenses",
|
|
|
|
* operationId="storeExpense",
|
|
|
|
* tags={"expenses"},
|
2020-01-20 02:31:58 +01:00
|
|
|
* summary="Adds a client",
|
|
|
|
* description="Adds an client to a company",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2020-01-20 02:31:58 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/include"),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Returns the saved client object",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2020-01-20 02:31:58 +01:00
|
|
|
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
|
|
|
|
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
|
2020-01-20 05:53:40 +01:00
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Expense"),
|
2020-01-20 02:31:58 +01:00
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
*
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*/
|
2020-01-20 05:53:40 +01:00
|
|
|
public function store(StoreExpenseRequest $request)
|
2020-01-20 02:31:58 +01:00
|
|
|
{
|
2023-11-03 01:07:05 +01:00
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
|
|
|
|
$expense = $this->expense_repo->save($request->all(), ExpenseFactory::create($user->company()->id, $user->id));
|
2020-01-20 02:31:58 +01:00
|
|
|
|
2023-11-03 01:07:05 +01:00
|
|
|
event(new ExpenseWasCreated($expense, $expense->company, Ninja::eventVars($user ? $user->id : null)));
|
2020-11-03 11:04:15 +01:00
|
|
|
|
2023-02-01 04:12:44 +01:00
|
|
|
event('eloquent.created: App\Models\Expense', $expense);
|
|
|
|
|
2020-01-20 05:53:40 +01:00
|
|
|
return $this->itemResponse($expense);
|
2020-01-20 02:31:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param DestroyExpenseRequest $request
|
|
|
|
* @param Expense $expense
|
|
|
|
* @return Response
|
2020-01-20 02:31:58 +01:00
|
|
|
*
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @throws \Exception
|
2020-01-20 02:31:58 +01:00
|
|
|
* @OA\Delete(
|
2020-01-20 05:53:40 +01:00
|
|
|
* path="/api/v1/expenses/{id}",
|
|
|
|
* operationId="deleteExpense",
|
|
|
|
* tags={"expenses"},
|
2020-01-20 02:31:58 +01:00
|
|
|
* summary="Deletes a client",
|
|
|
|
* description="Handles the deletion of a client by id",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2020-01-20 02:31:58 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/include"),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="id",
|
|
|
|
* in="path",
|
2020-01-20 05:53:40 +01:00
|
|
|
* description="The Expense Hashed ID",
|
2020-01-20 02:31:58 +01:00
|
|
|
* example="D2J234DFA",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* format="string",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Returns a HTTP status",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2020-01-20 02:31:58 +01:00
|
|
|
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
|
|
|
|
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
*
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*/
|
2020-01-20 05:53:40 +01:00
|
|
|
public function destroy(DestroyExpenseRequest $request, Expense $expense)
|
2020-01-20 02:31:58 +01:00
|
|
|
{
|
2021-01-13 21:33:45 +01:00
|
|
|
$this->expense_repo->delete($expense);
|
2020-01-20 02:31:58 +01:00
|
|
|
|
2021-01-13 21:33:45 +01:00
|
|
|
return $this->itemResponse($expense->fresh());
|
2020-01-20 02:31:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Perform bulk actions on the list view.
|
2020-01-20 02:31:58 +01:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return Response
|
2020-01-20 02:31:58 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* @OA\Post(
|
2020-01-20 05:53:40 +01:00
|
|
|
* path="/api/v1/expenses/bulk",
|
|
|
|
* operationId="bulkExpenses",
|
|
|
|
* tags={"expenses"},
|
|
|
|
* summary="Performs bulk actions on an array of expenses",
|
2020-01-20 02:31:58 +01:00
|
|
|
* description="",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2020-01-20 02:31:58 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/index"),
|
|
|
|
* @OA\RequestBody(
|
|
|
|
* description="User credentials",
|
|
|
|
* required=true,
|
|
|
|
* @OA\MediaType(
|
|
|
|
* mediaType="application/json",
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="array",
|
|
|
|
* @OA\Items(
|
|
|
|
* type="integer",
|
|
|
|
* description="Array of hashed IDs to be bulk 'actioned",
|
|
|
|
* example="[0,1,2,3]",
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
2020-01-20 05:53:40 +01:00
|
|
|
* description="The Expense User response",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2020-01-20 02:31:58 +01:00
|
|
|
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
|
|
|
|
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
|
2020-01-20 05:53:40 +01:00
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Expense"),
|
2020-01-20 02:31:58 +01:00
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*/
|
2023-11-03 01:07:05 +01:00
|
|
|
public function bulk(BulkExpenseRequest $request)
|
2020-01-20 02:31:58 +01:00
|
|
|
{
|
2023-11-03 01:07:05 +01:00
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = auth()->user();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2023-11-03 01:07:05 +01:00
|
|
|
$expenses = Expense::withTrashed()->find($request->ids);
|
|
|
|
|
|
|
|
if($request->action == 'bulk_categorize' && $user->can('edit', $expenses->first())) {
|
|
|
|
$this->expense_repo->categorize($expenses, $request->category_id);
|
|
|
|
$expenses = collect([]);
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2023-11-03 01:07:05 +01:00
|
|
|
$expenses->each(function ($expense) use ($request, $user) {
|
|
|
|
if ($user->can('edit', $expense)) {
|
|
|
|
$this->expense_repo->{$request->action}($expense);
|
2020-01-20 02:31:58 +01:00
|
|
|
}
|
|
|
|
});
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2023-11-03 01:07:05 +01:00
|
|
|
return $this->listResponse(Expense::withTrashed()->whereIn('id', $request->ids));
|
2020-01-20 02:31:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Returns a client statement.
|
2020-01-20 02:31:58 +01:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return void [type] [description]
|
2020-01-20 02:31:58 +01:00
|
|
|
*/
|
|
|
|
public function statement()
|
|
|
|
{
|
|
|
|
//todo
|
|
|
|
}
|
2021-02-15 12:07:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param UploadExpenseRequest $request
|
|
|
|
* @param Expense $expense
|
|
|
|
* @return Response
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @OA\Put(
|
|
|
|
* path="/api/v1/expenses/{id}/upload",
|
|
|
|
* operationId="uploadExpense",
|
|
|
|
* tags={"expense"},
|
|
|
|
* summary="Uploads a document to a expense",
|
|
|
|
* description="Handles the uploading of a document to a expense",
|
2023-02-10 10:21:10 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
|
2021-02-15 12:07:47 +01:00
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/include"),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="id",
|
|
|
|
* in="path",
|
|
|
|
* description="The Expense Hashed ID",
|
|
|
|
* example="D2J234DFA",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* format="string",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Returns the Expense object",
|
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
|
|
|
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
|
|
|
|
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Expense"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
*
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public function upload(UploadExpenseRequest $request, Expense $expense)
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $this->checkFeature(Account::FEATURE_DOCUMENTS)) {
|
2021-03-07 22:32:38 +01:00
|
|
|
return $this->featureFailure();
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->has('documents')) {
|
2023-08-20 06:05:26 +02:00
|
|
|
$this->saveDocuments($request->file('documents'), $expense, $request->input('is_public', true));
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-02-15 12:07:47 +01:00
|
|
|
|
|
|
|
return $this->itemResponse($expense->fresh());
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2020-01-20 02:31:58 +01:00
|
|
|
}
|