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.cierres.add', compact('proyectos', 'regiones')); } public static function agregar() { $f = Carbon::createFromDate(post('year'), post('month'), post('day'), config('app.timezone')); $id_proyecto = post('proyecto'); $proyecto = model(Proyecto::class)->findOne($id_proyecto); $id_agente = post('agente'); $agente = model(Agente::class)->findOne($id_agente); $direccion = model(Direccion::class) ->where('calle', post('calle')) ->where('numero', post('numero')) ->where('extra', post('extra')) ->where('comuna', post('comuna')) ->findOne(); if (!$direccion) { $data = [ 'calle' => post('calle'), 'numero' => post('numero'), 'extra' => post('extra'), 'comuna' => post('comuna') ]; $direccion = model(Direccion::class)->create($data); $direccion->save(); } list($rut, $dv) = explode('-', str_replace('.', '', post('rut'))); $propietario = model(Propietario::class)->findOne($rut); if (!$propietario) { $data = [ 'rut' => $rut, 'dv' => $dv, 'nombres' => trim(post('nombres')), 'apellido_paterno' => post('paterno'), 'apellido_materno' => post('materno'), 'sexo' => post('sexo'), 'estado_civil' => post('estado_civil'), 'profesion' => post('profesion'), 'direccion' => $direccion->id, 'telefono' => post('codigo_telefono') . post('telefono'), 'email' => post('email') . '@' . post('email_domain'), 'representante' => 0, 'otro' => 0 ]; $propietario = model(Propietario::class)->create($data); $propietario->save(); } $unis = json_decode(post('unidades')); $id_principal = array_shift($unis); $unidad = model(Unidad::class)->findOne(post('unidad' . $id_principal)); $u = model(U::class)->findOne($unidad->id); if (!$u) { $unidad->save(); } $data = [ 'unidad_id' => $unidad->id ]; $reserva = model(Reserva::class)->create($data); $reserva->save(); foreach ($unis as $id_unidad) { $unidad = model(Unidad::class)->findOne(post('unidad' . $id_unidad)); $data = [ 'reserva_id' => $reserva->id, 'unidad_id' => $unidad->id ]; $ur = model(UnidadReserva::class)->create($data); $ur->save(); } $data = [ 'proyecto_id' => $proyecto->id, 'agente_id' => $agente->id, 'propietario_rut' => $propietario->rut, 'reserva_id' => $reserva->id, 'fecha' => $f->format('Y-m-d'), 'valor' => correctNumber(post('valor')), 'pie' => correctNumber(post('pie')), 'credito' => correctNumber(post('credito')), 'estado' => 1 ]; $cierre = model(Cierre::class)->create($data); $cierre->save(); header('Location: ' . url('', ['p' => 'cierres', 'a' => 'list'])); } public static function list() { $proyectos = Cierre::proyectos(); return view('ventas.cierres.list', compact('proyectos')); } public static function show() { $id = get('cierre'); $cierre = model(Cierre::class)->findOne($id); return view('ventas.cierres.show', compact('cierre')); } public static function guardar() { $proyecto = \model(Proyecto::class)->findOne(post('proyecto')); $fecha = Carbon::createFromDate(post('year'), post('month'), post('day'), config('app.timezone')); $unidad = \model(Unidad::class)->findOne(post('departamento')); $relacionado = (post('relacionado') === true) ? true : false; $subrelacionado = (post('subrelacionado') === true) ? true : false; $precio = (float) post('precio') ?: 0; $input = [ 'proyecto' => $proyecto, 'fecha' => $fecha, 'departamento' => $unidad, 'precio' => $precio, 'relacionado' => $relacionado, 'subrelacionado' => $subrelacionado, 'unidades' => [], 'pie' => (float) post('pie') ]; $ebs = 0; if (post('unidades') != '') { $unidades = json_decode(html_entity_decode(post('unidades')), true); foreach ($unidades as $un) { $u = \model(Unidad::class)->findOne($un); $input['unidades'] []= $u; if ($u->precio($fecha) !== false) { $ebs += $u->precio($fecha)->valor; } } } $promo = 0; if (post('promocion') != null) { $promo = (float) post('promocion'); $input['promocion'] = $promo; } $bono = 0; if (post('bono') != null) { $bono = (float) post('bono'); $input['bono'] = $bono; } $operador = 0; if (post('operador') != null) { $operador = ($precio - $bono - $promo) * (float) post('operador') / 100; $input['operador'] = $operador; } $cierre = Cierre::find($proyecto, $unidad, $precio)->findOne(); if ($cierre === false) { $cierre = model(Cierre::class)->create(); $cierre->guardar((object) $input); } $output = ['status' => 'ok', 'cierre' => $cierre->asArray()]; return json_encode($output); } public static function aprobar() { $fecha = Carbon::today(config('app.timezone')); $cierre = model(Cierre::class)->findOne(post('cierre')); if ($cierre->estado()->tipo()->descripcion == "revisado" or $cierre->estado()->tipo()->descripcion == "rechazado") { $cierre->aprobar($fecha); return json_encode(['estado' => 'aprobado']); } return json_encode(['estado' => 'no vigente']); } public static function rechazar() { $fecha = Carbon::today(config('app.timezone')); $cierre = model(Cierre::class)->findOne(post('cierre')); if ($cierre->estado()->tipo()->vigente == 1) { $cierre->rechazar($fecha); return json_encode(['estado' => 'rechazado']); } return json_encode(['estado' => 'no vigente']); } public static function abandonar() { $id = get('cierre'); $cierre = model(Cierre::class)->findOne($id); $tipo = model(TipoEstadoCierre::class)->where('descripcion', 'abandonado')->findOne(); $today = Carbon::today(config('app.timezone')); $data = [ 'cierre' => $cierre->id, 'tipo' => $tipo->id, 'fecha' => $today->format('Y-m-d') ]; $estado = model(EstadoCierre::class)->create($data); $estado->save(); header('Location: ' . url('', ['p' => 'cierres', 'a' => 'list'])); } public static function promesar() { $id = get('cierre'); $cierre = model(Cierre::class)->findOne($id); $tipo = model(TipoEstadoCierre::class)->where('descripcion', 'promesado')->findOne(); $today = Carbon::today(config('app.timezone')); $data = [ 'cierre' => $cierre->id, 'tipo' => $tipo->id, 'fecha' => $today->format('Y-m-d') ]; $estado = model(EstadoCierre::class)->create($data); $estado->save(); header('Location: ' . url('', ['p' => 'cierres', 'a' => 'show', 'cierre' => $cierre->id])); } public static function borrador() { $id = get('cierre'); $cierre = model(Cierre::class)->findOne($id); $borrador = new Borrador($cierre); d($borrador->show()); $borrador->create(); } public static function evalue() { $proyectos = \model(Proyecto::class)->orderByAsc('descripcion')->findMany(); return view('ventas.cierres.evaluar', compact('proyectos')); } public static function evaluar() { $proyecto = \model(Proyecto::class)->findOne(post('proyecto')); $fecha = Carbon::createFromDate(post('year'), post('month'), post('day'), config('app.timezone')); $unidad = \model(Unidad::class)->findOne(post('departamento')); $relacionado = (post('relacionado') === 'true') ? true : false; $subrelacionado = (post('subrelacionado') === 'true') ? true : false; $precio = (float) post('precio') ?: 0; $neto = $precio; $ebs = 0; if (post('unidades') != '') { $unidades = json_decode(html_entity_decode(post('unidades')), true); foreach ($unidades as $un) { $u = \model(Unidad::class)->findOne($un); if ($u->precio($fecha) !== false) { $ebs += $u->precio($fecha)->valor; } } } $promocion = 0; if (post('promocion') != null) { $promocion = (float) post('promocion'); } $bono = 0; if (post('bono') != null) { $bono = (float) post('bono'); } $operador = 0; if (post('operador') != null) { $operador = ($precio - $bono - $promocion) * (float) post('operador') / 100; } $rel = 0; if ($relacionado) { $rel = ($unidad->precio($fecha)->valor) * 6 / 100; } if ($subrelacionado) { $rel = ($unidad->precio($fecha)->valor) * 3 / 100; } $neto = $precio - $bono - $promocion - $operador - $ebs; $output = [ 'unidad' => [ 'tipo' => [ 'nombre' => $unidad->tipologia()->nombre, 'tipologia' => $unidad->tipologia()->tipologia()->descripcion ], 'superficie' => format('m2', $unidad->m2()) . ' m²' ], 'oferta' => [ 'bruto' => format('ufs', $precio, null, true), 'neto' => format('ufs', $neto, null, true), 'uf_m2' => format('ufs', $neto / $unidad->m2('vendible'), null, true) . '/m²', 'fecha' => format('shortDate', $unidad->precio($fecha)->inicio()->fecha()) ], 'lista' => [ 'precio' => format('ufs', $unidad->precio($fecha)->valor, null, true), 'uf_m2' => format('ufs', $unidad->precio($fecha)->valor / $unidad->m2('vendible'), null, true) . '/m²' ], 'precios' => [ 'bruto' => format('ufs', $precio, null, true), 'neto' => format('ufs', $neto, null, true), 'departamento' => format('ufs', $unidad->precio($fecha)->valor, null, true), 'relacionado' => format('ufs', $unidad->precio($fecha)->valor - $rel, null, true), 'fecha' => format('shortDate', $unidad->precio($fecha)->inicio()->fecha()) ], 'uf_m2' => [ 'neto' => format('ufs', $neto / $unidad->m2('vendible'), null, true) . '/m²', 'departamento' => format('ufs', $unidad->precio($fecha)->valor / $unidad->m2('vendible'), null, true) . '/m²', 'relacionado' => format('ufs', ($unidad->precio($fecha)->valor - $rel) / $unidad->m2('vendible'), null, true) . '/²' ], 'evaluacion' => Cierre::evaluar($neto, $unidad, $fecha, $rel), 'estado' => ['id' => 0, 'descripcion' => 'no existe'] ]; if ($rel > 0) { $output ['relacionado'] = [ 'precio' => format('ufs', $unidad->precio($fecha)->valor - $rel, null, true), 'uf_m2' => format('ufs', ($unidad->precio($fecha)->valor - $rel) / $unidad->m2('vendible'), null, true) . '/²' ]; } $estado = Cierre::find($proyecto, $unidad, $precio)->findOne(); if ($estado) { $output['estado'] = [ 'id' => $estado->estado()->tipo()->id, 'cierre' => $estado->id, 'descripcion' => $estado->estado()->tipo()->descripcion, 'fecha' => format('shortDate', $estado->estado()->fecha) ]; } return json_encode($output); } public static function edit() { $cierre = model(Cierre::class)->findOne(get('cierre')); $proyectos = model(Proyecto::class)->findMany(); $regiones = model(Region::class)->findMany(); $valores = model(TipoValorCierre::class)->findMany(); return view('ventas.cierres.edit', compact('cierre', 'proyectos', 'regiones', 'valores')); } public static function do_edit() { $cierre = model(Cierre::class)->findOne(get('cierre')); $data = [ 'calle' => post('calle'), 'numero' => post('numero'), 'extra' => post('extra'), 'comuna' => post('comuna') ]; $direccion = (new Factory(Direccion::class))->where($data)->find(); if (!$direccion) { $direccion = model(Direccion::class)->create($data); $direccion->save(); } if (post('rut') != '') { $data = [ 'rut' => explode('-', str_replace('.', '', post('rut')))[0], ]; $propietario = (new Factory(Propietario::class))->where($data)->find(); if (!$propietario) { $data = array_merge($data, [ 'nombres' => post('nombres'), 'apellido_paterno' => post('paterno'), 'apellido_materno' => post('materno'), 'dv' => (post('rut')) ? explode('-', str_replace('.', '', post('rut')))[1] : '', 'sexo' => post('sexo'), 'estado_civil' => post('estado_civil'), 'profesion' => post('profesion'), 'telefono' => post('codigo_telefono') . post('telefono'), 'email' => post('email') . '@' . post('email_domain'), 'direccion' => $direccion->id ]); $propietario = model(Propietario::class)->create($data); $propietario->save(); } } $f = Carbon::createFromDate(post('year'), post('month'), post('day'), config('app.timezone')); $data = [ 'proyecto' => post('proyecto'), 'precio' => post('precio'), 'fecha' => $f->format('Y-m-d'), 'relacionado' => (post('relacionado')) ? 1 : 0, 'propietario' => (isset($propietario) and $propietario) ? $propietario->rut : 0 ]; foreach ($data as $field => $value) { if ($value != $cierre->$field) { $cierre->$field = $value; } } $cierre->save(); $valores = model(TipoValorCierre::class)->findMany(); foreach ($valores as $valor) { if (post($valor->descripcion) == '') { continue; } if ($cierre->valor($valor->descripcion)) { if ($cierre->valor($valor->descripcion)->valor != post($valor->descripcion)) { $v = $cierre->valor($valor->descripcion); $v->valor = post($valor->descripcion); $v->save(); } continue; } $data = [ 'tipo' => $valor->descripcion, 'valor' => post($valor->descripcion) ]; $cierre->addValor($data); } header('Location: ' . nUrl('cierres', 'show', ['cierre' => $cierre->id])); } } ?>