diff --git a/app/Actions/CreateDefaultCategories.php b/app/Actions/CreateDefaultCategories.php index e24196fe..1271454c 100644 --- a/app/Actions/CreateDefaultCategories.php +++ b/app/Actions/CreateDefaultCategories.php @@ -11,14 +11,13 @@ class CreateDefaultCategories */ public function handle(User $user): void { - if ($user->categories()->exists()) { - return; - } - $defaultCategories = self::getDefaultCategories(); foreach ($defaultCategories as $category) { - $user->categories()->create($category); + $user->categories()->firstOrCreate( + ['name' => $category['name']], + $category + ); } } diff --git a/app/Console/Commands/ResetUserCategories.php b/app/Console/Commands/ResetUserCategories.php index a564c077..da2adad7 100644 --- a/app/Console/Commands/ResetUserCategories.php +++ b/app/Console/Commands/ResetUserCategories.php @@ -44,7 +44,7 @@ class ResetUserCategories extends Command $categoriesCount = $user->categories()->count(); if ($categoriesCount > 0) { - $user->categories()->delete(); + $user->categories()->forceDelete(); $this->info("Deleted {$categoriesCount} existing categories."); } else { $this->info('No existing categories found.'); diff --git a/app/Services/PeriodComparator.php b/app/Services/PeriodComparator.php index 2bd2bb80..bd7f32a8 100644 --- a/app/Services/PeriodComparator.php +++ b/app/Services/PeriodComparator.php @@ -19,10 +19,10 @@ class PeriodComparator if ($this->isFullMonthRange()) { $months = $this->from->diffInMonths($this->to->copy()->addDay()); - return new self( - $this->from->copy()->subMonths($months), - $this->to->copy()->subMonths($months)->endOfMonth() - ); + $previousFrom = $this->from->copy()->subMonths($months); + $previousTo = $previousFrom->copy()->endOfMonth(); + + return new self($previousFrom, $previousTo); } return new self(