2023-09-12
This commit is contained in:
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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user