diff --git a/ci4/app/Config/Routes.php b/ci4/app/Config/Routes.php index f2aa64e6..e7f8686b 100644 --- a/ci4/app/Config/Routes.php +++ b/ci4/app/Config/Routes.php @@ -505,15 +505,12 @@ $routes->group('pedidos', ['namespace' => 'App\Controllers\Pedidos'], function ( $routes->resource('pedidos', ['namespace' => 'App\Controllers\Pedidos', 'controller' => 'Pedido', 'except' => 'show,new,create,update']); -$routes->group('albaranes', ['namespace' => 'App\Controllers\Pedidos'], function ($routes) { - $routes->post('add', 'Albaran::add', ['as' => 'crearAlbaranesPedido']); - $routes->post('update/(:any)', 'Albaran::update/$1', ['as' => 'actualizarAlbaran']); - $routes->post('updateLinea/(:any)', 'Albaran::updateLinea/$1', ['as' => 'actualizarLineaAlbaran']); - $routes->post('deletelinea', 'Albaran::borrarlinea', ['as' => 'borrarAlbaranLinea']); - $routes->get('delete/(:any)', 'Albaran::delete/$1', ['as' => 'borrarAlbaran']); - $routes->get('getalbaranes/(:any)', 'Albaran::getAlbaranes/$1', ['as' => 'getAlbaranes']); - $routes->get('nuevalinea/(:any)', 'Albaran::addLinea/$1', ['as' => 'addAlbaranLinea']); - $routes->post('nuevalinea/(:any)', 'Albaran::addLinea/$1', ['as' => 'addIVA']); +$routes->group('albaranes', ['namespace' => 'App\Controllers\Albaranes'], function ($routes) { + $routes->post('generarAlbaran', 'Albaran::generateAlbaran', ['as' => 'generarAlbaran']); + $routes->get('albaranesEnvio', 'Albaran::getAlbaranes'); + $routes->get('datatablesAlbaranLinea', 'Albaran::datatablesLineasAlbaran'); + $routes->post('updateAlbaran', 'Albaran::updateAlbaran'); + $routes->post('borrarAlbaranLinea', 'Albaran::borrarLinea'); }); $routes->resource('albaranes', ['namespace' => 'App\Controllers\Pedidos', 'controller' => 'Albaran', 'except' => 'show,new,create,update']); @@ -788,7 +785,7 @@ $routes->group('logistica', ['namespace' => 'App\Controllers\Logistica'], functi $routes->get('addLineaEnvio', 'LogisticaController::addEnvioLinea'); $routes->post('updateCajaLinea', 'LogisticaController::setCajaLinea'); $routes->post('deleteLineasEnvio', 'LogisticaController::deleteLineas'); - $routes->post('updateUnidadesEnvio', 'LogisticaController::updateUnidadesEnvio'); + $routes->post('updateLineaEnvio', 'LogisticaController::updateLineaEnvio'); $routes->post('updateComentariosEnvio', 'LogisticaController::saveComments'); }); diff --git a/ci4/app/Controllers/Logistica/LogisticaController.php b/ci4/app/Controllers/Logistica/LogisticaController.php index 32e71498..cb03b5a4 100644 --- a/ci4/app/Controllers/Logistica/LogisticaController.php +++ b/ci4/app/Controllers/Logistica/LogisticaController.php @@ -164,7 +164,7 @@ class LogisticaController extends BaseController return redirect()->to(base_url('logistica/selectEnvios/simple'))->with('error', lang('Logistica.errors.noEnvio')); } $envioEntity->nextCaja = model('App\Models\Logistica\EnvioLineaModel')->getMaxCaja(); - + $viewData = [ 'currentModule' => static::$controllerSlug, 'boxTitle' => '' . ' ' . lang('Logistica.envio') . ' [' . $envioEntity->id . ']: ' . $envioEntity->direccion, @@ -190,14 +190,6 @@ class LogisticaController extends BaseController return ''; } ) - ->add("action", callback: function ($q) { - - return ' -
- -
- '; - }) ->edit( "pedido", function ($row, $meta) { @@ -209,7 +201,23 @@ class LogisticaController extends BaseController function ($row, $meta) { return '' . $row->presupuesto . ''; } - );; + ) + ->edit( + "cajas", + function ($row, $meta) { + return ''; + } + )->edit( + "unidadesEnvio", + function ($row, $meta) { + return ''; + } + ) + ->edit('cajasRaw', function ($row) { + return is_null($row->cajas) ? '__SIN__ASIGNAR__' : $row->cajas; + }); return $result->toJson(returnAsObject: true); } @@ -248,12 +256,13 @@ class LogisticaController extends BaseController } } - public function updateUnidadesEnvio() + public function updateLineaEnvio() { $id = $this->request->getPost('id'); - $unidades = $this->request->getPost('unidades_envio'); + $fieldName = $this->request->getPost('name'); + $fieldValue = $this->request->getPost('value'); - if (!$id || !$unidades || intval($unidades) <= 0) { + if (!$id || !$fieldName || ($fieldName=='unidades_envio' && !$fieldValue)) { return $this->response->setJSON([ 'status' => false, 'message' => 'Datos inválidos' @@ -262,7 +271,7 @@ class LogisticaController extends BaseController $model = model('App\Models\Logistica\EnvioLineaModel'); $updated = $model->update($id, [ - 'unidades_envio' => $unidades, + "" . $fieldName => $fieldValue==""? null: $fieldValue, ]); return $this->response->setJSON([ diff --git a/ci4/app/Controllers/Pdf/PrintAlbaranes.php b/ci4/app/Controllers/Pdf/PrintAlbaranes.php index 506d9114..f5d88265 100644 --- a/ci4/app/Controllers/Pdf/PrintAlbaranes.php +++ b/ci4/app/Controllers/Pdf/PrintAlbaranes.php @@ -11,8 +11,8 @@ class PrintAlbaranes extends BaseController public function index($albaran_id) { - $albaranModel = model('App\Models\Pedidos\AlbaranModel'); - $lineasAlbaranModel = model('App\Models\Pedidos\AlbaranLineaModel'); + $albaranModel = model('App\Models\Albaranes\AlbaranModel'); + $lineasAlbaranModel = model('App\Models\Albaranes\AlbaranLineaModel'); $data['albaran'] = $albaranModel->getResourceForPdf($albaran_id)->get()->getRow(); $data['albaranLineas'] = $lineasAlbaranModel->getResourceForPdf($albaran_id)->get()->getResultObject(); @@ -25,8 +25,8 @@ class PrintAlbaranes extends BaseController { // Cargar modelos - $albaranModel = model('App\Models\Pedidos\AlbaranModel'); - $lineasAlbaranModel = model('App\Models\Pedidos\AlbaranLineaModel'); + $albaranModel = model('App\Models\Albaranes\AlbaranModel'); + $lineasAlbaranModel = model('App\Models\Albaranes\AlbaranLineaModel'); // Informacion del presupuesto $data['albaran'] = $albaranModel->getResourceForPdf($albaran_id)->get()->getRow(); diff --git a/ci4/app/Controllers/Pedidos/Albaran.php b/ci4/app/Controllers/Pedidos/Albaran.php deleted file mode 100644 index f5af3504..00000000 --- a/ci4/app/Controllers/Pedidos/Albaran.php +++ /dev/null @@ -1,388 +0,0 @@ -request->isAJAX()) { - - $newTokenHash = csrf_hash(); - $csrfTokenName = csrf_token(); - - $model_linea = model('App\Models\Pedidos\AlbaranLineaModel'); - $model_linea->where('albaran_id', $id)->delete(); - - $this->model->where('id', $id)->delete(); - - $data = [ - 'error' => 0, - $csrfTokenName => $newTokenHash - ]; - return $this->respond($data); - } - else { - return $this->failUnauthorized('Invalid request', 403); - } - } - - public function addLinea($albaran_id){ - - if ($this->request->isAJAX()) { - - $model_linea = model('App\Models\Pedidos\AlbaranLineaModel'); - $newTokenHash = csrf_hash(); - $csrfTokenName = csrf_token(); - - // si es un post, es el iva - if($this->request->getPost()){ - $reqData = $this->request->getPost(); - $albaran_id = $reqData['albaran_id'] ?? 0; - - $albaran = $this->model->find($albaran_id); - if($albaran == false){ - $data = [ - 'error' => 'Albaran no encontrado', - $csrfTokenName => $newTokenHash - ]; - return $this->respond($data); - } - $presupuesto_model = model('App\Models\Presupuestos\PresupuestoModel'); - $presupuesto = $presupuesto_model->find($albaran->presupuesto_id); - if($presupuesto == false){ - $data = [ - 'error' => 'Presupuesto no encontrado', - $csrfTokenName => $newTokenHash - ]; - return $this->respond($data); - } - $iva_reducido = $presupuesto->iva_reducido; - $lineas = $model_linea->where('albaran_id', $albaran_id)->findAll(); - $total = 0; - foreach($lineas as $linea){ - $total += $linea->total; - } - $iva = $iva_reducido? $total * 4.0 / 100: $total * 21.0 / 100; - $data_linea= [ - 'albaran_id' => $albaran_id, - 'titulo' => $iva_reducido?lang('Pedidos.iva4'):lang('Pedidos.iva21'), - 'cantidad' => 1, - 'precio_unidad' => round($iva,2), - 'total' => round($iva,2), - 'user_created_id' => auth()->user()->id, - 'user_updated_id' => auth()->user()->id - ]; - $id_linea = $model_linea->insert($data_linea); - $linea = $model_linea->find($id_linea); - $data = [ - 'error' => 0, - 'data' => $linea, - $csrfTokenName => $newTokenHash - ]; - return $this->respond($data); - } - else{ - $linea = [ - 'albaran_id' => $albaran_id, - 'user_created_id' => auth()->user()->id, - 'user_updated_id' => auth()->user()->id - ]; - $id_linea = $model_linea->insert($linea); - $data = $model_linea->find($id_linea); - - $data = [ - 'error' => 0, - 'data' => $data, - $csrfTokenName => $newTokenHash - ]; - return $this->respond($data); - } - } - else { - return $this->failUnauthorized('Invalid request', 403); - } - } - - public function add() - { - if ($this->request->isAJAX()) { - - $user = auth()->user()->id; - - $newTokenHash = csrf_hash(); - $csrfTokenName = csrf_token(); - - $reqData = $this->request->getPost(); - $pedido_id = $reqData['pedido_id'] ?? 0; - $presupuestos_id = $reqData['presupuestos_id'] ?? 0; - - $return_data = $this->model->generarAlbaranes($pedido_id, $presupuestos_id, $user); - $data = [ - 'data' => $return_data, - $csrfTokenName => $newTokenHash - ]; - - return $this->respond($data); - - } - else { - return $this->failUnauthorized('Invalid request', 403); - } - } - - public function update($id = null){ - - if ($this->request->isAJAX()) { - $newTokenHash = csrf_hash(); - $csrfTokenName = csrf_token(); - - if ($id == null) : - $data = [ - 'error' => 2, - $csrfTokenName => $newTokenHash - ]; - return $this->respond($data); - endif; - $id = filter_var($id, FILTER_SANITIZE_URL); - $albaranEntity = $this->model->find($id); - - if ($albaranEntity == false) : - $message = lang('Basic.global.notFoundWithIdErr', [mb_strtolower(lang('Pedidos.albaran')), $id]); - $data = [ - 'error' => $message, - $csrfTokenName => $newTokenHash - ]; - return $this->respond($data); - endif; - - if ($this->request->getPost()) : - - $nullIfEmpty = true; // !(phpversion() >= '8.1'); - - $postData = $this->request->getPost(); - - $sanitizedData = $this->sanitized($postData, $nullIfEmpty); - - // JJO - $sanitizedData['user_updated_id'] = auth()->user()->id; - - $noException = true; - if ($successfulResult = $this->canValidate()) : // if ($successfulResult = $this->validate($this->formValidationRules) ) : - - if ($this->canValidate()) : - try { - $successfulResult = $this->model->skipValidation(true)->update($id, $sanitizedData); - } catch (\Exception $e) { - $noException = false; - $this->dealWithException($e); - } - else: - $this->viewData['warningMessage'] = lang('Basic.global.formErr1', [mb_strtolower(lang('Pedidos.albaran'))]); - $this->session->setFlashdata('formErrors', $this->model->errors()); - - endif; - - $albaranEntity->fill($sanitizedData); - - endif; - if ($noException && $successfulResult) : - $id = $albaranEntity->id ?? $id; - $message = lang('Basic.global.updateSuccess', [lang('Basic.global.record')]) . '.'; - - $data = [ - 'error' => 0, - $csrfTokenName => $newTokenHash - ]; - return $this->respond($data); - - endif; // $noException && $successfulResult - endif; // ($requestMethod === 'post') - - $data = [ - 'error' => 1, - $csrfTokenName => $newTokenHash - ]; - return $this->respond($data); - } - else { - return $this->failUnauthorized('Invalid request', 403); - } - } - - - public function updateLinea($id = null){ - - if ($this->request->isAJAX()) { - - $model_linea = model('App\Models\Pedidos\AlbaranLineaModel'); - $newTokenHash = csrf_hash(); - $csrfTokenName = csrf_token(); - - if ($id == null) : - $data = [ - 'error' => 2, - $csrfTokenName => $newTokenHash - ]; - return $this->respond($data); - endif; - $id = filter_var($id, FILTER_SANITIZE_URL); - $albaranEntity = $model_linea->find($id); - - if ($albaranEntity == false) : - $message = lang('Basic.global.notFoundWithIdErr', [mb_strtolower(lang('Pedidos.albaran')), $id]); - $data = [ - 'error' => $message, - $csrfTokenName => $newTokenHash - ]; - return $this->respond($data); - endif; - - if ($this->request->getPost()) : - - $nullIfEmpty = true; // !(phpversion() >= '8.1'); - - $postData = $this->request->getPost(); - - $sanitizedData = $this->sanitized($postData, $nullIfEmpty); - - // JJO - $sanitizedData['user_updated_id'] = auth()->user()->id; - - $noException = true; - if ($successfulResult = $this->canValidate()) : // if ($successfulResult = $this->validate($this->formValidationRules) ) : - - if ($this->canValidate()) : - try { - $successfulResult = $model_linea->skipValidation(true)->update($id, $sanitizedData); - } catch (\Exception $e) { - $noException = false; - $this->dealWithException($e); - } - else: - $this->viewData['warningMessage'] = lang('Basic.global.formErr1', [mb_strtolower(lang('Pedidos.albaran'))]); - $this->session->setFlashdata('formErrors', $model_linea->errors()); - - endif; - - $albaranEntity->fill($sanitizedData); - - endif; - if ($noException && $successfulResult) : - $id = $albaranEntity->id ?? $id; - $message = lang('Basic.global.updateSuccess', [lang('Basic.global.record')]) . '.'; - - $data = [ - 'error' => 0, - $csrfTokenName => $newTokenHash - ]; - return $this->respond($data); - - endif; // $noException && $successfulResult - endif; // ($requestMethod === 'post') - - $data = [ - 'error' => 1, - $csrfTokenName => $newTokenHash - ]; - return $this->respond($data); - } - else { - return $this->failUnauthorized('Invalid request', 403); - } - } - - public function borrarLinea(){ - if ($this->request->isAJAX()) { - - $model_linea = model('App\Models\Pedidos\AlbaranLineaModel'); - $newTokenHash = csrf_hash(); - $csrfTokenName = csrf_token(); - - $reqData = $this->request->getPost(); - $id = $reqData['id'] ?? 0; - $id = filter_var($id, FILTER_SANITIZE_URL); - $albaranLineaEntity = $model_linea->find($id); - - if ($albaranLineaEntity == false) : - $message = lang('Basic.global.notFoundWithIdErr', [mb_strtolower(lang('Pedidos.albaran')), $id]); - $message = lang('Basic.global.notFoundWithIdErr', [mb_strtolower(lang('Pedidos.albaran')), $id]); - $data = [ - 'error' => $message, - $csrfTokenName => $newTokenHash - ]; - return $this->respond($data); - endif; - - $successfulResult = $model_linea->skipValidation(true)->update($id, ['deleted_at' => date('Y-m-d H:i:s')]); - - if ($successfulResult) : - $data = [ - 'error' => 0, - $csrfTokenName => $newTokenHash - ]; - else: - $data = [ - 'error' => 1, - $csrfTokenName => $newTokenHash - ]; - endif; - return $this->respond($data); - } - else { - return $this->failUnauthorized('Invalid request', 403); - } - } - - public function getAlbaranes($pedido_id = null){ - if ($this->request->isAJAX()) { - - $model_linea = model('App\Models\Pedidos\AlbaranLineaModel'); - $newTokenHash = csrf_hash(); - $csrfTokenName = csrf_token(); - - $returnData = []; - $albaranes = $this->model->asArray()->where('pedido_id', $pedido_id)->findAll(); - - foreach($albaranes as $albaran){ - $albaran['fecha_albaran'] = $albaran['updated_at']; - array_push($returnData, - [ - 'albaran' => $albaran, - 'lineas' => $model_linea->asArray()->where('albaran_id', $albaran['id'])->findAll()] - ); - } - - $data = [ - 'data' => $returnData, - $csrfTokenName => $newTokenHash - ]; - return $this->respond($data); - } - else { - return $this->failUnauthorized('Invalid request', 403); - } - } -} - \ No newline at end of file diff --git a/ci4/app/Entities/Logistica/EnvioEntity.php b/ci4/app/Entities/Logistica/EnvioEntity.php index c35b2271..5ff38b95 100644 --- a/ci4/app/Entities/Logistica/EnvioEntity.php +++ b/ci4/app/Entities/Logistica/EnvioEntity.php @@ -17,6 +17,7 @@ class EnvioEntity extends Entity 'finalizado' => 'boolean', 'codigo_seguimiento'=> 'string', 'proveedor_id' => 'int', + 'cliente_id' => 'int', 'comentarios' => 'string', 'att' => 'string', 'direccion' => 'string', diff --git a/ci4/app/Entities/Pedidos/AlbaranEntity.php b/ci4/app/Entities/Pedidos/AlbaranEntity.php deleted file mode 100644 index bdff8334..00000000 --- a/ci4/app/Entities/Pedidos/AlbaranEntity.php +++ /dev/null @@ -1,46 +0,0 @@ - null, - 'pedido_id' => null, - 'presupuesto_id' => null, - 'presupuesto_direccion_id' => null, - 'cliente_id' => null, - 'serie_id' => null, - 'numero_albaran' => null, - 'mostrar_precios' => null, - 'total' => null, - 'direccion_albaran' => null, - 'att_albaran' => null, - 'user_created_id' => null, - 'user_updated_id' => null, - 'created_at' => null, - 'updated_at' => null, - 'deleted_at' => null, - ]; - - protected $dates = ['created_at', 'updated_at', 'deleted_at']; - - protected $casts = [ - 'id' => 'integer', - 'pedido_id' => '?integer', - 'presupuesto_id' => '?integer', - 'presupuesto_direccion_id' => '?integer', - 'cliente_id' => '?integer', - 'serie_id' => '?integer', - 'numero_albaran' => '?string', - 'mostrar_precios' => '?boolean', - 'total' => 'float', - 'direccion_albaran' => '?string', - 'att_albaran' => '?string', - 'user_created_id' => 'integer', - 'user_updated_id' => 'integer', - ]; - - // Agrega tus métodos personalizados aquí -} diff --git a/ci4/app/Entities/Pedidos/AlbaranLineaEntity.php b/ci4/app/Entities/Pedidos/AlbaranLineaEntity.php deleted file mode 100644 index c2137df8..00000000 --- a/ci4/app/Entities/Pedidos/AlbaranLineaEntity.php +++ /dev/null @@ -1,40 +0,0 @@ - null, - 'albaran_id' => null, - 'titulo' => null, - 'isbn' => null, - 'ref_cliente' => null, - 'cantidad' => null, - 'cajas' => null, - 'ejemplares_por_caja' => null, - 'precio_unidad' => null, - 'total' => null, - 'user_created_id' => null, - 'user_updated_id' => null, - 'created_at' => null, - 'updated_at' => null, - 'deleted_at' => null, - ]; - - protected $casts = [ - 'id' => 'integer', - 'albaran_id' => '?integer', - 'titulo' => 'string', - 'isbn' => '?string', - 'ref_cliente' => '?string', - 'cantidad' => '?integer', - 'cajas' => '?integer', - 'ejemplares_por_caja' => '?integer', - 'precio_unidad' => 'float', - 'total' => 'float', - 'user_created_id' => 'integer', - 'user_updated_id' => 'integer', - ]; -} \ No newline at end of file diff --git a/ci4/app/Language/es/Logistica.php b/ci4/app/Language/es/Logistica.php index a9cb7db5..059e1d34 100644 --- a/ci4/app/Language/es/Logistica.php +++ b/ci4/app/Language/es/Logistica.php @@ -51,6 +51,8 @@ return [ 'imprimirEtiquetas' => 'Imprimir etiquetas', 'buttonsActions' => 'Acciones sobre las filas seleccionadas', 'addCaja' => 'Añadir caja', + 'numCaja' => 'Número de caja', + 'selectAll' => 'Seleccionar todo', 'errors' => [ 'noEnvio' => 'No se ha encontrado el envio', diff --git a/ci4/app/Models/Logistica/EnvioLineaModel.php b/ci4/app/Models/Logistica/EnvioLineaModel.php index 6b31d8d4..e642a5fe 100644 --- a/ci4/app/Models/Logistica/EnvioLineaModel.php +++ b/ci4/app/Models/Logistica/EnvioLineaModel.php @@ -37,8 +37,9 @@ class EnvioLineaModel extends Model $builder = $this->db ->table($this->table . " t1") ->select( - "t1.id, t1.pedido_id as pedido, t3.id as presupuesto,t1.cajas, - t3.titulo as titulo, t1.unidades_envio as unidadesEnvio, t1.unidades_total as unidadesTotal, + "t1.id, t1.pedido_id as pedido, t3.id as presupuesto,t1.cajas, t1.cajas as cajasRaw, + t3.titulo as titulo, t1.unidades_envio as unidadesEnvio, t1.unidades_envio as unidadesEnvioRaw, + t1.unidades_total as unidadesTotal, IFNULL(( SELECT SUM(t_sub.unidades_envio) FROM " . $this->table . " t_sub diff --git a/ci4/app/Models/Logistica/EnvioModel.php b/ci4/app/Models/Logistica/EnvioModel.php index 445c4f77..2dbcf385 100644 --- a/ci4/app/Models/Logistica/EnvioModel.php +++ b/ci4/app/Models/Logistica/EnvioModel.php @@ -19,6 +19,7 @@ class EnvioModel extends Model 'codigo_seguimiento', 'proveedor_id', 'comentarios', + 'cliente_id', 'att', 'direccion', 'ciudad', diff --git a/ci4/app/Models/Pedidos/AlbaranLineaModel.php b/ci4/app/Models/Pedidos/AlbaranLineaModel.php deleted file mode 100644 index 9253b2a2..00000000 --- a/ci4/app/Models/Pedidos/AlbaranLineaModel.php +++ /dev/null @@ -1,66 +0,0 @@ -db - - ->table($this->table . " t1") - ->select( - "t1.id AS id, t1.albaran_id AS albaran_id, t1.titulo AS titulo, t1.isbn AS isbn, - t1.ref_cliente AS ref_cliente, t1.cantidad AS cantidad, t1.cajas AS cajas, - t1.ejemplares_por_caja AS ejemplares_por_caja, t1.precio_unidad AS precio_unidad, - t1.total AS total" - ); - - $builder->where("t1.deleted_at IS NULL"); - $builder->where("t1.albaran_id", $albaran_id); - - return $builder; - } - -} \ No newline at end of file diff --git a/ci4/app/Models/Pedidos/AlbaranModel.php b/ci4/app/Models/Pedidos/AlbaranModel.php deleted file mode 100644 index 5ac511e2..00000000 --- a/ci4/app/Models/Pedidos/AlbaranModel.php +++ /dev/null @@ -1,161 +0,0 @@ -find($presupuestos_id); - - $return_data = []; - - foreach ($presupuestos as $presupuesto) { - - $envios = $model_presupuesto_direcciones->where('presupuesto_id', $presupuesto->id)->findAll(); - - foreach($envios as $envio){ - - // se buscan los albaranes en este presupuesto con la misma direccion y con el mismo presupuesto_id en albaran - // en albaran linea para obtener la cantidad total enviada - $model_albaran = model('App\Models\Pedidos\AlbaranModel'); - $model_albaran_linea = model('App\Models\Pedidos\AlbaranLineaModel'); - $albaranes = $model_albaran->where('presupuesto_id', $presupuesto->id) - ->where('presupuesto_direccion_id', $envio->id)->findAll(); - // se suman las cantidades de los albaranes - $cantidad_enviada = 0; - foreach($albaranes as $albaran){ - $lineas = $model_albaran_linea->where('albaran_id', $albaran->id)->findAll(); - foreach($lineas as $linea){ - $cantidad_enviada += $linea->cantidad; - } - } - - if($cantidad_enviada >= intval($envio->cantidad)){ - continue; - } - // calculo precio_unidad - $precio_unidad = $presupuesto->total_aceptado/$presupuesto->tirada; - - $albaran_linea = []; - $albaran_linea = [ - 'titulo' => $presupuesto->titulo, - 'isbn' => $presupuesto->isbn, - 'ref_cliente' => $presupuesto->ref_cliente, - 'cantidad' => intval($envio->cantidad)-$cantidad_enviada, - 'cajas' => 1, - 'ejemplares_por_caja' => intval($envio->cantidad)-$cantidad_enviada, - 'precio_unidad' => $precio_unidad, - 'total' => $precio_unidad * $envio->cantidad, - 'user_created_id' => $user_id, - 'user_updated_id' => $user_id, - ]; - - - $serie = $model_series->find(11); - $numero_albaran = str_replace('{number}', $serie->next, $serie->formato); - $numero_albaran = str_replace( '{year}', date("Y"), $numero_albaran); - - $serie->next = $serie->next + 1; - $model_series->save($serie); - - $albaran = [ - 'pedido_id' => $pedido_id, - 'presupuesto_id' => $presupuesto->id, - 'presupuesto_direccion_id' => $envio->id, - 'cliente_id' => $presupuesto->cliente_id, - 'serie_id' => 11, // Serie de albaranes - 'numero_albaran' => $numero_albaran, - 'mostrar_precios' => 0, - 'total' => $albaran_linea['total'], - 'direccion_albaran' => $envio->direccion, - 'att_albaran' => $envio->att, - 'user_created_id' => $user_id, - 'user_updated_id' => $user_id, - 'fecha_albaran' => date('d/m/Y'), - ]; - - $id_albaran = $this->insert($albaran); - $model_albaran_linea = model('App\Models\Pedidos\AlbaranLineaModel'); - $albaran['id'] = $id_albaran; - $albaran_linea['albaran_id'] = $id_albaran; - $id_albaran_linea =$model_albaran_linea->insert($albaran_linea); - $albaran_linea['id'] = $id_albaran_linea; - - array_push($return_data, ["albaran"=>$albaran, "lineas" =>[$albaran_linea]]); - } - } - - return $return_data; - } - - /** - * Get resource data for creating PDFs. - * - * @param string $search - * - * @return \CodeIgniter\Database\BaseBuilder - */ - public function getResourceForPdf($albaran_id = -1) - { - $builder = $this->db - - ->table($this->table . " t1") - ->select( - "t1.id AS id, t1.pedido_id AS pedido_id, t1.presupuesto_id AS presupuesto_id, - t1.presupuesto_direccion_id AS presupuesto_direccion_id, t1.cliente_id AS cliente_id, - t1.serie_id AS serie_id, t1.numero_albaran AS numero_albaran, t1.mostrar_precios AS mostrar_precios, - t1.total AS total, t1.direccion_albaran AS direccion_albaran, t1.att_albaran AS att_albaran, - t1.user_created_id AS user_created_id, t1.user_updated_id AS user_updated_id, - t1.created_at AS created_at, t1.updated_at AS updated_at, - t2.nombre AS cliente" - ); - $builder->join("clientes t2", "t1.cliente_id = t2.id", "left"); - - $builder->where("t1.deleted_at IS NULL"); - $builder->where("t1.id", $albaran_id); - - return $builder; - } -} \ No newline at end of file diff --git a/ci4/app/Services/LogisticaService.php b/ci4/app/Services/LogisticaService.php index 73e6c27d..3cbab564 100644 --- a/ci4/app/Services/LogisticaService.php +++ b/ci4/app/Services/LogisticaService.php @@ -86,45 +86,51 @@ class LogisticaService public static function findLineaEnvioPorEnvio(int $envio_id) { $db = \Config\Database::connect(); + $subCliente = $db->table('envios') + ->select('cliente_id') + ->where('id', $envio_id) + ->getCompiledSelect(); $builder = $db->table('envios e_main'); $builder->select(" - CONCAT('[', p.id, '] - ', pr.titulo) AS name, - p.id AS id, - pl.id AS linea_id, - pl.cantidad, - ( - SELECT IFNULL(SUM(el.unidades_envio), 0) - FROM envios_lineas el - JOIN envios e ON e.id = el.envio_id - WHERE el.pedido_id = p.id - AND e.direccion = e_main.direccion - ) AS unidades_enviadas, - (pl.cantidad - ( + CONCAT('[', p.id, '] - ', pr.titulo) AS name, + pl.id AS id, + pl.cantidad, + ( + SELECT IFNULL(SUM(el.unidades_envio), 0) + FROM envios_lineas el + JOIN envios e ON e.id = el.envio_id + WHERE el.pedido_id = p.id + AND e.direccion = e_main.direccion + AND pr.cliente_id = ($subCliente) + ) AS unidades_enviadas, + ( + pl.cantidad - ( SELECT IFNULL(SUM(el2.unidades_envio), 0) FROM envios_lineas el2 JOIN envios e2 ON e2.id = el2.envio_id WHERE el2.pedido_id = p.id AND e2.direccion = e_main.direccion - )) AS unidades_pendientes - "); + AND pr.cliente_id = ($subCliente) + ) + ) AS unidades_pendientes + "); - $builder->join('envios_lineas el_main', 'el_main.envio_id = e_main.id'); - $builder->join('pedidos p', 'p.id = el_main.pedido_id'); - $builder->join('pedidos_linea pl', 'pl.pedido_id = p.id'); + $builder->join('pedidos_linea pl', '1=1'); // para incluir líneas sin envío aún + $builder->join('pedidos p', 'p.id = pl.pedido_id'); $builder->join('presupuestos pr', 'pr.id = pl.presupuesto_id'); - - $builder->where('p.estado', 'finalizado'); $builder->where('e_main.id', $envio_id); + $builder->where('p.estado', 'finalizado'); + $builder->where("pr.cliente_id = ($subCliente)", null, false); - $builder->groupBy('pl.id'); - $builder->having('unidades_enviadas < pl.cantidad', null, false); + $builder->having('unidades_pendientes >', 0); $builder->orderBy('name', 'ASC'); return $builder; } + public static function addLineaEnvio($envio_id = null, $pedido_id = null, $direccion = null) { $modelPedido = model('App\Models\Pedidos\PedidoModel'); @@ -183,10 +189,10 @@ class LogisticaService return [ 'status' => true, 'data' => [ - 'unidades_envio' => $result[0]->unidades_envio, - 'unidades_enviadas' => $result[0]->unidades_enviadas, - 'total_unidades' => $result[0]->total_unidades, - ] + 'unidades_envio' => $result[0]->unidades_envio, + 'unidades_enviadas' => $result[0]->unidades_enviadas, + 'total_unidades' => $result[0]->total_unidades, + ] ]; } @@ -211,7 +217,8 @@ class LogisticaService presupuesto_direcciones.email, presupuesto_direcciones.pais_id, presupuesto_direcciones.cantidad - IFNULL(SUM(envios_lineas.unidades_envio), 0) as cantidad, - presupuesto_direcciones.cantidad as cantidad_total + presupuesto_direcciones.cantidad as cantidad_total, + presupuestos.cliente_id as cliente_id ') ->join('pedidos_linea', 'pedidos_linea.presupuesto_id = presupuesto_direcciones.presupuesto_id') ->join('pedidos', 'pedidos.id = pedidos_linea.pedido_id') @@ -225,6 +232,7 @@ class LogisticaService // se genera un nuevo envio con estos datos $EnvioModel = model('App\Models\Logistica\EnvioModel'); + $EnvioModel->set('cliente_id', $datosEnvio->cliente_id); $EnvioModel->set('att', $datosEnvio->att); $EnvioModel->set('direccion', $datosEnvio->direccion); $EnvioModel->set('ciudad', $datosEnvio->ciudad); @@ -261,9 +269,9 @@ class LogisticaService return [ 'status' => true, 'data' => [ - 'id_envio' => $idEnvio, - 'multienvio' => false, - ], + 'id_envio' => $idEnvio, + 'multienvio' => false, + ], ]; } diff --git a/ci4/app/Views/themes/vuexy/form/logistica/viewEnvioEditForm.php b/ci4/app/Views/themes/vuexy/form/logistica/viewEnvioEditForm.php index 0684a072..a8f4d5ce 100644 --- a/ci4/app/Views/themes/vuexy/form/logistica/viewEnvioEditForm.php +++ b/ci4/app/Views/themes/vuexy/form/logistica/viewEnvioEditForm.php @@ -41,7 +41,8 @@ + class="form-control" + value="direccion) ?>">
@@ -95,7 +96,8 @@ + class="form-control" + value="comentarios) ?>">
@@ -119,13 +121,13 @@
-
+

-
+
@@ -159,6 +161,13 @@

+
+ +
@@ -197,12 +206,21 @@ + + + - - - + + + + + + + + + @@ -212,32 +230,54 @@ - +
-
+
+
+

+ +

+ +
+
+ + +
+ +
+
+
+ + +
endSection() ?> section('css') ?> - - - + + + endSection() ?> section('additionalExternalJs') ?> - - - + + + - - + + endSection() ?> \ No newline at end of file diff --git a/ci4/app/Views/themes/vuexy/form/pedidos/_albaranesItems.php b/ci4/app/Views/themes/vuexy/form/pedidos/_albaranesItems.php index 6a5ba19e..e69de29b 100644 --- a/ci4/app/Views/themes/vuexy/form/pedidos/_albaranesItems.php +++ b/ci4/app/Views/themes/vuexy/form/pedidos/_albaranesItems.php @@ -1,787 +0,0 @@ -
- -
-

- -

- -
-
- - -
-
- - -
- -
- - -
-
- -
-
-
-
- - -section('additionalInlineJs') ?> - - - -$('#generar_albaranes').on('click', function(){ - - var lineasPedido = $('#tableOfLineasPedido').DataTable(); - var presupuestos = lineasPedido.column(0).data().unique().toArray(); - - $.ajax({ - url: '', - type: 'POST', - data: { - pedido_id: id ?>, - presupuestos_id: presupuestos, - : v, - }, - success: function(response){ - - if(response.data.length > 0){ - Object.values(response.data).forEach(function(item){ - generarAlbaran(item); - }); - } - cambios_cantidad_albaranes(); - } - }); -}) - -const deleteLineaBtns = function(data) { - return ` - -
- -
- `; -}; - -function generarAlbaran(item){ - - // Crear los elementos necesarios - const accordion = $('
', { - class: 'accordion accordion-bordered mt-3 accordion-albaran', - id: 'accordioAlbaran' + item.albaran.id, - albaran: item.albaran.id - }); - - const card = $('
', { - class: 'card accordion-item active' - }); - - const header = $('

', { - class: 'accordion-header', - id: 'headingAlbaran' + item.albaran.id - }); - - const button = $('