diff --git a/app/src/Service/Venta/MediosPago/AbstractEndPoint.php b/app/src/Service/Venta/MediosPago/AbstractEndPoint.php index b5aeed5..16cc98c 100644 --- a/app/src/Service/Venta/MediosPago/AbstractEndPoint.php +++ b/app/src/Service/Venta/MediosPago/AbstractEndPoint.php @@ -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 mapSave(array $data): array; - abstract protected function save(array $data): bool; } diff --git a/app/src/Service/Venta/MediosPago/Toku.php b/app/src/Service/Venta/MediosPago/Toku.php index 81eb16a..d41c207 100644 --- a/app/src/Service/Venta/MediosPago/Toku.php +++ b/app/src/Service/Venta/MediosPago/Toku.php @@ -1,6 +1,8 @@ sendPersona($venta->propietario()); $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 = []; $errors = []; foreach ($venta->formaPago()->pie->cuotas() as $cuota) { if (count($cuotas_ids) > 0 and !in_array($cuota->id, $cuotas_ids)) { 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 { $invoices []= $this->invoice->getById($cuota->id); } catch (InvalidResult $exception) { @@ -109,7 +125,8 @@ class Toku extends Ideal\Service 'customer' => $customer['toku_id'], 'product_id' => $venta->id, 'subscription' => $subscription['toku_id'], - 'cuota' => $cuota + 'cuota' => $cuota, + 'venta' => $venta ]; if (!$this->invoice->add($invoiceData)) { throw new EmptyResponse("Could not add Invoice for Cuota {$cuota->id}", $exception); diff --git a/app/src/Service/Venta/MediosPago/Toku/Customer.php b/app/src/Service/Venta/MediosPago/Toku/Customer.php index 2af755b..82731f7 100644 --- a/app/src/Service/Venta/MediosPago/Toku/Customer.php +++ b/app/src/Service/Venta/MediosPago/Toku/Customer.php @@ -42,7 +42,7 @@ class Customer extends AbstractEndPoint $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); } diff --git a/app/src/Service/Venta/MediosPago/Toku/Invoice.php b/app/src/Service/Venta/MediosPago/Toku/Invoice.php index 464f8cd..10cd77e 100644 --- a/app/src/Service/Venta/MediosPago/Toku/Invoice.php +++ b/app/src/Service/Venta/MediosPago/Toku/Invoice.php @@ -3,6 +3,8 @@ namespace Incoviba\Service\Venta\MediosPago\Toku; use DateMalformedStringException; use DateTimeImmutable; +use Psr\Http\Client\ClientInterface; +use Incoviba\Common\Implement\Exception\EmptyResponse; use Incoviba\Common\Implement\Exception\EmptyResult; use Incoviba\Exception\InvalidResult; use Incoviba\Model; @@ -10,7 +12,6 @@ use Incoviba\Repository; use Incoviba\Service\UF; use Incoviba\Service\Venta\MediosPago\AbstractEndPoint; use Incoviba\Service\Venta\Pago; -use Psr\Http\Client\ClientInterface; class Invoice extends AbstractEndPoint { @@ -50,6 +51,17 @@ class Invoice extends AbstractEndPoint $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 array $data @@ -89,7 +101,7 @@ class Invoice extends AbstractEndPoint 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); } @@ -122,7 +134,11 @@ class Invoice extends AbstractEndPoint continue; } 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; } if ($ref === 'datosCuota') { diff --git a/app/src/Service/Venta/MediosPago/Toku/Subscription.php b/app/src/Service/Venta/MediosPago/Toku/Subscription.php index 27c704a..8b46a75 100644 --- a/app/src/Service/Venta/MediosPago/Toku/Subscription.php +++ b/app/src/Service/Venta/MediosPago/Toku/Subscription.php @@ -70,7 +70,7 @@ class Subscription extends AbstractEndPoint return compact('existingSubscriptions', 'missingVentas'); } - protected function save(array $data): bool + public function save(array $data): bool { return $this->doSave($this->subscriptionRepsitory, $data); }