db->table('chats') ->select( [ "chats.*", "chat_messages.created_at as messageCreatedAt", "chat_messages.message as messageText", ] ) ->join("chat_messages", "chats.id = chat_messages.chat_id", "left") ->orderBy("created_at", "desc") ->where("chats.id", $chat_id) ->get()->getResultObject(); } public function getChatPresupuesto(int $chat_department_id, int $presupuesto_id) { return $this->builder()->where("presupuesto_id", $presupuesto_id)->where("chat_department_id", $chat_department_id)->get()->getFirstRow(); } public function getChatPedido(int $chat_department_id, int $pedido_id) { return $this->builder()->where("pedido_id", $pedido_id)->where("chat_department_id", $chat_department_id)->get()->getFirstRow(); } public function getChatFactura(int $chat_department_id, int $factura_id) { return $this->builder()->where("factura_id", $factura_id)->where("chat_department_id", $chat_department_id)->get()->getFirstRow(); } public function createChatPresupuesto(int $chat_department_id, int $presupuesto_id): int { return $this->insert([ "presupuesto_id" => $presupuesto_id, "chat_department_id" => $chat_department_id ]); } public function createChatPedido(int $chat_department_id, int $pedido_id) : int { return $this->insert([ "pedido_id" => $pedido_id, "chat_department_id" => $chat_department_id ]); } public function createChatFactura(int $chat_department_id, int $factura_id) : int { return $this->insert([ "factura_id" => $factura_id, "chat_department_id" => $chat_department_id ]); } public function createChatSingle() : int { return $this->insert(["chat_department_id" => null]); } public function existChatPresupuesto(int $chat_department_id, int $presupuesto_id): bool { $countChatPresupuesto = $this->builder() ->where("presupuesto_id", $presupuesto_id) ->where("chat_department_id", $chat_department_id) ->countAllResults(); return $countChatPresupuesto > 0; } public function existChatPedido(int $chat_department_id, int $pedido_id): bool { $countChatPresupuesto = $this->builder() ->where("pedido_id", $pedido_id) ->where("chat_department_id", $chat_department_id) ->countAllResults(); return $countChatPresupuesto > 0; } public function existChatFactura(int $chat_department_id, int $factura_id): bool { $countChatPresupuesto = $this->builder() ->where("factura_id", $factura_id) ->where("chat_department_id", $chat_department_id) ->countAllResults(); return $countChatPresupuesto > 0; } public function getChatPedidosChat() : array { $query = $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") ->get()->getResultObject(); return $query; } public function getChatPresupuestosChat() : array { $query = $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", "presupuestos.titulo as title" ]) ->join("chat_departments","chat_departments.id = chats.chat_department_id","left") ->join("presupuestos","presupuestos.id = chats.pedido_id","left") ->get()->getResultObject(); return $query; } public function getChatFacturasChat() : array { $query = $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", "facturas.numero as title" ]) ->join("chat_departments","chat_departments.id = chats.chat_department_id","left") ->join("facturas","facturas.id = chats.pedido_id","left") ->get()->getResultObject(); return $query; } public function getChatSingleChat() : array { $query = $this->db->table("chats") ->select([ "chats.id as chatId", "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.pedido_id","left") ->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; } }