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

@@ -177,6 +177,7 @@ class Navigation
'year' => 'startOfYear', 'year' => 'startOfYear',
'yearly' => 'startOfYear', 'yearly' => 'startOfYear',
'1Y' => 'startOfYear', '1Y' => 'startOfYear',
'MTD' => 'startOfMonth',
]; ];
$parameterMap = [ $parameterMap = [
@@ -274,13 +275,20 @@ class Navigation
/** @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(),
@@ -327,7 +335,7 @@ 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
@@ -382,7 +390,7 @@ class Navigation
)); ));
} }
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
@@ -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;
} }
@@ -463,18 +471,18 @@ class Navigation
// 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 = [];
@@ -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';
} }
@@ -682,7 +690,7 @@ class Navigation
/** @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;