-
+
+
diff --git a/ci4/app/Config/Routes.php b/ci4/app/Config/Routes.php index 3f308c76..718e504b 100644 --- a/ci4/app/Config/Routes.php +++ b/ci4/app/Config/Routes.php @@ -647,6 +647,8 @@ $routes->group('chat', ['namespace' => 'App\Controllers\Chat'], function ($route $routes->get('direct/client/users/select/presupuesto/(:num)', 'ChatController::get_presupuesto_client_users/$1/$2', ['as' => 'getPresupuestoClientUsers']); $routes->get('direct/client/users/select/pedido/(:num)', 'ChatController::get_pedido_client_users/$1/$2', ['as' => 'getPedidoClientUsers']); $routes->get('direct/client/users/select/factura/(:num)', 'ChatController::get_factura_client_users/$1/$2', ['as' => 'getFacturaClientUsers']); + $routes->get('direct/client/users/select/ot/(:num)', 'ChatController::get_orden_trabajo_client_users/$1/$2', ['as' => 'getOrdenTrabajoClientUsers']); + $routes->get('direct/users/(:num)', 'ChatController::get_chat_direct_users', ['as' => 'getChatDirectUsers']); $routes->post('direct/users/(:num)', 'ChatController::store_chat_direct_users/$1', ['as' => 'storeChatDirectUsers']); @@ -663,7 +665,10 @@ $routes->group('chat', ['namespace' => 'App\Controllers\Chat'], function ($route $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/ot/(:num)/(:num)', 'ChatController::get_chat_orden_trabajo/$1/$2', ['as' => 'getChatOrdenTrabajo']); $routes->get('department/users/presupuesto/(:num)/(:num)', 'ChatController::get_chat_department_presupuesto_users/$1/$2', ['as' => 'getPresupuestoChatDepartmentUsers']); + $routes->get('department/users/ot/(:num)/(:num)', 'ChatController::get_chat_department_orden_trabajo_users/$1/$2', ['as' => 'getOrdenTrabajoChatDepartmentUsers']); + $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']); diff --git a/ci4/app/Controllers/Chat/ChatController.php b/ci4/app/Controllers/Chat/ChatController.php index ba44d28e..71ee0d2d 100644 --- a/ci4/app/Controllers/Chat/ChatController.php +++ b/ci4/app/Controllers/Chat/ChatController.php @@ -11,6 +11,7 @@ use App\Models\ChatNotification; use App\Models\ChatUser; use App\Models\Clientes\ClienteModel; 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; @@ -132,6 +133,25 @@ class ChatController extends BaseController $data["chat"] = $chat; return $this->response->setJSON($data); } + public function get_chat_orden_trabajo(int $chat_department_id, int $orden_trabajo_id) + { + + $data = [ + "department" => $this->chatDeparmentModel->find($chat_department_id), + "chat" => null, + "messages" => null, + "count" => 0, + ]; + $chat = $this->chatModel->getChatOrdenTrabajo($chat_department_id, $orden_trabajo_id); + 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; + return $this->response->setJSON($data); + } public function get_chat_direct_view($chat_id) { $chat = $this->chatModel->find($chat_id); @@ -295,6 +315,11 @@ class ChatController extends BaseController $data = $this->chatDeparmentModel->find($chat_department_id)->withUsers($factura_id, 'factura'); return $this->response->setJSON($data); } + public function get_chat_department_orden_trabajo_users(int $chat_department_id, $orden_trabajo_id) + { + $data = $this->chatDeparmentModel->find($chat_department_id)->withUsers($orden_trabajo_id, 'ot'); + return $this->response->setJSON($data); + } public function get_chat_users_internal() { $query = $this->userModel->builder()->select( @@ -357,6 +382,15 @@ class ChatController extends BaseController $clienteContactos = $cm->querySelectClienteContacto($f->cliente_id,$this->request->getGet('q')); return $this->response->setJSON($clienteContactos); } + public function get_orden_trabajo_client_users(int $orden_trabajo_id) + { + $otm = model(OrdenTrabajoModel::class); + $ot = $otm->find($orden_trabajo_id); + $cm = model(ClienteModel::class); + $cliente = $ot->pedido()->cliente(); + $clienteContactos = $cm->querySelectClienteContacto($cliente->id,$this->request->getGet('q')); + return $this->response->setJSON($clienteContactos); + } public function store_hebra(string $model) { $auth_user = auth()->user(); diff --git a/ci4/app/Database/Migrations/2025-04-11-180001_AddUserIdFechaEntregaPedido copy.php b/ci4/app/Database/Migrations/2025-04-11-180001_AddUserIdFechaEntregaPedido.php similarity index 100% rename from ci4/app/Database/Migrations/2025-04-11-180001_AddUserIdFechaEntregaPedido copy.php rename to ci4/app/Database/Migrations/2025-04-11-180001_AddUserIdFechaEntregaPedido.php diff --git a/ci4/app/Database/Migrations/2025-04-14-235200_AddFkOrdenTrabajoChatDepartment.php b/ci4/app/Database/Migrations/2025-04-14-235200_AddFkOrdenTrabajoChatDepartment.php new file mode 100644 index 00000000..3c86d242 --- /dev/null +++ b/ci4/app/Database/Migrations/2025-04-14-235200_AddFkOrdenTrabajoChatDepartment.php @@ -0,0 +1,40 @@ + [ + "type" => "INT", + "constraint" => 10, + "unsigned" => true, + "null" => true + ], + ]; + + + public function up() + { + $this->forge->addColumn("chats", $this->COLUMNS); + $this->forge->addColumn("chat_department_users", $this->COLUMNS); + $this->forge->addForeignKey('orden_trabajo_id', 'ordenes_trabajo', 'id'); + $this->forge->processIndexes('chats'); + $this->forge->addForeignKey('orden_trabajo_id', 'ordenes_trabajo', 'id'); + $this->forge->processIndexes('chat_department_users'); + + + + + } + + public function down() + { + $this->forge->dropColumn("chats", ["orden_trabajo_id"]); + $this->forge->dropColumn("chat_department_users", ["orden_trabajo_id"]); + + } +} diff --git a/ci4/app/Entities/Chat/ChatDepartmentEntity.php b/ci4/app/Entities/Chat/ChatDepartmentEntity.php index 5017c310..ba3a1049 100644 --- a/ci4/app/Entities/Chat/ChatDepartmentEntity.php +++ b/ci4/app/Entities/Chat/ChatDepartmentEntity.php @@ -36,7 +36,9 @@ class ChatDepartmentEntity extends Entity $chatDepartmentUsers = $m->where('chat_department_id',$this->attributes['id']) ->where('pedido_id',null) ->where('factura_id',null) - ->where('presupuesto_id',null)->findAll(); + ->where('presupuesto_id',null) + ->where('orden_trabajo_id',null) + ->findAll(); return $chatDepartmentUsers; } /** @@ -62,6 +64,9 @@ class ChatDepartmentEntity extends Entity case 'factura': $m->where('pedido_id',$modelFkId); break; + case 'ot': + $m->where('orden_trabajo_id',$modelFkId); + break; default: break; } diff --git a/ci4/app/Entities/Chat/ChatDepartmentUserEntity.php b/ci4/app/Entities/Chat/ChatDepartmentUserEntity.php index c261f568..047070f4 100644 --- a/ci4/app/Entities/Chat/ChatDepartmentUserEntity.php +++ b/ci4/app/Entities/Chat/ChatDepartmentUserEntity.php @@ -20,6 +20,7 @@ class ChatDepartmentUserEntity extends Entity "pedido_id" => null, "factura_id" => null, "presupuesto_id" => null, + "orden_trabajo_id" => null, ]; @@ -29,6 +30,7 @@ class ChatDepartmentUserEntity extends Entity "pedido_id" => "?integer", "factura_id" => "?integer", "presupuesto_id" => "?integer", + "orden_trabajo_id" => "?integer", ]; public function user() : ?UserEntity diff --git a/ci4/app/Entities/Chat/ChatEntity.php b/ci4/app/Entities/Chat/ChatEntity.php index 1669f3cd..2bae32de 100644 --- a/ci4/app/Entities/Chat/ChatEntity.php +++ b/ci4/app/Entities/Chat/ChatEntity.php @@ -5,11 +5,13 @@ namespace App\Entities\Chat; use App\Entities\Facturas\FacturaEntity; use App\Entities\Pedidos\PedidoEntity; use App\Entities\Presupuestos\PresupuestoEntity; +use App\Entities\Produccion\OrdenTrabajoEntity; use App\Entities\Usuarios\UserEntity; use App\Models\Chat\ChatDeparmentModel; use App\Models\Chat\ChatMessageModel; 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; @@ -25,6 +27,7 @@ class ChatEntity extends Entity "pedido_id" => null, "presupuesto_id" => null, "factura_id" => null, + "orden_trabajo_id" => null, "title" => null ]; @@ -34,6 +37,7 @@ class ChatEntity extends Entity "pedido_id" => "?integer", "presupuesto_id" => "?integer", "factura_id" => "?integer", + "orden_trabajo_id" => "?integer", "title" => "string" ]; @@ -71,6 +75,11 @@ class ChatEntity extends Entity $m = model(FacturaEntity::class); return $m->find($this->attributes['factura_id']); } + public function orden_trabajo(): ?OrdenTrabajoEntity + { + $m = model(OrdenTrabajoModel::class); + return $m->find($this->attributes['orden_trabajo_id']); + } public function messages(): ?array { $m = model(ChatMessageModel::class); @@ -100,12 +109,14 @@ class ChatEntity extends Entity $models = [ "presupuesto_id" => model(PresupuestoModel::class), "pedido_id" => model(PedidoModel::class), - "factura_id" => model(FacturaModel::class) + "factura_id" => model(FacturaModel::class), + "orden_trabajo_id" => model(OrdenTrabajoModel::class), ]; $fks = [ "presupuesto_id" => $this->attributes["presupuesto_id"], "pedido_id" => $this->attributes["pedido_id"], "factura_id" => $this->attributes["factura_id"], + "orden_trabajo_id" => $this->attributes["orden_trabajo"], ]; foreach ($fks as $key => $fk) { if ($fk) { @@ -123,6 +134,7 @@ class ChatEntity extends Entity "presupuesto" => $this->attributes["presupuesto_id"], "pedido" => $this->attributes["pedido_id"], "factura" => $this->attributes["factura_id"], + "ot" => $this->attributes["orden_trabajo_id"], ]; foreach ($fks as $key => $fk) { if ($fk) { diff --git a/ci4/app/Models/Chat/ChatDeparmentModel.php b/ci4/app/Models/Chat/ChatDeparmentModel.php index c5d81c87..efe215b8 100644 --- a/ci4/app/Models/Chat/ChatDeparmentModel.php +++ b/ci4/app/Models/Chat/ChatDeparmentModel.php @@ -136,6 +136,7 @@ class ChatDeparmentModel extends Model ->where('chat_department_users.presupuesto_id',null) ->where('chat_department_users.pedido_id',null) ->where('chat_department_users.factura_id',null) + ->where('chat_department_users.orden_trabajo_id',null) ->get(); return $result->getResultObject() ?: []; @@ -161,6 +162,13 @@ class ChatDeparmentModel extends Model ->get()->getResultObject(); return $result; } + public function getChatDeparmentOrdenTrabajoUsers(int $chat_deparment_id, int $orden_trabajo_id) + { + $result = $this->getChatDeparmentUserQuery($chat_deparment_id) + ->where('chat_department_users.orden_trabajo_id', $orden_trabajo_id) + ->get()->getResultObject(); + return $result; + } public function getDisplay(int $chat_deparment_id): string { return $this->find($chat_deparment_id)->display; diff --git a/ci4/app/Models/Chat/ChatDeparmentUserModel.php b/ci4/app/Models/Chat/ChatDeparmentUserModel.php index 1afdca85..b311e11e 100644 --- a/ci4/app/Models/Chat/ChatDeparmentUserModel.php +++ b/ci4/app/Models/Chat/ChatDeparmentUserModel.php @@ -19,7 +19,9 @@ class ChatDeparmentUserModel extends Model "user_id", "pedido_id", "factura_id", - "presupuesto_id" + "presupuesto_id", + "orden_trabajo_id", + ]; protected bool $allowEmptyInserts = false; diff --git a/ci4/app/Models/Chat/ChatModel.php b/ci4/app/Models/Chat/ChatModel.php index 88bbad19..5ac10631 100644 --- a/ci4/app/Models/Chat/ChatModel.php +++ b/ci4/app/Models/Chat/ChatModel.php @@ -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); diff --git a/ci4/app/Services/ChatService.php b/ci4/app/Services/ChatService.php index 9b0a002d..cf651ef6 100644 --- a/ci4/app/Services/ChatService.php +++ b/ci4/app/Services/ChatService.php @@ -40,6 +40,7 @@ class ChatService extends BaseService "presupuesto" => "presupuesto_id", "pedido" => "pedido_id", "factura" => "factura_id", + "ot" => "orden_trabajo_id" ]; protected array $modelClassMap; public function __construct() @@ -122,6 +123,9 @@ class ChatService extends BaseService case 'factura': $r = $this->chatModel->createChatFactura($chatDepartmentId, $modelId); break; + case 'ot': + $r = $this->chatModel->createChatOrdenTrabajo($chatDepartmentId, $modelId); + break; default: break; } diff --git a/ci4/app/Views/themes/vuexy/components/chat_internal_orden_trabajo.php b/ci4/app/Views/themes/vuexy/components/chat_internal_orden_trabajo.php new file mode 100644 index 00000000..5fd73e23 --- /dev/null +++ b/ci4/app/Views/themes/vuexy/components/chat_internal_orden_trabajo.php @@ -0,0 +1,41 @@ +