From c651db61ffd1fa3bf02a81ed3fb1d50024da764b Mon Sep 17 00:00:00 2001 From: amazuecos Date: Wed, 25 Sep 2024 17:42:55 +0000 Subject: [PATCH] feat:chat modules --- ci4/app/Config/Routes.php | 4 + ci4/app/Controllers/Chat/ChatController.php | 30 + ci4/app/Controllers/Configuracion/Users.php | 36 +- ci4/app/Language/es/Users.php | 1 + ci4/app/Models/Chat/ChatDeparmentModel.php | 22 + .../Models/Chat/ChatDeparmentUserModel.php | 8 + ci4/app/Models/Chat/ChatModel.php | 71 +- ci4/app/Models/Clientes/ClienteModel.php | 35 + .../themes/vuexy/components/chat_factura.php | 27 +- .../themes/vuexy/components/chat_general.php | 18 +- .../themes/vuexy/components/chat_pedido.php | 22 +- .../vuexy/components/chat_presupuesto.php | 31 +- .../themes/vuexy/form/user/_userFormItems.php | 19 + .../Views/themes/vuexy/main/defaultlayout.php | 633 +++++++++--------- httpdocs/assets/js/safekat/components/chat.js | 230 ++++++- .../js/safekat/pages/chatNotification.js | 3 + 16 files changed, 808 insertions(+), 382 deletions(-) create mode 100644 httpdocs/assets/js/safekat/pages/chatNotification.js diff --git a/ci4/app/Config/Routes.php b/ci4/app/Config/Routes.php index 5bed71a5..352900d5 100644 --- a/ci4/app/Config/Routes.php +++ b/ci4/app/Config/Routes.php @@ -742,6 +742,8 @@ $routes->group('chat', ['namespace' => 'App\Controllers\Chat'], function ($route $routes->get('department/presupuesto/(:num)/(:num)', 'ChatController::get_chat_presupuesto/$1/$2', ['as' => 'getChatPresupuesto']); $routes->get('department/pedido/(:num)/(:num)', 'ChatController::get_chat_pedido/$1/$2', ['as' => 'getChatPedido']); $routes->get('department/factura/(:num)/(:num)', 'ChatController::get_chat_factura/$1/$2', ['as' => 'getChatFactura']); + $routes->get('department/(:num)/users', 'ChatController::get_chat_department_users/$1', ['as' => 'getChatDepartmentUsers']); + $routes->get('(:num)', 'ChatController::get_chat/$1', ['as' => 'getChat']); $routes->post('message/presupuesto', 'ChatController::store_chat_message_presupuesto', ['as' => 'storeChatMessagePresupuesto']); @@ -751,6 +753,8 @@ $routes->group('chat', ['namespace' => 'App\Controllers\Chat'], function ($route $routes->get('contacts', 'ChatController::get_chat_internal_contacts', ['as' => 'getChatInternalContacts']); $routes->get('contacts/(:num)', 'ChatController::get_chat_internal_contact/$1', ['as' => 'getChatInternalContact']); $routes->get('contact/(:num)/messages', 'ChatController::get_chat_internal_messages/$1', ['as' => 'getChatInternalMessages']); + $routes->get('notifications', 'ChatController::get_chat_cliente/$1', ['as' => 'getChatCliente']); + diff --git a/ci4/app/Controllers/Chat/ChatController.php b/ci4/app/Controllers/Chat/ChatController.php index 775ae028..9b9962fd 100644 --- a/ci4/app/Controllers/Chat/ChatController.php +++ b/ci4/app/Controllers/Chat/ChatController.php @@ -7,6 +7,7 @@ use App\Models\Chat\ChatDeparmentModel; use App\Models\Chat\ChatDeparmentUserModel; use App\Models\Chat\ChatMessageModel; use App\Models\Chat\ChatModel; +use App\Models\Clientes\ClienteModel; use App\Models\Usuarios\UserModel; use CodeIgniter\HTTP\ResponseInterface; use CodeIgniter\HTTP\RequestInterface; @@ -20,6 +21,8 @@ class ChatController extends BaseController protected ChatModel $chatModel; protected ChatMessageModel $chatMessageModel; protected UserModel $userModel; + protected ClienteModel $clienteModel; + @@ -36,6 +39,8 @@ class ChatController extends BaseController $this->chatModel = model(ChatModel::class); $this->chatMessageModel = model(ChatMessageModel::class); $this->userModel = model(UserModel::class); + $this->clienteModel = model(ClienteModel::class); + } public function index() {} public function get_chat_departments() @@ -161,6 +166,9 @@ class ChatController extends BaseController } public function get_chat_internal_contacts() { + if(auth()->user()->cliente_id){ + return $this->response->setJSON([]); + } $users = $this->userModel->builder() ->where("cliente_id", null) ->whereNotIn("id", [auth()->user()->id]) @@ -173,6 +181,9 @@ class ChatController extends BaseController } public function get_chat_internal_contact(int $user_id) { + if(auth()->user()->cliente_id){ + return $this->response->setJSON([]); + } $users = $this->userModel->builder() ->where("cliente_id", null) ->where("deleted_at", null) @@ -186,4 +197,23 @@ class ChatController extends BaseController $conversation = $this->chatMessageModel->get_chat_contact_messages($user_id); return $this->response->setJSON($conversation); } + public function get_chat_cliente() + { + $cliente_id = auth()->user()->cliente_id; + $response = []; + if($cliente_id){ + $data = $this->clienteModel->getClienteDataPresupuestoPedidoFactura($cliente_id); + $response["chatFacturas"] = $this->chatModel->getClienteChatFacturas($data["facturas"]); + $response["chatPresupuestos"] = $this->chatModel->getClienteChatPresupuestos($data["presupuestos"]); + $response["chatPedidos"] = $this->chatModel->getClienteChatPedidos($data["pedidos"]); + $response["data"] = $data; + + } + return $this->response->setJSON($response); + } + public function get_chat_department_users(int $chat_department_id) + { + $data = $this->chatDeparmentModel->getChatDepartmentUsers($chat_department_id); + return $this->response->setJSON($data); + } } diff --git a/ci4/app/Controllers/Configuracion/Users.php b/ci4/app/Controllers/Configuracion/Users.php index a48c1b28..dcb8a140 100755 --- a/ci4/app/Controllers/Configuracion/Users.php +++ b/ci4/app/Controllers/Configuracion/Users.php @@ -1,8 +1,8 @@ group_model = new GroupModel(); $this->group_user_model = new GroupsUsersModel(); $this->user_model = new UserModel(); + $this->chat_department_model = model(ChatDeparmentModel::class); + $this->chat_department_user_model = model(ChatDeparmentUserModel::class); + $this->viewData['pageTitle'] = lang('Users.moduleTitle'); @@ -76,7 +82,10 @@ class Users extends \App\Controllers\GoBaseController } // Obtener los grupos a los que pertenece $currentGroups = $postData['group'] ?? []; + $chatDepartments = $postData['chatDepartments'] ?? []; unset($postData['group']); + unset($postData['chatDepartments']); + // Generar el nombre de usuario $postData['username'] = strstr($postData['email'], '@', true); $sanitizedData = $this->sanitized($postData, true); @@ -128,6 +137,14 @@ class Users extends \App\Controllers\GoBaseController ]; $this->group_user_model->insert($group_user_data); } + $this->chat_department_user_model->where("user_id",$id)->delete(); + foreach($chatDepartments as $chatDepartment) + { + $this->chat_department_user_model->insert([ + "user_id" => $id, + "chat_department_id" => $this->chat_department_model->where("name",$chatDepartment)->first()["id"] + ]); + } $message = lang('Basic.global.saveSuccess', [mb_strtolower(lang('Users.user'))]) . '.'; $message = ucfirst(str_replace("'", "\'", $message)); @@ -150,6 +167,7 @@ class Users extends \App\Controllers\GoBaseController $this->viewData['clienteList'] = $this->getClienteListItems(); $this->viewData['formAction'] = route_to('createUser'); $this->viewData['groups'] = $this->group_model->select('keyword, title')->findAll(); + $this->viewData['chatDepartments'] = $this->chat_department_model->findAll(); $this->viewData['boxTitle'] = lang('Basic.global.addNew') . ' ' . lang('Users.user') . ' ' . lang('Basic.global.addNewSuffix'); @@ -176,7 +194,10 @@ class Users extends \App\Controllers\GoBaseController $postData = $this->request->getPost(); $currentGroups = $postData['group'] ?? []; + $chatDepartments = $postData['chatDepartments'] ?? []; + unset($postData['group']); + unset($postData['chatDepartments']); // Obtener contraseña nueva si se ha introducido en texto plano // Obtener contraseña nueva si se ha introducido en texto plano @@ -233,7 +254,14 @@ class Users extends \App\Controllers\GoBaseController ]; $this->group_user_model->insert($group_user_data); } - + $this->chat_department_user_model->where("user_id",$id)->delete(); + foreach($chatDepartments as $chatDepartment) + { + $this->chat_department_user_model->insert([ + "user_id" => $id, + "chat_department_id" => $this->chat_department_model->where("name",$chatDepartment)->first()["id"] + ]); + } $id = $user->id ?? $id; $message = lang('Basic.global.updateSuccess', [mb_strtolower(lang('Users.user'))]) . '.'; $message = ucfirst(str_replace("'", "\'", $message)); @@ -256,6 +284,8 @@ class Users extends \App\Controllers\GoBaseController $this->viewData['formAction'] = route_to('updateUser', $id); $this->viewData['selectedGroups'] = $this->group_model->getUsersRoles($requestedId); $this->viewData['groups'] = $this->group_model->select('keyword, title')->findAll(); + $this->viewData['chatDepartments'] = $this->chat_department_model->select(["display","name","id as chatDepartmentId"])->findAll(); + $this->viewData['chatDepartmentUser'] = $this->chat_department_user_model->getChatDepartmentUser($user->id); $this->viewData['boxTitle'] = lang('Basic.global.edit2') . ' ' . lang('Users.user') . ' ' . lang('Basic.global.edit3'); return $this->displayForm(__METHOD__, $id); diff --git a/ci4/app/Language/es/Users.php b/ci4/app/Language/es/Users.php index 98e0c86f..ff61a685 100755 --- a/ci4/app/Language/es/Users.php +++ b/ci4/app/Language/es/Users.php @@ -18,6 +18,7 @@ return [ 'emailConfirmed' => 'Email Confirmado', 'firstName' => 'Nombre', 'group' => 'Rol', + 'chatDepartments' => 'Chat departamento', 'idUser' => 'ID Usuario', 'language' => 'Idioma', 'lastAccess' => 'Último acceso', diff --git a/ci4/app/Models/Chat/ChatDeparmentModel.php b/ci4/app/Models/Chat/ChatDeparmentModel.php index 4558bbd3..ba4e026c 100644 --- a/ci4/app/Models/Chat/ChatDeparmentModel.php +++ b/ci4/app/Models/Chat/ChatDeparmentModel.php @@ -98,4 +98,26 @@ class ChatDeparmentModel extends Model } return $departments; } + public function getChatDepartmentUsers(int $chat_deparment_id) + { + $result = $this->db->table('chat_departments') + ->select( + [ + "users.*" + ] + ) + ->join( + "chat_department_users", + "chat_department_users.chat_department_id = chat_departments.id", + 'left' + ) + ->join( + "users", + "chat_department_users.user_id = users.id", + 'left' + )->where("chat_departments.id",$chat_deparment_id) + ->get()->getResultObject(); + return $result; + + } } diff --git a/ci4/app/Models/Chat/ChatDeparmentUserModel.php b/ci4/app/Models/Chat/ChatDeparmentUserModel.php index 36fe4836..0c701d14 100644 --- a/ci4/app/Models/Chat/ChatDeparmentUserModel.php +++ b/ci4/app/Models/Chat/ChatDeparmentUserModel.php @@ -47,4 +47,12 @@ class ChatDeparmentUserModel extends Model protected $afterFind = []; protected $beforeDelete = []; protected $afterDelete = []; + public function getChatDepartmentUser(int $user_id) + { + return $this->db->table($this->table." t1") + ->select("chat_departments.*") + ->join("chat_departments","t1.chat_department_id = chat_departments.id","left") + ->where("t1.user_id",$user_id)->get()->getResultObject(); + + } } diff --git a/ci4/app/Models/Chat/ChatModel.php b/ci4/app/Models/Chat/ChatModel.php index 65cf010e..ee6788bd 100644 --- a/ci4/app/Models/Chat/ChatModel.php +++ b/ci4/app/Models/Chat/ChatModel.php @@ -2,7 +2,7 @@ namespace App\Models\Chat; - +use App\Models\Usuarios\UserModel; use CodeIgniter\Model; use stdClass; @@ -192,5 +192,74 @@ class ChatModel extends Model ->get()->getResultObject(); return $query; } + public function getClienteChatPedidos(array $pedidos) : array + { + $results = $this->db->table("chats") + ->select([ + "chats.id as chatId", + "chats.pedido_id as pedidoId", + "chats.chat_department_id as chatDepartmentId", + "chat_departments.display as chatDisplay", + "pedidos.id as title" + ]) + ->join("chat_departments","chat_departments.id = chats.chat_department_id","left") + ->join("pedidos","pedidos.id = chats.pedido_id","left") + ->join("chat_messages","pedidos.id = chats.pedido_id","left") + ->whereNotIn("chat_messages.sender_id",[auth()->user()->id]) + ->whereIn("pedidos.id",$pedidos) + ->get()->getResultObject(); + $chatMessageModel = model(ChatMessageModel::class); + + foreach ($results as $row) { + $row->messages = $chatMessageModel->get_chat_messages($row->chatId); + } + return $results; + } + public function getClienteChatFacturas(array $facturas) : array + { + $results = $this->db->table("chats") + ->select([ + "chats.id as chatId", + "chats.factura_id as facturaId", + "chats.chat_department_id as chatDepartmentId", + "chat_departments.display as chatDisplay", + "facturas.numero as title" + ]) + ->join("chat_departments","chat_departments.id = chats.chat_department_id","left") + ->join("facturas","facturas.id = chats.factura_id","left") + ->join("chat_messages","chats.id = chat_messages.chat_id","left") + ->whereNotIn("chat_messages.sender_id",[auth()->user()->id]) + ->whereIn("facturas.id",$facturas) + ->get()->getResultObject(); + $chatMessageModel = model(ChatMessageModel::class); + + foreach ($results as $row) { + $row->messages = $chatMessageModel->get_chat_messages($row->chatId); + } + return $results; + } + public function getClienteChatPresupuestos(array $presupuestos) : array + { + $results = $this->db->table("chats") + ->select([ + "chats.id as chatId", + "chats.presupuesto_id as presupuestoId", + "chats.chat_department_id as chatDepartmentId", + "chat_departments.display as chatDisplay", + "presupuestos.titulo as title" + ]) + ->join("chat_departments","chat_departments.id = chats.chat_department_id","left") + ->join("presupuestos","presupuestos.id = chats.presupuesto_id","left") + ->join("chat_messages","chats.id = chat_messages.chat_id","left") + ->whereNotIn("chat_messages.sender_id",[auth()->user()->id]) + ->whereIn("presupuestos.id",$presupuestos) + ->get()->getResultObject(); + $chatMessageModel = model(ChatMessageModel::class); + + foreach ($results as $row) { + $row->messages = $chatMessageModel->get_chat_messages($row->chatId); + } + return $results; + } } diff --git a/ci4/app/Models/Clientes/ClienteModel.php b/ci4/app/Models/Clientes/ClienteModel.php index 85df9782..8563eef1 100755 --- a/ci4/app/Models/Clientes/ClienteModel.php +++ b/ci4/app/Models/Clientes/ClienteModel.php @@ -332,4 +332,39 @@ class ClienteModel extends \App\Models\BaseModel return $builder->get()->getResultArray(); } + public function getClienteDataPresupuestoPedidoFactura(int $cliente_id) : array + { + $query = $this->db + ->table($this->table." t1") + ->select([ + "t1.id as clienteId", + "presupuestos.id as presupuestoId", + "pedidos.id as pedidoId", + "presupuesto_estados.estado as presupuestoEstado", + "facturas_pedidos_lineas.factura_id as facturaId", + + ]) + ->join("presupuestos","t1.id = presupuestos.cliente_id","left") + ->join("presupuesto_estados","presupuestos.estado_id = presupuesto_estados.id","left") + ->join("pedidos_linea","presupuestos.id = pedidos_linea.presupuesto_id","left") + ->join("pedidos","pedidos.id = pedidos_linea.pedido_id","left") + ->join("facturas_pedidos_lineas","facturas_pedidos_lineas.pedido_linea_id = pedidos_linea.id","left") + ->where("t1.id",$cliente_id); + $data = $query->get()->getResultObject(); + $facturas = []; + $presupuestos = []; + $pedidos = []; + $result = []; + foreach ($data as $row) { + $facturas[] = $row->facturaId; + $presupuestos[] = $row->presupuestoId; + $pedidos[] = $row->pedidoId; + } + $result["facturas"] = array_unique(array_filter($facturas)); + $result["presupuestos"] = array_unique(array_filter($presupuestos)); + $result["pedidos"] = array_unique(array_filter($pedidos)); + + return $result; + } + } diff --git a/ci4/app/Views/themes/vuexy/components/chat_factura.php b/ci4/app/Views/themes/vuexy/components/chat_factura.php index b06bcdaf..ac2cd84e 100644 --- a/ci4/app/Views/themes/vuexy/components/chat_factura.php +++ b/ci4/app/Views/themes/vuexy/components/chat_factura.php @@ -64,7 +64,7 @@
-
+
P
-
Departamento Producción
- Consulta sobre el presupuesto - P001 +
+
+
diff --git a/ci4/app/Views/themes/vuexy/components/chat_general.php b/ci4/app/Views/themes/vuexy/components/chat_general.php index b28a8b3e..af8ff7ef 100644 --- a/ci4/app/Views/themes/vuexy/components/chat_general.php +++ b/ci4/app/Views/themes/vuexy/components/chat_general.php @@ -46,15 +46,15 @@
No Contacts Found
+ +
+ JJ +
+
+
Jaime Jimenez
+
+
+ -->
diff --git a/ci4/app/Views/themes/vuexy/components/chat_pedido.php b/ci4/app/Views/themes/vuexy/components/chat_pedido.php index 6375ea59..09115a2c 100644 --- a/ci4/app/Views/themes/vuexy/components/chat_pedido.php +++ b/ci4/app/Views/themes/vuexy/components/chat_pedido.php @@ -64,7 +64,7 @@
-
+
+
diff --git a/ci4/app/Views/themes/vuexy/components/chat_presupuesto.php b/ci4/app/Views/themes/vuexy/components/chat_presupuesto.php index 851c9a12..c7247afa 100644 --- a/ci4/app/Views/themes/vuexy/components/chat_presupuesto.php +++ b/ci4/app/Views/themes/vuexy/components/chat_presupuesto.php @@ -1,13 +1,13 @@
-
+

