Auth, Login, Home, Venta->Listados->Precios

This commit is contained in:
Juan Pablo Vial
2023-07-24 20:55:26 -04:00
parent d9d5a15376
commit 1a7b10ce3c
130 changed files with 4302 additions and 0 deletions

View File

@ -0,0 +1,90 @@
<?php
namespace Incoviba\Controller;
use DateTimeImmutable;
use DateInterval;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Alias\View;
use Incoviba\Service;
use Incoviba\Repository;
class Base
{
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, View $view, Service\Login $service, Repository\Venta\Cuota $cuotaRepository, Repository\Venta\Cierre $cierreRepository): ResponseInterface
{
if ($service->isIn()) {
return $this->home($response, $view, $cuotaRepository, $cierreRepository);
}
return $this->login($response, $view);
}
protected function home(ResponseInterface $response, View $view, Repository\Venta\Cuota $cuotaRepository, Repository\Venta\Cierre $cierreRepository): ResponseInterface
{
$cuotas_hoy = count($cuotaRepository->fetchHoy()) ?? 0;
$cuotas_pendientes = count($cuotaRepository->fetchPendientes()) ?? 0;
$cuotas_por_vencer = $this->getCuotasPorVencer($cuotaRepository);
$cierres_vigentes = $this->getCierresVigentes($cierreRepository);
return $view->render($response, 'home', compact('cuotas_hoy', 'cuotas_pendientes', 'cuotas_por_vencer', 'cierres_vigentes'));
}
protected function login(ResponseInterface $response, View $view): ResponseInterface
{
return $view->render($response, 'guest');
}
protected function getCuotasPorVencer(Repository\Venta\Cuota $cuotaRepository): array
{
$cuotas = $cuotaRepository->fetchDatosPorVencer();
$output = [];
foreach ($cuotas as $row) {
$fecha = $row['Fecha'];
$date = new DateTimeImmutable($fecha);
if (($weekday = $date->format('N')) > 5) {
$day_diff = 7 - $weekday + 1;
$date = $date->add(new DateInterval("P{$day_diff}D"));
$fecha = $date->format('Y-m-d');
}
if (!isset($output[$fecha])) {
$output[$fecha] = [];
}
if (!isset($output[$fecha][$row['Proyecto']])) {
$output[$fecha][$row['Proyecto']] = 0;
}
$output[$fecha][$row['Proyecto']] += $row['Cantidad'];
}
foreach ($output as $fecha => $day) {
uksort($day, function($a, $b) {
return strcmp($a, $b);
});
$output[$fecha] = $day;
}
return $output;
}
protected function getCierresVigentes(Repository\Venta\Cierre $cierreRepository): array
{
$cierres = $cierreRepository->fetchDatosVigentes();
$output = [];
$estados = [
'revisado' => 'pendientes',
'rechazado' => 'rechazados',
'aprobado' => 'pendientes',
'vendido' => 'promesados',
'abandonado' => 'rechazados',
'promesado' => 'promesados',
'resciliado' => 'rechazados'
];
foreach ($cierres as $row) {
if (!isset($output[$row['Proyecto']])) {
$output[$row['Proyecto']] = [
'promesados' => 0,
'pendientes' => 0,
'rechazados' => 0,
'total' => 0
];
}
$estado = $estados[$row['Estado']];
$output[$row['Proyecto']][$estado] += $row['Cantidad'];
$output[$row['Proyecto']]['total'] += $row['Cantidad'];
}
return $output;
}
}

View File

