diff --git a/ci4/app/Config/Routes.php b/ci4/app/Config/Routes.php index a90c3767..c348d809 100644 --- a/ci4/app/Config/Routes.php +++ b/ci4/app/Config/Routes.php @@ -255,6 +255,7 @@ $routes->resource('maquinastarifasimpresion', ['namespace' => 'App\Controllers\ $routes->group('maquinaspapelesimpresion', ['namespace' => 'App\Controllers\Configuracion'], function ($routes) { $routes->post('datatable', 'Maquinaspapelesimpresion::datatable', ['as' => 'dataTableOfMaquinasPapelesImpresion']); $routes->post('datatable_editor', 'Maquinaspapelesimpresion::datatable_editor', ['as' => 'editorOfMaquinasPapelImpresion']); + $routes->post('updateOnTarifasChange', 'Maquinaspapelesimpresion::updateOnTarifasChange', ['as' => 'updateMaquinaPapelOnTarifasChange']); }); $routes->resource('maquinastarifasimpresion', ['namespace' => 'App\Controllers\Configuracion', 'controller' => 'Maquinastarifasimpresion', 'except' => 'show,new,create,update']); diff --git a/ci4/app/Controllers/Configuracion/Maquinaspapelesimpresion.php b/ci4/app/Controllers/Configuracion/Maquinaspapelesimpresion.php index b2aaef1d..0a294715 100644 --- a/ci4/app/Controllers/Configuracion/Maquinaspapelesimpresion.php +++ b/ci4/app/Controllers/Configuracion/Maquinaspapelesimpresion.php @@ -8,6 +8,8 @@ use App\Models\Collection; use App\Entities\Configuracion\MaquinasPapelesImpresionEntity; use App\Models\Configuracion\MaquinasPapelesImpresionModel; +use App\Models\Configuracion\MaquinasTarifasImpresionModel; +use App\Models\Configuracion\PapelImpresionModel; use App\Models\Configuracion\MaquinaModel; @@ -62,6 +64,10 @@ class Maquinaspapelesimpresion extends \App\Controllers\GoBaseResourceController public function datatable() { if ($this->request->isAJAX()) { + + // modelos usados + $tarifa_model = new MaquinasTarifasImpresionModel(); + $reqData = $this->request->getPost(); if (!isset($reqData['draw']) || !isset($reqData['columns']) ) { $errstr = 'No data available in response to this specific request.'; @@ -75,23 +81,73 @@ class Maquinaspapelesimpresion extends \App\Controllers\GoBaseResourceController $order = MaquinasPapelesImpresionModel::SORTABLE[$requestedOrder >= 0 ? $requestedOrder : 1]; $dir = $reqData['order']['0']['dir'] ?? 'asc'; - // el primer dato representa el uso y el segundo el tipo - //$tarifas = $reqData['tarifas'] ?? []; + $maquina_id = $reqData['maquina_id'] ?? -1; // Para saber si el papel que se tiene que mostrar es para rotativa $isRotativa= $reqData['isRotativa'] ?? 0; - $resourceData = $this->model->getResource($search, $isRotativa)->orderBy($order, $dir)->limit($length, $start)->get()->getResultObject(); + // Se obtienen las líneas de las tarifas seleccionadas para esta máquina + $tarifas = $tarifa_model->getResource("", $maquina_id)->get()->getResultObject(); + + $resourceData = $this->model->getResource($search, $isRotativa, $tarifas, $maquina_id)->orderBy($order, $dir)->limit($length, $start)->get()->getResultObject(); return $this->respond(Collection::datatable( $resourceData, $this->model->getResource()->countAllResults(), - $this->model->getResource($search, $isRotativa)->countAllResults() + $this->model->getResource($search, $isRotativa, $tarifas, $maquina_id)->countAllResults() )); } else { return $this->failUnauthorized('Invalid request', 403); } } + public function updateOnTarifasChange(){ + + if ($this->request->isAJAX()) { + + $reqData = $this->request->getPost(); + + $maquina_id = $reqData['maquina_id'] ?? -1; + // Para saber si el papel que se tiene que mostrar es para rotativa + $isRotativa= $reqData['isRotativa'] ?? 0; + + // modelos usados + $tarifa_model = new MaquinasTarifasImpresionModel(); + $papelimpresion_model = new PapelImpresionModel(); + + $tarifas = $tarifa_model->getResource("", $maquina_id)->get()->getResultObject(); + + // 1- Se obtienen los papeles que hay seleccionados (de la tabla lg_maquina_papel_impresion y que coincidan con las tarifas actuales) + $initData = $this->model->getInitData($isRotativa, $tarifas, $maquina_id); + // 2- Se cuentan los resultados. Si no hay coincidencias, hay que crear la tabla pivote con todos los papeles que cumplan la + // condición de las tarifas y con la columna active a 0. Si hay coincidencias, se guardan los activos en un array + $active_values = []; + if($initData->countAllResults() != 0){ + $active_values = $this->model->getInitData($isRotativa, $tarifas, $maquina_id)->get()->getResult('array'); + } + // Primero se borran las filas con la maquina_id de la tabla pivote + $this->model->deleteRows($maquina_id); + // Se insertan en la tabla pivote los resultados de los papeles que cumplan con las tarifas con el campo active = 0 + $papeles_ids = $papelimpresion_model->getIdPapelesActivos($tarifas)->get()->getResult('array'); + $data = []; + foreach($papeles_ids as $papel_id){ + $value = [ + 'active' => 0, + 'maquina_id' => $maquina_id, + 'papel_impresion_id' => $papel_id['id'] + ]; + array_push($data, $value); + } + $this->model->insertRows($data); + + // Se actualizan los activos que había en caso de que los hubiera + if(!empty($active_values)){ + $this->model->updateRows($active_values); + } + } else { + return $this->failUnauthorized('Invalid request', 403); + } + } + public function datatable_editor() { if ($this->request->isAJAX()) { @@ -106,9 +162,9 @@ class Maquinaspapelesimpresion extends \App\Controllers\GoBaseResourceController ) ->validator( function($editor, $action, $data){ - /*if ($action === Editor::ACTION_CREATE || $action === Editor::ACTION_EDIT){ + if ($action === Editor::ACTION_EDIT){ foreach ($data['data'] as $pkey => $values ){ - // Si no se quiere borrar... + /*// Si no se quiere borrar... if($data['data'][$pkey]['is_deleted'] != 1) { // Cubierta y sobrecubierta sólo pueden ser en color @@ -129,9 +185,9 @@ class Maquinaspapelesimpresion extends \App\Controllers\GoBaseResourceController return lang('MaquinasTarifasImpresions.validation.duplicated_uso_tipo'); } - } + }*/ } - }*/ + } }) ->debug(true) ->process( $_POST ) diff --git a/ci4/app/Models/Configuracion/MaquinasPapelesImpresionModel.php b/ci4/app/Models/Configuracion/MaquinasPapelesImpresionModel.php index ce4c84b8..515a5c15 100644 --- a/ci4/app/Models/Configuracion/MaquinasPapelesImpresionModel.php +++ b/ci4/app/Models/Configuracion/MaquinasPapelesImpresionModel.php @@ -1,6 +1,8 @@ db ->table($this->table . " t1") @@ -43,44 +45,52 @@ class MaquinasPapelesImpresionModel extends \App\Models\GoBaseModel "t1.maquina_id AS maquina_id, t1.papel_impresion_id AS papel_impresion_id, t1.active AS active, t2.nombre AS maquina, t3.nombre AS papel_impresion, t3.gramaje AS gramaje, , t3.bn AS bn, t3.color AS color, t3.cubierta AS cubierta, , t3.sobrecubierta AS sobrecubierta, t3.rotativa AS rotativa, t4.nombre as papel_generico" - ); - - - $builder->join("lg_maquinas t2", "t1.maquina_id = t2.id", "left"); - $builder->join("lg_papel_impresion t3", "t1.papel_impresion_id = t3.id", "left"); - $builder->join("lg_papel_generico t4", "t3.papel_generico_id = t4.id", "left"); - + ) + ->join("lg_papel_impresion t3", "t1.papel_impresion_id = t3.id", "left") + ->join("lg_maquinas t2", "t1.maquina_id = t2.id", "left") + ->join("lg_papel_generico t4", "t3.papel_generico_id = t4.id", "left"); + + $builder->where("t1.maquina_id", $maquina_id); $builder->where("t2.is_deleted", 0); $builder->where("t3.is_deleted", 0); $builder->where("t4.is_deleted", 0); $builder->where("t3.isActivo", 1); + $builder->where("t3.rotativa", $isRotativa); - $isFirst = false; - $where_str = ""; - // Si hay tarifas... - /*if (!empty($tarifas)){ + $isFirst = true; + $where_str = "("; + //Si hay tarifas... + if (!empty($tarifas)){ foreach ($tarifas as $tarifa){ + if (!$isFirst) - $where_str += ' OR '; - if ($tarifa->uso == 'portada') - $where_str += "t3.portada=1 AND "; - if ($tarifa->tipo == 'negro' || $tarifa->tipo == 'negrohq') - $where_str += "t3.bn=1"; - else - $where_str += "t3.bn=1"; + $where_str .= ' OR '; + else{ + $isFirst = false; + } + if ($tarifa->uso == 'cubierta') + $where_str .= "t3.cubierta=1"; + else if ($tarifa->uso == 'sobrecubierta') + $where_str .= "t3.sobrecubierta=1"; + else{ + if ($tarifa->tipo == 'negro' || $tarifa->tipo == 'negrohq') + $where_str .= "t3.bn=1"; + else + $where_str .= "t3.color=1"; + } } - - $builder->where($where_str); - }*/ + $where_str .= ")"; + $builder->where(new RawSql($where_str)); + } // si no hay tarifas no hay que devolver nada - /*else{ - return ""; - }*/ - + else{ + // Se pone una condicion que no se puede dar + $builder->where("t3.bn", 2); + } return empty($search) @@ -91,4 +101,80 @@ class MaquinasPapelesImpresionModel extends \App\Models\GoBaseModel ->orLike("t3.nombre", $search) ->groupEnd(); } + + public function getInitData($isRotativa = 0, $tarifas = [], $maquina_id = -1) + { + $builder = $this->db + ->table($this->table . " t1") + ->select( + "t1.*" + ); + + + $builder->join("lg_papel_impresion t2", "t1.papel_impresion_id = t2.id", "left"); + + $builder->where("t1.maquina_id", $maquina_id); + $builder->where("t2.is_deleted", 0); + $builder->where("t2.isActivo", 1); + $builder->where("t2.rotativa", $isRotativa); + $builder->where("t1.active", 1); + + + + $isFirst = true; + $where_str = ""; + //Si hay tarifas... + if (!empty($tarifas)){ + foreach ($tarifas as $tarifa){ + if (!$isFirst) + $where_str .= ' OR '; + else{ + $isFirst = false; + } + if ($tarifa->uso == 'cubierta') + $where_str .= "`t2`.`cubierta`=1"; + else if ($tarifa->uso == 'sobrecubierta') + $where_str .= "`t2`.`sobrecubierta`=1"; + else{ + if ($tarifa->tipo == 'negro' || $tarifa->tipo == 'negrohq') + $where_str .= "`t2`.`bn`=1 "; + else + $where_str .= "`t2`.`color`=1 "; + } + } + $builder->where($where_str); + } + // si no hay tarifas no hay que devolver nada + else{ + // Se pone una condicion que no se puede dar + $builder->where("t3.bn", 2); + } + + + return $builder; + } + + public function deleteRows($maquina_id = -1){ + $this->db + ->table($this->table . " t1") + ->where("maquina_id", $maquina_id) + ->delete(); + } + + public function insertRows($values = []){ + if (!empty($values)){ + $this->db->table($this->table)->insertBatch($values); + } + } + + public function updateRows($values = []){ + if (!empty($values)){ + foreach($values as $value){ + $builder = $this->db->table($this->table)->where('maquina_id', $value['maquina_id']); + $builder->where('papel_impresion_id', $value['papel_impresion_id']); + $builder->update($value); + } + + } + } } diff --git a/ci4/app/Models/Configuracion/PapelImpresionModel.php b/ci4/app/Models/Configuracion/PapelImpresionModel.php index 5bff620a..0b8e23b4 100644 --- a/ci4/app/Models/Configuracion/PapelImpresionModel.php +++ b/ci4/app/Models/Configuracion/PapelImpresionModel.php @@ -214,6 +214,7 @@ class PapelImpresionModel extends \App\Models\GoBaseModel $builder->join("lg_papel_generico t2", "t1.papel_generico_id = t2.id", "left"); $builder->where("t1.is_deleted", 0); + $builder->where("t1.isActivo", 1); if($generico_id>0){ $builder->where("t1.papel_generico_id", $generico_id); } @@ -229,4 +230,47 @@ class PapelImpresionModel extends \App\Models\GoBaseModel ->orLike("t2.nombre", $search) ->groupEnd(); } + + public function getIdPapelesActivos($tarifas = []) + { + $builder = $this->db + ->table($this->table . " t1") + ->select( + "t1.id AS id" + ); + + $builder->where("t1.is_deleted", 0); + $builder->where("t1.isActivo", 1); + + $isFirst = true; + $where_str = ""; + //Si hay tarifas... + if (!empty($tarifas)){ + foreach ($tarifas as $tarifa){ + if (!$isFirst) + $where_str .= ' OR '; + else{ + $isFirst = false; + } + if ($tarifa->uso == 'cubierta') + $where_str .= "`t1`.`cubierta`=1"; + else if ($tarifa->uso == 'sobrecubierta') + $where_str .= "`t1`.`sobrecubierta`=1"; + else{ + if ($tarifa->tipo == 'negro' || $tarifa->tipo == 'negrohq') + $where_str .= "`t1`.`bn`=1 "; + else + $where_str .= "`t1`.`color`=1 "; + } + } + $builder->where($where_str); + } + // si no hay tarifas no hay que devolver nada + else{ + // Se pone una condicion que no se puede dar + $builder->where("t1.bn", 2); + } + + return $builder; + } } diff --git a/ci4/app/Views/themes/backend/vuexy/form/configuracion/maquinas/viewMaquinaForm.php b/ci4/app/Views/themes/backend/vuexy/form/configuracion/maquinas/viewMaquinaForm.php index 872cf7e4..d4ec0ea2 100644 --- a/ci4/app/Views/themes/backend/vuexy/form/configuracion/maquinas/viewMaquinaForm.php +++ b/ci4/app/Views/themes/backend/vuexy/form/configuracion/maquinas/viewMaquinaForm.php @@ -274,6 +274,22 @@ theTable.clearPipeline(); theTable.draw(); + + $.ajax({ + url: '', + data: function (d) { + d.id_maquina= id; + d.isRotativa = isRotativa.watch; + d. = v; + }, + method: 'POST', + headers: {'X-Requested-With': 'XMLHttpRequest'}, + success:function(response){ + yeniden(response.); + theTable2.clearPipeline(); + theTable2.draw(); + } + }); }); @@ -284,31 +300,47 @@ : v, }, }, - table : "#tableOfMaquinaspapelesimpresion", + table : "#tableOfPapelesImpresion", fields: [ - { + { + name: "active", + type: "checkbox", + separator: "|", + ipOpts: [ + { label: '', value: 1 } + ] + },{ + "name": "papel_generico" + },{ + "name": "papel_impresion", + },{ + "name": "gramaje" + },{ + "name": "bn" + },{ + "name": "color" + },{ + "name": "cubierta" + },{ + "name": "sobrecubierta" + },{ + "name": "rotativa" + },{ "name": "maquina_id", "type": "hidden" - },{ - "name": "papel_generico_id", - "type": "hidden" - },{ - "name": "active", - }, + } ] } ); - editor2.on( 'preSubmit', function ( e, d, type ) { - if ( type === 'create'){ - d.data[0]['maquina_id'] = id; - } - else if(type === 'edit' ) { + /*editor2.on( 'preSubmit', function ( e, d, type ) { + if(type === 'edit' ) { for (v in d.data){ d.data[v]['maquina_id'] = id; + //d.data[v]['papel_impresion_id'] = papel_impresion_id; } } - }); + });*/ editor2.on( 'postSubmit', function ( e, json, data, action ) { @@ -316,12 +348,6 @@ yeniden(json.); }); - /*editor2.on( 'submitSuccess', function ( e, json, data, action ) { - - theTable2.clearPipeline(); - theTable2.draw(); - });*/ - var theTable = $('#tableOfMaquinastarifasimpresion').DataTable( { serverSide: true, @@ -396,6 +422,7 @@ } ] } ); + var theTable2 = $('#tableOfPapelesImpresion').DataTable( { serverSide: true, processing: true, @@ -412,7 +439,7 @@ ajax : $.fn.dataTable.pipeline( { url: '', data: function (d) { - d.id_maquina = id; + d.maquina_id = id; d.isRotativa = isRotativa.watch; }, method: 'POST', @@ -452,6 +479,15 @@ } ], } ); + + // Cuando se clica en un checkbox hacer submit en el editor + theTable2.on( 'change', 'input.editor-active', function () { + console.log($(this).closest('tr')[0])); + editor2 + .edit( $(this).closest('tr'), false ) + .set( 'active', $(this).prop( 'checked' ) ? 1 : 0 ) + .submit(); + } ); // Activate an inline edit on click of a table cell @@ -500,7 +536,6 @@ .set( 'deleted_at', new Date().toISOString().slice(0, 19).replace('T', ' ') ) .set( 'is_deleted', 1 ) .submit(); - } }); }); @@ -528,6 +563,6 @@ - + endSection() ?> \ No newline at end of file