Kategori adalah kosakata aplikasi keuangan: sebelum pengguna bisa mencatat “makan siang”, harus ada kategori Makanan yang bisa dipilih. Bagian ini membangun pengelolaan kategori penuh — tambah, ubah, hapus — dengan dua aturan khas: setiap kategori membawa kode warna hex yang nanti mewarnai laporan, dan kategori yang sudah dipakai transaksi dilindungi dari perubahan yang merusak konsistensi. Di sinilah keputusan Bagian 1 menagih janjinya: karena setiap transaksi menyimpan salinan jenisnya, kategori tidak boleh berubah jenis setelah dipakai.
Model dengan Relasi dan Scope #
Model Category ringkas namun membawa dua konstanta yang dipakai di seluruh aplikasi, relasi ke transaksinya, dan scope penyaring jenis:
class Category extends Model
{
public const TYPE_INCOME = 'income';
public const TYPE_EXPENSE = 'expense';
protected $fillable = [
'name',
'type',
'color',
];
public function transactions(): HasMany
{
return $this->hasMany(Transaction::class);
}
public function scopeOfType(Builder $query, string $type): Builder
{
return $query->where('type', $type);
}
}Scope ofType dipakai form transaksi Bagian 2 untuk mengisi dua optgroup (pemasukan dan pengeluaran), sedangkan relasi transactions() menjadi alat hitung seberapa banyak sebuah kategori dipakai. Halaman daftar memanfaatkannya lewat withCount('transactions') sehingga satu query menampilkan setiap kategori beserta jumlah transaksinya tanpa masalah N+1. Konstanta TYPE_INCOME dan TYPE_EXPENSE menjadi jangkar dua nilai sah tersebut: controller pencatatan memanggil ofType(Category::TYPE_INCOME) dan laporan memanggil byCategory(..., Transaction::TYPE_EXPENSE), sehingga string income/expense tidak ditulis mentah di banyak tempat — bila daftar nilai itu kelak bertambah, pengubahannya cukup di satu titik.
Satu keputusan kecil yang layak diperhatikan: warna disimpan pada kategori, bukan pada transaksi. Konsekuensinya, mengubah warna Makanan dari merah ke oranye cukup dilakukan sekali dan seluruh label serta batang grafik ikut berubah, karena halaman transaksi maupun laporan membaca warna melalui relasi kategori. Bayangkan bila warna disimpan per transaksi: sepuluh ribu baris lama akan tetap merah selamanya meski kategorinya sudah diwarnai ulang.
Validasi Nama, Jenis, dan Warna #
Satu CategoryRequest melayani store dan update sekaligus:
public function rules(): array
{
$unique = Rule::unique('categories', 'name');
if ($category = $this->route('category')) {
$unique->ignore($category);
}
return [
'name' => ['required', 'string', 'max:60', $unique],
'type' => ['required', 'in:income,expense'],
'color' => ['required', 'regex:/^#[0-9a-fA-F]{6}$/'],
];
}Aturan unique memastikan tidak pernah ada dua kategori bernama sama. Saat update, $this->route('category') mengembalikan model dari binding route sehingga ignore($category) mengecualikan baris sendiri — mengubah kategori Makanan tetap boleh memakai nama “Makanan” tanpa dianggap duplikat. Warna divalidasi regex ^#[0-9a-fA-F]{6}$ sehingga hanya kode hex enam digit seperti #ef4444 yang lolos; input merah atau #12345 ditolak dengan pesan kustom pada messages().
Di sisi tampilan, kolom warna memakai <input type="color"> bawaan HTML sehingga browser menyuguhkan pemilih warna visual yang selalu menghasilkan format hex — regex tetap berjaga di belakangnya karena request bisa dikirim tanpa browser. Halaman daftar kemudian memakai warna itu sebagai titik kecil (color-dot) di samping nama kategori, berdampingan dengan badge jenis pemasukan/pengeluaran dan hitungan jumlah transaksi hasil withCount.
Melindungi Kategori yang Dipakai #
Dua penjaga bekerja pada CategoryController. Yang pertama menolak perubahan jenis:
if ($category->transactions()->exists()
&& $request->validated('type') !== $category->type) {
return back()
->withErrors(['type' => 'Jenis kategori tidak dapat diubah karena sudah dipakai transaksi.'])
->withInput();
}Yang kedua menolak penghapusan kategori yang masih menampung transaksi, mengembalikan pengguna ke daftar dengan pesan error. Penjaga ketiga tidak terlihat di controller karena ia bekerja di database: restrictOnDelete() pada foreign key category_id memastikan tidak ada jalur apa pun — termasuk query manual — yang bisa menghapus kategori bertransaksi. Lapisan berlapis ini menjawab pertanyaan “bagaimana jika data sudah dipakai?” secara tuntas.
Hasil Eksekusi Aturan #
Keenam aturan kategori diuji oleh CategoryTest dengan data yang dibuat eksplisit per test:
php artisan test --filter=CategoryTestOutput yang Dihasilkan:
PASS Tests\Feature\CategoryTest
✓ bisa menambah kategori 0.17s
✓ warna harus kode hex enam digit 0.01s
✓ nama kategori harus unik 0.01s
✓ bisa mengubah kategori dengan nama sendiri 0.01s
✓ jenis tidak bisa berubah setelah dipakai 0.01s
✓ kategori dipakai tidak bisa dihapus 0.01s
Tests: 6 passed (27 assertions)
Duration: 0.30sDengan kategori yang tertata, aplikasi siap menjawab pertanyaan yang sesungguhnya: berapa total yang masuk, keluar, dan tersisa bulan ini? Bagian berikutnya membangun mesin hitungnya.
Berkas Sumber
Berkas pendukung halaman ini dapat diunduh dan dijalankan langsung sesuai penjelasan di atas.
app/Http/Controllers/AuthController.php
Unduh berkas<?php
namespace App\Http\Controllers;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AuthController extends Controller
{
public function showLogin(): View
{
return view('auth.login');
}
public function login(Request $request): RedirectResponse
{
$credentials = $request->validate([
'email' => ['required', 'email'],
'password' => ['required'],
]);
if (Auth::attempt($credentials)) {
$request->session()->regenerate();
return redirect()->intended(route('transactions.index'));
}
return back()
->withErrors(['email' => 'Email atau password salah.'])
->onlyInput('email');
}
public function logout(Request $request): RedirectResponse
{
Auth::logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect()->route('login');
}
}
app/Http/Controllers/CategoryController.php
Unduh berkas<?php
namespace App\Http\Controllers;
use App\Http\Requests\CategoryRequest;
use App\Models\Category;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
class CategoryController extends Controller
{
public function index(): View
{
$categories = Category::query()
->withCount('transactions')
->orderBy('type')
->orderBy('name')
->get();
return view('categories.index', [
'categories' => $categories,
]);
}
public function create(): View
{
return view('categories.create', [
'category' => new Category([
'type' => old('type', 'expense'),
'color' => old('color', '#64748b'),
]),
]);
}
public function store(CategoryRequest $request): RedirectResponse
{
Category::create($request->validated());
return redirect()
->route('categories.index')
->with('status', 'Kategori berhasil ditambahkan.');
}
public function edit(Category $category): View
{
return view('categories.edit', [
'category' => $category,
]);
}
public function update(CategoryRequest $request, Category $category): RedirectResponse
{
// Jenis kategori tidak boleh berubah setelah dipakai transaksi,
// karena setiap transaksi menyimpan salinan jenisnya sendiri.
if ($category->transactions()->exists()
&& $request->validated('type') !== $category->type) {
return back()
->withErrors(['type' => 'Jenis kategori tidak dapat diubah karena sudah dipakai transaksi.'])
->withInput();
}
$category->update($request->validated());
return redirect()
->route('categories.index')
->with('status', 'Kategori berhasil diubah.');
}
public function destroy(Category $category): RedirectResponse
{
if ($category->transactions()->exists()) {
return redirect()
->route('categories.index')
->withErrors(['name' => 'Kategori masih dipakai transaksi dan tidak dapat dihapus.']);
}
$category->delete();
return redirect()
->route('categories.index')
->with('status', 'Kategori berhasil dihapus.');
}
}
app/Http/Controllers/Controller.php
Unduh berkas<?php
namespace App\Http\Controllers;
abstract class Controller
{
//
}
app/Http/Controllers/ReportController.php
Unduh berkas<?php
namespace App\Http\Controllers;
use App\Models\Transaction;
use App\Services\ReportService;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
class ReportController extends Controller
{
public function __construct(private ReportService $reports)
{
}
public function show(Request $request, string $month): View
{
if (! preg_match('/^\d{4}-(0[1-9]|1[0-2])$/', $month)) {
abort(404);
}
Carbon::setLocale('id');
$user = $request->user();
return view('reports.show', [
'month' => $month,
'title' => Carbon::parse($month . '-01')->translatedFormat('F Y'),
'summary' => $this->reports->monthlySummary($user, $month),
'expensesByCategory' => $this->reports->byCategory($user, $month, Transaction::TYPE_EXPENSE),
'trend' => $this->reports->monthlyTrend($user, 6, $month),
'previous' => Carbon::parse($month . '-01')->subMonth()->format('Y-m'),
'next' => Carbon::parse($month . '-01')->addMonth()->format('Y-m'),
]);
}
/**
* Laporan bulan berjalan: cukup teruskan ke /reports/{bulan-ini}.
*/
public function current(): RedirectResponse
{
return redirect()->route('reports.show', now()->format('Y-m'));
}
}
app/Http/Controllers/TransactionController.php
Unduh berkas<?php
namespace App\Http\Controllers;
use App\Http\Requests\TransactionRequest;
use App\Models\Category;
use App\Models\Transaction;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
class TransactionController extends Controller
{
public function index(Request $request): View
{
// String kosong (filter dikosongkan) diperlakukan tanpa filter.
$month = $request->input('month') ?: null;
if ($month !== null && ! preg_match('/^\d{4}-(0[1-9]|1[0-2])$/', $month)) {
abort(404);
}
$transactions = Transaction::query()
->with('category')
->where('user_id', $request->user()->id)
->when($month, fn ($query) => $query->inMonth($month))
->orderByDesc('transaction_date')
->orderByDesc('id')
->paginate(10)
->withQueryString();
return view('transactions.index', [
'transactions' => $transactions,
'month' => $month,
]);
}
public function create(Request $request): View
{
return view('transactions.create', [
'transaction' => new Transaction(),
'categories' => $this->categoriesForForm(),
'type' => old('type', 'expense'),
]);
}
public function store(TransactionRequest $request): RedirectResponse
{
$request->user()->transactions()->create($request->validated());
return redirect()
->route('transactions.index')
->with('status', 'Transaksi berhasil dicatat.');
}
public function edit(Request $request, Transaction $transaction): View
{
$this->authorizeOwner($request, $transaction);
return view('transactions.edit', [
'transaction' => $transaction,
'categories' => $this->categoriesForForm(),
'type' => old('type', $transaction->type),
]);
}
public function update(TransactionRequest $request, Transaction $transaction): RedirectResponse
{
$this->authorizeOwner($request, $transaction);
$transaction->update($request->validated());
return redirect()
->route('transactions.index')
->with('status', 'Transaksi berhasil diubah.');
}
public function destroy(Request $request, Transaction $transaction): RedirectResponse
{
$this->authorizeOwner($request, $transaction);
$transaction->delete();
return redirect()
->route('transactions.index')
->with('status', 'Transaksi berhasil dihapus.');
}
/**
* Kategori dikelompokkan per jenis untuk select ber-optgroup.
*
* @return array<string, \Illuminate\Database\Eloquent\Collection<int, Category>>
*/
private function categoriesForForm(): array
{
return [
'income' => Category::query()->ofType(Category::TYPE_INCOME)->orderBy('name')->get(),
'expense' => Category::query()->ofType(Category::TYPE_EXPENSE)->orderBy('name')->get(),
];
}
private function authorizeOwner(Request $request, Transaction $transaction): void
{
abort_if($transaction->user_id !== $request->user()->id, 403);
}
}
app/Http/Requests/CategoryRequest.php
Unduh berkas<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class CategoryRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
/**
* Dipakai untuk store dan update; saat update, nama kategori
* sendiri dikecualikan dari aturan unique.
*
* @return array<string, array<int, string|Rule>>
*/
public function rules(): array
{
$unique = Rule::unique('categories', 'name');
if ($category = $this->route('category')) {
$unique->ignore($category);
}
return [
'name' => ['required', 'string', 'max:60', $unique],
'type' => ['required', 'in:income,expense'],
'color' => ['required', 'regex:/^#[0-9a-fA-F]{6}$/'],
];
}
/**
* @return array<string, string>
*/
public function attributes(): array
{
return [
'name' => 'nama kategori',
'type' => 'jenis kategori',
'color' => 'warna',
];
}
/**
* @return array<string, string>
*/
public function messages(): array
{
return [
'color.regex' => 'Warna harus berupa kode hex 6 digit, contoh: #ef4444.',
];
}
}
app/Http/Requests/TransactionRequest.php
Unduh berkas<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class TransactionRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
/**
* Aturan store dan update identik, jadi satu class cukup.
*
* @return array<string, array<int, string|Rule>>
*/
public function rules(): array
{
return [
'category_id' => [
'required',
'integer',
// Kategori harus tersedia DAN jenisnya cocok dengan
// jenis transaksi yang dipilih (income/expense).
Rule::exists('categories', 'id')->where('type', $this->input('type')),
],
'type' => ['required', 'in:income,expense'],
'amount' => ['required', 'integer', 'min:1', 'max:999999999999'],
'transaction_date' => [
'required',
'date',
'after:2000-01-01',
'before_or_equal:today',
],
'note' => ['nullable', 'string', 'max:255'],
];
}
/**
* @return array<string, string>
*/
public function attributes(): array
{
return [
'category_id' => 'kategori',
'type' => 'jenis transaksi',
'amount' => 'nominal',
'transaction_date' => 'tanggal transaksi',
'note' => 'catatan',
];
}
/**
* @return array<string, string>
*/
public function messages(): array
{
return [
'category_id.exists' => 'Kategori tidak ditemukan atau tidak cocok dengan jenis transaksi.',
];
}
}
app/Models/Category.php
Unduh berkas<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Category extends Model
{
public const TYPE_INCOME = 'income';
public const TYPE_EXPENSE = 'expense';
protected $fillable = [
'name',
'type',
'color',
];
protected function casts(): array
{
return [
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
}
public function transactions(): HasMany
{
return $this->hasMany(Transaction::class);
}
/**
* Ambil kategori untuk satu jenis transaksi saja (income/expense).
*/
public function scopeOfType(Builder $query, string $type): Builder
{
return $query->where('type', $type);
}
}
app/Models/Transaction.php
Unduh berkas<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Carbon;
class Transaction extends Model
{
public const TYPE_INCOME = 'income';
public const TYPE_EXPENSE = 'expense';
protected $fillable = [
'category_id',
'type',
'amount',
'transaction_date',
'note',
];
protected function casts(): array
{
return [
'category_id' => 'integer',
'user_id' => 'integer',
'amount' => 'integer',
'transaction_date' => 'date:Y-m-d',
];
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function category(): BelongsTo
{
return $this->belongsTo(Category::class);
}
/**
* Batasi query pada satu bulan (format Y-m, contoh: 2026-10).
*/
public function scopeInMonth(Builder $query, string $month): Builder
{
$start = Carbon::parse($month . '-01');
return $query->whereBetween('transaction_date', [
$start->toDateString(),
$start->copy()->endOfMonth()->toDateString(),
]);
}
}
app/Models/User.php
Unduh berkas<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Hidden;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
#[Fillable(['name', 'email', 'password'])]
#[Hidden(['password', 'remember_token'])]
class User extends Authenticatable
{
/** @use HasFactory<UserFactory> */
use HasFactory, Notifiable;
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
public function transactions(): HasMany
{
return $this->hasMany(Transaction::class);
}
}
app/Services/ReportService.php
Unduh berkas<?php
namespace App\Services;
use App\Models\User;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
class ReportService
{
/**
* Ringkasan satu bulan: total pemasukan, pengeluaran, dan selisihnya.
*
* @return array{income: int, expense: int, balance: int}
*/
public function monthlySummary(User $user, string $month): array
{
$start = Carbon::parse($month . '-01');
$row = DB::table('transactions')
->where('user_id', $user->id)
->whereBetween('transaction_date', [
$start->toDateString(),
$start->copy()->endOfMonth()->toDateString(),
])
->selectRaw("COALESCE(SUM(CASE WHEN type = 'income' THEN amount ELSE 0 END), 0) AS income")
->selectRaw("COALESCE(SUM(CASE WHEN type = 'expense' THEN amount ELSE 0 END), 0) AS expense")
->first();
$income = (int) $row->income;
$expense = (int) $row->expense;
return [
'income' => $income,
'expense' => $expense,
'balance' => $income - $expense,
];
}
/**
* Total per kategori untuk satu bulan dan satu jenis transaksi,
* diurutkan dari total terbesar.
*
* @return \Illuminate\Support\Collection<int, array{id: int, name: string, color: string, total: int}>
*/
public function byCategory(User $user, string $month, string $type): Collection
{
$start = Carbon::parse($month . '-01');
return DB::table('transactions')
->join('categories', 'categories.id', '=', 'transactions.category_id')
->where('transactions.user_id', $user->id)
->where('transactions.type', $type)
->whereBetween('transaction_date', [
$start->toDateString(),
$start->copy()->endOfMonth()->toDateString(),
])
->groupBy('categories.id', 'categories.name', 'categories.color')
->selectRaw('categories.id AS id')
->selectRaw('categories.name AS name')
->selectRaw('categories.color AS color')
->selectRaw('SUM(transactions.amount) AS total')
->orderByDesc('total')
->get()
->map(fn (object $row) => [
'id' => (int) $row->id,
'name' => $row->name,
'color' => $row->color,
'total' => (int) $row->total,
]);
}
/**
* Tren arus kas N bulan terakhir sampai (dan termasuk) $untilMonth.
*
* @return \Illuminate\Support\Collection<int, array{month: string, label: string, income: int, expense: int}>
*/
public function monthlyTrend(User $user, int $months = 6, ?string $untilMonth = null): Collection
{
$until = Carbon::parse(($untilMonth ?? now()->format('Y-m')) . '-01')->endOfMonth();
$from = $until->copy()->startOfMonth()->subMonths($months - 1);
$totals = DB::table('transactions')
->where('user_id', $user->id)
->whereBetween('transaction_date', [$from->toDateString(), $until->toDateString()])
->groupBy(DB::raw('substr(transaction_date, 1, 7)'))
->selectRaw("substr(transaction_date, 1, 7) AS month")
->selectRaw("COALESCE(SUM(CASE WHEN type = 'income' THEN amount ELSE 0 END), 0) AS income")
->selectRaw("COALESCE(SUM(CASE WHEN type = 'expense' THEN amount ELSE 0 END), 0) AS expense")
->get()
->keyBy('month');
return Collection::times($months, function (int $i) use ($from, $totals) {
$cursor = $from->copy()->addMonths($i - 1);
$key = $cursor->format('Y-m');
$row = $totals->get($key);
return [
'month' => $key,
'label' => $cursor->translatedFormat('M Y'),
'income' => (int) ($row->income ?? 0),
'expense' => (int) ($row->expense ?? 0),
];
});
}
}
app/Support/Rupiah.php
Unduh berkas<?php
namespace App\Support;
/**
* Format angka rupiah: selalu bulat (tanpa sen), pemisah ribuan titik.
*/
final class Rupiah
{
public static function format(int $amount): string
{
return 'Rp ' . number_format($amount, 0, ',', '.');
}
}
database/migrations/2026_11_01_000010_create_categories_table.php
Unduh berkas<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name', 60)->unique();
$table->enum('type', ['income', 'expense']);
$table->char('color', 7)->default('#64748b');
$table->timestamps();
$table->index(['type', 'name']);
});
}
public function down(): void
{
Schema::dropIfExists('categories');
}
};
database/migrations/2026_11_01_000020_create_transactions_table.php
Unduh berkas<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('transactions', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')
->constrained()
->cascadeOnDelete();
$table->foreignId('category_id')
->constrained()
->restrictOnDelete();
$table->decimal('amount', 14, 0);
$table->enum('type', ['income', 'expense']);
$table->date('transaction_date');
$table->string('note')->nullable();
$table->timestamps();
$table->index(['user_id', 'transaction_date']);
$table->index(['category_id', 'transaction_date']);
});
}
public function down(): void
{
Schema::dropIfExists('transactions');
}
};
database/seeders/DatabaseSeeder.php
Unduh berkas<?php
namespace Database\Seeders;
use App\Models\Category;
use App\Models\Transaction;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
class DatabaseSeeder extends Seeder
{
public function run(): void
{
$budi = User::create([
'name' => 'Budi Santoso',
'email' => '[email protected]',
'password' => Hash::make('password'),
]);
$categories = [
'Gaji' => ['type' => 'income', 'color' => '#16a34a'],
'Bonus' => ['type' => 'income', 'color' => '#22c55e'],
'Makanan' => ['type' => 'expense', 'color' => '#ef4444'],
'Transportasi' => ['type' => 'expense', 'color' => '#f97316'],
'Hiburan' => ['type' => 'expense', 'color' => '#a855f7'],
];
foreach ($categories as $name => $attributes) {
Category::create(['name' => $name] + $attributes);
}
$rows = [
// [kategori, jenis, nominal, tanggal, catatan]
['Gaji', 'income', 5000000, '2026-10-01', 'Gaji bulan Oktober'],
['Bonus', 'income', 500000, '2026-10-10', 'Bonus proyek aplikasi'],
['Makanan', 'expense', 25000, '2026-10-04', 'Makan siang di kantin'],
['Makanan', 'expense', 45000, '2026-10-07', 'Kopi dan camilan'],
['Makanan', 'expense', 60000, '2026-10-12', 'Belanja bahan makanan'],
['Transportasi', 'expense', 15000, '2026-10-03', 'Ojek ke kantor'],
['Transportasi', 'expense', 20000, '2026-10-09', 'Isi bensin motor'],
['Hiburan', 'expense', 80000, '2026-10-11', 'Tiket bioskop akhir pekan'],
['Gaji', 'income', 4800000, '2026-09-01', 'Gaji bulan September'],
['Makanan', 'expense', 40000, '2026-09-16', 'Makan malam bersama teman'],
['Makanan', 'expense', 30000, '2026-08-02', 'Camilan akhir bulan'],
];
foreach ($rows as [$categoryName, $type, $amount, $date, $note]) {
$budi->transactions()->create([
'category_id' => Category::where('name', $categoryName)->value('id'),
'type' => $type,
'amount' => $amount,
'transaction_date' => $date,
'note' => $note,
]);
}
$this->command?->info('Transaksi terisi: ' . Transaction::count());
}
}
routes/web.php
Unduh berkas<?php
use App\Http\Controllers\AuthController;
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\TransactionController;
use Illuminate\Support\Facades\Route;
Route::get('/', fn () => redirect()->route('transactions.index'))->name('home');
Route::middleware('guest')->group(function () {
Route::get('login', [AuthController::class, 'showLogin'])->name('login');
Route::post('login', [AuthController::class, 'login'])->name('login.attempt');
});
Route::post('logout', [AuthController::class, 'logout'])
->middleware('auth')
->name('logout');
Route::middleware('auth')->group(function () {
Route::resource('transactions', TransactionController::class);
Route::resource('categories', CategoryController::class)
->except('show');
Route::get('reports', [ReportController::class, 'current'])->name('reports.index');
Route::get('reports/{month}', [ReportController::class, 'show'])
->where('month', '[0-9]{4}-[0-9]{2}')
->name('reports.show');
});
tests/Feature/AuthTest.php
Unduh berkas<?php
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AuthTest extends TestCase
{
use RefreshDatabase;
public function test_tamu_diarahkan_ke_halaman_login(): void
{
$this->get(route('transactions.index'))
->assertRedirect(route('login'));
}
public function test_login_berhasil_dan_gagal(): void
{
User::factory()->create([
'name' => 'Budi Santoso',
'email' => '[email protected]',
'password' => bcrypt('password-rahasia'),
]);
// Kredensial salah: kembali dengan error.
$this->from(route('login'))
->post(route('login.attempt'), [
'email' => '[email protected]',
'password' => 'salah',
])
->assertSessionHasErrors('email');
// Kredensial benar: masuk dan diarahkan ke daftar transaksi.
$this->post(route('login.attempt'), [
'email' => '[email protected]',
'password' => 'password-rahasia',
])
->assertRedirect(route('transactions.index'));
$this->assertAuthenticated();
}
}
tests/Feature/CategoryTest.php
Unduh berkas<?php
namespace Tests\Feature;
use App\Models\Category;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class CategoryTest extends TestCase
{
use RefreshDatabase;
public function test_bisa_menambah_kategori(): void
{
$user = User::factory()->create([
'name' => 'Budi Santoso',
'email' => '[email protected]',
]);
$response = $this->actingAs($user)->post(route('categories.store'), [
'name' => 'Transportasi',
'type' => 'expense',
'color' => '#f97316',
]);
$response->assertRedirect(route('categories.index'));
$this->assertDatabaseHas('categories', [
'name' => 'Transportasi',
'type' => 'expense',
'color' => '#f97316',
]);
}
public function test_warna_harus_kode_hex_enam_digit(): void
{
$user = User::factory()->create([
'name' => 'Budi Santoso',
'email' => '[email protected]',
]);
foreach (['merah', '#12345', '#zzzzzz'] as $invalidColor) {
$this->actingAs($user)->post(route('categories.store'), [
'name' => 'Kategori Uji',
'type' => 'expense',
'color' => $invalidColor,
])->assertSessionHasErrors('color');
}
$this->assertDatabaseCount('categories', 0);
}
public function test_nama_kategori_harus_unik(): void
{
$user = User::factory()->create([
'name' => 'Budi Santoso',
'email' => '[email protected]',
]);
Category::create([
'name' => 'Makanan',
'type' => 'expense',
'color' => '#ef4444',
]);
$this->actingAs($user)->post(route('categories.store'), [
'name' => 'Makanan',
'type' => 'expense',
'color' => '#ef4444',
])->assertSessionHasErrors('name');
$this->assertDatabaseCount('categories', 1);
}
public function test_bisa_mengubah_kategori_dengan_nama_sendiri(): void
{
$user = User::factory()->create([
'name' => 'Budi Santoso',
'email' => '[email protected]',
]);
$category = Category::create([
'name' => 'Makanan',
'type' => 'expense',
'color' => '#ef4444',
]);
// Nama yang sama tetap boleh: aturan unique mengabaikan baris sendiri.
$this->actingAs($user)
->put(route('categories.update', $category), [
'name' => 'Makanan',
'type' => 'expense',
'color' => '#dc2626',
])
->assertRedirect(route('categories.index'));
$this->assertDatabaseHas('categories', [
'id' => $category->id,
'name' => 'Makanan',
'color' => '#dc2626',
]);
}
public function test_jenis_tidak_bisa_berubah_setelah_dipakai(): void
{
$user = User::factory()->create([
'name' => 'Budi Santoso',
'email' => '[email protected]',
]);
$category = Category::create([
'name' => 'Makanan',
'type' => 'expense',
'color' => '#ef4444',
]);
$user->transactions()->create([
'category_id' => $category->id,
'type' => 'expense',
'amount' => 45000,
'transaction_date' => '2026-10-05',
]);
$this->actingAs($user)
->put(route('categories.update', $category), [
'name' => 'Makanan',
'type' => 'income',
'color' => '#ef4444',
])
->assertSessionHasErrors('type');
$this->assertDatabaseHas('categories', [
'id' => $category->id,
'type' => 'expense',
]);
}
public function test_kategori_dipakai_tidak_bisa_dihapus(): void
{
$user = User::factory()->create([
'name' => 'Budi Santoso',
'email' => '[email protected]',
]);
$used = Category::create([
'name' => 'Makanan',
'type' => 'expense',
'color' => '#ef4444',
]);
$unused = Category::create([
'name' => 'Hiburan',
'type' => 'expense',
'color' => '#a855f7',
]);
$user->transactions()->create([
'category_id' => $used->id,
'type' => 'expense',
'amount' => 45000,
'transaction_date' => '2026-10-05',
]);
$this->actingAs($user)
->delete(route('categories.destroy', $used))
->assertRedirect(route('categories.index'))
->assertSessionHasErrors('name');
$this->assertDatabaseHas('categories', ['id' => $used->id]);
// Kategori tanpa transaksi boleh dihapus.
$this->actingAs($user)
->delete(route('categories.destroy', $unused))
->assertRedirect(route('categories.index'));
$this->assertDatabaseMissing('categories', ['id' => $unused->id]);
}
}
tests/Feature/ExampleTest.php
Unduh berkas<?php
namespace Tests\Feature;
use Tests\TestCase;
class ExampleTest extends TestCase
{
/**
* Halaman depan cukup meneruskan pengunjung ke daftar transaksi;
* dari sana middleware auth yang meneruskan tamu ke halaman login.
*/
public function test_halaman_depan_meneruskan_ke_daftar_transaksi(): void
{
$this->get('/')
->assertRedirect(route('transactions.index'));
}
}
tests/Feature/ReportCalculationTest.php
Unduh berkas<?php
namespace Tests\Feature;
use App\Models\Category;
use App\Models\User;
use App\Services\ReportService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ReportCalculationTest extends TestCase
{
use RefreshDatabase;
/**
* Data tetap dengan angka yang mudah dihitung manual.
*
* Oktober 2026 (milik Budi):
* - Pemasukan: Gaji 5.000.000 + Bonus 500.000 = 5.500.000
* - Pengeluaran: Makanan 130.000, Hiburan 80.000,
* Transportasi 35.000 = 245.000
* - Selisih = 5.255.000
*/
private function seedBudi(): User
{
$budi = User::factory()->create([
'name' => 'Budi Santoso',
'email' => '[email protected]',
]);
$gaji = Category::create(['name' => 'Gaji', 'type' => 'income', 'color' => '#16a34a']);
$bonus = Category::create(['name' => 'Bonus', 'type' => 'income', 'color' => '#22c55e']);
$makanan = Category::create(['name' => 'Makanan', 'type' => 'expense', 'color' => '#ef4444']);
$transportasi = Category::create(['name' => 'Transportasi', 'type' => 'expense', 'color' => '#f97316']);
$hiburan = Category::create(['name' => 'Hiburan', 'type' => 'expense', 'color' => '#a855f7']);
$budi->transactions()->createMany([
['category_id' => $gaji->id, 'type' => 'income', 'amount' => 5000000, 'transaction_date' => '2026-10-01', 'note' => 'Gaji bulan Oktober'],
['category_id' => $bonus->id, 'type' => 'income', 'amount' => 500000, 'transaction_date' => '2026-10-10', 'note' => 'Bonus proyek aplikasi'],
['category_id' => $makanan->id, 'type' => 'expense', 'amount' => 25000, 'transaction_date' => '2026-10-04', 'note' => 'Makan siang di kantin'],
['category_id' => $makanan->id, 'type' => 'expense', 'amount' => 45000, 'transaction_date' => '2026-10-07', 'note' => 'Kopi dan camilan'],
['category_id' => $makanan->id, 'type' => 'expense', 'amount' => 60000, 'transaction_date' => '2026-10-12', 'note' => 'Belanja bahan makanan'],
['category_id' => $transportasi->id, 'type' => 'expense', 'amount' => 15000, 'transaction_date' => '2026-10-03', 'note' => 'Ojek ke kantor'],
['category_id' => $transportasi->id, 'type' => 'expense', 'amount' => 20000, 'transaction_date' => '2026-10-09', 'note' => 'Isi bensin motor'],
['category_id' => $hiburan->id, 'type' => 'expense', 'amount' => 80000, 'transaction_date' => '2026-10-11', 'note' => 'Tiket bioskop akhir pekan'],
['category_id' => $gaji->id, 'type' => 'income', 'amount' => 4800000, 'transaction_date' => '2026-09-01', 'note' => 'Gaji bulan September'],
['category_id' => $makanan->id, 'type' => 'expense', 'amount' => 40000, 'transaction_date' => '2026-09-16', 'note' => 'Makan malam bersama teman'],
['category_id' => $makanan->id, 'type' => 'expense', 'amount' => 30000, 'transaction_date' => '2026-08-02', 'note' => 'Camilan akhir bulan'],
]);
return $budi;
}
public function test_ringkasan_bulan_oktober(): void
{
$budi = $this->seedBudi();
$summary = app(ReportService::class)->monthlySummary($budi, '2026-10');
$this->assertSame(5500000, $summary['income']);
$this->assertSame(245000, $summary['expense']);
$this->assertSame(5255000, $summary['balance']);
}
public function test_ringkasan_bulan_september(): void
{
$budi = $this->seedBudi();
$summary = app(ReportService::class)->monthlySummary($budi, '2026-09');
$this->assertSame(4800000, $summary['income']);
$this->assertSame(40000, $summary['expense']);
$this->assertSame(4760000, $summary['balance']);
}
public function test_bulan_tanpa_transaksi_menghasilkan_nol(): void
{
$budi = $this->seedBudi();
$summary = app(ReportService::class)->monthlySummary($budi, '2025-01');
$this->assertSame(0, $summary['income']);
$this->assertSame(0, $summary['expense']);
$this->assertSame(0, $summary['balance']);
}
public function test_total_pengeluaran_per_kategori_bulan_oktober(): void
{
$budi = $this->seedBudi();
$rows = app(ReportService::class)
->byCategory($budi, '2026-10', 'expense');
$this->assertSame(
[
['id' => 3, 'name' => 'Makanan', 'color' => '#ef4444', 'total' => 130000],
['id' => 5, 'name' => 'Hiburan', 'color' => '#a855f7', 'total' => 80000],
['id' => 4, 'name' => 'Transportasi', 'color' => '#f97316', 'total' => 35000],
],
$rows->all(),
);
}
public function test_total_pemasukan_per_kategori_bulan_oktober(): void
{
$budi = $this->seedBudi();
$rows = app(ReportService::class)
->byCategory($budi, '2026-10', 'income');
$this->assertSame(
[
['id' => 1, 'name' => 'Gaji', 'color' => '#16a34a', 'total' => 5000000],
['id' => 2, 'name' => 'Bonus', 'color' => '#22c55e', 'total' => 500000],
],
$rows->all(),
);
}
public function test_tren_arus_kas_enam_bulan(): void
{
$budi = $this->seedBudi();
$trend = app(ReportService::class)
->monthlyTrend($budi, 6, '2026-10');
$this->assertSame(
['2026-05', '2026-06', '2026-07', '2026-08', '2026-09', '2026-10'],
$trend->pluck('month')->all(),
);
$byMonth = $trend->keyBy('month');
$this->assertSame(0, $byMonth['2026-05']['income']);
$this->assertSame(0, $byMonth['2026-05']['expense']);
$this->assertSame(30000, $byMonth['2026-08']['expense']);
$this->assertSame(4800000, $byMonth['2026-09']['income']);
$this->assertSame(40000, $byMonth['2026-09']['expense']);
$this->assertSame(5500000, $byMonth['2026-10']['income']);
$this->assertSame(245000, $byMonth['2026-10']['expense']);
}
public function test_laporan_hanya_menghitung_transaksi_milik_sendiri(): void
{
$budi = $this->seedBudi();
$siti = User::factory()->create([
'name' => 'Siti Rahayu',
'email' => '[email protected]',
]);
$gaji = Category::where('name', 'Gaji')->first();
$siti->transactions()->create([
'category_id' => $gaji->id,
'type' => 'income',
'amount' => 2000000,
'transaction_date' => '2026-10-02',
]);
$service = app(ReportService::class);
// Laporan Budi tidak berubah oleh transaksi Siti.
$this->assertSame(5500000, $service->monthlySummary($budi, '2026-10')['income']);
// Laporan Siti hanya berisi transaksinya sendiri.
$this->assertSame(2000000, $service->monthlySummary($siti, '2026-10')['income']);
$this->assertSame(0, $service->monthlySummary($siti, '2026-10')['expense']);
}
public function test_halaman_laporan_menampilkan_angka_dan_batang(): void
{
$budi = $this->seedBudi();
$response = $this->actingAs($budi)->get(route('reports.show', '2026-10'));
$response->assertOk()
->assertSee('Laporan Oktober 2026')
->assertSee('Rp 5.500.000')
->assertSee('Rp 245.000')
->assertSee('Rp 5.255.000')
// Lebar batang kategori: persentase dari total pengeluaran.
->assertSee('width: 53%') // Makanan: 130.000 / 245.000
->assertSee('width: 33%') // Hiburan: 80.000 / 245.000
->assertSee('width: 14%') // Transportasi: 35.000 / 245.000
// Lebar batang tren: persentase dari nilai terbesar (5.500.000).
->assertSee('width: 100%') // Pemasukan Oktober.
->assertSee('width: 4%'); // Pengeluaran Oktober: 245.000 / 5.500.000.
}
public function test_bulan_tidak_valid_ditolak(): void
{
$budi = $this->seedBudi();
$this->actingAs($budi)->get('/reports/2026-13')->assertNotFound();
$this->actingAs($budi)->get('/reports/bulan-ini')->assertNotFound();
}
}
tests/Feature/TransactionCrudTest.php
Unduh berkas<?php
namespace Tests\Feature;
use App\Models\Category;
use App\Models\Transaction;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class TransactionCrudTest extends TestCase
{
use RefreshDatabase;
private function user(): User
{
return User::factory()->create([
'name' => 'Budi Santoso',
'email' => '[email protected]',
]);
}
/**
* @return array<string, Category>
*/
private function categories(): array
{
return [
'income' => Category::create([
'name' => 'Gaji',
'type' => 'income',
'color' => '#16a34a',
]),
'expense' => Category::create([
'name' => 'Makanan',
'type' => 'expense',
'color' => '#ef4444',
]),
];
}
public function test_bisa_mencatat_transaksi_pengeluaran(): void
{
$user = $this->user();
$categories = $this->categories();
$response = $this->actingAs($user)->post(route('transactions.store'), [
'category_id' => $categories['expense']->id,
'type' => 'expense',
'amount' => 45000,
'transaction_date' => '2026-10-05',
'note' => 'Makan siang di kantin',
]);
$response->assertRedirect(route('transactions.index'));
$this->assertDatabaseHas('transactions', [
'user_id' => $user->id,
'category_id' => $categories['expense']->id,
'type' => 'expense',
'amount' => 45000,
'transaction_date' => '2026-10-05',
'note' => 'Makan siang di kantin',
]);
}
public function test_bisa_mencatat_transaksi_pemasukan(): void
{
$user = $this->user();
$categories = $this->categories();
$this->actingAs($user)->post(route('transactions.store'), [
'category_id' => $categories['income']->id,
'type' => 'income',
'amount' => 5000000,
'transaction_date' => '2026-10-01',
'note' => 'Gaji bulan Oktober',
]);
$this->assertDatabaseHas('transactions', [
'user_id' => $user->id,
'type' => 'income',
'amount' => 5000000,
]);
}
public function test_nominal_wajib_bulat_dan_minimal_satu(): void
{
$user = $this->user();
$categories = $this->categories();
foreach ([0, -100, 2500.5] as $invalidAmount) {
$this->actingAs($user)->post(route('transactions.store'), [
'category_id' => $categories['expense']->id,
'type' => 'expense',
'amount' => $invalidAmount,
'transaction_date' => '2026-10-05',
'note' => 'Nominal tidak valid',
])->assertSessionHasErrors('amount');
}
$this->assertDatabaseCount('transactions', 0);
}
public function test_tanggal_masa_depan_ditolak(): void
{
$user = $this->user();
$categories = $this->categories();
$this->actingAs($user)->post(route('transactions.store'), [
'category_id' => $categories['expense']->id,
'type' => 'expense',
'amount' => 15000,
'transaction_date' => now()->addDay()->toDateString(),
'note' => 'Belum terjadi',
])->assertSessionHasErrors('transaction_date');
}
public function test_kategori_harus_cocok_dengan_jenis_transaksi(): void
{
$user = $this->user();
$categories = $this->categories();
// Jenis expense, tetapi kategori yang dikirim bertipe income.
$this->actingAs($user)->post(route('transactions.store'), [
'category_id' => $categories['income']->id,
'type' => 'expense',
'amount' => 50000,
'transaction_date' => '2026-10-05',
'note' => 'Salah kombinasi',
])->assertSessionHasErrors('category_id');
$this->assertDatabaseCount('transactions', 0);
}
public function test_bisa_mengubah_transaksi(): void
{
$user = $this->user();
$categories = $this->categories();
$transaction = $user->transactions()->create([
'category_id' => $categories['expense']->id,
'type' => 'expense',
'amount' => 45000,
'transaction_date' => '2026-10-05',
'note' => 'Kopi sore',
]);
$this->actingAs($user)
->put(route('transactions.update', $transaction), [
'category_id' => $categories['expense']->id,
'type' => 'expense',
'amount' => 55000,
'transaction_date' => '2026-10-06',
'note' => 'Kopi sore dan camilan',
])
->assertRedirect(route('transactions.index'));
$this->assertDatabaseHas('transactions', [
'id' => $transaction->id,
'amount' => 55000,
'transaction_date' => '2026-10-06',
]);
$this->assertDatabaseCount('transactions', 1);
}
public function test_bisa_menghapus_transaksi(): void
{
$user = $this->user();
$categories = $this->categories();
$transaction = $user->transactions()->create([
'category_id' => $categories['expense']->id,
'type' => 'expense',
'amount' => 45000,
'transaction_date' => '2026-10-05',
]);
$this->actingAs($user)
->delete(route('transactions.destroy', $transaction))
->assertRedirect(route('transactions.index'));
$this->assertDatabaseMissing('transactions', ['id' => $transaction->id]);
}
public function test_transaksi_pengguna_lain_tidak_bisa_diakses(): void
{
$budi = $this->user();
$categories = $this->categories();
$transaction = $budi->transactions()->create([
'category_id' => $categories['expense']->id,
'type' => 'expense',
'amount' => 45000,
'transaction_date' => '2026-10-05',
'note' => 'Catatan milik Budi',
]);
$siti = User::factory()->create([
'name' => 'Siti Rahayu',
'email' => '[email protected]',
]);
$this->actingAs($siti)->get(route('transactions.edit', $transaction))->assertForbidden();
$this->actingAs($siti)->put(route('transactions.update', $transaction), [
'category_id' => $categories['expense']->id,
'type' => 'expense',
'amount' => 1,
'transaction_date' => '2026-10-05',
])->assertForbidden();
$this->actingAs($siti)->delete(route('transactions.destroy', $transaction))->assertForbidden();
// Daftar milik Siti tidak menampilkan transaksi milik Budi.
$this->actingAs($siti)
->get(route('transactions.index'))
->assertOk()
->assertDontSee('Catatan milik Budi');
$this->assertDatabaseHas('transactions', ['id' => $transaction->id, 'amount' => 45000]);
}
public function test_filter_bulan_pada_daftar_transaksi(): void
{
$user = $this->user();
$categories = $this->categories();
$user->transactions()->createMany([
[
'category_id' => $categories['expense']->id,
'type' => 'expense',
'amount' => 40000,
'transaction_date' => '2026-09-16',
'note' => 'Makan malam bulan lalu',
],
[
'category_id' => $categories['expense']->id,
'type' => 'expense',
'amount' => 25000,
'transaction_date' => '2026-10-04',
'note' => 'Makan siang bulan ini',
],
]);
$this->actingAs($user)
->get(route('transactions.index', ['month' => '2026-10']))
->assertOk()
->assertSee('Makan siang bulan ini')
->assertDontSee('Makan malam bulan lalu');
}
}
tests/Unit/ExampleTest.php
Unduh berkas<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*/
public function test_that_true_is_true(): void
{
$this->assertTrue(true);
}
}