Updated API code.

This commit is contained in:
James Cole
2021-03-05 07:03:28 +01:00
parent b0d4ebbf4b
commit 30d653faee
24 changed files with 521 additions and 78 deletions

View File

@@ -90,7 +90,7 @@ class AccountController extends Controller
}
$return[] = [
'id' => $account->id,
'id' => (string)$account->id,
'name' => $account->name,
'name_with_balance' => $nameWithBalance,
'type' => $account->accountType->type,

View File

@@ -69,7 +69,7 @@ class BillController extends Controller
$filtered = $result->map(
static function (Bill $item) {
return [
'id' => $item->id,
'id' => (string)$item->id,
'name' => $item->name,
];
}

View File

@@ -69,7 +69,7 @@ class BudgetController extends Controller
$filtered = $result->map(
static function (Budget $item) {
return [
'id' => $item->id,
'id' => (string)$item->id,
'name' => $item->name,
];
}

View File

@@ -68,7 +68,7 @@ class CategoryController extends Controller
$filtered = $result->map(
static function (Category $item) {
return [
'id' => $item->id,
'id' => (string)$item->id,
'name' => $item->name,
];
}

View File

@@ -71,7 +71,7 @@ class CurrencyController extends Controller
/** @var TransactionCurrency $currency */
foreach ($collection as $currency) {
$result[] = [
'id' => $currency->id,
'id' => (string) $currency->id,
'name' => sprintf('%s (%s)', $currency->name, $currency->code),
'code' => $currency->code,
'symbol' => $currency->symbol,
@@ -96,7 +96,7 @@ class CurrencyController extends Controller
/** @var TransactionCurrency $currency */
foreach ($collection as $currency) {
$result[] = [
'id' => $currency->id,
'id' => (string) $currency->id,
'name' => $currency->name,
'code' => $currency->code,
'symbol' => $currency->symbol,

View File

@@ -71,7 +71,7 @@ class ObjectGroupController extends Controller
/** @var ObjectGroup $account */
foreach ($result as $objectGroup) {
$return[] = [
'id' => $objectGroup->id,
'id' => (string)$objectGroup->id,
'name' => $objectGroup->title,
'title' => $objectGroup->title,
];

View File

@@ -78,7 +78,7 @@ class PiggyBankController extends Controller
foreach ($piggies as $piggy) {
$currency = $this->accountRepository->getAccountCurrency($piggy->account) ?? $defaultCurrency;
$response[] = [
'id' => $piggy->id,
'id' => (string)$piggy->id,
'name' => $piggy->name,
'currency_id' => $currency->id,
'currency_name' => $currency->name,
@@ -106,7 +106,7 @@ class PiggyBankController extends Controller
$currency = $this->accountRepository->getAccountCurrency($piggy->account) ?? $defaultCurrency;
$currentAmount = $this->piggyRepository->getRepetition($piggy)->currentamount ?? '0';
$response[] = [
'id' => $piggy->id,
'id' => (string)$piggy->id,
'name' => $piggy->name,
'name_with_balance' => sprintf(
'%s (%s / %s)', $piggy->name, app('amount')->formatAnything($currency, $currentAmount, false),

View File

@@ -25,11 +25,56 @@ namespace FireflyIII\Api\V1\Controllers\Autocomplete;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\Autocomplete\AutocompleteRequest;
use FireflyIII\Models\Rule;
use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface;
use Illuminate\Http\JsonResponse;
/**
* Class RecurrenceController
*/
class RecurrenceController extends Controller
{
private RecurringRepositoryInterface $repository;
/**
* RecurrenceController constructor.
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
$this->repository = app(RecurringRepositoryInterface::class);
$this->repository->setUser(auth()->user());
return $next($request);
}
);
}
/**
* @param AutocompleteRequest $request
*
* @return JsonResponse
*/
public function recurring(AutocompleteRequest $request): JsonResponse
{
$data = $request->getData();
$rules = $this->repository->searchRecurrence($data['query'], $data['limit']);
$response = [];
/** @var Rule $rule */
foreach ($rules as $rule) {
$response[] = [
'id' => (string)$rule->id,
'name' => $rule->title,
'description' => $rule->description,
];
}
return response()->json($response);
}
}

View File

@@ -25,11 +25,56 @@ namespace FireflyIII\Api\V1\Controllers\Autocomplete;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\Autocomplete\AutocompleteRequest;
use FireflyIII\Models\Rule;
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
use Illuminate\Http\JsonResponse;
/**
* Class RuleController
*/
class RuleController extends Controller
{
private RuleRepositoryInterface $repository;
/**
* RuleController constructor.
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
$this->repository = app(RuleRepositoryInterface::class);
$this->repository->setUser(auth()->user());
return $next($request);
}
);
}
/**
* @param AutocompleteRequest $request
*
* @return JsonResponse
*/
public function rules(AutocompleteRequest $request): JsonResponse
{
$data = $request->getData();
$rules = $this->repository->searchRule($data['query'], $data['limit']);
$response = [];
/** @var Rule $rule */
foreach ($rules as $rule) {
$response[] = [
'id' => (string)$rule->id,
'name' => $rule->title,
'description' => $rule->description,
];
}
return response()->json($response);
}
}

View File

@@ -25,11 +25,55 @@ namespace FireflyIII\Api\V1\Controllers\Autocomplete;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\Autocomplete\AutocompleteRequest;
use FireflyIII\Models\RuleGroup;
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
use Illuminate\Http\JsonResponse;
/**
* Class RuleGroupController
*/
class RuleGroupController extends Controller
{
private RuleGroupRepositoryInterface $repository;
/**
* RuleGroupController constructor.
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
$this->repository = app(RuleGroupRepositoryInterface::class);
$this->repository->setUser(auth()->user());
return $next($request);
}
);
}
/**
* @param AutocompleteRequest $request
*
* @return JsonResponse
*/
public function ruleGroups(AutocompleteRequest $request): JsonResponse
{
$data = $request->getData();
$groups = $this->repository->searchRuleGroup($data['query'], $data['limit']);
$response = [];
/** @var RuleGroup $group */
foreach ($groups as $group) {
$response[] = [
'id' => (string)$group->id,
'name' => $group->title,
'description' => $group->description,
];
}
return response()->json($response);
}
}

View File

@@ -71,7 +71,7 @@ class TagController extends Controller
/** @var Tag $tag */
foreach ($result as $tag) {
$array[] = [
'id' => $tag->id,
'id' => (string)$tag->id,
'name' => $tag->tag,
'tag' => $tag->tag,
];

View File

@@ -81,8 +81,8 @@ class TransactionController extends Controller
/** @var TransactionJournal $journal */
foreach ($filtered as $journal) {
$array[] = [
'id' => $journal->id,
'transaction_group_id' => $journal->transaction_group_id,
'id' => (string)$journal->id,
'transaction_group_id' => (string)$journal->transaction_group_id,
'name' => $journal->description,
'description' => $journal->description,
];
@@ -120,8 +120,8 @@ class TransactionController extends Controller
/** @var TransactionJournal $journal */
foreach ($result as $journal) {
$array[] = [
'id' => $journal->id,
'transaction_group_id' => $journal->transaction_group_id,
'id' => (string)$journal->id,
'transaction_group_id' => (string)$journal->transaction_group_id,
'name' => sprintf('#%d: %s', $journal->transaction_group_id, $journal->description),
'description' => sprintf('#%d: %s', $journal->transaction_group_id, $journal->description),
];

View File

@@ -69,7 +69,7 @@ class TransactionTypeController extends Controller
foreach ($types as $type) {
// different key for consistency.
$array[] = [
'id' => $type->id,
'id' =>(string) $type->id,
'name' => $type->type,
'type' => $type->type,
];

View File

@@ -193,7 +193,7 @@ class AccountController extends Controller
}
$currentSet = [
'label' => $account->name,
'currency_id' => $currency->id,
'currency_id' => (string) $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,

View File

@@ -143,6 +143,65 @@ class AccountController extends Controller
}
// sort temp array by amount.
$amounts = array_column($tempData, 'difference_float');
array_multisort($amounts, SORT_ASC, $tempData);
return response()->json($tempData);
}
/**
* @param DateRequest $request
*
* @return JsonResponse
*/
public function asset(DateRequest $request): JsonResponse
{
$dates = $request->getAll();
/** @var Carbon $start */
$start = $dates['start'];
/** @var Carbon $end */
$end = $dates['end'];
$start->subDay();
// prep some vars:
$currencies = [];
$tempData = [];
// grab all accounts and names
$accounts = $this->repository->getAccountsByType([AccountType::ASSET]);
$accountNames = $this->extractNames($accounts);
$startBalances = app('steam')->balancesPerCurrencyByAccounts($accounts, $start);
$endBalances = app('steam')->balancesPerCurrencyByAccounts($accounts, $end);
// loop the end balances. This is an array for each account ($expenses)
foreach ($endBalances as $accountId => $expenses) {
$accountId = (int)$accountId;
// loop each expense entry (each entry can be a different currency).
foreach ($expenses as $currencyId => $endAmount) {
$currencyId = (int)$currencyId;
// see if there is an accompanying start amount.
// grab the difference and find the currency.
$startAmount = $startBalances[$accountId][$currencyId] ?? '0';
$diff = bcsub($endAmount, $startAmount);
$currencies[$currencyId] = $currencies[$currencyId] ?? $this->currencyRepository->findNull($currencyId);
if (0 !== bccomp($diff, '0')) {
// store the values in a temporary array.
$tempData[] = [
'id' => $accountId,
'name' => $accountNames[$accountId],
'difference' => bcmul($diff, '-1'),
'difference_float' => ((float)$diff) * -1,
'currency_id' => (string) $currencyId,
'currency_code' => $currencies[$currencyId]->code,
];
}
}
}
// sort temp array by amount.
$amounts = array_column($tempData, 'difference_float');
array_multisort($amounts, SORT_ASC, $tempData);

View File

@@ -0,0 +1,95 @@
<?php
/*
* BudgetController.php
* Copyright (c) 2021 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Api\V1\Controllers\Insight\Expense;
use Carbon\Carbon;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\DateRequest;
use FireflyIII\Models\Budget;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;
/**
* Class BudgetController
*/
class BudgetController extends Controller
{
private OperationsRepositoryInterface $opsRepository;
private BudgetRepositoryInterface $repository;
/**
* AccountController constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
$this->opsRepository = app(OperationsRepositoryInterface::class);
$this->repository = app(BudgetRepositoryInterface::class);
$this->opsRepository->setUser(auth()->user());
$this->repository->setUser(auth()->user());
return $next($request);
}
);
}
/**
* @param DateRequest $request
*
* @return JsonResponse
*/
public function budget(DateRequest $request): JsonResponse
{
$dates = $request->getAll();
/** @var Carbon $start */
$start = $dates['start'];
/** @var Carbon $end */
$end = $dates['end'];
$result = [];
$budgets = $this->repository->getActiveBudgets();
/** @var Budget $budget */
foreach ($budgets as $budget) {
$expenses = $this->opsRepository->sumExpenses($start, $end, null, new Collection([$budget]), null);
/** @var array $expense */
foreach ($expenses as $expense) {
$result[] = [
'id' => (string)$budget->id,
'name' => $budget->name,
'difference' => $expense['sum'],
'difference_float' => (float)$expense['sum'],
'currency_id' => (string)$expense['currency_id'],
'currency_code' => $expense['currency_code'],
];
}
}
return response()->json($result);
}
}

View File

@@ -0,0 +1,85 @@
<?php
/*
* PeriodController.php
* Copyright (c) 2021 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Api\V1\Controllers\Insight\Expense;
use Carbon\Carbon;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\DateRequest;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\TransactionType;
use Illuminate\Http\JsonResponse;
/**
* Class PeriodController
*/
class PeriodController extends Controller
{
/**
* @param DateRequest $request
*
* @return JsonResponse
*/
public function total(DateRequest $request): JsonResponse
{
$dates = $request->getAll();
/** @var Carbon $start */
$start = $dates['start'];
/** @var Carbon $end */
$end = $dates['end'];
$response = [];
// collect all expenses in this period (regardless of type)
$collector = app(GroupCollectorInterface::class);
$collector->setTypes([TransactionType::WITHDRAWAL])->setRange($start, $end);
$genericSet = $collector->getExtractedJournals();
foreach ($genericSet as $journal) {
$currencyId = (int)$journal['currency_id'];
$foreignCurrencyId = (int)$journal['foreign_currency_id'];
if (0 !== $currencyId) {
$response[$currencyId] = $response[$currencyId] ?? [
'difference' => '0',
'difference_float' => 0,
'currency_id' => (string)$currencyId,
'currency_code' => $journal['currency_code'],
];
$response[$currencyId]['difference'] = bcadd($response[$currencyId]['difference'], $journal['amount']);
$response[$currencyId]['difference_float'] = (float)$response[$currencyId]['difference'];
}
if (0 !== $foreignCurrencyId) {
$response[$foreignCurrencyId] = $response[$foreignCurrencyId] ?? [
'difference' => '0',
'difference_float' => 0,
'currency_id' => (string)$foreignCurrencyId,
'currency_code' => $journal['foreign_currency_code'],
];
$response[$foreignCurrencyId]['difference'] = bcadd($response[$foreignCurrencyId]['difference'], $journal['foreign_amount']);
$response[$foreignCurrencyId]['difference_float'] = (float)$response[$foreignCurrencyId]['difference'];
}
}
return response()->json(array_values($response));
}
}

View File

@@ -53,6 +53,7 @@ use Log;
class RecurringRepository implements RecurringRepositoryInterface
{
use CalculateRangeOccurrences, CalculateXOccurrences, CalculateXOccurrencesSince, FiltersWeekends;
private User $user;
@@ -522,6 +523,7 @@ class RecurringRepository implements RecurringRepositoryInterface
// filter out everything if "repeat_until" is set.
$repeatUntil = $repetition->recurrence->repeat_until;
return $this->filterMaxDate($repeatUntil, $occurrences);
}
@@ -560,16 +562,16 @@ class RecurringRepository implements RecurringRepositoryInterface
public function totalTransactions(Recurrence $recurrence, RecurrenceRepetition $repetition): int
{
// if repeat = null just return 0.
if (null === $recurrence->repeat_until && 0 === (int) $recurrence->repetitions) {
if (null === $recurrence->repeat_until && 0 === (int)$recurrence->repetitions) {
return 0;
}
// expect X transactions then stop. Return that number
if (null === $recurrence->repeat_until && 0 !== (int) $recurrence->repetitions) {
return (int) $recurrence->repetitions;
if (null === $recurrence->repeat_until && 0 !== (int)$recurrence->repetitions) {
return (int)$recurrence->repetitions;
}
// need to calculate, this depends on the repetition:
if (null !== $recurrence->repeat_until && 0 === (int) $recurrence->repetitions) {
if (null !== $recurrence->repeat_until && 0 === (int)$recurrence->repetitions) {
$occurrences = $this->getOccurrencesInRange($repetition, $recurrence->first_date ?? today(), $recurrence->repeat_until);
return count($occurrences);
@@ -577,4 +579,19 @@ class RecurringRepository implements RecurringRepositoryInterface
return 0;
}
/**
* @inheritDoc
*/
public function searchRecurrence(string $query, int $limit): Collection
{
$search = $this->user->recurrences();
if ('' !== $query) {
$search->where('recurrences.title', 'LIKE', sprintf('%%%s%%', $query));
}
$search
->orderBy('recurrences.title', 'ASC');
return $search->take($limit)->get(['id', 'title', 'description']);
}
}

View File

@@ -44,6 +44,14 @@ interface RecurringRepositoryInterface
*/
public function destroyAll(): void;
/**
* @param string $query
* @param int $limit
*
* @return Collection
*/
public function searchRecurrence(string $query, int $limit): Collection;
/**
* Calculate how many transactions are to be expected from this recurrence.
*

View File

@@ -565,4 +565,19 @@ class RuleRepository implements RuleRepositoryInterface
return implode(' ', $params);
}
/**
* @inheritDoc
*/
public function searchRule(string $query, int $limit): Collection
{
$search = $this->user->rules();
if ('' !== $query) {
$search->where('rules.title', 'LIKE', sprintf('%%%s%%', $query));
}
$search->orderBy('rules.order', 'ASC')
->orderBy('rules.title', 'ASC');
return $search->take($limit)->get(['id','title','description']);
}
}

View File

@@ -39,6 +39,14 @@ interface RuleRepositoryInterface
*/
public function count(): int;
/**
* @param string $query
* @param int $limit
*
* @return Collection
*/
public function searchRule(string $query, int $limit): Collection;
/**
* Return search query for rule.
*

View File

@@ -388,4 +388,19 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
$group->delete();
}
}
/**
* @inheritDoc
*/
public function searchRuleGroup(string $query, int $limit): Collection
{
$search = $this->user->ruleGroups();
if ('' !== $query) {
$search->where('rule_groups.title', 'LIKE', sprintf('%%%s%%', $query));
}
$search->orderBy('rule_groups.order', 'ASC')
->orderBy('rule_groups.title', 'ASC');
return $search->take($limit)->get(['id','title','description']);
}
}

View File

@@ -36,6 +36,14 @@ interface RuleGroupRepositoryInterface
*/
public function destroyAll(): void;
/**
* @param string $query
* @param int $limit
*
* @return Collection
*/
public function searchRuleGroup(string $query, int $limit): Collection;
/**
* @return int
*/
@@ -155,7 +163,7 @@ interface RuleGroupRepositoryInterface
/**
* @param RuleGroup $ruleGroup
* @param array $data
* @param array $data
*
* @return RuleGroup
*/