-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from hisabi-app/feat/budget-system
[beta] feature: add budget backend logic
- Loading branch information
Showing
8 changed files
with
269 additions
and
3 deletions.
There are no files selected for viewing
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,86 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Carbon\CarbonPeriod; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Relations\BelongsToMany; | ||
|
||
class Budget extends Model | ||
{ | ||
use HasFactory; | ||
|
||
const CUSTOM = "CUSTOM"; | ||
const DAILY = "DAILY"; | ||
const WEEKLY = "WEEKLY"; | ||
const MONTHLY = "MONTHLY"; | ||
const YEARLY = "YEARLY"; | ||
|
||
protected $guarded = []; | ||
|
||
protected $casts = [ | ||
'start_at' => 'datetime', | ||
'end_at' => 'datetime', | ||
]; | ||
|
||
/** | ||
* @return BelongsToMany | ||
*/ | ||
public function categories(): BelongsToMany | ||
{ | ||
return $this->belongsToMany(Category::class); | ||
} | ||
|
||
public function getStartAtDateAttribute() | ||
{ | ||
return $this->getCurrentWindowStartAndEndDates()[0]->format('Y-m-d'); | ||
} | ||
|
||
public function getEndAtDateAttribute() | ||
{ | ||
return $this->getCurrentWindowStartAndEndDates()[1]->format('Y-m-d'); | ||
} | ||
|
||
public function getTotalAccumulatedTransactionsAmountAttribute() | ||
{ | ||
$categories = $this->categories()->with('transactions')->get(); | ||
[$startAt, $endAt] = $this->getCurrentWindowStartAndEndDates(); | ||
|
||
return $categories->sum(function ($category) use ($startAt, $endAt){ | ||
return $category->transactions() | ||
->whereBetween('transactions.created_at', [$startAt, $endAt]) | ||
->sum('amount'); | ||
}); | ||
} | ||
|
||
private function getCurrentWindowStartAndEndDates() | ||
{ | ||
if($this->reoccurrence === self::CUSTOM) { | ||
return [$this->start_at, $this->end_at]; | ||
} | ||
|
||
$unit = $this->getUnitMapping(); | ||
$intervalString = $this->period . ' ' . $unit; | ||
$ranges = CarbonPeriod::create($this->start_at->startOfDay(), $intervalString, now()->copy()->add($unit, $this->period))->toArray(); | ||
|
||
foreach (array_reverse($ranges) as $range) { | ||
if(now()->isAfter($range)) { | ||
return [$range->copy(), $range->copy()->add($unit, $this->period)]; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
private function getUnitMapping(): string | ||
{ | ||
return [ | ||
self::DAILY => 'day', | ||
self::WEEKLY => 'week', | ||
self::MONTHLY => 'month', | ||
self::YEARLY => 'year', | ||
][$this->reoccurrence]; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Database\Factories; | ||
|
||
use App\Models\Budget; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
/** | ||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Budget> | ||
*/ | ||
class BudgetFactory extends Factory | ||
{ | ||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function definition(): array | ||
{ | ||
return [ | ||
'name' => $this->faker->name(), | ||
'amount' => $this->faker->numberBetween(), | ||
'start_at' => now(), | ||
'reoccurrence' => Budget::DAILY, | ||
'period' => 1, | ||
]; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
database/migrations/2024_04_21_173031_create_budgets_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,34 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('budgets', function (Blueprint $table) { | ||
$table->id(); | ||
$table->string('name'); | ||
$table->decimal('amount'); | ||
$table->dateTime('start_at'); | ||
$table->dateTime('end_at')->nullable(); | ||
$table->boolean('saving')->default(false); | ||
$table->unsignedInteger('period')->default(1); | ||
$table->string('reoccurrence'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('budgets'); | ||
} | ||
}; |
29 changes: 29 additions & 0 deletions
29
database/migrations/2024_04_22_011017_create_budget_category_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,29 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('budget_category', function (Blueprint $table) { | ||
$table->id(); | ||
$table->foreignId('budget_id')->constrained()->onDelete('cascade'); | ||
$table->foreignId('category_id')->constrained()->onDelete('cascade'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('budget_category'); | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\Models\Budgets; | ||
|
||
use Tests\TestCase; | ||
use App\Models\Brand; | ||
use App\Models\Budget; | ||
use App\Models\Category; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
|
||
class BudgetTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
/** @test */ | ||
public function it_has_name() | ||
{ | ||
$sut = Budget::factory()->create(['name' => 'test']); | ||
|
||
$this->assertEquals("test", $sut->name); | ||
} | ||
|
||
/** @test */ | ||
public function it_belongs_to_categories() | ||
{ | ||
$categories = Category::factory()->createMany(3); | ||
$sut = Budget::factory()->create(); | ||
$sut->categories()->attach($categories); | ||
|
||
$this->assertCount(3, $sut->categories); | ||
} | ||
|
||
/** @test */ | ||
public function it_has_total_categories_transactions_amount() | ||
{ | ||
$category = Category::factory()->create(); | ||
$brand = Brand::factory()->create(['category_id' => $category->id]); | ||
$sut = Budget::factory()->create(['start_at' => now()->subDays(1), 'end_at' => now()->addDays(1)]); | ||
$sut->categories()->attach($category); | ||
|
||
$category->transactions()->create(['amount' => 100, 'brand_id' => $brand->id]); | ||
$category->transactions()->create(['amount' => 200, 'brand_id' => $brand->id]); | ||
$category->transactions()->create(['amount' => 200, 'brand_id' => $brand->id, 'created_at' => now()->addDays(2)]); | ||
|
||
$this->assertEquals(300, $sut->totalAccumulatedTransactionsAmount); | ||
} | ||
|
||
/** @test */ | ||
public function it_has_start_and_end_at() | ||
{ | ||
$sut = Budget::factory()->create(['start_at' => now()->subDays(1), 'end_at' => now()->addDays(1)]); | ||
|
||
$this->assertEquals(now()->subDays(1)->format('Y-m-d'), $sut->start_at->format('Y-m-d')); | ||
$this->assertEquals(now()->addDays(1)->format('Y-m-d'), $sut->end_at->format('Y-m-d')); | ||
} | ||
|
||
/** @test */ | ||
public function it_has_start_and_end_dates_window() | ||
{ | ||
$sut = Budget::factory()->create(['start_at' => now()->subDays(1), 'period' => 1, 'reoccurrence' => Budget::DAILY]); | ||
|
||
$this->assertEquals(now()->format('Y-m-d'), $sut->startAtDate); | ||
$this->assertEquals(now()->addDays(1)->format('Y-m-d'), $sut->endAtDate); | ||
} | ||
} |