2023-09-12
This commit is contained in:
41
app/src/Service/Money.php
Normal file
41
app/src/Service/Money.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
namespace Incoviba\Service;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Incoviba\Common\Define\Money\Provider;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResponse;
|
||||
use Incoviba\Service\Money\MiIndicador;
|
||||
|
||||
class Money
|
||||
{
|
||||
protected array $providers;
|
||||
public function register(string $name, Provider $provider): Money
|
||||
{
|
||||
if (isset($this->providers) and isset($this->providers[$name]) and $this->providers[$name] === $provider) {
|
||||
return $this;
|
||||
}
|
||||
$this->providers[$name] = $provider;
|
||||
return $this;
|
||||
}
|
||||
public function getProvider(string $name): Provider
|
||||
{
|
||||
return $this->providers[$name];
|
||||
}
|
||||
|
||||
public function getUF(DateTimeInterface $dateTime): float
|
||||
{
|
||||
try {
|
||||
return $this->getProvider('uf')->get(MiIndicador::UF, $dateTime);
|
||||
} catch (EmptyResponse) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
public function getIPC(DateTimeInterface $dateTime): float
|
||||
{
|
||||
try {
|
||||
return $this->getProvider('ipc')->get(MiIndicador::IPC, $dateTime);
|
||||
} catch (EmptyResponse) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
39
app/src/Service/Money/MiIndicador.php
Normal file
39
app/src/Service/Money/MiIndicador.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Money;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Incoviba\Common\Define\Money\Provider;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResponse;
|
||||
|
||||
class MiIndicador implements Provider
|
||||
{
|
||||
const UF = 'uf';
|
||||
const IPC = 'ipc';
|
||||
const USD = 'dolar_intercambio';
|
||||
|
||||
public function __construct(protected ClientInterface $client) {}
|
||||
|
||||
public function get(string $money_symbol, DateTimeInterface $dateTime): float
|
||||
{
|
||||
$request_uri = "{$money_symbol}/{$dateTime->format('d-m-Y')}";
|
||||
try {
|
||||
$response = $this->client->get($request_uri);
|
||||
} catch (GuzzleException) {
|
||||
throw new EmptyResponse($request_uri);
|
||||
}
|
||||
|
||||
if ((int) floor($response->getStatusCode() / 100) !== 2) {
|
||||
throw new EmptyResponse($request_uri);
|
||||
}
|
||||
|
||||
$body = $response->getBody();
|
||||
$json = json_decode($body->getContents());
|
||||
|
||||
if ($json->codigo !== $money_symbol or count($json->serie) === 0) {
|
||||
throw new EmptyResponse($request_uri);
|
||||
}
|
||||
return $json->serie[0]->valor;
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace Incoviba\Service;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Incoviba\Common\Implement;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Model;
|
||||
@ -9,7 +10,15 @@ class Venta
|
||||
{
|
||||
public function __construct(
|
||||
protected Repository\Venta $ventaRepository,
|
||||
protected Repository\Venta\EstadoVenta $estadoVentaRepository
|
||||
protected Repository\Venta\EstadoVenta $estadoVentaRepository,
|
||||
protected Repository\Venta\TipoEstadoVenta $tipoEstadoVentaRepository,
|
||||
protected Venta\Propietario $propietarioService,
|
||||
protected Venta\Propiedad $propiedadService,
|
||||
protected Venta\Pie $pieService,
|
||||
protected Venta\Subsidio $subsidioService,
|
||||
protected Venta\Credito $creditoService,
|
||||
protected Venta\BonoPie $bonoPieService,
|
||||
protected Money $moneyService
|
||||
) {}
|
||||
|
||||
public function getById(int $venta_id): Model\Venta
|
||||
@ -34,8 +43,220 @@ class Venta
|
||||
public function getByProyectoAndUnidad(string $proyecto_nombre, int $unidad_descripcion): Model\Venta
|
||||
{
|
||||
$venta = $this->ventaRepository->fetchByProyectoAndUnidad($proyecto_nombre, $unidad_descripcion);
|
||||
$venta->addFactory('estados', ['callable' => [$this->estadoVentaRepository, 'fetchByVenta'], 'args' => [$venta->id]]);
|
||||
$venta->addFactory('currentEstado', ['callable' => [$this->estadoVentaRepository, 'fetchCurrentByVenta'], 'args' => [$venta->id]]);
|
||||
$venta->addFactory('estados', (new Implement\Repository\Factory())->setCallable([$this->estadoVentaRepository, 'fetchByVenta'])->setArgs([$venta->id]));
|
||||
$venta->addFactory('currentEstado', (new Implement\Repository\Factory())->setCallable([$this->estadoVentaRepository, 'fetchCurrentByVenta'])->setArgs([$venta->id]));
|
||||
return $venta;
|
||||
}
|
||||
|
||||
public function add(array $data): void
|
||||
{
|
||||
$fecha = new DateTimeImmutable($data['fecha_venta']);
|
||||
$data['uf'] = $this->moneyService->getUF($fecha);
|
||||
$propietario = $this->addPropietario($data);
|
||||
$propiedad = $this->addPropiedad($data);
|
||||
$forma_pago = $this->addFormaPago($data);
|
||||
$venta_data = [
|
||||
'propietario' => $propietario->rut,
|
||||
'propiedad' => $propiedad->id,
|
||||
'fecha' => $fecha->format('Y-m-d'),
|
||||
'valor_uf' => $data['valor'],
|
||||
'fecha_ingreso' => (new DateTimeImmutable())->format('Y-m-d'),
|
||||
'uf' => $data['uf']
|
||||
];
|
||||
$map = ['pie', 'subsidio', 'credito', 'bono_pie'];
|
||||
foreach ($map as $field) {
|
||||
$name = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $field))));
|
||||
if (isset($forma_pago->{$name})) {
|
||||
$venta_data[$field] = $forma_pago->{$name}->id;
|
||||
}
|
||||
}
|
||||
$venta = $this->ventaRepository->create($venta_data);
|
||||
$venta = $this->ventaRepository->save($venta);
|
||||
$tipoEstado = $this->tipoEstadoVentaRepository->fetchByDescripcion('vigente');
|
||||
$estado = $this->estadoVentaRepository->create([
|
||||
'venta' => $venta->id,
|
||||
'estado' => $tipoEstado->id,
|
||||
'fecha' => $venta->fecha->format('Y-m-d')
|
||||
]);
|
||||
$this->estadoVentaRepository->save($estado);
|
||||
}
|
||||
protected function addPropietario(array $data): Model\Venta\Propietario
|
||||
{
|
||||
if (isset($data['natural_uno'])) {
|
||||
if (isset($data['natural_multiple'])) {
|
||||
return $this->addDosPropietarios($data);
|
||||
}
|
||||
return $this->addUnPropietario($data);
|
||||
}
|
||||
return $this->addSociedad($data);
|
||||
}
|
||||
protected function addUnPropietario(array $data): Model\Venta\Propietario
|
||||
{
|
||||
$fields = array_fill_keys([
|
||||
'rut',
|
||||
'nombres',
|
||||
'apellido_paterno',
|
||||
'apellido_materno',
|
||||
'calle',
|
||||
'numero',
|
||||
'extra',
|
||||
'comuna'
|
||||
], 0);
|
||||
$filtered_data = array_intersect_key($data, $fields);
|
||||
return $this->propietarioService->addPropietario($filtered_data);
|
||||
}
|
||||
protected function addDosPropietarios(array $data): Model\Venta\Propietario
|
||||
{
|
||||
$fields = array_fill_keys([
|
||||
'rut_otro',
|
||||
'nombres_otro',
|
||||
'apellido_paterno_otro',
|
||||
'apellido_materno_otro',
|
||||
'calle_otro',
|
||||
'numero_otro',
|
||||
'extra_otro',
|
||||
'comuna_otro'
|
||||
], 0);
|
||||
$filtered_data = array_intersect_key($data, $fields);
|
||||
$mapped_data = array_combine([
|
||||
'rut',
|
||||
'nombres',
|
||||
'apellido_paterno',
|
||||
'apellido_materno',
|
||||
'calle',
|
||||
'numero',
|
||||
'extra',
|
||||
'comuna'
|
||||
], $filtered_data);
|
||||
$otro = $this->propietarioService->addPropietario($mapped_data);
|
||||
|
||||
$data['otro'] = $otro->rut;
|
||||
$fields = array_fill_keys([
|
||||
'rut',
|
||||
'nombres',
|
||||
'apellido_paterno',
|
||||
'apellido_materno',
|
||||
'calle',
|
||||
'numero',
|
||||
'extra',
|
||||
'comuna',
|
||||
'otro'
|
||||
], 0);
|
||||
$filtered_data = array_intersect_key($data, $fields);
|
||||
return $this->propietarioService->addPropietario($filtered_data);
|
||||
}
|
||||
protected function addSociedad(array $data): Model\Venta\Propietario
|
||||
{
|
||||
$representante = $this->addUnPropietario($data);
|
||||
|
||||
$data['representante'] = $representante->rut;
|
||||
$fields = array_fill_keys([
|
||||
'rut_sociedad',
|
||||
'razon_social',
|
||||
'calle_comercial',
|
||||
'numero_comercial',
|
||||
'extra_comercial',
|
||||
'comuna_comercial',
|
||||
'representante'
|
||||
], 0);
|
||||
$filtered_data = array_intersect_key($data, $fields);
|
||||
$mapped_data = array_combine([
|
||||
'rut',
|
||||
'razon_social',
|
||||
'calle',
|
||||
'numero',
|
||||
'extra',
|
||||
'comuna',
|
||||
'representante'
|
||||
], $filtered_data);
|
||||
return $this->propietarioService->addSociedad($mapped_data);
|
||||
}
|
||||
protected function addPropiedad(array $data): Model\Venta\Propiedad
|
||||
{
|
||||
$ids = array_filter($data, function($key) {
|
||||
return str_contains($key, 'unidad');
|
||||
}, ARRAY_FILTER_USE_KEY);
|
||||
|
||||
return $this->propiedadService->addPropiedad($ids);
|
||||
}
|
||||
protected function addFormaPago(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}"])) {
|
||||
$method = 'add' . str_replace(' ', '', ucwords(str_replace('_', ' ', $name)));
|
||||
$obj = $this->{$method}($data);
|
||||
$forma_pago->{$name} = $obj;
|
||||
}
|
||||
}
|
||||
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);
|
||||
$mapped_data = array_combine([
|
||||
'fecha',
|
||||
'valor',
|
||||
'cuotas',
|
||||
'uf'
|
||||
], $filtered_data);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
15
app/src/Service/Venta/BonoPie.php
Normal file
15
app/src/Service/Venta/BonoPie.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Venta;
|
||||
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Model;
|
||||
|
||||
class BonoPie
|
||||
{
|
||||
public function __construct(protected Repository\Venta\BonoPie $bonoPieRepository) {}
|
||||
|
||||
public function add(array $data): Model\Venta\BonoPie
|
||||
{
|
||||
return new Model\Venta\BonoPie();
|
||||
}
|
||||
}
|
47
app/src/Service/Venta/Credito.php
Normal file
47
app/src/Service/Venta/Credito.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Venta;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Service\Money;
|
||||
|
||||
class Credito
|
||||
{
|
||||
public function __construct(
|
||||
protected Repository\Venta\Credito $creditoRepository,
|
||||
protected Repository\Venta\Pago $pagoRepository,
|
||||
protected Repository\Venta\TipoPago $tipoPagoRepository,
|
||||
protected Repository\Venta\EstadoPago $estadoPagoRepository,
|
||||
protected Repository\Venta\TipoEstadoPago $tipoEstadoPagoRepository,
|
||||
protected Money $moneyService
|
||||
) {}
|
||||
|
||||
public function add(array $data): Model\Venta\Credito
|
||||
{
|
||||
$fecha = new DateTimeImmutable($data['fecha']);
|
||||
$uf = $data['uf'] ?? $this->moneyService->getUF($fecha);
|
||||
$tipoPago = $this->tipoPagoRepository->fetchByDescripcion('carta de resguardo');
|
||||
$pago = $this->addPago(['fecha' => $fecha->format('Y-m-d'), 'valor' => $data['valor'] * $uf, 'uf' => $uf, 'tipo' => $tipoPago->id]);
|
||||
$credito = $this->creditoRepository->create([
|
||||
'valor' => $data['valor'],
|
||||
'fecha' => $fecha->format('Y-m-d'),
|
||||
'pago' => $pago->id
|
||||
]);
|
||||
return $this->creditoRepository->save($credito);
|
||||
}
|
||||
protected function addPago(array $data): Model\Venta\Pago
|
||||
{
|
||||
$pago = $this->pagoRepository->create($data);
|
||||
$pago = $this->pagoRepository->save($pago);
|
||||
$tipoEstado = $this->tipoEstadoPagoRepository->fetchByDescripcion('no pagado');
|
||||
$data = [
|
||||
'pago' => $pago->id,
|
||||
'fecha' => $pago->fecha->format('Y-m-d'),
|
||||
'estado' => $tipoEstado->id
|
||||
];
|
||||
$estado = $this->estadoPagoRepository->create($data);
|
||||
$this->estadoPagoRepository->save($estado);
|
||||
return $pago;
|
||||
}
|
||||
}
|
@ -5,10 +5,15 @@ use DateTimeImmutable;
|
||||
use DateInterval;
|
||||
use IntlDateFormatter;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Model;
|
||||
|
||||
class Cuota
|
||||
{
|
||||
public function __construct(protected Repository\Venta\Cuota $cuotaRepository) {}
|
||||
public function __construct(
|
||||
protected Repository\Venta\Cuota $cuotaRepository,
|
||||
protected Pago $pagoService,
|
||||
protected Repository\Venta\TipoPago $tipoPagoRepository
|
||||
) {}
|
||||
|
||||
public function pendientes(): array
|
||||
{
|
||||
@ -68,4 +73,41 @@ class Cuota
|
||||
}
|
||||
return $cuotas_depositadas;
|
||||
}
|
||||
public function getVigenteByPie(int $pie_id): array
|
||||
{
|
||||
return $this->cuotaRepository->fetchVigenteByPie($pie_id);
|
||||
}
|
||||
|
||||
public function add(array $data): Model\Venta\Cuota
|
||||
{
|
||||
$tipoPago = $this->tipoPagoRepository->fetchByDescripcion('cheque');
|
||||
$fields = array_fill_keys([
|
||||
'fecha',
|
||||
'banco',
|
||||
'valor',
|
||||
'identificador'
|
||||
], 0);
|
||||
$filtered_data = array_intersect_key($data, $fields);
|
||||
$pago_data = array_merge($filtered_data, ['tipo' => $tipoPago->id]);
|
||||
$pago = $this->pagoService->add($pago_data);
|
||||
$data['pago'] = $pago->id;
|
||||
$data['estado'] = $pago->currentEstado->tipoEstadoPago->id;
|
||||
$fields = array_fill_keys([
|
||||
'pie',
|
||||
'fecha',
|
||||
'valor',
|
||||
'estado',
|
||||
'banco',
|
||||
'uf',
|
||||
'pago',
|
||||
'numero'
|
||||
], 0);
|
||||
$filtered_data = array_intersect_key($data, $fields);
|
||||
$mapped_data = $filtered_data;
|
||||
$mapped_data['valor_$'] = $mapped_data['valor'];
|
||||
unset($mapped_data['valor']);
|
||||
$cuota = $this->cuotaRepository->create($mapped_data);
|
||||
$this->cuotaRepository->save($cuota);
|
||||
return $cuota;
|
||||
}
|
||||
}
|
||||
|
@ -3,15 +3,20 @@ namespace Incoviba\Service\Venta;
|
||||
|
||||
use DateTimeInterface;
|
||||
use DateTimeImmutable;
|
||||
use Incoviba\Service\Money;
|
||||
use PDOException;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Service;
|
||||
|
||||
class Pago
|
||||
{
|
||||
public function __construct(protected Repository\Venta\Pago $pagoRepository,
|
||||
protected Repository\Venta\EstadoPago $estadoPagoRepository,
|
||||
protected Repository\Venta\TipoEstadoPago $tipoEstadoPagoRepository) {}
|
||||
public function __construct(
|
||||
protected Repository\Venta\Pago $pagoRepository,
|
||||
protected Repository\Venta\EstadoPago $estadoPagoRepository,
|
||||
protected Repository\Venta\TipoEstadoPago $tipoEstadoPagoRepository,
|
||||
protected Service\Money $moneyService
|
||||
) {}
|
||||
|
||||
public function depositar(Model\Venta\Pago $pago, DateTimeInterface $fecha): bool
|
||||
{
|
||||
@ -66,6 +71,12 @@ class Pago
|
||||
$pago = $this->pagoRepository->fetchById($pago_id);
|
||||
$pago->estados = $this->estadoPagoRepository->fetchByPago($pago_id);
|
||||
$pago->currentEstado = $this->estadoPagoRepository->fetchCurrentByPago($pago_id);
|
||||
if (($pago->uf === null or $pago->uf === 0.0) and $pago->fecha < new DateTimeImmutable()) {
|
||||
$pago->uf = $this->moneyService->getUF($pago->fecha);
|
||||
if ($pago->uf !== 0.0) {
|
||||
$this->pagoRepository->edit($pago, ['uf' => $pago->uf]);
|
||||
}
|
||||
}
|
||||
return $pago;
|
||||
}
|
||||
|
||||
@ -81,4 +92,33 @@ class Pago
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function add(array $data): Model\Venta\Pago
|
||||
{
|
||||
if (!isset($data['uf'])) {
|
||||
$data['uf'] = $this->moneyService->getUF(new DateTimeImmutable($data['fecha']));
|
||||
}
|
||||
$fields = array_fill_keys([
|
||||
'valor',
|
||||
'banco',
|
||||
'tipo',
|
||||
'identificador',
|
||||
'fecha',
|
||||
'uf',
|
||||
'pagador',
|
||||
'asociado'
|
||||
], 0);
|
||||
$filtered_data = array_intersect_key($data, $fields);
|
||||
$pago = $this->pagoRepository->create($filtered_data);
|
||||
$pago = $this->pagoRepository->save($pago);
|
||||
$tipoEstado = $this->tipoEstadoPagoRepository->fetchByDescripcion('no pagado');
|
||||
$estado = $this->estadoPagoRepository->create([
|
||||
'pago' => $pago->id,
|
||||
'fecha' => $pago->fecha->format('Y-m-d'),
|
||||
'estado' => $tipoEstado->id
|
||||
]);
|
||||
$estado = $this->estadoPagoRepository->save($estado);
|
||||
$pago->currentEstado = $estado;
|
||||
return $pago;
|
||||
}
|
||||
}
|
||||
|
@ -8,13 +8,23 @@ class Pie
|
||||
{
|
||||
public function __construct(
|
||||
protected Repository\Venta\Pie $pieRepository,
|
||||
protected Repository\Venta\Cuota $cuotaRepository
|
||||
protected Cuota $cuotaService
|
||||
) {}
|
||||
|
||||
public function getById(int $pie_id): Model\Venta\Pie
|
||||
{
|
||||
$pie = $this->pieRepository->fetchById($pie_id);
|
||||
$pie->cuotasArray = $this->cuotaRepository->fetchVigenteByPie($pie_id);
|
||||
$pie->cuotasArray = $this->cuotaService->getVigenteByPie($pie_id);
|
||||
return $pie;
|
||||
}
|
||||
|
||||
public function add(array $data): Model\Venta\Pie
|
||||
{
|
||||
$pie = $this->pieRepository->create($data);
|
||||
return $this->pieRepository->save($pie);
|
||||
}
|
||||
public function addCuota(array $data): Model\Venta\Cuota
|
||||
{
|
||||
return $this->cuotaService->add($data);
|
||||
}
|
||||
}
|
||||
|
87
app/src/Service/Venta/Propiedad.php
Normal file
87
app/src/Service/Venta/Propiedad.php
Normal file
@ -0,0 +1,87 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Venta;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Model;
|
||||
|
||||
class Propiedad
|
||||
{
|
||||
public function __construct(
|
||||
protected Repository\Venta\Propiedad $propiedadRepository,
|
||||
protected Repository\Venta\Unidad $unidadRepository,
|
||||
protected Define\Connection $connection
|
||||
) {}
|
||||
|
||||
public function addPropiedad(array $ids): Model\Venta\Propiedad
|
||||
{
|
||||
$unidades = [];
|
||||
foreach ($ids as $unidad_id) {
|
||||
$unidades []= $this->unidadRepository->fetchById($unidad_id);
|
||||
}
|
||||
usort($unidades, function(Model\Venta\Unidad $a, Model\Venta\Unidad $b) {
|
||||
$t = $a->proyectoTipoUnidad->tipoUnidad->orden - $b->proyectoTipoUnidad->tipoUnidad->orden;
|
||||
if ($t === 0) {
|
||||
return strcmp(str_pad($a->descripcion, 4, '0', STR_PAD_LEFT), str_pad($b->descripcion, 4, '0', STR_PAD_LEFT));
|
||||
}
|
||||
return $t;
|
||||
});
|
||||
try {
|
||||
$propiedad = $this->propiedadRepository->fetchVigenteByUnidad($unidades[0]->id);
|
||||
} catch (EmptyResult) {
|
||||
$propiedad = $this->propiedadRepository->create([
|
||||
'unidad_principal' => $unidades[0]->id,
|
||||
'estado' => 1
|
||||
]);
|
||||
$propiedad = $this->propiedadRepository->save($propiedad);
|
||||
}
|
||||
$this->addUnidades($propiedad, $unidades);
|
||||
$this->cleanUpUnidades($propiedad, $unidades);
|
||||
|
||||
return $propiedad;
|
||||
}
|
||||
protected function addUnidades(Model\Venta\Propiedad $propiedad, array $unidades): void
|
||||
{
|
||||
$query = "SELECT 1 FROM `propiedad_unidad` WHERE `propiedad` = ? AND `unidad` = ?";
|
||||
$statement = $this->connection->prepare($query);
|
||||
$query2 = "INSERT INTO `propiedad_unidad` (`propiedad`, `unidad`, `principal`) VALUES (?, ?, ?)";
|
||||
$insert = $this->connection->prepare($query2);
|
||||
foreach ($unidades as $ix => $unidad) {
|
||||
try {
|
||||
$statement->execute([$propiedad->id, $unidad->id]);
|
||||
$result = $statement->fetch(PDO::FETCH_ASSOC);
|
||||
if (!$result) {
|
||||
throw new EmptyResult($query);
|
||||
}
|
||||
} catch (PDOException|EmptyResult) {
|
||||
$insert->execute([$propiedad->id, $unidad->id, ($ix === 0) ? 1 : 0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
protected function cleanUpUnidades(Model\Venta\Propiedad $propiedad, array $unidades): void
|
||||
{
|
||||
$query = "SELECT `unidad` FROM `propiedad_unidad` WHERE `propiedad` = ?";
|
||||
$statement = $this->connection->prepare($query);
|
||||
$statement->execute([$propiedad->id]);
|
||||
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$results) {
|
||||
return;
|
||||
}
|
||||
|
||||
$all_ids = array_map(function($row) {return $row['unidad'];}, $results);
|
||||
$new_ids = array_map(function(Model\Venta\Unidad $unidad) {return $unidad->id;}, $unidades);
|
||||
$diff = array_diff($all_ids, $new_ids);
|
||||
if (count($diff) === 0) {
|
||||
return;
|
||||
}
|
||||
$query = "DELECT FROM `propiedad_unidad` WHERE `propiedad` = ? AND `unidad` = ?";
|
||||
$statement = $this->connection->prepare($query);
|
||||
foreach ($diff as $id) {
|
||||
$statement->execute([$propiedad->id, $id]);
|
||||
}
|
||||
}
|
||||
}
|
102
app/src/Service/Venta/Propietario.php
Normal file
102
app/src/Service/Venta/Propietario.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Venta;
|
||||
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Model;
|
||||
|
||||
class Propietario
|
||||
{
|
||||
public function __construct(
|
||||
protected Repository\Venta\Propietario $propietarioRepository,
|
||||
protected Repository\Direccion $direccionRepository
|
||||
) {}
|
||||
|
||||
public function addPropietario(array $data): Model\Venta\Propietario
|
||||
{
|
||||
$direccion = $this->addDireccion($data);
|
||||
$data['direccion'] = $direccion->id;
|
||||
|
||||
if (str_contains($data['rut'], '-')) {
|
||||
$data['rut'] = explode('-', $data['rut'])[0];
|
||||
}
|
||||
|
||||
$fields = array_fill_keys([
|
||||
'rut',
|
||||
'nombres',
|
||||
'apellido_paterno',
|
||||
'apellido_materno',
|
||||
'direccion'
|
||||
], 0);
|
||||
$filtered_data = array_intersect_key($data, $fields);
|
||||
|
||||
try {
|
||||
$propietario = $this->propietarioRepository->fetchById($data['rut']);
|
||||
$edits = [];
|
||||
if ($propietario->datos->direccion->id !== $filtered_data['direccion']) {
|
||||
$edits['direccion'] = $filtered_data['direccion'];
|
||||
}
|
||||
$propietario = $this->propietarioRepository->edit($propietario, $edits);
|
||||
} catch (EmptyResult) {
|
||||
$propietario = $this->propietarioRepository->create($filtered_data);
|
||||
$propietario = $this->propietarioRepository->save($propietario);
|
||||
}
|
||||
return $propietario;
|
||||
}
|
||||
public function addSociedad(array $data): Model\Venta\Propietario
|
||||
{
|
||||
$direccion = $this->addDireccion($data);
|
||||
$data['direccion'] = $direccion->id;
|
||||
|
||||
if (str_contains($data['rut'], '-')) {
|
||||
$data['rut'] = explode('-', $data['rut'])[0];
|
||||
}
|
||||
|
||||
$fields = array_fill_keys([
|
||||
'rut',
|
||||
'razon_social',
|
||||
'direccion',
|
||||
'representante'
|
||||
], 0);
|
||||
$filtered_data = array_intersect_key($data, $fields);
|
||||
$mapped_data = array_combine([
|
||||
'rut',
|
||||
'nombres',
|
||||
'direccion',
|
||||
'representante'
|
||||
], $filtered_data);
|
||||
|
||||
try {
|
||||
$sociedad = $this->propietarioRepository->fetchById($data['rut']);
|
||||
$edits = [];
|
||||
if ($sociedad->datos->direccion->id !== $mapped_data['direccion']) {
|
||||
$edits['direccion'] = $mapped_data['direccion'];
|
||||
}
|
||||
if ($sociedad->representante->rut !== $mapped_data['representante']) {
|
||||
$edits['representante'] = $mapped_data['representante'];
|
||||
}
|
||||
$sociedad = $this->propietarioRepository->edit($sociedad, $edits);
|
||||
} catch (EmptyResult) {
|
||||
$sociedad = $this->propietarioRepository->create($mapped_data);
|
||||
$sociedad = $this->propietarioRepository->save($sociedad);
|
||||
}
|
||||
return $sociedad;
|
||||
}
|
||||
protected function addDireccion(array $data): Model\Direccion
|
||||
{
|
||||
$fields = array_fill_keys([
|
||||
'calle',
|
||||
'numero',
|
||||
'extra',
|
||||
'comuna'
|
||||
], 0);
|
||||
$filtered_data = array_intersect_key($data, $fields);
|
||||
try {
|
||||
$direccion = $this->direccionRepository->fetchByCalleAndNumeroAndExtra($filtered_data['calle'], $filtered_data['numero'], $filtered_data['extra']);
|
||||
} catch (EmptyResult) {
|
||||
$direccion = $this->direccionRepository->create($filtered_data);
|
||||
$direccion = $this->direccionRepository->save($direccion);
|
||||
}
|
||||
return $direccion;
|
||||
}
|
||||
}
|
44
app/src/Service/Venta/Subsidio.php
Normal file
44
app/src/Service/Venta/Subsidio.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Venta;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Service;
|
||||
|
||||
class Subsidio
|
||||
{
|
||||
public function __construct(
|
||||
protected Repository\Venta\Subsidio $subsidioRepository,
|
||||
protected Repository\Venta\Pago $pagoRepository,
|
||||
protected Repository\Venta\TipoPago $tipoPagoRepository,
|
||||
protected Repository\Venta\EstadoPago $estadoPagoRepository,
|
||||
protected Repository\Venta\TipoEstadoPago $tipoEstadoPagoRepository,
|
||||
protected Service\Money $moneyService
|
||||
) {}
|
||||
|
||||
public function add(array $data): Model\Venta\Subsidio
|
||||
{
|
||||
$fecha = new DateTimeImmutable($data['fecha']);
|
||||
$uf = $data['uf'] ?? $this->moneyService->getUF($fecha);
|
||||
$tipoPago = $this->tipoPagoRepository->fetchByDescripcion('vale vista');
|
||||
$ahorro = $this->addPago(['fecha' => $fecha->format('Y-m-d'), 'valor' => $data['ahorro'] * $uf, 'uf' => $uf, 'tipo' => $tipoPago->id]);
|
||||
$subsidio = $this->addPago(['fecha' => $fecha->format('Y-m-d'), 'valor' => $data['subsidio'] * $uf, 'uf' => $uf, 'tipo' => $tipoPago->id]);
|
||||
$subsidio = $this->subsidioRepository->create(['pago' => $ahorro->id, 'subsidio' => $subsidio->id]);
|
||||
return $this->subsidioRepository->save($subsidio);
|
||||
}
|
||||
protected function addPago(array $data): Model\Venta\Pago
|
||||
{
|
||||
$pago = $this->pagoRepository->create($data);
|
||||
$pago = $this->pagoRepository->save($pago);
|
||||
$tipoEstado = $this->tipoEstadoPagoRepository->fetchByDescripcion('no pagado');
|
||||
$data = [
|
||||
'pago' => $pago->id,
|
||||
'fecha' => $pago->fecha->format('Y-m-d'),
|
||||
'estado' => $tipoEstado->id
|
||||
];
|
||||
$estado = $this->estadoPagoRepository->create($data);
|
||||
$this->estadoPagoRepository->save($estado);
|
||||
return $pago;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user