Update subscriptions

This commit is contained in:
Juan Pablo Vial
2025-07-03 16:37:33 -04:00
parent 4980839568
commit cb0731b67d
8 changed files with 182 additions and 9 deletions

View File

@ -146,4 +146,24 @@ class Toku extends Controller
}
return $this->withJson($response, $output);
}
public function update(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\MediosPago\Toku $tokuService, ?string $type = null): ResponseInterface
{
$body = $request->getBody()->getContents();
$input = json_decode($body, true);
$output = [
'type' => $type,
'input' => $input,
'success' => false
];
try {
$tokuService->update($input, $type);
$output['success'] = true;
} catch (Exception $exception) {
$this->logger->error($exception);
}
return $this->withJson($response, $output);
}
}

View File

@ -1,10 +1,10 @@
<?php
namespace Incoviba\Service;
use Exception;
use DateTimeImmutable;
use DateMalformedStringException;
use Incoviba\Exception\ServiceAction\{Create, Read, Update};
use Incoviba\Common\Define;
use PDOException;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal\Service;
@ -12,7 +12,7 @@ use Incoviba\Common\Implement;
use Incoviba\Repository;
use Incoviba\Model;
class Venta extends Service
class Venta extends Service\Repository
{
public function __construct(
LoggerInterface $logger,
@ -189,6 +189,11 @@ class Venta extends Service
}
}
public function getRepository(): Define\Repository
{
return $this->ventaRepository;
}
protected function process(Model\Venta $venta): Model\Venta
{
if ($venta->uf === 0.0) {

View File

@ -302,6 +302,49 @@ class Toku extends Ideal\Service
return $queues;
}
public function update(array $ids, ?string $type = null): array
{
if ($type === null) {
$types = [
'customers',
'subscriptions',
'invoices'
];
$results = [];
foreach ($types as $type) {
$results[$type] = $this->update($ids[$type], $type);
}
return $results;
}
$results = [];
switch ($type) {
case 'customers':
try {
$results['subscription'] = $this->subscription->updateByCustomer($ids);
$results['invoice'] = $this->invoice->updateByCustomer($ids);
} catch (EmptyResult $exception) {
$this->logger->error($exception);
}
break;
case 'subscriptions':
try {
$results['subscription'] = $this->subscription->update($ids);
$results['invoice'] = $this->invoice->updateBySubscription($ids);
} catch (EmptyResult | EmptyResponse $exception) {
$this->logger->error($exception);
}
break;
case 'invoices':
try {
$results['invoice'] = $this->invoice->updateAll($ids);
} catch (EmptyResult $exception) {
$this->logger->error($exception);
}
break;
}
return $results;
}
/**
* @param ServerRequestInterface $request
* @param array $tokenConfig

View File

@ -1,6 +1,7 @@
<?php
namespace Incoviba\Service\Venta\MediosPago\Toku;
use PDO;
use PDOException;
use Psr\Http\Client\ClientInterface;
use Incoviba\Common\Implement\Exception\EmptyResponse;
@ -111,6 +112,68 @@ class Subscription extends AbstractEndPoint
}
}
/**
* @param array $idsData
* @return array
* @throws EmptyResult
* @throws EmptyResponse
*/
public function update(array $idsData): array
{
$toku_ids = array_column($idsData, 'toku_id');
$old_pids = array_column($idsData, 'product_id');
$placeholders = array_map(fn($id) => "id{$id}", array_keys($old_pids));
$placeholdersString = implode(', ', array_map(fn($id) => ":{$id}", $placeholders));
$query = $this->ventaService->getRepository()->getConnection()->getQueryBuilder()
->select('venta.id, CONCAT_WS("-", unidad.descripcion, CONCAT_WS("-", propietario.rut, propietario.dv)) AS old_pid')
->from('venta')
->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, $old_pids);
try {
$statement = $this->ventaService->getRepository()->getConnection()->execute($query, $values);
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $exception) {
$this->logger->error($exception);
throw new EmptyResult($query, $exception);
}
$accountKeys = $this->getAccountKey(array_column($results, 'id'));
$new_pids = [];
$keys = [];
foreach ($results as $result) {
$idx = array_search($result['old_pid'], $old_pids);
$new_pids[$idx] = $result['id'];
if (array_key_exists($result['id'], $accountKeys)) {
$keys[$idx] = $accountKeys[$result['id']];
}
}
$output = [];
foreach ($toku_ids as $idx => $toku_id) {
if (!isset($new_pids[$idx])) {
continue;
}
$data = [
'product_id' => $new_pids[$idx],
];
if (!$this->edit($toku_id, $data, array_key_exists($idx, $keys) ? $keys[$idx] : null)) {
continue;
}
$output[] = [
'toku_id' => $toku_id,
'old_pid' => $old_pids[$idx],
'product_id' => $new_pids[$idx],
'account_key' => array_key_exists($idx, $keys) ? $keys[$idx] : null
];
}
return $output;
}
public function save(array $data): bool
{
return $this->doSave($this->subscriptionRepsitory, $data);
@ -133,11 +196,11 @@ class Subscription extends AbstractEndPoint
if ($ref === null) {
continue;
}
if ($ref === 'pieValor') {
$params[$key] = $data['venta']->formaPago()?->pie?->valor ?? 0;
if ($ref === 'pieValor' and array_key_exists('venta', $data)) {
$params[$key] = $data['venta']?->formaPago()?->pie?->valor ?? 0;
continue;
}
if ($ref === 'datosVenta') {
if ($ref === 'datosVenta' and array_key_exists('venta', $data)) {
$params[$key] = $this->datosVenta($data['venta']);
continue;
}
@ -169,4 +232,35 @@ class Subscription extends AbstractEndPoint
'Unidades' => $venta->propiedad()->summary()
];
}
/**
* @param array $ventaIds
* @return array
* @throws EmptyResult
*/
protected function getAccountKey(array $ventaIds): array
{
$placeholders = array_map(fn($id) => "id{$id}", array_keys($ventaIds));
$placeholdersString = implode(', ', array_map(fn($id) => ":{$id}", $placeholders));
$query = $this->ventaService->getRepository()->getConnection()->getQueryBuilder()
->select('account_key, venta.id AS venta_id')
->from('toku_accounts')
->joined('JOIN proyecto ON proyecto.inmobiliaria = toku_accounts.sociedad_rut')
->joined('JOIN proyecto_tipo_unidad ptu ON ptu.proyecto = proyecto.id')
->joined('JOIN unidad ON unidad.pt = ptu.id')
->joined('JOIN propiedad_unidad pu ON pu.unidad = unidad.id')
->joined('JOIN venta ON venta.propiedad = pu.propiedad')
->where("venta.id IN ({$placeholdersString}) AND toku_accounts.enabled = 1");
$values = array_combine($placeholders, $ventaIds);
try {
$statement = $this->ventaService->getRepository()->getConnection()->execute($query, $values);
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $exception) {
$this->logger->error($exception);
throw new EmptyResult($query, $exception);
}
$keys = array_column($results, 'account_key');
$ids = array_column($results, 'venta_id');
return array_combine($ids, $keys);
}
}