diff --git a/app/Livewire/Datatable/Studio/Finance/ExpensesTable.php b/app/Livewire/Datatable/Studio/Finance/ExpensesTable.php index a727a2d..e92b26a 100644 --- a/app/Livewire/Datatable/Studio/Finance/ExpensesTable.php +++ b/app/Livewire/Datatable/Studio/Finance/ExpensesTable.php @@ -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']); } } diff --git a/app/Livewire/Forms/Studio/Finance/ExpenseForm.php b/app/Livewire/Forms/Studio/Finance/ExpenseForm.php index 0b945c2..a367d37 100644 --- a/app/Livewire/Forms/Studio/Finance/ExpenseForm.php +++ b/app/Livewire/Forms/Studio/Finance/ExpenseForm.php @@ -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), diff --git a/app/Models/Expense.php b/app/Models/Expense.php index 5dfcc7e..a375eee 100644 --- a/app/Models/Expense.php +++ b/app/Models/Expense.php @@ -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); diff --git a/app/Models/User.php b/app/Models/User.php index 3bbe3f2..af350b9 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -136,4 +136,9 @@ public function pushNotifications(): HasMany { return $this->hasMany(PushNotification::class); } + + public function users(): HasMany + { + return $this->hasMany(User::class); + } } diff --git a/database/migrations/2025_10_02_114249_create_expenses_table.php b/database/migrations/2025_10_02_114249_create_expenses_table.php index e1ea1ae..81a38a5 100644 --- a/database/migrations/2025_10_02_114249_create_expenses_table.php +++ b/database/migrations/2025_10_02_114249_create_expenses_table.php @@ -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);