Skip to content

Commit

Permalink
fix: update current month sync on assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
jesusantguerrero committed Mar 28, 2024
1 parent e8514ae commit fd1141a
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ protected function index(Request $request) {
return $model->withCurrentSavings($startDate);
}));


return inertia($this->templates['index'],
[
$resourceName => $this->parser($resources),
$resourceName => $resources,
"serverSearchOptions" => $this->getServerParams(),
"accountTotal" => $this->accountService->getBalanceAs($teamId, $endDate),
"distribution" => fn () => BudgetMonth::getMonthAssignmentByGroup($teamId, $startDate),
Expand Down
5 changes: 0 additions & 5 deletions app/Domains/Budget/Services/BudgetCategoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Brick\Money\Money;
use Illuminate\Support\Str;
use App\Events\BudgetAssigned;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use App\Helpers\RequestQueryHelper;
Expand Down Expand Up @@ -227,10 +226,6 @@ public function getCategoryActivity(Category $category, string $month)
$activity = $category->getMonthBalance($monthDate->format('Y-m'))?->balance;
}

// if ($monthDate->format('Y-m') == '2024-01') {
// dd($transactions, $activity);
// }

return ($activity + $transactions) ?? 0;
}

Expand Down
13 changes: 10 additions & 3 deletions app/Domains/Budget/Services/BudgetMovementService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Domains\Budget\Services;

use App\Events\BudgetAssigned;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Insane\Journal\Models\Core\Category;
use App\Domains\Budget\Models\BudgetMonth;
Expand All @@ -17,7 +18,7 @@ class BudgetMovementService

const MODE_SUBTRACT = 'subtract';

public function __construct(public BudgetCategoryService $budgetService) {}
public function __construct(public BudgetCategoryService $budgetService, private BudgetRolloverService $budgetRolloverService) {}

/**
* Based on the direction of the movement for each account add or subtract the amount
Expand Down Expand Up @@ -70,7 +71,10 @@ public function registerMovement(BudgetMovementData $data, $quietly = false, $fr
$this->updateBalances($sourceId, $savedMovement, $savedMovement->date, $amount, self::MODE_SUBTRACT);
}
DB::commit();
BudgetAssigned::dispatch($data, $formData);
$this->budgetRolloverService->startFrom($data->team_id, substr($data->date, 0, 7), 1);
if (!now()->isSameMonth(Carbon::createFromFormat("Y-m-d", $data->date))) {
BudgetAssigned::dispatch($data, $formData);
}
}

public function registerAssignment(BudgetAssignData $data, $quietly = false)
Expand Down Expand Up @@ -99,7 +103,10 @@ public function registerAssignment(BudgetAssignData $data, $quietly = false)
$this->updateBalances($sourceId, $savedMovement, $savedMovement->date, $amount, self::MODE_SUBTRACT);
}
DB::commit();
BudgetAssigned::dispatch($data, $formData);
$this->budgetRolloverService->startFrom($data->team_id, substr($data->date, 0, 7), 1);
if (!now()->isSameMonth(Carbon::createFromFormat("Y-m-d", $data->date))) {
BudgetAssigned::dispatch($data, $formData);
}
}

public function getBalanceOfCategory($categoryId, string $month)
Expand Down
8 changes: 4 additions & 4 deletions app/Domains/Budget/Services/BudgetRolloverService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ public function rollMonth($teamId, $month, $categories = null) {
}
$overspending = 0;
$fundedFromBudgets = 0;
$overspendingCategories = [
$overspendingCategories = [];

];
foreach ($categories as $category) {
if($category->account_id) {
$fundedFromBudgets += $this->budgetCategoryService->updateFundedSpending($category, $month);
Expand Down Expand Up @@ -216,7 +215,7 @@ private function reduceOverspent() {
// Not seeing an Underfunded Alert in your Credit Card Payment category? We're testing this new feature in stages and releasing it to everyone soon.
}

public function startFrom($teamId, $yearMonth) {
public function startFrom($teamId, $yearMonth, $limit = null) {
$this->team = Team::find($teamId);
$this->accounts = Account::getByDetailTypes($teamId, AccountDetailType::ALL_CASH)->pluck('id');

Expand All @@ -231,6 +230,7 @@ public function startFrom($teamId, $yearMonth) {
->selectRaw("date_format(transaction_lines.date, '%Y-%m') AS date")
->groupBy(DB::raw("date_format(transaction_lines.date, '%Y-%m')"))
->whereRaw("date_format(transaction_lines.date, '%Y-%m') >= ?", [$yearMonth])
->when($limit, fn($q) => $q->limit($limit))
->get()
->pluck('date');

Expand All @@ -249,5 +249,5 @@ public function startFrom($teamId, $yearMonth) {
}
}

// transactions with more than 3 days prior to the las recinciled transaction are not imported
// transactions with more than 3 days prior to the las reconciled transaction are not imported
}
1 change: 0 additions & 1 deletion app/Events/BudgetAssigned.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Events;

use Illuminate\Queue\SerializesModels;
use App\Domains\Budget\Models\BudgetMonth;
use App\Domains\Budget\Data\BudgetAssignData;
use Illuminate\Foundation\Events\Dispatchable;
use App\Domains\Budget\Data\BudgetMovementData;
Expand Down
1 change: 0 additions & 1 deletion app/Listeners/UpdateBudgetAvailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Listeners;

use App\Events\BudgetAssigned;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Domains\Budget\Services\BudgetCategoryService;
use App\Domains\Budget\Services\BudgetRolloverService;
Expand Down
Loading

0 comments on commit fd1141a

Please sign in to comment.