mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-03 11:08:28 +00:00
Update category overview to be multi-currency aware.
This commit is contained in:
@@ -81,7 +81,7 @@ class BudgetController extends Controller
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function dashboard(DateRequest $request): JsonResponse
|
||||
public function overview(DateRequest $request): JsonResponse
|
||||
{
|
||||
$params = $request->getAll();
|
||||
|
||||
@@ -149,14 +149,20 @@ class BudgetController extends Controller
|
||||
'currency_name' => $row['currency_name'],
|
||||
'currency_decimal_places' => $row['currency_decimal_places'],
|
||||
'period' => null,
|
||||
'start' => $row['start'],
|
||||
'end' => $row['end'],
|
||||
'date' => $row['start'],
|
||||
'start_date' => $row['start'],
|
||||
'end_date' => $row['end'],
|
||||
'yAxisID' => 0,
|
||||
'type' => 'bar',
|
||||
'entries' => [
|
||||
'budgeted' => $row['budgeted'],
|
||||
'spent' => $row['spent'],
|
||||
'left' => $row['left'],
|
||||
'overspent' => $row['overspent'],
|
||||
],
|
||||
'pc_entries' => [
|
||||
|
||||
],
|
||||
];
|
||||
$return[] = $current;
|
||||
}
|
||||
|
@@ -34,6 +34,7 @@ use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
use FireflyIII\Support\Facades\Steam;
|
||||
use FireflyIII\Support\Http\Api\CleansChartData;
|
||||
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
|
||||
use FireflyIII\Support\Http\Api\ValidatesUserGroupTrait;
|
||||
@@ -77,7 +78,7 @@ class CategoryController extends Controller
|
||||
*
|
||||
* @SuppressWarnings("PHPMD.UnusedFormalParameter")
|
||||
*/
|
||||
public function dashboard(DateRequest $request): JsonResponse
|
||||
public function overview(DateRequest $request): JsonResponse
|
||||
{
|
||||
/** @var Carbon $start */
|
||||
$start = $this->parameters->get('start');
|
||||
@@ -108,18 +109,21 @@ class CategoryController extends Controller
|
||||
$currencyCode = (string)$currency->code;
|
||||
$currencySymbol = (string)$currency->symbol;
|
||||
$currencyDecimalPlaces = (int)$currency->decimal_places;
|
||||
$amount = app('steam')->positive($journal['amount']);
|
||||
$amount = Steam::positive($journal['amount']);
|
||||
$pcAmount = null;
|
||||
|
||||
// overrule if necessary:
|
||||
if ($this->convertToPrimary && $journalCurrencyId === $this->primaryCurrency->id) {
|
||||
$pcAmount = $amount;
|
||||
}
|
||||
if ($this->convertToPrimary && $journalCurrencyId !== $this->primaryCurrency->id) {
|
||||
$currencyId = (int)$this->primaryCurrency->id;
|
||||
$currencyName = (string)$this->primaryCurrency->name;
|
||||
$currencyCode = (string)$this->primaryCurrency->code;
|
||||
$currencySymbol = (string)$this->primaryCurrency->symbol;
|
||||
$currencyDecimalPlaces = (int)$this->primaryCurrency->decimal_places;
|
||||
$convertedAmount = $converter->convert($currency, $this->primaryCurrency, $journal['date'], $amount);
|
||||
Log::debug(sprintf('Converted %s %s to %s %s', $journal['currency_code'], $amount, $this->primaryCurrency->code, $convertedAmount));
|
||||
$amount = $convertedAmount;
|
||||
$pcAmount = $converter->convert($currency, $this->primaryCurrency, $journal['date'], $amount);
|
||||
Log::debug(sprintf('Converted %s %s to %s %s', $journal['currency_code'], $amount, $this->primaryCurrency->code, $pcAmount));
|
||||
}
|
||||
|
||||
|
||||
@@ -129,23 +133,38 @@ class CategoryController extends Controller
|
||||
$return[$key] ??= [
|
||||
'label' => $categoryName,
|
||||
'currency_id' => (string)$currencyId,
|
||||
'currency_code' => $currencyCode,
|
||||
'currency_name' => $currencyName,
|
||||
'currency_code' => $currencyCode,
|
||||
'currency_symbol' => $currencySymbol,
|
||||
'currency_decimal_places' => $currencyDecimalPlaces,
|
||||
'primary_currency_id' => (string)$this->primaryCurrency->id,
|
||||
'primary_currency_name' => (string)$this->primaryCurrency->name,
|
||||
'primary_currency_code' => (string)$this->primaryCurrency->code,
|
||||
'primary_currency_symbol' => (string)$this->primaryCurrency->symbol,
|
||||
'primary_currency_decimal_places' => (int)$this->primaryCurrency->decimal_places,
|
||||
'period' => null,
|
||||
'start' => $start->toAtomString(),
|
||||
'end' => $end->toAtomString(),
|
||||
'amount' => '0',
|
||||
'start_date' => $start->toAtomString(),
|
||||
'end_date' => $end->toAtomString(),
|
||||
'yAxisID' => 0,
|
||||
'type' => 'bar',
|
||||
'entries' => [
|
||||
'spent' => '0'
|
||||
],
|
||||
'pc_entries' => [
|
||||
'spent' => '0'
|
||||
],
|
||||
];
|
||||
|
||||
// add monies
|
||||
$return[$key]['amount'] = bcadd($return[$key]['amount'], (string)$amount);
|
||||
$return[$key]['entries']['spent'] = bcadd($return[$key]['entries']['spent'], (string)$amount);
|
||||
if (null !== $pcAmount) {
|
||||
$return[$key]['pc_entries']['spent'] = bcadd($return[$key]['pc_entries']['spent'], (string)$pcAmount);
|
||||
}
|
||||
}
|
||||
$return = array_values($return);
|
||||
|
||||
// order by amount
|
||||
usort($return, static fn (array $a, array $b) => (float)$a['amount'] < (float)$b['amount'] ? 1 : -1);
|
||||
usort($return, static fn(array $a, array $b) => (float)$a['entries']['spent'] < (float)$b['entries']['spent'] ? 1 : -1);
|
||||
|
||||
return response()->json($this->clean($return));
|
||||
}
|
||||
|
@@ -80,6 +80,7 @@ class AccountBalanceGrouped
|
||||
'start_date' => $this->start->toAtomString(),
|
||||
'end_date' => $this->end->toAtomString(),
|
||||
'yAxisID' => 0,
|
||||
'type' => 'line',
|
||||
'period' => $this->preferredRange,
|
||||
'entries' => [],
|
||||
'pc_entries' => [],
|
||||
@@ -97,6 +98,7 @@ class AccountBalanceGrouped
|
||||
'date' => $this->start->toAtomString(),
|
||||
'start_date' => $this->start->toAtomString(),
|
||||
'end_date' => $this->end->toAtomString(),
|
||||
'type' => 'line',
|
||||
'yAxisID' => 0,
|
||||
'period' => $this->preferredRange,
|
||||
'entries' => [],
|
||||
|
@@ -61,7 +61,10 @@ trait CleansChartData
|
||||
if (array_key_exists('primary_currency_id', $array)) {
|
||||
$array['primary_currency_id'] = (string)$array['primary_currency_id'];
|
||||
}
|
||||
$required = ['start_date', 'end_date', 'period', 'yAxisID'];
|
||||
$required = [
|
||||
'start_date', 'end_date', 'period', 'yAxisID','type','entries','pc_entries',
|
||||
'currency_id', 'primary_currency_id'
|
||||
];
|
||||
foreach ($required as $field) {
|
||||
if (!array_key_exists($field, $array)) {
|
||||
throw new FireflyException(sprintf('Data-set "%s" is missing the "%s"-variable.', $index, $field));
|
||||
|
@@ -128,7 +128,7 @@ Route::group(
|
||||
'as' => 'api.v1.chart.budget.',
|
||||
],
|
||||
static function (): void {
|
||||
Route::get('dashboard', ['uses' => 'BudgetController@dashboard', 'as' => 'dashboard']);
|
||||
Route::get('overview', ['uses' => 'BudgetController@overview', 'as' => 'overview']);
|
||||
}
|
||||
);
|
||||
|
||||
@@ -139,7 +139,7 @@ Route::group(
|
||||
'as' => 'api.v1.chart.category.',
|
||||
],
|
||||
static function (): void {
|
||||
Route::get('dashboard', ['uses' => 'CategoryController@dashboard', 'as' => 'dashboard']);
|
||||
Route::get('overview', ['uses' => 'CategoryController@overview', 'as' => 'overview']);
|
||||
}
|
||||
);
|
||||
|
||||
|
Reference in New Issue
Block a user