1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Http/Controllers/ExpenseCategoryController.php

472 lines
18 KiB
PHP
Raw Normal View History

2020-10-13 06:08:56 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @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-10-13 06:08:56 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-10-13 06:08:56 +02:00
*/
namespace App\Http\Controllers;
use App\Factory\ExpenseCategoryFactory;
use App\Filters\ExpenseCategoryFilters;
2020-10-13 06:08:56 +02:00
use App\Http\Requests\ExpenseCategory\CreateExpenseCategoryRequest;
use App\Http\Requests\ExpenseCategory\DestroyExpenseCategoryRequest;
use App\Http\Requests\ExpenseCategory\EditExpenseCategoryRequest;
use App\Http\Requests\ExpenseCategory\ShowExpenseCategoryRequest;
use App\Http\Requests\ExpenseCategory\StoreExpenseCategoryRequest;
use App\Http\Requests\ExpenseCategory\UpdateExpenseCategoryRequest;
use App\Models\Expense;
2020-10-13 06:08:56 +02:00
use App\Models\ExpenseCategory;
use App\Repositories\BaseRepository;
use App\Transformers\ExpenseCategoryTransformer;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Request;
2020-10-28 11:10:49 +01:00
use Illuminate\Http\Response;
2020-10-13 06:08:56 +02:00
/**
* Class ExpenseCategoryController.
*/
class ExpenseCategoryController extends BaseController
{
use MakesHash;
protected $entity_type = ExpenseCategory::class;
protected $entity_transformer = ExpenseCategoryTransformer::class;
protected $base_repo;
public function __construct(BaseRepository $base_repo)
{
parent::__construct();
$this->base_repo = $base_repo;
}
/**
* @OA\Get(
* path="/api/v1/expense_categories",
* operationId="getExpenseCategorys",
* tags={"expense_categories"},
* summary="Gets a list of expense_categories",
* description="Lists tax rates",
* @OA\Parameter(ref="#/components/parameters/index"),
* @OA\Response(
* response=200,
* description="A list of expense_categories",
* @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/ExpenseCategory"),
* ),
* @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"),
* ),
* )
*
*
* Display a listing of the resource.
*
2020-10-28 11:10:49 +01:00
* @return Response
2020-10-13 06:08:56 +02:00
*/
public function index(ExpenseCategoryFilters $filters)
2020-10-13 06:08:56 +02:00
{
$expense_categories = ExpenseCategory::filter($filters);
2020-10-13 06:08:56 +02:00
return $this->listResponse($expense_categories);
}
2020-10-13 06:08:56 +02:00
/**
* Show the form for creating a new resource.
*
2020-10-28 11:10:49 +01:00
* @param CreateExpenseCategoryRequest $request
* @return Response
2020-10-13 06:08:56 +02:00
*
*
*
* @OA\Get(
* path="/api/v1/expense_categories/create",
* operationId="getExpenseCategoryCreate",
* tags={"expense_categories"},
* summary="Gets a new blank Expens Category 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-10-13 06:08:56 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Response(
* response=200,
* description="A blank Expens Category 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/ExpenseCategory"),
* ),
* @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 create(CreateExpenseCategoryRequest $request)
{
2020-10-13 07:02:12 +02:00
$expense_category = ExpenseCategoryFactory::create(auth()->user()->company()->id, auth()->user()->id);
2020-10-13 06:08:56 +02:00
2020-10-13 07:02:12 +02:00
return $this->itemResponse($expense_category);
2020-10-13 06:08:56 +02:00
}
2020-10-13 06:08:56 +02:00
/**
* Store a newly created resource in storage.
*
* @param StoreInvoiceRequest $request The request
*
2020-10-28 11:10:49 +01:00
* @return Response
*
*
* @OA\Post(
* path="/api/v1/expense_categories",
* operationId="storeExpenseCategory",
* tags={"expense_categories"},
* summary="Adds a expense category",
* description="Adds an expense category to the system",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response(
* response=200,
* description="Returns the saved invoice 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/ExpenseCategory"),
* ),
* @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-13 06:08:56 +02:00
*/
public function store(StoreExpenseCategoryRequest $request)
{
2020-10-13 07:02:12 +02:00
$expense_category = ExpenseCategoryFactory::create(auth()->user()->company()->id, auth()->user()->id);
$expense_category->fill($request->all());
$expense_category->save();
2020-10-13 06:08:56 +02:00
2020-10-13 07:02:12 +02:00
return $this->itemResponse($expense_category);
2020-10-13 06:08:56 +02:00
}
/**
* Display the specified resource.
*
2020-10-28 11:10:49 +01:00
* @param ShowExpenseCategoryRequest $request
* @param ExpenseCategory $expense_category
* @return Response
2020-10-13 06:08:56 +02:00
*
*
* @OA\Get(
* path="/api/v1/expense_categories/{id}",
* operationId="showExpenseCategory",
* tags={"expense_categories"},
* summary="Shows a Expens Category",
* description="Displays an ExpenseCategory by id",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2020-10-13 06:08:56 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The ExpenseCategory Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the Expens Category 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/ExpenseCategory"),
* ),
* @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-13 07:02:12 +02:00
public function show(ShowExpenseCategoryRequest $request, ExpenseCategory $expense_category)
2020-10-13 06:08:56 +02:00
{
2020-10-13 07:02:12 +02:00
return $this->itemResponse($expense_category);
2020-10-13 06:08:56 +02:00
}
/**
* Show the form for editing the specified resource.
*
2020-10-28 11:10:49 +01:00
* @param EditExpenseCategoryRequest $request
* @param ExpenseCategory $expense_category
* @return Response
2020-10-13 06:08:56 +02:00
*
*
* @OA\Get(
* path="/api/v1/expense_categories/{id}/edit",
* operationId="editExpenseCategory",
* tags={"expense_categories"},
* summary="Shows a Expens Category for editting",
* description="Displays a Expens Category by id",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2020-10-13 06:08:56 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The ExpenseCategory Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the Expens Category 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/ExpenseCategory"),
* ),
* @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-13 07:02:12 +02:00
public function edit(EditExpenseCategoryRequest $request, ExpenseCategory $expense_category)
2020-10-13 06:08:56 +02:00
{
2020-10-13 07:02:12 +02:00
return $this->itemResponse($expense_category);
2020-10-13 06:08:56 +02:00
}
/**
* Update the specified resource in storage.
*
2020-10-28 11:10:49 +01:00
* @param UpdateExpenseCategoryRequest $request
* @param ExpenseCategory $expense_category
* @return Response
2020-10-13 06:08:56 +02:00
*
*
*
* @OA\Put(
* path="/api/v1/expense_categories/{id}",
* operationId="updateExpenseCategory",
* tags={"expense_categories"},
* summary="Updates a tax rate",
* description="Handles the updating of a tax rate by id",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2020-10-13 06:08:56 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The ExpenseCategory Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the ExpenseCategory 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/ExpenseCategory"),
* ),
* @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-13 07:02:12 +02:00
public function update(UpdateExpenseCategoryRequest $request, ExpenseCategory $expense_category)
2020-10-13 06:08:56 +02:00
{
2020-10-13 07:02:12 +02:00
$expense_category->fill($request->all());
$expense_category->save();
2020-10-13 06:08:56 +02:00
2020-10-13 07:02:12 +02:00
return $this->itemResponse($expense_category);
2020-10-13 06:08:56 +02:00
}
/**
* Remove the specified resource from storage.
*
2020-10-28 11:10:49 +01:00
* @param DestroyExpenseCategoryRequest $request
* @param ExpenseCategory $expense_category
* @return Response
2020-10-13 06:08:56 +02:00
*
*
2020-10-28 11:10:49 +01:00
* @throws \Exception
2020-10-13 06:08:56 +02:00
* @OA\Delete(
* path="/api/v1/expense_categories/{id}",
* operationId="deleteExpenseCategory",
* tags={"expense_categories"},
* summary="Deletes a ExpenseCategory",
* description="Handles the deletion of an ExpenseCategory by id",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2020-10-13 06:08:56 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The ExpenseCategory Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns a HTTP status",
* @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\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-13 07:02:12 +02:00
public function destroy(DestroyExpenseCategoryRequest $request, ExpenseCategory $expense_category)
2020-10-13 06:08:56 +02:00
{
2020-10-13 07:02:12 +02:00
$expense_category->is_deleted = true;
$expense_category->save();
$expense_category->delete();
2020-10-13 06:08:56 +02:00
2020-10-13 07:02:12 +02:00
return $this->itemResponse($expense_category);
2020-10-13 06:08:56 +02:00
}
/**
* Perform bulk actions on the list view.
*
2020-10-28 11:10:49 +01:00
* @return Response
2020-10-13 06:08:56 +02:00
*
*
* @OA\Post(
* path="/api/v1/expense_categories/bulk",
* operationId="bulkExpenseCategorys",
* tags={"expense_categories"},
* summary="Performs bulk actions on an array of ExpenseCategorys",
* description="",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2020-10-13 06:08:56 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/index"),
* @OA\RequestBody(
* description="Expens Categorys",
* 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,
* description="The ExpenseCategory List response",
* @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/Webhook"),
* ),
* @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 bulk()
{
$action = request()->input('action');
$ids = request()->input('ids');
$expense_categories = ExpenseCategory::withTrashed()->find($this->transformKeys($ids));
2020-10-13 07:02:12 +02:00
$expense_categories->each(function ($expense_category, $key) use ($action) {
if (auth()->user()->can('edit', $expense_category)) {
$this->base_repo->{$action}($expense_category);
2020-10-13 06:08:56 +02:00
}
});
return $this->listResponse(ExpenseCategory::withTrashed()->whereIn('id', $this->transformKeys($ids)));
}
}