mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
326 lines
11 KiB
PHP
Executable File
326 lines
11 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models\Presupuestos;
|
|
|
|
class PresupuestoAcabadosModel extends \App\Models\BaseModel
|
|
{
|
|
protected $table = "presupuesto_acabados";
|
|
|
|
const TARIFA_RESERVA_UVI_ID = 8;
|
|
|
|
/**
|
|
* Whether primary key uses auto increment.
|
|
*
|
|
* @var bool
|
|
*/
|
|
protected $useAutoIncrement = true;
|
|
|
|
const SORTABLE = [
|
|
0 => "t2.nombre",
|
|
1 => "t1.proveedor_id",
|
|
2 => "t1.precio_unidad",
|
|
3 => "t1.precio_total"
|
|
];
|
|
|
|
protected $allowedFields = ["presupuesto_id", "tarifa_acabado_id", "proveedor_id", "nombre", "precio_total", "importe_fijo", "importe_minimo", "precio_unidad", "margen", "cubierta", "sobrecubierta", "faja"];
|
|
protected $returnType = "App\Entities\Presupuestos\PresupuestoAcabadosEntity";
|
|
|
|
protected $useTimestamps = true;
|
|
protected $useSoftDeletes = false;
|
|
|
|
protected $createdField = "created_at";
|
|
protected $updatedField = "updated_at";
|
|
|
|
public static $labelField = "nombre";
|
|
|
|
protected $validationRules = [
|
|
"precio_total" => [
|
|
"label" => "Presupuestos.precioTotal",
|
|
"rules" => "decimal|required",
|
|
],
|
|
];
|
|
|
|
protected $validationMessages = [
|
|
"precio_total" => [
|
|
"decimal" => "Presupuestos.validation.decimal",
|
|
"requerido" => "Presupuestos.validation.decimal",
|
|
],
|
|
];
|
|
|
|
|
|
public function getProveedoresForSelector($tarifa_acabado_id, $tirada)
|
|
{
|
|
|
|
$proveedores = [];
|
|
$modelTarifa = model('App\Models\Tarifas\Acabados\TarifaAcabadoModel');
|
|
|
|
$tarifa_value = $modelTarifa->getTarifaPresupuestoAcabado($tarifa_acabado_id, $tirada);
|
|
|
|
if (count($tarifa_value) > 0) {
|
|
foreach ($tarifa_value as $tarifa)
|
|
array_push(
|
|
$proveedores,
|
|
(object) [
|
|
'id' => $tarifa->proveedor_id,
|
|
'text' => $tarifa->proveedor_nombre,
|
|
]
|
|
);
|
|
}
|
|
return $proveedores;
|
|
}
|
|
|
|
public function getPrecioTarifa($tarifa_acabado_id, $tirada, $proveedor_id, $POD)
|
|
{
|
|
|
|
$modelTarifa = model('App\Models\Tarifas\Acabados\TarifaAcabadoModel');
|
|
$tarifa_value = $modelTarifa->getTarifaPresupuestoAcabado($tarifa_acabado_id, $tirada, $proveedor_id);
|
|
if (count($tarifa_value) > 0) {
|
|
|
|
$ret_array = [];
|
|
foreach ($tarifa_value as $tarifa) {
|
|
$result_data = $this->calcularTarifa($tarifa, $tirada, $POD < $tirada ? false : true);
|
|
array_push($ret_array, (object) [
|
|
'tarifa_id' => $tarifa->tarifa_acabado_id,
|
|
'tarifa_nombre' => $tarifa->tarifa_acabado_nombre,
|
|
'nombre' => $tarifa->tarifa_acabado_nombre,
|
|
'precio_unidad' => round($result_data[0], 2),
|
|
'total' => round($result_data[1], 2),
|
|
'precio_total' => round($result_data[1], 2),
|
|
'margen' => $result_data[2],
|
|
'proveedor' => $tarifa->proveedor_nombre,
|
|
'proveedor_id' => $tarifa->proveedor_id,
|
|
]);
|
|
}
|
|
usort($ret_array, function ($a, $b) {
|
|
return $a->precio_total <=> $b->precio_total;
|
|
});
|
|
return $ret_array;
|
|
} else {
|
|
$ret_array[] = (object) [
|
|
'tarifa_id' => $tarifa_acabado_id,
|
|
'tarifa_nombre' => $modelTarifa->getNombreTarifaAcabado($tarifa_acabado_id)[0]->nombre,
|
|
'nombre' => $modelTarifa->getNombreTarifaAcabado($tarifa_acabado_id)[0]->nombre,
|
|
'precio_unidad' => 0,
|
|
'total' => 0,
|
|
'precio_total' => 0,
|
|
'margen' => 0,
|
|
'proveedor' => '',
|
|
'proveedor_id' => 0,
|
|
];
|
|
return $ret_array;
|
|
}
|
|
return [];
|
|
}
|
|
|
|
|
|
public function deleteAllServicios($presupuesto_id)
|
|
{
|
|
|
|
$this->db
|
|
->table($this->table . " t1")
|
|
->where('presupuesto_id', $presupuesto_id)
|
|
->delete();
|
|
}
|
|
|
|
public function deleteServiciosNotInArray($presupuesto_id, $tarifas)
|
|
{
|
|
|
|
$builder = $this->db
|
|
->table($this->table . " t1");
|
|
$builder->select("*");
|
|
$builder->where('presupuesto_id', $presupuesto_id);
|
|
$results = $builder->get()->getResultObject();
|
|
|
|
$ids_for_delete = [];
|
|
foreach ($results as $result) {
|
|
$found = false;
|
|
foreach ($tarifas as $tarifa) {
|
|
if (
|
|
$tarifa->tarifa_id == $result->tarifa_acabado_id &&
|
|
$tarifa->cubierta == $result->cubierta &&
|
|
$tarifa->sobrecubierta == $result->sobrecubierta &&
|
|
$tarifa->faja == $result->faja
|
|
)
|
|
$found = true;
|
|
}
|
|
if (!$found) {
|
|
array_push($ids_for_delete, $result->id);
|
|
}
|
|
}
|
|
|
|
$builder = $this->db
|
|
->table($this->table . " t1");
|
|
$builder->where('presupuesto_id', $presupuesto_id);
|
|
foreach ($ids_for_delete as $id) {
|
|
$builder->orWhere('id', $id);
|
|
}
|
|
$builder->delete();
|
|
}
|
|
|
|
public function updateTarifas($presupuesto_id, $tarifas)
|
|
{
|
|
|
|
foreach ($tarifas as $tarifa) {
|
|
|
|
$proveedor = $tarifa->proveedor_id == 'undefined' ? 'NULL' : $tarifa->proveedor_id;
|
|
|
|
$builder = $this->db
|
|
->table($this->table . " t1");
|
|
$builder->select("id");
|
|
$builder->where('presupuesto_id', $presupuesto_id);
|
|
$builder->where('tarifa_acabado_id', $tarifa->tarifa_id);
|
|
$builder->where('cubierta', $tarifa->cubierta);
|
|
$builder->where('sobrecubierta', $tarifa->sobrecubierta);
|
|
$builder->where('faja', $tarifa->faja);
|
|
$result = $builder->get()->getResultObject();
|
|
if (count($result) > 0) {
|
|
$this->db
|
|
->table($this->table . " t1")
|
|
->where('presupuesto_id', $presupuesto_id)
|
|
->where('tarifa_acabado_id', $tarifa->tarifa_id)
|
|
->set('proveedor_id', $proveedor)
|
|
->set('precio_unidad', $tarifa->precio_unidad)
|
|
->set('precio_total', $tarifa->precio_total)
|
|
->set('margen', $tarifa->margen)
|
|
->set('cubierta', $tarifa->cubierta)
|
|
->set('sobrecubierta', $tarifa->sobrecubierta)
|
|
->set('faja', $tarifa->faja)
|
|
->update();
|
|
|
|
|
|
} else {
|
|
$this->db
|
|
->table($this->table . " t1")
|
|
->set('presupuesto_id', $presupuesto_id)
|
|
->set('tarifa_acabado_id', $tarifa->tarifa_id)
|
|
->set('proveedor_id', $proveedor, false)
|
|
->set('precio_unidad', $tarifa->precio_unidad)
|
|
->set('precio_total', $tarifa->precio_total)
|
|
->set('margen', $tarifa->margen)
|
|
->set('cubierta', $tarifa->cubierta)
|
|
->set('sobrecubierta', $tarifa->sobrecubierta)
|
|
->set('faja', $tarifa->faja)
|
|
->insert();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get resource data.
|
|
*
|
|
* @param string $search
|
|
*
|
|
* @return \CodeIgniter\Database\BaseBuilder
|
|
*/
|
|
public function getResource($presupuesto_id = -1)
|
|
{
|
|
$builder = $this->db
|
|
->table($this->table . " t1")
|
|
->select(
|
|
"t1.id AS id, t1.tarifa_acabado_id AS tarifa_acabado_id, t1.tarifa_acabado_id AS tarifa_id, t1.precio_unidad AS precio_unidad,
|
|
t1.precio_total AS precio_total, t1.margen AS margen, t2.nombre AS nombre, t1.cubierta AS cubierta, t1.sobrecubierta AS sobrecubierta,
|
|
t1.faja AS faja,
|
|
t1.proveedor_id AS proveedor_id, t3.nombre AS proveedor,"
|
|
);
|
|
|
|
$builder->where('t1.presupuesto_id', $presupuesto_id);
|
|
$builder->join("lg_tarifa_acabado t2", "t1.tarifa_acabado_id = t2.id", "left");
|
|
$builder->join("lg_proveedores t3", "t1.proveedor_id = t3.id", "left");
|
|
|
|
return $builder;
|
|
}
|
|
|
|
|
|
/**
|
|
* Get resource data for creating PDFs.
|
|
*
|
|
* @param string $search
|
|
*
|
|
* @return \CodeIgniter\Database\BaseBuilder
|
|
*/
|
|
public function getAcabadoCubiertaForPdf($presupuesto_id = -1)
|
|
{
|
|
$builder = $this->db
|
|
->table($this->table . " t1")
|
|
->select(
|
|
"t1.id as ID, t2.nombre AS tipo_acabado"
|
|
);
|
|
$builder->join("lg_tarifa_acabado t2", "t1.tarifa_acabado_id = t2.id", "left");
|
|
|
|
$builder->where("t1.cubierta", 1);
|
|
$builder->where("t1.presupuesto_id", $presupuesto_id);
|
|
|
|
return $builder;
|
|
}
|
|
|
|
/**
|
|
* Get resource data for creating PDFs.
|
|
*
|
|
* @param string $search
|
|
*
|
|
* @return \CodeIgniter\Database\BaseBuilder
|
|
*/
|
|
public function getAcabadoSobrecubiertaForPdf($presupuesto_id = -1)
|
|
{
|
|
$builder = $this->db
|
|
->table($this->table . " t1")
|
|
->select(
|
|
"t1.id as ID, t2.nombre AS tipo_acabado"
|
|
);
|
|
$builder->join("lg_tarifa_acabado t2", "t1.tarifa_acabado_id = t2.id", "left");
|
|
|
|
$builder->where("t1.sobrecubierta", 1);
|
|
$builder->where("t1.presupuesto_id", $presupuesto_id);
|
|
|
|
return $builder;
|
|
}
|
|
|
|
/**
|
|
* Get resource data for creating PDFs.
|
|
*
|
|
* @param string $search
|
|
*
|
|
* @return \CodeIgniter\Database\BaseBuilder
|
|
*/
|
|
public function getAcabadoReservaUviForPdf($presupuesto_id = -1)
|
|
{
|
|
$builder = $this->db
|
|
->table($this->table . " t1")
|
|
->select(
|
|
"t1.id as ID, t2.nombre AS tipo_acabado"
|
|
);
|
|
$builder->join("lg_tarifa_acabado t2", "t1.tarifa_acabado_id = t2.id", "left");
|
|
|
|
$builder->where("t1.tarifa_acabado_id", self::TARIFA_RESERVA_UVI_ID);
|
|
$builder->where("t1.presupuesto_id", $presupuesto_id);
|
|
|
|
return $builder;
|
|
}
|
|
|
|
private function calcularTarifa($tarifa, $tirada, $is_POD = false)
|
|
{
|
|
|
|
$precio_unidad = floatval($tarifa->precio_min) - (floatval($tarifa->precio_min) - floatval($tarifa->precio_max)) / ($tarifa->tirada_max - $tarifa->tirada_min) * ($tirada - $tarifa->tirada_min);
|
|
if ($tirada > $tarifa->tirada_max)
|
|
$precio_unidad = $tarifa->precio_max;
|
|
$precio_unidad = $precio_unidad * (1 + floatval($tarifa->margen) / 100.0);
|
|
|
|
if (!$is_POD) {
|
|
$precio_unidad += floatval($tarifa->tarifa_importe_fijo)/floatval($tirada);
|
|
}
|
|
|
|
$total = $precio_unidad * $tirada;
|
|
$margen = floatval($tarifa->margen);
|
|
|
|
if ($tarifa->tarifa_precio_min > $total) {
|
|
$total = $total - ($total * $margen / 100.0);
|
|
$margen = round(100.0 * (floatval($tarifa->tarifa_precio_min) - $total) / floatval($tarifa->tarifa_precio_min), 0);
|
|
$total = floatval($tarifa->tarifa_precio_min);
|
|
$precio_unidad = round(floatval($total / $tirada), 2);
|
|
}
|
|
|
|
return [$precio_unidad, $total, $margen];
|
|
}
|
|
|
|
}
|