@ -0,0 +1,55 @@
<?php
namespace Incoviba\Controller;
use PDOException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Alias\View;
use Incoviba\Repository;
use Incoviba\Service;
class Login
{
public function form(ServerRequestInterface $request, ResponseInterface $response, View $view, Service\Login $service): ResponseInterface
{
$redirect_uri = $request->hasHeader('Referer') ? $request->getHeaderLine('Referer') : $view->get('urls')->base;
if ($service->isIn()) {
$redirect_uri = str_replace('/login', '', $redirect_uri);
return $response->withStatus(301)->withHeader('Location', $redirect_uri);
}
if ($request->hasHeader('X-Redirect-URI')) {
$redirect_uri = $request->getHeaderLine('X-Redirect-URI');
}
return $view->render($response, 'login.form', compact('redirect_uri'));
}
public function login(ServerRequestInterface $request, ResponseInterface $response, Repository\User $userRepository, Service\Login $service): ResponseInterface
{
$body = $request->getParsedBody();
$user = $userRepository->fetchByName($body['name']);
$output = [
'name' => $user->name,
'login' => false
];
if ($user->validate($body['password'])) {
$output['login'] = $service->login($user);
}
$response->getBody()->write(json_encode($output));
return $response->withHeader('Content-Type', 'application/json');
}
public function logout(ServerRequestInterface $request, ResponseInterface $response, Repository\Login $loginRepository, Service\Login $service): ResponseInterface
{
$output = [
'name' => '',
'logout' => false
];
try {
$user = $service->getUser();
$output = [
'name' => $user->name,
'logout' => $service->logout($user)
];
} catch (PDOException) {}
$response->getBody()->write(json_encode($output));
return $response->withHeader('Content-Type', 'application/json');
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace Incoviba\Controller;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Alias\View;
use Incoviba\Repository;
class Proyectos
{
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, View $view): ResponseInterface
{
return $view->render($response, 'proyectos.list');
}
public function list(ServerRequestInterface $request, ResponseInterface $response, Repository\Proyecto $proyectoRepository): ResponseInterface
{
$proyectos = $proyectoRepository->fetchAllActive();
$response->getBody()->write(json_encode(['proyectos' => $proyectos, 'total' => count($proyectos)]));
return $response->withHeader('Content-Type', 'application/json');
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace Incoviba\Controller;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Alias\View;
use Incoviba\Repository;
class Users
{
}

View File

@ -0,0 +1,60 @@
<?php
namespace Incoviba\Controller\Ventas;
use DateTimeImmutable;
use DateInterval;
use IntlDateFormatter;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Alias\View;
use Incoviba\Repository;
use Incoviba\Service;
class Cuotas
{
public function pendientes(ServerRequestInterface $request, ResponseInterface $response, View $view, Repository\Venta\Cuota $cuotaRepository): ResponseInterface
{
$cuotas = $cuotaRepository->fetchPendientes();
$cuotas_pendientes = [];
$today = new DateTimeImmutable();
$formatter = new IntlDateFormatter('es_ES');
$formatter->setPattern('EEEE dd');
foreach ($cuotas as $cuota) {
$date = new DateTimeImmutable($cuota['fecha']);
$day = clone $date;
$weekday = $date->format('N');
if ($weekday > 5) {
$diff = 7 - $weekday + 1;
$day = $day->add(new DateInterval("P{$diff}D"));
}
$cuotas_pendientes []= [
'id' => $cuota['cuota_id'],
'venta_id' => $cuota['venta_id'],
'Proyecto' => $cuota['Proyecto'],
'Departamento' => $cuota['Departamento'],
'Valor' => $cuota['Valor'],
'Dia' => $formatter->format($day),
'Numero' => $cuota['Numero'],
'Propietario' => $cuota['Propietario'],
'Banco' => $cuota['Banco'],
'Fecha Cheque' => $date->format('d-m-Y'),
'Vencida' => $today->diff($date)->days,
'Fecha ISO' => $date->format('Y-m-d')
];
}
return $view->render($response, 'ventas.cuotas.pendientes', compact('cuotas_pendientes'));
}
public function depositar(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Cuota $cuotaRepository, Service\Ventas\Pago $pagoService): ResponseInterface
{
$body = $request->getBody();
$json = json_decode($body->getContents());
$cuota_id = $json->cuota_id;
$cuota = $cuotaRepository->fetchById($cuota_id);
$output = [
'cuota_id' => $cuota_id,
'depositada' => $pagoService->depositar($cuota->pago)
];
$response->getBody()->write(json_encode($output));
return $response->withHeader('Content-Type', 'application/json');
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace Incoviba\Controller\Ventas;
use Incoviba\Common\Alias\View;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Service;
class Precios
{
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, View $view): ResponseInterface
{
return $view->render($response, 'ventas.precios.list');
}
public function proyecto(ServerRequestInterface $request, ResponseInterface $response, Service\Ventas\Precio $precioService): ResponseInterface
{
$body = $request->getBody();
$json = json_decode($body->getContents());
$proyecto_id = $json->proyecto_id;
$precios = $precioService->getByProyecto($proyecto_id);
$response->getBody()->write(json_encode(['precios' => $precios, 'total' => count($precios)]));
return $response->withHeader('Content-Type', 'application/json');
}
}