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

144 lines
3.8 KiB
PHP

<?php
namespace App\Livewire\Cajas\Components;
use App\Enums\TipoIngreso;
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 IngresosComponent extends Component
{
use Interactions;
public Turno $turno;
#[Validate('required')]
public $tipo = null;
#[Validate('required|numeric')]
public $ingreso_inicial = null;
#[Validate('required|numeric')]
public $ingreso_final = null;
#[Validate('required|numeric')]
public $total = null;
public $currentIngreso = null;
public function mount(): void
{
$this->tipo = TipoIngreso::cases()[0];
}
public function render(): View
{
return view('livewire.cajas.components.ingresos-component');
}
public function save(): void
{
$this->validate();
if ($this->currentIngreso) {
Ingreso::where('id', $this->currentIngreso)->update([
'tipo_ingreso' => $this->tipo,
'numero' => 0,
'numero_z' => 0,
'ingreso_inicial' => $this->ingreso_inicial,
'ingreso_final' => $this->ingreso_final,
'total' => $this->total,
]);
} else {
$this->turno->ingresos()->create([
'tipo_ingreso' => $this->tipo,
'numero' => 0,
'numero_z' => 0,
'ingreso_inicial' => $this->ingreso_inicial,
'ingreso_final' => $this->ingreso_final,
'total' => $this->total,
]);
}
if ($this->currentIngreso) {
$this->toast()->success('Exito!', 'Ingreso modificado correctamente')->send();
} else {
$this->toast()->success('Exito!', 'Ingreso guardado correctamente')->send();
}
$this->currentIngreso = null;
$this->ingreso_inicial = null;
$this->ingreso_final = null;
$this->total = null;
$this->dispatch('updated_totals');
}
public function edit($id): void
{
$this->currentIngreso = $id;
$ingreso = Ingreso::find($id);
$this->tipo = $ingreso->tipo_ingreso;
$this->ingreso_inicial = $ingreso->ingreso_inicial;
$this->ingreso_final = $ingreso->ingreso_final;
$this->total = $ingreso->total;
}
public function confirmDelete($id): void
{
$this->dialog()->question('¿Esta seguro de eliminar este ingreso?', 'No podrá recuperarlo')
->confirm('Eliminar Ingreso', method: 'delete', params: $id)
->cancel()
->send();
}
public function delete($id): void
{
Ingreso::where('id', $id)->delete();
$this->toast()->success('Ingreso eliminado correctamente')->send();
$this->dispatch('updated_totals');
}
#[Computed]
public function headers(): array
{
return [
['index' => 'tipo_ingreso', 'label' => 'Tipo Documento'],
['index' => 'ingreso_inicial', 'label' => 'Documento Inicial'],
['index' => 'ingreso_final', 'label' => 'Documento Final'],
['index' => 'total', 'label' => 'Total'],
['index' => 'action', 'label' => 'Acciones'],
];
}
#[Computed]
public function rows(): Collection
{
return $this->turno->ingresos()
->orderBy('created_at', 'desc')
->get();
}
#[Computed]
public function tipos(): array
{
return TipoIngreso::cases();
}
#[Computed]
public function totalIngresos(): string
{
return Number::currency($this->turno->ingresos()->sum('total'));
}
}