Files
oficial/app/src/Service/Venta/Propietario.php
2025-05-15 15:23:34 -04:00

187 lines
5.9 KiB
PHP

<?php
namespace Incoviba\Service\Venta;
use Incoviba\Common\Ideal\Service;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Exception\ServiceAction\Create;
use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Exception\ServiceAction\Update;
use Incoviba\Repository;
use Incoviba\Model;
use PDOException;
use Psr\Log\LoggerInterface;
class Propietario extends Service
{
public function __construct(
LoggerInterface $logger,
protected Repository\Venta\Propietario $propietarioRepository,
protected Repository\Direccion $direccionRepository
) {
parent::__construct($logger);
}
/**
* @param int $rut
* @return Model\Venta\Propietario
* @throws Read
*/
public function getByRut(int $rut): Model\Venta\Propietario
{
try {
return $this->propietarioRepository->fetchById($rut);
} catch (EmptyResult $exception) {
throw new Read(__CLASS__, $exception);
}
}
/**
* @param Model\Venta\Propietario $propietario
* @param array $data
* @return Model\Venta\Propietario
* @throws Create
* @throws Update
*/
public function edit(Model\Venta\Propietario $propietario, array $data): Model\Venta\Propietario
{
if (isset($data['calle']) or isset($data['numero']) or isset($data['extra']) or isset($data['comuna'])) {
$direccion = $this->addDireccion($data);
$data['direccion'] = $direccion->id;
}
$filteredData = $this->propietarioRepository->filterData($data);
try {
return $this->propietarioRepository->edit($propietario, $filteredData);
} catch (PDOException | EmptyResult $exception) {
throw new Update(__CLASS__, $exception);
}
}
/**
* @param array $data
* @return Model\Venta\Propietario
* @throws Create
*/
public function addPropietario(array $data): Model\Venta\Propietario
{
$direccion = $this->addDireccion($data);
$data['direccion'] = $direccion->id;
$data['dv'] = 'i';
if (str_contains($data['rut'], '-')) {
list($rut, $dv) = explode('-', $data['rut']);
$data['rut'] = $rut;
$data['dv'] = $dv;
}
$fields = array_flip([
'rut',
'dv',
'nombres',
'apellido_paterno',
'apellido_materno',
'direccion',
'email',
'telefono'
]);
$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) {
try {
$propietario = $this->propietarioRepository->create($filtered_data);
$propietario = $this->propietarioRepository->save($propietario);
} catch (PDOException $exception) {
throw new Create(__CLASS__, $exception);
}
}
return $propietario;
}
/**
* @param array $data
* @return Model\Venta\Propietario
* @throws Create
* @throws Update
*/
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_flip([
'rut',
'razon_social',
'direccion',
'representante'
]);
$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->contacto->rut !== $mapped_data['representante']) {
$edits['representante'] = $mapped_data['representante'];
}
try {
$sociedad = $this->propietarioRepository->edit($sociedad, $edits);
} catch (PDOException $exception) {
throw new Update(__CLASS__, $exception);
}
} catch (EmptyResult) {
try {
$sociedad = $this->propietarioRepository->create($mapped_data);
$sociedad = $this->propietarioRepository->save($sociedad);
} catch (PDOException $exception) {
throw new Create(__CLASS__, $exception);
}
}
return $sociedad;
}
/**
* @param array $data
* @return Model\Direccion
* @throws Create
*/
protected function addDireccion(array $data): Model\Direccion
{
$fields = array_flip([
'calle',
'numero',
'extra',
'comuna'
]);
$filtered_data = array_intersect_key($data, $fields);
try {
$direccion = $this->direccionRepository->fetchByCalleAndNumeroAndExtraAndComuna($filtered_data['calle'], $filtered_data['numero'], $filtered_data['extra'], $filtered_data['comuna']);
} catch (EmptyResult) {
try {
$direccion = $this->direccionRepository->create($filtered_data);
$direccion = $this->direccionRepository->save($direccion);
} catch (PDOException $exception) {
throw new Create(__CLASS__, $exception);
}
}
return $direccion;
}
}