feature/cierres (#25)
Varios cambios Co-authored-by: Juan Pablo Vial <jpvialb@incoviba.cl> Reviewed-on: http://git.provm.cl/Incoviba/oficial/pulls/25
This commit is contained in:
106
app/src/Controller/API/Proyectos/Brokers.php
Normal file
106
app/src/Controller/API/Proyectos/Brokers.php
Normal 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);
|
||||
}
|
||||
}
|
||||
178
app/src/Controller/API/Proyectos/Brokers/Contracts.php
Normal file
178
app/src/Controller/API/Proyectos/Brokers/Contracts.php
Normal 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);
|
||||
}
|
||||
}
|
||||
62
app/src/Controller/API/Proyectos/Unidades.php
Normal file
62
app/src/Controller/API/Proyectos/Unidades.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user