Base de Datos

This commit is contained in:
Juan Pablo Vial
2025-02-18 16:02:10 -03:00
parent 8b386b8b44
commit 9d135e2c26
25 changed files with 1091 additions and 1 deletions

View File

@ -0,0 +1,43 @@
<?php
namespace Incoviba\Repository\Proyecto;
use Incoviba\Common;
use Incoviba\Repository;
use Incoviba\Model;
class Broker extends Common\Ideal\Repository
{
public function getTable(): string
{
return 'brokers';
}
public function create(?array $data = null): Model\Proyecto\Broker
{
$map = new Common\Implement\Repository\MapperParser(['rut', 'digit', 'name']);
return $this->parseData(new Model\Proyecto\Broker(), $data, $map);
}
public function save(Common\Define\Model $model): Model\Proyecto\Broker
{
$this->saveNew(
['rut', 'digit', 'name'],
[$model->rut, $model->digit, $model->name]
);
return $model;
}
public function edit(Common\Define\Model $model, array $new_data): Model\Proyecto\Broker
{
return $this->update($model, ['rut', 'digit', 'name'], $new_data);
}
public function fetchByName(string $name): ?Model\Proyecto\Broker
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('name = :name');
return $this->fetchOne($query, ['name' => $name]);
}
}

View File

@ -0,0 +1,93 @@
<?php
namespace Incoviba\Repository\Proyecto\Broker;
use Incoviba\Common;
use Incoviba\Repository;
use Incoviba\Model;
class Contract extends Common\Ideal\Repository
{
public function __construct(Common\Define\Connection $connection, protected Repository\Proyecto\Broker $brokerRepository,
protected Repository\Proyecto $proyectoRepository)
{
parent::__construct($connection);
}
public function create(?array $data = null): Model\Proyecto\Broker\Contract
{
$map = (new Common\Implement\Repository\MapperParser(['commission']))
->register('broker_rut', (new Common\Implement\Repository\Mapper())
->setProperty('broker')
->setFunction(function($data) {
return $this->brokerRepository->fetchById($data['broker_rut']);
})
)
->register('proyecto_id', (new Common\Implement\Repository\Mapper())
->setProperty('proyecto')
->setFunction(function($data) {
return $this->proyectoRepository->fetchById($data['proyecto_id']);
})
);
return $this->parseData(new Model\Proyecto\Broker\Contract(), $data, $map);
}
public function save(Common\Define\Model $model): Model\Proyecto\Broker\Contract
{
$model->id = $this->saveNew(
['broker_rut', 'proyecto_id', 'commission'],
[$model->broker->rut, $model->proyecto->id, $model->commission]);
return $model;
}
public function edit(Common\Define\Model $model, array $new_data): Model\Proyecto\Broker\Contract
{
return $this->update($model, ['broker_rut', 'proyecto_id', 'commission'], $new_data);
}
public function fetchByBroker(int $brokerRut): array
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('broker_rut = :broker_rut');
return $this->fetchMany($query, ['broker_rut' => $brokerRut]);
}
public function fetchByProject(int $proyecto_id): array
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('proyecto_id = :proyecto_id');
return $this->fetchMany($query, ['proyecto_id' => $proyecto_id]);
}
public function fetchActiveByBroker(int $brokerRut): array
{
$query = $this->connection->getQueryBuilder()
->select('a.*')
->from("{$this->getTable()} a")
->joined($this->statusJoin())
->where('a.broker_rut = :broker_rut AND bcs.state = :state');
return $this->fetchMany($query, ['broker_rut' => $brokerRut, 'state' => Model\Proyecto\Broker\Contract\Type::ACTIVE]);
}
public function fetchActiveByProject(int $proyecto_id): array
{
$query = $this->connection->getQueryBuilder()
->select('a.*')
->from("{$this->getTable()} a")
->joined($this->statusJoin())
->where('a.proyecto_id = :proyecto_id AND bcs.state = :state');
return $this->fetchMany($query, ['proyecto_id' => $proyecto_id, 'state' => Model\Proyecto\Broker\Contract\Type::ACTIVE]);
}
public function fetchActiveByProjectAndBroker(int $proyecto_id, int $brokerRut): Model\Proyecto\Broker\Contract
{
$query = $this->connection->getQueryBuilder()
->select('a.*')
->from("{$this->getTable()} a")
->joined($this->statusJoin())
->where('a.proyecto_id = :proyecto_id AND a.broker_rut = :broker_rut AND bcs.state = :state');
return $this->fetchOne($query, ['proyecto_id' => $proyecto_id, 'broker_rut' => $brokerRut, 'state' => Model\Proyecto\Broker\Contract\Type::ACTIVE]);
}
protected function statusJoin(): string
{
return 'INNER JOIN (SELECT bcs1.* FROM broker_contract_states bcs1 INNER JOIN (SELECT MAX(id) AS id, contract_id FROM broker_contract_states GROUP BY contract_id) bcs0 ON bcs0.id = bcs1.id) bcs ON bcs.contract = a.id';
}
}

