Files
intranet/app/Controller/Ventas.php

624 lines
18 KiB
PHP
Raw Normal View History

2020-12-01 17:23:13 -03:00
<?php
namespace App\Controller;
use Carbon\Carbon;
use App\Definition\Controller;
use Incoviba\old\Common\Banco;
use Incoviba\old\Common\Direccion;
use Incoviba\old\Common\Region;
use Incoviba\old\Proyecto\Agente;
use Incoviba\old\Proyecto\Proyecto;
use Incoviba\old\Venta\BonoPie;
use Incoviba\old\Venta\Credito;
use Incoviba\old\Venta\EstadoVenta;
use Incoviba\old\Venta\Pago;
use Incoviba\old\Venta\Pie;
use Incoviba\old\Venta\Promocion;
use Incoviba\old\Venta\PromocionVenta;
use Incoviba\old\Venta\Propiedad;
use Incoviba\old\Venta\PropiedadUnidad;
use Incoviba\old\Venta\Propietario;
use Incoviba\old\Venta\TipoEstadoVenta;
use Incoviba\old\Venta\Unidad;
use Incoviba\old\Venta\Venta;
class Ventas
{
use Controller;
protected static function setDefault()
{
self::$default = self::list();
}
public static function list()
{
$proyecto = get('proyecto');
if ($proyecto == null) {
return self::listProyectos();
}
$proyecto = model(Proyecto::class)->findOne($proyecto);
$ventas = $proyecto->ventas();
self::sort($ventas);
return view('ventas.list', compact('proyecto', 'ventas'));
}
protected static function sort(&$ventas)
{
$sort = get('sort');
if ($sort == null) {
$sort = 'departamento';
}
$direction = get('sort_dir');
if ($direction == null) {
$direction = 1;
}
switch ($sort) {
case 'departamento':
usort($ventas, function($a, $b) use ($direction) {
return ($a->propiedad()->unidad()->descripcion - $b->propiedad()->unidad()->descripcion) * $direction;
});
break;
case 'propietario':
usort($ventas, function($a, $b) use ($direction) {
$pa = trim($a->propietario()->nombreCompleto(true), ', ');
$pb = trim($b->propietario()->nombreCompleto(true), ', ');
return $direction * strcasecmp($pa, $pb);
});
break;
case 'valor_uf':
usort($ventas, function($a, $b) use ($direction) {
return $direction * ($a->valor_uf - $b->valor_uf);
});
break;
case 'uf_m2':
usort($ventas, function($a, $b) use ($direction) {
return $direction * ($a->uf_m2() - $b->uf_m2());
});
break;
case 'fecha_venta':
usort($ventas, function($a, $b) use ($direction) {
return ($a->fecha()->timestamp - $b->fecha()->timestamp) * $direction;
});
break;
}
if ($direction == 'desc') {
$ventas = array_reverse($ventas);
}
}
public static function listProyectos()
{
$proyectos = model(Proyecto::class)
->select('proyecto.*')
->join('estado_proyecto', ['estado.proyecto', '=', 'proyecto.id'], 'estado')
->join('tipo_estado_proyecto', ['tipo.id', '=', 'estado.estado'], 'tipo')
->join('etapa_proyecto', ['etapa.id', '=', 'tipo.etapa'], 'etapa')
->whereGte('etapa.orden', 4)
->orderByAsc('proyecto.descripcion')
->groupBy('proyecto.id')
->findMany();
echo view('ventas.proyectos', compact('proyectos'));
}
public static function show()
{
$id = get('venta');
$venta = model(Venta::class)->findOne($id);
return view('ventas.show', compact('venta'));
}
public static function new()
{
$proyectos = model(Proyecto::class)
->select('proyecto.*')
->join('estado_proyecto', ['estado.proyecto', '=', 'proyecto.id'], 'estado')
->join('tipo_estado_proyecto', ['tipo.id', '=', 'estado.estado'], 'tipo')
->join('etapa_proyecto', ['etapa.id', '=', 'tipo.etapa'], 'etapa')
->whereGte('etapa.orden', 3)
->orderByAsc('proyecto.descripcion')
->groupBy('proyecto.id')
->findMany();
$regiones = model(Region::class)->order_by_asc('numeracion')->findMany();
return view('ventas.add', compact('proyectos', 'regiones'));
}
public static function agregar()
{
$f = Carbon::createFromDate(post('year'), post('month'), post('day'), config('app.timezone'));
$t = Carbon::today(config('app.timezone'));
$uf = uf($f);
$calle = trim(post('calle'));
$numero = post('numero');
$extra = trim(post('extra'));
$comuna = post('comuna');
$direccion = model(Direccion::class)
->where('calle', $calle)
->where('numero', $numero)
->where('extra', $extra)
->where('comuna', $comuna)
->findOne();
if (!$direccion) {
$direccion = model(Direccion::class)->create();
$direccion->calle = $calle;
$direccion->numero = $numero;
$direccion->extra = $extra;
$direccion->comuna = $comuna;
$direccion->save();
}
list($rut, $dv) = explode('-', str_replace('.', '', post('rut')));
$propietario = model(Propietario::class)->where('rut', $rut)->findOne();
if (!$propietario) {
$propietario = model(Propietario::class)->create();
$propietario->rut = $rut;
$propietario->dv = $dv;
$propietario->nombres = trim(post('nombres'));
$propietario->apellido_paterno = trim(post('paterno'));
$propietario->apellido_materno = trim(post('materno'));
$propietario->direccion = $direccion->id;
if (post('otro') != null) {
$propietario->otro = 1;
}
$propietario->save();
}
$unis = json_decode(post('unidades'));
$id_principal = array_shift($unis);
$principal = model(Unidad::class)->findOne(post('unidad' . $id_principal));
$propiedad = model(Propiedad::class)
->select('propiedad.*')
->join('unidad', ['unidad.id', '=', 'propiedad.unidad_principal'])
->where('propiedad.unidad_principal', $principal->id)
->where('unidad.proyecto', post('proyecto'))
->orderByDesc('propiedad.id')
->findOne();
// Revisar si existe la propiedad y si está vigente.
if (!$propiedad or ($propiedad->venta() and $propiedad->venta()->estado() and $propiedad->venta()->estado()->tipo()->descripcion != 'vigente')) {
if (!$propiedad) {
$propiedad = model(Propiedad::class)->create();
}
$propiedad->unidad_principal = $principal->id;
$propiedad->save();
$data = [
'propiedad' => $propiedad->id,
'unidad' => $principal->id,
'principal' => 1
];
$pu = model(PropiedadUnidad::class)->create($data);
$pu->save();
foreach ($unis as $id_unidad) {
$data = [
'propiedad' => $propiedad->id,
'unidad' => post('unidad' . $id_unidad),
'principal' => 0
];
$pu = model(PropiedadUnidad::class)->create($data);
$pu->save();
}
/*$ests = [];
$bods = [];
foreach ($unis as $id_unidad) {
$unidad = model(Unidad::class)->findOne(post('unidad' . $id_unidad));
if ($unidad->tipo == 2) {
$ests []= $unidad->id;
}
if ($unidad->tipo == 3) {
$bods []= $unidad->id;
}
}
$propiedad->estacionamientos = implode(';', $ests);
$propiedad->bodegas = implode(';', $bods);
$propiedad->save();*/
} elseif ($propiedad->venta() and $propiedad->venta()->estado()->tipo()->descripcion == 'vigente') {
// Existe la propiedad en este proyecto y está vigente. Error, no se debiese vender si está vigente.
throw new \Exception('Existe la propiedad en este proyecto y está vigente. Error, no se debiese vender si está vigente.');
}
$venta = model(Venta::class)->create();
$venta->propietario = $propietario->rut;
$venta->propiedad = $propiedad->id;
if (post('pie')) {
$pie = model(Pie::class)->create();
$pie->valor = post('pie');
$pie->fecha = $f->format('Y-m-d');
$pie->cuotas = post('cuotas');
$pie->uf = $uf->uf->value;
$pie->save();
$venta->pie = $pie->id;
}
if (post('bono_pie')) {
$bono = model(BonoPie::class)->create();
$bono->valor = post('bono_pie');
$pago = model(Pago::class)->create();
$pago->fecha = $f->format('Y-m-d');
$pago->uf = $uf->uf->value;
$pago->valor = $bono->valor * $uf->uf->value;
$pago->tipo = 8;
$pago->new();
$bono->pago = $pago->id;
$bono->save();
$venta->bono_pie = $bono->id;
}
if (post('credito')) {
$pago = model(Pago::class)->create();
$pago->fecha = $f->format('Y-m-d');
$pago->uf = $uf->uf->value;
$pago->valor = post('credito') * $uf->uf->value;
$pago->tipo = 2;
$pago->new();
$credito = model(Credito::class)->create();
$credito->pago = $pago->id;
$credito->save();
$venta->credito = $credito->id;
}
$venta->fecha = $f->format('Y-m-d');
$venta->valor_uf = post('valor');
$venta->fecha_ingreso = $t->format('Y-m-d');
if (post('operador') != 0) {
$venta->agente = post('operador');
}
$venta->uf = $uf->uf->value;
$venta->new();
if (post('promociones') != 0) {
$promos = json_decode(post('promociones'));
foreach ($promos as $id_promo) {
$promocion = model(Promocion::class)->findOne(post('promocion' . $id_promo));
$promo = model(PromocionVenta::class)->create();
$promo->promocion = $promocion->id;
$promo->venta = $venta->id;
$promo->valor = post('promo' . $id_promo);
$promo->save();
}
}
header('Location: ' . url('', ['p' => 'ventas', 'a' => 'show', 'venta' => $venta->id]));
}
public static function edit()
{
$id = get('venta');
$venta = model(Venta::class)->findOne($id);
$proyectos = model(Proyecto::class)->order_by_asc('descripcion')->findMany();
$regiones = model(Region::class)->order_by_asc('numeracion')->findMany();
return view('ventas.edit', compact('venta', 'proyectos', 'regiones'));
}
public static function editar()
{
$id = get('venta');
$venta = model(Venta::class)->findOne($id);
$f = Carbon::createFromDate(post('year'), post('month'), post('day'), config('app.timezone'));
$uf = uf($f);
$valor = correctNumber(post('valor'));
$change = false;
if ($venta->fecha != $f->format('Y-m-d')) {
$venta->fecha = $f->format('Y-m-d');
$venta->uf = $uf->uf->value;
$change = true;
}
if ($venta->valor_uf != $valor) {
$venta->valor_uf = $valor;
$change = true;
}
if ($change) {
$venta->save();
}
$direccion = $venta->propietario()->direccion();
$calle = post('calle');
$numero = post('numero');
$extra = post('extra');
$comuna = post('comuna');
$change = false;
if ($direccion->calle != $calle) {
$direccion->calle = $calle;
$change = true;
}
if ($direccion->numero != $numero) {
$direccion->numero = $numero;
$change = true;
}
if ($direccion->extra != $extra) {
$direccion->extra = $extra;
$change = true;
}
if ($direccion->comuna != $comuna) {
$direccion->comuna = $comuna;
$change = true;
}
if ($change) {
$direccion->save();
}
$propietario = $venta->propietario();
list($rut, $dv) = explode('-', str_replace('.', '', post('rut')));
$nombres = post('nombres');
$paterno = post('paterno');
$materno = post('materno');
$change = false;
if ($propietario->rut != $rut) {
$propietario->rut = $rut;
$propietario->dv = $dv;
$venta->propietario = $rut;
$venta->save();
$change = true;
}
if ($propietario->nombres != $nombres) {
$propietario->nombres = $nombres;
$change = true;
}
if ($propietario->apellido_paterno != $paterno) {
$propietario->apellido_paterno = $paterno;
$change = true;
}
if ($propietario->apellido_materno != $materno) {
$propietario->apellido_materno = $materno;
$change = true;
}
if ($change) {
$propietario->save();
}
$unidades = json_decode(post('unidades'));
if (count($unidades) > 0) {
$propiedad = $venta->propiedad();
$ests = explode(';', $propiedad->estacionamientos);
$bods = explode(';', $propiedad->bodegas);
$change = false;
foreach ($unidades as $n) {
$id = post('unidad' . $n);
$unidad = model(Unidad::class)->findOne($id);
if ($unidad->tipo == 1 and $propiedad->unidad_principal != $unidad->id) {
$propiedad->unidad_principal = $unidad->id;
$change = true;
}
if ($unidad->tipo == 2 and array_search($unidad->id, $ests) === false) {
$ests []= $unidad->id;
}
if ($unidad->tipo == 3 and array_search($unidad->id, $bods) === false) {
$bods []= $unidad->id;
}
}
$ests = implode(';', $ests);
$bods = implode(';', $bods);
if ($propiedad->estacionamientos != $ests) {
$propiedad->estacionamientos = $ests;
$change = true;
}
if ($propiedad->bodegas != $bods) {
$propiedad->bodegas = $bods;
$change = true;
}
if ($change) {
$propiedad->save();
}
}
if (post('pie')) {
$pie = $venta->pie();
$valor = correctNumber(post('pie'));
$cuotas = post('cuotas');
$change = false;
if ($pie->valor != $valor) {
$pie->valor = $valor;
$change = true;
}
if ($pie->cuotas != $cuotas) {
$pie->cuotas = $cuotas;
$change = true;
}
if ($change) {
$pie->save();
}
}
$credito = $venta->credito();
$valor = post('credito');
header('Location: ' . url('', ['p' => 'ventas', 'a' => 'show', 'venta' => $venta->id]));
}
public static function desistir()
{
$id = get('venta');
$venta = model(Venta::class)->findOne($id);
return view('ventas.desist', compact('venta'));
}
public static function desistiendo()
{
$id = get('venta');
$venta = model(Venta::class)->findOne($id);
$f = Carbon::createFromDate(post('year'), post('month'), post('day'), config('app.timezone'));
$valor = correctNumber(post('pago'));
$uf = uf($f);
$venta->estado = 0;
$tipo = model(TipoEstadoVenta::class)->where('descripcion', 'desistida')->findOne();
$data = [
'venta' => $venta->id,
'estado' => $tipo->id,
'fecha' => $f->format('Y-m-d')
];
$estado = model(EstadoVenta::class)->create($data);
$propiedad = $venta->propiedad();
$propiedad->estado = 0;
$pago = model(Pago::class)->create();
$pago->fecha = $f->format('Y-m-d');
$pago->valor = (double) $valor;
$pago->uf = $uf->uf->value;
$pago->tipo = 1;
$pago->new();
$propiedad->save();
$estado->save();
$venta->resciliacion = $pago->id;
$venta->save();
header('Location: ' . url('', ['p' => 'ventas', 'a' => 'show', 'venta' => $venta->id]));
}
public static function ceder()
{
$id = get('venta');
$venta = model(Venta::class)->findOne($id);
return view('ventas.ceder', compact('venta'));
}
public static function cediendo()
{
$id = get('venta');
$venta = model(Venta::class)->findOne($id);
2020-12-14 17:33:53 -03:00
$nueva_venta = model(Venta::class)->create();
2020-12-01 17:23:13 -03:00
$f = Carbon::createFromDate(post('year'), post('month'), post('day'), config('app.timezone'));
2020-12-14 17:33:53 -03:00
list($rut, $dv) = explode('-', str_replace('.', '', post('rut')));
$propietario = model(Propietario::class)->where('rut', $rut)->findOne();
if (!$propietario) {
$propietario = model(Propietario::class)->create();
$propietario->rut = $rut;
$propietario->dv = $dv;
$propietario->nombres = trim(post('nombres'));
$propietario->apellido_paterno = trim(post('paterno'));
$propietario->apellido_materno = trim(post('materno'));
$propietario->direccion = $direccion->id;
if (post('otro') != null) {
$propietario->otro = 1;
}
$propietario->save();
}
$nueva_venta->fecha_ingreso = $f->format();
$nueva_venta->estado = 1;
$cols = [
'propiedad',
'pie',
'bono_pie',
'credito',
'escritura',
'subsidio',
'fecha',
'valor_uf',
'agente',
'uf'
];
foreach ($cols as $col) {
$nueva_venta->{$col} = $venta->{$col};
}
$nueva_venta->new();
2020-12-01 17:23:13 -03:00
$venta->estado = -1;
$tipo = model(TipoEstadoVenta::class)->where('descripcion', 'cedida')->findOne();
$data = [
'venta' => $venta->id,
'estado' => $tipo->id,
'fecha' => $f->format('Y-m-d')
];
$estado = model(EstadoVenta::class)->create($data);
$estado->save();
2020-12-14 17:33:53 -03:00
2020-12-01 17:23:13 -03:00
$venta->save();
header('Location: ' . url('', ['p' => 'ventas', 'a' => 'show', 'venta' => $venta->id]));
}
public static function entregar()
{
$id = get('venta');
$venta = model(Venta::class)->findOne($id);
return view('ventas.entregar', compact('venta'));
}
public static function consolidacion()
{
if (get('proyecto')) {
$id_proyecto = get('proyecto');
$proyecto = model(Proyecto::class)->findOne($id_proyecto);
$ventas = $proyecto->ventas();
set_time_limit(count($ventas));
$f = Carbon::today(config('app.timezone'));
setlocale(LC_TIME, 'es');
return view('ventas.consolidacion.show', compact('ventas', 'f'));
} else {
$proyectos = model(Proyecto::class)->order_by_asc('descripcion')->find_many();
return view('ventas.consolidacion.proyectos', compact('proyectos'));
}
}
public static function devolucion()
{
$id = get('venta');
$venta = model(Venta::class)->findOne($id);
$uf = uf(Carbon::now(config('app.config')))->uf->value;
$valor = round($venta->saldo() * $uf);
return view('ventas.devolucion', compact('venta', 'valor', 'uf'));
}
public static function devolver()
{
$id = get('venta');
$venta = model(Venta::class)->findOne($id);
$f = Carbon::createFromDate(post('year'), post('month'), post('day'), config('app.timezone'));
$uf = uf($f);
$valor = correctNumber(post('valor'));
$data = [
'fecha' => $f->format('Y-m-d'),
'valor' => $valor,
'tipo' => 1,
'uf' => $uf->uf->value,
'identificador' => post('identificador'),
'banco' => 0
];
$banco = model(Banco::class)->where('nombre', post('banco'))->findOne();
if ($banco) {
$data['banco'] = $banco->id;
}
$pago = model(Pago::class)->create($data);
$pago->newPagado();
$venta->devolucion = $pago->id;
$venta->save();
header('Location: ' . url('', ['p' => 'ventas', 'a' => 'show', 'venta' => $venta->id]));
}
public static function resciliaciones()
{
$resciliaciones = model(Venta::class)->where('estado', 0)->findMany();
return view('ventas.resciliaciones', compact('resciliaciones'));
}
public static function firmar()
{
$venta = \model(Venta::class)->findOne(get('venta'));
return view('ventas.firmar', compact('venta'));
}
public static function firmando()
{
$venta = \model(Venta::class)->findOne(get('venta'));
$f = Carbon::createFromDate(post('year'), post('month'), post('day'), config('app.timezone'));
$venta->firmar($f);
header('Location: ' . nUrl('ventas', 'show', ['venta' => $venta->id]));
}
public static function archivar()
{
$venta = \model(Venta::class)->findOne(get('venta'));
return view('ventas.archivar', compact('venta'));
}
public static function archivando()
{
$venta = \model(Venta::class)->findOne(get('venta'));
$f = Carbon::createFromDate(post('year'), post('month'), post('day'), config('app.timezone'));
$venta->archivar($f);
header('Location: ' . nUrl('ventas', 'show', ['venta' => $venta->id]));
}
}
?>