Chart field name refactor

This commit is contained in:
James Cole 2023-08-01 10:26:26 +02:00
parent 3517452ea1
commit 7c9f7f04b7
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
4 changed files with 18 additions and 40 deletions

View File

@ -123,7 +123,7 @@ class AccountController extends Controller
'start_date' => $start->toAtomString(),
'end_date' => $end->toAtomString(),
'entries' => [],
'converted_entries' => [],
'native_entries' => [],
];
$currentStart = clone $start;
$range = app('steam')->balanceInRange($account, $start, clone $end, $currency);
@ -140,8 +140,8 @@ class AccountController extends Controller
$previousConverted = $balanceConverted;
$currentStart->addDay();
$currentSet['entries'][$label] = $balance;
$currentSet['converted_entries'][$label] = $balanceConverted;
$currentSet['entries'][$label] = $balance;
$currentSet['native_entries'][$label] = $balanceConverted;
}
$chartData[] = $currentSet;
}

View File

@ -81,8 +81,7 @@ class BalanceController extends Controller
$preferredRange = $params['period'];
// set some formats, based on input parameters.
$format = app('navigation')->preferredCarbonFormatByPeriod($preferredRange);
$titleFormat = app('navigation')->preferredCarbonLocalizedFormatByPeriod($preferredRange);
$format = app('navigation')->preferredCarbonFormatByPeriod($preferredRange);
// prepare for currency conversion and data collection:
$ids = $accounts->pluck('id')->toArray();
@ -144,11 +143,11 @@ class BalanceController extends Controller
// set the array (in monetary info) with spent/earned in this $period, if it does not exist.
$data[$currencyId][$period] = $data[$currencyId][$period] ?? [
'period' => $period,
'spent' => '0',
'earned' => '0',
'converted_spent' => '0',
'converted_earned' => '0',
'period' => $period,
'spent' => '0',
'earned' => '0',
'native_spent' => '0',
'native_earned' => '0',
];
// is this journal's amount in- our outgoing?
$key = 'spent';
@ -179,7 +178,7 @@ class BalanceController extends Controller
$data[$currencyId][$period][$key] = bcadd($data[$currencyId][$period][$key], $amount);
// add converted entry
$convertedKey = sprintf('converted_%s', $key);
$convertedKey = sprintf('native_%s', $key);
$data[$currencyId][$period][$convertedKey] = bcadd($data[$currencyId][$period][$convertedKey], $amountConverted);
}
@ -198,7 +197,7 @@ class BalanceController extends Controller
'native_code' => $currency['native_code'],
'native_decimal_places' => $currency['native_decimal_places'],
'entries' => [],
'converted_entries' => [],
'native_entries' => [],
];
$expense = [
'label' => sprintf('spent-%s', $currency['currency_code']),
@ -211,21 +210,21 @@ class BalanceController extends Controller
'native_code' => $currency['native_code'],
'native_decimal_places' => $currency['native_decimal_places'],
'entries' => [],
'converted_entries' => [],
'native_entries' => [],
];
// loop all possible periods between $start and $end, and add them to the correct dataset.
$currentStart = clone $start;
while ($currentStart <= $end) {
$key = $currentStart->format($format);
$title = $currentStart->isoFormat($titleFormat);
$label = $currentStart->toAtomString();
// normal entries
$income['entries'][$title] = app('steam')->bcround(($currency[$key]['earned'] ?? '0'), $currency['currency_decimal_places']);
$expense['entries'][$title] = app('steam')->bcround(($currency[$key]['spent'] ?? '0'), $currency['currency_decimal_places']);
$income['entries'][$label] = app('steam')->bcround(($currency[$key]['earned'] ?? '0'), $currency['currency_decimal_places']);
$expense['entries'][$label] = app('steam')->bcround(($currency[$key]['spent'] ?? '0'), $currency['currency_decimal_places']);
// converted entries
$income['converted_entries'][$title] = app('steam')->bcround(($currency[$key]['converted_earned'] ?? '0'), $currency['native_decimal_places']);
$expense['converted_entries'][$title] = app('steam')->bcround(($currency[$key]['converted_spent'] ?? '0'), $currency['native_decimal_places']);
$income['converted_entries'][$label] = app('steam')->bcround(($currency[$key]['converted_earned'] ?? '0'), $currency['native_decimal_places']);
$expense['converted_entries'][$label] = app('steam')->bcround(($currency[$key]['converted_spent'] ?? '0'), $currency['native_decimal_places']);
// next loop
$currentStart = app('navigation')->addPeriod($currentStart, $preferredRange, 0);
@ -234,8 +233,6 @@ class BalanceController extends Controller
$chartData[] = $income;
$chartData[] = $expense;
}
//$data = $this->generator->multiSet($chartData);
return response()->json($chartData);
}

View File

@ -559,25 +559,6 @@ class Navigation
return $format;
}
/**
* Same as preferredCarbonLocalizedFormat but based on the period.
*
* @param string $period
*
* @return string
*/
public function preferredCarbonLocalizedFormatByPeriod(string $period): string
{
$locale = app('steam')->getLocale();
return match ($period) {
default => (string)trans('config.month_and_day_js', [], $locale),
'1W' => (string)trans('config.week_in_year_js', [], $locale),
'1M' => (string)trans('config.month_js', [], $locale),
'3M', '6M' => (string)trans('config.half_year_js', [], $locale),
'1Y' => (string)trans('config.year_js', [], $locale),
};
}
/**
* If the date difference between start and end is less than a month, method returns "endOfDay". If the difference
* is less than a year, method returns "endOfMonth". If the date difference is larger, method returns "endOfYear".

View File

@ -376,7 +376,7 @@ trait ConvertsDataTypes
{
$result = null;
try {
$result = $this->get($field) ? new Carbon($this->get($field)) : null;
$result = $this->get($field) ? new Carbon($this->get($field), config('app.timezone')) : null;
} catch (InvalidFormatException $e) {
// @ignoreException
}