98 lines
3.5 KiB
PHP
98 lines
3.5 KiB
PHP
<?php
|
|
namespace Incoviba\Controller\API\Ventas;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
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);
|
|
}
|
|
}
|