Skip to content

Firefly III: a personal finances manager

Add cleanup method for issue #7853 and some more debug info.

James Colee3c4dde4ffdb
diff --git a/app/Http/Controllers/Budget/IndexController.php b/app/Http/Controllers/Budget/IndexController.php
index 8d541550b7..9060760cea 100644
--- a/app/Http/Controllers/Budget/IndexController.php
+++ b/app/Http/Controllers/Budget/IndexController.php
@@ -101,6 +101,7 @@ class IndexController extends Controller
*/
public function index(Request $request, Carbon $start = null, Carbon $end = null)
{
+ $this->abRepository->cleanup();
Log::debug(sprintf('Start of IndexController::index("%s", "%s")', $start?->format('Y-m-d'), $end?->format('Y-m-d')));
// collect some basic vars:
@@ -137,7 +138,7 @@ class IndexController extends Controller
// get budgeted for default currency:
if (0 === count($availableBudgets)) {
- $budgeted = $this->blRepository->budgeted($start, $end, $defaultCurrency, );
+ $budgeted = $this->blRepository->budgeted($start, $end, $defaultCurrency,);
$spentArr = $this->opsRepository->sumExpenses($start, $end, null, null, $defaultCurrency);
$spent = $spentArr[$defaultCurrency->id]['sum'] ?? '0';
unset($spentArr);
@@ -196,7 +197,7 @@ class IndexController extends Controller
$array['spent'] = $spentArr[$entry->transaction_currency_id]['sum'] ?? '0';
// budgeted in period:
- $budgeted = $this->blRepository->budgeted($entry->start_date, $entry->end_date, $entry->transactionCurrency, );
+ $budgeted = $this->blRepository->budgeted($entry->start_date, $entry->end_date, $entry->transactionCurrency,);
$array['budgeted'] = $budgeted;
$availableBudgets[] = $array;
unset($spentArr);
@@ -337,6 +338,7 @@ class IndexController extends Controller
*/
public function reorder(Request $request, BudgetRepositoryInterface $repository): JsonResponse
{
+ $this->abRepository->cleanup();
$budgetIds = $request->get('budgetIds');
foreach ($budgetIds as $index => $budgetId) {
diff --git a/app/Http/Controllers/Json/BoxController.php b/app/Http/Controllers/Json/BoxController.php
index 2e1b04743b..53a6cc23b8 100644
--- a/app/Http/Controllers/Json/BoxController.php
+++ b/app/Http/Controllers/Json/BoxController.php
@@ -60,6 +60,7 @@ class BoxController extends Controller
$opsRepository = app(OperationsRepositoryInterface::class);
/** @var AvailableBudgetRepositoryInterface $abRepository */
$abRepository = app(AvailableBudgetRepositoryInterface::class);
+ $abRepository->cleanup();
/** @var Carbon $start */
$start = session('start', today(config('app.timezone'))->startOfMonth());
/** @var Carbon $end */
@@ -86,6 +87,11 @@ class BoxController extends Controller
$availableBudgets = $availableBudgets->filter(
static function (AvailableBudget $availableBudget) use ($currency) {
if ($availableBudget->transaction_currency_id === $currency->id) {
+ app('log')->debug(sprintf('Will include AB #%d: from %s-%s amount %s',
+ $availableBudget->id,
+ $availableBudget->start_date->format('Y-m-d'),
+ $availableBudget->end_date->format('Y-m-d'),
+ $availableBudget->amount));
return $availableBudget;
}
diff --git a/app/Repositories/Budget/AvailableBudgetRepository.php b/app/Repositories/Budget/AvailableBudgetRepository.php
index a17b17d4e2..bc551c37d4 100644
--- a/app/Repositories/Budget/AvailableBudgetRepository.php
+++ b/app/Repositories/Budget/AvailableBudgetRepository.php
@@ -39,6 +39,49 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
{
private User $user;
+ /**
+ * @inheritDoc
+ */
+ public function cleanup(): void
+ {
+ $exists = [];
+ $availableBudgets = $this->user->availableBudgets()->get();
+ /** @var AvailableBudget $availableBudget */
+ foreach ($availableBudgets as $availableBudget) {
+ $start = $availableBudget->start_date->format('Y-m-d');
+ $end = $availableBudget->end_date->format('Y-m-d');
+ $key = sprintf('%s-%s-%s', $availableBudget->transaction_currency_id, $start, $end);
+ if (array_key_exists($key, $exists)) {
+ app('log')->debug(sprintf('Found duplicate AB: %s %s, %s-%s. Has been deleted', $availableBudget->transaction_currency_id, $availableBudget->amount, $start, $end));
+ $availableBudget->delete();
+ }
+ $exists[$key] = true;
+ }
+ }
+
+ /**
+ * Return a list of all available budgets (in all currencies) (for the selected period).
+ *
+ * @param Carbon|null $start
+ * @param Carbon|null $end
+ *
+ * @return Collection
+ */
+ public function get(?Carbon $start = null, ?Carbon $end = null): Collection
+ {
+ $query = $this->user->availableBudgets()->with(['transactionCurrency']);
+ if (null !== $start && null !== $end) {
+ $query->where(
+ static function (Builder $q1) use ($start, $end) {
+ $q1->where('start_date', '=', $start->format('Y-m-d'));
+ $q1->where('end_date', '=', $end->format('Y-m-d'));
+ }
+ );
+ }
+
+ return $query->get(['available_budgets.*']);
+ }
+
/**
* Delete all available budgets.
*/
@@ -122,29 +165,6 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
return $return;
}
- /**
- * Return a list of all available budgets (in all currencies) (for the selected period).
- *
- * @param Carbon|null $start
- * @param Carbon|null $end
- *
- * @return Collection
- */
- public function get(?Carbon $start = null, ?Carbon $end = null): Collection
- {
- $query = $this->user->availableBudgets()->with(['transactionCurrency']);
- if (null !== $start && null !== $end) {
- $query->where(
- static function (Builder $q1) use ($start, $end) {
- $q1->where('start_date', '=', $start->format('Y-m-d'));
- $q1->where('end_date', '=', $end->format('Y-m-d'));
- }
- );
- }
-
- return $query->get(['available_budgets.*']);
- }
-
/**
* Returns all available budget objects.
*
diff --git a/app/Repositories/Budget/AvailableBudgetRepositoryInterface.php b/app/Repositories/Budget/AvailableBudgetRepositoryInterface.php
index 56cd8172e1..dcc0be4e7c 100644
--- a/app/Repositories/Budget/AvailableBudgetRepositoryInterface.php
+++ b/app/Repositories/Budget/AvailableBudgetRepositoryInterface.php
@@ -35,6 +35,11 @@ use Illuminate\Support\Collection;
*/
interface AvailableBudgetRepositoryInterface
{
+ /**
+ * @return void
+ */
+ public function cleanup(): void;
+
/**
* Delete all available budgets.
*/