mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Full API coverage.
This commit is contained in:
@@ -26,6 +26,7 @@ namespace FireflyIII\Api\V1\Controllers\Chart;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Api\V1\Controllers\Controller;
|
||||
use FireflyIII\Api\V1\Requests\DateRequest;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
@@ -35,7 +36,6 @@ use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
use FireflyIII\Support\Http\Api\ApiSupport;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* Class AccountController
|
||||
@@ -50,6 +50,7 @@ class AccountController extends Controller
|
||||
|
||||
/**
|
||||
* AccountController constructor.
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
@@ -70,22 +71,20 @@ class AccountController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param DateRequest $request
|
||||
*
|
||||
* @return JsonResponse
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function expenseOverview(Request $request): JsonResponse
|
||||
public function expenseOverview(DateRequest $request): JsonResponse
|
||||
{
|
||||
// parameters for chart:
|
||||
$start = (string)$request->get('start');
|
||||
$end = (string)$request->get('end');
|
||||
if ('' === $start || '' === $end) {
|
||||
throw new FireflyException('Start and end are mandatory parameters.');
|
||||
}
|
||||
$dates = $request->getAll();
|
||||
/** @var Carbon $start */
|
||||
$start = Carbon::createFromFormat('Y-m-d', $start);
|
||||
$end = Carbon::createFromFormat('Y-m-d', $end);
|
||||
$start = $dates['start'];
|
||||
/** @var Carbon $end */
|
||||
$end = $dates['end'];
|
||||
|
||||
$start->subDay();
|
||||
|
||||
// prep some vars:
|
||||
@@ -159,31 +158,30 @@ class AccountController extends Controller
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param DateRequest $request
|
||||
*
|
||||
* @return JsonResponse
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function overview(Request $request): JsonResponse
|
||||
public function overview(DateRequest $request): JsonResponse
|
||||
{
|
||||
// parameters for chart:
|
||||
$start = (string)$request->get('start');
|
||||
$end = (string)$request->get('end');
|
||||
if ('' === $start || '' === $end) {
|
||||
throw new FireflyException('Start and end are mandatory parameters.');
|
||||
}
|
||||
|
||||
$start = Carbon::createFromFormat('Y-m-d', $start);
|
||||
$end = Carbon::createFromFormat('Y-m-d', $end);
|
||||
$dates = $request->getAll();
|
||||
/** @var Carbon $start */
|
||||
$start = $dates['start'];
|
||||
/** @var Carbon $end */
|
||||
$end = $dates['end'];
|
||||
|
||||
// user's preferences
|
||||
$defaultSet = $this->repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET])->pluck('id')->toArray();
|
||||
$defaultSet = $this->repository->getAccountsByType([AccountType::ASSET])->pluck('id')->toArray();
|
||||
$frontPage = app('preferences')->get('frontPageAccounts', $defaultSet);
|
||||
$default = app('amount')->getDefaultCurrency();
|
||||
// @codeCoverageIgnoreStart
|
||||
if (0 === count($frontPage->data)) {
|
||||
$frontPage->data = $defaultSet;
|
||||
$frontPage->save();
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
|
||||
// get accounts:
|
||||
$accounts = $this->repository->getAccountsById($frontPage->data);
|
||||
@@ -192,7 +190,7 @@ class AccountController extends Controller
|
||||
foreach ($accounts as $account) {
|
||||
$currency = $this->repository->getAccountCurrency($account);
|
||||
if (null === $currency) {
|
||||
$currency = $default;
|
||||
$currency = $default; // @codeCoverageIgnore
|
||||
}
|
||||
$currentSet = [
|
||||
'label' => $account->name,
|
||||
@@ -223,22 +221,20 @@ class AccountController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param DateRequest $request
|
||||
*
|
||||
* @return JsonResponse
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function revenueOverview(Request $request): JsonResponse
|
||||
public function revenueOverview(DateRequest $request): JsonResponse
|
||||
{
|
||||
// parameters for chart:
|
||||
$start = (string)$request->get('start');
|
||||
$end = (string)$request->get('end');
|
||||
if ('' === $start || '' === $end) {
|
||||
throw new FireflyException('Start and end are mandatory parameters.');
|
||||
}
|
||||
$dates = $request->getAll();
|
||||
/** @var Carbon $start */
|
||||
$start = Carbon::createFromFormat('Y-m-d', $start);
|
||||
$end = Carbon::createFromFormat('Y-m-d', $end);
|
||||
$start = $dates['start'];
|
||||
/** @var Carbon $end */
|
||||
$end = $dates['end'];
|
||||
|
||||
$start->subDay();
|
||||
|
||||
// prep some vars:
|
||||
@@ -269,7 +265,8 @@ class AccountController extends Controller
|
||||
$tempData[] = [
|
||||
'name' => $accountNames[$accountId],
|
||||
'difference' => bcmul($diff, '-1'),
|
||||
'diff_float' => (float)$diff * -1,
|
||||
// For some reason this line is never covered in code coverage:
|
||||
'diff_float' => ((float)$diff) * -1, // @codeCoverageIgnore
|
||||
'currency_id' => $currencyId,
|
||||
];
|
||||
}
|
||||
|
@@ -42,6 +42,7 @@ class AvailableBudgetController extends Controller
|
||||
|
||||
/**
|
||||
* AvailableBudgetController constructor.
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
|
@@ -26,6 +26,7 @@ namespace FireflyIII\Api\V1\Controllers\Chart;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Api\V1\Controllers\Controller;
|
||||
use FireflyIII\Api\V1\Requests\DateRequest;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
||||
use FireflyIII\User;
|
||||
@@ -43,6 +44,7 @@ class CategoryController extends Controller
|
||||
|
||||
/**
|
||||
* AccountController constructor.
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
@@ -61,23 +63,21 @@ class CategoryController extends Controller
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param DateRequest $request
|
||||
*
|
||||
* @return JsonResponse
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function overview(Request $request): JsonResponse
|
||||
public function overview(DateRequest $request): JsonResponse
|
||||
{
|
||||
// parameters for chart:
|
||||
$start = (string)$request->get('start');
|
||||
$end = (string)$request->get('end');
|
||||
if ('' === $start || '' === $end) {
|
||||
throw new FireflyException('Start and end are mandatory parameters.');
|
||||
}
|
||||
$dates = $request->getAll();
|
||||
/** @var Carbon $start */
|
||||
$start = Carbon::createFromFormat('Y-m-d', $start);
|
||||
$start = $dates['start'];
|
||||
/** @var Carbon $end */
|
||||
$end = Carbon::createFromFormat('Y-m-d', $end);
|
||||
$end = $dates['end'];
|
||||
|
||||
|
||||
$tempData = [];
|
||||
$spent = $this->categoryRepository->spentInPeriodPerCurrency(new Collection, new Collection, $start, $end);
|
||||
$earned = $this->categoryRepository->earnedInPeriodPerCurrency(new Collection, new Collection, $start, $end);
|
||||
@@ -129,7 +129,7 @@ class CategoryController extends Controller
|
||||
'entries' => [],
|
||||
];
|
||||
}
|
||||
$amount = round($income['spent'], $decimalPlaces);
|
||||
$amount = round($income['earned'], $decimalPlaces);
|
||||
$categories[$categoryName] = isset($categories[$categoryName]) ? $categories[$categoryName] + $amount : $amount;
|
||||
$tempData[$key]['entries'][$categoryName]
|
||||
= $amount;
|
||||
|
@@ -27,6 +27,7 @@ namespace FireflyIII\Api\V1\Controllers;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Exception;
|
||||
use FireflyIII\Api\V1\Requests\DateRequest;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
||||
use FireflyIII\Helpers\Report\NetWorthInterface;
|
||||
@@ -91,18 +92,13 @@ class SummaryController extends Controller
|
||||
* @throws FireflyException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function basic(Request $request): JsonResponse
|
||||
public function basic(DateRequest $request): JsonResponse
|
||||
{
|
||||
// parameters for boxes:
|
||||
$start = (string)$request->get('start');
|
||||
$end = (string)$request->get('end');
|
||||
if ('' === $start || '' === $end) {
|
||||
throw new FireflyException('Start and end are mandatory parameters.');
|
||||
}
|
||||
/** @var Carbon $start */
|
||||
$start = Carbon::createFromFormat('Y-m-d', $start);
|
||||
/** @var Carbon $end */
|
||||
$end = Carbon::createFromFormat('Y-m-d', $end);
|
||||
$dates = $request->getAll();
|
||||
$start = $dates['start'];
|
||||
$end = $dates['end'];
|
||||
|
||||
// balance information:
|
||||
$balanceData = $this->getBalanceInformation($start, $end);
|
||||
$billData = $this->getBillInformation($start, $end);
|
||||
@@ -366,7 +362,7 @@ class SummaryController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
return 0.0;
|
||||
return 0.0; // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -404,7 +400,7 @@ class SummaryController extends Controller
|
||||
|
||||
$netWorthSet = $netWorthHelper->getNetWorthByCurrency($filtered, $date);
|
||||
$return = [];
|
||||
foreach ($netWorthSet as $index => $data) {
|
||||
foreach ($netWorthSet as $data) {
|
||||
/** @var TransactionCurrency $currency */
|
||||
$currency = $data['currency'];
|
||||
$amount = round($data['balance'], $currency->decimal_places);
|
||||
|
@@ -24,6 +24,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Api\V1\Controllers;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Api\V1\Requests\DateRequest;
|
||||
use FireflyIII\Api\V1\Requests\TagRequest;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
||||
@@ -36,6 +37,7 @@ use FireflyIII\User;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Collection;
|
||||
use League\Fractal\Manager;
|
||||
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
||||
use League\Fractal\Resource\Collection as FractalCollection;
|
||||
@@ -53,7 +55,7 @@ class TagController extends Controller
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* RuleController constructor.
|
||||
* TagController constructor.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
@@ -74,54 +76,23 @@ class TagController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param DateRequest $request
|
||||
*
|
||||
* @return JsonResponse
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function cloud(Request $request): JsonResponse
|
||||
public function cloud(DateRequest $request): JsonResponse
|
||||
{
|
||||
// parameters for cloud:
|
||||
$start = (string)$request->get('start');
|
||||
$end = (string)$request->get('end');
|
||||
if ('' === $start || '' === $end) {
|
||||
throw new FireflyException('Start and end are mandatory parameters.');
|
||||
}
|
||||
/** @var Carbon $start */
|
||||
$start = Carbon::createFromFormat('Y-m-d', $start);
|
||||
/** @var Carbon $end */
|
||||
$end = Carbon::createFromFormat('Y-m-d', $end);
|
||||
// parameters for boxes:
|
||||
$dates = $request->getAll();
|
||||
$start = $dates['start'];
|
||||
$end = $dates['end'];
|
||||
|
||||
// get all tags:
|
||||
$tags = $this->repository->get();
|
||||
$min = null;
|
||||
$max = 0;
|
||||
$return = [
|
||||
'tags' => [],
|
||||
];
|
||||
/** @var Tag $tag */
|
||||
foreach ($tags as $tag) {
|
||||
$earned = (float)$this->repository->earnedInPeriod($tag, $start, $end);
|
||||
$spent = (float)$this->repository->spentInPeriod($tag, $start, $end);
|
||||
$size = ($spent * -1) + $earned;
|
||||
$min = $min ?? $size;
|
||||
if ($size > 0) {
|
||||
$max = $size > $max ? $size : $max;
|
||||
$return['tags'][] = [
|
||||
'tag' => $tag->tag,
|
||||
'id' => $tag->id,
|
||||
'size' => $size,
|
||||
];
|
||||
}
|
||||
}
|
||||
foreach ($return['tags'] as $index => $info) {
|
||||
$return['tags'][$index]['relative'] = $return['tags'][$index]['size'] / $max;
|
||||
}
|
||||
$return['min'] = $min;
|
||||
$return['max'] = $max;
|
||||
$tags = $this->repository->get();
|
||||
$cloud = $this->getTagCloud($tags, $start, $end);
|
||||
|
||||
|
||||
return response()->json($return);
|
||||
return response()->json($cloud);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -305,4 +276,54 @@ class TagController extends Controller
|
||||
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $tags
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @return array
|
||||
*/
|
||||
private function getTagCloud(Collection $tags, Carbon $start, Carbon $end): array
|
||||
{
|
||||
$min = null;
|
||||
$max = 0;
|
||||
$cloud = [
|
||||
'tags' => [],
|
||||
];
|
||||
/** @var Tag $tag */
|
||||
foreach ($tags as $tag) {
|
||||
$earned = (float)$this->repository->earnedInPeriod($tag, $start, $end);
|
||||
$spent = (float)$this->repository->spentInPeriod($tag, $start, $end);
|
||||
$size = ($spent * -1) + $earned;
|
||||
$min = $min ?? $size;
|
||||
if ($size > 0) {
|
||||
$max = $size > $max ? $size : $max;
|
||||
$cloud['tags'][] = [
|
||||
'tag' => $tag->tag,
|
||||
'id' => $tag->id,
|
||||
'size' => $size,
|
||||
];
|
||||
}
|
||||
}
|
||||
$cloud = $this->analyseTagCloud($cloud, $min, $max);
|
||||
|
||||
return $cloud;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $cloud
|
||||
* @param float $min
|
||||
* @param float $max
|
||||
* @return array
|
||||
*/
|
||||
private function analyseTagCloud(array $cloud, float $min, float $max): array
|
||||
{
|
||||
foreach (array_keys($cloud['tags']) as $index) {
|
||||
$cloud['tags'][$index]['relative'] = round($cloud['tags'][$index]['size'] / $max, 4);
|
||||
}
|
||||
$cloud['min'] = $min;
|
||||
$cloud['max'] = $max;
|
||||
|
||||
return $cloud;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user