FormaPago Service

This commit is contained in:
Juan Pablo Vial
2024-03-13 14:38:44 -03:00
parent c7dd309185
commit 98953cce42
21 changed files with 261 additions and 71 deletions

View File

@ -12,4 +12,8 @@ class BonoPie
{
return new Model\Venta\BonoPie();
}
public function getByVenta(int $venta_id): Model\Venta\BonoPie
{
return $this->bonoPieRepository->fetchByVenta($venta_id);
}
}

View File

@ -22,6 +22,11 @@ class Credito extends Service
parent::__construct($logger);
}
public function getByVenta(int $venta_id): Model\Venta\Credito
{
return $this->creditoRepository->fetchByVenta($venta_id);
}
public function add(array $data): Model\Venta\Credito
{
$fecha = new DateTimeImmutable($data['fecha']);

View File

@ -0,0 +1,143 @@
<?php
namespace Incoviba\Service\Venta;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Implement;
use Incoviba\Common\Ideal;
use Incoviba\Model;
use Incoviba\Repository;
class FormaPago extends Ideal\Service
{
public function __construct(LoggerInterface $logger,
protected Pie $pieService,
protected BonoPie $bonoPieService,
protected Credito $creditoService,
protected Repository\Venta\Escritura $escrituraRepository,
protected Subsidio $subsidioService,
protected Pago $pagoService)
{
parent::__construct($logger);
}
public function getByVenta(int $venta_id): Model\Venta\FormaPago
{
$formaPago = new Model\Venta\FormaPago();
try {
$formaPago->pie = $this->pieService->getByVenta($venta_id);
} catch (Implement\Exception\EmptyResult) {}
try {
$formaPago->bonoPie = $this->bonoPieService->getByVenta($venta_id);
} catch (Implement\Exception\EmptyResult) {}
try {
$formaPago->credito = $this->creditoService->getByVenta($venta_id);
} catch (Implement\Exception\EmptyResult) {}
try {
$formaPago->escritura = $this->escrituraRepository->fetchByVenta($venta_id);
} catch (Implement\Exception\EmptyResult) {}
try {
$formaPago->subsidio = $this->subsidioService->getByVenta($venta_id);
} catch (Implement\Exception\EmptyResult) {}
try {
$formaPago->devolucion = $this->pagoService->getDevolucionByVenta($venta_id);
} catch (Implement\Exception\EmptyResult) {}
return $formaPago;
}
public function add(array $data): Model\Venta\FormaPago
{
$fields = [
'pie',
'subsidio',
'credito',
'bono_pie'
];
$forma_pago = new Model\Venta\FormaPago();
foreach ($fields as $name) {
if (isset($data["has_{$name}"])) {
try {
$method = 'add' . str_replace(' ', '', ucwords(str_replace('_', ' ', $name)));
$obj = $this->{$method}($data);
$forma_pago->{$name} = $obj;
} catch (\Error $error) {
$this->logger->critical($error);
}
}
}
return $forma_pago;
}
protected function addPie(array $data): Model\Venta\Pie
{
$fields = array_fill_keys([
'fecha_venta',
'pie',
'cuotas',
'uf'
], 0);
$filtered_data = array_intersect_key($data, $fields);
$this->logger->critical(var_export($filtered_data,true));
$mapped_data = array_combine([
'fecha',
'valor',
'cuotas',
'uf'
], $filtered_data);
$mapped_data['valor'] = $this->cleanValue($mapped_data['valor']);
return $this->pieService->add($mapped_data);
}
protected function addSubsidio(array $data): Model\Venta\Subsidio
{
$fields = array_fill_keys([
'fecha_venta',
'ahorro',
'subsidio',
'uf'
], 0);
$filtered_data = array_intersect_key($data, $fields);
$mapped_data = array_combine([
'fecha',
'ahorro',
'subsidio',
'uf'
], $filtered_data);
return $this->subsidioService->add($mapped_data);
}
protected function addCredito(array $data): Model\Venta\Credito
{
$fields = array_fill_keys([
'fecha_venta',
'credito',
'uf'
], 0);
$filtered_data = array_intersect_key($data, $fields);
$mapped_data = array_combine([
'fecha',
'valor',
'uf'
], $filtered_data);
return $this->creditoService->add($mapped_data);
}
protected function addBonoPie(array $data): Model\Venta\BonoPie
{
$fields = array_fill_keys([
'fecha_venta',
'bono_pie'
], 0);
$filtered_data = array_intersect_key($data, $fields);
$mapped_data = array_combine([
'fecha',
'valor'
], $filtered_data);
return $this->bonoPieService->add($mapped_data);
}
protected function cleanValue($value): float
{
if ((float) $value == $value) {
return (float) $value;
}
return (float) str_replace(['.', ','], ['', '.'], $value);
}
}

View File

@ -109,6 +109,10 @@ class Pago
{
return [];
}
public function getDevolucionByVenta(int $venta_id): Model\Venta\Pago
{
return $this->process($this->pagoRepository->fetchDevolucionByVenta($venta_id));
}
public function add(array $data): Model\Venta\Pago
{

View File

@ -17,6 +17,10 @@ class Pie
{
return $this->process($this->pieRepository->fetchById($pie_id));
}
public function getByVenta(int $venta_id): Model\Venta\Pie
{
return $this->process($this->pieRepository->fetchByVenta($venta_id));
}
public function add(array $data): Model\Venta\Pie
{

View File

@ -17,6 +17,11 @@ class Subsidio
protected Service\Money $moneyService
) {}
public function getByVenta(int $venta_id): Model\Venta\Subsidio
{
return $this->subsidioRepository->fetchByVenta($venta_id);
}
public function add(array $data): Model\Venta\Subsidio
{
$fecha = new DateTimeImmutable($data['fecha']);