Update various code for sqlite compatibility.

This commit is contained in:
James Cole
2024-12-28 13:36:25 +01:00
parent 42dc8486e9
commit 507040f1fd
9 changed files with 76 additions and 67 deletions

View File

@@ -276,13 +276,13 @@ class BasicController extends Controller
*/ */
private function getLeftToSpendInfo(Carbon $start, Carbon $end): array private function getLeftToSpendInfo(Carbon $start, Carbon $end): array
{ {
Log::debug(sprintf('Now in getLeftToSpendInfo("%s", "%s")', $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
$return = []; $return = [];
$today = today(config('app.timezone')); $today = today(config('app.timezone'));
$available = $this->abRepository->getAvailableBudgetWithCurrency($start, $end); $available = $this->abRepository->getAvailableBudgetWithCurrency($start, $end);
$budgets = $this->budgetRepository->getActiveBudgets(); $budgets = $this->budgetRepository->getActiveBudgets();
$spent = $this->opsRepository->sumExpenses($start, $end, null, $budgets); $spent = $this->opsRepository->sumExpenses($start, $end, null, $budgets);
$days = (int) $today->diffInDays($end, true) + 1; $days = (int) $today->diffInDays($end, true) + 1;
Log::debug(sprintf('Now in getLeftToSpendInfo("%s", "%s")', $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
foreach ($spent as $row) { foreach ($spent as $row) {
// either an amount was budgeted or 0 is available. // either an amount was budgeted or 0 is available.

View File

@@ -45,7 +45,6 @@ class ReportsIntegrity extends Command
return 1; return 1;
} }
$commands = [ $commands = [
// 'firefly-iii:add-timezones-to-dates',
'integrity:empty-objects', 'integrity:empty-objects',
'integrity:total-sums', 'integrity:total-sums',
]; ];

View File

@@ -32,6 +32,7 @@ use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\BudgetLimit;
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Support\Facades\Log;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
use Spatie\Period\Boundaries; use Spatie\Period\Boundaries;
@@ -45,20 +46,20 @@ class BudgetLimitHandler
{ {
public function created(Created $event): void public function created(Created $event): void
{ {
app('log')->debug(sprintf('BudgetLimitHandler::created(#%s)', $event->budgetLimit->id)); Log::debug(sprintf('BudgetLimitHandler::created(#%s)', $event->budgetLimit->id));
$this->updateAvailableBudget($event->budgetLimit); $this->updateAvailableBudget($event->budgetLimit);
} }
private function updateAvailableBudget(BudgetLimit $budgetLimit): void private function updateAvailableBudget(BudgetLimit $budgetLimit): void
{ {
app('log')->debug(sprintf('Now in updateAvailableBudget(#%d)', $budgetLimit->id)); Log::debug(sprintf('Now in updateAvailableBudget(limit #%d)', $budgetLimit->id));
$budget = Budget::find($budgetLimit->budget_id); $budget = Budget::find($budgetLimit->budget_id);
if (null === $budget) { if (null === $budget) {
app('log')->warning('Budget is null, probably deleted, find deleted version.'); Log::warning('Budget is null, probably deleted, find deleted version.');
$budget = Budget::withTrashed()->find($budgetLimit->budget_id); $budget = Budget::withTrashed()->find($budgetLimit->budget_id);
} }
if (null === $budget) { if (null === $budget) {
app('log')->warning('Budget is still null, cannot continue, will delete budget limit.'); Log::warning('Budget is still null, cannot continue, will delete budget limit.');
$budgetLimit->forceDelete(); $budgetLimit->forceDelete();
return; return;
@@ -69,7 +70,7 @@ class BudgetLimitHandler
// sanity check. It happens when the budget has been deleted so the original user is unknown. // sanity check. It happens when the budget has been deleted so the original user is unknown.
if (null === $user) { if (null === $user) {
app('log')->warning('User is null, cannot continue.'); Log::warning('User is null, cannot continue.');
$budgetLimit->forceDelete(); $budgetLimit->forceDelete();
return; return;
@@ -82,7 +83,7 @@ class BudgetLimitHandler
try { try {
$viewRange = app('preferences')->getForUser($user, 'viewRange', '1M')->data; $viewRange = app('preferences')->getForUser($user, 'viewRange', '1M')->data;
} catch (ContainerExceptionInterface|NotFoundExceptionInterface $e) { } catch (ContainerExceptionInterface|NotFoundExceptionInterface $e) {
app('log')->error($e->getMessage()); Log::error($e->getMessage());
$viewRange = '1M'; $viewRange = '1M';
} }
// safety catch // safety catch
@@ -97,7 +98,7 @@ class BudgetLimitHandler
// limit period in total is: // limit period in total is:
$limitPeriod = Period::make($start, $end, precision: Precision::DAY(), boundaries: Boundaries::EXCLUDE_NONE()); $limitPeriod = Period::make($start, $end, precision: Precision::DAY(), boundaries: Boundaries::EXCLUDE_NONE());
app('log')->debug(sprintf('Limit period is from %s to %s', $start->format('Y-m-d'), $end->format('Y-m-d'))); Log::debug(sprintf('Limit period is from %s to %s', $start->format('Y-m-d'), $end->format('Y-m-d')));
// from the start until the end of the budget limit, need to loop! // from the start until the end of the budget limit, need to loop!
$current = clone $start; $current = clone $start;
@@ -106,16 +107,14 @@ class BudgetLimitHandler
// create or find AB for this particular period, and set the amount accordingly. // create or find AB for this particular period, and set the amount accordingly.
/** @var null|AvailableBudget $availableBudget */ /** @var null|AvailableBudget $availableBudget */
$availableBudget = $user->availableBudgets()->where('start_date', $current->format('Y-m-d'))->where( $availableBudget = $user->availableBudgets()->where('start_date', $current->format('Y-m-d'))->where('end_date', $currentEnd->format('Y-m-d'))->where('transaction_currency_id', $budgetLimit->transaction_currency_id)->first();
'end_date',
$currentEnd->format('Y-m-d')
)->where('transaction_currency_id', $budgetLimit->transaction_currency_id)->first();
if (null !== $availableBudget) { if (null !== $availableBudget) {
app('log')->debug('Found 1 AB, will update.'); Log::debug('Found 1 AB, will update.');
$this->calculateAmount($availableBudget); $this->calculateAmount($availableBudget);
} }
if (null === $availableBudget) { if (null === $availableBudget) {
Log::debug('No AB found, will create.');
// if not exists: // if not exists:
$currentPeriod = Period::make($current, $currentEnd, precision: Precision::DAY(), boundaries: Boundaries::EXCLUDE_NONE()); $currentPeriod = Period::make($current, $currentEnd, precision: Precision::DAY(), boundaries: Boundaries::EXCLUDE_NONE());
$daily = $this->getDailyAmount($budgetLimit); $daily = $this->getDailyAmount($budgetLimit);
@@ -126,10 +125,10 @@ class BudgetLimitHandler
$amount = 0 === $budgetLimit->id ? '0' : $budgetLimit->amount; $amount = 0 === $budgetLimit->id ? '0' : $budgetLimit->amount;
} }
if (0 === bccomp($amount, '0')) { if (0 === bccomp($amount, '0')) {
app('log')->debug('Amount is zero, will not create AB.'); Log::debug('Amount is zero, will not create AB.');
} }
if (0 !== bccomp($amount, '0')) { if (0 !== bccomp($amount, '0')) {
app('log')->debug(sprintf('Will create AB for period %s to %s', $current->format('Y-m-d'), $currentEnd->format('Y-m-d'))); Log::debug(sprintf('Will create AB for period %s to %s', $current->format('Y-m-d'), $currentEnd->format('Y-m-d')));
$availableBudget = new AvailableBudget( $availableBudget = new AvailableBudget(
[ [
'user_id' => $user->id, 'user_id' => $user->id,
@@ -143,7 +142,7 @@ class BudgetLimitHandler
] ]
); );
$availableBudget->save(); $availableBudget->save();
app('log')->debug(sprintf('ID of new AB is #%d', $availableBudget->id)); Log::debug(sprintf('ID of new AB is #%d', $availableBudget->id));
} }
} }
@@ -158,7 +157,7 @@ class BudgetLimitHandler
$repository->setUser($availableBudget->user); $repository->setUser($availableBudget->user);
$newAmount = '0'; $newAmount = '0';
$abPeriod = Period::make($availableBudget->start_date, $availableBudget->end_date, Precision::DAY()); $abPeriod = Period::make($availableBudget->start_date, $availableBudget->end_date, Precision::DAY());
app('log')->debug( Log::debug(
sprintf( sprintf(
'Now at AB #%d, ("%s" to "%s")', 'Now at AB #%d, ("%s" to "%s")',
$availableBudget->id, $availableBudget->id,
@@ -168,11 +167,11 @@ class BudgetLimitHandler
); );
// have to recalculate everything just in case. // have to recalculate everything just in case.
$set = $repository->getAllBudgetLimitsByCurrency($availableBudget->transactionCurrency, $availableBudget->start_date, $availableBudget->end_date); $set = $repository->getAllBudgetLimitsByCurrency($availableBudget->transactionCurrency, $availableBudget->start_date, $availableBudget->end_date);
app('log')->debug(sprintf('Found %d interesting budget limit(s).', $set->count())); Log::debug(sprintf('Found %d interesting budget limit(s).', $set->count()));
/** @var BudgetLimit $budgetLimit */ /** @var BudgetLimit $budgetLimit */
foreach ($set as $budgetLimit) { foreach ($set as $budgetLimit) {
app('log')->debug( Log::debug(
sprintf( sprintf(
'Found interesting budget limit #%d ("%s" to "%s")', 'Found interesting budget limit #%d ("%s" to "%s")',
$budgetLimit->id, $budgetLimit->id,
@@ -189,16 +188,16 @@ class BudgetLimitHandler
); );
// if both equal each other, amount from this BL must be added to the AB // if both equal each other, amount from this BL must be added to the AB
if ($limitPeriod->equals($abPeriod)) { if ($limitPeriod->equals($abPeriod)) {
app('log')->debug('This budget limit is equal to the available budget period.'); Log::debug('This budget limit is equal to the available budget period.');
$newAmount = bcadd($newAmount, $budgetLimit->amount); $newAmount = bcadd($newAmount, $budgetLimit->amount);
} }
// if budget limit period is inside AB period, it can be added in full. // if budget limit period is inside AB period, it can be added in full.
if (!$limitPeriod->equals($abPeriod) && $abPeriod->contains($limitPeriod)) { if (!$limitPeriod->equals($abPeriod) && $abPeriod->contains($limitPeriod)) {
app('log')->debug('This budget limit is smaller than the available budget period.'); Log::debug('This budget limit is smaller than the available budget period.');
$newAmount = bcadd($newAmount, $budgetLimit->amount); $newAmount = bcadd($newAmount, $budgetLimit->amount);
} }
if (!$limitPeriod->equals($abPeriod) && !$abPeriod->contains($limitPeriod) && $abPeriod->overlapsWith($limitPeriod)) { if (!$limitPeriod->equals($abPeriod) && !$abPeriod->contains($limitPeriod) && $abPeriod->overlapsWith($limitPeriod)) {
app('log')->debug('This budget limit is something else entirely!'); Log::debug('This budget limit is something else entirely!');
$overlap = $abPeriod->overlap($limitPeriod); $overlap = $abPeriod->overlap($limitPeriod);
if (null !== $overlap) { if (null !== $overlap) {
$length = $overlap->length(); $length = $overlap->length();
@@ -208,12 +207,12 @@ class BudgetLimitHandler
} }
} }
if (0 === bccomp('0', $newAmount)) { if (0 === bccomp('0', $newAmount)) {
app('log')->debug('New amount is zero, deleting AB.'); Log::debug('New amount is zero, deleting AB.');
$availableBudget->delete(); $availableBudget->delete();
return; return;
} }
app('log')->debug(sprintf('Concluded new amount for this AB must be %s', $newAmount)); Log::debug(sprintf('Concluded new amount for this AB must be %s', $newAmount));
$availableBudget->amount = app('steam')->bcround($newAmount, $availableBudget->transactionCurrency->decimal_places); $availableBudget->amount = app('steam')->bcround($newAmount, $availableBudget->transactionCurrency->decimal_places);
$availableBudget->save(); $availableBudget->save();
} }
@@ -231,7 +230,7 @@ class BudgetLimitHandler
); );
$days = $limitPeriod->length(); $days = $limitPeriod->length();
$amount = bcdiv($budgetLimit->amount, (string) $days, 12); $amount = bcdiv($budgetLimit->amount, (string) $days, 12);
app('log')->debug( Log::debug(
sprintf('Total amount for budget limit #%d is %s. Nr. of days is %d. Amount per day is %s', $budgetLimit->id, $budgetLimit->amount, $days, $amount) sprintf('Total amount for budget limit #%d is %s. Nr. of days is %d. Amount per day is %s', $budgetLimit->id, $budgetLimit->amount, $days, $amount)
); );
@@ -240,7 +239,7 @@ class BudgetLimitHandler
public function deleted(Deleted $event): void public function deleted(Deleted $event): void
{ {
app('log')->debug(sprintf('BudgetLimitHandler::deleted(#%s)', $event->budgetLimit->id)); Log::debug(sprintf('BudgetLimitHandler::deleted(#%s)', $event->budgetLimit->id));
$budgetLimit = $event->budgetLimit; $budgetLimit = $event->budgetLimit;
$budgetLimit->id = 0; $budgetLimit->id = 0;
$this->updateAvailableBudget($event->budgetLimit); $this->updateAvailableBudget($event->budgetLimit);
@@ -248,7 +247,7 @@ class BudgetLimitHandler
public function updated(Updated $event): void public function updated(Updated $event): void
{ {
app('log')->debug(sprintf('BudgetLimitHandler::updated(#%s)', $event->budgetLimit->id)); Log::debug(sprintf('BudgetLimitHandler::updated(#%s)', $event->budgetLimit->id));
$this->updateAvailableBudget($event->budgetLimit); $this->updateAvailableBudget($event->budgetLimit);
} }
} }

View File

@@ -46,6 +46,7 @@ class AvailableBudgetObserver
private function updateNativeAmount(AvailableBudget $availableBudget): void private function updateNativeAmount(AvailableBudget $availableBudget): void
{ {
if (!Amount::convertToNative($availableBudget->user)) { if (!Amount::convertToNative($availableBudget->user)) {
Log::debug('Do not update native available amount of the available budget.');
return; return;
} }
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($availableBudget->user->userGroup); $userCurrency = app('amount')->getDefaultCurrencyByUserGroup($availableBudget->user->userGroup);

View File

@@ -46,6 +46,7 @@ class BudgetLimitObserver
private function updateNativeAmount(BudgetLimit $budgetLimit): void private function updateNativeAmount(BudgetLimit $budgetLimit): void
{ {
if (!Amount::convertToNative($budgetLimit->budget->user)) { if (!Amount::convertToNative($budgetLimit->budget->user)) {
Log::debug('Do not update native amount of the budget limit.');
return; return;
} }
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($budgetLimit->budget->user->userGroup); $userCurrency = app('amount')->getDefaultCurrencyByUserGroup($budgetLimit->budget->user->userGroup);

View File

@@ -62,8 +62,8 @@ class InstallController extends Controller
'migrate' => ['--seed' => true, '--force' => true], 'migrate' => ['--seed' => true, '--force' => true],
'generate-keys' => [], // an exception :( 'generate-keys' => [], // an exception :(
'firefly-iii:upgrade-database' => [], 'firefly-iii:upgrade-database' => [],
'firefly-iii:correct-database' => [], //'firefly-iii:correct-database' => [],
'firefly-iii:report-integrity' => [], //'firefly-iii:report-integrity' => [],
'firefly-iii:set-latest-version' => ['--james-is-cool' => true], 'firefly-iii:set-latest-version' => ['--james-is-cool' => true],
'firefly-iii:verify-security-alerts' => [], 'firefly-iii:verify-security-alerts' => [],
]; ];

View File

@@ -68,8 +68,8 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
if (null !== $start && null !== $end) { if (null !== $start && null !== $end) {
$query->where( $query->where(
static function (Builder $q1) use ($start, $end): void { // @phpstan-ignore-line static function (Builder $q1) use ($start, $end): void { // @phpstan-ignore-line
$q1->where('start_date', '=', $start->format('Y-m-d')); $q1->where('start_date', '=', $start->format('Y-m-d H:i:s'));
$q1->where('end_date', '=', $end->format('Y-m-d')); $q1->where('end_date', '=', $end->format('Y-m-d H:i:s'));
} }
); );
} }
@@ -128,12 +128,15 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
public function getAvailableBudgetWithCurrency(Carbon $start, Carbon $end): array public function getAvailableBudgetWithCurrency(Carbon $start, Carbon $end): array
{ {
Log::debug(sprintf('Now in %s(%s, %s)',__METHOD__, $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
$return = []; $return = [];
$availableBudgets = $this->user->availableBudgets() $availableBudgets = $this->user->availableBudgets()
->where('start_date', $start->format('Y-m-d')) ->where('start_date', $start->format('Y-m-d H:i:s'))
->where('end_date', $end->format('Y-m-d'))->get() ->where('end_date', $end->format('Y-m-d H:i:s'))->get()
; ;
Log::debug(sprintf('Found %d available budgets', $availableBudgets->count()));
// use native amount if necessary? // use native amount if necessary?
$convertToNative = Amount::convertToNative($this->user); $convertToNative = Amount::convertToNative($this->user);
$default = Amount::getDefaultCurrency(); $default = Amount::getDefaultCurrency();
@@ -144,6 +147,7 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
$field = $convertToNative && $availableBudget->transaction_currency_id !== $default->id ? 'native_amount' : 'amount'; $field = $convertToNative && $availableBudget->transaction_currency_id !== $default->id ? 'native_amount' : 'amount';
$return[$currencyId] ??= '0'; $return[$currencyId] ??= '0';
$return[$currencyId] = bcadd($return[$currencyId], $availableBudget->{$field}); $return[$currencyId] = bcadd($return[$currencyId], $availableBudget->{$field});
Log::debug(sprintf('Add #%d %s (%s) for a total of %s', $currencyId, $availableBudget->{$field}, $field, $return[$currencyId]));
} }
return $return; return $return;

View File

@@ -211,7 +211,7 @@ class OperationsRepository implements OperationsRepositoryInterface
?Collection $budgets = null, ?Collection $budgets = null,
?TransactionCurrency $currency = null ?TransactionCurrency $currency = null
): array { ): array {
Log::debug('Start of sumExpenses.'); Log::debug(sprintf('Start of %s.', __METHOD__));
// this collector excludes all transfers TO liabilities (which are also withdrawals) // this collector excludes all transfers TO liabilities (which are also withdrawals)
// because those expenses only become expenses once they move from the liability to the friend. // because those expenses only become expenses once they move from the liability to the friend.

View File

@@ -28,9 +28,10 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\UserGroup; use FireflyIII\Models\UserGroup;
use FireflyIII\Support\Facades\Preferences;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use FireflyIII\Support\Facades\Preferences; use Illuminate\Support\Facades\Log;
/** /**
* Class Amount. * Class Amount.
@@ -71,10 +72,14 @@ class Amount
public function convertToNative(?User $user = null): bool public function convertToNative(?User $user = null): bool
{ {
if (null === $user) { if (null === $user) {
return Preferences::get('convert_to_native', false)->data && config('cer.enabled'); $result = Preferences::get('convert_to_native', false)->data && config('cer.enabled');
// Log::debug(sprintf('convertToNative [a]: %s', var_export($result, true)));
return $result;
} }
return Preferences::getForUser($user, 'convert_to_native', false)->data && config('cer.enabled'); $result = Preferences::getForUser($user, 'convert_to_native', false)->data && config('cer.enabled');
//Log::debug(sprintf('convertToNative [b]: %s', var_export($result, true)));
return $result;
} }
/** /**