public override void OnSave()
        {
            if (Convert.ToBoolean(PriEngine.Engine.Base.Fornecedores.NumeroContribuintesRepetidos(txtNif.Text)) && !chkEdit.Checked)
            {
                MessageBox.Show("Cannot continue because the NIF for the current entity already exist.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                BasBEFornecedor supplier = new BasBEFornecedor();

                supplier.Nome            = TxtDescription.Text;
                supplier.Fornecedor      = txtName.Text;
                supplier.Morada          = txtAdress.Text;
                supplier.Morada2         = txtAdress2.Text;
                supplier.Telefone        = txtphone.Text;
                supplier.NumContribuinte = txtNif.Text;
                supplier.Moeda           = "EUR";

                // Check if entity already exist.
                // If exists them set this flag to true to tell the system to perform an update operation.
                supplier.EmModoEdicao = PriEngine.Engine.Base.Fornecedores.Existe(txtName.Text) ? true : false;

                try
                {
                    PriEngine.Engine.Base.Fornecedores.Actualiza(supplier);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Unable to save client. \n" + ex.Message);
                }
            }

            base.OnSave();
        }
        public override void OnValidateParty()
        {
            BasBEFornecedor supplier   = new BasBEFornecedor();
            StringBuilder   CamposUtil = new StringBuilder();
            List <string>   row        = new List <string>();

            listViewCDU.Clear();
            listViewCDU.View        = View.Details;
            listViewCDU.HeaderStyle = ColumnHeaderStyle.Clickable;

            if (txtName.Text.Length != 0)
            {
                try
                {
                    if (PriEngine.Engine.Base.Fornecedores.Existe(txtName.Text))
                    {
                        supplier = PriEngine.Engine.Base.Fornecedores.Edita(txtName.Text);

                        txtName.Text        = supplier.Fornecedor;
                        TxtDescription.Text = supplier.Nome;
                        txtAdress.Text      = supplier.Morada;
                        txtAdress2.Text     = supplier.Morada2;
                        txtphone.Text       = supplier.Telefone;
                        txtNif.Text         = supplier.NumContribuinte;

                        chkEdit.Checked = supplier.EmModoEdicao;

                        // load user fields
                        foreach (StdBECampo objCDU in supplier.CamposUtil)
                        {
                            ColumnHeader columnHeader = new ColumnHeader
                            {
                                Name  = objCDU.Nome,
                                Text  = objCDU.Nome,
                                Width = 100
                            };

                            listViewCDU.Columns.Add(columnHeader);

                            row.Add(Convert.ToString(objCDU.Valor));
                        }

                        ListViewItem listViewItem = new ListViewItem(row.ToArray());
                        listViewCDU.Items.Add(listViewItem);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("It was not possible to load the entity. \n" + ex.Message);
                }
            }

            base.OnValidateParty();
        }
        internal static List <String> UpdateEntity(ERPContext oERPContext, string EntityType, string Entity)
        {
            Dictionary <String, String> groupCompanies = CrossCompany.Platform.GetGroupCompanies(oERPContext);
            List <String> updatedCompanies             = new List <string>();

            //Exit if no companies where found
            if (groupCompanies.Count == 0)
            {
                return(updatedCompanies);
            }

            groupCompanies.Remove(oERPContext.BSO.Contexto.CodEmp);
            foreach (string groupCompany in groupCompanies.Keys)
            {
                ErpBS oCompany = new ErpBS();

                oCompany.AbreEmpresaTrabalho(
                    StdBE100.StdBETipos.EnumTipoPlataforma.tpEmpresarial,
                    groupCompany,
                    Properties.Settings.Default.User,
                    Properties.Settings.Default.Password);

                //get the last exercise (to create the entity accounts for each exercise)
                int lastYear = oERPContext.BSO.Contabilidade.ExerciciosCBL.DaUltimoAno();

                switch (EntityType)
                {
                case "C":
                    if (Convert.ToBoolean(oERPContext.BSO.Base.Clientes.DaValorAtributo(Entity, "CDU_EntidadeGrupo")))
                    {
                        //Entity
                        BasBECliente objNewEntity = oERPContext.BSO.Base.Clientes.Edita(Entity);
                        if (!oCompany.Base.Clientes.Existe(Entity))
                        {
                            objNewEntity.EmModoEdicao = false;
                        }
                        oCompany.Base.Clientes.Actualiza(objNewEntity);
                        updatedCompanies.Add(groupCompany);

                        //Connection to CBL
                        for (int currentYear = DateTime.Now.Year; currentYear <= lastYear; currentYear++)
                        {
                            CblBECnfTabLinhaLigCBL objNewLinhaCnfTabLigCBL = oERPContext.BSO.Contabilidade.ConfiguracaoTabCBL.Edita(CblBE100.CblBECnfTabLinhaLigCBL.TETipoTabela.GCPClientes, currentYear, "001", Entity, 1);
                            if (objNewLinhaCnfTabLigCBL != null)
                            {
                                if (oCompany.Contabilidade.ConfiguracaoTabCBL.ExisteID(objNewLinhaCnfTabLigCBL.Id))
                                {
                                    oCompany.Contabilidade.ConfiguracaoTabCBL.ActualizaValorAtributoID(objNewLinhaCnfTabLigCBL.Id, "Conta", objNewLinhaCnfTabLigCBL.Conta);
                                }
                                else
                                {
                                    objNewLinhaCnfTabLigCBL.EmModoEdicao = false;
                                    CblBECnfTabLigCBL objNewCnfTabLigCBL = oCompany.Contabilidade.ConfiguracaoTabCBL.EditaTabela(CblBECnfTabLinhaLigCBL.TETipoTabela.GCPClientes);
                                    objNewCnfTabLigCBL.PlanoExercicios.GetEdita(1).Linhas.Insere(objNewLinhaCnfTabLigCBL);
                                    oCompany.Contabilidade.ConfiguracaoTabCBL.Actualiza(objNewCnfTabLigCBL);
                                }
                            }
                        }
                    }
                    break;

                case "F":
                    if (Convert.ToBoolean(oERPContext.BSO.Base.Fornecedores.DaValorAtributo(Entity, "CDU_EntidadeGrupo")))
                    {
                        //Entity
                        BasBEFornecedor objNewEntity = oERPContext.BSO.Base.Fornecedores.Edita(Entity);
                        if (!oCompany.Base.Fornecedores.Existe(Entity))
                        {
                            objNewEntity.EmModoEdicao = false;
                        }
                        oCompany.Base.Fornecedores.Actualiza(objNewEntity);
                        updatedCompanies.Add(groupCompany);

                        //Connection to CBL
                        for (int currentYear = DateTime.Now.Year; currentYear <= lastYear; currentYear++)
                        {
                            CblBECnfTabLinhaLigCBL objNewLinhaCnfTabLigCBL = oERPContext.BSO.Contabilidade.ConfiguracaoTabCBL.Edita(CblBE100.CblBECnfTabLinhaLigCBL.TETipoTabela.GCPFornecedores, currentYear, "001", Entity, 1);
                            if (objNewLinhaCnfTabLigCBL != null)
                            {
                                if (oCompany.Contabilidade.ConfiguracaoTabCBL.ExisteID(objNewLinhaCnfTabLigCBL.Id))
                                {
                                    oCompany.Contabilidade.ConfiguracaoTabCBL.ActualizaValorAtributoID(objNewLinhaCnfTabLigCBL.Id, "Conta", objNewLinhaCnfTabLigCBL.Conta);
                                }
                                else
                                {
                                    objNewLinhaCnfTabLigCBL.EmModoEdicao = false;
                                    CblBECnfTabLigCBL objNewCnfTabLigCBL = oCompany.Contabilidade.ConfiguracaoTabCBL.EditaTabela(CblBECnfTabLinhaLigCBL.TETipoTabela.GCPFornecedores);
                                    objNewCnfTabLigCBL.PlanoExercicios.GetEdita(1).Linhas.Insere(objNewLinhaCnfTabLigCBL);
                                    oCompany.Contabilidade.ConfiguracaoTabCBL.Actualiza(objNewCnfTabLigCBL);
                                }
                            }
                        }
                    }
                    break;

                case "R":
                case "D":
                    if (Convert.ToBoolean(oERPContext.BSO.Base.OutrosTerceiros.DaValorAtributo(Entity, EntityType, "CDU_EntidadeGrupo")))
                    {
                        //Entity
                        BasBEOutroTerceiro objNewEntity = oERPContext.BSO.Base.OutrosTerceiros.Edita(Entity);
                        if (!oCompany.Base.OutrosTerceiros.Existe(Entity))
                        {
                            objNewEntity.EmModoEdicao = false;
                        }
                        oCompany.Base.OutrosTerceiros.Actualiza(objNewEntity);
                        updatedCompanies.Add(groupCompany);

                        //Connection to CBL
                        for (int currentYear = DateTime.Now.Year; currentYear <= lastYear; currentYear++)
                        {
                            CblBECnfTabLinhaLigCBL objNewLinhaCnfTabLigCBL = oERPContext.BSO.Contabilidade.ConfiguracaoTabCBL.Edita(CblBE100.CblBECnfTabLinhaLigCBL.TETipoTabela.GCPOutrosTerceiros, currentYear, "001", Entity, 1);
                            if (objNewLinhaCnfTabLigCBL != null)
                            {
                                if (oCompany.Contabilidade.ConfiguracaoTabCBL.ExisteID(objNewLinhaCnfTabLigCBL.Id))
                                {
                                    oCompany.Contabilidade.ConfiguracaoTabCBL.ActualizaValorAtributoID(objNewLinhaCnfTabLigCBL.Id, "Conta", objNewLinhaCnfTabLigCBL.Conta);
                                }
                                else
                                {
                                    objNewLinhaCnfTabLigCBL.EmModoEdicao = false;
                                    CblBECnfTabLigCBL objNewCnfTabLigCBL = oCompany.Contabilidade.ConfiguracaoTabCBL.EditaTabela(CblBECnfTabLinhaLigCBL.TETipoTabela.GCPOutrosTerceiros);
                                    objNewCnfTabLigCBL.PlanoExercicios.GetEdita(1).Linhas.Insere(objNewLinhaCnfTabLigCBL);
                                    oCompany.Contabilidade.ConfiguracaoTabCBL.Actualiza(objNewCnfTabLigCBL);
                                }
                            }
                        }
                    }
                    break;

                case "E":
                    if (Convert.ToBoolean(oERPContext.BSO.CRM.EntidadesExternas.DaValorAtributo(Entity, "CDU_EntidadeGrupo")))
                    {
                        //Entity
                        CrmBEEntidadeExterna objNewEntity = oERPContext.BSO.CRM.EntidadesExternas.Edita(Entity);
                        if (!oCompany.CRM.EntidadesExternas.Existe(Entity))
                        {
                            objNewEntity.EmModoEdicao = false;
                        }
                        oCompany.CRM.EntidadesExternas.Actualiza(objNewEntity);
                        updatedCompanies.Add(groupCompany);
                    }
                    break;

                default:
                    break;
                }

                oCompany.FechaEmpresaTrabalho();
            }

            return(updatedCompanies);
        }