Files
oficial/app/src/Service/Venta/MediosPago/Toku/Subscription.php
2025-06-10 17:48:36 -04:00

173 lines
5.7 KiB
PHP

<?php
namespace Incoviba\Service\Venta\MediosPago\Toku;
use PDOException;
use Psr\Http\Client\ClientInterface;
use Incoviba\Common\Implement\Exception\EmptyResponse;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Model\Venta;
use Incoviba\Repository;
use Incoviba\Service;
use Incoviba\Service\Venta\MediosPago\AbstractEndPoint;
class Subscription extends AbstractEndPoint
{
public function __construct(ClientInterface $client,
protected Repository\Venta\MediosPago\Toku\Subscription $subscriptionRepsitory,
protected Service\Venta $ventaService)
{
parent::__construct($client);
}
public function getById(string $id): array
{
return $this->doGetById([$this->subscriptionRepsitory, 'fetchByVenta'], $id, "No existe toku_id para Venta {$id}");
}
public function getByExternalId(string $id): array
{
return $this->doGetById([$this->subscriptionRepsitory, 'fetchByTokuId'], $id, "No existe Subscription para toku_id {$id}");
}
public function get(string $id): array
{
$request_uri = "/subscriptions/{$id}";
return $this->sendGet($request_uri, [200], [401, 404, 422]);
}
public function add(array $data): bool
{
$request_uri = '/subscriptions';
return $this->sendAdd($request_uri, $data, [200, 201], [401, 404, 409, 422]);
}
public function edit(string $id, array $data): bool
{
$request_uri = "/subscriptions/{$id}";
return $this->sendEdit($request_uri, $data, [200], [401, 404, 409, 422]);
}
public function delete(string $id): void
{
$request_uri = "/subscriptions/{$id}";
$this->sendDelete($request_uri, [204], [404, 409]);
}
public function reset(array $skip = []): array
{
try {
$tokuIds = $this->subscriptionRepsitory->fetchAllTokuIds();
$tokuIds = array_filter($tokuIds, function (string $tokuId) use ($skip) {
return !in_array($tokuId, $skip);
});
} catch (EmptyResult $exception) {
$this->logger->warning($exception);
return [];
}
foreach ($tokuIds as $tokuId) {
try {
$this->delete($tokuId);
$this->subscriptionRepsitory->removeByTokuId($tokuId);
} catch (EmptyResponse | PDOException $exception) {
$this->logger->warning($exception, ['subscription->toku_id' => $tokuId]);
}
}
return $tokuIds;
}
/**
* @return array
* @throws Read
*/
public function check(): array
{
$ventas = $this->ventaService->getAllWithCuotaPending();
$ids = array_column($ventas, 'id');
$existingSubscriptions = [];
try {
$existingSubscriptions = $this->subscriptionRepsitory->fetchByVentas($ids);
array_walk($existingSubscriptions, function(&$subscription) {
$subscription->venta = $this->ventaService->getById($subscription->venta->id);
});
} catch (EmptyResult) {}
if (count($existingSubscriptions) === 0) {
$missingVentas = $ventas;
} else {
$missingVentas = array_filter($ventas, function($venta) use ($existingSubscriptions) {
return !array_any($existingSubscriptions, fn($subscription) => $subscription->venta->id === $venta->id);
});
}
return compact('existingSubscriptions', 'missingVentas');
}
public function queue(int $venta_id): bool
{
try {
$venta = $this->ventaService->getById($venta_id);
} catch (Read $exception) {
$this->logger->warning($exception);
return false;
}
try {
$subscription = $this->subscriptionRepsitory->fetchByVenta($venta_id);
return false;
} catch (EmptyResult) {
return true;
}
}
public function save(array $data): bool
{
return $this->doSave($this->subscriptionRepsitory, $data);
}
protected function mapParams(array $data): array
{
$paramsMap = [
'customer' => 'customer',
'product_id' => 'product_id',
'pac_mandate_id' => null,
'is_recurring' => null,
'due_day' => null,
'amount' => 'pieValor',
'receipt_product_code' => null,
'metadata' => 'datosVenta'
];
$params = [];
foreach ($paramsMap as $key => $ref) {
if ($ref === null) {
continue;
}
if ($ref === 'pieValor') {
$params[$key] = $data['venta']->formaPago()?->pie?->valor ?? 0;
continue;
}
if ($ref === 'datosVenta') {
$params[$key] = $this->datosVenta($data['venta']);
continue;
}
if (array_key_exists($ref, $data) and $data[$ref] !== '' and $data[$ref] !== null) {
$params[$key] = $data[$ref];
}
}
return $params;
}
protected function mapSave(array $data): array
{
$responseMap = [
'product_id' => 'venta_id',
'id' => 'toku_id'
];
$mappedData = [];
foreach ($responseMap as $responseKey => $dataKey) {
if (isset($data[$responseKey])) {
$mappedData[$dataKey] = $data[$responseKey];
}
}
return $mappedData;
}
protected function datosVenta(Venta $venta): array
{
return [
'Proyecto' => $venta->proyecto()->descripcion,
'Unidades' => $venta->propiedad()->summary()
];
}
}