Agregar, editar y eliminar promociones

This commit is contained in:
Juan Pablo Vial
2025-03-18 19:13:47 -03:00
parent 2b3f476df7
commit d3b0026ca4
4 changed files with 107 additions and 17 deletions

View File

@ -19,25 +19,50 @@ class Promotion extends Common\Ideal\Repository
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('end_date', new Common\Implement\Repository\Mapper\DateTime('end_date', 'endDate'))
->register('valid_until', new Common\Implement\Repository\Mapper\DateTime('valid_until', 'validUntil'));
->register('end_date', (new Common\Implement\Repository\Mapper\DateTime('end_date', 'endDate'))
->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);
}
public function save(Common\Define\Model $model): Model\Venta\Promotion
{
$model->id = $this->saveNew(
['amount', 'type', 'start_date', 'end_date', 'valid_until'],
[$model->amount, $model->type, $model->startDate->format('Y-m-d'),
$model->endDate->format('Y-m-d'), $model->validUntil->format('Y-m-d')]
['description', 'amount', 'type', 'start_date', 'end_date', 'valid_until'],
[$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')]
);
return $model;
}
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;
}
/**