Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release/beta 9 #292

Merged
merged 18 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ PUSHER_APP_CLUSTER=mt1
VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

# Google apis
GOOGLE_CREDENTIALS_PATH=
GOOGLE_CLIENT_ID=
VITE_GOOGLE_APP_KEY=
VITE_GOOGLE_CLIENT_ID=
VITE_WEATHER_ENDPOINT=https://fcc-weather-api.glitch.me/api/current?
40 changes: 40 additions & 0 deletions app/Console/Commands/MonthlyRollover.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Console\Commands;

use App\Domains\AppCore\Models\Category;
use App\Domains\Budget\Data\BudgetReservedNames;
use App\Domains\Budget\Services\BudgetRolloverService;
use Illuminate\Console\Command;

use Illuminate\Support\Facades\DB;

class MonthlyRollover extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:monthly-rollover {teamId} {month}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Execute the console command.
*/
public function handle(BudgetRolloverService $rolloverService)
{

$teamId = $this->argument('teamId');
$month = $this->argument('month');

$rolloverService->rollMonth($teamId, $month."-01");

}
}
56 changes: 56 additions & 0 deletions app/Console/Commands/TeamBudgetRollover.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace App\Console\Commands;

use App\Domains\AppCore\Models\Category;
use App\Domains\Budget\Data\BudgetReservedNames;
use App\Domains\Budget\Services\BudgetRolloverService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class TeamBudgetRollover extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:team-budget-rollover {teamId}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Execute the console command.
*/
public function handle(BudgetRolloverService $rolloverService)
{

$teamId = $this->argument('teamId');

$categories = Category::where([
'team_id' => $teamId,
])
->whereNot('name', BudgetReservedNames::READY_TO_ASSIGN->value)
->get();

$monthsWithTransactions = DB::table('transaction_lines')
->selectRaw("date_format(transaction_lines.date, '%Y-%m') AS date")
->groupBy(DB::raw("date_format(transaction_lines.date, '%Y-%m')"))
->get()
->pluck('date');

$total = count($monthsWithTransactions);
$count = 0;
foreach ($monthsWithTransactions as $month) {
$count++;
$rolloverService->rollMonth($teamId, $month."-01", $categories);
echo "updated month {$month}".PHP_EOL;
echo "{$count} of {$total}".PHP_EOL.PHP_EOL;
}
}
}
9 changes: 8 additions & 1 deletion app/Domains/AppCore/Policies/FinanceAccountPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ public function show(User $user, Account $account)
{
return $user->current_team_id === $account->team_id
? Response::allow()
: Response::deny('You do not own this post.');
: Response::deny('You do not own this account.');
}

public function update(User $user, Account $account)
{
return $user->current_team_id === $account->team_id
? Response::allow()
: Response::deny('You do not own this account.');
}

public function create(User $user)
Expand Down
2 changes: 2 additions & 0 deletions app/Domains/Budget/Models/BudgetMonth.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class BudgetMonth extends Model
'available',
'funded_spending',
'payments',
'left_from_last_month',
'funded_spending_previous_month',
];

public function category()
Expand Down
32 changes: 24 additions & 8 deletions app/Domains/Budget/Services/BudgetCategoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,28 +97,29 @@ public function getBudgetInfo($category, string $month)
$budgeted = $monthBudget ? $monthBudget->budgeted : 0;

if ($category->account_id) {
$prevMonthLeftOver = $this->getPrevMonthFundedLeftOver($category, $yearMonth);
$prevMonthPaymentsLeftOver = $this->getPrevMonthPaymentsLeftOver($category, $yearMonth);
$funded = $monthBudget ? $monthBudget->funded_spending : 0;
$monthPayment = $monthBudget ? $monthBudget->payments : 0;

+ $monthBudget->activity;

$monthBalance = Money::of($funded, $category->account->currency_code)->minus($monthPayment)->getAmount()->toFloat();
$available = Money::of($prevMonthPaymentsLeftOver, 'USD')
// ->plus($prevMonthLeftOver)
// ->minus($monthBalance)
$available = Money::of($monthBalance, 'USD')
->plus(($monthBudget->left_from_last_month * -1))
->getAmount()
->toFloat();
} else {
$monthBalance = (float) $category->getMonthBalance($yearMonth)->balance;
$prevMonthLeftOver = $this->getPrevMonthLeftOver($category, $yearMonth);
$available = Money::of($budgeted, 'USD')->plus($prevMonthLeftOver)->plus($monthBalance)->getAmount()->toFloat();
$available = Money::of($budgeted, 'USD')->plus($monthBudget?->left_from_last_month ?? 0)->plus($monthBalance)->getAmount()->toFloat();
}

$data = [
'budgeted' => $budgeted,
'activity' => $monthBalance,
'available' => $available,
'prevMonthLeftOver' => $prevMonthLeftOver,
'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' => $monthBudget?->funded_spending ?? 0,
'name' => $category->name,
'month' => $yearMonth,
];
Expand Down Expand Up @@ -235,6 +236,21 @@ public function updateActivity(Category $category, string $month)
echo "{$category->name} updated to {$activity}".PHP_EOL;
}

