mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Budgets and budget limits end point.
This commit is contained in:
parent
b0e1c85c55
commit
108d43f967
@ -23,11 +23,19 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace FireflyIII\Api\V1\Controllers;
|
namespace FireflyIII\Api\V1\Controllers;
|
||||||
|
|
||||||
|
use FireflyIII\Api\V1\Requests\BudgetLimitRequest;
|
||||||
use FireflyIII\Api\V1\Requests\BudgetRequest;
|
use FireflyIII\Api\V1\Requests\BudgetRequest;
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
|
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
|
||||||
|
use FireflyIII\Helpers\Filter\InternalTransferFilter;
|
||||||
use FireflyIII\Models\Budget;
|
use FireflyIII\Models\Budget;
|
||||||
|
use FireflyIII\Models\TransactionType;
|
||||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||||
|
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||||
|
use FireflyIII\Support\Http\Api\TransactionFilter;
|
||||||
|
use FireflyIII\Transformers\BudgetLimitTransformer;
|
||||||
use FireflyIII\Transformers\BudgetTransformer;
|
use FireflyIII\Transformers\BudgetTransformer;
|
||||||
|
use FireflyIII\Transformers\TransactionTransformer;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
@ -45,6 +53,7 @@ use League\Fractal\Serializer\JsonApiSerializer;
|
|||||||
*/
|
*/
|
||||||
class BudgetController extends Controller
|
class BudgetController extends Controller
|
||||||
{
|
{
|
||||||
|
use TransactionFilter;
|
||||||
/** @var BudgetRepositoryInterface The budget repository */
|
/** @var BudgetRepositoryInterface The budget repository */
|
||||||
private $repository;
|
private $repository;
|
||||||
|
|
||||||
@ -68,6 +77,33 @@ class BudgetController extends Controller
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display a listing of the resource.
|
||||||
|
*
|
||||||
|
* @param Request $request
|
||||||
|
* @param Budget $budget
|
||||||
|
*
|
||||||
|
* @return JsonResponse
|
||||||
|
*/
|
||||||
|
public function budgetLimits(Request $request, Budget $budget): JsonResponse
|
||||||
|
{
|
||||||
|
$manager = new Manager;
|
||||||
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
||||||
|
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
|
||||||
|
$this->parameters->set('budget_id', $budget->id);
|
||||||
|
$collection = $this->repository->getBudgetLimits($budget, $this->parameters->get('start'), $this->parameters->get('end'));
|
||||||
|
$count = $collection->count();
|
||||||
|
$budgetLimits = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
|
||||||
|
$paginator = new LengthAwarePaginator($budgetLimits, $count, $pageSize, $this->parameters->get('page'));
|
||||||
|
$paginator->setPath(route('api.v1.budgets.budget_limits', [$budget->id]) . $this->buildParams());
|
||||||
|
|
||||||
|
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
||||||
|
$resource = new FractalCollection($budgetLimits, new BudgetLimitTransformer($this->parameters), 'budget_limits');
|
||||||
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
||||||
|
|
||||||
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove the specified resource from storage.
|
* Remove the specified resource from storage.
|
||||||
*
|
*
|
||||||
@ -115,7 +151,6 @@ class BudgetController extends Controller
|
|||||||
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Show a budget.
|
* Show a budget.
|
||||||
*
|
*
|
||||||
@ -158,6 +193,76 @@ class BudgetController extends Controller
|
|||||||
throw new FireflyException('Could not store new budget.'); // @codeCoverageIgnore
|
throw new FireflyException('Could not store new budget.'); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store a newly created resource in storage.
|
||||||
|
*
|
||||||
|
* @param BudgetLimitRequest $request
|
||||||
|
* @param Budget $budget
|
||||||
|
*
|
||||||
|
* @return JsonResponse
|
||||||
|
*/
|
||||||
|
public function storeBudgetLimit(BudgetLimitRequest $request, Budget $budget): JsonResponse
|
||||||
|
{
|
||||||
|
$data = $request->getAll();
|
||||||
|
$data['budget'] = $budget;
|
||||||
|
$budgetLimit = $this->repository->storeBudgetLimit($data);
|
||||||
|
$manager = new Manager;
|
||||||
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
||||||
|
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
||||||
|
|
||||||
|
$resource = new Item($budgetLimit, new BudgetLimitTransformer($this->parameters), 'budget_limits');
|
||||||
|
|
||||||
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show all transactions.
|
||||||
|
*
|
||||||
|
* @param Request $request
|
||||||
|
*
|
||||||
|
* @param Budget $budget
|
||||||
|
*
|
||||||
|
* @return JsonResponse
|
||||||
|
*/
|
||||||
|
public function transactions(Request $request, Budget $budget): JsonResponse
|
||||||
|
{
|
||||||
|
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
|
||||||
|
$type = $request->get('type') ?? 'default';
|
||||||
|
$this->parameters->set('type', $type);
|
||||||
|
|
||||||
|
$types = $this->mapTransactionTypes($this->parameters->get('type'));
|
||||||
|
$manager = new Manager();
|
||||||
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
||||||
|
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
||||||
|
|
||||||
|
/** @var User $admin */
|
||||||
|
$admin = auth()->user();
|
||||||
|
/** @var TransactionCollectorInterface $collector */
|
||||||
|
$collector = app(TransactionCollectorInterface::class);
|
||||||
|
$collector->setUser($admin);
|
||||||
|
$collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
|
||||||
|
$collector->setAllAssetAccounts();
|
||||||
|
$collector->setBudget($budget);
|
||||||
|
|
||||||
|
if (\in_array(TransactionType::TRANSFER, $types, true)) {
|
||||||
|
$collector->removeFilter(InternalTransferFilter::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) {
|
||||||
|
$collector->setRange($this->parameters->get('start'), $this->parameters->get('end'));
|
||||||
|
}
|
||||||
|
$collector->setLimit($pageSize)->setPage($this->parameters->get('page'));
|
||||||
|
$collector->setTypes($types);
|
||||||
|
$paginator = $collector->getPaginatedTransactions();
|
||||||
|
$paginator->setPath(route('api.v1.budgets.transactions', [$budget->id]) . $this->buildParams());
|
||||||
|
$transactions = $paginator->getCollection();
|
||||||
|
$repository = app(JournalRepositoryInterface::class);
|
||||||
|
|
||||||
|
$resource = new FractalCollection($transactions, new TransactionTransformer($this->parameters, $repository), 'transactions');
|
||||||
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
||||||
|
|
||||||
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update a budget.
|
* Update a budget.
|
||||||
|
@ -26,9 +26,15 @@ namespace FireflyIII\Api\V1\Controllers;
|
|||||||
|
|
||||||
use FireflyIII\Api\V1\Requests\BudgetLimitRequest;
|
use FireflyIII\Api\V1\Requests\BudgetLimitRequest;
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
|
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
|
||||||
|
use FireflyIII\Helpers\Filter\InternalTransferFilter;
|
||||||
use FireflyIII\Models\BudgetLimit;
|
use FireflyIII\Models\BudgetLimit;
|
||||||
|
use FireflyIII\Models\TransactionType;
|
||||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||||
|
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||||
|
use FireflyIII\Support\Http\Api\TransactionFilter;
|
||||||
use FireflyIII\Transformers\BudgetLimitTransformer;
|
use FireflyIII\Transformers\BudgetLimitTransformer;
|
||||||
|
use FireflyIII\Transformers\TransactionTransformer;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
@ -48,6 +54,7 @@ use League\Fractal\Serializer\JsonApiSerializer;
|
|||||||
*/
|
*/
|
||||||
class BudgetLimitController extends Controller
|
class BudgetLimitController extends Controller
|
||||||
{
|
{
|
||||||
|
use TransactionFilter;
|
||||||
/** @var BudgetRepositoryInterface The budget repository */
|
/** @var BudgetRepositoryInterface The budget repository */
|
||||||
private $repository;
|
private $repository;
|
||||||
|
|
||||||
@ -163,6 +170,52 @@ class BudgetLimitController extends Controller
|
|||||||
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show all transactions.
|
||||||
|
*
|
||||||
|
* @param Request $request
|
||||||
|
* @param BudgetLimit $budgetLimit
|
||||||
|
*
|
||||||
|
* @return JsonResponse
|
||||||
|
*/
|
||||||
|
public function transactions(Request $request, BudgetLimit $budgetLimit): JsonResponse
|
||||||
|
{
|
||||||
|
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
|
||||||
|
$type = $request->get('type') ?? 'default';
|
||||||
|
$this->parameters->set('type', $type);
|
||||||
|
|
||||||
|
$types = $this->mapTransactionTypes($this->parameters->get('type'));
|
||||||
|
$manager = new Manager();
|
||||||
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
||||||
|
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
||||||
|
|
||||||
|
/** @var User $admin */
|
||||||
|
$admin = auth()->user();
|
||||||
|
/** @var TransactionCollectorInterface $collector */
|
||||||
|
$collector = app(TransactionCollectorInterface::class);
|
||||||
|
$collector->setUser($admin);
|
||||||
|
$collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
|
||||||
|
$collector->setAllAssetAccounts();
|
||||||
|
$collector->setBudget($budgetLimit->budget);
|
||||||
|
$collector->setRange($budgetLimit->start_date, $budgetLimit->end_date);
|
||||||
|
|
||||||
|
if (\in_array(TransactionType::TRANSFER, $types, true)) {
|
||||||
|
$collector->removeFilter(InternalTransferFilter::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
$collector->setLimit($pageSize)->setPage($this->parameters->get('page'));
|
||||||
|
$collector->setTypes($types);
|
||||||
|
$paginator = $collector->getPaginatedTransactions();
|
||||||
|
$paginator->setPath(route('api.v1.budget_limits.transactions', [$budgetLimit->id]) . $this->buildParams());
|
||||||
|
$transactions = $paginator->getCollection();
|
||||||
|
$repository = app(JournalRepositoryInterface::class);
|
||||||
|
|
||||||
|
$resource = new FractalCollection($transactions, new TransactionTransformer($this->parameters, $repository), 'transactions');
|
||||||
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
||||||
|
|
||||||
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the specified resource in storage.
|
* Update the specified resource in storage.
|
||||||
*
|
*
|
||||||
|
@ -49,9 +49,11 @@ class BudgetLimitRequest extends Request
|
|||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'budget_id' => $this->integer('budget_id'),
|
'budget_id' => $this->integer('budget_id'),
|
||||||
'start_date' => $this->date('start_date'),
|
'start' => $this->date('start'),
|
||||||
'end_date' => $this->date('end_date'),
|
'end' => $this->date('end'),
|
||||||
'amount' => $this->string('amount'),
|
'amount' => $this->string('amount'),
|
||||||
|
'currency_id' => $this->integer('currency_id'),
|
||||||
|
'currency_code' => $this->string('currency_code'),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,9 +66,11 @@ class BudgetLimitRequest extends Request
|
|||||||
{
|
{
|
||||||
$rules = [
|
$rules = [
|
||||||
'budget_id' => 'required|exists:budgets,id|belongsToUser:budgets,id',
|
'budget_id' => 'required|exists:budgets,id|belongsToUser:budgets,id',
|
||||||
'start_date' => 'required|before:end_date|date',
|
'start' => 'required|before:end|date',
|
||||||
'end_date' => 'required|after:start_date|date',
|
'end' => 'required|after:start|date',
|
||||||
'amount' => 'required|more:0',
|
'amount' => 'required|more:0',
|
||||||
|
'currency_id' => 'numeric|exists:transaction_currencies,id',
|
||||||
|
'currency_code' => 'min:3|max:3|exists:transaction_currencies,code',
|
||||||
];
|
];
|
||||||
switch ($this->method()) {
|
switch ($this->method()) {
|
||||||
default:
|
default:
|
||||||
@ -76,6 +80,12 @@ class BudgetLimitRequest extends Request
|
|||||||
$rules['budget_id'] = 'required|exists:budgets,id|belongsToUser:budgets,id';
|
$rules['budget_id'] = 'required|exists:budgets,id|belongsToUser:budgets,id';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
// if request has a budget already, drop the rule.
|
||||||
|
$budget = $this->route()->parameter('budget');
|
||||||
|
if (null !== $budget) {
|
||||||
|
unset($rules['budget_id']);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return $rules;
|
return $rules;
|
||||||
}
|
}
|
||||||
|
@ -40,6 +40,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|||||||
* @property int $budget_id
|
* @property int $budget_id
|
||||||
* @property string spent
|
* @property string spent
|
||||||
* @property int $transaction_currency_id
|
* @property int $transaction_currency_id
|
||||||
|
* @property TransactionCurrency $transactionCurrency
|
||||||
*/
|
*/
|
||||||
class BudgetLimit extends Model
|
class BudgetLimit extends Model
|
||||||
{
|
{
|
||||||
|
@ -25,6 +25,7 @@ namespace FireflyIII\Repositories\Budget;
|
|||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Exception;
|
use Exception;
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
|
use FireflyIII\Factory\TransactionCurrencyFactory;
|
||||||
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
|
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
|
||||||
use FireflyIII\Models\AccountType;
|
use FireflyIII\Models\AccountType;
|
||||||
use FireflyIII\Models\AvailableBudget;
|
use FireflyIII\Models\AvailableBudget;
|
||||||
@ -108,7 +109,7 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
} catch (Exception|FatalThrowableError $e) {
|
} catch (Exception|FatalThrowableError $e) {
|
||||||
Log::debug(sprintf('Could not delete budget limit: %s', $e->getMessage()));
|
Log::debug(sprintf('Could not delete budget limit: %s', $e->getMessage()));
|
||||||
}
|
}
|
||||||
Budget::where('order',0)->update(['order' => 100]);
|
Budget::where('order', 0)->update(['order' => 100]);
|
||||||
|
|
||||||
// do the clean up by hand because Sqlite can be tricky with this.
|
// do the clean up by hand because Sqlite can be tricky with this.
|
||||||
$budgetLimits = BudgetLimit::orderBy('created_at', 'DESC')->get(['id', 'budget_id', 'start_date', 'end_date']);
|
$budgetLimits = BudgetLimit::orderBy('created_at', 'DESC')->get(['id', 'budget_id', 'start_date', 'end_date']);
|
||||||
@ -773,24 +774,33 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
/** @var Budget $budget */
|
/** @var Budget $budget */
|
||||||
$budget = $data['budget'];
|
$budget = $data['budget'];
|
||||||
|
|
||||||
// find limit with same date range.
|
// if no currency has been provided, use the user's default currency:
|
||||||
// if it exists, throw error.
|
/** @var TransactionCurrencyFactory $factory */
|
||||||
$limits = $budget->budgetlimits()
|
$factory = app(TransactionCurrencyFactory::class);
|
||||||
->where('budget_limits.start_date', $data['start_date']->format('Y-m-d 00:00:00'))
|
$currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null);
|
||||||
->where('budget_limits.end_date', $data['end_date']->format('Y-m-d 00:00:00'))
|
if (null === $currency) {
|
||||||
->get(['budget_limits.*'])->count();
|
$currency = app('amount')->getDefaultCurrencyByUser($this->user);
|
||||||
Log::debug(sprintf('Found %d budget limits.', $limits));
|
|
||||||
if ($limits > 0) {
|
|
||||||
throw new FireflyException('A budget limit for this budget, and this date range already exists. You must update the existing one.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// find limit with same date range.
|
||||||
|
// if it exists, return that one.
|
||||||
|
$limit = $budget->budgetlimits()
|
||||||
|
->where('budget_limits.start_date', $data['start']->format('Y-m-d 00:00:00'))
|
||||||
|
->where('budget_limits.end_date', $data['end']->format('Y-m-d 00:00:00'))
|
||||||
|
->where('budget_limits.transaction_currency_id', $currency->id)
|
||||||
|
->get(['budget_limits.*'])->first();
|
||||||
|
if (null !== $limit) {
|
||||||
|
return $limit;
|
||||||
|
}
|
||||||
Log::debug('No existing budget limit, create a new one');
|
Log::debug('No existing budget limit, create a new one');
|
||||||
|
|
||||||
// or create one and return it.
|
// or create one and return it.
|
||||||
$limit = new BudgetLimit;
|
$limit = new BudgetLimit;
|
||||||
$limit->budget()->associate($budget);
|
$limit->budget()->associate($budget);
|
||||||
$limit->start_date = $data['start_date']->format('Y-m-d 00:00:00');
|
$limit->start_date = $data['start']->format('Y-m-d 00:00:00');
|
||||||
$limit->end_date = $data['end_date']->format('Y-m-d 00:00:00');
|
$limit->end_date = $data['end']->format('Y-m-d 00:00:00');
|
||||||
$limit->amount = $data['amount'];
|
$limit->amount = $data['amount'];
|
||||||
|
$limit->transaction_currency_id = $currency->id;
|
||||||
$limit->save();
|
$limit->save();
|
||||||
Log::debug(sprintf('Created new budget limit with ID #%d and amount %s', $limit->id, $data['amount']));
|
Log::debug(sprintf('Created new budget limit with ID #%d and amount %s', $limit->id, $data['amount']));
|
||||||
|
|
||||||
@ -861,9 +871,19 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
$budget = $data['budget'];
|
$budget = $data['budget'];
|
||||||
|
|
||||||
$budgetLimit->budget()->associate($budget);
|
$budgetLimit->budget()->associate($budget);
|
||||||
$budgetLimit->start_date = $data['start_date']->format('Y-m-d 00:00:00');
|
$budgetLimit->start_date = $data['start']->format('Y-m-d 00:00:00');
|
||||||
$budgetLimit->end_date = $data['end_date']->format('Y-m-d 00:00:00');
|
$budgetLimit->end_date = $data['end']->format('Y-m-d 00:00:00');
|
||||||
$budgetLimit->amount = $data['amount'];
|
$budgetLimit->amount = $data['amount'];
|
||||||
|
|
||||||
|
// if no currency has been provided, use the user's default currency:
|
||||||
|
/** @var TransactionCurrencyFactory $factory */
|
||||||
|
$factory = app(TransactionCurrencyFactory::class);
|
||||||
|
$currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null);
|
||||||
|
if (null === $currency) {
|
||||||
|
$currency = app('amount')->getDefaultCurrencyByUser($this->user);
|
||||||
|
}
|
||||||
|
$budgetLimit->transaction_currency_id = $currency->id;
|
||||||
|
|
||||||
$budgetLimit->save();
|
$budgetLimit->save();
|
||||||
Log::debug(sprintf('Updated budget limit with ID #%d and amount %s', $budgetLimit->id, $data['amount']));
|
Log::debug(sprintf('Updated budget limit with ID #%d and amount %s', $budgetLimit->id, $data['amount']));
|
||||||
|
|
||||||
|
@ -24,7 +24,6 @@ declare(strict_types=1);
|
|||||||
namespace FireflyIII\Transformers;
|
namespace FireflyIII\Transformers;
|
||||||
|
|
||||||
use FireflyIII\Models\BudgetLimit;
|
use FireflyIII\Models\BudgetLimit;
|
||||||
use League\Fractal\Resource\Item;
|
|
||||||
use League\Fractal\TransformerAbstract;
|
use League\Fractal\TransformerAbstract;
|
||||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||||
|
|
||||||
@ -57,17 +56,35 @@ class BudgetLimitTransformer extends TransformerAbstract
|
|||||||
*/
|
*/
|
||||||
public function transform(BudgetLimit $budgetLimit): array
|
public function transform(BudgetLimit $budgetLimit): array
|
||||||
{
|
{
|
||||||
|
$currency = $budgetLimit->transactionCurrency;
|
||||||
|
$amount = $budgetLimit->amount;
|
||||||
|
$currencyId = null;
|
||||||
|
$currencyName = null;
|
||||||
|
$currencyCode = null;
|
||||||
|
$currencySymbol = null;
|
||||||
|
if (null !== $currency) {
|
||||||
|
$amount = round($budgetLimit->amount, $budgetLimit->transactionCurrency->decimal_places);
|
||||||
|
$currencyId = $currency->id;
|
||||||
|
$currencyName = $currency->name;
|
||||||
|
$currencyCode = $currency->code;
|
||||||
|
$currencySymbol = $currency->symbol;
|
||||||
|
}
|
||||||
$data = [
|
$data = [
|
||||||
'id' => (int)$budgetLimit->id,
|
'id' => (int)$budgetLimit->id,
|
||||||
'updated_at' => $budgetLimit->updated_at->toAtomString(),
|
'updated_at' => $budgetLimit->updated_at->toAtomString(),
|
||||||
'created_at' => $budgetLimit->created_at->toAtomString(),
|
'created_at' => $budgetLimit->created_at->toAtomString(),
|
||||||
'start_date' => $budgetLimit->start_date->format('Y-m-d'),
|
'start_date' => $budgetLimit->start_date->format('Y-m-d'),
|
||||||
'end_date' => $budgetLimit->end_date->format('Y-m-d'),
|
'end_date' => $budgetLimit->end_date->format('Y-m-d'),
|
||||||
'amount' => $budgetLimit->amount,
|
'budget_id' => $budgetLimit->budget_id,
|
||||||
|
'currency_id' => $currencyId,
|
||||||
|
'currency_code' => $currencyCode,
|
||||||
|
'currency_name' => $currencyName,
|
||||||
|
'currency_symbol' => $currencySymbol,
|
||||||
|
'amount' => $amount,
|
||||||
'links' => [
|
'links' => [
|
||||||
[
|
[
|
||||||
'rel' => 'self',
|
'rel' => 'self',
|
||||||
'uri' => '/budget_limits/' . $budgetLimit->id,
|
'uri' => '/budgets/limits/' . $budgetLimit->id,
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
@ -72,8 +72,9 @@ Route::group(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
Route::group(
|
Route::group(
|
||||||
['middleware' => ['auth:api', 'bindings'], 'namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'budget_limits', 'as' => 'api.v1.budget_limits.'],
|
['middleware' => ['auth:api', 'bindings'], 'namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'budgets/limits', 'as' => 'api.v1.budget_limits.'],
|
||||||
function () {
|
function () {
|
||||||
|
|
||||||
// Budget Limit API routes:
|
// Budget Limit API routes:
|
||||||
@ -82,22 +83,10 @@ Route::group(
|
|||||||
Route::get('{budgetLimit}', ['uses' => 'BudgetLimitController@show', 'as' => 'show']);
|
Route::get('{budgetLimit}', ['uses' => 'BudgetLimitController@show', 'as' => 'show']);
|
||||||
Route::put('{budgetLimit}', ['uses' => 'BudgetLimitController@update', 'as' => 'update']);
|
Route::put('{budgetLimit}', ['uses' => 'BudgetLimitController@update', 'as' => 'update']);
|
||||||
Route::delete('{budgetLimit}', ['uses' => 'BudgetLimitController@delete', 'as' => 'delete']);
|
Route::delete('{budgetLimit}', ['uses' => 'BudgetLimitController@delete', 'as' => 'delete']);
|
||||||
|
Route::get('{budgetLimit}/transactions', ['uses' => 'BudgetLimitController@transactions', 'as' => 'transactions']);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
Route::group(
|
|
||||||
['middleware' => ['auth:api', 'bindings'], 'namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'bills', 'as' => 'api.v1.bills.'], function () {
|
|
||||||
|
|
||||||
// Bills API routes:
|
|
||||||
Route::get('', ['uses' => 'BillController@index', 'as' => 'index']);
|
|
||||||
Route::post('', ['uses' => 'BillController@store', 'as' => 'store']);
|
|
||||||
Route::get('{bill}', ['uses' => 'BillController@show', 'as' => 'show']);
|
|
||||||
Route::put('{bill}', ['uses' => 'BillController@update', 'as' => 'update']);
|
|
||||||
Route::delete('{bill}', ['uses' => 'BillController@delete', 'as' => 'delete']);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
Route::group(
|
Route::group(
|
||||||
['middleware' => ['auth:api', 'bindings'], 'namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'budgets', 'as' => 'api.v1.budgets.'],
|
['middleware' => ['auth:api', 'bindings'], 'namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'budgets', 'as' => 'api.v1.budgets.'],
|
||||||
function () {
|
function () {
|
||||||
@ -108,9 +97,28 @@ Route::group(
|
|||||||
Route::get('{budget}', ['uses' => 'BudgetController@show', 'as' => 'show']);
|
Route::get('{budget}', ['uses' => 'BudgetController@show', 'as' => 'show']);
|
||||||
Route::put('{budget}', ['uses' => 'BudgetController@update', 'as' => 'update']);
|
Route::put('{budget}', ['uses' => 'BudgetController@update', 'as' => 'update']);
|
||||||
Route::delete('{budget}', ['uses' => 'BudgetController@delete', 'as' => 'delete']);
|
Route::delete('{budget}', ['uses' => 'BudgetController@delete', 'as' => 'delete']);
|
||||||
|
Route::get('{budget}/transactions', ['uses' => 'BudgetController@transactions', 'as' => 'transactions']);
|
||||||
|
Route::get('{budget}/limits', ['uses' => 'BudgetController@budgetLimits', 'as' => 'budget_limits']);
|
||||||
|
Route::post('{budget}/limits', ['uses' => 'BudgetController@storeBudgetLimit', 'as' => 'store_budget_limit']);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Route::group(
|
||||||
|
['middleware' => ['auth:api', 'bindings'], 'namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'bills', 'as' => 'api.v1.bills.'], function () {
|
||||||
|
|
||||||
|
// Bills API routes:
|
||||||
|
Route::get('', ['uses' => 'BillController@index', 'as' => 'index']);
|
||||||
|
Route::post('', ['uses' => 'BillController@store', 'as' => 'store']);
|
||||||
|
Route::get('{bill}', ['uses' => 'BillController@show', 'as' => 'show']);
|
||||||
|
Route::put('{bill}', ['uses' => 'BillController@update', 'as' => 'update']);
|
||||||
|
Route::delete('{bill}', ['uses' => 'BillController@delete', 'as' => 'delete']);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
Route::group(
|
Route::group(
|
||||||
['middleware' => ['auth:api', 'bindings'], 'namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'categories', 'as' => 'api.v1.categories.'],
|
['middleware' => ['auth:api', 'bindings'], 'namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'categories', 'as' => 'api.v1.categories.'],
|
||||||
function () {
|
function () {
|
||||||
|
Loading…
Reference in New Issue
Block a user