feat(expense): menambahan kolom baru yaitu user_id

- membuat relasi nya di model
- menyesuaikan logika crudnya
- menampilkan nama pegawai di table
- menyesuaikan data yang di tampilkan dan action nya di tab;e
This commit is contained in:
Yoga Pangestu 2025-11-16 18:24:22 +07:00
parent cf0556574d
commit 8ebb1f5fe8
5 changed files with 37 additions and 17 deletions

View File

@ -20,6 +20,8 @@ class ExpensesTable extends DataTableComponent
public function columns(): array
{
return [
Column::make('Pegawai', 'user.employee.full_name')->searchable(),
Column::make('Keterangan', 'description')->searchable(),
Column::make('Jumlah', 'amount')
@ -37,30 +39,34 @@ public function columns(): array
->label(function ($row) {
$actions = '';
if (auth()->user()->can('update expense')) {
$actions .= view('components.datatables.edit-modal', [
'id' => $row->hash,
'method' => 'update',
'modalTitle' => 'Ubah Kategori',
])->render();
}
if ($row->user_id == auth()->id() || auth()->user()->hasAnyRole(['Developer', 'Owner'])) {
if (auth()->user()->can('update expense')) {
$actions .= view('components.datatables.edit-modal', [
'id' => $row->hash,
'method' => 'update',
'modalTitle' => 'Ubah Kategori',
])->render();
}
if (auth()->user()->can('delete expense')) {
$actions .= view('components.datatables.delete', [
'id' => $row->hash,
'deleteRoute' => route('studio.finance.expense.delete', $row->hash),
])->render();
if (auth()->user()->can('delete expense')) {
$actions .= view('components.datatables.delete', [
'id' => $row->hash,
'deleteRoute' => route('studio.finance.expense.delete', $row->hash),
])->render();
}
}
return $actions;
})
->html()
->hideIf(auth()->user()->cannot('update expense') && auth()->user()->cannot('delete expense')),
->hideIf(auth()->user()->cannot('update expense') && auth()->user()->cannot('delete expense') && auth()->id !== $row->user_id),
];
}
public function builder(): Builder
{
return Expense::select('expenses.id', 'description', 'amount', 'expenses.created_at')->with('outlet');
return Expense::select('expenses.id', 'expenses.user_id', 'description', 'amount', 'expenses.created_at')
->whereIn('outlet_id', auth()->user()->outlets->pluck('id')->toArray())
->with(['outlet', 'user', 'user.employee']);
}
}

View File

@ -48,7 +48,7 @@ public function setExpense(Expense $expense)
$this->expense = $expense;
$this->description = $expense->description;
$this->amount = $expense->amount;
$this->amount = currency($expense->amount);
$this->outlet_id = $expense->outlet_id;
$this->image = $this->mapMediaCollection($expense->getMedia('image'));
@ -60,6 +60,7 @@ public function store()
DB::transaction(function () {
$expense = Expense::create([
'user_id' => auth()->id(),
'type' => ExpenseType::OPERATIONAL,
'description' => $this->description,
'amount' => replaceCurrency($this->amount),

View File

@ -9,11 +9,13 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Veelasky\LaravelHashId\Eloquent\HashableId;
class Expense extends Model
class Expense extends Model implements HasMedia
{
use HasFactory, HashableId, SoftDeletes;
use HasFactory, HashableId, InteractsWithMedia, SoftDeletes;
protected $guarded = ['id'];
@ -37,6 +39,11 @@ public function yesterday(Builder $query): void
$query->whereDate('created_at', today()->subDay());
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function outlet(): BelongsTo
{
return $this->belongsTo(Outlet::class);

View File

@ -136,4 +136,9 @@ public function pushNotifications(): HasMany
{
return $this->hasMany(PushNotification::class);
}
public function users(): HasMany
{
return $this->hasMany(User::class);
}
}

View File

@ -14,6 +14,7 @@ public function up(): void
{
Schema::create('expenses', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->foreignId('outlet_id')->constrained()->cascadeOnDelete();
$table->unsignedInteger('amount');
$table->string('description', 100);