Broker Contact
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
namespace Incoviba\Controller\Proyectos;
|
||||
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Exception\ServiceAction\Read;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Incoviba\Common\Alias\View;
|
||||
@ -11,14 +12,12 @@ use Psr\Log\LoggerInterface;
|
||||
|
||||
class Brokers
|
||||
{
|
||||
public function projects(ServerRequestInterface $request, ResponseInterface $response, LoggerInterface $logger, Repository\Proyecto $proyectoRepository, View $view): ResponseInterface
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, LoggerInterface $logger, Service\Proyecto\Broker $brokerService, View $view): ResponseInterface
|
||||
{
|
||||
$projects = [];
|
||||
$brokers = [];
|
||||
try {
|
||||
$projects = $proyectoRepository->fetchAll('descripcion');
|
||||
} catch (EmptyResult $exception) {
|
||||
$logger->debug($exception);
|
||||
}
|
||||
return $view->render($response, 'proyectos.brokers', compact('projects'));
|
||||
$brokers = $brokerService->getAll();
|
||||
} catch (Read) {}
|
||||
return $view->render($response, 'proyectos.brokers', compact('brokers'));
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ class Broker extends Common\Ideal\Model
|
||||
public string $digit;
|
||||
public string $name;
|
||||
|
||||
protected ?Broker\Data $data = null;
|
||||
protected ?Broker\Data $data;
|
||||
public function data(): ?Broker\Data
|
||||
{
|
||||
if (!isset($this->data)) {
|
||||
@ -18,13 +18,28 @@ class Broker extends Common\Ideal\Model
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
protected array $contracts;
|
||||
public function contracts(): ?array
|
||||
{
|
||||
if (!isset($this->contracts)) {
|
||||
$this->contracts = $this->runFactory('contracts');
|
||||
}
|
||||
return $this->contracts;
|
||||
}
|
||||
|
||||
public function rutFull(): string
|
||||
{
|
||||
return implode('-', [number_format($this->rut, 0, ',', '.'), $this->digit]);
|
||||
}
|
||||
|
||||
public function jsonSerialize(): mixed
|
||||
{
|
||||
return [
|
||||
'rut' => $this->rut,
|
||||
'digit' => $this->digit,
|
||||
'name' => $this->name,
|
||||
'data' => $this->data()
|
||||
'data' => $this->data(),
|
||||
'rut_full' => $this->rutFull()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
24
app/src/Model/Proyecto/Broker/Contact.php
Normal file
24
app/src/Model/Proyecto/Broker/Contact.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
namespace Incoviba\Model\Proyecto\Broker;
|
||||
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Model\Direccion;
|
||||
use Incoviba\Model\Proyecto\Broker;
|
||||
|
||||
class Contact extends Ideal\Model
|
||||
{
|
||||
public string $name;
|
||||
public ?string $email = null;
|
||||
public ?string $phone = null;
|
||||
public ?Direccion $address = null;
|
||||
|
||||
protected function jsonComplement(): array
|
||||
{
|
||||
return [
|
||||
'name' => $this->name,
|
||||
'email' => $this->email,
|
||||
'phone' => $this->phone,
|
||||
'address' => $this->address
|
||||
];
|
||||
}
|
||||
}
|
@ -7,15 +7,15 @@ use Incoviba\Model;
|
||||
class Data extends Common\Ideal\Model
|
||||
{
|
||||
public Model\Proyecto\Broker $broker;
|
||||
public ?Model\Persona $representative = null;
|
||||
public ?Model\Proyecto\Broker\Contact $representative = null;
|
||||
public ?string $legalName = null;
|
||||
|
||||
protected function jsonComplement(): array
|
||||
{
|
||||
return [
|
||||
'broker_rut' => $this->broker->rut,
|
||||
'representative_rut' => $this->representative?->rut,
|
||||
'representative' => $this->representative,
|
||||
'legal_name' => $this->legalName
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
60
app/src/Repository/Proyecto/Broker/Contact.php
Normal file
60
app/src/Repository/Proyecto/Broker/Contact.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
namespace Incoviba\Repository\Proyecto\Broker;
|
||||
|
||||
use Incoviba\Common;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Model;
|
||||
|
||||
class Contact extends Common\Ideal\Repository
|
||||
{
|
||||
public function __construct(Define\Connection $connection, protected Repository\Direccion $direccionRepository)
|
||||
{
|
||||
parent::__construct($connection);
|
||||
$this->setTable('broker_contacts');
|
||||
}
|
||||
|
||||
public function create(?array $data = null): Model\Proyecto\Broker\Contact
|
||||
{
|
||||
$map = (new Common\Implement\Repository\MapperParser(['name', 'email', 'phone']))
|
||||
->register('address_id', (new Common\Implement\Repository\Mapper())
|
||||
->setProperty('address')
|
||||
->setDefault(null)
|
||||
->setFunction(function($data) {
|
||||
if ($data['address_id'] === null) return null;
|
||||
try {
|
||||
return $this->direccionRepository->fetchById($data['address_id']);
|
||||
} catch (Common\Implement\Exception\EmptyResult) {
|
||||
return null;
|
||||
}
|
||||
}));
|
||||
return $this->parseData(new Model\Proyecto\Broker\Contact(), $data, $map);
|
||||
}
|
||||
public function save(Define\Model $model): Model\Proyecto\Broker\Contact
|
||||
{
|
||||
$model->id = $this->saveNew([
|
||||
'name', 'email', 'phone', 'address_id'
|
||||
], [
|
||||
$model->name, $model->email, $model->phone, $model->address?->id
|
||||
]);
|
||||
return $model;
|
||||
}
|
||||
public function edit(Define\Model $model, array $new_data): Model\Proyecto\Broker\Contact
|
||||
{
|
||||
return $this->update($model, ['name', 'email', 'phone', 'address_id'], $new_data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return Model\Proyecto\Broker\Contact
|
||||
* @throws Common\Implement\Exception\EmptyResult
|
||||
*/
|
||||
public function fetchByName(string $name): Model\Proyecto\Broker\Contact
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where('name = :name');
|
||||
return $this->fetchOne($query, ['name' => $name]);
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@ class Data extends Common\Ideal\Repository
|
||||
{
|
||||
public function __construct(Common\Define\Connection $connection,
|
||||
protected Repository\Proyecto\Broker $brokerRepository,
|
||||
protected Repository\Persona $personaRepository)
|
||||
protected Repository\Proyecto\Broker\Contact $contactRepository)
|
||||
{
|
||||
parent::__construct($connection);
|
||||
}
|
||||
@ -27,13 +27,13 @@ class Data extends Common\Ideal\Repository
|
||||
->setFunction(function($data) {
|
||||
return $this->brokerRepository->fetchById($data['broker_rut']);
|
||||
}))
|
||||
->register('representative_rut', (new Common\Implement\Repository\Mapper())
|
||||
->register('representative_id', (new Common\Implement\Repository\Mapper())
|
||||
->setProperty('representative')
|
||||
->setDefault(null)
|
||||
->setFunction(function($data) {
|
||||
if ($data['representative_rut'] == null) return null;
|
||||
if ($data['representative_id'] === null) return null;
|
||||
try {
|
||||
return $this->personaRepository->fetchById($data['representative_rut']);
|
||||
return $this->contactRepository->fetchById($data['representative_id']);
|
||||
} catch (Common\Implement\Exception\EmptyResult) {
|
||||
return null;
|
||||
}
|
||||
@ -46,8 +46,8 @@ class Data extends Common\Ideal\Repository
|
||||
public function save(Common\Define\Model $model): Model\Proyecto\Broker\Data
|
||||
{
|
||||
$model->id = $this->saveNew(
|
||||
['broker_rut', 'representative_rut', 'legal_name'],
|
||||
[$model->broker->rut, $model->representative?->rut, $model->legalName]);
|
||||
['broker_rut', 'representative_id', 'legal_name'],
|
||||
[$model->broker->rut, $model->representative?->id, $model->legalName]);
|
||||
return $model;
|
||||
}
|
||||
|
||||
@ -59,7 +59,7 @@ class Data extends Common\Ideal\Repository
|
||||
*/
|
||||
public function edit(Common\Define\Model $model, array $new_data): Model\Proyecto\Broker\Data
|
||||
{
|
||||
return $this->update($model, ['representative_rut', 'legal_name'], $new_data);
|
||||
return $this->update($model, ['representative_id', 'legal_name'], $new_data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -15,7 +15,9 @@ class Broker extends Ideal\Service
|
||||
{
|
||||
public function __construct(LoggerInterface $logger,
|
||||
protected Repository\Proyecto\Broker $brokerRepository,
|
||||
protected Repository\Proyecto\Broker\Data $dataRepository, protected Service\Persona $personaService)
|
||||
protected Repository\Proyecto\Broker\Data $dataRepository,
|
||||
protected Repository\Proyecto\Broker\Contact $contactRepository,
|
||||
protected Repository\Proyecto\Broker\Contract $contractRepository)
|
||||
{
|
||||
parent::__construct($logger);
|
||||
}
|
||||
@ -110,9 +112,11 @@ class Broker extends Ideal\Service
|
||||
protected function process(Model\Proyecto\Broker $broker): Model\Proyecto\Broker
|
||||
{
|
||||
$broker->addFactory('data', (new Factory())
|
||||
->setArgs(['broker_rut' => $broker->rut])
|
||||
->setCallable([$this->dataRepository, 'fetchByBroker'])
|
||||
);
|
||||
->setArgs(['broker_rut' => $broker->rut])
|
||||
->setCallable([$this->dataRepository, 'fetchByBroker']))
|
||||
->addFactory('contracts', (new Factory())
|
||||
->setArgs(['brokerRut' => $broker->rut])
|
||||
->setCallable([$this->contractRepository, 'fetchByBroker']));
|
||||
|
||||
return $broker;
|
||||
}
|
||||
@ -127,11 +131,27 @@ class Broker extends Ideal\Service
|
||||
{
|
||||
$data['broker_rut'] = $broker->rut;
|
||||
$filteredData = $this->dataRepository->filterData($data);
|
||||
if (isset($filteredData['representative_rut'])) {
|
||||
if (isset($data['contact'])) {
|
||||
try {
|
||||
$this->personaService->getById($filteredData['representative_rut']);
|
||||
} catch (ServiceAction\Read) {
|
||||
unset($filteredData['representative_rut']);
|
||||
$representative = $this->contactRepository->fetchByName($data['contact']);
|
||||
$filteredData['representative_id'] = $representative->id;
|
||||
} catch (EmptyResult) {
|
||||
$representativeData = $this->contactRepository->filterData($data);
|
||||
$representativeData['name'] = $data['contact'];
|
||||
$representative = $this->contactRepository->create($representativeData);
|
||||
try {
|
||||
$representative = $this->contactRepository->save($representative);
|
||||
$filteredData['representative_id'] = $representative->id;
|
||||
} catch (PDOException) {
|
||||
unset($representative);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isset($filteredData['representative_id'])) {
|
||||
try {
|
||||
$this->contactRepository->fetchById($filteredData['representative_id']);
|
||||
} catch (EmptyResult) {
|
||||
unset($filteredData['representative_id']);
|
||||
}
|
||||
}
|
||||
try {
|
||||
|
Reference in New Issue
Block a user