diff --git a/ci4/app/Config/Routes.php b/ci4/app/Config/Routes.php index 4888631d..62f6a3e7 100755 --- a/ci4/app/Config/Routes.php +++ b/ci4/app/Config/Routes.php @@ -628,6 +628,9 @@ $routes->group('pedidos', ['namespace' => 'App\Controllers\Pedidos'], function ( $routes->post('add', 'Pedido::add', ['as' => 'crearPedido']); $routes->get('edit/(:any)', 'Pedido::edit/$1', ['as' => 'editarPedido']); $routes->post('getlineas', 'Pedido::getLineas', ['as' => 'tablaLineasPedido']); + $routes->post('cambiarestado', 'Pedido::cambiarEstado', ['as' => 'cambiarEstadoPedido']); + $routes->post('update/(:any)', 'Pedido::update/$1', ['as' => 'actualizarPedido']); + }); $routes->resource('pedidos', ['namespace' => 'App\Controllers\Pedidos', 'controller' => 'Pedido', 'except' => 'show,new,create,update']); @@ -635,6 +638,12 @@ $routes->resource('pedidos', ['namespace' => 'App\Controllers\Pedidos', 'control $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->resource('albaranes', ['namespace' => 'App\Controllers\Pedidos', 'controller' => 'Albaran', 'except' => 'show,new,create,update']); diff --git a/ci4/app/Controllers/Pedidos/Albaran.php b/ci4/app/Controllers/Pedidos/Albaran.php index af89d98f..f5af3504 100644 --- a/ci4/app/Controllers/Pedidos/Albaran.php +++ b/ci4/app/Controllers/Pedidos/Albaran.php @@ -1,6 +1,7 @@ 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() { @@ -64,14 +159,22 @@ class Albaran extends \App\Controllers\BaseResourceController $csrfTokenName = csrf_token(); if ($id == null) : - return $this->redirect2listView(); + $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]); - return $this->redirect2listView('sweet-error', $message); + $data = [ + 'error' => $message, + $csrfTokenName => $newTokenHash + ]; + return $this->respond($data); endif; if ($this->request->getPost()) : @@ -128,5 +231,158 @@ class Albaran extends \App\Controllers\BaseResourceController } } + + 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/Controllers/Pedidos/Pedido.php b/ci4/app/Controllers/Pedidos/Pedido.php index 1fa366fd..d17b0902 100755 --- a/ci4/app/Controllers/Pedidos/Pedido.php +++ b/ci4/app/Controllers/Pedidos/Pedido.php @@ -80,6 +80,99 @@ class Pedido extends \App\Controllers\BaseResourceController } + public function cambiarEstado(){ + if($this->request->isAJAX()){ + + $id = $this->request->getPost('id'); + $estado = $this->request->getPost('estado'); + + $this->model->where('id', $id)->set(['estado' => $estado])->update(); + return $this->respond(['status' => 'success', 'message' => lang('Basic.global.success')]); + }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); + $pedidoEntity = $this->model->find($id); + + if ($pedidoEntity == false) : + $message = lang('Basic.global.notFoundWithIdErr', [mb_strtolower(lang('Pedidos.pedido')), $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; + + $pedidoEntity->fill($sanitizedData); + + endif; + if ($noException && $successfulResult) : + $id = $pedidoEntity->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 edit($id=null){ if ($id == null) : diff --git a/ci4/app/Entities/Pedidos/AlbaranLineaEntity.php b/ci4/app/Entities/Pedidos/AlbaranLineaEntity.php index de5bda52..c2137df8 100644 --- a/ci4/app/Entities/Pedidos/AlbaranLineaEntity.php +++ b/ci4/app/Entities/Pedidos/AlbaranLineaEntity.php @@ -16,6 +16,11 @@ class AlbaranLineaEntity extends \CodeIgniter\Entity\Entity '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 = [ @@ -29,5 +34,7 @@ class AlbaranLineaEntity extends \CodeIgniter\Entity\Entity '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/en/Pedidos.php b/ci4/app/Language/en/Pedidos.php index 4ca46f10..0e58640a 100644 --- a/ci4/app/Language/en/Pedidos.php +++ b/ci4/app/Language/en/Pedidos.php @@ -45,6 +45,9 @@ return [ 'pedidos' => 'Orders', 'pedidosList' => 'Orders List', + 'cancelar' => 'Cancel', + 'finalizar' => 'Finish', + 'unaCara' => '1 side', 'dosCaras' => '2 sides', @@ -68,6 +71,8 @@ return [ 'nuevaLinea' => 'New line', 'addIva' => "Add VAT", 'mostrarPrecios' => 'Show prices', + 'iva4' => "4,00 % VAT", + 'iva21' => "21,00 % VAT", 'facturas' => 'Invoices', diff --git a/ci4/app/Language/es/Pedidos.php b/ci4/app/Language/es/Pedidos.php index 906321ee..5e790760 100644 --- a/ci4/app/Language/es/Pedidos.php +++ b/ci4/app/Language/es/Pedidos.php @@ -44,6 +44,9 @@ return [ 'pedidos' => 'Pedidos', 'pedidosList' => 'Lista de Pedidos', + 'cancelar' => 'Cancelar', + 'finalizar' => 'Finalizar', + 'unaCara' => '1 cara', 'dosCaras' => '2 caras', @@ -67,6 +70,9 @@ return [ 'nuevaLinea' => 'Nueva línea', 'addIva' => "Añadir IVA", 'mostrarPrecios' => 'Mostrar precios', + 'iva4' => "4,00 % IVA", + 'iva21' => "21,00 % IVA", + 'facturas' => 'Facturas', diff --git a/ci4/app/Models/Pedidos/AlbaranLineaModel.php b/ci4/app/Models/Pedidos/AlbaranLineaModel.php index a384fb22..62f6ccd4 100644 --- a/ci4/app/Models/Pedidos/AlbaranLineaModel.php +++ b/ci4/app/Models/Pedidos/AlbaranLineaModel.php @@ -25,6 +25,17 @@ class AlbaranLineaModel extends \App\Models\BaseModel 'ejemplares_por_caja', 'precio_unidad', 'total', + 'user_created_id', + 'user_updated_id', + 'created_at', + 'updated_at', + 'deleted_at', ]; + protected $useSoftDeletes = true; + protected $useTimestamps = true; + protected $createdField = 'created_at'; + protected $updatedField = 'updated_at'; + protected $deletedField = 'deleted_at'; + } \ No newline at end of file diff --git a/ci4/app/Models/Pedidos/AlbaranModel.php b/ci4/app/Models/Pedidos/AlbaranModel.php index 63415da1..3834ca30 100644 --- a/ci4/app/Models/Pedidos/AlbaranModel.php +++ b/ci4/app/Models/Pedidos/AlbaranModel.php @@ -2,6 +2,8 @@ namespace App\Models\Pedidos; + + class AlbaranModel extends \App\Models\BaseModel { protected $table = "albaranes"; @@ -33,9 +35,11 @@ class AlbaranModel extends \App\Models\BaseModel 'deleted_at', ]; + protected $useSoftDeletes = true; protected $useTimestamps = true; protected $createdField = 'created_at'; protected $updatedField = 'updated_at'; + protected $deletedField = 'deleted_at'; public function generarAlbaranes($pedido_id, $presupuestos_id, $user_id){ @@ -64,7 +68,9 @@ class AlbaranModel extends \App\Models\BaseModel 'cajas' => 1, 'ejemplares_por_caja' => $envio->cantidad, 'precio_unidad' => $precio_unidad, - 'total' => $precio_unidad * $envio->cantidad + 'total' => $precio_unidad * $envio->cantidad, + 'user_created_id' => $user_id, + 'user_updated_id' => $user_id, ]; @@ -98,7 +104,7 @@ class AlbaranModel extends \App\Models\BaseModel $id_albaran_linea =$model_albaran_linea->insert($albaran_linea); $albaran_linea['id'] = $id_albaran_linea; - array_push($return_data, ["albaran"=>$albaran, "albaran_linea" =>$albaran_linea]); + array_push($return_data, ["albaran"=>$albaran, "lineas" =>[$albaran_linea]]); } } diff --git a/ci4/app/Views/themes/vuexy/form/pedidos/_albaranesItems.php b/ci4/app/Views/themes/vuexy/form/pedidos/_albaranesItems.php index caf1a195..55eacaac 100644 --- a/ci4/app/Views/themes/vuexy/form/pedidos/_albaranesItems.php +++ b/ci4/app/Views/themes/vuexy/form/pedidos/_albaranesItems.php @@ -59,7 +59,7 @@ const deleteLineaBtns = function(data) { return `