1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 22:54:25 +01:00
invoiceninja/app/Http/Controllers/ExpenseApiController.php

153 lines
3.9 KiB
PHP
Raw Normal View History

<?php namespace App\Http\Controllers;
2016-05-01 22:55:13 +02:00
use App\Models\Expense;
2016-05-16 04:30:24 +02:00
use App\Ninja\Repositories\ExpenseRepository;
use App\Services\ExpenseService;
2016-10-23 10:01:21 +02:00
use App\Http\Requests\ExpenseRequest;
use App\Http\Requests\CreateExpenseRequest;
use App\Http\Requests\UpdateExpenseRequest;
class ExpenseApiController extends BaseAPIController
{
// Expenses
protected $expenseRepo;
protected $expenseService;
2016-05-01 22:55:13 +02:00
protected $entityType = ENTITY_EXPENSE;
public function __construct(ExpenseRepository $expenseRepo, ExpenseService $expenseService)
{
2016-03-02 15:36:46 +01:00
parent::__construct();
$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,
* description="A list with expenses",
* @SWG\Schema(type="array", @SWG\Items(ref="#/definitions/Expense"))
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
public function index()
{
$expenses = Expense::scope()
->withTrashed()
2016-11-29 18:47:26 +01:00
->with('client', 'invoice', 'vendor', 'expense_category')
->orderBy('created_at','desc');
2016-05-02 10:38:01 +02:00
return $this->listResponse($expenses);
}
2016-10-23 10:01:21 +02:00
/**
* @SWG\Post(
* path="/expenses",
* tags={"expense"},
* summary="Create a expense",
* @SWG\Parameter(
* in="body",
* name="body",
* @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-10-23 10:01:21 +02:00
$expense = $this->expenseRepo->save($request->input());
2016-10-23 10:01:21 +02:00
$expense = Expense::scope($expense->public_id)
->with('client', 'invoice', 'vendor')
->first();
2016-10-23 10:01:21 +02:00
return $this->itemResponse($expense);
}
2016-10-23 10:01:21 +02:00
/**
* @SWG\Put(
* path="/expenses/{expense_id}",
* tags={"expense"},
* summary="Update a expense",
* @SWG\Parameter(
* in="body",
* name="body",
* @SWG\Schema(ref="#/definitions/Expense")
* ),
* @SWG\Response(
* response=200,
* description="Update expense",
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Expense"))
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
public function update(UpdateExpenseRequest $request, $publicId)
{
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-10-23 10:01:21 +02:00
return $this->itemResponse($expense);
}
2016-10-23 10:01:21 +02:00
/**
* @SWG\Delete(
* path="/expenses/{expense_id}",
* tags={"expense"},
* summary="Delete a expense",
* @SWG\Parameter(
* in="body",
* name="body",
* @SWG\Schema(ref="#/definitions/Expense")
* ),
* @SWG\Response(
* response=200,
* description="Delete expense",
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Expense"))
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
public function destroy(ExpenseRequest $request)
{
2016-10-23 10:01:21 +02:00
$expense = $request->entity();
$this->expenseRepo->delete($expense);
2016-10-23 10:01:21 +02:00
return $this->itemResponse($expense);
}
2016-10-23 10:01:21 +02:00
}