Restore old behavior

This commit is contained in:
James Cole
2024-03-17 12:00:28 +01:00
parent ab2772abe0
commit 3913fa5086
18 changed files with 51 additions and 51 deletions

View File

@@ -281,7 +281,7 @@ class BasicController extends Controller
$spentInCurrency = $row['sum']; $spentInCurrency = $row['sum'];
$leftToSpend = bcadd($amount, $spentInCurrency); $leftToSpend = bcadd($amount, $spentInCurrency);
$days = $today->diffInDays($end) + 1; $days = (int)$today->diffInDays($end, true) + 1;
$perDay = '0'; $perDay = '0';
if (0 !== $days && bccomp($leftToSpend, '0') > -1) { if (0 !== $days && bccomp($leftToSpend, '0') > -1) {
$perDay = bcdiv($leftToSpend, (string)$days); $perDay = bcdiv($leftToSpend, (string)$days);

View File

@@ -46,7 +46,7 @@ class DateRequest extends FormRequest
{ {
$start = $this->getCarbonDate('start'); $start = $this->getCarbonDate('start');
$end = $this->getCarbonDate('end'); $end = $this->getCarbonDate('end');
if ($start->diffInYears($end) > 5) { if ($start->diffInYears($end, true) > 5) {
throw new FireflyException('Date range out of range.'); throw new FireflyException('Date range out of range.');
} }

View File

@@ -298,7 +298,7 @@ class BasicController extends Controller
app('log')->debug(sprintf('Amount left is %s', $left)); app('log')->debug(sprintf('Amount left is %s', $left));
// how much left per day? // how much left per day?
$days = $today->diffInDays($end) + 1; $days = (int) $today->diffInDays($end,true) + 1;
$perDay = '0'; $perDay = '0';
$perDayNative = '0'; $perDayNative = '0';
if (0 !== $days && bccomp($left, '0') > -1) { if (0 !== $days && bccomp($left, '0') > -1) {

View File

@@ -110,7 +110,7 @@ class AppendBudgetLimitPeriods extends Command
return 'daily'; return 'daily';
} }
// is weekly // is weekly
if ('1' === $limit->start_date->format('N') && '7' === $limit->end_date->format('N') && 6 === $limit->end_date->diffInDays($limit->start_date)) { if ('1' === $limit->start_date->format('N') && '7' === $limit->end_date->format('N') && 6 === (int)$limit->end_date->diffInDays($limit->start_date, true)) {
return 'weekly'; return 'weekly';
} }
@@ -129,7 +129,7 @@ class AppendBudgetLimitPeriods extends Command
if ( if (
in_array($limit->start_date->format('j-n'), $start, true) // start of quarter in_array($limit->start_date->format('j-n'), $start, true) // start of quarter
&& in_array($limit->end_date->format('j-n'), $end, true) // end of quarter && in_array($limit->end_date->format('j-n'), $end, true) // end of quarter
&& 2 === $limit->start_date->diffInMonths($limit->end_date) && 2 === (int)$limit->start_date->diffInMonths($limit->end_date, true)
) { ) {
return 'quarterly'; return 'quarterly';
} }
@@ -139,7 +139,7 @@ class AppendBudgetLimitPeriods extends Command
if ( if (
in_array($limit->start_date->format('j-n'), $start, true) // start of quarter in_array($limit->start_date->format('j-n'), $start, true) // start of quarter
&& in_array($limit->end_date->format('j-n'), $end, true) // end of quarter && in_array($limit->end_date->format('j-n'), $end, true) // end of quarter
&& 5 === $limit->start_date->diffInMonths($limit->end_date) && 5 === (int)$limit->start_date->diffInMonths($limit->end_date, true)
) { ) {
return 'half_year'; return 'half_year';
} }

View File

@@ -40,12 +40,12 @@ class ReportGeneratorFactory
{ {
$period = 'Month'; $period = 'Month';
// more than two months date difference means year report. // more than two months date difference means year report.
if ($start->diffInMonths($end) > 1) { if ($start->diffInMonths($end, true) > 1) {
$period = 'Year'; $period = 'Year';
} }
// more than one year date difference means multi year report. // more than one year date difference means multi-year report.
if ($start->diffInMonths($end) > 12) { if ($start->diffInMonths($end, true) > 12) {
$period = 'MultiYear'; $period = 'MultiYear';
} }

View File

@@ -405,7 +405,7 @@ class UserEventHandler
} }
// clean up old entries (6 months) // clean up old entries (6 months)
$carbon = Carbon::createFromFormat('Y-m-d H:i:s', $preference[$index]['time']); $carbon = Carbon::createFromFormat('Y-m-d H:i:s', $preference[$index]['time']);
if (false !== $carbon && $carbon->diffInMonths(today()) > 6) { if (false !== $carbon && $carbon->diffInMonths(today(), true) > 6) {
app('log')->debug(sprintf('Entry for %s is very old, remove it.', $row['ip'])); app('log')->debug(sprintf('Entry for %s is very old, remove it.', $row['ip']));
unset($preference[$index]); unset($preference[$index]);
} }

View File

@@ -97,10 +97,10 @@ class HomeController extends Controller
app('log')->debug('Range is now marked as "custom".'); app('log')->debug('Range is now marked as "custom".');
} }
$diff = $start->diffInDays($end) + 1; $diff = $start->diffInDays($end, true) + 1;
if ($diff > 50) { if ($diff > 50) {
$request->session()->flash('warning', (string)trans('firefly.warning_much_data', ['days' => $diff])); $request->session()->flash('warning', (string)trans('firefly.warning_much_data', ['days' => (int)$diff]));
} }
$request->session()->put('is_custom_range', $isCustomRange); $request->session()->put('is_custom_range', $isCustomRange);

View File

@@ -113,7 +113,7 @@ class BoxController extends Controller
$spentAmount = $spent[$currency->id]['sum'] ?? '0'; $spentAmount = $spent[$currency->id]['sum'] ?? '0';
app('log')->debug(sprintf('Spent for default currency for all budgets in this period: %s', $spentAmount)); app('log')->debug(sprintf('Spent for default currency for all budgets in this period: %s', $spentAmount));
$days = $today->between($start, $end) ? $today->diffInDays($start) + 1 : $end->diffInDays($start) + 1; $days = (int)($today->between($start, $end) ? $today->diffInDays($start, true) + 1 : $end->diffInDays($start, true) + 1);
app('log')->debug(sprintf('Number of days left: %d', $days)); app('log')->debug(sprintf('Number of days left: %d', $days));
$spentPerDay = bcdiv($spentAmount, (string)$days); $spentPerDay = bcdiv($spentAmount, (string)$days);
app('log')->debug(sprintf('Available to spend per day: %s', $spentPerDay)); app('log')->debug(sprintf('Available to spend per day: %s', $spentPerDay));

View File

@@ -128,7 +128,7 @@ class WarnAboutBills implements ShouldQueue
$today = clone $this->date; $today = clone $this->date;
$carbon = clone $bill->{$field}; $carbon = clone $bill->{$field};
return $today->diffInDays($carbon, false); return (int) $today->diffInDays($carbon);
} }
private function sendWarning(Bill $bill, string $field): void private function sendWarning(Bill $bill, string $field): void

