Agregar, editar y eliminar promociones
This commit is contained in:
97
app/src/Controller/API/Ventas/Promotions.php
Normal file
97
app/src/Controller/API/Ventas/Promotions.php
Normal file
@ -0,0 +1,97 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
19
app/src/Controller/Ventas/Promotions.php
Normal file
19
app/src/Controller/Ventas/Promotions.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?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]);
|
||||
}
|
||||
}
|
16
app/src/Model/Venta/Promotion/Type.php
Normal file
16
app/src/Model/Venta/Promotion/Type.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
namespace Incoviba\Model\Venta\Promotion;
|
||||
|
||||
enum Type: int
|
||||
{
|
||||
case FIXED = 1;
|
||||
case VARIABLE = 2;
|
||||
|
||||
public function name(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::FIXED => 'fijo',
|
||||
self::VARIABLE => 'variable'
|
||||
};
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user