public function getCategoryActivity(Category $category, string $month)
{
$monthDate = Carbon::createFromFormat('Y-m-d', $month);
$transactions = 0;
$activity = 0;

if ($category->account) {
$transactions = $category->account->getMonthBalance($monthDate->format('Y-m'))->balance;
} else {
$activity = $category->getMonthBalance($monthDate->format('Y-m'))?->balance;
}

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

public function findByAccount(Account $account)
{
return Category::where([
Expand Down
144 changes: 144 additions & 0 deletions app/Domains/Budget/Services/BudgetRolloverService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

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;

class BudgetRolloverService {
public function __construct(private BudgetCategoryService $budgetCategoryService) {

}
public function rollMonth($teamId, $month, $categories = null) {
if (!$categories) {
$categories = Category::where([
'team_id' => $teamId,
])
->whereNot('name', BudgetReservedNames::READY_TO_ASSIGN->value)
->get();
}

foreach ($categories as $category) {
if($category->account_id) {
$this->budgetCategoryService->updateFundedSpending($category, $month);
}
$this->setNewMonthBudget($category, $month);
}
$this->moveReadyToAssign($teamId, $month);

}

private function setNewMonthBudget($category, $month) {
$activity = (new BudgetCategoryService($category))->getCategoryActivity($category, $month);
$budgetMonth = BudgetMonth::where([
'category_id' => $category->id,
'team_id' => $category->team_id,
'month' => $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.

// 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) {
$nextMonth = Carbon::createFromFormat("Y-m-d", $oldMonth)->addMonthsWithNoOverflow(1)->format('Y-m-d');
BudgetMonth::updateOrCreate([
'category_id' => $category->id,
'team_id' => $category->team_id,
'month' => $nextMonth,
'name' => $nextMonth,
], [
'user_id' => $category->user_id,
'left_from_last_month' => $available ?? 0,
'funded_spending_previous_month' => $leftFunded
]);
}

private function moveReadyToAssign($teamId, $month) {
$readyToAssignCategory = Category::where([
"name" => BudgetReservedNames::READY_TO_ASSIGN->value,
"team_id" => $teamId
])->first();

$results = DB::table('budget_months')
->where([
'team_id' => $teamId,
'month' => $month,
])->whereNot('category_id', $readyToAssignCategory->id)
->selectRaw("
coalesce(sum(budgeted), 0) as budgeted,
coalesce(sum(payments), 0) as payments,
coalesce(sum(funded_spending), 0) as funded_spending,
coalesce(sum(funded_spending_previous_month), 0) as funded_spending_previous_month
")
->first();

$budgetMonth = BudgetMonth::where([
'category_id' => $readyToAssignCategory->id,
'team_id' => $readyToAssignCategory->team_id,
'month' => $month,
'name' => $month,
])->first();

$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);

$nextMonth = Carbon::createFromFormat("Y-m-d", $month)->addMonthsWithNoOverflow(1)->format('Y-m-d');

// close current month
BudgetMonth::updateOrCreate([
'category_id' => $readyToAssignCategory->id,
'team_id' => $readyToAssignCategory->team_id,
'month' => $month,
'name' => $month,
], [
'user_id' => $readyToAssignCategory->user_id,
'budgeted' => $results?->budgeted,
'activity' => $activity,
'available' => $activityPlusLeft,
'funded_spending' => $results?->funded_spending ?? 0,
'payments' => $results?->payments ?? 0,
]);

// set left over to the next month
BudgetMonth::updateOrCreate([
'category_id' => $readyToAssignCategory->id,
'team_id' => $readyToAssignCategory->team_id,
'month' => $nextMonth,
'name' => $nextMonth,
], [
'user_id' => $readyToAssignCategory->user_id,
'left_from_last_month' => $available ?? 0,
'funded_spending_previous_month' => $leftFunded ?? 0,
]);
}

private function reduceOverspent() {
// If your category had been overspent in cash (negative red Available), that amount will be deducted from Ready to Assign in the new month.

// 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 setReadyToAssign() {

}

// transactions with more than 3 days prior to the las recinciled transaction are not imported
}
4 changes: 2 additions & 2 deletions app/Domains/Integration/Services/GoogleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ public static function requestAccessToken($data, $user)
]);
$client->addScope([
Gmail::GMAIL_READONLY,
Oauth2::USERINFO_PROFILE,
Oauth2::USERINFO_EMAIL,
"https://www.googleapis.com/auth/userinfo.profile",
"https://www.googleapis.com/auth/userinfo.email"
]);
$client->setRedirectUri(config('app.url').'/services/accept-oauth');
$client->setAccessType('offline');
Expand Down
3 changes: 1 addition & 2 deletions app/Domains/Journal/Actions/AccountUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ public function update(User $user, Account $account, array $accountData): Accoun
{
$this->validate($user, $account);
$account->update($accountData);

return $account;
}

public function validate(mixed $user, mixed $account)
public function validate(User $user, Account $account)
{
Gate::forUser($user)->authorize('update', $account);
}
Expand Down
2 changes: 1 addition & 1 deletion app/Domains/Journal/Policies/AccountPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function create(User $user)

public function update(User $user, Account $account)
{
return $user->team_id == $account->team_id;
return $user->current_team_id == $account->team_id;
}

public function delete(User $user, Account $account)
Expand Down
3 changes: 2 additions & 1 deletion app/Domains/Transaction/Traits/TransactionLineTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public function scopeExpenseCategories($query, array $categories = null)

public function scopePayees($query, array $payees)
{
return $query->whereIn('payee_id', $payees);
return $query->whereIn('transaction_lines.payee_id', $payees)
->join('categories', 'transaction_lines.category_id', '=', 'categories.id');
}
}
Loading