Broker Contact
This commit is contained in:
@ -25,10 +25,10 @@ final class CreateBrokerData extends AbstractMigration
|
||||
|
||||
$this->table('broker_data')
|
||||
->addColumn('broker_rut', 'integer', ['signed' => false, 'null' => false])
|
||||
->addColumn('representative_rut', 'integer', ['signed' => false, 'null' => true, 'default' => null])
|
||||
->addColumn('representative_id', 'integer', ['signed' => false, 'null' => true, 'default' => null])
|
||||
->addColumn('legal_name', 'string', ['length' => 255, 'default' => null, 'null' => true])
|
||||
->addForeignKey('broker_rut', 'brokers', ['rut'], ['delete' => 'CASCADE', 'update' => 'CASCADE'])
|
||||
->addForeignKey('representative_rut', 'personas', ['rut'], ['delete' => 'CASCADE', 'update' => 'CASCADE'])
|
||||
->addForeignKey('representative_id', 'broker_contacts', ['id'], ['delete' => 'CASCADE', 'update' => 'CASCADE'])
|
||||
->create();
|
||||
|
||||
$this->execute('SET unique_checks=1; SET foreign_key_checks=1;');
|
||||
|
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Phinx\Migration\AbstractMigration;
|
||||
|
||||
final class CreateBrokerContacts extends AbstractMigration
|
||||
{
|
||||
/**
|
||||
* Change Method.
|
||||
*
|
||||
* Write your reversible migrations using this method.
|
||||
*
|
||||
* More information on writing migrations is available here:
|
||||
* https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
|
||||
*
|
||||
* Remember to call "create()" or "update()" and NOT "save()" when working
|
||||
* with the Table class.
|
||||
*/
|
||||
public function change(): void
|
||||
{
|
||||
$this->execute('SET unique_checks=0; SET foreign_key_checks=0;');
|
||||
$this->execute("ALTER DATABASE CHARACTER SET 'utf8mb4';");
|
||||
$this->execute("ALTER DATABASE COLLATE='utf8mb4_general_ci';");
|
||||
|
||||
$this->table('broker_contacts')
|
||||
->addColumn('rut', 'integer', ['signed' => false, 'null' => true])
|
||||
->addColumn('digit', 'string', ['length' => 1, 'null' => true])
|
||||
->addColumn('name', 'string', ['length' => 255, 'null' => true])
|
||||
->addColumn('email', 'string', ['length' => 255, 'null' => true])
|
||||
->addColumn('phone', 'string', ['length' => 255, 'null' => true])
|
||||
->addColumn('address_id', 'integer', ['signed' => false, 'null' => true])
|
||||
->addForeignKey('address_id', 'direccion', ['id'], ['delete' => 'CASCADE', 'update' => 'CASCADE'])
|
||||
->create();
|
||||
|
||||
$this->execute('SET unique_checks=1; SET foreign_key_checks=1;');
|
||||
}
|
||||
}
|
@ -2,5 +2,5 @@
|
||||
use Incoviba\Controller\Proyectos\Brokers;
|
||||
|
||||
$app->group('/brokers', function($app) {
|
||||
$app->get('[/]', [Brokers::class, 'projects']);
|
||||
$app->get('[/]', Brokers::class);
|
||||
});
|
||||
|
@ -1,119 +1,181 @@
|
||||
@extends('proyectos.brokers.base')
|
||||
|
||||
@section('brokers_content')
|
||||
@include('proyectos.brokers.proyectos')
|
||||
<div id="brokers"></div>
|
||||
<table id="brokers" class="ui table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>RUT</th>
|
||||
<th>Nombre</th>
|
||||
<th>Contacto</th>
|
||||
<th>Contratos</th>
|
||||
<th class="right aligned">
|
||||
<button class="ui small tertiary green icon button" id="add_button">
|
||||
<i class="plus icon"></i>
|
||||
</button>
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
@foreach($brokers as $broker)
|
||||
<tr>
|
||||
<td>{{$broker->rutFull()}}</td>
|
||||
<td>
|
||||
<span
|
||||
@if ($broker->data()?->legalName !== '')
|
||||
data-tooltip="{{ $broker->data()?->legalName }}" data-position="right center"
|
||||
@endif
|
||||
>
|
||||
{{$broker->name}}
|
||||
</span>
|
||||
</td>
|
||||
<td>
|
||||
<span
|
||||
@if ($broker->data()?->representative?->email !== '' || $broker->data()?->representative?->phone !== '')
|
||||
data-tooltip="{{ ($broker->data()?->representative?->email !== '') ? "Email: " . $broker->data()?->representative?->email : '' }}{{ ($broker->data()?->representative?->phone !== '') ? ' Teléfono: ' . $broker->data()?->representative?->phone : '' }}" data-position="right center"
|
||||
@endif
|
||||
>
|
||||
{{ $broker->data()?->representative?->name }}
|
||||
</span>
|
||||
</td>
|
||||
<td>
|
||||
<div class="ui list">
|
||||
@foreach($broker->contracts() as $contract)
|
||||
<div class="item">
|
||||
<a href="{{$urls->base}}/proyecto/{{$contract->project->id}}">{{$contract->project->descripcion}} ({{$format->percent($contract->commission, 2, true)}})</a>
|
||||
</div>
|
||||
@endforeach
|
||||
</div>
|
||||
</td>
|
||||
<td class="right aligned">
|
||||
<button class="ui small tertiary icon button edit_button" data-index="{{$broker->rut}}">
|
||||
<i class="edit icon"></i>
|
||||
</button>
|
||||
<button class="ui small tertiary red icon button remove_button" data-index="{{$broker->rut}}">
|
||||
<i class="remove icon"></i>
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
</table>
|
||||
@include('proyectos.brokers.add_modal')
|
||||
@endsection
|
||||
|
||||
@push('page_scripts')
|
||||
<script>
|
||||
const brokers = {
|
||||
data: {
|
||||
contracts: {},
|
||||
project_id: null
|
||||
},
|
||||
formatters: {
|
||||
date: new Intl.DateTimeFormat('es-CL', {dateStyle: 'medium'}),
|
||||
percent: new Intl.NumberFormat('es-CL', {style: 'percent', minimumFractionDigits: 2, maximumFractionDigits: 2})
|
||||
},
|
||||
draw() {
|
||||
return {
|
||||
brokers: project_id => {
|
||||
$('#projects').hide()
|
||||
const div = document.getElementById('brokers')
|
||||
const table = document.createElement('table')
|
||||
table.classList.add('ui', 'table')
|
||||
table.innerHTML = [
|
||||
"<thead>",
|
||||
"<tr>",
|
||||
"<th>Operador</th>",
|
||||
"<th>Comisión</th>",
|
||||
"<th>Fecha Inicio</th>",
|
||||
"<th class='right aligned'><button class='ui small tertiary green icon button' id='add_button'><i class='plus icon'></i></button></th>",
|
||||
"</tr>",
|
||||
"</thead>"].join('')
|
||||
const tbody = document.createElement('tbody')
|
||||
brokers.data.contracts[project_id].forEach(contract => {
|
||||
const dateParts = contract.current.date.split('-')
|
||||
const date = new Date([dateParts[0], parseInt(dateParts[1]) - 1, dateParts[2]].join('-'))
|
||||
date.setMonth(date.getMonth() + 1)
|
||||
tbody.insertAdjacentHTML('beforeend', [
|
||||
"<tr>",
|
||||
`<td>${contract.broker.name}</td>`,
|
||||
`<td>${brokers.formatters.percent.format(contract.commission)}</td>`,
|
||||
`<td>${brokers.formatters.date.format(date)}</td>`,
|
||||
`<td class="right aligned"><button class="ui small tertiary red icon button remove_button" data-id="${contract.id}"><i class="remove icon"></i></button></td>`,
|
||||
"</tr>"].join(''))
|
||||
})
|
||||
table.append(tbody)
|
||||
div.innerHTML = ''
|
||||
div.append(table)
|
||||
$('#brokers').show()
|
||||
|
||||
}
|
||||
const brokersHandler = {
|
||||
ids: {
|
||||
buttons: {
|
||||
add: 'add_button',
|
||||
edit: 'edit_button',
|
||||
remove: 'remove_button'
|
||||
},
|
||||
modals: {
|
||||
add: '',
|
||||
edit: ''
|
||||
},
|
||||
forms: {
|
||||
add: '',
|
||||
edit: ''
|
||||
}
|
||||
},
|
||||
get() {
|
||||
events() {
|
||||
return {
|
||||
contracts: project_id => {
|
||||
brokers.actions().startSpinner()
|
||||
brokers.data.project_id = project_id
|
||||
const url = `{{$urls->api}}/proyecto/${project_id}/brokers`
|
||||
APIClient.fetch(url).then(response => {
|
||||
if (!response.ok) {
|
||||
return
|
||||
}
|
||||
return response.json().then(json => {
|
||||
brokers.data.contracts[json.proyecto_id] = json.contracts
|
||||
const promises = []
|
||||
json.contracts.forEach(contract => {
|
||||
promises.push(brokers.get().broker(contract.broker_rut).then(broker => {
|
||||
brokers.data.contracts[json.proyecto_id].find(contract => contract.broker_rut === broker.rut).broker = broker
|
||||
}))
|
||||
})
|
||||
Promise.all(promises).then(() => {
|
||||
brokers.draw().brokers(json.proyecto_id)
|
||||
})
|
||||
})
|
||||
}).finally(() => {
|
||||
brokers.actions().stopSpinner()
|
||||
})
|
||||
add: clickEvent => {
|
||||
console.debug(clickEvent)
|
||||
clickEvent.preventDefault()
|
||||
brokersHandler.actions().add()
|
||||
},
|
||||
broker: broker_rut => {
|
||||
const url = `{{$urls->api}}/proyectos/broker/${broker_rut}`
|
||||
return APIClient.fetch(url).then(response => {
|
||||
if (!response.ok) {
|
||||
return
|
||||
}
|
||||
return response.json().then(json => {
|
||||
return json.broker
|
||||
})
|
||||
})
|
||||
edit: clickEvent => {
|
||||
clickEvent.preventDefault()
|
||||
const broker_rut = clickEvent.currentTarget.dataset.index
|
||||
brokersHandler.actions().edit(broker_rut)
|
||||
},
|
||||
delete: clickEvent => {
|
||||
clickEvent.preventDefault()
|
||||
const broker_rut = clickEvent.currentTarget.dataset.index
|
||||
brokersHandler.actions().delete(broker_rut)
|
||||
}
|
||||
}
|
||||
},
|
||||
buttonWatch() {
|
||||
document.getElementById(brokersHandler.ids.buttons.add).addEventListener('click', brokersHandler.events().add)
|
||||
Array.from(document.getElementsByClassName(brokersHandler.ids.buttons.edit)).forEach(button => {
|
||||
button.addEventListener('click', brokersHandler.events().edit)
|
||||
})
|
||||
Array.from(document.getElementsByClassName(brokersHandler.ids.buttons.remove)).forEach(button => {
|
||||
button.addEventListener('click', brokersHandler.events().delete)
|
||||
})
|
||||
},
|
||||
actions() {
|
||||
return {
|
||||
startSpinner: () => {
|
||||
$('#refresh_button').addClass('loading')
|
||||
add: () => {
|
||||
$(`#${brokersHandler.ids.modals.add}`).modal('show')
|
||||
},
|
||||
stopSpinner: () => {
|
||||
$('#refresh_button').removeClass('loading')
|
||||
},
|
||||
refresh: () => {
|
||||
brokers.get().contracts(brokers.data.project_id)
|
||||
},
|
||||
up: () => {
|
||||
$('#brokers').hide()
|
||||
$('#projects').show()
|
||||
brokers.data.project_id = null
|
||||
document.getElementById('brokers').innerHTML = ''
|
||||
edit: broker_rut => {},
|
||||
delete: broker_rut => {
|
||||
brokersHandler.execute().delete(broker_rut)
|
||||
}
|
||||
}
|
||||
},
|
||||
execute() {
|
||||
return {
|
||||
add: data => {
|
||||
const url = '{{$urls->api}}/proyectos/brokers/add'
|
||||
const method = 'post'
|
||||
const body = new FormData()
|
||||
body.append('brokers[]', JSON.stringify(data))
|
||||
return APIClient.fetch(url, {method, body}).then(response => response.json()).then(json => {
|
||||
if (!json.success) {
|
||||
console.error(json.errors)
|
||||
alert('No se pudo agregar operador.')
|
||||
return
|
||||
}
|
||||
window.location.reload()
|
||||
})
|
||||
},
|
||||
edit: (broker_rut, data) => {},
|
||||
delete: broker_rut => {
|
||||
const url = '{{$urls->api}}/proyectos/broker/' + broker_rut
|
||||
const method = 'delete'
|
||||
return APIClient.fetch(url, {method}).then(response => response.json()).then(json => {
|
||||
if (!json.success) {
|
||||
console.error(json.errors)
|
||||
alert('No se pudo eliminar operador.')
|
||||
return
|
||||
}
|
||||
window.location.reload()
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
setup(ids) {
|
||||
brokersHandler.ids = ids
|
||||
brokersHandler.buttonWatch()
|
||||
$(`#${brokersHandler.ids.modals.add}`).modal('hide')
|
||||
|
||||
new AddModal(brokersHandler)
|
||||
}
|
||||
}
|
||||
$(document).ready(() => {
|
||||
$('#brokers').hide()
|
||||
brokersHandler.setup({
|
||||
buttons: {
|
||||
add: 'add_button',
|
||||
edit: 'edit_button',
|
||||
remove: 'remove_button'
|
||||
},
|
||||
modals: {
|
||||
add: 'add_broker_modal',
|
||||
edit: 'edit_broker_modal'
|
||||
},
|
||||
forms: {
|
||||
add: {
|
||||
base: 'add_broker_form',
|
||||
digit: 'digit'
|
||||
},
|
||||
edit: {
|
||||
base: 'edit_broker_form'
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
</script>
|
||||
@endpush
|
||||
|
110
app/resources/views/proyectos/brokers/add_modal.blade.php
Normal file
110
app/resources/views/proyectos/brokers/add_modal.blade.php
Normal file
@ -0,0 +1,110 @@
|
||||
<div class="ui modal" id="add_broker_modal">
|
||||
<div class="header">
|
||||
Agregar Operador
|
||||
</div>
|
||||
<div class="content">
|
||||
<form class="ui form" id="add_broker_form">
|
||||
<div class="fields">
|
||||
<div class="field">
|
||||
<label>RUT</label>
|
||||
<div class="ui right labeled input">
|
||||
<input type="text" name="rut" placeholder="RUT" required />
|
||||
<div class="ui basic label">-<span id="digit"></span></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="fields">
|
||||
<div class="field">
|
||||
<label>Nombre</label>
|
||||
<input type="text" name="name" placeholder="Nombre" required />
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Razón Social</label>
|
||||
<input type="text" name="legal_name" placeholder="Razón Social" required />
|
||||
</div>
|
||||
</div>
|
||||
<div class="fields">
|
||||
<div class="field">
|
||||
<label>Contacto</label>
|
||||
<input type="text" name="contact" placeholder="Contacto" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="fields">
|
||||
<div class="field">
|
||||
<label>Correo</label>
|
||||
<input type="email" name="email" placeholder="Correo" />
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Teléfono</label>
|
||||
<input type="text" name="phone" placeholder="Teléfono" />
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<div class="ui deny button">
|
||||
Cancelar
|
||||
</div>
|
||||
<div class="ui positive right labeled icon button">
|
||||
Agregar
|
||||
<i class="checkmark icon"></i>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@include('layout.body.scripts.rut')
|
||||
@push('page_scripts')
|
||||
<script>
|
||||
class AddModal
|
||||
{
|
||||
ids
|
||||
handler
|
||||
constructor(handler)
|
||||
{
|
||||
this.handler = handler
|
||||
this.ids = {
|
||||
modal: handler.ids.modals.add,
|
||||
form: handler.ids.forms.add.base,
|
||||
digit: handler.ids.forms.add.digit
|
||||
}
|
||||
$(`#${this.ids.modal}`).modal({
|
||||
onApprove: () => {
|
||||
const form = document.getElementById(this.ids.form)
|
||||
const data = {
|
||||
rut: form.querySelector('[name="rut"]').value,
|
||||
digit: Rut.digitoVerificador(form.querySelector('[name="rut"]').value),
|
||||
name: form.querySelector('[name="name"]').value,
|
||||
legal_name: form.querySelector('[name="legal_name"]').value,
|
||||
contact: form.querySelector('[name="contact"]').value || '',
|
||||
email: form.querySelector('[name="email"]').value || '',
|
||||
phone: form.querySelector('[name="phone"]').value || ''
|
||||
}
|
||||
this.handler.execute().add(data)
|
||||
}
|
||||
})
|
||||
const value = document.querySelector(`#${this.ids.form} input[name="rut"]`).value
|
||||
this.update().digit(value)
|
||||
this.watch().rut()
|
||||
}
|
||||
update() {
|
||||
return {
|
||||
digit: value => {
|
||||
if (value.length > 3) {
|
||||
document.getElementById(this.ids.digit).textContent = Rut.digitoVerificador(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
watch() {
|
||||
return {
|
||||
rut: () => {
|
||||
document.querySelector(`#${this.ids.form} input[name="rut"]`).addEventListener('input', event => {
|
||||
const value = event.currentTarget.value
|
||||
this.update().digit(value)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
@endpush
|
@ -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