mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
159 lines
4.7 KiB
PHP
Executable File
159 lines
4.7 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models\Presupuestos;
|
|
|
|
class PresupuestoServiciosExtraModel extends \App\Models\BaseModel
|
|
{
|
|
protected $table = "presupuesto_serviciosExtra";
|
|
|
|
/**
|
|
* 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_extra_id", "precio_total", "precio_unidad", "margen"];
|
|
protected $returnType = "App\Entities\Presupuestos\PresupuestoServiciosExtraEntity";
|
|
|
|
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_extra_id){
|
|
|
|
$modelTarifa = model('App\Models\Tarifas\TarifaextraModel');
|
|
$tarifa_value = $modelTarifa->getTarifaPresupuestoExtra($tarifa_extra_id);
|
|
if (count($tarifa_value)>0) {
|
|
|
|
$result_data = $this->calcularTarifa($tarifa_value[0]);
|
|
$ret_array[] = (object)[
|
|
'tarifa_id'=> $tarifa_value[0]->tarifa_extra_id,
|
|
'tarifa_nombre'=> $tarifa_value[0]->tarifa_extra_nombre,
|
|
'precio'=> $result_data[0],
|
|
'margen'=> $result_data[1],
|
|
];
|
|
return $ret_array;
|
|
}
|
|
else{
|
|
$ret_array[] = (object)[
|
|
'tarifa_id'=> $tarifa_extra_id,
|
|
'tarifa_nombre'=> $modelTarifa->getNombreTarifaPreimpresion($tarifa_extra_id)[0]->nombre,
|
|
'precio' => 0,
|
|
'margen' => 0,
|
|
];
|
|
return $ret_array;
|
|
}
|
|
return [];
|
|
}
|
|
|
|
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_extra_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_extra_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_extra_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_extra_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_extra_id AS tarifa_extra_id, t1.precio AS precio, t1.margen AS margen, t2.nombre AS nombre"
|
|
);
|
|
|
|
$builder->where('t1.presupuesto_id', $presupuesto_id);
|
|
$builder->join("tarifa_extra t2", "t1.tarifa_extra_id = t2.id", "left");
|
|
|
|
return $builder;
|
|
}
|
|
}
|