From 1b27f6143c7d6e47b0a510dbfcbf95f084485a85 Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 27 Nov 2020 06:13:46 +0100 Subject: [PATCH] Expand API with still undocumented features --- .../Insight/Expense/DateController.php | 130 ++++++++++++++++++ app/Api/V1/Controllers/SummaryController.php | 24 +--- routes/api.php | 13 ++ 3 files changed, 149 insertions(+), 18 deletions(-) create mode 100644 app/Api/V1/Controllers/Insight/Expense/DateController.php diff --git a/app/Api/V1/Controllers/Insight/Expense/DateController.php b/app/Api/V1/Controllers/Insight/Expense/DateController.php new file mode 100644 index 0000000000..650e674fb9 --- /dev/null +++ b/app/Api/V1/Controllers/Insight/Expense/DateController.php @@ -0,0 +1,130 @@ +. + */ + +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\AccountType; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; +use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; +use FireflyIII\Support\Http\Api\ApiSupport; +use FireflyIII\User; +use Illuminate\Http\JsonResponse; + +/** + * Class DateController + * + * Shows expense information grouped or limited by date. + * Ie. all expenses grouped by account + currency. + */ +class DateController extends Controller +{ + use ApiSupport; + + private CurrencyRepositoryInterface $currencyRepository; + private AccountRepositoryInterface $repository; + + + /** + * AccountController constructor. + * + * @codeCoverageIgnore + */ + public function __construct() + { + parent::__construct(); + $this->middleware( + function ($request, $next) { + /** @var User $user */ + $user = auth()->user(); + $this->repository = app(AccountRepositoryInterface::class); + $this->repository->setUser($user); + + $this->currencyRepository = app(CurrencyRepositoryInterface::class); + $this->currencyRepository->setUser($user); + + return $next($request); + } + ); + } + + /** + * + */ + public function basic(DateRequest $request): JsonResponse + { + // parameters for chart: + $dates = $request->getAll(); + /** @var Carbon $start */ + $start = $dates['start']; + /** @var Carbon $end */ + $end = $dates['end']; + + $start->subDay(); + + // prep some vars: + $currencies = []; + $chartData = []; + $tempData = []; + + // grab all accounts and names + $accounts = $this->repository->getAccountsByType([AccountType::EXPENSE]); + $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' => $currencyId, + 'currency_code' => $currencies[$currencyId]->code, + ]; + } + } + } + + + // sort temp array by amount. + $amounts = array_column($tempData, 'difference_float'); + array_multisort($amounts, SORT_ASC, $tempData); + + return response()->json($tempData); + } + +} \ No newline at end of file diff --git a/app/Api/V1/Controllers/SummaryController.php b/app/Api/V1/Controllers/SummaryController.php index 0ec4ac1daa..56ebbff539 100644 --- a/app/Api/V1/Controllers/SummaryController.php +++ b/app/Api/V1/Controllers/SummaryController.php @@ -47,24 +47,12 @@ use Illuminate\Http\JsonResponse; */ class SummaryController extends Controller { - /** @var AvailableBudgetRepositoryInterface */ - private $abRepository; - - /** @var AccountRepositoryInterface */ - private $accountRepository; - - /** @var BillRepositoryInterface */ - private $billRepository; - - /** @var BudgetRepositoryInterface */ - private $budgetRepository; - - /** @var CurrencyRepositoryInterface */ - private $currencyRepos; - - /** @var OperationsRepositoryInterface */ - private $opsRepository; - + private AvailableBudgetRepositoryInterface $abRepository; + private AccountRepositoryInterface $accountRepository; + private BillRepositoryInterface $billRepository; + private BudgetRepositoryInterface $budgetRepository; + private CurrencyRepositoryInterface $currencyRepos; + private OperationsRepositoryInterface $opsRepository; /** * SummaryController constructor. diff --git a/routes/api.php b/routes/api.php index b2cd3eb29b..04d286774c 100644 --- a/routes/api.php +++ b/routes/api.php @@ -395,6 +395,7 @@ Route::group( } ); +// destroy data route. Route::group( ['namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'data', 'as' => 'api.v1.data.',], @@ -405,6 +406,18 @@ Route::group( } ); +// INSIGHT +Route::group( + ['namespace' => 'FireflyIII\Api\V1\Controllers\Insight', 'prefix' => 'insight', + 'as' => 'api.v1.insight.',], + static function () { + + // Insight in expenses. + // Insight in expenses by date. + Route::get('expense/date/basic', ['uses' => 'Expense\DateController@basic', 'as' => 'expense.date.basic']); + } +); + Route::group( ['namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'currencies', 'as' => 'api.v1.currencies.',],