Import Precios

This commit is contained in:
Juan Pablo Vial
2025-08-27 19:27:18 -04:00
parent 4e4c0b7648
commit ad64ffa436
19 changed files with 1049 additions and 21 deletions

View File

@ -1,19 +1,31 @@
<?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\Read;
use Incoviba\Repository;
use Incoviba\Exception\ServiceAction;
use Incoviba\Model;
use Incoviba\Repository;
class Precio
class Precio extends Ideal\Service
{
public function __construct(protected Repository\Venta\Precio $precioRepository, protected Repository\Venta\EstadoPrecio $estadoPrecioRepository) {}
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 Read
* @throws ServiceAction\Read
*/
public function getByProyecto(int $proyecto_id): array
{
@ -25,14 +37,14 @@ class Precio
}
return $precios;
} catch (EmptyResult $exception) {
throw new Read(__CLASS__, $exception);
throw new ServiceAction\Read(__CLASS__, $exception);
}
}
/**
* @param int $unidad_id
* @return Model\Venta\Precio
* @throws Read
* @throws ServiceAction\Read
*/
public function getVigenteByUnidad(int $unidad_id): Model\Venta\Precio
{
@ -42,14 +54,14 @@ class Precio
$precio->current = $this->estadoPrecioRepository->fetchCurrentByPrecio($precio->id);
return $precio;
} catch (EmptyResult $exception) {
throw new Read(__CLASS__, $exception);
throw new ServiceAction\Read(__CLASS__, $exception);
}
}
/**
* @param int $unidad_id
* @return array
* @throws Read
* @throws ServiceAction\Read
*/
public function getByUnidad(int $unidad_id): array
{
@ -61,7 +73,51 @@ class Precio
}
return $precios;
} catch (EmptyResult $exception) {
throw new Read(__CLASS__, $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;
}
}