2025-03-13 12:18:08 -03:00
|
|
|
<?php
|
|
|
|
namespace Incoviba\Service\Venta;
|
|
|
|
|
2025-03-18 19:13:47 -03:00
|
|
|
use Incoviba\Controller\API\Ventas\Promotions;
|
|
|
|
use PDOException;
|
2025-03-13 12:18:08 -03:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Incoviba\Common\Ideal;
|
|
|
|
use Incoviba\Common\Implement;
|
|
|
|
use Incoviba\Exception;
|
|
|
|
use Incoviba\Model;
|
|
|
|
use Incoviba\Repository;
|
|
|
|
use Incoviba\Service;
|
|
|
|
|
|
|
|
class Promotion extends Ideal\Service
|
|
|
|
{
|
|
|
|
public function __construct(LoggerInterface $logger,
|
|
|
|
protected Repository\Venta\Promotion $promotionRepository,
|
2025-03-17 22:49:48 -03:00
|
|
|
protected Repository\Proyecto\Broker\Contract $contractRepository,
|
|
|
|
protected Repository\Venta\Unidad $unidadRepository)
|
2025-03-13 12:18:08 -03:00
|
|
|
{
|
|
|
|
parent::__construct($logger);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAll(null|string|array $order = null): array
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return array_map([$this, 'process'], $this->promotionRepository->fetchAll($order));
|
|
|
|
} catch (Implement\Exception\EmptyResult) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $promotion_id
|
|
|
|
* @return Model\Venta\Promotion
|
|
|
|
* @throws Exception\ServiceAction\Read
|
|
|
|
*/
|
|
|
|
public function getById(int $promotion_id): Model\Venta\Promotion
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return $this->process($this->promotionRepository->fetchById($promotion_id));
|
|
|
|
} catch (Implement\Exception\EmptyResult $exception) {
|
|
|
|
throw new Exception\ServiceAction\Read(__CLASS__, $exception);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $contract_id
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getByContract(int $contract_id): array
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return array_map([$this, 'process'], $this->promotionRepository->fetchByContract($contract_id));
|
|
|
|
} catch (Implement\Exception\EmptyResult) {
|
|
|
|
try {
|
|
|
|
$contract = $this->contractRepository->fetchById($contract_id);
|
|
|
|
return array_map([$this, 'process'], $this->promotionRepository->fetchByProject($contract->project->id));
|
|
|
|
} catch (Implement\Exception\EmptyResult) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $contract_id
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getActiveByContract(int $contract_id): array
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return array_map([$this, 'process'], $this->promotionRepository->fetchActiveByContract($contract_id));
|
|
|
|
} catch (Implement\Exception\EmptyResult) {
|
|
|
|
try {
|
|
|
|
$contract = $this->contractRepository->fetchById($contract_id);
|
|
|
|
return array_map([$this, 'process'], $this->promotionRepository->fetchActiveByProject($contract->project->id));
|
|
|
|
} catch (Implement\Exception\EmptyResult) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-18 19:13:47 -03:00
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-13 12:18:08 -03:00
|
|
|
|
|
|
|
protected function process(Model\Venta\Promotion $model): Model\Venta\Promotion
|
|
|
|
{
|
2025-03-17 22:49:48 -03:00
|
|
|
$model->addFactory('contracts', (new Implement\Repository\Factory())
|
|
|
|
->setCallable([$this->contractRepository, 'fetchByPromotion'])
|
|
|
|
->setArgs(['promotion_id' => $model->id]));
|
|
|
|
$model->addFactory('units', (new Implement\Repository\Factory())
|
|
|
|
->setCallable([$this->unidadRepository, 'fetchByPromotion'])
|
|
|
|
->setArgs(['promotion_id' => $model->id]));
|
2025-03-13 12:18:08 -03:00
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
}
|