Merge branch 'main' into 'fix/presupuesto_pdf_fb'

Main

See merge request jjimenez/safekat!570
This commit is contained in:
Ignacio Martinez Navajas
2025-02-26 07:25:59 +00:00
8 changed files with 184 additions and 43 deletions

View File

@ -906,6 +906,8 @@ $routes->group('chat', ['namespace' => 'App\Controllers\Chat'], function ($route
$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"]);
$routes->get('users/notifications-unviewed/(:num)','ChatController::get_notifications_not_viewed_from_chat/$1');
});

View File

@ -60,7 +60,8 @@ class ChatController extends BaseController
$data = $this->chatDeparmentModel->getChatDepartments();
return $this->response->setJSON($data);
}
public function get_chat_department_select(){
public function get_chat_department_select()
{
$query = $this->chatDeparmentModel->getChatDepartmentSelect($this->request->getGet("q"));
return $this->response->setJSON($query->get()->getResultObject());
}
@ -142,9 +143,9 @@ class ChatController extends BaseController
$this->chatModel->setAsViewedChatUserNotifications($chat_id, $auth_user->id);
$this->chatModel->setAsUnviewedChatUserMessages($chat_id, $auth_user->id);
if($chat->chat_department_id){
if ($chat->chat_department_id) {
return view(static::$viewPath . 'messageChatPresupuesto', $this->viewData);
}else{
} else {
return view(static::$viewPath . 'messageChatInternal', $this->viewData);
}
}
@ -160,9 +161,9 @@ class ChatController extends BaseController
$auth_user = auth()->user();
$this->chatModel->setAsViewedChatUserNotifications($chat_id, $auth_user->id);
$this->chatModel->setAsUnviewedChatUserMessages($chat_id, $auth_user->id);
if($chat->chat_department_id ){
if ($chat->chat_department_id) {
return view(static::$viewPath . 'messageChatPedido', $this->viewData);
}else{
} else {
return view(static::$viewPath . 'messageChatInternal', $this->viewData);
}
}
@ -179,9 +180,9 @@ class ChatController extends BaseController
$this->chatModel->setAsViewedChatUserNotifications($chat_id, $auth_user->id);
$this->chatModel->setAsUnviewedChatUserMessages($chat_id, $auth_user->id);
if($chat->chat_department_id){
if ($chat->chat_department_id) {
return view(static::$viewPath . 'messageChatFactura', $this->viewData);
}else{
} else {
return view(static::$viewPath . 'messageChatInternal', $this->viewData);
}
}
@ -411,7 +412,7 @@ class ChatController extends BaseController
)
->where("deleted_at", null)
->whereNotIn("id", [auth()->user()->id])
->where("cliente_id",$p->cliente_id);
->where("cliente_id", $p->cliente_id);
if ($this->request->getGet("q")) {
$query->groupStart()
->orLike("users.username", $this->request->getGet("q"))
@ -433,7 +434,7 @@ class ChatController extends BaseController
)
->where("deleted_at", null)
->whereNotIn("id", [auth()->user()->id])
->where("cliente_id",$p->cliente()->id);
->where("cliente_id", $p->cliente()->id);
if ($this->request->getGet("q")) {
$query->groupStart()
->orLike("users.username", $this->request->getGet("q"))
@ -455,7 +456,7 @@ class ChatController extends BaseController
)
->where("deleted_at", null)
->whereNotIn("id", [auth()->user()->id])
->where("cliente_id",$f->cliente_id);
->where("cliente_id", $f->cliente_id);
if ($this->request->getGet("q")) {
$query->groupStart()
->orLike("users.username", $this->request->getGet("q"))
@ -619,53 +620,79 @@ class ChatController extends BaseController
public function datatable_messages()
{
$auth_user_id = auth()->user()->id;
$isAdmin = auth()->user()->inGroup('admin');
$query = $this->chatModel->getQueryDatatable($auth_user_id);
return DataTable::of($query)
->edit('created_at', fn($q) => Time::createFromFormat('Y-m-d H:i:s', $q->created_at)->format("d/m/Y H:i"))
->edit('updated_at', fn($q) => Time::createFromFormat('Y-m-d H:i:s', $q->updated_at)->format("d/m/Y H:i"))
->add("creator", fn($q) => $this->chatModel->getChatFirstUser($q->id)->userFullName)
->add("viewed", fn($q) => $this->chatModel->isMessageChatViewed($q->id, $auth_user_id))
->add("action", fn($q) => ["type" => "direct","modelId" => $q->id])
->add("action", fn($q) => [
"type" => "direct",
"modelId" => $q->id,
"isAdmin" => $isAdmin,
"lang" => [
"view_chat" => lang('Chat.view_chat'),
"view_by_alt_message" => lang('Chat.view_by_alt_message')
]
])
->toJson(true);
}
public function datatable_presupuesto_messages()
{
$auth_user_id = auth()->user()->id;
$isAdmin = auth()->user()->inGroup('admin');
$query = $this->chatModel->getQueryDatatableMessagePresupuesto($auth_user_id);
return DataTable::of($query)
->edit('created_at', fn($q) => $q->created_at ? Time::createFromFormat('Y-m-d H:i:s', $q->created_at)->format("d/m/Y H:i") : "")
->edit('updated_at', fn($q) => $q->updated_at ? Time::createFromFormat('Y-m-d H:i:s', $q->updated_at)->format("d/m/Y H:i") : "")
->add("creator", fn($q) => $this->chatModel->getChatFirstUser($q->id)->userFullName)
->add("viewed", fn($q) => $this->chatModel->isMessageChatViewed($q->id, $auth_user_id))
->add("action", fn($q) => ["type" => "presupuesto","modelId" => $q->id])
->add("action", fn($q) => ["type" => "presupuesto", "modelId" => $q->id, "isAdmin" => $isAdmin, "lang" => [
"view_chat" => lang('Chat.view_chat'),
"view_by_alt_message" => lang('Chat.view_by_alt_message')
]])
->toJson(true);
}
public function datatable_pedido_messages()
{
$auth_user_id = auth()->user()->id;
$isAdmin = auth()->user()->inGroup('admin');
$query = $this->chatModel->getQueryDatatableMessagePedido($auth_user_id);
return DataTable::of($query)
->edit('created_at', fn($q) => $q->created_at ? Time::createFromFormat('Y-m-d H:i:s', $q->created_at)->format("d/m/Y H:i") : "")
->edit('updated_at', fn($q) => $q->updated_at ? Time::createFromFormat('Y-m-d H:i:s', $q->updated_at)->format("d/m/Y H:i") : "")
->add("creator", fn($q) => $this->chatModel->getChatFirstUser($q->id)->userFullName)
->add("viewed", fn($q) => $this->chatModel->isMessageChatViewed($q->id, $auth_user_id))
->add("action", fn($q) => ["type" => "pedido","modelId" => $q->id])
->add("action", fn($q) => ["type" => "pedido", "modelId" => $q->id, "isAdmin" => $isAdmin, "lang" => [
"view_chat" => lang('Chat.view_chat'),
"view_by_alt_message" => lang('Chat.view_by_alt_message')
]])
->toJson(true);
}
public function datatable_factura_messages()
{
$auth_user_id = auth()->user()->id;
$isAdmin = auth()->user()->inGroup('admin');
$query = $this->chatModel->getQueryDatatableMessageFactura($auth_user_id);
return DataTable::of($query)
->edit('created_at', fn($q) => $q->created_at ? Time::createFromFormat('Y-m-d H:i:s', $q->created_at)->format("d/m/Y H:i") : "")
->edit('updated_at', fn($q) => $q->updated_at ? Time::createFromFormat('Y-m-d H:i:s', $q->updated_at)->format("d/m/Y H:i") : "")
->add("creator", fn($q) => $this->chatModel->getChatFirstUser($q->id)->userFullName)
->add("viewed", fn($q) => $this->chatModel->isMessageChatViewed($q->id, $auth_user_id))
->add("action", fn($q) => ["type" => "factura","modelId" => $q->id])
->add("action", fn($q) => ["type" => "factura", "modelId" => $q->id, "isAdmin" => $isAdmin, "lang" => [
"view_chat" => lang('Chat.view_chat'),
"view_by_alt_message" => lang('Chat.view_by_alt_message')
]])
->toJson(true);
}
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]);
}
public function store_new_direct_message()
{
$bodyData = $this->request->getPost();
@ -702,7 +729,7 @@ class ChatController extends BaseController
]);
}
$users = $this->chatDeparmentModel->getChatDepartmentUsers($bodyData["chat_department_id"]);
$bodyData["users"] = array_map(fn($q) => $q->id,$users);
$bodyData["users"] = array_map(fn($q) => $q->id, $users);
dd(1);
$this->chatModel->createNewDirectChat(...$bodyData);
return $this->response->setJSON(["message" => lang("Chat.new_message_ok"), "status" => true]);
@ -770,7 +797,7 @@ class ChatController extends BaseController
{
$bodyData = $this->request->getPost();
$messageService = service('messages');
$r = $messageService->createErrorMessagePresupuesto("Error",$bodyData['presupuesto_id']);
return $this->response->setJSON(["message" => "ok","data" => $r]);
$r = $messageService->createErrorMessagePresupuesto("Error", $bodyData['presupuesto_id']);
return $this->response->setJSON(["message" => "ok", "data" => $r]);
}
}

