From 3436206edff5f0dc043f49d9ef472dc939086cf4 Mon Sep 17 00:00:00 2001 From: Jesus Guerrero Date: Sat, 18 Nov 2023 06:34:17 -0400 Subject: [PATCH] fix improove budget totals --- .../Budget/Services/BudgetCategoryService.php | 28 ++++++++++------- .../Budget/Services/BudgetRolloverService.php | 30 +++++-------------- resources/js/Pages/Dashboard.vue | 4 +-- resources/js/domains/budget/budgetTotals.ts | 4 +-- .../budget/components/BudgetBalanceAssign.vue | 6 +++- resources/js/domains/budget/useBudget.ts | 15 +++++++--- 6 files changed, 46 insertions(+), 41 deletions(-) diff --git a/app/Domains/Budget/Services/BudgetCategoryService.php b/app/Domains/Budget/Services/BudgetCategoryService.php index 9622b013..7ea71820 100644 --- a/app/Domains/Budget/Services/BudgetCategoryService.php +++ b/app/Domains/Budget/Services/BudgetCategoryService.php @@ -121,26 +121,31 @@ public function getBudgetInfo($category, string $month) ->toFloat(); } else { $monthBalance = (float) $category->getMonthBalance($yearMonth)->balance; - $available = Money::of($budgeted, 'USD')->plus($monthBudget?->left_from_last_month ?? 0)->plus($monthBalance)->getAmount()->toFloat(); + + $available = Money::of($budgeted, 'USD') + ->plus($monthBudget?->left_from_last_month ?? 0) + ->plus($monthBalance) + ->getAmount() + ->toFloat(); + + if ($category->display_id == 'ready_to_assign') { + $available = $monthBudget?->available; + $monthBalance = $monthBudget?->activity; + // dd($monthBalance, $available, $monthBalance, $monthBudget); + } } $data = [ - 'budgeted' => $budgeted, + 'budgeted' => $monthBudget?->budgeted, 'activity' => $monthBalance, 'available' => $available, 'payments' => $monthBudget?->payments ?? 0, 'left_from_last_month' => $monthBudget?->left_from_last_month ?? 0, - 'funded_spending_previous_month' => $monthBudget?->funded_spending_previous_month ?? 0, + 'funded_spending_previous_month' => 0, 'funded_spending' => $monthBudget?->funded_spending ?? 0, 'name' => $category->name, 'month' => $yearMonth, ]; - - // if ($category->id == 733) { - // // dd($data); - // // dd($prevMonthLeftOver); - // } - return $data; } @@ -282,6 +287,8 @@ public function updateFundedSpending(Category $category, string $month) $transactions = $category->account->getMonthFundedSpending($yearMonth)->balance; $payments = $category->account->getMonthPayments($yearMonth)->balance; + $fundedSpending = ($transactions * -1) ?? 0; + BudgetMonth::updateOrCreate([ 'category_id' => $category->id, 'team_id' => $category->team_id, @@ -289,8 +296,9 @@ public function updateFundedSpending(Category $category, string $month) 'name' => $month, ], [ 'user_id' => $category->user_id, - 'funded_spending' => ($transactions * -1) ?? 0, + 'funded_spending' => $fundedSpending, 'payments' => $payments ?? 0, + 'available' => DB::raw("$fundedSpending + available - $payments"), ]); echo "{$category->name} updated to {$activity}".PHP_EOL; diff --git a/app/Domains/Budget/Services/BudgetRolloverService.php b/app/Domains/Budget/Services/BudgetRolloverService.php index d6e9cebd..2c12da8b 100644 --- a/app/Domains/Budget/Services/BudgetRolloverService.php +++ b/app/Domains/Budget/Services/BudgetRolloverService.php @@ -2,16 +2,15 @@ namespace App\Domains\Budget\Services; -use App\Domains\Budget\Data\BudgetReservedNames; -use App\Domains\Budget\Models\BudgetMonth; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\DB; use Insane\Journal\Models\Core\Category; +use App\Domains\Budget\Models\BudgetMonth; +use App\Domains\Budget\Data\BudgetReservedNames; class BudgetRolloverService { - public function __construct(private BudgetCategoryService $budgetCategoryService) { + public function __construct(private BudgetCategoryService $budgetCategoryService) {} - } public function rollMonth($teamId, $month, $categories = null) { if (!$categories) { $categories = Category::where([ @@ -40,19 +39,11 @@ private function setNewMonthBudget($category, $month) { 'name' => $month, ])->first(); $available = ($budgetMonth?->budgeted ?? 0) + ($budgetMonth->left_from_last_month ?? 0) + $activity; - $leftFunded = 0; - if ($category->account_id) { - $leftFunded = ($budgetMonth?->funded_spending ?? 0) + ($budgetMonth?->funded_spending_previous_month ?? 0) - ($budgetMonth?->payments ?? 0); - } - $this->movePositiveAmounts($category, $month, $available, $leftFunded); - // If your category had been overspent in cash (negative red Available), that amount will be deducted from Ready to Assign in the new month. + $this->movePositiveAmounts($category, $month, $available); - // If your category had been overspent in credit (negative yellow Available), the amount you overspent will be represented as an Underfunded alert ↗️ in your Credit Card Payment category. If you can't cover this overspending in the month it happens, you'll need to assign funds directly to the Credit Card Payment category to pay back the debt. - - // 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. } - private function movePositiveAmounts($category, $oldMonth, $available, $leftFunded = 0) { + private function movePositiveAmounts($category, $oldMonth, $available) { $nextMonth = Carbon::createFromFormat("Y-m-d", $oldMonth)->addMonthsWithNoOverflow(1)->format('Y-m-d'); BudgetMonth::updateOrCreate([ 'category_id' => $category->id, @@ -62,7 +53,7 @@ private function movePositiveAmounts($category, $oldMonth, $available, $leftFund ], [ 'user_id' => $category->user_id, 'left_from_last_month' => $available ?? 0, - 'funded_spending_previous_month' => $leftFunded + 'funded_spending_previous_month' => 0 ]); } @@ -94,8 +85,7 @@ private function moveReadyToAssign($teamId, $month) { $activity = (new BudgetCategoryService($readyToAssignCategory))->getCategoryActivity($readyToAssignCategory, $month); $activityPlusLeft = $activity + $budgetMonth->left_from_last_month; - $available = $activityPlusLeft - ($results?->budgeted ?? 0); - $leftFunded = ($results?->funded_spending ?? 0) + ($results?->funded_spending_previous_month ?? 0) - ($results?->payments ?? 0); + $available = $activityPlusLeft - ($results?->budgeted ?? 0) ; $nextMonth = Carbon::createFromFormat("Y-m-d", $month)->addMonthsWithNoOverflow(1)->format('Y-m-d'); @@ -123,7 +113,7 @@ private function moveReadyToAssign($teamId, $month) { ], [ 'user_id' => $readyToAssignCategory->user_id, 'left_from_last_month' => $available ?? 0, - 'funded_spending_previous_month' => $leftFunded ?? 0, + 'funded_spending_previous_month' => 0, ]); } @@ -136,9 +126,5 @@ 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. } - private function setReadyToAssign() { - - } - // transactions with more than 3 days prior to the las recinciled transaction are not imported } diff --git a/resources/js/Pages/Dashboard.vue b/resources/js/Pages/Dashboard.vue index 617794bb..0dc53102 100644 --- a/resources/js/Pages/Dashboard.vue +++ b/resources/js/Pages/Dashboard.vue @@ -120,7 +120,7 @@
- +
diff --git a/resources/js/domains/budget/budgetTotals.ts b/resources/js/domains/budget/budgetTotals.ts index 53c2b75a..d8204645 100644 --- a/resources/js/domains/budget/budgetTotals.ts +++ b/resources/js/domains/budget/budgetTotals.ts @@ -27,14 +27,14 @@ export const getCategoriesTotals = (categories: Record, config = { categoryTotals.budgeted = ExactMath.add(categoryTotals.budgeted, category.budgeted || 0) categoryTotals.activity = ExactMath.add(categoryTotals.activity, category.activity || 0) categoryTotals.available = ExactMath.add(categoryTotals.available, category.available || 0) - categoryTotals.budgetAvailable = ExactMath.add(categoryTotals.budgetAvailable, !category.account_id && category.name !== InflowCategories.READY_TO_ASSIGN ? category.available : 0) + categoryTotals.budgetAvailable = ExactMath.add(categoryTotals.budgetAvailable, category.name !== InflowCategories.READY_TO_ASSIGN ? category.available : 0) categoryTotals.prevMonthLeftOver = ExactMath.add(categoryTotals.prevMonthLeftOver, category.prevMonthLeftOver || 0) // credit cards categoryTotals.budgetedSpending = ExactMath.add(categoryTotals.budgetedSpending, !category.account_id && category.name !== InflowCategories.READY_TO_ASSIGN ? category.activity : 0) categoryTotals.payments = ExactMath.add(categoryTotals.payments, category.name !== InflowCategories.READY_TO_ASSIGN ? category.payments : 0) categoryTotals.fundedSpending = ExactMath.add(categoryTotals.fundedSpending, category.name !== InflowCategories.READY_TO_ASSIGN ? category.funded_spending : 0) - categoryTotals.fundedSpendingPreviousMonth = ExactMath.add(categoryTotals.fundedSpendingPreviousMonth, category.name !== InflowCategories.READY_TO_ASSIGN ? category.funded_spending_previous_month : 0) + categoryTotals.fundedSpendingPreviousMonth = ExactMath.add(categoryTotals.fundedSpendingPreviousMonth, category.account_id && category.name !== InflowCategories.READY_TO_ASSIGN ? category.available : 0) if (category.account_id) { console.log(categoryTotals, category); diff --git a/resources/js/domains/budget/components/BudgetBalanceAssign.vue b/resources/js/domains/budget/components/BudgetBalanceAssign.vue index 71c0f227..340b4677 100644 --- a/resources/js/domains/budget/components/BudgetBalanceAssign.vue +++ b/resources/js/domains/budget/components/BudgetBalanceAssign.vue @@ -208,8 +208,12 @@

Available for funds:

+

Funded:

+

funded spending:

+

Total:

Budgeted:

-

Funded:

+

payments:

+

Total:

Assigned in month:

Balance:

diff --git a/resources/js/domains/budget/useBudget.ts b/resources/js/domains/budget/useBudget.ts index 3e00f3a4..4636dcf5 100644 --- a/resources/js/domains/budget/useBudget.ts +++ b/resources/js/domains/budget/useBudget.ts @@ -1,7 +1,10 @@ +import { isCurrentMonth } from './../../utils/index'; +import { budgetCols } from './budgetCols'; import { cloneDeep } from "lodash"; import { computed, watch, reactive, toRefs, Ref } from "vue"; import { getCategoriesTotals, getGroupTotals } from './index'; import { ICategory } from "../transactions/models"; +import { format } from 'date-fns'; export const BudgetState = reactive({ data: [], @@ -50,16 +53,20 @@ export const BudgetState = reactive({ readyToAssign: computed(() => { const budgetTotals = BudgetState.budgetTotals; const category = BudgetState.inflow?.subCategories[0] ?? {} - const creditCardFunded = parseFloat(budgetTotals?.fundedSpendingPreviousMonth ?? 0) + parseFloat(category?.funded_spending ?? 0) - const availableForFunding = (category?.activity + parseFloat(category?.left_from_last_month ?? 0)); - const assigned = budgetTotals.budgeted + (creditCardFunded - budgetTotals.payments); - const balance = availableForFunding - assigned; + const creditCardFunded = parseFloat(budgetTotals?.fundedSpendingPreviousMonth ?? 0) + const availableForFunding = parseFloat(category.available ?? 0); + const fundedSpending = parseFloat(category?.funded_spending ?? 0); + const assigned = budgetTotals.budgeted; + const isCurrentMonth = category.month == format(new Date(), 'yyyy-MM-01') + const balance = (availableForFunding) - (assigned + (isCurrentMonth ? creditCardFunded : 0)); + console.log(category) return { assigned, availableForFunding, balance, creditCardFunded, + fundedSpending, inflow: BudgetState.inflow?.activity, toAssign: category, ...budgetTotals,