2023-09-13 18:51:46 -03:00
|
|
|
<?php
|
|
|
|
namespace Incoviba\Service\Venta;
|
|
|
|
|
2024-02-13 17:59:46 -03:00
|
|
|
use Incoviba\Common\Ideal\Service;
|
2023-09-13 18:51:46 -03:00
|
|
|
use Incoviba\Common\Implement\Exception\EmptyResult;
|
|
|
|
use Incoviba\Repository;
|
|
|
|
use Incoviba\Model;
|
2024-02-13 17:59:46 -03:00
|
|
|
use Psr\Log\LoggerInterface;
|
2023-09-13 18:51:46 -03:00
|
|
|
|
2024-02-13 17:59:46 -03:00
|
|
|
class Propietario extends Service
|
2023-09-13 18:51:46 -03:00
|
|
|
{
|
|
|
|
public function __construct(
|
2024-02-13 17:59:46 -03:00
|
|
|
LoggerInterface $logger,
|
2023-09-13 18:51:46 -03:00
|
|
|
protected Repository\Venta\Propietario $propietarioRepository,
|
|
|
|
protected Repository\Direccion $direccionRepository
|
2024-02-13 17:59:46 -03:00
|
|
|
) {
|
|
|
|
parent::__construct($logger);
|
|
|
|
}
|
2023-09-13 18:51:46 -03:00
|
|
|
|
|
|
|
public function addPropietario(array $data): Model\Venta\Propietario
|
|
|
|
{
|
|
|
|
$direccion = $this->addDireccion($data);
|
|
|
|
$data['direccion'] = $direccion->id;
|
2024-02-13 17:59:46 -03:00
|
|
|
$data['dv'] = 'i';
|
2023-09-13 18:51:46 -03:00
|
|
|
|
|
|
|
if (str_contains($data['rut'], '-')) {
|
2024-02-13 17:59:46 -03:00
|
|
|
list($rut, $dv) = explode('-', $data['rut']);
|
|
|
|
$data['rut'] = $rut;
|
|
|
|
$data['dv'] = $dv;
|
2023-09-13 18:51:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
$fields = array_fill_keys([
|
|
|
|
'rut',
|
2024-02-13 17:59:46 -03:00
|
|
|
'dv',
|
2023-09-13 18:51:46 -03:00
|
|
|
'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;
|
|
|
|
}
|
|
|
|
}
|