Organizando y comenzando con interfaces

This commit is contained in:
2025-01-11 20:10:14 -03:00
parent 07b62a07a0
commit b87ae08b9a
52 changed files with 9492 additions and 361 deletions

View File

@@ -0,0 +1,138 @@
<?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;
}
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();
}
#[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'));
}
}

View File

@@ -0,0 +1,140 @@
<?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;
}
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();
}
#[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'));
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace App\Livewire\Cajas;
use App\Models\Turno;
use Illuminate\Contracts\View\View;
use Livewire\Attributes\Url;
use Livewire\Component;
class Edit extends Component
{
public Turno $turno;
#[Url]
public $tab = 'Ingresos';
public function render(): View
{
return view('livewire.cajas.edit');
}
}

View File

@@ -2,12 +2,143 @@
namespace App\Livewire\Cajas;
use App\Models\Turno;
use Illuminate\Contracts\View\View;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Number;
use Illuminate\Validation\ValidationException;
use Livewire\Attributes\Computed;
use Livewire\Component;
use Livewire\WithPagination;
use Log;
use TallStackUi\Traits\Interactions;
class Index extends Component
{
public function render()
use Interactions, WithPagination;
public $modal = false;
public $fecha = null;
public $caja = null;
public $turno = null;
public array $sort = [
'column' => 'fecha',
'direction' => 'desc',
];
public function render(): View
{
return view('livewire.cajas.index');
}
public function createTurno(): void
{
$this->modal = true;
$this->fecha = now()->format('Y-m-d');
$this->caja = 1;
if (Turno::where('fecha', $this->fecha)->exists()) {
$this->turno = Turno::where('fecha', $this->fecha)->max('numero_turno') + 1;
} else {
$this->turno = 1;
}
}
public function storeTurno(): void
{
$this->validate([
'fecha' => 'required|date',
'caja' => 'required|gte:1|lte:10',
'turno' => 'required|gte:1|lte:10',
]);
$exists = Turno::where('fecha', $this->fecha)
->where('numero_caja', $this->caja)
->where('numero_turno', $this->turno)
->exists();
if ($exists) {
throw ValidationException::withMessages(['fecha' => 'Ya hay un registro para esta fecha, caja y turno']);
}
$turno = Turno::create([
'fecha' => $this->fecha,
'numero_caja' => $this->caja,
'numero_turno' => $this->turno,
]);
Session::flash('toast', ['type' => 'success', 'message' => 'Caja registrada correctamente']);
$this->redirectRoute('cajas.edit', $turno);
}
public function closeTurnoModal(): void
{
$this->modal = false;
$this->fecha = null;
$this->caja = null;
$this->turno = null;
$this->clearValidation();
}
public function confirmDelete($id): void
{
$this->dialog()->question('¿Esta seguro de eliminar este turno?', 'Se eliminara este junto a los ingresos y egresos registrados')
->confirm('Eliminar Turno', method: 'doDelete', params: $id)
->cancel()
->send();
}
public function doDelete($id): void
{
Turno::find($id)->delete();
$this->toast()->success('Turno eliminado correctamente')->send();
}
#[Computed]
public function headers(): array
{
return [
['index' => 'fecha', 'label' => 'Fecha'],
['index' => 'numero_caja', 'label' => 'Caja'],
['index' => 'numero_turno', 'label' => 'Turno'],
['index' => 'ingresos', 'label' => 'Ingresos'],
['index' => 'egresos', 'label' => 'Egresos'],
['index' => 'documentos', 'label' => 'Documento'],
['index' => 'diferencia', 'label' => 'diferencia'],
['index' => 'action', 'label' => 'Acciones'],
];
}
#[Computed]
public function rows(): LengthAwarePaginator
{
$turnos = Turno::orderBy('fecha', 'desc')
->orderBy('numero_caja', 'asc')
->orderBy('numero_turno', 'asc')
->paginate()
->through(function ($turno) {
return (object) [
'id' => $turno->id,
'fecha' => $turno->fecha->format('d-m-Y'),
'numero_caja' => $turno->numero_caja,
'numero_turno' => $turno->numero_turno,
'ingresos' => Number::currency($turno->ingresos()->sum('total')),
// 'egresos' => Number::currency(477960),
// 'documentos' => Number::currency(294717),
// 'diferencia' => Number::currency(-7544),
];
});
Log::info('', [$turnos]);
return $turnos;
}
}