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:
parent
cf0556574d
commit
8ebb1f5fe8
@ -20,6 +20,8 @@ class ExpensesTable extends DataTableComponent
|
|||||||
public function columns(): array
|
public function columns(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
Column::make('Pegawai', 'user.employee.full_name')->searchable(),
|
||||||
|
|
||||||
Column::make('Keterangan', 'description')->searchable(),
|
Column::make('Keterangan', 'description')->searchable(),
|
||||||
|
|
||||||
Column::make('Jumlah', 'amount')
|
Column::make('Jumlah', 'amount')
|
||||||
@ -37,30 +39,34 @@ public function columns(): array
|
|||||||
->label(function ($row) {
|
->label(function ($row) {
|
||||||
$actions = '';
|
$actions = '';
|
||||||
|
|
||||||
if (auth()->user()->can('update expense')) {
|
if ($row->user_id == auth()->id() || auth()->user()->hasAnyRole(['Developer', 'Owner'])) {
|
||||||
$actions .= view('components.datatables.edit-modal', [
|
if (auth()->user()->can('update expense')) {
|
||||||
'id' => $row->hash,
|
$actions .= view('components.datatables.edit-modal', [
|
||||||
'method' => 'update',
|
'id' => $row->hash,
|
||||||
'modalTitle' => 'Ubah Kategori',
|
'method' => 'update',
|
||||||
])->render();
|
'modalTitle' => 'Ubah Kategori',
|
||||||
}
|
])->render();
|
||||||
|
}
|
||||||
|
|
||||||
if (auth()->user()->can('delete expense')) {
|
if (auth()->user()->can('delete expense')) {
|
||||||
$actions .= view('components.datatables.delete', [
|
$actions .= view('components.datatables.delete', [
|
||||||
'id' => $row->hash,
|
'id' => $row->hash,
|
||||||
'deleteRoute' => route('studio.finance.expense.delete', $row->hash),
|
'deleteRoute' => route('studio.finance.expense.delete', $row->hash),
|
||||||
])->render();
|
])->render();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $actions;
|
return $actions;
|
||||||
})
|
})
|
||||||
->html()
|
->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
|
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']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -48,7 +48,7 @@ public function setExpense(Expense $expense)
|
|||||||
$this->expense = $expense;
|
$this->expense = $expense;
|
||||||
|
|
||||||
$this->description = $expense->description;
|
$this->description = $expense->description;
|
||||||
$this->amount = $expense->amount;
|
$this->amount = currency($expense->amount);
|
||||||
$this->outlet_id = $expense->outlet_id;
|
$this->outlet_id = $expense->outlet_id;
|
||||||
|
|
||||||
$this->image = $this->mapMediaCollection($expense->getMedia('image'));
|
$this->image = $this->mapMediaCollection($expense->getMedia('image'));
|
||||||
@ -60,6 +60,7 @@ public function store()
|
|||||||
|
|
||||||
DB::transaction(function () {
|
DB::transaction(function () {
|
||||||
$expense = Expense::create([
|
$expense = Expense::create([
|
||||||
|
'user_id' => auth()->id(),
|
||||||
'type' => ExpenseType::OPERATIONAL,
|
'type' => ExpenseType::OPERATIONAL,
|
||||||
'description' => $this->description,
|
'description' => $this->description,
|
||||||
'amount' => replaceCurrency($this->amount),
|
'amount' => replaceCurrency($this->amount),
|
||||||
|
|||||||
@ -9,11 +9,13 @@
|
|||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
use Spatie\MediaLibrary\HasMedia;
|
||||||
|
use Spatie\MediaLibrary\InteractsWithMedia;
|
||||||
use Veelasky\LaravelHashId\Eloquent\HashableId;
|
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'];
|
protected $guarded = ['id'];
|
||||||
|
|
||||||
@ -37,6 +39,11 @@ public function yesterday(Builder $query): void
|
|||||||
$query->whereDate('created_at', today()->subDay());
|
$query->whereDate('created_at', today()->subDay());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function user(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class);
|
||||||
|
}
|
||||||
|
|
||||||
public function outlet(): BelongsTo
|
public function outlet(): BelongsTo
|
||||||
{
|
{
|
||||||
return $this->belongsTo(Outlet::class);
|
return $this->belongsTo(Outlet::class);
|
||||||
|
|||||||
@ -136,4 +136,9 @@ public function pushNotifications(): HasMany
|
|||||||
{
|
{
|
||||||
return $this->hasMany(PushNotification::class);
|
return $this->hasMany(PushNotification::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function users(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(User::class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,6 +14,7 @@ public function up(): void
|
|||||||
{
|
{
|
||||||
Schema::create('expenses', function (Blueprint $table) {
|
Schema::create('expenses', function (Blueprint $table) {
|
||||||
$table->id();
|
$table->id();
|
||||||
|
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||||||
$table->foreignId('outlet_id')->constrained()->cascadeOnDelete();
|
$table->foreignId('outlet_id')->constrained()->cascadeOnDelete();
|
||||||
$table->unsignedInteger('amount');
|
$table->unsignedInteger('amount');
|
||||||
$table->string('description', 100);
|
$table->string('description', 100);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user