89 lines
2.4 KiB
PHP
89 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Forms\Studio\Finance;
|
|
|
|
use App\Enums\ExpenseType;
|
|
use App\Models\Expense;
|
|
use App\Rules\UnsignedInteger;
|
|
use App\Traits\WithMediaHandler;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\Rule;
|
|
use Livewire\Form;
|
|
|
|
class ExpenseForm extends Form
|
|
{
|
|
use WithMediaHandler;
|
|
|
|
public ?Expense $expense = null;
|
|
|
|
public string $description = '';
|
|
|
|
public string $amount = '';
|
|
|
|
public string $outlet_id = '';
|
|
|
|
public array $image = [];
|
|
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'description' => ['required', 'string', 'max:100'],
|
|
'amount' => ['required', new UnsignedInteger],
|
|
'outlet_id' => [Rule::requiredIf(auth()->user()->outlets()->count() > 1), Rule::exists('outlets', 'id')],
|
|
'image' => ['nullable', 'array'],
|
|
];
|
|
}
|
|
|
|
public function validationAttributes(): array
|
|
{
|
|
return [
|
|
'description' => 'keterangan',
|
|
'amount' => 'jumlah',
|
|
'image' => 'gambar',
|
|
];
|
|
}
|
|
|
|
public function setExpense(Expense $expense): void
|
|
{
|
|
$this->expense = $expense;
|
|
|
|
$this->description = $expense->description;
|
|
$this->amount = formatCurrencyNumber($expense->amount);
|
|
$this->outlet_id = $expense->outlet_id;
|
|
|
|
$this->image = $this->mapMediaCollection($expense->getMedia('image'));
|
|
}
|
|
|
|
public function store(): void
|
|
{
|
|
$this->validate();
|
|
|
|
DB::transaction(function () {
|
|
$expense = Expense::create([
|
|
'user_id' => auth()->id(),
|
|
'type' => ExpenseType::OPERATIONAL,
|
|
'description' => $this->description,
|
|
'amount' => parseRupiahToInt($this->amount),
|
|
'outlet_id' => auth()->user()->outlets()->count() > 1 ? $this->outlet_id : auth()->user()->outlets()->first()->id,
|
|
]);
|
|
|
|
$this->uploadMedia($this->image, $expense, 'image');
|
|
});
|
|
}
|
|
|
|
public function update(): void
|
|
{
|
|
$this->validate();
|
|
|
|
DB::transaction(function () {
|
|
$this->expense->update([
|
|
'description' => $this->description,
|
|
'amount' => parseRupiahToInt($this->amount),
|
|
]);
|
|
|
|
$this->syncMedia($this->image, $this->expense, 'image');
|
|
$this->uploadMedia($this->image, $this->expense, 'image');
|
|
});
|
|
}
|
|
}
|