View File

@@ -132,7 +132,7 @@ class BudgetRepository implements BudgetRepositoryInterface
continue; continue;
} }
$total = $limit->start_date->diffInDays($limit->end_date) + 1; // include the day itself. $total = $limit->start_date->diffInDays($limit->end_date, true) + 1; // include the day itself.
$days = $this->daysInOverlap($limit, $start, $end); $days = $this->daysInOverlap($limit, $start, $end);
$amount = bcmul(bcdiv($limit->amount, (string)$total), (string)$days); $amount = bcmul(bcdiv($limit->amount, (string)$total), (string)$days);
$return[$currencyCode]['sum'] = bcadd($return[$currencyCode]['sum'], $amount); $return[$currencyCode]['sum'] = bcadd($return[$currencyCode]['sum'], $amount);
@@ -183,21 +183,21 @@ class BudgetRepository implements BudgetRepositoryInterface
// |-----------| // |-----------|
// |----------------| // |----------------|
if ($start->gte($limit->start_date) && $end->lte($limit->end_date)) { if ($start->gte($limit->start_date) && $end->lte($limit->end_date)) {
return $start->diffInDays($end) + 1; // add one day return (int) $start->diffInDays($end, true) + 1; // add one day
} }
// limit starts earlier and limit ends first: // limit starts earlier and limit ends first:
// |-----------| // |-----------|
// |-------| // |-------|
if ($limit->start_date->lte($start) && $limit->end_date->lte($end)) { if ($limit->start_date->lte($start) && $limit->end_date->lte($end)) {
// return days in the range $start-$limit_end // return days in the range $start-$limit_end
return $start->diffInDays($limit->end_date) + 1; // add one day, the day itself return (int) $start->diffInDays($limit->end_date, true) + 1; // add one day, the day itself
} }
// limit starts later and limit ends earlier // limit starts later and limit ends earlier
// |-----------| // |-----------|
// |-------| // |-------|
if ($limit->start_date->gte($start) && $limit->end_date->gte($end)) { if ($limit->start_date->gte($start) && $limit->end_date->gte($end)) {
// return days in the range $limit_start - $end // return days in the range $limit_start - $end
return $limit->start_date->diffInDays($end) + 1; // add one day, the day itself return (int) $limit->start_date->diffInDays($end, true) + 1; // add one day, the day itself
} }
return 0; return 0;

