139 lines
3.6 KiB
PHP
139 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Cajas;
|
|
|
|
use App\Models\Turno;
|
|
use Carbon\Carbon;
|
|
use Exception;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Livewire\Attributes\Computed;
|
|
use Livewire\Attributes\On;
|
|
use Livewire\Component;
|
|
use Livewire\WithPagination;
|
|
use Log;
|
|
use TallStackUi\Traits\Interactions;
|
|
|
|
class Index extends Component
|
|
{
|
|
use Interactions, WithPagination;
|
|
|
|
public $searchFecha = null;
|
|
|
|
public $fecha = null;
|
|
|
|
public $caja = null;
|
|
|
|
public $turno = null;
|
|
|
|
public function render(): View
|
|
{
|
|
return view('livewire.cajas.index');
|
|
}
|
|
|
|
public function createTurno(): void
|
|
{
|
|
$this->fecha = now()->format('Y-m-d');
|
|
$this->caja = 1;
|
|
|
|
if (Turno::where('fecha', now())->exists()) {
|
|
$this->turno = Turno::where('fecha', now())->max('numero_turno') + 1;
|
|
} else {
|
|
$this->turno = 1;
|
|
}
|
|
|
|
$this->dispatch('show-modal', "turno");
|
|
}
|
|
|
|
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,
|
|
]);
|
|
|
|
$turno->efectivo()->create([
|
|
'veinte_mil' => 0,
|
|
'diez_mil' => 0,
|
|
'cinco_mil' => 0,
|
|
'dos_mil' => 0,
|
|
'mil' => 0,
|
|
'quinientos' => 0,
|
|
'cien' => 0,
|
|
'cincuenta' => 0,
|
|
'diez' => 0,
|
|
]);
|
|
|
|
Session::flash('toast', ['type' => 'success', 'message' => 'Caja registrada correctamente']);
|
|
|
|
$this->redirectRoute('cajas.edit', $turno, navigate: true);
|
|
}
|
|
|
|
#[On('close-modal-turno')]
|
|
public function closeTurnoModal(): void
|
|
{
|
|
Log::info("Cerrando modal");
|
|
$this->fecha = null;
|
|
$this->caja = null;
|
|
$this->turno = null;
|
|
|
|
$this->clearValidation();
|
|
$this->dispatch('hide-modal', 'turno');
|
|
}
|
|
|
|
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 rows(): LengthAwarePaginator
|
|
{
|
|
return Turno::orderBy('fecha', 'desc')
|
|
->orderBy('numero_caja', 'asc')
|
|
->orderBy('numero_turno', 'asc')
|
|
->when($this->searchFecha, function ($query) {
|
|
if (!$this->searchFecha) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$fecha = Carbon::parse($this->searchFecha);
|
|
$query->whereDate('fecha', $fecha);
|
|
} catch (Exception $exception) {
|
|
// Pass nada que hacer
|
|
}
|
|
})
|
|
->paginate();
|
|
}
|
|
}
|