Agregar datos propietario

This commit is contained in:
Juan Pablo Vial
2025-05-15 15:23:34 -04:00
parent e33edc4d7b
commit 03317b3aa5
10 changed files with 441 additions and 132 deletions

View File

@ -4,7 +4,8 @@ namespace Incoviba\Service;
use Exception;
use DateTimeImmutable;
use DateMalformedStringException;
use Incoviba\Exception\ServiceAction\{Read, Update};
use Incoviba\Exception\ServiceAction\{Create, Read, Update};
use PDOException;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal\Service;
use Incoviba\Common\Implement;
@ -225,15 +226,24 @@ class Venta extends Service
}
/**
* @throws Exception
* @throws Create|Update
*/
public function add(array $data): Model\Venta
{
$fecha = new DateTimeImmutable($data['fecha_venta']);
try {
$fecha = new DateTimeImmutable($data['fecha_venta']);
} catch (DateMalformedStringException $exception) {
throw new Create(__CLASS__, $exception);
}
$data['uf'] = $this->ufService->get($fecha);
$propietario = $this->addPropietario($data);
$propiedad = $this->addPropiedad($data);
$formaPago = $this->addFormaPago($data);
try {
$formaPago = $this->addFormaPago($data);
} catch (Create) {}
$venta_data = [
'propietario' => $propietario->rut,
'propiedad' => $propiedad->id,
@ -242,31 +252,46 @@ class Venta extends Service
'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($formaPago->{$name})) {
$venta_data[$field] = $formaPago->{$name}->id;
if (isset($formaPago)) {
$map = ['pie', 'subsidio', 'credito', 'bono_pie'];
foreach ($map as $field) {
$name = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $field))));
if (isset($formaPago->{$name})) {
$venta_data[$field] = $formaPago->{$name}->id;
}
}
}
try {
return $this->ventaRepository->fetchByPropietarioAndPropiedad($propietario->rut, $propiedad->id);
} catch (Implement\Exception\EmptyResult) {
$venta = $this->ventaRepository->create($venta_data);
$venta->setFormaPago($formaPago);
if (isset($formaPago)) {
$venta->setFormaPago($formaPago);
}
$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);
try {
$tipoEstado = $this->tipoEstadoVentaRepository->fetchByDescripcion('vigente');
try {
$estado = $this->estadoVentaRepository->create([
'venta' => $venta->id,
'estado' => $tipoEstado->id,
'fecha' => $venta->fecha->format('Y-m-d')
]);
$this->estadoVentaRepository->save($estado);
} catch (PDOException) {}
} catch (Implement\Exception\EmptyResult) {}
return $venta;
}
}
/**
* @param array $data
* @return Model\Venta\Propietario
* @throws Create|Update
*/
protected function addPropietario(array $data): Model\Venta\Propietario
{
if (isset($data['natural_uno'])) {
@ -277,9 +302,15 @@ class Venta extends Service
}
return $this->addSociedad($data);
}
/**
* @param array $data
* @return Model\Venta\Propietario
* @throws Create
*/
protected function addUnPropietario(array $data): Model\Venta\Propietario
{
$fields = array_fill_keys([
$fields = array_flip([
'rut',
'nombres',
'apellido_paterno',
@ -287,14 +318,22 @@ class Venta extends Service
'calle',
'numero',
'extra',
'comuna'
], 0);
'comuna',
'email',
'telefono'
]);
$filtered_data = array_intersect_key($data, $fields);
return $this->propietarioService->addPropietario($filtered_data);
}
/**
* @param array $data
* @return Model\Venta\Propietario
* @throws Create
*/
protected function addDosPropietarios(array $data): Model\Venta\Propietario
{
$fields = array_fill_keys([
$fields = array_flip([
'rut_otro',
'nombres_otro',
'apellido_paterno_otro',
@ -302,8 +341,10 @@ class Venta extends Service
'calle_otro',
'numero_otro',
'extra_otro',
'comuna_otro'
], 0);
'comuna_otro',
'email_otro',
'telefono_otro'
]);
$filtered_data = array_intersect_key($data, $fields);
$mapped_data = array_combine([
'rut',
@ -313,12 +354,14 @@ class Venta extends Service
'calle',
'numero',
'extra',
'comuna'
'comuna',
'email',
'telefono'
], $filtered_data);
$otro = $this->propietarioService->addPropietario($mapped_data);
$data['otro'] = $otro->rut;
$fields = array_fill_keys([
$fields = array_flip([
'rut',
'nombres',
'apellido_paterno',
@ -327,17 +370,26 @@ class Venta extends Service
'numero',
'extra',
'comuna',
'otro'
], 0);
'otro',
'email',
'telefono'
]);
$filtered_data = array_intersect_key($data, $fields);
return $this->propietarioService->addPropietario($filtered_data);
}
/**
* @param array $data
* @return Model\Venta\Propietario
* @throws Create
* @throws Update
*/
protected function addSociedad(array $data): Model\Venta\Propietario
{
$representante = $this->addUnPropietario($data);
$data['representante'] = $representante->rut;
$fields = array_fill_keys([
$fields = array_flip([
'rut_sociedad',
'razon_social',
'calle_comercial',
@ -345,7 +397,7 @@ class Venta extends Service
'extra_comercial',
'comuna_comercial',
'representante'
], 0);
]);
$filtered_data = array_intersect_key($data, $fields);
$mapped_data = array_combine([
'rut',
@ -358,6 +410,12 @@ class Venta extends Service
], $filtered_data);
return $this->propietarioService->addSociedad($mapped_data);
}
/**
* @param array $data
* @return Model\Venta\Propiedad
* @throws Create
*/
protected function addPropiedad(array $data): Model\Venta\Propiedad
{
$ids = array_filter($data, function($key) {
@ -366,6 +424,12 @@ class Venta extends Service
return $this->propiedadService->addPropiedad($ids);
}
/**
* @param array $data
* @return Model\Venta\FormaPago
* @throws Create
*/
protected function addFormaPago(array $data): Model\Venta\FormaPago
{
return $this->formaPagoService->add($data);