View File

@ -0,0 +1,52 @@
<?php
namespace Incoviba\Repository\Proyecto\Broker\Contract;
use Incoviba\Common;
use Incoviba\Repository;
use Incoviba\Model;
class State extends Common\Ideal\Repository
{
public function __construct(Common\Define\Connection $connection, protected Repository\Proyecto\Broker\Contract $contractRepository)
{
parent::__construct($connection);
}
public function create(?array $data = null): Model\Proyecto\Broker\Contract\State
{
$map = (new Common\Implement\Repository\MapperParser(['type']))
->register('contract_id', (new Common\Implement\Repository\Mapper())
->setProperty('contract')
->setFunction(function($data) {
return $this->contractRepository->fetchById($data['contract_id']);
}))
->register('date', new Common\Implement\Repository\Mapper\DateTime('date'));
return $this->parseData(new Model\Proyecto\Broker\Contract\State(), $data, $map);
}
public function save(Common\Define\Model $model): Model\Proyecto\Broker\Contract\State
{
$model->id = $this->saveNew(['contract_id', 'date', 'type'], [$model->contract->id, $model->date->format('Y-m-d'), $model->type]);
return $model;
}
public function edit(Common\Define\Model $model, array $new_data): Model\Proyecto\Broker\Contract\State
{
return $this->update($model, ['contract_id', 'date', 'type'], $new_data);
}
public function fetchByContract(int $contract_id): array
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('contract_id = :contract_id');
return $this->fetchMany($query, ['contract_id' => $contract_id]);
}
public function fetchActiveByContract(int $contract_id): Model\Proyecto\Broker\Contract\State
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('contract_id = :contract_id AND type = :type');
return $this->fetchOne($query, ['contract_id' => $contract_id, 'type' => Model\Proyecto\Broker\Contract\State\Type::ACTIVE]);
}
}

View File

@ -0,0 +1,59 @@
<?php
namespace Incoviba\Repository\Proyecto\Broker;
use Incoviba\Common;
use Incoviba\Repository;
use Incoviba\Model;
class Data extends Common\Ideal\Repository
{
public function __construct(Common\Define\Connection $connection,
protected Repository\Proyecto\Broker $brokerRepository,
protected Repository\Persona $personaRepository)
{
parent::__construct($connection);
}
public function create(?array $data = null): Model\Proyecto\Broker\Data
{
$map = (new Common\Implement\Repository\MapperParser())
->register('broker_rut', (new Common\Implement\Repository\Mapper())
->setProperty('broker')
->setFunction(function($data) {
return $this->brokerRepository->fetchById($data['broker_rut']);
}))
->register('representative_rut', (new Common\Implement\Repository\Mapper())
->setProperty('representative')
->setDefault(null)
->setFunction(function($data) {
try {
return $this->personaRepository->fetchById($data['representative_rut']);
} catch (Common\Implement\Exception\EmptyResult) {
return null;
}
}))
->register('legalName', (new Common\Implement\Repository\Mapper())
->setDefault(null));
return $this->parseData(new Model\Proyecto\Broker\Data(), $data, $map);
}
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]);
return $model;
}
public function edit(Common\Define\Model $model, array $new_data): Model\Proyecto\Broker\Data
{
return $this->update($model, ['broker_rut', 'representative_rut', 'legal_name'], $new_data);
}
public function fetchByBroker(int $brokerRut): Model\Proyecto\Broker\Data
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('broker_rut = :broker_rut');
return $this->fetchOne($query, ['broker_rut' => $brokerRut]);
}
}

View File

