mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
360 lines
13 KiB
PHP
360 lines
13 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Chat;
|
|
|
|
use App\Models\Facturas\FacturaModel;
|
|
use App\Models\Pedidos\PedidoModel;
|
|
use App\Models\Presupuestos\PresupuestoModel;
|
|
use App\Models\Usuarios\UserModel;
|
|
use CodeIgniter\Model;
|
|
use stdClass;
|
|
|
|
class ChatModel extends Model
|
|
{
|
|
protected $table = 'chats';
|
|
protected $primaryKey = 'id';
|
|
protected $useAutoIncrement = true;
|
|
protected $returnType = 'object';
|
|
protected $useSoftDeletes = false;
|
|
protected $protectFields = true;
|
|
protected $allowedFields = [
|
|
"pedido_id",
|
|
"chat_department_id",
|
|
"presupuesto_id",
|
|
"factura_id",
|
|
];
|
|
|
|
protected bool $allowEmptyInserts = false;
|
|
protected bool $updateOnlyChanged = true;
|
|
|
|
protected array $casts = [];
|
|
protected array $castHandlers = [];
|
|
|
|
// Dates
|
|
protected $useTimestamps = false;
|
|
protected $dateFormat = 'datetime';
|
|
protected $createdField = 'created_at';
|
|
protected $updatedField = 'updated_at';
|
|
protected $deletedField = 'deleted_at';
|
|
|
|
// Validation
|
|
protected $validationRules = [];
|
|
protected $validationMessages = [];
|
|
protected $skipValidation = false;
|
|
protected $cleanValidationRules = true;
|
|
|
|
// Callbacks
|
|
protected $allowCallbacks = true;
|
|
protected $beforeInsert = [];
|
|
protected $afterInsert = [];
|
|
protected $beforeUpdate = [];
|
|
protected $afterUpdate = [];
|
|
protected $beforeFind = [];
|
|
protected $afterFind = [];
|
|
protected $beforeDelete = [];
|
|
protected $afterDelete = [];
|
|
|
|
|
|
public function getChat(int $chat_id): array
|
|
{
|
|
return $this->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")
|
|
->whereIn("pedidos.id",$pedidos)
|
|
->get()->getResultObject();
|
|
$chatMessageModel = model(ChatMessageModel::class);
|
|
$count = 0;
|
|
foreach ($results as $row) {
|
|
$messages = $chatMessageModel->get_chat_messages($row->chatId);
|
|
foreach ($messages as $key => $message) {
|
|
if($message->sender_id != auth()->user()->id && $message->viewed == false){
|
|
$count++;
|
|
}
|
|
}
|
|
$row->unreadMessages=$count;
|
|
}
|
|
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")
|
|
->whereIn("facturas.id",$facturas)
|
|
->get()->getResultObject();
|
|
$chatMessageModel = model(ChatMessageModel::class);
|
|
$count = 0;
|
|
foreach ($results as $row) {
|
|
$messages = $chatMessageModel->get_chat_messages($row->chatId);
|
|
foreach ($messages as $key => $message) {
|
|
if($message->sender_id != auth()->user()->id && $message->viewed == false){
|
|
$count++;
|
|
}
|
|
}
|
|
$row->unreadMessages=$count;
|
|
}
|
|
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")
|
|
->whereIn("presupuestos.id",$presupuestos)
|
|
->get()->getResultObject();
|
|
$chatMessageModel = model(ChatMessageModel::class);
|
|
$count = 0;
|
|
foreach ($results as $row) {
|
|
$messages = $chatMessageModel->get_chat_messages($row->chatId);
|
|
foreach ($messages as $key => $message ) {
|
|
if($message->sender_id != auth()->user()->id && $message->viewed == false){
|
|
$count++;
|
|
}
|
|
}
|
|
$row->unreadMessages=$count;
|
|
}
|
|
|
|
return $results;
|
|
}
|
|
public function getChatDepartmentNotifications()
|
|
{
|
|
$chatMessageModel = model(ChatMessageModel::class);
|
|
$presupuestoModel = model(PresupuestoModel::class);
|
|
$facturaModel = model(FacturaModel::class);
|
|
$pedidoModel = model(PedidoModel::class);
|
|
|
|
$q = $this->db->table("chats")
|
|
->select([
|
|
"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.chat_department_id as chatDepartmentId",
|
|
"chat_departments.display as chatDisplay",
|
|
])
|
|
->join("chat_departments","chat_departments.id = chats.chat_department_id","left")
|
|
->join("chat_department_users","chat_department_users.chat_department_id = chats.chat_department_id","left")
|
|
->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;
|
|
foreach ($messages as $m) {
|
|
if($m->viewed == false && $m->sender_id != auth()->user()->id)
|
|
$count++;
|
|
}
|
|
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 = $count;
|
|
}
|
|
elseif($row->pedidoId){
|
|
$row->model = $pedidoModel->find($row->pedidoId);
|
|
$row->uri = "/pedidos/edit/".$row->pedidoId;
|
|
$row->title = $row->pedidoId;
|
|
$row->avatar = "P";
|
|
$row->unreadMessages = $count;
|
|
|
|
}
|
|
elseif($row->facturaId){
|
|
$row->model = $facturaModel->find($row->facturaId);
|
|
$row->uri = "/facturas/edit/".$row->facturaId;
|
|
$row->avatar = "F";
|
|
$row->title = $row->facturaId;
|
|
$row->unreadMessages = $count;
|
|
}
|
|
}
|
|
return $rows;
|
|
|
|
}
|
|
public function getChatInternalNotifications()
|
|
{
|
|
$chatMessageModel = model(ChatMessageModel::class);
|
|
$userModel = model(UserModel::class);
|
|
$internalMessages = $chatMessageModel->builder()
|
|
->select([
|
|
"chat_id as chatId",
|
|
"sender_id",
|
|
"COUNT(viewed) as unreadMessages",
|
|
])
|
|
->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 = "#";
|
|
}
|
|
}
|
|
return $internalMessages;
|
|
}
|
|
}
|