Files
safekat/ci4/app/Models/Chat/ChatModel.php
2024-09-23 09:19:45 +02:00

197 lines
6.7 KiB
PHP

<?php
namespace App\Models\Chat;
use CodeIgniter\Model;
use stdClass;
class ChatModel extends Model
{
protected $table = 'chats';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
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;
}
}