View File

@ -33,5 +33,7 @@ return [
"check_as_unviewed" => "Marcar como no leídos",
"add_notification_message" => "Envía a los usuarios añadidos una notificación con los mensajes presentes en el chat.",
"chat_title_presupuesto" => 'Presupuesto[{title,string,0}][{id}]',
"no_messages_notification" => 'No hay mensajes que revisar por el momento'
"no_messages_notification" => 'No hay mensajes que revisar por el momento',
"view_chat" => 'Ver chat',
"view_by_alt_message" => 'Muestra los usuarios que han leído o no los mensajes'
];

View File

@ -11,7 +11,7 @@ class ChatDeparmentModel extends Model
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'object';
protected $useSoftDeletes = false;
protected $useSoftDeletes = true;
protected $protectFields = true;
protected $allowedFields = [
"name",
@ -107,7 +107,7 @@ class ChatDeparmentModel extends Model
}
public function getChatDepartmentUsers(int $chat_deparment_id)
{
$result = $this->db->table('chat_departments')
$result = $this->builder()
->select(
[
"users.*"
@ -123,6 +123,8 @@ class ChatDeparmentModel extends Model
"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)
->get()->getResultObject();
return $result;
}

View File

@ -52,7 +52,9 @@ class ChatDeparmentUserModel extends Model
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();
->where("t1.user_id",$user_id)
->where("t1.deleted_at",null)
->get()->getResultObject();
}
}

