Conecciones de Promociones

This commit is contained in:
Juan Pablo Vial
2025-04-03 13:15:56 -03:00
parent ced673e452
commit 8a1e6a7761
17 changed files with 1144 additions and 79 deletions

View File

@ -3,6 +3,7 @@ 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;
@ -11,7 +12,8 @@ class Promotions
{
use withJson;
public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Promotion $promotionService): ResponseInterface
public function add(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\Promotion $promotionService): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
@ -43,7 +45,8 @@ class Promotions
return $this->withJson($response, $output);
}
public function edit(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Promotion $promotionService): ResponseInterface
public function edit(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\Promotion $promotionService): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
@ -77,7 +80,8 @@ class Promotions
return $this->withJson($response, $output);
}
public function remove(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Promotion $promotionService, int $promotion_id): ResponseInterface
public function remove(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\Promotion $promotionService, int $promotion_id): ResponseInterface
{
$output = [
'promotion_id' => $promotion_id,
@ -94,4 +98,122 @@ class Promotions
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['operator']) > 0 and $input['operator'][0] !== '') {
$contract_ids = array_filter($input['operator'], function($operator_id) { return $operator_id !== ''; });
$total += count($contract_ids);
foreach ($contract_ids as $contract_id) {
try {
$promotionService->addContract($promotion_id, $contract_id);
$output['connections'] []= [
'operator_id' => $contract_id,
'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,
'connection' => null,
'success' => false,
];
try {
$output['connection'] = $promotionService->removeProject($promotion_id, $project_id);
$output['success'] = true;
} catch (ServiceAction\Delete $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, $output);
}
}