2025-02-18 16:02:10 -03:00
|
|
|
<?php
|
|
|
|
namespace Incoviba\Repository\Venta;
|
|
|
|
|
|
|
|
use Incoviba\Common;
|
|
|
|
use Incoviba\Model;
|
|
|
|
|
|
|
|
class Promotion extends Common\Ideal\Repository
|
|
|
|
{
|
|
|
|
public function __construct(Common\Define\Connection $connection, protected Precio $precioRepository)
|
|
|
|
{
|
|
|
|
parent::__construct($connection);
|
|
|
|
}
|
|
|
|
|
2025-02-24 12:39:42 -03:00
|
|
|
public function getTable(): string
|
|
|
|
{
|
|
|
|
return 'promotions';
|
|
|
|
}
|
|
|
|
|
2025-02-18 16:02:10 -03:00
|
|
|
public function create(?array $data = null): Model\Venta\Promotion
|
|
|
|
{
|
|
|
|
$map = (new Implement\Repository\MapperParser(['amount', 'type']))
|
|
|
|
->register('price_id', (new Implement\Repository\Mapper())
|
|
|
|
->setProperty('price')
|
|
|
|
->setFunction(function($data) {
|
|
|
|
return $this->precioRepository->create($data);
|
|
|
|
}))
|
|
|
|
->register('start_date', new Implement\Repository\Mapper\DateTime('start_date', 'startDate'))
|
|
|
|
->register('end_date', new Implement\Repository\Mapper\DateTime('end_date', 'endDate'))
|
|
|
|
->register('valid_until', new Implement\Repository\Mapper\DateTime('valid_until', 'validUntil'));
|
|
|
|
|
|
|
|
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', 'price_id'],
|
|
|
|
[$model->amount, $model->type, $model->startDate->format('Y-m-d'), $model->endDate->format('Y-m-d'), $model->validUntil->format('Y-m-d'), $model->price->id]
|
|
|
|
);
|
|
|
|
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', 'price_id'], $new_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fetchByPrice(int $price_id): array
|
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->select()
|
|
|
|
->from($this->getTable())
|
|
|
|
->where('price_id = :price_id');
|
|
|
|
return $this->fetchMany($query, ['price_id' => $price_id]);
|
|
|
|
}
|
|
|
|
public function fetchActiveByPrice(int $price_id): array
|
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->select()
|
|
|
|
->from($this->getTable())
|
|
|
|
->where('price_id = :price_id AND state = :state');
|
|
|
|
return $this->fetchMany($query, ['price_id' => $price_id, 'state' => Model\Venta\Promotion\State::ACTIVE]);
|
|
|
|
}
|
2025-02-24 12:39:42 -03:00
|
|
|
}
|