2023-07-24 20:55:26 -04:00
|
|
|
<?php
|
|
|
|
namespace Incoviba\Repository\Venta;
|
|
|
|
|
|
|
|
use DateTimeImmutable;
|
|
|
|
use Incoviba\Common\Define;
|
|
|
|
use Incoviba\Common\Ideal;
|
2023-08-08 23:53:49 -04:00
|
|
|
use Incoviba\Common\Implement;
|
2023-07-24 20:55:26 -04:00
|
|
|
use Incoviba\Model;
|
|
|
|
|
|
|
|
class Pie extends Ideal\Repository
|
|
|
|
{
|
|
|
|
public function __construct(Define\Connection $connection, protected Pago $pagoRepository)
|
|
|
|
{
|
|
|
|
parent::__construct($connection);
|
|
|
|
$this->setTable('pie');
|
|
|
|
}
|
|
|
|
|
2023-09-13 18:51:46 -03:00
|
|
|
public function create(?array $data = null): Model\Venta\Pie
|
2023-07-24 20:55:26 -04:00
|
|
|
{
|
2023-08-08 23:53:49 -04:00
|
|
|
$map = (new Implement\Repository\MapperParser(['valor', 'uf', 'cuotas']))
|
|
|
|
->register('fecha', new Implement\Repository\Mapper\DateTime('fecha'))
|
|
|
|
->register('asociado', (new Implement\Repository\Mapper())
|
|
|
|
->setFunction(function($data) {
|
2023-07-24 20:55:26 -04:00
|
|
|
if ($data['asociado'] === null or $data['asociado'] === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->fetchById($data['asociado']);
|
2023-08-08 23:53:49 -04:00
|
|
|
}))
|
|
|
|
->register('reajuste', (new Implement\Repository\Mapper())
|
|
|
|
->setFunction(function($data) {
|
2023-07-24 20:55:26 -04:00
|
|
|
if ($data['reajuste'] === null or $data['reajuste'] === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->pagoRepository->fetchById($data['reajuste']);
|
2023-08-08 23:53:49 -04:00
|
|
|
}));
|
2023-07-24 20:55:26 -04:00
|
|
|
return $this->parseData(new Model\Venta\Pie(), $data, $map);
|
|
|
|
}
|
2023-09-13 18:51:46 -03:00
|
|
|
public function save(Define\Model $model): Model\Venta\Pie
|
2023-07-24 20:55:26 -04:00
|
|
|
{
|
|
|
|
$model->id = $this->saveNew(
|
|
|
|
['fecha', 'valor', 'uf', 'cuotas', 'asociado', 'reajuste'],
|
2023-09-13 18:51:46 -03:00
|
|
|
[$model->fecha->format('Y-m-d H:i:s'), $model->valor, $model->uf, $model->cuotas, $model->asociado?->id, $model->reajuste?->id]
|
2023-07-24 20:55:26 -04:00
|
|
|
);
|
|
|
|
return $model;
|
|
|
|
}
|
2023-09-13 18:51:46 -03:00
|
|
|
public function edit(Define\Model $model, array $new_data): Model\Venta\Pie
|
2023-07-24 20:55:26 -04:00
|
|
|
{
|
|
|
|
return $this->update($model, ['fecha', 'valor', 'uf', 'cuotas', 'asociado', 'reajuste'], $new_data);
|
|
|
|
}
|
2023-12-20 08:44:49 -03:00
|
|
|
|
2025-03-03 14:57:22 -03:00
|
|
|
/**
|
|
|
|
* @param int $pie_id
|
|
|
|
* @return array
|
|
|
|
* @throws Implement\Exception\EmptyResult
|
|
|
|
*/
|
2023-12-20 08:44:49 -03:00
|
|
|
public function fetchAsociados(int $pie_id): array
|
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->select()
|
|
|
|
->from($this->getTable())
|
|
|
|
->where('asociado = ?');
|
|
|
|
return $this->fetchMany($query, [$pie_id]);
|
|
|
|
}
|
2025-03-03 14:57:22 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param float $value
|
|
|
|
* @return array
|
|
|
|
* @throws Implement\Exception\EmptyResult
|
|
|
|
*/
|
2024-01-08 17:33:42 -03:00
|
|
|
public function fetchByValue(float $value): array
|
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->select()
|
|
|
|
->from($this->getTable())
|
|
|
|
->where('valor = ?');
|
|
|
|
return $this->fetchMany($query, [$value]);
|
|
|
|
}
|
|
|
|
public function fetchByReajuste(int $reajuste_id): Model\Venta\Pie
|
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->select()
|
|
|
|
->from($this->getTable())
|
|
|
|
->where('reajuste = ?');
|
|
|
|
return $this->fetchOne($query, [$reajuste_id]);
|
|
|
|
}
|
2024-03-13 14:38:44 -03:00
|
|
|
public function fetchByVenta(int $venta_id): Model\Venta\Pie
|
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->select('a.*')
|
|
|
|
->from("{$this->getTable()} a")
|
|
|
|
->joined('JOIN venta ON venta.pie = a.id')
|
|
|
|
->where('venta.id = ?');
|
|
|
|
return $this->fetchOne($query, [$venta_id]);
|
|
|
|
}
|
2024-07-03 15:13:13 -04:00
|
|
|
public function filterData(array $data): array
|
|
|
|
{
|
|
|
|
return array_intersect_key($data, array_fill_keys(['fecha', 'valor', 'uf', 'cuotas', 'asociado', 'reajuste'], 0));
|
|
|
|
}
|
2023-07-24 20:55:26 -04:00
|
|
|
}
|