Files
zenithar/app/Livewire/Cajas/Components/CalculoFondoComponent.php
2025-01-12 13:00:21 -03:00

132 lines
3.3 KiB
PHP

<?php
namespace App\Livewire\Cajas\Components;
use App\Enums\TipoDocumento;
use App\Models\CalculoFondo;
use App\Models\Documento;
use App\Models\Turno;
use Exception;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
use Illuminate\Support\Number;
use Illuminate\Validation\ValidationException;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Validate;
use Livewire\Component;
use Log;
use TallStackUi\Traits\Interactions;
class CalculoFondoComponent extends Component
{
use Interactions;
public Turno $turno;
#[Validate('required|numeric')]
public $fondo = null;
#[Validate('nullable')]
public $descripcion = null;
#[Validate('required|numeric')]
public $valor = null;
public $currentCalculo = null;
public function mount()
{
$this->fondo = $this->turno->fondo;
}
public function render(): View
{
return view('livewire.cajas.components.calculo-fondo-component');
}
public function updatedFondo(): void
{
$this->validateOnly('fondo');
$this->turno->update(['fondo' => $this->fondo]);
}
public function save(): void
{
$this->validateOnly('descripcion');
$this->validateOnly('valor');
if ($this->currentCalculo) {
CalculoFondo::where('id', $this->currentCalculo)->update([
'descripcion' => $this->descripcion,
'valor' => $this->valor,
]);
} else {
$this->turno->calculosFondo()->create([
'descripcion' => $this->descripcion,
'valor' => $this->valor,
]);
}
if ($this->currentCalculo) {
$this->toast()->success('Exito!', 'Registro modificado correctamente')->send();
} else {
$this->toast()->success('Exito!', 'Registro guardado correctamente')->send();
}
$this->currentCalculo = null;
$this->descripcion = null;
$this->valor = null;
$this->dispatch('updated_totals');
}
public function edit($id): void
{
$this->currentCalculo = $id;
$calculo = CalculoFondo::find($id);
$this->descripcion = $calculo->descripcion;
$this->valor = $calculo->valor;
}
public function confirmDelete($id): void
{
$this->dialog()->question('¿Esta seguro de eliminar este registro?', 'No podrá recuperarlo')
->confirm('Eliminar Registro', method: 'delete', params: $id)
->cancel()
->send();
}
public function delete($id): void
{
CalculoFondo::where('id', $id)->delete();
$this->toast()->success('Registro eliminado correctamente')->send();
$this->dispatch('updated_totals');
}
#[Computed]
public function headers(): array
{
return [
['index' => 'valor', 'label' => 'Total'],
['index' => 'descripcion', 'label' => 'Descripción'],
['index' => 'action', 'label' => 'Acciones'],
];
}
#[Computed]
public function rows(): Collection
{
return $this->turno->calculosFondo()
->orderBy('created_at', 'asc')
->get();
}
#[Computed]
public function totalCalculo(): int
{
return $this->turno->calculosFondo()->sum('valor');
}
}