2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
2016-05-01 22:55:13 +02:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Http\Requests\ExpenseRequest;
|
2017-03-09 11:11:45 +01:00
|
|
|
use App\Http\Requests\CreateExpenseRequest;
|
2017-01-30 20:40:43 +01:00
|
|
|
use App\Http\Requests\UpdateExpenseRequest;
|
2016-02-17 04:32:20 +01:00
|
|
|
use App\Models\Expense;
|
2016-05-16 04:30:24 +02:00
|
|
|
use App\Ninja\Repositories\ExpenseRepository;
|
2016-02-17 04:32:20 +01:00
|
|
|
use App\Services\ExpenseService;
|
|
|
|
|
|
|
|
class ExpenseApiController extends BaseAPIController
|
|
|
|
{
|
|
|
|
// Expenses
|
|
|
|
protected $expenseRepo;
|
|
|
|
protected $expenseService;
|
|
|
|
|
2016-05-01 22:55:13 +02:00
|
|
|
protected $entityType = ENTITY_EXPENSE;
|
|
|
|
|
2016-02-17 04:32:20 +01:00
|
|
|
public function __construct(ExpenseRepository $expenseRepo, ExpenseService $expenseService)
|
|
|
|
{
|
2016-03-02 15:36:46 +01:00
|
|
|
parent::__construct();
|
2016-02-17 04:32:20 +01:00
|
|
|
|
|
|
|
$this->expenseRepo = $expenseRepo;
|
|
|
|
$this->expenseService = $expenseService;
|
|
|
|
}
|
|
|
|
|
2016-10-23 10:01:21 +02:00
|
|
|
/**
|
|
|
|
* @SWG\Get(
|
|
|
|
* path="/expenses",
|
|
|
|
* summary="List of expenses",
|
|
|
|
* tags={"expense"},
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
2017-03-08 17:03:35 +01:00
|
|
|
* description="A list of expenses",
|
2016-10-23 10:01:21 +02:00
|
|
|
* @SWG\Schema(type="array", @SWG\Items(ref="#/definitions/Expense"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
2016-02-17 04:32:20 +01:00
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
$expenses = Expense::scope()
|
|
|
|
->withTrashed()
|
2016-11-29 18:47:26 +01:00
|
|
|
->with('client', 'invoice', 'vendor', 'expense_category')
|
2017-01-30 17:05:31 +01:00
|
|
|
->orderBy('created_at', 'desc');
|
2016-02-17 04:32:20 +01:00
|
|
|
|
2016-05-02 10:38:01 +02:00
|
|
|
return $this->listResponse($expenses);
|
2016-02-17 04:32:20 +01:00
|
|
|
}
|
|
|
|
|
2017-03-08 17:03:35 +01:00
|
|
|
/**
|
|
|
|
* @SWG\Get(
|
|
|
|
* path="/expenses/{expense_id}",
|
|
|
|
* tags={"expense"},
|
|
|
|
* summary="Retrieve an expense",
|
|
|
|
* @SWG\Parameter(
|
|
|
|
* in="path",
|
|
|
|
* name="expense_id",
|
|
|
|
* type="integer",
|
2017-03-09 11:11:45 +01:00
|
|
|
* required=true
|
2017-03-08 17:03:35 +01:00
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="A single expense",
|
|
|
|
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Expense"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public function show(ExpenseRequest $request)
|
|
|
|
{
|
|
|
|
return $this->itemResponse($request->entity());
|
|
|
|
}
|
|
|
|
|
2016-10-23 10:01:21 +02:00
|
|
|
/**
|
|
|
|
* @SWG\Post(
|
|
|
|
* path="/expenses",
|
|
|
|
* tags={"expense"},
|
2017-03-08 17:03:35 +01:00
|
|
|
* summary="Create an expense",
|
2016-10-23 10:01:21 +02:00
|
|
|
* @SWG\Parameter(
|
|
|
|
* in="body",
|
2017-03-08 17:03:35 +01:00
|
|
|
* name="expense",
|
2016-10-23 10:01:21 +02:00
|
|
|
* @SWG\Schema(ref="#/definitions/Expense")
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="New expense",
|
|
|
|
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Expense"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public function store(CreateExpenseRequest $request)
|
2016-02-17 04:32:20 +01:00
|
|
|
{
|
2016-10-23 10:01:21 +02:00
|
|
|
$expense = $this->expenseRepo->save($request->input());
|
2016-02-17 04:32:20 +01:00
|
|
|
|
2016-10-23 10:01:21 +02:00
|
|
|
$expense = Expense::scope($expense->public_id)
|
2016-10-28 03:59:57 +02:00
|
|
|
->with('client', 'invoice', 'vendor')
|
|
|
|
->first();
|
2016-10-23 10:01:21 +02:00
|
|
|
|
|
|
|
return $this->itemResponse($expense);
|
2016-02-17 04:32:20 +01:00
|
|
|
}
|
|
|
|
|
2016-10-23 10:01:21 +02:00
|
|
|
/**
|
|
|
|
* @SWG\Put(
|
|
|
|
* path="/expenses/{expense_id}",
|
|
|
|
* tags={"expense"},
|
2017-03-08 17:03:35 +01:00
|
|
|
* summary="Update an expense",
|
|
|
|
* @SWG\Parameter(
|
|
|
|
* in="path",
|
|
|
|
* name="expense_id",
|
|
|
|
* type="integer",
|
2017-03-09 11:11:45 +01:00
|
|
|
* required=true
|
2017-03-08 17:03:35 +01:00
|
|
|
* ),
|
2016-10-23 10:01:21 +02:00
|
|
|
* @SWG\Parameter(
|
|
|
|
* in="body",
|
2017-03-08 17:03:35 +01:00
|
|
|
* name="expense",
|
2016-10-23 10:01:21 +02:00
|
|
|
* @SWG\Schema(ref="#/definitions/Expense")
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
2017-03-08 17:03:35 +01:00
|
|
|
* description="Updated expense",
|
2016-10-23 10:01:21 +02:00
|
|
|
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Expense"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
2017-01-30 20:54:09 +01:00
|
|
|
*
|
2017-01-30 20:49:42 +01:00
|
|
|
* @param mixed $publicId
|
2016-10-23 10:01:21 +02:00
|
|
|
*/
|
|
|
|
public function update(UpdateExpenseRequest $request, $publicId)
|
2016-02-17 04:32:20 +01:00
|
|
|
{
|
2016-10-23 10:01:21 +02:00
|
|
|
if ($request->action) {
|
|
|
|
return $this->handleAction($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = $request->input();
|
|
|
|
$data['public_id'] = $publicId;
|
|
|
|
$expense = $this->expenseRepo->save($data, $request->entity());
|
2016-02-17 04:32:20 +01:00
|
|
|
|
2016-10-23 10:01:21 +02:00
|
|
|
return $this->itemResponse($expense);
|
2016-02-17 04:32:20 +01:00
|
|
|
}
|
|
|
|
|
2016-10-23 10:01:21 +02:00
|
|
|
/**
|
|
|
|
* @SWG\Delete(
|
|
|
|
* path="/expenses/{expense_id}",
|
|
|
|
* tags={"expense"},
|
2017-03-08 17:03:35 +01:00
|
|
|
* summary="Delete an expense",
|
2016-10-23 10:01:21 +02:00
|
|
|
* @SWG\Parameter(
|
2017-03-08 17:03:35 +01:00
|
|
|
* in="path",
|
|
|
|
* name="expense_id",
|
|
|
|
* type="integer",
|
2017-03-09 11:11:45 +01:00
|
|
|
* required=true
|
2016-10-23 10:01:21 +02:00
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
2017-03-08 17:03:35 +01:00
|
|
|
* description="Deleted expense",
|
2016-10-23 10:01:21 +02:00
|
|
|
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Expense"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
2017-03-09 11:11:45 +01:00
|
|
|
public function destroy(UpdateExpenseRequest $request)
|
2016-02-17 04:32:20 +01:00
|
|
|
{
|
2016-10-23 10:01:21 +02:00
|
|
|
$expense = $request->entity();
|
|
|
|
|
|
|
|
$this->expenseRepo->delete($expense);
|
2016-02-17 04:32:20 +01:00
|
|
|
|
2016-10-23 10:01:21 +02:00
|
|
|
return $this->itemResponse($expense);
|
2016-02-17 04:32:20 +01:00
|
|
|
}
|
2016-10-23 10:01:21 +02:00
|
|
|
}
|