229 lines
6.5 KiB
PHP
229 lines
6.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Livewire;
|
|
|
|
use App\Models\Movimiento;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Livewire\Component;
|
|
use Livewire\WithPagination;
|
|
|
|
class VentaEspecialController extends Component
|
|
{
|
|
use WithPagination;
|
|
|
|
const CODIGO_VENTA_ESPECIAL = 2;
|
|
public $buscador = '', $modal = false, $modalDate = false, $created_at, $modalLiquidar;
|
|
public $ventaEspecial, $total_pagar, $ventaAnterior, $abonado, $cambio, $faltante;
|
|
|
|
protected $rules = [
|
|
'ventaEspecial.codigo' => 'min:0',
|
|
'ventaEspecial.pago_efectivo' => 'numeric',
|
|
'ventaEspecial.pago_tarjeta' => 'numeric',
|
|
'ventaEspecial.pago_vales' => 'numeric',
|
|
'ventaEspecial.precio_venta' => 'numeric',
|
|
'ventaEspecial.pago_transferencia' => 'numeric',
|
|
'ventaEspecial.nota_credito' => 'numeric',
|
|
'ventaEspecial.estado_movimiento_id' => 'numeric',
|
|
];
|
|
|
|
public function render()
|
|
{
|
|
$ventas = Movimiento::where([['codigo','like','%'.$this->buscador.'%'],['estado_movimiento_id','=',VentaEspecialController::CODIGO_VENTA_ESPECIAL]])->paginate(10);
|
|
return view('venta.venta-especial',[
|
|
'ventas' => $ventas,
|
|
]);
|
|
}
|
|
|
|
public function updatingBuscador()
|
|
{
|
|
$this->resetPage();
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->clearInputs();
|
|
$this->ventaEspecial = new Movimiento([
|
|
'codigo' => 0,
|
|
'pago_efectivo' => 0,
|
|
'pago_tarjeta' => 0,
|
|
'pago_vales' => 0,
|
|
'pago_transferencia' => 0,
|
|
'nota_credito' => 0,
|
|
'precio_venta' => 0,
|
|
'estado_movimiento_id' => VentaEspecialController::CODIGO_VENTA_ESPECIAL,
|
|
|
|
]);
|
|
$this->showModal();
|
|
}
|
|
|
|
public function liquidar(Movimiento $venta)
|
|
{
|
|
$this->ventaAnterior = $venta;
|
|
|
|
$this->ventaEspecial = new Movimiento([
|
|
'codigo' => $venta->codigo,
|
|
'pago_efectivo' => 0,
|
|
'pago_tarjeta' => 0,
|
|
'pago_vales' => 0,
|
|
'pago_transferencia' => 0,
|
|
'nota_credito' => 0,
|
|
'precio_venta' => $this->ventaAnterior->faltante(),
|
|
'estado_movimiento_id' => VentaEspecialController::CODIGO_VENTA_ESPECIAL,
|
|
]);
|
|
$this->total_pagar = $this->calcularTotalPagar($venta);
|
|
$this->abonado = $venta->precio_venta - $this->total_pagar;
|
|
|
|
$this->updated();
|
|
|
|
$this->showModalLiquidar();
|
|
}
|
|
|
|
public function updated()
|
|
{
|
|
if(!isset($this->ventaAnterior) || !isset($this->ventaEspecial)) return;
|
|
|
|
if($this->ventaEspecial->cambio()>0)
|
|
{
|
|
$this->cambio = $this->ventaEspecial->cambio();
|
|
$this->faltante = 0;
|
|
}
|
|
else
|
|
{
|
|
$this->faltante = $this->ventaEspecial->faltante();
|
|
$this->cambio = 0;
|
|
}
|
|
}
|
|
|
|
public function calcularTotalPagar($venta)
|
|
{
|
|
return ((float)$venta->precio_venta - ((float)$venta->pago_efectivo +
|
|
(float)$venta->pago_tarjeta + (float)$venta->pago_vales +
|
|
(float)$venta->pago_transferencia + (float)$venta->nota_credito));
|
|
}
|
|
|
|
|
|
public function save()
|
|
{
|
|
$this->validate([
|
|
'ventaEspecial.pago_efectivo' => 'numeric',
|
|
'ventaEspecial.pago_tarjeta' => 'numeric',
|
|
'ventaEspecial.pago_vales' => 'numeric',
|
|
'ventaEspecial.pago_transferencia' => 'numeric',
|
|
'ventaEspecial.nota_credito' => 'numeric',
|
|
'ventaEspecial.precio_venta' => 'numeric|gte:2',
|
|
'ventaEspecial.estado_movimiento_id' => 'numeric',
|
|
]);
|
|
|
|
$this->ventaEspecial->user_id = Auth::user()->id;
|
|
|
|
if($this->calcularTotalPagar($this->ventaEspecial)<=0)
|
|
{
|
|
$this->ventaEspecial->is_liquidado = true;
|
|
}
|
|
$this->ventaEspecial->save();
|
|
|
|
session()->flash('message',"La venta especial se ha registrado correctamente!");
|
|
|
|
$this->clearInputs();
|
|
$this->closeModal();
|
|
}
|
|
|
|
public function saveLiquidar()
|
|
{
|
|
$this->validate([
|
|
'ventaEspecial.pago_efectivo' => 'numeric',
|
|
'ventaEspecial.pago_tarjeta' => 'numeric',
|
|
'ventaEspecial.pago_vales' => 'numeric',
|
|
'ventaEspecial.pago_transferencia' => 'numeric',
|
|
'ventaEspecial.nota_credito' => 'numeric',
|
|
'ventaEspecial.precio_venta' => 'numeric|gte:2',
|
|
'ventaEspecial.estado_movimiento_id' => 'numeric',
|
|
]);
|
|
|
|
|
|
if(($this->calcularTotalPagar($this->ventaEspecial) - $this->ventaAnterior->totalAbono()) >0){
|
|
session()->flash("message_abono","Debes liquidar la deuda para cerrarla.");
|
|
return;
|
|
}
|
|
if($this->ventaEspecial->isTotalMayorCambio())
|
|
{
|
|
session()->flash("error","Pago diferente al efectivo no pueden superar el total de venta.");
|
|
return;
|
|
}
|
|
|
|
$this->ventaAnterior->is_liquidado = true;
|
|
$this->ventaAnterior->save();
|
|
|
|
$this->ventaEspecial->user_id = Auth::user()->id;
|
|
$this->ventaEspecial->is_liquidado = true;
|
|
|
|
|
|
$this->ventaEspecial->ajusteCambio();
|
|
$this->ventaEspecial->precio_venta = $this->ventaAnterior->precio_venta;
|
|
|
|
$this->ventaEspecial->save();
|
|
|
|
session()->flash('message',"La venta se ha registrado correctamente!");
|
|
|
|
$this->clearInputs();
|
|
$this->closeModalLiquidar();
|
|
}
|
|
|
|
public function editDate(Movimiento $movimiento)
|
|
{
|
|
$this->ventaEspecial = $movimiento;
|
|
$this->showModal('modalDate');
|
|
}
|
|
|
|
public function saveDate()
|
|
{
|
|
$this->validate([
|
|
'created_at' => 'required',
|
|
]);
|
|
|
|
$this->ventaEspecial->created_at = $this->created_at;
|
|
$this->ventaEspecial->save();
|
|
session()->flash('message',"La fecha ha sido cambiada correctamente.");
|
|
|
|
$this->clearInputs();
|
|
$this->closeModal('modalDate');
|
|
}
|
|
|
|
public function delete(Movimiento $venta)
|
|
{
|
|
|
|
if(auth()->user()->hasRole('admin'))
|
|
{
|
|
$venta->delete();
|
|
session()->flash('message',"La venta especial se ha eliminado del sistema.");
|
|
}
|
|
}
|
|
|
|
|
|
public function showModal($modal = "modal")
|
|
{
|
|
$this->{$modal} = true;
|
|
}
|
|
|
|
public function closeModal($modal = "modal")
|
|
{
|
|
$this->{$modal} = false;
|
|
}
|
|
|
|
public function showModalLiquidar()
|
|
{
|
|
$this->modalLiquidar = true;
|
|
}
|
|
|
|
public function closeModalLiquidar()
|
|
{
|
|
$this->modalLiquidar = false;
|
|
}
|
|
|
|
public function clearInputs()
|
|
{
|
|
$this->reset();
|
|
}
|
|
|
|
}
|