FIX: Check if invoice exists before sending, if exists then save

Send correct UF amount if date in future
This commit is contained in:
Juan Pablo Vial
2025-05-16 14:36:20 -04:00
parent 8ba54fd3ad
commit 8ce7d2570d
5 changed files with 45 additions and 12 deletions

View File

@ -163,7 +163,7 @@ abstract class AbstractEndPoint extends LoggerEnabled implements EndPoint
} }
} }
abstract public function save(array $data): bool;
abstract protected function mapParams(array $data): array; abstract protected function mapParams(array $data): array;
abstract protected function mapSave(array $data): array; abstract protected function mapSave(array $data): array;
abstract protected function save(array $data): bool;
} }

View File

@ -1,6 +1,8 @@
<?php <?php
namespace Incoviba\Service\Venta\MediosPago; namespace Incoviba\Service\Venta\MediosPago;
use InvalidArgumentException;
use Incoviba\Service\Venta\MediosPago\Toku\{Customer,Subscription,Invoice};
use Incoviba\Common\Ideal; use Incoviba\Common\Ideal;
use Incoviba\Common\Implement\Exception\EmptyResponse; use Incoviba\Common\Implement\Exception\EmptyResponse;
use Incoviba\Exception\InvalidResult; use Incoviba\Exception\InvalidResult;
@ -8,8 +10,6 @@ use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Model; use Incoviba\Model;
use Incoviba\Model\Persona; use Incoviba\Model\Persona;
use Incoviba\Model\Venta\Propietario; use Incoviba\Model\Venta\Propietario;
use InvalidArgumentException;
use Psr\Log\LoggerInterface;
class Toku extends Ideal\Service class Toku extends Ideal\Service
{ {
@ -17,9 +17,9 @@ class Toku extends Ideal\Service
const string SUBSCRIPTION = 'subscription'; const string SUBSCRIPTION = 'subscription';
const string INVOICE = 'invoice'; const string INVOICE = 'invoice';
protected EndPoint $customer; protected Customer $customer;
protected EndPoint $subscription; protected Subscription $subscription;
protected EndPoint $invoice; protected Invoice $invoice;
public function register(string $type, EndPoint $endPoint): self public function register(string $type, EndPoint $endPoint): self
{ {
if (!in_array(strtolower($type), ['customer', 'subscription', 'invoice'])) { if (!in_array(strtolower($type), ['customer', 'subscription', 'invoice'])) {
@ -95,12 +95,28 @@ class Toku extends Ideal\Service
$customer = $this->sendPersona($venta->propietario()); $customer = $this->sendPersona($venta->propietario());
$subscription = $this->sendVenta($venta); $subscription = $this->sendVenta($venta);
$customerInvoices = [];
try {
$customerInvoices = $this->invoice->getByCustomer($customer['toku_id']);
$customerInvoices = array_filter($customerInvoices, function($invoiceRow) use ($subscription) {
return $invoiceRow['subscription'] === $subscription['toku_id'];
});
} catch (EmptyResponse) {}
$invoices = []; $invoices = [];
$errors = []; $errors = [];
foreach ($venta->formaPago()->pie->cuotas() as $cuota) { foreach ($venta->formaPago()->pie->cuotas() as $cuota) {
if (count($cuotas_ids) > 0 and !in_array($cuota->id, $cuotas_ids)) { if (count($cuotas_ids) > 0 and !in_array($cuota->id, $cuotas_ids)) {
continue; continue;
} }
if (count($customerInvoices) and in_array($cuota->id, array_column($customerInvoices, 'invoice_external_id'))) {
$invoice = array_find($customerInvoices, function($invoiceRow) use ($cuota) {
return $invoiceRow['invoice_external_id'] === $cuota->id;
});
$invoices []= $invoice;
$this->invoice->save($invoice);
continue;
}
try { try {
$invoices []= $this->invoice->getById($cuota->id); $invoices []= $this->invoice->getById($cuota->id);
} catch (InvalidResult $exception) { } catch (InvalidResult $exception) {
@ -109,7 +125,8 @@ class Toku extends Ideal\Service
'customer' => $customer['toku_id'], 'customer' => $customer['toku_id'],
'product_id' => $venta->id, 'product_id' => $venta->id,
'subscription' => $subscription['toku_id'], 'subscription' => $subscription['toku_id'],
'cuota' => $cuota 'cuota' => $cuota,
'venta' => $venta
]; ];
if (!$this->invoice->add($invoiceData)) { if (!$this->invoice->add($invoiceData)) {
throw new EmptyResponse("Could not add Invoice for Cuota {$cuota->id}", $exception); throw new EmptyResponse("Could not add Invoice for Cuota {$cuota->id}", $exception);

View File

@ -42,7 +42,7 @@ class Customer extends AbstractEndPoint
$this->sendDelete($request_uri, [204], [404, 409]); $this->sendDelete($request_uri, [204], [404, 409]);
} }
protected function save(array $data): bool public function save(array $data): bool
{ {
return $this->doSave($this->customerRepository, $data); return $this->doSave($this->customerRepository, $data);
} }

View File

@ -3,6 +3,8 @@ namespace Incoviba\Service\Venta\MediosPago\Toku;
use DateMalformedStringException; use DateMalformedStringException;
use DateTimeImmutable; use DateTimeImmutable;
use Psr\Http\Client\ClientInterface;
use Incoviba\Common\Implement\Exception\EmptyResponse;
use Incoviba\Common\Implement\Exception\EmptyResult; use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Exception\InvalidResult; use Incoviba\Exception\InvalidResult;
use Incoviba\Model; use Incoviba\Model;
@ -10,7 +12,6 @@ use Incoviba\Repository;
use Incoviba\Service\UF; use Incoviba\Service\UF;
use Incoviba\Service\Venta\MediosPago\AbstractEndPoint; use Incoviba\Service\Venta\MediosPago\AbstractEndPoint;
use Incoviba\Service\Venta\Pago; use Incoviba\Service\Venta\Pago;
use Psr\Http\Client\ClientInterface;
class Invoice extends AbstractEndPoint class Invoice extends AbstractEndPoint
{ {
@ -50,6 +51,17 @@ class Invoice extends AbstractEndPoint
$this->sendDelete($request_uri, [204], [404, 409]); $this->sendDelete($request_uri, [204], [404, 409]);
} }
/**
* @param string $customer_id
* @return array
* @throws EmptyResponse
*/
public function getByCustomer(string $customer_id): array
{
$request_uri = "/invoices/customer/{$customer_id}";
return $this->sendGet($request_uri, [200], [404]);
}
/** /**
* @param string $invoice_toku_id * @param string $invoice_toku_id
* @param array $data * @param array $data
@ -89,7 +101,7 @@ class Invoice extends AbstractEndPoint
return $this->pagoService->depositar($invoice->cuota->pago, $date); return $this->pagoService->depositar($invoice->cuota->pago, $date);
} }
protected function save(array $data): bool public function save(array $data): bool
{ {
return $this->doSave($this->invoiceRepository, $data); return $this->doSave($this->invoiceRepository, $data);
} }
@ -122,7 +134,11 @@ class Invoice extends AbstractEndPoint
continue; continue;
} }
if ($ref === 'valor') { if ($ref === 'valor') {
$params[$key] = $data['cuota']->pago->valor(); $valor = $data['cuota']->pago->valor();
if ($valor === 0) {
$valor = $data['cuota']->pago->valor / $data['venta']->uf;
}
$params[$key] = $valor;
continue; continue;
} }
if ($ref === 'datosCuota') { if ($ref === 'datosCuota') {

View File

@ -70,7 +70,7 @@ class Subscription extends AbstractEndPoint
return compact('existingSubscriptions', 'missingVentas'); return compact('existingSubscriptions', 'missingVentas');
} }
protected function save(array $data): bool public function save(array $data): bool
{ {
return $this->doSave($this->subscriptionRepsitory, $data); return $this->doSave($this->subscriptionRepsitory, $data);
} }