LevelOS-Core/UI/Dashboards/Cadastros/SituacoesOSPanel.cs

186 lines
7.8 KiB
C#

using CPM;
using MLL;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace UI.Dashboards.Cadastro
{
public class SituacoesOSPanel : FormularioModelo
{
// Controles marcados como anuláveis para sanar avisos do compilador modernos (NRT)
private LV_TEXTBOX1? txtId, txtEmpresaId, txtDescricao, txtTipo, txtCorFundo, txtCorFonte, txtPlanoContasId, txtOrdem, txtCriadoEm, txtAtualizadoEm;
private CheckBox? chkConsideraAberta, chkConsideraFechada, chkMarcaComoPronto, chkAtivo;
public SituacoesOSPanel()
{
this.Titulo = "SITUAÇÕES E STATUS DE ORDENS DE SERVIÇO (OS)";
MontarInterfaceCompleta();
}
private void MontarInterfaceCompleta()
{
// --- SEÇÃO 1: IDENTIFICAÇÃO E ESPECIFICAÇÃO ---
content.Controls.Add(CreateSectionHeader("Identificação do Status da OS", 20));
txtId = AddInput(content, "ID SITUAÇÃO", 20, 55, 100, 28, true);
txtEmpresaId = AddInput(content, "ID EMPRESA", 135, 55, 100, 28);
txtDescricao = AddInput(content, "DESCRIÇÃO DO STATUS (EX: EM ANDAMENTO)", 250, 55, 350, 28);
txtTipo = AddInput(content, "TIPO", 615, 55, 140, 28);
chkAtivo = new CheckBox
{
Text = "Status Ativo",
Location = new Point(770, 58),
Size = new Size(110, 24),
Font = new Font("Segoe UI", 10f, FontStyle.Bold),
ForeColor = Color.FromArgb(45, 45, 45),
Checked = true
};
content.Controls.Add(chkAtivo);
// --- SEÇÃO 2: REGRAS DE COMPORTAMENTO ---
content.Controls.Add(CreateSectionHeader("Comportamento e Regras de Negócio no Fluxo de OS", 115));
chkConsideraAberta = new CheckBox
{
Text = "Considerar OS Aberta (Pendente / Em Execução)",
Location = new Point(25, 150),
Size = new Size(350, 24),
Font = new Font("Segoe UI", 9.5f)
};
chkConsideraFechada = new CheckBox
{
Text = "Considerar OS Fechada (Finalizada / Encerrada)",
Location = new Point(400, 150),
Size = new Size(350, 24),
Font = new Font("Segoe UI", 9.5f)
};
chkMarcaComoPronto = new CheckBox
{
Text = "Marcar OS como Pronta (Aguardando Retirada / Entrega)",
Location = new Point(25, 185),
Size = new Size(450, 24),
Font = new Font("Segoe UI", 9.5f)
};
content.Controls.AddRange(new Control[] { chkConsideraAberta, chkConsideraFechada, chkMarcaComoPronto });
// --- SEÇÃO 3: INTEGRALIZAÇÃO, CORES E TRIAGEM ---
content.Controls.Add(CreateSectionHeader("Integração Financeira, Ordenação e Identidade Visual", 235));
txtPlanoContasId = AddInput(content, "ID PLANO DE CONTAS", 20, 270, 150, 28);
txtOrdem = AddInput(content, "ORDEM DE EXIBIÇÃO", 185, 270, 140, 28);
txtCorFundo = AddInput(content, "COR DO FUNDO (HEX)", 340, 270, 180, 28);
txtCorFonte = AddInput(content, "COR DA FONTE (HEX)", 535, 270, 180, 28);
// --- SEÇÃO 4: AUDITORIA ---
content.Controls.Add(CreateSectionHeader("Histórico do Registro", 335));
txtCriadoEm = AddInput(content, "DATA DE CRIAÇÃO", 20, 370, 180, 28, true);
txtAtualizadoEm = AddInput(content, "ÚLTIMA ATUALIZAÇÃO", 215, 370, 180, 28, true);
}
// --- MÉTODOS OBRIGATÓRIOS DO FORMULÁRIO MODELO ---
protected override void OnNovo()
{
// Limpa caixas de texto editáveis
foreach (Control c in content.Controls)
{
if (c is LV_TEXTBOX1 txt && !txt.ReadOnly)
{
txt.Text = string.Empty;
}
}
// Reseta CheckBoxes
if (chkConsideraAberta != null) chkConsideraAberta.Checked = false;
if (chkConsideraFechada != null) chkConsideraFechada.Checked = false;
if (chkMarcaComoPronto != null) chkMarcaComoPronto.Checked = false;
if (chkAtivo != null) chkAtivo.Checked = true;
// Valores padrão iniciais
if (txtId != null) txtId.Text = "0";
if (txtEmpresaId != null) txtEmpresaId.Text = "1";
if (txtOrdem != null) txtOrdem.Text = "1";
if (txtCorFundo != null) txtCorFundo.Text = "#FFFFFF";
if (txtCorFonte != null) txtCorFonte.Text = "#000000";
if (txtCriadoEm != null) txtCriadoEm.Text = DateTime.Now.ToString("g");
if (txtAtualizadoEm != null) txtAtualizadoEm.Text = DateTime.Now.ToString("g");
txtDescricao?.Focus();
}
protected override void OnSalvar()
{
if (string.IsNullOrWhiteSpace(txtDescricao?.Text))
{
MessageBox.Show("A descrição do status da OS é obrigatória.", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtDescricao?.Focus();
return;
}
// Tratamento resiliente de inteiros
int idStatus = string.IsNullOrEmpty(txtId?.Text) ? 0 : Convert.ToInt32(txtId.Text);
int idEmpresa = string.IsNullOrEmpty(txtEmpresaId?.Text) ? 0 : Convert.ToInt32(txtEmpresaId.Text);
// Tratamento seguro de inteiros anuláveis (int?)
int? planoContasId = null;
if (!string.IsNullOrWhiteSpace(txtPlanoContasId?.Text) && int.TryParse(txtPlanoContasId.Text, out int pcParsed))
planoContasId = pcParsed;
int? ordemExibicao = null;
if (!string.IsNullOrWhiteSpace(txtOrdem?.Text) && int.TryParse(txtOrdem.Text, out int ordParsed))
ordemExibicao = ordParsed;
// Mapeamento absoluto de todas as 14 propriedades da classe ModeloSituacoesOS
var situacaoOS = new ModeloSituacoesOS
{
Id = idStatus,
EmpresaId = idEmpresa,
Descricao = txtDescricao.Text,
Tipo = txtTipo?.Text ?? string.Empty,
ConsideraAberta = chkConsideraAberta?.Checked ?? false,
ConsideraFechada = chkConsideraFechada?.Checked ?? false,
MarcaComoPronto = chkMarcaComoPronto?.Checked ?? false,
CorFundo = txtCorFundo?.Text ?? string.Empty,
CorFonte = txtCorFonte?.Text ?? string.Empty,
PlanoContasId = planoContasId,
Ordem = ordemExibicao,
Ativo = chkAtivo?.Checked ?? false,
CriadoEm = string.IsNullOrEmpty(txtCriadoEm?.Text) ? DateTime.Now : Convert.ToDateTime(txtCriadoEm.Text),
AtualizadoEm = DateTime.Now // Atualiza o carimbo de data
};
// Envio do objeto estruturado para a persistência em banco de dados SQL Server via BLL/DAL
MessageBox.Show($"Status de OS '{situacaoOS.Descricao}' salvo com sucesso!", "LevelOS", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
protected override void OnAlterar()
{
txtDescricao?.Focus();
}
protected override void OnExcluir()
{
if (MessageBox.Show("A exclusão deste status pode invalidar o histórico de fluxo das ordens de serviço associadas. Deseja continuar?", "LevelOS", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
OnNovo();
}
}
protected override void OnLocalizar()
{
// Aciona o grid interno para localização de status de OS existentes
}
protected override void OnCancelar()
{
OnNovo();
}
}
}