124 lines
4.4 KiB
PHP
124 lines
4.4 KiB
PHP
<?php
|
|
namespace Incoviba\Service\Venta;
|
|
|
|
use DateTimeInterface;
|
|
use PDOException;
|
|
use Psr\Http\Message\UploadedFileInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
use Incoviba\Common\Ideal;
|
|
use Incoviba\Common\Implement\Exception\EmptyResult;
|
|
use Incoviba\Exception\ServiceAction;
|
|
use Incoviba\Model;
|
|
use Incoviba\Repository;
|
|
|
|
class Precio extends Ideal\Service
|
|
{
|
|
public function __construct(LoggerInterface $logger,
|
|
protected Repository\Venta\Precio $precioRepository,
|
|
protected Repository\Venta\EstadoPrecio $estadoPrecioRepository,
|
|
protected Precio\Estado $estadoPrecioService,
|
|
protected Precio\Import $importService)
|
|
{
|
|
parent::__construct($logger);
|
|
}
|
|
|
|
/**
|
|
* @param int $proyecto_id
|
|
* @return array
|
|
* @throws ServiceAction\Read
|
|
*/
|
|
public function getByProyecto(int $proyecto_id): array
|
|
{
|
|
try {
|
|
$precios = $this->precioRepository->fetchByProyecto($proyecto_id);
|
|
foreach ($precios as $precio) {
|
|
$precio->estados = $this->estadoPrecioRepository->fetchByPrecio($precio->id);
|
|
$precio->current = $this->estadoPrecioRepository->fetchCurrentByPrecio($precio->id);
|
|
}
|
|
return $precios;
|
|
} catch (EmptyResult $exception) {
|
|
throw new ServiceAction\Read(__CLASS__, $exception);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param int $unidad_id
|
|
* @return Model\Venta\Precio
|
|
* @throws ServiceAction\Read
|
|
*/
|
|
public function getVigenteByUnidad(int $unidad_id): Model\Venta\Precio
|
|
{
|
|
try {
|
|
$precio = $this->precioRepository->fetchVigenteByUnidad($unidad_id);
|
|
$precio->estados = $this->estadoPrecioRepository->fetchByPrecio($precio->id);
|
|
$precio->current = $this->estadoPrecioRepository->fetchCurrentByPrecio($precio->id);
|
|
return $precio;
|
|
} catch (EmptyResult $exception) {
|
|
throw new ServiceAction\Read(__CLASS__, $exception);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param int $unidad_id
|
|
* @return array
|
|
* @throws ServiceAction\Read
|
|
*/
|
|
public function getByUnidad(int $unidad_id): array
|
|
{
|
|
try {
|
|
$precios = $this->precioRepository->fetchByUnidad($unidad_id);
|
|
foreach ($precios as $precio) {
|
|
$precio->estados = $this->estadoPrecioRepository->fetchByPrecio($precio->id);
|
|
$precio->current = $this->estadoPrecioRepository->fetchCurrentByPrecio($precio->id);
|
|
}
|
|
return $precios;
|
|
} catch (EmptyResult $exception) {
|
|
throw new ServiceAction\Read(__CLASS__, $exception);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param int $projectId
|
|
* @param DateTimeInterface $date
|
|
* @param UploadedFileInterface $uploadedFile
|
|
* @return array
|
|
* @throws ServiceAction\Create
|
|
*/
|
|
public function import(int $projectId, DateTimeInterface $date, UploadedFileInterface $uploadedFile): array
|
|
{
|
|
$pricesData = $this->importService->importData($projectId, $date, $uploadedFile);
|
|
$prices = [];
|
|
foreach ($pricesData as $data) {
|
|
try {
|
|
$price = $this->precioRepository->create($data);
|
|
$price = $this->precioRepository->save($price);
|
|
$prices[] = $price;
|
|
} catch (EmptyResult | PDOException $exception) {
|
|
$this->logger->error('Problemas para agregar precio', ['data' => $data, 'exception' => $exception]);
|
|
}
|
|
}
|
|
|
|
foreach ($prices as $price) {
|
|
$idx = array_search($price->unidad->id, array_column($pricesData, 'unidad'));
|
|
$row = $pricesData[$idx];
|
|
try {
|
|
$this->estadoPrecioService->replacePrices($price->unidad, $date, $price);
|
|
} catch (ServiceAction\Update $exception) {
|
|
$this->logger->error('Problemas para reemplazar precios', [
|
|
'data' => $row,
|
|
'exception' => $exception
|
|
]);
|
|
}
|
|
try {
|
|
$this->estadoPrecioService->updatePrice($price, $row['fecha']);
|
|
} catch (ServiceAction\Update $exception) {
|
|
$this->logger->error('Problemas para actualizar estado de precio', [
|
|
'data' => $row,
|
|
'exception' => $exception
|
|
]);
|
|
}
|
|
}
|
|
return $prices;
|
|
}
|
|
}
|