feat: introduce expense management feature with dedicated model, migration, seeder, and Filament resource.
This commit is contained in:
parent
ee5e3952ac
commit
f6a1abfa0e
148
app/Filament/Resources/Finance/Expenses/ExpenseResource.php
Normal file
148
app/Filament/Resources/Finance/Expenses/ExpenseResource.php
Normal file
@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Finance\Expenses;
|
||||
|
||||
use App\Filament\Actions\Cheerful\DeleteAction;
|
||||
use App\Filament\Actions\Cheerful\EditAction;
|
||||
use App\Filament\Actions\Cheerful\ForceDeleteAction;
|
||||
use App\Filament\Actions\Cheerful\RestoreAction;
|
||||
use App\Filament\Actions\DefaultBulkActions;
|
||||
use App\Filament\Columns\TimestampColumns;
|
||||
use App\Filament\Resources\Finance\Expenses\Pages\ManageExpenses;
|
||||
use App\Models\Expense;
|
||||
use BackedEnum;
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Forms\Components\DatePicker;
|
||||
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Enums\Width;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\TrashedFilter;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||
use UnitEnum;
|
||||
|
||||
class ExpenseResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Expense::class;
|
||||
|
||||
protected static string|UnitEnum|null $navigationGroup = 'Keuangan';
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::CreditCard;
|
||||
|
||||
protected static ?string $navigationLabel = 'Pengeluaran';
|
||||
|
||||
protected static ?int $navigationSort = 1;
|
||||
|
||||
protected static ?string $recordTitleAttribute = 'notes';
|
||||
|
||||
protected static ?string $slug = 'finance/expenses';
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
DatePicker::make('expense_date')
|
||||
->label('Tanggal')
|
||||
->default(now())
|
||||
->required()
|
||||
->native(false)
|
||||
->displayFormat('l, d F Y'),
|
||||
|
||||
TextInput::make('amount')
|
||||
->label('Nominal')
|
||||
->placeholder('0')
|
||||
->autocomplete(false)
|
||||
->required()
|
||||
->currencyMask(thousandSeparator: '.', decimalSeparator: ',', precision: 0)
|
||||
->prefix('Rp')
|
||||
->dehydrateStateUsing(fn ($state) => (int) str()->replace('.', '', (string) ($state ?? 0))),
|
||||
|
||||
Textarea::make('notes')
|
||||
->label('Keterangan / Catatan')
|
||||
->placeholder('Bayar listrik bulanan')
|
||||
->required()
|
||||
->autocomplete(false),
|
||||
|
||||
SpatieMediaLibraryFileUpload::make('image')
|
||||
->label('Bukti')
|
||||
->disk(config('filesystems.default'))
|
||||
->acceptedFileTypes(['image/*'])
|
||||
->maxSize(1024 * 3)
|
||||
->collection('expenses')
|
||||
->customProperties(fn (): array => [
|
||||
'feature' => 'expenses',
|
||||
'date' => now()->toDateString(),
|
||||
])
|
||||
->image(),
|
||||
])
|
||||
->columns(1);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('expense_date')
|
||||
->label('Tanggal')
|
||||
->date('l, d F Y')
|
||||
->sortable(),
|
||||
|
||||
TextColumn::make('notes')
|
||||
->label('Keterangan')
|
||||
->searchable()
|
||||
->sortable()
|
||||
->limit(50),
|
||||
|
||||
TextColumn::make('amount')
|
||||
->label('Nominal')
|
||||
->money('IDR', decimalPlaces: 0)
|
||||
->sortable(),
|
||||
|
||||
...TimestampColumns::make(),
|
||||
])
|
||||
->filters([
|
||||
TrashedFilter::make()
|
||||
->native(false),
|
||||
])
|
||||
->recordActions([
|
||||
EditAction::make()
|
||||
->modalWidth(Width::Large),
|
||||
|
||||
DeleteAction::make(),
|
||||
|
||||
ForceDeleteAction::make(),
|
||||
|
||||
RestoreAction::make(),
|
||||
])
|
||||
->toolbarActions([
|
||||
BulkActionGroup::make([
|
||||
...DefaultBulkActions::make('Pengeluaran'),
|
||||
]),
|
||||
])
|
||||
->emptyStateIcon(Heroicon::CreditCard)
|
||||
->emptyStateDescription('Setelah Anda membuat data pertama, maka akan muncul disini.')
|
||||
->defaultSort('expense_date', 'desc')
|
||||
->deferFilters(false);
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ManageExpenses::route('/'),
|
||||
];
|
||||
}
|
||||
|
||||
public static function getRecordRouteBindingEloquentQuery(): Builder
|
||||
{
|
||||
return parent::getRecordRouteBindingEloquentQuery()
|
||||
->withoutGlobalScopes([
|
||||
SoftDeletingScope::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Finance\Expenses\Pages;
|
||||
|
||||
use App\Filament\Actions\Cheerful\CreateAction;
|
||||
use App\Filament\Resources\Finance\Expenses\ExpenseResource;
|
||||
use Filament\Resources\Pages\ManageRecords;
|
||||
use Filament\Support\Enums\Width;
|
||||
|
||||
class ManageExpenses extends ManageRecords
|
||||
{
|
||||
protected static string $resource = ExpenseResource::class;
|
||||
|
||||
protected static ?string $title = 'Pengeluaran';
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make()
|
||||
->label('Tambah')
|
||||
->modalHeading('Tambah Pengeluaran')
|
||||
->modalSubmitActionLabel('Simpan')
|
||||
->modalCancelActionLabel('Batal')
|
||||
->extraModalFooterActions(fn (CreateAction $action): array => [
|
||||
$action->makeModalSubmitAction('createAnother', arguments: ['another' => true])
|
||||
->label('Simpan dan Tambah Lagi'),
|
||||
])
|
||||
->modalWidth(Width::Large),
|
||||
];
|
||||
}
|
||||
}
|
||||
20
app/Models/Expense.php
Normal file
20
app/Models/Expense.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Spatie\MediaLibrary\HasMedia;
|
||||
use Spatie\MediaLibrary\InteractsWithMedia;
|
||||
|
||||
class Expense extends Model implements HasMedia
|
||||
{
|
||||
use HasFactory, InteractsWithMedia, SoftDeletes;
|
||||
|
||||
protected $guarded = ['id'];
|
||||
|
||||
protected $casts = [
|
||||
'expense_date' => 'date',
|
||||
];
|
||||
}
|
||||
@ -70,6 +70,7 @@ public function panel(Panel $panel): Panel
|
||||
->navigationGroups([
|
||||
'Master',
|
||||
'Kelola',
|
||||
'Keuangan',
|
||||
'Pelindung',
|
||||
'Sistem',
|
||||
])
|
||||
|
||||
25
database/factories/ExpenseFactory.php
Normal file
25
database/factories/ExpenseFactory.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Expense>
|
||||
*/
|
||||
class ExpenseFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'expense_date' => fake()->dateTimeBetween('-1 month', 'now'),
|
||||
'amount' => fake()->numberBetween(10000, 5000000),
|
||||
'notes' => fake()->sentence(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -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('expenses', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->date('expense_date');
|
||||
$table->unsignedInteger('amount');
|
||||
$table->text('notes')->nullable();
|
||||
$table->timestamp('created_at')->useCurrent();
|
||||
$table->timestamp('updated_at')->nullable()->useCurrentOnUpdate();
|
||||
$table->softDeletes();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('expenses');
|
||||
}
|
||||
};
|
||||
@ -26,6 +26,7 @@ public function run(): void
|
||||
EggPriceSeeder::class,
|
||||
OrderSeeder::class,
|
||||
DelayedGoodSeeder::class,
|
||||
ExpenseSeeder::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
17
database/seeders/ExpenseSeeder.php
Normal file
17
database/seeders/ExpenseSeeder.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\Expense;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class ExpenseSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
Expense::factory()->count(20)->create();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user