ya funciona añadir los servicios

This commit is contained in:
Jaime Jiménez
2024-02-21 21:58:58 +01:00
parent ae133e8bea
commit 417bc74e1f
10 changed files with 150 additions and 49 deletions

View File

@ -19,7 +19,7 @@ class PresupuestoAcabadosModel extends \App\Models\GoBaseModel
2 => "t1.precio_total"
];
protected $allowedFields = ["presupuesto_id", "tarifa_acabado_id", "nombre", "precio_total", "precio_unidad", "margen"];
protected $allowedFields = ["presupuesto_id", "tarifa_acabado_id", "nombre", "precio_total", "precio_unidad", "margen", "cubierta", "sobrecubierta"];
protected $returnType = "App\Entities\Presupuestos\PresupuestoAcabadosEntity";
protected $useTimestamps = true;
@ -98,13 +98,33 @@ class PresupuestoAcabadosModel extends \App\Models\GoBaseModel
->delete();
}
public function deleteServiciosNotInArray($presupuesto_id, $tarifas_id){
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)
$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($tarifas_id as $id){
$builder->where('tarifa_acabado_id !=', $id);
foreach($ids_for_delete as $id){
$builder->orWhere('id', $id);
}
$builder->delete();
}
@ -118,6 +138,8 @@ class PresupuestoAcabadosModel extends \App\Models\GoBaseModel
$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);
$result = $builder->get()->getResultObject();
if(count($result)>0){
$this->db
@ -127,6 +149,8 @@ class PresupuestoAcabadosModel extends \App\Models\GoBaseModel
->set('precio_unidad', $tarifa->precio_unidad)
->set('precio_total', $tarifa->precio_total)
->set('margen', $tarifa->margen)
->set('cubierta', $tarifa->cubierta)
->set('sobrecubierta', $tarifa->sobrecubierta)
->update();
@ -139,6 +163,8 @@ class PresupuestoAcabadosModel extends \App\Models\GoBaseModel
->set('precio_unidad', $tarifa->precio_unidad)
->set('precio_total', $tarifa->precio_total)
->set('margen', $tarifa->margen)
->set('cubierta', $tarifa->cubierta)
->set('sobrecubierta', $tarifa->sobrecubierta)
->insert();
}
}
@ -157,7 +183,7 @@ class PresupuestoAcabadosModel extends \App\Models\GoBaseModel
->table($this->table . " t1")
->select(
"t1.id AS id, t1.tarifa_acabado_id AS tarifa_acabado_id, t1.precio_unidad AS precio_unidad,
t1.precio_total AS precio_total, t1.margen AS margen, t2.nombre AS nombre"
t1.precio_total AS precio_total, t1.margen AS margen, t2.nombre AS nombre, t1.cubierta AS cubierta, t1.sobrecubierta AS sobrecubierta"
);
$builder->where('t1.presupuesto_id', $presupuesto_id);