Files
safekat/ci4/app/Models/Presupuestos/PresupuestoManipuladosModel.php

216 lines
7.4 KiB
PHP
Executable File

<?php
namespace App\Models\Presupuestos;
class PresupuestoManipuladosModel extends \App\Models\BaseModel
{
protected $table = "presupuesto_manipulados";
/**
* 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_manipulado_id", "nombre", "precio_total", "precio_unidad", "margen"];
protected $returnType = "App\Entities\Presupuestos\PresupuestoManipuladosEntity";
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_manipulado_id, $tirada, $POD){
$modelTarifa = model('App\Models\Tarifas\TarifaManipuladoModel');
$tarifa_value = $modelTarifa->getTarifaPresupuestoManipulado($tarifa_manipulado_id, $tirada);
if (count($tarifa_value)>0) {
$result_data = $this->calcularTarifa($tarifa_value[0], $tirada, $POD<$tirada?false:true);
$ret_array[] = (object)[
'tarifa_id'=> $tarifa_value[0]->tarifa_manipulado_id,
'tarifa_nombre'=> $tarifa_value[0]->tarifa_manipulado_nombre,
'precio_unidad'=> $result_data[0],
'total'=> $result_data[1],
'margen' => $result_data[2],
];
return $ret_array;
}
else{
$ret_array[] = (object)[
'tarifa_id'=> $tarifa_manipulado_id,
'tarifa_nombre'=> $modelTarifa->getNombreTarifaManipulado($tarifa_manipulado_id)[0]->nombre,
'precio_unidad' => 0,
'total'=> 0,
'margen' => 0,
];
return $ret_array;
}
return [];
}
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);
$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);
}
if (!$is_POD){
$total += floatval($tarifa->tarifa_importe_fijo);
}
return [$precio_unidad, $total, $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_manipulado_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_manipulado_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_manipulado_id', $tarifa->tarifa_id)
->set('precio_unidad', $tarifa->precio_unidad)
->set('precio_total', $tarifa->precio_total)
->set('margen', $tarifa->margen)
->update();
}
else{
$this->db
->table($this->table . " t1")
->set('presupuesto_id', $presupuesto_id)
->set('tarifa_manipulado_id', $tarifa->tarifa_id)
->set('precio_unidad', $tarifa->precio_unidad)
->set('precio_total', $tarifa->precio_total)
->set('margen', $tarifa->margen)
->insert();
}
}
}
public function initPresupuesto($tipo_presupuesto, $solapas, $tirada, $POD){
$model = model('App\Models\Presupuestos\TipoPresupuestoServiciosDefectoModel');
$tarifas_procesar = $model->get_tarifas($tipo_presupuesto, $solapas, "manipulado");
$modelTarifa = model('App\Models\Tarifas\TarifaManipuladoModel');
$tarifas = [];
foreach($tarifas_procesar as $tarifa){
$tarifa_value = $modelTarifa->getTarifaPresupuestoManipulado($tarifa['tarifa_id'], $tirada);
if (count($tarifa_value)>0) {
$result_data = $this->calcularTarifa($tarifa_value[0], $tirada, $POD<$tirada?false:true);
array_push($tarifas, (object)[
'tarifa_id'=> $tarifa_value[0]->tarifa_manipulado_id,
'tarifa_nombre'=> $tarifa_value[0]->tarifa_manipulado_nombre,
'precio_unidad'=> $result_data[0],
'total'=> $result_data[1],
'margen'=> $result_data[2],
]);
}
else{
array_push($tarifas,
(object)[
'tarifa_id'=> $tarifa['tarifa_id'] ,
'tarifa_nombre'=> $tarifa['tarifa_nombre'],
'precio_unidad' => 0,
'total'=> 0,
'margen' => 0,
]);
}
}
return $tarifas;
}
/**
* 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_manipulado_id AS tarifa_manipulado_id, t1.precio_unidad AS precio_unidad,
t1.precio_total AS precio_total, t1.margen AS margen, t2.nombre AS nombre"
);
$builder->where('t1.presupuesto_id', $presupuesto_id);
$builder->join("lg_tarifa_manipulado t2", "t1.tarifa_manipulado_id = t2.id", "left");
return $builder;
}
}