mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
corregidas direfencias en totales presupuestos. tiradas alternativas ok. corregido calculo envio base
This commit is contained in:
@ -22,7 +22,7 @@ class PresupuestoAcabadosModel extends \App\Models\BaseModel
|
||||
3 => "t1.precio_total"
|
||||
];
|
||||
|
||||
protected $allowedFields = ["presupuesto_id", "tarifa_acabado_id", "proveedor_id", "nombre", "precio_total", "precio_unidad", "margen", "cubierta", "sobrecubierta", "faja"];
|
||||
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;
|
||||
@ -48,55 +48,58 @@ class PresupuestoAcabadosModel extends \App\Models\BaseModel
|
||||
];
|
||||
|
||||
|
||||
public function getProveedoresForSelector($tarifa_acabado_id, $tirada){
|
||||
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,
|
||||
]);
|
||||
|
||||
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){
|
||||
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) {
|
||||
|
||||
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],
|
||||
$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) {
|
||||
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,
|
||||
} 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,
|
||||
@ -104,13 +107,14 @@ class PresupuestoAcabadosModel extends \App\Models\BaseModel
|
||||
'proveedor' => '',
|
||||
'proveedor_id' => 0,
|
||||
];
|
||||
return $ret_array;
|
||||
return $ret_array;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
public function deleteAllServicios($presupuesto_id){
|
||||
|
||||
public function deleteAllServicios($presupuesto_id)
|
||||
{
|
||||
|
||||
$this->db
|
||||
->table($this->table . " t1")
|
||||
@ -118,25 +122,28 @@ class PresupuestoAcabadosModel extends \App\Models\BaseModel
|
||||
->delete();
|
||||
}
|
||||
|
||||
public function deleteServiciosNotInArray($presupuesto_id, $tarifas){
|
||||
public function deleteServiciosNotInArray($presupuesto_id, $tarifas)
|
||||
{
|
||||
|
||||
$builder = $this->db
|
||||
->table($this->table . " t1");
|
||||
->table($this->table . " t1");
|
||||
$builder->select("*");
|
||||
$builder->where('presupuesto_id', $presupuesto_id);
|
||||
$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)
|
||||
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){
|
||||
if (!$found) {
|
||||
array_push($ids_for_delete, $result->id);
|
||||
}
|
||||
}
|
||||
@ -144,28 +151,29 @@ class PresupuestoAcabadosModel extends \App\Models\BaseModel
|
||||
$builder = $this->db
|
||||
->table($this->table . " t1");
|
||||
$builder->where('presupuesto_id', $presupuesto_id);
|
||||
foreach($ids_for_delete as $id){
|
||||
foreach ($ids_for_delete as $id) {
|
||||
$builder->orWhere('id', $id);
|
||||
}
|
||||
$builder->delete();
|
||||
}
|
||||
|
||||
public function updateTarifas($presupuesto_id, $tarifas){
|
||||
public function updateTarifas($presupuesto_id, $tarifas)
|
||||
{
|
||||
|
||||
foreach($tarifas as $tarifa){
|
||||
|
||||
$proveedor = $tarifa->proveedor_id=='undefined'?'NULL':$tarifa->proveedor_id;
|
||||
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);
|
||||
$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){
|
||||
if (count($result) > 0) {
|
||||
$this->db
|
||||
->table($this->table . " t1")
|
||||
->where('presupuesto_id', $presupuesto_id)
|
||||
@ -178,10 +186,9 @@ class PresupuestoAcabadosModel extends \App\Models\BaseModel
|
||||
->set('sobrecubierta', $tarifa->sobrecubierta)
|
||||
->set('faja', $tarifa->faja)
|
||||
->update();
|
||||
|
||||
|
||||
}
|
||||
else{
|
||||
|
||||
} else {
|
||||
$this->db
|
||||
->table($this->table . " t1")
|
||||
->set('presupuesto_id', $presupuesto_id)
|
||||
@ -290,27 +297,28 @@ class PresupuestoAcabadosModel extends \App\Models\BaseModel
|
||||
return $builder;
|
||||
}
|
||||
|
||||
private function calcularTarifa($tarifa, $tirada, $is_POD=false){
|
||||
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);
|
||||
$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);
|
||||
|
||||
$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);
|
||||
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];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user