using DAL; using MLL; using BLL; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Modelo; using System.Drawing.Printing; namespace IBRCAD { public partial class frm_cadproducts : Form { public string operacao; public string foto = ""; public void displayclean() { this.txtCod.Text = string.Empty; this.txt_nameProd.Text = string.Empty; this.txtdiscprod.Text = string.Empty; this.txtValorPago.Text = "0.00"; this.txtValorVenda.Text = "0.00"; this.txtQtde.Text = "0"; }// end displayclean private void loading() { this.alterabotoes(1); //combo da categoria DALLconexao cx = new DALLconexao(DadosDaConexao.StringDeConexao); BLLCategoria bll = new BLLCategoria(cx); cbCategoria.DataSource = bll.Localizar(""); cbCategoria.DisplayMember = "cat_nome"; cbCategoria.ValueMember = "cat_cod"; try { //combo da subcategoria BLLSubCategoria sbll = new BLLSubCategoria(cx); cbSubCategoria.DataSource = sbll.LocalizarPorCategoria((int)cbCategoria.SelectedValue); cbSubCategoria.DisplayMember = "scat_nome"; cbSubCategoria.ValueMember = "scat_cod"; } catch { //MessageBox.Show("Cadastre uma categoria"); } //combo und medida BLLUnidadeDeMedida ubll = new BLLUnidadeDeMedida(cx); cbUnd.DataSource = ubll.Localizar(""); cbUnd.DisplayMember = "umed_nome"; cbUnd.ValueMember = "umed_cod"; }//end loading private void additenscombox(int op) { if (op == 0) { frm_cadCat f = new frm_cadCat(); f.ShowDialog(); f.Dispose(); //combo da categoria DALLconexao cx = new DALLconexao(DadosDaConexao.StringDeConexao); BLLCategoria bll = new BLLCategoria(cx); cbCategoria.DataSource = bll.Localizar(""); cbCategoria.DisplayMember = "cat_nome"; cbCategoria.ValueMember = "cat_cod"; try { //combo da subcategoria BLLSubCategoria sbll = new BLLSubCategoria(cx); cbSubCategoria.DataSource = sbll.LocalizarPorCategoria((int)cbCategoria.SelectedValue); cbSubCategoria.DisplayMember = "scat_nome"; cbSubCategoria.ValueMember = "scat_cod"; } catch { //MessageBox.Show("Cadastre uma categoria"); } }// if of add category == 0 if (op == 1) { frm_cadsubcat f = new frm_cadsubcat(); f.ShowDialog(); f.Dispose(); //combo da categoria DALLconexao cx = new DALLconexao(DadosDaConexao.StringDeConexao); try { //combo da subcategoria BLLSubCategoria sbll = new BLLSubCategoria(cx); cbSubCategoria.DataSource = sbll.LocalizarPorCategoria((int)cbCategoria.SelectedValue); cbSubCategoria.DisplayMember = "scat_nome"; cbSubCategoria.ValueMember = "scat_cod"; } catch { //MessageBox.Show("Cadastre uma categoria"); } }// if of add subcategory == 1 if (op == 2) { frm_cadunidademedida f = new frm_cadunidademedida(); f.ShowDialog(); f.Dispose(); DALLconexao cx = new DALLconexao(DadosDaConexao.StringDeConexao); //combo und medida BLLUnidadeDeMedida ubll = new BLLUnidadeDeMedida(cx); cbUnd.DataSource = ubll.Localizar(""); cbUnd.DisplayMember = "umed_nome"; cbUnd.ValueMember = "umed_cod"; }//if of add unidademedida == 2 } public frm_cadproducts() { InitializeComponent(); } public void alterabotoes(int op) { pn_data.Enabled = false; btn_inserir.Enabled = false; btn_localizar.Enabled = false; btn_alterar.Enabled = false; btn_excluir.Enabled = false; btn_save.Enabled = false; btn_cancel.Enabled = false; if (op == 1) { btn_inserir.Enabled = true; btn_localizar.Enabled = true; } if (op == 2) { pn_data.Enabled = true; btn_save.Enabled = true; btn_save.Enabled = true; } if (op == 3) { btn_alterar.Enabled = true; btn_excluir.Enabled = true; btn_save.Enabled = true; } }//end alterar botões private void btn_inserir_Click(object sender, EventArgs e) { this.operacao = "inserir"; this.alterabotoes(2); }//btn inserir private void btn_localizar_Click(object sender, EventArgs e) { frm_consultaprodutos f = new frm_consultaprodutos(); f.ShowDialog(); if (f.codigo != 0) { DALLconexao cx = new DALLconexao(DadosDaConexao.StringDeConexao); BLLProduto bll = new BLLProduto(cx); ModeloProduto modelo = bll.CarregaModeloProduto(f.codigo); txtCod.Text = modelo.CatCod.ToString(); //colocar os dados na tela txtCod.Text = modelo.ProCod.ToString(); txtdiscprod.Text = modelo.ProDescricao; txt_nameProd.Text = modelo.ProNome; txtQtde.Text = modelo.ProQtde.ToString(); txtValorPago.Text = modelo.ProValorPago.ToString(); txtValorVenda.Text = modelo.ProValorVenda.ToString(); cbCategoria.SelectedValue = modelo.CatCod; cbSubCategoria.SelectedValue = modelo.ScatCod; cbUnd.SelectedValue = modelo.UmedCod; try { MemoryStream ms = new MemoryStream(modelo.ProFoto); pb_image.Image = Image.FromStream(ms); this.foto = "Foto Original"; } catch { } txtQtde_Leave(sender, e); txtValorPago_Leave(sender, e); txtValorVenda_Leave(sender, e); alterabotoes(3); } else { this.displayclean(); this.alterabotoes(1); } f.Dispose(); }//btn localizar private void btn_alterar_Click(object sender, EventArgs e) { this.operacao = "alterar"; this.alterabotoes(2); }//end update private void btn_excluir_Click(object sender, EventArgs e) { try { DialogResult d = MessageBox.Show("Deseja excluir o registro?", "Aviso", MessageBoxButtons.YesNo); if (d.ToString() == "Yes") { DALLconexao cx = new DALLconexao(DadosDaConexao.StringDeConexao); BLLProduto bll = new BLLProduto(cx); bll.Excluir(Convert.ToInt32(txtCod.Text)); this.displayclean(); this.alterabotoes(1); } } catch { MessageBox.Show("Impossível excluir o registro. \n O registro esta sendo utilizado em outro local."); this.alterabotoes(3); } }//end delete private void btn_addimage_Click(object sender, EventArgs e) { OpenFileDialog od = new OpenFileDialog(); od.ShowDialog(); if (!string.IsNullOrEmpty(od.FileName)) { this.foto = od.FileName; pb_image.Load(this.foto); } }//end add image private void btn_delImage_Click(object sender, EventArgs e) { this.foto = ""; pb_image.Image = null; }//end remove image panel private void btn_save_Click(object sender, EventArgs e) { try { //leitura dos dados ModeloProduto modelo = new ModeloProduto(); modelo.ProNome = txt_nameProd.Text; modelo.ProDescricao = txtdiscprod.Text; modelo.ProValorPago = Convert.ToDouble(txtValorPago.Text); modelo.ProValorVenda = Convert.ToDouble(txtValorVenda.Text); modelo.ProQtde = Convert.ToDouble(txtQtde.Text); modelo.UmedCod = Convert.ToInt32(cbUnd.SelectedValue); modelo.CatCod = Convert.ToInt32(cbCategoria.SelectedValue); modelo.ScatCod = Convert.ToInt32(cbSubCategoria.SelectedValue); //obj para gravar os dados no banco DALLconexao cx = new DALLconexao(DadosDaConexao.StringDeConexao); BLLProduto bll = new BLLProduto(cx); if (this.operacao == "inserir") { //cadastrar uma Produto modelo.CarregaImagem(this.foto); bll.Incluir(modelo); MessageBox.Show("Cadastro efetuado: Código " + modelo.ProCod.ToString()); } else { modelo.ProCod = Convert.ToInt32(txtCod.Text); //alterar um produto if (this.foto == "Foto Original") { ModeloProduto mt = bll.CarregaModeloProduto(modelo.ProCod); modelo.ProFoto = mt.ProFoto; } else { modelo.CarregaImagem(this.foto); } bll.Alterar(modelo); MessageBox.Show("Cadastro alterado"); } this.displayclean(); this.alterabotoes(1); } catch (Exception erro) { MessageBox.Show(erro.Message); } }//end btn save private void cbCategoria_SelectedIndexChanged(object sender, EventArgs e) { //combo da categoria DALLconexao cx = new DALLconexao(DadosDaConexao.StringDeConexao); try { cbSubCategoria.Text = ""; //combo da subcategoria BLLSubCategoria sbll = new BLLSubCategoria(cx); cbSubCategoria.DataSource = sbll.LocalizarPorCategoria((int)cbCategoria.SelectedValue); cbSubCategoria.DisplayMember = "scat_nome"; cbSubCategoria.ValueMember = "scat_cod"; } catch { //MessageBox.Show("Cadastre uma categoria"); } }//end cbCategoria_SelectedIndexChanged private void txtQtde_Leave(object sender, EventArgs e) { if (txtQtde.Text.Contains(",") == false) { txtQtde.Text += ",00"; } else { if (txtQtde.Text.IndexOf(",") == txtQtde.Text.Length - 1) { txtQtde.Text += "00"; } } try { Double d = Convert.ToDouble(txtQtde.Text); } catch { txtQtde.Text = "0,00"; } }//end txtQtde_Leave private void txtQtde_KeyPress(object sender, KeyPressEventArgs e) { if (!Char.IsDigit(e.KeyChar) && e.KeyChar != (char)8 && e.KeyChar != ',' && e.KeyChar != '.') { e.Handled = true; } if (e.KeyChar == ',' || e.KeyChar == '.') { if (!txtQtde.Text.Contains(",")) { e.KeyChar = ','; } else e.Handled = true; } }//end txtQtde_KeyPress private void txtValorVenda_Leave(object sender, EventArgs e) { if (txtValorVenda.Text.Contains(",") == false) { txtValorVenda.Text += ",00"; } else { if (txtValorVenda.Text.IndexOf(",") == txtValorVenda.Text.Length - 1) { txtValorVenda.Text += "00"; } } try { Double d = Convert.ToDouble(txtValorVenda.Text); } catch { txtValorVenda.Text = "0,00"; } }// end txtValorVenda_Leave private void txtValorVenda_KeyPress(object sender, KeyPressEventArgs e) { if (!Char.IsDigit(e.KeyChar) && e.KeyChar != (char)8 && e.KeyChar != ',' && e.KeyChar != '.') { e.Handled = true; } if (e.KeyChar == ',' || e.KeyChar == '.') { if (!txtValorVenda.Text.Contains(",")) { e.KeyChar = ','; } else e.Handled = true; } }// end txtValorVenda_KeyPress private void txtValorPago_Leave(object sender, EventArgs e) { if (txtValorPago.Text.Contains(",") == false) { txtValorPago.Text += ",00"; } else { if (txtValorPago.Text.IndexOf(",") == txtValorPago.Text.Length - 1) { txtValorPago.Text += "00"; } } try { Double d = Convert.ToDouble(txtValorPago.Text); } catch { txtValorPago.Text = "0,00"; } }// end txtValorPago_Leave private void txtValorPago_KeyPress(object sender, KeyPressEventArgs e) { if (!Char.IsDigit(e.KeyChar) && e.KeyChar != (char)8 && e.KeyChar != ',' && e.KeyChar != '.') { e.Handled = true; } if (e.KeyChar == ',' || e.KeyChar == '.') { if (!txtValorPago.Text.Contains(",")) { e.KeyChar = ','; } else e.Handled = true; } }// end txtValorPago_KeyPress private void frm_cadproducts_Load(object sender, EventArgs e) { this.loading(); }// end frm_cadproducts_Load private void btn_cancel_Click(object sender, EventArgs e) { this.alterabotoes(1); this.displayclean(); }//end cancel private void btn_addcat_Click(object sender, EventArgs e) { this.additenscombox(0); }//end add cat private void btn_addsubcat_Click(object sender, EventArgs e) { this.additenscombox(1); }//end add subcat private void btn_addunidmed_Click(object sender, EventArgs e) { this.additenscombox(2); }//end add unidade private void pb_exit_Click(object sender, EventArgs e) { this.Close(); }//end close } }