diff --git a/app/src/Service/Venta/MediosPago/Toku/Invoice.php b/app/src/Service/Venta/MediosPago/Toku/Invoice.php index c769f29..ef275c8 100644 --- a/app/src/Service/Venta/MediosPago/Toku/Invoice.php +++ b/app/src/Service/Venta/MediosPago/Toku/Invoice.php @@ -4,6 +4,7 @@ namespace Incoviba\Service\Venta\MediosPago\Toku; use DateMalformedStringException; use DateTimeImmutable; use DateTimeZone; +use PDO; use PDOException; use Psr\Http\Client\ClientInterface; use Psr\Log\LoggerInterface; @@ -199,6 +200,41 @@ class Invoice extends AbstractEndPoint return $this->pagoService->depositar($invoice->cuota->pago, $date); } + /** + * @param array $idsData + * @return array + * @throws EmptyResult + */ + public function updateAll(array $idsData): array + { + $tokuIds = array_column($idsData, 'toku_id'); + $oldIds = array_column($idsData, 'product_id'); + + $placeholders = array_map(fn($id) => "id{$id}", array_keys($oldIds)); + $placeholdersString = implode(', ', array_map(fn($id) => ":{$id}", $placeholders)); + $query = $this->pagoService->getRepository()->getConnection()->getQueryBuilder() + ->select('pago.id, CONCAT_WS("-", unidad.descripcion, CONCAT_WS("-", propietario.rut, propietario.dv)) AS old_pid') + ->from('pago') + ->joined('JOIN cuota ON cuota.pago = pago.id') + ->joined('JOIN venta ON venta.pie = cuota.pie') + ->joined('JOIN propietario ON propietario.rut = venta.propietario') + ->joined('JOIN propiedad_unidad pu ON pu.propiedad = venta.propiedad') + ->joined('JOIN unidad ON pu.unidad = unidad.id') + ->having("old_pid IN ({$placeholdersString})"); + $values = array_combine($placeholders, $oldIds); + try { + $statement = $this->pagoService->getRepository()->getConnection()->execute($query, $values); + $results = $statement->fetchAll(PDO::FETCH_ASSOC); + } catch (PDOException $exception) { + $this->logger->error($exception); + throw new EmptyResult($query, $exception); + } + + $ids = array_column($results, 'pago.id'); + $newIds = array_combine($ids, $tokuIds); + return array_map(fn($id) => ['product_id' => $id, 'toku_id' => $newIds[$id]], $ids); + } + public function save(array $data): bool { return $this->doSave($this->invoiceRepository, $data); @@ -215,7 +251,7 @@ class Invoice extends AbstractEndPoint { $paramsMap = [ 'customer' => 'customer', - 'product_id' => 'product_id', + 'product_id' => 'cuota_id', 'due_date' => 'fecha', 'subscription' => 'subscription', 'amount' => 'valor', diff --git a/app/src/Service/Venta/Pago.php b/app/src/Service/Venta/Pago.php index 8d54938..3ddc4bc 100644 --- a/app/src/Service/Venta/Pago.php +++ b/app/src/Service/Venta/Pago.php @@ -4,25 +4,37 @@ namespace Incoviba\Service\Venta; use DateTimeInterface; use DateTimeImmutable; use DateMalformedStringException; +use Incoviba\Common\Define; use Incoviba\Exception\ServiceAction\Create; use Incoviba\Exception\ServiceAction\Read; use Incoviba\Exception\ServiceAction\Update; use PDOException; +use Incoviba\Common\Ideal; use Incoviba\Common\Implement\Exception\EmptyResult; use Incoviba\Repository; use Incoviba\Model; use Incoviba\Service; +use Psr\Log\LoggerInterface; -class Pago +class Pago extends Ideal\Service\Repository { public function __construct( + LoggerInterface $logger, protected Repository\Venta\Pago $pagoRepository, protected Repository\Venta\EstadoPago $estadoPagoRepository, protected Repository\Venta\TipoEstadoPago $tipoEstadoPagoRepository, protected Service\UF $ufService, protected Service\Valor $valorService, protected Service\Queue $queueService - ) {} + ) + { + parent::__construct($logger); + } + + public function getRepository(): Define\Repository + { + return $this->pagoRepository; + } public function depositar(Model\Venta\Pago $pago, DateTimeInterface $fecha): bool {