falta editar

This commit is contained in:
2025-01-03 16:03:54 +01:00
parent 883308d052
commit adacb6ddc8
7 changed files with 451 additions and 270 deletions

View File

@ -25,21 +25,22 @@ class ClientePreciosModel extends \App\Models\BaseModel
];
protected $allowedFields = [
"cliente_id",
"cliente_id",
"plantilla_id",
"tipo",
"tipo_maquina",
"tipo_impresion",
"tiempo_min",
"tiempo_max",
"precio_hora",
"margen",
"is_deleted",
"deleted_at",
"created_at",
"tipo",
"tipo_maquina",
"tipo_impresion",
"tiempo_min",
"tiempo_max",
"precio_hora",
"margen",
"is_deleted",
"deleted_at",
"created_at",
"updated_at",
"user_created_id",
"user_updated_id"];
"user_updated_id"
];
protected $returnType = "App\Entities\Clientes\ClientePreciosEntity";
@ -82,14 +83,14 @@ class ClientePreciosModel extends \App\Models\BaseModel
"rules" => "required|decimal",
],
];
protected $validationMessages = [
"cliente_id" => [
"required" => "ClientePrecios.validation.required",
],
"tipo" => [
"required" => "ClientePrecios.validation.required",
@ -111,7 +112,8 @@ class ClientePreciosModel extends \App\Models\BaseModel
],
];
function clean_plantilla_id($cliente_id = 0){
function clean_plantilla_id($cliente_id = 0)
{
$this->db
->table($this->table . " t1")
->where('cliente_id', $cliente_id)
@ -119,7 +121,8 @@ class ClientePreciosModel extends \App\Models\BaseModel
->update();
}
function set_plantilla_id($cliente_id = 0, $plantilla_id = null){
function set_plantilla_id($cliente_id = 0, $plantilla_id = null)
{
$this->db
->table($this->table . " t1")
->where('cliente_id', $cliente_id)
@ -127,8 +130,9 @@ class ClientePreciosModel extends \App\Models\BaseModel
->update();
}
function delete_values($cliente_id = 0){
function delete_values($cliente_id = 0)
{
$session = session();
$datetime = (new \CodeIgniter\I18n\Time("now"));
$date_value = $datetime->format('Y-m-d H:i:s');
@ -141,19 +145,76 @@ class ClientePreciosModel extends \App\Models\BaseModel
->set('user_updated_id', $session->id_user)
->update();
}
function copy_from_plantilla($cliente_id = 0, $plantilla_id = 0){
function debug_all_clientes_to_defecto()
{
$plantilla_id = 5; // por defecto
// Se borran los valores existentes para todos los clientes en una sola consulta
$modelCliente = model('App\Models\Clientes\ClienteModel');
$clientes = $modelCliente->select('id')->where('is_deleted', 0)->findAll();
// Se borra la tabla
$this->db->table($this->table)->truncate();
// Preparar los datos para `insertBatch`
$data = [];
$date_value = date('Y-m-d H:i:s'); // Asume que $date_value es la fecha actual
$userId = auth()->user()->id; // ID del usuario actual
$plantillaModel = model('App\Models\Clientes\ClientePlantillaPreciosLineasModel');
$values = $plantillaModel->getResource([], $plantilla_id)->get()->getResultObject();
$batchSize = 1000;
$data = [];
foreach ($clientes as $index => $cliente) {
foreach ($values as $value) {
$data[] = [
'cliente_id' => $cliente->id,
'plantilla_id' => $plantilla_id,
'tipo' => $value->tipo,
'tipo_maquina' => $value->tipo_maquina,
'tipo_impresion' => $value->tipo_impresion,
'tiempo_min' => $value->tiempo_min,
'tiempo_max' => $value->tiempo_max,
'precio_hora' => $value->precio_hora,
'margen' => $value->margen,
'user_updated_id' => $value->user_updated_id,
'updated_at' => $value->updated_at,
'user_created_id' => $userId,
'created_at' => $date_value,
];
if (count($data) === $batchSize) {
$this->db->table($this->table)->insertBatch($data);
$data = [];
}
}
}
// Insertar los datos restantes
if (!empty($data)) {
$this->db->table($this->table)->insertBatch($data);
}
}
function copy_from_plantilla($cliente_id = 0, $plantilla_id = 0)
{
$session = session();
$datetime = (new \CodeIgniter\I18n\Time("now"));
$date_value = $datetime->format('Y-m-d H:i:s');
// Se borran los valores existentes
$this->delete_values($cliente_id);
// Se cargan los valores de la plantilla
$plantillaModel = model('App\Models\Clientes\ClientePlantillaPreciosLineasModel');
$values = $plantillaModel->getResource([],$plantilla_id)->get()->getResultObject();
$values = $plantillaModel->getResource([], $plantilla_id)->get()->getResultObject();
foreach ($values as $value) {
$this->db
->table($this->table . " t1")
@ -176,9 +237,10 @@ class ClientePreciosModel extends \App\Models\BaseModel
}
function update_from_plantilla($plantilla_id = 0){
function update_from_plantilla($plantilla_id = 0)
{
$datetime = (new \CodeIgniter\I18n\Time("now"));
/*$datetime = (new \CodeIgniter\I18n\Time("now"));
$date_value = $datetime->format('Y-m-d H:i:s');
// Se obtienen todos los id de clientes que usen esa tarifa
@ -217,8 +279,65 @@ class ClientePreciosModel extends \App\Models\BaseModel
->set('updated_at', $value->updated_at)
->set('user_created_id', auth()->user()->id)
->set('created_at', $date_value)
->insert();
->insertBatch($values);
}
}*/
// Se borran los valores existentes para todos los clientes en una sola consulta
$clientes = $this->db
->table($this->table . " t1")
->select("t1.cliente_id AS id")
->where('t1.plantilla_id', $plantilla_id)
->where('t1.is_deleted', 0)
->distinct()
->get()->getResultArray();
// Extraer solo los IDs de los clientes
$clienteIds = array_map(fn($cliente) => $cliente['id'], $clientes);
// Borrar los valores existentes para esos clientes en una sola consulta
if (!empty($clienteIds)) {
$this->db->table($this->table)->whereIn('cliente_id', $clienteIds)->delete();
}
// Preparar los datos para `insertBatch`
$data = [];
$date_value = date('Y-m-d H:i:s'); // Asume que $date_value es la fecha actual
$userId = auth()->user()->id; // ID del usuario actual
$plantillaModel = model('App\Models\Clientes\ClientePlantillaPreciosLineasModel');
$values = $plantillaModel->getResource([], $plantilla_id)->get()->getResultObject();
$batchSize = 1000;
$data = [];
foreach ($clientes as $cliente) {
foreach ($values as $value) {
$data[] = [
'cliente_id' => $cliente['id'],
'plantilla_id' => $plantilla_id,
'tipo' => $value->tipo,
'tipo_maquina' => $value->tipo_maquina,
'tipo_impresion' => $value->tipo_impresion,
'tiempo_min' => $value->tiempo_min,
'tiempo_max' => $value->tiempo_max,
'precio_hora' => $value->precio_hora,
'margen' => $value->margen,
'user_updated_id' => $value->user_updated_id,
'updated_at' => $value->updated_at,
'user_created_id' => $userId,
'created_at' => $date_value,
];
if (count($data) === $batchSize) {
$this->db->table($this->table)->insertBatch($data);
$data = [];
}
}
}
// Insertar todos los datos de una vez
if (!empty($data)) {
$this->db->table($this->table)->insertBatch($data);
}
}
@ -227,8 +346,9 @@ class ClientePreciosModel extends \App\Models\BaseModel
// - tipo_maquina: 'toner', 'inkjet'
// - tipo_impresion: 'negro', 'color', 'negrohq', 'colorhq'
function get_precio_hora($cliente_id, $config, $tiempo){
function get_precio_hora($cliente_id, $config, $tiempo)
{
// Se cargan los valores de la plantilla
$values = $this->db
->table($this->table . " t1")
@ -241,14 +361,14 @@ class ClientePreciosModel extends \App\Models\BaseModel
->where('tiempo_max >=', $tiempo)
->where('is_deleted', 0)
->get()->getResultObject();
if(count($values)>0){
if (count($values) > 0) {
return [floatval(($values[0])->precio_hora), floatval(($values[0])->margen)];
}
return [null, null];
}
/**
* Get resource data.
*
@ -268,7 +388,7 @@ class ClientePreciosModel extends \App\Models\BaseModel
);
$builder->join("users t2", "t1.user_updated_id = t2.id", "left");
$builder->where('t1.is_deleted', 0);
$builder->where('t1.cliente_id', $cliente_id);
@ -277,30 +397,31 @@ class ClientePreciosModel extends \App\Models\BaseModel
else {
$builder->groupStart();
foreach ($search as $col_search) {
if ($col_search[0] > 0 && $col_search[0] < 4)
$builder->where(self::SORTABLE[$col_search[0]], $col_search[2]);
else
$builder->like(self::SORTABLE[$col_search[0]], $col_search[2]);
if ($col_search[0] > 0 && $col_search[0] < 4)
$builder->where(self::SORTABLE[$col_search[0]], $col_search[2]);
else
$builder->like(self::SORTABLE[$col_search[0]], $col_search[2]);
}
$builder->groupEnd();
return $builder;
}
}
public function getPlantilla($cliente_id = -1){
public function getPlantilla($cliente_id = -1)
{
$builder = $this->db
->table($this->table . " t1")
->select(
"t1.plantilla_id AS id, t2.nombre AS nombre"
);
$builder->where('t1.is_deleted', 0);
$builder->where('t1.cliente_id', $cliente_id);
$builder->join("cliente_plantilla_precios t2", "t1.plantilla_id = t2.id", "left");
$builder->limit(1);
$values = $builder->get()->getResultArray();
if(count($values)>0){
if (count($values) > 0) {
return $values[0];
}
@ -309,11 +430,12 @@ class ClientePreciosModel extends \App\Models\BaseModel
public function checkIntervals($data = [], $id_linea = null, $cliente_id = null){
public function checkIntervals($data = [], $id_linea = null, $cliente_id = null)
{
helper('general');
if(floatval($data["tiempo_min"])>= floatval($data["tiempo_max"])){
if (floatval($data["tiempo_min"]) >= floatval($data["tiempo_max"])) {
return lang('ClientePrecios.errors.error_tiempo_range');
}
@ -329,29 +451,36 @@ class ClientePreciosModel extends \App\Models\BaseModel
foreach ($rows as $row) {
if (!is_null($id_linea)){
if($row->id == $id_linea){
if (!is_null($id_linea)) {
if ($row->id == $id_linea) {
continue;
}
}
if(check_overlap(floatval($data["tiempo_min"]), floatval($data["tiempo_max"]),
$row->tiempo_min, $row->tiempo_max)){
return lang('ClientePrecios.errors.error_tiempo_overlap');
if (
check_overlap(
floatval($data["tiempo_min"]),
floatval($data["tiempo_max"]),
$row->tiempo_min,
$row->tiempo_max
)
) {
return lang('ClientePrecios.errors.error_tiempo_overlap');
}
}
return "";
}
public function get_plantilla_precios($cliente_id = -1){
public function get_plantilla_precios($cliente_id = -1)
{
$value = $this->db
->table($this->table)
->select("plantilla_id")
->where("is_deleted", 0)
->where("cliente_id", $cliente_id)
->limit(1)->get()->getResultObject();
if(count($value)>0){
if (count($value) > 0) {
return $value[0]->plantilla_id;
}
return null;