diff --git a/ci4/app/Config/Routes.php b/ci4/app/Config/Routes.php index b289ef74..96e82b01 100644 --- a/ci4/app/Config/Routes.php +++ b/ci4/app/Config/Routes.php @@ -133,6 +133,9 @@ $routes->group('configuracion', ['namespace' => 'App\Controllers\Configuracion'] $routes->get('edit/(:num)', 'MaquinaTarea::viewForm/$1', ['as' => 'maquinaTareaViewForm']); $routes->get('datatable', 'MaquinaTarea::datatable', ['as' => 'maquinaTareaDatatable']); }); + $routes->group("messages", ["namespace" => 'App\Controllers\Chat'], function ($routes) { + $routes->get('', 'ChatController::config_view', ['as' => 'configMessagesIndex']); + }); }); @@ -892,17 +895,25 @@ $routes->group('chat', ['namespace' => 'App\Controllers\Chat'], function ($route $routes->post('direct/messages/unread/(:num)', 'ChatController::update_chat_direct_message_unread/$1', ['as' => 'updateChatDirectMessageUnread']); + $routes->post('department', 'ChatController::store_chat_department', ['as' => 'storeChatDepartment']); + $routes->delete('department/(:num)', 'ChatController::delete_chat_department/$1', ['as' => 'deleteChatDepartment']); + $routes->post('department/update/(:num)', 'ChatController::update_chat_department/$1', ['as' => 'updateChatDepartment']); $routes->get('departments', 'ChatController::get_chat_departments', ['as' => 'getChatDepartments']); $routes->get('departments/select', 'ChatController::get_chat_department_select', ['as' => 'getChatDepartmentSelect']); $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/users/presupuesto/(:num)/(:num)', 'ChatController::get_chat_department_presupuesto_users/$1/$2', ['as' => 'getPresupuestoChatDepartmentUsers']); + $routes->get('department/datatable', 'ChatController::chat_department_datatable', ['as' => 'chatDepartmentDatatable']); + $routes->get('department/edit/(:num)', 'ChatController::chat_department_edit/$1', ['as' => 'chatDepartmentEditView']); + $routes->get('department/users/datatable/(:num)', 'ChatController::chat_department_user_datatable/$1', ['as' => 'chatDepartmentUsersDatatable']); $routes->get('department/users/pedido/(:num)/(:num)', 'ChatController::get_chat_department_pedido_users/$1/$2', ['as' => 'getPedidoChatDepartmentUsers']); $routes->get('department/users/factura/(:num)/(:num)', 'ChatController::get_chat_department_factura_users/$1/$2', ['as' => 'getFacturaChatDepartmentUsers']); - + $routes->get('department/users/select/add/(:num)', 'ChatController::select_users_not_in_chat_department/$1', ['as' => 'selectUsersNotInChatDepartment']); $routes->post('department/user', 'ChatController::subscribe_to_chat_deparment/$1', ['as' => 'subscribeToChatDepartment']); + $routes->post('department/subscribe/admin/user', 'ChatController::subscribe_admin_to_department', ['as' => 'subscribeAdminToChatDepartment']); $routes->delete('department/user/(:num)', 'ChatController::delete_user_from_department/$1', ['as' => 'deleteUserFromDepartment']); + $routes->delete('department/admin/user/(:num)/(:num)', 'ChatController::delete_user_admin_from_department/$1/$2', ['as' => 'deleteUserAdminFromDepartment']); $routes->get('(:num)', 'ChatController::get_chat/$1', ['as' => 'getChat']); diff --git a/ci4/app/Config/Validation.php b/ci4/app/Config/Validation.php index 1c8614bf..982ce5c9 100755 --- a/ci4/app/Config/Validation.php +++ b/ci4/app/Config/Validation.php @@ -162,4 +162,15 @@ class Validation extends BaseConfig ], ]; + public array $chat_department = + [ + "display" => [ + "rules" => "required|string", + "label" => "tarifa acabado", + ], + "description" => [ + "rules" => "permit_empty", + "label" => "maquina", + ], + ]; } diff --git a/ci4/app/Controllers/Chat/ChatController.php b/ci4/app/Controllers/Chat/ChatController.php index 098d6734..840785ce 100644 --- a/ci4/app/Controllers/Chat/ChatController.php +++ b/ci4/app/Controllers/Chat/ChatController.php @@ -21,6 +21,7 @@ use CodeIgniter\Log\Logger; use Psr\Log\LoggerInterface; use Hermawan\DataTables\DataTable; use CodeIgniter\I18n\Time; +use CodeIgniter\Validation\Validation; class ChatController extends BaseController { @@ -32,6 +33,8 @@ class ChatController extends BaseController protected ClienteModel $clienteModel; protected ChatUser $chatUserModel; protected ChatNotification $chatNotificationModel; + protected Validation $validation; + protected array $viewData; protected static $viewPath = 'themes/vuexy/form/mensajes/'; @@ -52,6 +55,7 @@ class ChatController extends BaseController $this->clienteModel = model(ClienteModel::class); $this->chatUserModel = model(ChatUser::class); $this->chatNotificationModel = model(ChatNotification::class); + $this->validation = service("validation"); } public function index() {} public function get_chat_departments() @@ -77,6 +81,7 @@ class ChatController extends BaseController if ($chat) { $data["messages"] = $this->chatMessageModel->get_chat_messages($chat->id); $this->chatMessageModel->set_chat_department_messages_as_read($chat->id); + $this->chatModel->setAsViewedChatUserNotifications($chat->id, auth()->user()->id); $data["count"] = count($data["messages"]); } $data["chat"] = $chat; @@ -94,6 +99,8 @@ class ChatController extends BaseController if ($chat) { $data["messages"] = $this->chatMessageModel->get_chat_messages($chat->id); $this->chatMessageModel->set_chat_department_messages_as_read($chat->id); + $this->chatModel->setAsViewedChatUserNotifications($chat->id, auth()->user()->id); + $data["count"] = count($data["messages"]); } $data["chat"] = $chat; @@ -111,6 +118,7 @@ class ChatController extends BaseController if ($chat) { $data["messages"] = $this->chatMessageModel->get_chat_messages($chat->id); $this->chatMessageModel->set_chat_department_messages_as_read($chat->id); + $this->chatModel->setAsViewedChatUserNotifications($chat->id, auth()->user()->id); $data["count"] = count($data["messages"]); } $data["chat"] = $chat; @@ -207,19 +215,18 @@ class ChatController extends BaseController $chat_message_id = $this->chatMessageModel->insert(["chat_id" => $chatId, "sender_id" => auth()->user()->id, "message" => $data["message"]]); $dataResponse = $this->chatMessageModel->find($chat_message_id); $chatDepartmentUsers = $this->chatDeparmentModel->getChatDepartmentUsers($data["chat_department_id"]); - $chatExternalUsers = $this->chatDeparmentModel->getChatDeparmentPresupuestoUsers($data["chat_department_id"],$data["model_id"]); - $this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $data['client']]); - $cliente_in_department_count = $this->chatDeparmentUserModel->where('chat_department_id',$data["chat_department_id"])->where('user_id',$data['client'])->where('presupuesto_id',$data['model_id'])->countAllResults(); - $auth_user_in_department_count = $this->chatDeparmentUserModel->where('chat_department_id',$data["chat_department_id"])->where('user_id',auth()->user()->id)->countAllResults(); - if($cliente_in_department_count == 0){ - $this->chatDeparmentUserModel->insert(['chat_department_id' => $data["chat_department_id"],'user_id' => $data['client'],'presupuesto_id' => $data['model_id']]); - } - if($auth_user_in_department_count){ - $users_always_in_department = array_map(fn($q) => $q->id,$chatDepartmentUsers); - if(!in_array(auth()->user()->id,$users_always_in_department)){ - $this->chatDeparmentUserModel->insert(['chat_department_id' => $data["chat_department_id"],'user_id' => auth()->user()->id,'presupuesto_id' => $data['model_id']]); + if (auth()->user()->inGroup('admin')) { + $cliente_in_department_count = $this->chatDeparmentUserModel->where('chat_department_id', $data["chat_department_id"])->where('user_id', $data['client'])->where('presupuesto_id', $data['model_id'])->countAllResults(); + if ($cliente_in_department_count == 0) { + $this->chatDeparmentUserModel->insert(['chat_department_id' => $data["chat_department_id"], 'user_id' => $data['client'], 'presupuesto_id' => $data['model_id']]); } } + $chatExternalUsers = $this->chatDeparmentModel->getChatDeparmentPresupuestoUsers($data["chat_department_id"], $data["model_id"]); + $auth_user_in_department_count = $this->chatDeparmentUserModel->where('chat_department_id', $data["chat_department_id"])->where('user_id', auth()->user()->id)->countAllResults(); + + if ($auth_user_in_department_count) { + $this->chatDeparmentUserModel->insert(['chat_department_id' => $data["chat_department_id"], 'user_id' => auth()->user()->id, 'presupuesto_id' => $data['model_id']]); + } foreach ($chatDepartmentUsers as $user) { if ($user->id != auth()->user()->id) { @@ -227,9 +234,7 @@ class ChatController extends BaseController } } foreach ($chatExternalUsers as $user) { - if ($user->id != auth()->user()->id) { - $this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $user->id]); - } + $this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $user->id]); } return $this->response->setJSON($dataResponse); } @@ -247,20 +252,17 @@ class ChatController extends BaseController $chat_message_id = $this->chatMessageModel->insert(["chat_id" => $chatId, "sender_id" => auth()->user()->id, "message" => $data["message"]]); $dataResponse = $this->chatMessageModel->find($chat_message_id); $chatDepartmentUsers = $this->chatDeparmentModel->getChatDepartmentUsers($data["chat_department_id"]); - $chatExternalUsers = $this->chatDeparmentModel->getChatDeparmentPedidoUsers($data["chat_department_id"],$data["model_id"]); - $this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $data['client']]); - $cliente_in_department_count = $this->chatDeparmentUserModel->where('chat_department_id',$data["chat_department_id"])->where('user_id',$data['client'])->where('pedido_id',$data['model_id'])->countAllResults(); - $auth_user_in_department_count = $this->chatDeparmentUserModel->where('chat_department_id',$data["chat_department_id"])->where('user_id',auth()->user()->id)->countAllResults(); - if($cliente_in_department_count == 0){ - $this->chatDeparmentUserModel->insert(['chat_department_id' => $data["chat_department_id"],'user_id' => $data['client'],'pedido_id' => $data['model_id']]); - } - if($auth_user_in_department_count){ - $users_always_in_department = array_map(fn($q) => $q->id,$chatDepartmentUsers); - if(!in_array(auth()->user()->id,$users_always_in_department)){ - $this->chatDeparmentUserModel->insert(['chat_department_id' => $data["chat_department_id"],'user_id' => auth()->user()->id,'pedido_id' => $data['model_id']]); - + if (auth()->user()->inGroup('admin')) { + $cliente_in_department_count = $this->chatDeparmentUserModel->where('chat_department_id', $data["chat_department_id"])->where('user_id', $data['client'])->where('pedido_id', $data['model_id'])->countAllResults(); + if ($cliente_in_department_count == 0) { + $this->chatDeparmentUserModel->insert(['chat_department_id' => $data["chat_department_id"], 'user_id' => $data['client'], 'pedido_id' => $data['model_id']]); } } + $chatExternalUsers = $this->chatDeparmentModel->getChatDeparmentPedidoUsers($data["chat_department_id"], $data["model_id"]); + $auth_user_in_department_count = $this->chatDeparmentUserModel->where('chat_department_id', $data["chat_department_id"])->where('user_id', auth()->user()->id)->countAllResults(); + if ($auth_user_in_department_count == 0) { + $this->chatDeparmentUserModel->insert(['chat_department_id' => $data["chat_department_id"], 'user_id' => auth()->user()->id, 'pedido_id' => $data['model_id']]); + } foreach ($chatDepartmentUsers as $user) { if ($user->id != auth()->user()->id) { @@ -268,40 +270,34 @@ class ChatController extends BaseController } } foreach ($chatExternalUsers as $user) { - if ($user->id != auth()->user()->id) { - $this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $user->id]); - } + $this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $user->id]); } return $this->response->setJSON($dataResponse); } public function store_chat_message_factura() { - $data = $this->request->getPost(); - $existChat = $this->chatModel->existChatFactura($data["chat_department_id"], $data["model_id"]); + $existChat = $this->chatModel->existChatPedido($data["chat_department_id"], $data["model_id"]); if ($existChat == false) { - $chatId = $this->chatModel->createChatFactura($data["chat_department_id"], $data["model_id"]); + $chatId = $this->chatModel->createChatPedido($data["chat_department_id"], $data["model_id"]); } else { - $chat = $this->chatModel->getChatFactura($data["chat_department_id"], $data["model_id"]); + $chat = $this->chatModel->getChatPedido($data["chat_department_id"], $data["model_id"]); $chatId = $chat->id; } $chat_message_id = $this->chatMessageModel->insert(["chat_id" => $chatId, "sender_id" => auth()->user()->id, "message" => $data["message"]]); $dataResponse = $this->chatMessageModel->find($chat_message_id); $chatDepartmentUsers = $this->chatDeparmentModel->getChatDepartmentUsers($data["chat_department_id"]); - $chatExternalUsers = $this->chatDeparmentModel->getChatDeparmentFacturaUsers($data["chat_department_id"],$data["model_id"]); - $this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $data['client']]); - $cliente_in_department_count = $this->chatDeparmentUserModel->where('chat_department_id',$data["chat_department_id"])->where('user_id',$data['client'])->where('factura_id',$data['model_id'])->countAllResults(); - $auth_user_in_department_count = $this->chatDeparmentUserModel->where('chat_department_id',$data["chat_department_id"])->where('user_id',auth()->user()->id)->countAllResults(); - if($cliente_in_department_count == 0){ - $this->chatDeparmentUserModel->insert(['chat_department_id' => $data["chat_department_id"],'user_id' => $data['client'],'factura_id' => $data['model_id']]); - } - if($auth_user_in_department_count){ - $users_always_in_department = array_map(fn($q) => $q->id,$chatDepartmentUsers); - if(!in_array(auth()->user()->id,$users_always_in_department)){ - $this->chatDeparmentUserModel->insert(['chat_department_id' => $data["chat_department_id"],'user_id' => auth()->user()->id,'factura_id' => $data['model_id']]); - + if (auth()->user()->inGroup('admin')) { + $cliente_in_department_count = $this->chatDeparmentUserModel->where('chat_department_id', $data["chat_department_id"])->where('user_id', $data['client'])->where('pedido_id', $data['model_id'])->countAllResults(); + if ($cliente_in_department_count == 0) { + $this->chatDeparmentUserModel->insert(['chat_department_id' => $data["chat_department_id"], 'user_id' => $data['client'], 'factura_id' => $data['model_id']]); } } + $chatExternalUsers = $this->chatDeparmentModel->getChatDeparmentPedidoUsers($data["chat_department_id"], $data["model_id"]); + $auth_user_in_department_count = $this->chatDeparmentUserModel->where('chat_department_id', $data["chat_department_id"])->where('user_id', auth()->user()->id)->countAllResults(); + if ($auth_user_in_department_count == 0) { + $this->chatDeparmentUserModel->insert(['chat_department_id' => $data["chat_department_id"], 'user_id' => auth()->user()->id, 'pedido_id' => $data['model_id']]); + } foreach ($chatDepartmentUsers as $user) { if ($user->id != auth()->user()->id) { @@ -309,9 +305,7 @@ class ChatController extends BaseController } } foreach ($chatExternalUsers as $user) { - if ($user->id != auth()->user()->id) { - $this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $user->id]); - } + $this->chatNotificationModel->insert(["chat_message_id" => $chat_message_id, "user_id" => $user->id]); } return $this->response->setJSON($dataResponse); } @@ -377,7 +371,7 @@ class ChatController extends BaseController $cliente_id = auth()->user()->cliente_id; $response = []; if ($cliente_id) { - $data = $this->clienteModel->getClienteDataPresupuestoPedidoFactura($cliente_id); + // $data = $this->clienteModel->getClienteDataPresupuestoPedidoFactura($cliente_id); $response["internals"] = $this->chatModel->getChatDepartmentNotifications(); $response["totalMessages"] = 0; $mensajes_directos = $this->chatModel->getChatDirectMessageNotifications(); @@ -389,10 +383,10 @@ class ChatController extends BaseController // foreach ($response["chatPresupuestos"] as $key => $value) { // $response["totalMessages"] += $value->unreadMessages; // } - // foreach ($mensajes_directos as $value) { - // $response["internals"][] = $value; - // $response["totalMessages"] += $value->unreadMessages; - // } + foreach ($mensajes_directos as $value) { + $response["internals"][] = $value; + $response["totalMessages"] += $value->unreadMessages; + } // $response["chatPedidos"] = $this->chatModel->getClienteChatPedidos($data["pedidos"]); // foreach ($response["chatPedidos"] as $key => $value) { // $response["totalMessages"] += $value->unreadMessages; @@ -420,23 +414,23 @@ class ChatController extends BaseController return $this->response->setJSON($response); } - public function get_chat_department_presupuesto_users(int $chat_department_id,int $presupuesto_id) + public function get_chat_department_presupuesto_users(int $chat_department_id, int $presupuesto_id) { $adminUsers = $this->chatDeparmentModel->getChatDepartmentUsers($chat_department_id); - $externalUsers = $this->chatDeparmentModel->getChatDeparmentPresupuestoUsers($chat_department_id,$presupuesto_id); - return $this->response->setJSON(['admin_users' => $adminUsers,'external_users' => $externalUsers]); + $externalUsers = $this->chatDeparmentModel->getChatDeparmentPresupuestoUsers($chat_department_id, $presupuesto_id); + return $this->response->setJSON(['admin_users' => $adminUsers, 'external_users' => $externalUsers]); } - public function get_chat_department_pedido_users(int $chat_department_id,$pedido_id) + public function get_chat_department_pedido_users(int $chat_department_id, $pedido_id) { $adminUsers = $this->chatDeparmentModel->getChatDepartmentUsers($chat_department_id); - $externalUsers = $this->chatDeparmentModel->getChatDeparmentPedidoUsers($chat_department_id,$pedido_id); - return $this->response->setJSON(['admin_users' => $adminUsers,'external_users' => $externalUsers]); + $externalUsers = $this->chatDeparmentModel->getChatDeparmentPedidoUsers($chat_department_id, $pedido_id); + return $this->response->setJSON(['admin_users' => $adminUsers, 'external_users' => $externalUsers]); } - public function get_chat_department_factura_users(int $chat_department_id,$factura_id) + public function get_chat_department_factura_users(int $chat_department_id, $factura_id) { $adminUsers = $this->chatDeparmentModel->getChatDepartmentUsers($chat_department_id); - $externalUsers = $this->chatDeparmentModel->getChatDeparmentFacturaUsers($chat_department_id,$factura_id); - return $this->response->setJSON(['admin_users' => $adminUsers,'external_users' => $externalUsers]); + $externalUsers = $this->chatDeparmentModel->getChatDeparmentFacturaUsers($chat_department_id, $factura_id); + return $this->response->setJSON(['admin_users' => $adminUsers, 'external_users' => $externalUsers]); } public function get_chat_users_internal() { @@ -766,8 +760,18 @@ class ChatController extends BaseController } public function get_notifications_not_viewed_from_chat(int $chat_id) { - $data = $this->chatModel->getUsersNotificationNotViewedFromChat($chat_id); - return $this->response->setJSON(["data" => $data, "chat_id" => $chat_id]); + $unviewedNotifications = $this->chatModel->getUsersNotificationNotViewedFromChat($chat_id); + $viewedNotifications = $this->chatModel->getUsersNotificationViewedFromChat($chat_id); + + return $this->response->setJSON( + [ + "data" => [ + "unviewedNotifications" => $unviewedNotifications, + "viewedNotifications" => $viewedNotifications + ], + "chat_id" => $chat_id + ] + ); } public function store_new_direct_message() { @@ -878,46 +882,142 @@ class ChatController extends BaseController } public function delete_user_from_department($chat_department_id) { - $data = $this->request->getPost(); + $data = $this->request->getRawInput(); $user_id = auth()->user()->id; - $adminExist = $this->chatDeparmentUserModel->where('chat_department_id',$chat_department_id) - ->where('user_id',$user_id) - ->where('pedido_id',null) - ->where('factura_id',null) - ->where('presupuesto_id',null)->countAllResults(); - if($adminExist) - { - return $this->response->setJSON(["message" => lang('Chat.exit_admin_chat_wrong'),"status" => false]); + $adminExist = $this->chatDeparmentUserModel->where('chat_department_id', $chat_department_id) + ->where('user_id', $user_id) + ->where('pedido_id', null) + ->where('factura_id', null) + ->where('presupuesto_id', null)->countAllResults(); + if ($adminExist) { + return $this->response->setJSON(["message" => lang('Chat.exit_admin_chat_wrong'), "status" => false]); } - $chatDepartmentUserEntity = $this->chatDeparmentUserModel->where('chat_department_id',$chat_department_id)->where('user_id',$user_id)->where($data['model_fk'],$data['model_id_fk']); - if($chatDepartmentUserEntity->countAllResults() > 0) - { - $deleted = $this->chatDeparmentUserModel->where('chat_department_id',$chat_department_id)->where('user_id',$user_id)->delete(); + $chatDepartmentUserEntity = $this->chatDeparmentUserModel->where('chat_department_id', $chat_department_id)->where('user_id', $user_id)->where($data['model_fk'], $data['model_id_fk']); + if ($chatDepartmentUserEntity->countAllResults() > 0) { + $deleted = $this->chatDeparmentUserModel->where('chat_department_id', $chat_department_id)->where('user_id', $user_id)->delete(purge: true); return $this->response->setJSON(["message" => lang('Chat.exit_chat_ok'), "status" => true]); - }else{ - return $this->response->setJSON(["message" => lang('Chat.exit_chat_wrong'),"status" => false]); + } else { + return $this->response->setJSON(["message" => lang('Chat.exit_chat_wrong'), "status" => false]); } } + public function delete_user_admin_from_department($chat_department_id, $user_id) + { + $this->chatDeparmentUserModel->where('chat_department_id', $chat_department_id)->where('user_id', $user_id)->delete(); + return $this->response->setJSON(["message" => lang('Chat.user_deleted_ok'), "status" => true]); + } public function subscribe_to_chat_deparment() { $data = $this->request->getPost(); $user_id = auth()->user()->id; - $adminExist = $this->chatDeparmentUserModel->where('chat_department_id',$data['chat_department_id']) - ->where('user_id',$user_id) - ->where('pedido_id',null) - ->where('factura_id',null) - ->where('presupuesto_id',null)->countAllResults(); - if($adminExist) - { - return $this->response->setJSON(["message" => lang('Chat.subscribe_chat_wrong'),"status" => false]); + $adminExist = $this->chatDeparmentUserModel->where('chat_department_id', $data['chat_department_id']) + ->where('user_id', $user_id) + ->where('pedido_id', null) + ->where('factura_id', null) + ->where('presupuesto_id', null)->countAllResults(); + if ($adminExist) { + return $this->response->setJSON(["message" => lang('Chat.subscribe_chat_wrong'), "status" => false]); } - $chatDepartmentUserEntity = $this->chatDeparmentUserModel->where('chat_department_id',$data['chat_department_id'])->where('user_id',$user_id)->where($data['model_fk'],$data['model_id_fk']); - if($chatDepartmentUserEntity->countAllResults() > 0) - { - return $this->response->setJSON(["message" => lang('Chat.subscribe_chat_wrong'),"status" => false]); - }else{ - $this->chatDeparmentUserModel->insert(["chat_department_id" => $data["chat_department_id"],"user_id" => $user_id,$data['model_fk'] => $data['model_id_fk']]); + $chatDepartmentUserEntity = $this->chatDeparmentUserModel->where('chat_department_id', $data['chat_department_id'])->where('user_id', $user_id)->where($data['model_fk'], $data['model_id_fk']); + if ($chatDepartmentUserEntity->countAllResults() > 0) { + return $this->response->setJSON(["message" => lang('Chat.subscribe_chat_wrong'), "status" => false]); + } else { + $this->chatDeparmentUserModel->insert(["chat_department_id" => $data["chat_department_id"], "user_id" => $user_id, $data['model_fk'] => $data['model_id_fk']]); return $this->response->setJSON(["message" => lang('Chat.subscribe_chat_ok'), "status" => true]); } } + public function subscribe_admin_to_department() + { + $data = $this->request->getPost(); + if ($data['user_id']) { + + $this->chatDeparmentUserModel + ->insert( + [ + "chat_department_id" => $data["chat_department_id"], + "user_id" => $data["user_id"], + ] + ); + return $this->response->setJSON(["message" => lang('Chat.subscribe_admin_chat_ok'), "status" => true]); + } else { + return $this->response->setStatusCode(422)->setJSON(["message" => lang('Chat.subscribe_admin_chat_wrong'), "status" => false]); + } + } + public function config_view() + { + $this->viewData['breadcrumb'] = [ + ['title' => lang("App.menu_configuration"), 'route' => 'javascript:void(0);', 'active' => false], + ['title' => lang("App.menu_config_messages"), 'route' => route_to("configMessagesIndex"), 'active' => true] + ]; + + return view('themes/vuexy/form/configuracion/messages/configView', $this->viewData); + } + public function chat_department_edit($chat_department_id) + { + $chatDepartment = $this->chatDeparmentModel->find($chat_department_id); + $this->viewData['breadcrumb'] = [ + ['title' => lang("App.menu_configuration"), 'route' => 'javascript:void(0);', 'active' => false], + ['title' => lang("App.menu_config_messages"), 'route' => route_to("configMessagesIndex"), 'active' => false], + ['title' => $chatDepartment->display, 'route' => route_to("chatDepartmentEditView", $chat_department_id), 'active' => true] + + ]; + $this->viewData["chat_department"] = $chatDepartment; + return view('themes/vuexy/form/configuracion/messages/editChatDepartmentForm', $this->viewData); + } + public function chat_department_datatable() + { + $q = $this->chatDeparmentModel->datatableQuery(); + return DataTable::of($q) + ->add('action', fn($r) => $r->id) + ->toJson(true); + } + public function chat_department_user_datatable(int $chat_department_id) + { + $q = $this->chatDeparmentUserModel->datatableQuery($chat_department_id); + $datatable = DataTable::of($q); + if (auth()->user()->inGroup('admin')) { + $datatable->add('action', fn($r) => $r->userId); + } + return $datatable->toJson(true); + } + public function select_users_not_in_chat_department(int $chat_department_id) + { + $paramQuery = $this->request->getGet("q"); + $data = $this->chatDeparmentUserModel->querySelectUsersNotInDepartment($chat_department_id, $paramQuery); + return $this->response->setJSON($data); + } + public function store_chat_department() + { + $data = $this->request->getPost(); + $validated = $this->validation->run($data, 'chat_department'); + if ($validated) { + $dataValidated = $this->validation->getValidated(); + $dataValidated['name'] = newUUID(); + $this->chatDeparmentModel->insert($dataValidated); + return $this->response->setJSON(["message" => lang('App.global_alert_save_success'), "status" => true, "data" => $dataValidated]); + } else { + return $this->response->setStatusCode(422)->setJSON(["message" => lang('App.global_alert_save_success'), "status" => false, "errors" => $this->validation->getErrors()]); + } + return $this->response->setJSON(["message" => lang('App.global_alert_save_success'), "status" => true]); + } + public function update_chat_department(int $chat_department_id) + { + $data = $this->request->getPost(); + $validated = $this->validation->run($data, 'chat_department'); + if ($validated) { + $dataValidated = $this->validation->getValidated(); + $this->chatDeparmentModel->update($chat_department_id, $dataValidated); + return $this->response->setJSON(["message" => lang('App.global_alert_save_success'), "status" => true, "data" => $dataValidated]); + } else { + return $this->response->setStatusCode(422)->setJSON(["message" => lang('App.global_alert_save_success'), "status" => false, "errors" => $this->validation->getErrors()]); + } + } + public function delete_chat_department($chat_department_id) + { + if (auth()->user()->inGroup('admin')) { + $this->chatDeparmentModel->delete($chat_department_id); + return $this->response->setJSON(["message" => lang('App.user_alert_delete'), "status" => true]); + } else { + return $this->response->setStatusCode(403)->setJSON(["message" => lang('App.user_alert_forbidden'), "status" => false]); + } + } } diff --git a/ci4/app/Language/en/Chat.php b/ci4/app/Language/en/Chat.php index bb9c813c..42eb23b1 100644 --- a/ci4/app/Language/en/Chat.php +++ b/ci4/app/Language/en/Chat.php @@ -24,6 +24,14 @@ return [ "viewed" => "Viewed", ], + "datatable_departments" => [ + "name" => "Name", + "description" => "Description", + ], + "datatable_department_users" => [ + "name" => "Name", + "user" => "User", + ], "new_message_ok" => "Message sent successfully", "participants" => "Participants", "choose_department" => "Choose a deparment", @@ -42,4 +50,12 @@ return [ "exit_chat_wrong" => "You can't exit the chat because you don't belong to it.", "subscribe_chat_ok" => "You've subscribed to chat successfully. Now on you'll receive notifications.", "subscribe_chat_wrong" => "You already belong to this chat.", -]; \ No newline at end of file + "message_configuration" => "Messages configuration", + "new_chat_department" => "New chat department", + "user_deleted_ok" => "User deleted succesfully", + "subscribe_admin_chat_ok" => "User added successfully", + "subscribe_admin_chat_wrong" => "You must select a user", + + + +]; diff --git a/ci4/app/Language/es/App.php b/ci4/app/Language/es/App.php index 3bb95036..095247ad 100755 --- a/ci4/app/Language/es/App.php +++ b/ci4/app/Language/es/App.php @@ -226,6 +226,8 @@ return [ "user_alert_add" => "¡Agregado exitosamente!", "user_alert_edit" => "¡Editado con éxito!", "user_alert_delete" => "¡Eliminado con éxito!", + "user_alert_forbidden" => "No tienes permiso para realizar esta acción", + // USER - form "user_add_title" => "Nuevo Usuario", @@ -686,6 +688,7 @@ return [ "menu_configuration" => "Configuración", "menu_variables" => "Variables sistema", + "menu_config_messages" => "Mensajería", "menu_error_presupuesto" => "Errores presupuesto", "menu_calendario" => "Calendario", "menu_paises" => "Paises", diff --git a/ci4/app/Language/es/Chat.php b/ci4/app/Language/es/Chat.php index 122ab47a..300a3974 100644 --- a/ci4/app/Language/es/Chat.php +++ b/ci4/app/Language/es/Chat.php @@ -24,6 +24,14 @@ return [ "viewed" => "Leído", ], + "datatable_departments" => [ + "name" => "Nombre", + "description" => "Descripción", + ], + "datatable_department_users" => [ + "name" => "Nombre", + "user" => "Usuario", + ], "new_message_ok" => "Mensaje enviado correctamente", "participants" => "Participantes", "choose_department" => "Elige un departamento", @@ -43,4 +51,14 @@ return [ "exit_admin_chat_wrong" => "Perteneces como personal a este chat y no puedes salirte. Contacta administrador para ello.", "subscribe_chat_ok" => "Te has subscrito al chat correctamente. Ahora recibirás notificaciones cuando lleguen mensajes.", "subscribe_chat_wrong" => "Ya perteneces a este chat.", + "message_configuration" => "Configuración mensajería", + "new_chat_department" => "Nuevo departamento", + "user_deleted_ok" => "Usuario eliminado con éxito", + "department_users" => "Usuarios departamento", + "subscribe_admin_chat_ok" => "Usuario añadido correctamente.", + "subscribe_admin_chat_wrong" => "Tienes que seleccionar un usuario.", + "help_select_chat_department_user" => "Solamente son listados los usuarios que pertenecen al personal. Los clientes no son listados, para añadirlos a la conversación se realiza desde la sección de mensajería de las diferentes secciones(presupuesto,pedido,factura ...)", + "store_department" => "Crear departamento", + + ]; \ No newline at end of file diff --git a/ci4/app/Models/Chat/ChatDeparmentModel.php b/ci4/app/Models/Chat/ChatDeparmentModel.php index ecaab80c..ec3a857a 100644 --- a/ci4/app/Models/Chat/ChatDeparmentModel.php +++ b/ci4/app/Models/Chat/ChatDeparmentModel.php @@ -108,7 +108,7 @@ class ChatDeparmentModel extends Model public function getChatDeparmentUserQuery(int $chat_deparment_id) { - $query = $this->builder() + $query = $this ->select( [ "users.*" @@ -123,20 +123,21 @@ class ChatDeparmentModel extends Model "users", "chat_department_users.user_id = users.id", 'left' - )->where("chat_departments.id", $chat_deparment_id) - ->where("chat_department_users.deleted_at", null) - - ->where("users.deleted_at", null); + ) + ->where("chat_department_users.chat_department_id", $chat_deparment_id) + ->where("chat_department_users.deleted_at",null) + ->where("users.deleted_at",null); return $query; } public function getChatDepartmentUsers(int $chat_deparment_id) { $result = $this->getChatDeparmentUserQuery($chat_deparment_id) - ->where('chat_department_users.presupuesto_id', null) - ->where('chat_department_users.pedido_id', null) - ->where('chat_department_users.factura_id', null) - ->get()->getResultObject(); - return $result; + ->where('chat_department_users.presupuesto_id',null) + ->where('chat_department_users.pedido_id',null) + ->where('chat_department_users.factura_id',null) + ->get(); + + return $result->getResultObject() ?: []; } public function getChatDeparmentPresupuestoUsers(int $chat_deparment_id, int $presupuesto_id) { @@ -163,7 +164,7 @@ class ChatDeparmentModel extends Model { return $this->find($chat_deparment_id)->display; } - public function getChatDepartmentSelect(string $query = null) + public function getChatDepartmentSelect(?string $query) { $q = $this->builder()->select([ "id", @@ -176,4 +177,8 @@ class ChatDeparmentModel extends Model } return $q; } + public function datatableQuery() + { + return $this->select(['id','display','description'])->where('deleted_at',null); + } } diff --git a/ci4/app/Models/Chat/ChatDeparmentUserModel.php b/ci4/app/Models/Chat/ChatDeparmentUserModel.php index 4b550648..7726e5d9 100644 --- a/ci4/app/Models/Chat/ChatDeparmentUserModel.php +++ b/ci4/app/Models/Chat/ChatDeparmentUserModel.php @@ -2,7 +2,7 @@ namespace App\Models\Chat; - +use App\Models\Usuarios\UserModel; use CodeIgniter\Model; class ChatDeparmentUserModel extends Model @@ -53,11 +53,55 @@ class ChatDeparmentUserModel extends Model 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) - ->where("t1.deleted_at",null) - ->get()->getResultObject(); + 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) + ->where("t1.deleted_at", null) + ->get()->getResultObject(); + } + public function datatableQuery(int $chat_department_id) + { + return $this->builder()->select( + [ + 'users.id as userId', + 'CONCAT(users.first_name," ",users.last_name) as userFullName', + 'users.username' + ] + ) + ->join('users', 'users.id = chat_department_users.user_id', 'left') + ->join('chat_departments', 'chat_departments.id = chat_department_users.chat_department_id', 'left') + ->where('chat_departments.id', $chat_department_id) + ->where('chat_department_users.pedido_id', null) + ->where('chat_department_users.presupuesto_id', null) + ->where('chat_department_users.factura_id', null) + ->where('chat_department_users.deleted_at', null); + } + public function querySelectUsersNotInDepartment(int $chat_department_id, ?string $q): array + { + $query = $this->builder()->select([ + 'users.id as userId', + ]) + ->join('users', 'users.id = chat_department_users.user_id', 'left') + ->join('chat_departments', 'chat_departments.id = chat_department_users.chat_department_id', 'left') + ->where('chat_departments.id', $chat_department_id) + ->where('chat_department_users.deleted_at', null); + + $usersInDepartment = array_map(fn($q) => $q->userId, $query->get()->getResultObject()); + $userModel = model(UserModel::class); + $queryUser = $userModel->builder()->select([ + 'id', + 'CONCAT(first_name," ",last_name) as name', + 'username as description', + ]) + ->where('cliente_id',null) + ->where('deleted_at', null); + if ($usersInDepartment) { + $queryUser->whereNotIn("id", $usersInDepartment); + } + if ($q) { + $queryUser->like('CONCAT(first_name," ",last_name)', $q); + } + return $queryUser->get()->getResultArray(); } } diff --git a/ci4/app/Models/Chat/ChatModel.php b/ci4/app/Models/Chat/ChatModel.php index 4055fca9..70db1dca 100644 --- a/ci4/app/Models/Chat/ChatModel.php +++ b/ci4/app/Models/Chat/ChatModel.php @@ -329,18 +329,19 @@ class ChatModel extends Model "chats.presupuesto_id as presupuestoId", "chats.factura_id as facturaId", "chats.title as chatDisplay", + "COUNT(chat_notifications.viewed) as unreadMessages" ]) ->join("chat_messages", "chat_messages.chat_id = chats.id", "left") ->join("chat_notifications", "chat_notifications.chat_message_id = chat_messages.id", "left") ->where("chat_notifications.user_id", auth()->user()->id) ->where("chat_notifications.viewed", false) - ->where("chats.chat_department_id is NOT NULL", NULL, FALSE); + ->where("chats.chat_department_id is NOT NULL", NULL, FALSE) + ->groupBy('chats.id'); $rows = $q->get()->getResultObject(); $auth_user = auth()->user(); $rows_new = []; foreach ($rows as $row) { - $row->unreadMessages = 0; if ($row->presupuestoId) { $row->model = $presupuestoModel->find($row->presupuestoId); if ($auth_user->cliente_id) { @@ -355,7 +356,6 @@ class ChatModel extends Model $row->chatDisplay .= "[INTERNAL]"; } $row->avatar = "PRE"; - $row->unreadMessages = $this->countUnreadMessagePresupuesto($row->presupuestoId); $rows_new[] = $row; } elseif ($row->pedidoId) { $row->model = $pedidoModel->find($row->pedidoId); @@ -365,7 +365,6 @@ class ChatModel extends Model $row->chatDisplay .= "[INTERNAL]"; } $row->avatar = "P"; - $row->unreadMessages = $this->countUnreadMessagePedido($row->pedidoId); $rows_new[] = $row; } elseif ($row->facturaId) { $row->model = $facturaModel->find($row->facturaId); @@ -375,7 +374,6 @@ class ChatModel extends Model $row->chatDisplay .= "[INTERNAL]"; } $row->title = $row->facturaId; - $row->unreadMessages = $this->countUnreadMessageFactura($row->facturaId); $rows_new[] = $row; } } @@ -587,7 +585,7 @@ class ChatModel extends Model } return $data; } - public function countUnreadMessagePresupuesto($presupuesto_id): int|string + public function countUnreadMessagePresupuesto(int $presupuesto_id,int $chat_department_id): int|string { return $this->builder()->select() ->join("chat_messages", "chat_messages.chat_id = chats.id", "left") @@ -595,6 +593,7 @@ class ChatModel extends Model ->where("chats.presupuesto_id", $presupuesto_id) ->where("chat_notifications.viewed", false) ->where("chat_notifications.user_id", auth()->user()->id) + ->where('chats.chat_department_id',$chat_department_id) ->countAllResults(); } public function countUnreadMessagePedido($pedido_id): int|string @@ -711,9 +710,7 @@ class ChatModel extends Model [ "CONCAT(users.first_name,' ',users.last_name) as userFullName", "users.username as userName", - "SUM(chat_notifications.viewed) as viewed_count", - "COUNT(chat_notifications.id) as notification_count" - + "chat_notifications.viewed" ] ) @@ -722,6 +719,26 @@ class ChatModel extends Model ->join("users", "users.id = chat_notifications.user_id", 'left') ->where("chats.id", $chat_id) ->where("chat_notifications.deleted_at", null) + ->where('chat_notifications.viewed',false) + ->get()->getResultArray(); + return $q; + } + public function getUsersNotificationViewedFromChat(int $chat_id): array + { + $q = $this->builder()->distinct()->select( + [ + "CONCAT(users.first_name,' ',users.last_name) as userFullName", + "users.username as userName", + "chat_notifications.viewed" + ] + + ) + ->join("chat_messages", "chat_messages.chat_id = chats.id", 'left') + ->join("chat_notifications", "chat_notifications.chat_message_id = chat_messages.id", 'left') + ->join("users", "users.id = chat_notifications.user_id", 'left') + ->where("chats.id", $chat_id) + ->where("chat_notifications.deleted_at", null) + ->where('chat_notifications.viewed',true) ->get()->getResultArray(); return $q; } @@ -903,16 +920,17 @@ class ChatModel extends Model public function setAsViewedChatUserNotifications(int $chat_id, int $user_id) { $query = $this->builder() - ->select("chat_messages.id") + ->select("chat_notifications.id as notificationId") ->join('chat_messages', 'chat_messages.chat_id = chats.id', 'left') + ->join('chat_notifications', 'chat_notifications.chat_message_id = chat_messages.id', 'left') + ->where('chat_notifications.user_id',$user_id) ->where('chat_messages.chat_id', $chat_id) ->get()->getResultObject(); - $chat_messages_ids = array_map(fn($q) => $q->id, $query); - - $this->db->table("chat_notifications") - ->where("user_id", $user_id) - ->whereIn("chat_message_id", $chat_messages_ids) - ->update(["viewed" => true]); + $chat_messages_ids = array_map(fn($q) => $q->notificationId, $query); + $chatNotificationModel = model(ChatNotification::class); + if($chat_messages_ids){ + $chatNotificationModel->setNotificationsAsViewed($chat_messages_ids); + } } public function setAsViewedChatUserMessages(int $chat_id, int $user_id) { @@ -924,16 +942,17 @@ class ChatModel extends Model public function setAsUnviewedChatUserNotifications(int $chat_id, int $user_id) { $query = $this->builder() - ->select("chat_messages.id") + ->select("chat_notifications.id as notificationId") ->join('chat_messages', 'chat_messages.chat_id = chats.id', 'left') + ->join('chat_notifications', 'chat_notifications.chat_message_id = chat_messages.id', 'left') + ->where('chat_notifications.user_id',$user_id) ->where('chat_messages.chat_id', $chat_id) ->get()->getResultObject(); - $chat_messages_ids = array_map(fn($q) => $q->id, $query); - - $this->db->table("chat_notifications") - ->where("user_id", $user_id) - ->whereIn("chat_message_id", $chat_messages_ids) - ->update(["viewed" => false]); + $chat_messages_ids = array_map(fn($q) => $q->notificationId, $query); + $chatNotificationModel = model(ChatNotification::class); + if($chat_messages_ids){ + $chatNotificationModel->setNotificationsAsUnViewed($chat_messages_ids); + } } public function setAsUnviewedChatUserMessages(int $chat_id, int $user_id) diff --git a/ci4/app/Models/ChatNotification.php b/ci4/app/Models/ChatNotification.php index 007f26c5..7d8824d4 100644 --- a/ci4/app/Models/ChatNotification.php +++ b/ci4/app/Models/ChatNotification.php @@ -59,4 +59,12 @@ class ChatNotification extends Model } return $status; } + public function setNotificationsAsViewed(array $notificationIds) : bool + { + return $this->update($notificationIds,["viewed" => 1]); + } + public function setNotificationsAsUnViewed(array $notificationIds) : bool + { + return $this->update($notificationIds,["viewed" => 0]); + } } diff --git a/ci4/app/Views/themes/vuexy/components/chat_factura.php b/ci4/app/Views/themes/vuexy/components/chat_factura.php index 0a50c300..df2495c1 100644 --- a/ci4/app/Views/themes/vuexy/components/chat_factura.php +++ b/ci4/app/Views/themes/vuexy/components/chat_factura.php @@ -26,11 +26,11 @@