121 lines
2.9 KiB
PHP
121 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Livewire;
|
|
|
|
use App\Models\EstadoMovimiento;
|
|
use App\Models\Movimiento;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Livewire\Component;
|
|
use Livewire\WithPagination;
|
|
|
|
class VentaController extends Component
|
|
{
|
|
use WithPagination;
|
|
|
|
const CODIGO_VENTA = 1;
|
|
|
|
public $buscador = '', $modal = false;
|
|
public $venta, $cambio, $faltante;
|
|
|
|
protected $rules = [
|
|
'venta.codigo' => 'min:0',
|
|
'venta.pago_efectivo' => 'numeric',
|
|
'venta.pago_tarjeta' => 'numeric',
|
|
'venta.pago_vales' => 'numeric',
|
|
'venta.precio_venta' => 'required|numeric|gte:1',
|
|
'venta.pago_transferencia' => 'numeric',
|
|
'venta.estado_movimiento_id' => 'numeric',
|
|
'venta.user_id',
|
|
];
|
|
|
|
public function updatingBuscador()
|
|
{
|
|
$this->resetPage();
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
$ventas = Movimiento::where([['codigo','like','%'.$this->buscador.'%'],['estado_movimiento_id','=',VentaController::CODIGO_VENTA]])->paginate(10);
|
|
return view('venta.venta',[
|
|
'ventas' => $ventas,
|
|
'estadoVentas' => EstadoMovimiento::all(),
|
|
]);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->clearInputs();
|
|
$this->venta = new Movimiento([
|
|
'codigo' => 0,
|
|
'precio_venta' => 0,
|
|
'pago_efectivo' => 0,
|
|
'pago_tarjeta' => 0,
|
|
'pago_vales' => 0,
|
|
'pago_transferencia' => 0,
|
|
'estado_movimiento_id' => 1,
|
|
]);
|
|
$this->cambio;
|
|
$this->showModal();
|
|
}
|
|
|
|
public function updated()
|
|
{
|
|
if(!$this->modal)
|
|
return;
|
|
if($this->venta->cambio()>0)
|
|
{
|
|
$this->cambio = $this->venta->cambio();
|
|
$this->faltante = 0;
|
|
}
|
|
else
|
|
{
|
|
$this->faltante = $this->venta->faltante();
|
|
$this->cambio = 0;
|
|
}
|
|
}
|
|
|
|
public function save()
|
|
{
|
|
$this->validate([
|
|
'venta.pago_efectivo' => 'numeric',
|
|
'venta.precio_venta' => 'required|numeric|gte:1',
|
|
'venta.pago_tarjeta' => 'required|numeric',
|
|
'venta.pago_vales' => 'numeric',
|
|
'venta.pago_transferencia' => 'numeric',
|
|
'venta.estado_movimiento_id' => 'numeric',
|
|
]);
|
|
|
|
if($this->venta->isTotalMayorCambio())
|
|
{
|
|
session()->flash("error","Pago diferente al efectivo no pueden superar el total de venta.");
|
|
return;
|
|
}
|
|
|
|
$this->venta->ajusteCambio();
|
|
|
|
$this->venta->user_id = Auth::user()->id;
|
|
$this->venta->save();
|
|
|
|
session()->flash('message',"La venta se ha registrado correctamente!");
|
|
|
|
$this->clearInputs();
|
|
$this->closeModal();
|
|
}
|
|
|
|
|
|
public function showModal()
|
|
{
|
|
$this->modal = true;
|
|
}
|
|
|
|
public function closeModal()
|
|
{
|
|
$this->modal = false;
|
|
}
|
|
|
|
public function clearInputs()
|
|
{
|
|
$this->reset();
|
|
}
|
|
}
|