add messages to ot

This commit is contained in:
amazuecos
2025-04-15 00:42:24 +02:00
parent 14c5cf493c
commit 9f46569b01
17 changed files with 445 additions and 14 deletions

View File

@ -6,11 +6,13 @@ use App\Entities\Chat\ChatEntity;
use App\Models\ChatNotification;
use App\Models\ChatUser;
use App\Models\Facturas\FacturaModel;
use App\Models\OrdenTrabajo\OrdenTrabajoModel;
use App\Models\Pedidos\PedidoModel;
use App\Models\Presupuestos\PresupuestoModel;
use App\Models\Usuarios\UserModel;
use CodeIgniter\Model;
use CodeIgniter\Database\BaseBuilder;
use Config\OrdenTrabajo;
class ChatModel extends Model
{
@ -25,6 +27,7 @@ class ChatModel extends Model
"chat_department_id",
"presupuesto_id",
"factura_id",
"orden_trabajo_id",
"title"
];
@ -86,6 +89,10 @@ class ChatModel extends Model
{
return $this->builder()->where("factura_id", $factura_id)->where("chat_department_id", $chat_department_id)->get()->getFirstRow();
}
public function getChatOrdenTrabajo(int $chat_department_id, int $orden_trabajo_id)
{
return $this->builder()->where("orden_trabajo_id", $orden_trabajo_id)->where("chat_department_id", $chat_department_id)->get()->getFirstRow();
}
public function createChatPresupuesto(int $chat_department_id, int $presupuesto_id): int
{
@ -123,6 +130,17 @@ class ChatModel extends Model
"chat_department_id" => $chat_department_id
]);
}
public function createChatOrdenTrabajo(int $chat_department_id, int $orden_trabajo_id): int
{
$model = model(OrdenTrabajoModel::class);
$chatDeparmentModel = model(ChatDeparmentModel::class);
$ot = $model->find($orden_trabajo_id);
return $this->insert([
"title" => "[OT]".$ot->id . "[" . $chatDeparmentModel->getDisplay($chat_department_id) . "]",
"orden_trabajo_id" => $orden_trabajo_id,
"chat_department_id" => $chat_department_id
]);
}
public function createChatSingle(): int
{
return $this->insert(["chat_department_id" => null]);
@ -323,6 +341,7 @@ class ChatModel extends Model
"chats.pedido_id as pedidoId",
"chats.presupuesto_id as presupuestoId",
"chats.factura_id as facturaId",
"chats.orden_trabajo_id as ordenTrabajoId",
"chats.title as chatDisplay",
"COUNT(chat_notifications.id) as unreadMessages"
])
@ -363,6 +382,13 @@ class ChatModel extends Model
$row->title = $row->facturaId;
$rows_new[] = $row;
}
elseif ($row->ordenTrabajoId) {
// $row->model = $facturaModel->find($row->facturaId);
$row->uri = "/chat/ot/" . $row->ordenTrabajoId . "#accordionChatOrdenTrabajo";
$row->avatar = "OT";
$row->title = $row->ordenTrabajoId;
$rows_new[] = $row;
}
}
return $rows_new;
}
@ -374,6 +400,7 @@ class ChatModel extends Model
"chats.chat_department_id as chatDepartmentId",
"chats.pedido_id as pedidoId",
"chats.presupuesto_id as presupuestoId",
"chats.orden_trabajo_id as ordenTrabajoId",
"chats.factura_id as facturaId",
"chats.title as chatDisplay",
"COUNT(chat_messages.id) as unreadMessages"
@ -413,6 +440,13 @@ class ChatModel extends Model
$row->title = $row->facturaId;
$rows_new[] = $row;
}
elseif ($row->ordenTrabajoId) {
$row->uri = "/produccion/ordentrabajo/edit/" . $row->ordenTrabajoId . "#accordionChatOrdenTrabajo";
$row->avatar = "OT";
$row->chatDisplay .= "[INTERNAL]";
$row->title = $row->ordenTrabajoId;
$rows_new[] = $row;
}
}
return $rows_new;
}
@ -433,6 +467,7 @@ class ChatModel extends Model
->where("chats.chat_department_id", null)
->where("chats.pedido_id", null)
->where("chats.factura_id", null)
->where("chats.orden_trabajo_id", null)
->where("chat_notifications.viewed", false)
->where("chat_notifications.user_id", auth()->user()->id);
$rows = $q->get()->getResultObject();
@ -513,6 +548,27 @@ class ChatModel extends Model
$data["users"] = $this->getChatUsers($chat_id);
return $data;
}
public function getChatInternalHebraOrdenTrabajo($chat_id, $orden_trabajo_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.orden_trabajo_id", $orden_trabajo_id);
$data["chatId"] = $chat_id;
$data["messages"] = $query->get()->getResultObject();
$data["chatTitle"] = "OT" . "[" . $orden_trabajo_id . "] - ";
$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([
@ -557,6 +613,17 @@ class ChatModel extends Model
}
return $data;
}
public function getOrdenTrabajoHebras($orden_trabajo_id): array
{
$data = [];
$chats = $this->builder()->select("chats.id as chatId")
->where("chats.chat_department_id", null)
->where("chats.orden_trabajo_id", $orden_trabajo_id)->get()->getResultObject();
foreach ($chats as $chat) {
$data[$chat->chatId] = $this->getChatInternalHebraOrdenTrabajo($chat->chatId, $orden_trabajo_id);
}
return $data;
}
public function countUnreadMessagePresupuesto(int $presupuesto_id): int|string
{
return $this->builder()->select()
@ -586,6 +653,15 @@ class ChatModel extends Model
->where("chat_notifications.viewed", false)
->where("chat_notifications.user_id", auth()->user()->id)->countAllResults();
}
public function countUnreadMessageOrdenTrabajo($orden_trabajo_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.orden_trabajo_id", $orden_trabajo_id)
->where("chat_notifications.viewed", false)
->where("chat_notifications.user_id", auth()->user()->id)->countAllResults();
}
public function countUnreadMessageDirectos(int $chat_id): int|string
{
return $this->builder()->select()
@ -606,6 +682,7 @@ class ChatModel extends Model
->where("chats.presupuesto_id", null)
->where("chats.pedido_id", null)
->where("chats.factura_id", null)
->where("chats.orden_trabajo_id", null)
->where("chats.id", $chat_id)
->where("chat_messages.receiver_id", auth()->user()->id)->get()->getFirstRow();
$userModel = model(UserModel::class);
@ -837,6 +914,36 @@ class ChatModel extends Model
}
return $query->groupBy('chatMessageId');
}
public function getQueryDatatableMessageOrdenTrabajo(int $user_id): BaseBuilder
{
$query = $this->builder()
->select([
"chats.id",
"cm.id as chatMessageId",
"u.id as userId",
"cm.message",
"chats.created_at",
"
(
SELECT cm2.updated_at
FROM chat_messages cm2
WHERE cm2.chat_id = chats.id
ORDER BY cm2.updated_at DESC LIMIT 1
) as updated_at",
"CONCAT(u.first_name,' ',u.last_name) as creator",
"chats.title",
])
->join("chat_messages cm", "chats.id = cm.chat_id", "left")
->join("users u", "u.id = cm.sender_id", 'left')
->join("ordenes_trabajo", "ordenes_trabajo.id = chats.orden_trabajo_id", "left")
->where("chats.orden_trabajo_id is NOT NULL", NULL, FALSE);
if (auth()->user()->inGroup("cliente-administrador","cliente")) {
$query->where('facturas.cliente_id', auth()->user()->cliente_id)
->where("chats.chat_department_id is NOT NULL", NULL, FALSE);
}
return $query->groupBy('chatMessageId');
}
public function createNewDirectChat(string $title, string $message, array $users)
{
$chatMessageModel = model(ChatMessageModel::class);