From 16561f49cee2d7ce481ea0494e84fdc2e9aaa101 Mon Sep 17 00:00:00 2001 From: Jaime Jimenez Date: Sun, 3 Dec 2023 20:16:46 +0100 Subject: [PATCH] terminados servicios acabados --- ci4/app/Config/Routes.php | 8 +- .../Presupuestos/Cosidotapablanda.php | 8 +- .../Presupuestos/PresupuestoAcabados.php | 121 ------ .../Presupuestos/Presupuestoacabados.php | 93 +++++ ci4/app/Controllers/Test.php | 12 +- ci4/app/Language/es/Presupuestos.php | 1 + .../Presupuestos/PresupuestoAcabadosModel.php | 97 ++++- .../PresupuestoEncuadernacionesModel.php | 43 +-- ci4/app/Models/Tarifas/TarifaacabadoModel.php | 36 ++ .../cosidotapablanda/_datosServiciosItems.php | 364 +++++++++++------- 10 files changed, 480 insertions(+), 303 deletions(-) delete mode 100755 ci4/app/Controllers/Presupuestos/PresupuestoAcabados.php create mode 100755 ci4/app/Controllers/Presupuestos/Presupuestoacabados.php diff --git a/ci4/app/Config/Routes.php b/ci4/app/Config/Routes.php index 92dba46a..6a551a25 100755 --- a/ci4/app/Config/Routes.php +++ b/ci4/app/Config/Routes.php @@ -493,16 +493,16 @@ $routes->group('cosidotapablanda', ['namespace' => 'App\Controllers\Presupuestos $routes->resource('cosidotapablanda', ['namespace' => 'App\Controllers\Presupuestos', 'controller' => 'Cosidotapablanda', 'except' => 'show,new,create,update']); $routes->group('serviciosacabados', ['namespace' => 'App\Controllers\Presupuestos'], function ($routes) { - $routes->post('datatable', 'PresupuestoAcabados::datatable', ['as' => 'dataTableOfPresupuestoAcabados']); - $routes->post('datatable_editor', 'PresupuestoAcabados::datatable_editor', ['as' => 'editorOfPresupuestoAcabados']); - $routes->get('delete/(:num)', 'PresupuestoAcabados::delete/$1', ['as' => 'deletePresupuestoAcabado']); + $routes->post('datatable', 'Presupuestoacabados::datatable', ['as' => 'dataTableOfPresupuestoAcabados']); + $routes->get('delete/(:num)', 'Presupuestoacabados::delete/$1', ['as' => 'deletePresupuestoAcabado']); + $routes->post('edit/(:num)', 'Presupuestoacabados::edit/$1', ['as' => 'updatePresupuestoacabados']); }); $routes->group('serviciosencuadernaciones', ['namespace' => 'App\Controllers\Presupuestos'], function ($routes) { $routes->post('datatable', 'Presupuestoencuadernaciones::datatable', ['as' => 'dataTableOfPresupuestoEncuadernaciones']); $routes->post('menuitems', 'Presupuestoencuadernaciones::menuItems', ['as' => 'menuItemsOfPresupuestoEncuadernaciones']); $routes->get('delete/(:num)', 'Presupuestoencuadernaciones::delete/$1', ['as' => 'deletePresupuestoencuadernaciones']); - $routes->post('edit/(:num)', 'Presupuestoencuadernaciones::edit/$1', ['as' => 'updateCosidotapablanda']); + $routes->post('edit/(:num)', 'Presupuestoencuadernaciones::edit/$1', ['as' => 'updatePresupuestoencuadernaciones']); }); diff --git a/ci4/app/Controllers/Presupuestos/Cosidotapablanda.php b/ci4/app/Controllers/Presupuestos/Cosidotapablanda.php index bcb9b199..f4812f10 100755 --- a/ci4/app/Controllers/Presupuestos/Cosidotapablanda.php +++ b/ci4/app/Controllers/Presupuestos/Cosidotapablanda.php @@ -11,6 +11,7 @@ use App\Models\Configuracion\PapelGenericoModel; use App\Models\Presupuestos\PresupuestoModel; use App\Models\Presupuestos\PresupuestoEncuadernacionesModel; +use App\Models\Presupuestos\PresupuestoAcabadosModel; use App\Services\PresupuestoService; use App\Models\Configuracion\PapelImpresionModel; @@ -277,11 +278,12 @@ class Cosidotapablanda extends \App\Controllers\GoBaseResourceController $this->viewData['lineasPresupuesto'] = $this->getLineasPresupuestoActivas($id); // Tarifas + $this->viewData['serviciosAcabado'] = $this->getServiciosAcabado(); $this->viewData['serviciosPreimpresion'] = $this->getServiciosPreimpresion(); $this->viewData['serviciosEncuadernacion'] = $this->getServiciosEncuadernacion(); - $this->viewData['serviciosAcabado'] = $this->getServiciosAcabado(); $this->viewData['serviciosManipulado'] = $this->getServiciosManipulado(); $this->viewData['serviciosEncuadernacionList'] = (new PresupuestoEncuadernacionesModel())->getResource($id)->get()->getResultObject(); + $this->viewData['serviciosAcabadosList'] = (new PresupuestoAcabadosModel())->getResource($id)->get()->getResultObject(); $this->viewData['POD'] = $this->getPOD(); @@ -999,10 +1001,10 @@ class Cosidotapablanda extends \App\Controllers\GoBaseResourceController { $model = model('App\Models\Tarifas\TarifaacabadoModel'); $data = $model->getServiciosAcabadoSelector(); - array_unshift($data, (object)array( + /*array_unshift($data, (object)array( "value" => 0, "label" => lang('Basic.global.pleaseSelectA', [mb_strtolower(lang('Presupuestos.servicioAcabado'))]) - )); + ));*/ return $data; } diff --git a/ci4/app/Controllers/Presupuestos/PresupuestoAcabados.php b/ci4/app/Controllers/Presupuestos/PresupuestoAcabados.php deleted file mode 100755 index d5374629..00000000 --- a/ci4/app/Controllers/Presupuestos/PresupuestoAcabados.php +++ /dev/null @@ -1,121 +0,0 @@ -request->isAJAX()) { - $reqData = $this->request->getPost(); - if (!isset($reqData['draw']) || !isset($reqData['columns'])) { - $errstr = 'No data available in response to this specific request.'; - $response = $this->respond(Collection::datatable([], 0, 0, $errstr), 400, $errstr); - return $response; - } - $start = $reqData['start'] ?? 0; - $length = $reqData['length'] ?? 5; - $requestedOrder = $reqData['order']['0']['column'] ?? 1; - $order = PresupuestoAcabadosModel::SORTABLE[$requestedOrder >= 0 ? $requestedOrder : 1]; - $dir = $reqData['order']['0']['dir'] ?? 'asc'; - - $id_P = $reqData['id_presupuesto'] ?? -1; - - $resourceData = $this->model->getResource($id_P)->orderBy($order, $dir)->limit($length, $start)->get()->getResultObject(); - - return $this->respond(Collection::datatable( - $resourceData, - $this->model->getResource()->countAllResults(), - $this->model->getResource($id_P)->countAllResults() - )); - } else { - return $this->failUnauthorized('Invalid request', 403); - } - } - - public function datatable_editor() { - if ($this->request->isAJAX()) { - - include(APPPATH . "ThirdParty/DatatablesEditor/DataTables.php"); - - // Build our Editor instance and process the data coming from _POST - $response = Editor::inst( $db, 'presupuesto_acabados' ) - ->fields( - Field::inst( 'tarifa_acabado_id' ) - ->validator( 'Validate::notEmpty',array( - 'message' => 'Selecciones servicios de acabado' ) - ), - Field::inst( 'precio_unidad' ) - ->validator( 'Validate::notEmpty',array( - 'message' => 'Falta precio unitario' ) - ), - Field::inst( 'precio_total' ) - ->validator( 'Validate::notEmpty',array( - 'message' => 'Falta precio total' ) - ), - - Field::inst( 'presupuesto_id' ), - - ) - ->validator( function($editor, $action, $data){ - if ($action === Editor::ACTION_CREATE || $action === Editor::ACTION_EDIT){ - //return $response; - /*foreach ($data['data'] as $pkey => $values ){ - // No se pueden duplicar valores al crear o al editar - if (!empty($response)){ - return $response; - } - }*/ - } - }) - ->debug(true) - ->process( $_POST ) - ->data(); - - $newTokenHash = csrf_hash(); - $csrfTokenName = csrf_token(); - - $response[$csrfTokenName] = $newTokenHash; - - echo json_encode($response); - - } else { - return $this->failUnauthorized('Invalid request', 403); - } - } - - - -} diff --git a/ci4/app/Controllers/Presupuestos/Presupuestoacabados.php b/ci4/app/Controllers/Presupuestos/Presupuestoacabados.php new file mode 100755 index 00000000..c451649a --- /dev/null +++ b/ci4/app/Controllers/Presupuestos/Presupuestoacabados.php @@ -0,0 +1,93 @@ +request->getJSON(); + $tarifas = array_column($postData->datos, 'tarifa_id'); + if(count($tarifas)>0){ + $this->model->deleteServiciosNotInArray($requestedId, $tarifas); + } + else{ + $this->model->deleteAllServicios($requestedId); + } + + $this->model->updateTarifas($requestedId, $postData->datos); + + $newTokenHash = csrf_hash(); + $csrfTokenName = csrf_token(); + $data = [ + $csrfTokenName => $newTokenHash + ]; + + return $this->respond($data); + } + + public function datatable() + { + if ($this->request->isAJAX()) { + $reqData = $this->request->getPost(); + + $tarifa_acabado_id = $reqData['tarifa_acabado_id'] ?? 0; + $tirada = $reqData['tirada'] ?? 0; + $POD = $reqData['POD'] ?? 0; + + $newTokenHash = csrf_hash(); + $csrfTokenName = csrf_token(); + + $values = $this->model->getPrecioTarifa($tarifa_acabado_id, $tirada, $POD); + + $data = [ + 'values' => $values, + $csrfTokenName => $newTokenHash + ]; + + return $this->respond($data); + + } else { + return $this->failUnauthorized('Invalid request', 403); + } + } + + +} diff --git a/ci4/app/Controllers/Test.php b/ci4/app/Controllers/Test.php index 747e90a7..58601ca6 100755 --- a/ci4/app/Controllers/Test.php +++ b/ci4/app/Controllers/Test.php @@ -18,12 +18,7 @@ class Test extends BaseController public function index() { echo '
';
-        $datetime = (new \CodeIgniter\I18n\Time("now"));
-
-        $modelTarifa = model('App\Models\Tarifas\TarifaEncuadernacionLineaHorasModel');
-        $test = $modelTarifa->removeAllEncuadernacionLineasHorasForTarifa(2, $datetime, 1);        
-        var_dump($test);
-        //var_dump($this->get_precio_tarifa_horas(2, 200, 100, -1, false));
+        var_dump($this->get_precio_tarifa_acabado(1, 200, 30));
         echo '
'; } @@ -38,6 +33,11 @@ class Test extends BaseController return $model->getPrecioTarifa($tarifa_encuadernacion_id, $paginas, $tirada, $ancho, $alto, $proveedor_id, $POD); } + public function get_precio_tarifa_acabado($tarifa_id, $tirada, $POD){ + $model = model('App\Models\Presupuestos\PresupuestoAcabadosModel'); + return $model->getPrecioTarifa($tarifa_id, $tirada, $POD); + } + public function get_precio_tarifa_horas($tarifa_encuadernacion_id, $paginas, $tirada, $proveedor_id, $POD){ $model = model('App\Models\Presupuestos\PresupuestoEncuadernacionesModel'); return $model->getPrecioTarifaHoras($tarifa_encuadernacion_id, $paginas, $tirada, $proveedor_id, $POD); diff --git a/ci4/app/Language/es/Presupuestos.php b/ci4/app/Language/es/Presupuestos.php index c0e48986..2f711d42 100755 --- a/ci4/app/Language/es/Presupuestos.php +++ b/ci4/app/Language/es/Presupuestos.php @@ -152,6 +152,7 @@ return [ 'servicioPreimpresion' => 'Servicio de preimpresión', 'servicioEncuadernado' => 'Servicio de encuadernación', 'servicioEncuadernadoList' => 'Servicios de encuadernación', + 'servicioAcabadoList' => 'Servicios de acabado', 'servicioManipulado' => 'Servicio de manipulado', 'comentarios' => 'Comentarios', diff --git a/ci4/app/Models/Presupuestos/PresupuestoAcabadosModel.php b/ci4/app/Models/Presupuestos/PresupuestoAcabadosModel.php index ed32bfb4..510d1bd4 100755 --- a/ci4/app/Models/Presupuestos/PresupuestoAcabadosModel.php +++ b/ci4/app/Models/Presupuestos/PresupuestoAcabadosModel.php @@ -44,6 +44,100 @@ class PresupuestoAcabadosModel extends \App\Models\GoBaseModel ], ]; + public function getPrecioTarifa($tarifa_acabado_id, $tirada, $POD){ + + $modelTarifa = model('App\Models\Tarifas\TarifaacabadoModel'); + $tarifa_value = $modelTarifa->getTarifaPresupuestoAcabado($tarifa_acabado_id, $tirada); + if (count($tarifa_value)>0) { + + $result_data = $this->calcularTarifa($tarifa_value[0], $tirada, $POD<$tirada?false:true); + $ret_array[] = (object)[ + 'tarifa_id'=> $tarifa_value[0]->tarifa_acabado_id, + 'tarifa_nombre'=> $tarifa_value[0]->tarifa_acabado_nombre, + 'precio_unidad'=> $result_data[0], + 'total'=> $result_data[1], + ]; + return $ret_array; + } + else{ + $ret_array[] = (object)[ + 'tarifa_id'=> $tarifa_acabado_id, + 'tarifa_nombre'=> $modelTarifa->getNombreTarifaAcabado($tarifa_acabado_id)[0]->nombre, + 'precio_unidad' => 0, + 'total'=> 0, + ]; + return $ret_array; + } + return []; + } + + private function calcularTarifa($tarifa, $tirada, $is_POD=false){ + + $precio_unidad = floatval($tarifa->precio_min) - (floatval($tarifa->precio_min) - floatval($tarifa->precio_max))/($tarifa->tirada_max - $tarifa->tirada_min) * ($tirada - $tarifa->tirada_min); + if ($tirada > $tarifa->tirada_max) + $precio_unidad = $tarifa->precio_max; + $precio_unidad = $precio_unidad* (1+ floatval($tarifa->margen)/100.0); + + $total = $precio_unidad * $tirada; + + if (!$is_POD){ + $total += floatval($tarifa->tarifa_importe_fijo); + } + + return [$precio_unidad, $total]; + } + + 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_acabado_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_acabado_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_acabado_id', $tarifa->tarifa_id) + ->set('precio_unidad', $tarifa->precio_unidad) + ->set('precio_total', $tarifa->precio_total) + ->update(); + + + } + else{ + $this->db + ->table($this->table . " t1") + ->set('presupuesto_id', $presupuesto_id) + ->set('tarifa_acabado_id', $tarifa->tarifa_id) + ->set('precio_unidad', $tarifa->precio_unidad) + ->set('precio_total', $tarifa->precio_total) + ->insert(); + } + } + } /** * Get resource data. @@ -57,7 +151,8 @@ class PresupuestoAcabadosModel extends \App\Models\GoBaseModel $builder = $this->db ->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, t2.nombre AS nombre" + "t1.id AS id, t1.tarifa_acabado_id AS tarifa_acabado_id, t1.precio_unidad AS precio_unidad, + t1.precio_total AS precio_total, t2.nombre AS nombre" ); $builder->where('t1.presupuesto_id', $presupuesto_id); diff --git a/ci4/app/Models/Presupuestos/PresupuestoEncuadernacionesModel.php b/ci4/app/Models/Presupuestos/PresupuestoEncuadernacionesModel.php index 64f8e8b3..440f2d97 100755 --- a/ci4/app/Models/Presupuestos/PresupuestoEncuadernacionesModel.php +++ b/ci4/app/Models/Presupuestos/PresupuestoEncuadernacionesModel.php @@ -14,11 +14,11 @@ class PresupuestoEncuadernacionesModel extends \App\Models\GoBaseModel protected $useAutoIncrement = true; const SORTABLE = [ - 1 => "t1.tarifa_encuadernado_id", - 2 => "t2.nombre", - 4 => "t3.nombre", - 5 => "t1.precio_unidad", - 6 => "t1.precio_total" + 0 => "t1.tarifa_encuadernado_id", + 1 => "t2.nombre", + 2 => "t3.nombre", + 3 => "t1.precio_unidad", + 4 => "t1.precio_total" ]; protected $allowedFields = ["presupuesto_id", "tarifa_encuadernado_id", "nombre", "precio_total", "precio_unidad"]; @@ -61,11 +61,8 @@ class PresupuestoEncuadernacionesModel extends \App\Models\GoBaseModel if($modelTarifa->isTarifaPorHoras($tarifa['tarifa_encuadernacion_id'])){ - $maquinaModel = model("App\Models\Configuracion\MaquinaModel"); - $pliegos_libro = $paginas/4; - $pliegos_pedido = $pliegos_libro*$tirada; - $velocidad = $maquinaModel->getVelocidad(16); // ID fija. Cambiar cuando se metan maquinas de corte. Velocidad en minutos - $tiempo = round($pliegos_pedido/($velocidad*60.0), 2); + $tiempo = $this->calcularTiempo(16, $paginas, $tirada); // ID fija. Cambiar cuando se metan maquinas de corte. Velocidad en minutos + $tarifa_value = $modelTarifa->getTarifaPresupuestoEncuadernacionHoras($tarifa['tarifa_encuadernacion_id'], $tiempo, $tirada); if (count($tarifa_value)>0) { @@ -130,12 +127,8 @@ class PresupuestoEncuadernacionesModel extends \App\Models\GoBaseModel $proveedores = []; $modelTarifa = model('App\Models\Tarifas\TarifaEncuadernacionModel'); - $maquinaModel = model("App\Models\Configuracion\MaquinaModel"); - - $pliegos_libro = $paginas/4; - $pliegos_pedido = $pliegos_libro*$tirada; - $velocidad = $maquinaModel->getVelocidad(16); // ID fija. Cambiar cuando se metan maquinas de corte. Velocidad en minutos - $tiempo = round($pliegos_pedido/($velocidad*60.0), 2); + + $tiempo = $this->calcularTiempo(16, $paginas, $tirada); // ID fija. Cambiar cuando se metan maquinas de corte. Velocidad en minutos if($modelTarifa->isTarifaPorHoras($tarifa_encuadernacion_id)){ $tarifa_value = $modelTarifa->getTarifaPresupuestoEncuadernacionHoras($tarifa_encuadernacion_id, $tiempo, $tirada); @@ -190,11 +183,8 @@ class PresupuestoEncuadernacionesModel extends \App\Models\GoBaseModel public function getPrecioTarifaHoras($tarifa_encuadernacion_id, $paginas, $tirada, $proveedor_id, $POD){ $modelTarifa = model('App\Models\Tarifas\TarifaEncuadernacionModel'); - $maquinaModel = model("App\Models\Configuracion\MaquinaModel"); - $pliegos_libro = $paginas/4; - $pliegos_pedido = $pliegos_libro*$tirada; - $velocidad = $maquinaModel->getVelocidad(16); // ID fija. Cambiar cuando se metan maquinas de corte. Velocidad en minutos - $tiempo = round($pliegos_pedido/($velocidad*60.0), 2); + + $tiempo = $this->calcularTiempo(16, $paginas, $tirada); // ID fija. Cambiar cuando se metan maquinas de corte. Velocidad en minutos $tarifa_value = $modelTarifa->getTarifaPresupuestoEncuadernacionHoras($tarifa_encuadernacion_id, $tiempo, $tirada, $proveedor_id); if (count($tarifa_value)>0) { @@ -228,7 +218,7 @@ class PresupuestoEncuadernacionesModel extends \App\Models\GoBaseModel } - public function calcularTarifa($tarifa, $paginas, $is_POD=false){ + private function calcularTarifa($tarifa, $paginas, $is_POD=false){ $precio_unidad = floatval($tarifa->precio_min) - (floatval($tarifa->precio_min) - floatval($tarifa->precio_max))/($tarifa->paginas_max - $tarifa->paginas_min) * ($paginas - $tarifa->paginas_min); if ($paginas > $tarifa->paginas_max) @@ -334,4 +324,13 @@ class PresupuestoEncuadernacionesModel extends \App\Models\GoBaseModel return $builder; } + + private function calcularTiempo($maquina_id, $paginas, $tirada){ + + $maquinaModel = model("App\Models\Configuracion\MaquinaModel"); + $pliegos_libro = $paginas/4; + $pliegos_pedido = $pliegos_libro*$tirada; + $velocidad = $maquinaModel->getVelocidad($maquina_id); + return round($pliegos_pedido/($velocidad*60.0), 2); + } } diff --git a/ci4/app/Models/Tarifas/TarifaacabadoModel.php b/ci4/app/Models/Tarifas/TarifaacabadoModel.php index ddadf1d5..ac94e90e 100755 --- a/ci4/app/Models/Tarifas/TarifaacabadoModel.php +++ b/ci4/app/Models/Tarifas/TarifaacabadoModel.php @@ -111,5 +111,41 @@ class TarifaacabadoModel extends \App\Models\GoBaseModel return $builder->orderBy("t1.nombre", "asc")->get()->getResultObject(); } + public function getTarifaPresupuestoAcabado($tarifa_id, $tirada){ + + $builder = $this->db + ->table($this->table . " t1") + ->select( + "t1.id AS tarifa_acabado_id, t1.nombre AS tarifa_acabado_nombre, t1.precio_min AS tarifa_precio_min, t1.importe_fijo AS tarifa_importe_fijo, + t2.id AS tarifa_linea_id, t2.tirada_min AS tirada_min, t2.tirada_max AS tirada_max, + t2.precio_min AS precio_min, t2.precio_max AS precio_max, t2.margen AS margen" + ) + ->join("tarifa_acabado_lineas t2", "t1.id = t2.tarifa_acabado_id", "left") + ->where("t1.is_deleted", 0) + //->where("t1.mostrar_en_presupuesto", 1) + ->where("t2.is_deleted", 0); + + $builder->where('t1.id =', $tarifa_id); + $builder->where('t2.tirada_min <=', $tirada); + $builder->where('t2.tirada_max >', $tirada); + + return $builder->get()->getResultObject(); + } + + public function getNombreTarifaAcabado($id=-1) + { + /* + Todos los servicios de encuadernacion activas que se pueden usar en presupuestos + */ + $builder = $this->db + ->table($this->table . " t1") + ->select( + "t1.nombre AS nombre" + ) + ->where("t1.id", $id) + ->where("t1.is_deleted", 0); + + return $builder->orderBy("t1.nombre", "asc")->get()->getResultObject(); + } } diff --git a/ci4/app/Views/themes/backend/vuexy/form/presupuestos/cosidotapablanda/_datosServiciosItems.php b/ci4/app/Views/themes/backend/vuexy/form/presupuestos/cosidotapablanda/_datosServiciosItems.php index a1bd4542..e2acac24 100755 --- a/ci4/app/Views/themes/backend/vuexy/form/presupuestos/cosidotapablanda/_datosServiciosItems.php +++ b/ci4/app/Views/themes/backend/vuexy/form/presupuestos/cosidotapablanda/_datosServiciosItems.php @@ -64,9 +64,14 @@
+
+
+
+
+ @@ -76,8 +81,25 @@
+
+
+ +
+
+ +
+ +
+
@@ -164,6 +186,7 @@ section("additionalInlineJs") ?> + const url = window.location.href; const url_parts = url.split('/'); if(url_parts[url_parts.length-2] == 'edit'){ @@ -178,6 +201,7 @@ }) + const POD = ; // Delete row $(document).on('click', '.btn-delete-serv', function(e) { @@ -188,156 +212,221 @@ if($(this).closest('table').attr('id').includes('tableOfServiciosEncuadernacion')){ table = "tableOfServiciosEncuadernacion"; } + else if($(this).closest('table').attr('id').includes('tableOfServiciosAcabado')){ + table = "tableOfServiciosAcabado"; + } else{ table = ""; } - $('#' + table).DataTable().rows( function ( idx, data, node ) { - return data[0] === dataId; - } ) - .remove() - .draw(); - + if(table != ""){ + $('#' + table).DataTable().rows( function ( idx, data, node ) { + return data[0] === dataId; + } ) + .remove() + .draw(); + } + check_serv_enc_error() + check_serv_acabado_error() }); + function save_servicios(){ + + var datosAcabado = get_datos_acabado() + + var datosEnc = get_datos_encuadernacion() + const domain = window.location.origin + + fetch(domain + "/presupuestos/presupuestoencuadernaciones/edit/" + id , { + method: "POST", + body: JSON.stringify({ + datos: datosEnc, + : v + }), + headers: { + "Content-type": "application/json; charset=UTF-8" + } + }) + .then(response => response.json()) + .then(data => { + yeniden(data.); + }) + .then( + fetch(domain + "/presupuestos/presupuestoacabados/edit/" + id , { + method: "POST", + body: JSON.stringify({ + datos: datosAcabado, + : v + }), + headers: { + "Content-type": "application/json; charset=UTF-8" + } + }) + .then(response => response.json()) + .then(data => { + yeniden(data.); + }) + + ) + + } + endSection() ?> section("additionalInlineJs") ?> -/* - const lastColNr = $('#tableOfServiciosAcabado').find("tr:first th").length - 1; - - - editor = new $.fn.dataTable.Editor( { - ajax: { - url: "", - headers: { - : v, - }, - }, - table : "#tableOfServiciosAcabado", - idSrc: 'id', - fields: [ { - "name": "tarifa_acabado_id", - "type": "select" - }, { - "name": "precio_unidad" - }, { - "name": "precio_total" - }, { - "name": "presupuesto_id", - "type": "hidden" - }, - ] - } ); // Generación de la lista de servicios de acabado (id, nombre) const acabadosList = ; - editor.field( 'tarifa_acabado_id' ).update( acabadosList ); - - editor.on( 'preSubmit', function ( e, d, type ) { - if ( type === 'create'){ - d.data[0]['presupuesto_id'] = id; - } - else if(type === 'edit' ) { - for (v in d.data){ - d.data[v]['presupuesto_id'] = id; - } - } - }); - - editor.on( 'postSubmit', function ( e, json, data, action ) { - yeniden(json.); - }); - - editor.on( 'submitSuccess', function ( e, json, data, action ) { - theTable.clearPipeline(); - theTable.draw(); - }); - - // Activate an inline edit on click of a table cell - $('#tableOfServiciosAcabado').on( 'click', 'tbody span.edit', function (e) { - editor.inline( - theTable.cells(this.parentNode.parentNode, '*').nodes(), - { - cancelHtml: '', - cancelTrigger: 'span.cancel', - submitHtml: '', - submitTrigger: 'span.edit', - submit: 'allIfChanged' - } - ); - } ); - - var theTable = $('#tableOfServiciosAcabado').DataTable( { - draw: 1, - serverSide: true, - processing: true, - autoWidth: true, - responsive: true, - lengthMenu: [ 5, 10, 25], - order: [[ 0, "asc" ], [ 1, "asc" ]], - pageLength: 10, - lengthChange: true, + var tableServiciosAcabado = new DataTable('#tableOfServiciosAcabado',{ + scrollX: true, searching: false, - paging: true, + paging: false, info: false, - dom: '<"mt-4"><"float-start"l><"mt-4 mb-3"p>', - ajax : $.fn.dataTable.pipeline( { - url: '', - data: { - id_presupuesto: id, - }, - method: 'POST', - headers: {'X-Requested-With': 'XMLHttpRequest'}, - async: false, - }), - columns: [ - { 'data': 'id'}, - { 'data': 'tarifa_acabado_id'}, - { 'data': 'precio_unidad' }, - { 'data': 'precio_total' }, - { - data: actionBtns, - className: 'row-edit dt-center' - } - ], - columnDefs: [ - {'visible':false, 'targets' : 2}, - { - orderable: false, - searchable: false, - targets: [lastColNr] - }, - { - "orderData": [ 0, 1 ], - "targets": 1 - }, - - ], + ordering: false, + responsive: true, + select: false, language: { url: "//cdn.datatables.net/plug-ins/1.13.4/i18n/i18n ?>.json" }, - buttons: [ { - className: 'btn btn-primary float-end me-sm-3 me-1', - extend: "createInline", - editor: editor, - formOptions: { - submitTrigger: -1, - submitHtml: '' - } - } ] - } ); + + }); function init_servicio_acabado(){ + const serviciosacabadosList = ; + $('#add_servicio_acabado_list').select2({ + allowClear: false, + minimumResultsForSearch: -1, + placeholder: '' + }) + + serviciosacabadosList.forEach((element) =>{ + + tableServiciosAcabado.row.add([ + element.tarifa_acabado_id, + element.nombre, + '' + parseFloat(element.precio_unidad).toFixed(2) + '', + '', + '' + ]).draw(false) + + }) + check_serv_acabado_error() } -*/ + + function check_serv_acabado_error(){ + + var htmlString = ''; + + $('#tableOfServiciosAcabado tr').each(function(){ + if($(this).find('td').eq(2).text() == '0.00'){ + htmlString = ` + `; + } + }) + $('#serv-acabado-error').html(htmlString) + } + + function get_tarifas_acabado(tarifa_id = -1){ + const dimension = getDimensionLibro(); + + if(parseInt($('#tirada').val())>0){ + var tirada = parseInt($('#tirada').val()) + } + else{ + var tirada = 0 + } + + var datos = { + tarifa_acabado_id : tarifa_id, + tirada: tirada, + POD: POD, + : v + }; + + + $.ajax({ + type: "POST", + url: '', + data: datos, + success: function (data) { + + data.values.forEach((row) => { + + tableServiciosAcabado.row.add([ + row.tarifa_id, + row.tarifa_nombre, + '' + parseFloat(row.precio_unidad).toFixed(2) + '', + '', + '' + ]).draw(false) + }); + + check_serv_acabado_error() + yeniden(data.); + return true; + }, + error: function(e){ + return false; + } + }) + return false; + } + + $('#insertar_serv_acabado').on('click', function(){ + const tarifa_text = $('#add_servicio_acabado_list').select2('data')[0].text.trim() + + if( $('#add_servicio_acabado_list').select2('data')[0].text.trim().length > 0){ + if($('#tableOfServiciosAcabado tr > td:contains(' + tarifa_text + ')').length == 0) + get_tarifas_acabado($('#add_servicio_acabado_list').select2('data')[0].id); + else{ + popErrorAlert("Ese servicio ya existe", 'serv-acabado-alert') + } + } + check_serv_enc_error() + }) + + function get_datos_acabado(){ + + var datosAcabado = [] + $("#tableOfServiciosAcabado tr").each(function (index,tr) { + var values = {} + $(this).find("td").each(function (index2) { + + switch (index2) { + case 0: + values['tarifa_id'] = $(this).text() + break + case 2: + values['precio_unidad'] = $(this).text() + break + case 3: + values['precio_total'] = $(this).children(":first").val() + break + } + }) + if(Object.keys(values).length>0) + datosAcabado.push(values) + }) + + return datosAcabado + } + + init_servicio_acabado() + endSection() ?> @@ -345,8 +434,6 @@ section("additionalInlineJs") ?> - const POD = ; - // Generación de la lista de servicios de acabado (id, nombre) const encuadernadosList = ; @@ -610,9 +697,9 @@ check_serv_enc_error() }) - - function save_servicios(){ - var datos = [] + function get_datos_encuadernacion(){ + + var datosEnc = [] $("#tableOfServiciosEncuadernacion tr").each(function (index,tr) { var values = {} $(this).find("td").each(function (index2) { @@ -633,28 +720,13 @@ } }) if(Object.keys(values).length>0) - datos.push(values) + datosEnc.push(values) }) - const domain = window.location.origin - - fetch(domain + "/presupuestos/presupuestoencuadernaciones/edit/" + id , { - method: "POST", - body: JSON.stringify({ - datos: datos, - : v - }), - headers: { - "Content-type": "application/json; charset=UTF-8" - } - }) - .then(response => response.json()) - .then(data => { - yeniden(data.); - }); - + return datosEnc } + init_servicio_encuadernado() endSection() ?>