Files
zenithar/app/Livewire/Cajas/Components/DocumentosComponent.php
2025-01-12 02:03:23 -03:00

131 lines
3.3 KiB
PHP

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