Exemplo n.º 1
0
        private void BtnSalvar_Click(object sender, EventArgs e)
        {
            try
            {
                ValidateData();
            }
            catch (Exception)
            {
                return;
            }

            Customer customer = null;

            try
            {
                customer = _customerService.ReadById(int.Parse(cbCliente.Text.Split(' ')[0]));
            }
            catch (Exception)
            {
                MessageBox.Show("Cliente inválido", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (customer != null)
            {
                ProjectState projectState;

                projectState = ProjectStateExtensions.GetValueFromDescription <ProjectState>(cbEstado.Text);

                Project project = new Project()
                {
                    Name            = txtNome.Text,
                    StartDate       = dtpInicio.Value.Date,
                    EndDate         = dtpFim.Value.Date,
                    Description     = txtDescricao.Text,
                    ExpectedReveneu = (double)numReceita.Value,
                    State           = projectState,
                    ManagerId       = defaultManager.Id,
                    CustomerId      = customer.Id
                };

                if (idToUpdate != null)
                {
                    project.Id = Convert.ToInt64(idToUpdate);
                }

                List <Planning> plannings = new List <Planning>();

                foreach (DataGridViewRow row in dgvRecursos.Rows)
                {
                    long jobRoleId = Convert.ToInt64(row.Cells[0].Value);
                    if (jobRoleId != 0)
                    {
                        JobRole jobRole = _jobRoleService.ReadById(jobRoleId);

                        if (jobRole == null)
                        {
                            MessageBox.Show("Cargo não encontrado: " + row.Cells[1], "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }

                        Planning planning = new Planning()
                        {
                            JobRoleId    = jobRole.Id,
                            ProjectId    = project.Id,
                            Quantity     = Convert.ToInt32(row.Cells[3].Value),
                            PlannedHours = Convert.ToInt32(row.Cells[4].Value)
                        };

                        plannings.Add(planning);
                    }
                }
                project.Plannings = plannings;

                if (idToUpdate == null)
                {
                    _service.Create(project);
                }
                else
                {
                    if (planningsToDelete != null)
                    {
                        foreach (var planning in planningsToDelete)
                        {
                            _planningService.DeleteById(planning.Id);
                        }
                    }
                    _service.Update(project);
                }
                MessageBox.Show("Projeto salvo com sucesso", "Projetos", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                Close();
            }
            else
            {
                MessageBox.Show("Cliente não encontrado", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }