feature/cierres (#25)

Varios cambios

Co-authored-by: Juan Pablo Vial <jpvialb@incoviba.cl>
Reviewed-on: #25
This commit is contained in:
2025-07-22 13:18:00 +00:00
parent ba57cad514
commit 307f2ac7d7
418 changed files with 20045 additions and 984 deletions

View File

@ -1,11 +1,13 @@
<?php
namespace Incoviba\Controller\API\Admin;
use Exception;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Controller\API\withJson;
use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Exception\ServiceAction\Update;
use Incoviba\Exception\ServiceAction\Delete;
use Incoviba\Service;
class Users
@ -27,4 +29,34 @@ class Users
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
public function edit(ServerRequestInterface $request, ResponseInterface $response,
Service\Login $loginService, int $user_id): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'input' => array_filter($input, fn($key) => $key !== 'password', ARRAY_FILTER_USE_KEY),
'success' => false,
'user' => null
];
try {
$user = $loginService->editUser($user_id, $input);
$output['success'] = true;
$output['user'] = $user;
} catch (Read|Update) {}
return $this->withJson($response, $output);
}
public function delete(ServerRequestInterface $request, ResponseInterface $response,
Service\Login $loginService, int $user_id): ResponseInterface
{
$output = [
'success' => false,
'user' => null
];
try {
$user = $loginService->deleteUser($user_id);
$output['success'] = true;
$output['user'] = $user;
} catch (Read|Delete) {}
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace Incoviba\Controller\API;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Ideal;
use Incoviba\Service;
class External extends Ideal\Controller
{
use withJson;
public function check(ServerRequestInterface $request, ResponseInterface $response, Service\External $externalService): ResponseInterface
{
if ($externalService->check()) {
return $response->withStatus(204);
}
return $response->withStatus(409);
}
public function update(ServerRequestInterface $request, ResponseInterface $response, Service\External $externalService): ResponseInterface
{
if ($externalService->check(true)) {
return $response->withStatus(204);
}
return $response->withStatus(409);
}
}

View File

@ -1,14 +1,14 @@
<?php
namespace Incoviba\Controller\API;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Common\Ideal;
use Incoviba\Repository;
use Incoviba\Service;
use Psr\Log\LoggerInterface;
class Login
class Login extends Ideal\Controller
{
use withJson;
@ -20,14 +20,23 @@ class Login
$body = $request->getParsedBody();
$output = [
'username' => $body['username'],
'success' => false
];
$statusCode = 409;
try {
$user = $userRepository->fetchByName($body['username']);
if ($user->validate($body['password'])) {
$loginService->login($user);
$output['token'] = $loginService->getToken();
if (!$user->validate($body['password'])) {
throw new EmptyResult("Invalid password");
}
} catch (EmptyResult) {}
return $this->withJson($response, $output);
$loginService->login($user);
$output['token'] = $loginService->getToken();
$output['success'] = true;
$statusCode = 200;
} catch (EmptyResult $exception) {
$this->logger->error($exception, [
'username' => $body['username']
]);
}
return $this->withJson($response, $output, $statusCode);
}
}

View File

@ -4,6 +4,7 @@ namespace Incoviba\Controller\API;
use Incoviba\Common\Implement\Exception\EmptyRedis;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Controller\withRedis;
use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service;
@ -170,4 +171,21 @@ class Proyectos
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
public function brokers(ServerRequestInterface $request, ResponseInterface $response,
Repository\Proyecto $proyectoRepository, Service\Proyecto\Broker\Contract $contractService,
int $proyecto_id): ResponseInterface
{
$output = [
'proyecto_id' => $proyecto_id,
'contracts' => []
];
try {
$proyecto = $proyectoRepository->fetchById($proyecto_id);
$output['contracts'] = $contractService->getByProject($proyecto->id);
} catch (EmptyResult | Read $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,106 @@
<?php
namespace Incoviba\Controller\API\Proyectos;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Controller\API\withJson;
use Incoviba\Exception\ServiceAction;
use Incoviba\Service;
class Brokers
{
use withJson;
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker $brokerService): ResponseInterface
{
$brokers = $brokerService->getAll();
return $this->withJson($response, compact('brokers'));
}
public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker $brokerService, int $broker_rut): ResponseInterface
{
try {
$broker = $brokerService->get($broker_rut);
return $this->withJson($response, compact('broker'));
} catch (ServiceAction\Read $exception) {
return $this->withError($response, $exception);
}
}
public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker $brokerService): ResponseInterface
{
$body = $request->getParsedBody();
$output = [
'input' => $body,
'brokers' => [],
'success' => false,
'partial' => false,
'errors' => []
];
foreach ($body['brokers'] as $jsonData) {
try {
$data = json_decode($jsonData, true);
if (is_array($jsonData)) {
$data = $jsonData;
}
$output['brokers'] []= [
'broker' => $brokerService->add($data),
'success' => true
];
$output['partial'] = true;
} catch (ServiceAction\Create $exception) {
$output['errors'] []= $this->parseError($exception);
}
}
if (count($output['brokers']) == count($body['brokers'])) {
$output['success'] = true;
}
return $this->withJson($response, $output);
}
public function edit(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker $brokerService): ResponseInterface
{
$body = $request->getParsedBody();
$output = [
'input' => $body,
'brokers' => [],
'success' => false,
'partial' => false,
'errors' => []
];
foreach ($body['brokers'] as $data) {
try {
$data = json_decode($data, true);
$output['brokers'] []= [
'rut' => $data['rut'],
'broker' => $brokerService->edit($data),
'success' => true
];
$output['partial'] = true;
} catch (ServiceAction\Update $exception) {
$output['errors'] []= $this->parseError($exception);
}
}
if (count($output['brokers']) == count($body['brokers'])) {
$output['success'] = true;
}
return $this->withJson($response, $output);
}
public function delete(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker $brokerService, int $broker_rut): ResponseInterface
{
$output = [
'broker_rut' => $broker_rut,
'broker' => null,
'success' => false
];
try {
$output['broker'] = $brokerService->delete($broker_rut);
$output['success'] = true;
} catch (ServiceAction\Delete $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,178 @@
<?php
namespace Incoviba\Controller\API\Proyectos\Brokers;
use DateTimeImmutable;
use DateMalformedStringException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Implement;
use Incoviba\Controller\API\withJson;
use Incoviba\Exception\ServiceAction;
use Incoviba\Repository;
use Incoviba\Service;
use Psr\Log\LoggerInterface;
class Contracts
{
use withJson;
public function __invoke(ServerRequestInterface $request, ResponseInterface $response,
Service\Proyecto\Broker\Contract $contractService): ResponseInterface
{
$contracts = $contractService->getAll();
return $this->withJson($response, compact('contracts'));
}
public function getByBroker(ServerRequestInterface $request, ResponseInterface $response,
Service\Proyecto\Broker\Contract $contractService,
Service\Proyecto\Broker $brokerService, int $broker_rut): ResponseInterface
{
$output = [
'broker_rut' => $broker_rut,
'contracts' => []
];
try {
$broker = $brokerService->get($broker_rut);
$output['contracts'] = $contractService->getByBroker($broker->rut);
} catch (ServiceAction\Read $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, $output);
}
public function get(ServerRequestInterface $request, ResponseInterface $response,
Service\Proyecto\Broker\Contract $contractService, int $contract_id): ResponseInterface
{
$output = [
'contract_id' => $contract_id,
'contract' => null
];
try {
$output['contract'] = $contractService->getById($contract_id);
} catch (ServiceAction\Read $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, $output);
}
public function add(ServerRequestInterface $request, ResponseInterface $response,
Service\Proyecto\Broker\Contract $contractService): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'input' => $input,
'contracts' => [],
'success' => false,
'partial' => false,
'errors' => [],
];
foreach ($input['contracts'] as $jsonData) {
try {
$contractData = json_decode($jsonData, true);
if (is_array($jsonData)) {
$contractData = $jsonData;
}
$contract = $contractService->add($contractData);
$output['contracts'] []= [
'contract' => $contract,
'success' => true
];
$output['partial'] = true;
} catch (ServiceAction\Create $exception) {
$output['errors'] []= $this->parseError($exception);
}
}
if (count($output['contracts']) == count($input['contracts'])) {
$output['success'] = true;
}
return $this->withJson($response, $output);
}
public function edit(ServerRequestInterface $request, ResponseInterface $response,
Service\Proyecto\Broker\Contract $contractService, int $contract_id): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'contract_id' => $contract_id,
'input' => $input,
'contract' => null,
'success' => false
];
try {
$contract = $contractService->getById($contract_id);
$output['contract'] = $contractService->edit($contract, $input);
$output['success'] = true;
} catch (ServiceAction\Read | ServiceAction\Update $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, $output);
}
public function inactive(ServerRequestInterface $request, ResponseInterface $response,
Service\Proyecto\Broker\Contract $contractService, int $contract_id): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'contract_id' => $contract_id,
'input' => $input,
'contract' => null,
'success' => false
];
try {
$contract = $contractService->getById($contract_id);
$date = new DateTimeImmutable();
if (!empty($input['date'])) {
try {
$date = new DateTimeImmutable($input['date']);
} catch (DateMalformedStringException) {}
}
$output['contract'] = $contractService->inactive($contract, $date);
$output['success'] = true;
} catch (ServiceAction\Read | ServiceAction\Update $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, $output);
}
public function delete(ServerRequestInterface $request, ResponseInterface $response,
Service\Proyecto\Broker\Contract $contractService, int $contract_id): ResponseInterface
{
$output = [
'contract_id' => $contract_id,
'contract' => null,
'success' => false
];
try {
$output['contract'] = $contractService->delete($contract_id);
$output['success'] = true;
} catch (ServiceAction\Delete $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, $output);
}
public function promotions(ServerRequestInterface $request, ResponseInterface $response,
Service\Proyecto\Broker\Contract $contractService,
Repository\Venta\Unidad $unitRepository,
Repository\Venta\Promotion $promotionRepository,
LoggerInterface $logger,
int $contract_id): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'contract_id' => $contract_id,
'input' => $input,
'unidades' => [],
];
$unit_ids = $input['unidad_ids'];
if (is_string($unit_ids)) {
$unit_ids = json_decode($input['unidad_ids'], true);
}
try {
$output['unidades'] = $promotionRepository->fetchByContractAndUnits($contract_id, $unit_ids);
} catch (Implement\Exception\EmptyResult $exception) {
$logger->debug($exception);
}
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,62 @@
<?php
namespace Incoviba\Controller\API\Proyectos;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement;
use Incoviba\Controller\API\withJson;
use Incoviba\Controller\withRedis;
use Incoviba\Exception;
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service;
class Unidades extends Ideal\Controller
{
use withJson;
use withRedis;
public function precios(ServerRequestInterface $request, ResponseInterface $response,
Service\Proyecto $proyectoService,
Repository\Venta\Precio $precioRepository,
int $proyecto_id): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'proyecto_id' => $proyecto_id,
'input' => $input,
'precios' => []
];
$unidad_ids = $input['unidad_ids'];
if (is_string($unidad_ids)) {
$unidad_ids = json_decode($input['unidad_ids'], true);
}
try {
$proyecto = $proyectoService->getById($proyecto_id);
$output['precios'] = $precioRepository->fetchVigentesByUnidades($unidad_ids);
} catch (Implement\Exception\EmptyResult) {}
return $this->withJson($response, $output);
}
public function estados(ServerRequestInterface $request, ResponseInterface $response,
Service\Proyecto $proyectoService,
Repository\Venta\Unidad $unidadRepository,
int $proyecto_id): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'proyecto_id' => $proyecto_id,
'input' => $input,
'estados' => []
];
$unidad_ids = $input['unidad_ids'];
if (is_string($unidad_ids)) {
$unidad_ids = json_decode($input['unidad_ids'], true);
}
try {
$proyecto = $proyectoService->getById($proyecto_id);
$output['estados'] = $unidadRepository->fetchSoldByUnidades($unidad_ids);
} catch (Implement\Exception\EmptyResult) {}
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace Incoviba\Controller\API;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Incoviba\Common\Ideal;
use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Service;
class Queues extends Ideal\Controller
{
use withJson;
public function __invoke(ServerRequestInterface $request, ResponseInterface $response,
Service\Queue $queueService): ResponseInterface
{
$output = [
'success' => false
];
if ($queueService->run($request)) {
$output['success'] = true;
}
return $this->withJson($response, $output);
}
}

View File

@ -7,8 +7,13 @@ use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Ideal\Controller;
use Incoviba\Common\Implement\Exception\EmptyRedis;
use Incoviba\Common\Implement\Exception\EmptyResponse;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Controller\withRedis;
use Incoviba\Exception\InvalidResult;
use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Exception\ServiceAction\Create;
use Incoviba\Exception\ServiceAction\Update;
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service;
@ -36,7 +41,17 @@ class Ventas extends Controller
$proyectos = $proyectoRepository->fetchAllActive();
$this->saveRedis($redisService, $proyectosKey, $proyectos);
}
$proyecto = array_values(array_filter($proyectos, function($proyecto) use ($proyecto_id) {return $proyecto->id === $proyecto_id;}))[0];
$filtrado = array_filter($proyectos, function($proyecto) use ($proyecto_id) {return $proyecto->id === $proyecto_id;});
if (count($filtrado) === 0) {
$proyectos = $proyectoRepository->fetchAllActive();
$this->saveRedis($redisService, $proyectosKey, $proyectos);
$filtrado = array_filter($proyectos, function($proyecto) use ($proyecto_id) {return $proyecto->id === $proyecto_id;});
if (count($filtrado) === 0) {
return $this->withJson($response, $output);
}
}
$proyecto = array_values($filtrado)[0];
$output['proyecto']['descripcion'] = $proyecto->descripcion;
$redisKey = "ventas:proyecto:{$proyecto_id}";
@ -93,7 +108,7 @@ class Ventas extends Controller
$venta = $service->getById($venta_id);
$output['ventas'] []= $venta;
$this->saveRedis($redisService, $redisKey, $venta);
} catch (EmptyResult $exception) {
} catch (Read $exception) {
$this->logger->notice($exception);
}
}
@ -212,7 +227,8 @@ class Ventas extends Controller
$this->saveRedis($redisService, "venta:{$venta->id}", $venta);
$output['venta_id'] = $venta->id;
$output['status'] = true;
} catch (EmptyResult | PDOException $exception) {
} catch (Create | Update $exception) {
$this->logger->error($exception);
$output['errors'] = [
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
@ -303,6 +319,7 @@ class Ventas extends Controller
return $this->withJson($response, $output);
}
public function propietario(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\MediosPago\Toku\Customer $customerService,
Service\Venta\Propietario $propietarioService,
Repository\Venta $ventaRepository, int $venta_id): ResponseInterface
{
@ -332,8 +349,20 @@ class Ventas extends Controller
} else {
$propietario = $propietarioService->edit($propietario, $data);
}
try {
$customer = $customerService->getById($propietario->rut);
$customerData = [
'rut' => $customer['rut'],
'nombreCompleto' => $propietario->nombreCompleto(),
'email' => $propietario->datos?->email ?? '',
'telefono' => $propietario->datos?->telefono ?? ''
];
$customerService->edit($customer['toku_id'], $customerData);
} catch (InvalidResult|EmptyResponse $exception) {
$this->logger->warning($exception);
}
$output['edited'] = true;
} catch (EmptyResult) {}
} catch (EmptyResult|Read|Create|Update) {}
return $this->withJson($response, $output);
}
@ -368,4 +397,17 @@ class Ventas extends Controller
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
public function byUnidades(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta $ventaService, Service\Venta\Unidad $unidadService): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'input' => $input,
'ventas' => []
];
try {
$output['ventas'] = $ventaService->getActiveByUnidadIds($input['unidad_ids']);
} catch (Read) {}
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,170 @@
<?php
namespace Incoviba\Controller\API\Ventas\MediosPago;
use Exception;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Ideal\Controller;
use Incoviba\Controller\API\withJson;
use Incoviba\Exception\InvalidResult;
use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Service;
use Incoviba\Model;
class Toku extends Controller
{
use withJson;
public function cuotas(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\MediosPago\Toku $tokuService, Service\Venta $ventaService, int $venta_id): ResponseInterface
{
$input = $request->getParsedBody();
if ($input === null) {
$input = json_decode($request->getBody()->getContents(), true);
}
$output = [
'input' => $input,
'venta_id' => $venta_id,
'cuotas' => [],
'success' => false,
'partial' => false,
];
try {
$venta = $ventaService->getById($venta_id);
$cuotas = $tokuService->sendCuotas($venta, $input['cuotas'] ?? []);
$output['cuotas'] = array_map(function($row) { return $row['cuota_id']; }, $cuotas);
if (count($cuotas) < count($input['cuotas'] ?? [])) {
$output['partial'] = true;
} else {
$output['success'] = true;
}
} catch (Read | InvalidResult $exception) {
$this->logger->debug($exception);
}
return $this->withJson($response, $output);
}
public function success(ServerRequestInterface $request, ResponseInterface $response,
ResponseFactoryInterface $responseFactory,
Service\Venta\MediosPago\Toku $tokuService): ResponseInterface
{
$input = $request->getParsedBody();
if ($input === null) {
$body = $request->getBody()->getContents();
$input = json_decode($body, true);
}
$this->logger->info('Toku payment success', ['input' => $input]);
try {
if ($tokuService->successEvent($input)) {
return $responseFactory->createResponse(200);
}
$this->logger->warning("Could not update payment", ['input' => $input]);
return $responseFactory->createResponse(409, 'Payment could not be updated');
} catch (InvalidResult $exception) {
$this->logger->warning($exception, [
'uri' => $request->getUri()->getPath(),
'body' => $body,
'input' => $input
]);
if (str_contains($exception->getMessage(), 'Customer')) {
$message = 'Customer not found';
} elseif (str_contains($exception->getMessage(), 'Invoice')) {
$message = 'Invoice not found';
} else {
$message = $exception->getMessage();
}
return $responseFactory->createResponse($exception->getCode(), $message);
}
}
public function test(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta $ventaService, Service\Queue $queueService): ResponseInterface
{
$output = [
'success' => false,
'queue' => []
];
try {
$ventas = $ventaService->getAllWithCuotaPending();
foreach ($ventas as $venta) {
$cuotas = $venta->formaPago()->pie->cuotas();
if (count($cuotas) === 0) {
continue;
}
$queueData = [
'type' => 'request',
'url' => "/api/external/toku/cuotas/{$venta->id}",
'method' => 'post',
'body' => ['cuotas' => array_map(function(Model\Venta\Cuota $cuota) {return $cuota->id;}, $cuotas)]
];
$output['queue'] []= $queueData;
$queueService->enqueue($queueData);
}
$output['success'] = true;
} catch (Read) {
$response->withStatus(404);
return $response;
}
return $this->withJson($response, $output);
}
public function reset(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\MediosPago\Toku $tokuService, ContainerInterface $container): ResponseInterface
{
if (!$container->has('TOKU_ENV') or strtolower($container->get('TOKU_ENV')) !== 'sandbox') {
return $this->withJson($response, ['success' => false], 409);
}
$this->logger->info('Toku reset');
$input = $request->getParsedBody();
$output = [
'input' => $input,
'success' => false
];
try {
$tokuService->reset($input['skips'] ?? []);
$output['success'] = true;
} catch (Exception $exception) {
$this->logger->error($exception);
}
return $this->withJson($response, $output);
}
public function enqueue(ServerRequestInterface $request, ResponseInterface $response,
Service\Queue $queueService, Service\Venta\MediosPago\Toku $tokuService): ResponseInterface
{
$body = $request->getBody()->getContents();
$input = json_decode($body, true);
$output = [
'input' => $input,
'success' => false
];
$queue = $tokuService->queue($input['venta_ids'] ?? []);
if (count($queue) > 0) {
foreach ($queue as $job) {
$queueService->enqueue($job);
}
$output['success'] = true;
}
return $this->withJson($response, $output);
}
public function update(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\MediosPago\Toku $tokuService, ?string $type = null): ResponseInterface
{
$body = $request->getBody()->getContents();
$input = json_decode($body, true);
$output = [
'type' => $type,
'input' => $input,
'output' => [],
'success' => false
];
try {
$output['output'] = $tokuService->update($input, $type);
$output['success'] = true;
} catch (Exception $exception) {
$this->logger->error($exception);
}
return $this->withJson($response, $output);
}
}

View File

@ -5,7 +5,8 @@ use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Incoviba\Controller\API\{withJson, emptyBody};
use Incoviba\Controller\withRedis;
use Incoviba\Common\Implement\Exception\{EmptyRedis, EmptyResult};
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Exception\ServiceAction\{Create, Read, Update};
use Incoviba\Service;
class Pies
@ -27,4 +28,35 @@ class Pies
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Venta $ventaService,
Service\Venta\Pie $pieService, int $venta_id): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'venta_id' => $venta_id,
'input' => $input,
'pie' => null,
'success' => false
];
try {
$venta = $ventaService->getById($venta_id);
} catch (Read $exception) {
return $this->withError($response, $exception);
}
try {
$pie = $pieService->add($input);
} catch (Create $exception) {
return $this->withError($response, $exception);
}
try {
$ventaService->edit($venta, ['pie' => $pie->id]);
$output['pie'] = $pie;
$output['success'] = true;
} catch (Update $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,287 @@
<?php
namespace Incoviba\Controller\API\Ventas;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Incoviba\Controller\API\withJson;
use Incoviba\Exception\ServiceAction;
use Incoviba\Service;
class Promotions
{
use withJson;
public function add(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\Promotion $promotionService): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'input' => $input,
'promotions' => [],
'success' => false,
'partial' => false,
'errors' => [],
];
if (is_string($input['promotions'])) {
$input['promotions'] = json_decode($input['promotions'], true);
}
foreach ($input['promotions'] as $promotion) {
try {
$promotionData = json_decode($promotion, true);
$promotion = $promotionService->add($promotionData);
$output['promotions'] []= [
'promotion' => $promotion,
'success' => true,
];
$output['partial'] = true;
} catch (ServiceAction\Create $exception) {
$output['errors'] []= $this->parseError($exception);
}
}
if (count($output['promotions']) == count($input['promotions'])) {
$output['success'] = true;
}
return $this->withJson($response, $output);
}
public function edit(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\Promotion $promotionService): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'input' => $input,
'promotions' => [],
'success' => false,
'partial' => false,
'errors' => [],
];
if (is_string($input['promotions'])) {
$input['promotions'] = json_decode($input['promotions'], true);
}
foreach ($input['promotions'] as $promotion) {
try {
$promotionData = json_decode($promotion, true);
$promotion = $promotionService->getById($promotionData['id']);
unset($promotionData['id']);
$promotion = $promotionService->edit($promotion, $promotionData);
$output['promotions'] []= [
'promotion' => $promotion,
'success' => true,
];
$output['partial'] = true;
} catch (ServiceAction\Read | ServiceAction\Update $exception) {
$output['errors'] []= $this->parseError($exception);
}
}
if (count($output['promotions']) == count($input['promotions'])) {
$output['success'] = true;
}
return $this->withJson($response, $output);
}
public function remove(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\Promotion $promotionService, int $promotion_id): ResponseInterface
{
$output = [
'promotion_id' => $promotion_id,
'promotion' => null,
'success' => false,
'errors' => [],
];
try {
$output['promotion'] = $promotionService->remove($promotion_id);
$output['success'] = true;
} catch (ServiceAction\Delete $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, $output);
}
public function addConnections(ServerRequestInterface $request, ResponseInterface $response,
LoggerInterface $logger,
Service\Venta\Promotion $promotionService, int $promotion_id): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'promotion_id' => $promotion_id,
'input' => $input,
'connections' => [],
'success' => false,
'partial' => false
];
$total = 0;
if (count($input['project']) > 0 and $input['project'][0] !== '') {
$project_ids = array_filter($input['project'], function($project_id) { return $project_id !== ''; });
$total += count($project_ids);
foreach ($project_ids as $project_id) {
try {
$promotionService->addProject($promotion_id, $project_id);
$output['connections'] []= [
'project_id' => $project_id,
'success' => true,
];
$output['partial'] = true;
} catch (ServiceAction\Create $exception) {
$logger->error($exception);
}
}
}
if (count($input['broker']) > 0 and $input['broker'][0] !== '') {
$broker_ruts = array_filter($input['broker'], function($broker_rut) { return $broker_rut !== ''; });
$total += count($broker_ruts);
foreach ($broker_ruts as $broker_rut) {
try {
$promotionService->addBroker($promotion_id, $broker_rut);
$output['connections'] []= [
'broker_rut' => $broker_rut,
'success' => true,
];
$output['partial'] = true;
} catch (ServiceAction\Create $exception) {
$logger->error($exception);
}
}
}
if (count($input['unit_type']) > 0 and $input['unit_type'][0] !== '') {
$unit_type_ids = array_filter($input['unit_type'], function($unit_type_id) { return $unit_type_id !== ''; });
$total += count($unit_type_ids);
foreach ($unit_type_ids as $unit_type_id) {
try {
$promotionService->addUnitType($promotion_id, $input['type_project'], $unit_type_id);
$output['connections'] []= [
'unit_type_id' => $unit_type_id,
'success' => true,
];
$output['partial'] = true;
} catch (ServiceAction\Create $exception) {
$logger->error($exception);
}
}
}
if (count($input['line']) > 0 and $input['line'][0] !== '') {
$line_ids = array_filter($input['line'], function($line_id) { return $line_id !== ''; });
$total += count($line_ids);
foreach ($line_ids as $line_id) {
try {
$promotionService->addUnitLine($promotion_id, $line_id);
$output['connections'] []= [
'line_id' => $line_id,
'success' => true,
];
$output['partial'] = true;
} catch (ServiceAction\Create $exception) {
$logger->error($exception);
}
}
}
if (count($input['unit']) > 0 and $input['unit'][0] !== '') {
$unit_ids = array_filter($input['unit'], function($unit_id) { return $unit_id !== ''; });
$total += count($unit_ids);
foreach ($unit_ids as $unit_id) {
try {
$promotionService->addUnit($promotion_id, $unit_id);
$output['connections'] []= [
'unit_id' => $unit_id,
'success' => true,
];
$output['partial'] = true;
} catch (ServiceAction\Create $exception) {
$logger->error($exception);
}
}
}
if (count($output['connections']) === $total) {
$output['success'] = true;
}
return $this->withJson($response, $output);
}
public function removeProject(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\Promotion $promotionService,
int $promotion_id, int $project_id): ResponseInterface
{
$output = [
'promotion_id' => $promotion_id,
'project_id' => $project_id,
'success' => false,
];
try {
$promotionService->removeProject($promotion_id, $project_id);
$output['success'] = true;
} catch (ServiceAction\Delete $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, $output);
}
public function removeBroker(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\Promotion $promotionService,
int $promotion_id, string $broker_rut): ResponseInterface
{
$output = [
'promotion_id' => $promotion_id,
'broker_rut' => $broker_rut,
'success' => false,
];
try {
$promotionService->removeBroker($promotion_id, $broker_rut);
$output['success'] = true;
} catch (ServiceAction\Delete $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, $output);
}
public function removeUnitType(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\Promotion $promotionService,
int $promotion_id, int $project_id, int $unit_type_id): ResponseInterface
{
$output = [
'promotion_id' => $promotion_id,
'project_id' => $project_id,
'unit_type_id' => $unit_type_id,
'success' => false,
];
try {
$promotionService->removeUnitType($promotion_id, $project_id, $unit_type_id);
$output['success'] = true;
} catch (ServiceAction\Delete $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, $output);
}
public function removeUnitLine(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\Promotion $promotionService,
int $promotion_id, int $unit_line_id): ResponseInterface
{
$output = [
'promotion_id' => $promotion_id,
'unit_line_id' => $unit_line_id,
'success' => false,
];
try {
$promotionService->removeUnitLine($promotion_id, $unit_line_id);
$output['success'] = true;
} catch (ServiceAction\Delete $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, $output);
}
public function removeUnit(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\Promotion $promotionService,
int $promotion_id, int $unit_id): ResponseInterface
{
$output = [
'promotion_id' => $promotion_id,
'unit_id' => $unit_id,
'success' => false,
];
try {
$promotionService->removeUnit($promotion_id, $unit_id);
$output['success'] = true;
} catch (ServiceAction\Delete $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, $output);
}
}

View File

@ -4,9 +4,11 @@ namespace Incoviba\Controller\API\Ventas;
use PDOException;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Incoviba\Controller\API\{withJson, emptyBody};
use Incoviba\Controller\withRedis;
use Incoviba\Common\Implement\Exception\{EmptyResult,EmptyRedis};
use Incoviba\Exception\ServiceAction\{Read, Create, Update};
use Incoviba\Repository;
use Incoviba\Service;
@ -14,7 +16,9 @@ class PropiedadesUnidades
{
use emptyBody, withJson, withRedis;
public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\PropiedadUnidad $propiedadUnidadService): ResponseInterface
public function add(ServerRequestInterface $request, ResponseInterface $response,
LoggerInterface $logger,
Service\Venta\PropiedadUnidad $propiedadUnidadService): ResponseInterface
{
$body = $request->getParsedBody();
$output = [
@ -26,8 +30,8 @@ class PropiedadesUnidades
$pu = $propiedadUnidadService->add($body);
$output['propiedad_unidad'] = $pu;
$output['added'] = true;
} catch (EmptyResult $exception) {
error_log($exception);
} catch (Read | Create $exception) {
$logger->notice($exception);
}
return $this->withJson($response, $output);
}
@ -44,7 +48,7 @@ class PropiedadesUnidades
$pu = $propiedadUnidadService->getById($pu_id);
$propiedadUnidadService->edit($pu, (array) $body);
$output['edited'] = true;
} catch (PDOException | EmptyResult) {}
} catch (PDOException | Read | EmptyResult | Update) {}
return $this->withJson($response, $output);
}
public function remove(ServerRequestInterface $request, ResponseInterface $response,
@ -58,7 +62,7 @@ class PropiedadesUnidades
$pu = $propiedadUnidadRepository->fetchById($pu_id);
$propiedadUnidadRepository->remove($pu);
$output['removed'] = true;
} catch (EmptyResult) {}
} catch (PDOException | EmptyResult) {}
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,103 @@
<?php
namespace Incoviba\Controller\API\Ventas;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Controller\API\withJson;
use Incoviba\Exception\ServiceAction;
use Incoviba\Service;
class Reservations
{
use withJson;
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Reservation $reservationService): ResponseInterface
{
$reservations = [];
try {
$reservations = $reservationService->getAll();
} catch (ServiceAction\Read $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, compact('reservations'));
}
public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Reservation $reservationService, int $reservation_id): ResponseInterface
{
$output = [
'reservation_id' => $reservation_id,
'reservation' => null,
];
try {
$output['reservation'] = $reservationService->get($reservation_id);
} catch (ServiceAction\Read $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, $output);
}
public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Reservation $reservationService): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'input' => $input,
'reservations' => [],
'success' => false,
'partial' => false,
'errors' => [],
];
try {
$output['reservations'] []= [
'reservation' => $reservationService->add($input),
'success' => true
];
$output['partial'] = true;
} catch (ServiceAction\Create $exception) {
$output['errors'] []= $this->parseError($exception);
}
if (count($input['reservations']) === count($output['reservations'])) {
$output['success'] = true;
}
return $this->withJson($response, $output);
}
public function edit(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Reservation $reservationService, int $reservation_id): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'input' => $input,
'reservation_id' => $reservation_id,
'reservations' => null,
'success' => false,
];
try {
$reservation = $reservationService->get($reservation_id);
$output['reservations'] = $reservationService->edit($reservation, $input);
$output['success'] = true;
} catch (ServiceAction\Read | ServiceAction\Update $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, $output);
}
public function delete(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Reservation $reservationService, int $reservation_id): ResponseInterface
{
$output = [
'reservation_id' => $reservation_id,
'success' => false,
];
try {
$reservationService->delete($reservation_id);
$output['success'] = true;
} catch (ServiceAction\Delete $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace Incoviba\Controller\Proyectos;
use Psr\Log\LoggerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Incoviba\Common\Alias\View;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Repository;
use Incoviba\Service;
class Brokers
{
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, LoggerInterface $logger,
Service\Proyecto\Broker $brokerService, Repository\Proyecto $proyectoRepository,
View $view): ResponseInterface
{
$projects = [];
try {
$projects = $proyectoRepository->fetchAll('descripcion');
} catch (EmptyResult $exception) {
$logger->error($exception);
}
$brokers = $brokerService->getAll();
return $view->render($response, 'proyectos.brokers', compact('brokers', 'projects'));
}
public function show(ServerRequestInterface $request, ResponseInterface $response, LoggerInterface $logger,
Service\Proyecto\Broker $brokerService, Repository\Proyecto $proyectoRepository,
View $view, int $broker_rut): ResponseInterface
{
$broker = null;
try {
$broker = $brokerService->get($broker_rut);
} catch (Read $exception) {
$logger->error($exception);
}
$projects = [];
try {
$projects = $proyectoRepository->fetchAll('descripcion');
} catch (EmptyResult $exception) {
$logger->error($exception);
}
return $view->render($response, 'proyectos.brokers.show', compact('broker', 'projects'));
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace Incoviba\Controller\Proyectos\Brokers;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Alias\View;
use Incoviba\Common\Ideal\Controller;
use Incoviba\Exception;
use Incoviba\Service;
class Contracts extends Controller
{
public function show(ServerRequestInterface $request, ResponseInterface $response, View $view,
Service\Proyecto\Broker\Contract $contractService,
int $broker_rut, int $contract_id): ResponseInterface
{
$contract = null;
try {
$contract = $contractService->getById($contract_id);
} catch (Exception\ServiceAction\Read) {}
return $view->render($response, 'proyectos.brokers.contracts.show', compact('contract'));
}
}

View File

@ -4,6 +4,9 @@ namespace Incoviba\Controller;
use Incoviba\Common\Alias\View;
use Incoviba\Common\Implement\Exception\EmptyRedis;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Exception\ServiceAction\Create;
use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Exception\ServiceAction\Update;
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service;
@ -142,9 +145,24 @@ class Ventas
return $view->render($response, 'ventas.desistir', compact('venta'));
}
public function desistida(ServerRequestInterface $request, ResponseInterface $response, Service\Venta $ventaService,
Service\Venta\Pago $pagoService,
View $view, int $venta_id): ResponseInterface
{
$venta = $ventaService->getById($venta_id);
try {
$venta = $ventaService->getById($venta_id);
} catch (Read) {
return $view->render($response->withStatus(404), 'not_found');
}
if ($venta->resciliacion() === null) {
$pagoData = [
'fecha' => $venta->currentEstado()->fecha->format('Y-m-d'),
'valor' => 0
];
try {
$pago = $pagoService->add($pagoData);
$venta = $ventaService->edit($venta, ['resciliacion' => $pago->id]);
} catch (Create | Update) {}
}
return $view->render($response, 'ventas.desistida', compact('venta'));
}
}

View File

@ -6,6 +6,7 @@ use Psr\Http\Message\ServerRequestInterface;
use DateTimeImmutable;
use Incoviba\Common\Alias\View;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Exception\ServiceAction\Create;
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service;
@ -84,13 +85,15 @@ class Cuotas
return $view->render($response, 'ventas.pies.cuotas.add', compact('pie', 'venta', 'bancos'));
}
public function doAdd(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pie $pieService,
Repository\Venta $ventaRepository, Service\Valor $valorService, int $pie_id): ResponseInterface
Repository\Venta $ventaRepository, Service\Valor $valorService,
Service\Queue $queueService, int $pie_id): ResponseInterface
{
$body = $request->getParsedBody();
$pie = $pieService->getById($pie_id);
$venta = $ventaRepository->fetchByPie($pie_id);
$start = count($pie->cuotas(vigentes: true));
$total = $pie->cuotas;
$cuotas = [];
for ($i = $start; $i < $total; $i ++) {
if ($body["banco{$i}"] === '') {
continue;
@ -103,7 +106,18 @@ class Cuotas
'valor' => $valorService->clean(str_replace('.', '', $body["valor{$i}"])),
'numero' => $i + 1,
];
$pieService->addCuota($data);
try {
$cuotas []= $pieService->addCuota($data)->id;
} catch (Create) {}
}
if (count($cuotas) > 0) {
$data = [
'type' => 'request',
'url' => "/api/external/toku/cuotas/{$venta->id}",
'method' => 'post',
'body' => compact('cuotas')
];
$queueService->enqueue($data);
}
return $response->withHeader('Location', "/venta/{$venta->id}");
}

View File

@ -1,7 +1,6 @@
<?php
namespace Incoviba\Controller\Ventas;
use Exception;
use DateInterval;
use DateTimeImmutable;
use Psr\Http\Message\ResponseInterface;
@ -23,9 +22,6 @@ class Facturacion extends Ideal\Controller
return $view->render($response, 'ventas.facturacion', compact('proyectos'));
}
/**
* @throws Exception
*/
public function show(ServerRequestInterface $request, ResponseInterface $response, View $view,
Service\Venta $ventaService, Service\Proyecto\Terreno $terrenoService,
Service\IPC $ipcService, Service\UF $ufService,

View File

@ -1,12 +1,12 @@
<?php
namespace Incoviba\Controller\Ventas;
use Incoviba\Common\Alias\View;
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Alias\View;
use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Repository;
use Incoviba\Service;
class Pies
{
@ -21,4 +21,13 @@ class Pies
$estados = $tipoEstadoPagoRepository->fetchAll('descripcion');
return $view->render($response, 'ventas.pies.cuotas', compact('pie', 'venta', 'bancos', 'estados', 'ventaRepository'));
}
public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Venta $ventaService,
View $view, int $venta_id): ResponseInterface
{
$venta = null;
try {
$venta = $ventaService->getById($venta_id);
} catch (Read) {}
return $view->render($response, 'ventas.pies.add', compact('venta'));
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace Incoviba\Controller\Ventas;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Alias\View;
use Incoviba\Common\Ideal;
use Incoviba\Exception\ServiceAction;
use Incoviba\Service;
class Promotions extends Ideal\Controller
{
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, View $view,
Service\Venta\Promotion $promotionService): ResponseInterface
{
$promotions = $promotionService->getAll('description');
return $view->render($response, 'ventas.promotions', ['promotions' => $promotions]);
}
public function show(ServerRequestInterface $request, ResponseInterface $response, View $view,
Service\Venta\Promotion $promotionService, Service\Proyecto $proyectoService,
Service\Proyecto\Broker $brokerService,
int $promotion_id): ResponseInterface
{
$promotion = null;
try {
$promotion = $promotionService->getById($promotion_id);
} catch (ServiceAction\Read) {}
$projects = $proyectoService->getVendibles('descripcion');
$brokers = $brokerService->getAll('name');
return $view->render($response, 'ventas.promotions.show', ['promotion' => $promotion,
'projects' => $projects, 'brokers' => $brokers]);
}
}