mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
trabajando en presupuestos servicios extra
This commit is contained in:
158
ci4/app/Models/Presupuestos/PresupuestoServiciosExtraModel.php
Executable file
158
ci4/app/Models/Presupuestos/PresupuestoServiciosExtraModel.php
Executable file
@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Presupuestos;
|
||||
|
||||
class PresupuestoServiciosExtraModel extends \App\Models\GoBaseModel
|
||||
{
|
||||
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", "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_extra_id){
|
||||
|
||||
$modelTarifa = model('App\Models\Tarifas\TarifaextraModel');
|
||||
$tarifa_value = $modelTarifa->getTarifaPresupuestoPreimpresion($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;
|
||||
}
|
||||
}
|
||||
@ -80,7 +80,7 @@ class TarifaextraModel extends \App\Models\GoBaseModel
|
||||
],
|
||||
];
|
||||
|
||||
public function getServiciosPreimpresionSelector()
|
||||
public function getServiciosExtraSelector()
|
||||
{
|
||||
/*
|
||||
Todos los servicios de extra activas que se pueden usar en presupuestos
|
||||
@ -96,7 +96,7 @@ class TarifaextraModel extends \App\Models\GoBaseModel
|
||||
return $builder->orderBy("t1.nombre", "asc")->get()->getResultObject();
|
||||
}
|
||||
|
||||
public function getTarifaPresupuestoPreimpresion($tarifa_id){
|
||||
public function getTarifaPresupuestoExtra($tarifa_id){
|
||||
|
||||
$builder = $this->db
|
||||
->table($this->table . " t1")
|
||||
@ -111,7 +111,7 @@ class TarifaextraModel extends \App\Models\GoBaseModel
|
||||
return $builder->get()->getResultObject();
|
||||
}
|
||||
|
||||
public function getNombreTarifaPreimpresion($id=-1)
|
||||
public function getNombreTarifaExtra($id=-1)
|
||||
{
|
||||
/*
|
||||
Todos los servicios de encuadernacion activas que se pueden usar en presupuestos
|
||||
|
||||
Reference in New Issue
Block a user