220 lines
8.7 KiB
PHP
220 lines
8.7 KiB
PHP
<?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['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);
|
|
}
|
|
}
|