diff --git a/ci4/app/Config/Routes.php b/ci4/app/Config/Routes.php index f2958a65..1142b7f2 100644 --- a/ci4/app/Config/Routes.php +++ b/ci4/app/Config/Routes.php @@ -769,6 +769,18 @@ $routes->group('chat', ['namespace' => 'App\Controllers\Chat'], function ($route $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']); + $routes->get('users/internal', 'ChatController::get_chat_users_internal', ['as' => 'getChatUsersInternal']); + $routes->post('hebra/presupuesto', 'ChatController::store_hebra_presupuesto', ['as' => 'storeHebraPresupuesto']); + $routes->post('hebra/pedido', 'ChatController::store_hebra_pedido', ['as' => 'storeHebraPedido']); + $routes->post('hebra/factura', 'ChatController::store_hebra_factura', ['as' => 'storeHebraFactura']); + $routes->post('hebra/(:num)', 'ChatController::update_hebra/$1', ['as' => 'updateHebra']); + $routes->get('hebra/presupuesto/(:num)',"ChatController::get_hebra_presupuesto/$1",["as" => "getHebraPresupuesto"]); + $routes->get('hebra/pedido/(:num)',"ChatController::get_hebra_pedido/$1",["as" => "getHebraPedido"]); + $routes->get('hebra/factura/(:num)',"ChatController::get_hebra_factura/$1",["as" => "getHebraFactura"]); + + + + }); diff --git a/ci4/app/Controllers/Chat/ChatController.php b/ci4/app/Controllers/Chat/ChatController.php index daa1f564..cad8ad6b 100644 --- a/ci4/app/Controllers/Chat/ChatController.php +++ b/ci4/app/Controllers/Chat/ChatController.php @@ -7,6 +7,8 @@ use App\Models\Chat\ChatDeparmentModel; use App\Models\Chat\ChatDeparmentUserModel; use App\Models\Chat\ChatMessageModel; use App\Models\Chat\ChatModel; +use App\Models\ChatNotification; +use App\Models\ChatUser; use App\Models\Clientes\ClienteModel; use App\Models\Usuarios\UserModel; use CodeIgniter\HTTP\ResponseInterface; @@ -22,6 +24,8 @@ class ChatController extends BaseController protected ChatMessageModel $chatMessageModel; protected UserModel $userModel; protected ClienteModel $clienteModel; + protected ChatUser $chatUserModel; + protected ChatNotification $chatNotificationModel; @@ -40,6 +44,9 @@ class ChatController extends BaseController $this->chatMessageModel = model(ChatMessageModel::class); $this->userModel = model(UserModel::class); $this->clienteModel = model(ClienteModel::class); + $this->chatUserModel = model(ChatUser::class); + $this->chatNotificationModel = model(ChatNotification::class); + } public function index() {} @@ -55,11 +62,13 @@ class ChatController extends BaseController $data = [ "chat" => null, "messages" => null, + "count" => 0, ]; $chat = $this->chatModel->getChatPresupuesto($chat_department_id, $presupuesto_id); if ($chat) { $data["messages"] = $this->chatMessageModel->get_chat_messages($chat->id); $this->chatMessageModel->set_chat_department_messages_as_read($chat->id); + $data["count"] = count($data["messages"]); } $data["chat"] = $chat; return $this->response->setJSON($data); @@ -70,11 +79,13 @@ class ChatController extends BaseController $data = [ "chat" => null, "messages" => null, + "count" => 0, ]; $chat = $this->chatModel->getChatPedido($chat_department_id, $pedido_id); if ($chat) { $data["messages"] = $this->chatMessageModel->get_chat_messages($chat->id); $this->chatMessageModel->set_chat_department_messages_as_read($chat->id); + $data["count"] = count($data["messages"]); } $data["chat"] = $chat; @@ -86,12 +97,13 @@ class ChatController extends BaseController $data = [ "chat" => null, "messages" => null, + "count" => 0, ]; $chat = $this->chatModel->getChatFactura($chat_department_id, $factura_id); if ($chat) { $data["messages"] = $this->chatMessageModel->get_chat_messages($chat->id); $this->chatMessageModel->set_chat_department_messages_as_read($chat->id); - + $data["count"] = count($data["messages"]); } $data["chat"] = $chat; return $this->response->setJSON($data); @@ -151,7 +163,7 @@ class ChatController extends BaseController public function store_chat_message_single() { $data = $this->request->getPost(); - + $existChat = $this->chatMessageModel->get_chat_contact_messages($data["receiver_id"]); if (count($existChat) > 0) { $chatId = $existChat[0]->chat_id; @@ -171,7 +183,7 @@ class ChatController extends BaseController } public function get_chat_internal_contacts() { - if(auth()->user()->cliente_id){ + if (auth()->user()->cliente_id) { return $this->response->setJSON([]); } $users = $this->userModel->builder() @@ -187,7 +199,7 @@ class ChatController extends BaseController } public function get_chat_internal_contact(int $user_id) { - if(auth()->user()->cliente_id){ + if (auth()->user()->cliente_id) { return $this->response->setJSON([]); } $users = $this->userModel->builder() @@ -207,7 +219,7 @@ class ChatController extends BaseController { $cliente_id = auth()->user()->cliente_id; $response = []; - if($cliente_id){ + if ($cliente_id) { $data = $this->clienteModel->getClienteDataPresupuestoPedidoFactura($cliente_id); $response["totalMessages"] = 0; $response["chatFacturas"] = $this->chatModel->getClienteChatFacturas($data["facturas"]); @@ -223,8 +235,7 @@ class ChatController extends BaseController $response["totalMessages"] += $value->unreadMessages; } $response["data"] = $data; - - }else{ + } else { $response["internals"] = $this->chatModel->getChatDepartmentNotifications(); $internal_notifications = $this->chatModel->getChatInternalNotifications(); foreach ($internal_notifications as $value) { @@ -243,4 +254,167 @@ class ChatController extends BaseController $data = $this->chatDeparmentModel->getChatDepartmentUsers($chat_department_id); return $this->response->setJSON($data); } + public function get_chat_users_internal() + { + $query = $this->userModel->builder()->select( + [ + "id", + "CONCAT(first_name,' ',last_name,'(',username,')') as name" + ] + )->where("cliente_id", null) + ->where("deleted_at", null) + ->whereNotIn("id",[auth()->user()->id]); + if ($this->request->getGet("q")) { + $query->groupStart() + ->orLike("users.username", $this->request->getGet("q")) + ->orLike("CONCAT(first_name,' ',last_name)", $this->request->getGet("q")) + ->groupEnd(); + } + + return $this->response->setJSON($query->get()->getResultObject()); + } + public function store_hebra_presupuesto() + { + $bodyData = $this->request->getPost(); + $chat_id = $this->chatModel->insert([ + "presupuesto_id" => $bodyData["modelId"], + "title" => $bodyData["title"] + ]); + $chatMessageId = $this->chatMessageModel->insert([ + "chat_id" => $chat_id, + "message" => $bodyData["message"], + "sender_id" => auth()->user()->id + ]); + if(isset($bodyData["users"])){ + $chatUserData = array_map(fn($x) => ["user_id" => $x,"chat_id" => $chat_id],$bodyData["users"]); + $this->chatUserModel->insertBatch($chatUserData); + foreach ($bodyData["users"] as $userId) { + $this->chatNotificationModel->insert( + ["chat_message_id" => $chatMessageId,"user_id" => $userId]); + } + } + return $this->response->setJSON(["message" => "Hebra creada correctamente","status" => true]); + } + + public function store_hebra_pedido() + { + $bodyData = $this->request->getPost(); + $chat_id = $this->chatModel->insert([ + "pedido_id" => $bodyData["modelId"], + "title" => $bodyData["title"] + + ]); + $chatMessageId = $this->chatMessageModel->insert([ + "chat_id" => $chat_id, + "message" => $bodyData["message"], + "sender_id" => auth()->user()->id + ]); + if(isset($bodyData["users"])){ + $chatUserData = array_map(fn($x) => ["user_id" => $x,"chat_id" => $chat_id],$bodyData["users"]); + $this->chatUserModel->insertBatch($chatUserData); + foreach ($bodyData["users"] as $userId) { + $this->chatNotificationModel->insert( + ["chat_message_id" => $chatMessageId,"user_id" => $userId]); + } + } + return $this->response->setJSON(["message" => "Hebra creada correctamente","status" => true]); + } + + public function store_hebra_factura() + { + $bodyData = $this->request->getPost(); + $chat_id = $this->chatModel->insert([ + "factura_id" => $bodyData["modelId"], + "title" => $bodyData["title"] + ]); + $chatMessageId = $this->chatMessageModel->insert([ + "chat_id" => $chat_id, + "message" => $bodyData["message"], + "sender_id" => auth()->user()->id + ]); + if(isset($bodyData["users"])){ + $chatUserData = array_map(fn($x) => ["user_id" => $x,"chat_id" => $chat_id],$bodyData["users"]); + $this->chatUserModel->insertBatch($chatUserData); + foreach ($bodyData["users"] as $userId) { + $this->chatNotificationModel->insert( + ["chat_message_id" => $chatMessageId,"user_id" => $userId]); + } + } + return $this->response->setJSON(["message" => "Hebra creada correctamente","status" => true]); + + } + public function update_hebra($chat_id) + { + $bodyData = $this->request->getPost(); + $chatMessageId = $this->chatMessageModel->insert([ + "chat_id" => $chat_id, + "message" => $bodyData["message"], + "sender_id" => auth()->user()->id + ]); + $actualUsers = $this->chatUserModel->builder()->select("user_id")->where("chat_id",$chat_id)->get()->getResultArray(); + $actualUsersArray = array_map(fn($x) => $x["user_id"],$actualUsers); + foreach ($actualUsersArray as $key => $user_id) { + $this->chatNotificationModel->insert( + ["chat_message_id" => $chatMessageId,"user_id" => $user_id]); + } + if(isset($bodyData["users"])){ + foreach ($bodyData["users"] as $userId) { + if(in_array($userId,$actualUsersArray) == false){ + $chatUserData = ["user_id" => $userId,"chat_id" => $chat_id]; + $this->chatUserModel->insert($chatUserData); + + } + $this->chatNotificationModel->insert( + ["chat_message_id" => $chatMessageId,"user_id" => $userId]); + } + } + return $this->response->setJSON(["message" => "Hebra actualizada correctamente","status" => true]); + } + public function get_hebra_presupuesto($presupuesto_id){ + $data = $this->chatModel->getPresupuestoHebras($presupuesto_id); + $notifications = $this->chatModel->builder()->select([ + "chat_notifications.id" + ]) + ->join("chat_messages","chat_messages.chat_id = chats.id","left") + ->join("chat_notifications","chat_notifications.chat_message_id = chat_messages.id","left") + ->where("chats.presupuesto_id",$presupuesto_id) + ->where("chat_notifications.user_id",auth()->user()->id) + ->get()->getResultArray(); + foreach ($notifications as $notification) { + $this->chatNotificationModel->update($notification["id"],["viewed" => true]); + } + return $this->response->setJSON($data); + } + public function get_hebra_pedido($pedido_id){ + $data = $this->chatModel->getPedidoHebras($pedido_id); + $notifications = $this->chatModel->builder()->select([ + "chat_notifications.id" + ]) + ->join("chat_messages","chat_messages.chat_id = chats.id","left") + ->join("chat_notifications","chat_notifications.chat_message_id = chat_messages.id","left") + ->where("chats.pedido_id",$pedido_id) + ->where("chat_notifications.user_id",auth()->user()->id) + ->get()->getResultArray(); + foreach ($notifications as $notification) { + $this->chatNotificationModel->update($notification["id"],["viewed" => true]); + } + return $this->response->setJSON($data); + } + public function get_hebra_factura($factura_id){ + $data = $this->chatModel->getFacturaHebras($factura_id); + $notifications = $this->chatModel->builder()->select([ + "chat_notifications.id" + ]) + ->join("chat_messages","chat_messages.chat_id = chats.id","left") + ->join("chat_notifications","chat_notifications.chat_message_id = chat_messages.id","left") + ->where("chats.factura_id",$factura_id) + ->where("chat_notifications.user_id",auth()->user()->id) + ->get()->getResultArray(); + foreach ($notifications as $notification) { + $this->chatNotificationModel->update($notification["id"],["viewed" => true]); + } + return $this->response->setJSON($data); + } + + } diff --git a/ci4/app/Database/Migrations/2024-10-12-151500_ChatUsers.php b/ci4/app/Database/Migrations/2024-10-12-151500_ChatUsers.php new file mode 100644 index 00000000..2b2ab3ef --- /dev/null +++ b/ci4/app/Database/Migrations/2024-10-12-151500_ChatUsers.php @@ -0,0 +1,55 @@ + [ + "type" => "INT", + "unsigned" => true, + "auto_increment" => true + ], + "chat_id" => [ + "type" => "INT", + "unsigned" => true, + ], + "user_id" => [ + "type" => "INT", + "unsigned" => true, + ], + ]; + public function up() + { + $this->forge->addField($this->COLUMNS); + $currenttime = new RawSql("CURRENT_TIMESTAMP"); + $this->forge->addField([ + "created_at" => [ + "type" => "TIMESTAMP", + "default" => $currenttime, + ], + "updated_at" => [ + "type" => "TIMESTAMP", + "null" => true, + + ], + "deleted_at" => [ + "type" => "TIMESTAMP", + "null" => true, + + ], + ]); + $this->forge->addPrimaryKey('id'); + $this->forge->addForeignKey(["user_id"], "users", ["id"]); + $this->forge->addForeignKey(["chat_id"], "chats", ["id"]); + $this->forge->createTable("chat_users", true); + } + + public function down() + { + $this->forge->dropTable("chat_users"); + } +} diff --git a/ci4/app/Database/Migrations/2024-10-13-185128_AlterChatTableAddTitleColumn.php b/ci4/app/Database/Migrations/2024-10-13-185128_AlterChatTableAddTitleColumn.php new file mode 100644 index 00000000..0f884dc2 --- /dev/null +++ b/ci4/app/Database/Migrations/2024-10-13-185128_AlterChatTableAddTitleColumn.php @@ -0,0 +1,33 @@ + [ + "type" => "VARCHAR", + "constraint" => 255, + "null" => true + ], + + + + ]; + public function up() + { + + $this->forge->addColumn("chats",$this->COLUMNS); + + } + + public function down() + { + // + $this->forge->dropColumn("chats","title"); + } +} diff --git a/ci4/app/Database/Migrations/2024-10-13-200759_ChatNotifications.php b/ci4/app/Database/Migrations/2024-10-13-200759_ChatNotifications.php new file mode 100644 index 00000000..c117b193 --- /dev/null +++ b/ci4/app/Database/Migrations/2024-10-13-200759_ChatNotifications.php @@ -0,0 +1,58 @@ + [ + "type" => "INT", + "unsigned" => true, + "auto_increment" => true + ], + "chat_message_id" => [ + "type" => "INT", + ], + "user_id" => [ + "type" => "INT", + "unsigned" => true, + ], + "viewed" => [ + "type" => "BOOLEAN", + "default" => false + ], + ]; + public function up() + { + $this->forge->addField($this->COLUMNS); + $currenttime = new RawSql("CURRENT_TIMESTAMP"); + $this->forge->addField([ + "created_at" => [ + "type" => "TIMESTAMP", + "default" => $currenttime, + ], + "updated_at" => [ + "type" => "TIMESTAMP", + "null" => true, + + ], + "deleted_at" => [ + "type" => "TIMESTAMP", + "null" => true, + + ], + ]); + $this->forge->addPrimaryKey('id'); + $this->forge->addForeignKey(["chat_message_id"], "chat_messages", ["id"]); + $this->forge->addForeignKey(["user_id"], "users", ["id"]); + $this->forge->createTable("chat_notifications", true); + } + + public function down() + { + $this->forge->dropTable("chat_notifications"); + } +} diff --git a/ci4/app/Language/es/Chat.php b/ci4/app/Language/es/Chat.php index 3981f2ce..1e649965 100644 --- a/ci4/app/Language/es/Chat.php +++ b/ci4/app/Language/es/Chat.php @@ -1,5 +1,13 @@ "Chat" + "chat" => "Mensajería", + "modal" => [ + "new_hebra" => "Nueva hebra", + "title" => "Título", + "new_message" => "Nuevo mensaje", + "new_receivers" => "Nuevos participantes", + "btn_send" => "Enviar", + "btn_send_update" => "Enviar" + ] ]; \ No newline at end of file diff --git a/ci4/app/Models/Chat/ChatDeparmentModel.php b/ci4/app/Models/Chat/ChatDeparmentModel.php index 9d7fc206..e51f29ce 100644 --- a/ci4/app/Models/Chat/ChatDeparmentModel.php +++ b/ci4/app/Models/Chat/ChatDeparmentModel.php @@ -53,13 +53,17 @@ class ChatDeparmentModel extends Model public function getChatDepartments(string $type = "general"): array { $userModel = model(UserModel::class); + $chatMessageModel = model(ChatMessageModel::class); + $query = $this->db->table('chat_departments') ->select( [ + 'chat_departments.id', 'chat_departments.name', 'chat_departments.display', 'chat_department_users.user_id', + 'chats.id as chatId', ] ) ->join( @@ -67,22 +71,28 @@ class ChatDeparmentModel extends Model "chat_department_users.chat_department_id = chat_departments.id", 'left' ) + ->join("chats", "chats.chat_department_id = chat_departments.id", "left") ->join( "users", "chat_department_users.user_id = users.id", 'left' - )->where("chat_departments.type",$type); - if(auth()->user()->cliente_id == null){ - $query->where("chat_department_users.user_id",auth()->user()->id); - } - + )->join( + "chat_messages", + "chat_messages.chat_id = chats.id", + "left" + ) + ->where("chat_departments.type", $type); + if (auth()->user()->cliente_id == null) { + $query->where("chat_department_users.user_id", auth()->user()->id); + } + $results = $query->get()->getResultArray(); // Create the desired structure $departments = []; foreach ($results as $row) { $departmentName = $row['name']; - + // If the department is not yet added to the array, initialize it if (!isset($departments[$departmentName])) { $departments[$departmentName] = [ @@ -117,9 +127,8 @@ class ChatDeparmentModel extends Model "users", "chat_department_users.user_id = users.id", 'left' - )->where("chat_departments.id",$chat_deparment_id) + )->where("chat_departments.id", $chat_deparment_id) ->get()->getResultObject(); return $result; - } } diff --git a/ci4/app/Models/Chat/ChatModel.php b/ci4/app/Models/Chat/ChatModel.php index bbddee86..161b8115 100644 --- a/ci4/app/Models/Chat/ChatModel.php +++ b/ci4/app/Models/Chat/ChatModel.php @@ -2,6 +2,7 @@ namespace App\Models\Chat; +use App\Models\ChatNotification; use App\Models\Facturas\FacturaModel; use App\Models\Pedidos\PedidoModel; use App\Models\Presupuestos\PresupuestoModel; @@ -22,6 +23,7 @@ class ChatModel extends Model "chat_department_id", "presupuesto_id", "factura_id", + "title" ]; protected bool $allowEmptyInserts = false; @@ -279,6 +281,7 @@ class ChatModel extends Model } public function getChatDepartmentNotifications() { + $chatDeparmentModel = model(ChatDeparmentModel::class); $chatMessageModel = model(ChatMessageModel::class); $presupuestoModel = model(PresupuestoModel::class); $facturaModel = model(FacturaModel::class); @@ -299,11 +302,14 @@ class ChatModel extends Model ->where("chat_department_users.user_id",auth()->user()->id); $rows = $q->get()->getResultObject(); + foreach ($rows as $row) { $messages = $chatMessageModel->get_chat_messages($row->chatId); $count = 0; + $chatDeparmentUsers = $chatDeparmentModel->getChatDepartmentUsers($row->chatDepartmentId); + $chatDeparmentUsersId = array_map(fn($x) => $x->id,$chatDeparmentUsers); foreach ($messages as $m) { - if($m->viewed == false && $m->sender_id != auth()->user()->id) + if($m->viewed == false && $m->sender_id != auth()->user()->id && in_array($m->sender_id,$chatDeparmentUsersId) == false) $count++; } if($row->presupuestoId){ @@ -335,25 +341,184 @@ class ChatModel extends Model public function getChatInternalNotifications() { $chatMessageModel = model(ChatMessageModel::class); - $userModel = model(UserModel::class); - $internalMessages = $chatMessageModel->builder() + $presupuestoModel = model(PresupuestoModel::class); + $facturaModel = model(FacturaModel::class); + $pedidoModel = model(PedidoModel::class); + $chatNotificationModel = model(ChatNotification::class); + + $q = $this->db->table("chats") ->select([ - "chat_id as chatId", - "sender_id", - "COUNT(viewed) as unreadMessages", + "chats.id as chatId", + "chats.pedido_id as pedidoId", + "chats.presupuesto_id as presupuestoId", + "chats.factura_id as facturaId", + "chats.presupuesto_id as presupuestoId", + "chats.title as chatDisplay" ]) - ->where("receiver_id",auth()->user()->id) - ->where("viewed",false)->get()->getResultObject(); - foreach ($internalMessages as $m) { - if($m->sender_id){ - $sender = $userModel->find($m->sender_id); - $m->model = $sender; - $m->title = $sender->username; - $m->chatDisplay = ($sender->first_name ?? "")." ".($sender->last_name ?? ""); - $m->avatar = strtoupper(mb_substr($sender->first_name, 0, 1).mb_substr($sender->last_name, 0, 1)); - $m->uri = "#"; + ->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); + $rows = $q->get()->getResultObject(); + + foreach ($rows as $row) { + if($row->presupuestoId){ + $row->model = $presupuestoModel->find($row->presupuestoId); + $row->uri = "/presupuestos/cosidotapablanda/edit/".$row->presupuestoId; + $row->title = $row->presupuestoId; + $row->avatar = "PRE"; + $row->unreadMessages = $this->countUnreadMessagePresupuesto($row->presupuestoId); + } + elseif($row->pedidoId){ + $row->model = $pedidoModel->find($row->pedidoId); + $row->uri = "/pedidos/edit/".$row->pedidoId; + $row->title = $row->pedidoId; + $row->avatar = "P"; + $row->unreadMessages = $this->countUnreadMessagePedido($row->pedidoId); + + + } + elseif($row->facturaId){ + $row->model = $facturaModel->find($row->facturaId); + $row->uri = "/facturas/edit/".$row->facturaId; + $row->avatar = "F"; + $row->title = $row->facturaId; + $row->unreadMessages = $this->countUnreadMessageFactura($row->facturaId); + } } - return $internalMessages; + return $rows; + } + public function getChatInternalHebraPresupuesto(int $chat_id,int $presupuesto_id) : array + { + + $data = []; + $query = $this->builder()->select([ + "chats.id as chatId", + "chat_messages.message", + "users.username as senderUserName", + "chat_messages.created_at", + "CONCAT(users.first_name,' ',users.last_name) as senderFullName", + ]) + ->join("chat_messages","chat_messages.chat_id = chats.id","left") + ->join("users","users.id = chat_messages.sender_id","left") + ->where("chats.id",$chat_id) + ->where("chats.presupuesto_id",$presupuesto_id); + $data["chatId"] = $chat_id; + $data["messages"] = $query->get()->getResultObject(); + $data["chatTitle"] = $this->find($chat_id)->title; + $data["users"] = $this->getChatUsers($chat_id); + return $data; + } + public function getChatInternalHebraPedido($chat_id,$pedido_id){ + $data = []; + $query = $this->builder()->select([ + "chats.id as chatId", + "chat_messages.message", + "users.username as senderUserName", + "chat_messages.created_at", + "CONCAT(users.first_name,' ',users.last_name) as senderFullName", + ]) + ->join("chat_messages","chat_messages.chat_id = chats.id","left") + ->join("users","users.id = chat_messages.sender_id","left") + ->where("chats.id",$chat_id) + ->where("chats.pedido_id",$pedido_id); + $data["chatId"] = $chat_id; + $data["messages"] = $query->get()->getResultObject(); + $data["chatTitle"] = $this->find($chat_id)->title; + $data["users"] = $this->getChatUsers($chat_id); + return $data; + } + public function getChatInternalHebraFactura($chat_id,$factura_id){ + $data = []; + $query = $this->builder()->select([ + "chats.id as chatId", + "chat_messages.message", + "users.username as senderUserName", + "chat_messages.created_at", + "CONCAT(users.first_name,' ',users.last_name) as senderFullName", + ]) + ->join("chat_messages","chat_messages.chat_id = chats.id","left") + ->join("users","users.id = chat_messages.sender_id","left") + ->where("chats.id",$chat_id) + ->where("chats.factura_id",$factura_id); + $data["chatId"] = $chat_id; + $data["messages"] = $query->get()->getResultObject(); + $data["chatTitle"] = $this->find($chat_id)->title; + $data["users"] = $this->getChatUsers($chat_id); + return $data; + } + public function getChatUsers(int $chat_id) + { + $query = $this->builder()->select([ + "users.username", + "CONCAT(users.first_name,' ',users.last_name) as userFullName", + ]) + ->join("chat_users","chat_users.chat_id = chats.id") + ->join("users","users.id = chat_users.user_id","left") + ->where("chats.id",$chat_id); + return $query->get()->getResultObject(); + } + public function getPresupuestoHebras($presupuesto_id) : array + { + $data = []; + $chats = $this->builder()->select("chats.id as chatId") + ->where("chats.chat_department_id",null) + ->where("chats.presupuesto_id",$presupuesto_id)->get()->getResultObject(); + foreach ($chats as $chat) { + $data[$chat->chatId] = $this->getChatInternalHebraPresupuesto($chat->chatId,$presupuesto_id); + } + return $data; + } + public function getPedidoHebras($pedido_id) : array + { + $data = []; + $chats = $this->builder()->select("chats.id as chatId") + ->where("chats.chat_department_id",null) + ->where("chats.pedido_id",$pedido_id)->get()->getResultObject(); + foreach ($chats as $chat) { + $data[$chat->chatId] = $this->getChatInternalHebraPedido($chat->chatId,$pedido_id); + } + return $data; + } + public function getFacturaHebras($factura_id) : array + { + $data = []; + $chats = $this->builder()->select("chats.id as chatId") + ->where("chats.chat_department_id",null) + ->where("chats.factura_id",$factura_id)->get()->getResultObject(); + foreach ($chats as $chat) { + $data[$chat->chatId] = $this->getChatInternalHebraFactura($chat->chatId,$factura_id); + } + return $data; + } + public function countUnreadMessagePresupuesto($presupuesto_id) : int|string + { + return $this->builder()->select() + ->join("chat_messages","chat_messages.chat_id = chats.id","left") + ->join("chat_notifications","chat_notifications.chat_message_id = chat_messages.id","left") + ->where("chats.presupuesto_id",$presupuesto_id) + ->where("chat_notifications.viewed",false) + ->where("chat_notifications.user_id",auth()->user()->id) + ->countAllResults(); + } + public function countUnreadMessagePedido($pedido_id) : int|string + { + return $this->builder()->select() + ->join("chat_messages","chat_messages.chat_id = chats.id","left") + ->join("chat_notifications","chat_notifications.chat_message_id = chat_messages.id","left") + ->where("chats.pedido_id",$pedido_id) + ->where("chat_notifications.viewed",false) + ->where("chat_notifications.user_id",auth()->user()->id) + ->countAllResults(); + } + public function countUnreadMessageFactura($factura_id) : int|string + { + return $this->builder()->select() + ->join("chat_messages","chat_messages.chat_id = chats.id","left") + ->join("chat_notifications","chat_notifications.chat_message_id = chat_messages.id","left") + ->where("chats.factura_id",$factura_id) + ->where("chat_notifications.viewed",false) + ->where("chat_notifications.user_id",auth()->user()->id)->countAllResults(); } } diff --git a/ci4/app/Models/ChatNotification.php b/ci4/app/Models/ChatNotification.php new file mode 100644 index 00000000..fdbc6e2b --- /dev/null +++ b/ci4/app/Models/ChatNotification.php @@ -0,0 +1,50 @@ + "Tarifaextra.nombre", "rules" => "trim|required|max_length[255]", ], - "importe_fijo" => [ - "label" => "Tarifaextra.importeFijo", - "rules" => "required|decimal", - ], "margen" => [ "label" => "Tarifaextra.margen", "rules" => "required|decimal", diff --git a/ci4/app/Views/themes/vuexy/components/chat_factura.php b/ci4/app/Views/themes/vuexy/components/chat_factura.php index f177503d..797d8aa0 100644 --- a/ci4/app/Views/themes/vuexy/components/chat_factura.php +++ b/ci4/app/Views/themes/vuexy/components/chat_factura.php @@ -1,140 +1,116 @@ -
-
-

- -

-
-
-
-
-
+
+
+
- -
- + section('css') ?> endSection() ?> diff --git a/ci4/app/Views/themes/vuexy/components/chat_internal_factura.php b/ci4/app/Views/themes/vuexy/components/chat_internal_factura.php new file mode 100644 index 00000000..210be798 --- /dev/null +++ b/ci4/app/Views/themes/vuexy/components/chat_internal_factura.php @@ -0,0 +1,34 @@ +
+
+

+ +

+
+
+ +
+
+
+
\ No newline at end of file diff --git a/ci4/app/Views/themes/vuexy/components/chat_internal_pedido.php b/ci4/app/Views/themes/vuexy/components/chat_internal_pedido.php new file mode 100644 index 00000000..800229f2 --- /dev/null +++ b/ci4/app/Views/themes/vuexy/components/chat_internal_pedido.php @@ -0,0 +1,34 @@ +
+
+

+ +

+
+
+ +
+
+
+
\ No newline at end of file diff --git a/ci4/app/Views/themes/vuexy/components/chat_internal_presupuesto.php b/ci4/app/Views/themes/vuexy/components/chat_internal_presupuesto.php new file mode 100644 index 00000000..e8775dde --- /dev/null +++ b/ci4/app/Views/themes/vuexy/components/chat_internal_presupuesto.php @@ -0,0 +1,34 @@ +
+
+

+ +

+
+
+ +
+
+
+
\ No newline at end of file diff --git a/ci4/app/Views/themes/vuexy/components/chat_pedido.php b/ci4/app/Views/themes/vuexy/components/chat_pedido.php index ac2dbc31..44e17de6 100644 --- a/ci4/app/Views/themes/vuexy/components/chat_pedido.php +++ b/ci4/app/Views/themes/vuexy/components/chat_pedido.php @@ -1,140 +1,125 @@ -
-
-

- -

-
-
-
+
-
-
+
+
- -
- section('css') ?> diff --git a/ci4/app/Views/themes/vuexy/components/chat_presupuesto.php b/ci4/app/Views/themes/vuexy/components/chat_presupuesto.php index 18d1cfc2..cf791d27 100644 --- a/ci4/app/Views/themes/vuexy/components/chat_presupuesto.php +++ b/ci4/app/Views/themes/vuexy/components/chat_presupuesto.php @@ -1,138 +1,112 @@ -
-
-

- -

-
-
-
-
-
+
+
+
- -
-