@ -0,0 +1,57 @@
<?php
namespace Incoviba\Repository\Venta;
use Incoviba\Common;
use Incoviba\Model;
class Promotion extends Common\Ideal\Repository
{
public function __construct(Common\Define\Connection $connection, protected Precio $precioRepository)
{
parent::__construct($connection);
}
public function create(?array $data = null): Model\Venta\Promotion
{
$map = (new Implement\Repository\MapperParser(['amount', 'type']))
->register('price_id', (new Implement\Repository\Mapper())
->setProperty('price')
->setFunction(function($data) {
return $this->precioRepository->create($data);
}))
->register('start_date', new Implement\Repository\Mapper\DateTime('start_date', 'startDate'))
->register('end_date', new Implement\Repository\Mapper\DateTime('end_date', 'endDate'))
->register('valid_until', new Implement\Repository\Mapper\DateTime('valid_until', 'validUntil'));
return $this->parseData(new Model\Venta\Promotion(), $data, $map);
}
public function save(Common\Define\Model $model): Model\Venta\Promotion
{
$model->id = $this->saveNew(
['amount', 'type', 'start_date', 'end_date', 'valid_until', 'price_id'],
[$model->amount, $model->type, $model->startDate->format('Y-m-d'), $model->endDate->format('Y-m-d'), $model->validUntil->format('Y-m-d'), $model->price->id]
);
return $model;
}
public function edit(Common\Define\Model $model, array $new_data): Model\Venta\Promotion
{
return $this->update($model, ['amount', 'type', 'start_date', 'end_date', 'valid_until', 'price_id'], $new_data);
}
public function fetchByPrice(int $price_id): array
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('price_id = :price_id');
return $this->fetchMany($query, ['price_id' => $price_id]);
}
public function fetchActiveByPrice(int $price_id): array
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('price_id = :price_id AND state = :state');
return $this->fetchMany($query, ['price_id' => $price_id, 'state' => Model\Venta\Promotion\State::ACTIVE]);
}
}

View File

