James Cole
2024-08-16 09:39:29 +02:00
parent debfd9160c
commit 8ef17f6686
4 changed files with 76 additions and 65 deletions

View File

@@ -9,11 +9,13 @@ use FireflyIII\Rules\IsBoolean;
use FireflyIII\Rules\IsValidPositiveAmount; use FireflyIII\Rules\IsValidPositiveAmount;
use FireflyIII\Rules\UniqueAccountNumber; use FireflyIII\Rules\UniqueAccountNumber;
use FireflyIII\Rules\UniqueIban; use FireflyIII\Rules\UniqueIban;
use FireflyIII\Support\Request\ConvertsDataTypes;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use LaravelJsonApi\Laravel\Http\Requests\ResourceRequest; use LaravelJsonApi\Laravel\Http\Requests\ResourceRequest;
class AccountRequest extends ResourceRequest class AccountRequest extends ResourceRequest
{ {
use ConvertsDataTypes;
/** /**
* Get the validation rules for the resource. * Get the validation rules for the resource.
*/ */

View File

@@ -41,9 +41,8 @@ trait DateCalculation
{ {
$difference = (int)($start->diffInDays($end, true) + 1); $difference = (int)($start->diffInDays($end, true) + 1);
$today = today(config('app.timezone'))->startOfDay(); $today = today(config('app.timezone'))->startOfDay();
if ($start->lte($today) && $end->gte($today)) { if ($start->lte($today) && $end->gte($today)) {
$difference = $today->diffInDays($end); $difference = $today->diffInDays($end) + 1;
} }
return (int)(0 === $difference ? 1 : $difference); return (int)(0 === $difference ? 1 : $difference);

View File

@@ -130,6 +130,8 @@ trait GetConfigurationData
/** @var Carbon $todayEnd */ /** @var Carbon $todayEnd */
$todayEnd = app('navigation')->endOfPeriod($todayStart, $viewRange); $todayEnd = app('navigation')->endOfPeriod($todayStart, $viewRange);
if ($todayStart->ne($start) || $todayEnd->ne($end)) { if ($todayStart->ne($start) || $todayEnd->ne($end)) {
$ranges[ucfirst((string)trans('firefly.today'))] = [$todayStart, $todayEnd]; $ranges[ucfirst((string)trans('firefly.today'))] = [$todayStart, $todayEnd];
} }

View File

@@ -76,10 +76,10 @@ class Navigation
if (!array_key_exists($repeatFreq, $functionMap)) { if (!array_key_exists($repeatFreq, $functionMap)) {
Log::error(sprintf( Log::error(sprintf(
'The periodicity %s is unknown. Choose one of available periodicity: %s', 'The periodicity %s is unknown. Choose one of available periodicity: %s',
$repeatFreq, $repeatFreq,
implode(', ', array_keys($functionMap)) implode(', ', array_keys($functionMap))
)); ));
return $theDate; return $theDate;
} }
@@ -110,7 +110,7 @@ class Navigation
if ($end < $start) { if ($end < $start) {
[$start, $end] = [$end, $start]; [$start, $end] = [$end, $start];
} }
$periods = []; $periods = [];
// first, 13 periods of [range] // first, 13 periods of [range]
$loopCount = 0; $loopCount = 0;
$loopDate = clone $end; $loopDate = clone $end;
@@ -160,9 +160,9 @@ class Navigation
public function startOfPeriod(Carbon $theDate, string $repeatFreq): Carbon public function startOfPeriod(Carbon $theDate, string $repeatFreq): Carbon
{ {
$date = clone $theDate; $date = clone $theDate;
Log::debug(sprintf('Now in startOfPeriod("%s", "%s")', $date->toIso8601String(), $repeatFreq)); Log::debug(sprintf('Now in startOfPeriod("%s", "%s")', $date->toIso8601String(), $repeatFreq));
$functionMap = [ $functionMap = [
'1D' => 'startOfDay', '1D' => 'startOfDay',
'daily' => 'startOfDay', 'daily' => 'startOfDay',
'1W' => 'startOfWeek', '1W' => 'startOfWeek',
@@ -177,6 +177,7 @@ class Navigation
'year' => 'startOfYear', 'year' => 'startOfYear',
'yearly' => 'startOfYear', 'yearly' => 'startOfYear',
'1Y' => 'startOfYear', '1Y' => 'startOfYear',
'MTD' => 'startOfMonth',
]; ];
$parameterMap = [ $parameterMap = [
@@ -208,7 +209,7 @@ class Navigation
return $date; return $date;
} }
$result = match ($repeatFreq) { $result = match ($repeatFreq) {
'last7' => $date->subDays(7)->startOfDay(), 'last7' => $date->subDays(7)->startOfDay(),
'last30' => $date->subDays(30)->startOfDay(), 'last30' => $date->subDays(30)->startOfDay(),
'last90' => $date->subDays(90)->startOfDay(), 'last90' => $date->subDays(90)->startOfDay(),
@@ -236,7 +237,7 @@ class Navigation
public function endOfPeriod(Carbon $end, string $repeatFreq): Carbon public function endOfPeriod(Carbon $end, string $repeatFreq): Carbon
{ {
$currentEnd = clone $end; $currentEnd = clone $end;
Log::debug(sprintf('Now in endOfPeriod("%s", "%s").', $currentEnd->toIso8601String(), $repeatFreq)); Log::debug(sprintf('Now in endOfPeriod("%s", "%s").', $currentEnd->toIso8601String(), $repeatFreq));
$functionMap = [ $functionMap = [
@@ -270,19 +271,26 @@ class Navigation
Log::debug('Session data available.'); Log::debug('Session data available.');
/** @var Carbon $tStart */ /** @var Carbon $tStart */
$tStart = session('start', today(config('app.timezone'))->startOfMonth()); $tStart = session('start', today(config('app.timezone'))->startOfMonth());
/** @var Carbon $tEnd */ /** @var Carbon $tEnd */
$tEnd = session('end', today(config('app.timezone'))->endOfMonth()); $tEnd = session('end', today(config('app.timezone'))->endOfMonth());
$diffInDays = (int)$tStart->diffInDays($tEnd, true); $diffInDays = (int) $tStart->diffInDays($tEnd, true);
} }
Log::debug(sprintf('Diff in days is %d', $diffInDays)); Log::debug(sprintf('Diff in days is %d', $diffInDays));
$currentEnd->addDays($diffInDays); $currentEnd->addDays($diffInDays);
return $currentEnd; return $currentEnd;
} }
if('MTD' === $repeatFreq) {
$today = today();
if($today->isSameMonth($end)) {
return $today->endOfDay();
}
return $end->endOfMonth();
}
$result = match ($repeatFreq) { $result = match ($repeatFreq) {
'last7' => $currentEnd->addDays(7)->startOfDay(), 'last7' => $currentEnd->addDays(7)->startOfDay(),
'last30' => $currentEnd->addDays(30)->startOfDay(), 'last30' => $currentEnd->addDays(30)->startOfDay(),
'last90' => $currentEnd->addDays(90)->startOfDay(), 'last90' => $currentEnd->addDays(90)->startOfDay(),
@@ -302,7 +310,7 @@ class Navigation
return $end; return $end;
} }
$function = $functionMap[$repeatFreq]; $function = $functionMap[$repeatFreq];
if (array_key_exists($repeatFreq, $modifierMap)) { if (array_key_exists($repeatFreq, $modifierMap)) {
$currentEnd->{$function}($modifierMap[$repeatFreq]); // @phpstan-ignore-line $currentEnd->{$function}($modifierMap[$repeatFreq]); // @phpstan-ignore-line
@@ -327,19 +335,19 @@ class Navigation
{ {
$endOfMonth = $date->copy()->endOfMonth(); $endOfMonth = $date->copy()->endOfMonth();
return (int)$date->diffInDays($endOfMonth, true); return (int) $date->diffInDays($endOfMonth, true);
} }
public function diffInPeriods(string $period, int $skip, Carbon $beginning, Carbon $end): int public function diffInPeriods(string $period, int $skip, Carbon $beginning, Carbon $end): int
{ {
Log::debug(sprintf( Log::debug(sprintf(
'diffInPeriods: %s (skip: %d), between %s and %s.', 'diffInPeriods: %s (skip: %d), between %s and %s.',
$period, $period,
$skip, $skip,
$beginning->format('Y-m-d'), $beginning->format('Y-m-d'),
$end->format('Y-m-d') $end->format('Y-m-d')
)); ));
$map = [ $map = [
'daily' => 'diffInDays', 'daily' => 'diffInDays',
'weekly' => 'diffInWeeks', 'weekly' => 'diffInWeeks',
'monthly' => 'diffInMonths', 'monthly' => 'diffInMonths',
@@ -352,7 +360,7 @@ class Navigation
return 1; return 1;
} }
$func = $map[$period]; $func = $map[$period];
// first do the diff // first do the diff
$floatDiff = $beginning->{$func}($end, true); // @phpstan-ignore-line $floatDiff = $beginning->{$func}($end, true); // @phpstan-ignore-line
@@ -367,7 +375,7 @@ class Navigation
} }
// then do ceil() // then do ceil()
$diff = ceil($floatDiff); $diff = ceil($floatDiff);
Log::debug(sprintf('Diff is %f periods (%d rounded up)', $floatDiff, $diff)); Log::debug(sprintf('Diff is %f periods (%d rounded up)', $floatDiff, $diff));
@@ -375,14 +383,14 @@ class Navigation
$parameter = $skip + 1; $parameter = $skip + 1;
$diff = ceil($diff / $parameter) * $parameter; $diff = ceil($diff / $parameter) * $parameter;
Log::debug(sprintf( Log::debug(sprintf(
'diffInPeriods: skip is %d, so param is %d, and diff becomes %d', 'diffInPeriods: skip is %d, so param is %d, and diff becomes %d',
$skip, $skip,
$parameter, $parameter,
$diff $diff
)); ));
} }
return (int)$diff; return (int) $diff;
} }
public function endOfX(Carbon $theCurrentEnd, string $repeatFreq, ?Carbon $maxDate): Carbon public function endOfX(Carbon $theCurrentEnd, string $repeatFreq, ?Carbon $maxDate): Carbon
@@ -404,7 +412,7 @@ class Navigation
'yearly' => 'endOfYear', 'yearly' => 'endOfYear',
]; ];
$currentEnd = clone $theCurrentEnd; $currentEnd = clone $theCurrentEnd;
if (array_key_exists($repeatFreq, $functionMap)) { if (array_key_exists($repeatFreq, $functionMap)) {
$function = $functionMap[$repeatFreq]; $function = $functionMap[$repeatFreq];
@@ -428,7 +436,7 @@ class Navigation
if (is_array($range)) { if (is_array($range)) {
$range = '1M'; $range = '1M';
} }
$range = (string)$range; $range = (string) $range;
if (!$correct) { if (!$correct) {
return $range; return $range;
} }
@@ -459,25 +467,25 @@ class Navigation
*/ */
public function listOfPeriods(Carbon $start, Carbon $end): array public function listOfPeriods(Carbon $start, Carbon $end): array
{ {
$locale = app('steam')->getLocale(); $locale = app('steam')->getLocale();
// define period to increment // define period to increment
$increment = 'addDay'; $increment = 'addDay';
$format = $this->preferredCarbonFormat($start, $end); $format = $this->preferredCarbonFormat($start, $end);
$displayFormat = (string)trans('config.month_and_day_js', [], $locale); $displayFormat = (string) trans('config.month_and_day_js', [], $locale);
$diff = $start->diffInMonths($end, true); $diff = $start->diffInMonths($end, true);
// increment by month (for year) // increment by month (for year)
if ($diff >= 1.0001) { if ($diff >= 1.0001) {
$increment = 'addMonth'; $increment = 'addMonth';
$displayFormat = (string)trans('config.month_js'); $displayFormat = (string) trans('config.month_js');
} }
// increment by year (for multi-year) // increment by year (for multi-year)
if ($diff >= 12.0001) { if ($diff >= 12.0001) {
$increment = 'addYear'; $increment = 'addYear';
$displayFormat = (string)trans('config.year_js'); $displayFormat = (string) trans('config.year_js');
} }
$begin = clone $start; $begin = clone $start;
$entries = []; $entries = [];
while ($begin < $end) { while ($begin < $end) {
$formatted = $begin->format($format); $formatted = $begin->format($format);
$displayed = $begin->isoFormat($displayFormat); $displayed = $begin->isoFormat($displayFormat);
@@ -514,19 +522,19 @@ class Navigation
{ {
$date = clone $theDate; $date = clone $theDate;
$formatMap = [ $formatMap = [
'1D' => (string)trans('config.specific_day_js'), '1D' => (string) trans('config.specific_day_js'),
'daily' => (string)trans('config.specific_day_js'), 'daily' => (string) trans('config.specific_day_js'),
'custom' => (string)trans('config.specific_day_js'), 'custom' => (string) trans('config.specific_day_js'),
'1W' => (string)trans('config.week_in_year_js'), '1W' => (string) trans('config.week_in_year_js'),
'week' => (string)trans('config.week_in_year_js'), 'week' => (string) trans('config.week_in_year_js'),
'weekly' => (string)trans('config.week_in_year_js'), 'weekly' => (string) trans('config.week_in_year_js'),
'1M' => (string)trans('config.month_js'), '1M' => (string) trans('config.month_js'),
'month' => (string)trans('config.month_js'), 'month' => (string) trans('config.month_js'),
'monthly' => (string)trans('config.month_js'), 'monthly' => (string) trans('config.month_js'),
'1Y' => (string)trans('config.year_js'), '1Y' => (string) trans('config.year_js'),
'year' => (string)trans('config.year_js'), 'year' => (string) trans('config.year_js'),
'yearly' => (string)trans('config.year_js'), 'yearly' => (string) trans('config.year_js'),
'6M' => (string)trans('config.half_year_js'), '6M' => (string) trans('config.half_year_js'),
]; ];
if (array_key_exists($repeatFrequency, $formatMap)) { if (array_key_exists($repeatFrequency, $formatMap)) {
@@ -567,13 +575,13 @@ class Navigation
public function preferredCarbonLocalizedFormat(Carbon $start, Carbon $end): string public function preferredCarbonLocalizedFormat(Carbon $start, Carbon $end): string
{ {
$locale = app('steam')->getLocale(); $locale = app('steam')->getLocale();
$format = (string)trans('config.month_and_day_js', [], $locale); $format = (string) trans('config.month_and_day_js', [], $locale);
if ($start->diffInMonths($end, true) > 1) { if ($start->diffInMonths($end, true) > 1) {
$format = (string)trans('config.month_js', [], $locale); $format = (string) trans('config.month_js', [], $locale);
} }
if ($start->diffInMonths($end, true) > 12) { if ($start->diffInMonths($end, true) > 12) {
$format = (string)trans('config.year_js', [], $locale); $format = (string) trans('config.year_js', [], $locale);
} }
return $format; return $format;
@@ -586,11 +594,11 @@ class Navigation
public function preferredEndOfPeriod(Carbon $start, Carbon $end): string public function preferredEndOfPeriod(Carbon $start, Carbon $end): string
{ {
$format = 'endOfDay'; $format = 'endOfDay';
if ((int)$start->diffInMonths($end, true) > 1) { if ((int) $start->diffInMonths($end, true) > 1) {
$format = 'endOfMonth'; $format = 'endOfMonth';
} }
if ((int)$start->diffInMonths($end, true) > 12) { if ((int) $start->diffInMonths($end, true) > 12) {
$format = 'endOfYear'; $format = 'endOfYear';
} }
@@ -604,11 +612,11 @@ class Navigation
public function preferredRangeFormat(Carbon $start, Carbon $end): string public function preferredRangeFormat(Carbon $start, Carbon $end): string
{ {
$format = '1D'; $format = '1D';
if ((int)$start->diffInMonths($end, true) > 1) { if ((int) $start->diffInMonths($end, true) > 1) {
$format = '1M'; $format = '1M';
} }
if ((int)$start->diffInMonths($end, true) > 12) { if ((int) $start->diffInMonths($end, true) > 12) {
$format = '1Y'; $format = '1Y';
} }
@@ -622,11 +630,11 @@ class Navigation
public function preferredSqlFormat(Carbon $start, Carbon $end): string public function preferredSqlFormat(Carbon $start, Carbon $end): string
{ {
$format = '%Y-%m-%d'; $format = '%Y-%m-%d';
if ((int)$start->diffInMonths($end, true) > 1) { if ((int) $start->diffInMonths($end, true) > 1) {
$format = '%Y-%m'; $format = '%Y-%m';
} }
if ((int)$start->diffInMonths($end, true) > 12) { if ((int) $start->diffInMonths($end, true) > 12) {
$format = '%Y'; $format = '%Y';
} }
@@ -639,7 +647,7 @@ class Navigation
public function subtractPeriod(Carbon $theDate, string $repeatFreq, ?int $subtract = null): Carbon public function subtractPeriod(Carbon $theDate, string $repeatFreq, ?int $subtract = null): Carbon
{ {
$subtract ??= 1; $subtract ??= 1;
$date = clone $theDate; $date = clone $theDate;
// 1D 1W 1M 3M 6M 1Y // 1D 1W 1M 3M 6M 1Y
$functionMap = [ $functionMap = [
'1D' => 'subDays', '1D' => 'subDays',
@@ -678,11 +686,11 @@ class Navigation
// this is then subtracted from $theDate (* $subtract). // this is then subtracted from $theDate (* $subtract).
if ('custom' === $repeatFreq) { if ('custom' === $repeatFreq) {
/** @var Carbon $tStart */ /** @var Carbon $tStart */
$tStart = session('start', today(config('app.timezone'))->startOfMonth()); $tStart = session('start', today(config('app.timezone'))->startOfMonth());
/** @var Carbon $tEnd */ /** @var Carbon $tEnd */
$tEnd = session('end', today(config('app.timezone'))->endOfMonth()); $tEnd = session('end', today(config('app.timezone'))->endOfMonth());
$diffInDays = (int)$tStart->diffInDays($tEnd, true); $diffInDays = (int) $tStart->diffInDays($tEnd, true);
$date->subDays($diffInDays * $subtract); $date->subDays($diffInDays * $subtract);
return $date; return $date;
@@ -772,7 +780,7 @@ class Navigation
return $fiscalHelper->endOfFiscalYear($end); return $fiscalHelper->endOfFiscalYear($end);
} }
$list = [ $list = [
'last7', 'last7',
'last30', 'last30',
'last90', 'last90',