View File

@@ -51,7 +51,7 @@ class OperationsRepository implements OperationsRepositoryInterface
$total = '0'; $total = '0';
$count = 0; $count = 0;
foreach ($budget->budgetlimits as $limit) { foreach ($budget->budgetlimits as $limit) {
$diff = $limit->start_date->diffInDays($limit->end_date); $diff = (int) $limit->start_date->diffInDays($limit->end_date, true);
$diff = 0 === $diff ? 1 : $diff; $diff = 0 === $diff ? 1 : $diff;
$amount = $limit->amount; $amount = $limit->amount;
$perDay = bcdiv($amount, (string)$diff); $perDay = bcdiv($amount, (string)$diff);

View File

@@ -301,7 +301,7 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
if (null !== $piggyBank->targetdate && $repetition->currentamount < $piggyBank->targetamount) { if (null !== $piggyBank->targetdate && $repetition->currentamount < $piggyBank->targetamount) {
$now = today(config('app.timezone')); $now = today(config('app.timezone'));
$startDate = null !== $piggyBank->startdate && $piggyBank->startdate->gte($now) ? $piggyBank->startdate : $now; $startDate = null !== $piggyBank->startdate && $piggyBank->startdate->gte($now) ? $piggyBank->startdate : $now;
$diffInMonths = $startDate->diffInMonths($piggyBank->targetdate, false); $diffInMonths = (int) $startDate->diffInMonths($piggyBank->targetdate);
$remainingAmount = bcsub($piggyBank->targetamount, $repetition->currentamount); $remainingAmount = bcsub($piggyBank->targetamount, $repetition->currentamount);
// more than 1 month to go and still need money to save: // more than 1 month to go and still need money to save:

View File

@@ -476,7 +476,7 @@ class RecurringRepository implements RecurringRepositoryInterface
if (false === $repDate) { if (false === $repDate) {
$repDate = clone $today; $repDate = clone $today;
} }
$diffInYears = $today->diffInYears($repDate); $diffInYears = (int) $today->diffInYears($repDate, true);
$repDate->addYears($diffInYears); // technically not necessary. $repDate->addYears($diffInYears); // technically not necessary.
$string = $repDate->isoFormat((string)trans('config.month_and_day_no_year_js')); $string = $repDate->isoFormat((string)trans('config.month_and_day_no_year_js'));

View File

@@ -113,7 +113,7 @@ class WholePeriodChartGenerator
protected function calculateStep(Carbon $start, Carbon $end): string protected function calculateStep(Carbon $start, Carbon $end): string
{ {
$step = '1D'; $step = '1D';
$months = $start->diffInMonths($end); $months = $start->diffInMonths($end, true);
if ($months > 3) { if ($months > 3) {
$step = '1W'; $step = '1W';
} }

View File

@@ -39,7 +39,7 @@ trait DateCalculation
*/ */
public function activeDaysLeft(Carbon $start, Carbon $end): int public function activeDaysLeft(Carbon $start, Carbon $end): int
{ {
$difference = $start->diffInDays($end) + 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)) {
@@ -56,11 +56,11 @@ trait DateCalculation
*/ */
protected function activeDaysPassed(Carbon $start, Carbon $end): int protected function activeDaysPassed(Carbon $start, Carbon $end): int
{ {
$difference = $start->diffInDays($end) + 1; $difference = $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 = $start->diffInDays($today) + 1; $difference = $start->diffInDays($today, true) + 1;
} }
return (int) $difference; return (int) $difference;
@@ -69,7 +69,7 @@ trait DateCalculation
protected function calculateStep(Carbon $start, Carbon $end): string protected function calculateStep(Carbon $start, Carbon $end): string
{ {
$step = '1D'; $step = '1D';
$months = $start->diffInMonths($end); $months = $start->diffInMonths($end, true);
if ($months > 3) { if ($months > 3) {
$step = '1W'; $step = '1W';
} }

View File

@@ -252,7 +252,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 = $tStart->diffInDays($tEnd); $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);
@@ -304,7 +304,7 @@ class Navigation
{ {
$endOfMonth = $date->copy()->endOfMonth(); $endOfMonth = $date->copy()->endOfMonth();
return (int) $date->diffInDays($endOfMonth); 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
@@ -317,12 +317,12 @@ class Navigation
$end->format('Y-m-d') $end->format('Y-m-d')
)); ));
$map = [ $map = [
'daily' => 'floatDiffInDays', 'daily' => 'diffInDays',
'weekly' => 'floatDiffInWeeks', 'weekly' => 'diffInWeeks',
'monthly' => 'floatDiffInMonths', 'monthly' => 'diffInMonths',
'quarterly' => 'floatDiffInMonths', 'quarterly' => 'diffInMonths',
'half-year' => 'floatDiffInMonths', 'half-year' => 'diffInMonths',
'yearly' => 'floatDiffInYears', 'yearly' => 'diffInYears',
]; ];
if (!array_key_exists($period, $map)) { if (!array_key_exists($period, $map)) {
Log::warning(sprintf('No diffInPeriods for period "%s"', $period)); Log::warning(sprintf('No diffInPeriods for period "%s"', $period));
@@ -331,7 +331,7 @@ class Navigation
} }
$func = $map[$period]; $func = $map[$period];
// first do the diff // first do the diff
$floatDiff = $beginning->{$func}($end); // @phpstan-ignore-line $floatDiff = $beginning->{$func}($end, true); // @phpstan-ignore-line
// then correct for quarterly or half-year // then correct for quarterly or half-year
if ('quarterly' === $period) { if ('quarterly' === $period) {
@@ -442,13 +442,13 @@ class Navigation
$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);
// increment by month (for year) // increment by month (for year)
if ($start->diffInMonths($end) > 1) { if ($start->diffInMonths($end, true) > 1) {
$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 ($start->diffInMonths($end) > 12) { if ($start->diffInMonths($end, true) > 12) {
$increment = 'addYear'; $increment = 'addYear';
$displayFormat = (string)trans('config.year_js'); $displayFormat = (string)trans('config.year_js');
} }
@@ -471,11 +471,11 @@ class Navigation
public function preferredCarbonFormat(Carbon $start, Carbon $end): string public function preferredCarbonFormat(Carbon $start, Carbon $end): string
{ {
$format = 'Y-m-d'; $format = 'Y-m-d';
if ($start->diffInMonths($end) > 1) { if ($start->diffInMonths($end, true) > 0) {
$format = 'Y-m'; $format = 'Y-m';
} }
if ($start->diffInMonths($end) > 12) { if ($start->diffInMonths($end, true) > 12) {
$format = 'Y'; $format = 'Y';
} }
@@ -540,11 +540,11 @@ class Navigation
{ {
$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) > 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) > 12) { if ($start->diffInMonths($end, true) > 12) {
$format = (string)trans('config.year_js', [], $locale); $format = (string)trans('config.year_js', [], $locale);
} }
@@ -558,11 +558,11 @@ class Navigation
public function preferredEndOfPeriod(Carbon $start, Carbon $end): string public function preferredEndOfPeriod(Carbon $start, Carbon $end): string
{ {
$format = 'endOfDay'; $format = 'endOfDay';
if ($start->diffInMonths($end) > 1) { if ($start->diffInMonths($end, true) > 1) {
$format = 'endOfMonth'; $format = 'endOfMonth';
} }
if ($start->diffInMonths($end) > 12) { if ($start->diffInMonths($end, true) > 12) {
$format = 'endOfYear'; $format = 'endOfYear';
} }
@@ -576,11 +576,11 @@ class Navigation
public function preferredRangeFormat(Carbon $start, Carbon $end): string public function preferredRangeFormat(Carbon $start, Carbon $end): string
{ {
$format = '1D'; $format = '1D';
if ($start->diffInMonths($end) > 1) { if ($start->diffInMonths($end, true) > 1) {
$format = '1M'; $format = '1M';
} }
if ($start->diffInMonths($end) > 12) { if ($start->diffInMonths($end, true) > 12) {
$format = '1Y'; $format = '1Y';
} }
@@ -594,11 +594,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 ($start->diffInMonths($end) > 1) { if ($start->diffInMonths($end, true) > 1) {
$format = '%Y-%m'; $format = '%Y-%m';
} }
if ($start->diffInMonths($end) > 12) { if ($start->diffInMonths($end, true) > 12) {
$format = '%Y'; $format = '%Y';
} }
@@ -654,7 +654,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 = $tStart->diffInDays($tEnd); $diffInDays = (int) $tStart->diffInDays($tEnd, true);
$date->subDays($diffInDays * $subtract); $date->subDays($diffInDays * $subtract);
return $date; return $date;

View File

@@ -248,7 +248,7 @@ class PiggyBankTransformer extends AbstractTransformer
if (bccomp($currentAmount, $targetAmount) < 1) { if (bccomp($currentAmount, $targetAmount) < 1) {
$now = today(config('app.timezone')); $now = today(config('app.timezone'));
$startDate = null !== $startDate && $startDate->gte($now) ? $startDate : $now; $startDate = null !== $startDate && $startDate->gte($now) ? $startDate : $now;
$diffInMonths = $startDate->diffInMonths($targetDate, false); $diffInMonths = (int) $startDate->diffInMonths($targetDate);
$remainingAmount = bcsub($targetAmount, $currentAmount); $remainingAmount = bcsub($targetAmount, $currentAmount);
// more than 1 month to go and still need money to save: // more than 1 month to go and still need money to save:

View File

@@ -48,7 +48,7 @@ final class BillDateCalculatorTest extends TestCase
{ {
// Carbon $earliest, Carbon $latest, Carbon $billStart, string $period, int $skip, ?Carbon $lastPaid // Carbon $earliest, Carbon $latest, Carbon $billStart, string $period, int $skip, ?Carbon $lastPaid
return [ return [
// basic monthly bill. // basic monthly bill.x
'1Ma' => ['earliest' => Carbon::parse('2023-11-01'), 'latest' => Carbon::parse('2023-11-30'), 'billStart' => Carbon::parse('2023-01-01'), 'period' => 'monthly', 'skip' => 0, 'lastPaid' => null, 'expected' => ['2023-11-01']], '1Ma' => ['earliest' => Carbon::parse('2023-11-01'), 'latest' => Carbon::parse('2023-11-30'), 'billStart' => Carbon::parse('2023-01-01'), 'period' => 'monthly', 'skip' => 0, 'lastPaid' => null, 'expected' => ['2023-11-01']],
// already paid on the first, expect it next month. // already paid on the first, expect it next month.
'1Mb' => ['earliest' => Carbon::parse('2023-11-01'), 'latest' => Carbon::parse('2023-11-30'), 'billStart' => Carbon::parse('2023-01-01'), 'period' => 'monthly', 'skip' => 0, 'lastPaid' => Carbon::parse('2023-11-01'), 'expected' => ['2023-12-01']], '1Mb' => ['earliest' => Carbon::parse('2023-11-01'), 'latest' => Carbon::parse('2023-11-30'), 'billStart' => Carbon::parse('2023-01-01'), 'period' => 'monthly', 'skip' => 0, 'lastPaid' => Carbon::parse('2023-11-01'), 'expected' => ['2023-12-01']],