56 lines
1.9 KiB
PHP
56 lines
1.9 KiB
PHP
![]() |
<?php
|
||
|
namespace Incoviba\Repository\Venta;
|
||
|
|
||
|
use Incoviba\Common\Ideal;
|
||
|
use Incoviba\Common\Define;
|
||
|
use Incoviba\Model;
|
||
|
use Incoviba\Repository;
|
||
|
|
||
|
class Precio extends Ideal\Repository
|
||
|
{
|
||
|
public function __construct(Define\Connection $connection, protected Repository\Venta\Unidad $unidadRepository)
|
||
|
{
|
||
|
parent::__construct($connection);
|
||
|
$this->setTable('precio');
|
||
|
}
|
||
|
|
||
|
public function create(?array $data = null): Define\Model
|
||
|
{
|
||
|
$map = [
|
||
|
'unidad' => [
|
||
|
'function' => function($data) {
|
||
|
return $this->unidadRepository->fetchById($data['unidad']);
|
||
|
}
|
||
|
],
|
||
|
'valor' => []
|
||
|
];
|
||
|
return $this->parseData(new Model\Venta\Precio(), $data, $map);
|
||
|
}
|
||
|
public function save(Define\Model $model): Define\Model
|
||
|
{
|
||
|
$model->id = $this->saveNew(
|
||
|
['unidad', 'valor'],
|
||
|
[$model->unidad->id, $model->valor]
|
||
|
);
|
||
|
return $model;
|
||
|
}
|
||
|
public function edit(Define\Model $model, array $new_data): Define\Model
|
||
|
{
|
||
|
return $this->update($model, ['unidad', 'valor'], $new_data);
|
||
|
}
|
||
|
|
||
|
public function fetchByProyecto(int $proyecto_id): array
|
||
|
{
|
||
|
$query = "SELECT a.*
|
||
|
FROM `{$this->getTable()}` a
|
||
|
JOIN (SELECT e1.* FROM `estado_precio` e1 JOIN (SELECT MAX(`id`) AS 'id', `precio` FROM `estado_precio` GROUP BY `precio`) e0 ON e0.`id` = e1.`id`) ep ON ep.`precio` = a.`id`
|
||
|
JOIN `tipo_estado_precio` tep ON tep.`id` = ep.`estado`
|
||
|
JOIN `unidad` ON `unidad`.`id` = a.`unidad`
|
||
|
JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt`
|
||
|
JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo`
|
||
|
WHERE ptu.`proyecto` = ? AND tep.`descripcion` = 'vigente'
|
||
|
ORDER BY tu.`orden`, ptu.`nombre`, `unidad`.`subtipo`, LPAD(`unidad`.`descripcion`, 4, '0')";
|
||
|
return $this->fetchMany($query, [$proyecto_id]);
|
||
|
}
|
||
|
}
|