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

142 lines
3.6 KiB
PHP

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