@ -0,0 +1,202 @@
<?php
namespace Incoviba\Repository\Venta;
use Incoviba\Common\Define;
use PDO;
use Incoviba\Common;
use Incoviba\Model;
use Incoviba\Repository;
class Reservation extends Common\Ideal\Repository
{
public function __construct(Common\Define\Connection $connection, protected Repository\Persona $personaRepository,
protected Repository\Proyecto\Broker $brokerRepository,
protected Unidad $unitRepository, protected Promotion $promotionRepository)
{
parent::__construct($connection);
}
public function create(?array $data = null): Model\Venta\Reservation
{
$map = (new Common\Implement\Repository\MapperParser())
->register('buyer_rut', (new Common\Implement\Repository\Mapper())
->setProperty('buyer')
->setFunction(function($data) use ($data) {
return $this->personaRepository->fetchById($data['buyer_rut']);
}))
->register('date', new Common\Implement\Repository\Mapper\DateTime('date'))
->register('broker_rut', (new Common\Implement\Repository\Mapper())
->setProperty('broker')
->setDefault(null)
->setFunction(function($data) use ($data) {
try {
return $this->brokerRepository->fetchById($data['broker_rut']);
} catch (Common\Implement\Exception\EmptyResult) {
return null;
}
}));
return $this->parseData(new Model\Venta\Reservation(), $data, $map);
}
public function save(Common\Define\Model $model): Model\Venta\Reservation
{
$model->id = $this->saveNew([
'buyer_rut',
'date',
'broker_rut'
], [
$model->buyer->rut,
$model->date->format('Y-m-d'),
$model->broker?->rut
]);
$this->saveUnits($model);
$this->savePromotions($model);
return $model;
}
public function edit(Common\Define\Model $model, array $new_data): Model\Venta\Reservation
{
return $this->update($model, ['buyer_rut', 'date', 'broker_rut'], $new_data);
}
public function load(array $data_row): Model\Venta\Reservation
{
$model = parent::load($data_row);
$this->fetchUnits($model);
$this->fetchPromotions($model);
return $model;
}
protected function saveUnits(Model\Venta\Reservation $reservation): void
{
if (empty($reservation->units)) {
return;
}
$queryCheck = $this->connection->getQueryBuilder()
->select('COUNT(id) AS cnt')
->from('reservation_data')
->where('reservation_id = :id AND type = "Unit" AND reference_id = :unit_id');
$statementCheck = $this->connection->prepare($queryCheck);
$queryInsert = $this->connection->getQueryBuilder()
->insert()
->into('reservation_data')
->columns(['reservation_id', 'type', 'reference_id', 'value'])
->values([':reservation_id', ':type', ':reference_id', ':value']);
$statementInsert = $this->connection->prepare($queryInsert);
foreach ($reservation->units as $unit) {
$statementCheck->execute(['id' => $reservation->id, 'unit_id' => $unit['unit']->id]);
$result = $statementCheck->fetch(PDO::FETCH_ASSOC);
if ($result['cnt'] > 0) {
continue;
}
$statementInsert->execute(['reservation_id' => $reservation->id, 'type' => 'Unit', 'reference_id' => $unit['unit']->id, 'value' => $unit['value']]);
}
}
protected function savePromotions(Model\Venta\Reservation $reservation): void
{
if (empty($reservation->promotions)) {
return;
}
$queryCheck = $this->connection->getQueryBuilder()
->select('COUNT(id) AS cnt')
->from('reservation_data')
->where('reservation_id = :id AND type = "Promotion" AND reference_id = :promotion_id');
$statementCheck = $this->connection->prepare($queryCheck);
$queryInsert = $this->connection->getQueryBuilder()
->insert()
->into('reservation_data')
->columns(['reservation_id', 'type', 'reference_id'])
->values([':reservation_id', ':type', ':reference_id']);
$statementInsert = $this->connection->prepare($queryInsert);
foreach ($reservation->promotions as $promotion) {
$statementCheck->execute(['id' => $reservation->id, 'promotion_id' => $promotion->id]);
$result = $statementCheck->fetch(PDO::FETCH_ASSOC);
if ($result['cnt'] > 0) {
continue;
}
$statementInsert->execute(['reservation_id' => $reservation->id, 'type' => 'Promotion', 'reference_id' => $promotion->id]);
}
}
protected function editUnits(Model\Venta\Reservation $reservation, array $new_data): void
{
$querySelect = $this->connection->getQueryBuilder()
->select()
->from('reservation_data')
->where('reservation_id = :id AND type = "Unit" AND reference_id = :unit_id');
$statementSelect = $this->connection->prepare($querySelect);
$queryUpdate = $this->connection->getQueryBuilder()
->update('reservation_data')
->set('value = :value')
->where('reservation_id = :id AND type = "Unit" AND reference_id = :unit_id');
$statementUpdate = $this->connection->prepare($queryUpdate);
foreach ($new_data as $unit_id => $value) {
$idx = $reservation->findUnit($unit_id);
if ($idx === null) {
$reservation->addUnit($this->unitRepository->fetchById($unit_id), $value);
continue;
}
$statementSelect->execute(['id' => $reservation->id, 'unit_id' => $unit_id]);
$result = $statementSelect->fetch(PDO::FETCH_ASSOC);
if (!$result) {
continue;
}
$statementUpdate->execute(['id' => $reservation->id, 'unit_id' => $unit_id, 'value' => $value]);
$reservation->units[$idx]['value'] = $value;
}
}
protected function editPromotions(Model\Venta\Reservation $reservation, array $new_data): void
{
$querySelect = $this->connection->getQueryBuilder()
->select()
->from('reservation_data')
->where('reservation_id = :id AND type = "Promotion" AND reference_id = :promotion_id');
$statementSelect = $this->connection->prepare($querySelect);
$queryUpdate = $this->connection->getQueryBuilder()
->update('reservation_data')
->set('value = :value')
->where('reservation_id = :id AND type = "Promotion" AND reference_id = :promotion_id');
$statementUpdate = $this->connection->prepare($queryUpdate);
foreach ($new_data as $promotion_id => $value) {
$idx = array_search($promotion_id, array_column($reservation->promotions, 'id'));
if ($idx === false) {
$reservation->promotions []= $this->promotionRepository->fetchById($promotion_id);
continue;
}
$statementSelect->execute(['id' => $reservation->id, 'promotion_id' => $promotion_id]);
$result = $statementSelect->fetch(PDO::FETCH_ASSOC);
if (!$result) {
continue;
}
$statementUpdate->execute(['id' => $reservation->id, 'promotion_id' => $promotion_id, 'value' => $value]);
$reservation->promotions[$idx] = $this->promotionRepository->fetchById($promotion_id);
}
}
protected function fetchUnits(Model\Venta\Reservation &$reservation): Model\Venta\Reservation
{
$query = $this->connection->getQueryBuilder()
->select()
->from('reservation_data')
->where('reservation_id = :id AND type = "Unit"');
$statement = $this->connection->execute($query, ['id' => $reservation->id]);
while ($result = $statement->fetch(PDO::FETCH_ASSOC)) {
try {
$reservation->addUnit($this->unitRepository->fetchById($result['reference_id']), $result['value']);
} catch (Common\Implement\Exception\EmptyResult) {}
}
return $reservation;
}
protected function fetchPromotions(Model\Venta\Reservation $reservation): Model\Venta\Reservation
{
$query = $this->connection->getQueryBuilder()
->select()
->from('reservation_data')
->where('type = "Promotion" AND reservation_id = :id');
$statement = $this->connection->execute($query, ['id' => $reservation->id]);
while ($result = $statement->fetch(PDO::FETCH_ASSOC)) {
try {
$reservation->promotions []= $this->promotionRepository->fetchById($result['reference_id']);
} catch (Common\Implement\Exception\EmptyResult) {}
}
return $reservation;
}
}