Agregar Proveedor
This commit is contained in:
@ -1,16 +1,20 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Inmobiliaria;
|
||||
|
||||
use PDOException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Implement;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Service;
|
||||
|
||||
class Proveedor extends Ideal\Service
|
||||
{
|
||||
public function __construct(LoggerInterface $logger, protected Repository\Inmobiliaria\Proveedor $proveedorRepository)
|
||||
public function __construct(LoggerInterface $logger,
|
||||
protected Repository\Inmobiliaria\Proveedor $proveedorRepository,
|
||||
protected Repository\Inmobiliaria\Proveedor\Datos $datosRepository,
|
||||
protected Service\Persona $contactoService)
|
||||
{
|
||||
parent::__construct($logger);
|
||||
}
|
||||
@ -18,18 +22,61 @@ class Proveedor extends Ideal\Service
|
||||
public function getAll(?string $orderBy = null): array
|
||||
{
|
||||
try {
|
||||
return $this->proveedorRepository->fetchAll($orderBy);
|
||||
return array_map([$this, 'process'], $this->proveedorRepository->fetchAll($orderBy));
|
||||
} catch (Implement\Exception\EmptyResult) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
public function getByRut(int $proveedor_rut): ?Model\Inmobiliaria\Proveedor
|
||||
public function getById(int $proveedor_rut): ?Model\Inmobiliaria\Proveedor
|
||||
{
|
||||
try {
|
||||
return $this->proveedorRepository->fetchById($proveedor_rut);
|
||||
return $this->process($this->proveedorRepository->fetchById($proveedor_rut));
|
||||
} catch (Implement\Exception\EmptyResult) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public function add(array $data): Model\Inmobiliaria\Proveedor
|
||||
{
|
||||
$filteredData = $this->proveedorRepository->filterData($data);
|
||||
try {
|
||||
$proveedor = $this->process($this->proveedorRepository->fetchByNombre($filteredData['nombre']));
|
||||
if ($proveedor->contacto->rut !== $data['contacto']['rut']) {
|
||||
$contacto = $this->contactoService->add($data['contacto']);
|
||||
return $this->proveedorRepository->edit($proveedor, ['contacto_rut' => $contacto->rut]);
|
||||
}
|
||||
return $proveedor;
|
||||
} catch (Implement\Exception\EmptyResult) {
|
||||
$contacto = $this->contactoService->add($data['contacto']);
|
||||
$filteredData['contacto_rut'] = $contacto->rut;
|
||||
$proveedor = $this->proveedorRepository->create($filteredData);
|
||||
return $this->process($this->proveedorRepository->save($proveedor));
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @TODO Implement
|
||||
* @param Model\Inmobiliaria\Proveedor $proveedor
|
||||
* @param array $data
|
||||
* @return Model\Inmobiliaria\Proveedor
|
||||
*/
|
||||
public function edit(Model\Inmobiliaria\Proveedor $proveedor, array $data): Model\Inmobiliaria\Proveedor
|
||||
{
|
||||
return $proveedor;
|
||||
}
|
||||
public function delete(Model\Inmobiliaria\Proveedor $proveedor): bool
|
||||
{
|
||||
try {
|
||||
$this->proveedorRepository->remove($proveedor);
|
||||
return true;
|
||||
} catch (Implement\Exception\EmptyResult | PDOException) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected function process(Model\Inmobiliaria\Proveedor $proveedor): Model\Inmobiliaria\Proveedor
|
||||
{
|
||||
$proveedor->addFactory('datos', (new Implement\Repository\Factory())->setCallable([$this->datosRepository, 'fetchByProveedor'])->setArgs(['proveedor_rut' => $proveedor->rut]));
|
||||
return $proveedor;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user