-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd1141a
commit 9a6144d
Showing
18 changed files
with
342 additions
and
10 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
app/Domains/Budget/Http/Controllers/BudgetFundController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace App\Domains\Budget\Http\Controllers; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Domains\AppCore\Models\Category; | ||
use App\Domains\Budget\Models\BudgetTarget; | ||
use App\Domains\Budget\Services\BudgetFundService; | ||
use App\Domains\Budget\Services\BudgetTargetService; | ||
|
||
class BudgetFundController extends Controller | ||
{ | ||
|
||
public function index(BudgetFundService $budgetFundService) { | ||
if (request()->get('json')) { | ||
return $budgetFundService->list(request()->user()->current_team_id); | ||
} | ||
return inertia("Finance/EmergencyFund/Index", | ||
[]); | ||
} | ||
|
||
public function show() { | ||
return inertia("Finance/EmergencyFund/Index", | ||
[]); | ||
} | ||
|
||
public function store(BudgetFundService $budgetFundService) | ||
{ | ||
$postData = request()->post(); | ||
$budgetFundService->add(request()->user(), $postData); | ||
return redirect()->back(); | ||
} | ||
|
||
public function update(Category $category, BudgetTarget $budgetTarget, BudgetTargetService $budgetTargetService) | ||
{ | ||
$postData = request()->post(); | ||
$budgetTargetService->update($category, $budgetTarget, request()->user(), $postData); | ||
return redirect()->back(); | ||
} | ||
|
||
public function complete(Category $category, BudgetTarget $budgetTarget, BudgetTargetService $budgetTargetService) | ||
{ | ||
$postData = request()->post(); | ||
$budgetTargetService->complete($budgetTarget, $category, $postData); | ||
return redirect()->back(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace App\Domains\Budget\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Modules\Watchlist\Models\Watchlist; | ||
use App\Domains\AppCore\Models\Category; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
|
||
class BudgetFund extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $fillable = [ | ||
'team_id', | ||
'user_id', | ||
'amount', | ||
'name', | ||
'category_id', | ||
'watchlist_id', | ||
]; | ||
|
||
public function category() | ||
{ | ||
return $this->belongsTo(Category::class); | ||
} | ||
|
||
public function watchlist() | ||
{ | ||
return $this->belongsTo(Watchlist::class); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace App\Domains\Budget\Services; | ||
|
||
use Exception; | ||
use App\Models\User; | ||
use App\Domains\AppCore\Models\Category; | ||
use App\Domains\Budget\Models\BudgetFund; | ||
use App\Domains\Budget\Models\BudgetTarget; | ||
|
||
class BudgetFundService | ||
{ | ||
public function update(Category $category, BudgetTarget $budgetTarget, User $user, $postData) { | ||
if ($category->id !== $budgetTarget->category_id){ | ||
throw new Exception(__("This target doent belongs to this category")); | ||
} | ||
|
||
$budgetTarget->update([ | ||
...$postData, | ||
'team_id' => $user->current_team_id, | ||
'user_id' => $user->id, | ||
'name' => $category->name, | ||
'category_id' => $budgetTarget->category_id, | ||
]); | ||
} | ||
|
||
public function add(User $user, mixed $postData) | ||
{ | ||
return BudgetFund::create([ | ||
...$postData, | ||
'name' => "Emergency fund", | ||
'team_id' => $user->current_team_id, | ||
"user_id" => $user->id | ||
]); | ||
} | ||
|
||
public function list($teamId) { | ||
$items = BudgetFund::where([ | ||
"team_id" => $teamId | ||
])->get(); | ||
|
||
return $items->map(fn( $item) => [ | ||
...$item->toArray(), | ||
...$this->getData($item) | ||
]); | ||
} | ||
|
||
public function getData(BudgetFund $budgetFund) { | ||
$available = $budgetFund->category->budgets[1]?->available ?? 0; | ||
$expenses = $budgetFund->watchlist->fullData(); | ||
$expenseTotal = $expenses["month"]?->total ?? 0; | ||
|
||
return [ | ||
'balance' => $available, | ||
'monthlyExpense' => $expenseTotal, | ||
'total' => $available / $expenseTotal, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
database/migrations/2024_03_28_164253_create_budget_funds_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('budget_funds', function (Blueprint $table) { | ||
$table->id(); | ||
$table->foreignId('team_id'); | ||
$table->foreignId('user_id'); | ||
$table->foreignId('watchlist_team_id'); | ||
$table->foreignId('watchlist_id'); | ||
$table->foreignId('category_team_id'); | ||
$table->foreignId('category_id'); | ||
$table->string('name'); | ||
$table->text('description')->nullable(); | ||
$table->json('meta_data')->nullable(); | ||
$table->integer('index')->default(0); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('budget_funds'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
40 changes: 40 additions & 0 deletions
40
resources/js/Pages/Dashboard/Partials/BudgetFundWidget.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<script setup lang="ts"> | ||
import { onMounted, ref } from 'vue'; | ||
import WidgetContainer from '@/Components/WidgetContainer.vue'; | ||
import axios from 'axios'; | ||
import { formatMoney } from '@/utils'; | ||
const budgetData = ref({}) | ||
const fetchChecks = () => { | ||
return axios.get(`/budget-funds?json=true`).then(({ data }) => { | ||
budgetData.value = data?.at(0); | ||
}); | ||
} | ||
onMounted(() => { | ||
fetchChecks() | ||
}) | ||
</script> | ||
|
||
<template> | ||
<WidgetContainer | ||
:message="$t('Emergency Fund Builder')" | ||
> | ||
<template #content> | ||
<section> | ||
<p> | ||
{{ formatMoney(budgetData.balance) }} | ||
</p> | ||
<p> | ||
{{ formatMoney(budgetData.monthlyExpense) }} | ||
</p> | ||
<p> | ||
{{ budgetData.total?.toFixed?.(2) }} Months | ||
</p> | ||
</section> | ||
</template> | ||
</WidgetContainer> | ||
</template> | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<script setup> | ||
import AppLayout from '@/Components/templates/AppLayout.vue' | ||
import { usePage } from '@inertiajs/vue3' | ||
import BudgetFundForm from './Partials/BudgetFundForm.vue' | ||
import SettingsSectionNav from '@/Components/templates/SettingsSectionNav.vue' | ||
defineProps(['sessions']) | ||
const pageProps = usePage().props | ||
</script> | ||
|
||
|
||
<template> | ||
<AppLayout title="Emergency Fund Builder"> | ||
<template #header> | ||
<SettingsSectionNav /> | ||
</template> | ||
|
||
<section class="max-w-7xl mx-auto space-y-5 [&>*]:pt-5 divide-y pt-16 pb-20 sm:px-6 lg:px-8"> | ||
<BudgetFundForm :user="pageProps.user" /> | ||
</section> | ||
</AppLayout> | ||
</template> | ||
|
Oops, something went wrong.