359 lines
12 KiB
PHP
359 lines
12 KiB
PHP
<?php
|
|
namespace App\Controller;
|
|
|
|
use Carbon\Carbon;
|
|
use App\Definition\Controller;
|
|
use Incoviba\old\Venta\Pago;
|
|
use Incoviba\old\Venta\TipoPago;
|
|
use Incoviba\old\Venta\TipoEstadoPago;
|
|
use Incoviba\old\Common\Banco;
|
|
use Incoviba\old\Venta\Venta;
|
|
use Incoviba\old\Venta\EstadoPago;
|
|
|
|
class Pagos
|
|
{
|
|
use Controller;
|
|
|
|
public static function edit()
|
|
{
|
|
$id = get('pago');
|
|
$asociado = get('asociado');
|
|
$id_asociado = get($asociado);
|
|
|
|
$pago = model(Pago::class)->findOne($id);
|
|
$tipos = model(TipoPago::class)->orderByAsc('descripcion')->findMany();
|
|
$estados = model(TipoEstadoPago::class)->orderByAsc('descripcion')->findMany();
|
|
|
|
return view('ventas.pagos.edit', compact('pago', 'asociado', 'id_asociado', 'tipos', 'estados'));
|
|
}
|
|
public static function editar()
|
|
{
|
|
$id = get('pago');
|
|
$pago = model(Pago::class)->findOne($id);
|
|
|
|
$fp = Carbon::createFromDate(post('year'), post('month'), post('day'), config('app.timezone'));
|
|
$tipo = model(TipoPago::class)->findOne(post('tipo'));
|
|
$valor = correctNumber(post('valor'));
|
|
$banco = model(Banco::class)->where('nombre', post('banco'))->findOne();
|
|
|
|
$fe = Carbon::createFromDate(post('yearestado'), post('monthestado'), post('dayestado'), config('app.timezone'));
|
|
$estado = model(TipoEstadoPago::class)->findOne(post('estado'));
|
|
$uf = uf($fe);
|
|
|
|
$est = $pago->estado();
|
|
if ($est->fecha != $fe->format('Y-m-d')) {
|
|
$est->fecha = $fe->format('Y-m-d');
|
|
$pago->uf = $uf->uf->value;
|
|
}
|
|
if ($est->estado != $estado->id) {
|
|
$est->estado = $estado->id;
|
|
}
|
|
|
|
if ($pago->fecha != $fp->format('Y-m-d')) {
|
|
$pago->fecha = $fp->format('Y-m-d');
|
|
}
|
|
if ($pago->tipo != $tipo->id) {
|
|
$pago->tipo = $tipo->id;
|
|
}
|
|
if ($pago->valor != $valor) {
|
|
$pago->valor = $valor;
|
|
}
|
|
if ($pago->identificador != post('identificador')) {
|
|
$pago->identificador = post('identificador');
|
|
}
|
|
if ($pago->pagador != post('pagador')) {
|
|
$pago->pagador = post('pagador');
|
|
}
|
|
if ($pago->banco != $banco->id) {
|
|
$pago->banco = $banco->id;
|
|
}
|
|
|
|
$est->save();
|
|
$pago->save();
|
|
header('Location: ' . url('', ['p' => get('asociado') . 's', 'a' => 'show', get('asociado') => get(get('asociado'))]));
|
|
}
|
|
public static function pendientes()
|
|
{
|
|
$ventas = model(Venta::class)
|
|
->select('venta.*')
|
|
->rawJoin('JOIN (SELECT e1.* FROM estado_venta e1 JOIN (SELECT venta, MAX(id) AS id FROM estado_venta GROUP BY venta) e0 ON e0.id = e1.id)', ['ev.venta', '=', 'venta.id'], 'ev')
|
|
->join('tipo_estado_venta', ['te.id', '=', 'ev.estado'], 'te')
|
|
->join('propiedad', ['propiedad.id', '=', 'venta.propiedad'])
|
|
->join('unidad', ['unidad.id', '=', 'propiedad.unidad_principal'])
|
|
->join('proyecto', ['proyecto.id', '=', 'unidad.proyecto'])
|
|
->where('te.activa', 1)
|
|
->orderByAsc('proyecto.descripcion')
|
|
->orderByExpr('LPAD(unidad.descripcion, 4, "0")')
|
|
->findMany();
|
|
$n = 30;
|
|
$mod = floor(count($ventas) / $n);
|
|
$i_rest = count($ventas) - count($ventas) % $mod + 1;
|
|
$rest = count($ventas) - $i_rest;
|
|
$lots = (object) ['size' => $mod, 'N' => $n, 'rest' => (object) [
|
|
'size' => $rest,
|
|
'start' => $i_rest
|
|
]
|
|
];
|
|
return view('ventas.pagos.pendientes', compact('ventas', 'lots'));
|
|
}
|
|
public static function para_pendientes()
|
|
{
|
|
$timezone = config('app.timezone');
|
|
$today = Carbon::today($timezone);
|
|
$days = [];
|
|
$fechas = [];
|
|
for ($i = $today->copy()->subDays(15); $i <= $today->copy()->addDays(15); $i = $i->copy()->addDay()) {
|
|
$days []= $i->format('Y-m-d');
|
|
$fechas []= $i->format('d-m-Y');
|
|
}
|
|
$pagos_pendientes = model(Pago::class)
|
|
->select('estado_pago.fecha')
|
|
->selectExpr('COUNT(pago.id)', 'cantidad')
|
|
->join('cuota', ['cuota.pago', '=', 'pago.id'])
|
|
->join('venta', ['venta.pie', '=', 'cuota.pie'])
|
|
->filter('filterEstado')
|
|
->where('estado_pago.estado', 0)
|
|
->where('venta.estado', 1)
|
|
->whereGte('estado_pago.fecha', $today->copy()->subDays(15)->format('Y-m-d'))
|
|
->whereLte('estado_pago.fecha', $today->copy()->addDays(15)->format('Y-m-d'))
|
|
->orderByAsc('estado_pago.fecha')
|
|
->groupBy('estado_pago.fecha')
|
|
->findMany();
|
|
$valores = array_fill(0, count($days), 0);
|
|
$anteriores = model(Pago::class)
|
|
->join('cuota', ['cuota.pago', '=', 'pago.id'])
|
|
->join('venta', ['venta.pie', '=', 'cuota.pie'])
|
|
->filter('filterEstado')
|
|
->where('estado_pago.estado', 0)
|
|
->where('venta.estado', 1)
|
|
->whereLt('estado_pago.fecha', $today->copy()->subDays(15)->format('Y-m-d'))
|
|
->count();
|
|
foreach ($pagos_pendientes as $pago) {
|
|
$valores[array_search($pago->fecha()->format('Y-m-d'), $days)] = $pago->cantidad;
|
|
}
|
|
$acum = [];
|
|
$sum = 0;
|
|
foreach ($valores as $valor) {
|
|
$sum += $valor;
|
|
$acum []= $sum;
|
|
}
|
|
$t = array_search($today->format('Y-m-d'), $days);
|
|
$color = array_merge(
|
|
array_fill(0, $t, 'red'),
|
|
['blue'],
|
|
array_fill(0, count($days) - $t, 'green')
|
|
);
|
|
$pagos = ['data' => $acum, 'historico' => $anteriores, 'backgroundColor' => $color];
|
|
$abonos_pendientes = model(Pago::class)
|
|
->select('estado_pago.fecha')
|
|
->selectExpr('COUNT(pago.id)', 'cantidad')
|
|
->filter('filterEstado')
|
|
->where('estado_pago.estado', 1)
|
|
->whereGte('estado_pago.fecha', $today->copy()->subDays(15)->format('Y-m-d'))
|
|
->whereLt('estado_pago.fecha', $today->copy()->format('Y-m-d'))
|
|
->orderByAsc('estado_pago.fecha')
|
|
->groupBy('estado_pago.fecha')
|
|
->findMany();
|
|
$anteriores = model(Pago::class)
|
|
->join('cuota', ['cuota.pago', '=', 'pago.id'])
|
|
->join('venta', ['venta.pie', '=', 'cuota.pie'])
|
|
->filter('filterEstado')
|
|
->where('estado_pago.estado', 1)
|
|
->where('venta.estado', 1)
|
|
->whereLt('estado_pago.fecha', $today->copy()->subDays(15)->format('Y-m-d'))
|
|
->count();
|
|
$valores = array_fill(0, count($days), 0);
|
|
foreach ($abonos_pendientes as $pago) {
|
|
$valores[array_search($pago->fecha()->format('Y-m-d'), $days)] = $pago->cantidad;
|
|
}
|
|
$acum = [];
|
|
$sum = 0;
|
|
foreach ($valores as $valor) {
|
|
$sum += $valor;
|
|
$acum []= $sum;
|
|
}
|
|
$color = array_fill(0, count($pagos), 'rgb(200, 0, 0)');
|
|
$abonos = ['data' => $acum, 'historico' => $anteriores, 'backgroundColor' => $color];
|
|
$output = ['count' => count($days), 'fechas' => $fechas, 'pagos' => $pagos, 'abonos' => $abonos];
|
|
echo json_encode($output);
|
|
}
|
|
public static function para_abonar()
|
|
{
|
|
$ids = json_decode(post('ids'));
|
|
//$id = get('id');
|
|
function checkPago(&$pagos, $tipo, $pago) {
|
|
if (!$pago) {
|
|
return;
|
|
}
|
|
if (!$pago->estado()) {
|
|
$pagos []= [
|
|
'tipo' => $tipo,
|
|
'pago' => $pago->asArray(),
|
|
'estado' => -1
|
|
];
|
|
return;
|
|
}
|
|
if ($pago->estado()->tipo()->descripcion == 'depositado') {
|
|
$pagos []= [
|
|
'tipo' => $tipo,
|
|
'pago' => $pago->asArray(),
|
|
'fecha' => format('shortDate', $pago->estado()->fecha),
|
|
'valor' => format('pesos', $pago->valor, true),
|
|
'estado' => 1
|
|
];
|
|
}
|
|
}
|
|
$output = [];
|
|
foreach ($ids as $id) {
|
|
$venta = model(Venta::class)->findOne($id);
|
|
if ($venta->estado()->tipo()->activa == 0) {
|
|
$output []= ['status' => -1, 'venta' => $venta->id];
|
|
continue;
|
|
}
|
|
$pagos = [];
|
|
if ($venta->pie()) {
|
|
foreach ($venta->pie()->cuotas() as $cuota) {
|
|
checkPago($pagos, 'Pie', $cuota->pago());
|
|
}
|
|
if ($venta->pie()->reajuste()) {
|
|
checkPago($pagos, 'Reajuste', $venta->pie()->reajuste());
|
|
}
|
|
}
|
|
if ($venta->credito()) {
|
|
checkPago($pagos, 'Credito', $venta->credito()->pago());
|
|
}
|
|
if ($venta->escritura()) {
|
|
checkPago($pagos, 'Abono Escritura', $venta->escritura()->pago());
|
|
}
|
|
if ($venta->subsidio()) {
|
|
checkPago($pagos, 'Subsidio', $venta->subsidio()->subsidio());
|
|
checkPago($pagos, 'Ahorro', $venta->subsidio()->pago());
|
|
}
|
|
if (count($pagos) <= 0) {
|
|
$output []= ['status' => -1, 'venta' => $venta->id];
|
|
continue;
|
|
}
|
|
$output []= [
|
|
'status' => 1,
|
|
'proyecto' => $venta->proyecto()->descripcion,
|
|
'venta' => $venta->id,
|
|
'propietario' => $venta->propietario()->nombreCompleto(),
|
|
'departamento' => $venta->unidad()->descripcion,
|
|
'pagos' => $pagos
|
|
];
|
|
}
|
|
return json_encode($output);
|
|
}
|
|
public static function rebotes()
|
|
{
|
|
$ids = json_decode(post('ids'));
|
|
$response = [];
|
|
foreach ($ids as $id) {
|
|
//$id = get('id');
|
|
$venta = model(Venta::class)->findOne($id);
|
|
$rebotes = $venta->pagos(-1);
|
|
if (count($rebotes) < 1) {
|
|
$response []= ['status' => -1, 'venta' => $venta->id];
|
|
continue;
|
|
}
|
|
usort($rebotes, function($a, $b) {
|
|
return $b->estado()->fecha()->diffInDays($a->estado()->fecha(), false);
|
|
});
|
|
|
|
$output = [];
|
|
$textos = [];
|
|
foreach ($rebotes as $rebote) {
|
|
$fuente = $rebote->fuente()[0];
|
|
$text = '<td>' . ucwords(str_replace('_', ' ', $fuente->tipo)) . '</td>';
|
|
$info = ['tipo' => ucwords(str_replace('_', ' ', $fuente->tipo))];
|
|
switch ($fuente->tipo) {
|
|
case('cuota'):
|
|
$text .= '<td>' . $fuente->obj->pie()->venta()->proyecto()->descripcion
|
|
. '</td><td><a href="' . nUrl('ventas', 'show', ['venta' => $fuente->obj->pie()->venta()->id])
|
|
. '">' . $fuente->obj->pie()->venta()->unidad()->descripcion . '</a></td><td>'
|
|
. $fuente->obj->pie()->venta()->propietario()->nombreCompleto()
|
|
. '</td><td>' . format('shortDate', $rebote->estado()->fecha) . '</td>';
|
|
$info['proyecto'] = $fuente->obj->pie()->venta()->proyecto()->descripcion;
|
|
$info['venta'] = $fuente->obj->pie()->venta()->id;
|
|
$info['departamento'] = $fuente->obj->pie()->venta()->unidad()->descripcion;
|
|
$info['propietario'] = $fuente->obj->pie()->venta()->propietario()->nombreCompleto();
|
|
$info['fecha'] = format('shortDate', $rebote->estado()->fecha);
|
|
break;
|
|
}
|
|
$text .= '<td>' . format('pesos', $rebote->valor('pesos'), true) . '</td>';
|
|
$info['valor'] = format('pesos', $rebote->valor('pesos'), true);
|
|
$output []= array_merge(['id' => $rebote->id], $info);
|
|
$textos []= ['id' => $rebote->id, 'text' => $text];
|
|
}
|
|
$response []= ['status' => 1, 'venta' => $venta->id, 'textos' => $textos, 'rebotes' => $output];
|
|
}
|
|
|
|
return json_encode($response);
|
|
}
|
|
public static function show()
|
|
{
|
|
$id = get('pago');
|
|
$asociado = get('asociado');
|
|
$id_asociado = get($asociado);
|
|
|
|
$pago = model(Pago::class)->findOne($id);
|
|
|
|
return view('ventas.pagos.show', compact('pago', 'asociado', 'id_asociado'));
|
|
}
|
|
public static function pagar()
|
|
{
|
|
$id = get('pago');
|
|
$pago = model(Pago::class)->findOne($id);
|
|
$asociado = get('asociado');
|
|
$id_asociado = get($asociado);
|
|
|
|
return view('ventas.pagos.pagar', compact('pago', 'asociado', 'id_asociado'));
|
|
}
|
|
public static function pagando()
|
|
{
|
|
$id = get('pago');
|
|
$pago = model(Pago::class)->findOne($id);
|
|
$asociado = get('asociado');
|
|
$id_asociado = get($asociado);
|
|
|
|
$f = Carbon::createFromDate(post('year'), post('month'), post('day'), config('app.timezone'));
|
|
$data = [
|
|
'fecha' => $f->format('Y-m-d'),
|
|
'pago' => $pago->id,
|
|
'estado' => 1
|
|
];
|
|
$estado = model(EstadoPago::class)->create($data);
|
|
|
|
$estado->save();
|
|
header('Location: ' . url('', ['p' => $asociado . 's', 'a' => 'show', $asociado => $id_asociado]));
|
|
}
|
|
public static function abonar()
|
|
{
|
|
$id = get('pago');
|
|
$pago = model(Pago::class)->findOne($id);
|
|
$asociado = get('asociado');
|
|
$id_asociado = get($asociado);
|
|
|
|
return view('ventas.pagos.abonar', compact('pago', 'asociado', 'id_asociado'));
|
|
}
|
|
public static function abonando()
|
|
{
|
|
$id = get('pago');
|
|
$pago = model(Pago::class)->findOne($id);
|
|
$asociado = get('asociado');
|
|
$id_asociado = get($asociado);
|
|
|
|
$f = Carbon::createFromDate(post('year'), post('month'), post('day'), config('app.timezone'));
|
|
$data = [
|
|
'fecha' => $f->format('Y-m-d'),
|
|
'pago' => $pago->id,
|
|
'estado' => 2
|
|
];
|
|
$estado = model(EstadoPago::class)->create($data);
|
|
|
|
$estado->save();
|
|
header('Location: ' . url('', ['p' => $asociado . 's', 'a' => 'show', $asociado => $id_asociado]));
|
|
}
|
|
}
|
|
?>
|