-

-
@@ -77,26 +77,23 @@ class="avatar-initial rounded-circle bg-label-primary">P
-
Departamento Producción
- Consulta sobre el presupuesto - P001 +
+
+
diff --git a/ci4/app/Views/themes/vuexy/form/user/_userFormItems.php b/ci4/app/Views/themes/vuexy/form/user/_userFormItems.php index d394dc4e..3e75ae63 100644 --- a/ci4/app/Views/themes/vuexy/form/user/_userFormItems.php +++ b/ci4/app/Views/themes/vuexy/form/user/_userFormItems.php @@ -52,6 +52,25 @@
+
+
+ + +
+
+ ${contact.unreadMessages ? `${contact.unreadMessages}` : ""} ` @@ -339,6 +427,100 @@ class Chat { +} + + +export const showNotificationMessages = (dom) => { + let ajax = new Ajax( + '/chat/notifications', + null, + null, + (data) => { + let totalMessages = 0 + data?.chatPresupuestos?.map((e) => { + console.log(e) + let numberOfMessages = 0 + e.messages.forEach(m => { + + m.viewed == "1" ? numberOfMessages++ : null + }); + totalMessages+= numberOfMessages + if(numberOfMessages > 0){ + dom.append( + ` +
  • + +
    + ${e.presupuestoId} +
    +
    +
    [${e.title}] ${e.chatDisplay}
    +
    + ${numberOfMessages} +
    +
  • + ` + ) + } + }) + data?.chatFacturas?.map((e) => { + console.log(e) + let numberOfMessages = 0 + e.messages.forEach(m => { + + m.viewed == "1" ? numberOfMessages++ : null + }); + totalMessages+= numberOfMessages + if(numberOfMessages > 0){ + dom.append( + ` +
  • + +
    + ${e.facturaId} +
    +
    +
    [${e.title}] ${e.chatDisplay}
    +
    + ${numberOfMessages} +
    +
  • + ` + ) + } + }) + data?.chatPedidos?.map((e) => { + console.log(e) + let numberOfMessages = 0 + e.messages.forEach(m => { + + m.viewed == "1" ? numberOfMessages++ : null + }); + $("#chat-notification-number").text(numberOfMessages) + totalMessages+= numberOfMessages + if(numberOfMessages > 0){ + dom.append( + ` +
  • + +
    + ${e.pedidoId} +
    +
    +
    [${e.title}] ${e.chatDisplay}
    +
    + ${numberOfMessages} +
    +
  • + ` + ) + } + }) + $("#chat-notification-number").text(totalMessages) + }, + (err) => { } + ) + ajax.get() } export default Chat diff --git a/httpdocs/assets/js/safekat/pages/chatNotification.js b/httpdocs/assets/js/safekat/pages/chatNotification.js new file mode 100644 index 00000000..cc3a169b --- /dev/null +++ b/httpdocs/assets/js/safekat/pages/chatNotification.js @@ -0,0 +1,3 @@ +import {showNotificationMessages} from "../components/chat.js"; + +showNotificationMessages($("#chat-notification-list")) \ No newline at end of file