Skip to content

Commit

Permalink
fix improove budget totals
Browse files Browse the repository at this point in the history
  • Loading branch information
jesusantguerrero committed Nov 18, 2023
1 parent 11d615d commit 3436206
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 41 deletions.
28 changes: 18 additions & 10 deletions app/Domains/Budget/Services/BudgetCategoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -282,15 +287,18 @@ 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,
'month' => $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;
Expand Down
30 changes: 8 additions & 22 deletions app/Domains/Budget/Services/BudgetRolloverService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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,
Expand All @@ -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
]);
}

Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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,
]);
}

Expand All @@ -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
}
4 changes: 2 additions & 2 deletions resources/js/Pages/Dashboard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
<div class="mt-6 md:w-9/12">
<section class="flex flex-col md:flex-row md:space-x-4">
<BudgetTracker
class="md:w-8/12 w-full order-1 mt-2 md:mt-0"
class="md:w-8/12 w-full order-1 mt-2 md:mt-0"
ref="budgetTrackerRef"
:budget="budgetTotal"
:expenses="transactionTotal.total_amount"
Expand All @@ -137,7 +137,7 @@
:data="expenses"
/>
</BudgetTracker>
<WeatherWidget class="md:w-4/12" />
<WeatherWidget class="md:w-4/12 md:order-1" />
</section>

<section class="flex space-x-4">
Expand Down
4 changes: 2 additions & 2 deletions resources/js/domains/budget/budgetTotals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ export const getCategoriesTotals = (categories: Record<string, any>, 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,12 @@
</template>
<section class="text-center">
<p>Available for funds: <MoneyPresenter :value="toAssign.availableForFunding"/> </p>
<p class="text-red-500">Funded: <MoneyPresenter :value="toAssign.creditCardFunded"/></p>
<p>funded spending: <MoneyPresenter :value="toAssign.funded_spending"/> </p>
<p class="text-green-500">Total: <MoneyPresenter :value="toAssign.availableForFunding + toAssign.creditCardFunded"/> </p>
<p>Budgeted: <MoneyPresenter :value="toAssign.budgeted"/> </p>
<p>Funded: <MoneyPresenter :value="toAssign.creditCardFunded"/> </p>
<p>payments: <MoneyPresenter :value="toAssign.payments"/> </p>
<p class="text-green-500">Total: <MoneyPresenter :value="(toAssign.availableForFunding + toAssign.creditCardFunded) - (toAssign.budgeted + toAssign.payments)"/> </p>
<p>Assigned in month: <MoneyPresenter :value="toAssign.assigned" /> </p>
<p>Balance: <MoneyPresenter :value="toAssign.balance" /> </p>
</section>
Expand Down
15 changes: 11 additions & 4 deletions resources/js/domains/budget/useBudget.ts
Original file line number Diff line number Diff line change
@@ -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: [],
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 3436206

Please sign in to comment.