Cambio en queue para que no quede pegado esperando respuesta en cli.
Chequeo de servicios externos para agregar elementos pendientes.
This commit is contained in:
@ -4,6 +4,7 @@ namespace Incoviba\Service\Venta\MediosPago;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResponse;
|
||||
use Incoviba\Exception\InvalidResult;
|
||||
use Incoviba\Exception\ServiceAction\Read;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Model\Persona;
|
||||
use Incoviba\Model\Venta\Propietario;
|
||||
@ -149,6 +150,80 @@ class Toku extends Ideal\Service
|
||||
return $this->updatePago($paymentData);
|
||||
}
|
||||
}
|
||||
public function check(bool $update = false): array
|
||||
{
|
||||
try {
|
||||
list('existingSubscriptions' => $existingSubscriptions, 'missingVentas' => $missingVentas) = $this->subscription->check();
|
||||
} catch (Read) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$queues = [];
|
||||
if (count($missingVentas) > 0) {
|
||||
foreach ($missingVentas as $venta) {
|
||||
$cuotas = $venta->formaPago()->pie->cuotas();
|
||||
if (count($cuotas) === 0) {
|
||||
continue;
|
||||
}
|
||||
$queueData = [
|
||||
'type' => 'request',
|
||||
'url' => "/api/external/toku/cuotas/{$venta->id}",
|
||||
'method' => 'post',
|
||||
'body' => ['cuotas' => array_map(function(Model\Venta\Cuota $cuota) {return $cuota->id;}, $cuotas)]
|
||||
];
|
||||
$queues []= $queueData;
|
||||
}
|
||||
}
|
||||
if ($update and count($existingSubscriptions) > 0) {
|
||||
foreach ($existingSubscriptions as $subscription) {
|
||||
$cuotas = $subscription->venta->formaPago()->pie->cuotas();
|
||||
if (count($cuotas) === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$propietario = $subscription->venta->propietario();
|
||||
try {
|
||||
$customer = $this->customer->getById($propietario->rut());
|
||||
} catch (InvalidResult) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
$editData = [
|
||||
'rut' => $customer['rut'],
|
||||
'nombreCompleto' => $propietario->nombreCompleto(),
|
||||
'email' => $propietario->datos?->email ?? '',
|
||||
'telefono' => $propietario->datos?->telefono ?? ''
|
||||
];
|
||||
$this->customer->edit($customer['toku_id'], $editData);
|
||||
} catch (EmptyResponse) {}
|
||||
foreach ($cuotas as $cuota) {
|
||||
try {
|
||||
$invoice = $this->invoice->getById($cuota->id);
|
||||
$editData = [
|
||||
'customer' => $customer['toku_id'],
|
||||
'product_id' => $subscription->venta->id,
|
||||
'subscription' => $subscription->toku_id,
|
||||
'cuota' => $cuota
|
||||
];
|
||||
try {
|
||||
$this->invoice->edit($invoice['toku_id'], $editData);
|
||||
} catch (EmptyResponse) {}
|
||||
} catch (InvalidResult) {
|
||||
$invoiceData = [
|
||||
'customer' => $customer['toku_id'],
|
||||
'product_id' => $subscription->venta->id,
|
||||
'subscription' => $subscription->toku_id,
|
||||
'cuota' => $cuota
|
||||
];
|
||||
try {
|
||||
$this->invoice->add($invoiceData);
|
||||
} catch (EmptyResponse) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $queues;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $request
|
||||
|
@ -1,9 +1,9 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Venta\MediosPago\Toku;
|
||||
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Service\Venta\MediosPago\AbstractEndPoint;
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
|
||||
class Customer extends AbstractEndPoint
|
||||
{
|
||||
|
@ -1,14 +1,19 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Venta\MediosPago\Toku;
|
||||
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
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;
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
|
||||
class Subscription extends AbstractEndPoint
|
||||
{
|
||||
public function __construct(ClientInterface $client, protected Repository\Venta\MediosPago\Toku\Subscription $subscriptionRepsitory)
|
||||
public function __construct(ClientInterface $client,
|
||||
protected Repository\Venta\MediosPago\Toku\Subscription $subscriptionRepsitory,
|
||||
protected Service\Venta $ventaService)
|
||||
{
|
||||
parent::__construct($client);
|
||||
}
|
||||
@ -43,6 +48,28 @@ class Subscription extends AbstractEndPoint
|
||||
$this->sendDelete($request_uri, [204], [404, 409]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws Read
|
||||
*/
|
||||
public function check(): array
|
||||
{
|
||||
$ventas = $this->ventaService->getAllWithCuotaPending();
|
||||
$ids = array_column($ventas, 'id');
|
||||
$existingSubscriptions = [];
|
||||
try {
|
||||
$existingSubscriptions = $this->subscriptionRepsitory->fetchByVentas($ids);
|
||||
} 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');
|
||||
}
|
||||
|
||||
protected function save(array $data): bool
|
||||
{
|
||||
return $this->doSave($this->subscriptionRepsitory, $data);
|
||||
|
Reference in New Issue
Block a user