View File

@ -344,9 +344,9 @@ class ChatModel extends Model
if ($row->presupuestoId) {
$row->model = $presupuestoModel->find($row->presupuestoId);
if ($auth_user->cliente_id) {
$row->uri = "/presupuestocliente/edit/" . $row->presupuestoId."#accordionChatPresupuesto";
$row->uri = "/presupuestocliente/edit/" . $row->presupuestoId . "#accordionChatPresupuesto";
} else {
$row->uri = "/presupuestoadmin/edit/" . $row->presupuestoId."#accordionChatPresupuesto";
$row->uri = "/presupuestoadmin/edit/" . $row->presupuestoId . "#accordionChatPresupuesto";
}
$row->title = $row->presupuestoId;
if ($row->chatDepartmentId) {
@ -369,7 +369,7 @@ class ChatModel extends Model
$rows_new[] = $row;
} elseif ($row->facturaId) {
$row->model = $facturaModel->find($row->facturaId);
$row->uri = "/chat/factura/" . $row->facturaId."#accordionChatFactura";
$row->uri = "/chat/factura/" . $row->facturaId . "#accordionChatFactura";
$row->avatar = "F";
if ($row->chatDepartmentId) {
$row->chatDisplay .= "[INTERNAL]";
@ -409,7 +409,7 @@ class ChatModel extends Model
if ($row->presupuestoId) {
$row->model = $presupuestoModel->find($row->presupuestoId);
if ($auth_user->cliente_id) {
$row->uri = "/presupuestocliente/edit/" . $row->presupuestoId."#accordionChatPresupuesto";
$row->uri = "/presupuestocliente/edit/" . $row->presupuestoId . "#accordionChatPresupuesto";
} else {
$row->uri = "/presupuestoadmin/edit/" . $row->presupuestoId . "#accordionChatPresupuesto";
}
@ -677,7 +677,7 @@ class ChatModel extends Model
return $q->get()->getFirstRow();
}
/**
* Check if all messages of a chat sended to an user have been viewed.
* Check if all messages of a chat sent to an user have been viewed.
*
* @param integer $chat_id
* @param integer $user_id
@ -689,7 +689,7 @@ class ChatModel extends Model
->join("chat_messages", "chat_messages.chat_id = chats.id", 'left')
->join("chat_notifications", "chat_notifications.chat_message_id = chat_messages.id", 'left')
->where("chats.id", $chat_id)
->where("chat_notifications.user_id", $user_id)
// ->where("chat_notifications.user_id", $user_id)
->where("chat_notifications.viewed", false);
$unread_messages_count = $q->countAllResults();
if ($unread_messages_count > 0) {
@ -699,24 +699,54 @@ class ChatModel extends Model
}
return $result;
}
/**
* Return users who did not read the message.
*
* @param integer $chat_id
* @return array True : All messages readed
*/
public function getUsersNotificationNotViewedFromChat(int $chat_id): array
{
$q = $this->builder()->distinct()->select(
[
"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"
]
)
->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)
->get()->getResultArray();
return $q;
}
public function getQueryDatatable(int $user_id): BaseBuilder
{
$query = $this->builder()
->select([
"chats.id",
"chats.created_at",
"chats.updated_at",
"cm.updated_at",
"chats.title",
])
->join("chat_users", "chat_users.chat_id = chats.id", "left")
->join("chat_messages cm", "chats.id = cm.chat_id", "left")
->where("chat_department_id", null)
->where("pedido_id", null)
->where("presupuesto_id", null)
->where("factura_id", null)
->where("chat_users.user_id", $user_id)
->orderBy("created_at", "DESC")
->groupBy("chats.id");
->where("cm.updated_at = (
SELECT cm2.updated_at
FROM chat_messages cm2
WHERE cm2.chat_id = chats.id
ORDER BY cm2.updated_at DESC LIMIT 1
)");
return $query;
}
@ -726,18 +756,23 @@ class ChatModel extends Model
->select([
"chats.id",
"chats.created_at",
"chats.updated_at",
"cm.updated_at",
"chats.title",
])
->join("chat_users", "chats.id = chat_users.chat_id", "left")
->join("presupuestos", "presupuestos.id = chats.presupuesto_id", 'left')
->where("presupuesto_id is NOT NULL", NULL, FALSE)
->groupBy("chats.id");
->join("chat_messages cm", "chats.id = cm.chat_id", "left")
->where("chats.presupuesto_id is NOT NULL", NULL, FALSE)
->where("cm.updated_at = (
SELECT cm2.updated_at
FROM chat_messages cm2
WHERE cm2.chat_id = chats.id
ORDER BY cm2.updated_at DESC LIMIT 1
)");
if (auth()->user()->inGroup("admin") == false) {
$query->where('presupuestos.cliente_id', auth()->user()->cliente_id)
->where("chat_department_id is NOT NULL", NULL, FALSE);
}
$query->orderBy("created_at", "DESC");
return $query;
}
public function getQueryDatatableMessagePedido(int $user_id): BaseBuilder
@ -746,19 +781,24 @@ class ChatModel extends Model
->select([
"chats.id",
"chats.created_at",
"chats.updated_at",
"cm.updated_at",
"chats.title",
])
->join("chat_users", "chat_users.chat_id = chats.id", "left")
->join("pedidos_linea", "pedidos_linea.pedido_id = chats.pedido_id", 'left')
->join("chat_messages cm", "chats.id = cm.chat_id", "left")
->join("presupuestos", "presupuestos.id = pedidos_linea.presupuesto_id", 'left')
->where("chats.pedido_id is NOT NULL", NULL, FALSE)
->groupBy("chats.id");
->where("cm.updated_at = (
SELECT cm2.updated_at
FROM chat_messages cm2
WHERE cm2.chat_id = chats.id
ORDER BY cm2.updated_at DESC LIMIT 1
)");
if (auth()->user()->inGroup("admin") == false) {
$query->where('presupuestos.cliente_id', auth()->user()->cliente_id)
->where("chat_department_id is NOT NULL", NULL, FALSE);
}
$query->orderBy("created_at", "DESC");
return $query;
}
public function getQueryDatatableMessageFactura(int $user_id): BaseBuilder
@ -767,20 +807,25 @@ class ChatModel extends Model
->select([
"chats.id",
"chats.created_at",
"chats.updated_at",
"cm.updated_at",
"chats.title",
])
->join("chat_users", "chat_users.chat_id = chats.id", "left")
->join("chat_messages cm", "chats.id = cm.chat_id", "left")
->join("facturas", "facturas.id = chats.factura_id", "left")
->where("chats.factura_id is NOT NULL", NULL, FALSE)
->groupBy("chats.id");
->where("cm.updated_at = (
SELECT cm2.updated_at
FROM chat_messages cm2
WHERE cm2.chat_id = chats.id
ORDER BY cm2.updated_at DESC LIMIT 1
)");
if (auth()->user()->inGroup("admin") == false) {
if (auth()->user()->inGroup("admin") == false) {
$query->where('facturas.cliente_id', auth()->user()->cliente_id)
->where("chat_department_id is NOT NULL", NULL, FALSE);
}
}
$query->orderBy("created_at", "DESC");
return $query;
}
public function createNewDirectChat(string $title, string $message, array $users)

