Agregar, editar y eliminar promociones
This commit is contained in:
@ -29,7 +29,7 @@ final class CreatePromotion extends AbstractMigration
|
|||||||
->addColumn('start_date', 'date', ['null' => false])
|
->addColumn('start_date', 'date', ['null' => false])
|
||||||
->addColumn('end_date', 'date', ['null' => false])
|
->addColumn('end_date', 'date', ['null' => false])
|
||||||
->addColumn('valid_until', 'date', ['null' => false])
|
->addColumn('valid_until', 'date', ['null' => false])
|
||||||
->addColumn('state', 'integer', ['length' => 1, 'null' => false, 'default' => 0])
|
->addColumn('state', 'integer', ['length' => 1, 'null' => false, 'default' => 1])
|
||||||
->addForeignKey('price_id', 'prices', 'id', ['delete' => 'cascade', 'update' => 'cascade'])
|
->addForeignKey('price_id', 'prices', 'id', ['delete' => 'cascade', 'update' => 'cascade'])
|
||||||
->create();
|
->create();
|
||||||
|
|
||||||
|
@ -4,14 +4,18 @@ namespace Incoviba\Model\Venta;
|
|||||||
use DateTimeInterface;
|
use DateTimeInterface;
|
||||||
use Incoviba\Common;
|
use Incoviba\Common;
|
||||||
use Incoviba\Model\Proyecto\Broker;
|
use Incoviba\Model\Proyecto\Broker;
|
||||||
|
use Incoviba\Model\Venta\Promotion\State;
|
||||||
|
use Incoviba\Model\Venta\Promotion\Type;
|
||||||
|
|
||||||
class Promotion extends Common\Ideal\Model
|
class Promotion extends Common\Ideal\Model
|
||||||
{
|
{
|
||||||
|
public string $description;
|
||||||
public float $amount;
|
public float $amount;
|
||||||
public DateTimeInterface $startDate;
|
public DateTimeInterface $startDate;
|
||||||
public DateTimeInterface $endDate;
|
public ?DateTimeInterface $endDate;
|
||||||
public DateTimeInterface $validUntil;
|
public ?DateTimeInterface $validUntil;
|
||||||
public int $state;
|
public Type $type;
|
||||||
|
public State $state = State::ACTIVE;
|
||||||
|
|
||||||
protected array $contracts;
|
protected array $contracts;
|
||||||
public function contracts(): array
|
public function contracts(): array
|
||||||
@ -31,17 +35,24 @@ class Promotion extends Common\Ideal\Model
|
|||||||
return $this->units;
|
return $this->units;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function value(float $price): float
|
||||||
|
{
|
||||||
|
if ($this->type === Type::FIXED) {
|
||||||
|
return $price + $this->amount;
|
||||||
|
}
|
||||||
|
return $price / (1 - $this->amount);
|
||||||
|
}
|
||||||
|
|
||||||
protected function jsonComplement(): array
|
protected function jsonComplement(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
'description' => $this->description,
|
||||||
'amount' => $this->amount,
|
'amount' => $this->amount,
|
||||||
'start_date' => $this->startDate->format('Y-m-d'),
|
'start_date' => $this->startDate->format('Y-m-d'),
|
||||||
'end_date' => $this->endDate->format('Y-m-d'),
|
'end_date' => $this->endDate?->format('Y-m-d'),
|
||||||
'valid_until' => $this->validUntil->format('Y-m-d'),
|
'valid_until' => $this->validUntil?->format('Y-m-d'),
|
||||||
'state' => [
|
'type' => $this->type,
|
||||||
'id' => $this->state,
|
'state' => $this->state,
|
||||||
'description' => Promotion\State::name($this->state)
|
|
||||||
],
|
|
||||||
'contracts' => $this->contracts() ?? [],
|
'contracts' => $this->contracts() ?? [],
|
||||||
'units' => $this->units() ?? []
|
'units' => $this->units() ?? []
|
||||||
];
|
];
|
||||||
|
@ -19,25 +19,50 @@ class Promotion extends Common\Ideal\Repository
|
|||||||
|
|
||||||
public function create(?array $data = null): Model\Venta\Promotion
|
public function create(?array $data = null): Model\Venta\Promotion
|
||||||
{
|
{
|
||||||
$map = (new Common\Implement\Repository\MapperParser(['amount', 'type']))
|
$map = (new Common\Implement\Repository\MapperParser(['description', 'amount']))
|
||||||
|
->register('type', (new Common\Implement\Repository\Mapper())
|
||||||
|
->setFunction(function($data) {
|
||||||
|
return Model\Venta\Promotion\Type::from($data['type']);
|
||||||
|
}))
|
||||||
|
->register('state', (new Common\Implement\Repository\Mapper())
|
||||||
|
->setFunction(function($data) {
|
||||||
|
return Model\Venta\Promotion\State::from($data['state']);
|
||||||
|
})
|
||||||
|
->setDefault(Model\Venta\Promotion\State::ACTIVE))
|
||||||
->register('start_date', new Common\Implement\Repository\Mapper\DateTime('start_date', 'startDate'))
|
->register('start_date', new Common\Implement\Repository\Mapper\DateTime('start_date', 'startDate'))
|
||||||
->register('end_date', new Common\Implement\Repository\Mapper\DateTime('end_date', 'endDate'))
|
->register('end_date', (new Common\Implement\Repository\Mapper\DateTime('end_date', 'endDate'))
|
||||||
->register('valid_until', new Common\Implement\Repository\Mapper\DateTime('valid_until', 'validUntil'));
|
->setDefault(null))
|
||||||
|
->register('valid_until', (new Common\Implement\Repository\Mapper\DateTime('valid_until', 'validUntil'))
|
||||||
|
->setDefault(null));
|
||||||
|
|
||||||
return $this->parseData(new Model\Venta\Promotion(), $data, $map);
|
return $this->parseData(new Model\Venta\Promotion(), $data, $map);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function save(Common\Define\Model $model): Model\Venta\Promotion
|
public function save(Common\Define\Model $model): Model\Venta\Promotion
|
||||||
{
|
{
|
||||||
$model->id = $this->saveNew(
|
$model->id = $this->saveNew(
|
||||||
['amount', 'type', 'start_date', 'end_date', 'valid_until'],
|
['description', 'amount', 'type', 'start_date', 'end_date', 'valid_until'],
|
||||||
[$model->amount, $model->type, $model->startDate->format('Y-m-d'),
|
[$model->description, $model->amount, $model->type->value, $model->startDate->format('Y-m-d'),
|
||||||
$model->endDate->format('Y-m-d'), $model->validUntil->format('Y-m-d')]
|
$model->endDate?->format('Y-m-d'), $model->validUntil?->format('Y-m-d')]
|
||||||
);
|
);
|
||||||
return $model;
|
return $model;
|
||||||
}
|
}
|
||||||
public function edit(Common\Define\Model $model, array $new_data): Model\Venta\Promotion
|
public function edit(Common\Define\Model $model, array $new_data): Model\Venta\Promotion
|
||||||
{
|
{
|
||||||
return $this->update($model, ['amount', 'type', 'start_date', 'end_date', 'valid_until'], $new_data);
|
return $this->update($model, ['description', 'amount', 'type', 'start_date', 'end_date', 'valid_until'], $new_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function filterData(array $data): array
|
||||||
|
{
|
||||||
|
$filteredData = array_intersect_key($data, array_flip(['description', 'amount', 'type', 'start_date', 'end_date', 'valid_until']));
|
||||||
|
if (!isset($filteredData['amount']) and isset($data['value'])) {
|
||||||
|
$filteredData['amount'] = $data['value'];
|
||||||
|
}
|
||||||
|
$filteredData['type'] = (int) $filteredData['type'];
|
||||||
|
if ($filteredData['amount'] > 1 and $filteredData['type'] === Model\Venta\Promotion\Type::VARIABLE->value) {
|
||||||
|
$filteredData['amount'] = $filteredData['amount'] / 100;
|
||||||
|
}
|
||||||
|
return $filteredData;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Incoviba\Service\Venta;
|
namespace Incoviba\Service\Venta;
|
||||||
|
|
||||||
|
use Incoviba\Controller\API\Ventas\Promotions;
|
||||||
|
use PDOException;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Incoviba\Common\Ideal;
|
use Incoviba\Common\Ideal;
|
||||||
use Incoviba\Common\Implement;
|
use Incoviba\Common\Implement;
|
||||||
@ -78,6 +80,58 @@ class Promotion extends Ideal\Service
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $data
|
||||||
|
* @return Model\Venta\Promotion
|
||||||
|
* @throws Exception\ServiceAction\Create
|
||||||
|
*/
|
||||||
|
public function add(array $data): Model\Venta\Promotion
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$filteredData = $this->promotionRepository->filterData($data);
|
||||||
|
#throw new \Exception(var_export($filteredData, true));
|
||||||
|
$promotion = $this->promotionRepository->create($filteredData);
|
||||||
|
#throw new \Exception(var_export($promotion, true));
|
||||||
|
$promotion = $this->promotionRepository->save($promotion);
|
||||||
|
return $this->process($promotion);
|
||||||
|
} catch (PDOException $exception) {
|
||||||
|
throw new Exception\ServiceAction\Create(__CLASS__, $exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Model\Venta\Promotion $promotion
|
||||||
|
* @param array $data
|
||||||
|
* @return Model\Venta\Promotion
|
||||||
|
* @throws Exception\ServiceAction\Update
|
||||||
|
*/
|
||||||
|
public function edit(Model\Venta\Promotion $promotion, array $data): Model\Venta\Promotion
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$filteredData = $this->promotionRepository->filterData($data);
|
||||||
|
$promotion = $this->promotionRepository->edit($promotion, $filteredData);
|
||||||
|
return $this->process($promotion);
|
||||||
|
} catch (PDOException | Implement\Exception\EmptyResult $exception) {
|
||||||
|
throw new Exception\ServiceAction\Update(__CLASS__, $exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $promotion_id
|
||||||
|
* @return Model\Venta\Promotion
|
||||||
|
* @throws Exception\ServiceAction\Delete
|
||||||
|
*/
|
||||||
|
public function remove(int $promotion_id): Model\Venta\Promotion
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$promotion = $this->promotionRepository->fetchById($promotion_id);
|
||||||
|
$this->promotionRepository->remove($promotion);
|
||||||
|
return $promotion;
|
||||||
|
} catch (PDOException | Implement\Exception\EmptyResult $exception) {
|
||||||
|
throw new Exception\ServiceAction\Delete(__CLASS__, $exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
protected function process(Model\Venta\Promotion $model): Model\Venta\Promotion
|
protected function process(Model\Venta\Promotion $model): Model\Venta\Promotion
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user