Files
safekat/ci4/app/Models/Presupuestos/PresupuestoPreimpresionesModel.php
2025-01-12 21:47:39 +01:00

169 lines
5.3 KiB
PHP
Executable File

<?php
namespace App\Models\Presupuestos;
class PresupuestoPreimpresionesModel extends \App\Models\BaseModel
{
protected $table = "presupuesto_preimpresiones";
/**
* Whether primary key uses auto increment.
*
* @var bool
*/
protected $useAutoIncrement = true;
const SORTABLE = [
0 => "t2.nombre",
1 => "t1.precio_unidad",
2 => "t1.precio_total"
];
protected $allowedFields = ["presupuesto_id", "tarifa_preimpresion_id", "nombre", "precio_total", "precio_unidad", "margen"];
protected $returnType = "App\Entities\Presupuestos\PresupuestoPreimpresionesEntity";
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 getPrecioTarifa($tarifa_preimpresion_id){
$modelTarifa = model('App\Models\Tarifas\TarifapreimpresionModel');
$tarifa_value = $modelTarifa->getTarifaPresupuestoPreimpresion($tarifa_preimpresion_id);
if (count($tarifa_value)>0) {
$ret_array = [];
foreach ($tarifa_value as $tarifa) {
$result_data = $this->calcularTarifa($tarifa);
array_push($ret_array, (object)[
'tarifa_id'=> $tarifa->tarifa_preimpresion_id,
'tarifa_nombre'=> $tarifa->tarifa_preimpresion_nombre,
'nombre'=> $tarifa->tarifa_preimpresion_nombre,
'precio'=> $result_data[0],
'margen'=> $result_data[1],
]);
}
usort($ret_array, function($a, $b) {
return $a->precio <=> $b->precio;
});
return $ret_array;
}
else{
$ret_array[] = (object)[
'tarifa_id'=> $tarifa_preimpresion_id,
'tarifa_nombre'=> $modelTarifa->getNombreTarifaPreimpresion($tarifa_preimpresion_id)[0]->nombre,
'nombre'=> $modelTarifa->getNombreTarifaPreimpresion($tarifa_preimpresion_id)[0]->nombre,
'precio' => 0,
'margen' => 0,
];
return $ret_array;
}
}
private function calcularTarifa($tarifa){
$precio = floatval($tarifa->precio);
$precio = $precio * (1+ floatval($tarifa->margen)/100.0);
$margen = $tarifa->margen;
return [$precio, $margen];
}
public function deleteAllServicios($presupuesto_id){
$this->db
->table($this->table . " t1")
->where('presupuesto_id', $presupuesto_id)
->delete();
}
public function deleteServiciosNotInArray($presupuesto_id, $tarifas_id){
$builder = $this->db
->table($this->table . " t1");
$builder->where('presupuesto_id', $presupuesto_id);
foreach($tarifas_id as $id){
$builder->where('tarifa_preimpresion_id !=', $id);
}
$builder->delete();
}
public function updateTarifas($presupuesto_id, $tarifas){
foreach($tarifas as $tarifa){
$builder = $this->db
->table($this->table . " t1");
$builder->select("id");
$builder->where('presupuesto_id', $presupuesto_id);
$builder->where('tarifa_preimpresion_id', $tarifa->tarifa_id);
$result = $builder->get()->getResultObject();
if(count($result)>0){
$this->db
->table($this->table . " t1")
->where('presupuesto_id', $presupuesto_id)
->where('tarifa_preimpresion_id', $tarifa->tarifa_id)
->set('precio', $tarifa->precio)
->set('margen', $tarifa->margen)
->update();
}
else{
$this->db
->table($this->table . " t1")
->set('presupuesto_id', $presupuesto_id)
->set('tarifa_preimpresion_id', $tarifa->tarifa_id)
->set('precio', $tarifa->precio)
->set('margen', $tarifa->margen)
->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_preimpresion_id AS tarifa_preimpresion_id, t1.tarifa_preimpresion_id AS tarifa_id, t1.precio AS precio, t1.margen AS margen, t2.nombre AS nombre"
);
$builder->where('t1.presupuesto_id', $presupuesto_id);
$builder->join("lg_tarifa_preimpresion t2", "t1.tarifa_preimpresion_id = t2.id", "left");
return $builder;
}
}