View File

@ -66,6 +66,7 @@
<?= $this->endSection() ?>
<?= $this->section("additionalExternalJs") ?>
<script src="<?= site_url("themes/vuexy/vendor/libs/formvalidation/dist/js/FormValidation.js") ?>"></script>
<script src="<?= site_url('themes/vuexy/vendor/libs/popper/popper.js') ?>"></script>
<script src="<?= site_url("themes/vuexy/vendor/libs/formvalidation/dist/js/plugins/Bootstrap5.min.js") ?>"></script>
<script src="<?= site_url("themes/vuexy/vendor/libs/formvalidation/dist/js/plugins/AutoFocus.min.js") ?>"></script>
<script type="module" src="<?= site_url('assets/js/safekat/pages/configuracion/messages/index.js') ?>">

View File

@ -14,7 +14,7 @@ class MessagesDatatable {
{ data: 'title', searchable: true, sortable: true },
{ data: 'creator', searchable: false, sortable: false },
{
data: 'viewed', searchable: false, sortable: false,
data: 'viewed', searchable: false, sortable: true,
render: (d, t) => {
const iconClass = d == true ? "ti ti-sm ti-check" : "ti ti-sm ti-x"
return `<span class="${iconClass}"</span>`
@ -24,8 +24,19 @@ class MessagesDatatable {
data: 'action', searchable: false, sortable: false,
render: (d, t) => {
return `<div class="btn-group btn-group-sm">
<a href="/chat/${d.type}/${d.modelId}" class="message-edit"><i class="ti ti-eye ti-sm mx-2"></i></a>
</div>`
<a href="/chat/${d.type}/${d.modelId}" class="message-edit" title="${d.lang.view_chat}"><i class="ti ti-eye ti-sm mx-2"></i></a>
${d.isAdmin ?
`<div class="btn-group dropstart btn-group-sm">
<button data-id="${d.modelId}" type="button" title="${d.lang.view_by_alt_message}" class="btn btn-icon rounded-pill dropdown-toggle hide-arrow text-primary btn-notification-viewed" data-bs-toggle="dropdown">
<i class="icon-base ti ti-messages"></i>
</button>
<ul class="dropdown-menu" id="dropdown-viewed-${d.modelId}">
</ul>
</div>` : ""
}
</div>
`
}
}
]
@ -34,8 +45,14 @@ class MessagesDatatable {
}
init() {
this.item.on('click', '.btn-notification-viewed', (event) => {
const chatId = $(event.currentTarget).data('id')
$(`#dropdown-viewed-${chatId}`).empty()
this.handleDropUp(chatId)
})
this.datatable = this.datatableItem.DataTable({
processing: true,
order: [[1, 'desc']],
layout: {
topStart: 'pageLength',
topEnd: 'search',
@ -53,6 +70,7 @@ class MessagesDatatable {
this.datatablePresupuestoMessage = this.datatablePresupuestoMessageItem.DataTable({
processing: true,
order: [[1, 'desc']],
layout: {
topStart: 'pageLength',
topEnd: 'search',
@ -69,6 +87,7 @@ class MessagesDatatable {
});
this.datatablePedidoMessage = this.datatablePedidoMessageItem.DataTable({
processing: true,
order: [[1, 'desc']],
layout: {
topStart: 'pageLength',
topEnd: 'search',
@ -85,6 +104,7 @@ class MessagesDatatable {
});
this.datatableFacturaMessage = this.datatableFacturaMessageItem.DataTable({
processing: true,
order: [[1, 'desc']],
layout: {
topStart: 'pageLength',
topEnd: 'search',
@ -102,6 +122,46 @@ class MessagesDatatable {
}
handleDropUp(chatId) {
let ajax = new Ajax('/chat/users/notifications-unviewed/' + chatId,
null,
null,
this.handleDropUpSuccess.bind(this),
this.handleDropUpError.bind(this)
)
ajax.get();
}
handleDropUpSuccess(response) {
const chatId = response.chat_id
const users = response.data
if (users.length > 0) {
users.forEach(user => {
const viewed = (user.notification_count - user.viewed_count) == 0
if (user.userFullName || user.userName) {
$(`#dropdown-viewed-${chatId}`)
.append(`
<li >
<a class="d-flex flex-row justify-content-start align-items-center dropdown-item px-2 gap-2" href="javascript:void(0);" >
<span> ${user.userFullName ?? user.userName} </span>
<span class="badge badge-center rounded-pill text-bg-secondary">
<i class="ti ti-xs ti-${viewed ? "eye-check" : "eye-off"} "></i>
</span>
</a>
</li>
`)
}
});
} else {
$(`#dropdown-viewed-${chatId}`).append(`<li><a class="dropdown-item" href="javascript:void(0);" >Visto</a></li>`)
}
}
handleDropUpError() { }
}
export default MessagesDatatable;